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

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

[nongnu] elpa/bash-completion 540f8f516a 011/313: build bash cmdline


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion 540f8f516a 011/313: build bash cmdline
Date: Sat, 3 Dec 2022 10:59:10 -0500 (EST)

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

    build bash cmdline
---
 bash-complete.el      | 36 ++++++++++++++++++++++++++++++------
 bash-complete_test.el | 40 ++++++++++++++++++++++++++++++++++++++--
 2 files changed, 68 insertions(+), 8 deletions(-)

diff --git a/bash-complete.el b/bash-complete.el
index e102496f33..3f9292f379 100644
--- a/bash-complete.el
+++ b/bash-complete.el
@@ -44,14 +44,16 @@ Call bash to do the completion."
 (defun bash-complete-join (words)
   "Join WORDS into a shell line, escaped all words with single quotes"
   (if words
-      (concat "'"
-             (mapconcat
-              (lambda (word)
-                (replace-regexp-in-string "'" "\\\'" word :literal t))
-              words "' '")
-             "'")
+      (mapconcat
+       'bash-complete-quote
+       words " ")
     ""))
 
+(defun bash-complete-quote (word)
+  (if (string-match "^[a-zA-Z0-9_.-]*$" word)
+      word
+    (concat "'" (replace-regexp-in-string "'" "\\\'" word :literal t) "'")))
+
 (defun bash-complete-split (start end pos)
   "Split LINE like bash would do, keep track of current word at POS.
 
@@ -135,9 +137,31 @@ The result is a list of candidates, which might be empty."
     (set-process-query-on-exit-flag bash-complete-process nil)
     (bash-complete-send "PS1='\v'")
     (bash-complete-send "complete -p")
+    (bash-complete-send "function __bash_complete_wrapper { eval 
$__BASH_COMPLETE_WRAPPER }")
     (bash-complete-build-alist (process-buffer bash-complete-process)))
   bash-complete-process)
 
+(defun bash-complete-generate-line (line pos words cword)
+  (let* ( (command (file-name-nondirectory (car words)))
+         (compgen-args (cdr (assoc command bash-complete-alist))) )
+    (if (not compgen-args)
+       ;; no custom completion. use default completion
+       (bash-complete-join (list "compgen" "-o" "default" (nth cword words)))
+      ;; custom completion
+      (let* ( (args (copy-tree compgen-args))
+             (function (or (member "-F" args) (member "-C" args))) )
+       (if function
+           (let ((function-name (car (cdr function))))
+             (setcar function "-F")
+             (setcar (cdr function) "__bash_complete_wrapper")
+             (format "__BASH_COMPLETE_WRAPPER=%s compgen %s %s"
+                     (bash-complete-quote (format "COMP_LINE=%s; COMP_POS=%s; 
COMP_CWORD=%s; COMP_WORDS=( %s ); %s \"$@\""
+                                                  (bash-complete-quote line) 
pos cword (bash-complete-join words)
+                                                  (bash-complete-quote 
function-name)))
+                     (bash-complete-join args)
+                     (bash-complete-quote (nth cword words))))
+         (format "compgen %s %s" (bash-complete-join args) (nth cword 
words)))))))
+
 (defun bash-complete-kill-process ()
   (when (bash-complete-is-running)
     (kill-process bash-complete-process)))
diff --git a/bash-complete_test.el b/bash-complete_test.el
index 31b17544db..41b93c7356 100644
--- a/bash-complete_test.el
+++ b/bash-complete_test.el
@@ -30,11 +30,15 @@
 
      ("bash-complete-join simple"
       (bash-complete-join '("a" "hello" "world" "b" "c"))
-      "'a' 'hello' 'world' 'b' 'c'")
+      "a hello world b c")
 
      ("bash-complete-join escape quote"
       (bash-complete-join '("a" "hel'lo" "world" "b" "c"))
-      "'a' 'hel\\'lo' 'world' 'b' 'c'")
+      "a 'hel\\'lo' world b c")
+
+     ("bash-complete-join escape space"
+      (bash-complete-join '("a" "hello world" "b" "c"))
+      "a 'hello world' b c")
 
      ("bash-complete-split simple"
       (sz-testutils-with-buffer
@@ -151,6 +155,38 @@ garbage
        ("cv" "-F" "_cdargs_aliases")
        ("cb" "-F" "_cdargs_aliases")))
 
+     ("bash-complete-quote not necessary"
+      (bash-complete-quote "hello")
+      "hello")
+
+     ("bash-complete-quote not necessary"
+      (bash-complete-quote "hello world")
+      "'hello world'")
+
+     ("bash-complete-quote not necessary"
+      (bash-complete-quote "hell'o")
+      "'hell\\'o'")
+
+     ("bash-complete-generate-line no custom completion"
+      (let ((bash-complete-alist nil))
+       (bash-complete-generate-line "hello worl" 7 '("hello" "worl") 1))
+      "compgen -o default worl")
+
+     ("bash-complete-generate-line custom completion no function or command"
+      (let ((bash-complete-alist '(("zorg" . ("-A" "-G" "*.txt")))))
+       (bash-complete-generate-line "zorg worl" 7 '("zorg" "worl") 1))
+      "compgen -A -G '*.txt' worl")
+
+     ("bash-complete-generate-line custom completion function"
+      (let ((bash-complete-alist '(("zorg" . ("-F" "__zorg")))))
+       (bash-complete-generate-line "zorg worl" 7 '("zorg" "worl") 1))
+      "__BASH_COMPLETE_WRAPPER='COMP_LINE=\\'zorg worl\\'; COMP_POS=7; 
COMP_CWORD=1; COMP_WORDS=( zorg worl ); __zorg \"$@\"' compgen -F 
__bash_complete_wrapper worl")
+
+     ("bash-complete-generate-line custom completion command"
+      (let ((bash-complete-alist '(("zorg" . ("-C" "__zorg")))))
+       (bash-complete-generate-line "zorg worl" 7 '("zorg" "worl") 1))
+      "__BASH_COMPLETE_WRAPPER='COMP_LINE=\\'zorg worl\\'; COMP_POS=7; 
COMP_CWORD=1; COMP_WORDS=( zorg worl ); __zorg \"$@\"' compgen -F 
__bash_complete_wrapper worl")
+
       )))
 
 



reply via email to

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