branch: elpa/bash-completion
commit 34d11220dd5d474cfa3be2797388d566bd782860
Author: Stephane Zermatten <[email protected]>
Commit: Stephane Zermatten <[email protected]>
add prefix if missing
---
bash-completion.el | 21 +++++++++++++++++----
bash-completion_test.el | 22 ++++++++++++++++++++++
2 files changed, 39 insertions(+), 4 deletions(-)
diff --git a/bash-completion.el b/bash-completion.el
index f880c9ee65..d649d6c1d7 100644
--- a/bash-completion.el
+++ b/bash-completion.el
@@ -59,9 +59,7 @@ Call bash to do the completion."
(setq words (cdr wordsplit))
(setq stub (nth cword words)))
(let ((completions (bash-completion-comm line (- pos start) words cword)))
- (comint-dynamic-simple-complete
- stub
- completions))))
+ (comint-dynamic-simple-complete stub completions))))
(defun bash-completion-line-beginning-position (&optional start)
(save-excursion
@@ -163,7 +161,21 @@ calls compgen.
The result is a list of candidates, which might be empty."
(bash-completion-send (concat (bash-completion-generate-line line pos words
cword) " 2>/dev/null"))
(with-current-buffer (bash-completion-buffer)
- (mapcar 'bash-completion-addsuffix (split-string (buffer-string) "\n" t))))
+ (let ((bash-completion-prefix (nth cword words)))
+ (mapcar 'bash-completion-fix (split-string (buffer-string) "\n" t)))))
+
+(defun bash-completion-fix (str)
+ (bash-completion-addsuffix
+ (if (bash-completion-starts-with str bash-completion-prefix)
+ str
+ (concat bash-completion-prefix str))))
+
+(defun bash-completion-starts-with (str prefix)
+ (let ((prefix-len (length prefix))
+ (str-len (length str)))
+ (and
+ (>= str-len prefix-len)
+ (equal (substring str 0 prefix-len) prefix))))
(defun bash-completion-addsuffix (str)
(let ((end (substring str -1)))
@@ -253,6 +265,7 @@ The result is a list of candidates, which might be empty."
(and bash-completion-process (eq 'run (process-status
bash-completion-process))))
(defun bash-completion-send (commandline &optional process timeout)
+ (message commandline)
(let ((process (or process (bash-completion-require-process)))
(timeout (or timeout bash-completion-process-timeout)))
(with-current-buffer (process-buffer process)
diff --git a/bash-completion_test.el b/bash-completion_test.el
index 4c33ea003e..cd64a75a65 100644
--- a/bash-completion_test.el
+++ b/bash-completion_test.el
@@ -249,6 +249,28 @@ garbage
(bash-completion-line-beginning-position 1)))
'(5 5))
+ ("bash-completion-line-beginning-position variable assignment"
+ (sz-testutils-with-buffer
+ '("ls ; c=d export a=b" cursor)
+ (bash-completion-line-beginning-position 1))
+ 10)
+
+ ("bash-completion-starts-with empty str"
+ (bash-completion-starts-with "" "prefix")
+ nil)
+
+ ("bash-completion-starts-with starts with"
+ (bash-completion-starts-with "blah-blah" "blah-")
+ t)
+
+ ("bash-completion-starts-with does not starts with"
+ (bash-completion-starts-with "blah-blah" "blih-")
+ nil)
+
+ ("bash-completion-starts-with same"
+ (bash-completion-starts-with "blah-" "blah-")
+ t)
+
)))