branch: elpa/bash-completion
commit c10cfef5b8ae46c36f98bf3646c17f2382b95f0c
Author: Stephane Zermatten <[email protected]>
Commit: Stephane Zermatten <[email protected]>
complex line split
---
bash-complete.el | 51 +++++++++++++++++++++++++++++++--------------------
bash-complete_test.el | 22 ++++++++++++++++++++++
2 files changed, 53 insertions(+), 20 deletions(-)
diff --git a/bash-complete.el b/bash-complete.el
index 82a8072c8c..edbd4eacdb 100644
--- a/bash-complete.el
+++ b/bash-complete.el
@@ -35,7 +35,7 @@ Call bash to do the completion."
(defun bash-complete-dynamic-complete-0 ()
(let* ( (pos (point))
- (start (comint-line-beginning-position))
+ (start (bash-complete-line-beginning-position))
(end (line-end-position))
(line (buffer-substring-no-properties start end))
(wordsplit)
@@ -52,6 +52,15 @@ Call bash to do the completion."
(bash-complete-comm
line (- pos start) words cword))))
+(defun bash-complete-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)
+ (match-end 0)
+ start))))
+
(defun bash-complete-join (words)
"Join WORDS into a shell line, escaped all words with single quotes"
(if words
@@ -161,25 +170,27 @@ The result is a list of candidates, which might be empty."
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_POINT=%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)))))))
+ (concat
+ (if default-directory (concat "cd " (bash-complete-quote default-directory)
" && ") "")
+ (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_POINT=%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)
diff --git a/bash-complete_test.el b/bash-complete_test.el
index 3a77c25ba7..64e21ba716 100644
--- a/bash-complete_test.el
+++ b/bash-complete_test.el
@@ -191,6 +191,28 @@ garbage
(mapcar 'bash-complete-trim '(" hello " " world " "x"))
'("hello" "world" "x"))
+ ("bash-complete-line-beginning-position start"
+ (sz-testutils-with-buffer
+ "cd /home/x"
+ (bash-complete-line-beginning-position 1))
+ 1)
+
+ ("bash-complete-line-beginning-position semicolon"
+ (sz-testutils-with-buffer
+ '("cd /home/x ; " cursor "echo hello")
+ (list
+ (point)
+ (bash-complete-line-beginning-position 1)))
+ '(14 14))
+
+ ("bash-complete-line-beginning-position &&"
+ (sz-testutils-with-buffer
+ '("cd /home/x && " cursor "echo hello")
+ (list
+ (point)
+ (bash-complete-line-beginning-position 1)))
+ '(15 15))
+
)))