emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/bash-completion 482383dcd7 021/313: handle slash for direc


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 482383dcd7 021/313: handle slash for directory, usable completion
Date: Sat, 3 Dec 2022 10:59:11 -0500 (EST)

branch: elpa/bash-completion
commit 482383dcd79a1527a0ef10d72c8a3ed017a84846
Author: Stephane Zermatten <szermatt@gmx.net>
Commit: Stephane Zermatten <szermatt@gmx.net>

    handle slash for directory, usable completion
---
 bash-completion.el      | 47 ++++++++++++++++++++++++++++-------------------
 bash-completion_test.el | 44 ++++++++++++++++++++++++++++++++++++--------
 2 files changed, 64 insertions(+), 27 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index dc9e40a177..43fe828745 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -40,7 +40,7 @@ See `bash-completion-add-to-alist'.
   "Bash completion function for `comint-complete-dynamic-functions'.
 
 Call bash to do the completion."
-  (when (window-minibuffer-p)
+  (when (not (window-minibuffer-p))
     (message "Bash completion..."))
   (let* ( (pos (point))
          (start (bash-completion-line-beginning-position))
@@ -49,7 +49,10 @@ Call bash to do the completion."
          (wordsplit)
          (cword)
          (words)
-         (stub) )
+         (stub)
+         ;; Override configuration for comint-dynamic-simple-complete.
+         ;; Bash adds a space suffix automatically.
+         (comint-completion-addsuffix nil) )
     (save-excursion
       (setq wordsplit (bash-completion-split start end pos))
       (setq cword (car wordsplit))
@@ -57,15 +60,14 @@ Call bash to do the completion."
       (setq stub (nth cword words)))
     (comint-dynamic-simple-complete
      stub
-     (bash-completion-comm default-directory
-                        line (- pos start) words cword))))
+     (bash-completion-comm line (- pos start) words cword))))
 
 (defun bash-completion-line-beginning-position (&optional start)
   (save-excursion
     (let ((start (or start (comint-line-beginning-position)))
          (end (line-end-position)))
-      (goto-char start)
-      (if (search-forward-regexp "\\(;\\|\\(&&\\)\\|\\(||\\)\\)[ \t\n]" end t)
+      (goto-char end)
+      (if (search-backward-regexp "\\(;\\|\\(&&\\)\\|\\(||\\)\\|\\(=[^ 
\t]*\\)\\)[ \t\n]" start t)
          (match-end 0)
        start))))
 
@@ -144,22 +146,25 @@ at POS, the current word: ( (word1 word2 ...) . wordnum )"
       (concat "^ \t\n\r" (char-to-string quote))
     "^ \t\n\r'\""))
 
-(defun bash-completion-comm (dir line pos words cword)
-  "Set DIR, LINE, POS, WORDS and CWORD, call bash completion, return the 
result.
+(defun bash-completion-comm (line pos words cword)
+  "Set LINE, POS, WORDS and CWORD, call bash completion, return the result.
 
 This function starts a separate bash process if necessary, sets up the
 completion environment (COMP_LINE, COMP_POINT, COMP_WORDS, COMP_CWORD) and
 calls compgen.
 
 The result is a list of candidates, which might be empty."
-  (bash-completion-send (concat (bash-completion-generate-line dir line pos 
words cword) " 2>/dev/null"))
+  (bash-completion-send (concat (bash-completion-generate-line line pos words 
cword) " 2>/dev/null"))
   (with-current-buffer (bash-completion-buffer)
-    (mapcar 'bash-completion-trim (split-string (buffer-string) "\n" t))))
+    (mapcar 'bash-completion-addsuffix (split-string (buffer-string) "\n" t))))
 
-(defun bash-completion-trim (str)
-  (if (string-match "^ *\\(.*[^ ]\\) *$" str)
-      (match-string 1 str)
-    str))
+(defun bash-completion-addsuffix (str)
+  (let ((end (substring str -1)))
+    (if (and (not (eq end " "))
+            (not (eq end "/"))
+            (file-accessible-directory-p str))
+       (concat str "/")
+    str)))
 
 (defun bash-completion-require-process ()
   (if (bash-completion-is-running)
@@ -201,14 +206,18 @@ The result is a list of candidates, which might be empty."
                (kill-process process)
              (error nil))))))))
 
