initial commit
[emacs-init.git] / auto-install / 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
7 ;; Copyright (C) 1996-2011, Drew Adams, all rights reserved.
8 ;; Created: Mon Oct 16 13:33:18 1995
9 ;; Version: 21.0
10 ;; Last-Updated: Wed Aug 24 09:52:00 2011 (-0700)
11 ;;           By: dradams
12 ;;     Update #: 931
13 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/icomplete+.el
14 ;; Keywords: help, abbrev, internal, extensions, local
15 ;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x
16 ;;
17 ;; Features that might be required by this library:
18 ;;
19 ;;   `icomplete'.
20 ;;
21 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
22 ;;
23 ;;; Commentary:
24 ;;
25 ;;    Extensions to `icomplete.el'.
26 ;;
27 ;;  Faces defined here:
28 ;;
29 ;;    `icompletep-choices', `icompletep-determined',
30 ;;    `icompletep-keys', `icompletep-nb-candidates'.
31 ;;
32 ;;  User option defined here:
33 ;;
34 ;;    `icompletep-prospects-length'.
35 ;;
36 ;;  Macros defined here (but identical to those in Emacs 23):
37 ;;
38 ;;    `with-local-quit', `with-no-input'.
39 ;;
40 ;;
41 ;;  ***** NOTE: The following functions defined in `icomplete.el'
42 ;;              have been REDEFINED HERE:
43 ;;
44 ;;    `icomplete-completions' -
45 ;;       Prepends total number of candidates.
46 ;;       Sorts alternatives alphabetically, uses different face.
47 ;;       Highlights key-binding text.
48 ;;       Appends number of remaining cycle candidates (for Icicles).
49 ;;
50 ;;    `icomplete-exhibit' -
51 ;;       Saves match-data.
52 ;;       Doesn't insert if input begins with `('
53 ;;         (e.g. `repeat-complex-command').
54 ;;       Ensures that the insertion doesn't deactivate mark.
55 ;;
56 ;;
57 ;;  This file should be loaded after loading the standard GNU file
58 ;;  `icomplete.el'.  So, in your `~/.emacs' file, do this:
59 ;;  (eval-after-load "icomplete" '(progn (require 'icomplete+)))
60 ;;
61 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
62 ;;
63 ;;; Change log:
64 ;;
65 ;; 2011/08/24 dadams
66 ;;     Added top-level puts for common-lisp-indent-function.
67 ;;     with-local-quit, while-no-input:
68 ;;       Define only if not defined.  Use put for indentation.  Remove declare declaration.
69 ;; 2011/06/05 dadams
70 ;;     icomplete-completions: Handle Emacs 24's new METADATA arg for completion-try-completion.
71 ;; 2011/01/04 dadams
72 ;;     Removed autoload cookies from non def* sexps.  Added them for defgroup, defface.
73 ;; 2010/07/29 dadams
74 ;;     with-local-quit, with-no-input: Protect declare with fboundp.
75 ;; 2009/08/06 dadams
76 ;;     icomplete-completions (Emacs < 23): Bind, don't set, to initialize nb-candidates.
77 ;; 2008/06/01 dadams
78 ;;     icomplete-completions (Emacs 23): Set candidates to nil if ((nil)).
79 ;;     Commented out vanilla Emacs code that's not used (last, base-size).
80 ;; 2008/05/30 dadams
81 ;;     Updated for Emacs 23 - complete rewrite.
82 ;;       Added: macros with-local-quit and with-no-input.
83 ;;       Added and adapted icomplete-exhibit and icomplete-completions for Emacs 23.
84 ;;     icompletep-prospects-length: Use only for Emacs < 23.
85 ;;     icomplete-exhibit: Removed vestigial test of icicle-apropos-completing-p.
86 ;; 2006/07/30 dadams
87 ;;     icomplete-exhibit: Save match-data.
88 ;; 2006/07/16 dadams
89 ;;     Added dark-background face suggestions from Le Wang - thx.
90 ;; 2006/06/18 dadams
91 ;;      icomplete-exhibit: Don't insert if Icicles apropos-completing.
92 ;; 2006/01/07 dadams
93 ;;      Added :link for sending bug report.
94 ;; 2006/01/06 dadams
95 ;;      Added defgroup.  Added :link.
96 ;;      Renamed: prefix icomplete- to icompletep-.
97 ;; 2005/12/18 dadams
98 ;;     Renamed faces without "-face".
99 ;;     Use defface.  Removed require of def-face-const.el.
100 ;;     icomplete-prospects-length: defvar -> defcustom.
101 ;; 2005/09/30 dadams
102 ;;     Commented out redefinitions of primitives, so no longer reset
103 ;;       minibuffer-completion-table to nil. Leaving the commented code in for now.
104 ;; 2005/08/16 dadams
105 ;;     icomplete-completions: If icicles.el is loaded, change no-match message slightly.
106 ;; 2005/07/24 dadams
107 ;;     icomplete-exhibit: Set deactivate-mark to nil at end.
108 ;;     Remove commented Emacs 19 code at end.
109 ;; 2005/07/19 dadams
110 ;;     Added: icomplete-nb-candidates-face.
111 ;;     icomplete-completions: Add number of icomplete candidates.
112 ;;                            Append number of other cycle candidates (icicle).
113 ;; 2005/05/29 dadams
114 ;;     read-from-minibuffer: Updated to deal with new arg in Emacs 22.
115 ;; 2004/12/02 dadams
116 ;;     Highlight keys (icomplete-completions).
117 ;; 2004/09/21 dadams
118 ;;     Removed (icomplete-mode 99) at end.
119 ;; 2004/04/13 dadams
120 ;;     I'm not sure that some of the "enhancements" here are still
121 ;;     needed.  This code was written long ago.  In particular, I'm not
122 ;;     sure that the changes to `icomplete-exhibit' and the
123 ;;     redefinitions of the Emacs primitives are needed.  Even if they
124 ;;     are not needed, I'm leaving them in, as they are benign :).
125 ;; 1995/12/15 dadams
126 ;;     Defined replacements that reset minibuffer-completion-table to avoid
127 ;;     icompletion: read-string, read-from-minibuffer, read-no-blanks-input.
128 ;; 1995/11/30 dadams
129 ;;     Added redefinition of yes-or-no-p.
130 ;; 1995/10/17 dadams
131 ;;     1) Added icomplete-choices-face and icomplete-determined-face.
132 ;;     2) Redefined icomplete-exhibit: Doesn't insert if input
133 ;;        begins with `('  (e.g. repeat-complex-command).
134 ;;
135 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
136 ;;
137 ;; This program is free software; you can redistribute it and/or modify
138 ;; it under the terms of the GNU General Public License as published by
139 ;; the Free Software Foundation; either version 2, or (at your option)
140 ;; any later version.
141
142 ;; This program is distributed in the hope that it will be useful,
143 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
144 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
145 ;; GNU General Public License for more details.
146
147 ;; You should have received a copy of the GNU General Public License
148 ;; along with this program; see the file COPYING.  If not, write to
149 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
150 ;; Floor, Boston, MA 02110-1301, USA.
151 ;;
152 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
153 ;;
154 ;;; Code:
155
156 (require 'icomplete)
157
158 ;; Quiet the byte-compiler.
159 (defvar icomplete-eoinput)
160 (defvar icompletep-prospects-length)
161 (defvar icicle-nb-of-other-cycle-candidates)
162
163 ;;;;;;;;;;;;;;;;;;;
164
165 ;;;###autoload
166 (defgroup Icomplete-Plus nil
167   "Icomplete Enhancements."
168   :prefix "icompletep-"
169   :group 'completion :group 'convenience :group 'matching :group 'minibuffer
170   :link `(url-link :tag "Send Bug Report"
171           ,(concat "mailto:" "drew.adams" "@" "oracle" ".com?subject=\
172 icomplete+.el bug: \
173 &body=Describe bug here, starting with `emacs -q'.  \
174 Don't forget to mention your Emacs and library versions."))
175   :link '(url-link :tag "Other Libraries by Drew"
176           "http://www.emacswiki.org/cgi-bin/wiki/DrewsElispLibraries")
177   :link '(url-link :tag "Download"
178           "http://www.emacswiki.org/cgi-bin/wiki/icomplete+.el")
179   :link '(url-link :tag "Description"
180           "http://www.emacswiki.org/cgi-bin/wiki/IcompleteMode#IcompleteModePlus")
181   :link '(emacs-commentary-link :tag "Commentary" "icomplete+")
182   )
183
184 ;;;###autoload
185 (defface icompletep-choices
186     '((((background dark)) (:foreground "Snow4"))
187       (t (:foreground "DarkBlue")))
188   "*Face for minibuffer reminder of possible completion suffixes."
189   :group 'Icomplete-Plus)
190
191 ;;;###autoload
192 (defface icompletep-determined
193     '((t (:foreground "SeaGreen")))
194   "*Face for minibuffer reminder of possible completion prefix."
195   :group 'Icomplete-Plus)
196
197 ;;;###autoload
198 (defface icompletep-nb-candidates
199   '((((background dark)) (:foreground "SpringGreen"))
200     (t (:foreground "DarkMagenta")))
201   "*Face for minibuffer reminder of number of completion candidates.
202 This has no effect unless library `icicles.el' is being used."
203   :group 'Icomplete-Plus)
204
205 ;;;###autoload
206 (defface icompletep-keys
207     '((t (:foreground "Red")))
208   "*Face for minibuffer reminder of possible completion key bindings."
209   :group 'Icomplete-Plus)
210
211 (when (< emacs-major-version 23)
212   (defcustom icompletep-prospects-length 100 ; Default was 80
213     "*Length of string displaying icompletion candidates."
214     :type 'integer :group 'Icomplete-Plus))
215
216 ;;; Quiet the byte-compiler.
217 (defvar icomplete-overlay)
218 (defvar icomplete-prospects-height)
219
220
221
222 ;; REPLACES ORIGINAL defined in `icomplete.el':
223 ;;
224 ;; Save match-data.
225 ;; Don't insert if input begins with `(' (e.g. `repeat-complex-command').
226 ;;
227 (when (< emacs-major-version 23)        ; Emacs 20, 21, 22.
228   (defun icomplete-exhibit ()
229     "Insert icomplete completions display.
230 Should be run via minibuffer `post-command-hook'.
231 See `icomplete-mode' and `minibuffer-setup-hook'."
232     (when (icomplete-simple-completing-p)
233       (save-match-data
234         (let* ((minibuf-begin (if (< emacs-major-version 21)
235                                   (point-min)
236                                 (minibuffer-prompt-end)))
237                (contents (buffer-substring minibuf-begin (point-max)))
238                (buffer-undo-list t))
239           (save-excursion
240             (goto-char (point-max))
241             ;; Register the end of input, so we know where the extra stuff
242             ;; (match-status info) begins:
243             (unless (boundp 'icomplete-eoinput)
244               ;; In case it got wiped out by major mode business:
245               (make-local-variable 'icomplete-eoinput))
246             (setq icomplete-eoinput (point))
247             ;; Insert the match-status information:
248             (when (and (> (point-max) minibuf-begin)
249                        (save-excursion  ; Do nothing if looking at a list, string, etc.
250                          (goto-char minibuf-begin)
251                          (not (looking-at ; No (, ", ', 9 etc. at start.
252                                "\\(\\s-+$\\|\\s-*\\(\\s(\\|\\s\"\\|\\s'\\|\\s<\\|[0-9]\\)\\)")))
253                        (or
254                         ;; Don't bother with delay after certain number of chars:
255                         (> (point-max) icomplete-max-delay-chars)
256                         ;; Don't delay if alternatives number is small enough:
257                         (if minibuffer-completion-table
258                             (cond ((numberp minibuffer-completion-table)
259                                    (< minibuffer-completion-table
260                                       icomplete-delay-completions-threshold))
261                                   ((sequencep minibuffer-completion-table)
262                                    (< (length minibuffer-completion-table)
263                                       icomplete-delay-completions-threshold))
264                                   ))
265                         ;; Delay - give some grace time for next keystroke, before
266                         ;; embarking on computing completions:
267                         (sit-for icomplete-compute-delay)))
268               (insert
269                (icomplete-completions contents minibuffer-completion-table
270                                       minibuffer-completion-predicate
271                                       (not minibuffer-completion-confirm)))))
272           (setq deactivate-mark nil)))))) ; Don't let the insert deactivate the mark.
273
274
275 ;;; These two macros are defined in `subr.el' for Emacs 23+.
276 ;;; They are included here only so you can, if needed, byte-compile this file using Emacs < 23
277 ;;; and still use the byte-compiled file in Emacs 23+.
278 (unless (fboundp 'with-local-quit)
279   (defmacro with-local-quit (&rest body)
280     "Execute BODY, allowing quits to terminate BODY but not escape further.
281 When a quit terminates BODY, `with-local-quit' returns nil but
282 requests another quit.  That quit will be processed as soon as quitting
283 is allowed once again.  (Immediately, if `inhibit-quit' is nil.)"
284     `(condition-case nil
285       (let ((inhibit-quit nil))
286         ,@body)
287       (quit (setq quit-flag t)
288        ;; This call is to give a chance to handle quit-flag
289        ;; in case inhibit-quit is nil.
290        ;; Without this, it will not be handled until the next function
291        ;; call, and that might allow it to exit thru a condition-case
292        ;; that intends to handle the quit signal next time.
293        (eval '(ignore nil)))))
294   (put 'with-local-quit 'common-lisp-indent-function '(&body)))
295
296 (unless (fboundp 'while-no-input)
297   (defmacro while-no-input (&rest body) ; Defined in `subr.el'.
298     "Execute BODY only as long as there's no pending input.
299 If input arrives, that ends the execution of BODY,
300 and `while-no-input' returns t.  Quitting makes it return nil.
301 If BODY finishes, `while-no-input' returns whatever value BODY produced."
302     (let ((catch-sym (make-symbol "input")))
303       `(with-local-quit
304         (catch ',catch-sym
305           (let ((throw-on-input ',catch-sym))
306             (or (input-pending-p)
307                 (progn ,@body)))))))
308   (put 'while-no-input 'common-lisp-indent-function '(&body)))
309
310
311
312 ;; REPLACES ORIGINAL defined in `icomplete.el':
313 ;;
314 ;; Save match-data.
315 ;; Don't insert if input begins with `(' (e.g. `repeat-complex-command').
316 ;;
317 (when (> emacs-major-version 22)        ; Emacs 23+
318   (defun icomplete-exhibit ()
319     "Insert icomplete completions display.
320 Should be run via minibuffer `post-command-hook'.  See `icomplete-mode'
321 and `minibuffer-setup-hook'."
322     (when (and icomplete-mode (icomplete-simple-completing-p))
323       (save-excursion
324         (goto-char (point-max))
325         ;; Insert the match-status information.
326         (when (and (> (point-max) (minibuffer-prompt-end))
327                    buffer-undo-list     ; Wait for some user input.
328                    (save-excursion      ; Do nothing if looking at a list, string, etc.
329                      (goto-char (minibuffer-prompt-end))
330                      (save-match-data
331                        (not (looking-at ; No (, ", ', 9 etc. at start.
332                              "\\(\\s-+$\\|\\s-*\\(\\s(\\|\\s\"\\|\\s'\\|\\s<\\|[0-9]\\)\\)"))))
333                    (or
334                     ;; Don't bother with delay after certain number of chars:
335                     (> (- (point) (field-beginning)) icomplete-max-delay-chars)
336                     ;; Don't delay if alternatives number is small enough:
337                     (and (sequencep minibuffer-completion-table)
338                          (< (length minibuffer-completion-table)
339                             icomplete-delay-completions-threshold))
340                     ;; Delay - give some grace time for next keystroke, before
341                     ;; embarking on computing completions:
342                     (sit-for icomplete-compute-delay)))
343           (let ((text             (while-no-input
344                                    (icomplete-completions
345                                     (field-string)
346                                     minibuffer-completion-table
347                                     minibuffer-completion-predicate
348                                     (not minibuffer-completion-confirm))))
349                 (buffer-undo-list t)
350                 deactivate-mark)
351             ;; Do nothing if `while-no-input' was aborted.
352             (when (stringp text)
353               (move-overlay icomplete-overlay (point) (point) (current-buffer))
354               ;; The current C cursor code doesn't know to use the overlay's
355               ;; marker's stickiness to figure out whether to place the cursor
356               ;; before or after the string, so let's spoon-feed it the pos.
357               (put-text-property 0 1 'cursor t text)
358               (overlay-put icomplete-overlay 'after-string text))))))))
359
360
361
362 ;; REPLACES ORIGINAL defined in `icomplete.el':
363 ;;
364 ;; 1. Prepends total number of candidates.
365 ;; 2. Sorts alternatives, puts them in a different face, and separates them more.
366 ;; 3. Highlights key-binding text.
367 ;; 4. Appends number of remaining cycle candidates (for Icicles).
368 ;;
369 (when (< emacs-major-version 23)        ; Emacs 20, 21, 22.
370   (defun icomplete-completions (name candidates predicate require-match)
371     "Identify prospective candidates for minibuffer completion.
372 NAME is the name to complete.
373 CANDIDATES are the candidates to match.
374 PREDICATE filters matches: they succeed only if this returns non-nil.
375 REQUIRE-MATCH non-nil means the input must match a candidate.
376
377 The display is updated with each minibuffer keystroke during
378 minibuffer completion.
379
380 Prospective completion suffixes (if any) are displayed, bracketed by
381 \"()\", \"[]\", or \"{}\".  The choice of brackets is as follows:
382
383   \(...) - A single prospect is identified and matching is enforced.
384   \[...] - A single prospect is identified and matching is optional.
385   \{...} - Multiple prospects are indicated, and further input is
386           needed to distinguish a single one.
387
388 The displays for unambiguous matches have \" [ Matched ]\" appended
389 \(whether complete or not), or \" \[ No match ]\", if no eligible
390 matches exist.
391 Keybindings for uniquely matched commands are displayed within the [].
392
393 When more than one completion is available, the total number precedes
394 the suffixes display, like this:
395   M-x forw    14 (ard-) { char line list...}
396
397 If library `icicles.el' is also loaded, then you can cycle
398 completions.  When you change cycling direction, the number of
399 additional cycle candidates, besides the current one, is displayed
400 following the rest of the icomplete info:
401   M-x forward-line   [Matched]  (13 more)."
402     ;; `all-completions' doesn't like empty `minibuffer-completion-table's (ie: (nil))
403     (when (and (listp candidates) (null (car candidates))) (setq candidates nil))
404     (let* ((comps (all-completions name candidates predicate))
405            (open-bracket-determined (if require-match "(" " ["))
406            (close-bracket-determined (if require-match ") " "] "))
407            (keys nil)
408            (nb-candidates (length comps))
409            nb-candidates-string)
410       ;; `concat'/`mapconcat' is the slow part.  With the introduction of
411       ;; `icompletep-prospects-length', there is no need for `catch'/`throw'.
412       (if (null comps) (format (if (fboundp 'icicle-apropos-complete)
413                                    "\t%sNo prefix matches%s"
414                                  "\t%sNo matches%s")
415                                open-bracket-determined
416                                close-bracket-determined)
417         (let* ((most-try (try-completion name (mapcar #'list comps)))
418                (most (if (stringp most-try) most-try (car comps)))
419                (most-len (length most))
420                (determ (and (> most-len (length name))
421                             (concat open-bracket-determined
422                                     (substring most (length name))
423                                     close-bracket-determined)))
424                (open-bracket-prospects "{ ")
425                (close-bracket-prospects " }")
426                (prospects-len 0)
427                prompt prompt-rest prospects most-is-exact comp)
428           (when determ
429             (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
430           (if (eq most-try t)
431               (setq prospects nil)
432             (while (and comps (< prospects-len icompletep-prospects-length))
433               (setq comp (substring (car comps) most-len)
434                     comps (cdr comps))
435               (cond ((string-equal comp "") (setq most-is-exact t))
436                     ((member comp prospects))
437                     (t (setq prospects (cons comp prospects)
438                              prospects-len (+ (length comp) 1 prospects-len))))))
439           (setq prompt-rest
440                 (if prospects
441                     (concat open-bracket-prospects
442                             (and most-is-exact ", ")
443                             (mapconcat 'identity
444                                        (sort prospects (function string-lessp))
445                                        "  ")
446                             (and comps "...")
447                             close-bracket-prospects)
448                   (concat "\t[ Matched"
449                           (if (setq keys (and icomplete-show-key-bindings
450                                               (commandp (intern-soft most))
451                                               (icomplete-get-keys most)))
452                               (concat "; " keys)
453                             (setq keys nil))
454                           " ]")))
455           (put-text-property 0 (length prompt-rest)
456                              'face 'icompletep-choices prompt-rest)
457           (cond ((< nb-candidates 2)
458                  (setq prompt (concat "      " determ prompt-rest))
459                  (when (eq last-command this-command)
460                    (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
461                 (t
462                  (setq nb-candidates-string (format "%7d " nb-candidates))
463                  (put-text-property (string-match "\\S-" nb-candidates-string)
464                                     (1- (length nb-candidates-string))
465                                     'face 'icompletep-nb-candidates nb-candidates-string)
466                  (setq prompt (concat nb-candidates-string determ prompt-rest))))
467           ;; Highlight keys, after "Matched; " (18 chars).
468           (when keys (put-text-property (+ 18 (length determ)) (1- (length prompt))
469                                         'face 'icompletep-keys prompt))
470           ;; Append mention of number of other cycle candidates (from `icicles.el').
471           (when (and (boundp 'icicle-last-completion-candidate)
472                      (> icicle-nb-of-other-cycle-candidates 0)
473                      (= 1 nb-candidates)
474                      icicle-last-completion-candidate
475                      (not (eq last-command this-command)))
476             (setq nb-candidates-string  ; Reuse the string.
477                   (format "  (%d more)" icicle-nb-of-other-cycle-candidates))
478             (put-text-property (string-match "\\S-" nb-candidates-string)
479                                (length nb-candidates-string)
480                                'face 'icompletep-nb-candidates nb-candidates-string)
481             (setq prompt (concat prompt nb-candidates-string)))
482           prompt)))))
483
484
485
486 ;; REPLACES ORIGINAL defined in `icomplete.el':
487 ;;
488 ;; 1. Prepends total number of candidates.
489 ;; 2. Sorts alternatives alphabetically, puts them in a different face, and separates them more.
490 ;; 3. Highlights key-binding text.
491 ;; 4. Appends number of remaining cycle candidates (for Icicles).
492 ;;
493 (when (> emacs-major-version 22)        ; Emacs 23.
494   (defun icomplete-completions (name candidates predicate require-match)
495     "Identify prospective candidates for minibuffer completion.
496 NAME is the name to complete.
497 CANDIDATES are the candidates to match.
498 PREDICATE filters matches: they succeed only if it returns non-nil.
499 REQUIRE-MATCH non-nil means the input must match a candidate.
500
501 The display is updated with each minibuffer keystroke during
502 minibuffer completion.
503
504 Prospective completion suffixes (if any) are displayed, bracketed by
505 \"()\", \"[]\", or \"{}\".  The choice of brackets is as follows:
506
507   \(...) - A single prospect is identified, and matching is enforced.
508   \[...] - A single prospect is identified, and matching is optional.
509   \{...} - Multiple prospects are indicated, and further input is
510           needed to distinguish a single one.
511
512 The displays for unambiguous matches have ` [ Matched ]' appended
513 \(whether complete or not), or ` \[ No matches ]', if no eligible
514 matches exist.  \(Keybindings for uniquely matched commands are
515 exhibited within brackets, [].)
516
517 When more than one completion is available, the total number precedes
518 the suffixes display, like this:
519   M-x forw    14 (ard-) { char line list...}
520
521 If library `icicles.el' is also loaded, then you can cycle
522 completions.  When you change cycling direction, the number of
523 additional cycle candidates, besides the current one, is displayed
524 following the rest of the icomplete info:
525   M-x forward-line   [Matched]  (13 more)."
526     ;; `all-completions' doesn't like empty `minibuffer-completion-table's (ie: (nil))
527     (when (and (listp candidates) (null (car candidates))) (setq candidates nil))
528     (let* (;; Don't use `completion-all-sorted-completions' as in vanilla Emacs.
529            ;; We need the number of comps, and we don't need that sort order.
530            ;; (comps (completion-all-sorted-completions))
531            (comps (all-completions name candidates predicate))
532            (nb-candidates (length comps))
533 ;;; We don't use `completion-all-sorted-completions', so we don't need `last' or `base-size'.
534 ;;; $$$$$      (last (if (consp comps) (last comps)))
535 ;;;            (base-size (cdr last))
536            (open-bracket (if require-match "(" " ["))
537            (close-bracket (if require-match ") " "] ")))
538       ;; `concat'/`mapconcat' is the slow part.
539       (if (not (consp comps))
540           (format (if (fboundp 'icicle-apropos-complete)
541                       "\t%sNo prefix matches%s"
542                     "\t%sNo matches%s")
543                   open-bracket close-bracket)
544 ;;; $$$$$   (if last (setcdr last nil))
545         (let* ((mdata  (and (fboundp 'completion--field-metadata)
546                             (completion--field-metadata (field-beginning))))
547                (most-try
548 ;;; $$$$$           (if (and base-size (> base-size 0))
549 ;;;                     (completion-try-completion name candidates predicate (length name))
550 ;;;                   ;; If `comps' are 0-based, result should be the same with `comps'.
551
552                 ;; $$$$$$$$ UNLESS BUG #8795 is fixed, need METADATA even if nil.
553                 (if (fboundp 'completion--field-metadata) ; Emacs 24 added a 5th arg, METADATA.
554                     (completion-try-completion name comps nil (length name) mdata)
555                   (completion-try-completion name comps nil (length name))))
556                (most (if (consp most-try) (car most-try) (if most-try (car comps) "")))
557                ;; Compare name and most, so we can determine if name is
558                ;; a prefix of most, or something else.
559                (compare (compare-strings name nil nil most nil nil completion-ignore-case))
560                (determ (and (not (or (eq t compare) (eq t most-try)
561                                      (= (setq compare (1- (abs compare))) (length most))))
562                             (concat open-bracket
563                                     (cond ((= compare (length name)) ; Typical: name is a prefix
564                                            (substring most compare))
565                                           ((< compare 5) most)
566                                           (t (concat "..." (substring most compare))))
567                                     close-bracket)))
568                (prospects-len (+ (string-width (buffer-string)) ; for prompt
569                                  8      ; for `nb-candidates-string': "%7d "
570                                  (length determ) ; for determined part
571                                  2      ; for "{ "
572                                  -2     ; for missing last "  " after last candidate
573                                  5))    ; for "... }"
574                (prospects-max
575                 ;; Max total length to use, including the minibuffer content.
576                 (* (+ icomplete-prospects-height
577                       ;; If the minibuffer content already uses up more than
578                       ;; one line, increase the allowable space accordingly.
579                       (/ prospects-len (window-width)))
580                    (window-width)))
581                (prefix-len
582                 ;; Find the common prefix among `comps'.
583                 (if (eq t (compare-strings (car comps) nil (length most) most nil nil
584                                            completion-ignore-case))
585                     (length most)       ; Common case.
586                   (let ((comps-prefix (try-completion "" comps)))
587                     (and (stringp comps-prefix) (length comps-prefix)))))
588                (keys nil)
589                prompt nb-candidates-string prompt-rest
590                prospects most-is-exact comp limit)
591           (when determ
592             (put-text-property 0 (length determ) 'face 'icompletep-determined determ))
593           (if (eq most-try t)           ; (or (null (cdr comps))
594               (setq prospects nil)
595             (while (and comps (not limit))
596               (setq comp   (if prefix-len (substring (car comps) prefix-len) (car comps))
597                     comps  (cdr comps))
598               (cond ((string-equal comp "") (setq most-is-exact t))
599                     ((member comp prospects))
600                     (t (setq prospects-len (+ (string-width comp)
601                                               2 ; for "  "
602                                               prospects-len))
603                        (if (< prospects-len prospects-max)
604                            (push comp prospects)
605                          (setq limit t))))))
606 ;;; $$$$$    ;; Restore the base-size info, since `completion-all-sorted-completions' is cached.
607 ;;;          (when last (setcdr last base-size))
608           (setq prompt-rest
609                 (if prospects
610                     (concat "{ " (and most-is-exact ", ")
611                             (mapconcat 'identity (sort prospects (function string-lessp)) "  ")
612                             (and limit "...") " }")
613                   (concat "\t[ Matched"
614                           (if (setq keys (and icomplete-show-key-bindings
615                                               (commandp (intern-soft most))
616                                               (icomplete-get-keys most)))
617                               (concat "; " keys)
618                             (setq keys nil))
619                           " ]")))
620           (put-text-property 0 (length prompt-rest) 'face 'icompletep-choices prompt-rest)
621           (cond ((< nb-candidates 2)
622                  (setq prompt (concat "      " determ prompt-rest))
623                  (when (eq last-command this-command)
624                    (setq icicle-nb-of-other-cycle-candidates 0))) ; We know now, so reset it.
625                 (t
626                  (setq nb-candidates-string (format "%7d " nb-candidates))
627                  (put-text-property (string-match "\\S-" nb-candidates-string)
628                                     (1- (length nb-candidates-string))
629                                     'face 'icompletep-nb-candidates nb-candidates-string)
630                  (setq prompt (concat nb-candidates-string determ prompt-rest))))
631           ;; Highlight keys, after "Matched; " (18 chars).
632           (when keys (put-text-property (+ 18 (length determ)) (1- (length prompt))
633                                         'face 'icompletep-keys prompt))
634           ;; Append mention of number of other cycle candidates (from `icicles.el').
635           (when (and (boundp 'icicle-last-completion-candidate)
636                      (> icicle-nb-of-other-cycle-candidates 0)
637                      (= 1 nb-candidates)
638                      icicle-last-completion-candidate
639                      (not (eq last-command this-command)))
640             (setq nb-candidates-string  ; Reuse the string.
641                   (format "  (%d more)" icicle-nb-of-other-cycle-candidates))
642             (put-text-property (string-match "\\S-" nb-candidates-string)
643                                (length nb-candidates-string)
644                                'face 'icompletep-nb-candidates nb-candidates-string)
645             (setq prompt (concat prompt nb-candidates-string)))          
646           prompt)))))
647
648
649
650 ;;; The following functions have been REDEFINED to reset the
651 ;;; `minibuffer-completion-table' in order to avoid icompletion.
652 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
653
654
655 ;; Note:  The function `read-input' is an alias for `read-string'.
656
657 ;; (or (fboundp 'old-read-string)
658 ;; (fset 'old-read-string (symbol-function 'read-string)))
659
660 ;; ;; REPLACES ORIGINAL:
661 ;; ;; Resets `minibuffer-completion-table' to avoid icompletion.
662 ;; (defsubst read-string
663 ;;   (prompt &optional initial-input history default-value inherit-input-method)
664 ;;   "Read a string from the minibuffer, prompting with string PROMPT.
665 ;; If non-nil, second arg INITIAL-INPUT is a string to insert before
666 ;;     reading.  This argument has been superseded by DEFAULT-VALUE and
667 ;;     should normally be `nil' in new code.  It behaves as in
668 ;;     `read-from-minibuffer'.  See the documentation for that function.
669 ;; The third arg HISTORY, if non-nil, specifies a history list and
670 ;;     optionally the initial position in that list.
671 ;;     See `read-from-minibuffer' for details of argument HISTORY.
672 ;; Fourth arg DEFAULT-VALUE is the default value.  If non-nil, it is used
673 ;;     for history commands and as the value to return if the user enters
674 ;;     an empty string.
675 ;; Fifth arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer
676 ;;     inherits the current input method and setting of
677 ;;     `enable-multibyte-characters'."
678 ;;   (setq minibuffer-completion-table nil) ; So won't icomplete by default.
679 ;;   (old-read-string prompt initial-input history default-value inherit-input-method))
680
681
682 ;; (or (fboundp 'old-read-from-minibuffer)
683 ;; (fset 'old-read-from-minibuffer (symbol-function 'read-from-minibuffer)))
684
685 ;; ;; REPLACES ORIGINAL:
686 ;; ;; Resets `minibuffer-completion-table' to avoid icompletion.
687 ;; (defsubst read-from-minibuffer
688 ;;   (prompt &optional initial-contents keymap read hist default-value
689 ;;           inherit-input-method keep-all)
690 ;;   "Read a string from the minibuffer, prompting with string PROMPT.
691 ;; The optional second arg INITIAL-CONTENTS is an obsolete alternative to
692 ;;   DEFAULT-VALUE.  It normally should be nil in new code, except when
693 ;;   HIST is a cons.  It is discussed in more detail below.
694 ;; Third arg KEYMAP is a keymap to use while reading;
695 ;;   if omitted or nil, the default is `minibuffer-local-map'.
696 ;; If fourth arg READ is non-nil, then interpret the result as a Lisp
697 ;;   object and return that object.  In other words, do this:
698 ;;       `(car (read-from-string INPUT-STRING))'
699 ;; Fifth arg HIST, if non-nil, specifies a history list and optionally
700 ;;   the initial position in the list.
701 ;;   It can be a symbol, which is the history list variable to use,
702 ;;   or it can be a cons cell (HISTVAR . HISTPOS).
703 ;;   In that case, HISTVAR is the history-list variable to use,
704 ;;   and HISTPOS is the initial position for use by the minibuffer
705 ;;   history commands.  For consistency, you should also specify that
706 ;;   element of the history as the value of INITIAL-CONTENTS.
707 ;;   Positions are counted starting from 1 at the beginning of the list.
708 ;; Sixth arg DEFAULT-VALUE is the default value.  If non-nil, it is
709 ;;   available for history commands; but, unless READ is non-nil,
710 ;;   `read-from-minibuffer' does NOT return DEFAULT-VALUE if the user
711 ;;   enters empty input!  It returns the empty string.
712 ;; Seventh arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer
713 ;;   inherits the current input method and the setting of
714 ;;   `enable-multibyte-characters'.
715 ;; Eighth arg KEEP-ALL, if non-nil, says to put all inputs in the history
716 ;;  list, even empty or duplicate inputs. (This argument is not available
717 ;;  in Emacs versions prior to Emacs 22.)
718 ;; If variable `minibuffer-allow-text-properties' is non-nil, then the
719 ;;   string returned includes whatever text properties were present in
720 ;;   the minibuffer.  Otherwise the value has no text properties.
721
722 ;; The remainder of this documentation describes INITIAL-CONTENTS in more
723 ;; detail.  It is relevant only when studying existing code, or when HIST
724 ;; is a cons.  If non-nil, INITIAL-CONTENTS is a string to be inserted
725 ;; into the minibuffer before reading input.  Normally, point is put at
726 ;; the end of that string.  However, if INITIAL-CONTENTS is (STRING .
727 ;; POSITION), the initial input is STRING, but point is placed at
728 ;; _one-indexed_ position POSITION in the minibuffer.  Any integer value
729 ;; less than or equal to one puts point at the beginning of the string.
730 ;; *Note* that this behavior differs from the way such arguments are used
731 ;; in `completing-read' and some related functions, which use
732 ;; zero-indexing for POSITION."
733 ;;   (setq minibuffer-completion-table nil) ; So won't icomplete by default.
734 ;;   (if (or (string-match "22." emacs-version) (string-match "21.3.50" emacs-version))
735 ;;       (old-read-from-minibuffer prompt initial-contents keymap read hist
736 ;;                                 default-value inherit-input-method keep-all)
737 ;;     (old-read-from-minibuffer prompt initial-contents keymap read hist
738 ;;                               default-value inherit-input-method))) ; No KEEP-ALL
739
740
741 ;; (or (fboundp 'old-read-no-blanks-input)
742 ;; (fset 'old-read-no-blanks-input (symbol-function 'read-no-blanks-input)))
743
744 ;; ;; REPLACES ORIGINAL:
745 ;; ;; Resets `minibuffer-completion-table' to avoid icompletion.
746 ;; (defsubst read-no-blanks-input (prompt &optional initial-contents inherit-input-method)
747 ;;   "Read a string from the minibuffer, not allowing blanks.
748 ;; Arg PROMPT is a prompt string.  Whitespace terminates the input.
749
750 ;; If optional second arg INITIAL-CONTENTS is non-nil, it should be a
751 ;; string, which is used as initial input, with point positioned at the
752 ;; end, so that a SPACE will accept the input.  INITIAL-CONTENTS can
753 ;; alternatively be a cons of a string and an integer.  Such values are
754 ;; treated as in `read-from-minibuffer', but are normally not useful in
755 ;; this function.
756
757 ;; Third arg INHERIT-INPUT-METHOD, if non-nil, means the minibuffer
758 ;; inherits the current input method and the setting of
759 ;; `enable-multibyte-characters'."
760 ;;   (setq minibuffer-completion-table nil) ; So won't icomplete by default.
761 ;;   (old-read-no-blanks-input prompt initial-contents inherit-input-method))
762
763 ;;;;;;;;;;;;;;;;;;;;;;;
764
765 (provide 'icomplete+)
766
767 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
768 ;;; icomplete+.el ends here
769