Fix bug #13515 with processing DBCS file names on MS-Windows.
[emacs.git] / lisp / pcmpl-gnu.el
blob91b146fdc788b88cc0fad35f02462ba99a79abcb
1 ;;; pcmpl-gnu.el --- completions for GNU project tools -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2013 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 (provide 'pcmpl-gnu)
28 (require 'pcomplete)
29 (require 'pcmpl-unix)
31 (defgroup pcmpl-gnu nil
32 "Completions for GNU project tools."
33 :group 'pcomplete)
35 ;; User Variables:
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)
41 :group 'pcmpl-gnu)
43 ;; Functions:
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))))))))))
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)))))))))
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)))))))))
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)))))))))
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 (completion-table-in-turn
103 (pcmpl-gnu-make-rule-names)
104 (pcomplete-entries))
105 nil 'identity))))
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)
115 (cond
116 ((file-exists-p "GNUmakefile") "GNUmakefile")
117 ((file-exists-p "makefile") "makefile")
118 (t "Makefile"))))
119 rules)
120 (if (not (file-readable-p makefile))
121 (unless minus-f (list "-f"))
122 (with-temp-buffer
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
134 :type 'regexp
135 :group 'pcmpl-gnu)
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))))
150 (unwind-protect
151 (with-current-buffer ,buf
152 ,@body)
153 (when (and (not ,exist) (buffer-live-p ,buf))
154 (kill-buffer ,buf))))))
156 ;;;###autoload
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 (cons ?= pcomplete-suffix-list)))
162 (while (pcomplete-match "^-" 0)
163 (setq saw-option t)
164 (if (pcomplete-match "^--" 0)
165 (if (pcomplete-match "^--\\([^= \t\n\f]*\\)\\'" 0)
166 ;; FIXME: Extract this list from "tar --help".
167 (pcomplete-here*
168 '("--absolute-names"
169 "--after-date="
170 "--append"
171 "--atime-preserve"
172 "--backup"
173 "--block-number"
174 "--blocking-factor="
175 "--catenate"
176 "--checkpoint"
177 "--compare"
178 "--compress"
179 "--concatenate"
180 "--confirmation"
181 "--create"
182 "--delete"
183 "--dereference"
184 "--diff"
185 "--directory="
186 "--exclude="
187 "--exclude-from="
188 "--extract"
189 "--file="
190 "--files-from="
191 "--force-local"
192 "--get"
193 "--group="
194 "--gzip"
195 "--help"
196 "--ignore-failed-read"
197 "--ignore-zeros"
198 "--incremental"
199 "--info-script="
200 "--interactive"
201 "--keep-old-files"
202 "--label="
203 "--list"
204 "--listed-incremental"
205 "--mode="
206 "--modification-time"
207 "--multi-volume"
208 "--new-volume-script="
209 "--newer="
210 "--newer-mtime"
211 "--no-recursion"
212 "--null"
213 "--numeric-owner"
214 "--old-archive"
215 "--one-file-system"
216 "--owner="
217 "--portability"
218 "--posix"
219 "--preserve"
220 "--preserve-order"
221 "--preserve-permissions"
222 "--read-full-records"
223 "--record-size="
224 "--recursive-unlink"
225 "--remove-files"
226 "--rsh-command="
227 "--same-order"
228 "--same-owner"
229 "--same-permissions"
230 "--sparse"
231 "--starting-file="
232 "--suffix="
233 "--tape-length="
234 "--to-stdout"
235 "--totals"
236 "--uncompress"
237 "--ungzip"
238 "--unlink-first"
239 "--update"
240 "--use-compress-program="
241 "--verbose"
242 "--verify"
243 "--version"
244 "--volno-file=")))
245 (pcomplete-opt "01234567ABCFGKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz"))
246 (cond
247 ((pcomplete-match "\\`-\\'" 0)
248 (pcomplete-here*))
249 ((pcomplete-match "\\`--after-date=" 0)
250 (pcomplete-here*))
251 ((pcomplete-match "\\`--backup=" 0)
252 (pcomplete-here*))
253 ((pcomplete-match "\\`--blocking-factor=" 0)
254 (pcomplete-here*))
255 ((pcomplete-match "\\`--directory=\\(.*\\)" 0)
256 (pcomplete-here* (pcomplete-dirs)
257 (pcomplete-match-string 1 0)))
258 ((pcomplete-match "\\`--exclude-from=\\(.*\\)" 0)
259 (pcomplete-here* (pcomplete-entries)
260 (pcomplete-match-string 1 0)))
261 ((pcomplete-match "\\`--exclude=" 0)
262 (pcomplete-here*))
263 ((pcomplete-match "\\`--\\(extract\\|list\\)\\'" 0)
264 (setq complete-within t))
265 ((pcomplete-match "\\`--file=\\(.*\\)" 0)
266 (pcomplete-here* (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp)
267 (pcomplete-match-string 1 0)))
268 ((pcomplete-match "\\`--files-from=\\(.*\\)" 0)
269 (pcomplete-here* (pcomplete-entries)
270 (pcomplete-match-string 1 0)))
271 ((pcomplete-match "\\`--group=\\(.*\\)" 0)
272 (pcomplete-here* (pcmpl-unix-group-names)
273 (pcomplete-match-string 1 0)))
274 ((pcomplete-match "\\`--info-script=\\(.*\\)" 0)
275 (pcomplete-here* (pcomplete-entries)
276 (pcomplete-match-string 1 0)))
277 ((pcomplete-match "\\`--label=" 0)
278 (pcomplete-here*))
279 ((pcomplete-match "\\`--mode=" 0)
280 (pcomplete-here*))
281 ((pcomplete-match "\\`--new-volume-script=\\(.*\\)" 0)
282 (pcomplete-here* (pcomplete-entries)
283 (pcomplete-match-string 1 0)))
284 ((pcomplete-match "\\`--newer=" 0)
285 (pcomplete-here*))
286 ((pcomplete-match "\\`--owner=\\(.*\\)" 0)
287 (pcomplete-here* (pcmpl-unix-user-names)
288 (pcomplete-match-string 1 0)))
289 ((pcomplete-match "\\`--record-size=" 0)
290 (pcomplete-here*))
291 ((pcomplete-match "\\`--rsh-command=\\(.*\\)" 0)
292 (pcomplete-here* (funcall pcomplete-command-completion-function)
293 (pcomplete-match-string 1 0)))
294 ((pcomplete-match "\\`--starting-file=\\(.*\\)" 0)
295 (pcomplete-here* (pcomplete-entries)
296 (pcomplete-match-string 1 0)))
297 ((pcomplete-match "\\`--suffix=" 0)
298 (pcomplete-here*))
299 ((pcomplete-match "\\`--tape-length=" 0)
300 (pcomplete-here*))
301 ((pcomplete-match "\\`--use-compress-program=\\(.*\\)" 0)
302 (pcomplete-here* (funcall pcomplete-command-completion-function)
303 (pcomplete-match-string 1 0)))
304 ((pcomplete-match "\\`--volno-file=\\(.*\\)" 0)
305 (pcomplete-here* (pcomplete-entries)
306 (pcomplete-match-string 1 0))))))
307 (unless saw-option
308 (pcomplete-here
309 (mapcar 'char-to-string
310 (string-to-list
311 "01234567ABCFGIKLMNOPRSTUVWXZbcdfghiklmoprstuvwxz")))
312 (if (pcomplete-match "[xt]" 'first 1)
313 (setq complete-within t)))
314 (pcomplete-here (pcomplete-dirs-or-entries pcmpl-gnu-tarfile-regexp))
315 (while (pcomplete-here
316 (if (and complete-within
317 (let* ((fa (file-attributes (pcomplete-arg 1)))
318 (size (nth 7 fa)))
319 (and (numberp size)
320 (or (null large-file-warning-threshold)
321 (< size large-file-warning-threshold)))))
322 (let ((file (pcomplete-arg 1)))
323 (completion-table-dynamic
324 (lambda (_string)
325 (pcmpl-gnu-with-file-buffer file
326 (mapcar #'tar-header-name tar-parse-info)))))
327 (pcomplete-entries))
328 nil 'identity))))
330 ;;;###autoload
331 (defalias 'pcomplete/gdb 'pcomplete/xargs)
333 ;;; pcmpl-gnu.el ends here