icomplete.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. ;;; icomplete.el --- minibuffer completion incremental feedback
  2. ;; Copyright (C) 1992-1994, 1997, 1999, 2001-2012
  3. ;; Free Software 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. ;; Last update: Ken Manheimer <klm@i.am>, 11/18/1999.
  8. ;; Keywords: help, abbrev
  9. ;; This file is part of GNU Emacs.
  10. ;; GNU Emacs is free software: you can redistribute it and/or modify
  11. ;; it under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation, either version 3 of the License, or
  13. ;; (at your option) any later version.
  14. ;; GNU Emacs is distributed in the hope that it will be useful,
  15. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ;; GNU General Public License for more details.
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  20. ;;; Commentary:
  21. ;; Loading this package implements a more fine-grained minibuffer
  22. ;; completion feedback scheme. Prospective completions are concisely
  23. ;; indicated within the minibuffer itself, with each successive
  24. ;; keystroke.
  25. ;; See `icomplete-completions' docstring for a description of the
  26. ;; icomplete display format.
  27. ;; See the `icomplete-minibuffer-setup-hook' docstring for a means to
  28. ;; customize icomplete setup for interoperation with other
  29. ;; minibuffer-oriented packages.
  30. ;; To activate icomplete mode, load the package and use the
  31. ;; `icomplete-mode' function. You can subsequently deactivate it by
  32. ;; invoking the function icomplete-mode with a negative prefix-arg
  33. ;; (C-U -1 ESC-x icomplete-mode). Also, you can prevent activation of
  34. ;; the mode during package load by first setting the variable
  35. ;; `icomplete-mode' to nil. Icompletion can be enabled any time after
  36. ;; the package is loaded by invoking icomplete-mode without a prefix
  37. ;; arg.
  38. ;; Thanks to everyone for their suggestions for refinements of this
  39. ;; package. I particularly have to credit Michael Cook, who
  40. ;; implemented an incremental completion style in his 'iswitch'
  41. ;; functions that served as a model for icomplete. Some other
  42. ;; contributors: Noah Friedman (restructuring as minor mode), Colin
  43. ;; Rafferty (lemacs reconciliation), Lars Lindberg, RMS, and others.
  44. ;; klm.
  45. ;;; Code:
  46. ;;;_* Provide
  47. (provide 'icomplete)
  48. (defgroup icomplete nil
  49. "Show completions dynamically in minibuffer."
  50. :prefix "icomplete-"
  51. :group 'minibuffer)
  52. (defvar icomplete-prospects-length 80)
  53. (make-obsolete-variable
  54. 'icomplete-prospects-length 'icomplete-prospects-height "23.1")
  55. ;;;_* User Customization variables
  56. (defcustom icomplete-prospects-height
  57. ;; 20 is an estimated common size for the prompt + minibuffer content, to
  58. ;; try to guess the number of lines used up by icomplete-prospects-length.
  59. (+ 1 (/ (+ icomplete-prospects-length 20) (window-width)))
  60. "Maximum number of lines to use in the minibuffer."
  61. :type 'integer
  62. :group 'icomplete
  63. :version "23.1")
  64. (defcustom icomplete-compute-delay .3
  65. "Completions-computation stall, used only with large-number completions.
  66. See `icomplete-delay-completions-threshold'."
  67. :type 'number
  68. :group 'icomplete)
  69. (defcustom icomplete-delay-completions-threshold 400
  70. "Pending-completions number over which to apply `icomplete-compute-delay'."
  71. :type 'integer
  72. :group 'icomplete)
  73. (defcustom icomplete-max-delay-chars 3
  74. "Maximum number of initial chars to apply icomplete compute delay."
  75. :type 'integer
  76. :group 'icomplete)
  77. (defcustom icomplete-show-key-bindings t
  78. "If non-nil, show key bindings as well as completion for sole matches."
  79. :type 'boolean
  80. :group 'icomplete)
  81. (defcustom icomplete-minibuffer-setup-hook nil
  82. "Icomplete-specific customization of minibuffer setup.
  83. This hook is run during minibuffer setup if icomplete is active.
  84. It is intended for use in customizing icomplete for interoperation
  85. with other features and packages. For instance:
  86. \(add-hook 'icomplete-minibuffer-setup-hook
  87. \(function
  88. \(lambda ()
  89. \(make-local-variable 'max-mini-window-height)
  90. \(setq max-mini-window-height 3))))
  91. will constrain Emacs to a maximum minibuffer height of 3 lines when
  92. icompletion is occurring."
  93. :type 'hook
  94. :group 'icomplete)
  95. ;;;_* Initialization
  96. ;;;_ + Internal Variables
  97. ;;;_ = icomplete-eoinput nil
  98. (defvar icomplete-overlay (make-overlay (point-min) (point-min) nil t t)
  99. "Overlay used to display the list of completions.")
  100. ;;;_ = icomplete-pre-command-hook
  101. (defvar icomplete-pre-command-hook nil
  102. "Incremental-minibuffer-completion pre-command-hook.
  103. Is run in minibuffer before user input when `icomplete-mode' is non-nil.
  104. Use `icomplete-mode' function to set it up properly for incremental
  105. minibuffer completion.")
  106. (add-hook 'icomplete-pre-command-hook 'icomplete-tidy)
  107. ;;;_ = icomplete-post-command-hook
  108. (defvar icomplete-post-command-hook nil
  109. "Incremental-minibuffer-completion post-command-hook.
  110. Is run in minibuffer after user input when `icomplete-mode' is non-nil.
  111. Use `icomplete-mode' function to set it up properly for incremental
  112. minibuffer completion.")
  113. (add-hook 'icomplete-post-command-hook 'icomplete-exhibit)
  114. (defun icomplete-get-keys (func-name)
  115. "Return strings naming keys bound to FUNC-NAME, or nil if none.
  116. Examines the prior, not current, buffer, presuming that current buffer
  117. is minibuffer."
  118. (when (commandp func-name)
  119. (save-excursion
  120. (let* ((sym (intern func-name))
  121. (buf (other-buffer nil t))
  122. (keys (with-current-buffer buf (where-is-internal sym))))
  123. (when keys
  124. (concat "<"
  125. (mapconcat 'key-description
  126. (sort keys
  127. #'(lambda (x y)
  128. (< (length x) (length y))))
  129. ", ")
  130. ">"))))))
  131. ;;;_ = icomplete-with-completion-tables
  132. (defvar icomplete-with-completion-tables '(internal-complete-buffer)
  133. "Specialized completion tables with which icomplete should operate.
  134. Icomplete does not operate with any specialized completion tables
  135. except those on this list.")
  136. ;;;_ > icomplete-mode (&optional prefix)
  137. ;;;###autoload
  138. (define-minor-mode icomplete-mode
  139. "Toggle incremental minibuffer completion (Icomplete mode).
  140. With a prefix argument ARG, enable Icomplete mode if ARG is
  141. positive, and disable it otherwise. If called from Lisp, enable
  142. the mode if ARG is omitted or nil."
  143. :global t :group 'icomplete
  144. (if icomplete-mode
  145. ;; The following is not really necessary after first time -
  146. ;; no great loss.
  147. (add-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)
  148. (remove-hook 'minibuffer-setup-hook 'icomplete-minibuffer-setup)))
  149. ;;;_ > icomplete-simple-completing-p ()
  150. (defun icomplete-simple-completing-p ()
  151. "Non-nil if current window is minibuffer that's doing simple completion.
  152. Conditions are:
  153. the selected window is a minibuffer,
  154. and not in the middle of macro execution,
  155. and `minibuffer-completion-table' is not a symbol (which would
  156. indicate some non-standard, non-simple completion mechanism,
  157. like file-name and other custom-func completions)."
  158. (and (window-minibuffer-p (selected-window))
  159. (not executing-kbd-macro)
  160. minibuffer-completion-table
  161. (or (not (functionp minibuffer-completion-table))
  162. (eq icomplete-with-completion-tables t)
  163. (member minibuffer-completion-table
  164. icomplete-with-completion-tables))))
  165. ;;;_ > icomplete-minibuffer-setup ()
  166. (defun icomplete-minibuffer-setup ()
  167. "Run in minibuffer on activation to establish incremental completion.
  168. Usually run by inclusion in `minibuffer-setup-hook'."
  169. (when (and icomplete-mode (icomplete-simple-completing-p))
  170. (set (make-local-variable 'completion-show-inline-help) nil)
  171. (add-hook 'pre-command-hook
  172. (lambda () (run-hooks 'icomplete-pre-command-hook))
  173. nil t)
  174. (add-hook 'post-command-hook
  175. (lambda () (run-hooks 'icomplete-post-command-hook))
  176. nil t)
  177. (run-hooks 'icomplete-minibuffer-setup-hook)))
  178. ;
  179. ;;;_* Completion
  180. ;;;_ > icomplete-tidy ()
  181. (defun icomplete-tidy ()
  182. "Remove completions display \(if any) prior to new user input.
  183. Should be run in on the minibuffer `pre-command-hook'. See `icomplete-mode'
  184. and `minibuffer-setup-hook'."
  185. (delete-overlay icomplete-overlay))
  186. ;;;_ > icomplete-exhibit ()
  187. (defun icomplete-exhibit ()
  188. "Insert icomplete completions display.
  189. Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
  190. and `minibuffer-setup-hook'."
  191. (when (and icomplete-mode (icomplete-simple-completing-p))
  192. (save-excursion
  193. (goto-char (point-max))
  194. ; Insert the match-status information:
  195. (if (and (> (point-max) (minibuffer-prompt-end))
  196. buffer-undo-list ; Wait for some user input.
  197. (or
  198. ;; Don't bother with delay after certain number of chars:
  199. (> (- (point) (field-beginning)) icomplete-max-delay-chars)
  200. ;; Don't delay if alternatives number is small enough:
  201. (and (sequencep minibuffer-completion-table)
  202. (< (length minibuffer-completion-table)
  203. icomplete-delay-completions-threshold))
  204. ;; Delay - give some grace time for next keystroke, before
  205. ;; embarking on computing completions:
  206. (sit-for icomplete-compute-delay)))
  207. (let ((text (while-no-input
  208. (icomplete-completions
  209. (field-string)
  210. minibuffer-completion-table
  211. minibuffer-completion-predicate
  212. (not minibuffer-completion-confirm))))
  213. (buffer-undo-list t)
  214. deactivate-mark)
  215. ;; Do nothing if while-no-input was aborted.
  216. (when (stringp text)
  217. (move-overlay icomplete-overlay (point) (point) (current-buffer))
  218. ;; The current C cursor code doesn't know to use the overlay's
  219. ;; marker's stickiness to figure out whether to place the cursor
  220. ;; before or after the string, so let's spoon-feed it the pos.
  221. (put-text-property 0 1 'cursor t text)
  222. (overlay-put icomplete-overlay 'after-string text)))))))
  223. ;;;_ > icomplete-completions (name candidates predicate require-match)
  224. (defun icomplete-completions (name candidates predicate require-match)
  225. "Identify prospective candidates for minibuffer completion.
  226. The display is updated with each minibuffer keystroke during
  227. minibuffer completion.
  228. Prospective completion suffixes (if any) are displayed, bracketed by
  229. one of \(), \[], or \{} pairs. The choice of brackets is as follows:
  230. \(...) - a single prospect is identified and matching is enforced,
  231. \[...] - a single prospect is identified but matching is optional, or
  232. \{...} - multiple prospects, separated by commas, are indicated, and
  233. further input is required to distinguish a single one.
  234. The displays for unambiguous matches have ` [Matched]' appended
  235. \(whether complete or not), or ` \[No matches]', if no eligible
  236. matches exist. \(Keybindings for uniquely matched commands
  237. are exhibited within the square braces.)"
  238. (let* ((non-essential t)
  239. (md (completion--field-metadata (field-beginning)))
  240. (comps (completion-all-sorted-completions))
  241. (last (if (consp comps) (last comps)))
  242. (base-size (cdr last))
  243. (open-bracket (if require-match "(" "["))
  244. (close-bracket (if require-match ")" "]")))
  245. ;; `concat'/`mapconcat' is the slow part.
  246. (if (not (consp comps))
  247. (format " %sNo matches%s" open-bracket close-bracket)
  248. (if last (setcdr last nil))
  249. (let* ((most-try
  250. (if (and base-size (> base-size 0))
  251. (completion-try-completion
  252. name candidates predicate (length name) md)
  253. ;; If the `comps' are 0-based, the result should be
  254. ;; the same with `comps'.
  255. (completion-try-completion
  256. name comps nil (length name) md)))
  257. (most (if (consp most-try) (car most-try)
  258. (if most-try (car comps) "")))
  259. ;; Compare name and most, so we can determine if name is
  260. ;; a prefix of most, or something else.
  261. (compare (compare-strings name nil nil
  262. most nil nil completion-ignore-case))
  263. (determ (unless (or (eq t compare) (eq t most-try)
  264. (= (setq compare (1- (abs compare)))
  265. (length most)))
  266. (concat open-bracket
  267. (cond
  268. ((= compare (length name))
  269. ;; Typical case: name is a prefix.
  270. (substring most compare))
  271. ((< compare 5) most)
  272. (t (concat "..." (substring most compare))))
  273. close-bracket)))
  274. ;;"-prospects" - more than one candidate
  275. (prospects-len (+ (length determ) 6 ;; take {,...} into account
  276. (string-width (buffer-string))))
  277. (prospects-max
  278. ;; Max total length to use, including the minibuffer content.
  279. (* (+ icomplete-prospects-height
  280. ;; If the minibuffer content already uses up more than
  281. ;; one line, increase the allowable space accordingly.
  282. (/ prospects-len (window-width)))
  283. (window-width)))
  284. (prefix-len
  285. ;; Find the common prefix among `comps'.
  286. ;; We can't use the optimization below because its assumptions
  287. ;; aren't always true, e.g. when completion-cycling (bug#10850):
  288. ;; (if (eq t (compare-strings (car comps) nil (length most)
  289. ;; most nil nil completion-ignore-case))
  290. ;; ;; Common case.
  291. ;; (length most)
  292. ;; Else, use try-completion.
  293. (let ((comps-prefix (try-completion "" comps)))
  294. (and (stringp comps-prefix)
  295. (length comps-prefix)))) ;;)
  296. prospects most-is-exact comp limit)
  297. (if (eq most-try t) ;; (or (null (cdr comps))
  298. (setq prospects nil)
  299. (while (and comps (not limit))
  300. (setq comp
  301. (if prefix-len (substring (car comps) prefix-len) (car comps))
  302. comps (cdr comps))
  303. (cond ((string-equal comp "") (setq most-is-exact t))
  304. ((member comp prospects))
  305. (t (setq prospects-len
  306. (+ (string-width comp) 1 prospects-len))
  307. (if (< prospects-len prospects-max)
  308. (push comp prospects)
  309. (setq limit t))))))
  310. ;; Restore the base-size info, since completion-all-sorted-completions
  311. ;; is cached.
  312. (if last (setcdr last base-size))
  313. (if prospects
  314. (concat determ
  315. "{"
  316. (and most-is-exact ",")
  317. (mapconcat 'identity (nreverse prospects) ",")
  318. (and limit ",...")
  319. "}")
  320. (concat determ
  321. " [Matched"
  322. (let ((keys (and icomplete-show-key-bindings
  323. (commandp (intern-soft most))
  324. (icomplete-get-keys most))))
  325. (if keys (concat "; " keys) ""))
  326. "]"))))))
  327. ;;_* Local emacs vars.
  328. ;;Local variables:
  329. ;;allout-layout: (-2 :)
  330. ;;End:
  331. ;;; icomplete.el ends here