1 ;;; pcmpl-gnu.el --- completions for GNU project tools -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2014 Free Software Foundation, Inc.
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/>.
31 (defgroup pcmpl-gnu nil
32 "Completions for GNU project tools."
37 (defcustom pcmpl-gnu-makefile-regexps
38 '("\\`GNUmakefile" "\\`[Mm]akefile" "\\.ma?k\\'")
39 "A list of regexps that will match Makefile names."
40 :type
'(repeat regexp
)
46 (defun pcomplete/gzip
()
47 "Completion for `gzip'."
48 (let ((pcomplete-help "(gzip)"))
49 (pcomplete-opt "cdfhlLnNqrStvV123456789")
50 (while (pcomplete-here
51 (pcmpl-gnu-zipped-files
53 (let ((args pcomplete-args
))
55 (if (string-match "\\`-.*[dt]" (car args
))
56 (throw 'has-d-flag t
))
57 (setq args
(cdr args
))))))))))
59 (defun pcmpl-gnu-zipped-files (unzip-p)
60 "Find all zipped or unzipped files: the inverse of UNZIP-P."
65 (when (and (file-readable-p entry
)
66 (file-regular-p entry
))
67 (let ((zipped (string-match "\\.\\(t?gz\\|\\(ta\\)?Z\\)\\'"
69 (or (and unzip-p zipped
)
70 (and (not unzip-p
) (not zipped
)))))))))
73 (defun pcomplete/bzip2
()
74 "Completion for `bzip2'."
75 (pcomplete-opt "hdzkftcqvLVs123456789")
76 (while (pcomplete-here
77 (pcmpl-gnu-bzipped-files
79 (let ((args pcomplete-args
))
81 (if (string-match "\\`-.*[dt]" (car args
))
82 (throw 'has-d-flag t
))
83 (setq args
(cdr args
)))))))))
85 (defun pcmpl-gnu-bzipped-files (unzip-p)
86 "Find all zipped or unzipped files: the inverse of UNZIP-P."
91 (when (and (file-readable-p entry
)
92 (file-regular-p entry
))
93 (let ((zipped (string-match "\\.\\(t?z2\\|bz2\\)\\'" entry
)))
94 (or (and unzip-p zipped
)
95 (and (not unzip-p
) (not zipped
)))))))))
98 (defun pcomplete/make
()
99 "Completion for GNU `make'."
100 (let ((pcomplete-help "(make)Top"))
101 (pcomplete-opt "bmC/def(pcmpl-gnu-makefile-names)hiI/j?kl?no.pqrsStvwW.")
102 (while (pcomplete-here (completion-table-in-turn
103 (pcmpl-gnu-make-rule-names)
107 (defun pcmpl-gnu-makefile-names ()
108 "Return a list of possible makefile names."
109 (pcomplete-entries (mapconcat 'identity pcmpl-gnu-makefile-regexps
"\\|")))
111 (defun pcmpl-gnu-make-rule-names ()
112 "Return a list of possible make rule names in MAKEFILE."
113 (let* ((minus-f (member "-f" pcomplete-args
))
114 (makefile (or (cadr minus-f
)
116 ((file-exists-p "GNUmakefile") "GNUmakefile")
117 ((file-exists-p "makefile") "makefile")
120 (if (not (file-readable-p makefile
))
121 (unless minus-f
(list "-f"))
123 (ignore-errors ;Could be a directory or something.
124 (insert-file-contents makefile
))
125 (while (re-search-forward
126 (concat "^\\s-*\\([^\n#%.$][^:=\n]*\\)\\s-*:[^=]") nil t
)
127 (setq rules
(append (split-string (match-string 1)) rules
))))
128 (pcomplete-uniqify-list rules
))))
130 (defcustom pcmpl-gnu-tarfile-regexp
131 "\\.t\\(ar\\(\\.\\(gz\\|bz2\\|Z\\|xz\\)\\)?\\|gz\\|a[zZ]\\|z2\\)\\'"
132 "A regexp which matches any tar archive."
133 :version
"24.3" ; added xz
137 ;; Only used in tar-mode buffers.
138 (defvar tar-parse-info
)
139 (declare-function tar-header-name
"tar-mode" t t
)
141 (defmacro pcmpl-gnu-with-file-buffer
(file &rest body
)
142 "Run BODY inside a buffer visiting FILE."
143 (declare (debug t
) (indent 1))
144 (let ((exist (make-symbol "exist"))
145 (filesym (make-symbol "file"))
146 (buf (make-symbol "buf")))
147 `(let* ((,filesym
,file
)
148 (,exist
(find-buffer-visiting ,filesym
))
149 (,buf
(or ,exist
(find-file-noselect ,filesym
))))
151 (with-current-buffer ,buf
153 (when (and (not ,exist
) (buffer-live-p ,buf
))
154 (kill-buffer ,buf
))))))
157 (defun pcomplete/tar
()
158 "Completion for the GNU tar utility."
159 ;; options that end in an equal sign will want further completion...
160 (let (saw-option complete-within
)
161 (let ((pcomplete-suffix-list (if (boundp 'pcomplete-suffix-list
)
162 (cons ?
= pcomplete-suffix-list
))))
163 (while (pcomplete-match "^-" 0)
165 (if (pcomplete-match "^--" 0)
166 (if (pcomplete-match "^--\\([^= \t\n\f]*\\)\\'" 0)
167 ;; FIXME: Extract this list from "tar --help".
197 "--ignore-failed-read"
205 "--listed-incremental"
207 "--modification-time"
209 "--new-volume-script="
222 "--preserve-permissions"
223 "--read-full-records"
241 "--use-compress-program="
246 (pcomplete-opt "01234567ABCFGKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz"))
248 ((pcomplete-match "\\`-\\'" 0)
250 ((pcomplete-match "\\`--after-date=" 0)
252 ((pcomplete-match "\\`--backup=" 0)
254 ((pcomplete-match "\\`--blocking-factor=" 0)
256 ((pcomplete-match "\\`--directory=\\(.*\\)" 0)
257 (pcomplete-here* (pcomplete-dirs)
258 (pcomplete-match-string 1 0)))
259 ((pcomplete-match "\\`--exclude-from=\\(.*\\)" 0)
260 (pcomplete-here* (pcomplete-entries)
261 (pcomplete-match-string 1 0)))
262 ((pcomplete-match "\\`--exclude=" 0)
264 ((pcomplete-match "\\`--\\(extract\\|list\\)\\'" 0)
265 (setq complete-within t
))
266 ((pcomplete-match "\\`--file=\\(.*\\)" 0)
267 (pcomplete-here* (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp
)
268 (pcomplete-match-string 1 0)))
269 ((pcomplete-match "\\`--files-from=\\(.*\\)" 0)
270 (pcomplete-here* (pcomplete-entries)
271 (pcomplete-match-string 1 0)))
272 ((pcomplete-match "\\`--group=\\(.*\\)" 0)
273 (pcomplete-here* (pcmpl-unix-group-names)
274 (pcomplete-match-string 1 0)))
275 ((pcomplete-match "\\`--info-script=\\(.*\\)" 0)
276 (pcomplete-here* (pcomplete-entries)
277 (pcomplete-match-string 1 0)))
278 ((pcomplete-match "\\`--label=" 0)
280 ((pcomplete-match "\\`--mode=" 0)
282 ((pcomplete-match "\\`--new-volume-script=\\(.*\\)" 0)
283 (pcomplete-here* (pcomplete-entries)
284 (pcomplete-match-string 1 0)))
285 ((pcomplete-match "\\`--newer=" 0)
287 ((pcomplete-match "\\`--owner=\\(.*\\)" 0)
288 (pcomplete-here* (pcmpl-unix-user-names)
289 (pcomplete-match-string 1 0)))
290 ((pcomplete-match "\\`--record-size=" 0)
292 ((pcomplete-match "\\`--rsh-command=\\(.*\\)" 0)
293 (pcomplete-here* (funcall pcomplete-command-completion-function
)
294 (pcomplete-match-string 1 0)))
295 ((pcomplete-match "\\`--starting-file=\\(.*\\)" 0)
296 (pcomplete-here* (pcomplete-entries)
297 (pcomplete-match-string 1 0)))
298 ((pcomplete-match "\\`--suffix=" 0)
300 ((pcomplete-match "\\`--tape-length=" 0)
302 ((pcomplete-match "\\`--use-compress-program=\\(.*\\)" 0)
303 (pcomplete-here* (funcall pcomplete-command-completion-function
)
304 (pcomplete-match-string 1 0)))
305 ((pcomplete-match "\\`--volno-file=\\(.*\\)" 0)
306 (pcomplete-here* (pcomplete-entries)
307 (pcomplete-match-string 1 0))))))
310 (mapcar 'char-to-string
312 "01234567ABCFGIKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz")))
313 (if (pcomplete-match "[xt]" 'first
1)
314 (setq complete-within t
)))
315 (pcomplete-here (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp
))
316 (while (pcomplete-here
317 (if (and complete-within
318 (let* ((fa (file-attributes (pcomplete-arg 1)))
321 (or (null large-file-warning-threshold
)
322 (< size large-file-warning-threshold
)))))
323 (let ((file (pcomplete-arg 1)))
324 (completion-table-dynamic
326 (pcmpl-gnu-with-file-buffer file
327 (mapcar #'tar-header-name tar-parse-info
)))))
332 (defalias 'pcomplete
/gdb
'pcomplete
/xargs
)
334 ;;; pcmpl-gnu.el ends here