pcomplete.el 51 KB

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