bug-gnu-emacs
[Top][All Lists]
Advanced

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

bug#16197: 24.3.50; no completion on commands following "sudo" in shell


From: Thierry Volpiatto
Subject: bug#16197: 24.3.50; no completion on commands following "sudo" in shell and eshell
Date: Tue, 07 Jan 2014 14:11:39 +0100
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3.50 (gnu/linux)

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>> https://github.com/thierryvolpiatto/emacs-tv-config/blob/master/pcomplete-extension.el
>
> Not sure what I should look at there.

For a quick test, you can load this file and do the following tests in
`shell' and/or `eshell':

;; Tests
;; find . -name '*.el' | xargs et      =>ok
;; sudo apt-g                          =>ok
;; sudo apt-get in                     =>ok
;; sudo apt-get --                     =>ok
;; sudo apt-get -                      =>ok
;; sudo apt-get -V -                   =>ok
;; sudo apt-get -V --                  =>ok
;; sudo apt-get --reinstall ins        =>ok
;; sudo apt-get --reinstall install em =>ok
;; sudo -                              =>ok
;; sudo -p "pass" -                    =>ok
;; sudo -p "pass" apt-g                =>ok
;; sudo -p "pass" apt-get ins          =>ok
;; apt-get in                          =>ok
;; apt-get install em                  =>ok

Tell me if something went wrong on these tests or if you have other
cases that are not working.
NOTE: [...] | xargs -0 foo
is not working yet because args are not handled in pcomplete/xargs.
I would do it later if you accept the patch below.

> Can you send it as a patch against pcmpl-unix.el to fix pcomplete/xargs
> (and add pcomplete/sudo at the same time)?

* lisp/pcml-unix.el (pcomplete/sudo): New.
* lisp/pcomplete.el (pcomplete-command-name): Allow extracting command-name in 
more complex command lines.
* lisp/shell.el (shell-command-completion-function): Fix it (was not working at 
all).

--8<---------------cut here---------------start------------->8---
Changes from master to stash
3 files changed, 52 insertions(+), 4 deletions(-)
 lisp/pcmpl-unix.el | 16 ++++++++++++++++
 lisp/pcomplete.el  | 35 ++++++++++++++++++++++++++++++++++-
 lisp/shell.el      |  5 ++---

        Modified   lisp/pcmpl-unix.el
diff --git a/lisp/pcmpl-unix.el b/lisp/pcmpl-unix.el
index 6ac3e15..1c55f67 100644
--- a/lisp/pcmpl-unix.el
+++ b/lisp/pcmpl-unix.el
@@ -215,6 +215,22 @@ Includes files as well as host names followed by a colon."
                                        (pcmpl-ssh-hosts)))))))
                 (complete-with-action action table string pred))))))
 
+(defun pcomplete/sudo ()
+  (let ((pcomplete-cmd-name (pcomplete-command-name)))
+    (while (and (string= "sudo" pcomplete-cmd-name)
+                (pcomplete-match "^-" 'last))
+      (when (< pcomplete-index pcomplete-last)
+        (pcomplete-next-arg))
+      (pcomplete-opt "AbCDEegHhiKknPpSsUuVv-"))
+    (cond ((string= "sudo" pcomplete-cmd-name)
+           (while (pcomplete-here*
+                   (funcall pcomplete-command-completion-function)
+                   (pcomplete-arg 'last) t)))
+          (t
+           (funcall (or (pcomplete-find-completion-function
+                         pcomplete-cmd-name)
+                        pcomplete-default-completion-function))))))
+
 (provide 'pcmpl-unix)
 
 ;;; pcmpl-unix.el ends here
        Modified   lisp/pcomplete.el
diff --git a/lisp/pcomplete.el b/lisp/pcomplete.el
index dbeefda..68351e2 100644
--- a/lisp/pcomplete.el
+++ b/lisp/pcomplete.el
@@ -633,9 +633,42 @@ user actually typed in."
        (message "No completions")
        (throw 'pcompleted nil))))
 
+(defvar pcomplete-special-commands '("sudo" "xargs"))
 (defun pcomplete-command-name ()
   "Return the command name of the first argument."
-  (file-name-nondirectory (pcomplete-arg 'first)))
+  (let ((coms (cl-loop with lst = (reverse (pcomplete-parse-arguments))
+                       for str in (or (member "|" lst)
+                                      (member "||" lst)
+                                      (member "&" lst)
+                                      (member ";" lst)
+                                      lst)
+                       for exec = (or (executable-find str)
+                                      ;; `executable-find' or 'which'
+                                      ;; doesn't return these paths.
+                                      (car (member str '("cd" "pushd" 
"popd"))))
+                       when exec collect exec)))
+    (file-name-nondirectory
+     ;; we may have commands embeded in executables that looks
+     ;; like executables (e.g apt-get install).
+     ;; Assume that all executables are using only one command
+     ;; like this.
+     ;; e.g - if we have (install apt-get sudo)
+     ;;       what we want is apt-get.
+     ;;     - if we have (apt-get sudo)
+     ;;       what we want is sudo,
+     ;;       then pcomplete/sudo will check if
+     ;;       a pcomplete handler exists for apt-get.
+     (cond (;; e.g (install apt-get sudo)
+            (> (length coms) 2) (cadr coms))
+           (;; e.g (apt-get sudo)
+            (and (= (length coms) 2)
+                 (member (file-name-nondirectory (cadr coms))
+                         pcomplete-special-commands))
+            (car coms))
+           (;; e.g (sudo)
+            (= (length coms) 1) (car coms))
+           (t ;; e.g (install apt-get)
+            (cadr coms))))))
 
 (defun pcomplete-match (regexp &optional index offset start)
   "Like `string-match', but on the current completion argument."
        Modified   lisp/shell.el
diff --git a/lisp/shell.el b/lisp/shell.el
index 1d67f0b..5bb6a91 100644
--- a/lisp/shell.el
+++ b/lisp/shell.el
@@ -447,9 +447,8 @@ Thus, this does not include the shell's current directory.")
 This is the value of `pcomplete-command-completion-function' for
--8<---------------cut here---------------end--------------->8---
 Shell buffers.  It implements `shell-completion-execonly' for
 `pcomplete' completion."
-  (pcomplete-here (pcomplete-entries nil
-                                    (if shell-completion-execonly
-                                        'file-executable-p))))
+  (let ((data (shell--command-completion-data)))
+    (and data (pcomplete-here (all-completions "" (nth 2 data))))))
 
 (defun shell-completion-vars ()
   "Setup completion vars for `shell-mode' and `read-shell-command'."


-- 
Thierry
Get my Gnupg key:
gpg --keyserver pgp.mit.edu --recv-keys 59F29997 





reply via email to

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