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