Comment
[emacs.git] / lisp / pcomplete.el
blobcad2ffb2a2cd414e5f8d4b2478f4cf46ba758fb8
1 ;;; pcomplete.el --- programmable completion -*- lexical-binding: t -*-
3 ;; Copyright (C) 1999-2012 Free Software Foundation, Inc.
5 ;; Author: John Wiegley <johnw@gnu.org>
6 ;; Keywords: processes abbrev
8 ;; This file is part of GNU Emacs.
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.
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.
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/>.
23 ;;; Commentary:
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.
30 ;; To use pcomplete with shell-mode, for example, you will need the
31 ;; following in your .emacs file:
33 ;; (add-hook 'shell-mode-hook 'pcomplete-shell-setup)
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.
40 ;; Consider the following example, which will complete against
41 ;; filenames for the first two arguments, and directories for all
42 ;; remaining arguments:
44 ;; (defun pcomplete/my-command ()
45 ;; (pcomplete-here (pcomplete-entries))
46 ;; (pcomplete-here (pcomplete-entries))
47 ;; (while (pcomplete-here (pcomplete-dirs))))
49 ;; Here are the requirements for completion functions:
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).
56 ;; @ They must be callable with no arguments.
58 ;; @ Their return value is ignored. If they actually return normally,
59 ;; it means no completions were available.
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.
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.
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.
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.
80 ;; To facilitate writing completion logic, a special macro,
81 ;; `pcomplete-here', has been provided which does several things:
83 ;; 1. It will throw `pcompleted' (with a value of nil) whenever
84 ;; `pcomplete-index' exceeds `pcomplete-last'.
86 ;; 2. It will increment `pcomplete-index' if the final argument has
87 ;; not been reached yet.
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.
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.
103 ;; Here is an example which will only complete against directories for
104 ;; the second argument if the first argument is also a directory:
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))))
112 ;; For generating completion lists based on directory contents, see
113 ;; the functions `pcomplete-entries', `pcomplete-dirs',
114 ;; `pcomplete-executables' and `pcomplete-all-entries'.
116 ;; Consult the documentation for `pcomplete-here' for information
117 ;; about its other arguments.
119 ;;; Code:
121 (eval-when-compile (require 'cl))
122 (require 'comint)
124 (defgroup pcomplete nil
125 "Programmable completion."
126 :version "21.1"
127 :group 'processes)
129 ;;; User Variables:
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)
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)
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)
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)
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")
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.
164 A non-nil value is useful if `pcomplete-autolist' is non-nil too."
165 :type 'boolean
166 :group 'pcomplete)
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)
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)
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)
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)
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)
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)
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.
226 It must either return nil, or a cons cell of the form:
228 ((ARG...) (BEG-POS...))
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)
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)
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)
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)
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)
286 (defsubst pcomplete-executables (&optional regexp)
287 "Complete amongst a list of directories and executables."
288 (pcomplete-entries regexp 'file-executable-p))
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)
298 (defcustom pcomplete-command-name-function 'pcomplete-command-name
299 "Function called for determining the current command name."
300 :type 'function
301 :group 'pcomplete)
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)
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)
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)
330 ;;; Internal Variables:
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)
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)
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)
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)
363 ;;; User Functions:
365 ;;; Alternative front-end using the standard completion facilities.
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.
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.
377 (defvar pcomplete-unquote-argument-function nil)
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)
385 (replace-regexp-in-string "\\\\\\(.\\)" "\\1" s t))))
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 S2.
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 s2b)
398 (if (and (not (equal ss1 qss1))
399 (setq qc (pcomplete-quote-argument (substring ss1 0 1)))
400 (setq s2b (- (length s2) cs (length qc) -1))
401 (>= s2b 0) ;bug#11158.
402 (eq t (compare-strings s2 s2b (- (length s2) cs -1)
403 qc nil nil)))
404 ;; The difference found is just that one char is quoted in S2
405 ;; but not in S1, keep looking before this difference.
406 (pcomplete--common-quoted-suffix
407 (substring s1 0 (- (length s1) cs))
408 (substring s2 0 s2b))
409 (cons (substring s1 0 (- (length s1) cs))
410 (substring s2 0 (- (length s2) cs))))))
412 ;; I don't think such commands are usable before first setting up buffer-local
413 ;; variables to parse args, so there's no point autoloading it.
414 ;; ;;;###autoload
415 (defun pcomplete-completions-at-point ()
416 "Provide standard completion using pcomplete's completion tables.
417 Same as `pcomplete' but using the standard completion UI."
418 ;; FIXME: it only completes the text before point, whereas the
419 ;; standard UI may also consider text after point.
420 ;; FIXME: the `pcomplete' UI may be used internally during
421 ;; pcomplete-completions and then throw to `pcompleted', thus
422 ;; imposing the pcomplete UI over the standard UI.
423 (catch 'pcompleted
424 (let* ((pcomplete-stub)
425 pcomplete-seen pcomplete-norm-func
426 pcomplete-args pcomplete-last pcomplete-index
427 (pcomplete-autolist pcomplete-autolist)
428 (pcomplete-suffix-list pcomplete-suffix-list)
429 ;; Apparently the vars above are global vars modified by
430 ;; side-effects, whereas pcomplete-completions is the core
431 ;; function that finds the chunk of text to complete
432 ;; (returned indirectly in pcomplete-stub) and the set of
433 ;; possible completions.
434 (completions (pcomplete-completions))
435 ;; Usually there's some close connection between pcomplete-stub
436 ;; and the text before point. But depending on what
437 ;; pcomplete-parse-arguments-function does, that connection
438 ;; might not be that close. E.g. in eshell,
439 ;; pcomplete-parse-arguments-function expands envvars.
441 ;; Since we use minibuffer-complete, which doesn't know
442 ;; pcomplete-stub and works from the buffer's text instead,
443 ;; we need to trick minibuffer-complete, into using
444 ;; pcomplete-stub without its knowledge. To that end, we
445 ;; use comint--table-subvert to construct a completion
446 ;; table which expects strings using a prefix from the
447 ;; buffer's text but internally uses the corresponding
448 ;; prefix from pcomplete-stub.
449 (beg (max (- (point) (length pcomplete-stub))
450 (pcomplete-begin)))
451 (buftext (buffer-substring beg (point))))
452 (when completions
453 (let ((table
454 (cond
455 ((not (equal pcomplete-stub buftext))
456 ;; This isn't always strictly right (e.g. if
457 ;; FOO="toto/$FOO", then completion of /$FOO/bar may
458 ;; result in something incorrect), but given the lack of
459 ;; any other info, it's about as good as it gets, and in
460 ;; practice it should work just fine (fingers crossed).
461 (let ((prefixes (pcomplete--common-quoted-suffix
462 pcomplete-stub buftext)))
463 (comint--table-subvert
464 completions (cdr prefixes) (car prefixes)
465 #'pcomplete-quote-argument #'pcomplete-unquote-argument)))
467 (lambda (string pred action)
468 (let ((res (complete-with-action
469 action completions string pred)))
470 (if (stringp res)
471 (pcomplete-quote-argument res)
472 res))))))
473 (pred
474 ;; Pare it down, if applicable.
475 (when (and pcomplete-use-paring pcomplete-seen)
476 ;; Capture the dynbound values for later use.
477 (let ((norm-func pcomplete-norm-func)
478 (seen
479 (mapcar (lambda (f)
480 (funcall pcomplete-norm-func
481 (directory-file-name f)))
482 pcomplete-seen)))
483 (lambda (f)
484 (not (member
485 (funcall norm-func (directory-file-name f))
486 seen)))))))
487 (when pcomplete-ignore-case
488 (setq table (completion-table-case-fold table)))
489 (list beg (point) table
490 :predicate pred
491 :exit-function
492 (unless (zerop (length pcomplete-termination-string))
493 (lambda (_s finished)
494 (when (memq finished '(sole finished))
495 (if (looking-at
496 (regexp-quote pcomplete-termination-string))
497 (goto-char (match-end 0))
498 (insert pcomplete-termination-string)))))))))))
500 ;; I don't think such commands are usable before first setting up buffer-local
501 ;; variables to parse args, so there's no point autoloading it.
502 ;; ;;;###autoload
503 (defun pcomplete-std-complete ()
504 (let ((data (pcomplete-completions-at-point)))
505 (completion-in-region (nth 0 data) (nth 1 data) (nth 2 data)
506 (plist-get :predicate (nthcdr 3 data)))))
508 ;;; Pcomplete's native UI.
510 ;;;###autoload
511 (defun pcomplete (&optional interactively)
512 "Support extensible programmable completion.
513 To use this function, just bind the TAB key to it, or add it to your
514 completion functions list (it should occur fairly early in the list)."
515 (interactive "p")
516 (if (and interactively
517 pcomplete-cycle-completions
518 pcomplete-current-completions
519 (memq last-command '(pcomplete
520 pcomplete-expand-and-complete
521 pcomplete-reverse)))
522 (progn
523 (delete-char (- pcomplete-last-completion-length))
524 (if (eq this-command 'pcomplete-reverse)
525 (progn
526 (push (car (last pcomplete-current-completions))
527 pcomplete-current-completions)
528 (setcdr (last pcomplete-current-completions 2) nil))
529 (nconc pcomplete-current-completions
530 (list (car pcomplete-current-completions)))
531 (setq pcomplete-current-completions
532 (cdr pcomplete-current-completions)))
533 (pcomplete-insert-entry pcomplete-last-completion-stub
534 (car pcomplete-current-completions)
535 nil pcomplete-last-completion-raw))
536 (setq pcomplete-current-completions nil
537 pcomplete-last-completion-raw nil)
538 (catch 'pcompleted
539 (let* ((pcomplete-stub)
540 pcomplete-seen pcomplete-norm-func
541 pcomplete-args pcomplete-last pcomplete-index
542 (pcomplete-autolist pcomplete-autolist)
543 (pcomplete-suffix-list pcomplete-suffix-list)
544 (completions (pcomplete-completions))
545 (result (pcomplete-do-complete pcomplete-stub completions)))
546 (and result
547 (not (eq (car result) 'listed))
548 (cdr result)
549 (pcomplete-insert-entry pcomplete-stub (cdr result)
550 (memq (car result)
551 '(sole shortest))
552 pcomplete-last-completion-raw))))))
554 ;;;###autoload
555 (defun pcomplete-reverse ()
556 "If cycling completion is in use, cycle backwards."
557 (interactive)
558 (call-interactively 'pcomplete))
560 ;;;###autoload
561 (defun pcomplete-expand-and-complete ()
562 "Expand the textual value of the current argument.
563 This will modify the current buffer."
564 (interactive)
565 (let ((pcomplete-expand-before-complete t))
566 (pcomplete)))
568 ;;;###autoload
569 (defun pcomplete-continue ()
570 "Complete without reference to any cycling completions."
571 (interactive)
572 (setq pcomplete-current-completions nil
573 pcomplete-last-completion-raw nil)
574 (call-interactively 'pcomplete))
576 ;;;###autoload
577 (defun pcomplete-expand ()
578 "Expand the textual value of the current argument.
579 This will modify the current buffer."
580 (interactive)
581 (let ((pcomplete-expand-before-complete t)
582 (pcomplete-expand-only-p t))
583 (pcomplete)
584 (when (and pcomplete-current-completions
585 (> (length pcomplete-current-completions) 0)) ;??
586 (delete-char (- pcomplete-last-completion-length))
587 (while pcomplete-current-completions
588 (unless (pcomplete-insert-entry
589 "" (car pcomplete-current-completions) t
590 pcomplete-last-completion-raw)
591 (insert-and-inherit pcomplete-termination-string))
592 (setq pcomplete-current-completions
593 (cdr pcomplete-current-completions))))))
595 ;;;###autoload
596 (defun pcomplete-help ()
597 "Display any help information relative to the current argument."
598 (interactive)
599 (let ((pcomplete-show-help t))
600 (pcomplete)))
602 ;;;###autoload
603 (defun pcomplete-list ()
604 "Show the list of possible completions for the current argument."
605 (interactive)
606 (when (and pcomplete-cycle-completions
607 pcomplete-current-completions
608 (eq last-command 'pcomplete-argument))
609 (delete-char (- pcomplete-last-completion-length))
610 (setq pcomplete-current-completions nil
611 pcomplete-last-completion-raw nil))
612 (let ((pcomplete-show-list t))
613 (pcomplete)))
615 ;;; Internal Functions:
617 ;; argument handling
618 (defun pcomplete-arg (&optional index offset)
619 "Return the textual content of the INDEXth argument.
620 INDEX is based from the current processing position. If INDEX is
621 positive, values returned are closer to the command argument; if
622 negative, they are closer to the last argument. If the INDEX is
623 outside of the argument list, nil is returned. The default value for
624 INDEX is 0, meaning the current argument being examined.
626 The special indices `first' and `last' may be used to access those
627 parts of the list.
629 The OFFSET argument is added to/taken away from the index that will be
630 used. This is really only useful with `first' and `last', for
631 accessing absolute argument positions."
632 (setq index
633 (if (eq index 'first)
635 (if (eq index 'last)
636 pcomplete-last
637 (- pcomplete-index (or index 0)))))
638 (if offset
639 (setq index (+ index offset)))
640 (nth index pcomplete-args))
642 (defun pcomplete-begin (&optional index offset)
643 "Return the beginning position of the INDEXth argument.
644 See the documentation for `pcomplete-arg'."
645 (setq index
646 (if (eq index 'first)
648 (if (eq index 'last)
649 pcomplete-last
650 (- pcomplete-index (or index 0)))))
651 (if offset
652 (setq index (+ index offset)))
653 (nth index pcomplete-begins))
655 (defsubst pcomplete-actual-arg (&optional index offset)
656 "Return the actual text representation of the last argument.
657 This is different from `pcomplete-arg', which returns the textual value
658 that the last argument evaluated to. This function returns what the
659 user actually typed in."
660 (buffer-substring (pcomplete-begin index offset) (point)))
662 (defsubst pcomplete-next-arg ()
663 "Move the various pointers to the next argument."
664 (setq pcomplete-index (1+ pcomplete-index)
665 pcomplete-stub (pcomplete-arg))
666 (if (> pcomplete-index pcomplete-last)
667 (progn
668 (message "No completions")
669 (throw 'pcompleted nil))))
671 (defun pcomplete-command-name ()
672 "Return the command name of the first argument."
673 (file-name-nondirectory (pcomplete-arg 'first)))
675 (defun pcomplete-match (regexp &optional index offset start)
676 "Like `string-match', but on the current completion argument."
677 (let ((arg (pcomplete-arg (or index 1) offset)))
678 (if arg
679 (string-match regexp arg start)
680 (throw 'pcompleted nil))))
682 (defun pcomplete-match-string (which &optional index offset)
683 "Like `match-string', but on the current completion argument."
684 (let ((arg (pcomplete-arg (or index 1) offset)))
685 (if arg
686 (match-string which arg)
687 (throw 'pcompleted nil))))
689 (defalias 'pcomplete-match-beginning 'match-beginning)
690 (defalias 'pcomplete-match-end 'match-end)
692 (defsubst pcomplete--test (pred arg)
693 "Perform a programmable completion predicate match."
694 (and pred
695 (cond ((eq pred t) t)
696 ((functionp pred)
697 (funcall pred arg))
698 ((stringp pred)
699 (string-match (concat "^" pred "$") arg)))
700 pred))
702 (defun pcomplete-test (predicates &optional index offset)
703 "Predicates to test the current programmable argument with."
704 (let ((arg (pcomplete-arg (or index 1) offset)))
705 (unless (null predicates)
706 (if (not (listp predicates))
707 (pcomplete--test predicates arg)
708 (let ((pred predicates)
709 found)
710 (while (and pred (not found))
711 (setq found (pcomplete--test (car pred) arg)
712 pred (cdr pred)))
713 found)))))
715 (defun pcomplete-parse-buffer-arguments ()
716 "Parse whitespace separated arguments in the current region."
717 (let ((begin (point-min))
718 (end (point-max))
719 begins args)
720 (save-excursion
721 (goto-char begin)
722 (while (< (point) end)
723 (skip-chars-forward " \t\n")
724 (push (point) begins)
725 (skip-chars-forward "^ \t\n")
726 (push (buffer-substring-no-properties
727 (car begins) (point))
728 args))
729 (cons (nreverse args) (nreverse begins)))))
731 ;;;###autoload
732 (defun pcomplete-comint-setup (completef-sym)
733 "Setup a comint buffer to use pcomplete.
734 COMPLETEF-SYM should be the symbol where the
735 dynamic-complete-functions are kept. For comint mode itself,
736 this is `comint-dynamic-complete-functions'."
737 (set (make-local-variable 'pcomplete-parse-arguments-function)
738 'pcomplete-parse-comint-arguments)
739 (add-hook 'completion-at-point-functions
740 'pcomplete-completions-at-point nil 'local)
741 (set (make-local-variable completef-sym)
742 (copy-sequence (symbol-value completef-sym)))
743 (let* ((funs (symbol-value completef-sym))
744 (elem (or (memq 'comint-filename-completion funs)
745 (memq 'shell-filename-completion funs)
746 (memq 'shell-dynamic-complete-filename funs)
747 (memq 'comint-dynamic-complete-filename funs))))
748 (if elem
749 (setcar elem 'pcomplete)
750 (add-to-list completef-sym 'pcomplete))))
752 ;;;###autoload
753 (defun pcomplete-shell-setup ()
754 "Setup `shell-mode' to use pcomplete."
755 ;; FIXME: insufficient
756 (pcomplete-comint-setup 'comint-dynamic-complete-functions))
758 (declare-function comint-bol "comint" (&optional arg))
760 (defun pcomplete-parse-comint-arguments ()
761 "Parse whitespace separated arguments in the current region."
762 (let ((begin (save-excursion (comint-bol nil) (point)))
763 (end (point))
764 begins args)
765 (save-excursion
766 (goto-char begin)
767 (while (< (point) end)
768 (skip-chars-forward " \t\n")
769 (push (point) begins)
770 (while
771 (progn
772 (skip-chars-forward "^ \t\n\\")
773 (when (eq (char-after) ?\\)
774 (forward-char 1)
775 (unless (eolp)
776 (forward-char 1)
777 t))))
778 (push (buffer-substring-no-properties (car begins) (point))
779 args))
780 (cons (nreverse args) (nreverse begins)))))
781 (make-obsolete 'pcomplete-parse-comint-arguments
782 'comint-parse-pcomplete-arguments "24.1")
784 (defun pcomplete-parse-arguments (&optional expand-p)
785 "Parse the command line arguments. Most completions need this info."
786 (let ((results (funcall pcomplete-parse-arguments-function)))
787 (when results
788 (setq pcomplete-args (or (car results) (list ""))
789 pcomplete-begins (or (cdr results) (list (point)))
790 pcomplete-last (1- (length pcomplete-args))
791 pcomplete-index 0
792 pcomplete-stub (pcomplete-arg 'last))
793 (let ((begin (pcomplete-begin 'last)))
794 (if (and pcomplete-cycle-completions
795 (listp pcomplete-stub) ;??
796 (not pcomplete-expand-only-p))
797 (let* ((completions pcomplete-stub) ;??
798 (common-stub (car completions))
799 (c completions)
800 (len (length common-stub)))
801 (while (and c (> len 0))
802 (while (and (> len 0)
803 (not (string=
804 (substring common-stub 0 len)
805 (substring (car c) 0
806 (min (length (car c))
807 len)))))
808 (setq len (1- len)))
809 (setq c (cdr c)))
810 (setq pcomplete-stub (substring common-stub 0 len)
811 pcomplete-autolist t)
812 (when (and begin (not pcomplete-show-list))
813 (delete-region begin (point))
814 (pcomplete-insert-entry "" pcomplete-stub))
815 (throw 'pcomplete-completions completions))
816 (when expand-p
817 (if (stringp pcomplete-stub)
818 (when begin
819 (delete-region begin (point))
820 (insert-and-inherit pcomplete-stub))
821 (if (and (listp pcomplete-stub)
822 pcomplete-expand-only-p)
823 ;; this is for the benefit of `pcomplete-expand'
824 (setq pcomplete-last-completion-length (- (point) begin)
825 pcomplete-current-completions pcomplete-stub)
826 (error "Cannot expand argument"))))
827 (if pcomplete-expand-only-p
828 (throw 'pcompleted t)
829 pcomplete-args))))))
831 (defun pcomplete-quote-argument (filename)
832 "Return FILENAME with magic characters quoted.
833 Magic characters are those in `pcomplete-arg-quote-list'."
834 (if (null pcomplete-arg-quote-list)
835 filename
836 (let ((index 0))
837 (mapconcat (lambda (c)
838 (prog1
839 (or (run-hook-with-args-until-success
840 'pcomplete-quote-arg-hook filename index)
841 (when (memq c pcomplete-arg-quote-list)
842 (string ?\\ c))
843 (char-to-string c))
844 (setq index (1+ index))))
845 filename
846 ""))))
848 ;; file-system completion lists
850 (defsubst pcomplete-dirs-or-entries (&optional regexp predicate)
851 "Return either directories, or qualified entries."
852 (pcomplete-entries
854 (lambda (f)
855 (or (file-directory-p f)
856 (and (or (null regexp) (string-match regexp f))
857 (or (null predicate) (funcall predicate f)))))))
859 (defun pcomplete--entries (&optional regexp predicate)
860 "Like `pcomplete-entries' but without env-var handling."
861 (let* ((ign-pred
862 (when (or pcomplete-file-ignore pcomplete-dir-ignore)
863 ;; Capture the dynbound value for later use.
864 (let ((file-ignore pcomplete-file-ignore)
865 (dir-ignore pcomplete-dir-ignore))
866 (lambda (file)
867 (not
868 (if (eq (aref file (1- (length file))) ?/)
869 (and dir-ignore (string-match dir-ignore file))
870 (and file-ignore (string-match file-ignore file))))))))
871 (reg-pred (if regexp (lambda (file) (string-match regexp file))))
872 (pred (cond
873 ((null (or ign-pred reg-pred)) predicate)
874 ((null (or ign-pred predicate)) reg-pred)
875 ((null (or reg-pred predicate)) ign-pred)
876 (t (lambda (f)
877 (and (or (null reg-pred) (funcall reg-pred f))
878 (or (null ign-pred) (funcall ign-pred f))
879 (or (null predicate) (funcall predicate f))))))))
880 (lambda (s p a)
881 (if (and (eq a 'metadata) pcomplete-compare-entry-function)
882 `(metadata (cycle-sort-function
883 . ,(lambda (comps)
884 (sort comps pcomplete-compare-entry-function)))
885 ,@(cdr (completion-file-name-table s p a)))
886 (let ((completion-ignored-extensions nil))
887 (completion-table-with-predicate
888 #'comint-completion-file-name-table pred 'strict s p a))))))
890 (defconst pcomplete--env-regexp
891 "\\(?:\\`\\|[^\\]\\)\\(?:\\\\\\\\\\)*\\(\\$\\(?:{\\([^}]+\\)}\\|\\(?2:[[:alnum:]_]+\\)\\)\\)")
893 (defun pcomplete-entries (&optional regexp predicate)
894 "Complete against a list of directory candidates.
895 If REGEXP is non-nil, it is a regular expression used to refine the
896 match (files not matching the REGEXP will be excluded).
897 If PREDICATE is non-nil, it will also be used to refine the match
898 \(files for which the PREDICATE returns nil will be excluded).
899 If no directory information can be extracted from the completed
900 component, `default-directory' is used as the basis for completion."
901 ;; FIXME: The old code did env-var expansion here, so we reproduce this
902 ;; behavior for now, but really env-var handling should be performed globally
903 ;; rather than here since it also applies to non-file arguments.
904 (let ((table (pcomplete--entries regexp predicate)))
905 (lambda (string pred action)
906 (let ((strings nil)
907 (orig-length (length string)))
908 ;; Perform env-var expansion.
909 (while (string-match pcomplete--env-regexp string)
910 (push (substring string 0 (match-beginning 1)) strings)
911 (push (getenv (match-string 2 string)) strings)
912 (setq string (substring string (match-end 1))))
913 (if (not (and strings
914 (or (eq action t)
915 (eq (car-safe action) 'boundaries))))
916 (let ((newstring
917 (mapconcat 'identity (nreverse (cons string strings)) "")))
918 ;; FIXME: We could also try to return unexpanded envvars.
919 (complete-with-action action table newstring pred))
920 (let* ((envpos (apply #'+ (mapcar #' length strings)))
921 (newstring
922 (mapconcat 'identity (nreverse (cons string strings)) ""))
923 (bounds (completion-boundaries newstring table pred
924 (or (cdr-safe action) ""))))
925 (if (>= (car bounds) envpos)
926 ;; The env-var is "out of bounds".
927 (if (eq action t)
928 (complete-with-action action table newstring pred)
929 (list* 'boundaries
930 (+ (car bounds) (- orig-length (length newstring)))
931 (cdr bounds)))
932 ;; The env-var is in the file bounds.
933 (if (eq action t)
934 (let ((comps (complete-with-action
935 action table newstring pred))
936 (len (- envpos (car bounds))))
937 ;; Strip the part of each completion that's actually
938 ;; coming from the env-var.
939 (mapcar (lambda (s) (substring s len)) comps))
940 (list* 'boundaries
941 (+ envpos (- orig-length (length newstring)))
942 (cdr bounds))))))))))
944 (defsubst pcomplete-all-entries (&optional regexp predicate)
945 "Like `pcomplete-entries', but doesn't ignore any entries."
946 (let (pcomplete-file-ignore
947 pcomplete-dir-ignore)
948 (pcomplete-entries regexp predicate)))
950 (defsubst pcomplete-dirs (&optional regexp)
951 "Complete amongst a list of directories."
952 (pcomplete-entries regexp 'file-directory-p))
954 ;; generation of completion lists
956 (defun pcomplete-find-completion-function (command)
957 "Find the completion function to call for the given COMMAND."
958 (let ((sym (intern-soft
959 (concat "pcomplete/" (symbol-name major-mode) "/" command))))
960 (unless sym
961 (setq sym (intern-soft (concat "pcomplete/" command))))
962 (and sym (fboundp sym) sym)))
964 (defun pcomplete-completions ()
965 "Return a list of completions for the current argument position."
966 (catch 'pcomplete-completions
967 (when (pcomplete-parse-arguments pcomplete-expand-before-complete)
968 (if (= pcomplete-index pcomplete-last)
969 (funcall pcomplete-command-completion-function)
970 (let ((sym (or (pcomplete-find-completion-function
971 (funcall pcomplete-command-name-function))
972 pcomplete-default-completion-function)))
973 (ignore
974 (pcomplete-next-arg)
975 (funcall sym)))))))
977 (defun pcomplete-opt (options &optional prefix _no-ganging _args-follow)
978 "Complete a set of OPTIONS, each beginning with PREFIX (?- by default).
979 PREFIX may be t, in which case no PREFIX character is necessary.
980 If NO-GANGING is non-nil, each option is separate (-xy is not allowed).
981 If ARGS-FOLLOW is non-nil, then options which take arguments may have
982 the argument appear after a ganged set of options. This is how tar
983 behaves, for example.
984 Arguments NO-GANGING and ARGS-FOLLOW are currently ignored."
985 (if (and (= pcomplete-index pcomplete-last)
986 (string= (pcomplete-arg) "-"))
987 (let ((len (length options))
988 (index 0)
989 char choices)
990 (while (< index len)
991 (setq char (aref options index))
992 (if (eq char ?\()
993 (let ((result (read-from-string options index)))
994 (setq index (cdr result)))
995 (unless (memq char '(?/ ?* ?? ?.))
996 (push (char-to-string char) choices))
997 (setq index (1+ index))))
998 (throw 'pcomplete-completions
999 (mapcar
1000 (function
1001 (lambda (opt)
1002 (concat "-" opt)))
1003 (pcomplete-uniqify-list choices))))
1004 (let ((arg (pcomplete-arg)))
1005 (when (and (> (length arg) 1)
1006 (stringp arg)
1007 (eq (aref arg 0) (or prefix ?-)))
1008 (pcomplete-next-arg)
1009 (let ((char (aref arg 1))
1010 (len (length options))
1011 (index 0)
1012 opt-char arg-char result)
1013 (while (< (1+ index) len)
1014 (setq opt-char (aref options index)
1015 arg-char (aref options (1+ index)))
1016 (if (eq arg-char ?\()
1017 (setq result
1018 (read-from-string options (1+ index))
1019 index (cdr result)
1020 result (car result))
1021 (setq result nil))
1022 (when (and (eq char opt-char)
1023 (memq arg-char '(?\( ?/ ?* ?? ?.)))
1024 (if (< pcomplete-index pcomplete-last)
1025 (pcomplete-next-arg)
1026 (throw 'pcomplete-completions
1027 (cond ((eq arg-char ?/) (pcomplete-dirs))
1028 ((eq arg-char ?*) (pcomplete-executables))
1029 ((eq arg-char ??) nil)
1030 ((eq arg-char ?.) (pcomplete-entries))
1031 ((eq arg-char ?\() (eval result))))))
1032 (setq index (1+ index))))))))
1034 (defun pcomplete--here (&optional form stub paring form-only)
1035 "Complete against the current argument, if at the end.
1036 See the documentation for `pcomplete-here'."
1037 (if (< pcomplete-index pcomplete-last)
1038 (progn
1039 (if (eq paring 0)
1040 (setq pcomplete-seen nil)
1041 (unless (eq paring t)
1042 (let ((arg (pcomplete-arg)))
1043 (when (stringp arg)
1044 (push (if paring
1045 (funcall paring arg)
1046 (file-truename arg))
1047 pcomplete-seen)))))
1048 (pcomplete-next-arg)
1050 (when pcomplete-show-help
1051 (pcomplete--help)
1052 (throw 'pcompleted t))
1053 (if stub
1054 (setq pcomplete-stub stub))
1055 (if (or (eq paring t) (eq paring 0))
1056 (setq pcomplete-seen nil)
1057 (setq pcomplete-norm-func (or paring 'file-truename)))
1058 (unless form-only
1059 (run-hooks 'pcomplete-try-first-hook))
1060 (throw 'pcomplete-completions
1061 (if (functionp form)
1062 (funcall form)
1063 ;; Old calling convention, might still be used by files
1064 ;; byte-compiled with the older code.
1065 (eval form)))))
1067 (defmacro pcomplete-here (&optional form stub paring form-only)
1068 "Complete against the current argument, if at the end.
1069 If completion is to be done here, evaluate FORM to generate the completion
1070 table which will be used for completion purposes. If STUB is a
1071 string, use it as the completion stub instead of the default (which is
1072 the entire text of the current argument).
1074 For an example of when you might want to use STUB: if the current
1075 argument text is 'long-path-name/', you don't want the completions
1076 list display to be cluttered by 'long-path-name/' appearing at the
1077 beginning of every alternative. Not only does this make things less
1078 intelligible, but it is also inefficient. Yet, if the completion list
1079 does not begin with this string for every entry, the current argument
1080 won't complete correctly.
1082 The solution is to specify a relative stub. It allows you to
1083 substitute a different argument from the current argument, almost
1084 always for the sake of efficiency.
1086 If PARING is nil, this argument will be pared against previous
1087 arguments using the function `file-truename' to normalize them.
1088 PARING may be a function, in which case that function is used for
1089 normalization. If PARING is t, the argument dealt with by this
1090 call will not participate in argument paring. If it is the
1091 integer 0, all previous arguments that have been seen will be
1092 cleared.
1094 If FORM-ONLY is non-nil, only the result of FORM will be used to
1095 generate the completions list. This means that the hook
1096 `pcomplete-try-first-hook' will not be run."
1097 (declare (debug t))
1098 `(pcomplete--here (lambda () ,form) ,stub ,paring ,form-only))
1101 (defmacro pcomplete-here* (&optional form stub form-only)
1102 "An alternate form which does not participate in argument paring."
1103 (declare (debug t))
1104 `(pcomplete-here ,form ,stub t ,form-only))
1106 ;; display support
1108 (defun pcomplete-restore-windows ()
1109 "If the only window change was due to Completions, restore things."
1110 (if pcomplete-last-window-config
1111 (let* ((cbuf (get-buffer "*Completions*"))
1112 (cwin (and cbuf (get-buffer-window cbuf))))
1113 (when (window-live-p cwin)
1114 (bury-buffer cbuf)
1115 (set-window-configuration pcomplete-last-window-config))))
1116 (setq pcomplete-last-window-config nil
1117 pcomplete-window-restore-timer nil))
1119 ;; Abstractions so that the code below will work for both Emacs 20 and
1120 ;; XEmacs 21
1122 (defalias 'pcomplete-event-matches-key-specifier-p
1123 (if (featurep 'xemacs)
1124 'event-matches-key-specifier-p
1125 'eq))
1127 (defun pcomplete-read-event (&optional prompt)
1128 (if (fboundp 'read-event)
1129 (read-event prompt)
1130 (aref (read-key-sequence prompt) 0)))
1132 (defun pcomplete-show-completions (completions)
1133 "List in help buffer sorted COMPLETIONS.
1134 Typing SPC flushes the help buffer."
1135 (when pcomplete-window-restore-timer
1136 (cancel-timer pcomplete-window-restore-timer)
1137 (setq pcomplete-window-restore-timer nil))
1138 (unless pcomplete-last-window-config
1139 (setq pcomplete-last-window-config (current-window-configuration)))
1140 (with-output-to-temp-buffer "*Completions*"
1141 (display-completion-list completions))
1142 (message "Hit space to flush")
1143 (let (event)
1144 (prog1
1145 (catch 'done
1146 (while (with-current-buffer (get-buffer "*Completions*")
1147 (setq event (pcomplete-read-event)))
1148 (cond
1149 ((pcomplete-event-matches-key-specifier-p event ?\s)
1150 (set-window-configuration pcomplete-last-window-config)
1151 (setq pcomplete-last-window-config nil)
1152 (throw 'done nil))
1153 ((or (pcomplete-event-matches-key-specifier-p event 'tab)
1154 ;; Needed on a terminal
1155 (pcomplete-event-matches-key-specifier-p event 9))
1156 (let ((win (or (get-buffer-window "*Completions*" 0)
1157 (display-buffer "*Completions*"
1158 'not-this-window))))
1159 (with-selected-window win
1160 (if (pos-visible-in-window-p (point-max))
1161 (goto-char (point-min))
1162 (scroll-up))))
1163 (message ""))
1165 (setq unread-command-events (list event))
1166 (throw 'done nil)))))
1167 (if (and pcomplete-last-window-config
1168 pcomplete-restore-window-delay)
1169 (setq pcomplete-window-restore-timer
1170 (run-with-timer pcomplete-restore-window-delay nil
1171 'pcomplete-restore-windows))))))
1173 ;; insert completion at point
1175 (defun pcomplete-insert-entry (stub entry &optional addsuffix raw-p)
1176 "Insert a completion entry at point.
1177 Returns non-nil if a space was appended at the end."
1178 (let ((here (point)))
1179 (if (not pcomplete-ignore-case)
1180 (insert-and-inherit (if raw-p
1181 (substring entry (length stub))
1182 (pcomplete-quote-argument
1183 (substring entry (length stub)))))
1184 ;; the stub is not quoted at this time, so to determine the
1185 ;; length of what should be in the buffer, we must quote it
1186 ;; FIXME: Here we presume that quoting `stub' gives us the exact
1187 ;; text in the buffer before point, which is not guaranteed;
1188 ;; e.g. it is not the case in eshell when completing ${FOO}tm[TAB].
1189 (delete-char (- (length (pcomplete-quote-argument stub))))
1190 ;; if there is already a backslash present to handle the first
1191 ;; character, don't bother quoting it
1192 (when (eq (char-before) ?\\)
1193 (insert-and-inherit (substring entry 0 1))
1194 (setq entry (substring entry 1)))
1195 (insert-and-inherit (if raw-p
1196 entry
1197 (pcomplete-quote-argument entry))))
1198 (let (space-added)
1199 (when (and (not (memq (char-before) pcomplete-suffix-list))
1200 addsuffix)
1201 (insert-and-inherit pcomplete-termination-string)
1202 (setq space-added t))
1203 (setq pcomplete-last-completion-length (- (point) here)
1204 pcomplete-last-completion-stub stub)
1205 space-added)))
1207 ;; selection of completions
1209 (defun pcomplete-do-complete (stub completions)
1210 "Dynamically complete at point using STUB and COMPLETIONS.
1211 This is basically just a wrapper for `pcomplete-stub' which does some
1212 extra checking, and munging of the COMPLETIONS list."
1213 (unless (stringp stub)
1214 (message "Cannot complete argument")
1215 (throw 'pcompleted nil))
1216 (if (null completions)
1217 (ignore
1218 (if (and stub (> (length stub) 0))
1219 (message "No completions of %s" stub)
1220 (message "No completions")))
1221 ;; pare it down, if applicable
1222 (when (and pcomplete-use-paring pcomplete-seen)
1223 (setq pcomplete-seen
1224 (mapcar 'directory-file-name pcomplete-seen))
1225 (dolist (p pcomplete-seen)
1226 (add-to-list 'pcomplete-seen
1227 (funcall pcomplete-norm-func p)))
1228 (setq completions
1229 (apply-partially 'completion-table-with-predicate
1230 completions
1231 (when pcomplete-seen
1232 (lambda (f)
1233 (not (member
1234 (funcall pcomplete-norm-func
1235 (directory-file-name f))
1236 pcomplete-seen))))
1237 'strict)))
1238 ;; OK, we've got a list of completions.
1239 (if pcomplete-show-list
1240 ;; FIXME: pay attention to boundaries.
1241 (pcomplete-show-completions (all-completions stub completions))
1242 (pcomplete-stub stub completions))))
1244 (defun pcomplete-stub (stub candidates &optional cycle-p)
1245 "Dynamically complete STUB from CANDIDATES list.
1246 This function inserts completion characters at point by completing
1247 STUB from the strings in CANDIDATES. A completions listing may be
1248 shown in a help buffer if completion is ambiguous.
1250 Returns nil if no completion was inserted.
1251 Returns `sole' if completed with the only completion match.
1252 Returns `shortest' if completed with the shortest of the matches.
1253 Returns `partial' if completed as far as possible with the matches.
1254 Returns `listed' if a completion listing was shown.
1256 See also `pcomplete-filename'."
1257 (let* ((completion-ignore-case pcomplete-ignore-case)
1258 (completions (all-completions stub candidates))
1259 (entry (try-completion stub candidates))
1260 result)
1261 (cond
1262 ((null entry)
1263 (if (and stub (> (length stub) 0))
1264 (message "No completions of %s" stub)
1265 (message "No completions")))
1266 ((eq entry t)
1267 (setq entry stub)
1268 (message "Sole completion")
1269 (setq result 'sole))
1270 ((= 1 (length completions))
1271 (setq result 'sole))
1272 ((and pcomplete-cycle-completions
1273 (or cycle-p
1274 (not pcomplete-cycle-cutoff-length)
1275 (<= (length completions)
1276 pcomplete-cycle-cutoff-length)))
1277 (let ((bound (car (completion-boundaries stub candidates nil ""))))
1278 (unless (zerop bound)
1279 (setq completions (mapcar (lambda (c) (concat (substring stub 0 bound) c))
1280 completions)))
1281 (setq entry (car completions)
1282 pcomplete-current-completions completions)))
1283 ((and pcomplete-recexact
1284 (string-equal stub entry)
1285 (member entry completions))
1286 ;; It's not unique, but user wants shortest match.
1287 (message "Completed shortest")
1288 (setq result 'shortest))
1289 ((or pcomplete-autolist
1290 (string-equal stub entry))
1291 ;; It's not unique, list possible completions.
1292 ;; FIXME: pay attention to boundaries.
1293 (pcomplete-show-completions completions)
1294 (setq result 'listed))
1296 (message "Partially completed")
1297 (setq result 'partial)))
1298 (cons result entry)))
1300 ;; context sensitive help
1302 (defun pcomplete--help ()
1303 "Produce context-sensitive help for the current argument.
1304 If specific documentation can't be given, be generic."
1305 (if (and pcomplete-help
1306 (or (and (stringp pcomplete-help)
1307 (fboundp 'Info-goto-node))
1308 (listp pcomplete-help)))
1309 (if (listp pcomplete-help)
1310 (message "%s" (eval pcomplete-help))
1311 (save-window-excursion (info))
1312 (switch-to-buffer-other-window "*info*")
1313 (funcall (symbol-function 'Info-goto-node) pcomplete-help))
1314 (if pcomplete-man-function
1315 (let ((cmd (funcall pcomplete-command-name-function)))
1316 (if (and cmd (> (length cmd) 0))
1317 (funcall pcomplete-man-function cmd)))
1318 (message "No context-sensitive help available"))))
1320 ;; general utilities
1322 (defun pcomplete-uniqify-list (l)
1323 "Sort and remove multiples in L."
1324 (setq l (sort l 'string-lessp))
1325 (let ((m l))
1326 (while m
1327 (while (and (cdr m)
1328 (string= (car m)
1329 (cadr m)))
1330 (setcdr m (cddr m)))
1331 (setq m (cdr m))))
1334 (defun pcomplete-process-result (cmd &rest args)
1335 "Call CMD using `call-process' and return the simplest result."
1336 (with-temp-buffer
1337 (apply 'call-process cmd nil t nil args)
1338 (skip-chars-backward "\n")
1339 (buffer-substring (point-min) (point))))
1341 ;; create a set of aliases which allow completion functions to be not
1342 ;; quite so verbose
1344 ;;; jww (1999-10-20): are these a good idea?
1345 ;; (defalias 'pc-here 'pcomplete-here)
1346 ;; (defalias 'pc-test 'pcomplete-test)
1347 ;; (defalias 'pc-opt 'pcomplete-opt)
1348 ;; (defalias 'pc-match 'pcomplete-match)
1349 ;; (defalias 'pc-match-string 'pcomplete-match-string)
1350 ;; (defalias 'pc-match-beginning 'pcomplete-match-beginning)
1351 ;; (defalias 'pc-match-end 'pcomplete-match-end)
1353 (provide 'pcomplete)
1355 ;;; pcomplete.el ends here