Commit | Line | Data |
---|---|---|
b0e2675c | 1 | ;;; pcmpl-gnu.el --- completions for GNU project tools -*- lexical-binding: t -*- |
4fa9f636 | 2 | |
73b0cd50 | 3 | ;; Copyright (C) 1999-2011 Free Software Foundation, Inc. |
4fa9f636 | 4 | |
bd78fa1d CY |
5 | ;; Package: pcomplete |
6 | ||
4fa9f636 GM |
7 | ;; This file is part of GNU Emacs. |
8 | ||
eb3fa2cf | 9 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
4fa9f636 | 10 | ;; it under the terms of the GNU General Public License as published by |
eb3fa2cf GM |
11 | ;; the Free Software Foundation, either version 3 of the License, or |
12 | ;; (at your option) any later version. | |
4fa9f636 GM |
13 | |
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. | |
18 | ||
19 | ;; You should have received a copy of the GNU General Public License | |
eb3fa2cf | 20 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
4fa9f636 | 21 | |
60370d40 PJ |
22 | ;;; Commentary: |
23 | ||
4fa9f636 GM |
24 | ;;; Code: |
25 | ||
26 | (provide 'pcmpl-gnu) | |
27 | ||
28 | (require 'pcomplete) | |
29 | (require 'pcmpl-unix) | |
30 | ||
31 | (defgroup pcmpl-gnu nil | |
32 | "Completions for GNU project tools." | |
33 | :group 'pcomplete) | |
34 | ||
35 | ;; User Variables: | |
36 | ||
37 | (defcustom pcmpl-gnu-makefile-regexps | |
ca7aae91 | 38 | '("\\`GNUmakefile" "\\`Makefile" "\\.mak\\'") |
9201cc28 | 39 | "A list of regexps that will match Makefile names." |
4fa9f636 GM |
40 | :type '(repeat regexp) |
41 | :group 'pcmpl-gnu) | |
42 | ||
43 | ;; Functions: | |
44 | ||
45 | ;;;###autoload | |
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 | |
52 | (catch 'has-d-flag | |
53 | (let ((args pcomplete-args)) | |
54 | (while args | |
55 | (if (string-match "\\`-.*[dt]" (car args)) | |
56 | (throw 'has-d-flag t)) | |
57 | (setq args (cdr args)))))))))) | |
58 | ||
59 | (defun pcmpl-gnu-zipped-files (unzip-p) | |
60 | "Find all zipped or unzipped files: the inverse of UNZIP-P." | |
61 | (pcomplete-entries | |
62 | nil | |
63 | (function | |
64 | (lambda (entry) | |
65 | (when (and (file-readable-p entry) | |
66 | (file-regular-p entry)) | |
67 | (let ((zipped (string-match "\\.\\(t?gz\\|\\(ta\\)?Z\\)\\'" | |
68 | entry))) | |
69 | (or (and unzip-p zipped) | |
70 | (and (not unzip-p) (not zipped))))))))) | |
71 | ||
72 | ;;;###autoload | |
73 | (defun pcomplete/bzip2 () | |
74 | "Completion for `bzip2'." | |
75 | (pcomplete-opt "hdzkftcqvLVs123456789") | |
76 | (while (pcomplete-here | |
77 | (pcmpl-gnu-bzipped-files | |
78 | (catch 'has-d-flag | |
79 | (let ((args pcomplete-args)) | |
80 | (while args | |
81 | (if (string-match "\\`-.*[dt]" (car args)) | |
82 | (throw 'has-d-flag t)) | |
83 | (setq args (cdr args))))))))) | |
84 | ||
85 | (defun pcmpl-gnu-bzipped-files (unzip-p) | |
86 | "Find all zipped or unzipped files: the inverse of UNZIP-P." | |
87 | (pcomplete-entries | |
88 | nil | |
89 | (function | |
90 | (lambda (entry) | |
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))))))))) | |
96 | ||
97 | ;;;###autoload | |
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 (pcmpl-gnu-make-rule-names) nil 'identity)))) | |
103 | ||
104 | (defun pcmpl-gnu-makefile-names () | |
105 | "Return a list of possible makefile names." | |
56b14058 | 106 | (pcomplete-entries (mapconcat 'identity pcmpl-gnu-makefile-regexps "\\|"))) |
4fa9f636 GM |
107 | |
108 | (defun pcmpl-gnu-make-rule-names () | |
109 | "Return a list of possible make rule names in MAKEFILE." | |
110 | (let* ((minus-f (member "-f" pcomplete-args)) | |
ca7aae91 JW |
111 | (makefile (or (cadr minus-f) |
112 | (if (file-exists-p "GNUmakefile") | |
113 | "GNUmakefile" | |
114 | "Makefile"))) | |
4fa9f636 GM |
115 | rules) |
116 | (if (not (file-readable-p makefile)) | |
117 | (unless minus-f (list "-f")) | |
118 | (with-temp-buffer | |
119 | (insert-file-contents-literally makefile) | |
120 | (while (re-search-forward | |
121 | (concat "^\\s-*\\([^\n#%.$][^:=\n]*\\)\\s-*:[^=]") nil t) | |
122 | (setq rules (append (split-string (match-string 1)) rules)))) | |
123 | (pcomplete-uniqify-list rules)))) | |
124 | ||
125 | (defcustom pcmpl-gnu-tarfile-regexp | |
126 | "\\.t\\(ar\\(\\.\\(gz\\|bz2\\|Z\\)\\)?\\|gz\\|a[zZ]\\|z2\\)\\'" | |
9201cc28 | 127 | "A regexp which matches any tar archive." |
4fa9f636 GM |
128 | :type 'regexp |
129 | :group 'pcmpl-gnu) | |
130 | ||
113b8dcc GM |
131 | ;; Only used in tar-mode buffers. |
132 | (defvar tar-parse-info) | |
133 | (declare-function tar-header-name "tar-mode" t t) | |
134 | ||
428fe61a SM |
135 | (defmacro pcmpl-gnu-with-file-buffer (file &rest body) |
136 | "Run BODY inside a buffer visiting FILE." | |
137 | (declare (debug t) (indent 1)) | |
138 | (let ((exist (make-symbol "exist")) | |
139 | (filesym (make-symbol "file")) | |
140 | (buf (make-symbol "buf"))) | |
141 | `(let* ((,filesym ,file) | |
142 | (,exist (find-buffer-visiting ,filesym)) | |
143 | (,buf (or ,exist (find-file-noselect ,filesym)))) | |
144 | (unwind-protect | |
145 | (with-current-buffer ,buf | |
146 | ,@body) | |
147 | (when (and (not ,exist) (buffer-live-p ,buf)) | |
148 | (kill-buffer ,buf)))))) | |
149 | ||
4fa9f636 GM |
150 | ;;;###autoload |
151 | (defun pcomplete/tar () | |
152 | "Completion for the GNU tar utility." | |
153 | ;; options that end in an equal sign will want further completion... | |
154 | (let (saw-option complete-within) | |
428fe61a SM |
155 | (let ((pcomplete-suffix-list (cons ?= pcomplete-suffix-list))) |
156 | (while (pcomplete-match "^-" 0) | |
157 | (setq saw-option t) | |
158 | (if (pcomplete-match "^--" 0) | |
159 | (if (pcomplete-match "^--\\([^= \t\n\f]*\\)\\'" 0) | |
160 | ;; FIXME: Extract this list from "tar --help". | |
161 | (pcomplete-here* | |
162 | '("--absolute-names" | |
163 | "--after-date=" | |
164 | "--append" | |
165 | "--atime-preserve" | |
166 | "--backup" | |
167 | "--block-number" | |
168 | "--blocking-factor=" | |
169 | "--catenate" | |
170 | "--checkpoint" | |
171 | "--compare" | |
172 | "--compress" | |
173 | "--concatenate" | |
174 | "--confirmation" | |
175 | "--create" | |
176 | "--delete" | |
177 | "--dereference" | |
178 | "--diff" | |
179 | "--directory=" | |
180 | "--exclude=" | |
181 | "--exclude-from=" | |
182 | "--extract" | |
183 | "--file=" | |
184 | "--files-from=" | |
185 | "--force-local" | |
186 | "--get" | |
187 | "--group=" | |
188 | "--gzip" | |
189 | "--help" | |
190 | "--ignore-failed-read" | |
191 | "--ignore-zeros" | |
192 | "--incremental" | |
193 | "--info-script=" | |
194 | "--interactive" | |
195 | "--keep-old-files" | |
196 | "--label=" | |
197 | "--list" | |
198 | "--listed-incremental" | |
199 | "--mode=" | |
200 | "--modification-time" | |
201 | "--multi-volume" | |
202 | "--new-volume-script=" | |
203 | "--newer=" | |
204 | "--newer-mtime" | |
205 | "--no-recursion" | |
206 | "--null" | |
207 | "--numeric-owner" | |
208 | "--old-archive" | |
209 | "--one-file-system" | |
210 | "--owner=" | |
211 | "--portability" | |
212 | "--posix" | |
213 | "--preserve" | |
214 | "--preserve-order" | |
215 | "--preserve-permissions" | |
216 | "--read-full-records" | |
217 | "--record-size=" | |
218 | "--recursive-unlink" | |
219 | "--remove-files" | |
220 | "--rsh-command=" | |
221 | "--same-order" | |
222 | "--same-owner" | |
223 | "--same-permissions" | |
224 | "--sparse" | |
225 | "--starting-file=" | |
226 | "--suffix=" | |
227 | "--tape-length=" | |
228 | "--to-stdout" | |
229 | "--totals" | |
230 | "--uncompress" | |
231 | "--ungzip" | |
232 | "--unlink-first" | |
233 | "--update" | |
234 | "--use-compress-program=" | |
235 | "--verbose" | |
236 | "--verify" | |
237 | "--version" | |
238 | "--volno-file="))) | |
239 | (pcomplete-opt "01234567ABCFGKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz")) | |
240 | (cond | |
241 | ((pcomplete-match "\\`--after-date=" 0) | |
242 | (pcomplete-here*)) | |
243 | ((pcomplete-match "\\`--backup=" 0) | |
244 | (pcomplete-here*)) | |
245 | ((pcomplete-match "\\`--blocking-factor=" 0) | |
246 | (pcomplete-here*)) | |
247 | ((pcomplete-match "\\`--directory=\\(.*\\)" 0) | |
248 | (pcomplete-here* (pcomplete-dirs) | |
249 | (pcomplete-match-string 1 0))) | |
250 | ((pcomplete-match "\\`--exclude-from=\\(.*\\)" 0) | |
251 | (pcomplete-here* (pcomplete-entries) | |
252 | (pcomplete-match-string 1 0))) | |
253 | ((pcomplete-match "\\`--exclude=" 0) | |
254 | (pcomplete-here*)) | |
255 | ((pcomplete-match "\\`--\\(extract\\|list\\)\\'" 0) | |
256 | (setq complete-within t)) | |
257 | ((pcomplete-match "\\`--file=\\(.*\\)" 0) | |
258 | (pcomplete-here* (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp) | |
259 | (pcomplete-match-string 1 0))) | |
260 | ((pcomplete-match "\\`--files-from=\\(.*\\)" 0) | |
261 | (pcomplete-here* (pcomplete-entries) | |
262 | (pcomplete-match-string 1 0))) | |
263 | ((pcomplete-match "\\`--group=\\(.*\\)" 0) | |
264 | (pcomplete-here* (pcmpl-unix-group-names) | |
265 | (pcomplete-match-string 1 0))) | |
266 | ((pcomplete-match "\\`--info-script=\\(.*\\)" 0) | |
267 | (pcomplete-here* (pcomplete-entries) | |
268 | (pcomplete-match-string 1 0))) | |
269 | ((pcomplete-match "\\`--label=" 0) | |
270 | (pcomplete-here*)) | |
271 | ((pcomplete-match "\\`--mode=" 0) | |
272 | (pcomplete-here*)) | |
273 | ((pcomplete-match "\\`--new-volume-script=\\(.*\\)" 0) | |
274 | (pcomplete-here* (pcomplete-entries) | |
275 | (pcomplete-match-string 1 0))) | |
276 | ((pcomplete-match "\\`--newer=" 0) | |
277 | (pcomplete-here*)) | |
278 | ((pcomplete-match "\\`--owner=\\(.*\\)" 0) | |
279 | (pcomplete-here* (pcmpl-unix-user-names) | |
280 | (pcomplete-match-string 1 0))) | |
281 | ((pcomplete-match "\\`--record-size=" 0) | |
282 | (pcomplete-here*)) | |
283 | ((pcomplete-match "\\`--rsh-command=\\(.*\\)" 0) | |
284 | (pcomplete-here* (funcall pcomplete-command-completion-function) | |
285 | (pcomplete-match-string 1 0))) | |
286 | ((pcomplete-match "\\`--starting-file=\\(.*\\)" 0) | |
287 | (pcomplete-here* (pcomplete-entries) | |
288 | (pcomplete-match-string 1 0))) | |
289 | ((pcomplete-match "\\`--suffix=" 0) | |
290 | (pcomplete-here*)) | |
291 | ((pcomplete-match "\\`--tape-length=" 0) | |
292 | (pcomplete-here*)) | |
293 | ((pcomplete-match "\\`--use-compress-program=\\(.*\\)" 0) | |
294 | (pcomplete-here* (funcall pcomplete-command-completion-function) | |
295 | (pcomplete-match-string 1 0))) | |
296 | ((pcomplete-match "\\`--volno-file=\\(.*\\)" 0) | |
297 | (pcomplete-here* (pcomplete-entries) | |
298 | (pcomplete-match-string 1 0)))))) | |
4fa9f636 GM |
299 | (unless saw-option |
300 | (pcomplete-here | |
301 | (mapcar 'char-to-string | |
302 | (string-to-list | |
303 | "01234567ABCFGIKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz"))) | |
304 | (if (pcomplete-match "[xt]" 'first 1) | |
305 | (setq complete-within t))) | |
306 | (pcomplete-here (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp)) | |
4fa9f636 | 307 | (while (pcomplete-here |
428fe61a SM |
308 | (if (and complete-within |
309 | (let* ((fa (file-attributes (pcomplete-arg 1))) | |
310 | (size (nth 7 fa))) | |
311 | (and (numberp size) | |
99c79fee SM |
312 | (or (null large-file-warning-threshold) |
313 | (< size large-file-warning-threshold))))) | |
b0e2675c SM |
314 | (let ((file (pcomplete-arg 1))) |
315 | (completion-table-dynamic | |
316 | (lambda (_string) | |
317 | (pcmpl-gnu-with-file-buffer file | |
318 | (mapcar #'tar-header-name tar-parse-info))))) | |
4fa9f636 GM |
319 | (pcomplete-entries)) |
320 | nil 'identity)))) | |
321 | ||
322 | ;;;###autoload | |
323 | (defalias 'pcomplete/gdb 'pcomplete/xargs) | |
324 | ||
325 | ;;; pcmpl-gnu.el ends here |