icomplete.el 21 KB

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