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

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

[nongnu] elpa/bash-completion f5384c6176 051/313: A little refactoring,


From: ELPA Syncer
Subject: [nongnu] elpa/bash-completion f5384c6176 051/313: A little refactoring, test case for wordbreak completion
Date: Sat, 3 Dec 2022 10:59:15 -0500 (EST)

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

    A little refactoring, test case for wordbreak completion
---
 bash-completion.el      | 51 +++++++++++++++++++++++++++++++++++++------------
 bash-completion_test.el | 18 +++++++++++++++++
 2 files changed, 57 insertions(+), 12 deletions(-)

diff --git a/bash-completion.el b/bash-completion.el
index fa2f0d03b7..4b2440ec9b 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -10,7 +10,11 @@
 (defvar bash-completion-prog "bash"
   "Command-line to execute bash")
 
-(defvar bash-completion-process-timeout 2.5)
+(defvar bash-completion-process-timeout 2.5
+  "Timeout value to apply when waiting from an answer from the
+bash process. If bash takes longer than that to answer, the answer
+will be ignored.")
+
 (defvar bash-completion-initial-timeout 30
   "Timeout value to apply when talking to bash for the first time.
 The first thing bash is supposed to do is process /etc/bash_complete,
@@ -29,8 +33,15 @@ the following entry is added to `bash-completion-alist':
 See `bash-completion-add-to-alist'.
 ")
 
-(defconst bash-completion-wordbreaks-str "\"'@><=;|&(:")
-(defconst bash-completion-wordbreaks (append bash-completion-wordbreaks-str 
nil))
+(defconst bash-completion-wordbreaks-str "\"'@><=;|&(:"
+  "The equivalent of COMP_WORDBREAKS: special characters that are
+considered word breaks in some cases when doing completion.  This
+was introduced initially to support file completion in
+colon-separated values.")
+
+(defconst bash-completion-wordbreaks
+  (append bash-completion-wordbreaks-str nil)
+  "`bash-completion-wordbreaks-str' as a list of characters")
 
 (defun bash-completion-setup ()
   (add-hook 'shell-dynamic-complete-functions
@@ -50,20 +61,32 @@ Call bash to do the completion."
          (end (line-end-position))
          (parsed (bash-completion-parse-line start end pos))
          (line (cdr (assq 'line parsed)))
+         (point (cdr (assq 'point parsed)))
          (cword (cdr (assq 'cword parsed)))
          (words (cdr (assq 'words parsed)))
          (stub (nth cword words))
+         (completions (bash-completion-comm line point words cword))
          ;; Override configuration for comint-dynamic-simple-complete.
          ;; Bash adds a space suffix automatically.
          (comint-completion-addsuffix nil) )
-    (let ((completions (bash-completion-comm line (- pos start) words cword)))
-      (if completions
-         (comint-dynamic-simple-complete stub completions)
-       ;; try default completion after a wordbreak
-       (let ((after-wordbreak (bash-completion-after-last-wordbreak stub)))
-         (when (not (equal stub after-wordbreak))
-           (bash-completion-send (concat (bash-completion-cd-command-prefix) 
"compgen -o default -- " after-wordbreak))
-           (comint-dynamic-simple-complete after-wordbreak 
(bash-completion-extract after-wordbreak))))))))
+    (if completions
+       (comint-dynamic-simple-complete stub completions)
+      ;; no standard completion
+      ;; try default (file) completion after a wordbreak
+      (bash-completion-dynamic-try-wordbreak-complete stub))))
+
+(defun bash-completion-dynamic-try-wordbreak-complete (stub)
+  (let* ((wordbreak-split (bash-completion-last-wordbreak-split stub))
+        (before-wordbreak (car wordbreak-split))
+        (after-wordbreak (cdr wordbreak-split)))
+    (when (car wordbreak-split)
+      (bash-completion-send (concat
+                            (bash-completion-cd-command-prefix)
+                            "compgen -o default -- "
+                            after-wordbreak))
+      (comint-dynamic-simple-complete
+       after-wordbreak
+       (bash-completion-extract after-wordbreak)))))
 
 (defun bash-completion-join (words)
   "Join WORDS into a shell line, escaped all words with single quotes"
@@ -116,6 +139,7 @@ Call bash to do the completion."
       (push "" strings))
     (list
      (cons 'line (buffer-substring-no-properties start (cdr 
(bash-completion-tokenize-get-range current))))
+     (cons 'point (- pos start))
      (cons 'cword cword)
      (cons 'words (nreverse strings)))))
 
@@ -234,7 +258,10 @@ Call bash to do the completion."
 (defconst bash-completion-nonsep-alist
   '((nil . "^ \t\n\r;&|'\"")
     (?' . "^ \t\n\r'")
-    (?\" . "^ \t\n\r\"")))
+    (?\" . "^ \t\n\r\""))
+  "Sets of non-breaking characters for all quoting
+environment (no quote, single quote and double quote).
+Get it using `bash-completion-nonsep'.")
 
 (defun bash-completion-nonsep (quote)
   (cdr (assq quote bash-completion-nonsep-alist)))
diff --git a/bash-completion_test.el b/bash-completion_test.el
index 688005598a..4f1f82c6aa 100644
--- a/bash-completion_test.el
+++ b/bash-completion_test.el
@@ -137,6 +137,7 @@ cases. That's why they need to be enabled manually.")
        '("a hello world" cursor " b c")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "a hello world b c")
+       (point . 13)
        (cword . 2)
        (words . ("a" "hello" "world" "b" "c"))))
 
@@ -145,6 +146,7 @@ cases. That's why they need to be enabled manually.")
        '("a hello wo" cursor "rld b c")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "a hello world b c")
+       (point . 10)
        (cword . 2)
        (words . ("a" "hello" "world" "b" "c"))))
 
@@ -153,6 +155,7 @@ cases. That's why they need to be enabled manually.")
        '(" " cursor " a hello world b c")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . " a hello world b c")
+       (point . 0)
        (cword . 0)
        (words . ("" "a" "hello" "world" "b" "c"))))
 
@@ -161,6 +164,7 @@ cases. That's why they need to be enabled manually.")
        '("a hello " cursor " world b c")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "a hello  world b c")
+       (point . 8)
        (cword . 2)
        (words . ("a" "hello" "" "world" "b" "c"))))
 
@@ -169,6 +173,7 @@ cases. That's why they need to be enabled manually.")
        '("a hello world b c" cursor)
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "a hello world b c")
+       (point . 17)
        (cword . 4)
        (words . ("a" "hello" "world" "b" "c"))))
 
@@ -177,6 +182,7 @@ cases. That's why they need to be enabled manually.")
        '("cd /var/tmp ; ZORG=t make -" cursor " -f Makefile && ./zorg")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "make - -f Makefile")
+       (point . 6)
        (cword . 1)
        (words . ("make" "-" "-f" "Makefile"))))
 
@@ -186,6 +192,7 @@ cases. That's why they need to be enabled manually.")
        '("ls /var/tmp | sort -" cursor)
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "sort -")
+       (point . 6)
        (cword . 1)
        (words . ("sort" "-"))))
 
@@ -194,6 +201,7 @@ cases. That's why they need to be enabled manually.")
        '("find -name '*.txt' -" cursor " -exec echo {} ';' | head")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "find -name '*.txt' - -exec echo {} ';'")
+       (point . 20)
        (cword . 3)
        (words . ("find" "-name" "*.txt" "-" "-exec" "echo" "{}" ";"))))
 
@@ -202,6 +210,7 @@ cases. That's why they need to be enabled manually.")
        '("cd /var/tmp ; A=f ZORG=t" cursor " make -f Makefile && ./zorg")
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "ZORG=t")
+       (point . 6)
        (cword . 0)
        (words . ("ZORG=t"))))
 
@@ -210,6 +219,7 @@ cases. That's why they need to be enabled manually.")
        '("a hello world b c " cursor)
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "a hello world b c")
+       (point . 18)
        (cword . 5)
        (words . ("a" "hello" "world" "b" "c" ""))))
 
@@ -218,6 +228,7 @@ cases. That's why they need to be enabled manually.")
        '("cd /vcr/shows/Dexter\\'s" cursor)
        (bash-completion-parse-line 1 (line-end-position) (point)))
       '((line . "cd /vcr/shows/Dexter\\'s")
+       (point . 23)
        (cword . 1)
        (words . ("cd" "/vcr/shows/Dexter's"))))
 
@@ -559,6 +570,13 @@ garbage
           (bash-completion-dynamic-complete)))
        "__bash_complete_wrapper ")
 
+       ("bash-completion execute wordbreak completion"
+       (bash-completion_test-with-shell
+        (let ((start (point)))
+          (insert "export PATH=/sbin:/b")
+          (bash-completion-dynamic-complete)))
+       "export PATH=/sbin:/bin/")
+
        )))
 
 ;; Run diagnostics when this module is evaluated or compiled



reply via email to

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