icomplete.el 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  1. ;;; icomplete.el --- minibuffer completion incremental feedback
  2. ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2017 Free Software
  3. ;; Foundation, Inc.
  4. ;; Author: Ken Manheimer <klm@i.am>
  5. ;; Maintainer: Ken Manheimer <klm@i.am>
  6. ;; Created: Mar 1993 Ken Manheimer, klm@nist.gov - first release to usenet
  7. ;; Keywords: help, abbrev
  8. ;; This file is part of GNU Emacs.
  9. ;; GNU Emacs is free software: you can redistribute it and/or modify
  10. ;; it under the terms of the GNU General Public License as published by
  11. ;; the Free Software Foundation, either version 3 of the License, or
  12. ;; (at your option) any later version.
  13. ;; GNU Emacs is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;; You should have received a copy of the GNU General Public License
  18. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  19. ;;; Commentary:
  20. ;; Enabling this package implements a more fine-grained minibuffer
  21. ;; completion feedback scheme. Prospective completions are concisely
  22. ;; indicated within the minibuffer itself, with each successive
  23. ;; keystroke.
  24. ;; See `icomplete-completions' docstring for a description of the
  25. ;; icomplete display format.
  26. ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
  27. ;; customize icomplete setup for interoperation with other
  28. ;; minibuffer-oriented packages.
  29. ;; To enable/disable icomplete mode, use the `icomplete-mode' function.
  30. ;; Thanks to everyone for their suggestions for refinements of this
  31. ;; package. I particularly have to credit Michael Cook, who
  32. ;; implemented an incremental completion style in his 'iswitch'
  33. ;; functions that served as a model for icomplete. Some other
  34. ;; contributors: Noah Friedman (restructuring as minor mode), Colin
  35. ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
  36. ;; klm.
  37. ;;; Code:
  38. (defgroup icomplete nil
  39. "Show completions dynamically in minibuffer."
  40. :prefix "icomplete-"
  41. :link '(info-link "(emacs)Icomplete")
  42. :group 'minibuffer)
  43. (defcustom icomplete-separator " | "
  44. "String used by Icomplete to separate alternatives in the minibuffer."
  45. :type 'string
  46. :version "24.4")
  47. (defcustom icomplete-hide-common-prefix t
  48. "When non-nil, hide common prefix from completion candidates.
  49. When nil, show candidates in full."
  50. :type 'boolean
  51. :version "24.4")
  52. (defcustom icomplete-show-matches-on-no-input nil
  53. "When non-nil, show completions when first prompting for input."
  54. :type 'boolean
  55. :version "24.4")
  56. (defcustom icomplete-with-completion-tables t
  57. "Specialized completion tables with which Icomplete should operate.
  58. If this is t, Icomplete operates on all tables.
  59. Otherwise this should be a list of the completion tables (e.g.,
  60. `internal-complete-buffer') on which Icomplete should operate."
  61. ;; Prior to 24.4, not a user-option, default '(internal-complete-buffer).
  62. :version "24.4"
  63. :type '(choice (const :tag "All" t)
  64. (repeat function)))
  65. (defface icomplete-first-match '((t :weight bold))
  66. "Face used by Icomplete for highlighting first match."
  67. :version "24.4")
  68. ;;;_* User Customization variables
  69. (defcustom icomplete-prospects-height 2
  70. ;; We used to compute how many lines 100 characters would take in
  71. ;; the current window width, but the return value of `window-width'
  72. ;; is unreliable on startup (e.g., if we're in daemon mode), so now
  73. ;; we simply base the default value on an 80 column window.
  74. "Maximum number of lines to use in the minibuffer."
  75. :type 'integer
  76. :version "26.1")
  77. (defcustom icomplete-compute-delay .3
  78. "Completions-computation stall, used only with large-number completions.
  79. See `icomplete-delay-completions-threshold'."
  80. :type 'number)
  81. (defcustom icomplete-delay-completions-threshold 400
  82. "Pending-completions number over which to apply `icomplete-compute-delay'."
  83. :type 'integer)
  84. (defcustom icomplete-max-delay-chars 3
  85. "Maximum number of initial chars to apply `icomplete-compute-delay'."
  86. :type 'integer)
  87. (defvar icomplete-in-buffer nil
  88. "If non-nil, also use Icomplete when completing in non-mini buffers.")
  89. (defcustom icomplete-minibuffer-setup-hook nil
  90. "Icomplete-specific customization of minibuffer setup.
  91. This hook is run during minibuffer setup if Icomplete is active.
  92. It is intended for use in customizing Icomplete for interoperation
  93. with other features and packages. For instance:
  94. (add-hook \\='icomplete-minibuffer-setup-hook
  95. (lambda () (setq-local max-mini-window-height 3)))
  96. will constrain Emacs to a maximum minibuffer height of 3 lines when
  97. icompletion is occurring."
  98. :type 'hook
  99. :group 'icomplete)
  100. ;;;_* Initialization
  101. ;;;_ + Internal Variables
  102. ;;;_ = icomplete-eoinput nil
  103. (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
  104. "Overlay used to display the list of completions.")
  105. (defun icomplete-pre-command-hook ()
  106. (let ((non-essential t))
  107. (icomplete-tidy)))
  108. (defun icomplete-post-command-hook ()
  109. (let ((non-essential t)) ;E.g. don't prompt for password!
  110. (icomplete-exhibit)))
  111. (defvar icomplete-minibuffer-map
  112. (let ((map (make-sparse-keymap)))
  113. (define-key map [?\M-\t] 'minibuffer-force-complete)
  114. (define-key map [?\C-j] 'icomplete-force-complete-and-exit)
  115. (define-key map [?\C-.] 'icomplete-forward-completions)
  116. (define-key map [?\C-,] 'icomplete-backward-completions)
  117. map)
  118. "Keymap used by `icomplete-mode' in the minibuffer.")
  119. (defun icomplete-force-complete-and-exit ()
  120. "Complete the minibuffer and exit.
  121. Use the first of the matches if there are any displayed, and use
  122. the default otherwise."
  123. (interactive)
  124. (if (or icomplete-show-matches-on-no-input
  125. (> (icomplete--field-end) (icomplete--field-beg)))
  126. (minibuffer-force-complete-and-exit)
  127. (minibuffer-complete-and-exit)))
  128. (defun icomplete-forward-completions ()
  129. "Step forward completions by one entry.
  130. Second entry becomes the first and can be selected with
  131. `icomplete-force-complete-and-exit'."
  132. (interactive)
  133. (let* ((beg (icomplete--field-beg))
  134. (end (icomplete--field-end))
  135. (comps (completion-all-sorted-completions beg end))
  136. (last (last comps)))
  137. (when comps
  138. (setcdr last (cons (car comps) (cdr last)))
  139. (completion--cache-all-sorted-completions beg end (cdr comps)))))
  140. (defun icomplete-backward-completions ()
  141. "Step backward completions by one entry.
  142. Last entry becomes the first and can be selected with
  143. `icomplete-force-complete-and-exit'."
  144. (interactive)
  145. (let* ((beg (icomplete--field-beg))
  146. (end (icomplete--field-end))
  147. (comps (completion-all-sorted-completions beg end))
  148. (last-but-one (last comps 2))
  149. (last (cdr last-but-one)))
  150. (when (consp last) ; At least two elements in comps
  151. (setcdr last-but-one (cdr last))
  152. (push (car last) comps)
  153. (completion--cache-all-sorted-completions beg end comps))))
  154. ;;;_ > icomplete-mode (&optional prefix)
  155. ;;;###autoload
  156. (define-minor-mode icomplete-mode
  157. "Toggle incremental minibuffer completion (Icomplete mode).
  158. With a prefix argument ARG, enable Icomplete mode if ARG is
  159. positive, and disable it otherwise. If called from Lisp, enable
  160. the mode if ARG is omitted or nil.
  161. When this global minor mode is enabled, typing in the minibuffer
  162. continuously displays a list of possible completions that match
  163. the string you have typed. See `icomplete-completions' for a
  164. description of how prospective completions are displayed.
  165. For more information, see Info node `(emacs)Icomplete'.
  166. For options you can set, `\\[customize-group] icomplete'.
  167. You can use the following key bindings to navigate and select
  168. completions:
  169. \\{icomplete-minibuffer-map}"
  170. :global t :group 'icomplete
  171. (remove-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)
  172. (remove-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup)
  173. (when icomplete-mode
  174. (when icomplete-in-buffer
  175. (add-hook 'completion-in-region-mode-hook #'icomplete--in-region-setup))
  176. (add-hook 'minibuffer-setup-hook #'icomplete-minibuffer-setup)))
  177. (defun icomplete--completion-table ()
  178. (if (window-minibuffer-p) minibuffer-completion-table
  179. (or (nth 2 completion-in-region--data)
  180. (message "In %S (w=%S): %S"
  181. (current-buffer) (selected-window) (window-minibuffer-p)))))
  182. (defun icomplete--completion-predicate ()
  183. (if (window-minibuffer-p) minibuffer-completion-predicate
  184. (nth 3 completion-in-region--data)))
  185. (defun icomplete--field-string ()
  186. (if (window-minibuffer-p) (minibuffer-contents)
  187. (buffer-substring-no-properties
  188. (nth 0 completion-in-region--data)
  189. (nth 1 completion-in-region--data))))
  190. (defun icomplete--field-beg ()
  191. (if (window-minibuffer-p) (minibuffer-prompt-end)
  192. (nth 0 completion-in-region--data)))
  193. (defun icomplete--field-end ()
  194. (if (window-minibuffer-p) (point-max)
  195. (nth 1 completion-in-region--data)))
  196. ;;;_ > icomplete-simple-completing-p ()
  197. (defun icomplete-simple-completing-p ()
  198. "Non-nil if current window is a minibuffer that's doing simple completion.
  199. Conditions are:
  200. the selected window is a minibuffer,
  201. and not in the middle of macro execution,
  202. and the completion table is not a function (which would
  203. indicate some non-standard, non-simple completion mechanism,
  204. like file-name and other custom-func completions),
  205. and `icomplete-with-completion-tables' doesn't restrict completion."
  206. (unless executing-kbd-macro
  207. (let ((table (icomplete--completion-table)))
  208. (and table
  209. (or (not (functionp table))
  210. (eq icomplete-with-completion-tables t)
  211. (member table icomplete-with-completion-tables))))))
  212. ;;;_ > icomplete-minibuffer-setup ()
  213. (defun icomplete-minibuffer-setup ()
  214. "Run in minibuffer on activation to establish incremental completion.
  215. Usually run by inclusion in `minibuffer-setup-hook'."
  216. (when (and icomplete-mode (icomplete-simple-completing-p))
  217. (set (make-local-variable 'completion-show-inline-help) nil)
  218. (use-local-map (make-composed-keymap icomplete-minibuffer-map
  219. (current-local-map)))
  220. (add-hook 'pre-command-hook #'icomplete-pre-command-hook nil t)
  221. (add-hook 'post-command-hook #'icomplete-post-command-hook nil t)
  222. (run-hooks 'icomplete-minibuffer-setup-hook)
  223. (when icomplete-show-matches-on-no-input
  224. (icomplete-exhibit))))
  225. (defvar icomplete--in-region-buffer nil)
  226. (defun icomplete--in-region-setup ()
  227. (when (or (not completion-in-region-mode)
  228. (and icomplete--in-region-buffer
  229. (not (eq icomplete--in-region-buffer (current-buffer)))))
  230. (with-current-buffer (or icomplete--in-region-buffer (current-buffer))
  231. (setq icomplete--in-region-buffer nil)
  232. (delete-overlay icomplete-overlay)
  233. (kill-local-variable 'completion-show-inline-help)
  234. (remove-hook 'pre-command-hook 'icomplete-pre-command-hook t)
  235. (remove-hook 'post-command-hook 'icomplete-post-command-hook t)
  236. (message nil)))
  237. (when (and completion-in-region-mode
  238. icomplete-mode (icomplete-simple-completing-p))
  239. (setq icomplete--in-region-buffer (current-buffer))
  240. (set (make-local-variable 'completion-show-inline-help) nil)
  241. (let ((tem (assq 'completion-in-region-mode
  242. minor-mode-overriding-map-alist)))
  243. (unless (memq icomplete-minibuffer-map (cdr tem))
  244. (setcdr tem (make-composed-keymap icomplete-minibuffer-map
  245. (cdr tem)))))
  246. (add-hook 'pre-command-hook 'icomplete-pre-command-hook nil t)
  247. (add-hook 'post-command-hook 'icomplete-post-command-hook nil t)))
  248. ;;;_* Completion
  249. ;;;_ > icomplete-tidy ()
  250. (defun icomplete-tidy ()
  251. "Remove completions display (if any) prior to new user input.
  252. Should be run in on the minibuffer `pre-command-hook'.
  253. See `icomplete-mode' and `minibuffer-setup-hook'."
  254. (delete-overlay icomplete-overlay))
  255. ;;;_ > icomplete-exhibit ()
  256. (defun icomplete-exhibit ()
  257. "Insert Icomplete completions display.
  258. Should be run via minibuffer `post-command-hook'.
  259. See `icomplete-mode' and `minibuffer-setup-hook'."
  260. (when (and icomplete-mode
  261. (icomplete-simple-completing-p)) ;Shouldn't be necessary.
  262. (save-excursion
  263. (goto-char (point-max))
  264. ; Insert the match-status information:
  265. (if (and (or icomplete-show-matches-on-no-input
  266. (> (icomplete--field-end) (icomplete--field-beg)))
  267. (or
  268. ;; Don't bother with delay after certain number of chars:
  269. (> (- (point) (icomplete--field-beg))
  270. icomplete-max-delay-chars)
  271. ;; Don't delay if the completions are known.
  272. completion-all-sorted-completions
  273. ;; Don't delay if alternatives number is small enough:
  274. (and (sequencep (icomplete--completion-table))
  275. (< (length (icomplete--completion-table))
  276. icomplete-delay-completions-threshold))
  277. ;; Delay - give some grace time for next keystroke, before
  278. ;; embarking on computing completions:
  279. (sit-for icomplete-compute-delay)))
  280. (let* ((field-string (icomplete--field-string))
  281. (text (while-no-input
  282. (icomplete-completions
  283. field-string
  284. (icomplete--completion-table)
  285. (icomplete--completion-predicate)
  286. (if (window-minibuffer-p)
  287. (not minibuffer-completion-confirm)))))
  288. (buffer-undo-list t)
  289. deactivate-mark)
  290. ;; Do nothing if while-no-input was aborted.
  291. (when (stringp text)
  292. (move-overlay icomplete-overlay (point) (point) (current-buffer))
  293. ;; The current C cursor code doesn't know to use the overlay's
  294. ;; marker's stickiness to figure out whether to place the cursor
  295. ;; before or after the string, so let's spoon-feed it the pos.
  296. (put-text-property 0 1 'cursor t text)
  297. (overlay-put icomplete-overlay 'after-string text)))))))
  298. ;;;_ > icomplete-completions (name candidates predicate require-match)
  299. (defun icomplete-completions (name candidates predicate require-match)
  300. "Identify prospective candidates for minibuffer completion.
  301. The display is updated with each minibuffer keystroke during
  302. minibuffer completion.
  303. Prospective completion suffixes (if any) are displayed, bracketed by
  304. one of (), [], or {} pairs. The choice of brackets is as follows:
  305. (...) - a single prospect is identified and matching is enforced,
  306. [...] - a single prospect is identified but matching is optional, or
  307. {...} - multiple prospects, separated by commas, are indicated, and
  308. further input is required to distinguish a single one.
  309. If there are multiple possibilities, `icomplete-separator' separates them.
  310. The displays for unambiguous matches have ` [Matched]' appended
  311. \(whether complete or not), or ` [No matches]', if no eligible
  312. matches exist."
  313. (let* ((minibuffer-completion-table candidates)
  314. (minibuffer-completion-predicate predicate)
  315. (md (completion--field-metadata (icomplete--field-beg)))
  316. (comps (completion-all-sorted-completions
  317. (icomplete--field-beg) (icomplete--field-end)))
  318. (last (if (consp comps) (last comps)))
  319. (base-size (cdr last))
  320. (open-bracket (if require-match "(" "["))
  321. (close-bracket (if require-match ")" "]")))
  322. ;; `concat'/`mapconcat' is the slow part.
  323. (if (not (consp comps))
  324. (progn ;;(debug (format "Candidates=%S field=%S" candidates name))
  325. (format " %sNo matches%s" open-bracket close-bracket))
  326. (if last (setcdr last nil))
  327. (when (and minibuffer-completing-file-name
  328. icomplete-with-completion-tables)
  329. (setq comps (completion-pcm--filename-try-filter comps)))
  330. (let* ((most-try
  331. (if (and base-size (> base-size 0))
  332. (completion-try-completion
  333. name candidates predicate (length name) md)
  334. ;; If the `comps' are 0-based, the result should be
  335. ;; the same with `comps'.
  336. (completion-try-completion
  337. name comps nil (length name) md)))
  338. (most (if (consp most-try) (car most-try)
  339. (if most-try (car comps) "")))
  340. ;; Compare name and most, so we can determine if name is
  341. ;; a prefix of most, or something else.
  342. (compare (compare-strings name nil nil
  343. most nil nil completion-ignore-case))
  344. (ellipsis (if (char-displayable-p ?…) "…" "..."))
  345. (determ (unless (or (eq t compare) (eq t most-try)
  346. (= (setq compare (1- (abs compare)))
  347. (length most)))
  348. (concat open-bracket
  349. (cond
  350. ((= compare (length name))
  351. ;; Typical case: name is a prefix.
  352. (substring most compare))
  353. ;; Don't bother truncating if it doesn't gain
  354. ;; us at least 2 columns.
  355. ((< compare (+ 2 (string-width ellipsis))) most)
  356. (t (concat ellipsis (substring most compare))))
  357. close-bracket)))
  358. ;;"-prospects" - more than one candidate
  359. (prospects-len (+ (string-width
  360. (or determ (concat open-bracket close-bracket)))
  361. (string-width icomplete-separator)
  362. (+ 2 (string-width ellipsis)) ;; take {…} into account
  363. (string-width (buffer-string))))
  364. (prospects-max
  365. ;; Max total length to use, including the minibuffer content.
  366. (* (+ icomplete-prospects-height
  367. ;; If the minibuffer content already uses up more than
  368. ;; one line, increase the allowable space accordingly.
  369. (/ prospects-len (window-width)))
  370. (window-width)))
  371. ;; Find the common prefix among `comps'.
  372. ;; We can't use the optimization below because its assumptions
  373. ;; aren't always true, e.g. when completion-cycling (bug#10850):
  374. ;; (if (eq t (compare-strings (car comps) nil (length most)
  375. ;; most nil nil completion-ignore-case))
  376. ;; ;; Common case.
  377. ;; (length most)
  378. ;; Else, use try-completion.
  379. (prefix (when icomplete-hide-common-prefix
  380. (try-completion "" comps)))
  381. (prefix-len
  382. (and (stringp prefix)
  383. ;; Only hide the prefix if the corresponding info
  384. ;; is already displayed via `most'.
  385. (string-prefix-p prefix most t)
  386. (length prefix))) ;;)
  387. prospects comp limit)
  388. (if (or (eq most-try t) (not (consp (cdr comps))))
  389. (setq prospects nil)
  390. (when (member name comps)
  391. ;; NAME is complete but not unique. This scenario poses
  392. ;; following UI issues:
  393. ;;
  394. ;; - When `icomplete-hide-common-prefix' is non-nil, NAME
  395. ;; is stripped empty. This would make the entry
  396. ;; inconspicuous.
  397. ;;
  398. ;; - Due to sorting of completions, NAME may not be the
  399. ;; first of the prospects and could be hidden deep in
  400. ;; the displayed string.
  401. ;;
  402. ;; - Because of `icomplete-prospects-height' , NAME may
  403. ;; not even be displayed to the user.
  404. ;;
  405. ;; To circumvent all the above problems, provide a visual
  406. ;; cue to the user via an "empty string" in the try
  407. ;; completion field.
  408. (setq determ (concat open-bracket "" close-bracket)))
  409. ;; Compute prospects for display.
  410. (while (and comps (not limit))
  411. (setq comp
  412. (if prefix-len (substring (car comps) prefix-len) (car comps))
  413. comps (cdr comps))
  414. (setq prospects-len
  415. (+ (string-width comp)
  416. (string-width icomplete-separator)
  417. prospects-len))
  418. (if (< prospects-len prospects-max)
  419. (push comp prospects)
  420. (setq limit t))))
  421. (setq prospects (nreverse prospects))
  422. ;; Decorate first of the prospects.
  423. (when prospects
  424. (let ((first (copy-sequence (pop prospects))))
  425. (put-text-property 0 (length first)
  426. 'face 'icomplete-first-match first)
  427. (push first prospects)))
  428. ;; Restore the base-size info, since completion-all-sorted-completions
  429. ;; is cached.
  430. (if last (setcdr last base-size))
  431. (if prospects
  432. (concat determ
  433. "{"
  434. (mapconcat 'identity prospects icomplete-separator)
  435. (and limit (concat icomplete-separator ellipsis))
  436. "}")
  437. (concat determ " [Matched]"))))))
  438. ;;; Iswitchb compatibility
  439. ;; We moved Iswitchb to `obsolete' in 24.4, but autoloads in files in
  440. ;; `obsolete' aren't obeyed (since that would encourage people to keep using
  441. ;; those packages, oblivious to their obsolescence). Given the fact that
  442. ;; Iswitchb was very popular, we decided to keep its autoload for a bit longer,
  443. ;; so we moved it here.
  444. ;;;###autoload(when (locate-library "obsolete/iswitchb")
  445. ;;;###autoload (autoload 'iswitchb-mode "iswitchb" "Toggle Iswitchb mode." t)
  446. ;;;###autoload (make-obsolete 'iswitchb-mode
  447. ;;;###autoload "use `icomplete-mode' or `ido-mode' instead." "24.4"))
  448. ;;;_* Provide
  449. (provide 'icomplete)
  450. ;;_* Local emacs vars.
  451. ;;Local variables:
  452. ;;allout-layout: (-2 :)
  453. ;;End:
  454. ;;; icomplete.el ends here