Skip to content

Commit e077918

Browse files
committed
Remove support for Bash 3.
This patch removes workarounds that were necessary to support old-style Bash 3 command completion.
1 parent 084fea0 commit e077918

File tree

3 files changed

+126
-158
lines changed

3 files changed

+126
-158
lines changed

bash-completion.el

Lines changed: 60 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ will be started to do completion."
149149
150150
This should be either an absolute path to the BASH executable or
151151
the name of the bash command if it is on Emacs' PATH. This should
152-
point to a recent version of BASH, 3 or 4, with support for
152+
point to a recent version of BASH 4 or 5, with support for
153153
command-line completion.
154154
155155
This variable is only used when creating separate processes for
@@ -354,74 +354,62 @@ returned."
354354

355355
(defun bash-completion--setup-bash-common (process)
356356
"Setup PROCESS to be ready for completion."
357-
(let (bash-major-version)
358-
(bash-completion-send "complete -p" process)
359-
(process-put process 'complete-p
360-
(bash-completion-build-alist (bash-completion--get-buffer process)))
361-
(bash-completion-send "echo -n ${BASH_VERSINFO[0]}" process)
362-
(setq bash-major-version
363-
(with-current-buffer (bash-completion--get-buffer process)
364-
(string-to-number (buffer-substring-no-properties
365-
(point-min) (point-max)))))
366-
(bash-completion-send
367-
(concat "function __emacs_complete_wrapper {"
368-
(if (>= bash-major-version 4)
369-
" COMP_TYPE=9; COMP_KEY=9; _EMACS_COMPOPT=\"\";"
370-
"")
371-
" eval $__EMACS_COMPLETE_WRAPPER;"
372-
" n=$?;"
373-
" if [[ $n = 124 ]]; then"
374-
(bash-completion--side-channel-data "wrapped-status" "124")
375-
" return 1; "
376-
" fi; "
377-
(when (>= bash-major-version 4)
378-
(concat
379-
" if [[ -n \"${_EMACS_COMPOPT}\" ]]; then"
380-
(bash-completion--side-channel-data "compopt" "${_EMACS_COMPOPT}")
381-
" fi;"))
382-
" return $n;"
383-
"}")
384-
process)
385-
(if (>= bash-major-version 4)
386-
(bash-completion-send
387-
(concat
388-
"function compopt {"
389-
" command compopt \"$@\" 2>/dev/null;"
390-
" ret=$?; "
391-
" if [[ $ret == 1 && \"$*\" = *\"-o nospace\"* ]]; then"
392-
" _EMACS_COMPOPT='-o nospace';"
393-
" return 0;"
394-
" fi;"
395-
" if [[ $ret == 1 && \"$*\" = *\"+o nospace\"* ]]; then"
396-
" _EMACS_COMPOPT='+o nospace';"
397-
" return 0;"
398-
" fi;"
399-
" return $ret; "
400-
"}")
401-
process))
402-
403-
;; some bash completion functions use quote_readline
404-
;; to double-quote strings - which compgen understands
405-
;; but only in some environment. disable this dreadful
406-
;; business to get a saner way of handling spaces.
407-
;; Noticed in bash_completion v1.872.
408-
(bash-completion-send "function quote_readline { echo \"$1\"; }" process)
409-
410-
(bash-completion-send "echo -n ${COMP_WORDBREAKS}" process)
411-
(process-put process 'wordbreaks
412-
(with-current-buffer (bash-completion--get-buffer process)
413-
(buffer-substring-no-properties
414-
(point-min) (point-max))))
415-
(process-put process 'bash-major-version bash-major-version)
416-
417-
(bash-completion-send "bind -v 2>/dev/null" process)
418-
(process-put process 'completion-ignore-case
419-
(with-current-buffer (bash-completion--get-buffer process)
420-
(save-excursion
421-
(goto-char (point-min))
422-
(and (search-forward "completion-ignore-case on" nil 'noerror) t))))
423-
424-
(process-put process 'setup-done t)))
357+
(bash-completion-send "complete -p" process)
358+
(process-put process 'complete-p
359+
(bash-completion-build-alist (bash-completion--get-buffer process)))
360+
(bash-completion-send
361+
(concat "function __emacs_complete_wrapper {"
362+
" COMP_TYPE=9; COMP_KEY=9; _EMACS_COMPOPT=\"\";"
363+
" eval $__EMACS_COMPLETE_WRAPPER;"
364+
" n=$?;"
365+
" if [[ $n = 124 ]]; then"
366+
(bash-completion--side-channel-data "wrapped-status" "124")
367+
" return 1; "
368+
" fi; "
369+
" if [[ -n \"${_EMACS_COMPOPT}\" ]]; then"
370+
(bash-completion--side-channel-data "compopt" "${_EMACS_COMPOPT}")
371+
" fi;"
372+
" return $n;"
373+
"}")
374+
process)
375+
(bash-completion-send
376+
(concat
377+
"function compopt {"
378+
" command compopt \"$@\" 2>/dev/null;"
379+
" ret=$?; "
380+
" if [[ $ret == 1 && \"$*\" = *\"-o nospace\"* ]]; then"
381+
" _EMACS_COMPOPT='-o nospace';"
382+
" return 0;"
383+
" fi;"
384+
" if [[ $ret == 1 && \"$*\" = *\"+o nospace\"* ]]; then"
385+
" _EMACS_COMPOPT='+o nospace';"
386+
" return 0;"
387+
" fi;"
388+
" return $ret; "
389+
"}")
390+
process)
391+
392+
;; some bash completion functions use quote_readline
393+
;; to double-quote strings - which compgen understands
394+
;; but only in some environment. disable this dreadful
395+
;; business to get a saner way of handling spaces.
396+
;; Noticed in bash_completion v1.872.
397+
(bash-completion-send "function quote_readline { echo \"$1\"; }" process)
398+
399+
(bash-completion-send "echo -n ${COMP_WORDBREAKS}" process)
400+
(process-put process 'wordbreaks
401+
(with-current-buffer (bash-completion--get-buffer process)
402+
(buffer-substring-no-properties
403+
(point-min) (point-max))))
404+
405+
(bash-completion-send "bind -v 2>/dev/null" process)
406+
(process-put process 'completion-ignore-case
407+
(with-current-buffer (bash-completion--get-buffer process)
408+
(save-excursion
409+
(goto-char (point-min))
410+
(and (search-forward "completion-ignore-case on" nil 'noerror) t))))
411+
412+
(process-put process 'setup-done t))
425413

426414
;;; Inline functions
427415

@@ -544,8 +532,7 @@ Returns (list stub-start stub-end completions) with
544532
(setq process (bash-completion--get-process)))
545533
(let* ((comp (bash-completion--parse
546534
comp-start comp-pos
547-
(process-get process 'wordbreaks)
548-
(process-get process 'bash-major-version)))
535+
(process-get process 'wordbreaks)))
549536
(stub-start (bash-completion--stub-start comp)))
550537

551538
(bash-completion--customize comp process)
@@ -597,16 +584,15 @@ functions adds single quotes around it and return the result."
597584
(replace-regexp-in-string "'" "'\\''" word nil t)
598585
"'"))))
599586

600-
(defun bash-completion--parse (comp-start comp-pos wordbreaks bash-major-version)
587+
(defun bash-completion--parse (comp-start comp-pos wordbreaks)
601588
"Process a command from COMP-START to COMP-POS.
602589
603590
WORDBREAK is the value of COMP_WORDBREAKS to use for this completion,
604591
usually taken from the current process.
605592
606593
Returns a completion struct."
607594
(let* ((all-tokens (bash-completion-tokenize
608-
comp-start comp-pos (if (>= bash-major-version 4)
609-
wordbreaks "")))
595+
comp-start comp-pos wordbreaks))
610596
(line-tokens (bash-completion-parse-current-command all-tokens))
611597
(first-token (car line-tokens))
612598
(last-token (car (last line-tokens)))
@@ -629,9 +615,6 @@ Returns a completion struct."
629615
parsed-stub ""
630616
words (append words '(""))
631617
rebuilt-line (buffer-substring-no-properties start comp-pos))
632-
(if (< bash-major-version 4)
633-
(setq last-token (car (last (bash-completion-tokenize
634-
start comp-pos wordbreaks)))))
635618
(setq stub-start (car (bash-completion-tokenize-get-range last-token))
636619
parsed-stub (bash-completion-tokenize-get-str last-token)
637620
unparsed-stub (buffer-substring-no-properties stub-start comp-pos)
@@ -1588,7 +1571,6 @@ Return the status code of the command, as a number."
15881571
(princ "Please retry\n\n")))
15891572

15901573
(bash-completion--debug-print-info 'use-separate-processes)
1591-
(bash-completion--debug-print-procinfo 'bash-major-version)
15921574
(bash-completion--debug-print 'emacs-version emacs-version)
15931575
(bash-completion--debug-print-procinfo 'completion-ignore-case)
15941576
(bash-completion--debug-print-info 'context)

test/bash-completion-integration-test.el

Lines changed: 44 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,6 @@
105105
(kill-process (get-buffer-process shell-buffer)))
106106
(kill-buffer shell-buffer)))))
107107

108-
(defun bash-completion_test-bash-major-version ()
109-
"Return the major version of the bash process."
110-
(process-get (bash-completion--get-process) 'bash-major-version))
111-
112108
(defun bash-completion_test-complete (complete-me)
113109
"Complete COMPLETE-ME and returns the resulting string."
114110
(goto-char (point-max))
@@ -347,11 +343,10 @@ across Emacs version."
347343
"}\n"
348344
"complete -D -F _default\n")
349345
use-separate-process
350-
(when (>= (bash-completion_test-bash-major-version) 4)
351-
(should (equal "dosomething dummy "
352-
(bash-completion_test-complete "dosomething du")))
353-
(should (equal "dosomethingelse du"
354-
(bash-completion_test-complete "dosomethingelse du"))))))
346+
(should (equal "dosomething dummy "
347+
(bash-completion_test-complete "dosomething du")))
348+
(should (equal "dosomethingelse du"
349+
(bash-completion_test-complete "dosomethingelse du")))))
355350

356351
(ert-deftest bash-completion-integration-bash-4-compopt ()
357352
(bash-completion_test-with-shell-harness
@@ -377,26 +372,25 @@ across Emacs version."
377372
"complete -F _sometimes_nospace sometimes_nospace\n"
378373
"complete -F _sometimes_not_nospace -o nospace sometimes_not_nospace\n")
379374
t ; use-separate-process
380-
(when (>= (bash-completion_test-bash-major-version) 4)
375+
(should (equal
376+
"sometimes_nospace dummy "
377+
(bash-completion_test-complete "sometimes_nospace du")))
378+
(should (equal
379+
"sometimes_nospace dummyo"
380+
(bash-completion_test-complete "sometimes_nospace dum")))
381+
(should (equal
382+
"sometimes_not_nospace dummy"
383+
(bash-completion_test-complete "sometimes_not_nospace du")))
384+
(should (equal
385+
"sometimes_not_nospace dummyo "
386+
(bash-completion_test-complete "sometimes_not_nospace dum")))
387+
(let ((bash-completion-nospace t)) ;; never nospace
381388
(should (equal
382-
"sometimes_nospace dummy "
389+
"sometimes_nospace dummy"
383390
(bash-completion_test-complete "sometimes_nospace du")))
384391
(should (equal
385-
"sometimes_nospace dummyo"
386-
(bash-completion_test-complete "sometimes_nospace dum")))
387-
(should (equal
388-
"sometimes_not_nospace dummy"
389-
(bash-completion_test-complete "sometimes_not_nospace du")))
390-
(should (equal
391-
"sometimes_not_nospace dummyo "
392-
(bash-completion_test-complete "sometimes_not_nospace dum")))
393-
(let ((bash-completion-nospace t)) ;; never nospace
394-
(should (equal
395-
"sometimes_nospace dummy"
396-
(bash-completion_test-complete "sometimes_nospace du")))
397-
(should (equal
398-
"sometimes_not_nospace dummyo"
399-
(bash-completion_test-complete "sometimes_not_nospace dum")))))))
392+
"sometimes_not_nospace dummyo"
393+
(bash-completion_test-complete "sometimes_not_nospace dum"))))))
400394

401395
(ert-deftest bash-completion-integration-bash-4-substring-completion ()
402396
(bash-completion_test-with-shell-harness
@@ -460,31 +454,30 @@ across Emacs version."
460454
(make-directory "libs")
461455
(make-directory "Library")
462456
(bash-completion_test-with-shell
463-
(when (>= (bash-completion_test-bash-major-version) 4)
464-
;; Case insensitive completion is done by compgen which, under
465-
;; bash 4, respects the case sensitivity settings set in
466-
;; .inputrc.
467-
(should (equal "ls some/" (bash-completion_test-complete "ls so")))
468-
(should (equal "ls some/" (bash-completion_test-complete "ls So")))
469-
(should (equal "ls Uppercase/" (bash-completion_test-complete "ls Up")))
470-
(should (equal "ls Uppercase/" (bash-completion_test-complete "ls up")))
471-
472-
(should (equal "ls libs/" (bash-completion_test-complete "ls li")))
473-
(should (equal "ls libs/" (bash-completion_test-complete "ls Li")))
474-
475-
(should (equal "ls Another\\ Uppercase/" (bash-completion_test-complete "ls Ano")))
476-
(should (equal "ls Another\\ Uppercase/" (bash-completion_test-complete "ls ano")))
477-
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls \"Ano")))
478-
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls \"ano")))
479-
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls 'Ano")))
480-
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls 'ano")))
481-
482-
;; When doing case-insensitive search, bash-completion.el cannot
483-
;; keep the exact same quotes, so it just puts the quote, if
484-
;; any, at the beginning, just after the tilde part.
485-
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls ano\"t")))
486-
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls ano't")))
487-
(should (equal "ls ~/\"Another Uppercase/" (bash-completion_test-complete "ls ~/ano\"t")))))))
457+
;; Case insensitive completion is done by compgen which, under
458+
;; bash 4, respects the case sensitivity settings set in
459+
;; .inputrc.
460+
(should (equal "ls some/" (bash-completion_test-complete "ls so")))
461+
(should (equal "ls some/" (bash-completion_test-complete "ls So")))
462+
(should (equal "ls Uppercase/" (bash-completion_test-complete "ls Up")))
463+
(should (equal "ls Uppercase/" (bash-completion_test-complete "ls up")))
464+
465+
(should (equal "ls libs/" (bash-completion_test-complete "ls li")))
466+
(should (equal "ls libs/" (bash-completion_test-complete "ls Li")))
467+
468+
(should (equal "ls Another\\ Uppercase/" (bash-completion_test-complete "ls Ano")))
469+
(should (equal "ls Another\\ Uppercase/" (bash-completion_test-complete "ls ano")))
470+
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls \"Ano")))
471+
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls \"ano")))
472+
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls 'Ano")))
473+
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls 'ano")))
474+
475+
;; When doing case-insensitive search, bash-completion.el cannot
476+
;; keep the exact same quotes, so it just puts the quote, if
477+
;; any, at the beginning, just after the tilde part.
478+
(should (equal "ls \"Another Uppercase/" (bash-completion_test-complete "ls ano\"t")))
479+
(should (equal "ls 'Another Uppercase/" (bash-completion_test-complete "ls ano't")))
480+
(should (equal "ls ~/\"Another Uppercase/" (bash-completion_test-complete "ls ~/ano\"t"))))))
488481

489482
(ert-deftest bash-completion-integration-case-sensitive-test ()
490483
(bash-completion_test-harness

0 commit comments

Comments
 (0)