Mark tiny change.
[emacs.git] / lisp / pcmpl-unix.el
blobe947bfe1da690133a4fa34bd650d5f99b301842a
1 ;;; pcmpl-unix.el --- standard UNIX completions
3 ;; Copyright (C) 1999-2011 Free Software Foundation, Inc.
5 ;; Package: pcomplete
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software: you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation, either version 3 of the License, or
12 ;; (at your option) any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
22 ;;; Commentary:
24 ;;; Code:
26 (require 'pcomplete)
28 ;; User Variables:
30 (defcustom pcmpl-unix-group-file "/etc/group"
31 "If non-nil, a string naming the group file on your system."
32 :type '(choice file (const nil))
33 :group 'pcmpl-unix)
35 (defcustom pcmpl-unix-passwd-file "/etc/passwd"
36 "If non-nil, a string naming the passwd file on your system."
37 :type '(choice file (const nil))
38 :group 'pcmpl-unix)
40 (defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts"
41 "If non-nil, a string naming your SSH \"known_hosts\" file.
42 This allows one method of completion of SSH host names, the other
43 being via `pcmpl-ssh-config-file'. Note that newer versions of
44 ssh hash the hosts by default, to prevent Island-hopping SSH
45 attacks. This can be disabled, at some risk, with the SSH option
46 \"HashKnownHosts no\"."
47 :type '(choice file (const nil))
48 :group 'pcmpl-unix
49 :version "23.1")
51 (defcustom pcmpl-ssh-config-file "~/.ssh/config"
52 "If non-nil, a string naming your SSH \"config\" file.
53 This allows one method of completion of SSH host names, the other
54 being via `pcmpl-ssh-known-hosts-file'."
55 :type '(choice file (const nil))
56 :group 'pcmpl-unix
57 :version "24.1")
59 ;; Functions:
61 ;;;###autoload
62 (defun pcomplete/cd ()
63 "Completion for `cd'."
64 (while (pcomplete-here (pcomplete-dirs))))
66 ;;;###autoload
67 (defalias 'pcomplete/pushd 'pcomplete/cd)
69 ;;;###autoload
70 (defun pcomplete/rmdir ()
71 "Completion for `rmdir'."
72 (while (pcomplete-here (pcomplete-dirs))))
74 ;;;###autoload
75 (defun pcomplete/rm ()
76 "Completion for `rm'."
77 (let ((pcomplete-help "(fileutils)rm invocation"))
78 (pcomplete-opt "dfirRv")
79 (while (pcomplete-here (pcomplete-all-entries) nil
80 'expand-file-name))))
82 ;;;###autoload
83 (defun pcomplete/xargs ()
84 "Completion for `xargs'."
85 (pcomplete-here (funcall pcomplete-command-completion-function))
86 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
87 pcomplete-default-completion-function)))
89 ;;;###autoload
90 (defalias 'pcomplete/time 'pcomplete/xargs)
92 ;;;###autoload
93 (defun pcomplete/which ()
94 "Completion for `which'."
95 (while (pcomplete-here (funcall pcomplete-command-completion-function))))
97 (defun pcmpl-unix-read-passwd-file (file)
98 "Return an alist correlating gids to group names in FILE.
100 If FILE is in hashed format (as described in the OpenSSH
101 documentation), this function returns nil."
102 (let (names)
103 (when (file-readable-p file)
104 (with-temp-buffer
105 (insert-file-contents file)
106 (goto-char (point-min))
107 (while (not (eobp))
108 (let* ((fields
109 (split-string (buffer-substring
110 (point) (progn (end-of-line)
111 (point))) ":")))
112 (setq names (cons (nth 0 fields) names)))
113 (forward-line))))
114 (pcomplete-uniqify-list names)))
116 (defsubst pcmpl-unix-group-names ()
117 "Read the contents of /etc/group for group names."
118 (if pcmpl-unix-group-file
119 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
121 (defsubst pcmpl-unix-user-names ()
122 "Read the contents of /etc/passwd for user names."
123 (if pcmpl-unix-passwd-file
124 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
126 ;;;###autoload
127 (defun pcomplete/chown ()
128 "Completion for the `chown' command."
129 (unless (pcomplete-match "\\`-")
130 (if (pcomplete-match "\\`[^.]*\\'" 0)
131 (pcomplete-here* (pcmpl-unix-user-names))
132 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
133 (pcomplete-here* (pcmpl-unix-group-names)
134 (pcomplete-match-string 1 0))
135 (pcomplete-here*))))
136 (while (pcomplete-here (pcomplete-entries))))
138 ;;;###autoload
139 (defun pcomplete/chgrp ()
140 "Completion for the `chgrp' command."
141 (unless (pcomplete-match "\\`-")
142 (pcomplete-here* (pcmpl-unix-group-names)))
143 (while (pcomplete-here (pcomplete-entries))))
146 ;; ssh support by Phil Hagelberg.
147 ;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
149 (defun pcmpl-ssh-known-hosts ()
150 "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
151 (when (and pcmpl-ssh-known-hosts-file
152 (file-readable-p pcmpl-ssh-known-hosts-file))
153 (with-temp-buffer
154 (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
155 (let (ssh-hosts-list)
156 (while (re-search-forward "^ *\\([-.[:alnum:]]+\\)[, ]" nil t)
157 (add-to-list 'ssh-hosts-list (match-string 1))
158 (while (and (looking-back ",")
159 (re-search-forward "\\([-.[:alnum:]]+\\)[, ]"
160 (line-end-position) t))
161 (add-to-list 'ssh-hosts-list (match-string 1))))
162 ssh-hosts-list))))
164 (defun pcmpl-ssh-config-hosts ()
165 "Return a list of hosts found in `pcmpl-ssh-config-file'."
166 (when (and pcmpl-ssh-config-file
167 (file-readable-p pcmpl-ssh-config-file))
168 (with-temp-buffer
169 (insert-file-contents-literally pcmpl-ssh-config-file)
170 (let (ssh-hosts-list
171 (case-fold-search t))
172 (while (re-search-forward "^ *host\\(name\\)? +\\([-.[:alnum:]]+\\)"
173 nil t)
174 (add-to-list 'ssh-hosts-list (match-string 2)))
175 ssh-hosts-list))))
177 (defun pcmpl-ssh-hosts ()
178 "Return a list of known SSH hosts.
179 Uses both `pcmpl-ssh-config-file' and `pcmpl-ssh-known-hosts-file'."
180 (let ((hosts (pcmpl-ssh-known-hosts)))
181 (dolist (h (pcmpl-ssh-config-hosts))
182 (add-to-list 'hosts h))
183 hosts))
185 ;;;###autoload
186 (defun pcomplete/ssh ()
187 "Completion rules for the `ssh' command."
188 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t)
189 (pcomplete-here (pcmpl-ssh-hosts)))
191 ;;;###autoload
192 (defun pcomplete/scp ()
193 "Completion rules for the `scp' command.
194 Includes files as well as host names followed by a colon."
195 (pcomplete-opt "1246BCpqrvcFiloPS")
196 (while t (pcomplete-here (append (pcomplete-all-entries)
197 (mapcar (lambda (host)
198 (concat host ":"))
199 (pcmpl-ssh-hosts))))))
201 (provide 'pcmpl-unix)
203 ;;; pcmpl-unix.el ends here