New org capture template
[emacs.git] / .emacs.d / elisp / icicle / icomplete+.el
1 ;;; icomplete+.el --- Extensions to `icomplete.el'.
2 ;;
3 ;; Filename: icomplete+.el
4 ;; Description: Extensions to `icomplete.el'.
5 ;; Author: Drew Adams
6 ;; Maintainer: Drew Adams (concat "drew.adams" "@" "oracle" ".com")
7 ;; Copyright (C) 1996-2014, Drew Adams, all rights reserved.
8 ;; Created: Mon Oct 16 13:33:18 1995
9 ;; Version: 0
10 ;; Package-Requires: ()
11 ;; Last-Updated: Mon Mar 10 10:26:42 2014 (-0700)
12 ;; By: dradams
13 ;; Update #: 1671
14 ;; URL: http://www.emacswiki.org/icomplete+.el
15 ;; Doc URL: http://emacswiki.org/IcompleteMode
16 ;; Keywords: help, abbrev, internal, extensions, local, completion, matching
17 ;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x, 24.x
18 ;;
19 ;; Features that might be required by this library:
20 ;;
21 ;; `icomplete'.
22 ;;
23 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
24 ;;
25 ;;; Commentary:
26 ;;
27 ;; Extensions to `icomplete.el'.
28 ;;
29 ;; * Better display of candidates, including highlighting them and showing how many there are.
30 ;; * Shows key bindings for command candidates, optionally including menu bindings.
31 ;; * Does not bind keys for cycling in `icomplete-mode'. Defines a separate mode for that, so you
32 ;; can use Icomplete with or without those key bindings. (Emacs 24.4+)
33 ;; * Support for Icicles:
34 ;; . Respect current Icicles sort order, which you can cycle using `C-,'.
35 ;; . When you change direction cycling candidates, show the number of other cycle candidates.
36 ;;
37 ;;
38 ;; Macros defined here (but identical to those in Emacs 23):
39 ;;
40 ;; `with-local-quit', `with-no-input'.
41 ;;
42 ;; Commands define here:
43 ;;
44 ;; `icompletep-cycling-mode'.
45 ;;
46 ;; Faces defined here:
47 ;;
48 ;; `icompletep-choices', `icompletep-determined',
49 ;; `icompletep-keys', `icompletep-nb-candidates'.
50 ;;
51 ;; User options defined here:
52 ;;
53 ;; `icompletep-exact-separator',
54 ;; `icompletep-include-menu-items-flag' (Emacs 23+),
55 ;; `icompletep-prospects-length' (Emacs < 23),
56 ;; `icomplete-show-key-bindings'.
57 ;;
58 ;; Non-interactive functions defined here:
59 ;;
60 ;; `icomplete-get-keys' (Emacs > 24.2),
61 ;; `icompletep-completion-all-sorted-completions',
62 ;; `icompletep-remove-if'.
63 ;;
64 ;;
65 ;; ***** NOTE: The following functions defined in `icomplete.el'
66 ;; have been REDEFINED HERE:
67 ;;
68 ;; `icomplete-get-keys' (Emacs < 24.3) -
69 ;; 1. Respect `icompletep-include-menu-items-flag'.
70 ;; 2. Do not wrap with `<...>'.
71 ;; 3. If string of keys would be too long then shorten it.
72 ;;
73 ;; `icomplete-completions' -
74 ;; 1. Prepend the total number of candidates.
75 ;; 2. For file-name completion, respect `completion-ignored-extensions'.
76 ;; 3. With Icicles, sort candidates using `icicle-reversible-sort' and show number of
77 ;; remaining cycle candidates. You can cycle the sort order using `C-,'.
78 ;; 4. Show candidates in a different face.
79 ;; 5. Optionally show and highlight key bindings, truncating if too long.
80 ;;
81 ;; `icomplete-exhibit' -
82 ;; 1. Save match-data.
83 ;; 2. Do not insert if input begins with `(' (e.g. `repeat-complex-command').
84 ;; 3. Ensure that the insertion does not deactivate mark.
85 ;;
86 ;;
87 ;; This file should be loaded after loading the standard GNU file
88 ;; `icomplete.el'. So, in your `~/.emacs' file, do this:
89 ;; (eval-after-load "icomplete" '(progn (require 'icomplete+)))
90 ;;
91 ;; Usage notes:
92 ;;
93 ;; * Starting with Emacs 23 you can get icompletion of things like
94 ;; file names also. See variable (non-option)
95 ;; `icomplete-with-completion-tables'. If you set it to the
96 ;; (undocumented) value `t' then icompletion is available anytime
97 ;; the completion COLLECTION parameter is a function, which
98 ;; includes file-name completion.
99 ;;
100 ;; * Starting with Emacs 24 you can specify the kinds of completion
101 ;; you want by customizing option `completion-category-overrides'
102 ;; for file names, buffer names, bookmark names, and so on.
103 ;;
104 ;; * Starting with Emacs 24.4, Icomplete mode automatically binds
105 ;; keys that are otherwise useful in the minibuffer (for Isearch,
106 ;; symbol completion, etc.) to its own keys for cycling among
107 ;; icompletion candidates. This is a BAD idea - see Emacs bug
108 ;; #13602. Icomplete+ fixes this by having a separate mode that
109 ;; binds Icomplete keys, making that optional. This is analogous
110 ;; to the difference between `cua-selection-mode' and `cua-mode'.
111 ;;
112 ;; So with Icomplete+, just turning on Icomplete mode does not
113 ;; co-opt those keys taking them away from you for use in the
114 ;; minibuffer. If you really want to do that then turn on
115 ;; `icomplete-cycling-mode' in addition to `icomplete-mode'. And
116 ;; in that case, consider also choosing different keys to bind in
117 ;; `icomplete-minibuffer-map' from those that are bound by default.
118 ;;
119 ;; (The first two features above are not particular to Icomplete+ -
120 ;; they are available also for vanilla Icomplete.)
121 ;;
122 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
123 ;;
124 ;;; Change Log:
125 ;;
126 ;; 2014/03/10 dadams
127 ;; icomplete-exhibit: Reverted fix from 2014-03-05 for Emacs < 23 (broke progressive completion).
128 ;; 2014/03/05 dadams
129 ;; icomplete-exhibit: Wrap body with with-current-buffer, to prevent using *Completions* buffer.
130 ;; 2014/01/11 dadams
131 ;; Soft, not hard require cl-lib.el. Only 24.3+ has it.
132 ;; icompletep-completion-all-sorted-completions for Emacs 24.1-3:
133 ;; Use icompletep-remove-if if cl-delete-if is not defined (i.e., for Emacs 24.1-2).
134 ;; 2013/12/31 dadams
135 ;; icomplete-exhibit, for Emacs 24.3.50: Protect icomplete-show-matches-on-no-input with boundp.
136 ;; 2013/12/27 dadams
137 ;; icomplete-exhibit, icomplete-completions, icompletep-completion-all-sorted-completions:
138 ;; Added 24.4+ version.
139 ;; icompletep-completion-all-sorted-completions:
140 ;; Use cl-delete-if, not delete-if. And require cl-lib.el.
141 ;; Take regexp-opt calculation out of the cl-delete-if loop, for performance.
142 ;; 2013/09/21 dadams
143 ;; icompletep-completion-all-sorted-completions:
144 ;; Temporary hack for 24.4: completion--cache-all-sorted-completions takes 3 args now.
145 ;; 2013/01/31 dadams
146 ;; Added: icompletep-cycling-mode. Turn it off by default.
147 ;; 2013/01/01 dadams
148 ;; Added: icompletep-completion-all-sorted-completions.
149 ;; icomplete-completions (Emacs 24 version):
150 ;; Rewrote to support Icicles sorting and Emacs 24 candidate metadata:
151 ;; Use icompletep-completion-all-sorted-completions instead of all-completions or
152 ;; completion-pcm--filename-try-filter.
153 ;; Use completion--field-metadata, last, and base-size.
154 ;; Use completion-try-completion, not try-completion.
155 ;; Do not sort using string-lessp (sorted by icompletep-completion-all-sorted-completions).
156 ;; 2012/12/19 dadams
157 ;; icomplete-completions: Use width of icomplete-separator, not 1 or 2. Use string-width more.
158 ;; icomplete-separator: Use same default value as vanilla Emacs.
159 ;; 2012/12/08 dadams
160 ;; icompletep-exact-separator: Different default value - Unicode star for Emacs 23+, * otherwise.
161 ;; 2012/12/05 dadams
162 ;; Updated wrt Emacs 24:
163 ;; Added: icomplete-show-key-bindings (removed from vanilla Emacs), icomplete-separator,
164 ;; icompletep-exact-separator.
165 ;; icomplete-exhibit: Added completion-all-sorted-completions as delay inhibition.
166 ;; icomplete-completions (all versions): Use icomplete-separator, icompletep-exact-separator.
167 ;; 2012/11/19 dadams
168 ;; icomplete-completions:
169 ;; Exclude file names with extensions in completion-ignored-extensions.
170 ;; Use same MOST sexp as in Emacs <23 versions, since use try-completion.
171 ;; 2012/08/06 dadams
172 ;; Removed old, commented code at end of file.
173 ;; 2012/07/21 dadams
174 ;; icomplete-completions: Fixed typo for Emacs 23 version: COLLECTION everywhere, not CANDIDATES.
175 ;; 2012/06/23 dadams
176 ;; icomplete-completions: Added new version for Emacs 24. Remove 24 stuff from Emacs 23 version.
177 ;; For Emacs 23 & 24: Use try-completion, not completion-try-completion.
178 ;; 2012/06/16 dadams
179 ;; icomplete-completions (Emacs 23+):
180 ;; Use actual length of nb-cands-string, not min length from %7d format spec.
181 ;; Include length of icicle-completion-prompt-overlay, if Icicles and completing.
182 ;; 2012/03/09 dadams
183 ;; icomplete-completions: Updated for Emacs 24: Bind non-essential and handle bug #10850.
184 ;; 2011/10/13 dadams
185 ;; icomplete-get-keys: Added optional arg EXACTP. Better spacing counts.
186 ;; icomplete-completions: Use MOST-TRY to determine exact match, pass to icomplete-get-keys.
187 ;; 2011/10/12 dadams
188 ;; Added: icompletep-include-menu-items-flag, icomplete-get-keys, icompletep-remove-if.
189 ;; icomplete-completions: Truncate key-binding text if too long.
190 ;; 2011/08/24 dadams
191 ;; Added top-level puts for common-lisp-indent-function.
192 ;; with-local-quit, while-no-input:
193 ;; Define only if not defined. Use put for indentation. Remove declare declaration.
194 ;; 2011/06/05 dadams
195 ;; icomplete-completions: Handle Emacs 24's new METADATA arg for completion-try-completion.
196 ;; 2011/01/04 dadams
197 ;; Removed autoload cookies from non def* sexps. Added them for defgroup, defface.
198 ;; 2010/07/29 dadams
199 ;; with-local-quit, with-no-input: Protect declare with fboundp.
200 ;; 2009/08/06 dadams
201 ;; icomplete-completions (Emacs < 23): Bind, don't set, to initialize nb-candidates.
202 ;; 2008/06/01 dadams
203 ;; icomplete-completions (Emacs 23): Set candidates to nil if ((nil)).
204 ;; Commented out vanilla Emacs code that's not used (last, base-size).
205 ;; 2008/05/30 dadams
206 ;; Updated for Emacs 23 - complete rewrite.
207 ;; Added: macros with-local-quit and with-no-input.
208 ;; Added and adapted icomplete-exhibit and icomplete-completions for Emacs 23.
209 ;; icompletep-prospects-length: Use only for Emacs < 23.
210 ;; icomplete-exhibit: Removed vestigial test of icicle-apropos-completing-p.
211 ;; 2006/07/30 dadams
212 ;; icomplete-exhibit: Save match-data.
213 ;; 2006/07/16 dadams
214 ;; Added dark-background face suggestions from Le Wang - thx.
215 ;; 2006/06/18 dadams
216 ;; icomplete-exhibit: Don't insert if Icicles apropos-completing.
217 ;; 2006/01/07 dadams
218 ;; Added :link for sending bug report.
219 ;; 2006/01/06 dadams
220 ;; Added defgroup. Added :link.
221 ;; Renamed: prefix icomplete- to icompletep-.
222 ;; 2005/12/18 dadams
223 ;; Renamed faces without "-face".
224 ;; Use defface. Removed require of def-face-const.el.
225 ;; icomplete-prospects-length: defvar -> defcustom.
226 ;; 2005/09/30 dadams
227 ;; Commented out redefinitions of primitives, so no longer reset
228 ;; minibuffer-completion-table to nil. Leaving the commented code in for now.
229 ;; 2005/08/16 dadams
230 ;; icomplete-completions: If icicles.el is loaded, change no-match message slightly.
231 ;; 2005/07/24 dadams
232 ;; icomplete-exhibit: Set deactivate-mark to nil at end.
233 ;; Remove commented Emacs 19 code at end.
234 ;; 2005/07/19 dadams
235 ;; Added: icomplete-nb-candidates-face.
236 ;; icomplete-completions: Add number of icomplete candidates.
237 ;; Append number of other cycle candidates (icicle).
238 ;; 2005/05/29 dadams
239 ;; read-from-minibuffer: Updated to deal with new arg in Emacs 22.
240 ;; 2004/12/02 dadams
241 ;; Highlight keys (icomplete-completions).
242 ;; 2004/09/21 dadams
243 ;; Removed (icomplete-mode 99) at end.
244 ;; 2004/04/13 dadams
245 ;; I'm not sure that some of the "enhancements" here are still
246 ;; needed. This code was written long ago. In particular, I'm not
247 ;; sure that the changes to `icomplete-exhibit' and the
248 ;; redefinitions of the Emacs primitives are needed. Even if they
249 ;; are not needed, I'm leaving them in, as they are benign :).
250 ;; 1995/12/15 dadams
251 ;; Defined replacements that reset minibuffer-completion-table to avoid
252 ;; icompletion: read-string, read-from-minibuffer, read-no-blanks-input.
253 ;; 1995/11/30 dadams
254 ;; Added redefinition of yes-or-no-p.
255 ;; 1995/10/17 dadams
256 ;; 1) Added icomplete-choices-face and icomplete-determined-face.
257 ;; 2) Redefined icomplete-exhibit: Doesn't insert if input
258 ;; begins with `(' (e.g. repeat-complex-command).
259 ;;
260 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
261 ;;
262 ;; This program is free software; you can redistribute it and/or modify
263 ;; it under the terms of the GNU General Public License as published by
264 ;; the Free Software Foundation; either version 2, or (at your option)
265 ;; any later version.
266
267 ;; This program is distributed in the hope that it will be useful,
268 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
269 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
270 ;; GNU General Public License for more details.
271
272 ;; You should have received a copy of the GNU General Public License
273 ;; along with this program; see the file COPYING. If not, write to
274 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
275 ;; Floor, Boston, MA 02110-1301, USA.
276 ;;
277 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
278 ;;
279 ;;; Code:
280
281 (require 'icomplete)
282
283 (when (> emacs-major-version 23) ; Emacs 24.3+ actually, so soft-require.
284 (require 'cl-lib nil t)) ; cl-delete-if
285
286 ;; Quiet the byte-compiler.
287 (defvar completion-all-sorted-completions)
288 (defvar icomplete-eoinput)
289 (defvar icomplete-hide-common-prefix)
290 (defvar icomplete-minibuffer-map)
291 (defvar icomplete-show-matches-on-no-input)
292 (defvar icomplete-with-completion-tables)
293 (defvar icompletep-include-menu-items-flag)
294 (defvar icompletep-ORIG-icomplete-minibuffer-map)
295 (defvar icompletep-prospects-length)
296
297 (defvar icicle-nb-of-other-cycle-candidates)
298
299 ;;;;;;;;;;;;;;;;;;;
300
301 ;;;###autoload
302 (defgroup Icomplete-Plus nil
303 "Icomplete Enhancements."
304 :prefix "icompletep-"
305 :group 'completion :group 'convenience :group 'matching :group 'minibuffer
306 :link `(url-link :tag "Send Bug Report"
307 ,(concat "mailto:" "drew.adams" "@" "oracle" ".com?subject=\
308 icomplete+.el bug: \
309 &body=Describe bug here, starting with `emacs -q'. \
310 Don't forget to mention your Emacs and library versions."))
311 :link '(url-link :tag "Other Libraries by Drew"
312 "http://www.emacswiki.org/cgi-bin/wiki/DrewsElispLibraries")
313 :link '(url-link :tag "Download"
314 "http://www.emacswiki.org/cgi-bin/wiki/icomplete+.el")
315 :link '(url-link :tag "Description"
316 "http://www.emacswiki.org/cgi-bin/wiki/IcompleteMode#IcompleteModePlus")
317 :link '(emacs-commentary-link :tag "Commentary" "icomplete+"))
318
319 ;;;###autoload
320 (defcustom icompletep-exact-separator (if (> emacs-major-version 22)
321 (string ?\u2605 ?\ ) ; star
322 ;; (string ?\u2714 ?\ ) ; check mark
323 ;; (string ?\u29eb ?\ ) ; diamond
324 ;; (string ?\u2205 ?\ ) ; empty set
325 "* ")
326 "String used by to separate exact match from other alternatives."
327 :type 'string :group 'Icomplete-Plus)
328
329 (unless (boundp 'icomplete-show-key-bindings) ; Emacs 24.3+
330 (defcustom icomplete-show-key-bindings t
331 "*Non-nil means show key bindings as well as completion for sole match."
332 :type 'boolean :group 'icomplete))
333
334 (when (boundp 'icomplete-show-key-bindings) ; Emacs 20-24.2
335 (defcustom icomplete-separator " | "
336 "String used to separate completion alternatives."
337 :type 'string :group 'icomplete :version "24.4"))
338
339 (when (< emacs-major-version 23)
340 (defcustom icompletep-prospects-length 100 ; Default was 80
341 "*Length of string displaying icompletion candidates."
342 :type 'integer :group 'Icomplete-Plus))
343
344 (when (> emacs-major-version 22)
345 (defcustom icompletep-include-menu-items-flag t
346 "*Non-nil means include menu bindings in the list of keys for a command."
347 :type 'boolean :group 'Icomplete-Plus))
348
349 ;;;###autoload
350 (defface icompletep-choices
351 '((((background dark)) (:foreground "Snow4"))
352 (t (:foreground "DarkBlue")))
353 "*Face for minibuffer reminder of possible completion suffixes."
354 :group 'Icomplete-Plus)
355
356 ;;;###autoload
357 (defface icompletep-determined
358 '((t (:foreground "SeaGreen")))
359 "*Face for minibuffer reminder of possible completion prefix."
360 :group 'Icomplete-Plus)
361
362 ;;;###autoload
363 (defface icompletep-nb-candidates
364 '((((background dark)) (:foreground "SpringGreen"))
365 (t (:foreground "DarkMagenta")))
366 "*Face for minibuffer reminder of number of completion candidates.
367 This has no effect unless library `icicles.el' is being used."
368 :group 'Icomplete-Plus)
369
370 ;;;###autoload
371 (defface icompletep-keys
372 '((t (:foreground "Red")))
373 "*Face for minibuffer reminder of possible completion key bindings."
374 :group 'Icomplete-Plus)
375
376 ;;; Quiet the byte-compiler.
377 (defvar icomplete-overlay)
378 (defvar icomplete-prospects-height)
379
380
381
382 ;; REPLACES ORIGINAL defined in `icomplete.el':
383 ;;
384 ;; Save match-data.
385 ;; Don't insert if input begins with `(' (e.g. `repeat-complex-command').
386 ;;
387 (when (< emacs-major-version 23) ; Emacs 20, 21, 22.
388 (defun icomplete-exhibit ()
389 "Insert icomplete completions display.
390 Should be run via minibuffer `post-command-hook'.
391 See `icomplete-mode' and `minibuffer-setup-hook'."
392 (when (icomplete-simple-completing-p)
393 (save-match-data
394 (let* ((minibuf-begin (if (< emacs-major-version 21)
395 (point-min)
396 (minibuffer-prompt-end)))
397 (contents (buffer-substring minibuf-begin (point-max)))
398 (buffer-undo-list t))
399 (save-excursion
400 (goto-char (point-max))
401 ;; Register the end of input, so we know where the extra stuff
402 ;; (match-status info) begins:
403 (unless (boundp 'icomplete-eoinput)
404 ;; In case it got wiped out by major mode business:
405 (make-local-variable 'icomplete-eoinput))
406 (setq icomplete-eoinput (point))
407 ;; Insert the match-status information:
408 (when (and (> (point-max) minibuf-begin)
409 (save-excursion ; Do nothing if looking at a list, string, etc.
410 (goto-char minibuf-begin)
411 (not (looking-at ; No (, ", ', 9 etc. at start.
412 "\\(\\s-+$\\|\\s-*\\(\\s(\\|\\s\"\\|\\s'\\|\\s<\\|[0-9]\\)\\)")))
413 (or
414 ;; Do not bother with delay after certain number of chars:
415 (> (point-max) icomplete-max-delay-chars)
416 ;; Do not delay if alternatives number is small enough:
417 (if minibuffer-completion-table
418 (cond ((numberp minibuffer-completion-table)
419 (< minibuffer-completion-table
420 icomplete-delay-completions-threshold))
421 ((sequencep minibuffer-completion-table)
422 (< (length minibuffer-completion-table)
423 icomplete-delay-completions-threshold))))
424 ;; Delay - give some grace time for next keystroke, before
425 ;; embarking on computing completions:
426 (sit-for icomplete-compute-delay)))
427 (insert
428 (icomplete-completions contents minibuffer-completion-table
429 minibuffer-completion-predicate
430 (not minibuffer-completion-confirm)))))
431 (setq deactivate-mark nil)))))) ; Don't let the insert deactivate the mark.
432
433
434 ;;; These two macros are defined in `subr.el' for Emacs 23+.
435 ;;; They are included here only so you can, if needed, byte-compile this file using Emacs < 23
436 ;;; and still use the byte-compiled file in Emacs 23+.
437 (unless (fboundp 'with-local-quit)
438 (defmacro with-local-quit (&rest body)
439 "Execute BODY, allowing quits to terminate BODY but not escape further.
440 When a quit terminates BODY, `with-local-quit' returns nil but
441 requests another quit. That quit will be processed as soon as quitting
442 is allowed once again. (Immediately, if `inhibit-quit' is nil.)"
443 `(condition-case nil
444 (let ((inhibit-quit nil))
445 ,@body)
446 (quit (setq quit-flag t)
447 ;; This call is to give a chance to handle quit-flag
448 ;; in case inhibit-quit is nil.
449 ;; Without this, it will not be handled until the next function
450 ;; call, and that might allow it to exit thru a condition-case
451 ;; that intends to handle the quit signal next time.
452 (eval '(ignore nil)))))
453 (put 'with-local-quit 'common-lisp-indent-function '(&body)))
454
455 (unless (fboundp 'while-no-input)
456 (defmacro while-no-input (&rest body) ; Defined in `subr.el'.
457 "Execute BODY only as long as there's no pending input.
458 If input arrives, that ends the execution of BODY,
459 and `while-no-input' returns t. Quitting makes it return nil.
460 If BODY finishes, `while-no-input' returns whatever value BODY produced."
461 (let ((catch-sym (make-symbol "input")))
462 `(with-local-quit
463 (catch ',catch-sym
464 (let ((throw-on-input ',catch-sym))
465 (or (input-pending-p) (progn ,@body)))))))
466 (put 'while-no-input 'common-lisp-indent-function '(&body)))
467
468
469
470 ;; REPLACES ORIGINAL defined in `icomplete.el':
471 ;;
472 ;; 1. Do not include menu items unless `icompletep-include-menu-items-flag'.
473 ;; 2. Do not wrap with `<...>', since:
474 ;; (a) not needed because we do not include `Matched;', so [...] suffices
475 ;; (b) `<...>' is also used for function keys, so it would be confusing
476 ;; 3. If string of keys would be too long then shorten it using `...',
477 ;; or if even `[ ... ]' would be too long then return `TOO-LONG' so the
478 ;; brackets can be removed altogether.
479 ;;
480 (defun icomplete-get-keys (func-name &optional exactp)
481 "Return strings naming keys bound to FUNC-NAME, or nil if none.
482 Examines the prior, not current, buffer, presuming that current buffer
483 is minibuffer.
484
485 Non-nil optional arg EXACTP means FUNC-NAME is an exact match, as
486 determined by `try-completion' or `completion-try-completion.
487
488 If option `icompletep-include-menu-items-flag' is non-nil then include
489 menu-bar bindings in the l of keys (Emacs 23+ only)."
490 (when (commandp func-name)
491 (save-excursion
492 (let* ((sym (intern func-name))
493 (buf (other-buffer nil t))
494 (keys (with-current-buffer buf (where-is-internal sym)))
495 (max-len (max 0 (- (window-width)
496 (if (fboundp 'minibuffer-prompt-end)
497 (minibuffer-prompt-end)
498 (point-max))
499 (if (fboundp 'minibuffer-prompt-end) (length func-name) 0)
500 (if exactp 7 9) ; 6 SPC, () around FUNC-NAME, 1 SPC after )
501 5)))) ; 1 space + 2 each for `[ ' and ` ]'
502 (when keys
503 (unless (and (boundp 'icompletep-include-menu-items-flag)
504 icompletep-include-menu-items-flag)
505 (setq keys (icompletep-remove-if (lambda (ky)
506 (and (vectorp ky) (eq (aref ky 0) 'menu-bar)))
507 keys)))
508 (setq keys (mapconcat 'key-description
509 (sort keys #'(lambda (x y) (< (length x) (length y))))
510 ", "))
511 (cond ((zerop max-len) (setq keys 'TOO-LONG))
512 ((> (length keys) max-len)
513 (setq keys (concat (substring keys 0 (max 0 (- max-len 5))) "...")))))
514 keys))))
515
516 ;; Same as `icicle-remove-if' in `icicles-fn.el'.
517 (defun icompletep-remove-if (pred xs)
518 "A copy of list XS with no elements that satisfy predicate PRED."
519 (let ((result ()))
520 (dolist (x xs) (unless (funcall pred x) (push x result)))
521 (nreverse result)))
522
523
524 ;; REPLACES ORIGINAL defined in `icomplete.el':
525 ;;
526 ;; Save match-data.
527 ;; Don't insert if input begins with `(' (e.g. `repeat-complex-command').
528 ;;
529 (when (and (> emacs-major-version 22) ; Emacs 23 through Emacs 24.3.
530 (or (< emacs-major-version 24)
531 (and (= emacs-major-version 24) (< emacs-minor-version 4))))
532 (defun icomplete-exhibit ()
533 "Insert icomplete completions display.
534 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
535 and `minibuffer-setup-hook'."
536 (when (and icomplete-mode (icomplete-simple-completing-p))
537 (with-current-buffer (window-buffer (active-minibuffer-window))
538 (save-excursion
539 (goto-char (point-max))
540 ;; Insert the match-status information.
541 (when (and (> (point-max) (minibuffer-prompt-end))
542 buffer-undo-list ; Wait for some user input.
543 (save-excursion ; Do nothing if looking at a list, string, etc.
544 (goto-char (minibuffer-prompt-end))
545 (save-match-data
546 (not (looking-at ; No (, ", ', 9 etc. at start.
547 "\\(\\s-+$\\|\\s-*\\(\\s(\\|\\s\"\\|\\s'\\|\\s<\\|[0-9]\\)\\)"))))
548 (or
549 ;; Do not bother with delay after certain number of chars:
550 (> (- (point) (field-beginning)) icomplete-max-delay-chars)
551 ;; Do not delay if completions are known.
552 completion-all-sorted-completions
553 ;; Do not delay if alternatives number is small enough:
554 (and (sequencep minibuffer-completion-table)
555 (< (length minibuffer-completion-table)
556 icomplete-delay-completions-threshold))
557 ;; Delay - give some grace time for next keystroke, before
558 ;; embarking on computing completions:
559 (sit-for icomplete-compute-delay)))
560 (let ((text (while-no-input (icomplete-completions
561 (field-string)
562 minibuffer-completion-table
563 minibuffer-completion-predicate
564 (not minibuffer-completion-confirm))))
565 (buffer-undo-list t)
566 deactivate-mark)
567 ;; Do nothing if `while-no-input' was aborted.
568 (when (stringp text)
569 (move-overlay icomplete-overlay (point) (point) (current-buffer))
570 ;; The current C cursor code doesn't know to use the overlay's
571 ;; marker's stickiness to figure out whether to place the cursor
572 ;; before or after the string, so let's spoon-feed it the pos.
573 (put-text-property 0 1 'cursor t text)
574 (overlay-put icomplete-overlay 'after-string text)))))))))
575
576
577 ;; REPLACES ORIGINAL defined in `icomplete.el':
578 ;;
579 ;; Save match-data.
580 ;; Don't insert if input begins with `(' (e.g. `repeat-complex-command').
581 ;;
582 (when (or (> emacs-major-version 24) ; Emacs 24.4+
583 (and (= emacs-major-version 24) (> emacs-minor-version 3))
584 (and (= emacs-major-version 24) (string-match-p "24.3.50" emacs-version))) ;@@@@ TO REMOVE
585 (defun icomplete-exhibit ()
586 "Insert icomplete completions display.
587 Should be run via minibuffer `post-command-hook'. See `icomplete-mode'
588 and `minibuffer-setup-hook'."
589 (when (and icomplete-mode (icomplete-simple-completing-p)) ;Shouldn't be necessary.
590 (with-current-buffer (window-buffer (active-minibuffer-window))
591 (save-excursion
592 (goto-char (point-max))
593 ;; Insert the match-status information.
594 (when (and (or (and (boundp 'icomplete-show-matches-on-no-input)
595 icomplete-show-matches-on-no-input)
596 (> (icomplete--field-end) (icomplete--field-beg)))
597 (save-excursion ; Do nothing if looking at a list, string, etc.
598 (goto-char (icomplete--field-end))
599 (save-match-data
600 (not (looking-at ; No (, ", ', 9 etc. at start.
601 "\\(\\s-+$\\|\\s-*\\(\\s(\\|\\s\"\\|\\s'\\|\\s<\\|[0-9]\\)\\)"))))
602 (or
603 ;; Do not bother with delay after certain number of chars:
604 (> (- (point) (icomplete--field-beg)) icomplete-max-delay-chars)
605 ;; Do not delay if completions are known.
606 completion-all-sorted-completions
607 ;; Do not delay if alternatives number is small enough:
608 (and (sequencep (icomplete--completion-table))
609 (< (length (icomplete--completion-table))
610 icomplete-delay-completions-threshold))
611 ;; Delay - give some grace time for next keystroke, before
612 ;; embarking on computing completions:
613 (sit-for icomplete-compute-delay)))
614 (let* ((field-string (icomplete--field-string))
615 (text (while-no-input (icomplete-completions
616 field-string
617 (icomplete--completion-table)
618 (icomplete--completion-predicate)
619 (and (window-minibuffer-p)
620 (not minibuffer-completion-confirm)))))
621 (buffer-undo-list t)
622 deactivate-mark)
623 ;; Do nothing if `while-no-input' was aborted.
624 (when (stringp text)
625 (move-overlay icomplete-overlay (point) (point) (current-buffer))
626 ;; The current C cursor code doesn't know to use the overlay's
627 ;; marker's stickiness to figure out whether to place the cursor
628 ;; before or after the string, so let's spoon-feed it the pos.
629 (put-text-property 0 1 'cursor t text)
630 (overlay-put icomplete-overlay 'after-string text)))))))))
631
632
633 ;; REPLACES ORIGINAL defined in `icomplete.el':
634 ;;
635 ;; 1. Prepends total number of candidates.
636 ;; 2. Sorts alternatives, puts them in a different face, and separates them more.
637 ;; 3. Highlights key-binding text, truncating it if too long.
638 ;; 4. Appends number of remaining cycle candidates (for Icicles).
639 ;;
640 (when (< emacs-major-version 23) ; Emacs 20, 21, 22.
641 (defun icomplete-completions (name candidates predicate require-match)
642 "Identify prospective candidates for minibuffer completion.
643 NAME is the name to complete.
644 CANDIDATES are the candidates to match.
645 PREDICATE filters matches: they succeed only if it returns non-nil.
646 REQUIRE-MATCH non-nil means the input must match a candidate.
647
648 The display is updated with each minibuffer keystroke during
649 minibuffer completion.
650
651 Prospective completion suffixes (if any) are displayed, bracketed by
652 \"()\", \"[]\", or \"{}\". The choice of brackets is as follows:
653
654 \(...) - A single prospect is identified, and matching is enforced.
655 \[...] - A single prospect is identified, and matching is optional.
656 \{...} - Multiple prospects are indicated, and further input is
657 needed to distinguish a single one.
658
659 The displays for unambiguous matches have ` [ Matched ]' appended
660 \(whether complete or not), or ` \[ No matches ]', if no eligible
661 matches exist. Keybindings for uniquely matched commands are
662 shown within brackets, [] (without the word \"Matched\"), if there is
663 room.
664
665 When more than one completion is available, the total number precedes
666 the suffixes display, like this:
667 M-x forw 14 (ard-) { char line list...}
668
669 If library `icicles.el' is also loaded, then you can cycle
670 completions. When you change cycling direction, the number of
671 additional cycle candidates, besides the current one, is displayed
672 following the rest of the icomplete info:
673 M-x forward-line [Matched] (13 more)."
674 ;; `all-completions' doesn't like empty `minibuffer-completion-table's (ie: (nil))
675 (when (and (listp candidates) (null (car candidates))) (setq candidates ()))
676 (let* ((comps (all-completions name candidates predicate))
677 (open-bracket-determined (if require-match "(" " ["))
678 (close-bracket-determined (if require-match ") " "] "))
679 (keys ())
680 (nb-candidates (length comps))
681 nb-candidates-string)
682 ;; `concat'/`mapconcat' is the slow part. With the introduction of
683 ;; `icompletep-prospects-length', there is no need for `catch'/`throw'.
684 (if (null comps) (format (if (fboundp 'icicle-apropos-complete)
685 "\t%sNo prefix matches%s"
686 "\t%sNo matches%s")
687 open-bracket-determined
688 close-bracket-determined)
689 (let* ((most-try (try-completion name (mapcar #'list comps)))
690 (most (if (stringp most-try) most-try (car comps)))
691 (most-len (length most))
692 (determ (and (> most-len (length name))
693 (concat open-bracket-determined
694 (substring most (length name))
695 close-bracket-determined)))
696 (open-bracket-prospects "{ ")
697 (close-bracket-prospects " }")
698 (prospects-len 0)
699 prompt prompt-rest prospects most-is-exact comp)
700 (when determ
701 (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
702 (if (eq most-try t)
703 (setq prospects ())
704 (while (and comps (< prospects-len icompletep-prospects-length))
705 (setq comp (substring (car comps) most-len)
706 comps (cdr comps))
707 (cond ((string= comp "") (setq most-is-exact t))
708 ((member comp prospects))
709 (t (setq prospects (cons comp prospects)
710 prospects-len (+ prospects-len
711 (string-width comp)
712 (string-width icomplete-separator)))))))
713 (setq prompt-rest
714 (if prospects
715 (concat open-bracket-prospects
716 (and most-is-exact icompletep-exact-separator)
717 (mapconcat 'identity (sort prospects (function string-lessp))
718 icomplete-separator)
719 (and comps "...")
720 close-bracket-prospects)
721 (setq keys (and icomplete-show-key-bindings
722 (commandp (intern-soft most))
723 (icomplete-get-keys most (eq t most-try))))
724 (if (eq keys 'TOO-LONG) ; No room even for ` [ ... ]'.
725 ""
726 (concat " [ " (and (not keys) "Matched") keys " ]"))))
727 (unless (string= "" prompt-rest)
728 (put-text-property 0 (length prompt-rest) 'face 'icompletep-choices prompt-rest))
729 (cond ((< nb-candidates 2)
730 (setq prompt (concat " " determ prompt-rest)) ; 6 spaces.
731 (when (eq last-command this-command)
732 (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
733 (t
734 (setq nb-candidates-string (format "%7d " nb-candidates))
735 (put-text-property (string-match "\\S-" nb-candidates-string)
736 (1- (length nb-candidates-string))
737 'face 'icompletep-nb-candidates nb-candidates-string)
738 (setq prompt (concat nb-candidates-string determ prompt-rest))))
739 ;; Highlight keys.
740 (when (stringp keys)
741 (put-text-property (+ 9 (length determ)) (1- (length prompt))
742 'face 'icompletep-keys prompt))
743 ;; Append mention of number of other cycle candidates (from `icicles.el').
744 (when (and (boundp 'icicle-last-completion-candidate)
745 (> icicle-nb-of-other-cycle-candidates 0)
746 (= 1 nb-candidates)
747 icicle-last-completion-candidate
748 (not (eq last-command this-command)))
749 (setq nb-candidates-string ; Reuse the string.
750 (format " (%d more)" icicle-nb-of-other-cycle-candidates))
751 (put-text-property (string-match "\\S-" nb-candidates-string)
752 (length nb-candidates-string)
753 'face 'icompletep-nb-candidates nb-candidates-string)
754 (setq prompt (concat prompt nb-candidates-string)))
755 prompt)))))
756
757
758
759 ;; REPLACES ORIGINAL defined in `icomplete.el':
760 ;;
761 ;; 1. Prepends total number of candidates.
762 ;; 2. Sorts alternatives alphabetically, puts them in a different face, and separates them more.
763 ;; 3. Highlights key-binding text, truncating it if too long.
764 ;; 4. Appends number of remaining cycle candidates (for Icicles).
765 ;;
766 (when (= emacs-major-version 23)
767 (defun icomplete-completions (name collection predicate require-match)
768 "Identify prospective candidates for minibuffer completion.
769 NAME is the name to complete.
770 COLLECTION is the collection of candidates to match.
771 PREDICATE filters matches: they succeed only if it returns non-nil.
772 REQUIRE-MATCH non-nil means the input must match a candidate.
773
774 The display is updated with each minibuffer keystroke during
775 minibuffer completion.
776
777 Prospective completion suffixes (if any) are displayed, bracketed by
778 \"()\", \"[]\", or \"{}\". The choice of brackets is as follows:
779
780 \(...) - A single prospect is identified, and matching is enforced.
781 \[...] - A single prospect is identified, and matching is optional.
782 \{...} - Multiple prospects are indicated, and further input is
783 needed to distinguish a single one.
784
785 The displays for unambiguous matches have ` [ Matched ]' appended
786 \(whether complete or not), or ` \[ No matches ]', if no eligible
787 matches exist. Keybindings for uniquely matched commands are
788 shown within brackets, [] (without the word \"Matched\"), if there is
789 room.
790
791 When more than one completion is available, the total number precedes
792 the suffixes display, like this:
793 M-x forw 14 (ard-) { char line list...}
794
795 If library `icicles.el' is also loaded, then you can cycle
796 completions. When you change cycling direction, the number of
797 additional cycle candidates, besides the current one, is displayed
798 following the rest of the icomplete info:
799 M-x forward-line [Matched] (13 more)."
800 ;; `all-completions' doesn't like empty `minibuffer-completion-table's (ie: (nil))
801 (when (and (listp collection) (null (car collection))) (setq collection ()))
802 (let* (;; Do not use `completion-all-sorted-completions' as in vanilla Emacs.
803 ;; We need the number of comps, and we do not need that sort order.
804 ;; (comps (completion-all-sorted-completions))
805
806 ;; Exclude file names with extensions in `completion-ignored-extensions'.
807 (comps (if (and minibuffer-completing-file-name
808 icomplete-with-completion-tables)
809 (completion-pcm--filename-try-filter
810 (all-completions name collection predicate))
811 (all-completions name collection predicate)))
812 (nb-candidates (length comps))
813 (nb-cands-string (if (< nb-candidates 2) "" (format "%7d " nb-candidates)))
814 ;;; We do not use `completion-all-sorted-completions', so we do not need `last' or `base-size'.
815 ;;; $$$$$ (last (if (consp comps) (last comps)))
816 ;;; (base-size (cdr last))
817 (open-bracket (if require-match "(" " ["))
818 (close-bracket (if require-match ") " "] ")))
819 ;; `concat'/`mapconcat' is the slow part.
820 (if (not (consp comps))
821 (format (if (fboundp 'icicle-apropos-complete)
822 "\t%sNo prefix matches%s"
823 "\t%sNo matches%s")
824 open-bracket close-bracket)
825 ;;; $$$$$ (if last (setcdr last ()))
826 (let* ((keys ())
827 (most-try
828 ;; We do not use BASE-SIZE. So we just use `try-completion'.
829 ;;; (if (and base-size (> base-size 0))
830 ;;; (completion-try-completion name collection predicate (length name))
831 ;;; ;; If the COMPS are 0-based, the result should be the same with COMPS.
832 ;;; (completion-try-completion name comps nil (length name))))
833 (try-completion name collection predicate))
834 ;; Since we use `try-completion', MOST-TRY will not be a cons.
835 ;; (most (if (consp most-try) (car most-try) (if most-try (car comps) "")))
836 (most (if (stringp most-try) most-try (car comps)))
837 ;; Compare NAME and MOST, so we can determine if NAME is
838 ;; a prefix of MOST, or something else.
839 (compare (compare-strings name nil nil most nil nil
840 completion-ignore-case))
841 (determ (and (not (or (eq t compare) (eq t most-try)
842 (= (setq compare (1- (abs compare))) (length most))))
843 (concat open-bracket
844 (cond ((= compare (length name)) ; NAME is a prefix
845 (substring most compare))
846 ((< compare 5) most)
847 (t (concat "..." (substring most compare))))
848 close-bracket)))
849 (prospects-len (+ (if (and (boundp 'icicle-mode) icicle-mode (icicle-completing-p))
850 2 ; for `icicle-completion-prompt-overlay'
851 0)
852 (string-width (buffer-string)) ; for prompt
853 (string-width nb-cands-string)
854 (length determ) ; for determined part
855 2 ; for "{ "
856 -2 ; for missing last " " after last candidate
857 5)) ; for "... }"
858 (prospects-max
859 ;; Max total length to use, including the minibuffer content.
860 (* (+ icomplete-prospects-height
861 ;; If the minibuffer content already uses up more than
862 ;; one line, increase the allowable space accordingly.
863 (/ prospects-len (window-width)))
864 (window-width)))
865 (prefix-len
866 ;; Find the common prefix among `comps'.
867 ;; Cannot use the optimization below because its assumptions
868 ;; are not always true, e.g. when completion-cycling (Emacs bug #10850):
869 ;; (if (eq t (compare-strings (car comps) nil (length most)
870 ;; most nil nil completion-ignore-case))
871 ;; ;; Common case.
872 ;; (length most)
873 ;; Else, use try-completion.
874 (let ((comps-prefix (try-completion "" comps)))
875 (and (stringp comps-prefix) (string-width comps-prefix))))
876 prompt prompt-rest prospects most-is-exact comp limit)
877 (when determ
878 (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
879 (if (eq most-try t) ; (or (null (cdr comps))
880 (setq prospects ())
881 (while (and comps (not limit))
882 (setq comp (if prefix-len (substring (car comps) prefix-len) (car comps))
883 comps (cdr comps))
884 (cond ((string= comp "") (setq most-is-exact t))
885 ((member comp prospects))
886 (t (setq prospects-len (+ (string-width comp)
887 (string-width icomplete-separator)
888 prospects-len))
889 (if (< prospects-len prospects-max)
890 (push comp prospects)
891 (setq limit t))))))
892 ;;; $$$$$ ;; Restore the base-size info, since `completion-all-sorted-completions' is cached.
893 ;;; (when last (setcdr last base-size))
894 (setq prompt-rest
895 (if prospects
896 (concat "{ " (and most-is-exact icompletep-exact-separator)
897 (mapconcat 'identity (sort prospects (function string-lessp))
898 icomplete-separator)
899 (and limit "...")
900 " }")
901 (setq keys (and icomplete-show-key-bindings
902 (commandp (intern-soft most))
903 (icomplete-get-keys most (eq t most-try))))
904 (if (eq keys 'TOO-LONG) ; No room even for `[ ... ]'.
905 ""
906 (concat " [ " (and (not keys) "Matched") keys " ]"))))
907 (unless (string= "" prompt-rest)
908 (put-text-property 0 (length prompt-rest) 'face 'icompletep-choices prompt-rest))
909 (cond ((< nb-candidates 2)
910 (setq prompt (concat " " determ prompt-rest)) ; 6 spaces.
911 (when (eq last-command this-command)
912 (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
913 (t
914 (put-text-property (string-match "\\S-" nb-cands-string)
915 (1- (length nb-cands-string))
916 'face 'icompletep-nb-candidates nb-cands-string)
917 (setq prompt (concat nb-cands-string determ prompt-rest))))
918 ;; Highlight keys.
919 (when (stringp keys)
920 (put-text-property (+ 9 (length determ)) (1- (length prompt))
921 'face 'icompletep-keys prompt))
922 ;; Append mention of number of other cycle candidates (from `icicles.el').
923 (when (and (boundp 'icicle-last-completion-candidate)
924 (> icicle-nb-of-other-cycle-candidates 0)
925 (= 1 nb-candidates)
926 icicle-last-completion-candidate
927 (not (eq last-command this-command)))
928 (setq nb-cands-string ; Reuse the string.
929 (format " (%d more)" icicle-nb-of-other-cycle-candidates))
930 (put-text-property (string-match "\\S-" nb-cands-string)
931 (length nb-cands-string)
932 'face 'icompletep-nb-candidates nb-cands-string)
933 (setq prompt (concat prompt nb-cands-string)))
934 prompt)))))
935
936
937 ;; REPLACES ORIGINAL defined in `icomplete.el':
938 ;;
939 ;; 1. Prepend total number of candidates.
940 ;; 2. With Icicles, sort candidates using `icicle-reversible-sort' and show number of remaining
941 ;; cycle candidates. You can cycle the sort order using `C-,'.
942 ;; 3. Show candidates in a different face.
943 ;; 4. Optionally show and highlight key bindings, truncating if too long.
944 ;;
945 (when (and (= emacs-major-version 24) (< emacs-minor-version 4)) ; Emacs 24.1 through Emacs 24.3.
946
947 (defun icomplete-completions (name candidates predicate require-match)
948 "Identify prospective candidates for minibuffer completion.
949 NAME is the name to complete.
950 CANDIDATES is the collection of candidates to match. See
951 `completing-read' for its possible values.
952 PREDICATE filters matches: they succeed only if it returns non-nil.
953 REQUIRE-MATCH non-nil means the input must match a candidate.
954
955 The display is updated with each minibuffer keystroke during
956 minibuffer completion.
957
958 Prospective completion suffixes (if any) are displayed, bracketed by
959 \"()\", \"[]\", or \"{}\". The choice of brackets is as follows:
960
961 \(...) - A single prospect is identified, and matching is enforced.
962 \[...] - A single prospect is identified, and matching is optional.
963 \{...} - Multiple prospects are indicated, and further input is
964 needed to distinguish a single one.
965
966 The displays for unambiguous matches have ` [ Matched ]' appended
967 \(whether complete or not), or ` \[ No matches ]', if no eligible
968 matches exist. Keybindings for uniquely matched commands are
969 shown within brackets, [] (without the word \"Matched\"), if there is
970 room.
971
972 When more than one completion is available, the total number precedes
973 the suffixes display, like this:
974
975 M-x forw 14 (ard-) { char line list...}
976
977 In Icicle mode:
978 * The current Icicles sort order is used.
979 * When cycling candidates and you change direction, the number of
980 other cycling candidates is shown, as `(N more)'. For example:
981
982 M-x forward-line [Matched] (13 more)."
983 ;; `concat'/`mapconcat' is the slow part.
984 (let* ((non-essential t)
985 (icyp (and (fboundp 'icicle-mode) icicle-mode))
986 (open-bracket (if require-match "(" " ["))
987 (close-bracket (if require-match ") " "] "))
988 (md (completion--field-metadata (field-beginning)))
989 (comps (icompletep-completion-all-sorted-completions
990 (and icyp #'icicle-reversible-sort)))
991 (last (and (consp comps) (last comps)))
992 (base-size (cdr last))
993 nb-candidates nb-cands-string)
994 (if (not (consp comps))
995 (format "\t%sNo matches%s" open-bracket close-bracket)
996 (when last (setcdr last ()))
997 (setq nb-candidates (length comps)
998 nb-cands-string (if (< nb-candidates 2) "" (format "%7d " nb-candidates)))
999 (let* ((most-try (if (and base-size (> base-size 0))
1000 (completion-try-completion name candidates predicate
1001 (length name) md)
1002 ;; If COMPS are 0-based, the result should be the same with COMPS.
1003 (completion-try-completion name comps nil (length name) md)))
1004 (most (if (consp most-try) (car most-try) (if most-try (car comps) "")))
1005 ;; Compare NAME & MOST, to determine if NAME is a prefix of MOST, or something else.
1006 (compare (compare-strings name nil nil most nil nil completion-ignore-case))
1007 (determ (and (not (or (eq t compare) (eq t most-try)
1008 (= (setq compare (1- (abs compare))) (length most))))
1009 (concat open-bracket
1010 (cond ((= compare (length name)) ; NAME is a prefix
1011 (substring most compare))
1012 ((< compare 5) most)
1013 (t (concat "..." (substring most compare))))
1014 close-bracket)))
1015 (prospects-len (+ (if (and icyp (icicle-completing-p))
1016 2 ; for `icicle-completion-prompt-overlay'
1017 0)
1018 (string-width (buffer-string)) ; for prompt
1019 (string-width nb-cands-string)
1020 (length determ) ; for determined part
1021 2 ; for "{ "
1022 -2 ; for missing last " " after last candidate
1023 5)) ; for "... }"
1024 ;; Max total length to use, including the minibuffer content.
1025 (prospects-max (* (+ icomplete-prospects-height
1026 ;; If the minibuffer content already uses up more than
1027 ;; one line, increase the allowable space accordingly.
1028 (/ prospects-len (window-width)))
1029 (window-width)))
1030 ;; Find the common prefix among COMPS.
1031 ;; Cannot use the optimization below because its assumptions are not always true,
1032 ;; e.g. when completion-cycling (Emacs bug #10850):
1033 ;; (if (eq t (compare-strings (car comps) nil (length most) most nil nil
1034 ;; completion-ignore-case))
1035 ;; (length most) ; Common case.
1036 ;; Else, use try-completion.
1037 (prefix-len (let ((comps-prefix (try-completion "" comps)))
1038 (and (stringp comps-prefix) (string-width comps-prefix))))
1039 (prospects ())
1040 (keys ())
1041 (limit nil)
1042 (most-is-exact nil)
1043 prompt prompt-rest comp)
1044
1045 (when determ (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
1046 (if (eq most-try t) ; (or (null (cdr comps))
1047 (setq prospects ())
1048 (while (and comps (not limit))
1049 (setq comp (if prefix-len (substring (car comps) prefix-len) (car comps))
1050 comps (cdr comps))
1051 (cond ((string= comp "") (setq most-is-exact t))
1052 ((member comp prospects))
1053 (t (setq prospects-len (+ (string-width comp)
1054 (string-width icomplete-separator)
1055 prospects-len))
1056 (if (< prospects-len prospects-max) (push comp prospects) (setq limit t))))))
1057 ;; Restore BASE-SIZE info, since `completion-all-sorted-completions' is cached.
1058 (when last (setcdr last base-size))
1059 (setq prompt-rest
1060 (if prospects
1061 (concat "{ " (and most-is-exact icompletep-exact-separator)
1062 (mapconcat 'identity (nreverse prospects) icomplete-separator)
1063 (and limit "...")
1064 " }")
1065 (setq keys (and icomplete-show-key-bindings
1066 (commandp (intern-soft most))
1067 (icomplete-get-keys most (eq t most-try))))
1068 (if (eq keys 'TOO-LONG) ; No room even for `[ ... ]'.
1069 ""
1070 (concat " [ " (and (not keys) "Matched") keys " ]"))))
1071 (unless (string= "" prompt-rest)
1072 (put-text-property 0 (length prompt-rest) 'face 'icompletep-choices prompt-rest))
1073 (cond ((< nb-candidates 2)
1074 (setq prompt (concat " " determ prompt-rest)) ; 6 spaces.
1075 (when (eq last-command this-command)
1076 (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
1077 (t
1078 (put-text-property (string-match "\\S-" nb-cands-string)
1079 (1- (length nb-cands-string))
1080 'face 'icompletep-nb-candidates nb-cands-string)
1081 (setq prompt (concat nb-cands-string determ prompt-rest))))
1082 (when (stringp keys) ; Highlight keys.
1083 (put-text-property (+ 9 (length determ)) (1- (length prompt))
1084 'face 'icompletep-keys prompt))
1085 ;; Append mention of number of other cycle candidates (from `icicles.el').
1086 (when (and (boundp 'icicle-last-completion-candidate)
1087 (> icicle-nb-of-other-cycle-candidates 0)
1088 (= 1 nb-candidates)
1089 icicle-last-completion-candidate
1090 (not (eq last-command this-command)))
1091 (setq nb-cands-string ; Reuse the string.
1092 (format " (%d more)" icicle-nb-of-other-cycle-candidates))
1093 (put-text-property (string-match "\\S-" nb-cands-string) (length nb-cands-string)
1094 'face 'icompletep-nb-candidates nb-cands-string)
1095 (setq prompt (concat prompt nb-cands-string)))
1096 prompt))))
1097
1098 ;; I sent a simplified version of this suggestion as Emacs bug #13322.
1099 ;; This version handles `completion-ignored-extensions', including for Icicles absolute file names.
1100 (defun icompletep-completion-all-sorted-completions (&optional sort-function dont-remove-dups)
1101 "Like `completion-all-sorted-completions', but with added optional args.
1102 If SORT-FUNCTION is nil, sort per `completion-all-sorted-completions':
1103 * per property `cycle-sort-function', if defined
1104 * else by shorter length, then by recent use."
1105 (or completion-all-sorted-completions
1106 (let* ((start (field-beginning))
1107 (end (field-end))
1108 (string (buffer-substring start end))
1109 (md (completion--field-metadata start))
1110 (all (completion-all-completions string minibuffer-completion-table
1111 minibuffer-completion-predicate
1112 (- (point) start) md))
1113 (last (last all))
1114 (base-size (or (cdr last) 0))
1115 (all-md (completion--metadata (buffer-substring-no-properties start (point))
1116 base-size md minibuffer-completion-table
1117 minibuffer-completion-predicate))
1118 (sort-fun (or sort-function
1119 (completion-metadata-get all-md 'cycle-sort-function))))
1120 (when last
1121 (setcdr last ())
1122 ;; Do the rest after resetting LAST's cdr to (), since we need a proper list.
1123 ;; Exclude file names with extensions in `completion-ignored-extensions'.
1124 (when (or minibuffer-completing-file-name
1125 (and (boundp 'icicle-abs-file-candidates) icicle-abs-file-candidates))
1126 (let ((ignore-regexp (regexp-opt completion-ignored-extensions))
1127 ;; Only Emacs 24.3+ has library `cl-lib.el', with `cl-delete-if'.
1128 (fun (if (fboundp 'cl-delete-if)
1129 #'cl-delete-if
1130 #'icompletep-remove-if)))
1131 (setq all (funcall fun (lambda (fl) (string-match-p ignore-regexp fl)) all))))
1132 (unless dont-remove-dups (setq all (delete-dups all))) ; Delete duplicates.
1133 (setq last (last all) ; Reset LAST, since it may be a different cons-cell.
1134 all (if sort-fun
1135 (funcall sort-fun all)
1136 (sort all (lambda (c1 c2) (< (length c1) (length c2)))))) ; Shorter first.
1137 (when (and (minibufferp) (not sort-fun))
1138 (let ((hist (symbol-value minibuffer-history-variable)))
1139 (setq all (sort all (lambda (c1 c2) (> (length (member c1 hist)) ; Recent first.
1140 (length (member c2 hist))))))))
1141 ;; Cache result. Not just for speed, but also so repeated calls to
1142 ;; `minibuffer-force-complete' can cycle through all possibilities.
1143
1144 ;; $$$$$$ Temporary fix - will need to change more when `icomplete.el' in Emacs 24.4
1145 ;; becomes more stable. They changed the number of args here from 1 to 3.
1146 (if (fboundp 'icomplete--field-beg) ; Emacs 24.4+
1147 (completion--cache-all-sorted-completions
1148 (icomplete--field-beg) (icomplete--field-end) (nconc all base-size))
1149 (completion--cache-all-sorted-completions (nconc all base-size))))))))
1150
1151
1152 (when (or (> emacs-major-version 24) ; Emacs 24.4+
1153 (and (= emacs-major-version 24) (> emacs-minor-version 3))
1154 (and (= emacs-major-version 24) (string-match-p "24.3.50" emacs-version))) ;@@@@ TO REMOVE
1155
1156
1157 ;; REPLACES ORIGINAL defined in `icomplete.el':
1158 ;;
1159 ;; 1. Prepend total number of candidates.
1160 ;; 2. With Icicles, sort candidates using `icicle-reversible-sort' and show number of remaining
1161 ;; cycle candidates. You can cycle the sort order using `C-,'.
1162 ;; 3. Show candidates in a different face.
1163 ;; 4. Optionally show and highlight key bindings, truncating if too long.
1164 ;;
1165 (defun icomplete-completions (name candidates predicate require-match)
1166 "Identify prospective candidates for minibuffer completion.
1167 NAME is the name to complete.
1168 CANDIDATES is the collection of candidates to match. See
1169 `completing-read' for its possible values.
1170 PREDICATE filters matches: they succeed only if it returns non-nil.
1171 REQUIRE-MATCH non-nil means the input must match a candidate.
1172
1173 The display is updated with each minibuffer keystroke during
1174 minibuffer completion.
1175
1176 Prospective completion suffixes (if any) are displayed, bracketed by
1177 \"()\", \"[]\", or \"{}\". The choice of brackets is as follows:
1178
1179 \(...) - A single prospect is identified, and matching is enforced.
1180 \[...] - A single prospect is identified, and matching is optional.
1181 \{...} - Multiple prospects are indicated, and further input is
1182 needed to distinguish a single one.
1183
1184 The displays for unambiguous matches have ` [ Matched ]' appended
1185 \(whether complete or not), or ` \[ No matches ]', if no eligible
1186 matches exist. Keybindings for uniquely matched commands are
1187 shown within brackets, [] (without the word \"Matched\"), if there is
1188 room.
1189
1190 When more than one completion is available, the total number precedes
1191 the suffixes display, like this:
1192
1193 M-x forw 14 (ard-) { char line list...}
1194
1195 In Icicle mode:
1196 * The current Icicles sort order is used.
1197 * When cycling candidates and you change direction, the number of
1198 other cycling candidates is shown, as `(N more)'. For example:
1199
1200 M-x forward-line [Matched] (13 more)."
1201 ;; `concat'/`mapconcat' is the slow part.
1202 (let* ((non-essential t)
1203 (minibuffer-completion-table candidates)
1204 (minibuffer-completion-predicate predicate)
1205 (icyp (and (fboundp 'icicle-mode) icicle-mode))
1206 (open-bracket (if require-match "(" " ["))
1207 (close-bracket (if require-match ") " "] "))
1208 (md (completion--field-metadata (icomplete--field-beg)))
1209 (comps (icompletep-completion-all-sorted-completions
1210 (icomplete--field-beg) (icomplete--field-end)
1211 (and icyp #'icicle-reversible-sort)))
1212 (last (and (consp comps) (last comps)))
1213 (base-size (cdr last))
1214 nb-candidates nb-cands-string)
1215 (if (not (consp comps))
1216 (format "\t%sNo matches%s" open-bracket close-bracket)
1217 (when last (setcdr last ()))
1218 (setq nb-candidates (length comps)
1219 nb-cands-string (if (< nb-candidates 2) "" (format "%7d " nb-candidates)))
1220 (let* ((most-try (if (and base-size (> base-size 0))
1221 (completion-try-completion
1222 name candidates predicate (length name) md)
1223 ;; If COMPS are 0-based, the result should be the same with COMPS.
1224 (completion-try-completion name comps nil (length name) md)))
1225 (most (if (consp most-try) (car most-try) (if most-try (car comps) "")))
1226 ;; Compare NAME & MOST, to determine if NAME is a prefix of MOST, or something else.
1227 (compare (compare-strings name nil nil most nil nil completion-ignore-case))
1228 (determ (and (not (or (eq t compare) (eq t most-try)
1229 (= (setq compare (1- (abs compare))) (length most))))
1230 (concat open-bracket
1231 (cond ((= compare (length name)) ; NAME is a prefix
1232 (substring most compare))
1233 ((< compare 5) most)
1234 (t (concat "..." (substring most compare))))
1235 close-bracket)))
1236 (prospects-len (+ (if (and icyp (icicle-completing-p))
1237 2 ; for `icicle-completion-prompt-overlay'
1238 0)
1239 (string-width (buffer-string)) ; for prompt
1240 (string-width nb-cands-string)
1241 (length determ) ; for determined part
1242 2 ; for "{ "
1243 -2 ; for missing last " " after last candidate
1244 5)) ; for "... }"
1245 ;; Max total length to use, including the minibuffer content.
1246 (prospects-max (* (+ icomplete-prospects-height
1247 ;; If the minibuffer content already uses up more than
1248 ;; one line, increase the allowable space accordingly.
1249 (/ prospects-len (window-width)))
1250 (window-width)))
1251 ;; Find the common prefix among COMPS.
1252 ;; Cannot use the optimization below because its assumptions are not always true,
1253 ;; e.g. when completion-cycling (Emacs bug #10850):
1254 ;; (if (eq t (compare-strings (car comps) nil (length most) most nil nil
1255 ;; completion-ignore-case))
1256 ;; (length most) ; Common case.
1257 ;; Else, use try-completion.
1258 (prefix (and icomplete-hide-common-prefix (try-completion "" comps)))
1259 (prefix-len (and (stringp prefix)
1260 ;; Hide prefix only if the info is already displayed via `most'.
1261 (string-prefix-p prefix most 'IGNORE-CASE)
1262 (string-width prefix)))
1263 (prospects ())
1264 (keys ())
1265 (limit nil)
1266 (most-is-exact nil)
1267 prompt prompt-rest comp)
1268
1269 (when determ (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
1270 (if (or (eq most-try t) (not (consp (cdr comps))))
1271 (setq prospects ())
1272
1273 ;; @@@@@@@@ ???????????
1274 (when (member name comps)
1275 ;; NAME is complete but not unique. This scenario poses following UI issues:
1276 ;; - When `icomplete-hide-common-prefix' is non-nil, NAME is stripped empty.
1277 ;; This would make the entry inconspicuous.
1278 ;; - Due to sorting of completions, NAME may not be the first of the prospects
1279 ;; and could be hidden deep in the displayed string.
1280 ;; - Because of `icomplete-prospects-height', NAME may not even be displayed.
1281 ;;
1282 ;; So provide a visual cue to user via an "empty string" in the try completion field.
1283 (setq determ (concat open-bracket "" close-bracket)))
1284 (while (and comps (not limit))
1285 (setq comp (if prefix-len (substring (car comps) prefix-len) (car comps))
1286 comps (cdr comps))
1287 (cond ((string= comp "") (setq most-is-exact t))
1288 ((member comp prospects))
1289 (t (setq prospects-len (+ (string-width comp)
1290 (string-width icomplete-separator)
1291 prospects-len))
1292 (if (< prospects-len prospects-max) (push comp prospects) (setq limit t))))))
1293 ;; Restore BASE-SIZE info, since `completion-all-sorted-completions' is cached.
1294 (when last (setcdr last base-size))
1295 (setq prompt-rest
1296 (if prospects
1297 (concat "{ " (and most-is-exact icompletep-exact-separator)
1298 (mapconcat 'identity (nreverse prospects) icomplete-separator)
1299 (and limit "...")
1300 " }")
1301 (setq keys (and icomplete-show-key-bindings
1302 (commandp (intern-soft most))
1303 (icomplete-get-keys most (eq t most-try))))
1304 (if (eq keys 'TOO-LONG) ; No room even for `[ ... ]'.
1305 ""
1306 (concat " [ " (and (not keys) "Matched") keys " ]"))))
1307 (unless (string= "" prompt-rest)
1308 (put-text-property 0 (length prompt-rest) 'face 'icompletep-choices prompt-rest))
1309 (cond ((< nb-candidates 2)
1310 (setq prompt (concat " " determ prompt-rest)) ; 6 spaces.
1311 (when (eq last-command this-command)
1312 (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
1313 (t
1314 (put-text-property (string-match "\\S-" nb-cands-string)
1315 (1- (length nb-cands-string))
1316 'face 'icompletep-nb-candidates nb-cands-string)
1317 (setq prompt (concat nb-cands-string determ prompt-rest))))
1318 (when (stringp keys) ; Highlight keys.
1319 (put-text-property (+ 9 (length determ)) (1- (length prompt))
1320 'face 'icompletep-keys prompt))
1321 ;; Append mention of number of other cycle candidates (from `icicles.el').
1322 (when (and (boundp 'icicle-last-completion-candidate)
1323 (> icicle-nb-of-other-cycle-candidates 0)
1324 (= 1 nb-candidates)
1325 icicle-last-completion-candidate
1326 (not (eq last-command this-command)))
1327 (setq nb-cands-string ; Reuse the string.
1328 (format " (%d more)" icicle-nb-of-other-cycle-candidates))
1329 (put-text-property (string-match "\\S-" nb-cands-string) (length nb-cands-string)
1330 'face 'icompletep-nb-candidates nb-cands-string)
1331 (setq prompt (concat prompt nb-cands-string)))
1332 prompt))))
1333
1334
1335 (defun icompletep-completion-all-sorted-completions (&optional start end
1336 sort-function dont-remove-dups)
1337 "Like `completion-all-sorted-completions', but with added optional args.
1338 Non-nil SORT-FUNCTION is a function that sorts the candidates.
1339 If SORT-FUNCTION is nil, sort per `completion-all-sorted-completions':
1340 * per property `cycle-sort-function', if defined
1341 * else by shorter length, then by recent use.
1342 Non-nil DONT-REMOVE-DUPS means do not remove duplicates."
1343 (or completion-all-sorted-completions
1344 (let* ((start (or start (minibuffer-prompt-end)))
1345 (end (or end (point-max)))
1346 (string (buffer-substring start end))
1347 (md (completion--field-metadata start))
1348 (all (completion-all-completions string minibuffer-completion-table
1349 minibuffer-completion-predicate
1350 (- (point) start) md))
1351 (last (last all))
1352 (base-size (or (cdr last) 0))
1353 (all-md (completion--metadata (buffer-substring-no-properties start (point))
1354 base-size md minibuffer-completion-table
1355 minibuffer-completion-predicate))
1356 (sort-fun (or sort-function
1357 (completion-metadata-get all-md 'cycle-sort-function))))
1358 (when last
1359 (setcdr last ())
1360 ;; Do the rest after resetting LAST's cdr to (), since we need a proper list.
1361 ;; Exclude file names with extensions in `completion-ignored-extensions'.
1362 (when (or minibuffer-completing-file-name
1363 (and (boundp 'icicle-abs-file-candidates) icicle-abs-file-candidates))
1364 (let ((ignore-regexp (regexp-opt completion-ignored-extensions)))
1365 (setq all (cl-delete-if (lambda (fl) (string-match-p ignore-regexp fl)) all))))
1366 (unless dont-remove-dups (setq all (delete-dups all))) ; Delete duplicates.
1367 (setq last (last all) ; Reset LAST, since it may be a different cons-cell.
1368 all (if sort-fun
1369 (funcall sort-fun all)
1370 (sort all (lambda (c1 c2) (< (length c1) (length c2)))))) ; Shorter first.
1371 (when (and (minibufferp) (not sort-fun))
1372 (let ((hist (symbol-value minibuffer-history-variable)))
1373 (setq all (sort all (lambda (c1 c2) (> (length (member c1 hist)) ; Recent first.
1374 (length (member c2 hist))))))))
1375 ;; Cache result. Not just for speed, but also so repeated calls to
1376 ;; `minibuffer-force-complete' can cycle through all possibilities.
1377 (completion--cache-all-sorted-completions start end (nconc all base-size)))))))
1378
1379
1380 (defvar icompletep-cycling-mode nil)
1381 (when (boundp 'icomplete-minibuffer-map) ; Emacs 24.4+
1382
1383 (setq icompletep-ORIG-icomplete-minibuffer-map icomplete-minibuffer-map ; Save it and wipe it out.
1384 icomplete-minibuffer-map nil)
1385
1386 ;; Eval this so that even if the library is byte-compiled with Emacs 20,
1387 ;; loading it into Emacs 21+ will define variable `icompletep-cycling-mode'.
1388 (eval '(define-minor-mode icompletep-cycling-mode
1389 "Icomplete mode, with its cycling keys enabled if on, disabled if off.
1390 If off then the cycling keys of Icomplete mode are free for their
1391 normal minibuffer uses. The default Icomplete-mode cycling keys are:
1392
1393 M-TAB\t- complete to exact match, repeat to cycle
1394 C-j\t- complete to first match and exit minibuffer
1395 C-s\t- cycle first entry to end
1396 C-r\t- cycle last entry to start
1397
1398 If you want Icomplete cycling behavior but do not want to use these
1399 keys for it, customize the bindings in `icomplete-minibuffer-map'.
1400
1401 Turning this mode on also turns on Icomplete mode.
1402 Turning it off does not turn Icomplete mode on or off."
1403 nil nil nil
1404 (when icompletep-cycling-mode (icomplete-mode 99)) ; Turn on Icomplete if cycling is on.
1405 (setq icomplete-minibuffer-map (and icompletep-cycling-mode
1406 icompletep-ORIG-icomplete-minibuffer-map))))
1407 (icompletep-cycling-mode -99)) ; Turn it off by default.
1408
1409 ;;;;;;;;;;;;;;;;;;;;;;;
1410
1411 (provide 'icomplete+)
1412
1413 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1414 ;;; icomplete+.el ends here
1415