pcomplete.el 51 KB

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