Add "Package:" file headers to denote built-in packages.
[bpt/emacs.git] / lisp / pcmpl-unix.el
CommitLineData
60370d40 1;;; pcmpl-unix.el --- standard UNIX completions
4fa9f636 2
c90f2757 3;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
114f9c96 4;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
4fa9f636 5
bd78fa1d
CY
6;; Package: pcomplete
7
4fa9f636
GM
8;; This file is part of GNU Emacs.
9
eb3fa2cf 10;; GNU Emacs is free software: you can redistribute it and/or modify
4fa9f636 11;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
4fa9f636
GM
14
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.
19
20;; You should have received a copy of the GNU General Public License
eb3fa2cf 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
4fa9f636 22
60370d40
PJ
23;;; Commentary:
24
4fa9f636
GM
25;;; Code:
26
4fa9f636
GM
27(require 'pcomplete)
28
29;; User Variables:
30
31(defcustom pcmpl-unix-group-file "/etc/group"
1e2eefdd
GM
32 "If non-nil, a string naming the group file on your system."
33 :type '(choice file (const nil))
4fa9f636
GM
34 :group 'pcmpl-unix)
35
36(defcustom pcmpl-unix-passwd-file "/etc/passwd"
1e2eefdd
GM
37 "If non-nil, a string naming the passwd file on your system."
38 :type '(choice file (const nil))
4fa9f636
GM
39 :group 'pcmpl-unix)
40
9c519cf2 41(defcustom pcmpl-ssh-known-hosts-file "~/.ssh/known_hosts"
1e2eefdd
GM
42 "If non-nil, a string naming your SSH \"known_hosts\" file.
43This allows completion of SSH host names. Note that newer
44versions of ssh hash the hosts by default to prevent
45Island-hopping SSH attacks. This can be disabled, at some risk,
46with the SSH option \"HashKnownHosts no\"."
47 :type '(choice file (const nil))
48 :group 'pcmpl-unix
49 :version "23.1")
9c519cf2 50
4fa9f636
GM
51;; Functions:
52
53;;;###autoload
54(defun pcomplete/cd ()
55 "Completion for `cd'."
f3b757f5 56 (while (pcomplete-here (pcomplete-dirs))))
4fa9f636
GM
57
58;;;###autoload
59(defalias 'pcomplete/pushd 'pcomplete/cd)
60
61;;;###autoload
62(defun pcomplete/rmdir ()
63 "Completion for `rmdir'."
64 (while (pcomplete-here (pcomplete-dirs))))
65
66;;;###autoload
67(defun pcomplete/rm ()
68 "Completion for `rm'."
69 (let ((pcomplete-help "(fileutils)rm invocation"))
70 (pcomplete-opt "dfirRv")
71 (while (pcomplete-here (pcomplete-all-entries) nil
72 'expand-file-name))))
73
74;;;###autoload
75(defun pcomplete/xargs ()
76 "Completion for `xargs'."
77 (pcomplete-here (funcall pcomplete-command-completion-function))
78 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
79 pcomplete-default-completion-function)))
80
81;;;###autoload
82(defalias 'pcomplete/time 'pcomplete/xargs)
83
84;;;###autoload
85(defun pcomplete/which ()
86 "Completion for `which'."
87 (while (pcomplete-here (funcall pcomplete-command-completion-function))))
88
89(defun pcmpl-unix-read-passwd-file (file)
abd5cfe8
CY
90 "Return an alist correlating gids to group names in FILE.
91
92If FILE is in hashed format (as described in the OpenSSH
93documentation), this function returns nil."
4fa9f636
GM
94 (let (names)
95 (when (file-readable-p file)
96 (with-temp-buffer
97 (insert-file-contents file)
98 (goto-char (point-min))
99 (while (not (eobp))
100 (let* ((fields
101 (split-string (buffer-substring
102 (point) (progn (end-of-line)
103 (point))) ":")))
104 (setq names (cons (nth 0 fields) names)))
105 (forward-line))))
106 (pcomplete-uniqify-list names)))
107
108(defsubst pcmpl-unix-group-names ()
109 "Read the contents of /etc/group for group names."
110 (if pcmpl-unix-group-file
111 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
112
113(defsubst pcmpl-unix-user-names ()
114 "Read the contents of /etc/passwd for user names."
115 (if pcmpl-unix-passwd-file
116 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
117
118;;;###autoload
119(defun pcomplete/chown ()
120 "Completion for the `chown' command."
121 (unless (pcomplete-match "\\`-")
122 (if (pcomplete-match "\\`[^.]*\\'" 0)
123 (pcomplete-here* (pcmpl-unix-user-names))
124 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
125 (pcomplete-here* (pcmpl-unix-group-names)
126 (pcomplete-match-string 1 0))
127 (pcomplete-here*))))
128 (while (pcomplete-here (pcomplete-entries))))
129
130;;;###autoload
131(defun pcomplete/chgrp ()
132 "Completion for the `chgrp' command."
133 (unless (pcomplete-match "\\`-")
134 (pcomplete-here* (pcmpl-unix-group-names)))
135 (while (pcomplete-here (pcomplete-entries))))
136
1e2eefdd 137
9c519cf2
GM
138;; ssh support by Phil Hagelberg.
139;; http://www.emacswiki.org/cgi-bin/wiki/pcmpl-ssh.el
140
1e2eefdd
GM
141(defun pcmpl-ssh-hosts ()
142 "Return a list of hosts found in `pcmpl-ssh-known-hosts-file'."
143 (when (and pcmpl-ssh-known-hosts-file
144 (file-readable-p pcmpl-ssh-known-hosts-file))
145 (with-temp-buffer
146 (insert-file-contents-literally pcmpl-ssh-known-hosts-file)
147 (let (ssh-hosts-list)
148 (while (re-search-forward "^ *\\([-.[:alnum:]]+\\)[, ]" nil t)
149 (add-to-list 'ssh-hosts-list (match-string 1))
150 (while (and (looking-back ",")
151 (re-search-forward "\\([-.[:alnum:]]+\\)[, ]"
152 (line-end-position) t))
153 (add-to-list 'ssh-hosts-list (match-string 1))))
154 ssh-hosts-list))))
9c519cf2
GM
155
156;;;###autoload
157(defun pcomplete/ssh ()
158 "Completion rules for the `ssh' command."
159 (pcomplete-opt "1246AaCfgKkMNnqsTtVvXxYbcDeFiLlmOopRSw" nil t)
160 (pcomplete-here (pcmpl-ssh-hosts)))
161
162;;;###autoload
163(defun pcomplete/scp ()
164 "Completion rules for the `scp' command.
9c519cf2
GM
165Includes files as well as host names followed by a colon."
166 (pcomplete-opt "1246BCpqrvcFiloPS")
167 (while t (pcomplete-here (append (pcomplete-all-entries)
1e2eefdd
GM
168 (mapcar (lambda (host)
169 (concat host ":"))
170 (pcmpl-ssh-hosts))))))
9c519cf2 171
1e2eefdd
GM
172(provide 'pcmpl-unix)
173
174;; arch-tag: 3f9eb5af-7e0e-449d-b586-381cbbf8fc5c
4fa9f636 175;;; pcmpl-unix.el ends here