initial commit
[emacs-init.git] / auto-install / icicles-mcmd.el
1 ;;; icicles-mcmd.el --- Minibuffer commands for Icicles
2 ;;
3 ;; Filename: icicles-mcmd.el
4 ;; Description: Minibuffer commands for Icicles
5 ;; Author: Drew Adams
6 ;; Maintainer: Drew Adams
7 ;; Copyright (C) 1996-2011, Drew Adams, all rights reserved.
8 ;; Created: Mon Feb 27 09:25:04 2006
9 ;; Version: 22.0
10 ;; Last-Updated: Fri Sep  9 10:26:30 2011 (-0700)
11 ;;           By: dradams
12 ;;     Update #: 17246
13 ;; URL: http://www.emacswiki.org/cgi-bin/wiki/icicles-mcmd.el
14 ;; Keywords: internal, extensions, help, abbrev, local, minibuffer,
15 ;;           keys, apropos, completion, matching, regexp, command
16 ;; Compatibility: GNU Emacs: 20.x, 21.x, 22.x, 23.x
17 ;;
18 ;; Features that might be required by this library:
19 ;;
20 ;;   `apropos', `apropos-fn+var', `backquote', `bytecomp', `cl',
21 ;;   `doremi', `el-swank-fuzzy', `ffap', `ffap-', `fuzzy',
22 ;;   `fuzzy-match', `hexrgb', `icicles-face', `icicles-fn',
23 ;;   `icicles-mac', `icicles-opt', `icicles-var', `image-dired',
24 ;;   `kmacro', `levenshtein', `mouse3', `mwheel', `pp', `pp+',
25 ;;   `regexp-opt', `ring', `ring+', `thingatpt', `thingatpt+',
26 ;;   `wid-edit', `wid-edit+', `widget'.
27 ;;
28 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
29 ;;
30 ;;; Commentary:
31 ;;
32 ;;  This is a helper library for library `icicles.el'.  It defines
33 ;;  commands to be used mainly in the minibuffer or buffer
34 ;;  `*Completions*' (and a few non-interactive functions used in those
35 ;;  commands).  For top-level commands, see `icicles-cmd1.el' and
36 ;;  `icicles-cmd2.el'.  For Icicles documentation, see
37 ;;  `icicles-doc1.el' and `icicles-doc2.el'.
38 ;;
39 ;;  Commands defined here:
40 ;;
41 ;;    `cycle-icicle-image-file-thumbnail',
42 ;;    `icicle-abort-recursive-edit', `icicle-add-file-to-fileset',
43 ;;    `icicle-add/update-saved-completion-set',
44 ;;    `icicle-all-candidates-action',
45 ;;    `icicle-all-candidates-alt-action',
46 ;;    `icicle-all-candidates-list-action',
47 ;;    `icicle-all-candidates-list-alt-action',
48 ;;    `icicle-apropos-complete', `icicle-apropos-complete-and-exit',
49 ;;    `icicle-apropos-complete-and-narrow',
50 ;;    `icicle-apropos-complete-and-widen',
51 ;;    `icicle-apropos-complete-no-display',
52 ;;    `icicle-backward-char-dots',
53 ;;    `icicle-backward-delete-char-untabify',
54 ;;    `icicle-backward-kill-paragraph',
55 ;;    `icicle-backward-kill-sentence', `icicle-backward-kill-sexp',
56 ;;    `icicle-backward-kill-word', `icicle-beginning-of-line+',
57 ;;    `icicle-candidate-action', `icicle-candidate-alt-action',
58 ;;    `icicle-candidate-read-fn-invoke',
59 ;;    `icicle-candidate-set-complement',
60 ;;    `icicle-candidate-set-define',
61 ;;    `icicle-candidate-set-difference',
62 ;;    `icicle-candidate-set-intersection',
63 ;;    `icicle-candidate-set-retrieve',
64 ;;    `icicle-candidate-set-retrieve-from-variable',
65 ;;    `icicle-candidate-set-retrieve-more',
66 ;;    `icicle-candidate-set-retrieve-persistent',
67 ;;    `icicle-candidate-set-save', `icicle-candidate-set-save-more',
68 ;;    `icicle-candidate-set-save-more-selected',
69 ;;    `icicle-candidate-set-save-persistently',
70 ;;    `icicle-candidate-set-save-selected',
71 ;;    `icicle-candidate-set-save-to-variable',
72 ;;    `icicle-candidate-set-swap', `icicle-candidate-set-truncate',
73 ;;    `icicle-candidate-set-union',
74 ;;    `icicle-change-alternative-sort-order',
75 ;;    `icicle-change-history-variable', `icicle-change-sort-order',
76 ;;    `icicle-choose-completion', `icicle-completing-read+insert',
77 ;;    `icicle-Completions-mouse-3-menu',
78 ;;    `icicle-cycle-image-file-thumbnail',
79 ;;    `icicle-delete-backward-char', `icicle-delete-candidate-object',
80 ;;    `icicle-delete-char', `icicle-delete-windows-on',
81 ;;    `icicle-describe-file', `icicle-digit-argument',
82 ;;    `icicle-dispatch-C-^', `icicle-dispatch-C-.',
83 ;;    `icicle-dispatch-C-x.', `icicle-dispatch-M-_',
84 ;;    `icicle-dispatch-M-comma', `icicle-dispatch-M-q',
85 ;;    `icicle-doremi-candidate-width-factor+',
86 ;;    `icicle-doremi-increment-max-candidates+',
87 ;;    `icicle-doremi-increment-swank-prefix-length+',
88 ;;    `icicle-doremi-increment-swank-timeout+',
89 ;;    `icicle-doremi-inter-candidates-min-spaces+',
90 ;;    `icicle-doremi-zoom-Completions+', `icicle-end-of-line+',
91 ;;    `icicle-erase-minibuffer',
92 ;;    `icicle-erase-minibuffer-or-history-element',
93 ;;    `icicle-exit-minibuffer', `icicle-forward-char-dots',
94 ;;    `icicle-goto/kill-failed-input', `icicle-help-on-candidate',
95 ;;    `icicle-help-on-next-apropos-candidate',
96 ;;    `icicle-help-on-next-prefix-candidate',
97 ;;    `icicle-help-on-previous-apropos-candidate',
98 ;;    `icicle-help-on-previous-prefix-candidate',
99 ;;    `icicle-help-string-completion',
100 ;;    `icicle-help-string-non-completion', `icicle-history',
101 ;;    `icicle-insert-completion', `icicle-insert-dot-command',
102 ;;    `icicle-insert-history-element',
103 ;;    `icicle-insert-key-description',
104 ;;    `icicle-insert-list-join-string',
105 ;;    `icicle-insert-newline-in-minibuffer',
106 ;;    `icicle-insert-string-at-point',
107 ;;    `icicle-insert-string-from-variable', `icicle-isearch-complete',
108 ;;    `icicle-keep-only-past-inputs', `icicle-kill-line',
109 ;;    `icicle-kill-paragraph', `icicle-kill-region',
110 ;;    `icicle-kill-region-wimpy', `icicle-kill-sentence',
111 ;;    `icicle-kill-sexp', `icicle-kill-word', `icicle-make-directory',
112 ;;    `icicle-minibuffer-complete-and-exit', `icicle-minibuffer-help',
113 ;;    `icicle-mouse-candidate-action',
114 ;;    `icicle-mouse-candidate-alt-action',
115 ;;    `icicle-mouse-candidate-read-fn-invoke',
116 ;;    `icicle-mouse-candidate-set-save',
117 ;;    `icicle-mouse-candidate-set-save-more',
118 ;;    `icicle-mouse-choose-completion',
119 ;;    `icicle-mouse-help-on-candidate',
120 ;;    `icicle-mouse-remove-candidate',
121 ;;    `icicle-mouse-save/unsave-candidate',
122 ;;    `icicle-mouse-save-then-kill', `icicle-mouse-yank-secondary',
123 ;;    `icicle-move-to-next-completion',
124 ;;    `icicle-move-to-previous-completion',
125 ;;    `icicle-narrow-candidates',
126 ;;    `icicle-narrow-candidates-with-predicate',
127 ;;    `icicle-negative-argument', `icicle-next-apropos-candidate',
128 ;;    `icicle-next-apropos-candidate-action',
129 ;;    `icicle-next-apropos-candidate-alt-action',
130 ;;    `icicle-next-candidate-per-mode',
131 ;;    `icicle-next-candidate-per-mode-action',
132 ;;    `icicle-next-candidate-per-mode-alt-action',
133 ;;    `icicle-next-history-element', `icicle-next-line',
134 ;;    `icicle-next-prefix-candidate',
135 ;;    `icicle-next-prefix-candidate-action',
136 ;;    `icicle-next-prefix-candidate-alt-action',
137 ;;    `icicle-next-S-TAB-completion-method',
138 ;;    `icicle-next-TAB-completion-method', `icicle-other-history',
139 ;;    `icicle-plus-saved-sort',
140 ;;    `icicle-pp-eval-expression-in-minibuffer',
141 ;;    `icicle-prefix-complete', `icicle-prefix-complete-no-display',
142 ;;    `icicle-prefix-word-complete',
143 ;;    `icicle-previous-apropos-candidate',
144 ;;    `icicle-previous-apropos-candidate-action',
145 ;;    `icicle-previous-apropos-candidate-alt-action',
146 ;;    `icicle-previous-candidate-per-mode',
147 ;;    `icicle-previous-candidate-per-mode-action',
148 ;;    `icicle-previous-candidate-per-mode-alt-action',
149 ;;    `icicle-previous-line', `icicle-previous-prefix-candidate',
150 ;;    `icicle-previous-prefix-candidate-action',
151 ;;    `icicle-previous-prefix-candidate-alt-action',
152 ;;    `icicle-read+insert-file-name', `icicle-regexp-quote-input',
153 ;;    `icicle-remove-candidate', `icicle-remove-Completions-window',
154 ;;    `icicle-resolve-file-name', `icicle-retrieve-last-input',
155 ;;    `icicle-retrieve-next-input', `icicle-retrieve-previous-input',
156 ;;    `icicle-reverse-sort-order',
157 ;;    `icicle-save-predicate-to-variable',
158 ;;    `icicle-save/unsave-candidate',
159 ;;    `icicle-scroll-Completions-backward',
160 ;;    `icicle-scroll-Completions-forward', `icicle-scroll-backward',
161 ;;    `icicle-scroll-forward', `icicle-search-define-replacement',
162 ;;    `icicle-self-insert', `icicle-sit-for',
163 ;;    `icicle-sort-alphabetical', `icicle-sort-by-abbrev-frequency',
164 ;;    `icicle-sort-by-directories-first',
165 ;;    `icicle-sort-by-directories-last', `icicle-sort-by-file-type',
166 ;;    `icicle-sort-by-last-file-modification-time',
167 ;;    `icicle-sort-by-last-use-as-input',
168 ;;    `icicle-sort-by-previous-use-alphabetically',
169 ;;    `icicle-sort-by-2nd-parts-alphabetically',
170 ;;    `icicle-sort-case-insensitive',
171 ;;    `icicle-sort-extra-candidates-first',
172 ;;    `icicle-sort-proxy-candidates-first',
173 ;;    `icicle-sort-special-candidates-first',
174 ;;    `icicle-sort-turned-OFF', `icicle-switch-to-Completions-buf',
175 ;;    `icicle-switch-to-completions',
176 ;;    `icicle-switch-to/from-minibuffer', `icicle-toggle-.',
177 ;;    `icicle-toggle-~-for-home-dir',
178 ;;    `icicle-toggle-alternative-sorting',
179 ;;    `icicle-toggle-angle-brackets',
180 ;;    `icicle-toggle-case-sensitivity', `icicle-toggle-C-for-actions',
181 ;;    `icicle-toggle-dot', `icicle-toggle-expand-to-common-match',
182 ;;    `icicle-toggle-hiding-common-match',
183 ;;    `icicle-toggle-hiding-non-matching-lines',
184 ;;    `icicle-toggle-highlight-all-current',
185 ;;    `icicle-toggle-highlight-historical-candidates',
186 ;;    `icicle-toggle-highlight-saved-candidates',
187 ;;    `icicle-toggle-ignored-extensions',
188 ;;    `icicle-toggle-ignored-space-prefix',
189 ;;    `icicle-toggle-ignoring-comments',
190 ;;    `icicle-toggle-incremental-completion',
191 ;;    `icicle-toggle-literal-replacement',
192 ;;    `icicle-toggle-proxy-candidates', `icicle-toggle-regexp-quote',
193 ;;    `icicle-toggle-remote-file-testing',
194 ;;    `icicle-toggle-search-cleanup',
195 ;;    `icicle-toggle-search-complementing-domain',
196 ;;    `icicle-toggle-search-replace-common-match',
197 ;;    `icicle-toggle-search-replace-whole',
198 ;;    `icicle-toggle-search-whole-word',
199 ;;    `icicle-toggle-show-multi-completion', `icicle-toggle-sorting',
200 ;;    `icicle-toggle-transforming',
201 ;;    `icicle-toggle-WYSIWYG-Completions', `icicle-transpose-chars',
202 ;;    `icicle-transpose-sexps', `icicle-transpose-words',
203 ;;    `icicle-universal-argument', `icicle-universal-argument-minus',
204 ;;    `icicle-universal-argument-more',
205 ;;    `icicle-universal-argument-other-key', `icicle-up-directory',
206 ;;    `icicle-use-interactive-command-history',
207 ;;    `icicle-widen-candidates', `icicle-yank', `icicle-yank-pop',
208 ;;    `icicle-yank-secondary', `old-choose-completion',
209 ;;    `old-exit-minibuffer', `old-minibuffer-complete-and-exit',
210 ;;    `old-sit-for', `old-switch-to-completions', `toggle-icicle-.',
211 ;;    `toggle-icicle-~-for-home-dir',
212 ;;    `toggle-icicle-alternative-sorting',
213 ;;    `toggle-icicle-angle-brackets',
214 ;;    `toggle-icicle-case-sensitivity', `toggle-icicle-C-for-actions',
215 ;;    `toggle-icicle-dot', `toggle-icicle-expand-to-common-match',
216 ;;    `toggle-icicle-hiding-common-match',
217 ;;    `toggle-icicle-hiding-non-matching-lines',
218 ;;    `toggle-icicle-highlight-all-current',
219 ;;    `toggle-icicle-highlight-historical-candidates',
220 ;;    `toggle-icicle-highlight-saved-candidates',
221 ;;    `toggle-icicle-ignored-extensions',
222 ;;    `toggle-icicle-ignored-space-prefix',
223 ;;    `toggle-icicle-incremental-completion',
224 ;;    `toggle-icicle-literal-replacement',
225 ;;    `toggle-icicle-proxy-candidates', `toggle-icicle-regexp-quote',
226 ;;    `toggle-icicle-remote-file-testing',
227 ;;    `toggle-icicle-search-cleanup',
228 ;;    `toggle-icicle-search-complementing-domain',
229 ;;    `toggle-icicle-search-replace-common-match',
230 ;;    `toggle-icicle-search-replace-whole',
231 ;;    `toggle-icicle-search-whole-word',
232 ;;    `toggle-icicle-show-multi-completion', `toggle-icicle-sorting',
233 ;;    `toggle-icicle-transforming',
234 ;;    `toggle-icicle-WYSIWYG-Completions'.
235 ;;
236 ;;  Non-interactive functions defined here:
237 ;;
238 ;;    `icicle-add/remove-tags-and-refresh',
239 ;;    `icicle-all-candidates-action-1', `icicle-all-exif-data',
240 ;;    `icicle-anychar-regexp', `icicle-apply-to-saved-candidate',
241 ;;    `icicle-apropos-complete-1',
242 ;;    `icicle-backward-delete-char-untabify-dots',
243 ;;    `icicle-bind-file-candidate-keys', `icicle-candidate-action-1',
244 ;;    `icicle-candidate-set-retrieve-1',
245 ;;    `icicle-candidate-set-save-1',
246 ;;    `icicle-candidate-set-save-selected-1',
247 ;;    `icicle-column-wise-cand-nb', `icicle-Completions-popup-choice',
248 ;;    `icicle-Completions-popup-choice-1', `icicle-convert-dots',
249 ;;    `icicle-current-completion-in-Completions',
250 ;;    `icicle-current-sort-functions', `icicle-current-sort-order',
251 ;;    `icicle-delete-backward-char-dots',
252 ;;    `icicle-delete-candidate-object-1', `icicle-delete-char-dots',
253 ;;    `icicle-delete-current-candidate-object',
254 ;;    `icicle-ensure-overriding-map-is-bound',
255 ;;    `icicle-help-on-candidate-symbol',
256 ;;    `icicle-input-is-a-completion-p', `icicle-insert-dot',
257 ;;    `icicle-insert-input', `icicle-insert-thing',
258 ;;    `icicle-looking-at-anychar-regexp-p',
259 ;;    `icicle-looking-back-at-anychar-regexp-p',
260 ;;    `icicle-markers-to-readable',
261 ;;    `icicle-maybe-multi-completion-completing-p',
262 ;;    `icicle-mouse-candidate-action-1', `icicle-nb-Completions-cols',
263 ;;    `icicle-nb-of-cand-at-Completions-pos',
264 ;;    `icicle-nb-of-cand-in-Completions-horiz',
265 ;;    `icicle-prefix-complete-1', `icicle-raise-Completions-frame',
266 ;;    `icicle-remove-cand-from-lists',
267 ;;    `icicle-remove-candidate-display-others',
268 ;;    `icicle-replace-input-w-parent-dir',
269 ;;    `icicle-retrieve-candidates-from-set',
270 ;;    `icicle-row-wise-cand-nb', `icicle-signum',
271 ;;    `icicle-substitute-keymap-vars', `icicle-successive-action',
272 ;;    `icicle-transform-sole-candidate',
273 ;;    `icicle-transpose-chars-dots',
274 ;;    `icicle-unbind-file-candidate-keys',
275 ;;    `icicle-upcase-if-ignore-case', `icicle-update-and-next'.
276 ;;
277 ;;  Internal variables defined here:
278 ;;
279 ;;    `overriding-map-is-bound', `saved-overriding-map'.
280 ;;
281 ;;
282 ;;  ***** NOTE: These EMACS PRIMITIVES have been REDEFINED HERE:
283 ;;
284 ;;  `exit-minibuffer'              - Remove *Completion* window and
285 ;;                                   input mismatch highlighting
286 ;;  `minibuffer-complete-and-exit' - Use Icicles prefix completion
287 ;;
288 ;;
289 ;;  ***** NOTE: The following function defined in `mouse.el' has
290 ;;              been REDEFINED HERE:
291 ;;
292 ;;  `choose-completion'       - Don't iconify frame or bury buffer.
293 ;;  `mouse-choose-completion' - Return the number of the completion.
294 ;;
295 ;;
296 ;;  ***** NOTE: The following function defined in `simple.el' has
297 ;;              been REDEFINED HERE:
298 ;;
299 ;;  `switch-to-completions' - Always selects `*Completions*' window.
300 ;;
301 ;;
302 ;;  Key bindings made by Icicles: See "Key Bindings" in
303 ;;  `icicles-doc2.el'.
304 ;;
305 ;;  For descriptions of changes to this file, see `icicles-chg.el'.
306  
307 ;;(@> "Index")
308 ;;
309 ;;  If you have library `linkd.el' and Emacs 22 or later, load
310 ;;  `linkd.el' and turn on `linkd-mode' now.  It lets you easily
311 ;;  navigate around the sections of this doc.  Linkd mode will
312 ;;  highlight this Index, as well as the cross-references and section
313 ;;  headings throughout this file.  You can get `linkd.el' here:
314 ;;  http://dto.freeshell.org/notebook/Linkd.html.
315 ;;
316 ;;  (@> "Redefined standard commands")
317 ;;  (@> "Icicles commands")
318 ;;    (@> "Minibuffer editing commands")
319 ;;    (@> "Commands to sort completion candidates")
320 ;;    (@> "Other commands to be used mainly in the minibuffer")
321  
322 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
323 ;;
324 ;; This program is free software; you can redistribute it and/or
325 ;; modify it under the terms of the GNU General Public License as
326 ;; published by the Free Software Foundation; either version 2, or (at
327 ;; your option) any later version.
328 ;;
329 ;; This program is distributed in the hope that it will be useful,
330 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
331 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
332 ;; GNU General Public License for more details.
333 ;;
334 ;; You should have received a copy of the GNU General Public License
335 ;; along with this program; see the file COPYING.  If not, write to
336 ;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
337 ;; Floor, Boston, MA 02110-1301, USA.
338 ;;
339 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
340 ;;
341 ;;; Code:
342
343 (eval-when-compile (require 'cl)) ;; case, flet, lexical-let, loop
344                                   ;; plus, for Emacs < 21: dolist, push
345 (eval-when-compile (require 'filesets nil t)) ; Emacs 22+.
346   ;; filesets-data, filesets-entry-get-files, filesets-entry-mode, filesets-entry-set-files,
347   ;; filesets-files-equalp, filesets-init, filesets-member, filesets-set-config
348
349 (eval-when-compile
350  (or (condition-case nil
351          (load-library "icicles-mac")   ; Use load-library to ensure latest .elc.
352        (error nil))
353      (require 'icicles-mac)))           ; Require, so can load separately if not on `load-path'.
354   ;; icicle-assoc-delete-all, icicle-define-sort-command
355 (require 'icicles-opt)                  ; (This is required anyway by `icicles-var.el'.)
356   ;; icicle-alternative-sort-comparer, icicle-move-Completions-frame,
357   ;; icicle-Completions-mouse-3-menu-entries, icicle-default-cycling-mode,
358   ;; icicle-default-thing-insertion, icicle-expand-input-to-common-match-flag,
359   ;; icicle-hide-common-match-in-Completions-flag, icicle-hide-non-matching-lines-flag,
360   ;; icicle-ignore-space-prefix-flag, icicle-incremental-completion-flag, icicle-input-string,
361   ;; icicle-key-descriptions-use-<>-flag, icicle-regexp-quote-flag, icicle-saved-completion-sets,
362   ;; icicle-search-cleanup-flag, icicle-search-highlight-all-current-flag, icicle-sort-comparer,
363   ;; icicle-sort-orders-alist, icicle-TAB-shows-candidates-flag, icicle-thing-at-point-functions,
364   ;; icicle-transform-function
365 (eval-and-compile (require 'icicles-var)) ; (This is required anyway by `icicles-fn.el'.)
366   ;; lacarte-menu-items-alist, icicle-candidate-action-fn, icicle-candidate-nb,
367   ;; icicle-complete-keys-alist, icicle-completion-candidates, 
368   ;; icicle-current-completion-candidate-overlay, icicle-current-completion-mode,
369   ;; icicle-current-input, icicle-current-raw-input, icicle-default-directory,
370   ;; icicle-default-thing-insertion-flipped-p, icicle-edit-update-p, icicle-general-help-string,
371   ;; icicle-get-alist-candidate-function, icicle-ignored-extensions, icicle-ignored-extensions-regexp,
372   ;; icicle-incremental-completion-p, icicle-insert-string-at-pt-end, `icicle-insert-string-at-pt-start,
373   ;; icicle-last-completion-candidate, icicle-last-completion-command, icicle-last-input,
374   ;; icicle-last-sort-comparer, icicle-last-transform-function, 
375   ;; icicle-nb-of-other-cycle-candidates, icicle-pre-minibuffer-buffer,
376   ;; icicle-saved-candidates-variables-obarray, icicle-saved-completion-candidates,
377   ;; icicle-saved-ignored-extensions, icicle-successive-grab-count, icicle-thing-at-pt-fns-pointer,
378   ;; icicle-universal-argument-map, icicle-variable-name-history
379 (require 'icicles-fn)
380   ;; icicle-isearch-complete-past-string, icicle-minibuf-input-sans-dir,
381   ;; icicle-toggle-icicle-mode-twice
382
383 (require 'pp+ nil t) ;; (no error if not found): pp-eval-expression
384 (require 'doremi nil t) ;; (no error if not found):
385                         ;; doremi, doremi(-boost)-(up|down)-keys, doremi-limit, doremi-wrap
386 (when (> emacs-major-version 22) (require 'help-fns+ nil t)) ;; (no error if not found):
387                                                              ;; help-commands-to-key-buttons
388
389 (eval-when-compile (require 'fit-frame nil t)) ;; (no error if not found): fit-frame
390 (eval-when-compile
391  (when (> emacs-major-version 21) (require 'linkd nil t))) ;; (no error if not found): linkd-mode
392
393 ;; Byte-compiling this file, you will likely get some byte-compiler warning messages.
394 ;; These are probably benign - ignore them.  Icicles is designed to work with multiple
395 ;; versions of Emacs, and that fact provokes compiler warnings.  If you get byte-compiler
396 ;; errors (not warnings), then please report a bug, using `M-x icicle-send-bug-report'.
397
398 ;; Some defvars to quiet byte-compiler a bit:
399
400 (when (< emacs-major-version 22)
401   (defvar overriding-map-is-bound)
402   (defvar read-file-name-completion-ignore-case) ; In `minibuffer.el'
403   (defvar read-file-name-predicate)
404   (defvar saved-overriding-map))
405
406 (when (< emacs-major-version 23)
407   (defvar read-buffer-completion-ignore-case)
408   (defvar mouse-drag-copy-region))
409
410 (defvar doremi-boost-down-keys)         ; In `doremi.el'
411 (defvar doremi-boost-up-keys)           ; In `doremi.el'
412 (defvar doremi-down-keys)               ; In `doremi.el'
413 (defvar doremi-up-keys)                 ; In `doremi.el'
414 (defvar filesets-data)                  ; In `filesets.el'.
415 (defvar ignore-comments-flag)           ; In `thing-cmds.el'.
416 (defvar minibuffer-confirm-exit-commands) ; In `minibuffer.el' in Emacs 23+.
417 (defvar minibuffer-local-filename-completion-map) ; In Emacs 22+.
418 (defvar minibuffer-local-filename-must-match-map) ; In Emacs 23.2 (but not Emacs 24+).
419 (defvar minibuffer-local-must-match-filename-map) ; In Emacs 22+.
420
421 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
422  
423 ;;(@* "Redefined standard commands")
424
425 ;;; Redefined standard commands --------------------------------------
426
427
428 ;; REPLACE ORIGINAL `next-history-element' in `simple.el',
429 ;; saving it for restoration when you toggle `icicle-mode'.
430 ;;
431 ;; Selects minibuffer contents and leaves point at its beginning.
432 ;;
433 (unless (fboundp 'old-next-history-element)
434   (defalias 'old-next-history-element (symbol-function 'next-history-element)))
435
436 ;;;###autoload
437 (defun icicle-next-history-element (arg) ; Bound to `M-n' in minibuffer.
438   "Insert the next element of the minibuffer history in the minibuffer.
439 With argument N, it uses the Nth following element."
440   (interactive "p")
441   (old-next-history-element (prefix-numeric-value arg))
442   (when (and icicle-mode (memq icicle-default-value '(preselect-start preselect-end)))
443     (icicle-select-minibuffer-contents)
444     (setq deactivate-mark  nil)))
445
446
447 ;; REPLACE ORIGINAL `exit-minibuffer' (built-in function),
448 ;; saving it for restoration when you toggle `icicle-mode'.
449 ;;
450 ;; Remove input mismatch highlighting.
451 ;; Remove *Completion* window.
452 ;;
453 (unless (fboundp 'old-exit-minibuffer)
454   (defalias 'old-exit-minibuffer (symbol-function 'exit-minibuffer)))
455
456 ;;;###autoload
457 (defun icicle-exit-minibuffer ()        ; Bound to `C-m' (`RET') in minibuffer.
458   "Terminate this minibuffer argument.
459 Remove `*Completions*' window.  Remove Icicles minibuffer faces."
460   ;; This removal lets users retrieve candidates that have other faces, and saves input-history space.
461   (interactive)
462   (when (active-minibuffer-window)
463     (with-current-buffer (window-buffer (minibuffer-window))
464       (let ((pos                (icicle-minibuffer-prompt-end))
465             (icy-minibuf-faces  '(icicle-input-completion-fail  icicle-input-completion-fail-lax
466                                   icicle-whitespace-highlight   icicle-match-highlight-minibuffer
467                                   icicle-complete-input))
468             (keep-faces         ()))
469         (while (< pos (point-max))
470           (let ((faces  (get-text-property pos 'face)))
471             (when (or (and (consp faces) (cdr faces) (atom (cdr faces))) ; (background-color . "abc")
472                       (and faces (atom faces))) ; face name
473               (setq faces  (list faces))) ; No-op: (foo (background-color . "abc") (:foreground "abc"))
474             (setq keep-faces  (icicle-set-union keep-faces
475                                                 (icicle-set-difference faces icy-minibuf-faces))))
476           (setq pos  (1+ pos)))
477         (when keep-faces                ; Don't add a nil `face' property.
478           (put-text-property (icicle-minibuffer-prompt-end) (point-max) 'face keep-faces)))
479       ;; $$$$$  (let ((pos  (icicle-minibuffer-prompt-end)))
480       ;;     (while (< pos (point-max))
481       ;;       (when (memq (get-text-property pos 'face)
482       ;;                   '(icicle-input-completion-fail icicle-input-completion-fail-lax))
483       ;;         (remove-text-properties pos (point-max) '(face))
484       ;;         (setq pos  (point-max)))
485       ;;       (setq pos  (1+ pos))))
486       ))
487   (icicle-remove-Completions-window)
488   (old-exit-minibuffer))
489
490
491 ;; REPLACE ORIGINAL `minibuffer-complete-and-exit' (built-in function),
492 ;; saving it for restoration when you toggle `icicle-mode'.
493 ;;
494 ;; Use Icicles completion.
495 ;;
496 (unless (fboundp 'old-minibuffer-complete-and-exit)
497   (defalias 'old-minibuffer-complete-and-exit (symbol-function 'minibuffer-complete-and-exit)))
498
499 ;; Bound to `C-m' (`RET') in `minibuffer-local-must-match-map'.
500 ;;;###autoload
501 (defun icicle-minibuffer-complete-and-exit ()
502   "If the minibuffer contents is a valid completion, then exit.
503 Otherwise try to complete it."
504   (interactive)
505   (let ((last-cmd  last-command))
506     (cond ((string= "" (if (icicle-file-name-input-p) ;  Empty input - exit.
507                            (icicle-minibuf-input-sans-dir)
508                          (icicle-input-from-minibuffer)))
509            (icicle-exit-minibuffer))
510           ;; This case serves when property `icicle-display-string' is used.
511           ;; What's returned is the replacement display string, not the original candidate.
512           ;; If you want to get the original candidate back, you'll need to search the obarray for a
513           ;; symbol that has this `icicle-display-string' value.  Or put the symbol on the display
514           ;; string as a text property.
515           ((icicle-input-is-a-completion-p) (icicle-exit-minibuffer))
516           ((eq minibuffer-completion-confirm 'confirm) ; User wants it anyway? - Emacs 23+.
517            (if (eq last-cmd this-command)
518                (icicle-exit-minibuffer)
519              (minibuffer-message "Confirm")
520              nil))
521           ((eq minibuffer-completion-confirm 'confirm-after-completion) ; Emacs 23+.
522            ;; Similar to `confirm', but only if trying to exit immediately
523            ;; after completing (this catches most minibuffer typos).
524            (if (not (memq last-cmd (and (boundp 'minibuffer-confirm-exit-commands)
525                                         (append icicle-confirm-exit-commands
526                                                 minibuffer-confirm-exit-commands))))
527                (icicle-exit-minibuffer)
528              (minibuffer-message "Confirm")
529              nil))
530           (t
531            (setq icicle-current-input  (icicle-input-from-minibuffer))
532            (let* (;; Bind these first two to suppress (a) the throw or (b) the message, highlighting,
533                   ;; mode-line help, and the wait involved in completing again.
534                   (icicle-prefix-complete-and-exit-p   t)
535                   (icicle-apropos-complete-and-exit-p  t)
536
537                   (candidates
538                    ;; If we're not using `icicle-candidates-alist', complete the input again.
539                    ;; If we're using `icicle-candidates-alist', try filtering it against just the
540                    ;; input.
541                    ;;   If the input is already complete, then we're done.  If not, then filtering
542                    ;;   will give nil and we will just continue to display the candidates.  If there
543                    ;;   are multiple matches, then the user can either cycle or complete again.
544                    (if (not icicle-candidates-alist)
545                        (if (eq icicle-current-completion-mode 'apropos)
546                            (icicle-apropos-complete-no-display 'nomsg)
547                          (icicle-prefix-complete-no-display 'nomsg))
548                      (icicle-filter-alist icicle-candidates-alist (list icicle-current-input)))))
549              (cond ((and (eq icicle-require-match-p t) ; Don't exit if non-nil and non-t.
550                          (icicle-input-is-a-completion-p))
551                     (icicle-exit-minibuffer))
552                    (t
553                     (icicle-display-candidates-in-Completions))))))))
554
555 (defun icicle-upcase-if-ignore-case (string)
556   "Return (icicle-upcase STRING) if `completion-ignore-case', else STRING."
557   (if completion-ignore-case (icicle-upcase string) string))
558
559 ;;;###autoload
560 (defun icicle-apropos-complete-and-exit () ; Bound to `S-RET' in `minibuffer-local-must-match-map'.
561   "If the minibuffer contents is a valid apropos completion, then exit.
562 Otherwise try to complete it.  If completion leads to a valid
563 completion, then exit.
564 This is to `minibuffer-complete-and-exit' as `icicle-apropos-complete'
565 is to `minibuffer-complete'.  That is, it is the regexp-match version."
566   (interactive)
567   (setq icicle-last-input  (icicle-input-from-minibuffer))
568   (let* ((icicle-apropos-complete-and-exit-p  t) ; Suppress the throw or the msg plus the wait.
569          (candidates                          (icicle-apropos-complete)))
570     (when (and candidates (null (cdr candidates))) (old-exit-minibuffer)))) ; Single candidate.
571
572
573 ;; REPLACE ORIGINAL `choose-completion' in `simple.el',
574 ;; saving it for restoration when you toggle `icicle-mode'.
575 ;;
576 ;; Don't iconify frame or bury buffer.
577 ;; Don't strip text properties.
578 ;;
579 (unless (fboundp 'old-choose-completion)
580   (defalias 'old-choose-completion (symbol-function 'choose-completion)))
581
582 ;;;###autoload
583 (defun icicle-choose-completion ()
584   "Choose the completion that point is in or next to."
585   (interactive)
586   (let ((buffer     completion-reference-buffer)
587         (base-size  completion-base-size)
588         beg end completion)
589     (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
590       (setq end  (point)
591             beg  (1+ (point))))
592     (when (and (>= (point) (icicle-start-of-candidates-in-Completions))
593                (get-text-property (1- (point)) 'mouse-face))
594       (setq end  (1- (point))
595             beg  (point)))
596     (unless beg (error "No completion here"))
597     (setq beg         (previous-single-property-change beg 'mouse-face)
598           end         (or (next-single-property-change end 'mouse-face) (point-max))
599           ;; $$$$ completion  (buffer-substring-no-properties beg end))
600           completion  (buffer-substring beg end))
601     ;; (let ((owindow  (selected-window)))
602     ;;   (if (and (one-window-p t 'selected-frame) (window-dedicated-p (selected-window)))
603     ;;    (iconify-frame (selected-frame)) ; Iconify special buffer's frame
604     ;;  (or (window-dedicated-p (selected-window)) (bury-buffer)))
605     ;;   (select-window owindow))
606     (unless (or (not (member completion icicle-extra-candidates))
607                 icicle-extra-candidates-dir-insert-p)
608       (setq base-size  0))
609     (choose-completion-string completion buffer base-size)))
610
611
612 ;; REPLACE ORIGINAL `mouse-choose-completion' in `mouse.el',
613 ;; saving it for restoration when you toggle `icicle-mode'.
614 ;;
615 ;; Return the number of the completion.
616 ;; Don't strip text properties.
617 ;;
618 (when (and (fboundp 'mouse-choose-completion) (not (fboundp 'old-mouse-choose-completion)))
619   (defalias 'old-mouse-choose-completion (symbol-function 'mouse-choose-completion)))
620
621 ;;;###autoload
622 (defun icicle-mouse-choose-completion (event) ; Bound to `mouse-2' in `*Completions*'.
623   "Click a completion candidate in buffer `*Completions*', to choose it.
624 Return the number of the candidate: 0 for first, 1 for second, ..."
625   (interactive "e")
626   ;; $$$$$ (unless (active-minibuffer-window) (error "Minibuffer is not active"))
627   ;; Give temporary modes such as isearch a chance to turn off.
628   (run-hooks 'mouse-leave-buffer-hook)
629   (let ((buffer  (window-buffer))
630          ;; $$$$$$ (icicle-orig-buff  buffer)
631         choice base-size)
632     (with-current-buffer (window-buffer (posn-window (event-start event)))
633       (save-excursion
634         (when completion-reference-buffer (setq buffer  completion-reference-buffer))
635         (setq base-size  completion-base-size)
636         (save-excursion
637           (goto-char (posn-point (event-start event)))
638           (let (beg end)
639             (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
640               (setq end  (point)
641                     beg  (1+ (point))))
642             (unless beg (error "No completion here"))
643             (setq beg  (previous-single-property-change beg 'mouse-face)
644                   end  (or (next-single-property-change end 'mouse-face) (point-max)))
645             ;; `icicle-insert-candidates' doesn't put `mouse-face' on the final \n of a candidate
646             ;; in `*Completions*'. Add the newline back. `icicle-insert-candidates' puts property
647             ;; `icicle-keep-newline' on the newline if it is part of the candidate, as opposed to
648             ;; being just part of the display in columns.
649             (when (and (eq ?\n (char-after end)) (get-text-property end 'icicle-keep-newline))
650               (setq end  (1+ end)))
651             ;; $$$$$$ (setq choice  (buffer-substring-no-properties beg end)))))
652             (setq choice  (buffer-substring beg end))))))
653     ;; $$$$$ (if (eq icicle-orig-buff (get-buffer "*Completions*"))
654     ;;    (icicle-remove-Completions-window)
655     ;;    (save-selected-window (icicle-remove-Completions-window)))
656     (setq icicle-candidate-nb  (icicle-nb-of-cand-at-Completions-pos (posn-point (event-start event))))
657     (when (and (icicle-file-name-input-p) insert-default-directory
658                (or (not (member choice icicle-extra-candidates))
659                    icicle-extra-candidates-dir-insert-p))
660       (let ((dir  (icicle-file-name-directory-w-default icicle-current-input)))
661         (with-current-buffer buffer
662           (icicle-clear-minibuffer)
663           (insert dir)
664           (setq choice     (concat dir choice)
665                 base-size  0))))
666     (choose-completion-string choice buffer base-size))
667   icicle-candidate-nb)
668
669 (defun icicle-nb-of-cand-at-Completions-pos (position)
670   "Return number of candidate at POSITION in `*Completions*'.
671 POSITION is a buffer position."
672   (let ((hor-nb  (icicle-nb-of-cand-in-Completions-horiz position)))
673     (save-excursion
674       (with-current-buffer (get-buffer "*Completions*")
675         (goto-char position)
676         (if (memq icicle-completions-format '(horizontal nil))
677             hor-nb
678           (let* ((cols      (icicle-nb-Completions-cols))
679                  (nb-cands  (length icicle-completion-candidates))
680                  (rows      (/ nb-cands cols)))
681             (unless (zerop (% nb-cands cols)) (setq rows  (1+ rows)))
682             (icicle-column-wise-cand-nb hor-nb nb-cands rows cols)))))))
683
684 (defun icicle-nb-of-cand-in-Completions-horiz (position)
685   "Return number of horizontal candidate at POSITION in `*Completions*'.
686 POSITION is a buffer position."
687   (let ((compl-buf  (get-buffer "*Completions*")))
688     (unless compl-buf (error "No `*Completions*' buffer"))
689     (save-window-excursion
690       (set-buffer compl-buf)
691       (goto-char position)
692       ;; If in a completion, move to its start, and set POSITION there.
693       (let ((prop  (get-text-property  (1- (point)) 'mouse-face)))
694         (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
695           (goto-char (previous-single-property-change (point) 'mouse-face nil
696                                                       (icicle-start-of-candidates-in-Completions)))))
697       (setq position  (point))
698       ;; Binary search.
699       (let ((cand-nb                             (/ (length icicle-completion-candidates) 2))
700             (last-nb                             0)
701             (icicle-completions-format           'horizontal)
702             delta)
703         (goto-char (point-min))
704         (icicle-move-to-next-completion cand-nb t)
705         (while (/= (point) position)
706           (setq delta    (max 1 (/ (abs (- cand-nb last-nb)) 2))
707                 last-nb  cand-nb)
708           (cond ((< (point) position)
709                  (icicle-move-to-next-completion delta t)
710                  (setq cand-nb  (+ cand-nb delta)))
711                 (t
712                  (icicle-move-to-next-completion (- delta) t)
713                  (setq cand-nb  (- cand-nb delta)))))
714         (set-buffer-modified-p nil)
715         (1- cand-nb)))))
716
717 (defun icicle-nb-Completions-cols ()
718   "Return the number of candidate columns in `*Completions*'."
719   (let* ((start       (icicle-start-of-candidates-in-Completions))
720          (eol         (save-excursion (goto-char start) (line-end-position)))
721          (mouse-chgs  0)
722          mousef)
723     (save-excursion
724       (goto-char start)
725       (while (< (point) eol)
726         (setq mousef  (next-single-property-change (point) 'mouse-face nil eol))
727         (when mousef
728           (goto-char mousef)
729           (setq mouse-chgs  (1+ mouse-chgs)))))
730     (/ (1+ mouse-chgs) 2)))             ; Return # of columns.
731
732 (defun icicle-column-wise-cand-nb (horiz-nb nb-cands rows cols)
733   "Column-wise number of horizontal candidate number HORIZ-NB."
734   (let ((row-lim  (- rows (- (* rows cols) nb-cands)))
735         (row      (/ horiz-nb cols))
736         (col      (mod horiz-nb cols))
737         nb)
738     (setq nb  (+ row (* col rows)))
739     (when (>= row row-lim)
740       (setq cols      (1- cols)
741             horiz-nb  (- horiz-nb row-lim)
742             row       (/ horiz-nb cols)
743             col       (mod horiz-nb cols)
744             nb        (+ row (* col rows))))
745     nb))
746
747 (defun icicle-row-wise-cand-nb (vert-nb nb-cands rows cols)
748   "Row-wise number of vertical candidate number VERT-NB."
749   (let* ((row  (mod vert-nb rows))
750          (col  (/ vert-nb rows))
751          (nb   (+ col (* row cols)))
752          (lim  (- rows (- (* rows cols) nb-cands))))
753     (when (> row lim) (setq nb  (- nb (- row lim))))
754     nb))
755
756
757 ;; REPLACE ORIGINAL `switch-to-completions' defined in `simple.el',
758 ;; saving it for restoration when you toggle `icicle-mode'.
759 ;;
760 ;; Selects `*Completions*' window even if on another frame.
761 ;;
762 (unless (fboundp 'old-switch-to-completions)
763   (defalias 'old-switch-to-completions (symbol-function 'switch-to-completions)))
764
765 ;;;###autoload
766 (defun icicle-switch-to-completions ()
767   "Select the completion list window, `*Completions*'."
768   (interactive)
769   ;; Make sure we have a completions window.
770   (or (get-buffer-window "*Completions*") (minibuffer-completion-help))
771   (let ((window  (get-buffer-window "*Completions*" 0))) ; Added 0 arg.
772     (when window
773       (select-window window)
774       (goto-char (icicle-start-of-candidates-in-Completions)))))
775
776 ;; The branch that deletes a history element is based on Juri Linkov's
777 ;; `delete-history-element', proposed for Emacs 22 but rejected by RMS.
778 ;;;###autoload
779 (defun icicle-erase-minibuffer-or-history-element () ; Bound to `M-k' in minibuffer.
780   "`icicle-erase-minibuffer' or, if using history, delete history element."
781   (interactive)
782   (if (not (memq last-command '(previous-history-element next-history-element
783                                 icicle-erase-minibuffer-or-history-element
784                                 previous-matching-history-element next-matching-history-element)))
785       (icicle-erase-minibuffer)
786     (let* ((curr-pos  (1- minibuffer-history-position))
787            (current   (nth curr-pos (and (boundp minibuffer-history-variable)
788                                          (symbol-value minibuffer-history-variable)))))
789       (cond ((= minibuffer-history-position 1)
790              (set minibuffer-history-variable (and (boundp minibuffer-history-variable)
791                                                    (cdr (symbol-value minibuffer-history-variable)))))
792             ((> minibuffer-history-position 1)
793              (setcdr (nthcdr (- minibuffer-history-position 2)
794                              (and (boundp minibuffer-history-variable)
795                                   (symbol-value minibuffer-history-variable)))
796                      (nthcdr minibuffer-history-position
797                              (and (boundp minibuffer-history-variable)
798                                   (symbol-value minibuffer-history-variable))))))
799       (condition-case nil
800           (cond ((memq last-command '(next-history-element next-matching-history-element))
801                  (next-history-element 1)
802                  (setq this-command  'next-history-element))
803                 ((memq last-command '(previous-history-element previous-matching-history-element))
804                  (next-history-element 1)
805                  (previous-history-element 1)
806                  (setq this-command  'previous-history-element)))
807         (error (icicle-condition-case-no-debug nil
808                    (cond ((memq last-command '(next-history-element next-matching-history-element))
809                           (previous-history-element 1)
810                           (setq this-command  'previous-history-element))
811                          ((memq last-command
812                                 '(previous-history-element previous-matching-history-element))
813                           (next-history-element 1)
814                           (setq this-command  'next-history-element)))
815                  (error nil))))
816       (when (and current (wholenump curr-pos))
817         (icicle-msg-maybe-in-minibuffer "Deleted `%s'" current)))))
818  
819 ;;(@* "Icicles commands")
820
821 ;;; Icicles commands -------------------------------------------------
822
823 ;;(@* "Minibuffer editing commands")
824
825 ;;; Minibuffer editing commands  . . . . . . . . . . . . . . . . . . .
826 ;;;
827 ;;; All except `icicle-erase-minibuffer' are bound in the minibuffer to whatever the same
828 ;;; command without `icicle-' is bound to globally.
829
830 (defun icicle-looking-back-at-anychar-regexp-p ()
831   "Return non-nil if `icicle-anychar-regexp' immediately precedes point."
832   (let ((len  (length icicle-anychar-regexp)))
833     (save-excursion (save-match-data
834                       (search-backward icicle-anychar-regexp
835                                        (max (- (point) len) (icicle-minibuffer-prompt-end)) t)))))
836
837 (defun icicle-looking-at-anychar-regexp-p ()
838   "Return non-nil if `icicle-anychar-regexp' immediately succeeds point."
839   (let ((len  (length icicle-anychar-regexp)))
840     (save-excursion (save-match-data
841                       (search-forward icicle-anychar-regexp (min (+ (point) len) (point-max)) t)))))
842
843 ;;;###autoload
844 (defun icicle-forward-char-dots (&optional n)
845   "Move forward N chars (backward if N is negative).  Handles dots (`.')."
846   (interactive "p")
847   (let ((len  (length icicle-anychar-regexp)))
848     (dotimes (i  (abs n))
849       (or (save-match-data
850             (if (wholenump n)
851                 (search-forward icicle-anychar-regexp (min (+ (point) len) (point-max)) t)
852               (search-backward icicle-anychar-regexp
853                                (max (- (point) len) (icicle-minibuffer-prompt-end)) t)))
854           (forward-char (if (wholenump n) 1 -1))))))
855
856 ;;;###autoload
857 (defun icicle-backward-char-dots (&optional n)
858   "Move backward N chars (forward if N is negative).  Handles dots (`.')."
859   (interactive "p")
860   (icicle-forward-char-dots (- n)))
861
862
863 ;; Make delete-selection mode recognize it, so region is deleted.
864 (put 'icicle-backward-delete-char-untabify 'delete-selection 'supersede)
865 ;;;###autoload
866 (defun icicle-backward-delete-char-untabify (n &optional killflag)
867   "`backward-delete-char-untabify' + update `*Completions*' with matches.
868 Handles Icicles dots (`.')."
869   (interactive "*p\nP")
870   (icicle-call-then-update-Completions #'icicle-backward-delete-char-untabify-dots n killflag))
871
872 (defun icicle-backward-delete-char-untabify-dots (n killflag)
873   "`backward-delete-char-untabify', but also handle dots (`.')."
874   (let ((len  (length icicle-anychar-regexp)))
875     (dotimes (i  (abs n))
876       (if (icicle-looking-back-at-anychar-regexp-p)
877           (backward-delete-char-untabify len killflag)
878         (backward-delete-char-untabify 1 killflag)))))
879
880
881 ;; Make delete-selection mode recognize it, so region is deleted.
882 (put 'icicle-delete-backward-char 'delete-selection 'supersede)
883 ;;;###autoload
884 (defun icicle-delete-backward-char (n &optional killflag) ; Bound to `DEL' in minibuffer.
885   "`delete-backward-char' and update `*Completions*' with input matches.
886 Handles Icicles dots (`.')."
887   (interactive "*p\nP")
888   (icicle-call-then-update-Completions #'icicle-delete-backward-char-dots n killflag))
889
890 (defun icicle-delete-backward-char-dots (n killflag)
891   "`delete-backward-char', but also handle dots (`.')."
892   (let ((len  (length icicle-anychar-regexp)))
893     (dotimes (i  (abs n))
894       (if (icicle-looking-back-at-anychar-regexp-p)
895           (delete-char (- len) killflag)
896         (delete-char -1 killflag)))))
897
898
899 ;; Make delete-selection mode recognize it, so region is deleted.
900 (put 'icicle-delete-char 'delete-selection 'supersede)
901 ;;;###autoload
902 (defun icicle-delete-char (n &optional killflag) ; Bound to `C-d' in minibuffer.
903   "`delete-char' and update `*Completions*' with input matches.
904 Handles Icicles dots (`.')."
905   (interactive "*p\nP")
906   (icicle-call-then-update-Completions #'icicle-delete-char-dots n killflag))
907
908 (defun icicle-delete-char-dots (n killflag)
909   "`delete-char', but also handle dots (`.')."
910   (let ((len  (length icicle-anychar-regexp)))
911     (dotimes (i  (abs n))
912       (if (icicle-looking-at-anychar-regexp-p)
913           (delete-char len killflag)
914         (delete-char 1 killflag)))))
915
916 ;;;###autoload
917 (defun icicle-backward-kill-word (arg)  ; Bound to `M-DEL' (`M-backspace') in minibuffer.
918   "`backward-kill-word' and update `*Completions*' with input matches.
919 See description of `backward-kill-word'."
920   (interactive "p")
921   (icicle-call-then-update-Completions #'backward-kill-word arg))
922
923 ;;;###autoload
924 (defun icicle-kill-word (arg)           ; Bound to `M-d' in minibuffer.
925   "`kill-word' and update `*Completions*' with regexp input matches.
926 See description of `kill-word'."
927   (interactive "p")
928   (icicle-call-then-update-Completions #'kill-word arg))
929
930 ;;;###autoload
931 (defun icicle-backward-kill-sexp (arg)  ; Bound to `C-M-backspace' in minibuffer.
932   "`backward-kill-sexp' and update `*Completions*' with input matches.
933 See description of `backward-kill-sexp'."
934   (interactive "p")
935   (icicle-call-then-update-Completions #'backward-kill-sexp arg))
936
937 ;;;###autoload
938 (defun icicle-kill-sexp (arg)           ; Bound to `C-M-delete' and `C-M-k' in minibuffer.
939   "`kill-sexp' and update `*Completions*' with regexp input matches.
940 See description of `kill-sexp'."
941   (interactive "p")
942   (icicle-call-then-update-Completions #'kill-sexp arg))
943
944 ;;;###autoload
945 (defun icicle-backward-kill-sentence (arg) ; Bound to `C-x DEL' in minibuffer.
946   "`backward-kill-sentence' and update `*Completions*' with input matches.
947 See description of `backward-kill-sentence'."
948   (interactive "p")
949   (icicle-call-then-update-Completions #'backward-kill-sentence arg))
950
951 ;;;###autoload
952 (defun icicle-kill-sentence (arg)
953   "`kill-sentence' and update `*Completions*' with regexp input matches.
954 See description of `kill-sentence'."
955   (interactive "p")
956   (icicle-call-then-update-Completions #'kill-sentence arg))
957
958 ;;;###autoload
959 (defun icicle-backward-kill-paragraph (arg) ; Bound to `C-backspace' in minibuffer, except for files.
960   "`backward-kill-paragraph' and update `*Completions*' with input matches.
961 See description of `backward-kill-paragraph'."
962   (interactive "p")
963   (icicle-call-then-update-Completions #'backward-kill-paragraph arg))
964
965 ;;;###autoload
966 (defun icicle-kill-paragraph (arg)      ; Bound to `C-delete' in minibuffer.
967   "`kill-paragraph' and update `*Completions*' with regexp input matches.
968 See description of `kill-paragraph'."
969   (interactive "p")
970   (icicle-call-then-update-Completions #'kill-paragraph arg))
971
972 ;;;###autoload
973 (defun icicle-kill-line (arg)           ; Bound to `C-k' and `deleteline' in minibuffer.
974   "`kill-line' and update `*Completions*' with regexp input matches.
975 See description of `kill-line'."
976   (interactive "P")
977   (icicle-call-then-update-Completions #'kill-line arg))
978
979 ;;;###autoload
980 (defun icicle-kill-region (beg end)     ; Bound to `C-w' in minibuffer.
981 ;; Don't bother with Emacs 22 optional 3rd arg.
982   "`kill-region' and update `*Completions*' with regexp input matches.
983 See description of `kill-region'."
984   (interactive "r")
985   (icicle-call-then-update-Completions #'kill-region beg end))
986
987 (when (fboundp 'kill-region-wimpy)
988   (defun icicle-kill-region-wimpy (beg end) ; Bound to `C-w' in minibuffer.
989     "`kill-region-wimpy' and update `*Completions*' with input matches.
990 See description of `kill-region-wimpy'."
991     (interactive "r")
992     (icicle-call-then-update-Completions #'kill-region-wimpy beg end)))
993
994 ;;;###autoload
995 (defun icicle-make-directory (dir)
996   "Create a directory."
997   (interactive
998    (let ((enable-recursive-minibuffers  t))
999      (list (funcall (if (fboundp 'read-directory-name) #'read-directory-name #'read-file-name)
1000                     "Create directory: " default-directory default-directory))))
1001   (setq dir  (directory-file-name (expand-file-name dir)))
1002   (while (file-exists-p dir)
1003     (message "%s already exists" dir) (sit-for 1)
1004     (let ((enable-recursive-minibuffers  t))
1005       (setq dir  (funcall (if (fboundp 'read-directory-name) #'read-directory-name #'read-file-name)
1006                           "Create directory: " default-directory default-directory))))
1007   ;;(setq dir  (directory-file-name (expand-file-name dir)))
1008   (if (not (y-or-n-p (format "Really create %s? " (file-name-as-directory dir))))
1009       (message "Directory creation canceled")
1010     (make-directory dir 'PARENTS-TOO)
1011     (unless (file-accessible-directory-p dir)
1012       (error "Could not create %s" (file-name-as-directory dir)))
1013     (message "Created %s" (file-name-as-directory dir))))
1014
1015 ;;;###autoload
1016 (defun icicle-up-directory () ; Bound to `C-backspace' in minibuffer, for file-name completion.
1017   "Replace minibuffer input with parent directory, then upate `*Completions*'."
1018   (interactive)
1019   (icicle-call-then-update-Completions #'icicle-replace-input-w-parent-dir))
1020
1021 ;;;###autoload
1022 (defun icicle-replace-input-w-parent-dir ()
1023   "Replace minibuffer input with the parent directory."
1024   (interactive)
1025   (goto-char (point-max))
1026   (let ((directoryp  (equal ?/ (char-before)))
1027         (bob         (icicle-minibuffer-prompt-end)))
1028     (while (and (> (point) bob) (not (equal ?/ (char-before))))  (delete-char -1))
1029     (when directoryp
1030       (delete-char -1)
1031       (while (and (> (point) bob) (not (equal ?/ (char-before))))  (delete-char -1)))))
1032
1033 ;;; ;;;###autoload
1034 ;;; (defun icicle-kill-failed-input ()      ; Bound to `C-M-l' in minibuffer during completion.
1035 ;;;   "Kill (delete) the part of the input that does not complete.
1036 ;;; Repeat to delete more."
1037 ;;;   (interactive)
1038 ;;;   (goto-char (1- (point-max)))
1039 ;;;   (while (and (not (bobp))
1040 ;;;               (memq (get-text-property (point) 'face)
1041 ;;;                     '(icicle-input-completion-fail icicle-input-completion-fail-lax)))
1042 ;;;     (delete-char 1)
1043 ;;;     (backward-char 1))
1044 ;;;   (unless (eobp) (forward-char))
1045 ;;;   (icicle-highlight-input-noncompletion))
1046
1047 ;;;###autoload
1048 (defun icicle-goto/kill-failed-input () ; Bound to `C-M-l' in minibuffer during completion.
1049   "Go to start of input portion that does not complete.  Repeat to kill.
1050 Kill (delete) the part of the input that does not complete.
1051 Repeat to delete more."
1052   (interactive)
1053   (if (eq last-command this-command)
1054       (unless (eobp) (kill-line))
1055     (when (and (overlayp icicle-input-completion-fail-overlay)
1056                (overlay-start icicle-input-completion-fail-overlay))
1057       (goto-char (overlay-start icicle-input-completion-fail-overlay)))))
1058
1059 ;;;###autoload
1060 (defun icicle-transpose-chars (arg)     ; Bound to `C-t' in minibuffer.
1061   "`transpose-chars' and update `*Completions*' with regexp input matches.
1062 Handles Icicles dots (`.')."
1063   (interactive "*P")
1064   (icicle-call-then-update-Completions #'icicle-transpose-chars-dots arg))
1065
1066 (defun icicle-transpose-chars-dots (arg)
1067   "`transpose-chars', but also handle dots (`.')."
1068   (and (null arg) (eolp) (icicle-forward-char-dots -1))
1069   (transpose-subr 'icicle-forward-char-dots (prefix-numeric-value arg)))
1070
1071 ;;;###autoload
1072 (defun icicle-transpose-words (arg)     ; Bound to `M-t' in minibuffer.
1073   "`transpose-words' and update `*Completions*' with regexp input matches.
1074 See description of `transpose-words'."
1075   (interactive "*p")
1076   (icicle-call-then-update-Completions #'transpose-words arg))
1077
1078 ;;;###autoload
1079 (defun icicle-transpose-sexps (arg)    ; Bound to `C-M-t' in minibuffer.
1080   "`transpose-sexps' and update `*Completions*' with regexp input matches.
1081 See description of `transpose-sexps'."
1082   (interactive "*p")
1083   (icicle-call-then-update-Completions #'transpose-sexps arg))
1084
1085 ;;;###autoload
1086 (defun icicle-yank (arg)                ; Bound to `C-y' and `S-insert' in minibuffer.
1087   "`yank' and update `*Completions*' with regexp input matches.
1088 See description of `yank'."
1089   (interactive "*P")
1090   (icicle-call-then-update-Completions #'yank arg))
1091
1092 ;;;###autoload
1093 (defun icicle-yank-pop (arg)            ; Bound to `M-y' and `M-insert' in minibuffer.
1094   "`yank-pop' and update `*Completions*' with regexp input matches.
1095 See description of `yank-pop'."
1096   (interactive "*p")
1097   (icicle-call-then-update-Completions #'yank-pop arg))
1098
1099 (when (fboundp 'yank-secondary)         ; In `second-sel.el'.
1100   (defun icicle-yank-secondary ()       ; Bound to `C-M-y' in minibuffer.
1101     "Insert the secondary selection at point.
1102 Move point to the end of the inserted text.  Does not change mark."
1103     (interactive "*")
1104     (icicle-call-then-update-Completions #'yank-secondary))
1105   ;; Tell `delete-selection-mode' to replace active region by yanked secondary selection.
1106   (put 'icicle-yank-secondary 'delete-selection 'yank))
1107
1108
1109 ;; Tell `delete-selection-mode' to replace active region by yanked secondary selection.
1110 (put 'icicle-mouse-yank-secondary 'delete-selection 'yank)
1111 ;;;###autoload
1112 (defun icicle-mouse-yank-secondary (event) ; Bound to `M-mouse-2' in minibuffer.
1113   "Insert the secondary selection where you click.
1114 Move point to the end of the inserted text.
1115 If `mouse-yank-at-point' is non-nil, insert at point
1116 regardless of where you click."
1117   (interactive "*e")
1118   (if (fboundp 'yank-secondary)         ; In `mouse+.el'.
1119       (icicle-call-then-update-Completions #'mouse-yank-secondary event current-prefix-arg)
1120     (icicle-call-then-update-Completions #'mouse-yank-secondary event)))
1121
1122
1123 ;; Make delete-selection mode recognize self-insertion, so it replaces region text.
1124 (put 'icicle-self-insert 'delete-selection t)
1125 ;;;###autoload
1126 (defun icicle-self-insert (n) ;; Bound in minibuffer to stuff bound globally to `self-insert-command'.
1127   "`self-insert' and update `*Completions*' with regexp input matches.
1128 See description of `self-insert'."
1129   (interactive "p")
1130   (if executing-kbd-macro
1131       (funcall #'self-insert-command n)
1132     (icicle-call-then-update-Completions #'self-insert-command n)))
1133
1134 ;;;###autoload
1135 (defun icicle-insert-a-space ()
1136   "Insert a space.
1137 For convenience in the minibuffer - does the same thing as `C-q SPC'.
1138 To use this, bind it to some key sequence in keymaps
1139 `minibuffer-local-completion-map',
1140 `minibuffer-local-filename-completion-map', and
1141 `minibuffer-local-must-match-map'."
1142   (interactive) (insert ?\ ))
1143
1144 ;;;###autoload
1145 (defun icicle-insert-dot-command (&optional arg) ; Bound to `.' in minibuffer during completion.
1146   "Insert `icicle-dot-string': either `.' or `icicle-anychar-regexp'.
1147 With a numeric prefix argument, insert the dot that many times.
1148
1149 With a plain prefix arg (`C-u'), insert the opposite kind of dot
1150 \(once) from what is indicated by the current value of
1151 `icicle-dot-string'."
1152   (interactive "P")
1153   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
1154   (if (consp arg)
1155       (let ((opposite  (if (string= icicle-dot-string-internal ".")
1156                            (icicle-anychar-regexp)
1157                          (let ((strg  "."))
1158                            (add-text-properties
1159                             0 1 '(icicle-user-plain-dot t rear-nonsticky (icicle-user-plain-dot))
1160                             strg)
1161                            strg))))
1162         (if executing-kbd-macro
1163             (insert opposite)
1164           (icicle-call-then-update-Completions (lambda () (insert opposite)))))
1165     (setq arg  (prefix-numeric-value arg))
1166     (if executing-kbd-macro
1167         (funcall #'icicle-insert-dot arg)
1168       (icicle-call-then-update-Completions #'icicle-insert-dot arg))))
1169
1170 (defun icicle-insert-dot (n)
1171   "Insert `icicle-dot-string' N times."
1172   (dotimes (i n)
1173     (if (not (string= icicle-dot-string-internal "."))
1174         (insert (icicle-anychar-regexp))
1175       (insert ".")
1176       (add-text-properties (1- (point)) (point) '(icicle-user-plain-dot t rear-nonsticky t)))))
1177
1178 (defun icicle-anychar-regexp ()
1179   "Return a regexp that matches any single character, including newline.
1180 The value returned is like that of constant `icicle-anychar-regexp',
1181 but the `display' string is unique for each call."
1182   (let ((strg  (copy-sequence "\\(.\\|[\n]\\)")))
1183     (set-text-properties 0 (length strg)
1184                          (if icicle-dot-show-regexp-flag
1185                              '(face highlight rear-nonsticky t)
1186                            `(display ,(copy-sequence ".") face highlight rear-nonsticky t))
1187                          strg)
1188     strg))
1189
1190 ;;;###autoload
1191 (defun icicle-erase-minibuffer ()       ; Bound to `M-S-backspace', `M-S-delete' in minibuffer.
1192   "Delete all user input in the minibuffer, then update completions."
1193   (interactive)
1194   (icicle-call-then-update-Completions #'icicle-clear-minibuffer))
1195  
1196 ;;(@* "Commands to sort completion candidates")
1197
1198 ;;; Commands to sort completion candidates . . . . . . . . . . . . . .
1199
1200 ;; We don't bother to define a command for the sort functions `icicle-prefix-keys-first-p' and
1201 ;; `icicle-command-names-alphabetic-p'.  They are bound in `icicle-complete-keys'.
1202
1203 ;; The order here defines the reverse order of `icicle-sort-orders-alist'.
1204 ;; The first here is also the default sort order.  Entries are traversed by `C-,' in
1205 ;; `icicle-sort-orders-alist' order.
1206
1207 ;;;###autoload (autoload 'icicle-sort-alphabetical "icicles-mcmd.el")
1208 (icicle-define-sort-command "alphabetical" ; `icicle-sort-alphabetical'
1209     icicle-case-string-less-p
1210   "Sort completion candidates alphabetically.
1211 Ignore letter case if `completion-ignore-case' or `case-fold-search'
1212 is non-nil.")
1213
1214 ;;;###autoload (autoload 'icicle-sort-special-candidates-first "icicles-mcmd.el")
1215 (icicle-define-sort-command "special candidates first" ; `icicle-sort-special-candidates-first'
1216     icicle-special-candidates-first-p
1217   "Sort completion candidates by putting special candidates first.
1218 Otherwise, sorting is alphabetical.  Ignore letter case if
1219 `completion-ignore-case' or `case-fold-search' is non-nil.")
1220
1221 ;;;###autoload (autoload 'icicle-sort-extra-candidates-first "icicles-mcmd.el")
1222 (icicle-define-sort-command "extra candidates first" ; `icicle-sort-extra-candidates-first'
1223     icicle-extra-candidates-first-p
1224   "Sort completion candidates by putting extra candidates first.
1225 Otherwise, sorting is alphabetical.  Ignore letter case if
1226 `completion-ignore-case' or `case-fold-search' is non-nil.
1227 An extra candidate is one that is a member of
1228 `icicle-extra-candidates'.")
1229
1230 ;;;###autoload (autoload 'icicle-sort-proxy-candidates-first "icicles-mcmd.el")
1231 (icicle-define-sort-command "proxy candidates first" ; `icicle-sort-proxy-candidates-first'
1232     icicle-proxy-candidate-first-p
1233   "Sort completion candidates by putting proxy candidates first.
1234 Otherwise, sorting is alphabetical.  Ignore letter case if
1235 `completion-ignore-case' or `case-fold-search' is non-nil.")
1236
1237 ;;;###autoload (autoload 'icicle-sort-case-insensitive "icicles-mcmd.el")
1238 (icicle-define-sort-command "case insensitive" ; `icicle-sort-case-insensitive'
1239     icicle-case-insensitive-string-less-p
1240   "Sort completion candidates alphabetically, but case-insenstively.")
1241
1242 ;;;###autoload (autoload 'icicle-sort-by-2nd-parts-alphabetically "icicles-mcmd.el")
1243 (icicle-define-sort-command "by 2nd parts alphabetically" ; `icicle-sort-by-2nd-parts-alphabetically'
1244     icicle-2nd-part-string-less-p
1245   "Sort multi-completion candidates alphabetically by their second parts.
1246 After that, sort alphabetically by the first parts.  Ignore letter
1247 case if `completion-ignore-case' or `case-fold-search' is non-nil.")
1248
1249 ;;;###autoload (autoload 'icicle-sort-by-last-file-modification-time "icicles-mcmd.el")
1250 (icicle-define-sort-command "by last file modification time"
1251     icicle-last-modified-first-p        ; `icicle-sort-by-last-file-modification-time'
1252   "Sort file-name completion candidates in order of last modification.
1253 If not doing file-name completion, then sort alphabetically.")
1254
1255 ;;;###autoload (autoload 'icicle-sort-by-file-type "icicles-mcmd.el")
1256 (icicle-define-sort-command "by file type" ; `icicle-sort-by-file-type'
1257     icicle-file-type-less-p
1258   "Sort file-name completion candidates by file type.
1259 Directories sort first, alphabetically.
1260 Then sort by file type (extension), alphabetically.
1261 Sort names that have the same extension alphabetically.
1262 If not doing file-name completion, sort candidates alphabetically.")
1263
1264 ;;;###autoload (autoload 'icicle-sort-by-directories-first "icicles-mcmd.el")
1265 (icicle-define-sort-command "by directories first" ; `icicle-sort-by-directories-first'
1266     icicle-dirs-first-p
1267   "Sort file-name completion candidates so that directories are first.
1268 If not doing file-name completion, then sort alphabetically.")
1269
1270 ;;;###autoload (autoload 'icicle-sort-by-directories-last "icicles-mcmd.el")
1271 (icicle-define-sort-command "by directories last" ; `icicle-sort-by-directories-last'
1272     icicle-dirs-last-p
1273   "Sort file-name completion candidates so that directories are last.
1274 If not doing file-name completion, then sort alphabetically.")
1275
1276 ;;;###autoload (autoload 'icicle-sort-by-last-use-as-input "icicles-mcmd.el")
1277 (icicle-define-sort-command "by last use as input" ; `icicle-sort-by-last-use-as-input'
1278     icicle-most-recent-first-p
1279   "Sort completion candidates in order of last use as minibuffer input.")
1280
1281 ;;;###autoload (autoload 'icicle-sort-by-previous-use-alphabetically "icicles-mcmd.el")
1282 (icicle-define-sort-command "by previous use alphabetically"
1283     icicle-historical-alphabetic-p      ; `icicle-sort-by-previous-use-alphabetically'
1284   "Sort completion candidates by previous use and alphabetically.
1285 Candidates matching previous inputs are available first.  Candidates
1286 are in two groups, each of which is sorted alphabetically separately:
1287 those matching previous inputs, followed by those that have not yet
1288 been used.")
1289
1290 ;;;###autoload (autoload 'icicle-sort-by-abbrev-frequency "icicles-mcmd.el")
1291 (icicle-define-sort-command "by abbrev frequency" ; `icicle-sort-by-abbrev-frequency'
1292     icicle-command-abbrev-used-more-p
1293   "Sort abbrev completion candidates by frequency of use
1294 Otherwise, sort alphabetically.  Ignore letter case if
1295 `completion-ignore-case' or `case-fold-search' is non-nil.")
1296
1297 ;;;###autoload (autoload 'icicle-sort-turned-OFF "icicles-mcmd.el")
1298 (icicle-define-sort-command "turned OFF" nil ; `icicle-sort-turned-OFF'
1299   "Do not sort completion candidates.")
1300
1301 ;;;###autoload
1302 (defun icicle-dispatch-M-_ ()           ; Bound to `M-_' in minibuffer.
1303   "Do the right thing for `M-_'.
1304 During Icicles search, call `icicle-toggle-search-replace-whole'.
1305 Otherwise, call `icicle-toggle-ignored-space-prefix'.
1306
1307 Bound to `M-_' in the minibuffer."
1308   (interactive)
1309   (if icicle-searching-p
1310       (icicle-toggle-search-replace-whole)
1311     (call-interactively #'icicle-toggle-ignored-space-prefix)))
1312
1313 ;;; No longer used.
1314 ;;; (defun icicle-dispatch-C-comma ()
1315 ;;;   "Do the right thing for `C-,'.
1316 ;;; When candidate sorting is possible, call `icicle-change-sort-order'.
1317 ;;; When searching, call `icicle-toggle-search-replace-whole'.
1318 ;;; Otherwise, do nothing.
1319 ;;;
1320 ;;; Bound to `C-,' in the minibuffer."
1321 ;;;   (interactive)
1322 ;;;   (cond (icicle-searching-p (icicle-toggle-search-replace-whole))
1323 ;;;         (icicle-inhibit-sort-p (message "Cannot sort candidates now"))
1324 ;;;         (t (call-interactively #'icicle-change-sort-order))))
1325
1326 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
1327 ;;;###autoload
1328 (defalias 'toggle-icicle-ignoring-comments 'icicle-toggle-ignoring-comments)
1329 ;;;###autoload
1330 (defun icicle-toggle-ignoring-comments () ; Bound to `C-M-;' in minibuffer.
1331   "Toggle the value of option `icicle-ignore-comments-flag'.
1332 If option `ignore-comments-flag' is defined (in library
1333 `thing-cmds.el') then it too is toggled.
1334 Bound to `C-M-;' in the minibuffer."
1335   (interactive)
1336   (setq icicle-ignore-comments-flag  (not icicle-ignore-comments-flag))
1337   (when (boundp 'ignore-comments-flag) (setq ignore-comments-flag  (not ignore-comments-flag)))
1338   (icicle-msg-maybe-in-minibuffer (if icicle-ignore-comments-flag
1339                                       "Ignoring comments is now ON"
1340                                     "Ignoring comments is now OFF")))
1341
1342 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
1343 ;;;###autoload
1344 (defalias 'toggle-icicle-search-replace-common-match 'icicle-toggle-search-replace-common-match)
1345 ;;;###autoload
1346 (defun icicle-toggle-search-replace-common-match () ; Bound to `M-;' in minibuffer.
1347   "Toggle the value of `icicle-search-replace-common-match-flag'.
1348 Note that that option has no effect if
1349 `icicle-expand-input-to-common-match-flag' is nil.
1350 Bound to `M-;' in the minibuffer."
1351   (interactive)
1352   (setq icicle-search-replace-common-match-flag  (not icicle-search-replace-common-match-flag))
1353   (icicle-msg-maybe-in-minibuffer (if icicle-search-replace-common-match-flag
1354                                       "Replacing expanded common match is now ON"
1355                                     "Replacing expanded common match is now OFF")))
1356
1357 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
1358 ;;;###autoload
1359 (defalias 'toggle-icicle-search-replace-whole 'icicle-toggle-search-replace-whole)
1360 ;;;###autoload
1361 (defun icicle-toggle-search-replace-whole ()
1362   "Toggle the value of `icicle-search-replace-whole-candidate-flag'.
1363 Bound to `M-_' in the minibuffer when searching."
1364   (interactive)
1365   (setq icicle-search-replace-whole-candidate-flag  (not icicle-search-replace-whole-candidate-flag))
1366   (icicle-msg-maybe-in-minibuffer (if icicle-search-replace-whole-candidate-flag
1367                                       "Replacing whole search context is now ON"
1368                                     "Replacing whole search context is now OFF")))
1369
1370 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
1371 ;;;###autoload
1372 (defalias 'toggle-icicle-dot 'icicle-toggle-dot)
1373 ;;;###autoload
1374 (defalias 'toggle-icicle-.   'icicle-toggle-dot)
1375 ;;;###autoload
1376 (defalias 'icicle-toggle-.   'icicle-toggle-dot)
1377 ;;;###autoload
1378 (defun icicle-toggle-dot ()             ; Bound to `C-M-.' in minibuffer.
1379   "Toggle `icicle-dot-string' between `.' and `icicle-anychar-regexp'.
1380 Bound to `C-M-.' in the minibuffer."
1381   (interactive)
1382   (setq icicle-dot-string  (if (string= icicle-dot-string ".") (icicle-anychar-regexp) "."))
1383   (icicle-msg-maybe-in-minibuffer
1384    (cond ((string= icicle-dot-string ".")
1385           (icicle-convert-dots (equal icicle-current-input icicle-last-input) t)
1386           "`.' now matches any char EXCEPT newline")
1387          (t
1388           (icicle-convert-dots (equal icicle-current-input icicle-last-input))
1389           "`.' now matches any char, including NEWLINE")))
1390   (setq icicle-dot-string-internal  icicle-dot-string))
1391
1392 (defun icicle-convert-dots (&optional no-confirm-p plainp)
1393   "Convert existing dots.
1394 Optional arg NO-CONFIRM-P means don't ask user for confirmation.
1395 Optional arg PLAINP means convert to plain `.'.
1396   Otherwise, convert to `icicle-anychar-regexp'."
1397   (if plainp
1398       (save-excursion
1399         (when (and (goto-char (icicle-minibuffer-prompt-end))
1400                    (search-forward icicle-anychar-regexp nil t))
1401           (goto-char (icicle-minibuffer-prompt-end))
1402           (while (search-forward icicle-anychar-regexp nil t)
1403             (replace-match "." nil t))))
1404     (save-excursion
1405       (when (and (goto-char (icicle-minibuffer-prompt-end)) (search-forward "." nil t))
1406         (goto-char (icicle-minibuffer-prompt-end))
1407         (let ((allp  nil))
1408           (while (search-forward "." nil t)
1409             ;; If we hit a plain dot inserted by user explicitly, ask if we should convert all such.
1410             (when (and (not allp)
1411                        (get-text-property (match-beginning 0) 'icicle-user-plain-dot)
1412                        (not no-confirm-p)
1413                        (y-or-n-p "Should all dots (`.') in current input match newlines too? "))
1414               (setq allp  t))
1415             (when (or allp (not (get-text-property (match-beginning 0) 'icicle-user-plain-dot)))
1416               (replace-match (icicle-anychar-regexp) nil t))))))))
1417
1418 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
1419 (when (require 'image-dired nil t)      ; Emacs 22+.
1420   (defalias 'cycle-icicle-image-file-thumbnail 'icicle-toggle-show-image-file-thumbnail)
1421   (defun icicle-cycle-image-file-thumbnail () ; Bound to `C-x t' in minibuffer.
1422     "Toggle `icicle-image-files-in-Completions'.
1423 This has no effect if you do not have library `image-dired.el' (Emacs 23+).
1424 Bound to `C-x t' in the minibuffer."
1425     (interactive)
1426     (if (not (require 'image-dired nil t))
1427         (message "No-op: this command requires library `image-dired.el'")
1428       (setq icicle-image-files-in-Completions
1429             (case icicle-image-files-in-Completions
1430               ((nil)       'image-only)
1431               (image-only  t)
1432               (t           nil)))
1433       (icicle-complete-again-update)
1434       (icicle-msg-maybe-in-minibuffer
1435        (case icicle-image-files-in-Completions
1436          ((nil)       "Image files in `*Completions*': showing only NAMES")
1437          (image-only  "Image files in `*Completions*': showing only IMAGES")
1438          (t           "Image files in `*Completions*': showing IMAGES and NAMES"))))))
1439
1440 ;;;###autoload
1441 (defun icicle-doremi-increment-max-candidates+ (&optional increment) ; `C-x #' in minibuffer
1442   "Change `icicle-max-candidates' incrementally.
1443 Use `up', `down' or the mouse wheel to increase or decrease.  You can
1444  use the `Meta' key (e.g. `M-up') to increment in larger steps.
1445 You can use a numeric prefix arg to specify the increment.
1446 A plain prefix arg (`C-u') resets `icicle-max-candidates' to nil,
1447  meaning no limit."
1448   (interactive "P")
1449   (cond ((consp increment)
1450          (setq icicle-max-candidates  nil)
1451          (icicle-msg-maybe-in-minibuffer "No longer any limit on number of candidates"))
1452         (t
1453          (setq increment  (prefix-numeric-value increment))
1454          (unless (require 'doremi nil t) (error "This command needs library `doremi.el'."))
1455          (let ((mini  (active-minibuffer-window)))
1456            (unwind-protect
1457                 (save-selected-window
1458                   (select-window (minibuffer-window))
1459                   (unless icicle-completion-candidates (message "Hit `TAB' or `S-TAB'"))
1460                   (let ((enable-recursive-minibuffers  t)
1461                         (nb-cands                      (length icicle-completion-candidates)))
1462                     (when (or (not icicle-max-candidates) (> icicle-max-candidates nb-cands))
1463                       (setq icicle-max-candidates  nb-cands))
1464                     (when (zerop icicle-max-candidates) (setq icicle-max-candidates 10))
1465                     (doremi (lambda (new-val)
1466                               (setq icicle-max-candidates
1467                                     (setq new-val (doremi-limit new-val 2 nil)))
1468                               (unless (input-pending-p)
1469                                 (let ((icicle-edit-update-p  t)
1470                                       (icicle-last-input     nil))
1471                                   (funcall (or icicle-last-completion-command
1472                                                (if (eq icicle-current-completion-mode 'prefix)
1473                                                    #'icicle-prefix-complete
1474                                                  #'icicle-apropos-complete)))
1475                                   (run-hooks 'icicle-update-input-hook)))
1476                               new-val)
1477                             icicle-max-candidates
1478                             increment))
1479                   (setq unread-command-events  ()))
1480              (unless mini (icicle-remove-Completions-window)))))))
1481
1482 ;;;###autoload
1483 (defun icicle-doremi-increment-swank-timeout+ () ; Bound to `C-x 1' in minibuffer (swank only)
1484   "Change `icicle-swank-timeout' incrementally.
1485 Use `up', `down' or the mouse wheel to increase or decrease.  You can
1486 use the `Meta' key (e.g. `M-up') to increment in larger steps."
1487   (interactive)
1488   (icicle-doremi-increment-variable+ 'icicle-swank-timeout 1000))
1489
1490 ;;;###autoload
1491 (defun icicle-doremi-increment-swank-prefix-length+ () ; Bound to `C-x 2' in minibuffer (swank only)
1492   "Change `icicle-swank-prefix-length' incrementally.
1493 Use `up', `down' or the mouse wheel to increase or decrease.  You can
1494 use the `Meta' key (e.g. `M-up') to increment in larger steps."
1495   (interactive)
1496   (icicle-doremi-increment-variable+ 'icicle-swank-prefix-length 1))
1497
1498 ;;;###autoload
1499 (defun icicle-next-TAB-completion-method (temporary-p) ; Bound to `C-(' in minibuffer.
1500   "Cycle to the next `TAB' completion method.
1501 Bound to \\<minibuffer-local-completion-map>`\\[icicle-next-TAB-completion-method]' \
1502 in the minibuffer.
1503 Option `icicle-TAB-completion-methods' determines the TAB completion
1504 methods that are available.
1505
1506 With a prefix argument, the newly chosen method is used only for the
1507 current command.  More precisely, the previously active method is
1508 restored as soon as you return to the top level."
1509   (interactive "P")
1510   (unless icicle-current-TAB-method     ; nil means the same as the default (first).
1511     (setq icicle-current-TAB-method  (car icicle-TAB-completion-methods)))
1512   (if temporary-p
1513       (unless (get 'icicle-last-top-level-command 'icicle-current-TAB-method)
1514         (put 'icicle-last-top-level-command 'icicle-current-TAB-method icicle-current-TAB-method))
1515     (put 'icicle-last-top-level-command 'icicle-current-TAB-method nil))
1516
1517   (let ((now  (memq icicle-current-TAB-method icicle-TAB-completion-methods)))
1518     (setq icicle-current-TAB-method  (or (cadr now) (car icicle-TAB-completion-methods)))
1519     ;; Skip any method that is not currently supported.
1520     (while (or (and (eq icicle-current-TAB-method 'fuzzy)        (not (featurep 'fuzzy-match)))
1521                (and (eq icicle-current-TAB-method 'vanilla)      (not (boundp 'completion-styles)))
1522                (and (eq icicle-current-TAB-method 'swank)        (not (featurep 'el-swank-fuzzy))))
1523       (setq now                        (memq icicle-current-TAB-method icicle-TAB-completion-methods)
1524             icicle-current-TAB-method  (or (cadr now) (car icicle-TAB-completion-methods)))))
1525   (cond ((and (eq icicle-current-TAB-method 'swank) (fboundp 'doremi))
1526          (define-key minibuffer-local-completion-map "\C-x1"
1527            'icicle-doremi-increment-swank-timeout+)
1528          (define-key minibuffer-local-must-match-map "\C-x1"
1529            'icicle-doremi-increment-swank-timeout+)
1530          (define-key minibuffer-local-completion-map "\C-x2"
1531            'icicle-doremi-increment-swank-prefix-length+)
1532          (define-key minibuffer-local-must-match-map "\C-x2"
1533            'icicle-doremi-increment-swank-prefix-length+))
1534         ((fboundp 'doremi)
1535          (define-key minibuffer-local-completion-map "\C-x1" nil)
1536          (define-key minibuffer-local-must-match-map "\C-x1" nil)
1537          (define-key minibuffer-local-completion-map "\C-x2" nil)
1538          (define-key minibuffer-local-must-match-map "\C-x2" nil)))
1539   ;; $$$$$$ Inhibiting sorting is not correct for file-name completion, and sorting would not be
1540   ;;        restored when change back to non-fuzzy.
1541   ;; (when (eq 'fuzzy icicle-current-TAB-method) (setq icicle-inhibit-sort-p  t))
1542   (icicle-msg-maybe-in-minibuffer "TAB completion is %s %s"
1543                                   (icicle-upcase (symbol-name icicle-current-TAB-method))
1544                                   (if temporary-p "for this command" "now")))
1545
1546 ;;;###autoload
1547 (defun icicle-next-S-TAB-completion-method (temporary-p) ; Bound to `M-(' in minibuffer.
1548   "Cycle to the next `S-TAB' completion method.
1549 Bound to `M-(' in the minibuffer.
1550 Option `icicle-S-TAB-completion-methods-alist' customizes the
1551 available TAB completion methods.
1552
1553 With a prefix argument, the newly chosen method is used only for the
1554 current command.  More precisely, the previously active method is
1555 restored as soon as you return to the top level."
1556   (interactive "P")
1557   (if temporary-p
1558       (unless (get 'icicle-last-top-level-command 'icicle-apropos-complete-match-fn)
1559         (put 'icicle-last-top-level-command 'icicle-apropos-complete-match-fn
1560              icicle-apropos-complete-match-fn))
1561     (put 'icicle-last-top-level-command 'icicle-apropos-complete-match-fn nil))
1562   (let ((entry  (rassq icicle-apropos-complete-match-fn icicle-S-TAB-completion-methods-alist)))
1563     (setq icicle-apropos-complete-match-fn
1564           (or (cdadr (member entry icicle-S-TAB-completion-methods-alist))
1565               (cdar icicle-S-TAB-completion-methods-alist))
1566           icicle-last-apropos-complete-match-fn  icicle-apropos-complete-match-fn) ; Backup copy.
1567     (icicle-msg-maybe-in-minibuffer
1568      (format "S-TAB completion is %s%s %s"
1569              (icicle-upcase (car (rassq icicle-apropos-complete-match-fn
1570                                         icicle-S-TAB-completion-methods-alist)))
1571              (if (memq icicle-apropos-complete-match-fn
1572                        '(icicle-levenshtein-match icicle-levenshtein-strict-match))
1573                  (format " (%d)" icicle-levenshtein-distance)
1574                "")
1575              (if temporary-p "for this command" "now")))))
1576     ;; (icicle-complete-again-update) ; No - too slow for some completion methods.
1577
1578 ;;;###autoload
1579 (defun icicle-change-sort-order (&optional arg alternativep) ; Bound to `C-,' in minibuffer.
1580   "Choose a sort order.
1581 With a numeric prefix arg, reverse the current sort order.
1582
1583 If plain `C-u' is used or `C-u' is not used at all:
1584
1585 - Use completion if `icicle-change-sort-order-completion-flag' is
1586   non-nil and no prefix arg is used, or if it is nil and a prefix arg
1587   is used.
1588
1589 - Otherwise, just cycle to the next sort order.
1590
1591 This command updates `icicle-sort-comparer'.  Non-interactively,
1592 optional arg ALTERNATIVEP means change the current alternative sort
1593 order instead, updating `icicle-alternative-sort-comparer'."
1594   (interactive "P")
1595   (setq icicle-sort-orders-alist  (delq nil icicle-sort-orders-alist)) ; Purge any nil entries.
1596   (if (and (interactive-p) icicle-inhibit-sort-p)
1597       (icicle-msg-maybe-in-minibuffer "Cannot sort candidates now")
1598     (if (and arg (not (consp arg)))
1599         (icicle-reverse-sort-order)
1600       (let (next-order)
1601         (cond ((or (and icicle-change-sort-order-completion-flag (not arg)) ; Use completion.
1602                    (and (not icicle-change-sort-order-completion-flag) arg))
1603                (setq next-order  (let ((icicle-whole-candidate-as-text-prop-p  nil)
1604                                        (enable-recursive-minibuffers           t))
1605                                    (save-selected-window
1606                                      (completing-read
1607                                       (format "New %ssort order: " (if alternativep "alternative " ""))
1608                                       (icicle-current-sort-functions)
1609                                       nil t))))
1610                (set (if alternativep 'icicle-alternative-sort-comparer 'icicle-sort-comparer)
1611                     (cdr (assoc next-order icicle-sort-orders-alist))))
1612               (t                        ; Cycle to next sort order.
1613                (let ((orders  (mapcar #'car (icicle-current-sort-functions))))
1614                  (setq next-order  (or (cadr (memq (icicle-current-sort-order alternativep) orders))
1615                                        (car orders)))
1616                  (set (if alternativep 'icicle-alternative-sort-comparer 'icicle-sort-comparer)
1617                       (cdr (assoc next-order icicle-sort-orders-alist))))))
1618         (icicle-complete-again-update)
1619         (icicle-msg-maybe-in-minibuffer
1620          "%sorting is now %s%s.  Reverse: `C-9 C-,'"
1621          (if alternativep "Alternative s" "S") next-order
1622          (if icicle-reverse-sort-p ", REVERSED" ""))))))
1623
1624 (defun icicle-current-sort-functions ()
1625   "Subset of `icicle-sort-orders-alist' that is currently appropriate.
1626 For some common kinds of completion, remove simple sort functions (not
1627 multi-sort comparers) that are not pertinent for the current kind of
1628 completion."
1629   (icicle-remove-if (lambda (pred)
1630                       (setq pred  (cdr pred))
1631                       (and pred (symbolp pred) ; Do not handle multi-sort comparers.
1632                            (or (and (get pred 'icicle-proxy-sort-predicate)
1633                                     (not icicle-add-proxy-candidates-flag))
1634                                (and (get pred 'icicle-file-name-sort-predicate)
1635                                     (not (icicle-file-name-input-p)))
1636                                ;; Not really needed yet, because we only add such sorts dynamically.
1637                                (and (get pred 'icicle-buffer-name-sort-predicate) ; Better than nothing.
1638                                     (not (eq minibuffer-history-variable 'buffer-name-history)))
1639                                (and (get pred 'icicle-command-sort-predicate)
1640                                     (not (and (eq minibuffer-completion-table obarray)
1641                                               ;; But this will fail if predicate is more complex.
1642                                               (eq minibuffer-completion-predicate 'commandp))))
1643                                ;; Sort order for multi-completions. `minibuffer-completion-table'
1644                                ;; could be a function (e.g. `icicle-describe-opt-of-type-complete')
1645                                ;; or it could be a list of multi-completions.
1646                                (and (get pred 'icicle-multi-completion-sort-predicate)
1647                                     (not (icicle-maybe-multi-completion-completing-p))))))
1648                     icicle-sort-orders-alist))
1649
1650 (defun icicle-maybe-multi-completion-completing-p ()
1651   "Returns non-nil if we might currently be multi-completion completing.
1652 Note: If `minibuffer-completion-table' is a function, multi-completion
1653 is possible but not sure.  Return non-nil in that case."
1654   (or (functionp minibuffer-completion-table) icicle-list-use-nth-parts))
1655
1656 ;;;###autoload
1657 (defun icicle-dispatch-M-comma ()       ; Bound to `M-,' in minibuffer.
1658   "Do the right thing for `M-,'.
1659 If sorting is possible, call `icicle-change-alternative-sort-order'.
1660 If using `icicle-search', call `icicle-search-define-replacement'.
1661 Otherwise, do nothing.
1662
1663 Bound to `M-,' in the minibuffer."
1664   (interactive)
1665   (cond (icicle-searching-p (icicle-search-define-replacement))
1666         (icicle-inhibit-sort-p (message "Cannot sort candidates now"))
1667         (t (icicle-change-alternative-sort-order))))
1668
1669 ;; Free vars here: `icicle-scan-fn-or-regexp' is bound in `icicle-search'.
1670 ;;;###autoload
1671 (defun icicle-search-define-replacement () ; Bound to `M-,' in minibuffer during `icicle-search'.
1672   "Prompt user and set new value of `icicle-search-replacement'.
1673 Bound to `M-,' in the minibuffer."
1674   (interactive)
1675   (save-selected-window
1676     (icicle-remove-Completions-window)) ; Prevent incremental completion kicking in from the get-go.
1677   (setq icicle-search-replacement
1678         (let ((enable-recursive-minibuffers        t)
1679               (icicle-incremental-completion-flag  t) ; Override current upgrade to `always'.
1680               (icicle-completion-candidates        icicle-completion-candidates)
1681               (icicle-current-input                icicle-current-input)
1682               (icicle-candidate-nb                 icicle-candidate-nb)
1683               (icicle-update-input-hook            nil))
1684           (icicle-completing-read-history "Replace with: " 'icicle-search-replacement-history)))
1685   ;; Just a sanity check.  Cannot really test equivalence of two regexps.
1686   (while (if icicle-search-replace-whole-candidate-flag
1687              (equal icicle-search-replacement icicle-scan-fn-or-regexp)
1688            (equal icicle-search-replacement icicle-current-input))
1689     (setq icicle-search-replacement
1690           (let ((enable-recursive-minibuffers        t)
1691                 (icicle-incremental-completion-flag  t) ; Override current upgrade to `always'.
1692                 (icicle-completion-candidates        icicle-completion-candidates)
1693                 (icicle-current-input                icicle-current-input)
1694                 (icicle-candidate-nb                 icicle-candidate-nb)
1695                 (icicle-update-input-hook            nil))
1696             (icicle-completing-read-history "Replacement = replaced.  Replace with: "
1697                                             'icicle-search-replacement-history)))))
1698
1699 ;;;###autoload
1700 (defun icicle-change-alternative-sort-order (&optional arg) ; Bound to `M-,' in minibuffer (not search).
1701   "Choose an alternative sort order.
1702 Similar to command `icicle-change-sort-order', but change the
1703 alternative sort order, not the current sort order."
1704   (interactive "P")
1705   (if (and (interactive-p) icicle-inhibit-sort-p)
1706       (icicle-msg-maybe-in-minibuffer "Cannot sort candidates now")
1707     (icicle-change-sort-order arg t)))
1708
1709 (defun icicle-current-sort-order (alternativep)
1710   "Current sort order, or nil if sorting is inactive.
1711 If ALTERNATIVEP is non-nil, the alternative sort order is returned."
1712   (car (rassq (if alternativep icicle-alternative-sort-comparer icicle-sort-comparer)
1713               icicle-sort-orders-alist)))
1714
1715 ;;;###autoload
1716 (defun icicle-reverse-sort-order ()
1717   "Reverse the current sort order."
1718   (interactive)
1719   (if (and (interactive-p) icicle-inhibit-sort-p)
1720       (icicle-msg-maybe-in-minibuffer "Cannot sort candidates now")
1721     (setq icicle-reverse-sort-p  (not icicle-reverse-sort-p))
1722     (icicle-display-candidates-in-Completions icicle-reverse-sort-p)
1723     (icicle-complete-again-update)
1724     (icicle-msg-maybe-in-minibuffer
1725      (format "Sort order is %s%s"
1726              (icicle-current-sort-order nil) (if icicle-reverse-sort-p ", REVERSED" "")))))
1727
1728 ;;;###autoload
1729 (defun icicle-plus-saved-sort ()        ; Bound to `C-M-+' during completion.
1730   "Sort candidates by combining their current order with the saved order."
1731   (interactive)
1732   (let ((icicle-sort-comparer  'icicle-merge-saved-order-less-p)
1733         (cands                 (copy-sequence icicle-completion-candidates)))
1734     (setq icicle-completion-candidates
1735           (if (or (icicle-file-name-input-p) icicle-abs-file-candidates)
1736               (icicle-strip-ignored-files-and-sort cands)
1737             (icicle-maybe-sort-maybe-truncate cands))))
1738   (when (get-buffer-window "*Completions*" 0) (icicle-display-candidates-in-Completions))
1739   (when (interactive-p) (icicle-msg-maybe-in-minibuffer "Added in the saved sort order")))
1740
1741  
1742 ;;(@* "Other commands to be used mainly in the minibuffer")
1743
1744 ;;; Other commands to be used mainly in the minibuffer . . . . . . . .
1745
1746 ;; $$ Probably need to do something to work around problem of Windows
1747 ;; selecting the new frame, when `pop-up-frames' is non-nil.  Need to
1748 ;; redirect focus back to the frame with the minibuffer.  Leave it as
1749 ;; is, for now, in hopes Emacs will eventually fix this.
1750 ;;
1751 ;;;###autoload
1752 (defun icicle-minibuffer-help ()        ; Bound to `C-?' in minibuffer.
1753   "Describe Icicles minibuffer and *Completion* buffer bindings."
1754   (interactive)
1755   (let ((cur-buf  (current-buffer)))
1756     (with-output-to-temp-buffer "*Help*"
1757       (help-setup-xref (list #'icicle-minibuffer-help) (interactive-p))
1758       (when (icicle-completing-p)
1759         (princ (concat "You are completing input" (and icicle-candidate-action-fn
1760                                                        " for an Icicles multi-command")
1761                        ".\n\n"))
1762         (princ "To show help on individual completion candidates:
1763      Current candidate                       C-M-RET, C-M-mouse-2
1764      Next, previous candidate                C-M-down, C-M-up,
1765                                               C-M- plus mouse wheel
1766                     prefix-match candidate   C-M-end, C-M-home
1767                     apropos-match candidate  C-M-next, C-M-prior\n\n")
1768         (when icicle-candidate-action-fn
1769           (princ "To act on individual candidates:
1770      Current candidate                       C-RET, C-mouse-2
1771      Next, previous candidate                C-down, C-up,
1772                                               C- plus mouse wheel
1773                     prefix-match candidate   C-end, C-home
1774                     apropos-match candidate  C-next, C-prior
1775      All candidates at once                  C-! (each) or M-! (list)
1776      Delete object named by candidate        S-delete
1777      Object-action: apply a fn to candidate  M-RET"))
1778         (when icicle-candidate-alt-action-fn
1779           (princ "\n\nFor alt action, use `C-S-' instead of `C-', but use `C-|' or `M-|',\n\
1780      instead of `C-!' or `M-!', to act on all.\n")))
1781       (if icicle-completing-p
1782           (with-current-buffer standard-output
1783             (insert (concat "\n" (icicle-help-string-completion))))
1784         (princ (icicle-help-string-non-completion))))
1785     ;; Don't bother to do this for Emacs 21.3.  Its `help-insert-xref-button' signature is different.
1786     (when (and (> emacs-major-version 21)
1787                (require 'help-mode nil t) (fboundp 'help-insert-xref-button)) ; In `help-mode.el'.
1788       (save-excursion
1789         (with-current-buffer (get-buffer "*Help*")
1790           (let ((buffer-read-only  nil))
1791             (goto-char (point-min))
1792             (help-insert-xref-button "[Icicles Help on the Web]" 'icicle-help-button)
1793             (insert "                        ")
1794             (help-insert-xref-button "[Icicles Doc, Part 1]" 'icicle-commentary1-button)
1795             (insert "\n")
1796             (help-insert-xref-button "[Icicles Options & Faces]" 'icicle-customize-button)
1797             (insert "                        ")
1798             (help-insert-xref-button "[Icicles Doc, Part 2]" 'icicle-commentary2-button)
1799             (insert "\n\n")
1800             (goto-char (point-max))
1801             (insert (make-string 70 ?_))
1802             (insert (funcall
1803                      (if (fboundp 'help-commands-to-key-buttons) ; In `help-fns.el'.
1804                          #'help-commands-to-key-buttons
1805                        #'substitute-command-keys)
1806                      "\n\nSend an Icicles bug report: `\\[icicle-send-bug-report]'.\n\n"))
1807             (help-insert-xref-button "[Icicles Help on the Web]" 'icicle-help-button)
1808             (insert "                        ")
1809             (help-insert-xref-button "[Icicles Doc, Part 1]" 'icicle-commentary1-button)
1810             (insert "\n")
1811             (help-insert-xref-button "[Icicles Options & Faces]" 'icicle-customize-button)
1812             (insert "                        ")
1813             (help-insert-xref-button "[Icicles Doc, Part 2]" 'icicle-commentary2-button)
1814             (insert "\n\n")
1815             (goto-char (point-min))))))
1816     (when (memq cur-buf (list (window-buffer (minibuffer-window)) (get-buffer "*Completions*")))
1817       (select-window (minibuffer-window))
1818       (select-frame-set-input-focus (selected-frame)))))
1819
1820 (defun icicle-help-string-completion ()
1821   "Update the bindings within the Icicles completion help string."
1822   (icicle-S-iso-lefttab-to-S-TAB
1823    (funcall
1824     (if (fboundp 'help-commands-to-key-buttons) ; In `help-fns+.el'.
1825         #'help-commands-to-key-buttons
1826       #'substitute-command-keys)
1827     (concat
1828      (format "\\<minibuffer-local-completion-map> 
1829
1830                     Icicles Minibuffer Completion
1831                     -----------------------------
1832
1833 Minibuffer input can be completed in several ways.
1834 These are the main Icicles actions and their minibuffer key bindings:
1835
1836  * Show Icicles minibuffer help (this).      \\[icicle-minibuffer-help]
1837      For help on individual completion candidates, see \"Show help on
1838      individual completion candidates\", below.
1839
1840  * Abandon or commit your input.
1841      Abandon input                           \\[icicle-abort-recursive-edit]
1842      Commit input to Emacs                   RET
1843        Complete partial input, then commit   \\<minibuffer-local-must-match-map>\
1844 \\[icicle-apropos-complete-and-exit]\\<minibuffer-local-completion-map>
1845
1846  * Toggle/cycle Icicles options on the fly.  Key:   \tCurrently:
1847      Highlighting of past inputs             \\[icicle-toggle-highlight-historical-candidates]\t%S
1848      Highlighting of saved candidates        \\[icicle-toggle-highlight-saved-candidates]\t%S
1849      Removal of duplicate candidates         \\[icicle-toggle-transforming]\t%S
1850      Sort order                              \\[icicle-change-sort-order]\t%s
1851      Alternative sort order                  \\[icicle-dispatch-M-comma]\t%s
1852      Swap alternative/normal sort            \\[icicle-toggle-alternative-sorting]\t- (swaps) -
1853      Case sensitivity                        \\[icicle-toggle-case-sensitivity]\t%S
1854      `.' matching newlines too (any char)    \\[icicle-toggle-dot]\t%S
1855      Escaping of special regexp chars        \\[icicle-toggle-regexp-quote]\t%S
1856      Incremental completion                  \\[icicle-toggle-incremental-completion]\t%S
1857      Input expansion to common match         \\[icicle-toggle-expand-to-common-match]\t%S
1858      Hiding common match in `*Completions*'  \\[icicle-dispatch-C-x.]\t%S
1859      Hiding no-match lines in `*Completions*' C-u \\[icicle-dispatch-C-x.]\t%S
1860      S-TAB completion method                 \\[icicle-next-S-TAB-completion-method]\t%s
1861      TAB completion method                   \\[icicle-next-TAB-completion-method]\t%s
1862      Showing image-file thumbnails (E22+)    C-x t\t%S
1863      Inclusion of proxy candidates           \\[icicle-toggle-proxy-candidates]\t%S
1864      Ignoring certain file extensions        \\[icicle-dispatch-C-.]\t%S
1865      Checking for remote file names          \\[icicle-dispatch-C-^]\t%S
1866      Ignoring space prefix                   \\[icicle-dispatch-M-_]\t%S
1867      Using `C-' for multi-command actions    \\[icicle-toggle-C-for-actions]\t%S
1868      Using `~' for your home directory       \\[icicle-toggle-~-for-home-dir]\t%S
1869      `icicle-search' all-current highlights  \\[icicle-dispatch-C-^]\t%S
1870      Whole-word searching                    \\[icicle-dispatch-M-q]\t%S
1871      Removal of `icicle-search' highlighting \\[icicle-dispatch-C-.]\t%S
1872      Replacement of whole search hit         \\[icicle-dispatch-M-_]\t%S
1873      Replacement of expanded common match    \\[icicle-toggle-search-replace-common-match]\t%S
1874
1875  * Regexp-quote input, then apropos-complete \\[icicle-regexp-quote-input]
1876
1877  * Change the set of completion candidates.  Modify your input.
1878      Edit your input                         (just edit in minibuffer)
1879      Erase your input (clear minibuffer)     \\[icicle-erase-minibuffer-or-history-element]
1880      Goto/kill non-matching portion of input \\[icicle-goto/kill-failed-input]
1881      Retrieve previous completion inputs     \\[icicle-retrieve-previous-input], \
1882 \\[icicle-retrieve-next-input]
1883      Match another regexp (chaining)         \\[icicle-narrow-candidates]
1884      Satisfy another predicate (chaining)    \\[icicle-narrow-candidates-with-predicate]
1885      Remove a candidate from set of matches  delete, S-mouse-2
1886      Yank text at cursor into minibuffer     \\[icicle-insert-string-at-point]
1887      Insert text (string) from a variable    \\[icicle-insert-string-from-variable]
1888      Insert `icicle-list-join-string'        \\[icicle-insert-list-join-string]
1889      Insert previously entered input         \\[icicle-insert-history-element]
1890      Insert key description (key completion) \\[icicle-dispatch-M-q]
1891
1892  * Complete your current input in the minibuffer.
1893      Apropos (regexp) completion             \\[icicle-apropos-complete]
1894        Without displaying candidates         \\[icicle-apropos-complete-no-display]
1895        Complete and match another regexp     \\[icicle-apropos-complete-and-narrow]
1896      Prefix completion
1897        As much as possible                   \\[icicle-prefix-complete]
1898          Without displaying candidates       \\[icicle-prefix-complete-no-display]
1899        A word at a time                      \\[icicle-prefix-word-complete]
1900      Complete and commit (if required match) \\<minibuffer-local-must-match-map>\
1901 \\[icicle-apropos-complete-and-exit]\\<minibuffer-local-completion-map>
1902      Complete search string using past input \\[icicle-apropos-complete]
1903
1904  * Display/navigate completions for current input (in `*Completions*').
1905      Show completion candidates
1906        Prefix completion                     \\[icicle-prefix-complete] (repeat)
1907        Apropos completion                    \\[icicle-apropos-complete]
1908      Move between minibuffer and list        \\<completion-list-mode-map>\
1909 \\[icicle-insert-completion]
1910      Cycle among completion candidates       right, left, \
1911 \\[icicle-move-to-next-completion], \\[icicle-move-to-previous-completion]
1912        Within a `*Completions*' column       down, up
1913      Choose a completion candidate           \\[choose-completion], \
1914 \\[mouse-choose-completion]\\<minibuffer-local-completion-map>
1915
1916  * Cycle among input candidates.
1917      Completion candidates
1918        Current mode                          down, up, mouse wheel
1919        Prefix completion                     end, home
1920        Apropos completion                    next, prior
1921      Minibuffer history items                \\[next-history-element], \
1922 \\[previous-history-element]
1923      Completion history items                \\[icicle-retrieve-previous-input], \
1924 \\[icicle-retrieve-next-input]
1925
1926  * Show help on individual completion candidates.
1927      Current candidate                       C-M-RET, C-M-mouse-2
1928      Next, previous candidate                C-M-down, C-M-up,
1929                                               C-M- plus mouse wheel
1930                     prefix-match candidate   C-M-end, C-M-home
1931                     apropos-match candidate  C-M-next, C-M-prior
1932
1933  * Choose a previous input from the minibuffer history.
1934      Complete to insert a previous input     \\[icicle-insert-history-element]
1935      Complete against history items          \\[icicle-history], \
1936 \\[icicle-keep-only-past-inputs]
1937      Restrict candidates to history items    \\[icicle-keep-only-past-inputs]
1938      Change to another history               \\[icicle-other-history]
1939      List history items first in Completions \\[icicle-toggle-alternative-sorting]
1940      Cycle among minibuffer history items    \\[next-history-element], \
1941 \\[previous-history-element]
1942      Search among minibuffer history items   \
1943 \\[next-matching-history-element], \\[previous-matching-history-element]
1944
1945  * Delete history entries
1946      Delete current entry (cycling)          \\[icicle-erase-minibuffer-or-history-element]
1947      Delete any or all entries               \\[icicle-clear-current-history]
1948
1949  * Multi-commands: Act on completion candidates.
1950    For alternative action, use `C-S-' instead of `C-', but
1951    `C-|' and `M-|' are alternative action versions of `C-!' and `M-!'.
1952      Current candidate                       C-RET, C-mouse-2
1953      Next, previous candidate                C-down, C-up,
1954                                               C- plus mouse wheel
1955                     prefix-match candidate   C-end, C-home
1956                     apropos-match candidate  C-next, C-prior
1957      Act on each matching candidate, in turn C-!
1958      Act on the list of matching candidates  M-!
1959      Delete object named by candidate        S-delete
1960      Remove candidate from set of matches    delete, S-mouse-2
1961      Save candidate (add to those saved)     insert, M-S-mouse-2
1962      Object-action: apply a fn to candidate  M-RET
1963
1964  * Search and replace (e.g. `C-c `').  See also `icicle-search'.
1965      Use action keys (prefix `C-') to navigate.
1966      Use alternative action keys (prefix `C-S-') to replace matches.
1967      Toggle input highlighting at all hits   \\[icicle-dispatch-C-^]
1968      Toggle whole-word searching             \\[icicle-dispatch-M-q]
1969      Toggle `.' matching newlines too        \\[icicle-toggle-dot]
1970      Toggle escaping of special regexp chars \\[icicle-toggle-regexp-quote]
1971      Toggle removal of search highlighting   \\[icicle-dispatch-C-.]
1972
1973      Replace all                             M-|
1974      Redefine the replacement string         \\[icicle-dispatch-M-comma]
1975      Toggle literal replacement              \\[icicle-toggle-literal-replacement]
1976      Toggle replacement of whole search hit  \\[icicle-dispatch-M-_]
1977      Toggle replacement of common match      \\[icicle-toggle-search-replace-common-match]
1978
1979  * Perform set operations on candidate sets.
1980      Remove candidate from current set       delete, S-mouse-2
1981      Add current candidate to saved set      insert, M-S-mouse-2
1982      Retrieve saved candidates from...
1983        `icicle-saved-completion-candidates'  \\[icicle-candidate-set-retrieve]
1984        another variable                      \\[icicle-candidate-set-retrieve-from-variable]
1985        a cache file                          \\[icicle-candidate-set-retrieve-persistent]
1986      Retrieve more saved candidates          \\[icicle-candidate-set-retrieve-more]
1987      Save candidates in current set to...
1988        `icicle-saved-completion-candidates'  \\[icicle-candidate-set-save]
1989        another variable                      \\[icicle-candidate-set-save-to-variable]
1990        a cache file                          \\[icicle-candidate-set-save-persistently]
1991      Save more candidates to current set     \\[icicle-candidate-set-save-more]
1992      Save, save more selected candidates     \\[icicle-candidate-set-save-selected], \
1993 \\[icicle-candidate-set-save-more-selected]  with region
1994      Clear all saved candidates              \\[icicle-candidate-set-save-selected] \
1995 with empty region
1996      Add new or update existing saved set
1997        \\[icicle-add/update-saved-completion-set]
1998      Remove a saved completion set
1999        \\[icicle-remove-saved-completion-set]
2000      Swap current and saved sets             \\[icicle-candidate-set-swap]
2001      Define current set by evaluating sexp   \\[icicle-candidate-set-define]
2002      Restrict candidates to history items    \\[icicle-keep-only-past-inputs]
2003      Set complement                          \\[icicle-candidate-set-complement]
2004      Set difference                          \\[icicle-candidate-set-difference]
2005      Set union                               \\[icicle-candidate-set-union]
2006      Set intersection                        \\[icicle-candidate-set-intersection]
2007      Set intersection using regexp           \\[icicle-narrow-candidates]
2008      Set intersection using predicate        \\[icicle-narrow-candidates-with-predicate]
2009        Save current predicate to a variable  \\[icicle-save-predicate-to-variable]
2010        Insert string variable as input       \\[icicle-insert-string-from-variable]
2011
2012  * Adjust Icicles options incrementally on the fly (uses Do Re Mi).
2013      `icicle-candidate-width-factor'        \\[icicle-doremi-candidate-width-factor+]
2014      `icicle-max-candidates'                \\[icicle-doremi-increment-max-candidates+]
2015      `icicle-swank-timeout'                 C-x 1
2016      `icicle-swank-prefix-length'           C-x 2
2017      `icicle-inter-candidates-min-spaces'   \\[icicle-doremi-inter-candidates-min-spaces+]
2018      Zoom `*Completions*' (not an option)   C-x -   (Emacs 23+)
2019
2020 Remember: You can always input any character (e.g. \\[icicle-prefix-complete]) that is bound
2021           to a command by preceding it with \\<global-map>\\[quoted-insert].
2022
2023 Though it has no direct connection with completion, you can use \
2024 `\\<minibuffer-local-completion-map>\\[icicle-pp-eval-expression-in-minibuffer]'
2025 in the minibuffer at any time to evaluate an Emacs-Lisp expression.
2026 This calls `icicle-pp-eval-expression-in-minibuffer', which displays
2027 the result in the echo area or a popup buffer, *Pp Eval Output*.
2028 It also provides some of the Emacs-Lisp key bindings during expression
2029 editing."
2030              icicle-highlight-historical-candidates-flag
2031              icicle-highlight-saved-candidates-flag
2032              icicle-transform-function
2033              (icicle-current-sort-order nil)
2034              (icicle-current-sort-order 'ALTERNATIVE)
2035              (not case-fold-search)
2036              (string= icicle-dot-string icicle-anychar-regexp)
2037              icicle-regexp-quote-flag
2038              icicle-incremental-completion-flag
2039              icicle-expand-input-to-common-match-flag
2040              icicle-hide-common-match-in-Completions-flag
2041              icicle-hide-non-matching-lines-flag
2042              (car (rassq icicle-apropos-complete-match-fn icicle-S-TAB-completion-methods-alist))
2043              (icicle-current-TAB-method)
2044              icicle-add-proxy-candidates-flag
2045              (and completion-ignored-extensions t)
2046              icicle-image-files-in-Completions
2047              icicle-test-for-remote-files-flag
2048              icicle-ignore-space-prefix-flag
2049              icicle-use-C-for-actions-flag
2050              icicle-use-~-for-home-dir-flag
2051              icicle-search-highlight-all-current-flag
2052              icicle-search-whole-word-flag
2053              icicle-search-cleanup-flag                
2054              icicle-search-replace-whole-candidate-flag
2055              icicle-search-replace-common-match-flag)
2056      icicle-general-help-string
2057      " 
2058
2059 These are all of the minibuffer bindings during completion:
2060
2061 \\{minibuffer-local-completion-map}"))))
2062
2063 (defun icicle-help-string-non-completion ()
2064   "Description of Icicles minibuffer bindings when not completing input."
2065   (icicle-S-iso-lefttab-to-S-TAB
2066    (substitute-command-keys
2067     (concat "\\<minibuffer-local-completion-map>\
2068               Icicles Minibuffer Input when Not Completing
2069               --------------------------------------------
2070
2071 These are the main Icicles minibuffer key bindings when completion is
2072 not available:
2073
2074  * Show this help.                           \\[icicle-minibuffer-help]
2075
2076  * Abandon your input.                       \\[icicle-abort-recursive-edit]
2077
2078  * Commit your input to Emacs.               RET
2079
2080  * Modify your input.
2081      Edit your input                         (just edit in minibuffer)
2082      Erase your input (clear minibuffer)     \\[icicle-erase-minibuffer-or-history-element]
2083      Yank text at cursor into minibuffer     \\[icicle-insert-string-at-point]
2084      Insert text (string) from a variable    \\[icicle-insert-string-from-variable]
2085      Insert previously entered input         \\[icicle-insert-history-element]
2086
2087  * Choose a previous input from the minibuffer history.
2088      Complete to insert a previous input     \\[icicle-insert-history-element]
2089      Cycle among minibuffer history items    \\[next-history-element], \
2090 \\[previous-history-element]
2091      Search among minibuffer history items   \
2092 \\[next-matching-history-element], \\[previous-matching-history-element]
2093
2094  * Delete history entries
2095      Delete current entry (cycling)          \\[icicle-erase-minibuffer-or-history-element]
2096      Delete any or all entries               \\[icicle-clear-current-history]
2097
2098  * Evaluate an Emacs-Lisp sexp on the fly    \\[icicle-pp-eval-expression-in-minibuffer]
2099
2100 Remember: You can always input any character that is bound to a
2101           command by preceding it with \\<global-map>\\[quoted-insert]."
2102             icicle-general-help-string
2103
2104             " 
2105 These are the minibuffer bindings when not completing input:
2106
2107 \\{minibuffer-local-map}"))))
2108
2109 (when (and (> emacs-major-version 21)
2110            (require 'help-mode nil t) (get 'help-xref 'button-category-symbol)) ; In `button.el'
2111   (define-button-type 'icicle-help-button
2112       :supertype 'help-xref
2113       'help-function #'(lambda () (browse-url "http://www.emacswiki.org/cgi-bin/wiki/Icicles"))
2114       'help-echo
2115       (purecopy "mouse-2, RET: Icicles documentation on the Emacs Wiki (requires Internet access)"))
2116   (define-button-type 'icicle-commentary1-button
2117       :supertype 'help-xref
2118       'help-function #'(lambda ()
2119                          (finder-commentary "icicles-doc1")
2120                          (when (require 'linkd nil t) (linkd-mode 1))
2121                          (when (require 'fit-frame nil t) (fit-frame)))
2122       'help-echo (purecopy "mouse-2, RET: Icicles documentation, Part 1 (no Internet needed)"))
2123   (define-button-type 'icicle-commentary2-button
2124       :supertype 'help-xref
2125       'help-function #'(lambda ()
2126                          (finder-commentary "icicles-doc2")
2127                          (when (require 'linkd nil t) (linkd-mode 1))
2128                          (when (require 'fit-frame nil t) (fit-frame)))
2129       'help-echo (purecopy "mouse-2, RET: Icicles documentation, Part 2 (no Internet needed)"))
2130   (define-button-type 'icicle-customize-button
2131       :supertype 'help-xref
2132       'help-function #'(lambda () (customize-group-other-window 'Icicles))
2133       'help-echo (purecopy "mouse-2, RET: Customize/Browse Icicles Options & Faces")))
2134
2135
2136 ;; This is just the macro expansion of the following:
2137 ;; `(def-completion-wrapper icicle-abort-recursive-edit :minibuffer-separator)'.
2138 ;; Taken from the definition of `def-completion-wrapper' in `completion.el'.
2139 (put 'icicle-abort-recursive-edit 'completion-function 'use-completion-minibuffer-separator)
2140 ;;;###autoload
2141 (defun icicle-abort-recursive-edit ()   ; Bound to `C-]',`C-g' in minibuf, `C-g',`q' in `*Completions*'.
2142   "Abort command that requested this recursive edit or minibuffer input.
2143 This calls `abort-recursive-edit' after killing the `*Completions*'
2144 buffer or (if called from the minibuffer) removing its window.
2145
2146 By default, Icicle mode remaps all key sequences that are normally
2147 bound to `abort-recursive-edit' to `icicle-abort-recursive-edit'.  If
2148 you do not want this remapping, then customize option
2149 `icicle-top-level-key-bindings'."
2150   (interactive)
2151   (if (not (active-minibuffer-window))
2152       (when (get-buffer "*Completions*") (kill-buffer (get-buffer "*Completions*")))
2153     (icicle-remove-Completions-window 'FORCE))
2154   (abort-recursive-edit))
2155
2156 (unless (fboundp 'save&set-overriding-map) ; Only Emacs 20-23 use `ensure-overriding-map-is-bound'.
2157   (defun icicle-ensure-overriding-map-is-bound ()
2158     "Set `overriding-terminal-local-map' to `icicle-universal-argument-map'."
2159     (if (not (boundp 'overriding-map-is-bound)) ; Emacs 20, 21.
2160         (setq overriding-terminal-local-map  icicle-universal-argument-map)
2161       (unless overriding-map-is-bound   ; Emacs 22+.
2162         (setq saved-overriding-map           overriding-terminal-local-map
2163               overriding-terminal-local-map  icicle-universal-argument-map
2164               overriding-map-is-bound        t)))))
2165
2166 ;;;###autoload
2167 (defun icicle-digit-argument (arg)      ; Bound to `C-<0-9>', `M-<0-9>', `C-M-<0-9>' in minibuffer.
2168   "`digit-argument', but also echo the prefix."
2169   (interactive "P")
2170   (let* ((char   (if (integerp last-command-char)
2171                      last-command-char
2172                    (get last-command-char 'ascii-character)))
2173          (digit  (- (logand char ?\177) ?0)))
2174     (cond ((integerp arg)
2175            (setq prefix-arg  (+ (* arg 10) (if (< arg 0) (- digit) digit))))
2176           ((eq arg '-)
2177            ;; Treat -0 as just -, so that -01 will work.
2178            (setq prefix-arg  (if (zerop digit) '- (- digit))))
2179           (t
2180            (setq prefix-arg  digit))))
2181   (setq universal-argument-num-events  (length (this-command-keys)))
2182   (if (fboundp 'save&set-overriding-map) ; Emacs 24+
2183       (save&set-overriding-map icicle-universal-argument-map)
2184     (icicle-ensure-overriding-map-is-bound))
2185   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2186
2187 ;;;###autoload
2188 (defun icicle-negative-argument (arg)   ; Bound to `M--', `C-M--' in minibuffer.
2189   "`negative-argument', but also echo the prefix."
2190   (interactive "P")
2191   (cond ((integerp arg) (setq prefix-arg  (- arg)))
2192         ((eq arg '-) (setq prefix-arg  nil))
2193         (t (setq prefix-arg  '-)))
2194   (setq universal-argument-num-events  (length (this-command-keys)))
2195   (if (fboundp 'save&set-overriding-map) ; Emacs 24+
2196       (save&set-overriding-map icicle-universal-argument-map)
2197     (icicle-ensure-overriding-map-is-bound))
2198   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2199
2200 ;;;###autoload
2201 (defun icicle-universal-argument ()     ; Bound to `C-u' in minibuffer.
2202   "`universal-argument', but also echo the prefix."
2203   (interactive)
2204   (setq prefix-arg                     (list 4)
2205         universal-argument-num-events  (length (this-command-keys)))
2206   (if (fboundp 'save&set-overriding-map) ; Emacs 24+
2207       (save&set-overriding-map icicle-universal-argument-map)
2208     (icicle-ensure-overriding-map-is-bound))
2209   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2210
2211 ;;;###autoload
2212 (defun icicle-universal-argument-more (arg)
2213   "`universal-argument-more', but also echo the prefix."
2214   (interactive "P")
2215   (universal-argument-more arg)
2216   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2217
2218 ;;;###autoload
2219 (defun icicle-universal-argument-other-key (arg)
2220   "`universal-argument-other-key', but also echo the prefix."
2221   (interactive "P")
2222   (universal-argument-other-key arg)
2223   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2224
2225 ;;;###autoload
2226 (defun icicle-universal-argument-minus (arg)
2227   "`universal-argument-minus', but also echo the prefix."
2228   (interactive "P")
2229   (universal-argument-minus arg)
2230   (icicle-msg-maybe-in-minibuffer "prefix %S" prefix-arg))
2231
2232
2233 ;; REPLACE ORIGINAL `sit-for' in `subr.el',
2234 ;; saving it for restoration when you toggle `icicle-mode'.
2235 ;;
2236 ;; Ensure that `sit-for' after `C-u' in the minibuffer is immediately interrupted by user input.
2237 ;; This fix is not needed for Emacs < 23.
2238 ;;
2239 (unless (fboundp 'old-sit-for)
2240   (defalias 'old-sit-for (symbol-function 'sit-for)))
2241
2242 (when (> emacs-major-version 22)
2243   (defun icicle-sit-for (seconds &optional nodisp obsolete)
2244     "Perform redisplay, then wait for SECONDS seconds or until input is available.
2245 SECONDS may be a floating-point value.
2246 \(On operating systems that do not support waiting for fractions of a
2247 second, floating-point values are rounded down to the nearest integer.)
2248
2249 If optional arg NODISP is t, don't redisplay, just wait for input.
2250 Redisplay does not happen if input is available before it starts.
2251
2252 Value is t if waited the full time with no input arriving, and nil otherwise.
2253
2254 An obsolete, but still supported form is
2255 \(sit-for SECONDS &optional MILLISECONDS NODISP)
2256 where the optional arg MILLISECONDS specifies an additional wait period,
2257 in milliseconds; this was useful when Emacs was built without
2258 floating point support."
2259     (if (numberp nodisp)
2260         (setq seconds  (+ seconds (* 1e-3 nodisp))
2261               nodisp   obsolete)
2262       (if obsolete (setq nodisp  obsolete)))
2263     (cond (noninteractive
2264            (sleep-for seconds)
2265            t)
2266           ((input-pending-p)
2267            nil)
2268           ((<= seconds 0)
2269            (or nodisp (redisplay)))
2270           (t
2271            (or nodisp (redisplay))
2272            (let ((read (read-event nil nil seconds)))
2273              (or (null read)
2274                  (progn
2275                    ;; If last command was a prefix arg, e.g. C-u, push this event onto
2276                    ;; `unread-command-events' as (t . EVENT) so it will be added to
2277                    ;; `this-command-keys' by `read-key-sequence'.
2278                    (if (memq overriding-terminal-local-map
2279                              (list universal-argument-map icicle-universal-argument-map))
2280                        (setq read (cons t read)))
2281                    (push read unread-command-events)
2282                    nil)))))))
2283
2284 ;;;###autoload
2285 (defun icicle-retrieve-next-input (&optional arg) ; Bound to `C-S-l' (`C-L') in minibuffer.
2286   "Retrieve next minibuffer input.
2287 Like `icicle-retrieve-previous-input', but traverses history toward
2288 the present.
2289
2290 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2291 \\[icicle-retrieve-next-input]')."
2292   (interactive "P")
2293   (icicle-retrieve-previous-input arg 'interactive-p)) ; Must be `interactive-p'.
2294
2295 ;;;###autoload
2296 (defun icicle-retrieve-previous-input (&optional arg reversep allow-empty-p) ; `C-l' in minibuffer.
2297   "Retrieve previous minibuffer input.
2298 The possible inputs were not necessarily those entered with `RET'.
2299 With a negative prefix arg, this just empties the completion history.
2300 Otherwise:
2301  Use completion if `icicle-C-l-uses-completion-flag' is non-nil and no
2302    prefix arg is used, or if it is nil and a prefix arg is used, or if
2303    `icicle-retrieve-previous-input' is not used interactively.
2304  Otherwise, just cycle to the previous input.
2305
2306 Non-interactively:
2307  Non-nil argument REVERSEP means reverse the history order: return the
2308   next, not the previous, input.
2309  Non-nil ALLOW-EMPTY-P means the retrieved input can be \"\".
2310
2311 You can use this command only from buffer *Completions or from the
2312 minibuffer (`\\<minibuffer-local-completion-map>\
2313 \\[icicle-retrieve-previous-input]')."
2314   (interactive "P")
2315   (let ((interactive-p       (or (interactive-p) (eq reversep 'interactive-p)))
2316         (prev-inputs-var     (if (icicle-file-name-input-p)
2317                                  'icicle-previous-raw-file-name-inputs
2318                                'icicle-previous-raw-non-file-name-inputs))
2319         ;; `irpi-was-cycling-p' is used to remember, for the second `C-l' in a row, that the first
2320         ;; `C-l' came after cycling.  In that case, the second `C-l' restores the current raw input.
2321         (irpi-was-cycling-p  icicle-cycling-p))
2322     (when interactive-p (icicle-barf-if-outside-Completions-and-minibuffer))
2323     (cond ((wholenump (prefix-numeric-value arg))
2324            (let ((input  ""))
2325              (save-selected-window
2326                (select-window (minibuffer-window))
2327                (icicle-clear-minibuffer)
2328                (let ((prev-inputs
2329                       (if allow-empty-p
2330                           (symbol-value prev-inputs-var)
2331                         (icicle-remove-if (lambda (x) (string= "" x)) ; Exclude "".
2332                                           (symbol-value prev-inputs-var)))))
2333                  (setq input
2334                        (if (and interactive-p (or (and icicle-C-l-uses-completion-flag (not arg))
2335                                                   (and (not icicle-C-l-uses-completion-flag) arg)))
2336                            (let ((icicle-whole-candidate-as-text-prop-p   nil)
2337                                  (enable-recursive-minibuffers            t)
2338                                  (icicle-show-Completions-initially-flag  t))
2339                              (prog1 (completing-read
2340                                      "Retrieve input: " (mapcar #'list prev-inputs) nil t)
2341                                (setq icicle-last-input  nil)))
2342                          (if (or (not interactive-p)
2343                                  (not (memq last-command '(icicle-retrieve-next-input
2344                                                            icicle-retrieve-previous-input))))
2345                              ;; We use this one, to exclude common-match expansions from completion
2346                              ;; history, and to save the typed input only when you complete.
2347                              (let ((try  (if icicle-cycling-p
2348                                              icicle-last-input
2349                                            icicle-current-raw-input)))
2350                                (if (or allow-empty-p (not (equal "" try))) try (car prev-inputs)))
2351
2352                            ;; You can use this one instead, if you want to include common-match
2353                            ;; expansions and save the typed input even when you don't complete.
2354                            ;; (or icicle-last-input icicle-current-raw-input)
2355                            
2356                            (let ((next  (member icicle-current-raw-input prev-inputs)))
2357                              (unless next (setq next  prev-inputs))
2358                              (if reversep
2359                                  (or (let ((res     ())
2360                                            (inputs  prev-inputs))
2361                                        (while (and (consp inputs) (not (eq inputs next)))
2362                                          (push (pop inputs) res))
2363                                        (car res))
2364                                      (car (last prev-inputs)))
2365                                ;; If we were cycling before the first `C-l', then need to pick up the
2366                                ;; current raw input.  Otherwise, we need to pick up the previous one.
2367                                (prog1 (if irpi-was-cycling-p (car next) (cadr next))
2368                                  (setq irpi-was-cycling-p  nil))))))) ; So third `C-l' acts normally.
2369                  (when input
2370                    (setq icicle-current-raw-input  input)
2371                    (insert input)
2372                    (icicle-highlight-initial-whitespace input) ; (e.g. user typo).
2373                    (icicle-place-cursor input 'deactivate-mark))))
2374              (let ((icicle-edit-update-p  t))
2375                (funcall (or icicle-last-completion-command 'icicle-apropos-complete))
2376                ;; Restore raw input.  Cycling resets it to "", so `icicle-save-or-restore-input'
2377                ;; doesn't use out-of-date raw input (cycling does not necessarily follow completion
2378                ;; or completion of the same kind).
2379                (setq icicle-current-raw-input  input))
2380              (setq icicle-last-input  nil ; So `TAB' will expand it - `icicle-save-or-restore-input'.
2381                    icicle-cycling-p   irpi-was-cycling-p))) ; Let next `C-l' know the state.
2382           (t
2383            (set prev-inputs-var nil)
2384            (setq icicle-current-raw-input  "")
2385            (icicle-msg-maybe-in-minibuffer "Cleared completion history")))))
2386
2387 ;; $$ No longer bound.  Now we bind `icicle-retrieve-previous-input', instead, to `C-l'.
2388 ;;;###autoload
2389 (defun icicle-retrieve-last-input ()
2390   "Put the last real input into the minibuffer.
2391 Use this to replace a completion candidate inserted during cycling.
2392 If `icicle-expand-input-to-common-match-flag' is non-nil or this is
2393 prefix completion, then using this once restores the expanded common
2394 match string, and using it twice in succession restores your original
2395 input.
2396
2397 You can use this command only from buffer *Completions or from the
2398 minibuffer."
2399   (interactive)
2400   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
2401   (save-selected-window
2402     (select-window (minibuffer-window))
2403     (icicle-clear-minibuffer)
2404     (if (and (or icicle-expand-input-to-common-match-flag (eq icicle-current-completion-mode 'prefix))
2405              (eq last-command 'icicle-retrieve-last-input))
2406         (insert icicle-current-raw-input)
2407       (insert icicle-current-input))
2408     ;;$$$ (when (interactive-p) (setq icicle-last-completion-command  nil))
2409     (let ((input  (if (and (or icicle-expand-input-to-common-match-flag
2410                                (eq icicle-current-completion-mode 'prefix))
2411                            (eq last-command this-command))
2412                       icicle-current-raw-input
2413                     icicle-current-input)))
2414       (icicle-highlight-initial-whitespace input) ; Highlight initial whitespace (e.g. user typo).
2415       (icicle-place-cursor input 'deactivate-mark))))
2416
2417 ;; $$ No longer used.  It was originally used in `icicle-retrieve-last-input'.
2418 (defun icicle-insert-input (input)
2419   "Insert INPUT.  Prepend the directory if appropriate."
2420   (insert (if (and (icicle-file-name-input-p) insert-default-directory
2421                    (or (not (member input icicle-extra-candidates))
2422                        icicle-extra-candidates-dir-insert-p))
2423               (icicle-expand-file-or-dir-name input (icicle-file-name-directory input))
2424             input)))
2425
2426 ;;;###autoload
2427 (defun icicle-insert-history-element () ; Bound to `M-o' in minibuffer.
2428   "Use completion to insert a previously entered input in the minibuffer.
2429 Always available for any minibuffer input, not just during completion."
2430   (interactive)
2431   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2432   (when (and (boundp minibuffer-history-variable) (consp (symbol-value minibuffer-history-variable)))
2433     (let ((enable-recursive-minibuffers  t))
2434       (insert (icicle-completing-read-history "Choose input: " minibuffer-history-variable))))
2435   (when (and icicle-mode (memq icicle-default-value '(preselect-start preselect-end)))
2436     (icicle-select-minibuffer-contents)
2437     (setq deactivate-mark  nil)))
2438
2439 ;;;###autoload
2440 (defun icicle-insert-string-at-point (&optional arg) ; Bound to `M-.' in minibuffer.
2441   "Insert text at the cursor into the minibuffer.
2442 Each time this command is called, some text at or near the cursor is
2443 inserted into the minibuffer.  One of two things happens, depending on
2444 the value of option `icicle-default-thing-insertion' and whether or
2445 not you use `C-u'.
2446
2447 See the doc for option `icicle-thing-at-point-functions' for a
2448 complete description of its behavior.  What follows is an overview.
2449
2450 `icicle-thing-at-point-functions' is a cons of two parts - call them
2451 ALTERNATIVES and FORWARD-THING.
2452
2453 If ALTERNATIVES is not nil and one of the following is true:
2454  - FORWARD-THING is nil
2455  - the value of `icicle-default-thing-insertion' is `alternatives' and
2456    you have not used plain `C-u' in this series of `M-.'
2457  - the value of `icicle-default-thing-insertion' is `more-of-the-same'
2458    and you have used plain `C-u' in this series of `M-.'
2459 then the next function in ALTERNATIVES is used to retrieve the text to
2460 be inserted.
2461
2462 If FORWARD-THING is not nil and one of the following is true:
2463  - ALTERNATIVES is nil
2464  - the value of `icicle-default-thing-insertion' is `more-of-the-same'
2465    and you have not used `C-u' in this series of `M-.'
2466  - the value of `icicle-default-thing-insertion' is `alternatives' and
2467    you have used `C-u' in this series of `M-.'
2468 then function FORWARD-THING is used to retrieve the text to be
2469 inserted.
2470
2471 If you use a numeric prefix arg (not just plain `C-u'), the behavior
2472 is as follows.
2473
2474 * If a function in ALTERNATIVES is used (see above), then the text
2475   that is grabbed at or near point is read as a Lisp sexp and
2476   evaluated, and the value is inserted instead of the grabbed text.
2477
2478   Yes, this means you need to know when the particular ALTERNATIVES
2479   function that you want is coming up next, and use, say, `C-9' just
2480   before hitting `M-.' for that alternative.  So if, e.g., you want to
2481   evaluate the active region and insert the value, then you use
2482   `M-. C-9 M-.', since it is the second `M-.' that grabs the region.
2483
2484 * If the FORWARD-THING is being used, then the prefix arg determines
2485   the number of things to grab, and the direction of grabbing.: A
2486   negative argument grabs text to the left of the cursor; a positive
2487   argument grabs text to the right.
2488
2489 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2490 \\[icicle-insert-string-at-point]')."
2491   (interactive "P")
2492   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2493   (when (consp icicle-thing-at-point-functions) ; Option should always be a cons cell.
2494     (unless (eq last-command this-command) (setq icicle-default-thing-insertion-flipped-p  nil))
2495     (let ((alt-fns       (car icicle-thing-at-point-functions))
2496           (fwd-thing-fn  (cdr icicle-thing-at-point-functions))
2497           (flipped       (or icicle-default-thing-insertion-flipped-p ; Already flipped.
2498                              (setq icicle-default-thing-insertion-flipped-p  (consp arg)))))
2499       (cond
2500         ;; Use alternative text-grabbing functions successively.
2501         ((and alt-fns (or (if (eq 'alternatives icicle-default-thing-insertion)
2502                               (not flipped) ; Normal behavior for `alternatives'.
2503                             flipped)    ; Flipped behavior for `more-of-the-same'.
2504                           (not fwd-thing-fn))) ; No alternative.
2505          (setq icicle-successive-grab-count  1 ; In this mode, reset other mode's accumulator.
2506                icicle-thing-at-pt-fns-pointer
2507                (if (eq last-command this-command) ; If repeated, get next text-grabbing function.
2508                    (mod (1+ icicle-thing-at-pt-fns-pointer) (length alt-fns))
2509                  0))
2510          (let ((thing   "")
2511                (alt-fn  (nth icicle-thing-at-pt-fns-pointer alt-fns)))
2512            (save-excursion (with-current-buffer icicle-pre-minibuffer-buffer
2513                              (setq thing  (funcall alt-fn))))
2514            (setq thing  (or thing "nil"))
2515            (when (and arg (atom arg))   ; Numeric prefix arg.
2516              (setq thing  (condition-case err
2517                               (format "%s" (eval (car (read-from-string thing))))
2518                             (error thing))))
2519            (icicle-insert-thing thing)
2520            (icicle-msg-maybe-in-minibuffer (format "`%s'" alt-fn))))
2521
2522         ;; Use same text-grabbing function successively.
2523         ((and fwd-thing-fn (or (if (eq 'alternatives icicle-default-thing-insertion)
2524                                    flipped ; Flipped behavior for `alternatives'.
2525                                  (not flipped)) ; Normal behavior for `more-of-the-same'.
2526                                (not alt-fns))) ; No alternative.
2527          (if (and arg (atom arg))
2528
2529              ;; Explicit numeric arg.  If it doesn't change direction, then increment
2530              ;; existing count.  Otherwise, set count absolutely.
2531              (if (eq last-command this-command)
2532                  (if (= (icicle-signum icicle-successive-grab-count) ; Repeated `M-.'.
2533                         (icicle-signum (prefix-numeric-value arg)))
2534                      (setq icicle-successive-grab-count ; Same direction - increment count.
2535                            (* (icicle-signum icicle-successive-grab-count)
2536                               (+ (abs icicle-successive-grab-count)
2537                                  (abs (prefix-numeric-value arg)))))
2538                    (setq icicle-successive-grab-count  (prefix-numeric-value arg))) ; New dir - set.
2539                (setq icicle-successive-grab-count  (prefix-numeric-value arg))) ; First `M-.' - set.
2540
2541            ;; No explicit numeric arg.
2542            ;; If first `M-.' or plain `C-u', set count. Otherwise, increment count.
2543            (if (eq last-command this-command)
2544                (setq icicle-successive-grab-count ; Repeated `M-.'.
2545                      (if (consp arg)
2546                          ;; We're here from plain `C-u' with `alternatives' - use 1, not 4.
2547                          (if (wholenump icicle-successive-grab-count) 1 -1)
2548                        (if (wholenump icicle-successive-grab-count) ; Increment count.
2549                            (+ icicle-successive-grab-count (abs (prefix-numeric-value arg)))
2550                          (- icicle-successive-grab-count (abs (prefix-numeric-value arg))))))
2551              (setq icicle-successive-grab-count  1))) ; First `M-.' - reset count.
2552          (let ((things  ""))
2553            (save-excursion
2554              (with-current-buffer (cadr (buffer-list))
2555                (setq things  (buffer-substring-no-properties
2556                               (point)
2557                               (save-excursion (funcall fwd-thing-fn icicle-successive-grab-count)
2558                                               (point))))))
2559            (icicle-insert-thing things)))))))
2560
2561 (defun icicle-signum (num)
2562   "Return 1 if NUM is positive, -1 if negative, 0 if zero."
2563   (cond ((< num 0) -1) ((> num 0) 1) (t 0)))
2564
2565 (defun icicle-insert-thing (text &optional no-replace-p)
2566   "Insert TEXT in the minibuffer.
2567 TEXT replaces the last text that was inserted, if this command repeats
2568 the last and NO-REPLACE-P is nil."
2569   (when (and (stringp text) (not (string= "" text)))
2570     (remove-text-properties 0 (length text) '(face nil) text)
2571     (when (and (eq last-command this-command) (not no-replace-p)
2572                icicle-insert-string-at-pt-start) ; Ensure that we've defined the ends.
2573       (delete-region icicle-insert-string-at-pt-start icicle-insert-string-at-pt-end))
2574     (setq icicle-insert-string-at-pt-start  (point))
2575     (insert text)
2576     (setq icicle-insert-string-at-pt-end  (point))))
2577
2578 ;;;###autoload
2579 (defun icicle-insert-string-from-variable (askp) ; Bound to `C-=' in minibuffer.
2580   "Insert text into the minibuffer from a variable.
2581 By default, the variable is user option `icicle-input-string'.  To
2582 insert from a different variable, use a prefix argument.  You are then
2583 prompted for the variable to use.  Completion candidates for this
2584 include all string-valued variables.
2585
2586 You can use command `icicle-save-string-to-variable' to save a string
2587 to a variable.  Typically, you store a regexp or part of a regexp in
2588 the variable.  This command is bound in the minibuffer to `C-=', by
2589 default.  This is especially useful when used with command
2590 `icicle-search'.
2591
2592 Some regexps that you might want to assign to variables:
2593
2594  \"[A-Za-z0-9_.-]+@[A-Za-z0-9_.-]+\"          ; Email address
2595  \"\\\\([0-9]+\\\.[0-9]+\\\.[0-9]+\\\.[0-9]+\\\\)\"     ; IP address
2596  \"[0-9]\\\\\\\={4\\\\}-[0-9]\\\\\\\={2\\\\}-[0-9]\\\\\\\={2\\\\}\"   ; Date: 2006-04-14, Time:
2597  \"^[ \\\=\\t]*[0-9]?[0-9]\\\\([:.]?[0-9][0-9]\\\\)?\\\\(am\\\\|pm\\\\|AM\\\\|PM\\\\)?\"
2598  \"`\\\\(\\\\sw\\\\sw+\\\\)'\"                        ; Words inside `_'
2599  \"\\\\*.*\\\\*\"                                 ; Special buffer name: *_*
2600
2601 Standard Emacs Lisp libraries are full of regexps that you can assign
2602 to variables for use with `C-='.
2603  See `align.el' for regexps for programming languages.
2604  See `url-dav.el' for regexps matching iso8601 dates.
2605  See `rmail.el', `sendmail.el', and `mh-show.el' for regexps matching
2606  mail-header fields.
2607
2608 Imenu regexps occurring as parts of different values of
2609 `imenu-generic-expression' for different buffer types can be used as
2610 variable values for `C-='.  They all work fine with `icicle-search',
2611 turning it into a browser or navigator for the given mode.
2612
2613 See, for example, `generic-x.el' and `lisp-mode.el'.  Here is a regexp
2614 for Javascript function definitions from `generic-x.el':
2615
2616  \"^function\\\\s-+\\\\([A-Za-z0-9_]+\\\\)\"
2617
2618 And `lisp-imenu-generic-expression' (in `lisp-mode.el') provides
2619 regexps for Lisp function, variable, and type definitions.  Here is
2620 the variable-definition regexp:
2621
2622  \"^\\\\s-*(\\\\(def\\\\(c\\\\(onst\\\\(ant\\\\)?\\\\|ustom\\\\)\\\\|ine-symbol-macro\\\\|
2623  parameter\\\\|var\\\\)\\\\)\\\\s-+\\\\(\\\\(\\\\sw\\\\|\\\\s_\\\\)+\\\\)\"
2624
2625 Command `icicle-imenu' exploits this to automatically let you browse
2626 definitions.  It is a specialization of `icicle-search' for Imenu.
2627
2628 For more useful regexps, grep for `font-lock-keywords' in Emacs `lisp'
2629 directory and subdirs.
2630
2631 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2632 \\[icicle-insert-string-from-variable]')."
2633   (interactive "P")
2634   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
2635   (save-selected-window
2636     (select-window (minibuffer-window))
2637     (if askp
2638         (let* ((icicle-whole-candidate-as-text-prop-p   nil)
2639                ;; If we didn't use this here we'd at least have to bind it to
2640                ;; `orig-must-pass-after-match-predicate', because of `icicle-execute-extended-command'.
2641                (icicle-must-pass-after-match-predicate  #'(lambda (s)
2642                                                             (let ((sym  (intern-soft s)))
2643                                                               (and sym  (boundp (intern s))
2644                                                                    (condition-case nil
2645                                                                        (icicle-var-is-of-type-p
2646                                                                         sym '(string color regexp)
2647                                                                         'inherit-or-value)
2648                                                                      (error nil))))))
2649                (enable-recursive-minibuffers            t)
2650                (var
2651                 (intern (completing-read "Insert text from variable: " obarray  nil  nil nil
2652                                          (if (boundp 'variable-name-history)
2653                                              'variable-name-history
2654                                            'icicle-variable-name-history))))
2655                ;; Make sure we use the buffer-local value of the variable, if there is one.
2656                (text
2657                 (with-current-buffer (cadr (buffer-list)) (symbol-value var))))
2658           (icicle-insert-thing text 'no-replace))
2659       (icicle-insert-thing icicle-input-string 'no-replace))))
2660
2661 ;;;###autoload
2662 (defun icicle-insert-list-join-string () ; Bound to `C-M-j' in minibuffer during completion.
2663   "Insert `icicle-list-join-string' in the minibuffer.
2664 Then, if `1on1-fit-minibuffer-frame-flag' is defined and non-nil, fit
2665 a standalone minibuffer frame to the new minibuffer contents.
2666 You need library `fit-frame.el' for the frame-fitting part."
2667   (interactive)
2668   (icicle-insert-thing icicle-list-join-string 'no-replace)
2669   (let ((len  (length icicle-list-join-string)))
2670     (when (and (string= "\C-j" (substring icicle-list-join-string (1- len) len))
2671                (boundp '1on1-fit-minibuffer-frame-flag) ; In `oneonone.el'.
2672                1on1-fit-minibuffer-frame-flag
2673                (require 'fit-frame nil t))
2674       (1on1-fit-minibuffer-frame))))
2675
2676 ;;;###autoload
2677 (defun icicle-dispatch-M-q (&optional arg) ; Bound to `M-q' in minibuffer.
2678   "Do the right thing for `M-q'.
2679 If searching, call `icicle-toggle-search-whole-word'.
2680 Otherwise, call `icicle-insert-key-description'.
2681 Bound to `M-q' in the minibuffer."
2682   (interactive "P") ; Argument is ignored for `icicle-toggle-search-whole-word'.
2683   (cond (icicle-searching-p (icicle-toggle-search-whole-word))
2684         (t (icicle-insert-key-description arg))))
2685
2686 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
2687 ;;;###autoload
2688 (defalias 'toggle-icicle-search-whole-word 'icicle-toggle-search-whole-word)
2689 ;;;###autoload
2690 (defun icicle-toggle-search-whole-word () ; Bound to `M-q' in minibuffer during Icicles search.
2691   "Toggle the value of `icicle-search-whole-word-flag'.
2692 The new value takes effect for the next Icicles search command.
2693 Bound to `M-q' in the minibuffer when searching."
2694   (interactive)
2695   (setq icicle-search-whole-word-flag  (not icicle-search-whole-word-flag))
2696   (icicle-msg-maybe-in-minibuffer (if icicle-search-whole-word-flag
2697                                       "Whole-word searching is now ON, starting with next search"
2698                                     "Whole-word searching is now OFF, starting with next search")))
2699
2700 ;;;###autoload
2701 (defun icicle-insert-key-description (toggle-angle-brackets-p) ; Bound to `M-q' in minibuffer.
2702   "Read key and insert its description.
2703 For example, if the key read is ^F, then \"C-f\" is inserted.
2704
2705 `icicle-key-descriptions-use-<>-flag' determines whether angle
2706 brackets (`<', `>') are used for named keys, such as function
2707 keys, but a prefix argument reverses the meaning of
2708 `icicle-key-descriptions-use-<>-flag'.
2709
2710 Bound to `M-q' in the minibuffer during key completion."
2711   (interactive "P")
2712   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2713   (let* ((enable-recursive-minibuffers  t)
2714          (key
2715           (progn (minibuffer-message " [Quoting key]") (read-event))))
2716     (insert (single-key-description key (if toggle-angle-brackets-p
2717                                             icicle-key-descriptions-use-<>-flag
2718                                           (not icicle-key-descriptions-use-<>-flag))))))
2719
2720 ;;;###autoload
2721 (defun icicle-pp-eval-expression-in-minibuffer (insert-value) ; Bound to `M-:' in minibuffer.
2722   "Evaluate an Emacs-Lisp expression and pretty-print its value.
2723 This just calls `pp-eval-expression' from a recursive minibuffer."
2724   (interactive "P")
2725   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
2726   (let ((enable-recursive-minibuffers  t))
2727     (call-interactively 'icicle-pp-eval-expression))
2728   (select-window (minibuffer-window))
2729   (select-frame-set-input-focus (selected-frame)))
2730
2731 ;;;###autoload
2732 (defun icicle-insert-newline-in-minibuffer (arg) ; Bound to `C-j' in minibuffer.
2733   "Insert a newline character (`C-j'), in the minibuffer.
2734 Then, if `1on1-fit-minibuffer-frame-flag' is defined and non-nil, fit
2735 a standalone minibuffer frame to the new minibuffer contents.
2736 You need library `fit-frame.el' for the frame-fitting part."
2737   (interactive "p")
2738   (icicle-self-insert arg)
2739   (when (and (boundp '1on1-fit-minibuffer-frame-flag) ; In `oneonone.el'.
2740              1on1-fit-minibuffer-frame-flag
2741              (require 'fit-frame nil t))
2742     (1on1-fit-minibuffer-frame)))
2743
2744 ;; Bound in minibuffer to keys in `icicle-modal-cycle-down-keys' (`down', `wheel-down').
2745 ;;;###autoload
2746 (defun icicle-next-candidate-per-mode (&optional nth)
2747   "Replace input by NTH next completion candidate.
2748 Default value of NTH is 1, meaning use the next candidate.
2749 Negative NTH means use a previous, not subsequent, candidate.
2750
2751 Uses the next prefix or apropos completion command, depending on
2752 `icicle-current-completion-mode'.  If that is nil and
2753 `icicle-default-cycling-mode' is non-nil, uses the next history
2754 element instead.
2755
2756 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2757 \\[icicle-next-candidate-per-mode]')."
2758   (interactive)
2759   (unless nth (setq nth  1))
2760   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2761   (case icicle-current-completion-mode
2762     (prefix
2763      (setq this-command
2764            (if (wholenump nth) 'icicle-next-prefix-candidate 'icicle-previous-prefix-candidate))
2765      (icicle-next-prefix-candidate nth))
2766     (apropos
2767      (setq this-command
2768            (if (wholenump nth) 'icicle-next-apropos-candidate 'icicle-previous-apropos-candidate))
2769      (icicle-next-apropos-candidate nth))
2770     ((nil)
2771      (when icicle-default-cycling-mode (next-history-element (or nth 1))))))
2772
2773 ;; Bound in minibuffer to keys in `icicle-modal-cycle-up-keys' (`up', `wheel-up').
2774 ;;;###autoload
2775 (defun icicle-previous-candidate-per-mode (&optional nth)
2776   "Replace input by NTH previous completion candidate.
2777 Default value of NTH is 1, meaning use the previous candidate.
2778 Negative NTH means use a subsequent, not previous, candidate.
2779
2780 Uses the previous prefix or apropos completion command, depending on
2781 `icicle-current-completion-mode'. If that is nil and
2782 `icicle-default-cycling-mode' is non-nil, uses the previous history
2783 element instead.
2784
2785 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2786 \\[icicle-previous-candidate-per-mode]')."
2787   (interactive)
2788   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2789   (icicle-next-candidate-per-mode (- (or nth 1))))
2790
2791
2792 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-previous-keys' (`home').
2793 (put 'icicle-previous-prefix-candidate 'icicle-cycling-command         'backward)
2794 (put 'icicle-previous-prefix-candidate 'icicle-prefix-cycling-command  'backward)
2795 ;;;###autoload
2796 (defun icicle-previous-prefix-candidate (&optional nth)
2797   "Replace input by NTH previous prefix completion for an input.
2798 Default value of NTH is 1, meaning use the previous prefix completion.
2799 Negative NTH means use a subsequent, not previous, prefix completion.
2800
2801 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2802 \\[icicle-previous-prefix-candidate]')."
2803   (interactive)
2804   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2805   (setq nth  (or nth 1))
2806   (icicle-next-prefix-candidate (- nth)))
2807
2808
2809 ;; Bound in minibuffer to keys in `icicle-next-cycle-previous-keys' (`end').
2810 (put 'icicle-next-prefix-candidate 'icicle-cycling-command         'forward)
2811 (put 'icicle-next-prefix-candidate 'icicle-prefix-cycling-command  'forward)
2812 ;;;###autoload
2813 (defun icicle-next-prefix-candidate (&optional nth)
2814   "Replace input by NTH next prefix completion for an input.
2815 Default value of NTH is 1, meaning use the next prefix completion.
2816 Negative NTH means use a previous, not subsequent, prefix completion.
2817
2818 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2819 \\[icicle-next-prefix-candidate]')."
2820   (interactive)
2821   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2822   (setq icicle-current-completion-mode         'prefix
2823         icicle-next-apropos-complete-cycles-p  nil)
2824   (icicle-next-candidate nth (if (icicle-file-name-input-p)
2825                                  'icicle-file-name-prefix-candidates
2826                                'icicle-prefix-candidates)))
2827
2828
2829 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-previous-keys' (`prior').
2830 (put 'icicle-previous-apropos-candidate 'icicle-cycling-command         'backward)
2831 (put 'icicle-previous-apropos-candidate 'icicle-apropos-cycling-command 'backward)
2832 ;;;###autoload
2833 (defun icicle-previous-apropos-candidate (&optional nth)
2834   "Replace input by NTH previous apropos completion for an input.
2835 Default value of NTH is 1, meaning use the previous apropos completion.
2836 Negative NTH means use a subsequent, not previous, apropos completion.
2837
2838 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2839 \\[icicle-previous-apropos-candidate]')."
2840   (interactive)
2841   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2842   (setq nth  (or nth 1))
2843   (icicle-next-apropos-candidate (- nth)))
2844
2845
2846 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-next-keys' (`next').
2847 (put 'icicle-next-apropos-candidate 'icicle-cycling-command         'forward)
2848 (put 'icicle-next-apropos-candidate 'icicle-apropos-cycling-command 'forward)
2849 ;;;###autoload
2850 (defun icicle-next-apropos-candidate (&optional nth)
2851   "Replace input by NTH next apropos completion for an input.
2852 Default value of NTH is 1, meaning use the next apropos completion.
2853 Negative NTH means use a previous, not subsequent, apropos completion.
2854
2855 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2856 \\[icicle-next-apropos-candidate]')."
2857   (interactive)
2858   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2859   (setq icicle-current-completion-mode        'apropos
2860         icicle-next-prefix-complete-cycles-p  nil)
2861   (icicle-next-candidate nth (if (icicle-file-name-input-p)
2862                                  'icicle-file-name-apropos-candidates
2863                                'icicle-apropos-candidates)
2864                          'regexp-p))
2865
2866 ;; Bound  in minibuffer to keys in `icicle-modal-cycle-up-action-keys' (`C-up').
2867 ;;;###autoload
2868 (defun icicle-previous-candidate-per-mode-action (&optional nth)
2869   "`icicle-previous-candidate-per-mode' and `icicle-candidate-action'.
2870 Option `icicle-act-before-cycle-flag' determines which occurs first.
2871
2872 Optional argument NTH is as for `icicle-previous-candidate-per-mode'.
2873
2874 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2875 \\[icicle-previous-candidate-per-mode-action]')."
2876   (interactive)
2877   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2878   (icicle-successive-action #'icicle-previous-candidate-per-mode #'icicle-candidate-action nth))
2879
2880 ;; Bound  in minibuffer to keys in `icicle-modal-cycle-up-alt-action-keys' (`C-S-up').
2881 ;;;###autoload
2882 (defun icicle-previous-candidate-per-mode-alt-action (&optional nth)
2883   "`icicle-previous-candidate-per-mode' and `icicle-candidate-alt-action'.
2884 Option `icicle-act-before-cycle-flag' determines which occurs first.
2885
2886 Optional argument NTH is as for `icicle-previous-candidate-per-mode'.
2887
2888 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2889 \\[icicle-previous-candidate-per-mode-alt-action]')."
2890   (interactive)
2891   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2892   (icicle-successive-action #'icicle-previous-candidate-per-mode #'icicle-candidate-alt-action nth))
2893
2894 ;; Bound in minibuffer to keys in `icicle-modal-cycle-down-action-keys' (`C-down').
2895 ;;;###autoload
2896 (defun icicle-next-candidate-per-mode-action (&optional nth)
2897   "`icicle-next-candidate-per-mode' and `icicle-candidate-action'.
2898 Option `icicle-act-before-cycle-flag' determines which occurs first.
2899
2900 Optional argument NTH is as for `icicle-next-candidate-per-mode'.
2901
2902 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2903 \\[icicle-next-candidate-per-mode-action]')."
2904   (interactive)
2905   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2906   (icicle-successive-action #'icicle-next-candidate-per-mode #'icicle-candidate-action nth))
2907
2908 ;; Bound in minibuffer to keys in `icicle-modal-cycle-down-alt-action-keys' (`C-S-down').
2909 ;;;###autoload
2910 (defun icicle-next-candidate-per-mode-alt-action (&optional nth)
2911   "`icicle-next-candidate-per-mode' and `icicle-candidate-alt-action'.
2912 Option `icicle-act-before-cycle-flag' determines which occurs first.
2913
2914 Optional argument NTH is as for `icicle-next-candidate-per-mode'.
2915
2916 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2917 \\[icicle-next-candidate-per-mode-alt-action]')."
2918   (interactive)
2919   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2920   (icicle-successive-action #'icicle-next-candidate-per-mode #'icicle-candidate-alt-action nth))
2921
2922 ;; Bound in minibuffer to keys in `icicle-modal-cycle-up-help-keys' (`C-M-up').
2923 ;;;###autoload
2924 (defun icicle-previous-candidate-per-mode-help (&optional nth)
2925   "`icicle-previous-candidate-per-mode' and `icicle-help-on-candidate'.
2926 Option `icicle-act-before-cycle-flag' determines which occurs first.
2927
2928 Optional argument NTH is as for `icicle-previous-candidate-per-mode'.
2929
2930 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2931 \\[icicle-previous-candidate-per-mode-help]')."
2932   (interactive)
2933   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2934   (icicle-successive-action #'icicle-previous-candidate-per-mode #'icicle-help-on-candidate nth))
2935
2936 ;; Bound in minibuffer to keys in `icicle-modal-cycle-down-help-keys' (`C-M-down').
2937 ;;;###autoload
2938 (defun icicle-next-candidate-per-mode-help (&optional nth)
2939   "`icicle-next-candidate-per-mode' and `icicle-help-on-candidate'.
2940 Option `icicle-act-before-cycle-flag' determines which occurs first.
2941
2942 Optional argument NTH is as for `icicle-next-candidate-per-mode'.
2943
2944 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2945 \\[icicle-next-candidate-per-mode-help]')."
2946   (interactive)
2947   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2948   (icicle-successive-action #'icicle-next-candidate-per-mode #'icicle-help-on-candidate nth))
2949
2950
2951 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-previous-action-keys' (`C-home').
2952 (put 'icicle-previous-prefix-candidate-action 'icicle-cycling-command         'backward)
2953 (put 'icicle-previous-prefix-candidate-action 'icicle-prefix-cycling-command  'backward)
2954 ;;;###autoload
2955 (defun icicle-previous-prefix-candidate-action (&optional nth)
2956   "`icicle-previous-prefix-candidate' and `icicle-candidate-action'.
2957 Option `icicle-act-before-cycle-flag' determines which occurs first.
2958
2959 Optional argument NTH is as for `icicle-previous-prefix-candidate'.
2960
2961 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2962 \\[icicle-previous-prefix-candidate-action]')."
2963   (interactive)
2964   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2965   (icicle-successive-action #'icicle-previous-prefix-candidate #'icicle-candidate-action nth))
2966
2967
2968 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-next-action-keys' (`C-end').
2969 (put 'icicle-next-prefix-candidate-action 'icicle-cycling-command         'forward)
2970 (put 'icicle-next-prefix-candidate-action 'icicle-prefix-cycling-command  'forward)
2971 ;;;###autoload
2972 (defun icicle-next-prefix-candidate-action (&optional nth)
2973   "`icicle-next-prefix-candidate' and `icicle-candidate-action'.
2974 Option `icicle-act-before-cycle-flag' determines which occurs first.
2975
2976 Optional argument NTH is as for `icicle-next-prefix-candidate'.
2977
2978 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2979 \\[icicle-next-prefix-candidate-action]')."
2980   (interactive)
2981   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2982   (icicle-successive-action #'icicle-next-prefix-candidate #'icicle-candidate-action nth))
2983
2984
2985 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-previous-action-keys' (`C-prior').
2986 (put 'icicle-previous-apropos-candidate-action 'icicle-cycling-command         'backward)
2987 (put 'icicle-previous-apropos-candidate-action 'icicle-apropos-cycling-command 'backward)
2988 ;;;###autoload
2989 (defun icicle-previous-apropos-candidate-action (&optional nth)
2990   "`icicle-previous-apropos-candidate' and `icicle-candidate-action'.
2991 Option `icicle-act-before-cycle-flag' determines which occurs first.
2992
2993 Optional argument NTH is as for `icicle-previous-apropos-candidate'.
2994
2995 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
2996 \\[icicle-previous-apropos-candidate-action]')."
2997   (interactive)
2998   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
2999   (icicle-successive-action #'icicle-previous-apropos-candidate #'icicle-candidate-action nth))
3000
3001
3002 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-next-action-keys' (`C-next').
3003 (put 'icicle-next-apropos-candidate-action 'icicle-cycling-command         'forward)
3004 (put 'icicle-next-apropos-candidate-action 'icicle-apropos-cycling-command 'forward)
3005 ;;;###autoload
3006 (defun icicle-next-apropos-candidate-action (&optional nth)
3007   "`icicle-next-apropos-candidate' and `icicle-candidate-action'.
3008 Option `icicle-act-before-cycle-flag' determines which occurs first.
3009
3010 Optional argument NTH is as for `icicle-next-apropos-candidate'.
3011
3012 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3013 \\[icicle-next-apropos-candidate-action]')."
3014   (interactive)
3015   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3016   (icicle-successive-action #'icicle-next-apropos-candidate #'icicle-candidate-action nth))
3017
3018
3019 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-previous-alt-action-keys' (`C-S-home').
3020 (put 'icicle-previous-prefix-candidate-alt-action 'icicle-cycling-command         'backward)
3021 (put 'icicle-previous-prefix-candidate-alt-action 'icicle-prefix-cycling-command  'backward)
3022 ;;;###autoload
3023 (defun icicle-previous-prefix-candidate-alt-action (&optional nth)
3024   "`icicle-previous-prefix-candidate' and `icicle-candidate-alt-action'.
3025 Option `icicle-act-before-cycle-flag' determines which occurs first.
3026
3027 Optional argument NTH is as for `icicle-previous-prefix-candidate'.
3028
3029 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3030 \\[icicle-previous-prefix-candidate-alt-action]')."
3031   (interactive)
3032   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3033   (icicle-successive-action #'icicle-previous-prefix-candidate #'icicle-candidate-alt-action nth))
3034
3035
3036 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-next-alt-action-keys' (`C-S-end').
3037 (put 'icicle-next-prefix-candidate-alt-action 'icicle-cycling-command         'forward)
3038 (put 'icicle-next-prefix-candidate-alt-action 'icicle-prefix-cycling-command  'forward)
3039 ;;;###autoload
3040 (defun icicle-next-prefix-candidate-alt-action (&optional nth)
3041   "`icicle-next-prefix-candidate' and `icicle-candidate-alt-action'.
3042 Option `icicle-act-before-cycle-flag' determines which occurs first.
3043
3044 Optional argument NTH is as for `icicle-next-prefix-candidate'.
3045
3046 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3047 \\[icicle-next-prefix-candidate-alt-action]')."
3048   (interactive)
3049   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3050   (icicle-successive-action #'icicle-next-prefix-candidate #'icicle-candidate-alt-action nth))
3051
3052
3053 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-previous-alt-action-keys' (`C-S-prior').
3054 (put 'icicle-previous-apropos-candidate-alt-action 'icicle-cycling-command         'backward)
3055 (put 'icicle-previous-apropos-candidate-alt-action 'icicle-apropos-cycling-command 'backward)
3056 ;;;###autoload
3057 (defun icicle-previous-apropos-candidate-alt-action (&optional nth)
3058   "`icicle-previous-apropos-candidate' and `icicle-candidate-alt-action'.
3059 Option `icicle-act-before-cycle-flag' determines which occurs first.
3060
3061 Optional argument NTH is as for `icicle-previous-apropos-candidate'.
3062
3063 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3064 \\[icicle-previous-apropos-candidate-alt-action]')."
3065   (interactive)
3066   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3067   (icicle-successive-action #'icicle-previous-apropos-candidate #'icicle-candidate-alt-action nth))
3068
3069
3070 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-next-alt-action-keys' (`C-S-next').
3071 (put 'icicle-next-apropos-candidate-alt-action 'icicle-cycling-command         'forward)
3072 (put 'icicle-next-apropos-candidate-alt-action 'icicle-apropos-cycling-command 'forward)
3073 ;;;###autoload
3074 (defun icicle-next-apropos-candidate-alt-action (&optional nth)
3075   "`icicle-next-apropos-candidate' and `icicle-candidate-alt-action'.
3076 Option `icicle-act-before-cycle-flag' determines which occurs first.
3077
3078 Optional argument NTH is as for `icicle-next-apropos-candidate'.
3079
3080 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3081 \\[icicle-next-apropos-candidate-alt-action]')."
3082   (interactive)
3083   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3084   (icicle-successive-action #'icicle-next-apropos-candidate #'icicle-candidate-alt-action nth))
3085
3086
3087 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-previous-help-keys' (`C-M-home').
3088 (put 'icicle-help-on-previous-prefix-candidate 'icicle-cycling-command         'backward)
3089 (put 'icicle-help-on-previous-prefix-candidate 'icicle-prefix-cycling-command  'backward)
3090 ;;;###autoload
3091 (defun icicle-help-on-previous-prefix-candidate (&optional nth)
3092   "`icicle-previous-prefix-candidate' and `icicle-help-on-candidate'.
3093 Option `icicle-act-before-cycle-flag' determines which occurs first.
3094
3095 Optional argument NTH is as for `icicle-previous-prefix-candidate'.
3096
3097 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3098 \\[icicle-help-on-previous-prefix-candidate]')."
3099   (interactive)
3100   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3101   (icicle-successive-action #'icicle-previous-prefix-candidate #'icicle-help-on-candidate nth))
3102
3103
3104 ;; Bound in minibuffer to keys in `icicle-prefix-cycle-next-help-keys' (`C-M-end').
3105 (put 'icicle-help-on-next-prefix-candidate 'icicle-cycling-command         'forward)
3106 (put 'icicle-help-on-next-prefix-candidate 'icicle-prefix-cycling-command  'forward)
3107 ;;;###autoload
3108 (defun icicle-help-on-next-prefix-candidate (&optional nth)
3109   "`icicle-next-prefix-candidate' and `icicle-help-on-candidate'.
3110 Option `icicle-act-before-cycle-flag' determines which occurs first.
3111
3112 Optional argument NTH is as for `icicle-next-prefix-candidate'.
3113
3114 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3115 \\[icicle-help-on-next-prefix-candidate]')."
3116   (interactive)
3117   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3118   (icicle-successive-action #'icicle-next-prefix-candidate #'icicle-help-on-candidate nth))
3119
3120
3121 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-previous-help-keys' (`C-M-prior').
3122 (put 'icicle-help-on-previous-apropos-candidate 'icicle-cycling-command         'backward)
3123 (put 'icicle-help-on-previous-apropos-candidate 'icicle-apropos-cycling-command 'backward)
3124 ;;;###autoload
3125 (defun icicle-help-on-previous-apropos-candidate (&optional nth)
3126   "`icicle-previous-apropos-candidate' and `icicle-help-on-candidate'.
3127 Option `icicle-act-before-cycle-flag' determines which occurs first.
3128
3129 Optional argument NTH is as for `icicle-previous-apropos-candidate'.
3130
3131 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3132 \\[icicle-help-on-previous-apropos-candidate]')."
3133   (interactive)
3134   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3135   (icicle-successive-action #'icicle-previous-apropos-candidate #'icicle-help-on-candidate nth))
3136
3137
3138 ;; Bound in minibuffer to keys in `icicle-apropos-cycle-previous-help-keys' (`C-M-next').
3139 (put 'icicle-help-on-next-apropos-candidate 'icicle-cycling-command         'forward)
3140 (put 'icicle-help-on-next-apropos-candidate 'icicle-apropos-cycling-command 'forward)
3141 ;;;###autoload
3142 (defun icicle-help-on-next-apropos-candidate (&optional nth)
3143   "`icicle-next-apropos-candidate' and `icicle-help-on-candidate'.
3144 Option `icicle-act-before-cycle-flag' determines which occurs first.
3145
3146 Optional argument NTH is as for `icicle-next-apropos-candidate'.
3147
3148 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3149 \\[icicle-help-on-next-apropos-candidate]')."
3150   (interactive)
3151   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3152   (icicle-successive-action #'icicle-next-apropos-candidate #'icicle-help-on-candidate nth))
3153
3154 (defun icicle-successive-action (nav-fn action-fn nth)
3155   "Call NAV-FN and ACTION-FN.  Pass argument NTH to NAV-FN.
3156 Set `icicle-current-completion-mode'.
3157 The order between NAV-FN and ACTION-FN respects the value of
3158 `icicle-act-before-cycle-flag'."
3159   ;; Set mode only if known.  Otherwise, leave it alone (e.g. for per-mode functions).
3160   (cond ((get nav-fn 'icicle-apropos-cycling-command)
3161          (setq icicle-current-completion-mode        'apropos
3162                icicle-next-prefix-complete-cycles-p  nil))
3163         ((get nav-fn 'icicle-prefix-cycling-command)
3164          (setq icicle-current-completion-mode         'prefix
3165                icicle-next-apropos-complete-cycles-p  nil)))
3166
3167   ;; We bind `icicle-acting-on-next/prev' to non-nil (and the direction) while calling the action
3168   ;; function.  This is used by Icicles search-and-replace (`icicle-search-highlight-and-maybe-replace')
3169   ;; to ensure the correct candidate number for a series of replacements.
3170   ;; (Not currently used for the `icicle-act-before-cycle-flag' case, but we do it there also, anyway.)
3171   (cond (icicle-act-before-cycle-flag
3172          (let ((icicle-acting-on-next/prev  (get nav-fn 'icicle-cycling-command)))
3173            (save-excursion (save-selected-window (funcall action-fn))))
3174          (funcall nav-fn nth))
3175         (t
3176          ;; Inhibit showing help in mode-line while moving to next/previous candidate
3177          ;; in `*Completions*', because help sits for `icicle-help-in-mode-line-delay' sec.
3178          ;; Display the help after we do the action.
3179          (let ((icicle-help-in-mode-line-delay  0)) (funcall nav-fn nth))
3180          (let ((icicle-acting-on-next/prev  (get nav-fn 'icicle-cycling-command)))
3181            (save-excursion (save-selected-window (funcall action-fn))))
3182          (when (stringp icicle-last-completion-candidate)
3183            (icicle-show-help-in-mode-line icicle-last-completion-candidate)))))
3184
3185
3186 ;; Bound in minibuffer to keys in `icicle-prefix-complete-keys' (`TAB').
3187 (put 'icicle-prefix-complete 'icicle-cycling-command t)
3188 (put 'icicle-prefix-complete 'icicle-prefix-cycling-command t)
3189 (put 'icicle-prefix-complete 'icicle-completing-command t)
3190 (put 'icicle-prefix-complete 'icicle-prefix-completing-command t)
3191 ;;;###autoload
3192 (defun icicle-prefix-complete ()
3193   "Complete the minibuffer contents as far as possible, as a prefix.
3194 Repeat this to cycle among candidate completions.
3195 If no characters can be completed, display the possible completions.
3196 Candidate completions are appropriate names whose prefix is the
3197 minibuffer input, where appropriateness is determined by the context
3198 \(command, variable, and so on).
3199 Return nil if there is no valid completion.
3200 Otherwise, return the list of completion candidates.
3201
3202 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3203 \\[icicle-prefix-complete]')."
3204   (interactive)
3205   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3206   (unless (string= icicle-dot-string-internal ".")
3207     (icicle-convert-dots t t)
3208     (setq icicle-dot-string-internal  "."))
3209   (icicle-prefix-complete-1))
3210
3211
3212 ;; Bound in minibuffer to keys in `icicle-prefix-complete-no-display-keys' (`C-M-TAB').
3213 (put 'icicle-prefix-complete-no-display 'icicle-cycling-command t)
3214 (put 'icicle-prefix-complete-no-display 'icicle-prefix-cycling-command t)
3215 (put 'icicle-prefix-complete-no-display 'icicle-completing-command t)
3216 (put 'icicle-prefix-complete-no-display 'icicle-prefix-completing-command t)
3217 ;;;###autoload
3218 (defun icicle-prefix-complete-no-display (&optional no-msg-p) ; Bound to `C-M-TAB' in minibuffer.
3219   "Like `icicle-prefix-complete', but without displaying `*Completions*'.
3220 Optional arg NO-MSG-P non-nil means do not show a minibuffer message
3221 indicating that candidates were updated.
3222
3223 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3224 \\[icicle-prefix-complete-no-display]')."
3225   (interactive)
3226   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3227   (icicle-prefix-complete-1 (if no-msg-p 'no-msg 'no-display)))
3228
3229
3230 ;; Bound in minibuffer to keys in `icicle-word-completion-keys' (`M-SPC').
3231 (put 'icicle-prefix-word-complete 'icicle-cycling-command t)
3232 (put 'icicle-prefix-word-complete 'icicle-prefix-cycling-command t)
3233 (put 'icicle-prefix-word-complete 'icicle-completing-command t)
3234 (put 'icicle-prefix-word-complete 'icicle-prefix-completing-command t)
3235 ;;;###autoload
3236 (defun icicle-prefix-word-complete ()
3237   "Complete the minibuffer contents at most a single word.
3238 Repeating this completes additional words.
3239 Spaces and hyphens in candidates are considered word separators.
3240 If only a single candidate matches, the input is completed entirely.
3241 Return nil if there is no valid completion, else t.
3242
3243 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3244 \\[icicle-prefix-word-complete]')."
3245   (interactive)
3246   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3247   (icicle-prefix-complete-1 nil t))
3248
3249 (defun icicle-prefix-complete-1 (&optional no-display-p word-p)
3250   "Helper function for `icicle-prefix-complete(-no-display)'.
3251 Return the list of completion candidates.
3252 Optional argument NO-DISPLAY-P non-nil means do not display buffer
3253  `*Completions*'.  If the value is `no-msg', then do not show any
3254   message either.  NO-DISPLAY-P is passed to
3255  `icicle-display-candidates-in-Completions' as its second arg.
3256 Optional argument WORD-P non-nil means complete only a word at a time."
3257   (let ((ipc1-was-cycling-p  icicle-cycling-p)
3258         (mode-line-help      nil))
3259     (setq icicle-current-input                   (if (and icicle-last-input
3260                                                           icicle-cycling-p
3261                                                           (not icicle-edit-update-p)
3262                                                           (eq icicle-current-completion-mode 'prefix)
3263                                                           (or (not word-p)
3264                                                               (eq this-command last-command))
3265                                                           (symbolp last-command)
3266                                                           (or (get last-command 'icicle-cycling-command)
3267                                                               (get last-command 'icicle-action-command))
3268                                                           icicle-completion-candidates)
3269                                                      icicle-last-input
3270                                                    (if (icicle-file-name-input-p)
3271                                                        (abbreviate-file-name
3272                                                         (icicle-input-from-minibuffer 'leave-envar))
3273                                                      (icicle-input-from-minibuffer)))
3274           icicle-current-completion-mode         'prefix
3275           icicle-next-apropos-complete-cycles-p  nil
3276           icicle-input-fail-pos                  nil
3277           icicle-cycling-p                       nil)
3278     (when icicle-edit-update-p (setq icicle-next-prefix-complete-cycles-p  nil))
3279     (let ((word-complete-input      "")
3280           (input-before-completion  icicle-current-input)
3281           return-value)
3282       (unless (and (stringp icicle-current-input) (stringp icicle-last-input)
3283                    (string= icicle-current-input icicle-last-input)
3284                    (or (get last-command 'icicle-prefix-completing-command)
3285                        (get last-command 'icicle-action-command))
3286                    (not word-p))
3287         (unless (or icicle-edit-update-p (get-buffer-window "*Completions*" 0) no-display-p)
3288           (message "Computing completion candidates..."))
3289         (if (not word-p)
3290             (setq icicle-completion-candidates
3291                   (icicle-condition-case-no-debug nil
3292                       (if (icicle-file-name-input-p)
3293                           (icicle-file-name-prefix-candidates icicle-current-input)
3294                         (icicle-prefix-candidates icicle-current-input))
3295                     (error icicle-completion-candidates))) ; No change if completion error.
3296           ;; Complete a word.  Save input before trying to complete.
3297           ;; Update `icicle-current-input': `minibuffer-complete-word' might have completed the input
3298           ;; beyond a complete candidate - e.g. `forwar-char' to `forward-char-'.
3299           (setq word-complete-input   (icicle-input-from-minibuffer)
3300                 return-value
3301                 (let ((temp-buffer-show-hook       nil) ; Don't let it fit frame here.
3302                       (completion-auto-help        nil) ; Don't show `*Completions*'.
3303                       (minibuffer-message-timeout  0)) ; No timeout.
3304                   (icicle-clear-minibuffer)
3305                   (insert icicle-current-input)
3306                   (save-selected-window (minibuffer-complete-word)))
3307                 icicle-current-input  (icicle-input-from-minibuffer)) ; Update input.
3308           ;; If incremental compl., or completed some, or not repeated, then update input and recompute.
3309           (when (or icicle-edit-update-p
3310                     (> (length icicle-current-input) (length word-complete-input))
3311                     (not (eq this-command last-command)))
3312             (setq word-complete-input           icicle-current-input
3313                   icicle-completion-candidates  (icicle-condition-case-no-debug nil
3314                                                     (if (icicle-file-name-input-p)
3315                                                         (icicle-file-name-prefix-candidates
3316                                                          icicle-current-input)
3317                                                       (icicle-prefix-candidates icicle-current-input))
3318                                                   (error icicle-completion-candidates)))))
3319         (message nil))                  ; Clear out "Computing completion candidates..." message.
3320       (unless word-p (setq return-value  icicle-completion-candidates)) ; Word returns special value.
3321       (icicle-save-or-restore-input)
3322       (cond ((null icicle-completion-candidates)
3323              (setq icicle-nb-of-other-cycle-candidates  0)
3324              (let ((icicle-incremental-completion-flag ; Upgrade if OK for explicit.
3325                     (or (memq icicle-highlight-input-completion-failure
3326                               '(explicit-strict explicit explicit-remote))
3327                         icicle-incremental-completion-flag)))
3328                (icicle-highlight-input-noncompletion))
3329              (save-selected-window (icicle-remove-Completions-window))
3330              (run-hooks 'icicle-no-match-hook)
3331              (unless (eq no-display-p 'no-msg)
3332                (minibuffer-message (case (icicle-current-TAB-method)
3333                                      (fuzzy        "  [No fuzzy completions]")
3334                                      (vanilla      "  [No vanilla completions]")
3335                                      (swank        "  [No swank (fuzzy symbol) completions]")
3336                                      (t            "  [No prefix completions]")))))
3337             ((null (cdr icicle-completion-candidates)) ; Single candidate.  Update minibuffer.
3338              ;; When `icicle-whole-candidate-as-text-prop-p' is t and
3339              ;; `icicle-expand-input-to-common-match-flag' is nil, we need to expand the input anyway.
3340              ;; That transfers any `icicle-whole-candidate' property from the candidate to
3341              ;; `icicle-current-input', so things that use `icicle-candidates-alist' will work.
3342              (when (and icicle-whole-candidate-as-text-prop-p
3343                         (not icicle-expand-input-to-common-match-flag))
3344                (setq icicle-common-match-string  (icicle-expanded-common-match
3345                                                   icicle-current-input icicle-completion-candidates))
3346                (when icicle-common-match-string
3347                  (let ((common  (if (and (icicle-file-name-input-p) insert-default-directory)
3348                                     (if (string= "" icicle-common-match-string)
3349                                         (or (icicle-file-name-directory icicle-current-input) "")
3350                                       (directory-file-name (icicle-abbreviate-or-expand-file-name
3351                                                             icicle-common-match-string
3352                                                             (icicle-file-name-directory
3353                                                              icicle-current-input))))
3354                                   icicle-common-match-string)))
3355                    ;; Save as current input, unless input is a directory.
3356                    (unless (and (icicle-file-name-input-p)  (file-directory-p icicle-current-input))
3357                      (setq icicle-current-input  common)))))
3358              ;; Expand file-name input to the common match for the current candidate.
3359              (when (icicle-file-name-input-p)
3360                (setq icicle-common-match-string  (icicle-expanded-common-match
3361                                                   (car icicle-completion-candidates)
3362                                                   icicle-completion-candidates))
3363                (when icicle-common-match-string
3364                  (let ((common  (if (and (icicle-file-name-input-p) insert-default-directory)
3365                                     (if (string= "" icicle-common-match-string)
3366                                         (or (icicle-file-name-directory icicle-current-input) "")
3367                                       (directory-file-name (icicle-abbreviate-or-expand-file-name
3368                                                             icicle-common-match-string
3369                                                             (icicle-file-name-directory
3370                                                              icicle-current-input))))
3371                                   icicle-common-match-string)))
3372                    (setq icicle-current-input  common))))
3373              (setq icicle-nb-of-other-cycle-candidates  0)
3374              (unless icicle-edit-update-p
3375                (icicle-clear-minibuffer)
3376                (let ((cand  (car icicle-completion-candidates)))
3377                  (if (icicle-file-name-input-p)
3378                      (cond ((string= "" cand) ; This indicates an empty dir.
3379                             (setq icicle-last-completion-candidate  icicle-current-input))
3380                            ((eq ?\/  (aref cand (1- (length cand)))) ; Add `/', so cycling expands dir.
3381                             (setq icicle-current-input (concat icicle-current-input "/")
3382                                   icicle-last-completion-candidate  icicle-current-input))
3383                            (t           ; Non-dir - use the candidate file, but without any dir.
3384                             (setq icicle-last-completion-candidate
3385                                   (icicle-file-name-nondirectory cand))))
3386                    (setq icicle-last-completion-candidate  cand)))
3387                (let ((inserted  (if (and (icicle-file-name-input-p) insert-default-directory
3388                                          (or (not (member icicle-last-completion-candidate
3389                                                           icicle-extra-candidates))
3390                                              icicle-extra-candidates-dir-insert-p))
3391                                     (icicle-abbreviate-or-expand-file-name
3392                                      icicle-last-completion-candidate
3393                                      (icicle-file-name-directory-w-default icicle-current-input))
3394                                   icicle-last-completion-candidate)))
3395                  (insert inserted)
3396                  (when (and (icicle-file-name-input-p)
3397                             (icicle-file-directory-p (icicle-abbreviate-or-expand-file-name inserted)))
3398                    (setq icicle-default-directory  (icicle-abbreviate-or-expand-file-name inserted)))))
3399              (save-selected-window (icicle-remove-Completions-window))
3400              (icicle-transform-sole-candidate)
3401              (unless (boundp 'icicle-prefix-complete-and-exit-p)
3402                (icicle-highlight-complete-input)
3403                (cond ((and icicle-top-level-when-sole-completion-flag
3404                            (sit-for icicle-top-level-when-sole-completion-delay))
3405                       (set minibuffer-history-variable
3406                            (cons icicle-current-input
3407                                  (symbol-value minibuffer-history-variable)))
3408                       (icicle-condition-case-no-debug icicle-prefix-complete-1
3409                           (throw 'icicle-read-top
3410                             (if (and (icicle-file-name-input-p) insert-default-directory
3411                                      (or (not (member icicle-current-input
3412                                                       icicle-extra-candidates))
3413                                          icicle-extra-candidates-dir-insert-p))
3414                                 (expand-file-name icicle-current-input)
3415                               icicle-current-input))
3416                         (no-catch
3417                          (icicle-retrieve-last-input)
3418                          icicle-current-input)
3419                         (error (message "%s" (error-message-string icicle-prefix-complete-1)))))
3420                      ((and icicle-edit-update-p (not (eq no-display-p 'no-msg)))
3421                       (minibuffer-message
3422                        (format (case (icicle-current-TAB-method)
3423                                  (fuzzy        "  [One fuzzy completion: %s]")
3424                                  (vanilla      "  [One vanilla completion: %s]")
3425                                  (swank        "  [One swank (fuzzy symbol) completion: %s]")
3426                                  (t            "  [One prefix completion: %s]"))
3427                                icicle-current-input))
3428                       (setq mode-line-help  icicle-current-input))
3429                      ((not (eq no-display-p 'no-msg))
3430                       (minibuffer-message (case (icicle-current-TAB-method)
3431                                             (fuzzy        "  [Sole fuzzy completion]")
3432                                             (vanilla      "  [Sole vanilla completion]")
3433                                             (swank        "  [Sole swank (fuzzy symbol) completion]")
3434                                             (t            "  [Sole prefix completion]")))
3435                       (setq mode-line-help  icicle-current-input)))))
3436             (t                          ; Multiple candidates.
3437              (if icicle-edit-update-p
3438                  (icicle-display-candidates-in-Completions nil no-display-p)
3439                (unless word-p
3440                  (icicle-clear-minibuffer)
3441                  (save-window-excursion
3442                    ;; Shouldn't need to explicitly select minibuffer like this, since `*Completions*'
3443                    ;; input is directed there.  But there seems to be an Emacs bug somewhere, because
3444                    ;; although using just `insert' inserts the input in the minibuffer OK, in some
3445                    ;; cases the cursor might not follow the insertion.
3446                    (select-window (active-minibuffer-window))
3447                    (insert icicle-current-input))
3448                  ;; Shouldn't need to do this if it is on `post-command-hook', but it seems we need to.
3449                  (when (and (boundp '1on1-fit-minibuffer-frame-flag) 1on1-fit-minibuffer-frame-flag
3450                             (require 'fit-frame nil t))
3451                    (1on1-fit-minibuffer-frame))) ; In `oneonone.el'.
3452                (deactivate-mark)
3453                (icicle-highlight-initial-whitespace icicle-current-input)
3454                (when (and (icicle-file-name-input-p)
3455                           (icicle-file-directory-p icicle-last-completion-candidate))
3456                  (setq icicle-default-directory  (icicle-abbreviate-or-expand-file-name
3457                                                   icicle-last-completion-candidate)))
3458                (when (and (icicle-input-is-a-completion-p icicle-current-input)
3459                           (not (boundp 'icicle-prefix-complete-and-exit-p)))
3460                  (icicle-highlight-complete-input)
3461                  (setq mode-line-help  (icicle-minibuf-input-sans-dir icicle-current-input)))
3462                (cond (;; Candidates visible.  If second prefix complete, cycle, else update candidates.
3463                       (get-buffer-window "*Completions*" 0)
3464                       (if (and (or ipc1-was-cycling-p icicle-next-prefix-complete-cycles-p)
3465                                (get icicle-last-completion-command 'icicle-prefix-completing-command)
3466                                (if word-p
3467                                    ;; Word completion cycles only if both of these are true:
3468                                    ;; * Input is not yet complete (null `return-value').
3469                                    ;; * Either last command was an edit and input does not end in `-',
3470                                    ;;          or the current input is from cycling.
3471                                    ;; E.g. `M-x fo M-SPC r M-SPC' cycles among foreground-color etc.
3472                                    (and (not return-value)
3473                                         (or (and (not (or (get last-command
3474                                                                'icicle-prefix-completing-command)
3475                                                           (get last-command 'icicle-action-command)))
3476                                                  (not (eq (aref icicle-current-input
3477                                                                 (1- (length icicle-current-input)))
3478                                                           ?-)))
3479                                             (not (string= icicle-last-input word-complete-input))))
3480                                  (or (get last-command 'icicle-prefix-completing-command)
3481                                      (get last-command 'icicle-action-command))))
3482                           ;; Second prefix complete in a row.  Cycle down.
3483                           (icicle-next-candidate 1 (if (icicle-file-name-input-p)
3484                                                        'icicle-file-name-prefix-candidates
3485                                                      'icicle-prefix-candidates))
3486                         ;; User did something else (e.g. changed input).  Update the candidates.
3487                         (icicle-display-candidates-in-Completions nil no-display-p)))
3488                      (;; No candidates shown.  Could be first completion or could follow `C-M-(S-)TAB'.
3489                       icicle-TAB-shows-candidates-flag
3490                       (if (not (and (or ipc1-was-cycling-p icicle-next-prefix-complete-cycles-p)
3491                                     (get icicle-last-completion-command
3492                                          'icicle-prefix-completing-command)
3493                                     (or (get last-command 'icicle-prefix-completing-command)
3494                                         (get last-command 'icicle-action-command))
3495                                     (not word-p)))
3496                           ;; First prefix complete is enough to update candidates.
3497                           (icicle-display-candidates-in-Completions nil no-display-p)
3498                         ;; Second prefix complete.  If `TAB', then it follows `C-M-TAB', so show window.
3499                         (unless no-display-p (icicle-display-candidates-in-Completions nil))
3500                         (icicle-next-candidate 1 (if (icicle-file-name-input-p)
3501                                                      'icicle-file-name-prefix-candidates
3502                                                    'icicle-prefix-candidates))))
3503                      (;; No candidates shown.  Second prefix complete.
3504                       ;; If NO-DISPLAY-P and either not WORD-P or input is complete, then cycle down.
3505                       ;; Else, vanilla Emacs: second `TAB' shows candidates.
3506                       (and (get icicle-last-completion-command 'icicle-prefix-completing-command)
3507                            (or (get last-command 'icicle-prefix-completing-command)
3508                                (get last-command 'icicle-action-command))
3509                            completion-auto-help)
3510                       (if (or (not no-display-p) (and word-p (not return-value)))
3511                           (icicle-display-candidates-in-Completions nil)
3512                         (icicle-next-candidate 1 (if (icicle-file-name-input-p)
3513                                                      'icicle-file-name-prefix-candidates
3514                                                    'icicle-prefix-candidates))))
3515                      ;; Input is complete, but exist other candidates with same prefix.
3516                      ((and (member icicle-current-input icicle-completion-candidates)
3517                            (not (eq no-display-p 'no-msg)))
3518                       (minibuffer-message "  [Complete, but not unique]"))))))
3519       (setq icicle-last-completion-command        (if word-p
3520                                                       'icicle-prefix-word-complete
3521                                                     (if no-display-p
3522                                                         'icicle-prefix-complete-no-display
3523                                                       'icicle-prefix-complete))
3524             icicle-next-prefix-complete-cycles-p  (equal input-before-completion
3525                                                          (icicle-input-from-minibuffer 'leave-envvars)))
3526       (when mode-line-help (icicle-show-help-in-mode-line mode-line-help))
3527       return-value)))
3528
3529 (defun icicle-input-is-a-completion-p (&optional input)
3530   "Return non-nil if the input is a valid completion.
3531 Optional arg INPUT is passed to `icicle-minibuffer-input-sans-dir'.
3532 This is essentially a `member' test, except for environment vars, for
3533 which the initial `$' is ignored."
3534   (let* ((input-sans-dir  (icicle-minibuf-input-sans-dir input))
3535          (env-var-name    (and (icicle-not-basic-prefix-completion-p)
3536                                (> (length input-sans-dir) 0)
3537                                (eq ?\$ (aref input-sans-dir 0))
3538                                (substring input-sans-dir 1))))
3539     (member (icicle-upcase-if-ignore-case (or env-var-name input-sans-dir))
3540             (mapcar #'icicle-upcase-if-ignore-case icicle-completion-candidates))))
3541
3542
3543 ;; Bound in minibuffer to keys in `icicle-apropos-complete-keys' (`S-TAB').
3544 (put 'icicle-apropos-complete 'icicle-cycling-command t)
3545 (put 'icicle-apropos-complete 'icicle-apropos-cycling-command t)
3546 (put 'icicle-apropos-complete 'icicle-completing-command t)
3547 (put 'icicle-apropos-complete 'icicle-apropos-completing-command t)
3548 ;;;###autoload
3549 (defun icicle-apropos-complete ()
3550   "Complete the minibuffer contents as far as possible.
3551 Repeat this to cycle among candidate completions.
3552 This uses \"apropos completion\", defined as follows:
3553 A completion contains the minibuffer input somewhere, as a substring.
3554 Display a list of possible completions in buffer `*Completions*'.
3555 Candidate completions are appropriate names that match the current
3556 input, taken as a regular expression, where appropriateness is
3557 determined by the context (command, variable, and so on).
3558 Return nil if there is no valid completion.
3559 Otherwise, return the list of completion candidates.
3560
3561 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3562 \\[icicle-apropos-complete]')."
3563   (interactive)
3564   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3565   (when (and (string= icicle-dot-string icicle-anychar-regexp)
3566              (not (string= icicle-dot-string-internal icicle-anychar-regexp)))
3567     (icicle-convert-dots (equal icicle-current-input icicle-last-input)) ; No confirm if same input.
3568     (setq icicle-dot-string-internal  (icicle-anychar-regexp)))
3569   (let* ((error-msg  nil)               ; Apropos complete.
3570          (candidates
3571           (icicle-condition-case-no-debug lossage
3572               (icicle-apropos-complete-1)
3573             (invalid-regexp
3574              (setq error-msg  (cadr lossage))
3575              (when (string-match "\\`Premature \\|\\`Unmatched \\|\\`Invalid " error-msg)
3576                (setq error-msg  "incomplete input")))
3577             (error (setq error-msg  (error-message-string lossage))))))
3578     (when error-msg (minibuffer-message (concat "  " error-msg)))
3579     candidates))
3580
3581
3582 ;; Bound in minibuffer to keys in `icicle-apropos-complete-no-display-keys' (`C-M-S-TAB').
3583 (put 'icicle-apropos-complete-no-display 'icicle-cycling-command t)
3584 (put 'icicle-apropos-complete-no-display 'icicle-apropos-cycling-command t)
3585 (put 'icicle-apropos-complete-no-display 'icicle-completing-command t)
3586 (put 'icicle-apropos-complete-no-display 'icicle-apropos-completing-command t)
3587 ;;;###autoload
3588 (defun icicle-apropos-complete-no-display (&optional no-msg-p)
3589   "Like `icicle-apropos-complete', but without displaying `*Completions*'.
3590 Optional arg NO-MSG-P non-nil means do not show a minibuffer message
3591 indicating that candidates were updated.
3592 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3593 \\[icicle-apropos-complete-no-display]')."
3594   (interactive)
3595   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3596   (let* ((error-msg  nil)
3597          (candidates
3598           (icicle-condition-case-no-debug lossage
3599               (icicle-apropos-complete-1 (if no-msg-p 'no-msg 'no-display))
3600             (invalid-regexp
3601              (setq error-msg  (cadr lossage))
3602              (when (string-match "\\`Premature \\|\\`Unmatched \\|\\`Invalid " error-msg)
3603                (setq error-msg  "incomplete input")))
3604             (error (setq error-msg  (error-message-string lossage))))))
3605     (when error-msg (minibuffer-message (concat "  " error-msg)))
3606     candidates))
3607
3608 (defun icicle-apropos-complete-1 (&optional no-display-p)
3609   "Helper function for `icicle-apropos-complete(-no-display)'.
3610 This does everything except deal with regexp-match errors.
3611 Return the list of completion candidates.
3612
3613 Optional argument NO-DISPLAY-P non-nil means do not display buffer
3614 `*Completions*'.  If the value is `no-msg', then do not show any
3615 message either.  NO-DISPLAY-P is passed to
3616 `icicle-display-candidates-in-Completions' as its second arg."
3617   (let ((iac1-was-cycling-p  icicle-cycling-p)
3618         (mode-line-help      nil)
3619         input-before-completion)
3620     (setq icicle-current-input                  (if (and icicle-last-input
3621                                                          icicle-cycling-p
3622                                                          (not icicle-edit-update-p)
3623                                                          (eq icicle-current-completion-mode 'apropos)
3624                                                          (symbolp last-command)
3625                                                          (or (get last-command 'icicle-cycling-command)
3626                                                              (get last-command 'icicle-action-command))
3627                                                          icicle-completion-candidates)
3628                                                     icicle-last-input
3629                                                   (icicle-input-from-minibuffer))
3630           icicle-current-completion-mode        'apropos
3631           icicle-next-prefix-complete-cycles-p  nil
3632           icicle-input-fail-pos                 nil
3633           icicle-cycling-p                      nil)
3634     (when icicle-edit-update-p (setq icicle-next-apropos-complete-cycles-p  nil))
3635     (when (icicle-file-name-input-p)
3636       (setq icicle-current-input  (abbreviate-file-name
3637                                    (if icicle-regexp-quote-flag
3638                                        (substitute-in-file-name icicle-current-input)
3639                                      icicle-current-input))))
3640     (setq input-before-completion  icicle-current-input)
3641     (unless (or icicle-edit-update-p (get-buffer-window "*Completions*" 0) no-display-p)
3642       (message "Computing completion candidates..."))
3643     (unless (and (stringp icicle-current-input) (stringp icicle-last-input)
3644                  (string= icicle-current-input icicle-last-input)
3645                  (or (get last-command 'icicle-apropos-completing-command)
3646                      (get last-command 'icicle-action-command)))
3647       (setq icicle-completion-candidates
3648             (icicle-condition-case-no-debug nil
3649                 (if (icicle-file-name-input-p)
3650                     (icicle-file-name-apropos-candidates icicle-current-input)
3651                   (icicle-apropos-candidates icicle-current-input))
3652               (error icicle-completion-candidates)))) ; No change if completion error.
3653     (icicle-save-or-restore-input)
3654     (cond ((null icicle-completion-candidates)
3655            (setq icicle-nb-of-other-cycle-candidates  0)
3656            (let ((icicle-incremental-completion-flag ; Upgrade if OK for explicit.
3657                   (or (memq icicle-highlight-input-completion-failure
3658                             '(explicit-strict explicit explicit-remote))
3659                       icicle-incremental-completion-flag)))
3660              (icicle-highlight-input-noncompletion))
3661            (save-selected-window (icicle-remove-Completions-window))
3662            (run-hooks 'icicle-no-match-hook)
3663            (unless (eq no-display-p 'no-msg)
3664              (minibuffer-message (let ((typ  (car (rassq icicle-apropos-complete-match-fn
3665                                                          icicle-S-TAB-completion-methods-alist))))
3666                                    (concat "  [No " typ (and typ " ") "completion]")))))
3667           ((null (cdr icicle-completion-candidates)) ; Single candidate. Update minibuffer.
3668            ;; When `icicle-whole-candidate-as-text-prop-p' is t
3669            ;; and `icicle-expand-input-to-common-match-flag' is nil, we need to expand the input anyway.
3670            ;; That transfers any `icicle-whole-candidate' property from the candidate to
3671            ;; `icicle-current-input', so things that use `icicle-candidates-alist' will work.
3672            (when (and icicle-whole-candidate-as-text-prop-p
3673                       (not icicle-expand-input-to-common-match-flag))
3674              (setq icicle-common-match-string  (icicle-expanded-common-match
3675                                                 icicle-current-input icicle-completion-candidates))
3676              (when icicle-common-match-string
3677                (let ((common  (if (and (icicle-file-name-input-p) insert-default-directory)
3678                                   (if (string= "" icicle-common-match-string)
3679                                       (or (icicle-file-name-directory icicle-current-input) "")
3680                                     (directory-file-name (icicle-abbreviate-or-expand-file-name
3681                                                           icicle-common-match-string
3682                                                           (icicle-file-name-directory
3683                                                            icicle-current-input))))
3684                                 icicle-common-match-string)))
3685                  ;; Save as current input, unless input is a directory.
3686                  (unless (and (icicle-file-name-input-p)  (file-directory-p icicle-current-input))
3687                    (setq icicle-current-input  common)))))
3688            ;; Expand file-name input to the common match for the current candidate.
3689            (when (icicle-file-name-input-p)
3690              (setq icicle-common-match-string  (icicle-expanded-common-match
3691                                                 (car icicle-completion-candidates)
3692                                                 icicle-completion-candidates))
3693              (when icicle-common-match-string
3694                (let ((common  (if (and (icicle-file-name-input-p) insert-default-directory)
3695                                   (if (string= "" icicle-common-match-string)
3696                                       (or (icicle-file-name-directory icicle-current-input) "")
3697                                     (directory-file-name (icicle-abbreviate-or-expand-file-name
3698                                                           icicle-common-match-string
3699                                                           (icicle-file-name-directory
3700                                                            icicle-current-input))))
3701                                 icicle-common-match-string)))
3702                  (setq icicle-current-input  common))))
3703            (setq icicle-nb-of-other-cycle-candidates  0)
3704            (unless icicle-edit-update-p
3705              (icicle-clear-minibuffer)
3706              (if (icicle-file-name-input-p)
3707                  (let ((cand  (car icicle-completion-candidates)))
3708                    (cond ((string= "" cand) ; This indicates an empty dir.
3709                           (setq icicle-last-completion-candidate  icicle-current-input))
3710                          ((eq ?\/  (aref cand (1- (length cand)))) ; Add `/', so cycling expands dir.
3711                           (setq icicle-current-input              (concat icicle-current-input "/")
3712                                 icicle-last-completion-candidate  icicle-current-input))
3713                          (t             ; Non-dir - use the candidate file.
3714                           (setq icicle-last-completion-candidate  (car icicle-completion-candidates)))))
3715                (setq icicle-last-completion-candidate  (car icicle-completion-candidates)))
3716              (let ((inserted  (if (and (icicle-file-name-input-p) insert-default-directory
3717                                        (or (not (member icicle-last-completion-candidate
3718                                                         icicle-extra-candidates))
3719                                            icicle-extra-candidates-dir-insert-p))
3720                                   (icicle-abbreviate-or-expand-file-name
3721                                    icicle-last-completion-candidate
3722                                    (icicle-file-name-directory-w-default icicle-current-input))
3723                                 icicle-last-completion-candidate)))
3724                (insert inserted)
3725                (when (and (icicle-file-name-input-p)
3726                           (icicle-file-directory-p (icicle-abbreviate-or-expand-file-name inserted)))
3727                  (setq icicle-default-directory  (icicle-abbreviate-or-expand-file-name inserted)))))
3728            (save-selected-window (icicle-remove-Completions-window))
3729            (icicle-transform-sole-candidate)
3730            (unless (boundp 'icicle-apropos-complete-and-exit-p)
3731              (icicle-highlight-complete-input)
3732              (cond ((and icicle-top-level-when-sole-completion-flag
3733                          (sit-for icicle-top-level-when-sole-completion-delay))
3734                     (set minibuffer-history-variable (cons (car icicle-completion-candidates)
3735                                                            (symbol-value minibuffer-history-variable)))
3736                     (icicle-condition-case-no-debug icicle-apropos-complete-1
3737                         (throw 'icicle-read-top
3738                           (if (and (icicle-file-name-input-p) insert-default-directory
3739                                    (or (not (member (car icicle-completion-candidates)
3740                                                     icicle-extra-candidates))
3741                                        icicle-extra-candidates-dir-insert-p))
3742                               (expand-file-name (car icicle-completion-candidates))
3743                             (car icicle-completion-candidates)))
3744                       (no-catch (setq icicle-current-input  (car icicle-completion-candidates))
3745                                 (icicle-retrieve-last-input)
3746                                 icicle-current-input)
3747                       (error (message "%s" (error-message-string icicle-apropos-complete-1)))))
3748                    ((and icicle-edit-update-p (not (eq no-display-p 'no-msg)))
3749                     (minibuffer-message (format "  [One apropos completion: %s]"
3750                                                 (car icicle-completion-candidates)))
3751                     (setq mode-line-help  (car icicle-completion-candidates)))
3752                    ((not (eq no-display-p 'no-msg))
3753                     (minibuffer-message "  [Sole apropos completion]")
3754                     (setq mode-line-help  (car icicle-completion-candidates))))))
3755           (t                            ; Multiple candidates.
3756            (if icicle-edit-update-p
3757                (icicle-display-candidates-in-Completions nil no-display-p)
3758              (icicle-clear-minibuffer)
3759              (insert icicle-current-input) ; Update minibuffer.
3760              ;; Shouldn't need to do this if it is on `post-command-hook', but it seems we need to.
3761              (when (and (boundp '1on1-fit-minibuffer-frame-flag) 1on1-fit-minibuffer-frame-flag
3762                         (require 'fit-frame nil t))
3763                (1on1-fit-minibuffer-frame)) ; In `oneonone.el'.
3764              (deactivate-mark)
3765              (icicle-highlight-initial-whitespace icicle-current-input)
3766              (when (and (icicle-file-name-input-p)
3767                         (icicle-file-directory-p icicle-last-completion-candidate))
3768                (setq icicle-default-directory  (icicle-abbreviate-or-expand-file-name
3769                                                 icicle-last-completion-candidate)))
3770              (let ((input-sans-dir  (icicle-minibuf-input-sans-dir icicle-current-input)))
3771                (when (and (member (icicle-upcase-if-ignore-case input-sans-dir)
3772                                   (mapcar #'icicle-upcase-if-ignore-case icicle-completion-candidates))
3773                           (not (boundp 'icicle-apropos-complete-and-exit-p)))
3774                  (icicle-highlight-complete-input)
3775                  (setq mode-line-help  input-sans-dir)))
3776              (cond (;; Candidates already displayed.  If second `S-TAB', cycle, else update candidates.
3777                     (get-buffer-window "*Completions*" 0)
3778                     (if (and (or iac1-was-cycling-p icicle-next-apropos-complete-cycles-p)
3779                              (get icicle-last-completion-command 'icicle-apropos-completing-command)
3780                              (or (get last-command 'icicle-apropos-completing-command)
3781                                  (get last-command 'icicle-action-command)))
3782                         ;; Second `S-TAB' in a row.  Cycle down.
3783                         (icicle-next-candidate 1 (if (icicle-file-name-input-p)
3784                                                      'icicle-file-name-apropos-candidates
3785                                                    'icicle-apropos-candidates)
3786                                                'regexp-p)
3787                       ;; User did something else (e.g. changed input).  (Possibly) update the display.
3788                       (icicle-display-candidates-in-Completions nil no-display-p)))
3789                    (t
3790                     (if (not (and (or iac1-was-cycling-p icicle-next-apropos-complete-cycles-p)
3791                                   (get icicle-last-completion-command
3792                                        'icicle-apropos-completing-command)
3793                                   (or (get last-command 'icicle-apropos-completing-command)
3794                                       (get last-command 'icicle-action-command))))
3795                         (icicle-display-candidates-in-Completions nil no-display-p)
3796                       ;; Second apropos complete.  If `S-TAB', it follows `C-M-S-TAB', so show window.
3797                       (unless no-display-p (icicle-display-candidates-in-Completions nil))
3798                       (icicle-next-candidate 1 (if (icicle-file-name-input-p)
3799                                                    'icicle-file-name-apropos-candidates
3800                                                  'icicle-apropos-candidates)
3801                                              'regexp-p)))))))
3802     (setq icicle-last-completion-command         (if no-display-p
3803                                                      'icicle-apropos-complete-no-display
3804                                                    'icicle-apropos-complete)
3805           icicle-next-apropos-complete-cycles-p  (equal input-before-completion
3806                                                         (icicle-input-from-minibuffer)))
3807     (when mode-line-help (icicle-show-help-in-mode-line mode-line-help))
3808     icicle-completion-candidates))
3809
3810 (defun icicle-transform-sole-candidate ()
3811   "Transform matching candidate according to `icicle-list-use-nth-parts'."
3812   (when (and icicle-list-use-nth-parts icicle-current-input)
3813     ;; $$$$$$ (let ((newcand  (icicle-transform-multi-completion (car icicle-completion-candidates))))
3814     (let ((newcand  (icicle-transform-multi-completion icicle-current-input)))
3815       (icicle-clear-minibuffer)
3816       (insert newcand)
3817       (setq icicle-completion-candidates      (list newcand)
3818             icicle-last-completion-candidate  newcand))))
3819
3820 ;;;###autoload
3821 (defun icicle-switch-to-Completions-buf () ; Bound to `C-insert' in minibuffer.
3822   "Select the completion list window.
3823 The cursor is placed on the first occurrence of the current minibuffer
3824 content.  You can use \\<completion-list-mode-map>\
3825 `\\[icicle-insert-completion]' to get back to the minibuffer.
3826
3827 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
3828 \\[icicle-switch-to-Completions-buf]')."
3829   (interactive)
3830   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
3831   (let ((window     (get-buffer-window "*Completions*" 0))
3832         (search-fn  'search-forward))
3833     (unless window                      ; Make sure we have a completions window.
3834       (icicle-apropos-complete)
3835       (setq window     (get-buffer-window "*Completions*" 0)
3836             search-fn  're-search-forward)) ; Use regexp search: input is not yet complete.
3837     (when window
3838       (select-window window)
3839       (let ((case-fold-search
3840              ;; Don't bother to detect buffer completion and check `read-buffer-completion-ignore-case'.
3841              (if (and (icicle-file-name-input-p)
3842                       (boundp 'read-file-name-completion-ignore-case))
3843                  read-file-name-completion-ignore-case
3844                completion-ignore-case)))
3845         (goto-char (icicle-start-of-candidates-in-Completions))
3846         (cond (icicle-candidate-nb
3847                (icicle-move-to-next-completion icicle-candidate-nb 'NO-MINIBUFFER-FOLLOW-P))
3848               (t
3849                (let ((inp  (icicle-minibuf-input-sans-dir icicle-current-input)))
3850                  (when (and (get icicle-last-completion-command 'icicle-apropos-completing-command)
3851                             ;; $$ Previously allowed the -action's.
3852                             (not (and (symbolp last-command) (get last-command 'icicle-cycling-command))))
3853                    (setq search-fn  're-search-forward)) ; Use regexp search: input is not yet complete.
3854                  (while (and (not (eobp))
3855                              (save-restriction
3856                                (narrow-to-region (point) (next-single-property-change (point) 'mouse-face
3857                                                                                       nil (point-max)))
3858                                (not (funcall search-fn inp nil 'leave-at-end))))))))
3859         (unless (eobp)
3860           (unless icicle-candidate-nb (goto-char (match-beginning 0)))
3861           (let ((prop  (get-text-property (1- (point)) 'mouse-face)))
3862             ;; If in a completion, move to the start of it.
3863             (when (and prop (eq prop (get-text-property (point) 'mouse-face)))
3864               (goto-char (previous-single-property-change (point) 'mouse-face nil (point-min)))))
3865           (icicle-place-overlay
3866            (point) (next-single-property-change (point) 'mouse-face nil (point-max))
3867            'icicle-current-completion-candidate-overlay 'icicle-current-candidate-highlight
3868            100 (current-buffer)))))))
3869
3870 ;;;###autoload
3871 (defun icicle-insert-completion (&optional completion) ; Bound to `C-insert' in `*Completions*'.
3872   "Select the active minibuffer window.  Insert current completion.
3873 The current candidate in `*Completions*' (under the cursor) is
3874 inserted into the minibuffer as the current input.  You can use \\<minibuffer-local-completion-map>\
3875 `\\[icicle-switch-to-Completions-buf]'
3876 to switch to the `*Completions*' window.
3877
3878 You can use this command only from buffer `*Completions*' (`\\<completion-list-mode-map>\
3879 \\[icicle-insert-completion]').
3880
3881 Non-interactively, optional arg COMPLETION is the completion to insert."
3882   (interactive)
3883   (when (interactive-p) (icicle-barf-if-outside-Completions))
3884   (when (active-minibuffer-window)
3885     (unwind-protect                     ; If no current completion, return to minibuffer anyway.
3886          (progn
3887            (setq completion                        (or completion
3888                                                        (icicle-current-completion-in-Completions))
3889                  icicle-last-completion-candidate  completion
3890                  icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos (point)))
3891            (select-window (active-minibuffer-window))
3892            (with-current-buffer (window-buffer) ; Needed if `*Completions*' is redirected to minibuffer.
3893              (goto-char (icicle-minibuffer-prompt-end))
3894              (icicle-clear-minibuffer)
3895              (icicle-insert-cand-in-minibuffer completion
3896                                                (not (eq icicle-current-completion-mode 'prefix)))
3897              (setq icicle-last-input  icicle-current-input
3898                    icicle-cycling-p   t)
3899              (icicle-show-help-in-mode-line icicle-last-completion-candidate)))
3900       (select-window (active-minibuffer-window)))))
3901
3902 (defun icicle-current-completion-in-Completions ()
3903   "The completion candidate under the cursor in buffer `*Completions*'.
3904 Return the name as a string." ; See also `choose-completion' and `mouse-choose-completion'.
3905   (let ((buffer          completion-reference-buffer)
3906         (base-size       completion-base-size)
3907         (start-of-cands  (icicle-start-of-candidates-in-Completions))
3908         beg end)
3909     (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
3910       (setq end  (point)
3911             beg  (1+ (point))))
3912     (when (and (> (point) start-of-cands) (get-text-property (1- (point)) 'mouse-face))
3913       (setq end  (1- (point))
3914             beg  (point)))
3915     (setq beg  (previous-single-property-change (or beg (point)) 'mouse-face nil start-of-cands)
3916           end  (next-single-property-change (or end (point)) 'mouse-face nil (point-max)))
3917     (unless beg (error "No completion here"))
3918     ;; `icicle-insert-candidates' doesn't put `mouse-face' on the final \n of a candidate
3919     ;; in `*Completions*'. Add the newline back. `icicle-insert-candidates' puts property
3920     ;; `icicle-keep-newline' on the newline if it is part of the candidate, as opposed to
3921     ;; being just part of the display in columns.
3922     (when (and (eq ?\n (char-after end)) (get-text-property end 'icicle-keep-newline))
3923       (setq end  (1+ end)))
3924     ;; $$$$ (buffer-substring-no-properties beg end)))
3925     (buffer-substring beg end)))
3926
3927 ;;;###autoload
3928 (defun icicle-switch-to/from-minibuffer () ; Bound to `pause' in Icicle mode.
3929   "Switch to minibuffer or previous buffer, in other window.
3930 If current buffer is the minibuffer, then switch to the buffer that
3931 was previously current.  Otherwise, switch to the minibuffer."
3932   (interactive)
3933   (unless (active-minibuffer-window) (error "Minibuffer is not active"))
3934   (if (eq (selected-window) (active-minibuffer-window))
3935       (switch-to-buffer-other-window icicle-pre-minibuffer-buffer)
3936     (select-window (active-minibuffer-window))))
3937
3938
3939 ;; Replaces `previous-completion' (defined in `simple.el').
3940 ;;;###autoload
3941 (defun icicle-move-to-previous-completion (n) ; Bound to `left', `S-TAB' in `*Completions*'.
3942   "Move to the previous item in the completion list.
3943
3944 You can use this command only from buffer `*Completions*' (`\\<completion-list-mode-map>\
3945 \\[icicle-move-to-previous-completion]')."
3946   (interactive "p")
3947   (when (interactive-p) (icicle-barf-if-outside-Completions))
3948   (setq n  (or n 0))
3949   (icicle-move-to-next-completion (- n)))
3950
3951
3952 ;; Replaces `next-completion' (defined in `simple.el').
3953 ;; This is similar, except:
3954 ;; 1. This highlights the current candidate.
3955 ;; 2. This wraps around from first to last and last to first.
3956 ;; 3. Properly handles completions laid out vertically.
3957 ;;
3958 ;;;###autoload
3959 (defun icicle-move-to-next-completion (n ; Bound to `right', `TAB' in `*Completions*'.
3960                                        &optional no-minibuffer-follow-p)
3961   "Move to the next item in the completion list.
3962 With prefix argument N, move N items (negative N means move backward).
3963 Optional second argument, if non-nil, means do not copy the completion
3964 back to the minibuffer.
3965
3966 You can use this command only from buffer `*Completions*' (`\\<completion-list-mode-map>\
3967 \\[icicle-move-to-next-completion]')."
3968   (interactive "p")
3969   (when (interactive-p) (icicle-barf-if-outside-Completions))
3970   (setq n  (or n 0))
3971   (when (eq icicle-completions-format 'vertical)
3972     (let* ((cols      (icicle-nb-Completions-cols))
3973            (nb-cands  (length icicle-completion-candidates))
3974            (rows      (/ nb-cands cols)))
3975       (unless (zerop (% nb-cands cols)) (setq rows  (1+ rows)))
3976       (setq n  (icicle-row-wise-cand-nb n nb-cands rows cols))))
3977   (let ((beg  (icicle-start-of-candidates-in-Completions))
3978         (end  (point-max)))
3979  
3980     ;; Forward: n > 0.
3981     (while (and (> n 0) (not (eobp)))
3982       (when (get-text-property (point) 'mouse-face) ; If in a candidate, move to its end.
3983         (goto-char (next-single-property-change (point) 'mouse-face nil end)))
3984       (unless (get-text-property (point) 'mouse-face) ; Move to start of next candidate.
3985         (goto-char (or (next-single-property-change (point) 'mouse-face)
3986                        beg)))           ; Wrap back to first candidate.
3987       (setq n  (1- n)))
3988
3989     ;; Backward: n < 0.
3990     (while (and (< n 0) (>= (count-lines 1 (point)) (if icicle-show-Completions-help-flag 3 2)))
3991       (let ((prop  (get-text-property (1- (point)) 'mouse-face)))
3992         (when (and prop (eq prop (get-text-property (point) 'mouse-face))) ; If in cand, move to start.
3993           (goto-char (previous-single-property-change (point) 'mouse-face nil beg))))
3994       (unless (or (< (count-lines 1 (point)) ; Move to end of previous candidate.
3995                      (if icicle-show-Completions-help-flag 3 2))
3996                   (get-text-property (1- (point)) 'mouse-face))
3997         (goto-char (or (previous-single-property-change (point) 'mouse-face)
3998                        end)))           ; Wrap back to last candidate.
3999
4000       ;; Move to the start of that candidate.
4001       (goto-char (previous-single-property-change (point) 'mouse-face nil beg))
4002       (setq n  (1+ n)))
4003
4004     (icicle-place-overlay
4005      (point) (next-single-property-change (point) 'mouse-face nil end)
4006      'icicle-current-completion-candidate-overlay 'icicle-current-candidate-highlight
4007      100 (current-buffer)))
4008   (unless no-minibuffer-follow-p (save-excursion (save-window-excursion (icicle-insert-completion)))))
4009
4010 ;;;###autoload
4011 (defun icicle-previous-line ()          ; Bound to `up' in `*Completions*'.
4012   "Move up a line, in `*Completions*' buffer.  Wrap around first to last.
4013 You can use this command only from buffer `*Completions*' (`\\<completion-list-mode-map>\
4014 \\[icicle-previous-line]')."
4015   (interactive)
4016   (when (interactive-p) (icicle-barf-if-outside-Completions))
4017   (let ((opoint          (point))
4018         (curr-col        1)
4019         (next-line-cols  1)
4020         (eol             (save-excursion (end-of-line) (point))))
4021     (save-excursion
4022       (beginning-of-line)
4023       (while (and (< (point) opoint) (re-search-forward "[^ ] +" eol t))
4024         (setq curr-col  (1+ curr-col))))
4025     (forward-line -1)
4026     (when (< (point) (icicle-start-of-candidates-in-Completions))
4027       (goto-char (point-max)) (beginning-of-line)) ; Wrap around
4028     (let ((eol  (save-excursion (end-of-line) (point))))
4029       (save-excursion
4030         (beginning-of-line)
4031         (while (re-search-forward "[^ ] +[^ ]" eol t) (setq next-line-cols  (1+ next-line-cols)))))
4032     (if (> curr-col next-line-cols)
4033         (icicle-move-to-next-completion (1- next-line-cols))
4034       (icicle-move-to-next-completion (1- curr-col)))))
4035
4036 ;;;###autoload
4037 (defun icicle-next-line ()              ; Bound to `down' in `*Completions*'.
4038   "Move down a line, in `*Completions*' buffer.  Wrap around last to first.
4039 You can use this command only from buffer `*Completions*' (`\\<completion-list-mode-map>\
4040 \\[icicle-next-line]')."
4041   (interactive)
4042   (when (interactive-p) (icicle-barf-if-outside-Completions))
4043   (let ((opoint          (point))
4044         (curr-col        1)
4045         (next-line-cols  1)
4046         (eol             (save-excursion (end-of-line) (point))))
4047     (save-excursion
4048       (beginning-of-line)
4049       (while (and (< (point) opoint) (re-search-forward "[^ ] +" eol t))
4050         (setq curr-col  (1+ curr-col))))
4051     (forward-line 1)
4052     (when (eobp) (goto-char (icicle-start-of-candidates-in-Completions))) ; Wrap around
4053     (let ((eol  (save-excursion (end-of-line) (point))))
4054       (save-excursion
4055         (beginning-of-line)
4056         (while (re-search-forward "[^ ] +[^ ]" eol t) (setq next-line-cols  (1+ next-line-cols)))))
4057     (if (> curr-col next-line-cols)
4058         (icicle-move-to-next-completion (1- next-line-cols))
4059       (icicle-move-to-next-completion (1- curr-col)))))
4060
4061 ;; Same as `end-of-line+' in `misc-cmds.el'.
4062 ;;;###autoload
4063 (defun icicle-end-of-line+ (&optional n) ; Bound to `C-e' in minibuffer and in `*Completions*'.
4064   "Move cursor to end of current line or end of next line if repeated.
4065 This is similar to `end-of-line', but:
4066   If called interactively with no prefix arg:
4067      If the previous command was also `end-of-line+', then move to the
4068      end of the next line.  Else, move to the end of the current line.
4069   Otherwise, move to the end of the Nth next line (Nth previous line
4070      if N<0).  Command `end-of-line', by contrast, moves to the end of
4071      the (N-1)th next line."
4072   (interactive
4073    (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 0)))
4074   (unless n (setq n  0))                ; non-interactive with no arg
4075   (if (and (eq this-command last-command) (not current-prefix-arg))
4076       (forward-line 1)
4077     (forward-line n))
4078   (let ((inhibit-field-text-motion  t)) ; Emacs 22+, so we get past the end of the prompt field.
4079     (end-of-line)))
4080
4081 ;; Same as `beginning-of-line+' in `misc-cmds.el'.
4082 ;;;###autoload
4083 (defun icicle-beginning-of-line+ (&optional n) ; Bound to `C-a' in minibuffer and in `*Completions*'.
4084   "Move cursor to beginning of current line or next line if repeated.
4085 This is the similar to `beginning-of-line', but:
4086 1. With arg N, the direction is the opposite: this command moves
4087    backward, not forward, N lines.
4088 2. If called interactively with no prefix arg:
4089       If the previous command was also `beginning-of-line+', then move
4090       to the beginning of the previous line.  Else, move to the
4091       beginning of the current line.
4092    Otherwise, move to the beginning of the Nth previous line (Nth next
4093       line if N<0).  Command `beginning-of-line', by contrast, moves to
4094       the beginning of the (N-1)th next line."
4095   (interactive
4096    (list (if current-prefix-arg (prefix-numeric-value current-prefix-arg) 0)))
4097   (unless n (setq n  0))                ; non-interactive with no arg
4098   (if (and (eq this-command last-command) (not current-prefix-arg))
4099       (forward-line -1)
4100     (forward-line (- n)))
4101   (when (bobp) (goto-char (icicle-minibuffer-prompt-end))))
4102
4103 ;; Same as `resolve-file-name' in `misc-cmds.el'.
4104 ;;;###autoload
4105 (defun icicle-resolve-file-name (bounds &optional killp) ; Bound to `C-x C-f' in minibuffer.
4106   "Replace the file name at/near point by its absolute, true file name.
4107 If the region is active, replace its content instead, treating it as a
4108 file name.
4109
4110 If library `thingatpt+.el' is available then use the file name
4111 *nearest* point.  Otherwise, use the file name *at* point.
4112
4113 With a prefix arg, add both the original file name and the true name
4114 to the kill ring.  Otherwise, add neither to the kill ring.  (If the
4115 region was active then its content was already added to the ring.)"
4116   (interactive
4117    (let* ((regionp   (and transient-mark-mode mark-active))
4118           (thg+bnds  (and (not regionp)
4119                           (require 'thingatpt+ nil t)
4120                           (thing-nearest-point-with-bounds 'filename)))
4121           (bnds      (if regionp
4122                          (cons (region-beginning) (region-end))
4123                        (if thg+bnds
4124                            (cdr thg+bnds)
4125                          (bounds-of-thing-at-point 'filename))))
4126           (fname     (if bnds
4127                          (buffer-substring (car bnds) (cdr bnds))
4128                        (message "No file name at point"))))
4129      (list bnds current-prefix-arg)))
4130   (when bounds
4131     (let* ((file       (buffer-substring (car bounds) (cdr bounds)))
4132            (absfile    (expand-file-name (buffer-substring (car bounds) (cdr bounds))))
4133            (dir        (or (file-name-directory absfile) default-directory))
4134            (true-dir   (file-truename dir))
4135            (relfile    (file-name-nondirectory absfile))
4136            (true-file  (concat true-dir relfile)))
4137       (unless (equal file true-file)
4138         (cond (killp
4139                (if (and transient-mark-mode mark-active)
4140                    (delete-region (car bounds) (cdr bounds)) ; Don't add it twice.
4141                  (kill-region (car bounds) (cdr bounds)))
4142                (insert (kill-new true-file)))
4143               (t
4144                (delete-region (car bounds) (cdr bounds))
4145                (insert true-file)))))))
4146
4147
4148 (put 'icicle-all-candidates-action 'icicle-action-command t)
4149 ;;;###autoload
4150 (defun icicle-all-candidates-action ()  ; Bound to `C-!' in minibuffer.
4151   "Take action on each completion candidate, in turn.
4152 Apply `icicle-candidate-action-fn' successively to each saved
4153 completion candidate (if any) or each candidate that matches the
4154 current input (a regular expression).  The candidates that were not
4155 successfully acted upon are listed in buffer *Help*.
4156
4157 If there are saved completion candidates, then they are acted on;
4158 if not, then all current matching candidates are acted on.
4159
4160 If `icicle-candidate-action-fn' is nil but
4161 `icicle-all-candidates-list-action-fn' is not, then apply the latter
4162 to the list of candidates as a whole, instead.
4163
4164 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4165 \\[icicle-all-candidates-action]')."
4166   (interactive)
4167   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
4168   (unless (or icicle-all-candidates-list-action-fn icicle-candidate-action-fn)
4169     (error "No action defined"))
4170   (if icicle-candidate-action-fn
4171       (icicle-all-candidates-action-1 icicle-candidate-action-fn nil)
4172     (icicle-all-candidates-action-1 icicle-all-candidates-list-action-fn t)))
4173
4174
4175 (put 'icicle-all-candidates-alt-action 'icicle-action-command t)
4176 ;;;###autoload
4177 (defun icicle-all-candidates-alt-action () ; Bound to `C-|' in minibuffer.
4178   "Take alternative action on each completion candidate, in turn.
4179 Apply `icicle-candidate-alt-action-fn' successively to each saved
4180 completion candidate (if any) or each candidate that matches the
4181 current input (a regular expression).  The candidates that were not
4182 successfully acted upon are listed in buffer *Help*.
4183
4184 If there are saved completion candidates, then they are acted on; if
4185 not, then all current matching candidates are acted on.
4186
4187 If `icicle-candidate-alt-action-fn' is nil but
4188 `icicle-all-candidates-list-alt-action-fn' is not, then apply the
4189 latter to the list of candidates as a whole, instead.
4190
4191 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4192 \\[icicle-all-candidates-alt-action]')."
4193   (interactive)
4194   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
4195   (unless (or icicle-all-candidates-list-alt-action-fn icicle-candidate-alt-action-fn)
4196     (error "No alternative action defined"))
4197   (if icicle-candidate-alt-action-fn
4198       (icicle-all-candidates-action-1 icicle-candidate-alt-action-fn nil t) ; ALTP flag
4199     (icicle-all-candidates-action-1 icicle-all-candidates-list-alt-action-fn t)))
4200
4201
4202 (put 'icicle-all-candidates-list-action 'icicle-action-command t)
4203 ;;;###autoload
4204 (defun icicle-all-candidates-list-action () ; Bound to `M-!' in minibuffer.
4205   "Take action on the list of all completion candidates.
4206 Apply `icicle-all-candidates-list-action-fn' to the list of saved
4207 completion candidates or the list of candidates that match the current
4208 input (a regular expression).
4209
4210 If there are saved completion candidates, then they are acted on; if
4211 not, then all current matching candidates are acted on.
4212
4213 If `icicle-all-candidates-list-action-fn' is nil but
4214 `icicle-candidate-action-fn' is not, then apply the latter to each
4215 matching candidate in turn, and print the candidates that were not
4216 successfully acted upon in buffer *Help*.
4217
4218 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4219 \\[icicle-all-candidates-list-action]')."
4220   (interactive)
4221   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
4222   (unless (or icicle-all-candidates-list-action-fn icicle-candidate-action-fn)
4223     (error "No action defined"))
4224   (if icicle-all-candidates-list-action-fn
4225       (icicle-all-candidates-action-1 icicle-all-candidates-list-action-fn t)
4226     (icicle-all-candidates-action-1 icicle-candidate-action-fn nil)))
4227
4228
4229 (put 'icicle-all-candidates-list-alt-action 'icicle-action-command t)
4230 ;;;###autoload
4231 (defun icicle-all-candidates-list-alt-action () ; Bound to `M-|' in minibuffer.
4232   "Take alternative action on the list of all completion candidates.
4233 Apply `icicle-all-candidates-list-alt-action-fn' to the list of saved
4234 completion candidates or the list of completion candidates that match
4235 the current input (a regular expression).
4236
4237 If there are saved completion candidates, then they are acted on;
4238 if not, then all current matching candidates are acted on.
4239
4240 If `icicle-all-candidates-list-alt-action-fn' is nil but
4241 `icicle-candidate-alt-action-fn' is not, then apply the latter to each
4242 matching candidate in turn, and print the candidates that were not
4243 successfully acted upon in buffer *Help*.
4244
4245 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4246 \\[icicle-all-candidates-list-alt-action]')."
4247   (interactive)
4248   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
4249   (unless (or icicle-all-candidates-list-alt-action-fn icicle-candidate-alt-action-fn)
4250     (error "No alternative action defined"))
4251   (unless icicle-completion-candidates
4252     (error "No completion candidates.  Did you use `TAB' or `S-TAB'?"))
4253   (if icicle-all-candidates-list-alt-action-fn
4254       (icicle-all-candidates-action-1 icicle-all-candidates-list-alt-action-fn t)
4255     (icicle-all-candidates-action-1 icicle-candidate-alt-action-fn nil t))) ; ALTP flag
4256
4257 (defun icicle-all-candidates-action-1 (fn-var listp &optional altp)
4258   "Helper function for `icicle-all-candidates(-alt)-action'.
4259 ALTP is used only if LISTP is nil.
4260 ALTP is passed to `icicle-candidate-action-1'."
4261   (let* ((local-saved
4262           (catch 'i-a-c-a-1
4263             (dolist (cand  icicle-saved-completion-candidates  icicle-saved-completion-candidates)
4264               (unless (member cand icicle-completion-candidates) (throw 'i-a-c-a-1 nil)))))
4265          (candidates                      (or local-saved icicle-completion-candidates))
4266          (failures                        nil)
4267          (icicle-minibuffer-message-ok-p  nil) ; Avoid delays from `icicle-msg-maybe-in-minibuffer'.
4268          (icicle-help-in-mode-line-delay  0) ; Avoid delays for individual candidate help.
4269          (icicle-all-candidates-action    t))
4270     (when local-saved (setq icicle-completion-candidates  local-saved))
4271     (if listp
4272         (funcall fn-var candidates)
4273       (while candidates
4274         (let ((error-msg  (icicle-condition-case-no-debug act-on-each
4275                               (icicle-candidate-action-1 fn-var altp (car candidates))
4276                             (error (error-message-string act-on-each)))))
4277           (when error-msg (setq failures  (cons (cons (car candidates) error-msg) failures)))
4278           (setq candidates  (cdr candidates))))
4279       (when failures
4280         (with-output-to-temp-buffer "*Help*"
4281           (princ "Action failures:")(terpri)(terpri)
4282           (mapcar (lambda (entry)
4283                     (princ (car entry)) (princ ":") (terpri) (princ "  ")
4284                     (princ (cdr entry)) (terpri))
4285                   failures))))))
4286 ;; $$$$$$ (icicle-abort-recursive-edit))
4287
4288
4289 (put 'icicle-candidate-action 'icicle-action-command t)
4290 ;;;###autoload
4291 (defun icicle-candidate-action ()       ; Bound to `C-RET' in minibuffer.
4292   "Take action on the current minibuffer-completion candidate.
4293 If `icicle-candidate-action-fn' is non-nil, it is a function to apply
4294 to the current candidate, to perform the action.
4295
4296 If no action is available in the current context, help on the
4297 candidate is shown - see `icicle-help-on-candidate'.
4298
4299 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4300 \\[icicle-candidate-action]')."
4301   (interactive)
4302   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
4303   (icicle-candidate-action-1 icicle-candidate-action-fn))
4304
4305
4306 (put 'icicle-candidate-alt-action 'icicle-action-command t)
4307 ;;;###autoload
4308 (defun icicle-candidate-alt-action ()   ; Bound to `C-S-RET' in minibuffer.
4309   "Take alternative action on the current completion candidate.
4310 If `icicle-candidate-alt-action-fn' is non-nil, it is a
4311 function to apply to the current candidate, to perform the action.
4312
4313 For many Icicles commands, if `icicle-candidate-alt-action-fn' is nil,
4314 you are prompted to choose an alternative action, using completion.
4315
4316 In any case, any alternative action defined for the current context by
4317 user option `icicle-alternative-actions-alist' always overrides
4318 `icicle-candidate-alt-action'.  That is, if
4319 `icicle-alternative-actions-alist' says to use function `foo', then
4320 Icicles uses `foo' as the alternative action, regardless of the value
4321 of `icicle-candidate-alt-action'.
4322
4323 If no alternative action is available in the current context, help on
4324 the candidate is shown - see `icicle-help-on-candidate'.  
4325
4326 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4327 \\[icicle-candidate-alt-action]')."
4328   (interactive)
4329   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
4330   (let ((alt-fn  (or (cdr (assq icicle-cmd-reading-input icicle-alternative-actions-alist))
4331                      icicle-candidate-alt-action-fn)))
4332     (icicle-candidate-action-1 alt-fn 'alternative-p)))
4333
4334 (defun icicle-candidate-action-1 (fn-var &optional altp cand)
4335   "Helper function for `icicle-candidate(-alt)-action'.
4336 FN-VAR is an Icicles action function or alternative action function.
4337 Optional arg ALTP non-nil means FN-VAR is alternative action function.
4338 Optional arg CAND non-nil means it is the candidate to act on."
4339   (when cand (setq icicle-last-completion-candidate  cand))
4340   (cond ((not fn-var) (icicle-help-on-candidate cand)) ; It doesn't `icicle-raise-Completions-frame'.
4341         ((icicle-require-match-p)
4342          ;; If no last candidate, then reset to first candidate matching input.
4343          (unless (stringp icicle-last-completion-candidate)
4344            (setq icicle-last-completion-candidate  icicle-current-input
4345                  last-command                      (if altp
4346                                                        'icicle-candidate-alt-action
4347                                                      'icicle-candidate-action))
4348            (let ((icicle-help-in-mode-line-delay  0)) ; Avoid delay for candidate help.
4349              (icicle-next-candidate 1 (if (eq icicle-current-completion-mode 'prefix)
4350                                           'icicle-prefix-candidates
4351                                         'icicle-apropos-candidates)
4352                                     (not (eq icicle-current-completion-mode 'prefix)))))
4353
4354          ;; NOTE: We no longer save and restore these things here.
4355          ;; We purposely allow an action function to modify these for subsequent actions.
4356          ;; If you need to save and restore these for a particular action function you define,
4357          ;; then you must do so in the action function itself.  This might be the case, for instance,
4358          ;; if your action function does its own completion (e.g. calls `completing-read'), that is, if
4359          ;; it uses a recursive minibuffer.  (But do not save and restore if you want the side effect.)
4360          (let (;; (icicle-candidate-nb               icicle-candidate-nb)              ; $$$$$$
4361                ;; (icicle-last-completion-candidate  icicle-last-completion-candidate) ; $$$$$$
4362                ;; (icicle-completion-candidates  icicle-completion-candidates)         ; $$$$$$
4363                )
4364            (when icicle-completion-candidates (funcall fn-var icicle-last-completion-candidate)))
4365          (when (or icicle-use-candidates-only-once-flag
4366                    (and altp icicle-use-candidates-only-once-alt-p))
4367            (icicle-remove-candidate-display-others 'all))
4368          (icicle-raise-Completions-frame))
4369         (t
4370          (let ((icicle-last-input         (or cand (icicle-input-from-minibuffer)))
4371                (icicle-default-directory  icicle-default-directory))
4372            (when (and (icicle-file-name-input-p) (icicle-file-directory-p icicle-last-input))
4373              (setq icicle-default-directory  icicle-last-input))
4374            ;; NOTE: We no longer save and restore these things here.
4375            ;; We purposely allow an action function to modify these for subsequent actions.
4376            ;; If you need to save and restore these for a particular action function you define,
4377            ;; then you must do so in the action function itself.  This might be the case, for instance,
4378            ;; if your action function does its own completion (e.g. calls `completing-read'), that is,
4379            ;; uses a recursive minibuffer.  (But do not save and restore if you want the side effect.)
4380            (let (;; (icicle-candidate-nb               icicle-candidate-nb)              ; $$$$$$
4381                  ;; (icicle-last-completion-candidate  icicle-last-completion-candidate) ; $$$$$$
4382                  ;; (icicle-completion-candidates      icicle-completion-candidates)     ; $$$$$$
4383                  )
4384              (funcall fn-var icicle-last-input))
4385            (when (and (or icicle-use-candidates-only-once-flag
4386                           (and altp icicle-use-candidates-only-once-alt-p))
4387                       (equal icicle-last-input
4388                              (if (icicle-file-name-input-p)
4389                                  (expand-file-name icicle-last-completion-candidate
4390                                                    (icicle-file-name-directory icicle-last-input))
4391                                icicle-last-completion-candidate)))
4392              (icicle-remove-candidate-display-others 'all))
4393            (icicle-raise-Completions-frame)))))
4394
4395
4396 ;; Bound to `C-down-mouse-2' (`C-mouse-2') in `*Completions*'.
4397 (put 'icicle-mouse-candidate-action 'icicle-action-command t)
4398 ;;;###autoload
4399 (defun icicle-mouse-candidate-action (event) ; `C-mouse-2'
4400   "Take action on the completion candidate clicked by `mouse-2'.
4401 If `icicle-candidate-action-fn' is non-nil, it is a function to apply
4402 to the clicked candidate, to perform the action.
4403
4404 If `icicle-candidate-action-fn' is nil, the default action is
4405 performed: display help on the candidate - see
4406 `icicle-help-on-candidate'."
4407   (interactive "e")
4408   (icicle-mouse-candidate-action-1 event icicle-candidate-action-fn))
4409
4410
4411 ; Bound to `C-S-down-mouse-2' (`C-S-mouse-2') in `*Completions*'.
4412 (put 'icicle-mouse-candidate-alt-action 'icicle-action-command t)
4413 ;;;###autoload
4414 (defun icicle-mouse-candidate-alt-action (event) ; `C-S-mouse-2'
4415   "Take alternative action on the candidate clicked by `mouse-2'.
4416 If `icicle-candidate-alt-action-fn' is non-nil, it is a
4417 function to apply to the clicked candidate, to perform the action.
4418
4419 If `icicle-candidate-action-fn' is nil, the default action is
4420 performed: display help on the candidate - see
4421 `icicle-help-on-candidate'."
4422   (interactive "e")
4423   (icicle-mouse-candidate-action-1 event icicle-candidate-alt-action-fn))
4424
4425 (defun icicle-mouse-candidate-action-1 (event fn-var)
4426   "Helper function for `icicle-mouse-candidate(-alt)-action'."
4427   (run-hooks 'mouse-leave-buffer-hook)  ; Give temp modes such as isearch a chance to turn off.
4428   (let ((posn-buf  (window-buffer (posn-window (event-start event))))
4429         (posn-pt   (posn-point (event-start event)))
4430         (posn-col  (car (posn-col-row (event-start event))))
4431         (posn-row  (cdr (posn-col-row (event-start event))))
4432         choice)
4433     (read-event)                        ; Swallow mouse up event.
4434     (with-current-buffer posn-buf
4435       (save-excursion
4436         (goto-char posn-pt)
4437         (let (beg end)
4438           (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
4439             (setq end  (point)
4440                   beg  (1+ (point))))
4441           (unless beg (error "No completion here"))
4442           (setq beg  (previous-single-property-change beg 'mouse-face)
4443                 end  (or (next-single-property-change end 'mouse-face) (point-max)))
4444           ;; `icicle-insert-candidates' doesn't put `mouse-face' on the final \n of a candidate
4445           ;; in `*Completions*'. Add the newline back. `icicle-insert-candidates' puts property
4446           ;; `icicle-keep-newline' on the newline if it is part of the candidate, as opposed to
4447           ;; being just part of the display in columns.
4448           (when (and (eq ?\n (char-after end)) (get-text-property end 'icicle-keep-newline))
4449             (setq end  (1+ end)))
4450           (setq choice  (if (and (icicle-file-name-input-p) insert-default-directory
4451                                  (or (not (member (buffer-substring-no-properties beg end)
4452                                                   icicle-extra-candidates))
4453                                      icicle-extra-candidates-dir-insert-p))
4454                             (concat default-directory (buffer-substring-no-properties beg end))
4455                           ;; $$$$$$ (buffer-substring-no-properties beg end))))))
4456                           (buffer-substring beg end)))
4457           (remove-text-properties 0 (length choice) '(mouse-face nil) choice))))
4458     (save-window-excursion
4459       (select-window (active-minibuffer-window))
4460       (delete-region (icicle-minibuffer-prompt-end) (point-max))
4461       (insert choice))
4462     (setq icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos posn-pt)
4463           icicle-last-completion-candidate  choice)
4464     (if (not fn-var)
4465         (icicle-help-on-candidate)      ; Doesn't `icicle-raise-Completions-frame'.
4466
4467       ;; NOTE: We no longer save and restore these things here.
4468       ;; We purposely allow an action function to modify these for subsequent actions.
4469       ;; If you need to save and restore these for a particular action function you define,
4470       ;; then you must do so in the action function itself.  This might be the case, for instance,
4471       ;; if your action function does its own completion (e.g. calls `completing-read'), that is, if
4472       ;; it uses a recursive minibuffer.  (But do not save and restore if you want the side effect.)
4473       (let (;; (icicle-candidate-nb               icicle-candidate-nb)              ; $$$$$$
4474             ;; (icicle-last-completion-candidate  icicle-last-completion-candidate) ; $$$$$$
4475             ;; (icicle-completion-candidates      icicle-completion-candidates)     ; $$$$$$
4476             )
4477         (funcall fn-var icicle-last-completion-candidate))
4478       (when icicle-use-candidates-only-once-flag (icicle-remove-candidate-display-others 'all))
4479       (when icicle-completion-candidates (icicle-update-and-next))
4480       (icicle-raise-Completions-frame posn-col posn-row))))
4481
4482
4483 ;; $$$$$ ??? (put 'icicle-remove-candidate 'icicle-action-command t)
4484 ;;;###autoload
4485 (defun icicle-remove-candidate ()       ; Bound to `delete' in minibuffer during completion.
4486   "Remove current completion candidate from the set of candidates.
4487 This has no effect on the object, if any, represented by the
4488 candidate; in particular, that object is not deleted.
4489
4490 Note: For Emacs versions prior to 22, this does not really remove a
4491 file-name candidate as a possible candidate.  If you use \\<minibuffer-local-completion-map>\
4492 \\[icicle-prefix-complete] or \\[icicle-apropos-complete],
4493 it will reappear as a possible candidate.
4494
4495 You can use this command only from the minibuffer (`\\[icicle-remove-candidate]')."
4496   (interactive)
4497   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
4498   (icicle-remove-candidate-display-others))
4499
4500
4501 ;; $$$$$ ??? (put 'icicle-mouse-remove-candidate 'icicle-action-command t)
4502 ;;;###autoload
4503 (defun icicle-mouse-remove-candidate (event) ; Bound to `S-mouse-2' in `*Completions*'.
4504   "Remove clicked completion candidate from the set of candidates.
4505 This has no effect on the object, if any, represented by the
4506 candidate; in particular, that object is not deleted.
4507
4508 See `icicle-remove-candidate' for more information."
4509   (interactive "e")
4510   (run-hooks 'mouse-leave-buffer-hook)  ; Give temp modes such as isearch a chance to turn off.
4511   (let ((posn-buf  (window-buffer (posn-window (event-start event))))
4512         (posn-pt   (posn-point (event-start event)))
4513         beg end)
4514     (read-event)                        ; Swallow mouse up event.
4515     (with-current-buffer posn-buf
4516       (save-excursion
4517         (goto-char posn-pt)
4518         (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
4519           (setq end  (point)
4520                 beg  (1+ (point))))
4521         (unless beg (error "No completion here"))
4522         (setq beg  (previous-single-property-change beg 'mouse-face)
4523               end  (or (next-single-property-change end 'mouse-face) (point-max)))
4524         ;; `icicle-insert-candidates' doesn't put `mouse-face' on the final \n of a candidate
4525         ;; in `*Completions*'. Add the newline back. `icicle-insert-candidates' puts property
4526         ;; `icicle-keep-newline' on the newline if it is part of the candidate, as opposed to
4527         ;; being just part of the display in columns.
4528         (when (and (eq ?\n (char-after end)) (get-text-property end 'icicle-keep-newline))
4529           (setq end  (1+ end)))
4530         (setq icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos posn-pt)
4531               icicle-last-completion-candidate  (buffer-substring beg end)))))
4532   (icicle-remove-candidate-display-others))
4533
4534 (defun icicle-remove-candidate-display-others (&optional allp)
4535   "Remove current completion candidate from list of possible candidates.
4536 Redisplay `*Completions*', unless there is only one candidate left.
4537 Non-nil optional argument ALLP means remove all occurrences of the
4538 current candidate.  Otherwise (nil) means remove only the current
4539 occurrence."
4540   (unless (stringp icicle-last-completion-candidate)
4541     (setq icicle-last-completion-candidate  icicle-current-input
4542           last-command                      'icicle-delete-candidate-object)
4543     (let ((icicle-help-in-mode-line-delay  0)) ; Avoid delay for candidate help.
4544       (icicle-next-candidate 1 (if (eq icicle-current-completion-mode 'prefix)
4545                                    'icicle-prefix-candidates
4546                                  'icicle-apropos-candidates)
4547                              (not (eq icicle-current-completion-mode 'prefix)))))
4548   (let ((maybe-mct-cand  (cond ((consp minibuffer-completion-table)
4549                                 (icicle-mctized-display-candidate icicle-last-completion-candidate))
4550                                ((arrayp minibuffer-completion-table)
4551                                 (intern icicle-last-completion-candidate))
4552                                (t
4553                                 icicle-last-completion-candidate))))
4554     (icicle-remove-cand-from-lists icicle-last-completion-candidate maybe-mct-cand allp))
4555   (icicle-update-and-next))
4556
4557
4558 (put 'icicle-delete-candidate-object 'icicle-action-command t)
4559 ;;;###autoload
4560 (defun icicle-delete-candidate-object (&optional allp) ; Bound to `S-delete' in minibuffer.
4561   "Delete the object named by the current completion candidate.
4562 With a prefix argument, delete *ALL* objects named by the current set
4563 of candidates, after confirmation.
4564
4565 Do nothing if `icicle-deletion-action-flag' is nil.
4566
4567 Otherwise:
4568
4569 * If the value of variable `icicle-delete-candidate-object' is a
4570   function, then apply it to the current completion candidate.  This
4571   should delete some object named by the completion candidate.
4572
4573 * If `icicle-delete-candidate-object' is not a function, then it
4574   should be a symbol bound to an alist.  In this case, invoke
4575   `icicle-delete-candidate-object' to delete the object named by the
4576   current completion candidate from that alist.
4577
4578 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
4579 \\[icicle-delete-candidate-object]')."
4580   (interactive "P")
4581   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
4582   (when icicle-deletion-action-flag
4583     (if (null icicle-completion-candidates)
4584         (message "Nothing to delete - use `S-TAB', `TAB', or a cycle key")
4585       (if allp
4586           (if (not (let ((icicle-completion-candidates  icicle-completion-candidates))
4587                      (yes-or-no-p "Are you SURE you want to DELETE ALL of the matching objects? ")))
4588               (message "OK, nothing deleted")
4589             (dolist (cand icicle-completion-candidates) (icicle-delete-candidate-object-1 cand t))
4590             (icicle-erase-minibuffer))
4591         ;; If no last candidate, then reset to first candidate matching input.
4592         (unless (stringp icicle-last-completion-candidate)
4593           (setq icicle-last-completion-candidate  icicle-current-input
4594                 last-command                      'icicle-delete-candidate-object)
4595           (let ((icicle-help-in-mode-line-delay  0)) ; Avoid delay for candidate help.
4596             (icicle-next-candidate 1 (if (eq icicle-current-completion-mode 'prefix)
4597                                          'icicle-prefix-candidates
4598                                        'icicle-apropos-candidates)
4599                                    (not (eq icicle-current-completion-mode 'prefix)))))
4600         (icicle-delete-candidate-object-1 icicle-last-completion-candidate)))))
4601
4602 (defun icicle-delete-candidate-object-1 (cand &optional no-display-p)
4603   "Helper function for `icicle-delete-candidate-object'.
4604 Delete object named CAND.
4605 Optional arg NO-DISPLAY-P non-nil means don't update `*Completions*'."
4606   (let ((display-cand  cand)            ; Use local vars: values might change.
4607         (maybe-mct-cand
4608          (cond ((consp minibuffer-completion-table) (icicle-mctized-display-candidate cand))
4609                ((arrayp minibuffer-completion-table) (intern cand))
4610                (t cand))))
4611     (save-selected-window
4612       (let ((icicle-completion-candidates  icicle-completion-candidates)) ; In case recursive minibuf.
4613         (if (functionp icicle-delete-candidate-object)
4614             (funcall icicle-delete-candidate-object cand)
4615           (icicle-delete-current-candidate-object cand))))
4616     (icicle-remove-cand-from-lists display-cand maybe-mct-cand nil) ; Use local vars.
4617     (unless no-display-p (message "Deleted object named: `%s'" display-cand) (sit-for 1.0)))
4618   (unless no-display-p (icicle-update-and-next))
4619   (select-window (minibuffer-window))
4620   (select-frame-set-input-focus (selected-frame)))
4621
4622 (defun icicle-delete-current-candidate-object (&optional cand)
4623   "Delete the object(s) corresponding to the current completion candidate.
4624 The value of `icicle-delete-candidate-object' must be a symbol
4625 \(variable) that is bound to a list of completion-candidate objects.
4626
4627 The entries in the list must be completion candidates for the current
4628 call to `completing-read', but the list itself need not be the
4629 COLLECTION argument to `completing-read'.  For example, the list might
4630 be a list of symbols, and the COLLECTION argument might be an obarray
4631 that contains those symbols.
4632
4633 The list can be an alist, a list of strings, or a list of symbols.
4634 Delete, from this list, the objects that correspond to the current
4635 completion candidate.  If the variable is also a user option, then
4636 save the option, after deleting the candidate object.
4637
4638 The full candidate object is what is deleted.  If the list contains
4639 multiple identical objects that correspond to the current completion
4640 candidate, they are all deleted."
4641   (setq cand  (or cand icicle-last-completion-candidate))
4642   (let ((val  (and (symbolp icicle-delete-candidate-object)
4643                    (symbol-value icicle-delete-candidate-object))))
4644     ;; The message could more accurately say "Value of `icicle-delete-candidate-object' must be
4645     ;; a symbol bound to a list", but this makes more sense.
4646     (unless (and val (consp val)) (error "Cannot delete candidate objects now"))
4647     (set icicle-delete-candidate-object ; Update the variable.
4648          (cond ((or icicle-whole-candidate-as-text-prop-p icicle-candidates-alist)
4649                 (delete (funcall icicle-get-alist-candidate-function cand) val))
4650                ((consp (car val))
4651                 (icicle-assoc-delete-all cand val))
4652                ((stringp (car val)) (delete cand val))
4653                ((symbolp (car val)) (delete (intern cand) val))
4654                (t (error "Entry in list value of `icicle-delete-candidate-object' is \
4655 not a cons, string, or symbol")))))
4656   (when (user-variable-p icicle-delete-candidate-object) ; Save the new user-option value.
4657     (funcall icicle-customize-save-variable-function
4658              icicle-delete-candidate-object
4659              (symbol-value icicle-delete-candidate-object))))
4660
4661 (defun icicle-remove-cand-from-lists (disp-cand mct-cand allp)
4662   "Delete first occurence or all occurences of candidate.
4663 The appropriate form of the candidate is removed from each of these:
4664  `icicle-candidates-alist'
4665  `icicle-completion-candidates'
4666  `minibuffer-completion-table' (if it is an alist)
4667 DISP-CAND is the display form of the candidate to delete.
4668 MCT-CAND is the MCT alist candidate that corresponds to DISP-CAND.
4669 If any of these conditions is true, remove all occurrences of CAND:
4670  * ALLP is non-nil
4671  * `icicle-transform-function' is `icicle-remove-duplicates'
4672  * `icicle-transform-function' is `icicle-remove-dups-if-extras'
4673    and `icicle-extra-candidates' is non-nil"
4674   (setq allp  (or allp (eq icicle-transform-function 'icicle-remove-duplicates)
4675                   (and (eq icicle-transform-function 'icicle-remove-dups-if-extras)
4676                        icicle-extra-candidates)))
4677   (when icicle-candidates-alist
4678     (setq icicle-candidates-alist
4679           (if allp
4680               (icicle-assoc-delete-all disp-cand icicle-candidates-alist)
4681             (delete (funcall icicle-get-alist-candidate-function disp-cand) icicle-candidates-alist))))
4682   (when (consp icicle-completion-candidates)
4683     (setq icicle-completion-candidates
4684           (if allp                      ; Delete only the first occurrence, or all if ALLP.
4685               (delete disp-cand icicle-completion-candidates)
4686             (icicle-delete-count disp-cand icicle-completion-candidates 1))))
4687
4688   ;; Update `minibuffer-completion-predicate' or `read-file-name-predicate'
4689   ;; to effectively remove this candidate.
4690   ;; The logic here is the same as for `icicle-narrow-candidates-with-predicate'.
4691   (cond (;; File name input, Emacs 22+.  Update `read-file-name-predicate'.
4692          (and (icicle-file-name-input-p) (> emacs-major-version 21))
4693          (setq read-file-name-predicate
4694                (if read-file-name-predicate
4695                    (lexical-let ((curr-pred  read-file-name-predicate))
4696                      `(lambda (file-cand)
4697                        (and (not (equal ',disp-cand file-cand)) (funcall ',curr-pred file-cand))))
4698                  `(lambda (file-cand) (not (equal ',disp-cand file-cand))))))
4699
4700         ;; File name input, Emacs 20 or 21.  We can do nothing for file name.
4701         ;; `TAB' or `S-TAB' will bring it back as a candidate.
4702         ((icicle-file-name-input-p))
4703
4704         (t;; Non-file name input, all Emacs versions.  Update `minibuffer-completion-predicate'.
4705          (setq minibuffer-completion-predicate
4706                (if minibuffer-completion-predicate
4707                    ;; Add excluding of candidate to the existing predicate.
4708                    (lexical-let ((curr-pred  minibuffer-completion-predicate))
4709                      `(lambda (cand)    ; This corresponds to what we do in `icicle-mctize-all'.
4710                        (and (not (equal cand ',(if (and (consp mct-cand) (stringp (car mct-cand)))
4711                                                    (cdr mct-cand)
4712                                                    mct-cand)))
4713                         (funcall ',curr-pred cand))))
4714                  ;; Set predicate to excluding the candidate.
4715                  `(lambda (cand) (not (equal cand ',(if (and (consp mct-cand) (stringp (car mct-cand)))
4716                                                         (cdr mct-cand)
4717                                                         mct-cand)))))))))
4718
4719 ;; $$$$$$$$$$$$ COULD USE THIS INSTEAD of updating the predicate,
4720 ;; but it works only when `minibuffer-completion-table' is an alist.
4721 ;;   (when (consp minibuffer-completion-table)
4722 ;;     (setq minibuffer-completion-table
4723 ;;           (if allp
4724 ;;               (delete mct-cand minibuffer-completion-table)
4725 ;;             (icicle-delete-count mct-cand minibuffer-completion-table 1)))))
4726
4727 (defun icicle-update-and-next ()
4728   "Update `*Completions*' and make next candidate current.
4729 If we don't know which candidate number this is, just display."
4730   (cond ((and icicle-completion-candidates (cdr icicle-completion-candidates) ; > 1 candidates left.
4731               (not (input-pending-p)))  ; Do nothing if user hit another key.
4732          (icicle-maybe-sort-and-strip-candidates)
4733          (message "Displaying completion candidates...")
4734          (save-selected-window (icicle-display-candidates-in-Completions))
4735          (when (wholenump icicle-candidate-nb)
4736            (with-current-buffer "*Completions*"
4737              (goto-char (icicle-start-of-candidates-in-Completions))
4738              (icicle-move-to-next-completion
4739               (mod icicle-candidate-nb (length icicle-completion-candidates)))
4740              (set-window-point (get-buffer-window "*Completions*" 0) (point))
4741              (setq icicle-last-completion-candidate  (icicle-current-completion-in-Completions))
4742              (set-buffer-modified-p nil))))
4743         (icicle-completion-candidates   ; Single candidate left
4744          (save-selected-window (icicle-remove-Completions-window))
4745          (let ((completion  (icicle-transform-multi-completion (car icicle-completion-candidates))))
4746            (select-window (active-minibuffer-window))
4747            (with-current-buffer (window-buffer) ; Needed if `*Completions*' redirected to minibuffer.
4748              (goto-char (icicle-minibuffer-prompt-end))
4749              (icicle-clear-minibuffer)
4750              (insert (if (and (icicle-file-name-input-p) insert-default-directory
4751                               (or (not (member icicle-current-input icicle-extra-candidates))
4752                                   icicle-extra-candidates-dir-insert-p))
4753                          (icicle-file-name-directory-w-default icicle-current-input)
4754                        "")
4755                      completion))))
4756         (t                              ; No candidates left
4757          ;; $$$$$$$$ `icicle-abort-recursive-edit' and `exit-recursive-edit' don't work,
4758          ;; because they take us back to top level.
4759          ;; $$$$ DO NOTHING? Do (icicle-remove-Completions-window)? Do (icicle-erase-minibuffer)?
4760          (icicle-erase-minibuffer))))
4761
4762 (defun icicle-add/remove-tags-and-refresh (add/remove)
4763   "Prompt for tags to add/remove, then add/remove them.
4764 If ADD/REMOVE is `add', then add tags to the current candidate.
4765 Otherwise, remove them.
4766
4767 If `icicle-full-cand-fn' is non-nil and `icicle-file-name-input-p' is
4768 nil, then update the current candidate to reflect the tag changes.
4769 Then update `*Completions*' and make the next candidate current.
4770
4771 The candidate is updated as follows:
4772 1. Apply `icicle-transform-multi-completion' to it.
4773 2. Apply `icicle-full-cand-fn' to the result of #1.
4774 3. Mctize the result of #2.  That is, make it usable for
4775    `minibuffer-completion-table'."
4776   `(lambda ()
4777     (interactive)
4778     (let ((mct-cand  (icicle-mctized-display-candidate icicle-last-completion-candidate))
4779           (cand      (icicle-transform-multi-completion icicle-last-completion-candidate))
4780           (tags      (let ((enable-recursive-minibuffers  t)) (bmkp-read-tags-completing))))
4781       (funcall ',(if (eq 'add add/remove) 'bmkp-autofile-add-tags 'bmkp-autofile-remove-tags)
4782                cand tags nil nil 'MSG)
4783       (when (and icicle-full-cand-fn  (not (icicle-file-name-input-p)))
4784         (icicle-replace-mct-cand-in-mct
4785          mct-cand
4786          (icicle-mctized-full-candidate (funcall icicle-full-cand-fn cand)))
4787         (icicle-update-and-next)))))
4788
4789
4790 (put 'icicle-mouse-help-on-candidate 'icicle-action-command t)
4791 ;;;###autoload
4792 (defun icicle-mouse-help-on-candidate (event) ; Bound to `C-M-mouse-2' in minibuffer.
4793   "Display help on the minibuffer-completion candidate clicked by mouse."
4794   (interactive "e")
4795   (let ((icicle-candidate-action-fn  nil)) (icicle-mouse-candidate-action event)))
4796
4797
4798 ;; Free vars here: `icicle-orig-buff' is bound in `icicle-complete-keys'.
4799 ;;                 `icicle-complete-keys-alist' is bound in `icicles-var.el'.
4800 (put 'icicle-help-on-candidate 'icicle-action-command t)
4801 ;;;###autoload
4802 (defun icicle-help-on-candidate (&optional cand) ; Bound to `C-M-RET', `C-help', `C-f1' in minibuffer,
4803                                         ; and to `C-M-RET' in *Completions.
4804   "Display help on the current minibuffer-completion candidate.
4805 The help displayed depends on the type of candidate, as follows:
4806
4807  menu item - the corresponding command is described using
4808              `describe-function' (only if `lacarte.el' is loaded)
4809  command or other function - described using `describe-function'
4810  keymap variable - described using `describe-keymap'
4811                    (if available - see library `help-fns+.el')
4812  user option or other variable - described using `describe-variable'
4813  face - described using `describe-face'
4814  command abbreviation - described using `apropos-command' for matches
4815  property list - described using `apropos-describe-plist'
4816  buffer name - modes described using `describe-mode' (Emacs > 20)
4817  file name - file properties described
4818
4819 If the same candidate names a function, a variable, and a face, or any
4820 two of these, then all such documentation is shown (Emacs 22+).
4821
4822 In the minibuffer, you can also use `C-M-down', `C-M-up',
4823 `C-M-wheel-down', `C-M-wheel-up', `C-M-next', `C-M-prior', `C-M-end',
4824 and `C-M-home', to display help on the candidate and then move to the
4825 next or previous candidate.  See, for example,
4826 `icicle-help-on-next-apropos-candidate'.
4827
4828 You can use this command only from the minibuffer or `*Completions*'
4829 \(`\\[icicle-help-on-candidate]')."
4830   (interactive)                         ; Interactively, just describes itself.
4831   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
4832   (let ((frame-with-focus  (selected-frame))
4833         (cand-symb         nil)
4834         transformed-cand)
4835     (cond (cand (setq icicle-last-completion-candidate  cand))
4836           ((eq (current-buffer) (get-buffer "*Completions*"))
4837            (setq icicle-last-completion-candidate  (icicle-current-completion-in-Completions)))
4838           ;; If no last candidate, then reset to first candidate matching input.
4839           ((not (stringp icicle-last-completion-candidate))
4840            (setq icicle-last-completion-candidate  icicle-current-input
4841                  last-command                      'icicle-help-on-candidate)
4842            (let ((icicle-help-in-mode-line-delay  0)) ; Avoid delay for candidate help.
4843              (icicle-next-candidate 1 (if (eq icicle-current-completion-mode 'prefix)
4844                                           'icicle-prefix-candidates
4845                                         'icicle-apropos-candidates)
4846                                     (not (eq icicle-current-completion-mode 'prefix))))))
4847     (cond (;; Use special help function.
4848            icicle-candidate-help-fn
4849            ;; Transform candidate, in case it's a multi-completion.
4850            (funcall icicle-candidate-help-fn
4851                     (icicle-transform-multi-completion icicle-last-completion-candidate)))
4852
4853           (;; Call to `lacarte-execute(-menu)-command' (defined in `lacarte.el').
4854            ;; Use command associated with menu item.
4855            (consp lacarte-menu-items-alist) ; `lacarte-menu-items-alist' is in `lacarte.el'.
4856            (setq cand-symb  (cdr (assoc icicle-last-completion-candidate lacarte-menu-items-alist)))
4857            (if cand-symb
4858                (icicle-help-on-candidate-symbol cand-symb)
4859              (icicle-msg-maybe-in-minibuffer "No help"))) ; Menu item with lambda definition.
4860
4861           (;; A key-completion candidate.  Get the true command from the candidate.
4862            icicle-completing-keys-p
4863            (save-match-data
4864              (string-match "\\(.+\\)  =  \\(.+\\)" icicle-last-completion-candidate)
4865              (setq cand-symb  (intern-soft (substring icicle-last-completion-candidate
4866                                                       (match-beginning 2) (match-end 2))))
4867              (cond ((eq '\.\.\. cand-symb) ; Prefix key - describe its binding.
4868                     (with-current-buffer icicle-orig-buff
4869                       (describe-key (car-safe
4870                                      (cdr-safe
4871                                       (assq (intern-soft
4872                                              (substring icicle-last-completion-candidate
4873                                                         (match-beginning 0) (match-end 0)))
4874                                             icicle-complete-keys-alist))))))
4875                    (cand-symb (icicle-help-on-candidate-symbol cand-symb)) ; Describe key's command.
4876                    (t (icicle-msg-maybe-in-minibuffer "No help")))))
4877
4878           (t;; Transform candidate, in case it's a multi-completion.
4879            (setq transformed-cand  (icicle-transform-multi-completion icicle-last-completion-candidate))
4880            ;; If buffer or file, describe its properties.  Otherwise, create symbol and get its help.
4881            (cond ((and (bufferp (get-buffer transformed-cand))
4882                        (with-current-buffer transformed-cand (describe-mode) t)))
4883                  ((file-exists-p transformed-cand) (icicle-describe-file transformed-cand
4884                                                                          current-prefix-arg))
4885                  (t (icicle-help-on-candidate-symbol (intern transformed-cand))))))
4886     ;;$$$ (icicle-raise-Completions-frame)
4887
4888     ;; This is a hack for MS Windows - otherwise, we can't continue to get more candidates,
4889     ;; because the *Help* frame takes the focus away from the minibuffer frame.
4890     ;; MS Windows always gives focus to a newly created frame - in this case, *Help*.
4891     (let* ((help-window  (get-buffer-window "*Help*" 0))
4892            (help-frame   (and help-window (window-frame help-window))))
4893       (when help-frame (redirect-frame-focus help-frame frame-with-focus))))
4894   (message nil))                        ; Let minibuffer contents show immediately.
4895
4896 (defun icicle-help-on-candidate-symbol (symb)
4897   "Helper function for `icicle-help-on-candidate'.  The arg is a symbol."
4898   (cond ((and (fboundp 'describe-keymap) (boundp symb) (keymapp (symbol-value symb)))
4899          (describe-keymap symb))
4900         ((and (fboundp 'help-follow-symbol) ; Emacs 22+
4901               (or (fboundp symb) (boundp symb) (facep symb)))
4902          (with-current-buffer (get-buffer-create "*Help*")
4903            ;; $$$$$$ (let ((help-xref-following  t)) (help-xref-interned symb)))
4904            (help-xref-interned symb))
4905          (when (fboundp 'fit-frame-if-one-window)
4906            (save-selected-window (select-window (get-buffer-window "*Help*" 'visible))
4907                                  (fit-frame-if-one-window))))
4908         ((fboundp symb) (describe-function symb))
4909         ((boundp symb) (describe-variable symb))
4910         ((facep symb) (describe-face symb))
4911         ((assq symb (mapcar #'cdr icicle-command-abbrev-alist))
4912          (let ((regexp  (icicle-command-abbrev-regexp symb))) (apropos-command regexp)))
4913         ((symbol-plist symb) (apropos-describe-plist symb))
4914         (t
4915          (setq symb  (symbol-name symb)) ; Convert symbol to string, and try some more.
4916          (cond ((and (bufferp (get-buffer symb))
4917                      (with-current-buffer (get-buffer symb) (describe-mode) t)))
4918                ((file-exists-p symb) (icicle-describe-file symb current-prefix-arg))
4919                (t (icicle-msg-maybe-in-minibuffer "No help"))))))
4920
4921 ;; This is the same as `describe-file' in `help-fns+.el', but we avoid requiring that library.
4922 ;; This is a top-level command, but we put it here to avoid library require cycles.
4923 (if (and (not (fboundp 'icicle-describe-file)) (fboundp 'describe-file))
4924     (defalias 'icicle-describe-file (symbol-function 'describe-file))
4925   (defun icicle-describe-file (filename &optional internal-form-p) ; Suggestion: bind to `C-h M-f'.
4926     "Describe the file named FILENAME.
4927 If FILENAME is nil, describe current directory (`default-directory').
4928
4929 Starting with Emacs 22, if the file is an image file then:
4930  * Show a thumbnail of the image as well.
4931  * If you have command-line tool `exiftool' installed and in your
4932    `$PATH' or `exec-path', then show EXIF data (metadata) about the
4933    image.  See standard Emacs library `image-dired.el' for more
4934    information about `exiftool'.
4935
4936 If FILENAME is the name of an autofile bookmark and you use library
4937 `Bookmark+', then show also the bookmark information (tags etc.).  In
4938 this case, a prefix arg shows the internal form of the bookmark."
4939     (interactive "FDescribe file: \nP")
4940     (unless filename (setq filename default-directory))
4941     (help-setup-xref `(icicle-describe-file ,filename ,internal-form-p) (interactive-p))
4942     (let ((attrs (file-attributes filename))
4943           ;; Functions `bmkp-*' are defined in `bookmark+.el'.
4944           (bmk   (and (fboundp 'bmkp-get-autofile-bookmark)  (bmkp-get-autofile-bookmark filename))))
4945       (unless attrs (error "Cannot open file `%s'" filename))
4946       (let* ((type            (nth 0 attrs))
4947              (numlinks        (nth 1 attrs))
4948              (uid             (nth 2 attrs))
4949              (gid             (nth 3 attrs))
4950              (last-access     (nth 4 attrs))
4951              (last-mod        (nth 5 attrs))
4952              (last-status-chg (nth 6 attrs))
4953              (size            (nth 7 attrs))
4954              (permissions     (nth 8 attrs))
4955              ;; Skip 9: t iff file's gid would change if file were deleted and recreated.
4956              (inode           (nth 10 attrs))
4957              (device          (nth 11 attrs))
4958              (thumb-string    (and (fboundp 'image-file-name-regexp) ; In `image-file.el' (Emacs 22+).
4959                                    (if (fboundp 'string-match-p)
4960                                        (string-match-p (image-file-name-regexp) filename)
4961                                      (save-match-data
4962                                        (string-match (image-file-name-regexp) filename)))
4963                                    (if (fboundp 'display-graphic-p) (display-graphic-p) window-system)
4964                                    (require 'image-dired nil t)
4965                                    (image-dired-get-thumbnail-image filename)
4966                                    (apply #'propertize "XXXX"
4967                                           `(display ,(append (image-dired-get-thumbnail-image filename)
4968                                                              '(:margin 10))
4969                                             rear-nonsticky (display)
4970                                             mouse-face highlight
4971                                             follow-link t
4972                                             help-echo "`mouse-2' or `RET': Show full image"
4973                                             keymap
4974                                             (keymap
4975                                              (mouse-2 . (lambda (e) (interactive "e")
4976                                                                 (find-file ,filename)))
4977                                              (13 . (lambda () (interactive)
4978                                                            (find-file ,filename))))))))
4979              (image-info      (and (require 'image-dired nil t)
4980                                    (fboundp 'image-file-name-regexp)
4981                                    (if (fboundp 'string-match-p)
4982                                        (string-match-p (image-file-name-regexp) filename)
4983                                      (save-match-data
4984                                        (string-match (image-file-name-regexp) filename)))
4985                                    (progn (message "Gathering image data...") t)
4986                                    (icicle-condition-case-no-debug nil
4987                                        (let ((all  (icicle-all-exif-data (expand-file-name filename))))
4988                                          (concat
4989                                           (and all (not (zerop (length all)))
4990                                                (format "\nImage Data (EXIF)\n-----------------\n%s"
4991                                                        all))))
4992                                      (error nil))))
4993              (help-text
4994               (concat
4995                (format "`%s'\n%s\n\n" filename (make-string (+ 2 (length filename)) ?-))
4996                (format "File Type:                       %s\n"
4997                        (cond ((eq t type) "Directory")
4998                              ((stringp type) (format "Symbolic link to `%s'" type))
4999                              (t "Normal file")))
5000                (format "Permissions:                %s\n" permissions)
5001                (and (not (eq t type)) (format "Size in bytes:              %g\n" size))
5002                (format-time-string
5003                 "Time of last access:        %a %b %e %T %Y (%Z)\n" last-access)
5004                (format-time-string
5005                 "Time of last modification:  %a %b %e %T %Y (%Z)\n" last-mod)
5006                (format-time-string
5007                 "Time of last status change: %a %b %e %T %Y (%Z)\n" last-status-chg)
5008                (format "Number of links:            %d\n" numlinks)
5009                (format "User ID (UID):              %s\n" uid)
5010                (format "Group ID (GID):             %s\n" gid)
5011                (format "Inode:                      %S\n" inode)
5012                (format "Device number:              %s\n" device)
5013                image-info)))
5014         (with-output-to-temp-buffer "*Help*"
5015           (when bmk (if internal-form-p
5016               (let* ((bname     (bookmark-name-from-full-record bmk))
5017                      (bmk-defn  (format "Bookmark `%s'\n%s\n\n%s"
5018                                         bname   (make-string (+ 11 (length bname)) ?-)
5019                                         (pp-to-string bmk))))
5020                 (princ bmk-defn) (terpri) (terpri))
5021             (princ (bmkp-bookmark-description bmk 'NO-IMAGE)) (terpri) (terpri)))
5022         (princ help-text))
5023       (when thumb-string
5024         (with-current-buffer "*Help*"
5025           (save-excursion
5026             (goto-char (point-min))
5027             (let ((buffer-read-only  nil))
5028               (when (re-search-forward "Device number:.+\n" nil t) (insert thumb-string))))))
5029       help-text))))                   ; Return displayed text.
5030
5031 ;; This is the same as `help-all-exif-data' in `help-fns+.el', but we avoid requiring that library.
5032 (defun icicle-all-exif-data (file)
5033   "Return all EXIF data from FILE, using command-line tool `exiftool'."
5034   (with-temp-buffer
5035     (delete-region (point-min) (point-max))
5036     (unless (eq 0 (call-process shell-file-name nil t nil shell-command-switch
5037                                 (format "exiftool -All \"%s\"" file)))
5038       (error "Could not get EXIF data"))
5039     (buffer-substring (point-min) (point-max))))
5040
5041 ;;;###autoload
5042 (defun icicle-candidate-read-fn-invoke () ; Bound to `M-RET' in minibuffer.
5043   "Read function name.  Invoke function on current completion candidate.
5044 Set `icicle-candidate-action-fn' to the interned name.
5045
5046 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5047 \\[icicle-candidate-read-fn-invoke]')."
5048   (interactive)
5049   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5050   ;; If no last candidate, then reset to first candidate matching input.
5051   (unless (stringp icicle-last-completion-candidate)
5052     (setq icicle-last-completion-candidate  icicle-current-input
5053           last-command                      'icicle-candidate-action)
5054     (let ((icicle-help-in-mode-line-delay  0)) ; Avoid delay for candidate help.
5055       (icicle-next-candidate 1 (if (eq icicle-current-completion-mode 'prefix)
5056                                    'icicle-prefix-candidates
5057                                  'icicle-apropos-candidates)
5058                              (not (eq icicle-current-completion-mode 'prefix)))))
5059   (let ((icicle-whole-candidate-as-text-prop-p  nil)
5060         (enable-recursive-minibuffers            t)
5061         (icicle-must-pass-after-match-predicate  #'(lambda (s) (functionp (intern s))))
5062         (icicle-saved-completion-candidate       icicle-last-completion-candidate)
5063         (icicle-candidate-action-fn              'icicle-apply-to-saved-candidate))
5064     (icicle-apply-to-saved-candidate
5065      (completing-read (format "Function to apply to `%s': " icicle-saved-completion-candidate)
5066                       obarray))))
5067
5068 ;;;###autoload
5069 (defun icicle-mouse-candidate-read-fn-invoke (event) ; Bound to `M-mouse-2' in `*Completions*'.
5070   "Read function name.  Invoke function on candidate clicked by mouse."
5071   (interactive "e")
5072   (run-hooks 'mouse-leave-buffer-hook)  ; Give temp modes such as isearch a chance to turn off.
5073   (let (;;$$$$$$ (buffer    (window-buffer))
5074         (posn-win  (posn-window (event-start event)))
5075         (posn-col  (car (posn-col-row (event-start event))))
5076         (posn-row  (cdr (posn-col-row (event-start event))))
5077         choice base-size)
5078     ;; (read-event)                 ; Swallow mouse up event. $$ Not needed if bound to up event.
5079     (with-current-buffer (window-buffer posn-win)
5080       (save-excursion
5081         ;; $$$$$$ (when completion-reference-buffer (setq buffer  completion-reference-buffer))
5082         (setq base-size  completion-base-size)
5083         (goto-char (posn-point (event-start event)))
5084         (let (beg end)
5085           (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
5086             (setq end  (point)
5087                   beg  (1+ (point))))
5088           (unless beg (error "No completion here"))
5089           (setq beg     (previous-single-property-change beg 'mouse-face)
5090                 end     (or (next-single-property-change end 'mouse-face)(point-max))
5091                 choice  (buffer-substring-no-properties beg end)))))
5092     (setq icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos
5093                                              (posn-point (event-start event)))
5094           icicle-last-completion-candidate  choice)
5095     (let ((icicle-whole-candidate-as-text-prop-p  nil)
5096           (enable-recursive-minibuffers            t)
5097           (icicle-must-pass-after-match-predicate  #'(lambda (s) (functionp (intern s))))
5098           (icicle-saved-completion-candidate       icicle-last-completion-candidate)
5099           (icicle-candidate-action-fn              'icicle-apply-to-saved-candidate))
5100       (icicle-apply-to-saved-candidate
5101        (completing-read (format "Function to apply to `%s': " icicle-saved-completion-candidate)
5102                         obarray)))))
5103
5104 (defun icicle-apply-to-saved-candidate (function &optional use-icicle-candidates-alist-p type)
5105   "Apply FUNCTION to `icicle-saved-completion-candidate'.
5106 If `current-prefix-arg' is non-nil, then pretty-print the result using
5107 `icicle-pp-eval-expression'.
5108 The string FUNCTION is read to obtain the real function to apply.
5109 If optional arg USE-ICICLE-CANDIDATES-ALIST-P is non-nil, then try to
5110 get the real function using `icicle-get-alist-candidate-function'.
5111 If that returns nil, then read string FUNCTION.
5112 Optional arg TYPE is the type of object that FUNCTION applies to."
5113   (let ((real-fn   (or (and use-icicle-candidates-alist-p
5114                             (cdr (funcall icicle-get-alist-candidate-function
5115                                           function 'no-error-no-msg)))
5116                        (car (read-from-string function))))
5117         (real-obj  (if (equal type "buffer") ; $$$$$$$ Eventually, perhaps look up TYPE in a list etc.
5118                        (get-buffer icicle-saved-completion-candidate)
5119                      icicle-saved-completion-candidate)))
5120     ;; Actually, we should test more than `functionp', to rule out macros and special forms.
5121     (unless (functionp real-fn) (error "Not a function: `%S'" real-fn))
5122     (icicle-condition-case-no-debug icicle-apply-to-saved-candidate
5123         (if current-prefix-arg
5124             (icicle-pp-eval-expression '(funcall real-fn real-obj))
5125           (funcall real-fn real-obj)
5126           (when (and (not icicle-all-candidates-action) (current-message))
5127             (sit-for 3)))               ; In case the function displays a message.
5128       (error (message  "ERROR invoking `%S' on `%s': %s" real-fn icicle-saved-completion-candidate
5129                        (error-message-string icicle-apply-to-saved-candidate))
5130              (sleep-for 6)))
5131     (select-window (minibuffer-window))
5132     (select-frame-set-input-focus (selected-frame))
5133     (icicle-raise-Completions-frame)))
5134
5135 (defun icicle-raise-Completions-frame (&optional mouse-col mouse-row)
5136   "Raise `*Completions*' frame, if displayed.
5137 This helps keep `*Completions*' on top.
5138
5139 If `icicle-move-Completions-frame' is non-nil and `*Completions*' is
5140 in its own frame, then move that frame to the display edge, out of the
5141 way.
5142
5143 Non-nil optional args MOUSE-COL and MOUSE-ROW move the mouse pointer
5144 to column MOUSE-COL and row MOUSE-ROW.  Do this because
5145 `icicle-candidate-action-fn' can call `select-frame-set-input-focus',
5146 which can position mouse pointer on a standalone minibuffer frame."
5147   ;; Raise `*Completions*' frame, if displayed.  This helps keep `*Completions*' on top.
5148   (let ((compl-win  (get-buffer-window "*Completions*" 'visible)))
5149     (when compl-win
5150       (save-window-excursion
5151         (select-window compl-win)
5152         ;; Move frame to the right, out of the way.
5153         (when (and (one-window-p t) icicle-move-Completions-frame)
5154           (modify-frame-parameters
5155            (selected-frame)             ; Hard-code 7 here - what does it depend on?
5156            (if (eq icicle-move-Completions-frame 'left)
5157                '((left . 0))
5158              `((left . ,(- (x-display-pixel-width) (+ (frame-pixel-width) 7))))))
5159           (raise-frame)
5160           (when (and (integerp mouse-col) (integerp mouse-row))
5161             (set-mouse-position (selected-frame) mouse-col mouse-row)))))))
5162
5163 ;;;###autoload
5164 (defun icicle-Completions-mouse-3-menu (event) ; Bound to `C-mouse-3' in `*Completions*'.
5165   "Pop-up menu on `C-mouse-3' for the current candidate in `*Completions*'."
5166   (interactive "e")
5167   (run-hooks 'mouse-leave-buffer-hook)  ; Give temp modes such as isearch a chance to turn off.
5168   (let (;; $$$$$$ (buffer    (window-buffer))
5169         (posn-win  (posn-window (event-start event)))
5170         (posn-col  (car (posn-col-row (event-start event))))
5171         (posn-row  (cdr (posn-col-row (event-start event))))
5172         candidate base-size)
5173     ;; (read-event)                 ; Swallow mouse up event. $$ Not needed if bound to up event.
5174     (with-current-buffer (window-buffer posn-win)
5175       (save-excursion
5176         ;; $$$$$$ (when completion-reference-buffer (setq buffer  completion-reference-buffer))
5177         (setq base-size  completion-base-size)
5178         (goto-char (posn-point (event-start event)))
5179         (let (beg end)
5180           (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
5181             (setq end  (point)
5182                   beg  (1+ (point))))
5183           (unless beg (error "No completion here"))
5184           (setq beg       (previous-single-property-change beg 'mouse-face)
5185                 end       (or (next-single-property-change end 'mouse-face)(point-max))
5186                 candidate (buffer-substring-no-properties beg end)))))
5187     (setq icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos
5188                                              (posn-point (event-start event)))
5189           icicle-last-completion-candidate  candidate)
5190     (let* ((menus   `((keymap "Completion" ,@(icicle-substitute-keymap-vars
5191                                               icicle-Completions-mouse-3-menu-entries))))
5192            (choice  (x-popup-menu event menus)))
5193       (icicle-Completions-popup-choice menus choice))))
5194
5195 (defun icicle-substitute-keymap-vars (menu-entries)
5196   "In MENU-ENTRIES, replace keymap vars by their values."
5197   (let ((new  ()))
5198     (dolist (jj  menu-entries)
5199       (cond ((and (symbolp jj) (keymapp (symbol-value jj))) ; Just a keymap var.
5200              (setq jj  (symbol-value jj))
5201              (dolist (ii  jj) (push ii new)))
5202             ;; (SYMBOL menu-item NAME MENU-KEYMAP . KEYWORDS), with a keymap var.
5203             ((and (consp jj) (symbolp (car jj)) (eq 'menu-item (cadr jj))
5204                   (stringp (car (cddr jj))) (symbolp (car (cdr (cddr jj))))
5205                   (not (commandp (car (cdr (cddr jj))))) (boundp (car (cdr (cddr jj))))
5206                   (keymapp (symbol-value (car (cdr (cddr jj))))))
5207              (setq jj  `(,(car jj) menu-item ,(car (cddr jj))
5208                          ,(symbol-value (car (cdr (cddr jj)))) ; Replace keymap var by its value.
5209                          ,@(cdr (cdr (cddr jj))))) ; Keywords.
5210              (push jj new))
5211             ((and (consp jj) (symbolp (car jj)) (stringp (cadr jj)) ; (SYMBOL NAME . MENU-KEYMAP)
5212                   (symbolp (cddr jj)) (boundp (cddr jj)) (keymapp (symbol-value (cddr jj))))
5213              (setq jj  `(,(car jj) ,(cadr jj) ,@(symbol-value (cddr jj)))) ; Replace keymap var by val.
5214              (push jj new))
5215             (t (push jj new))))
5216     (nreverse new)))
5217
5218 ;; This is the same as `mouse3-region-popup-choice' in `mouse3.el'.
5219 (if (require 'mouse3 nil t)
5220     (defalias 'icicle-Completions-popup-choice 'mouse3-region-popup-choice)
5221   (defun icicle-Completions-popup-choice (menus choice)
5222     "Invoke the command from MENUS that is represented by user's CHOICE.
5223 MENUS is a list that is acceptable as the second argument for
5224 `x-popup-menu'.  That is, it is one of the following, where MENU-TITLE
5225 is the menu title and PANE-TITLE is a submenu title.
5226
5227 * a keymap - MENU-TITLE is its `keymap-prompt'
5228 * a list of keymaps - MENU-TITLE is the first keymap's `keymap-prompt'
5229 * a menu of multiple panes, which has this form: (MENU-TITLE PANE...),
5230   where each PANE has this form: (PANE-TITLE ITEM...),
5231   where each ITEM has one of these forms:
5232   - STRING - an unselectable menu item
5233   - (STRING . COMMAND) - a selectable item that invokes COMMAND"
5234     (catch 'icicle-Completions-popup-choice (icicle-Completions-popup-choice-1 menus choice))))
5235
5236 ;; This is the same as `mouse3-region-popup-choice-1' in `mouse3.el'.
5237 (if (require 'mouse3 nil t)
5238     (defalias 'icicle-Completions-popup-choice-1 'mouse3-region-popup-choice-1)
5239   (defun icicle-Completions-popup-choice-1 (menus choice)
5240     "Helper function for `icicle-Completions-popup-choice'."
5241     (cond((keymapp menus)
5242           ;; Look up each ITEM-LIST entry in keymap MENUS.
5243           ;;   If what is found is a keymap, use that as MENUS for next iteration.
5244           ;;   If what is found is a command, invoke it (done).
5245           (let (binding)
5246             (while choice
5247               (setq binding  (lookup-key menus (vector (car choice))))
5248               (cond ((keymapp binding)
5249                      (setq menus   binding
5250                            choice  (cdr choice)))
5251                     ((commandp binding)
5252                      ;; You get only one.
5253                      (throw 'icicle-Completions-popup-choice (call-interactively binding)))
5254                     (t (error "`icicle-Completions-popup-choice', binding: %s" binding))))))
5255          ((consp menus)                 ; A list of keymaps or panes.
5256           (dolist (menu  menus)
5257             (if (keymapp menu)
5258                 (icicle-Completions-popup-choice-1 menu choice)
5259               (when choice              ; MENU is a pane.
5260                 (throw 'icicle-Completions-popup-choice (call-interactively choice)))))))))
5261
5262 ;;;###autoload
5263 (defun icicle-widen-candidates ()       ; Bound to `M-+' in minibuffer.
5264   "Complete, allowing also candidates that match an alternative regexp.
5265 You are prompted for the alternative input pattern.  Use `RET' to
5266 enter it.
5267
5268 To (apropos) complete using a wider set of candidates, you use this
5269 command after you have completed (`TAB' or `S-TAB').  A shortcut is to
5270 use `\\<minibuffer-local-completion-map>\\[icicle-apropos-complete-and-widen]' - \
5271 it is the same as `S-TAB' followed by `\\[icicle-widen-candidates]'.
5272
5273 This command turns off `icicle-expand-input-to-common-match-flag', for
5274 clarity.  You can use `\\[icicle-toggle-expand-to-common-match]' \
5275 to toggle that option."
5276   (interactive)
5277   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
5278   (unless icicle-completion-candidates
5279     (error "No completion candidates.  Did you use `TAB' or `S-TAB'?"))
5280   (let* ((raw-input                     icicle-current-raw-input)
5281          (enable-recursive-minibuffers  t)
5282          (new-regexp                    (icicle-read-string "Or match alternative (use RET): "
5283                                                             nil regexp-history)))
5284     (setq icicle-current-raw-input
5285           (concat (if (< emacs-major-version 22) "\\(" "\\(?:") raw-input "\\|" new-regexp "\\)")
5286           icicle-expand-input-to-common-match-flag  nil))
5287   (icicle-clear-minibuffer)
5288   (insert icicle-current-raw-input)
5289   (let ((icicle-edit-update-p  t)) (icicle-apropos-complete))
5290   (icicle-msg-maybe-in-minibuffer "Expansion to common match is OFF. \
5291 `\\<minibuffer-local-completion-map>\\[icicle-toggle-expand-to-common-match]' to toggle"))
5292
5293 ;;;###autoload
5294 (defun icicle-narrow-candidates ()      ; Bound to `M-*' in minibuffer.
5295   "Narrow the set of completion candidates using another input regexp.
5296 This, in effect, performs a set intersection operation on 1) the set
5297 of candidates in effect before the operation and 2) the set of
5298 candidates that match the current input.  You can repeatedly use this
5299 command to continue intersecting candidate sets, progressively
5300 narrowing the set of matches.
5301
5302 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5303 \\[icicle-narrow-candidates]')."
5304   ;; We handle `no-catch' errors here because `old-completing-read' and
5305   ;; `old-read-file-file-name' can still be called in Icicle mode by, for instance, an
5306   ;; `interactive' spec (e.g. (interactive "bBuffer: ")).  In that case, we throw to a
5307   ;; non-existant catch.  After doing that, we just insert the result, to pass it to the
5308   ;; next-higher recursive minibuffer.
5309   (interactive)
5310   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
5311   (setq icicle-current-completion-mode  'apropos)
5312   (let (;; Restore match function, in case it was bound to nil, e.g., by `C-h C-o'.
5313         (icicle-apropos-complete-match-fn  icicle-last-apropos-complete-match-fn)
5314         (icicle-progressive-completing-p   t) ; Inhibit completion by `icicle-minibuffer-setup'.
5315         (enable-recursive-minibuffers      t))
5316     (cond ((and icicle-completion-candidates  (null (cdr icicle-completion-candidates)))
5317            (if (not (and icicle-top-level-when-sole-completion-flag
5318                          (sit-for icicle-top-level-when-sole-completion-delay)))
5319                (minibuffer-message "  [Sole completion]")
5320              (set minibuffer-history-variable (cons (car icicle-completion-candidates)
5321                                                     (symbol-value minibuffer-history-variable)))
5322              ;; $$$$$$ Should this now use `icicle-current-input'
5323              ;;        instead of (car icicle-completion-candidates), for PCM?
5324              (icicle-condition-case-no-debug i-narrow-candidates
5325                  (throw 'icicle-read-top
5326                    (if (and (icicle-file-name-input-p) insert-default-directory
5327                             (or (not (member (car icicle-completion-candidates)
5328                                              icicle-extra-candidates))
5329                                 icicle-extra-candidates-dir-insert-p))
5330                        (expand-file-name (car icicle-completion-candidates))
5331                      (car icicle-completion-candidates)))
5332                (no-catch (setq icicle-current-input  (car icicle-completion-candidates))
5333                          (icicle-retrieve-last-input)
5334                          icicle-current-input)
5335                (error (message "%s" (error-message-string i-narrow-candidates))))))
5336           (t
5337            (let* (;; $$$$$$$$$$$$$ (icicle-whole-candidate-as-text-prop-p  nil)
5338                   (minibuffer-setup-hook ; Make sure the new minibuffer is the reference buffer.
5339                    (cons
5340                     (lambda ()
5341                       (with-current-buffer (get-buffer-create "*Completions*")
5342                         (set (make-local-variable 'completion-reference-buffer)
5343                              (window-buffer (active-minibuffer-window)))))
5344                     minibuffer-setup-hook))
5345                   (current-candidates  icicle-completion-candidates)
5346                   (result
5347                    (cond ((and (icicle-file-name-input-p)
5348                                (or (= emacs-major-version 22) ; Emacs 22 or 23.1
5349                                    (and (= emacs-major-version 23) (= emacs-minor-version 1))))
5350                           (read-file-name "Match also (regexp): "
5351                                           (icicle-file-name-directory-w-default icicle-current-input)
5352                                           nil icicle-require-match-p nil
5353                                           (lambda (file-cand) (member file-cand current-candidates))))
5354                          ((and (icicle-file-name-input-p) (> emacs-major-version 22)) ; Emacs 23.2+
5355                           (let ((icicle-must-pass-after-match-predicate
5356                                  #'(lambda (c) (member c current-candidates))))
5357                             (completing-read "Match also (regexp): " 'read-file-name-internal nil
5358                                              icicle-require-match-p nil minibuffer-history-variable)))
5359                          (t             ; Emacs 20, 21
5360                           ;; In Emacs < 22, there is no PREDICATE arg to `read-file-name', so
5361                           ;; we use `completing-read' even for file-name completion.  In that case, we
5362                           ;; tack the `default-directory' onto each candidate, unless it is already an
5363                           ;; absolute file name.  We also let completion functions (e.g. `S-TAB') know
5364                           ;; that this is not really file-name completion.
5365                           (completing-read
5366                            "Match also (regexp): "
5367                            (cond ((icicle-file-name-input-p)
5368                                   (setq minibuffer-completing-file-name  nil) ; Disavow completing file.
5369                                   (let ((dir  (icicle-file-name-directory-w-default
5370                                                icicle-current-input)))
5371                                     (mapcar (lambda (file)
5372                                               (list (if (file-name-absolute-p file)
5373                                                         file
5374                                                       (concat dir file))))
5375                                             icicle-completion-candidates)))
5376                                  (icicle-whole-candidate-as-text-prop-p
5377                                   (mapcar (lambda (cand)
5378                                             (funcall icicle-get-alist-candidate-function (car cand)))
5379                                           (icicle-filter-alist minibuffer-completion-table
5380                                                                icicle-completion-candidates)))
5381                                  (t
5382                                   (mapcar #'list icicle-completion-candidates)))
5383                            nil icicle-require-match-p nil minibuffer-history-variable)))))
5384              ;; Normally, `icicle-narrow-candidates' is called from the minibuffer.
5385              ;; If not, just return the result read.
5386              (if (> (minibuffer-depth) 0)
5387                  (icicle-condition-case-no-debug i-narrow-candidates
5388                      (throw 'icicle-read-top result)
5389                    (no-catch (setq icicle-current-input  result)
5390                              (icicle-retrieve-last-input)
5391                              icicle-current-input)
5392                    (error (message "%s" (error-message-string i-narrow-candidates))))
5393                result))))))
5394
5395 ;;;###autoload
5396 (defun icicle-apropos-complete-and-widen () ; Bound to `S-DEL' in minibuffer.
5397   "Apropos complete, then `icicle-widen-candidates'.
5398 You must enter the new, alternative input pattern using `RET'.
5399
5400 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5401 \\[icicle-apropos-complete-and-widen]')."
5402   (interactive)
5403   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
5404   ;; $$$$$ (let ((icicle-top-level-when-sole-completion-flag  t))
5405   (when (eq icicle-current-completion-mode 'prefix)
5406     (let ((icicle-incremental-completion-p  nil)
5407           (regexp-quoted-input              (regexp-quote icicle-last-input)))
5408       (setq regexp-quoted-input  (if (icicle-file-name-input-p)
5409                                      (concat (icicle-file-name-directory regexp-quoted-input) "^"
5410                                              (file-name-nondirectory regexp-quoted-input))
5411                                    (concat "^" regexp-quoted-input)))
5412       (icicle-erase-minibuffer)
5413       (insert regexp-quoted-input)))
5414   (if (eq icicle-last-completion-command 'icicle-apropos-complete-no-display)
5415       (icicle-apropos-complete-no-display)
5416     (icicle-apropos-complete))
5417   (icicle-widen-candidates))
5418
5419 ;;;###autoload
5420 (defun icicle-apropos-complete-and-narrow () ; Bound to `S-SPC' in minibuffer.
5421   "Apropos complete, then `icicle-narrow-candidates'.
5422 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5423 \\[icicle-apropos-complete-and-narrow]')."
5424   (interactive)
5425   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
5426   ;; $$$$$ (let ((icicle-top-level-when-sole-completion-flag  t))
5427   (when (and (eq icicle-current-completion-mode 'prefix)
5428              (eq icicle-current-TAB-method 'basic)
5429              icicle-last-input)
5430     (let ((icicle-incremental-completion-p  nil)
5431           (regexp-quoted-input              (regexp-quote icicle-last-input)))
5432       (setq regexp-quoted-input  (if (icicle-file-name-input-p)
5433                                      (concat (icicle-file-name-directory regexp-quoted-input) "^"
5434                                              (file-name-nondirectory regexp-quoted-input))
5435                                    (concat "^" regexp-quoted-input)))
5436       (icicle-erase-minibuffer)
5437       (insert regexp-quoted-input)))
5438   (setq icicle-next-apropos-complete-cycles-p  nil)
5439   (if (eq icicle-last-completion-command 'icicle-apropos-complete-no-display)
5440       (icicle-apropos-complete-no-display)
5441     (icicle-apropos-complete))
5442   (icicle-narrow-candidates))
5443
5444 ;;;###autoload
5445 (defun icicle-narrow-candidates-with-predicate (&optional predicate) ; Bound to `M-&' in minibuffer.
5446   "Narrow the set of completion candidates by applying a predicate.
5447 You can repeatedly use this command to apply additional predicates,
5448 progressively narrowing the set of candidates.
5449
5450 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5451 \\[icicle-narrow-candidates-with-predicate]').
5452
5453 When called from Lisp with non-nil arg PREDICATE, use that to narrow."
5454   (interactive)
5455   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
5456   (let (;; Restore match function, in case it was bound to nil, e.g., by `C-h C-o'.
5457         (icicle-apropos-complete-match-fn  icicle-last-apropos-complete-match-fn)
5458         (icicle-progressive-completing-p   t) ; Inhibit completion by `icicle-minibuffer-setup'.
5459         (last-completion-cmd               (or icicle-last-completion-command 'icicle-apropos-complete))
5460         (enable-recursive-minibuffers      t))
5461     (cond ((null icicle-completion-candidates)
5462            (error "No completion candidates.  Did you use `TAB' or `S-TAB'?"))
5463           ((null (cdr icicle-completion-candidates))
5464            (if (not (and icicle-top-level-when-sole-completion-flag
5465                          (sit-for icicle-top-level-when-sole-completion-delay)))
5466                (minibuffer-message "  [Sole completion]")
5467              (set minibuffer-history-variable (cons (car icicle-completion-candidates)
5468                                                     (symbol-value minibuffer-history-variable)))
5469              ;; $$$$$$ Should this now use `icicle-current-input'
5470              ;;        instead of (car icicle-completion-candidates), for PCM?
5471              (icicle-condition-case-no-debug i-narrow-candidates
5472                  (throw 'icicle-read-top
5473                    (if (and (icicle-file-name-input-p) insert-default-directory
5474                             (or (not (member (car icicle-completion-candidates)
5475                                              icicle-extra-candidates))
5476                                 icicle-extra-candidates-dir-insert-p))
5477                        (expand-file-name (car icicle-completion-candidates))
5478                      (car icicle-completion-candidates)))
5479                (no-catch (setq icicle-current-input  (car icicle-completion-candidates))
5480                          (icicle-retrieve-last-input)
5481                          icicle-current-input)
5482                (error (message "%s" (error-message-string i-narrow-candidates))))))
5483           (t                            ; Read new predicate and incorporate it.
5484            (let ((pred  (or predicate
5485                             (icicle-read-from-minibuf-nil-default
5486                              "Additional predicate to apply: "
5487                              nil read-expression-map t (if (boundp 'function-name-history)
5488                                                            'function-name-history
5489                                                          'icicle-function-name-history)))))
5490              ;; Update `read-file-name-predicate' or `minibuffer-completion-predicate'
5491              ;; to also use new predicate, PRED.
5492              ;; The logic here is the same as for `icicle-remove-cand-from-lists'.
5493              (cond (;; File name input, Emacs 22+.  Update `read-file-name-predicate'.
5494                     (and (icicle-file-name-input-p) (> emacs-major-version 21))
5495                     (setq read-file-name-predicate
5496                           (if read-file-name-predicate
5497                               (lexical-let ((curr-pred  read-file-name-predicate))
5498                                 `(lambda (file-cand)
5499                                   (and (funcall ',curr-pred file-cand) (funcall ',pred file-cand))))
5500                             pred)))
5501
5502                    ;; File name input, Emacs 20 or 21.  We can do nothing for file name.
5503                    ;; `TAB' or `S-TAB' will unfortunately bring it back as a candidate.
5504                    ((icicle-file-name-input-p))
5505
5506                    (t;; Non-file name input, all versions.  Update `minibuffer-completion-predicate'.
5507                     (setq minibuffer-completion-predicate
5508                           (if minibuffer-completion-predicate
5509                               ;; Add PRED to the existing predicate.
5510                               (lexical-let ((curr-pred  minibuffer-completion-predicate))
5511                                 `(lambda (cand)
5512                                   (and (funcall ',curr-pred cand) (funcall ',pred cand))))
5513                             ;; Set predicate to PRED.
5514                             pred)))))))
5515     (funcall last-completion-cmd)))
5516
5517 ;;;###autoload
5518 (defun icicle-save-predicate-to-variable (askp) ; Bound to `C-M-&' in minibuffer.
5519   "Save the current completion predicate to a variable.
5520 By default, the variable is `icicle-input-string'.  If you use a
5521 prefix argument, then you are prompted for the variable to use.
5522
5523 You can retrieve the saved predicate as a string using `\\<minibuffer-local-completion-map>\
5524 \\[icicle-insert-string-from-variable]'.
5525
5526 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5527 \\[icicle-save-predicate-to-variable]')."
5528   (interactive "P")
5529   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5530   (let* ((pred                                    minibuffer-completion-predicate)
5531          (icicle-whole-candidate-as-text-prop-p   nil)
5532          (enable-recursive-minibuffers            t)
5533          (icicle-must-pass-after-match-predicate  #'(lambda (s) (boundp (intern s))))
5534          (var                                     (if askp
5535                                                       (intern
5536                                                        (completing-read
5537                                                         "Save candidates in variable: " obarray nil
5538                                                         nil nil (if (boundp 'variable-name-history)
5539                                                                     'variable-name-history
5540                                                                   'icicle-variable-name-history)))
5541                                                     'icicle-input-string)))
5542     (set var (prin1-to-string pred))
5543     (save-selected-window (select-window (minibuffer-window))
5544                           (minibuffer-message (format "  [Predicate SAVED to `%s']" var)))))
5545
5546 ;;;###autoload
5547 (defun icicle-completing-read+insert () ; Bound to `C-M-S-c' (`C-M-C') in minibuffer.
5548   "Read something with completion, and insert it.
5549 Be sure to bind `icicle-completing-read+insert-candidates' to the set
5550 of candidates.
5551 Option `icicle-completing-read+insert-keys' controls which keys are
5552 bound to this command.
5553 Return the string that was inserted."
5554   (interactive)
5555   (if icicle-completing-read+insert-candidates
5556       (let ((enable-recursive-minibuffers  t)
5557             (use-dialog-box                nil)
5558             (result
5559              (icicle-completing-read "Choose: " icicle-completing-read+insert-candidates)))
5560         (insert result)
5561         result)
5562     (icicle-msg-maybe-in-minibuffer "On-demand completion not available")))
5563
5564 ;;;###autoload
5565 (defun icicle-read+insert-file-name (dir-too-p) ; Bound to `C-M-S-f' (`C-M-F') in minibuffer.
5566   "Read a file name and insert it, without its directory, by default.
5567 With a prefix argument, insert its directory also.
5568 Option `icicle-read+insert-file-name-keys' controls which keys are
5569  bound to this command.
5570 Return the string that was inserted."
5571   (interactive "P")
5572   (let ((completion-ignore-case                  (memq system-type '(ms-dos windows-nt cygwin)))
5573         (enable-recursive-minibuffers            t)
5574         (use-dialog-box                          nil)
5575         (minibuffer-local-completion-map
5576          (let ((map  (make-sparse-keymap)))
5577            (set-keymap-parent map minibuffer-local-completion-map)
5578            (define-key map [(control backspace)] 'icicle-up-directory)
5579            (define-key map "\C-c+"               'icicle-make-directory)
5580            map))
5581         (minibuffer-local-must-match-map
5582          (let ((map  (make-sparse-keymap)))
5583            (set-keymap-parent map minibuffer-local-must-match-map)
5584            (define-key map [(control backspace)] 'icicle-up-directory)
5585            (define-key map "\C-c+"               'icicle-make-directory)
5586            map))
5587         (icicle-must-pass-after-match-predicate  nil)
5588         result)
5589     (setq result  (icicle-read-file-name "Choose file: "))
5590     (unless dir-too-p                   ; Remove parent dir.
5591       (setq result  (if (file-directory-p result)
5592                         (file-name-as-directory (file-name-nondirectory (directory-file-name result)))
5593                       (file-name-nondirectory result))))
5594     (insert result)
5595     result))
5596
5597 ;; `minibuffer-local-filename-completion-map' and `minibuffer-local-must-match-filename-map'
5598 ;; were introduced in Emacs 22, and they inherit from `minibuffer-local-completion' and
5599 ;; `minibuffer-local-must-match-map', respectively.  For Emacs 23.1,
5600 ;; `minibuffer-local-must-match-filename-map' is an alias for
5601 ;; `minibuffer-local-filename-must-match-map'.  But for Emacs 23.2, there is no such alias!
5602 ;; And for Emacs 24+, there is no longer a `minibuffer-local-filename-must-match-map'.
5603 ;;;###autoload
5604 (defun icicle-bind-file-candidate-keys ()
5605   "Bind specific keys for acting on the current file candidate."
5606   (cond ((boundp 'minibuffer-local-filename-completion-map)
5607          (define-key minibuffer-local-filename-completion-map [(control backspace)]
5608            'icicle-up-directory)
5609          (define-key minibuffer-local-filename-completion-map "\C-c+"
5610            'icicle-make-directory))
5611         (t
5612          (define-key minibuffer-local-completion-map [(control backspace)]
5613            'icicle-up-directory)
5614          (define-key minibuffer-local-completion-map "\C-c+"
5615            'icicle-make-directory)))
5616   (cond ((boundp 'minibuffer-local-filename-must-match-map)
5617          (define-key minibuffer-local-filename-must-match-map [(control backspace)]
5618            'icicle-up-directory)
5619          (define-key minibuffer-local-filename-must-match-map "\C-c+"
5620            'icicle-make-directory))
5621         ((boundp 'minibuffer-local-must-match-filename-map)
5622          (define-key minibuffer-local-must-match-filename-map [(control backspace)]
5623            'icicle-up-directory)
5624          (define-key minibuffer-local-must-match-filename-map "\C-c+"
5625            'icicle-make-directory))
5626         (t
5627          (define-key minibuffer-local-must-match-map [(control backspace)]
5628            'icicle-up-directory)
5629          (define-key minibuffer-local-must-match-map "\C-c+"
5630            'icicle-make-directory)))
5631   (when (require 'bookmark+ nil t)
5632     (cond ((boundp 'minibuffer-local-filename-completion-map)
5633            (define-key minibuffer-local-filename-completion-map "\C-xm"
5634              'icicle-bookmark-file-other-window)
5635            (define-key minibuffer-local-filename-completion-map "\C-xa+"
5636              (icicle-add/remove-tags-and-refresh 'add))
5637            (define-key minibuffer-local-filename-completion-map "\C-xa-"
5638              (icicle-add/remove-tags-and-refresh 'remove)))
5639           (t
5640            (define-key minibuffer-local-completion-map "\C-xm"
5641              'icicle-bookmark-file-other-window)
5642            (define-key minibuffer-local-completion-map "\C-xa+"
5643              (icicle-add/remove-tags-and-refresh 'add))
5644            (define-key minibuffer-local-completion-map "\C-xa-"
5645              (icicle-add/remove-tags-and-refresh 'remove))))
5646     (cond ((boundp 'minibuffer-local-filename-must-match-map)
5647            (define-key minibuffer-local-filename-must-match-map "\C-xm"
5648              'icicle-bookmark-file-other-window)
5649            (define-key minibuffer-local-filename-must-match-map "\C-xa+"
5650              (icicle-add/remove-tags-and-refresh 'add))
5651            (define-key minibuffer-local-filename-must-match-map "\C-xa-"
5652              (icicle-add/remove-tags-and-refresh 'remove)))
5653           ((boundp 'minibuffer-local-must-match-filename-map)
5654            (define-key minibuffer-local-must-match-filename-map "\C-xm"
5655              'icicle-bookmark-file-other-window)
5656            (define-key minibuffer-local-must-match-filename-map "\C-xa+"
5657              (icicle-add/remove-tags-and-refresh 'add))
5658            (define-key minibuffer-local-must-match-filename-map "\C-xa-"
5659              (icicle-add/remove-tags-and-refresh 'remove)))
5660           (t
5661            (define-key minibuffer-local-completion-map "\C-xm"
5662              'icicle-bookmark-file-other-window)
5663            (define-key minibuffer-local-completion-map "\C-xa+"
5664              (icicle-add/remove-tags-and-refresh 'add))
5665            (define-key minibuffer-local-completion-map "\C-xa-"
5666              (icicle-add/remove-tags-and-refresh 'remove)))))
5667   ;; When using `completing-read', not `read-file-name', regardless of the Emacs version.
5668   (when (not (icicle-file-name-input-p))
5669     (define-key minibuffer-local-completion-map [(control backspace)]
5670       'icicle-up-directory)
5671     (define-key minibuffer-local-completion-map "\C-c+"
5672       'icicle-make-directory)
5673     (define-key minibuffer-local-completion-map "\C-xm"
5674       'icicle-bookmark-file-other-window)
5675     (define-key minibuffer-local-completion-map "\C-xa+"
5676       (icicle-add/remove-tags-and-refresh 'add))
5677     (define-key minibuffer-local-completion-map "\C-xa-"
5678       (icicle-add/remove-tags-and-refresh 'remove))))
5679
5680 ;;;###autoload
5681 (defun icicle-unbind-file-candidate-keys ()
5682   "Unbind specific keys for acting on the current file candidate."
5683   (when (boundp 'minibuffer-local-filename-completion-map)
5684     (define-key minibuffer-local-filename-completion-map [(control backspace)] nil)
5685     (define-key minibuffer-local-filename-completion-map "\C-c+"               nil)
5686     (define-key minibuffer-local-filename-completion-map "\C-xm"               nil)
5687     (define-key minibuffer-local-filename-completion-map "\C-xa+"              nil)
5688     (define-key minibuffer-local-filename-completion-map "\C-xa-"              nil)
5689     (define-key minibuffer-local-filename-completion-map "\C-xa"               nil))
5690   (when (boundp 'minibuffer-local-filename-must-match-map)
5691     (define-key minibuffer-local-filename-must-match-map [(control backspace)] nil)
5692     (define-key minibuffer-local-filename-must-match-map "\C-c+"               nil)
5693     (define-key minibuffer-local-filename-must-match-map "\C-xm"               nil)
5694     (define-key minibuffer-local-filename-must-match-map "\C-xa+"              nil)
5695     (define-key minibuffer-local-filename-must-match-map "\C-xa-"              nil)
5696     (define-key minibuffer-local-filename-must-match-map "\C-xa"               nil))
5697   (when (boundp 'minibuffer-local-must-match-filename-map)
5698     (define-key minibuffer-local-must-match-filename-map [(control backspace)] nil)
5699     (define-key minibuffer-local-must-match-filename-map "\C-c+"               nil)
5700     (define-key minibuffer-local-must-match-filename-map "\C-xm"               nil)
5701     (define-key minibuffer-local-must-match-filename-map "\C-xa+"              nil)
5702     (define-key minibuffer-local-must-match-filename-map "\C-xa-"              nil)
5703     (define-key minibuffer-local-must-match-filename-map "\C-xa"               nil))
5704   (define-key minibuffer-local-completion-map [(control backspace)]            nil)
5705   (define-key minibuffer-local-completion-map "\C-c+"                          nil)
5706   (define-key minibuffer-local-completion-map "\C-xm"                          nil)
5707   (define-key minibuffer-local-completion-map "\C-xa+"                         nil)
5708   (define-key minibuffer-local-completion-map "\C-xa-"                         nil)
5709   (define-key minibuffer-local-completion-map "\C-xa"                          nil))
5710
5711
5712 ;;;###autoload
5713 (defun icicle-candidate-set-swap ()     ; Bound to `C-%' in minibuffer.
5714   "Swap the saved set and current sets of completion candidates.
5715 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5716 \\[icicle-candidate-set-swap]')."
5717   (interactive)
5718   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5719   (setq icicle-saved-completion-candidates
5720         (prog1 icicle-completion-candidates
5721           (setq icicle-completion-candidates  icicle-saved-completion-candidates)))
5722   (minibuffer-message "  [Saved set of candidates SWAPPED with current]"))
5723
5724 ;;;###autoload
5725 (defun icicle-candidate-set-define ()   ; Bound to `C-:' in minibuffer.
5726   "Define the set of current completion candidates by evaluating a sexp.
5727 The Lisp sexp must evaluate to a list of strings, such as is returned
5728 by `all-completions'.
5729
5730 You can use this command at top level or from the minibuffer (`\\<minibuffer-local-completion-map>\
5731 \\[icicle-candidate-set-define]')."
5732   (interactive)
5733   (let* ((enable-recursive-minibuffers  t)
5734          (evald-sexp                    (eval-minibuffer
5735                                          "Set the completion candidates to sexp (eval): ")))
5736     (when (and evald-sexp (or (atom evald-sexp) (not (stringp (car evald-sexp)))))
5737       (error "Sexp did not evaluate to a list of strings: %S" evald-sexp))
5738     (setq icicle-completion-candidates  evald-sexp))
5739   (icicle-maybe-sort-and-strip-candidates)
5740   (message "List of completion candidates DEFINED: %S" icicle-completion-candidates)
5741   (when (> (minibuffer-depth) 0)
5742     (message "Displaying completion candidates...")
5743     (with-output-to-temp-buffer "*Completions*"
5744       (display-completion-list icicle-completion-candidates))
5745     (icicle-narrow-candidates)))
5746
5747 ;;;###autoload
5748 (defun icicle-candidate-set-difference () ; Bound to `C--' in minibuffer.
5749   "Take the set difference between the current and saved candidates.
5750 The new set of candidates is the set of candidates prior to executing
5751 this command minus the saved set of candidates.
5752
5753 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5754 \\[icicle-candidate-set-difference]')."
5755   (interactive)
5756   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5757   (message "Computing set difference: current minus saved candidates...")
5758   (icicle-candidate-set-1 'icicle-set-difference "  [saved set of candidates SUBTRACTED]"))
5759
5760 ;;;###autoload
5761 (defun icicle-candidate-set-union ()    ; Bound to `C-+' in minibuffer.
5762   "Take the set union between the current and saved candidates.
5763 The new set of candidates is the union of the saved set of candidates
5764 and the set of candidates prior to executing this command.
5765
5766 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5767 \\[icicle-candidate-set-union]')."
5768   (interactive)
5769   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5770   (message "Computing set union: current plus saved candidates...")
5771   (icicle-candidate-set-1 'icicle-set-union "  [saved set of candidates ADDED]"))
5772
5773 ;;;###autoload
5774 (defun icicle-candidate-set-intersection () ; Bound to `C-*' in minibuffer.
5775   "Take the set intersection between the current and saved candidates.
5776 The new set of candidates is the intersection of the saved set of
5777 candidates and the set of candidates prior to executing this command.
5778
5779 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5780 \\[icicle-candidate-set-intersection]')."
5781   (interactive)
5782   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5783   (message "Computing set intersection: current and saved candidates...")
5784   (icicle-candidate-set-1 'icicle-set-intersection
5785                           "  [INTERSECTION of saved and current sets of candidates]"))
5786
5787 ;;;###autoload
5788 (defun icicle-candidate-set-complement () ; Bound to `C-~' in minibuffer.
5789   "Complement the set of current completion candidates.
5790 The new set of candidates is the set of all candidates in the initial
5791 completion domain minus the set of matching candidates prior to
5792 executing this command - that is, all possible completions of the
5793 appropriate type, except for those that are in the current set of
5794 completions.
5795
5796 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5797 \\[icicle-candidate-set-complement]')."
5798   (interactive)
5799   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5800   (message "Complementing current set of candidates...")
5801   (let ((initial-cands  (icicle-all-completions "" minibuffer-completion-table
5802                                                 minibuffer-completion-predicate
5803                                                 icicle-ignore-space-prefix-flag)))
5804     (setq icicle-completion-candidates  (icicle-set-difference
5805                                          (if icicle-must-pass-after-match-predicate
5806                                              (icicle-remove-if-not
5807                                               icicle-must-pass-after-match-predicate initial-cands)
5808                                            initial-cands)
5809                                          icicle-completion-candidates)))
5810   (icicle-maybe-sort-and-strip-candidates)
5811   (message "Displaying completion candidates...")
5812   (with-output-to-temp-buffer "*Completions*" (display-completion-list icicle-completion-candidates))
5813   (minibuffer-message "  [Set of candidates COMPLEMENTED]")
5814   (icicle-narrow-candidates))
5815
5816 (defun icicle-candidate-set-truncate (n) ; Bound to `M-$' in minibuffer.
5817   "Trim the set of current completion candidates at the end.
5818 The first N candidates are kept.  N is read."
5819   ;; Ugly hack: `icicle-saved-completion-candidates-internal'.  No way to bind a variable
5820   ;; in `interactive' and have the binding be active in the function body.
5821   (interactive
5822    (list (let ((enable-recursive-minibuffers  t))
5823            (setq icicle-saved-completion-candidates-internal  icicle-completion-candidates)
5824            (if current-prefix-arg
5825                (prefix-numeric-value current-prefix-arg)
5826              (read-number "Number of candidates to keep: ")))))
5827   (setq icicle-completion-candidates  icicle-saved-completion-candidates-internal)
5828   (setcdr (nthcdr (1- n) icicle-completion-candidates) nil)
5829   (icicle-maybe-sort-and-strip-candidates)
5830   (message "Displaying completion candidates...")
5831   (with-output-to-temp-buffer "*Completions*" (display-completion-list icicle-completion-candidates))
5832   (message (format "  [Set of candidates TRUNCATED to %d]" n))
5833   (icicle-narrow-candidates))
5834
5835 ;;;###autoload
5836 (defun icicle-candidate-set-retrieve (&optional arg) ; Bound to `C-M-<' in minibuffer.
5837   "Retrieve a saved set of completion candidates, making it current.
5838 This retrieves candidates saved with `\\<minibuffer-local-completion-map>\
5839 \\[icicle-save/unsave-candidate]', `M-S-mouse-2',
5840 `\\<minibuffer-local-completion-map>\\[icicle-candidate-set-save]', \
5841 `\\[icicle-candidate-set-save-to-variable]', or `\\[icicle-candidate-set-save-persistently]'.
5842
5843 With no prefix arg, retrieve candidates from variable
5844  `icicle-saved-completion-candidates'.
5845 With a numeric prefix arg, retrieve candidates from another variable.
5846 With a plain prefix arg (`C-u'), retrieve candidates from a cache file
5847  or, if option `icicle-filesets-as-saved-completion-sets-flag' is
5848  non-nil, an Emacs fileset name (Emacs 22 or later).  To use filesets,
5849  you must also load library `filesets' and use `(filesets-init)'.
5850
5851 Completion is available when you are prompted for a cache file,
5852 fileset, or variable name.
5853
5854 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5855 \\[icicle-candidate-set-retrieve]')."
5856   (interactive "P")
5857   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5858   (icicle-candidate-set-retrieve-1 arg))
5859
5860 ;;;###autoload
5861 (defun icicle-candidate-set-retrieve-1 (arg &optional morep)
5862   "Helper function for `icicle-candidate-set-retrieve(-more)'.
5863 ARG is the same as the raw prefix arg for `icicle-candidate-set-retrieve'.
5864 MOREP non-nil means add the saved candidates, don't replace existing."
5865   (let ((name        nil)
5866         (variablep   (and arg (atom arg)))
5867         (curr-cands  icicle-completion-candidates)
5868         saved-cands)
5869     (if arg
5870         (let ((icicle-whole-candidate-as-text-prop-p  nil)
5871               (enable-recursive-minibuffers           t))
5872           (if variablep
5873               ;; Retrieve from a variable.  Prompt user for the variable to use.
5874               (setq saved-cands
5875                     (append (and morep curr-cands)
5876                             (symbol-value
5877                              (setq name  (intern
5878                                           (completing-read ; Variable name.
5879                                            "Retrieve candidates from variable: "
5880                                            icicle-saved-candidates-variables-obarray
5881                                            nil nil nil (if (boundp 'variable-name-history)
5882                                                            'variable-name-history
5883                                                          'icicle-variable-name-history)))))))
5884             ;; Retrieve from a persistent set (and save to `icicle-saved-completion-candidates').
5885             (setq name  (completing-read "Retrieve candidates from persistent set: "
5886                                          (if (and icicle-filesets-as-saved-completion-sets-flag
5887                                                   (featurep 'filesets) filesets-data)
5888                                              (append filesets-data icicle-saved-completion-sets)
5889                                            icicle-saved-completion-sets)
5890                                          nil nil nil 'icicle-completion-set-history))
5891             (icicle-retrieve-candidates-from-set name)
5892             (setq saved-cands  (append (and morep curr-cands) icicle-saved-completion-candidates))))
5893       ;; Retrieve from the default variable, `icicle-saved-completion-candidates'.
5894       (setq saved-cands  (append (and morep curr-cands)
5895                                  icicle-saved-completion-candidates)))
5896     (cond ((null saved-cands)
5897            (deactivate-mark)
5898            (icicle-display-candidates-in-Completions)
5899            (message "No saved candidates to restore") (sit-for 2))
5900           (t
5901            (setq icicle-completion-candidates ; Remove directories if completing file names
5902                  (if (icicle-file-name-input-p) ; using `read-file-name'.
5903                      (mapcar #'file-name-nondirectory saved-cands)
5904                    saved-cands))
5905            (cond ((and (consp icicle-completion-candidates) (null (cdr icicle-completion-candidates)))
5906                   ;; $$$$$$ Should this now use `icicle-current-input'
5907                   ;;        instead of (car icicle-completion-candidates), for PCM?
5908                   (icicle-remove-Completions-window)
5909                   (icicle-insert-completion (car icicle-completion-candidates)) ; Insert sole cand.
5910                   (minibuffer-message "  [Sole candidate restored]")
5911                   (save-selected-window (select-window (minibuffer-window))
5912                                         (icicle-highlight-complete-input))
5913                   (icicle-show-help-in-mode-line (car icicle-completion-candidates)))
5914                  ((consp icicle-completion-candidates)
5915                   (deactivate-mark)
5916                   (icicle-display-candidates-in-Completions)
5917                   (save-selected-window
5918                     (select-window (minibuffer-window))
5919                     (minibuffer-message (if name
5920                                             (format "  [Saved candidates RESTORED from %s `%s']"
5921                                                     (if variablep "variable" "cache file") name)
5922                                           "  [Saved candidates RESTORED]")))
5923                   (let ((icicle-minibuffer-setup-hook ; Pre-complete
5924                          (cons (if (eq icicle-last-completion-command
5925                                        'icicle-apropos-complete-no-display)
5926                                    'icicle-apropos-complete-no-display
5927                                  'icicle-apropos-complete)
5928                                icicle-minibuffer-setup-hook)))
5929                     (icicle-narrow-candidates))))))))
5930
5931 ;;;###autoload
5932 (defun icicle-candidate-set-retrieve-more (&optional arg) ; Bound to `C-<' in minibuffer.
5933   "Retrieve a saved set of completion candidates, adding it current.
5934 The saved candidates are added to those already current.
5935 A prefix argument acts as for `icicle-candidate-set-retrieve'.
5936
5937 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5938 \\[icicle-candidate-set-retrieve-more]')."
5939   (interactive "P")
5940   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5941   (icicle-candidate-set-retrieve-1 arg t))
5942
5943 ;;;###autoload
5944 (defun icicle-candidate-set-retrieve-from-variable () ; Bound to `C-M-{' in minibuffer.
5945   "Retrieve a saved set of completion candidates, making it current.
5946 This retrieves candidates saved with `\\<minibuffer-local-completion-map>\
5947 \\[icicle-save/unsave-candidate]', `M-S-mouse-2', or
5948 `\\[icicle-candidate-set-save-to-variable]' (or `\\[icicle-candidate-set-save]' with a numeric \
5949 prefix arg).
5950
5951 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5952 \\[icicle-candidate-set-retrieve-from-variable]')."
5953   (interactive)
5954   (icicle-candidate-set-retrieve 99))
5955
5956 ;;;###autoload
5957 (defun icicle-candidate-set-retrieve-persistent () ; Bound to `C-{' in minibuffer.
5958   "Retrieve a saved set of completion candidates, making it current.
5959 This retrieves candidates saved with `\\<minibuffer-local-completion-map>\
5960 \\[icicle-candidate-set-save-persistently]' or `C-u \\[icicle-candidate-set-save]'.
5961
5962 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5963 \\[icicle-candidate-set-retrieve-persistent]')."
5964   (interactive)
5965   (icicle-candidate-set-retrieve '(1)))
5966
5967 (defun icicle-retrieve-candidates-from-set (set-name)
5968   "Retrieve the saved set of completion candidates named SET-NAME.
5969 SET-NAME names an Icicles saved completion set or, if
5970  `icicle-filesets-as-saved-completion-sets-flag' is non-nil, an Emacs
5971  fileset.  If that option is non-nil and SET-NAME names a saved
5972  completion set that contains Emacs filesets, then the files specified
5973  for the filesets are also retrieved. 
5974 The candidates are retrieved to `icicle-saved-completion-candidates',
5975 and `icicle-candidates-alist' is updated."
5976   (setq icicle-saved-completion-candidates  (icicle-get-candidates-from-saved-set set-name))
5977   (when icicle-candidates-alist         ; Redefine `icicle-candidates-alist'.
5978     (let ((icicle-whole-candidate-as-text-prop-p  t))
5979       (setq icicle-candidates-alist  (mapcar icicle-get-alist-candidate-function
5980                                              icicle-saved-completion-candidates)))))
5981
5982 ;;;###autoload
5983 (defun icicle-save/unsave-candidate ()  ; Bound to `insert' in minibuffer.
5984   "Add/remove current candidate to/from `icicle-saved-completion-candidates'.
5985 If the candidate is already saved, then unsave it; otherwise, save it.
5986 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
5987 \\[icicle-save/unsave-candidate]')."
5988   (interactive)
5989   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
5990   (if (not (wholenump icicle-candidate-nb))
5991       (save-selected-window (select-window (minibuffer-window))
5992                             (minibuffer-message "  [No current candidate]"))
5993     (let ((cand  (elt icicle-completion-candidates icicle-candidate-nb)))
5994       (cond ((member cand icicle-saved-completion-candidates)
5995              (setq icicle-saved-completion-candidates
5996                    (delete icicle-last-completion-candidate icicle-saved-completion-candidates))
5997              (save-selected-window (select-window (minibuffer-window))
5998                                    (minibuffer-message "  [Candidate UNsaved]")))
5999             (t
6000              (push cand icicle-saved-completion-candidates)
6001              (save-selected-window (select-window (minibuffer-window))
6002                                    (minibuffer-message "  [Candidate SAVED]")))))))
6003 ;;;###autoload
6004 (defun icicle-mouse-save/unsave-candidate (event) ; Bound to `M-S-mouse-2' in *Completions.
6005   "Add/remove clicked candidate to/from `icicle-saved-completion-candidates'.
6006 If the candidate is already saved, then unsave it; otherwise, save it."
6007   (interactive "e")
6008   (run-hooks 'mouse-leave-buffer-hook)  ; Give temp modes such as isearch a chance to turn off.
6009   (let (;; $$$$$$ (buffer    (window-buffer))
6010         (posn-win  (posn-window (event-start event)))
6011         (posn-col  (car (posn-col-row (event-start event))))
6012         (posn-row  (cdr (posn-col-row (event-start event))))
6013         choice base-size)
6014     (read-event)                        ; Swallow mouse up event.
6015     (with-current-buffer (window-buffer posn-win)
6016       (save-excursion
6017         ;; $$$$$$ (when completion-reference-buffer (setq buffer  completion-reference-buffer))
6018         (setq base-size  completion-base-size)
6019         (goto-char (posn-point (event-start event)))
6020         (let (beg end)
6021           (when (and (not (eobp)) (get-text-property (point) 'mouse-face))
6022             (setq end  (point)
6023                   beg  (1+ (point))))
6024           (unless beg (error "No completion here"))
6025           (setq beg     (previous-single-property-change beg 'mouse-face)
6026                 end     (or (next-single-property-change end 'mouse-face)(point-max))
6027                 choice  (buffer-substring-no-properties beg end)))))
6028     (setq icicle-candidate-nb               (icicle-nb-of-cand-at-Completions-pos
6029                                              (posn-point (event-start event)))
6030           icicle-last-completion-candidate  choice)
6031     (cond ((member icicle-last-completion-candidate icicle-saved-completion-candidates)
6032            (setq icicle-saved-completion-candidates
6033                  (delete icicle-last-completion-candidate icicle-saved-completion-candidates))
6034            (save-selected-window (select-window (minibuffer-window))
6035                                  (minibuffer-message "  [Candidate UNsaved]")))
6036           (t
6037            (push icicle-last-completion-candidate icicle-saved-completion-candidates)
6038            (save-selected-window (select-window (minibuffer-window))
6039                                  (minibuffer-message "  [Candidate SAVED]"))))
6040     (deactivate-mark)
6041     (icicle-display-candidates-in-Completions)
6042     (icicle-raise-Completions-frame posn-col posn-row)))
6043
6044 ;;;###autoload
6045 (defun icicle-mouse-candidate-set-save (ignore &optional arg) ; `M-S-mouse-3' in `*Completions*'.
6046   "`icicle-candidate-set-save(-selected)'.
6047 If the region is active in `*Completions*', then
6048 `icicle-candidate-set-save-selected'.  Otherwise,
6049 `icicle-candidate-set-save'."
6050   (interactive "e\nP")
6051   (if (and (get-buffer "*Completions*")
6052            (save-current-buffer
6053              (set-buffer (get-buffer "*Completions*"))
6054              (and mark-active (mark) (/= (point) (mark)))))
6055       (icicle-candidate-set-save-selected arg)
6056     (icicle-candidate-set-save arg)))
6057
6058 ;;;###autoload
6059 (defun icicle-mouse-candidate-set-save-more (ignore &optional arg) ; `M-mouse-3' in `*Completions*'.
6060   "`icicle-candidate-set-save-more(-selected)'.
6061 If the region is active in `*Completions*', then
6062 `icicle-candidate-set-save-more-selected'.  Otherwise,
6063 `icicle-candidate-set-save-more'."
6064   (interactive "e\nP")
6065   (if (and (get-buffer "*Completions*")
6066            (save-current-buffer
6067              (set-buffer (get-buffer "*Completions*"))
6068              (and mark-active (mark) (/= (point) (mark)))))
6069       (icicle-candidate-set-save-more-selected arg)
6070     (icicle-candidate-set-save-more arg)))
6071
6072 ;;; `mouse-3' in `*Completions*'.
6073 (cond ((require 'mouse3 nil t)
6074        (defun icicle-mouse-save-then-kill (click &optional arg)
6075          "`mouse-save-then-kill', but click same place saves selected candidates."
6076          (interactive "e\nP")
6077          (let ((mouse3-save-then-kill-command  `(lambda (event prefix-arg)
6078                                                  (icicle-mouse-candidate-set-save-more nil ,arg))))
6079            (mouse-save-then-kill click))
6080          (setq this-command  'mouse-save-then-kill)))
6081
6082       ((< emacs-major-version 24)
6083        (defun icicle-mouse-save-then-kill (click &optional arg)
6084          "`mouse-save-then-kill', but click same place saves selected candidates."
6085          (interactive "e\nP")
6086          (flet ((mouse-save-then-kill-delete-region (beg end)
6087                   (icicle-mouse-candidate-set-save-more nil arg)))
6088            (mouse-save-then-kill click))
6089          (setq this-command  'mouse-save-then-kill)))
6090
6091       (t
6092        ;; The only thing Icicles-specific here is replacing killing or deleting the region by a call to
6093        ;; `icicle-mouse-candidate-set-save-more'.  Otherwise, this is just `mouse-save-then-kill'.
6094        (defun icicle-mouse-save-then-kill (click &optional arg) ; `mouse-3' in `*Completions*'.
6095          "`mouse-save-then-kill', but click same place saves selected candidates."
6096          (interactive "e\nP")
6097          (mouse-minibuffer-check click)
6098          (let* ((posn          (event-start click))
6099                 (click-pt      (posn-point posn))
6100                 (window        (posn-window posn))
6101                 (buf           (window-buffer window))
6102                 (this-command  this-command) ; Don't let subsequent kill cmd append to this one.
6103                 ;; Check if the user has multi-clicked to select words/lines.
6104                 (click-count   (if (and (eq mouse-selection-click-count-buffer buf)
6105                                         (with-current-buffer buf (mark t)))
6106                                    mouse-selection-click-count
6107                                  0)))
6108            (cond ((not (numberp click-pt)) nil)
6109                  ((and (eq last-command 'icicle-mouse-save-then-kill) ; User clicked at same position.
6110                        (eq click-pt mouse-save-then-kill-posn)
6111                        (eq window (selected-window)))
6112                   ;; Here is the Icicles difference from vanilla `mouse-save-then-kill'.
6113                   ;; Instead of killing/deleting the region, save the selected candidates.
6114                   (icicle-mouse-candidate-set-save-more nil arg)
6115                   (setq mouse-selection-click-count  0
6116                         mouse-save-then-kill-posn    nil))
6117                  ;; If there is a suitable region, adjust it by moving the closest end to CLICK-PT.
6118                  ((or (with-current-buffer buf (region-active-p))
6119                       (and (eq window (selected-window))
6120                            (mark t)
6121                            (or (and (eq last-command 'icicle-mouse-save-then-kill)
6122                                     mouse-save-then-kill-posn)
6123                                (and (memq last-command '(mouse-drag-region mouse-set-region))
6124                                     (or mark-even-if-inactive (not transient-mark-mode))))))
6125                   (select-window window)
6126                   (let* ((range  (mouse-start-end click-pt click-pt click-count)))
6127                     (if (< (abs (- click-pt (mark t))) (abs (- click-pt (point))))
6128                         (set-mark (car range))
6129                       (goto-char (nth 1 range)))
6130                     (setq deactivate-mark  nil)
6131                     (mouse-set-region-1)
6132                     (when mouse-drag-copy-region
6133                       ;; Previous region was copied to kill-ring, so replace with adjusted region.
6134                       (kill-new (filter-buffer-substring (mark t) (point)) t))
6135                     (setq mouse-save-then-kill-posn  click-pt))) ; Repeated `mouse-3' kills region.
6136                  (t                     ; Set the mark where point is and move to CLICK-PT.
6137                   (select-window window)
6138                   (mouse-set-mark-fast click)
6139                   (let ((before-scroll (with-current-buffer buf point-before-scroll)))
6140                     (when before-scroll (goto-char before-scroll)))
6141                   (exchange-point-and-mark)
6142                   (mouse-set-region-1)
6143                   (when mouse-drag-copy-region (kill-new (filter-buffer-substring (mark t) (point))))
6144                   (setq mouse-save-then-kill-posn  click-pt)))))))
6145
6146 ;;;###autoload
6147 (defun icicle-candidate-set-save (&optional arg) ; Bound to `C-M->' in minibuffer.
6148   "Save the set of current completion candidates, for later recall.
6149 Saves candidates in variable `icicle-saved-completion-candidates', by
6150 default.
6151 With a plain prefix arg (`C-u'), save candidates in a cache file.
6152 With a non-zero numeric prefix arg (`C-u N'), save candidates in a
6153  variable for which you are prompted.
6154 With a zero prefix arg (`C-0'), save candidates in a fileset (Emacs 22
6155  or later).  Use this only for file-name candidates, obviously.  To
6156  subsequently use a fileset for candidate retrieval, option
6157  `icicle-filesets-as-saved-completion-sets-flag' must be non-nil.
6158
6159 You can retrieve the saved set of candidates with `\\<minibuffer-local-completion-map>\
6160 \\[icicle-candidate-set-retrieve]'.
6161 You can use the saved set of candidates for operations such as
6162 \\<minibuffer-local-completion-map>
6163 `icicle-candidate-set-union' (`\\[icicle-candidate-set-union]'),
6164 `icicle-candidate-set-intersection' (`\\[icicle-candidate-set-intersection]'), and
6165 `icicle-candidate-set-difference' (`\\[icicle-candidate-set-difference]').
6166
6167 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6168 \\[icicle-candidate-set-save]')."
6169   (interactive "P")
6170   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
6171   (icicle-candidate-set-save-1 icicle-completion-candidates arg))
6172
6173 ;;;###autoload
6174 (defun icicle-candidate-set-save-more (&optional arg) ; Bound to `C->' in minibuffer.
6175   "Add current completion candidates to saved candidates set.
6176 The current candidates are added to those already saved.
6177 A prefix argument acts the same as for `icicle-candidate-set-save'.
6178
6179 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6180 \\[icicle-candidate-set-save-more]')."
6181   (interactive "P")
6182   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
6183   (icicle-candidate-set-save-1 icicle-completion-candidates arg t))
6184
6185 ;;;###autoload
6186 (defun icicle-candidate-set-save-selected (&optional arg) ; Bound to `C-M-)' in minibuffer.
6187   "`icicle-candidate-set-save', but only for the selected candidates.
6188 Candidates at least partially in the region are saved.
6189 A prefix argument acts the same as for `icicle-candidate-set-save'.
6190
6191 As a special case, if no candidates are selected, then this empties
6192 the current set of saved candidates.  That is, it UNsaves all saved
6193 candidates.
6194
6195 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6196 \\[icicle-candidate-set-save-selected]')."
6197   (interactive "P")
6198   (icicle-candidate-set-save-selected-1 arg nil 'no-error))
6199
6200 ;;;###autoload
6201 (defun icicle-candidate-set-save-more-selected (&optional arg) ; Bound to `C-)' in minibuffer.
6202   "`icicle-candidate-set-save-more', but only for the selected candidates.
6203 Candidates at least partially in the region are added to those saved.
6204 A prefix argument acts the same as for `icicle-candidate-set-save'.
6205
6206 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6207 \\[icicle-candidate-set-save-more-selected]')."
6208   (interactive "P")
6209   (icicle-candidate-set-save-selected-1 arg t))
6210
6211 ;; $$$$$$$ Maybe should also allow rectangle selection.
6212 (defun icicle-candidate-set-save-selected-1 (arg &optional morep no-error-p)
6213   "Helper function for `icicle-candidate-set-save(-more)(-region)'."
6214   (when (or (get-buffer-window "*Completions*" 0) no-error-p)
6215     (let ((beg-cand-nb       0)
6216           (end-cand-nb       0)
6217           (candidates        ())
6218           (icicle-orig-buff  (current-buffer)))
6219       (when (get-buffer-window "*Completions*" 0) ; Do nothing if not displayed.
6220         (with-current-buffer "*Completions*"
6221           (when (and mark-active (mark) (/= (point) (mark)) icicle-completion-candidates)
6222             (let ((bob  (icicle-start-of-candidates-in-Completions))
6223                   (eob  (point-max))
6224                   (beg  (region-beginning))
6225                   (end  (region-end))
6226                   temp)
6227
6228               ;; Extend region ends to include all of first and last selected candidates.
6229               (unless (get-text-property beg 'mouse-face)
6230                 (if (setq temp  (next-single-property-change beg 'mouse-face))
6231                     (setq beg  temp)
6232                   (setq beg  (next-single-property-change temp 'mouse-face))))
6233
6234               (when (> beg end)
6235                 (error "No candidates selected")) ; Active region but none selected.
6236
6237               (unless (get-text-property end 'mouse-face)
6238                 (if (setq temp  (previous-single-property-change end 'mouse-face))
6239                     (setq end  temp)
6240                   (setq end  (previous-single-property-change temp 'mouse-face))))
6241               (when (> beg end) (setq beg  (prog1 end (setq end  beg)))) ; Swap them.
6242               (while (and (>= beg bob) (get-text-property beg 'mouse-face)) (setq beg  (1- beg)))
6243               (while (and (<= end eob) (get-text-property end 'mouse-face)) (setq end  (1+ end)))
6244               (setq beg          (1+ beg)
6245                     end          (1- end)
6246                     beg-cand-nb  (icicle-nb-of-cand-at-Completions-pos beg)
6247                     end-cand-nb  (icicle-nb-of-cand-at-Completions-pos end))
6248               (when (> beg-cand-nb end-cand-nb) ; Swap them
6249                 (setq beg-cand-nb  (prog1 end-cand-nb (setq end-cand-nb  beg-cand-nb))))
6250               (while (<= beg-cand-nb end-cand-nb)
6251                 (push (elt icicle-completion-candidates beg-cand-nb) candidates)
6252                 (setq beg-cand-nb  (1+ beg-cand-nb)))))))
6253       (when (and morep (null candidates)) (error "No candidates selected")) ; Need selection for MOREP.
6254       (setq candidates  (nreverse candidates))
6255       (icicle-candidate-set-save-1 candidates arg morep t no-error-p)
6256       (let ((win  (get-buffer-window icicle-orig-buff 'visible)))
6257         (when win (select-window win))))))
6258
6259 (defun icicle-candidate-set-save-1 (new-cands arg &optional morep only-selected-p no-error-p)
6260   "Helper function for `icicle-candidate-set-save*' functions.
6261 NEW-CANDS are the candidates to save.
6262 ARG is the same as the raw prefix arg for `icicle-candidate-set-save'.
6263 MOREP non-nil means add the candidates, do not replace existing set.
6264 ONLY-SELECTED-P non-nil means NEW-CANDS are those selected in
6265  `*Completions*'.
6266 NO-ERROR-P non-nil means don't raise an error if NEW-CANDS is nil."
6267   (unless (or new-cands no-error-p)
6268     (error "Cannot save empty candidates set - did you use `S-TAB' or `TAB'?"))
6269   (let (where)
6270     (if arg
6271         (let ((enable-recursive-minibuffers  t))
6272           (cond ((consp arg)
6273                  ;; Save to cache file (and to `icicle-saved-completion-candidates').
6274                  (let* ((file-name
6275                          (prog1 (let ((icicle-completion-candidates  icicle-completion-candidates))
6276                                   (icicle-add/update-saved-completion-set))
6277                            (when (minibuffer-window-active-p (minibuffer-window))
6278                              (with-output-to-temp-buffer "*Completions*" ; Redisplay.
6279                                (display-completion-list icicle-completion-candidates)))
6280                            (select-window (minibuffer-window))))
6281                         (list-buf   (and morep (find-file-noselect file-name 'nowarn 'raw)))
6282                         (old-cands  ()))
6283                    (when morep
6284                      (unwind-protect
6285                           (condition-case nil
6286                               (setq old-cands  (read list-buf))
6287                             (end-of-file
6288                              (save-selected-window
6289                                (select-window (minibuffer-window))
6290                                (minibuffer-message (format "  [No completion candidates in file `%s']"
6291                                                            file-name)))))
6292                        (kill-buffer list-buf)))
6293                    ;; Convert to readable alist form, from propertized text.  Convert any markers
6294                    ;; to the form (icicle-file-marker FILE POS) or (icicle-marker BUFFER POS).
6295                    (when (and new-cands (get-text-property 0 'icicle-whole-candidate (car new-cands)))
6296                      (setq new-cands
6297                            (mapcar (lambda (cand)
6298                                      (icicle-markers-to-readable
6299                                       (or (funcall icicle-get-alist-candidate-function cand) cand)))
6300                                    new-cands)))
6301                    (setq icicle-saved-completion-candidates  (append new-cands old-cands)
6302                          where                               (format "cache file `%s'" file-name))
6303                    (with-temp-message (format "Writing candidates to cache file `%s'..." file-name)
6304                      (condition-case err
6305                          (with-temp-file file-name
6306                            (prin1 icicle-saved-completion-candidates (current-buffer)))
6307                        (error (error "Could not write to cache file.  %s"
6308                                      (error-message-string err)))))))
6309                 ((zerop (prefix-numeric-value arg))
6310                  ;; Save to a fileset (and to `icicle-saved-completion-candidates').
6311                  (unless (require 'filesets nil t)
6312                    (error "Cannot save to a fileset - feature `filesets' not provided"))
6313                  (filesets-init)
6314                  (let ((icicle-completion-candidates  icicle-completion-candidates))
6315                    (setq where  (completing-read "Save to fileset: " filesets-data)))
6316                  (dolist (cand  new-cands) (icicle-add-file-to-fileset cand where))
6317                  (when (minibuffer-window-active-p (minibuffer-window))
6318                    (with-output-to-temp-buffer "*Completions*" ; Redisplay.
6319                      (display-completion-list icicle-completion-candidates)))
6320                  (select-window (minibuffer-window))
6321                  (setq where  (format "`%s'" where)))
6322                 (t                      ; Save to a variable.  Prompt for the variable to use.
6323                  (let* ((varname
6324                          (prog1 (let ((icicle-completion-candidates
6325                                        icicle-completion-candidates)
6326                                       (icicle-whole-candidate-as-text-prop-p  nil))
6327                                   (completing-read (if morep
6328                                                        "Add candidates to variable: "
6329                                                      "Save candidates in variable: ")
6330                                                    icicle-saved-candidates-variables-obarray
6331                                                    nil nil nil (if (boundp 'variable-name-history)
6332                                                                    'variable-name-history
6333                                                                  'icicle-variable-name-history)))
6334                            (when (minibuffer-window-active-p (minibuffer-window))
6335                              (with-output-to-temp-buffer "*Completions*"
6336                                (display-completion-list icicle-completion-candidates)))
6337                            (select-window (minibuffer-window))))
6338                         (var  (intern varname))) ; Intern in standard `obarray'.
6339                    (intern varname icicle-saved-candidates-variables-obarray) ; For completion.
6340                    (set var (if (and morep (boundp var) (listp (symbol-value var)))
6341                                 (append new-cands (symbol-value var))
6342                               new-cands))
6343                    (setq where  (format "`%s'" var))))))
6344       ;; Save to default variable, `icicle-saved-completion-candidates'.
6345       (setq where  "`icicle-saved-completion-candidates'"
6346             icicle-saved-completion-candidates
6347             (if (and morep (listp icicle-saved-completion-candidates))
6348                 (append new-cands icicle-saved-completion-candidates)
6349               new-cands)))
6350     (deactivate-mark)
6351     (when (and (minibuffer-window-active-p (minibuffer-window))
6352                (get-buffer-window "*Completions*" 'visible))
6353       (icicle-display-candidates-in-Completions))
6354     (save-selected-window
6355       (select-window (minibuffer-window))
6356       (minibuffer-message
6357        (if morep
6358            (if new-cands
6359                (format "  [%sandidates ADDED to %s]" (if only-selected-p "Selected c" "C") where)
6360              "  [NO candidates selected to add]")
6361          (if new-cands
6362              (format "  [%sandidates SAVED to %s]" (if only-selected-p "Selected c" "C") where)
6363            "  [Saved candidates reset to NONE]"))))))
6364
6365 ;; This is actually a top-level command, but it is in this file because it is used by
6366 ;; `icicle-candidate-set-save-1'.
6367 ;;
6368 ;; We don't define this using `icicle-define-add-to-alist-command', because we want to
6369 ;; return the cache-file name.
6370 ;;;###autoload
6371 (defun icicle-add/update-saved-completion-set ()
6372   "Add or update an entry in `icicle-saved-completion-sets'.
6373 That is, create a new saved completion set or update an existing one.
6374 You are prompted for the name of a set of completion candidates and
6375 its cache file.  By default, the cache file name is the set name
6376 without spaces, and with file extension `icy'.  List
6377 `icicle-saved-completion-sets' is updated to have an entry with these
6378 set and file names.  Return the cache-file name."
6379   (interactive)
6380   (let* ((icicle-whole-candidate-as-text-prop-p  nil)
6381          (set-name                               (icicle-substring-no-properties
6382                                                   (completing-read
6383                                                    "Saved completion set: "
6384                                                    icicle-saved-completion-sets nil nil nil
6385                                                    'icicle-completion-set-history)))
6386          (file-name                              ""))
6387     (setq file-name  (expand-file-name
6388                       (read-file-name "Cache file for the set: " default-directory nil nil
6389                                       (concat (icicle-delete-whitespace-from-string set-name) ".icy"))))
6390     (while (not (icicle-file-writable-p file-name))
6391       (setq file-name  (expand-file-name
6392                         (read-file-name
6393                          "Cannot write to that file. Cache file: " default-directory nil nil
6394                          (concat (icicle-delete-whitespace-from-string set-name) ".icy")))))
6395     (setq icicle-saved-completion-sets  ; Remove any old definition of this set.
6396           (icicle-assoc-delete-all set-name icicle-saved-completion-sets))
6397     (push (cons set-name file-name) icicle-saved-completion-sets) ; Add new set definition.
6398     (funcall icicle-customize-save-variable-function
6399              'icicle-saved-completion-sets
6400              icicle-saved-completion-sets)
6401     (message "Added set to `icicle-saved-completion-sets': `%s'" set-name)
6402     file-name))                         ; Return cache-file name.
6403
6404 ;; Similar to `filesets-add-buffer', but that insists on a buffer.  This is actually a top-level
6405 ;; command, but it is in this file because it is used by `icicle-candidate-set-save-1'.
6406 ;;;###autoload
6407 (defun icicle-add-file-to-fileset (&optional file name)
6408   "Add FILE to the fileset called NAME.
6409 If FILE is nil, use file of current buffer.
6410 If NAME is nil, prompt for the fileset."
6411   (interactive)
6412   (unless (require 'filesets nil t) (error "Cannot find library `filesets'"))
6413   (setq file  (or file (buffer-file-name) (and (interactive-p) (read-file-name "File to add: "
6414                                                                                nil nil t))
6415                   (error "Current buffer has no associated file"))
6416         name  (or name (and (interactive-p) (completing-read (format "Add `%s' to fileset: " file)
6417                                                              filesets-data))
6418                   (error "No fileset")))
6419   (let ((entry  (or (assoc name filesets-data)
6420                     (and (interactive-p)
6421                          (when (y-or-n-p (format "Fileset `%s' does not exist. Create it? " name))
6422                            (add-to-list 'filesets-data (list name '(:files)))
6423                            (message "Fileset created.  Use `M-x filesets-save-config' to save it.")
6424                            (car filesets-data))))))
6425     (if (not entry)
6426         (when (interactive-p) (message "Operation cancelled - no fileset"))
6427       (let* ((files  (filesets-entry-get-files entry)))
6428         (cond ((filesets-member file files :test 'filesets-files-equalp)
6429                (message "`%s' is already in fileset `%s'" file name))
6430               ((and file (eq (filesets-entry-mode entry) ':files))
6431                (filesets-entry-set-files entry (cons file files) t)
6432                (filesets-set-config name 'filesets-data filesets-data))
6433               (t (error "Cannot add file. Fileset `%s' is not of type Files (:files)" name)))))))
6434
6435 (defun icicle-markers-to-readable (cand)
6436   "Convert (serialize) candidate CAND to Lisp-readable representation.
6437 CAND is a full completion candidate (collection alist entry).
6438 A Lisp-readable candidate uses one of the following forms to represent
6439 a marker:
6440   (icicle-file-marker  FILE-NAME    MARKER-POSITION)
6441   (icicle-marker       BUFFER-NAME  MARKER-POSITION)"
6442   (if (atom cand)
6443       (if (markerp cand)
6444           (let ((buf  (marker-buffer cand)))
6445             (unless buf (error "Marker in no buffer"))
6446             (list (if (buffer-file-name buf) 'icicle-file-marker 'icicle-marker)
6447                   (or (buffer-file-name buf) (buffer-name buf))
6448                   (marker-position cand)))
6449         cand)
6450     (cons (icicle-markers-to-readable (car cand)) (icicle-markers-to-readable (cdr cand)))))
6451
6452 ;;;###autoload
6453 (defun icicle-candidate-set-save-to-variable () ; Bound to `C-M-}' in minibuffer.
6454   "Save the set of current completion candidates in a variable you choose.
6455 You can retrieve the saved set of candidates with `\\<minibuffer-local-completion-map>\
6456 \\[icicle-candidate-set-retrieve-from-variable]' (or `\\[icicle-candidate-set-retrieve]'
6457 with a numeric prefix arg).
6458 You can use the saved set of candidates for operations such as
6459 \\<minibuffer-local-completion-map>
6460 `icicle-candidate-set-union' (`\\[icicle-candidate-set-union]'),
6461 `icicle-candidate-set-intersection' (`\\[icicle-candidate-set-intersection]'), and
6462 `icicle-candidate-set-difference' (`\\[icicle-candidate-set-difference]').
6463
6464 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6465 \\[icicle-candidate-set-save-to-variable]')."
6466   (interactive)
6467   (icicle-candidate-set-save 99))
6468
6469 ;;;###autoload
6470 (defun icicle-candidate-set-save-persistently (filesetp) ; Bound to `C-}' in minibuffer.
6471   "Save the set of current completion candidates persistently.
6472 With no prefix arg, save in a cache file.
6473 With a prefix arg, save in an Emacs fileset (Emacs 22 or later).
6474
6475 You can retrieve the saved set of candidates with `\\<minibuffer-local-completion-map>\
6476 \\[icicle-candidate-set-retrieve-persistent]' or `C-u \\[icicle-candidate-set-retrieve]'.
6477 You can use the saved set of candidates for operations such as
6478 \\<minibuffer-local-completion-map>
6479 `icicle-candidate-set-union' (`\\[icicle-candidate-set-union]'),
6480 `icicle-candidate-set-intersection' (`\\[icicle-candidate-set-intersection]'), and
6481 `icicle-candidate-set-difference' (`\\[icicle-candidate-set-difference]').
6482
6483 You can use this command only from the minibuffer (`\\<minibuffer-local-completion-map>\
6484 \\[icicle-candidate-set-save-persistently]')."
6485   (interactive "P")
6486   (icicle-candidate-set-save (if filesetp 0 '(1))))
6487
6488 ;;;###autoload
6489 (defun icicle-keep-only-past-inputs (&optional recent-first) ; Bound to`M-pause' in minibuffer.
6490   "Narrow completion candidates to those that have been used previously.
6491 This filters the set of current completion candidates, keeping only
6492 those that have been used before.  (You must first use `TAB' or
6493 `S-TAB' to establish an explicit candidate set.)
6494
6495 With a prefix arg, the previous inputs are sorted chronologically,
6496 most recent first.
6497
6498 Note that whatever completion mode (prefix or apropos) was in effect
6499 before you use `\\<minibuffer-local-completion-map>\
6500 \\[icicle-keep-only-past-inputs]' remains in \ effect for
6501 `icicle-keep-only-past-inputs'.  This command does not use a recursive
6502 minibuffer; it simply co-opts the current completion, changing it to
6503 completion against the history.
6504
6505 You can use this command only from the minibuffer \
6506 \(`\\[icicle-keep-only-past-inputs]').
6507
6508 See also `\\[icicle-history]' (`icicle-history')."
6509   (interactive "P")
6510   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
6511   (if (and recent-first (interactive-p) icicle-inhibit-sort-p)
6512       (icicle-msg-maybe-in-minibuffer "Cannot sort candidates now")
6513     (let ((icicle-sort-comparer  (if recent-first 'icicle-most-recent-first-p icicle-sort-comparer)))
6514       (when (or recent-first (eq icicle-last-completion-command 'icicle-keep-only-past-inputs))
6515         (icicle-complete-again-update 'no-display))
6516       (if (null icicle-completion-candidates)
6517           (minibuffer-message "  [No completion candidates to filter]")
6518         (unless (boundp minibuffer-history-variable) (set minibuffer-history-variable nil))
6519         (when (consp (symbol-value minibuffer-history-variable))
6520           (setq icicle-completion-candidates
6521                 (icicle-remove-if-not
6522                  (lambda (candidate)
6523                    (when (icicle-file-name-input-p)
6524                      (setq candidate  (expand-file-name
6525                                        candidate (icicle-file-name-directory icicle-last-input))))
6526                    (member candidate (symbol-value minibuffer-history-variable)))
6527                  icicle-completion-candidates))
6528           (cond ((null icicle-completion-candidates)
6529                  (save-selected-window (icicle-remove-Completions-window))
6530                  (minibuffer-message "  [None of the completions have been used before]"))
6531                 (t
6532                  (cond ((and (symbolp last-command) (get last-command 'icicle-cycling-command))
6533                         (setq icicle-current-input  icicle-last-input)
6534                         (icicle-retrieve-last-input))
6535                        (t
6536                         (setq icicle-current-input  (icicle-input-from-minibuffer))))
6537                  (cond ((null icicle-completion-candidates)
6538                         (setq icicle-nb-of-other-cycle-candidates  0)
6539                         (save-selected-window (icicle-remove-Completions-window))
6540                         (minibuffer-message "  [No matching history element]"))
6541                        ((null (cdr icicle-completion-candidates)) ; Single cand. Update minibuffer.
6542                         (setq icicle-nb-of-other-cycle-candidates  0)
6543                         (icicle-clear-minibuffer)
6544                         (setq icicle-last-completion-candidate  (car icicle-completion-candidates))
6545                         (let ((inserted  (if (and (icicle-file-name-input-p) insert-default-directory
6546                                                   (or (not (member icicle-last-completion-candidate
6547                                                                    icicle-extra-candidates))
6548                                                       icicle-extra-candidates-dir-insert-p))
6549                                              (icicle-abbreviate-or-expand-file-name
6550                                               icicle-last-completion-candidate
6551                                               (icicle-file-name-directory-w-default
6552                                                icicle-current-input))
6553                                            icicle-last-completion-candidate)))
6554                           (insert inserted))
6555                         (save-selected-window (icicle-remove-Completions-window))
6556                         (icicle-highlight-complete-input)
6557                         (icicle-show-help-in-mode-line icicle-last-completion-candidate)
6558                         (minibuffer-message (format "  [One matching history element]")))
6559                        (t
6560                         (when (member icicle-current-input icicle-completion-candidates)
6561                           (icicle-highlight-complete-input)
6562                           (icicle-show-help-in-mode-line icicle-current-input))
6563                         (icicle-display-candidates-in-Completions)
6564                         (save-window-excursion
6565                           (select-window (active-minibuffer-window))
6566                           (minibuffer-message
6567                            (concat "  [Filtered to (matching) historical candidates"
6568                                    (and recent-first ", most recent first")
6569                                    "]")))))
6570                  (setq icicle-last-completion-command  'icicle-keep-only-past-inputs)))))
6571       icicle-completion-candidates)))
6572
6573 ;;;###autoload
6574 (defun icicle-other-history (arg)       ; Bound to `C-M-pause' in minibuffer.
6575   "Choose a history, or complete against `icicle-interactive-history'.
6576 For Emacs 23 or later, if no prefix arg and you are completing a
6577   command, abbrev, or keyboard macro name, then complete against
6578   (non-nil) `icicle-interactive-history'.
6579 Otherwise, prompt with completion for a minibuffer history to use.
6580   The history choice lasts only for the current (main) completion.
6581   (To then complete against this history, use `M-h'.)"
6582   (interactive "P")
6583   (if (and (> emacs-major-version 22)
6584            (memq minibuffer-history-variable
6585                  '(extended-command-history icicle-command-abbrev-history icicle-kmacro-history))
6586            (not arg)
6587            icicle-interactive-history)
6588       (icicle-use-interactive-command-history)
6589     (call-interactively #'icicle-change-history-variable)))
6590
6591 ;;;###autoload
6592 (defun icicle-use-interactive-command-history ()
6593   "Complete input against `icicle-interactive-history'.
6594 This is a history of all Emacs commands called interactively.
6595 This history is available only for Emacs 23 and later, and only if
6596 option `icicle-populate-interactive-history-flag' is not nil."
6597   (interactive)
6598   (icicle-change-history-variable "icicle-interactive-history")
6599   (icicle-history))
6600
6601 ;;;###autoload
6602 (defun icicle-change-history-variable (hist-var)
6603   "Choose a history variable to use now for `minibuffer-history-variable'.
6604 Use completion to choose the history to use.
6605 The choice lasts only for the current (main) completion.
6606 Non-interactively, arg HIST-VAR is the (string) name of a history var."
6607   (interactive
6608    (let ((enable-recursive-minibuffers  t)
6609          (icicle-hist-vars              `((,(symbol-name minibuffer-history-variable))
6610                                           (,(symbol-name 'icicle-previous-raw-file-name-inputs))
6611                                           (,(symbol-name 'icicle-previous-raw-non-file-name-inputs)))))
6612      (when (and (boundp 'icicle-populate-interactive-history-flag) ; Emacs 23+.
6613                 icicle-populate-interactive-history-flag)
6614        (push (symbol-name 'icicle-interactive-history)  icicle-hist-vars))
6615      (mapatoms (lambda (x) (when (and (boundp x) (consp (symbol-value x))
6616                                       (stringp (car (symbol-value x)))
6617                                       (string-match "-\\(history\\|ring\\)\\'" (symbol-name x)))
6618                              (push (list (symbol-name x)) icicle-hist-vars))))
6619      (list (completing-read "Use history: " icicle-hist-vars nil t nil nil nil))))
6620   (when (interactive-p) (icicle-barf-if-outside-Completions-and-minibuffer))
6621   (setq minibuffer-history-variable  (intern hist-var)))
6622
6623 ;;;###autoload
6624 (defun icicle-scroll-forward (&optional arg) ; `C-M-v' in minibuffer.
6625   "Scroll `icicle-other-window' forward."
6626   (interactive "P")
6627   (let ((win  (if (window-live-p icicle-other-window)
6628                   icicle-other-window
6629                 (if (window-live-p icicle-orig-window)
6630                     icicle-orig-window
6631                   (get-buffer-window "*Completions*" 0)))))
6632     (when win (save-selected-window (select-window win) (scroll-up arg)))))
6633
6634 ;;;###autoload
6635 (defun icicle-scroll-backward (&optional arg) ; `C-M-S-v' (aka `C-M-V') in minibuffer.
6636   "Scroll `icicle-other-window' backward."
6637   (interactive "P")
6638   (let ((win  (if (window-live-p icicle-other-window)
6639                   icicle-other-window
6640                 (if (window-live-p icicle-orig-window)
6641                     icicle-orig-window
6642                   (get-buffer-window "*Completions*" 0)))))
6643     (when win (save-selected-window (select-window win) (scroll-down arg)))))
6644
6645 ;;;###autoload
6646 (defun icicle-scroll-Completions-forward (&optional reverse) ; `C-v' minib; `wheel-down' *Completions*.
6647   "Scroll the `*Completions*' window forward.
6648 With a prefix argument, or if `icicle-scroll-Completions-reverse-p' is
6649 non-nil, scroll backward."
6650   (interactive "P")
6651   (when (get-buffer-window "*Completions*" 0)
6652     (save-selected-window
6653       (select-window (get-buffer-window "*Completions*" 0))
6654       (when (if (interactive-p) reverse current-prefix-arg) ; Non-interactive use is for `TAB', `S-TAB'.
6655         (setq icicle-scroll-Completions-reverse-p  (not icicle-scroll-Completions-reverse-p)))
6656       (cond (icicle-scroll-Completions-reverse-p
6657              (if (not (= (window-start) (point-min)))
6658                  (scroll-down nil)
6659                (unless (= (window-end) (point-max))
6660                  (goto-char (point-max))
6661                  (scroll-down (1- (/ (window-height) 2)))
6662                  (beginning-of-line))))
6663             (t
6664              (if (not (= (window-end) (point-max)))
6665                  (scroll-up nil)
6666                (unless (= (window-start) (point-min))
6667                  (goto-char (icicle-start-of-candidates-in-Completions)))))))))
6668
6669 ;;;###autoload
6670 (defun icicle-scroll-Completions-backward () ; `M-v' in minibuf; `wheel-up' in `*Completions*'.
6671   "Scroll the `*Completions*' window backward.
6672 If `icicle-scroll-Completions-reverse-p' is non-nil, scroll forward."
6673   (interactive)
6674   (let ((icicle-scroll-Completions-reverse-p  (not icicle-scroll-Completions-reverse-p)))
6675     (icicle-scroll-Completions-forward)))
6676
6677 ;;;###autoload
6678 (defun icicle-history ()                ; Bound to `M-h' in minibuffer.
6679   "Access the appropriate history list using completion or cycling.
6680 Complete the current minibuffer input against items in the history
6681 list that is in use for the current command.
6682
6683 NOTE:
6684
6685 1. If the required input is a file or directory name, then the entire
6686 minibuffer input is what is matched against the history list.  The
6687 reason for this is that file names in the history list are usually
6688 absolute.  This is unlike the case for normal file-name completion,
6689 which assumes the default directory.
6690
6691 Keep this in mind for apropos (regexp) completion; it means that to
6692 match a file-name using a substring you must, in the minibuffer,
6693 either not specify a directory or explicitly use \".*\" before the
6694 file-name substring.
6695
6696 For example, `/foo/bar/lph' will not apropos-match the previously
6697 input file name `/foo/bar/alphabet-soup.el'; you should use either
6698 `/foo/bar/.*lph' or `lph' (no directory).
6699
6700 2. This also represents a difference in behavior compared to the
6701 similar command `icicle-keep-only-past-inputs' \
6702 \(`\\<minibuffer-local-completion-map>\
6703 \\[icicle-keep-only-past-inputs]' in the
6704 minibuffer).  That command simply filters the current set of
6705 completion candidates, which in the case of file-name completion is a
6706 set of relative file names.
6707
6708 3. Whatever completion mode (prefix or apropos) was in effect before
6709 you use `\\<minibuffer-local-completion-map>\ \\[icicle-history]' remains in \
6710 effect for `icicle-history'.  This command
6711 does not use a recursive minibuffer; it simply co-opts the current
6712 completion, changing it to completion against the history.
6713
6714 You can use this command only from the minibuffer \
6715 \(`\\[icicle-history]').
6716
6717 See also `\\[icicle-keep-only-past-inputs]' (`icicle-keep-only-past-inputs')."
6718   (interactive)
6719   (when (interactive-p) (icicle-barf-if-outside-minibuffer))
6720   (when (icicle-file-name-input-p)
6721     (setq minibuffer-completion-predicate  nil
6722           minibuffer-completing-file-name  nil))
6723   (when (and (arrayp minibuffer-completion-table) minibuffer-completion-predicate)
6724     (setq minibuffer-completion-predicate
6725           `(lambda (elt) (funcall ',minibuffer-completion-predicate
6726                           (intern (if (consp elt) (car elt) elt))))))
6727   (when (and (boundp minibuffer-history-variable) (consp (symbol-value minibuffer-history-variable)))
6728     (setq minibuffer-completion-table
6729           (mapcar #'list (icicle-remove-duplicates
6730                           ;; `command-history' is an exception: its entries are not strings.
6731                           (if (eq 'command-history minibuffer-history-variable)
6732                               (mapcar #'prin1-to-string (symbol-value minibuffer-history-variable))
6733                             (symbol-value minibuffer-history-variable))))))
6734   (save-selected-window (unless icicle-last-completion-command (icicle-apropos-complete)))
6735   (cond (icicle-cycling-p ;; $$$ (and (symbolp last-command) (get last-command 'icicle-cycling-command))
6736          (setq icicle-current-input  icicle-last-input)
6737          (icicle-retrieve-last-input))
6738         (t
6739          (setq icicle-current-input  (icicle-input-from-minibuffer)
6740                icicle-last-input     nil ; So `icicle-save-or-restore-input' thinks input has changed.
6741                last-command          'icicle-history)
6742          (funcall (or icicle-last-completion-command 'icicle-apropos-complete)))))
6743
6744 ;; Not actually a minibuffer command, since `isearch' technically uses the echo area.  This is not
6745 ;; shadowed by any `icicle-mode-map' binding, since `isearch-mode-map' is also a minor mode map.
6746 ;;;###autoload
6747 (defun icicle-isearch-complete ()       ; Bound to `M-TAB' and `M-o' in `isearch-mode-map'.
6748   "Complete the search string using candidates from the search ring."
6749   (interactive)
6750   (cond ((icicle-completing-p)          ; Cannot use the var here, since not sure to be in minibuf.
6751          (setq isearch-string  (if (fboundp 'field-string) (field-string) (buffer-string)))
6752          (when (icicle-isearch-complete-past-string)
6753            (if (fboundp 'delete-field) (delete-field) (erase-buffer))
6754            (insert isearch-string)))
6755         (t
6756          (icicle-isearch-complete-past-string)
6757          (setq isearch-message  (mapconcat 'isearch-text-char-description isearch-string ""))
6758          (isearch-edit-string))))
6759
6760 (when (fboundp 'text-scale-increase)    ; Bound to `C-x -' in the minibuffer (Emacs 23+).
6761   (defun icicle-doremi-zoom-Completions+ (&optional increment)
6762     "Zoom the text in buffer `*Completions*' incrementally.
6763 Use `=', `-', or the mouse wheel to increase or decrease text
6764 size.  You can use the `Meta' key (`M-=' or `M--') to increment in
6765 larger steps."
6766     (interactive "p")
6767     (unless (require 'doremi-frm nil t) (error "This command needs library `doremi-frm.el'."))
6768     (unless (get-buffer-window "*Completions*" 'visible)
6769       (if icicle-completion-candidates
6770           (icicle-display-candidates-in-Completions)
6771         (icicle-msg-maybe-in-minibuffer "Did you hit `TAB' or `S-TAB'?")))
6772     (let ((mini  (active-minibuffer-window)))
6773       (unwind-protect
6774            (save-selected-window
6775              (select-window (get-buffer-window "*Completions*" 'visible))
6776              (let ((enable-recursive-minibuffers  t)
6777                    (doremi-up-keys                '(?=))
6778                    (doremi-down-keys              '(?-))
6779                    (doremi-boost-up-keys          '(?\M-=))
6780                    (doremi-boost-down-keys        '(?\M--)))
6781                (doremi-buffer-font-size+ increment))
6782              (setq unread-command-events  ()))
6783         (unless mini (icicle-remove-Completions-window))))))
6784
6785 ;;;###autoload
6786 (defun icicle-doremi-candidate-width-factor+ (&optional increment) ; Bound to `C-x w' in minibuffer.
6787   "Change `icicle-candidate-width-factor' incrementally.
6788 Use `right', `left' or mouse wheel to increase or decrease.  You can
6789 use the `Meta' key (e.g. `M-right') to increment in larger steps.
6790
6791 Use `up', `down', or the mouse wheel to adjust
6792 `icicle-inter-candidates-min-spaces'."
6793   (interactive "p")
6794   (unless (require 'doremi nil t) (error "This command needs library `doremi.el'."))
6795   (let ((mini  (active-minibuffer-window)))
6796     (unwind-protect
6797          (save-selected-window
6798            (select-window (minibuffer-window))
6799            (unless icicle-completion-candidates (message "Hit `TAB' or `S-TAB'"))
6800            (let ((enable-recursive-minibuffers  t)
6801                  (doremi-up-keys                '(left)) ; Rebind, so more intuitive for width.
6802                  (doremi-boost-up-keys          '(M-left))
6803                  (doremi-down-keys              '(right))
6804                  (doremi-boost-down-keys        '(M-right)))
6805              (doremi (lambda (new-val)
6806                        (setq new-val                        (doremi-wrap new-val 1 100)
6807                              icicle-candidate-width-factor  new-val)
6808                        (icicle-display-candidates-in-Completions)
6809                        new-val)
6810                      icicle-candidate-width-factor
6811                      (- increment)))    ; Reverse, so arrows correspond.
6812            (when (member (car unread-command-events)
6813                          (append doremi-up-keys   doremi-boost-up-keys 
6814                                  doremi-down-keys doremi-boost-down-keys))
6815              (icicle-doremi-inter-candidates-min-spaces+ increment))
6816            (setq unread-command-events  ()))
6817       (unless mini (icicle-remove-Completions-window)))))
6818
6819 ;;;###autoload
6820 (defun icicle-doremi-inter-candidates-min-spaces+ (&optional increment) ; Bound to `C-x |' in minibuf.
6821   "Change `icicle-inter-candidates-min-spaces' incrementally.
6822 Use `up', `down' or the mouse wheel to increase or decrease.  You can
6823  use the `Meta' key (e.g. `M-right') to increment in larger steps.
6824 Use `left', `right', or the mouse wheel to adjust
6825 `icicle-candidate-width-factor'."
6826   (interactive "p")
6827   (unless (require 'doremi nil t) (error "This command needs library `doremi.el'."))
6828   (let ((mini  (active-minibuffer-window)))
6829     (unwind-protect
6830          (save-selected-window
6831            (select-window (minibuffer-window))
6832            (unless icicle-completion-candidates (message "Hit `TAB' or `S-TAB'"))
6833            (let* ((enable-recursive-minibuffers  t))
6834              (doremi (lambda (new-val)
6835                        (setq new-val                             (doremi-limit new-val 1 nil)
6836                              icicle-inter-candidates-min-spaces  new-val)
6837                        (icicle-display-candidates-in-Completions)
6838                        new-val)
6839                      icicle-inter-candidates-min-spaces
6840                      increment))
6841            (when (member (car unread-command-events)'(left right M-left M-right))
6842              (icicle-doremi-candidate-width-factor+ increment))
6843            (setq unread-command-events  ()))
6844       (unless mini (icicle-remove-Completions-window)))))
6845
6846 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6847 ;;;###autoload
6848 (defalias 'toggle-icicle-WYSIWYG-Completions 'icicle-toggle-WYSIWYG-Completions)
6849 ;;;###autoload
6850 (defun icicle-toggle-WYSIWYG-Completions ()
6851   "Toggle the value of option `icicle-WYSIWYG-Completions-flag'."
6852   (interactive)
6853   (setq icicle-WYSIWYG-Completions-flag  (not icicle-WYSIWYG-Completions-flag))
6854   (icicle-msg-maybe-in-minibuffer (if icicle-WYSIWYG-Completions-flag
6855                                       "Using WYSIWYG for `*Completions*' display is now ON"
6856                                     "Using WYSIWYG for `*Completions*' display is now OFF")))
6857
6858 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6859 ;;;###autoload
6860 (defalias 'toggle-icicle-~-for-home-dir 'icicle-toggle-~-for-home-dir)
6861 ;;;###autoload
6862 (defun icicle-toggle-~-for-home-dir ()  ; Bound to `M-~' in minibuffer.
6863   "Toggle the value of option `icicle-use-~-for-home-dir-flag'.
6864 Bound to `M-~' in the minibuffer."
6865   (interactive)
6866   (setq icicle-use-~-for-home-dir-flag  (not icicle-use-~-for-home-dir-flag))
6867   (icicle-msg-maybe-in-minibuffer (if icicle-use-~-for-home-dir-flag
6868                                       "Using `~' for home directory is now ON"
6869                                     "Using `~' for home directory is now OFF")))
6870
6871 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6872 ;;;###autoload
6873 (defalias 'toggle-icicle-C-for-actions 'icicle-toggle-C-for-actions)
6874 ;;;###autoload
6875 (defun icicle-toggle-C-for-actions ()   ; Bound to `M-g' in minibuffer.
6876   "Toggle the value of option `icicle-use-C-for-actions-flag'.
6877 Bound to `M-g' in the minibuffer."
6878   (interactive)
6879   (setq icicle-use-C-for-actions-flag  (not icicle-use-C-for-actions-flag))
6880   (icicle-toggle-icicle-mode-twice)
6881   (icicle-msg-maybe-in-minibuffer (if icicle-use-C-for-actions-flag
6882                                       "Using `C-' prefix for multi-command actions is now ON"
6883                                     "Using `C-' prefix for multi-command actions is now OFF")))
6884
6885 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6886 ;;;###autoload
6887 (defalias 'toggle-icicle-alternative-sorting 'icicle-toggle-alternative-sorting)
6888 ;;;###autoload
6889 (defun icicle-toggle-alternative-sorting () ; Bound to `C-M-,' in minibuffer.
6890   "Toggle alternative sorting of minibuffer completion candidates.
6891 This swaps `icicle-alternative-sort-comparer' and `icicle-sort-comparer'.
6892 Bound to `C-M-,' in the minibuffer."
6893   (interactive)
6894   (let ((alt-sort-fn  icicle-alternative-sort-comparer))
6895     (setq icicle-alternative-sort-comparer  (or icicle-sort-comparer icicle-last-sort-comparer)
6896           icicle-sort-comparer              (or alt-sort-fn icicle-last-sort-comparer))
6897     (icicle-complete-again-update)
6898     (icicle-msg-maybe-in-minibuffer
6899      (format "Sorting: `%s', Alternative: `%s'"
6900              icicle-sort-comparer icicle-alternative-sort-comparer))))
6901
6902 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6903 ;;;###autoload
6904 (defalias 'toggle-icicle-sorting 'icicle-toggle-sorting)
6905 ;;;###autoload
6906 (defun icicle-toggle-sorting ()         ; Not bound to a key.
6907   "Toggle sorting of minibuffer completion candidates.
6908 When sorting is active, comparison is done by `icicle-sort-comparer'."
6909   (interactive)
6910   (if (and (interactive-p) icicle-inhibit-sort-p)
6911       (icicle-msg-maybe-in-minibuffer "Cannot sort candidates now")
6912     (if icicle-sort-comparer
6913         (setq icicle-last-sort-comparer  icicle-sort-comparer ; Save it, for restoring.
6914               icicle-sort-comparer       nil)
6915       (setq icicle-sort-comparer  icicle-last-sort-comparer)) ; Restore it.
6916     (icicle-complete-again-update)
6917     (icicle-msg-maybe-in-minibuffer (if icicle-sort-comparer
6918                                         "Completion-candidate sorting is now ON"
6919                                       "Completion-candidate sorting is now OFF"))))
6920
6921 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6922 ;;;###autoload
6923 (defalias 'toggle-icicle-angle-brackets 'icicle-toggle-angle-brackets)
6924 ;;;###autoload
6925 (defun icicle-toggle-angle-brackets ()
6926   "Toggle `icicle-key-descriptions-use-<>-flag'."
6927   (interactive)
6928   (setq icicle-key-descriptions-use-<>-flag  (not icicle-key-descriptions-use-<>-flag))
6929   (icicle-complete-again-update)
6930   (icicle-msg-maybe-in-minibuffer (if icicle-key-descriptions-use-<>-flag
6931                                       "Displaying <...> in key descriptions is now ON"
6932                                     "Displaying <...> in key descriptions is now OFF")))
6933
6934 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6935 ;;;###autoload
6936 (defalias 'toggle-icicle-proxy-candidates 'icicle-toggle-proxy-candidates)
6937 ;;;###autoload
6938 (defun icicle-toggle-proxy-candidates () ; Bound to `C-M-_' in minibuffer.
6939   "Toggle `icicle-add-proxy-candidates-flag'.
6940 Bound to `\\<minibuffer-local-completion-map>\\[icicle-toggle-proxy-candidates]' in the minibuffer.
6941 With some commands, you must re-invoke the command for the new value
6942 to take effect.  (This is for performance reasons.)"
6943   (interactive)
6944   (setq icicle-add-proxy-candidates-flag  (not icicle-add-proxy-candidates-flag)
6945         icicle-saved-proxy-candidates     (prog1 icicle-proxy-candidates
6946                                             (setq icicle-proxy-candidates
6947                                                   icicle-saved-proxy-candidates)))
6948   (icicle-complete-again-update)
6949   (icicle-msg-maybe-in-minibuffer (if icicle-add-proxy-candidates-flag
6950                                       "Including proxy candidates is now ON"
6951                                     "Including proxy candidates is now OFF")))
6952
6953 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6954 ;;;###autoload
6955 (defalias 'toggle-icicle-transforming 'icicle-toggle-transforming)
6956 ;;;###autoload
6957 (defun icicle-toggle-transforming ()    ; Bound to `C-$' in minibuffer.
6958   "Toggle transforming of minibuffer completion candidates.
6959 When transforming is active, it is done by `icicle-transform-function'.
6960
6961 By default, transformation, if active, simply removes duplicate
6962 candidates.  Icicles commands already \"do the right thing\" when it
6963 comes to duplicate removal, so you might never need this command.
6964
6965 Bound to `C-$' in the minibuffer."
6966   (interactive)
6967   (if icicle-transform-function
6968       (setq icicle-last-transform-function  icicle-transform-function ; Save it, for restoring.
6969             icicle-transform-function       nil)
6970     (setq icicle-transform-function  icicle-last-transform-function)) ; Restore it.
6971   (icicle-complete-again-update)
6972   (icicle-msg-maybe-in-minibuffer (if icicle-transform-function
6973                                       "Completion-candidate transformation is now ON"
6974                                     "Completion-candidate transformation is now OFF")))
6975
6976 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6977 ;;;###autoload
6978 (defalias 'toggle-icicle-incremental-completion 'icicle-toggle-incremental-completion)
6979 ;;;###autoload
6980 (defun icicle-toggle-incremental-completion () ; Bound to `C-#' in minibuffer.
6981   "Toggle the value of option `icicle-incremental-completion-flag'.
6982 If the current value is t or `always', then it is set to nil.
6983 If the current value is nil, then it is set to t.
6984 This command never sets the value to non-nil and non-t.
6985
6986 Bound to `C-#' in the minibuffer."
6987   (interactive)
6988   (setq icicle-incremental-completion-flag  (not icicle-incremental-completion-flag)
6989         icicle-incremental-completion-p     icicle-incremental-completion-flag)
6990   (icicle-msg-maybe-in-minibuffer (if icicle-incremental-completion-flag
6991                                       "Incremental completion is now ON"
6992                                     "Incremental completion is now OFF")))
6993
6994 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
6995 ;;;###autoload
6996 (defalias 'toggle-icicle-expand-to-common-match 'icicle-toggle-expand-to-common-match)
6997 ;;;###autoload
6998 (defun icicle-toggle-expand-to-common-match () ; Bound to `C-;' in minibuffer.
6999   "Toggle the value of `icicle-expand-input-to-common-match-flag'.
7000 Bound to `C-;' in the minibuffer."
7001   (interactive)
7002   (setq icicle-expand-input-to-common-match-flag  (not icicle-expand-input-to-common-match-flag))
7003   (icicle-msg-maybe-in-minibuffer (if icicle-expand-input-to-common-match-flag
7004                                       "Expanding input to common match is now ON"
7005                                     "Expanding input to common match is now OFF")))
7006
7007 ;;;###autoload
7008 (defun icicle-dispatch-C-^ ()           ; Bound to `C-^' in minibuffer.
7009   "Do the right thing for `C-^'
7010 When Icicles searching, call `icicle-toggle-highlight-all-current'.
7011 Otherwise, call `icicle-toggle-remote-file-testing'.
7012 Bound to `C-^' in the minibuffer."
7013   (interactive)
7014   (if icicle-searching-p (icicle-toggle-highlight-all-current) (icicle-toggle-remote-file-testing)))
7015
7016 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7017 ;;;###autoload
7018 (defalias 'toggle-icicle-remote-file-testing 'icicle-toggle-remote-file-testing)
7019 ;;;###autoload
7020 (defun icicle-toggle-remote-file-testing () ; Bound to `C-^' in minibuffer.
7021   "Toggle `icicle-test-for-remote-files-flag'.
7022 If you use Tramp for accessing remote files, then turning this off
7023 also turns off Tramp file-name completion.  Therefore, if you use this
7024 command to turn off testing of remote file names, then use it also to
7025 turn testing back on (instead of just setting the option to non-nil).
7026
7027 Bound to `C-^' in the minibuffer, except during Icicles searching."
7028   (interactive)
7029   (setq icicle-test-for-remote-files-flag  (not icicle-test-for-remote-files-flag))
7030   (when (require 'tramp nil t)
7031     (if (not icicle-test-for-remote-files-flag)
7032         (tramp-unload-file-name-handlers) ; Turn off Tramp remote file-name completion.
7033       ;; Bind `partial-completion-mode' to force Tramp file-name handlers unconditionally, for older
7034       ;; Tramp versions than 2.1 (ugly HACK).  This code should work for all Tramp versions.
7035       (let ((non-essential            t) ; Emacs 23.2+
7036             (partial-completion-mode  t))
7037         (condition-case nil
7038             (tramp-register-file-name-handlers) ; Emacs 22+
7039           (void-function
7040            (tramp-register-file-name-handler) ; The order of these two matters.
7041            (tramp-register-completion-file-name-handler))))))
7042   (message "Updating completions...")
7043   (icicle-complete-again-update)
7044   (icicle-msg-maybe-in-minibuffer (if icicle-test-for-remote-files-flag
7045                                       "Testing remote file names is now ON"
7046                                     "Testing remote file names is now OFF")))
7047
7048 ;; NOT a top-level command (most toggle commands can be used at top-level).
7049 ;;;###autoload
7050 (defalias 'toggle-icicle-highlight-all-current 'icicle-toggle-highlight-all-current)
7051 ;;;###autoload
7052 (defun icicle-toggle-highlight-all-current () ; Bound to `C-^' in minibuffer.
7053   "Toggle `icicle-search-highlight-all-current-flag'.
7054 Bound to `C-^' in the minibuffer during Icicles searching (only)."
7055   (interactive)
7056   (icicle-barf-if-outside-Completions-and-minibuffer)
7057   (setq icicle-search-highlight-all-current-flag  (not icicle-search-highlight-all-current-flag))
7058   ;; Rehighlight to see effect of toggle.
7059   (let ((icicle-candidate-nb  icicle-candidate-nb))
7060     (let ((icicle-current-input             icicle-current-input)
7061           (icicle-incremental-completion-p  nil))
7062       (icicle-erase-minibuffer))
7063     (icicle-retrieve-last-input)
7064     (funcall (or icicle-last-completion-command  (if (eq icicle-current-completion-mode 'prefix)
7065                                                      #'icicle-prefix-complete
7066                                                    #'icicle-apropos-complete))))
7067   (icicle-search-highlight-all-input-matches icicle-current-input)
7068   (when icicle-candidate-nb (icicle-search-action "DUMMY")) ; Get back to current.
7069   (select-window (minibuffer-window))
7070   (select-frame-set-input-focus (selected-frame))
7071   (icicle-msg-maybe-in-minibuffer
7072    (if icicle-search-highlight-all-current-flag
7073        "Highlighting current input match in each main search hit is now ON"
7074      "Highlighting current input match in each main search hit is now OFF")))
7075
7076 ;;;###autoload
7077 (defun icicle-dispatch-C-x. (arg)       ; Bound to `C-x .' in minibuffer.
7078   "Do the right thing for `C-x .'.
7079 With a prefix arg, call `icicle-toggle-hiding-non-matching-lines'.
7080 With no prefix arg, call `icicle-toggle-hiding-common-match'.
7081 Bound to `C-x .' in the minibuffer."
7082   (interactive "P")
7083   (if arg (icicle-toggle-hiding-non-matching-lines) (icicle-toggle-hiding-common-match)))
7084
7085 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7086 ;;;###autoload
7087 (defalias 'toggle-icicle-hiding-common-match 'icicle-toggle-hiding-common-match)
7088 ;;;###autoload
7089 (defun icicle-toggle-hiding-common-match () ; Bound to `C-x .' in minibuffer.
7090   "Toggle `icicle-hide-common-match-in-Completions-flag'.
7091 Bound to `C-x .' (no prefix arg) in the minibuffer.
7092 See also option `icicle-hide-non-matching-lines-flag'."
7093   (interactive)
7094   (setq icicle-hide-common-match-in-Completions-flag
7095         (not icicle-hide-common-match-in-Completions-flag))
7096   (icicle-complete-again-update)
7097   (icicle-msg-maybe-in-minibuffer (if icicle-hide-common-match-in-Completions-flag
7098                                       "Hiding common match in `*Completions*' is now ON"
7099                                     "Hiding common match in `*Completions*' is now OFF")))
7100
7101 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7102 ;;;###autoload
7103 (defalias 'toggle-icicle-hiding-non-matching-lines 'icicle-toggle-hiding-non-matching-lines)
7104 ;;;###autoload
7105 (defun icicle-toggle-hiding-non-matching-lines () ; Bound to `C-u C-x .' in minibuffer.
7106   "Toggle `icicle-hide-non-matching-lines-flag'.
7107 Bound to `C-u C-x .' in the minibuffer.
7108 See also option `icicle-hide-common-match-in-Completions-flag'."
7109   (interactive)
7110   (setq icicle-hide-non-matching-lines-flag  (not icicle-hide-non-matching-lines-flag))
7111   (icicle-complete-again-update)
7112   (icicle-msg-maybe-in-minibuffer
7113    (if icicle-hide-non-matching-lines-flag
7114        "Hiding non-matching candidate lines in `*Completions*' is now ON"
7115      "Hiding non-matching candidate lines in `*Completions*' is now OFF")))
7116
7117 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7118 ;;;###autoload
7119 (defalias 'toggle-icicle-show-multi-completion 'icicle-toggle-show-multi-completion)
7120 ;;;###autoload
7121 (defun icicle-toggle-show-multi-completion () ; Bound to `M-m' in minibuffer.
7122   "Toggle `icicle-show-multi-completion-flag'.
7123 Bound to `M-m' in the minibuffer."
7124   (interactive)
7125   (setq icicle-show-multi-completion-flag  (not icicle-show-multi-completion-flag))
7126   (icicle-complete-again-update)
7127   (icicle-msg-maybe-in-minibuffer
7128    (if icicle-show-multi-completion-flag
7129        "Showing multi-completions (when available) is now ON"
7130      "Showing multi-completions (when available) is now OFF")))
7131
7132 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7133 ;;;###autoload
7134 (defalias 'toggle-icicle-ignored-space-prefix 'icicle-toggle-ignored-space-prefix)
7135 ;;;###autoload
7136 (defun icicle-toggle-ignored-space-prefix () ; Bound to `M-_' in minibuffer, except when searching.
7137   "Toggle `icicle-ignore-space-prefix-flag'.
7138 Bound to `M-_' in the minibuffer, except during Icicles searching.
7139
7140 Note: If the current command binds `icicle-ignore-space-prefix-flag'
7141 locally, then it is the local, not the global, value that is changed.
7142 For example, `icicle-buffer' binds it to the value of
7143 `icicle-buffer-ignore-space-prefix-flag'.  If that is non-nil, then
7144 \\<minibuffer-local-completion-map>`\\[icicle-toggle-ignored-space-prefix]' toggles \
7145 `icicle-ignore-space-prefix-flag' to nil only for the
7146 duration of `icicle-buffer'."
7147   (interactive)
7148   (setq icicle-ignore-space-prefix-flag  (not icicle-ignore-space-prefix-flag))
7149   (icicle-complete-again-update)
7150   (icicle-msg-maybe-in-minibuffer (if icicle-ignore-space-prefix-flag
7151                                       "Ignoring space prefix is now ON"
7152                                     "Ignoring space prefix is now OFF")))
7153
7154 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7155 ;;;###autoload
7156 (defalias 'toggle-icicle-highlight-historical-candidates
7157     'icicle-toggle-highlight-historical-candidates)
7158 ;;;###autoload
7159 (defun icicle-toggle-highlight-historical-candidates () ; Bound to `C-pause' in minibuffer.
7160   "Toggle `icicle-highlight-historical-candidates-flag'.
7161 Bound to `C-pause' in the minibuffer."
7162   (interactive)
7163   (setq icicle-highlight-historical-candidates-flag  (not icicle-highlight-historical-candidates-flag))
7164   (icicle-complete-again-update)
7165   (icicle-msg-maybe-in-minibuffer
7166    (if icicle-highlight-historical-candidates-flag
7167        "Highlighting previously used inputs in `*Completions*' is now ON"
7168      "Highlighting previously used inputs in `*Completions*' is now OFF")))
7169
7170 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7171 ;;;###autoload
7172 (defalias 'toggle-icicle-highlight-saved-candidates
7173     'icicle-toggle-highlight-saved-candidates)
7174 ;;;###autoload
7175 (defun icicle-toggle-highlight-saved-candidates () ; Bound to `S-pause' in minibuffer.
7176   "Toggle `icicle-highlight-saved-candidates-flag'.
7177 Bound to `S-pause' in the minibuffer."
7178   (interactive)
7179   (setq icicle-highlight-saved-candidates-flag  (not icicle-highlight-saved-candidates-flag))
7180   (icicle-complete-again-update)
7181   (icicle-msg-maybe-in-minibuffer
7182    (if icicle-highlight-saved-candidates-flag
7183        "Highlighting saved candidates in `*Completions*' is now ON"
7184      "Highlighting saved candidates in `*Completions*' is now OFF")))
7185
7186 ;;;###autoload
7187 (defun icicle-dispatch-C-. ()           ; Bound to `C-.' in minibuffer.
7188   "Do the right thing for `C-.'.
7189 When using Icicles search (`icicle-search' and similar commands), call
7190  `icicle-toggle-search-cleanup'.
7191 Otherwise, call `icicle-toggle-ignored-extensions'.
7192
7193 Bound to `C-.' in the minibuffer."
7194   (interactive)
7195   (if icicle-searching-p (icicle-toggle-search-cleanup) (icicle-toggle-ignored-extensions)))
7196
7197 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7198 ;;;###autoload
7199 (defalias 'toggle-icicle-ignored-extensions 'icicle-toggle-ignored-extensions)
7200 ;;;###autoload
7201 (defun icicle-toggle-ignored-extensions () ; Bound to `C-.' in minibuffer except in Icicles search.
7202   "Toggle respect of `completion-ignored-extensions'.
7203 Bound to `C-.' in minibuffer during file-name input."
7204   (interactive)
7205   (if (consp completion-ignored-extensions)
7206       (setq icicle-saved-ignored-extensions   completion-ignored-extensions ; Save it.
7207             completion-ignored-extensions     ()
7208             icicle-ignored-extensions-regexp  nil)
7209     (setq completion-ignored-extensions  icicle-saved-ignored-extensions ; Restore it.
7210           icicle-ignored-extensions-regexp ; Make regexp for ignored file extensions.
7211           (concat "\\(" (mapconcat #'regexp-quote completion-ignored-extensions "$\\|") "$\\)\\'")))
7212   ;; Flag to prevent updating `icicle-ignored-extensions-regexp' unless
7213   ;; `completion-ignored-extensions' changes.
7214   (setq icicle-ignored-extensions  completion-ignored-extensions)
7215   (icicle-complete-again-update)
7216   (icicle-msg-maybe-in-minibuffer (if completion-ignored-extensions
7217                                       "Ignoring selected file extensions is now ON"
7218                                     "Ignoring selected file extensions is now OFF")))
7219
7220 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7221 ;;;###autoload
7222 (defalias 'toggle-icicle-search-cleanup 'icicle-toggle-search-cleanup)
7223 ;;;###autoload
7224 (defun icicle-toggle-search-cleanup ()  ; Bound to `C-.' in minibuffer during Icicles search.
7225   "Toggle removal of `icicle-search' highlighting after a search.
7226 This toggles option `icicle-search-cleanup-flag'.
7227 Bound to `C-.' in the minibuffer during Icicles search."
7228   (interactive)
7229   (setq icicle-search-cleanup-flag  (not icicle-search-cleanup-flag))
7230   (icicle-complete-again-update)
7231   (icicle-msg-maybe-in-minibuffer (if icicle-search-cleanup-flag
7232                                       "Removal of Icicles search highlighting is now ON"
7233                                     "Removal of Icicles search highlighting is now OFF")))
7234
7235 ;;;###autoload
7236 (defalias 'toggle-icicle-search-complementing-domain 'icicle-toggle-search-complementing-domain)
7237 ;;;###autoload
7238 (defun icicle-toggle-search-complementing-domain () ; Bound to `C-M-~' in minibuffer.
7239   "Toggle searching the complements of the normal search contexts.
7240 This toggles internal variable `icicle-search-complement-domain-p'.
7241 If toggled during search it affects the next, not the current, search.
7242 Bound to `C-M-~' in the minibuffer."
7243   (interactive)
7244   (setq icicle-search-complement-domain-p  (not icicle-search-complement-domain-p))
7245   (icicle-msg-maybe-in-minibuffer
7246    (format "Future Icicles searches %suse the COMPLEMENT of the search domain"
7247            (if icicle-search-complement-domain-p "" "do NOT "))))
7248
7249 ;;$$$ (defun icicle-dispatch-C-backquote ()   ; Bound to `C-`' in minibuffer.
7250 ;;   "Do the right thing for `C-`'.
7251 ;; When searching, call `icicle-toggle-literal-replacement'.
7252 ;; Otherwise, call `icicle-toggle-regexp-quote'.
7253
7254 ;; Bound to `C-`' in the minibuffer."
7255 ;;   (interactive)
7256 ;;   (if icicle-searching-p (icicle-toggle-literal-replacement) (icicle-toggle-regexp-quote)))
7257
7258
7259 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7260 ;;;###autoload
7261 (defalias 'toggle-icicle-regexp-quote 'icicle-toggle-regexp-quote)
7262 ;;;###autoload
7263 (defun icicle-toggle-regexp-quote ()    ; Bound to `C-`' in minibuffer.
7264   "Toggle escaping of regexp special chars (`icicle-regexp-quote-flag').
7265 Bound to `C-`' in the minibuffer."
7266   (interactive)
7267   (setq icicle-regexp-quote-flag  (not icicle-regexp-quote-flag))
7268   (icicle-complete-again-update)
7269   (icicle-msg-maybe-in-minibuffer (if icicle-regexp-quote-flag
7270                                       "Escaping of regexp special characters is now ON"
7271                                     "Escaping of regexp special characters is now OFF")))
7272
7273 ;;;###autoload
7274 (defun icicle-regexp-quote-input (beg end) ; Bound to `M-%' in minibuffer.
7275   "Regexp quote current input or its active region, then apropos-complete.
7276 Use this if you want to literally match all of what is currently in
7277 the minibuffer or selected text there, but you also want to use that
7278 literal text as part of a regexp for apropos completion.
7279
7280 This turns off `icicle-expand-input-to-common-match-flag'.
7281 You can toggle that option using `C-;'.
7282
7283 Bound to `M-%' in the minibuffer."
7284   (interactive (if (and mark-active (mark))
7285                    (list (region-beginning) (region-end))
7286                  (list (point-max) (point-max))))
7287   (icicle-barf-if-outside-Completions-and-minibuffer)
7288   (let ((regionp  (and mark-active (mark) (/= (point) (mark))))
7289         quoted-part)
7290     (save-excursion
7291       (save-restriction
7292         (narrow-to-region (if regionp beg (icicle-minibuffer-prompt-end)) (if regionp end (point-max)))
7293         (setq quoted-part  (regexp-quote (icicle-input-from-minibuffer)))
7294         (delete-region (icicle-minibuffer-prompt-end) (point-max))
7295         (insert quoted-part))))
7296   (setq icicle-current-input                      (icicle-input-from-minibuffer)
7297         icicle-expand-input-to-common-match-flag  nil)
7298   (icicle-apropos-complete)
7299   (icicle-msg-maybe-in-minibuffer (substitute-command-keys "Expansion to common match is OFF. \
7300 `\\<minibuffer-local-completion-map>\\[icicle-toggle-expand-to-common-match]' to toggle")))
7301
7302 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7303 ;;;###autoload
7304 (defalias 'toggle-icicle-literal-replacement 'icicle-toggle-literal-replacement)
7305 ;;;###autoload
7306 (defun icicle-toggle-literal-replacement () ; Bound to `C-M-`' in minibuffer.
7307   "Toggle escaping of regexp special chars in replacement text.
7308 This toggles option `icicle-search-replace-literally-flag'.
7309
7310 Bound to `C-M-`' in the minibuffer."
7311   (interactive)
7312   (setq icicle-search-replace-literally-flag  (not icicle-search-replace-literally-flag))
7313   (icicle-msg-maybe-in-minibuffer (if icicle-search-replace-literally-flag
7314                                       "Replacement of text literally is now ON"
7315                                     "Replacement of text literally is now OFF")))
7316
7317 ;; Top-level commands.  Could instead be in `icicles-cmd2.el'.
7318 ;;;###autoload
7319 (defalias 'toggle-icicle-case-sensitivity 'icicle-toggle-case-sensitivity)
7320 ;;;###autoload
7321 (defun icicle-toggle-case-sensitivity (file+buff-p) ; Bound to `C-S-a' in minibuffer, i.e., `C-A'.
7322   "Toggle case sensitivity.
7323
7324 This toggles `case-fold-search' and `completion-ignore-case'.  With a
7325 prefix arg, it also toggles `read-file-name-completion-ignore-case'
7326 \(Emacs 22 and later) and `read-buffer-completion-ignore-case' (Emacs
7327 23 and later).
7328
7329 More precisely, this command first toggles the default value of
7330 `case-fold-search', and then it sets the other variables to the value
7331 of `case-fold-search'.
7332
7333 Note:
7334 1. This toggles the default value of `case-fold-search'.  This means
7335 that it does not matter which buffer is current when you call this
7336 command - all buffers are affected henceforth.
7337
7338 2. Some Icicles commands bind one or more of these variables, so
7339 invoking this command during command execution will not necessarily
7340 toggle the global values of all of the variables.
7341
7342 Bound to `C-A' in the minibuffer, that is, `C-S-a'."
7343   (interactive "P")
7344   (setq-default case-fold-search        (not case-fold-search)
7345                 completion-ignore-case  case-fold-search)
7346   (when file+buff-p
7347     (when (boundp 'read-file-name-completion-ignore-case) ; Emacs 22+
7348       (setq read-file-name-completion-ignore-case  case-fold-search))
7349     (when (boundp 'read-buffer-completion-ignore-case) ; Emacs 23+
7350       (setq read-buffer-completion-ignore-case  case-fold-search)))
7351   (icicle-complete-again-update)
7352   (icicle-highlight-lighter)
7353   (icicle-msg-maybe-in-minibuffer
7354    (cond ((and case-fold-search
7355                (or (not (boundp 'read-file-name-completion-ignore-case))
7356                    read-file-name-completion-ignore-case)
7357                (or (not (boundp 'read-buffer-completion-ignore-case))
7358                    read-buffer-completion-ignore-case))
7359           "Case-sensitive comparison is now OFF, everywhere")
7360          (case-fold-search "Case-sensitive comparison is now OFF, except for files and buffers")
7361          (t "Case-sensitive comparison is now ON, everywhere"))))
7362
7363 ;; `icicle-delete-window' (`C-x 0') does this in minibuffer.
7364 ;; `icicle-abort-recursive-edit' call this with non-nil FORCE.
7365 ;;;###autoload
7366 (defun icicle-remove-Completions-window (&optional force)
7367   "Remove the `*Completions*' window.
7368 If not called interactively and `*Completions*' is the selected
7369 window, then do not remove it unless optional arg FORCE is non-nil."
7370   (interactive)
7371   ;; We do nothing if `*Completions*' is the selected window
7372   ;; or the minibuffer window is selected and `*Completions*' window was selected just before.
7373   (let ((swin  (selected-window)))
7374     ;; Emacs 20-21 has no `minibuffer-selected-window' function, but we just ignore that.
7375     (when (and (window-minibuffer-p swin) (fboundp 'minibuffer-selected-window))
7376       (setq swin  (minibuffer-selected-window)))
7377     (cond (;; `*Completions*' is shown in the selected frame.
7378            (and (get-buffer-window "*Completions*")
7379                 (or force               ; Let user use `C-g' to get rid of it even if selected.
7380                     (and (window-live-p swin) ; Not sure needed.
7381                          (not (eq (window-buffer swin) (get-buffer "*Completions*"))))
7382                     (interactive-p)))
7383            ;; Ignore error, in particular, "Attempt to delete the sole visible or iconified frame".
7384            (condition-case nil (delete-window (get-buffer-window "*Completions*")) (error nil))
7385            (bury-buffer (get-buffer "*Completions*")))
7386           (;; `*Completions*' is shown in a different frame.
7387            (and (get-buffer-window "*Completions*" 'visible)
7388                 (or force               ; Let user use `C-g' to get rid of it even if selected.
7389                     (and (window-live-p swin)
7390                          (not (eq (window-buffer swin) (get-buffer "*Completions*"))))
7391                     (interactive-p)))
7392            ;; Ignore error, in particular, "Attempt to delete the sole visible or iconified frame".
7393            (when (window-dedicated-p (get-buffer-window "*Completions*" 'visible))
7394              (condition-case nil (icicle-delete-windows-on "*Completions*")  (error nil)))
7395            (bury-buffer (get-buffer "*Completions*"))))))
7396
7397 ;; This is actually a top-level command, but it is in this file because it is used by
7398 ;; `icicle-remove-Completions-window'.
7399 ;;;###autoload
7400 (defun icicle-delete-windows-on (buffer)
7401   "Delete all windows showing BUFFER.
7402 If such a window is alone in its frame, then delete the frame - unless
7403 it is the only frame or a standalone minibuffer frame."
7404   (interactive
7405    (list (let ((enable-recursive-minibuffers  t))
7406            (read-buffer "Remove all windows showing buffer: " (current-buffer) 'existing))))
7407   (setq buffer  (get-buffer buffer))    ; Convert to buffer.
7408   (when buffer                          ; Do nothing if null BUFFER.
7409     ;; Avoid error message "Attempt to delete minibuffer or sole ordinary window".
7410     (let* ((this-buffer-frames  (icicle-frames-on buffer t))
7411            (this-frame          (car this-buffer-frames)))
7412       (unless (and this-frame (frame-visible-p this-frame)
7413                    (null (cdr this-buffer-frames)) ; Only one frame shows BUFFER.
7414                    (eq (cdr (assoc 'minibuffer (frame-parameters this-frame)))
7415                        (active-minibuffer-window)) ; Has an active minibuffer.
7416                    (save-window-excursion
7417                      (select-frame this-frame)
7418                      (one-window-p t 'SELECTED-FRAME-ONLY))) ; Only one window.
7419         (let (win)
7420           (dolist (fr  this-buffer-frames)
7421             (setq win  (get-buffer-window buffer fr))
7422             (select-window win)
7423             (if (and (one-window-p t) (cdr (visible-frame-list))) ; Sole window but not sole frame.
7424                 (delete-frame)
7425               (delete-window (selected-window)))))))))
7426
7427 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7428
7429 (provide 'icicles-mcmd)
7430
7431 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
7432 ;;; icicles-mcmd.el ends here