1 ;;; pcmpl-unix.el --- standard UNIX completions
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defcustom pcmpl-unix-group-file
"/etc/group"
32 "If non-nil, a string naming the group file on your system."
33 :type
'(choice file
(const nil
))
36 (defcustom pcmpl-unix-passwd-file
"/etc/passwd"
37 "If non-nil, a string naming the passwd file on your system."
38 :type
'(choice file
(const nil
))
41 (defcustom pcmpl-ssh-known-hosts-file
"~/.ssh/known_hosts"
42 "If non-nil, a string naming your SSH \"known_hosts\" file.
43 This allows one method of completion of SSH host names, the other
44 being via `pcmpl-ssh-config-file'. Note that newer versions of
45 ssh hash the hosts by default, to prevent Island-hopping SSH
46 attacks. This can be disabled, at some risk, with the SSH option
47 \"HashKnownHosts no\"."
48 :type
'(choice file
(const nil
))
52 (defcustom pcmpl-ssh-config-file
"~/.ssh/config"
53 "If non-nil, a string naming your SSH \"config\" file.
54 This allows one method of completion of SSH host names, the other
55 being via `pcmpl-ssh-known-hosts-file'."
56 :type
'(choice file
(const nil
))
63 (defun pcomplete/cd
()
64 "Completion for `cd'."
65 (while (pcomplete-here (pcomplete-dirs))))
68 (defalias 'pcomplete
/pushd
'pcomplete
/cd
)
71 (defun pcomplete/rmdir
()
72 "Completion for `rmdir'."
73 (while (pcomplete-here (pcomplete-dirs))))
76 (defun pcomplete/rm
()
77 "Completion for `rm'."
78 (let ((pcomplete-help "(fileutils)rm invocation"))
79 (pcomplete-opt "dfirRv")
80 (while (pcomplete-here (pcomplete-all-entries) nil
84 (defun pcomplete/xargs
()
85 "Completion for `xargs'."
86 (pcomplete-here (funcall pcomplete-command-completion-function
))
87 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
88 pcomplete-default-completion-function
)))
91 (defalias 'pcomplete
/time
'pcomplete
/xargs
)
94 (defun pcomplete/which
()
95 "Completion for `which'."
96 (while (pcomplete-here (funcall pcomplete-command-completion-function
))))
98 (defun pcmpl-unix-read-passwd-file (file)
99 "Return an alist correlating gids to group names in FILE.
101 If FILE is in hashed format (as described in the OpenSSH
102 documentation), this function returns nil."
104 (when (file-readable-p file
)
106 (insert-file-contents file
)
107 (goto-char (point-min))
110 (split-string (buffer-substring
111 (point) (progn (end-of-line)
113 (setq names
(cons (nth 0 fields
) names
)))
115 (pcomplete-uniqify-list names
)))
117 (defsubst pcmpl-unix-group-names
()
118 "Read the contents of /etc/group for group names."
119 (if pcmpl-unix-group-file
120 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file
)))
122 (defsubst pcmpl-unix-user-names
()
123 "Read the contents of /etc/passwd for user names."
124 (if pcmpl-unix-passwd-file
125 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file
)))
128 (defun pcomplete/chown
()
129 "Completion for the `chown' command."
130 (unless (pcomplete-match "\\`-")
131 (if (pcomplete-match "\\`[^.]*\\'" 0)
132 (pcomplete-here* (pcmpl-unix-user-names))
133 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
134 (pcomplete-here* (pcmpl-unix-group-names)
135 (pcomplete-match-string 1 0))
137 (while (pcomplete-here (pcomplete-entries))))
140 (defun pcomplete/chgrp
()
141 "Completion for the `chgrp' command."
142 (unless (pcomplete-match "\\`-")
143 (pcomplete-here* (pcmpl-unix-group-names)))
144 (while (pcomplete-here (pcomplete-entries))))
147 ;; ssh support by Phil Hagelberg.
148 ;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
150 (defun pcmpl-ssh-known-hosts ()
151 "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
152 (when (and pcmpl-ssh-known-hosts-file
153 (file-readable-p pcmpl-ssh-known-hosts-file
))
155 (insert-file-contents-literally pcmpl-ssh-known-hosts-file
)
156 (let (ssh-hosts-list)
157 (while (re-search-forward "^ *\\([-.[:alnum:]]+\\)[, ]" nil t
)
158 (add-to-list 'ssh-hosts-list
(match-string 1))
159 (while (and (looking-back ",")
160 (re-search-forward "\\([-.[:alnum:]]+\\)[, ]"
161 (line-end-position) t
))
162 (add-to-list 'ssh-hosts-list
(match-string 1))))
165 (defun pcmpl-ssh-config-hosts ()
166 "Return a list of hosts found in `pcmpl-ssh-config-file'."
167 (when (and pcmpl-ssh-config-file
168 (file-readable-p pcmpl-ssh-config-file
))
170 (insert-file-contents-literally pcmpl-ssh-config-file
)
172 (case-fold-search t
))
173 (while (re-search-forward "^ *host\\(name\\)? +\\([-.[:alnum:]]+\\)"
175 (add-to-list 'ssh-hosts-list
(match-string 2)))
178 (defun pcmpl-ssh-hosts ()
179 "Return a list of known SSH hosts.
180 Uses both `pcmpl-ssh-config-file' and `pcmpl-ssh-known-hosts-file'."
181 (let ((hosts (pcmpl-ssh-known-hosts)))
182 (dolist (h (pcmpl-ssh-config-hosts))
183 (add-to-list 'hosts h
))
187 (defun pcomplete/ssh
()
188 "Completion rules for the `ssh' command."
189 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t
)
190 (pcomplete-here (pcmpl-ssh-hosts)))
193 (defun pcomplete/scp
()
194 "Completion rules for the `scp' command.
195 Includes files as well as host names followed by a colon."
196 (pcomplete-opt "1246BCpqrvcFiloPS")
197 (while t
(pcomplete-here (append (pcomplete-all-entries)
198 (mapcar (lambda (host)
200 (pcmpl-ssh-hosts))))))
202 (provide 'pcmpl-unix
)
204 ;;; pcmpl-unix.el ends here