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