Merge from emacs--rel--22
[emacs.git] / lisp / pcmpl-unix.el
blob9cd5de1e4d1b259fad37954c4e7364e00142389b
1 ;;; pcmpl-unix.el --- standard UNIX completions
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; This file is part of GNU Emacs.
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 2, or (at your option)
11 ;; any later version.
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 ;; GNU General Public License for more details.
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING. If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
23 ;;; Commentary:
25 ;;; Code:
27 (provide 'pcmpl-unix)
29 (require 'pcomplete)
31 ;; User Variables:
33 (defcustom pcmpl-unix-group-file "/etc/group"
34 "*If non-nil, a string naming the group file on your system."
35 :type 'file
36 :group 'pcmpl-unix)
38 (defcustom pcmpl-unix-passwd-file "/etc/passwd"
39 "*If non-nil, a string naming the passwd file on your system."
40 :type 'file
41 :group 'pcmpl-unix)
43 ;; Functions:
45 ;;;###autoload
46 (defun pcomplete/cd ()
47 "Completion for `cd'."
48 (pcomplete-here (pcomplete-dirs)))
50 ;;;###autoload
51 (defalias 'pcomplete/pushd 'pcomplete/cd)
53 ;;;###autoload
54 (defun pcomplete/rmdir ()
55 "Completion for `rmdir'."
56 (while (pcomplete-here (pcomplete-dirs))))
58 ;;;###autoload
59 (defun pcomplete/rm ()
60 "Completion for `rm'."
61 (let ((pcomplete-help "(fileutils)rm invocation"))
62 (pcomplete-opt "dfirRv")
63 (while (pcomplete-here (pcomplete-all-entries) nil
64 'expand-file-name))))
66 ;;;###autoload
67 (defun pcomplete/xargs ()
68 "Completion for `xargs'."
69 (pcomplete-here (funcall pcomplete-command-completion-function))
70 (funcall (or (pcomplete-find-completion-function (pcomplete-arg 1))
71 pcomplete-default-completion-function)))
73 ;;;###autoload
74 (defalias 'pcomplete/time 'pcomplete/xargs)
76 ;;;###autoload
77 (defun pcomplete/which ()
78 "Completion for `which'."
79 (while (pcomplete-here (funcall pcomplete-command-completion-function))))
81 (defun pcmpl-unix-read-passwd-file (file)
82 "Return an alist correlating gids to group names in FILE."
83 (let (names)
84 (when (file-readable-p file)
85 (with-temp-buffer
86 (insert-file-contents file)
87 (goto-char (point-min))
88 (while (not (eobp))
89 (let* ((fields
90 (split-string (buffer-substring
91 (point) (progn (end-of-line)
92 (point))) ":")))
93 (setq names (cons (nth 0 fields) names)))
94 (forward-line))))
95 (pcomplete-uniqify-list names)))
97 (defsubst pcmpl-unix-group-names ()
98 "Read the contents of /etc/group for group names."
99 (if pcmpl-unix-group-file
100 (pcmpl-unix-read-passwd-file pcmpl-unix-group-file)))
102 (defsubst pcmpl-unix-user-names ()
103 "Read the contents of /etc/passwd for user names."
104 (if pcmpl-unix-passwd-file
105 (pcmpl-unix-read-passwd-file pcmpl-unix-passwd-file)))
107 ;;;###autoload
108 (defun pcomplete/chown ()
109 "Completion for the `chown' command."
110 (unless (pcomplete-match "\\`-")
111 (if (pcomplete-match "\\`[^.]*\\'" 0)
112 (pcomplete-here* (pcmpl-unix-user-names))
113 (if (pcomplete-match "\\.\\([^.]*\\)\\'" 0)
114 (pcomplete-here* (pcmpl-unix-group-names)
115 (pcomplete-match-string 1 0))
116 (pcomplete-here*))))
117 (while (pcomplete-here (pcomplete-entries))))
119 ;;;###autoload
120 (defun pcomplete/chgrp ()
121 "Completion for the `chgrp' command."
122 (unless (pcomplete-match "\\`-")
123 (pcomplete-here* (pcmpl-unix-group-names)))
124 (while (pcomplete-here (pcomplete-entries))))
126 ;;; arch-tag: 3f9eb5af-7e0e-449d-b586-381cbbf8fc5c
127 ;;; pcmpl-unix.el ends here