-(defun bash-completion-generate-line (dir line pos words cword)
+(defun bash-completion-generate-line (line pos words cword)
   (concat
-   (if default-directory (concat "cd " (bash-completion-quote 
(expand-file-name dir)) " ; ") "")
-   (let* ( (command (file-name-nondirectory (car words)))
-          (compgen-args (cdr (assoc command bash-completion-alist))) )
+   (if default-directory (concat "cd " (bash-completion-quote 
(expand-file-name default-directory)) " ; ") "")
+   (let* ( (command-name (file-name-nondirectory (car words)))
+          (compgen-args (cdr (assoc command-name bash-completion-alist))) )
      (if (not compgen-args)
         ;; no custom completion. use default completion
-        (bash-completion-join (list "compgen" "-o" "default" (nth cword 
words)))
+        (if (= cword 0)
+            ;; a command. let emacs expand executable, let bash expand 
builtins, aliases and functions
+            (concat (bash-completion-join (list "compgen" "-S" " " "-b" "-a" 
"-A" "function" (car words))))
+          ;; argument
+          (bash-completion-join (list "compgen" "-o" "default" (nth cword 
words))))
        ;; custom completion
        (let* ( (args (copy-tree compgen-args))
               (function (or (member "-F" args) (member "-C" args))) )
diff --git a/bash-completion_test.el b/bash-completion_test.el
index fb188a14fa..64ef657b90 100644
--- a/bash-completion_test.el
+++ b/bash-completion_test.el
@@ -168,23 +168,27 @@ garbage
       "'hell'\\''o'")
 
      ("bash-completion-generate-line no custom completion"
-      (let ((bash-completion-alist nil))
-       (bash-completion-generate-line "~/test" "hello worl" 7 '("hello" 
"worl") 1))
+      (let ((bash-completion-alist nil)
+           (default-directory "~/test"))
+       (bash-completion-generate-line "hello worl" 7 '("hello" "worl") 1))
       (concat "cd " (expand-file-name "~/test") " ; compgen -o default worl"))
 
      ("bash-completion-generate-line custom completion no function or command"
-      (let ((bash-completion-alist '(("zorg" . ("-A" "-G" "*.txt")))))
-       (bash-completion-generate-line "/test" "zorg worl" 7 '("zorg" "worl") 
1))
+      (let ((bash-completion-alist '(("zorg" . ("-A" "-G" "*.txt"))))
+           (default-directory "/test"))
+       (bash-completion-generate-line "zorg worl" 7 '("zorg" "worl") 1))
       "cd /test ; compgen -A -G '*.txt' -- worl")
 
      ("bash-completion-generate-line custom completion function"
-      (let ((bash-completion-alist '(("zorg" . ("-F" "__zorg")))))
-       (bash-completion-generate-line "/test" "zorg worl" 7 '("zorg" "worl") 
1))
+      (let ((bash-completion-alist '(("zorg" . ("-F" "__zorg"))))
+           (default-directory "/test"))
+       (bash-completion-generate-line "zorg worl" 7 '("zorg" "worl") 1))
       "cd /test ; __BASH_COMPLETE_WRAPPER='COMP_LINE='\\''zorg worl'\\''; 
COMP_POINT=7; COMP_CWORD=1; COMP_WORDS=( zorg worl ); __zorg \"$@\"' compgen -F 
__bash_complete_wrapper -- worl")
 
      ("bash-completion-generate-line custom completion command"
-      (let ((bash-completion-alist '(("zorg" . ("-C" "__zorg")))))
-       (bash-completion-generate-line "/test" "zorg worl" 7 '("zorg" "worl") 
1))
+      (let ((bash-completion-alist '(("zorg" . ("-C" "__zorg"))))
+           (default-directory "/test"))
+       (bash-completion-generate-line "zorg worl" 7 '("zorg" "worl") 1))
       "cd /test ; __BASH_COMPLETE_WRAPPER='COMP_LINE='\\''zorg worl'\\''; 
COMP_POINT=7; COMP_CWORD=1; COMP_WORDS=( zorg worl ); __zorg \"$@\"' compgen -F 
__bash_complete_wrapper -- worl")
 
      ("bash-completion-trim"
@@ -205,6 +209,14 @@ garbage
        (bash-completion-line-beginning-position 1)))
       '(14 14))
 
+     ("bash-completion-line-beginning-position 2 semicolon"
+      (sz-testutils-with-buffer
+       '("cd /home/x ; blah; " cursor "echo hello")
+       (list
+       (point)
+       (bash-completion-line-beginning-position 1)))
+      '(20 20))
+
      ("bash-completion-line-beginning-position &&"
       (sz-testutils-with-buffer
        '("cd /home/x && " cursor "echo hello")
@@ -213,6 +225,22 @@ garbage
        (bash-completion-line-beginning-position 1)))
       '(15 15))
 
+     ("bash-completion-line-beginning-position ||"
+      (sz-testutils-with-buffer
+       '("cd /home/x || " cursor "echo hello")
+       (list
+       (point)
+       (bash-completion-line-beginning-position 1)))
+      '(15 15))
+
+     ("bash-completion-line-beginning-position variable assignment"
+      (sz-testutils-with-buffer
+       '("a=b " cursor "echo hello")
+       (list
+       (point)
+       (bash-completion-line-beginning-position 1)))
+      '(5 5))
+
       )))
 
 



reply via email to

[Prev in Thread] Current Thread [Next in Thread]