Commit | Line | Data |
---|---|---|
1c6d8c76 | 1 | ;;; pcomplete.el --- programmable completion -*- lexical-binding: t -*- |
affbf647 | 2 | |
acaf905b | 3 | ;; Copyright (C) 1999-2012 Free Software Foundation, Inc. |
affbf647 GM |
4 | |
5 | ;; Author: John Wiegley <johnw@gnu.org> | |
5751b8f9 | 6 | ;; Keywords: processes abbrev |
affbf647 GM |
7 | |
8 | ;; This file is part of GNU Emacs. | |
9 | ||
eb3fa2cf | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
affbf647 | 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. | |
affbf647 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/>. |
affbf647 GM |
22 | |
23 | ;;; Commentary: | |
24 | ||
25 | ;; This module provides a programmable completion facility using | |
26 | ;; "completion functions". Each completion function is responsible | |
27 | ;; for producing a list of possible completions relevant to the current | |
28 | ;; argument position. | |
29 | ;; | |
30 | ;; To use pcomplete with shell-mode, for example, you will need the | |
31 | ;; following in your .emacs file: | |
32 | ;; | |
affbf647 GM |
33 | ;; (add-hook 'shell-mode-hook 'pcomplete-shell-setup) |
34 | ;; | |
35 | ;; Most of the code below simply provides support mechanisms for | |
36 | ;; writing completion functions. Completion functions themselves are | |
37 | ;; very easy to write. They have few requirements beyond those of | |
38 | ;; regular Lisp functions. | |
39 | ;; | |
40 | ;; Consider the following example, which will complete against | |
41 | ;; filenames for the first two arguments, and directories for all | |
42 | ;; remaining arguments: | |
43 | ;; | |
44 | ;; (defun pcomplete/my-command () | |
45 | ;; (pcomplete-here (pcomplete-entries)) | |
46 | ;; (pcomplete-here (pcomplete-entries)) | |
47 | ;; (while (pcomplete-here (pcomplete-dirs)))) | |
48 | ;; | |
49 | ;; Here are the requirements for completion functions: | |
50 | ;; | |
51 | ;; @ They must be called "pcomplete/MAJOR-MODE/NAME", or | |
52 | ;; "pcomplete/NAME". This is how they are looked up, using the NAME | |
53 | ;; specified in the command argument (the argument in first | |
54 | ;; position). | |
55 | ;; | |
56 | ;; @ They must be callable with no arguments. | |
57 | ;; | |
58 | ;; @ Their return value is ignored. If they actually return normally, | |
59 | ;; it means no completions were available. | |
60 | ;; | |
61 | ;; @ In order to provide completions, they must throw the tag | |
3b067af1 SM |
62 | ;; `pcomplete-completions'. The value must be a completion table |
63 | ;; (i.e. a table that can be passed to try-completion and friends) | |
64 | ;; for the final argument. | |
affbf647 GM |
65 | ;; |
66 | ;; @ To simplify completion function logic, the tag `pcompleted' may | |
67 | ;; be thrown with a value of nil in order to abort the function. It | |
68 | ;; means that there were no completions available. | |
69 | ;; | |
70 | ;; When a completion function is called, the variable `pcomplete-args' | |
71 | ;; is in scope, and contains all of the arguments specified on the | |
72 | ;; command line. The variable `pcomplete-last' is the index of the | |
73 | ;; last argument in that list. | |
74 | ;; | |
75 | ;; The variable `pcomplete-index' is used by the completion code to | |
76 | ;; know which argument the completion function is currently examining. | |
77 | ;; It always begins at 1, meaning the first argument after the command | |
78 | ;; name. | |
79 | ;; | |
80 | ;; To facilitate writing completion logic, a special macro, | |
81 | ;; `pcomplete-here', has been provided which does several things: | |
82 | ;; | |
83 | ;; 1. It will throw `pcompleted' (with a value of nil) whenever | |
84 | ;; `pcomplete-index' exceeds `pcomplete-last'. | |
85 | ;; | |
86 | ;; 2. It will increment `pcomplete-index' if the final argument has | |
87 | ;; not been reached yet. | |
88 | ;; | |
89 | ;; 3. It will evaluate the form passed to it, and throw the result | |
90 | ;; using the `pcomplete-completions' tag, if it is called when | |
91 | ;; `pcomplete-index' is pointing to the final argument. | |
92 | ;; | |
93 | ;; Sometimes a completion function will want to vary the possible | |
94 | ;; completions for an argument based on the previous one. To | |
95 | ;; facilitate tests like this, the function `pcomplete-test' and | |
96 | ;; `pcomplete-match' are provided. Called with one argument, they | |
97 | ;; test the value of the previous command argument. Otherwise, a | |
98 | ;; relative index may be given as an optional second argument, where 0 | |
99 | ;; refers to the current argument, 1 the previous, 2 the one before | |
100 | ;; that, etc. The symbols `first' and `last' specify absolute | |
101 | ;; offsets. | |
102 | ;; | |
103 | ;; Here is an example which will only complete against directories for | |
104 | ;; the second argument if the first argument is also a directory: | |
105 | ;; | |
106 | ;; (defun pcomplete/example () | |
107 | ;; (pcomplete-here (pcomplete-entries)) | |
108 | ;; (if (pcomplete-test 'file-directory-p) | |
109 | ;; (pcomplete-here (pcomplete-dirs)) | |
110 | ;; (pcomplete-here (pcomplete-entries)))) | |
111 | ;; | |
112 | ;; For generating completion lists based on directory contents, see | |
113 | ;; the functions `pcomplete-entries', `pcomplete-dirs', | |
114 | ;; `pcomplete-executables' and `pcomplete-all-entries'. | |
115 | ;; | |
116 | ;; Consult the documentation for `pcomplete-here' for information | |
117 | ;; about its other arguments. | |
118 | ||
119 | ;;; Code: | |
120 | ||
3b067af1 | 121 | (eval-when-compile (require 'cl)) |
32c1fffd | 122 | (require 'comint) |
affbf647 GM |
123 | |
124 | (defgroup pcomplete nil | |
125 | "Programmable completion." | |
5751b8f9 | 126 | :version "21.1" |
affbf647 GM |
127 | :group 'processes) |
128 | ||
129 | ;;; User Variables: | |
130 | ||
131 | (defcustom pcomplete-file-ignore nil | |
9201cc28 | 132 | "A regexp of filenames to be disregarded during file completion." |
219227ea | 133 | :type '(choice regexp (const :tag "None" nil)) |
affbf647 GM |
134 | :group 'pcomplete) |
135 | ||
136 | (defcustom pcomplete-dir-ignore nil | |
9201cc28 | 137 | "A regexp of names to be disregarded during directory completion." |
219227ea | 138 | :type '(choice regexp (const :tag "None" nil)) |
affbf647 GM |
139 | :group 'pcomplete) |
140 | ||
c60ee5e7 | 141 | (defcustom pcomplete-ignore-case (memq system-type '(ms-dos windows-nt cygwin)) |
48feed59 SM |
142 | ;; FIXME: the doc mentions file-name completion, but the code |
143 | ;; seems to apply it to all completions. | |
9201cc28 | 144 | "If non-nil, ignore case when doing filename completion." |
affbf647 GM |
145 | :type 'boolean |
146 | :group 'pcomplete) | |
147 | ||
148 | (defcustom pcomplete-autolist nil | |
9201cc28 | 149 | "If non-nil, automatically list possibilities on partial completion. |
affbf647 GM |
150 | This mirrors the optional behavior of tcsh." |
151 | :type 'boolean | |
152 | :group 'pcomplete) | |
153 | ||
58cc447b | 154 | (defcustom pcomplete-suffix-list (list ?/ ?:) |
9201cc28 | 155 | "A list of characters which constitute a proper suffix." |
affbf647 GM |
156 | :type '(repeat character) |
157 | :group 'pcomplete) | |
1c6d8c76 | 158 | (make-obsolete-variable 'pcomplete-suffix-list nil "24.1") |
affbf647 GM |
159 | |
160 | (defcustom pcomplete-recexact nil | |
9201cc28 | 161 | "If non-nil, use shortest completion if characters cannot be added. |
affbf647 GM |
162 | This mirrors the optional behavior of tcsh. |
163 | ||
164 | A non-nil value is useful if `pcomplete-autolist' is non-nil too." | |
165 | :type 'boolean | |
166 | :group 'pcomplete) | |
167 | ||
b4ff4f1f SM |
168 | (define-obsolete-variable-alias |
169 | 'pcomplete-arg-quote-list 'comint-file-name-quote-list "24.2") | |
affbf647 GM |
170 | |
171 | (defcustom pcomplete-man-function 'man | |
9201cc28 | 172 | "A function to that will be called to display a manual page. |
affbf647 GM |
173 | It will be passed the name of the command to document." |
174 | :type 'function | |
175 | :group 'pcomplete) | |
176 | ||
177 | (defcustom pcomplete-compare-entry-function 'string-lessp | |
9201cc28 | 178 | "This function is used to order file entries for completion. |
affbf647 GM |
179 | The behavior of most all shells is to sort alphabetically." |
180 | :type '(radio (function-item string-lessp) | |
181 | (function-item file-newer-than-file-p) | |
182 | (function :tag "Other")) | |
183 | :group 'pcomplete) | |
184 | ||
185 | (defcustom pcomplete-help nil | |
9201cc28 | 186 | "A string or function (or nil) used for context-sensitive help. |
affbf647 GM |
187 | If a string, it should name an Info node that will be jumped to. |
188 | If non-nil, it must a sexp that will be evaluated, and whose | |
189 | result will be shown in the minibuffer. | |
190 | If nil, the function `pcomplete-man-function' will be called with the | |
191 | current command argument." | |
192 | :type '(choice string sexp (const :tag "Use man page" nil)) | |
193 | :group 'pcomplete) | |
194 | ||
195 | (defcustom pcomplete-expand-before-complete nil | |
9201cc28 | 196 | "If non-nil, expand the current argument before completing it. |
affbf647 GM |
197 | This means that typing something such as '$HOME/bi' followed by |
198 | \\[pcomplete-argument] will cause the variable reference to be | |
199 | resolved first, and the resultant value that will be completed against | |
200 | to be inserted in the buffer. Note that exactly what gets expanded | |
201 | and how is entirely up to the behavior of the | |
202 | `pcomplete-parse-arguments-function'." | |
203 | :type 'boolean | |
204 | :group 'pcomplete) | |
205 | ||
206 | (defcustom pcomplete-parse-arguments-function | |
207 | 'pcomplete-parse-buffer-arguments | |
9201cc28 | 208 | "A function to call to parse the current line's arguments. |
affbf647 GM |
209 | It should be called with no parameters, and with point at the position |
210 | of the argument that is to be completed. | |
211 | ||
212 | It must either return nil, or a cons cell of the form: | |
213 | ||
214 | ((ARG...) (BEG-POS...)) | |
215 | ||
216 | The two lists must be identical in length. The first gives the final | |
217 | value of each command line argument (which need not match the textual | |
218 | representation of that argument), and BEG-POS gives the beginning | |
219 | position of each argument, as it is seen by the user. The establishes | |
220 | a relationship between the fully resolved value of the argument, and | |
221 | the textual representation of the argument." | |
222 | :type 'function | |
223 | :group 'pcomplete) | |
224 | ||
225 | (defcustom pcomplete-cycle-completions t | |
9201cc28 | 226 | "If non-nil, hitting the TAB key cycles through the completion list. |
affbf647 GM |
227 | Typical Emacs behavior is to complete as much as possible, then pause |
228 | waiting for further input. Then if TAB is hit again, show a list of | |
229 | possible completions. When `pcomplete-cycle-completions' is non-nil, | |
230 | it acts more like zsh or 4nt, showing the first maximal match first, | |
231 | followed by any further matches on each subsequent pressing of the TAB | |
232 | key. \\[pcomplete-list] is the key to press if the user wants to see | |
233 | the list of possible completions." | |
234 | :type 'boolean | |
235 | :group 'pcomplete) | |
236 | ||
237 | (defcustom pcomplete-cycle-cutoff-length 5 | |
9201cc28 | 238 | "If the number of completions is greater than this, don't cycle. |
affbf647 GM |
239 | This variable is a compromise between the traditional Emacs style of |
240 | completion, and the \"cycling\" style. Basically, if there are more | |
241 | than this number of completions possible, don't automatically pick the | |
242 | first one and then expect the user to press TAB to cycle through them. | |
243 | Typically, when there are a large number of completion possibilities, | |
244 | the user wants to see them in a list buffer so that they can know what | |
245 | options are available. But if the list is small, it means the user | |
246 | has already entered enough input to disambiguate most of the | |
247 | possibilities, and therefore they are probably most interested in | |
248 | cycling through the candidates. Set this value to nil if you want | |
249 | cycling to always be enabled." | |
250 | :type '(choice integer (const :tag "Always cycle" nil)) | |
251 | :group 'pcomplete) | |
252 | ||
253 | (defcustom pcomplete-restore-window-delay 1 | |
9201cc28 | 254 | "The number of seconds to wait before restoring completion windows. |
affbf647 GM |
255 | Once the completion window has been displayed, if the user then goes |
256 | on to type something else, that completion window will be removed from | |
257 | the display (actually, the original window configuration before it was | |
258 | displayed will be restored), after this many seconds of idle time. If | |
259 | set to nil, completion windows will be left on second until the user | |
260 | removes them manually. If set to 0, they will disappear immediately | |
261 | after the user enters a key other than TAB." | |
262 | :type '(choice integer (const :tag "Never restore" nil)) | |
263 | :group 'pcomplete) | |
264 | ||
265 | (defcustom pcomplete-try-first-hook nil | |
9201cc28 | 266 | "A list of functions which are called before completing an argument. |
affbf647 GM |
267 | This can be used, for example, for completing things which might apply |
268 | to all arguments, such as variable names after a $." | |
269 | :type 'hook | |
270 | :group 'pcomplete) | |
271 | ||
004a00f4 DN |
272 | (defsubst pcomplete-executables (&optional regexp) |
273 | "Complete amongst a list of directories and executables." | |
274 | (pcomplete-entries regexp 'file-executable-p)) | |
275 | ||
affbf647 GM |
276 | (defcustom pcomplete-command-completion-function |
277 | (function | |
278 | (lambda () | |
279 | (pcomplete-here (pcomplete-executables)))) | |
9201cc28 | 280 | "Function called for completing the initial command argument." |
affbf647 GM |
281 | :type 'function |
282 | :group 'pcomplete) | |
283 | ||
284 | (defcustom pcomplete-command-name-function 'pcomplete-command-name | |
9201cc28 | 285 | "Function called for determining the current command name." |
affbf647 GM |
286 | :type 'function |
287 | :group 'pcomplete) | |
288 | ||
289 | (defcustom pcomplete-default-completion-function | |
290 | (function | |
291 | (lambda () | |
292 | (while (pcomplete-here (pcomplete-entries))))) | |
9201cc28 | 293 | "Function called when no completion rule can be found. |
affbf647 GM |
294 | This function is used to generate completions for every argument." |
295 | :type 'function | |
296 | :group 'pcomplete) | |
297 | ||
ca7aae91 | 298 | (defcustom pcomplete-use-paring t |
9201cc28 | 299 | "If t, pare alternatives that have already been used. |
ca7aae91 JW |
300 | If nil, you will always see the completion set of possible options, no |
301 | matter which of those options have already been used in previous | |
302 | command arguments." | |
303 | :type 'boolean | |
304 | :group 'pcomplete) | |
305 | ||
150158c4 | 306 | (defcustom pcomplete-termination-string " " |
9201cc28 | 307 | "A string that is inserted after any completion or expansion. |
150158c4 JW |
308 | This is usually a space character, useful when completing lists of |
309 | words separated by spaces. However, if your list uses a different | |
310 | separator character, or if the completion occurs in a word that is | |
311 | already terminated by a character, this variable should be locally | |
312 | modified to be an empty string, or the desired separation string." | |
313 | :type 'string | |
314 | :group 'pcomplete) | |
315 | ||
affbf647 GM |
316 | ;;; Internal Variables: |
317 | ||
318 | ;; for cycling completion support | |
319 | (defvar pcomplete-current-completions nil) | |
320 | (defvar pcomplete-last-completion-length) | |
321 | (defvar pcomplete-last-completion-stub) | |
322 | (defvar pcomplete-last-completion-raw) | |
323 | (defvar pcomplete-last-window-config nil) | |
324 | (defvar pcomplete-window-restore-timer nil) | |
325 | ||
326 | (make-variable-buffer-local 'pcomplete-current-completions) | |
327 | (make-variable-buffer-local 'pcomplete-last-completion-length) | |
328 | (make-variable-buffer-local 'pcomplete-last-completion-stub) | |
329 | (make-variable-buffer-local 'pcomplete-last-completion-raw) | |
330 | (make-variable-buffer-local 'pcomplete-last-window-config) | |
331 | (make-variable-buffer-local 'pcomplete-window-restore-timer) | |
332 | ||
333 | ;; used for altering pcomplete's behavior. These global variables | |
334 | ;; should always be nil. | |
335 | (defvar pcomplete-show-help nil) | |
336 | (defvar pcomplete-show-list nil) | |
337 | (defvar pcomplete-expand-only-p nil) | |
338 | ||
06b60517 JB |
339 | ;; for the sake of the bye-compiler, when compiling other files that |
340 | ;; contain completion functions | |
341 | (defvar pcomplete-args nil) | |
342 | (defvar pcomplete-begins nil) | |
343 | (defvar pcomplete-last nil) | |
344 | (defvar pcomplete-index nil) | |
345 | (defvar pcomplete-stub nil) | |
346 | (defvar pcomplete-seen nil) | |
347 | (defvar pcomplete-norm-func nil) | |
348 | ||
affbf647 GM |
349 | ;;; User Functions: |
350 | ||
2d085307 SM |
351 | ;;; Alternative front-end using the standard completion facilities. |
352 | ||
353 | ;; The way pcomplete-parse-arguments, pcomplete-stub, and | |
354 | ;; pcomplete-quote-argument work only works because of some deep | |
355 | ;; hypothesis about the way the completion work. Basically, it makes | |
356 | ;; it pretty much impossible to have completion other than | |
357 | ;; prefix-completion. | |
358 | ;; | |
b4ff4f1f SM |
359 | ;; pcomplete--common-suffix and completion-table-subvert try to work around |
360 | ;; this difficulty with heuristics, but it's really a hack. | |
361 | ||
362 | (defvar pcomplete-unquote-argument-function #'comint--unquote-argument) | |
363 | ||
364 | (defsubst pcomplete-unquote-argument (s) | |
365 | (funcall pcomplete-unquote-argument-function s)) | |
366 | ||
367 | (defvar pcomplete-requote-argument-function #'comint--requote-argument) | |
368 | ||
369 | (defun pcomplete--common-suffix (s1 s2) | |
370 | ;; Since S2 is expected to be the "unquoted/expanded" version of S1, | |
371 | ;; there shouldn't be any case difference, even if the completion is | |
372 | ;; case-insensitive. | |
373 | (let ((case-fold-search nil)) | |
374 | (string-match | |
375 | ;; \x3FFF7F is just an arbitrary char among the ones Emacs accepts | |
376 | ;; that hopefully will never appear in normal text. | |
377 | "\\(?:.\\|\n\\)*?\\(\\(?:.\\|\n\\)*\\)\x3FFF7F\\(?:.\\|\n\\)*\\1\\'" | |
378 | (concat s1 "\x3FFF7F" s2)) | |
379 | (- (match-end 1) (match-beginning 1)))) | |
380 | ||
c26ea4b2 | 381 | (defun pcomplete-completions-at-point () |
3b067af1 SM |
382 | "Provide standard completion using pcomplete's completion tables. |
383 | Same as `pcomplete' but using the standard completion UI." | |
0667de21 SM |
384 | ;; FIXME: it only completes the text before point, whereas the |
385 | ;; standard UI may also consider text after point. | |
c26ea4b2 SM |
386 | ;; FIXME: the `pcomplete' UI may be used internally during |
387 | ;; pcomplete-completions and then throw to `pcompleted', thus | |
388 | ;; imposing the pcomplete UI over the standard UI. | |
3b067af1 SM |
389 | (catch 'pcompleted |
390 | (let* ((pcomplete-stub) | |
391 | pcomplete-seen pcomplete-norm-func | |
392 | pcomplete-args pcomplete-last pcomplete-index | |
393 | (pcomplete-autolist pcomplete-autolist) | |
394 | (pcomplete-suffix-list pcomplete-suffix-list) | |
395 | ;; Apparently the vars above are global vars modified by | |
396 | ;; side-effects, whereas pcomplete-completions is the core | |
397 | ;; function that finds the chunk of text to complete | |
398 | ;; (returned indirectly in pcomplete-stub) and the set of | |
399 | ;; possible completions. | |
400 | (completions (pcomplete-completions)) | |
48feed59 SM |
401 | ;; Usually there's some close connection between pcomplete-stub |
402 | ;; and the text before point. But depending on what | |
403 | ;; pcomplete-parse-arguments-function does, that connection | |
404 | ;; might not be that close. E.g. in eshell, | |
405 | ;; pcomplete-parse-arguments-function expands envvars. | |
06b60517 | 406 | ;; |
48feed59 SM |
407 | ;; Since we use minibuffer-complete, which doesn't know |
408 | ;; pcomplete-stub and works from the buffer's text instead, | |
409 | ;; we need to trick minibuffer-complete, into using | |
410 | ;; pcomplete-stub without its knowledge. To that end, we | |
b4ff4f1f | 411 | ;; use completion-table-subvert to construct a completion |
48feed59 SM |
412 | ;; table which expects strings using a prefix from the |
413 | ;; buffer's text but internally uses the corresponding | |
414 | ;; prefix from pcomplete-stub. | |
415 | (beg (max (- (point) (length pcomplete-stub)) | |
2d085307 | 416 | (pcomplete-begin))) |
b4ff4f1f SM |
417 | (buftext (pcomplete-unquote-argument |
418 | (buffer-substring beg (point))))) | |
08abfaad SM |
419 | (when completions |
420 | (let ((table | |
b4ff4f1f SM |
421 | (completion-table-with-quoting |
422 | (if (equal pcomplete-stub buftext) | |
423 | completions | |
424 | ;; This may not always be strictly right, but given the lack | |
425 | ;; of any other info, it's about as good as it gets, and in | |
426 | ;; practice it should work just fine (fingers crossed). | |
427 | (let ((suf-len (pcomplete--common-suffix | |
08abfaad | 428 | pcomplete-stub buftext))) |
b4ff4f1f SM |
429 | (completion-table-subvert |
430 | completions | |
431 | (substring buftext 0 (- (length buftext) suf-len)) | |
432 | (substring pcomplete-stub 0 | |
433 | (- (length pcomplete-stub) suf-len))))) | |
434 | pcomplete-unquote-argument-function | |
435 | pcomplete-requote-argument-function)) | |
08abfaad SM |
436 | (pred |
437 | ;; Pare it down, if applicable. | |
438 | (when (and pcomplete-use-paring pcomplete-seen) | |
1c6d8c76 SM |
439 | ;; Capture the dynbound values for later use. |
440 | (let ((norm-func pcomplete-norm-func) | |
78054a46 SM |
441 | (seen |
442 | (mapcar (lambda (f) | |
443 | (funcall pcomplete-norm-func | |
444 | (directory-file-name f))) | |
445 | pcomplete-seen))) | |
1c6d8c76 SM |
446 | (lambda (f) |
447 | (not (member | |
448 | (funcall norm-func (directory-file-name f)) | |
449 | seen))))))) | |
08abfaad | 450 | (when pcomplete-ignore-case |
3dc61a09 | 451 | (setq table (completion-table-case-fold table))) |
a2a25d24 SM |
452 | (list beg (point) table |
453 | :predicate pred | |
454 | :exit-function | |
455 | (unless (zerop (length pcomplete-termination-string)) | |
456 | (lambda (_s finished) | |
457 | (when (memq finished '(sole finished)) | |
458 | (if (looking-at | |
459 | (regexp-quote pcomplete-termination-string)) | |
460 | (goto-char (match-end 0)) | |
461 | (insert pcomplete-termination-string))))))))))) | |
c26ea4b2 SM |
462 | |
463 | ;; I don't think such commands are usable before first setting up buffer-local | |
464 | ;; variables to parse args, so there's no point autoloading it. | |
465 | ;; ;;;###autoload | |
466 | (defun pcomplete-std-complete () | |
20e0e05f | 467 | (let ((data (pcomplete-completions-at-point))) |
14a7fbd8 SM |
468 | (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data) |
469 | (plist-get :predicate (nthcdr 3 data))))) | |
3b067af1 | 470 | |
2d085307 SM |
471 | ;;; Pcomplete's native UI. |
472 | ||
473 | ;;;###autoload | |
474 | (defun pcomplete (&optional interactively) | |
475 | "Support extensible programmable completion. | |
476 | To use this function, just bind the TAB key to it, or add it to your | |
477 | completion functions list (it should occur fairly early in the list)." | |
478 | (interactive "p") | |
479 | (if (and interactively | |
480 | pcomplete-cycle-completions | |
481 | pcomplete-current-completions | |
482 | (memq last-command '(pcomplete | |
483 | pcomplete-expand-and-complete | |
484 | pcomplete-reverse))) | |
485 | (progn | |
d355a0b7 | 486 | (delete-char (- pcomplete-last-completion-length)) |
2d085307 SM |
487 | (if (eq this-command 'pcomplete-reverse) |
488 | (progn | |
0667de21 SM |
489 | (push (car (last pcomplete-current-completions)) |
490 | pcomplete-current-completions) | |
2d085307 SM |
491 | (setcdr (last pcomplete-current-completions 2) nil)) |
492 | (nconc pcomplete-current-completions | |
493 | (list (car pcomplete-current-completions))) | |
494 | (setq pcomplete-current-completions | |
495 | (cdr pcomplete-current-completions))) | |
496 | (pcomplete-insert-entry pcomplete-last-completion-stub | |
497 | (car pcomplete-current-completions) | |
498 | nil pcomplete-last-completion-raw)) | |
499 | (setq pcomplete-current-completions nil | |
500 | pcomplete-last-completion-raw nil) | |
501 | (catch 'pcompleted | |
502 | (let* ((pcomplete-stub) | |
503 | pcomplete-seen pcomplete-norm-func | |
504 | pcomplete-args pcomplete-last pcomplete-index | |
505 | (pcomplete-autolist pcomplete-autolist) | |
506 | (pcomplete-suffix-list pcomplete-suffix-list) | |
507 | (completions (pcomplete-completions)) | |
508 | (result (pcomplete-do-complete pcomplete-stub completions))) | |
509 | (and result | |
510 | (not (eq (car result) 'listed)) | |
511 | (cdr result) | |
512 | (pcomplete-insert-entry pcomplete-stub (cdr result) | |
513 | (memq (car result) | |
514 | '(sole shortest)) | |
515 | pcomplete-last-completion-raw)))))) | |
516 | ||
affbf647 GM |
517 | ;;;###autoload |
518 | (defun pcomplete-reverse () | |
519 | "If cycling completion is in use, cycle backwards." | |
520 | (interactive) | |
521 | (call-interactively 'pcomplete)) | |
522 | ||
523 | ;;;###autoload | |
524 | (defun pcomplete-expand-and-complete () | |
525 | "Expand the textual value of the current argument. | |
526 | This will modify the current buffer." | |
527 | (interactive) | |
528 | (let ((pcomplete-expand-before-complete t)) | |
529 | (pcomplete))) | |
530 | ||
531 | ;;;###autoload | |
532 | (defun pcomplete-continue () | |
533 | "Complete without reference to any cycling completions." | |
534 | (interactive) | |
535 | (setq pcomplete-current-completions nil | |
536 | pcomplete-last-completion-raw nil) | |
537 | (call-interactively 'pcomplete)) | |
538 | ||
539 | ;;;###autoload | |
540 | (defun pcomplete-expand () | |
541 | "Expand the textual value of the current argument. | |
542 | This will modify the current buffer." | |
543 | (interactive) | |
544 | (let ((pcomplete-expand-before-complete t) | |
545 | (pcomplete-expand-only-p t)) | |
546 | (pcomplete) | |
547 | (when (and pcomplete-current-completions | |
3b067af1 | 548 | (> (length pcomplete-current-completions) 0)) ;?? |
d355a0b7 | 549 | (delete-char (- pcomplete-last-completion-length)) |
affbf647 GM |
550 | (while pcomplete-current-completions |
551 | (unless (pcomplete-insert-entry | |
552 | "" (car pcomplete-current-completions) t | |
3b067af1 | 553 | pcomplete-last-completion-raw) |
150158c4 | 554 | (insert-and-inherit pcomplete-termination-string)) |
affbf647 GM |
555 | (setq pcomplete-current-completions |
556 | (cdr pcomplete-current-completions)))))) | |
557 | ||
558 | ;;;###autoload | |
559 | (defun pcomplete-help () | |
560 | "Display any help information relative to the current argument." | |
561 | (interactive) | |
562 | (let ((pcomplete-show-help t)) | |
563 | (pcomplete))) | |
564 | ||
565 | ;;;###autoload | |
566 | (defun pcomplete-list () | |
567 | "Show the list of possible completions for the current argument." | |
568 | (interactive) | |
569 | (when (and pcomplete-cycle-completions | |
570 | pcomplete-current-completions | |
571 | (eq last-command 'pcomplete-argument)) | |
d355a0b7 | 572 | (delete-char (- pcomplete-last-completion-length)) |
affbf647 GM |
573 | (setq pcomplete-current-completions nil |
574 | pcomplete-last-completion-raw nil)) | |
575 | (let ((pcomplete-show-list t)) | |
576 | (pcomplete))) | |
577 | ||
578 | ;;; Internal Functions: | |
579 | ||
580 | ;; argument handling | |
affbf647 GM |
581 | (defun pcomplete-arg (&optional index offset) |
582 | "Return the textual content of the INDEXth argument. | |
583 | INDEX is based from the current processing position. If INDEX is | |
584 | positive, values returned are closer to the command argument; if | |
585 | negative, they are closer to the last argument. If the INDEX is | |
586 | outside of the argument list, nil is returned. The default value for | |
587 | INDEX is 0, meaning the current argument being examined. | |
588 | ||
589 | The special indices `first' and `last' may be used to access those | |
590 | parts of the list. | |
591 | ||
592 | The OFFSET argument is added to/taken away from the index that will be | |
593 | used. This is really only useful with `first' and `last', for | |
594 | accessing absolute argument positions." | |
595 | (setq index | |
596 | (if (eq index 'first) | |
597 | 0 | |
598 | (if (eq index 'last) | |
599 | pcomplete-last | |
600 | (- pcomplete-index (or index 0))))) | |
601 | (if offset | |
602 | (setq index (+ index offset))) | |
603 | (nth index pcomplete-args)) | |
604 | ||
605 | (defun pcomplete-begin (&optional index offset) | |
606 | "Return the beginning position of the INDEXth argument. | |
607 | See the documentation for `pcomplete-arg'." | |
608 | (setq index | |
609 | (if (eq index 'first) | |
610 | 0 | |
611 | (if (eq index 'last) | |
612 | pcomplete-last | |
613 | (- pcomplete-index (or index 0))))) | |
614 | (if offset | |
615 | (setq index (+ index offset))) | |
616 | (nth index pcomplete-begins)) | |
617 | ||
618 | (defsubst pcomplete-actual-arg (&optional index offset) | |
619 | "Return the actual text representation of the last argument. | |
21a2e05d | 620 | This is different from `pcomplete-arg', which returns the textual value |
affbf647 GM |
621 | that the last argument evaluated to. This function returns what the |
622 | user actually typed in." | |
623 | (buffer-substring (pcomplete-begin index offset) (point))) | |
624 | ||
625 | (defsubst pcomplete-next-arg () | |
626 | "Move the various pointers to the next argument." | |
627 | (setq pcomplete-index (1+ pcomplete-index) | |
628 | pcomplete-stub (pcomplete-arg)) | |
629 | (if (> pcomplete-index pcomplete-last) | |
630 | (progn | |
631 | (message "No completions") | |
632 | (throw 'pcompleted nil)))) | |
633 | ||
634 | (defun pcomplete-command-name () | |
635 | "Return the command name of the first argument." | |
636 | (file-name-nondirectory (pcomplete-arg 'first))) | |
637 | ||
638 | (defun pcomplete-match (regexp &optional index offset start) | |
639 | "Like `string-match', but on the current completion argument." | |
640 | (let ((arg (pcomplete-arg (or index 1) offset))) | |
641 | (if arg | |
642 | (string-match regexp arg start) | |
643 | (throw 'pcompleted nil)))) | |
644 | ||
645 | (defun pcomplete-match-string (which &optional index offset) | |
21a2e05d | 646 | "Like `match-string', but on the current completion argument." |
affbf647 GM |
647 | (let ((arg (pcomplete-arg (or index 1) offset))) |
648 | (if arg | |
649 | (match-string which arg) | |
650 | (throw 'pcompleted nil)))) | |
651 | ||
652 | (defalias 'pcomplete-match-beginning 'match-beginning) | |
653 | (defalias 'pcomplete-match-end 'match-end) | |
654 | ||
655 | (defsubst pcomplete--test (pred arg) | |
656 | "Perform a programmable completion predicate match." | |
657 | (and pred | |
658 | (cond ((eq pred t) t) | |
659 | ((functionp pred) | |
660 | (funcall pred arg)) | |
661 | ((stringp pred) | |
662 | (string-match (concat "^" pred "$") arg))) | |
663 | pred)) | |
664 | ||
665 | (defun pcomplete-test (predicates &optional index offset) | |
666 | "Predicates to test the current programmable argument with." | |
667 | (let ((arg (pcomplete-arg (or index 1) offset))) | |
668 | (unless (null predicates) | |
669 | (if (not (listp predicates)) | |
670 | (pcomplete--test predicates arg) | |
671 | (let ((pred predicates) | |
672 | found) | |
673 | (while (and pred (not found)) | |
674 | (setq found (pcomplete--test (car pred) arg) | |
675 | pred (cdr pred))) | |
676 | found))))) | |
677 | ||
678 | (defun pcomplete-parse-buffer-arguments () | |
679 | "Parse whitespace separated arguments in the current region." | |
680 | (let ((begin (point-min)) | |
681 | (end (point-max)) | |
682 | begins args) | |
683 | (save-excursion | |
684 | (goto-char begin) | |
685 | (while (< (point) end) | |
686 | (skip-chars-forward " \t\n") | |
0667de21 | 687 | (push (point) begins) |
affbf647 | 688 | (skip-chars-forward "^ \t\n") |
0667de21 SM |
689 | (push (buffer-substring-no-properties |
690 | (car begins) (point)) | |
691 | args)) | |
692 | (cons (nreverse args) (nreverse begins))))) | |
affbf647 GM |
693 | |
694 | ;;;###autoload | |
695 | (defun pcomplete-comint-setup (completef-sym) | |
696 | "Setup a comint buffer to use pcomplete. | |
697 | COMPLETEF-SYM should be the symbol where the | |
21a2e05d JB |
698 | dynamic-complete-functions are kept. For comint mode itself, |
699 | this is `comint-dynamic-complete-functions'." | |
affbf647 GM |
700 | (set (make-local-variable 'pcomplete-parse-arguments-function) |
701 | 'pcomplete-parse-comint-arguments) | |
1c6d8c76 SM |
702 | (add-hook 'completion-at-point-functions |
703 | 'pcomplete-completions-at-point nil 'local) | |
48feed59 SM |
704 | (set (make-local-variable completef-sym) |
705 | (copy-sequence (symbol-value completef-sym))) | |
208384c5 | 706 | (let* ((funs (symbol-value completef-sym)) |
8fff8daa SM |
707 | (elem (or (memq 'comint-filename-completion funs) |
708 | (memq 'shell-filename-completion funs) | |
709 | (memq 'shell-dynamic-complete-filename funs) | |
70f44c65 | 710 | (memq 'comint-dynamic-complete-filename funs)))) |
affbf647 GM |
711 | (if elem |
712 | (setcar elem 'pcomplete) | |
22eb1d41 | 713 | (add-to-list completef-sym 'pcomplete)))) |
affbf647 GM |
714 | |
715 | ;;;###autoload | |
716 | (defun pcomplete-shell-setup () | |
3b067af1 | 717 | "Setup `shell-mode' to use pcomplete." |
2d085307 | 718 | ;; FIXME: insufficient |
48feed59 | 719 | (pcomplete-comint-setup 'comint-dynamic-complete-functions)) |
affbf647 | 720 | |
004a00f4 DN |
721 | (declare-function comint-bol "comint" (&optional arg)) |
722 | ||
affbf647 GM |
723 | (defun pcomplete-parse-comint-arguments () |
724 | "Parse whitespace separated arguments in the current region." | |
725 | (let ((begin (save-excursion (comint-bol nil) (point))) | |
726 | (end (point)) | |
727 | begins args) | |
728 | (save-excursion | |
729 | (goto-char begin) | |
730 | (while (< (point) end) | |
731 | (skip-chars-forward " \t\n") | |
48feed59 | 732 | (push (point) begins) |
b3fd59bd SM |
733 | (while |
734 | (progn | |
735 | (skip-chars-forward "^ \t\n\\") | |
736 | (when (eq (char-after) ?\\) | |
737 | (forward-char 1) | |
738 | (unless (eolp) | |
739 | (forward-char 1) | |
740 | t)))) | |
48feed59 SM |
741 | (push (buffer-substring-no-properties (car begins) (point)) |
742 | args)) | |
743 | (cons (nreverse args) (nreverse begins))))) | |
b3fd59bd SM |
744 | (make-obsolete 'pcomplete-parse-comint-arguments |
745 | 'comint-parse-pcomplete-arguments "24.1") | |
affbf647 GM |
746 | |
747 | (defun pcomplete-parse-arguments (&optional expand-p) | |
748 | "Parse the command line arguments. Most completions need this info." | |
749 | (let ((results (funcall pcomplete-parse-arguments-function))) | |
750 | (when results | |
751 | (setq pcomplete-args (or (car results) (list "")) | |
752 | pcomplete-begins (or (cdr results) (list (point))) | |
753 | pcomplete-last (1- (length pcomplete-args)) | |
754 | pcomplete-index 0 | |
755 | pcomplete-stub (pcomplete-arg 'last)) | |
756 | (let ((begin (pcomplete-begin 'last))) | |
757 | (if (and pcomplete-cycle-completions | |
48feed59 | 758 | (listp pcomplete-stub) ;?? |
affbf647 | 759 | (not pcomplete-expand-only-p)) |
48feed59 | 760 | (let* ((completions pcomplete-stub) ;?? |
affbf647 GM |
761 | (common-stub (car completions)) |
762 | (c completions) | |
763 | (len (length common-stub))) | |
764 | (while (and c (> len 0)) | |
765 | (while (and (> len 0) | |
766 | (not (string= | |
767 | (substring common-stub 0 len) | |
768 | (substring (car c) 0 | |
769 | (min (length (car c)) | |
770 | len))))) | |
771 | (setq len (1- len))) | |
772 | (setq c (cdr c))) | |
773 | (setq pcomplete-stub (substring common-stub 0 len) | |
774 | pcomplete-autolist t) | |
775 | (when (and begin (not pcomplete-show-list)) | |
776 | (delete-region begin (point)) | |
777 | (pcomplete-insert-entry "" pcomplete-stub)) | |
778 | (throw 'pcomplete-completions completions)) | |
779 | (when expand-p | |
780 | (if (stringp pcomplete-stub) | |
781 | (when begin | |
782 | (delete-region begin (point)) | |
783 | (insert-and-inherit pcomplete-stub)) | |
784 | (if (and (listp pcomplete-stub) | |
785 | pcomplete-expand-only-p) | |
786 | ;; this is for the benefit of `pcomplete-expand' | |
787 | (setq pcomplete-last-completion-length (- (point) begin) | |
788 | pcomplete-current-completions pcomplete-stub) | |
789 | (error "Cannot expand argument")))) | |
790 | (if pcomplete-expand-only-p | |
791 | (throw 'pcompleted t) | |
792 | pcomplete-args)))))) | |
793 | ||
b4ff4f1f SM |
794 | (define-obsolete-function-alias |
795 | 'pcomplete-quote-argument #'comint-quote-filename "24.2") | |
affbf647 GM |
796 | |
797 | ;; file-system completion lists | |
798 | ||
799 | (defsubst pcomplete-dirs-or-entries (&optional regexp predicate) | |
800 | "Return either directories, or qualified entries." | |
3b067af1 SM |
801 | (pcomplete-entries |
802 | nil | |
1c6d8c76 SM |
803 | (lambda (f) |
804 | (or (file-directory-p f) | |
805 | (and (or (null regexp) (string-match regexp f)) | |
806 | (or (null predicate) (funcall predicate f))))))) | |
807 | ||
808 | (defun pcomplete--entries (&optional regexp predicate) | |
809 | "Like `pcomplete-entries' but without env-var handling." | |
810 | (let* ((ign-pred | |
811 | (when (or pcomplete-file-ignore pcomplete-dir-ignore) | |
812 | ;; Capture the dynbound value for later use. | |
813 | (let ((file-ignore pcomplete-file-ignore) | |
814 | (dir-ignore pcomplete-dir-ignore)) | |
815 | (lambda (file) | |
816 | (not | |
817 | (if (eq (aref file (1- (length file))) ?/) | |
818 | (and dir-ignore (string-match dir-ignore file)) | |
819 | (and file-ignore (string-match file-ignore file)))))))) | |
820 | (reg-pred (if regexp (lambda (file) (string-match regexp file)))) | |
821 | (pred (cond | |
822 | ((null (or ign-pred reg-pred)) predicate) | |
823 | ((null (or ign-pred predicate)) reg-pred) | |
824 | ((null (or reg-pred predicate)) ign-pred) | |
825 | (t (lambda (f) | |
826 | (and (or (null reg-pred) (funcall reg-pred f)) | |
827 | (or (null ign-pred) (funcall ign-pred f)) | |
828 | (or (null predicate) (funcall predicate f)))))))) | |
829 | (lambda (s p a) | |
830 | (if (and (eq a 'metadata) pcomplete-compare-entry-function) | |
831 | `(metadata (cycle-sort-function | |
832 | . ,(lambda (comps) | |
833 | (sort comps pcomplete-compare-entry-function))) | |
834 | ,@(cdr (completion-file-name-table s p a))) | |
835 | (let ((completion-ignored-extensions nil)) | |
836 | (completion-table-with-predicate | |
32c1fffd | 837 | #'comint-completion-file-name-table pred 'strict s p a)))))) |
1c6d8c76 SM |
838 | |
839 | (defconst pcomplete--env-regexp | |
840 | "\\(?:\\`\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(\\$\\(?:{\\([^}]+\\)}\\|\\(?2:[[:alnum:]_]+\\)\\)\\)") | |
affbf647 GM |
841 | |
842 | (defun pcomplete-entries (&optional regexp predicate) | |
843 | "Complete against a list of directory candidates. | |
affbf647 GM |
844 | If REGEXP is non-nil, it is a regular expression used to refine the |
845 | match (files not matching the REGEXP will be excluded). | |
846 | If PREDICATE is non-nil, it will also be used to refine the match | |
847 | \(files for which the PREDICATE returns nil will be excluded). | |
430190ba | 848 | If no directory information can be extracted from the completed |
21a2e05d | 849 | component, `default-directory' is used as the basis for completion." |
1c6d8c76 SM |
850 | ;; FIXME: The old code did env-var expansion here, so we reproduce this |
851 | ;; behavior for now, but really env-var handling should be performed globally | |
852 | ;; rather than here since it also applies to non-file arguments. | |
853 | (let ((table (pcomplete--entries regexp predicate))) | |
854 | (lambda (string pred action) | |
855 | (let ((strings nil) | |
856 | (orig-length (length string))) | |
857 | ;; Perform env-var expansion. | |
858 | (while (string-match pcomplete--env-regexp string) | |
859 | (push (substring string 0 (match-beginning 1)) strings) | |
860 | (push (getenv (match-string 2 string)) strings) | |
861 | (setq string (substring string (match-end 1)))) | |
862 | (if (not (and strings | |
863 | (or (eq action t) | |
864 | (eq (car-safe action) 'boundaries)))) | |
865 | (let ((newstring | |
866 | (mapconcat 'identity (nreverse (cons string strings)) ""))) | |
867 | ;; FIXME: We could also try to return unexpanded envvars. | |
868 | (complete-with-action action table newstring pred)) | |
869 | (let* ((envpos (apply #'+ (mapcar #' length strings))) | |
870 | (newstring | |
871 | (mapconcat 'identity (nreverse (cons string strings)) "")) | |
872 | (bounds (completion-boundaries newstring table pred | |
873 | (or (cdr-safe action) "")))) | |
874 | (if (>= (car bounds) envpos) | |
875 | ;; The env-var is "out of bounds". | |
876 | (if (eq action t) | |
877 | (complete-with-action action table newstring pred) | |
878 | (list* 'boundaries | |
879 | (+ (car bounds) (- orig-length (length newstring))) | |
880 | (cdr bounds))) | |
881 | ;; The env-var is in the file bounds. | |
882 | (if (eq action t) | |
883 | (let ((comps (complete-with-action | |
884 | action table newstring pred)) | |
885 | (len (- envpos (car bounds)))) | |
886 | ;; Strip the part of each completion that's actually | |
887 | ;; coming from the env-var. | |
888 | (mapcar (lambda (s) (substring s len)) comps)) | |
889 | (list* 'boundaries | |
890 | (+ envpos (- orig-length (length newstring))) | |
891 | (cdr bounds)))))))))) | |
affbf647 GM |
892 | |
893 | (defsubst pcomplete-all-entries (&optional regexp predicate) | |
894 | "Like `pcomplete-entries', but doesn't ignore any entries." | |
895 | (let (pcomplete-file-ignore | |
896 | pcomplete-dir-ignore) | |
897 | (pcomplete-entries regexp predicate))) | |
898 | ||
899 | (defsubst pcomplete-dirs (&optional regexp) | |
900 | "Complete amongst a list of directories." | |
901 | (pcomplete-entries regexp 'file-directory-p)) | |
902 | ||
affbf647 GM |
903 | ;; generation of completion lists |
904 | ||
905 | (defun pcomplete-find-completion-function (command) | |
906 | "Find the completion function to call for the given COMMAND." | |
907 | (let ((sym (intern-soft | |
908 | (concat "pcomplete/" (symbol-name major-mode) "/" command)))) | |
909 | (unless sym | |
910 | (setq sym (intern-soft (concat "pcomplete/" command)))) | |
911 | (and sym (fboundp sym) sym))) | |
912 | ||
913 | (defun pcomplete-completions () | |
914 | "Return a list of completions for the current argument position." | |
915 | (catch 'pcomplete-completions | |
916 | (when (pcomplete-parse-arguments pcomplete-expand-before-complete) | |
917 | (if (= pcomplete-index pcomplete-last) | |
918 | (funcall pcomplete-command-completion-function) | |
919 | (let ((sym (or (pcomplete-find-completion-function | |
920 | (funcall pcomplete-command-name-function)) | |
921 | pcomplete-default-completion-function))) | |
922 | (ignore | |
923 | (pcomplete-next-arg) | |
924 | (funcall sym))))))) | |
925 | ||
06b60517 | 926 | (defun pcomplete-opt (options &optional prefix _no-ganging _args-follow) |
affbf647 GM |
927 | "Complete a set of OPTIONS, each beginning with PREFIX (?- by default). |
928 | PREFIX may be t, in which case no PREFIX character is necessary. | |
21a2e05d JB |
929 | If NO-GANGING is non-nil, each option is separate (-xy is not allowed). |
930 | If ARGS-FOLLOW is non-nil, then options which take arguments may have | |
931 | the argument appear after a ganged set of options. This is how tar | |
06b60517 JB |
932 | behaves, for example. |
933 | Arguments NO-GANGING and ARGS-FOLLOW are currently ignored." | |
affbf647 GM |
934 | (if (and (= pcomplete-index pcomplete-last) |
935 | (string= (pcomplete-arg) "-")) | |
936 | (let ((len (length options)) | |
937 | (index 0) | |
938 | char choices) | |
939 | (while (< index len) | |
940 | (setq char (aref options index)) | |
941 | (if (eq char ?\() | |
942 | (let ((result (read-from-string options index))) | |
943 | (setq index (cdr result))) | |
944 | (unless (memq char '(?/ ?* ?? ?.)) | |
0667de21 | 945 | (push (char-to-string char) choices)) |
affbf647 GM |
946 | (setq index (1+ index)))) |
947 | (throw 'pcomplete-completions | |
948 | (mapcar | |
949 | (function | |
950 | (lambda (opt) | |
951 | (concat "-" opt))) | |
952 | (pcomplete-uniqify-list choices)))) | |
953 | (let ((arg (pcomplete-arg))) | |
954 | (when (and (> (length arg) 1) | |
955 | (stringp arg) | |
956 | (eq (aref arg 0) (or prefix ?-))) | |
957 | (pcomplete-next-arg) | |
958 | (let ((char (aref arg 1)) | |
959 | (len (length options)) | |
960 | (index 0) | |
961 | opt-char arg-char result) | |
962 | (while (< (1+ index) len) | |
963 | (setq opt-char (aref options index) | |
964 | arg-char (aref options (1+ index))) | |
965 | (if (eq arg-char ?\() | |
966 | (setq result | |
967 | (read-from-string options (1+ index)) | |
968 | index (cdr result) | |
969 | result (car result)) | |
970 | (setq result nil)) | |
971 | (when (and (eq char opt-char) | |
972 | (memq arg-char '(?\( ?/ ?* ?? ?.))) | |
973 | (if (< pcomplete-index pcomplete-last) | |
974 | (pcomplete-next-arg) | |
975 | (throw 'pcomplete-completions | |
976 | (cond ((eq arg-char ?/) (pcomplete-dirs)) | |
977 | ((eq arg-char ?*) (pcomplete-executables)) | |
978 | ((eq arg-char ??) nil) | |
979 | ((eq arg-char ?.) (pcomplete-entries)) | |
980 | ((eq arg-char ?\() (eval result)))))) | |
981 | (setq index (1+ index)))))))) | |
982 | ||
983 | (defun pcomplete--here (&optional form stub paring form-only) | |
21a2e05d | 984 | "Complete against the current argument, if at the end. |
affbf647 GM |
985 | See the documentation for `pcomplete-here'." |
986 | (if (< pcomplete-index pcomplete-last) | |
987 | (progn | |
988 | (if (eq paring 0) | |
989 | (setq pcomplete-seen nil) | |
990 | (unless (eq paring t) | |
991 | (let ((arg (pcomplete-arg))) | |
3b067af1 | 992 | (when (stringp arg) |
0667de21 SM |
993 | (push (if paring |
994 | (funcall paring arg) | |
995 | (file-truename arg)) | |
996 | pcomplete-seen))))) | |
affbf647 GM |
997 | (pcomplete-next-arg) |
998 | t) | |
999 | (when pcomplete-show-help | |
1000 | (pcomplete--help) | |
1001 | (throw 'pcompleted t)) | |
1002 | (if stub | |
1003 | (setq pcomplete-stub stub)) | |
1004 | (if (or (eq paring t) (eq paring 0)) | |
1005 | (setq pcomplete-seen nil) | |
1006 | (setq pcomplete-norm-func (or paring 'file-truename))) | |
1007 | (unless form-only | |
1008 | (run-hooks 'pcomplete-try-first-hook)) | |
3b067af1 SM |
1009 | (throw 'pcomplete-completions |
1010 | (if (functionp form) | |
1011 | (funcall form) | |
1012 | ;; Old calling convention, might still be used by files | |
1013 | ;; byte-compiled with the older code. | |
1014 | (eval form))))) | |
affbf647 GM |
1015 | |
1016 | (defmacro pcomplete-here (&optional form stub paring form-only) | |
21a2e05d | 1017 | "Complete against the current argument, if at the end. |
3b067af1 SM |
1018 | If completion is to be done here, evaluate FORM to generate the completion |
1019 | table which will be used for completion purposes. If STUB is a | |
affbf647 GM |
1020 | string, use it as the completion stub instead of the default (which is |
1021 | the entire text of the current argument). | |
1022 | ||
1023 | For an example of when you might want to use STUB: if the current | |
1024 | argument text is 'long-path-name/', you don't want the completions | |
1025 | list display to be cluttered by 'long-path-name/' appearing at the | |
1026 | beginning of every alternative. Not only does this make things less | |
3b067af1 | 1027 | intelligible, but it is also inefficient. Yet, if the completion list |
affbf647 GM |
1028 | does not begin with this string for every entry, the current argument |
1029 | won't complete correctly. | |
1030 | ||
1031 | The solution is to specify a relative stub. It allows you to | |
1032 | substitute a different argument from the current argument, almost | |
1033 | always for the sake of efficiency. | |
1034 | ||
1035 | If PARING is nil, this argument will be pared against previous | |
1036 | arguments using the function `file-truename' to normalize them. | |
21a2e05d JB |
1037 | PARING may be a function, in which case that function is used for |
1038 | normalization. If PARING is t, the argument dealt with by this | |
1039 | call will not participate in argument paring. If it is the | |
1040 | integer 0, all previous arguments that have been seen will be | |
1041 | cleared. | |
affbf647 GM |
1042 | |
1043 | If FORM-ONLY is non-nil, only the result of FORM will be used to | |
1044 | generate the completions list. This means that the hook | |
1045 | `pcomplete-try-first-hook' will not be run." | |
3b067af1 SM |
1046 | (declare (debug t)) |
1047 | `(pcomplete--here (lambda () ,form) ,stub ,paring ,form-only)) | |
1048 | ||
affbf647 GM |
1049 | |
1050 | (defmacro pcomplete-here* (&optional form stub form-only) | |
1051 | "An alternate form which does not participate in argument paring." | |
3b067af1 | 1052 | (declare (debug t)) |
5b31b787 | 1053 | `(pcomplete-here ,form ,stub t ,form-only)) |
affbf647 GM |
1054 | |
1055 | ;; display support | |
1056 | ||
1057 | (defun pcomplete-restore-windows () | |
1058 | "If the only window change was due to Completions, restore things." | |
1059 | (if pcomplete-last-window-config | |
1060 | (let* ((cbuf (get-buffer "*Completions*")) | |
1061 | (cwin (and cbuf (get-buffer-window cbuf)))) | |
ccb13f4d | 1062 | (when (window-live-p cwin) |
affbf647 GM |
1063 | (bury-buffer cbuf) |
1064 | (set-window-configuration pcomplete-last-window-config)))) | |
1065 | (setq pcomplete-last-window-config nil | |
1066 | pcomplete-window-restore-timer nil)) | |
1067 | ||
1068 | ;; Abstractions so that the code below will work for both Emacs 20 and | |
1069 | ;; XEmacs 21 | |
1070 | ||
a3269bc4 DN |
1071 | (defalias 'pcomplete-event-matches-key-specifier-p |
1072 | (if (featurep 'xemacs) | |
1073 | 'event-matches-key-specifier-p | |
1074 | 'eq)) | |
affbf647 | 1075 | |
2d8e5088 RS |
1076 | (defun pcomplete-read-event (&optional prompt) |
1077 | (if (fboundp 'read-event) | |
1078 | (read-event prompt) | |
affbf647 GM |
1079 | (aref (read-key-sequence prompt) 0))) |
1080 | ||
affbf647 GM |
1081 | (defun pcomplete-show-completions (completions) |
1082 | "List in help buffer sorted COMPLETIONS. | |
1083 | Typing SPC flushes the help buffer." | |
3b067af1 SM |
1084 | (when pcomplete-window-restore-timer |
1085 | (cancel-timer pcomplete-window-restore-timer) | |
1086 | (setq pcomplete-window-restore-timer nil)) | |
1087 | (unless pcomplete-last-window-config | |
1088 | (setq pcomplete-last-window-config (current-window-configuration))) | |
1089 | (with-output-to-temp-buffer "*Completions*" | |
1090 | (display-completion-list completions)) | |
1091 | (message "Hit space to flush") | |
1092 | (let (event) | |
1093 | (prog1 | |
1094 | (catch 'done | |
1095 | (while (with-current-buffer (get-buffer "*Completions*") | |
1096 | (setq event (pcomplete-read-event))) | |
1097 | (cond | |
1098 | ((pcomplete-event-matches-key-specifier-p event ?\s) | |
1099 | (set-window-configuration pcomplete-last-window-config) | |
1100 | (setq pcomplete-last-window-config nil) | |
1101 | (throw 'done nil)) | |
1102 | ((or (pcomplete-event-matches-key-specifier-p event 'tab) | |
1103 | ;; Needed on a terminal | |
1104 | (pcomplete-event-matches-key-specifier-p event 9)) | |
1105 | (let ((win (or (get-buffer-window "*Completions*" 0) | |
1106 | (display-buffer "*Completions*" | |
1107 | 'not-this-window)))) | |
1108 | (with-selected-window win | |
1109 | (if (pos-visible-in-window-p (point-max)) | |
1110 | (goto-char (point-min)) | |
1111 | (scroll-up)))) | |
1112 | (message "")) | |
1113 | (t | |
1114 | (setq unread-command-events (list event)) | |
1115 | (throw 'done nil))))) | |
1116 | (if (and pcomplete-last-window-config | |
1117 | pcomplete-restore-window-delay) | |
1118 | (setq pcomplete-window-restore-timer | |
1119 | (run-with-timer pcomplete-restore-window-delay nil | |
1120 | 'pcomplete-restore-windows)))))) | |
affbf647 GM |
1121 | |
1122 | ;; insert completion at point | |
1123 | ||
1124 | (defun pcomplete-insert-entry (stub entry &optional addsuffix raw-p) | |
1125 | "Insert a completion entry at point. | |
1126 | Returns non-nil if a space was appended at the end." | |
1127 | (let ((here (point))) | |
1128 | (if (not pcomplete-ignore-case) | |
1129 | (insert-and-inherit (if raw-p | |
1130 | (substring entry (length stub)) | |
b4ff4f1f | 1131 | (comint-quote-filename |
affbf647 GM |
1132 | (substring entry (length stub))))) |
1133 | ;; the stub is not quoted at this time, so to determine the | |
1134 | ;; length of what should be in the buffer, we must quote it | |
48feed59 SM |
1135 | ;; FIXME: Here we presume that quoting `stub' gives us the exact |
1136 | ;; text in the buffer before point, which is not guaranteed; | |
1137 | ;; e.g. it is not the case in eshell when completing ${FOO}tm[TAB]. | |
b4ff4f1f | 1138 | (delete-char (- (length (comint-quote-filename stub)))) |
affbf647 GM |
1139 | ;; if there is already a backslash present to handle the first |
1140 | ;; character, don't bother quoting it | |
1141 | (when (eq (char-before) ?\\) | |
1142 | (insert-and-inherit (substring entry 0 1)) | |
1143 | (setq entry (substring entry 1))) | |
1144 | (insert-and-inherit (if raw-p | |
1145 | entry | |
b4ff4f1f | 1146 | (comint-quote-filename entry)))) |
affbf647 GM |
1147 | (let (space-added) |
1148 | (when (and (not (memq (char-before) pcomplete-suffix-list)) | |
1149 | addsuffix) | |
150158c4 | 1150 | (insert-and-inherit pcomplete-termination-string) |
affbf647 GM |
1151 | (setq space-added t)) |
1152 | (setq pcomplete-last-completion-length (- (point) here) | |
1153 | pcomplete-last-completion-stub stub) | |
1154 | space-added))) | |
1155 | ||
b4ff4f1f | 1156 | ;; Selection of completions. |
affbf647 GM |
1157 | |
1158 | (defun pcomplete-do-complete (stub completions) | |
1159 | "Dynamically complete at point using STUB and COMPLETIONS. | |
1160 | This is basically just a wrapper for `pcomplete-stub' which does some | |
1161 | extra checking, and munging of the COMPLETIONS list." | |
1162 | (unless (stringp stub) | |
1163 | (message "Cannot complete argument") | |
1164 | (throw 'pcompleted nil)) | |
1165 | (if (null completions) | |
1166 | (ignore | |
1167 | (if (and stub (> (length stub) 0)) | |
1168 | (message "No completions of %s" stub) | |
1169 | (message "No completions"))) | |
1170 | ;; pare it down, if applicable | |
3b067af1 SM |
1171 | (when (and pcomplete-use-paring pcomplete-seen) |
1172 | (setq pcomplete-seen | |
1173 | (mapcar 'directory-file-name pcomplete-seen)) | |
1174 | (dolist (p pcomplete-seen) | |
1175 | (add-to-list 'pcomplete-seen | |
1176 | (funcall pcomplete-norm-func p))) | |
1177 | (setq completions | |
1178 | (apply-partially 'completion-table-with-predicate | |
1179 | completions | |
8fff8daa SM |
1180 | (when pcomplete-seen |
1181 | (lambda (f) | |
1182 | (not (member | |
1183 | (funcall pcomplete-norm-func | |
1184 | (directory-file-name f)) | |
1185 | pcomplete-seen)))) | |
3b067af1 | 1186 | 'strict))) |
affbf647 GM |
1187 | ;; OK, we've got a list of completions. |
1188 | (if pcomplete-show-list | |
3b067af1 SM |
1189 | ;; FIXME: pay attention to boundaries. |
1190 | (pcomplete-show-completions (all-completions stub completions)) | |
affbf647 GM |
1191 | (pcomplete-stub stub completions)))) |
1192 | ||
1193 | (defun pcomplete-stub (stub candidates &optional cycle-p) | |
1194 | "Dynamically complete STUB from CANDIDATES list. | |
1195 | This function inserts completion characters at point by completing | |
1196 | STUB from the strings in CANDIDATES. A completions listing may be | |
1197 | shown in a help buffer if completion is ambiguous. | |
1198 | ||
1199 | Returns nil if no completion was inserted. | |
1200 | Returns `sole' if completed with the only completion match. | |
1201 | Returns `shortest' if completed with the shortest of the matches. | |
1202 | Returns `partial' if completed as far as possible with the matches. | |
1203 | Returns `listed' if a completion listing was shown. | |
1204 | ||
1205 | See also `pcomplete-filename'." | |
1206 | (let* ((completion-ignore-case pcomplete-ignore-case) | |
3b067af1 SM |
1207 | (completions (all-completions stub candidates)) |
1208 | (entry (try-completion stub candidates)) | |
1209 | result) | |
1210 | (cond | |
1211 | ((null entry) | |
1212 | (if (and stub (> (length stub) 0)) | |
1213 | (message "No completions of %s" stub) | |
1214 | (message "No completions"))) | |
1215 | ((eq entry t) | |
1216 | (setq entry stub) | |
1217 | (message "Sole completion") | |
1218 | (setq result 'sole)) | |
1219 | ((= 1 (length completions)) | |
1220 | (setq result 'sole)) | |
1221 | ((and pcomplete-cycle-completions | |
1222 | (or cycle-p | |
1223 | (not pcomplete-cycle-cutoff-length) | |
1224 | (<= (length completions) | |
1225 | pcomplete-cycle-cutoff-length))) | |
1226 | (let ((bound (car (completion-boundaries stub candidates nil "")))) | |
1227 | (unless (zerop bound) | |
1228 | (setq completions (mapcar (lambda (c) (concat (substring stub 0 bound) c)) | |
1229 | completions))) | |
1230 | (setq entry (car completions) | |
1231 | pcomplete-current-completions completions))) | |
1232 | ((and pcomplete-recexact | |
1233 | (string-equal stub entry) | |
1234 | (member entry completions)) | |
1235 | ;; It's not unique, but user wants shortest match. | |
1236 | (message "Completed shortest") | |
1237 | (setq result 'shortest)) | |
1238 | ((or pcomplete-autolist | |
1239 | (string-equal stub entry)) | |
1240 | ;; It's not unique, list possible completions. | |
1241 | ;; FIXME: pay attention to boundaries. | |
1242 | (pcomplete-show-completions completions) | |
1243 | (setq result 'listed)) | |
1244 | (t | |
1245 | (message "Partially completed") | |
1246 | (setq result 'partial))) | |
1247 | (cons result entry))) | |
affbf647 GM |
1248 | |
1249 | ;; context sensitive help | |
1250 | ||
1251 | (defun pcomplete--help () | |
1252 | "Produce context-sensitive help for the current argument. | |
21a2e05d | 1253 | If specific documentation can't be given, be generic." |
affbf647 GM |
1254 | (if (and pcomplete-help |
1255 | (or (and (stringp pcomplete-help) | |
1256 | (fboundp 'Info-goto-node)) | |
1257 | (listp pcomplete-help))) | |
1258 | (if (listp pcomplete-help) | |
8a26c165 | 1259 | (message "%s" (eval pcomplete-help)) |
affbf647 GM |
1260 | (save-window-excursion (info)) |
1261 | (switch-to-buffer-other-window "*info*") | |
1262 | (funcall (symbol-function 'Info-goto-node) pcomplete-help)) | |
1263 | (if pcomplete-man-function | |
1264 | (let ((cmd (funcall pcomplete-command-name-function))) | |
1265 | (if (and cmd (> (length cmd) 0)) | |
1266 | (funcall pcomplete-man-function cmd))) | |
1267 | (message "No context-sensitive help available")))) | |
1268 | ||
1269 | ;; general utilities | |
1270 | ||
affbf647 GM |
1271 | (defun pcomplete-uniqify-list (l) |
1272 | "Sort and remove multiples in L." | |
1273 | (setq l (sort l 'string-lessp)) | |
1274 | (let ((m l)) | |
1275 | (while m | |
1276 | (while (and (cdr m) | |
1277 | (string= (car m) | |
1278 | (cadr m))) | |
1279 | (setcdr m (cddr m))) | |
1280 | (setq m (cdr m)))) | |
1281 | l) | |
1282 | ||
1283 | (defun pcomplete-process-result (cmd &rest args) | |
1284 | "Call CMD using `call-process' and return the simplest result." | |
1285 | (with-temp-buffer | |
1286 | (apply 'call-process cmd nil t nil args) | |
1287 | (skip-chars-backward "\n") | |
1288 | (buffer-substring (point-min) (point)))) | |
1289 | ||
1290 | ;; create a set of aliases which allow completion functions to be not | |
1291 | ;; quite so verbose | |
1292 | ||
3b067af1 SM |
1293 | ;;; jww (1999-10-20): are these a good idea? |
1294 | ;; (defalias 'pc-here 'pcomplete-here) | |
1295 | ;; (defalias 'pc-test 'pcomplete-test) | |
1296 | ;; (defalias 'pc-opt 'pcomplete-opt) | |
1297 | ;; (defalias 'pc-match 'pcomplete-match) | |
1298 | ;; (defalias 'pc-match-string 'pcomplete-match-string) | |
1299 | ;; (defalias 'pc-match-beginning 'pcomplete-match-beginning) | |
1300 | ;; (defalias 'pc-match-end 'pcomplete-match-end) | |
1301 | ||
1302 | (provide 'pcomplete) | |
affbf647 GM |
1303 | |
1304 | ;;; pcomplete.el ends here |