pcomplete.el 53 KB

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