(Qkbd_macro_termination_hook): New variable.
[bpt/emacs.git] / lisp / isearch.el
1 ;;; isearch.el --- incremental search minor mode.
2
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: matching
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Instructions
30
31 ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
32 ;; isearch-mode behaves modally and does not return until the search
33 ;; is completed. It uses a recursive-edit to behave this way.
34
35 ;; The key bindings active within isearch-mode are defined below in
36 ;; `isearch-mode-map' which is given bindings close to the default
37 ;; characters of the original isearch.el. With `isearch-mode',
38 ;; however, you can bind multi-character keys and it should be easier
39 ;; to add new commands. One bug though: keys with meta-prefix cannot
40 ;; be longer than two chars. Also see minibuffer-local-isearch-map
41 ;; for bindings active during `isearch-edit-string'.
42
43 ;; isearch-mode should work even if you switch windows with the mouse,
44 ;; in which case isearch-mode is terminated automatically before the
45 ;; switch.
46
47 ;; The search ring and completion commands automatically put you in
48 ;; the minibuffer to edit the string. This gives you a chance to
49 ;; modify the search string before executing the search. There are
50 ;; three commands to terminate the editing: C-s and C-r exit the
51 ;; minibuffer and search forward and reverse respectively, while C-m
52 ;; exits and does a nonincremental search.
53
54 ;; Exiting immediately from isearch uses isearch-edit-string instead
55 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
56 ;; The name of this option should probably be changed if we decide to
57 ;; keep the behavior. No point in forcing nonincremental search until
58 ;; the last possible moment.
59
60 ;; TODO
61 ;; - Integrate the emacs 19 generalized command history.
62 ;; - Think about incorporating query-replace.
63 ;; - Hooks and options for failed search.
64
65 ;;; Change Log:
66
67 ;; Changes before those recorded in ChangeLog:
68
69 ;; Revision 1.4 92/09/14 16:26:02 liberte
70 ;; Added prefix args to isearch-forward, etc. to switch between
71 ;; string and regular expression searching.
72 ;; Added some support for lemacs.
73 ;; Added general isearch-highlight option - but only for lemacs so far.
74 ;; Added support for frame switching in emacs 19.
75 ;; Added word search option to isearch-edit-string.
76 ;; Renamed isearch-quit to isearch-abort.
77 ;; Numerous changes to comments and doc strings.
78 ;;
79 ;; Revision 1.3 92/06/29 13:10:08 liberte
80 ;; Moved modal isearch-mode handling into isearch-mode.
81 ;; Got rid of buffer-local isearch variables.
82 ;; isearch-edit-string used by ring adjustments, completion, and
83 ;; nonincremental searching. C-s and C-r are additional exit commands.
84 ;; Renamed all regex to regexp.
85 ;; Got rid of found-start and found-point globals.
86 ;; Generalized handling of upper-case chars.
87
88 ;; Revision 1.2 92/05/27 11:33:57 liberte
89 ;; Emacs version 19 has a search ring, which is supported here.
90 ;; Other fixes found in the version 19 isearch are included here.
91 ;;
92 ;; Also see variables search-caps-disable-folding,
93 ;; search-nonincremental-instead, search-whitespace-regexp, and
94 ;; commands isearch-toggle-regexp, isearch-edit-string.
95 ;;
96 ;; semi-modal isearching is supported.
97
98 ;; Changes for 1.1
99 ;; 3/18/92 Fixed invalid-regexp.
100 ;; 3/18/92 Fixed yanking in regexps.
101
102 ;;; Code:
103
104 \f
105 ;;; Some additional options and constants.
106
107 (defgroup isearch nil
108 "Incremental search minor mode."
109 :link '(emacs-commentary-link "isearch")
110 :link '(custom-manual "(emacs)Incremental Search")
111 :prefix "isearch-"
112 :prefix "search-"
113 :group 'matching)
114
115
116 (defcustom search-exit-option t
117 "*Non-nil means random control characters terminate incremental search."
118 :type 'boolean
119 :group 'isearch)
120
121 (defcustom search-slow-window-lines 1
122 "*Number of lines in slow search display windows.
123 These are the short windows used during incremental search on slow terminals.
124 Negative means put the slow search window at the top (normally it's at bottom)
125 and the value is minus the number of lines."
126 :type 'integer
127 :group 'isearch)
128
129 (defcustom search-slow-speed 1200
130 "*Highest terminal speed at which to use \"slow\" style incremental search.
131 This is the style where a one-line window is created to show the line
132 that the search has reached."
133 :type 'integer
134 :group 'isearch)
135
136 (defcustom search-upper-case 'not-yanks
137 "*If non-nil, upper case chars disable case fold searching.
138 That is, upper and lower case chars must match exactly.
139 This applies no matter where the chars come from, but does not
140 apply to chars in regexps that are prefixed with `\\'.
141 If this value is `not-yanks', yanked text is always downcased."
142 :type '(choice (const :tag "off" nil)
143 (const not-yanks)
144 (other :tag "on" t))
145 :group 'isearch)
146
147 (defcustom search-nonincremental-instead t
148 "*If non-nil, do a nonincremental search instead if exiting immediately.
149 Actually, `isearch-edit-string' is called to let you enter the search
150 string, and RET terminates editing and does a nonincremental search."
151 :type 'boolean
152 :group 'isearch)
153
154 (defcustom search-whitespace-regexp "\\s-+"
155 "*If non-nil, regular expression to match a sequence of whitespace chars.
156 This applies to regular expression incremental search.
157 You might want to use something like \"[ \\t\\r\\n]+\" instead.
158 In the Customization buffer, that is `[' followed by a space,
159 a tab, a carriage return (control-M), a newline, and `]+'."
160 :type 'regexp
161 :group 'isearch)
162
163 (defcustom search-highlight t
164 "*Non-nil means incremental search highlights the current match."
165 :type 'boolean
166 :group 'isearch)
167
168 (defcustom search-invisible 'open
169 "If t incremental search can match hidden text.
170 nil means don't match invisible text.
171 If the value is `open', if the text matched is made invisible by
172 an overlay having an `invisible' property and that overlay has a property
173 `isearch-open-invisible', then incremental search will show the contents.
174 \(This applies when using `outline.el' and `hideshow.el'.)"
175 :type '(choice (const :tag "Match hidden text" t)
176 (const :tag "Open overlays" open)
177 (const :tag "Don't match hidden text" nil))
178 :group 'isearch)
179
180 (defcustom isearch-hide-immediately t
181 "If non-nil, re-hide an invisible match right away.
182 This variable makes a difference when `search-invisible' is set to `open'.
183 It means that after search makes some invisible text visible
184 to show the match, it makes the text invisible again when the match moves.
185 Ordinarily the text becomes invisible again at the end of the search."
186 :type 'boolean
187 :group 'isearch)
188
189 (defvar isearch-mode-hook nil
190 "Function(s) to call after starting up an incremental search.")
191
192 (defvar isearch-mode-end-hook nil
193 "Function(s) to call after terminating an incremental search.")
194
195 ;;; Search ring.
196
197 (defvar search-ring nil
198 "List of search string sequences.")
199 (defvar regexp-search-ring nil
200 "List of regular expression search string sequences.")
201
202 (defcustom search-ring-max 16
203 "*Maximum length of search ring before oldest elements are thrown away."
204 :type 'integer
205 :group 'isearch)
206 (defcustom regexp-search-ring-max 16
207 "*Maximum length of regexp search ring before oldest elements are thrown away."
208 :type 'integer
209 :group 'isearch)
210
211 (defvar search-ring-yank-pointer nil
212 "Index in `search-ring' of last string reused.
213 nil if none yet.")
214 (defvar regexp-search-ring-yank-pointer nil
215 "Index in `regexp-search-ring' of last string reused.
216 nil if none yet.")
217
218 (defcustom search-ring-update nil
219 "*Non-nil if advancing or retreating in the search ring should cause search.
220 Default value, nil, means edit the string instead."
221 :type 'boolean
222 :group 'isearch)
223
224 ;;; Define isearch-mode keymap.
225
226 (defvar isearch-mode-map
227 (let* ((i 0)
228 (map (make-keymap)))
229 (or (vectorp (nth 1 map))
230 (char-table-p (nth 1 map))
231 (error "The initialization of isearch-mode-map must be updated"))
232 ;; Make all multibyte characters search for themselves.
233 (let ((l (generic-character-list))
234 (table (nth 1 map)))
235 (while l
236 (set-char-table-default table (car l) 'isearch-printing-char)
237 (setq l (cdr l))))
238 ;; Make function keys, etc, exit the search.
239 (define-key map [t] 'isearch-other-control-char)
240 ;; Control chars, by default, end isearch mode transparently.
241 ;; We need these explicit definitions because, in a dense keymap,
242 ;; the binding for t does not affect characters.
243 ;; We use a dense keymap to save space.
244 (while (< i ?\ )
245 (define-key map (make-string 1 i) 'isearch-other-control-char)
246 (setq i (1+ i)))
247
248 ;; Single-byte printing chars extend the search string by default.
249 (setq i ?\ )
250 (while (< i 256)
251 (define-key map (vector i) 'isearch-printing-char)
252 (setq i (1+ i)))
253
254 ;; To handle local bindings with meta char prefix keys, define
255 ;; another full keymap. This must be done for any other prefix
256 ;; keys as well, one full keymap per char of the prefix key. It
257 ;; would be simpler to disable the global keymap, and/or have a
258 ;; default local key binding for any key not otherwise bound.
259 (let ((meta-map (make-sparse-keymap)))
260 (define-key map (char-to-string meta-prefix-char) meta-map)
261 (define-key map [escape] meta-map))
262 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
263
264 ;; Several non-printing chars change the searching behavior.
265 (define-key map "\C-s" 'isearch-repeat-forward)
266 (define-key map "\C-r" 'isearch-repeat-backward)
267 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
268 ;; combinations can be used to repeat regexp isearches that can
269 ;; be used to start these searches.
270 (define-key map "\M-\C-s" 'isearch-repeat-forward)
271 (define-key map "\M-\C-r" 'isearch-repeat-backward)
272 (define-key map "\177" 'isearch-delete-char)
273 (define-key map "\C-g" 'isearch-abort)
274
275 ;; This assumes \e is the meta-prefix-char.
276 (or (= ?\e meta-prefix-char)
277 (error "Inconsistency in isearch.el"))
278 (define-key map "\e\e\e" 'isearch-cancel)
279 (define-key map [escape escape escape] 'isearch-cancel)
280
281 (define-key map "\C-q" 'isearch-quote-char)
282
283 (define-key map "\r" 'isearch-exit)
284 (define-key map "\C-j" 'isearch-printing-char)
285 (define-key map "\t" 'isearch-printing-char)
286 (define-key map " " 'isearch-whitespace-chars)
287 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
288
289 (define-key map "\C-w" 'isearch-yank-word)
290 (define-key map "\C-y" 'isearch-yank-line)
291
292 ;; Define keys for regexp chars * ? |.
293 ;; Nothing special for + because it matches at least once.
294 (define-key map "*" 'isearch-*-char)
295 (define-key map "?" 'isearch-*-char)
296 (define-key map "|" 'isearch-|-char)
297
298 ;;; Turned off because I find I expect to get the global definition--rms.
299 ;;; ;; Instead bind C-h to special help command for isearch-mode.
300 ;;; (define-key map "\C-h" 'isearch-mode-help)
301
302 (define-key map "\M-n" 'isearch-ring-advance)
303 (define-key map "\M-p" 'isearch-ring-retreat)
304 (define-key map "\M-y" 'isearch-yank-kill)
305
306 (define-key map "\M-\t" 'isearch-complete)
307
308 ;; Pass frame events transparently so they won't exit the search.
309 ;; In particular, if we have more than one display open, then a
310 ;; switch-frame might be generated by someone typing at another keyboard.
311 (define-key map [switch-frame] nil)
312 (define-key map [delete-frame] nil)
313 (define-key map [iconify-frame] nil)
314 (define-key map [make-frame-visible] nil)
315 ;; For searching multilingual text.
316 (define-key map "\C-\\" 'isearch-toggle-input-method)
317 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
318
319 ;; People expect to be able to paste with the mouse.
320 (define-key map [mouse-2] #'isearch-mouse-yank)
321 (define-key map [down-mouse-2] nil)
322
323 ;; Some bindings you may want to put in your isearch-mode-hook.
324 ;; Suggest some alternates...
325 (define-key map "\M-c" 'isearch-toggle-case-fold)
326 (define-key map "\M-r" 'isearch-toggle-regexp)
327 (define-key map "\M-e" 'isearch-edit-string)
328
329 map)
330 "Keymap for `isearch-mode'.")
331
332 (defvar minibuffer-local-isearch-map
333 (let ((map (make-sparse-keymap)))
334 (set-keymap-parent map minibuffer-local-map)
335 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
336 (define-key map "\M-n" 'isearch-ring-advance-edit)
337 (define-key map "\M-p" 'isearch-ring-retreat-edit)
338 (define-key map "\M-\t" 'isearch-complete-edit)
339 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
340 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
341 map)
342 "Keymap for editing isearch strings in the minibuffer.")
343
344 ;; Internal variables declared globally for byte-compiler.
345 ;; These are all set with setq while isearching
346 ;; and bound locally while editing the search string.
347
348 (defvar isearch-forward nil) ; Searching in the forward direction.
349 (defvar isearch-regexp nil) ; Searching for a regexp.
350 (defvar isearch-word nil) ; Searching for words.
351
352 (defvar isearch-cmds nil) ; Stack of search status sets.
353 (defvar isearch-string "") ; The current search string.
354 (defvar isearch-message "") ; text-char-description version of isearch-string
355
356 (defvar isearch-success t) ; Searching is currently successful.
357 (defvar isearch-invalid-regexp nil) ; Regexp not well formed.
358 (defvar isearch-within-brackets nil) ; Regexp has unclosed [.
359 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
360 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
361 (defvar isearch-barrier 0)
362 (defvar isearch-just-started nil)
363
364 ; case-fold-search while searching.
365 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
366 ; case in the search string is ignored.
367 (defvar isearch-case-fold-search nil)
368
369 ;; Used to save default value while isearch is active
370 (defvar isearch-original-minibuffer-message-timeout nil)
371
372 (defvar isearch-adjusted nil)
373 (defvar isearch-slow-terminal-mode nil)
374 ;;; If t, using a small window.
375 (defvar isearch-small-window nil)
376 (defvar isearch-opoint 0)
377 ;;; The window configuration active at the beginning of the search.
378 (defvar isearch-window-configuration nil)
379
380 ;; Flag to indicate a yank occurred, so don't move the cursor.
381 (defvar isearch-yank-flag nil)
382
383 ;;; A function to be called after each input character is processed.
384 ;;; (It is not called after characters that exit the search.)
385 ;;; It is only set from an optional argument to `isearch-mode'.
386 (defvar isearch-op-fun nil)
387
388 ;;; Is isearch-mode in a recursive edit for modal searching.
389 (defvar isearch-recursive-edit nil)
390
391 ;;; Should isearch be terminated after doing one search?
392 (defvar isearch-nonincremental nil)
393
394 ;; New value of isearch-forward after isearch-edit-string.
395 (defvar isearch-new-forward nil)
396
397 ;; Accumulate here the overlays opened during searching.
398 (defvar isearch-opened-overlays nil)
399
400 ;; The value of input-method-function when isearch is invoked.
401 (defvar isearch-input-method-function nil)
402
403 ;; A flag to tell if input-method-function is locally bound when
404 ;; isearch is invoked.
405 (defvar isearch-input-method-local-p nil)
406
407 ;; Value of `signal-hook-function' before setting our own.
408 (defvar isearch-old-signal-hook nil)
409
410 ;; Minor-mode-alist changes - kind of redundant with the
411 ;; echo area, but if isearching in multiple windows, it can be useful.
412
413 (or (assq 'isearch-mode minor-mode-alist)
414 (nconc minor-mode-alist
415 (list '(isearch-mode isearch-mode))))
416
417 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
418 (make-variable-buffer-local 'isearch-mode)
419
420 (define-key global-map "\C-s" 'isearch-forward)
421 (define-key esc-map "\C-s" 'isearch-forward-regexp)
422 (define-key global-map "\C-r" 'isearch-backward)
423 (define-key esc-map "\C-r" 'isearch-backward-regexp)
424
425 ;;; Entry points to isearch-mode.
426
427 (defun isearch-forward (&optional regexp-p no-recursive-edit)
428 "\
429 Do incremental search forward.
430 With a prefix argument, do an incremental regular expression search instead.
431 \\<isearch-mode-map>
432 As you type characters, they add to the search string and are found.
433 The following non-printing keys are bound in `isearch-mode-map'.
434
435 Type \\[isearch-delete-char] to cancel characters from end of search string.
436 Type \\[isearch-exit] to exit, leaving point at location found.
437 Type LFD (C-j) to match end of line.
438 Type \\[isearch-repeat-forward] to search again forward,\
439 \\[isearch-repeat-backward] to search again backward.
440 Type \\[isearch-yank-word] to yank word from buffer onto end of search\
441 string and search for it.
442 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
443 and search for it.
444 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
445 and search for it.
446 Type \\[isearch-quote-char] to quote control character to search for it.
447 \\[isearch-abort] while searching or when search has failed cancels input\
448 back to what has
449 been found successfully.
450 \\[isearch-abort] when search is successful aborts and moves point to\
451 starting point.
452
453 Also supported is a search ring of the previous 16 search strings.
454 Type \\[isearch-ring-advance] to search for the next item in the search ring.
455 Type \\[isearch-ring-retreat] to search for the previous item in the search\
456 ring.
457 Type \\[isearch-complete] to complete the search string using the search ring.
458
459 The above keys, bound in `isearch-mode-map', are often controlled by
460 options; do M-x apropos on search-.* to find them.
461 Other control and meta characters terminate the search
462 and are then executed normally (depending on `search-exit-option').
463 Likewise for function keys and mouse button events.
464
465 If this function is called non-interactively, it does not return to
466 the calling function until the search is done."
467
468 (interactive "P\np")
469 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
470
471 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
472 "\
473 Do incremental search forward for regular expression.
474 With a prefix argument, do a regular string search instead.
475 Like ordinary incremental search except that your input
476 is treated as a regexp. See \\[isearch-forward] for more info."
477 (interactive "P\np")
478 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
479
480 (defun isearch-backward (&optional regexp-p no-recursive-edit)
481 "\
482 Do incremental search backward.
483 With a prefix argument, do a regular expression search instead.
484 See \\[isearch-forward] for more information."
485 (interactive "P\np")
486 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
487
488 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
489 "\
490 Do incremental search backward for regular expression.
491 With a prefix argument, do a regular string search instead.
492 Like ordinary incremental search except that your input
493 is treated as a regexp. See \\[isearch-forward] for more info."
494 (interactive "P\np")
495 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
496
497
498 (defun isearch-mode-help ()
499 (interactive)
500 (describe-function 'isearch-forward)
501 (isearch-update))
502
503 \f
504 ;; isearch-mode only sets up incremental search for the minor mode.
505 ;; All the work is done by the isearch-mode commands.
506
507 ;; Not used yet:
508 ;;(defvar isearch-commands '(isearch-forward isearch-backward
509 ;; isearch-forward-regexp isearch-backward-regexp)
510 ;; "List of commands for which isearch-mode does not recursive-edit.")
511
512
513 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
514 "Start isearch minor mode. Called by `isearch-forward', etc.
515
516 \\{isearch-mode-map}"
517
518 ;; Initialize global vars.
519 (setq isearch-forward forward
520 isearch-regexp regexp
521 isearch-word word-p
522 isearch-op-fun op-fun
523 isearch-case-fold-search case-fold-search
524 isearch-string ""
525 isearch-message ""
526 isearch-cmds nil
527 isearch-success t
528 isearch-wrapped nil
529 isearch-barrier (point)
530 isearch-adjusted nil
531 isearch-yank-flag nil
532 isearch-invalid-regexp nil
533 isearch-within-brackets nil
534 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
535 (> (window-height)
536 (* 4 search-slow-window-lines)))
537 isearch-other-end nil
538 isearch-small-window nil
539 isearch-just-started t
540
541 isearch-opoint (point)
542 search-ring-yank-pointer nil
543 isearch-opened-overlays nil
544 isearch-input-method-function input-method-function
545 isearch-input-method-local-p (local-variable-p 'input-method-function)
546 regexp-search-ring-yank-pointer nil
547
548 ;; Save the original value of `minibuffer-message-timeout', and
549 ;; set it to nil so that isearch's messages don't get timed out.
550 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
551 minibuffer-message-timeout nil)
552
553 ;; We must bypass input method while reading key. When a user type
554 ;; printable character, appropriate input method is turned on in
555 ;; minibuffer to read multibyte characters.
556 (or isearch-input-method-local-p
557 (make-local-variable 'input-method-function))
558 (setq input-method-function nil)
559
560 (looking-at "")
561 (setq isearch-window-configuration
562 (if isearch-slow-terminal-mode (current-window-configuration) nil))
563
564 ;; Maybe make minibuffer frame visible and/or raise it.
565 (let ((frame (window-frame (minibuffer-window))))
566 (if (not (memq (frame-live-p frame) '(nil t)))
567 (progn
568 (make-frame-visible frame)
569 (if minibuffer-auto-raise
570 (raise-frame frame)))))
571
572 (setq isearch-mode " Isearch") ;; forward? regexp?
573 (force-mode-line-update)
574
575 (isearch-push-state)
576
577 (setq overriding-terminal-local-map isearch-mode-map)
578 (isearch-update)
579 (run-hooks 'isearch-mode-hook)
580
581 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
582 (setq isearch-old-signal-hook signal-hook-function
583 signal-hook-function 'isearch-done)
584
585 ;; isearch-mode can be made modal (in the sense of not returning to
586 ;; the calling function until searching is completed) by entering
587 ;; a recursive-edit and exiting it when done isearching.
588 (if recursive-edit
589 (let ((isearch-recursive-edit t))
590 (recursive-edit)))
591 isearch-success)
592
593
594 ;; Some high level utilities. Others below.
595
596 (defun isearch-update ()
597 ;; Called after each command to update the display.
598 (if (null unread-command-events)
599 (progn
600 (if (not (input-pending-p))
601 (isearch-message))
602 (if (and isearch-slow-terminal-mode
603 (not (or isearch-small-window
604 (pos-visible-in-window-p))))
605 (let ((found-point (point)))
606 (setq isearch-small-window t)
607 (move-to-window-line 0)
608 (let ((window-min-height 1))
609 (split-window nil (if (< search-slow-window-lines 0)
610 (1+ (- search-slow-window-lines))
611 (- (window-height)
612 (1+ search-slow-window-lines)))))
613 (if (< search-slow-window-lines 0)
614 (progn (vertical-motion (- 1 search-slow-window-lines))
615 (set-window-start (next-window) (point))
616 (set-window-hscroll (next-window)
617 (window-hscroll))
618 (set-window-hscroll (selected-window) 0))
619 (other-window 1))
620 (goto-char found-point)))
621 (if isearch-other-end
622 (if (< isearch-other-end (point)) ; isearch-forward?
623 (isearch-highlight isearch-other-end (point))
624 (isearch-highlight (point) isearch-other-end))
625 (isearch-dehighlight nil))
626 ))
627 (setq ;; quit-flag nil not for isearch-mode
628 isearch-adjusted nil
629 isearch-yank-flag nil)
630 (isearch-lazy-highlight-new-loop)
631 ;; We must prevent the point moving to the end of composition when a
632 ;; part of the composition has just been searched.
633 (setq disable-point-adjustment t))
634
635 (defun isearch-done (&optional nopush edit)
636 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
637 ,isearch-word ,isearch-forward
638 ,isearch-message
639 ,isearch-case-fold-search)))
640 (unless (equal (car command-history) command)
641 (setq command-history (cons command command-history))))
642
643 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
644 (setq signal-hook-function isearch-old-signal-hook)
645
646 ;; Called by all commands that terminate isearch-mode.
647 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
648 (setq overriding-terminal-local-map nil)
649 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
650 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
651 (isearch-dehighlight t)
652 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
653 (let ((found-start (window-start (selected-window)))
654 (found-point (point)))
655 (if isearch-window-configuration
656 (set-window-configuration isearch-window-configuration))
657
658 (if isearch-small-window
659 (goto-char found-point)
660 ;; Exiting the save-window-excursion clobbers window-start; restore it.
661 (set-window-start (selected-window) found-start t))
662
663 ;; If there was movement, mark the starting position.
664 ;; Maybe should test difference between and set mark iff > threshold.
665 (if (/= (point) isearch-opoint)
666 (or (and transient-mark-mode mark-active)
667 (progn
668 (push-mark isearch-opoint t)
669 (or executing-kbd-macro (> (minibuffer-depth) 0)
670 (message "Mark saved where search started"))))))
671
672 (setq isearch-mode nil)
673 (if isearch-input-method-local-p
674 (setq input-method-function isearch-input-method-function)
675 (kill-local-variable 'input-method-function))
676
677 (force-mode-line-update)
678
679 ;; If we ended in the middle of some intangible text,
680 ;; move to the further end of that intangible text.
681 (let ((after (if (eobp) nil
682 (get-text-property (point) 'intangible)))
683 (before (if (bobp) nil
684 (get-text-property (1- (point)) 'intangible))))
685 (when (and before after (eq before after))
686 (if isearch-forward
687 (goto-char (next-single-property-change (point) 'intangible))
688 (goto-char (previous-single-property-change (point) 'intangible)))))
689
690 (if (and (> (length isearch-string) 0) (not nopush))
691 ;; Update the ring data.
692 (isearch-update-ring isearch-string isearch-regexp))
693
694 (run-hooks 'isearch-mode-end-hook)
695 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
696
697 (defun isearch-update-ring (string &optional regexp)
698 "Add STRING to the beginning of the search ring.
699 REGEXP says which ring to use."
700 (if regexp
701 (if (or (null regexp-search-ring)
702 (not (string= string (car regexp-search-ring))))
703 (progn
704 (setq regexp-search-ring
705 (cons string regexp-search-ring))
706 (if (> (length regexp-search-ring) regexp-search-ring-max)
707 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
708 nil))))
709 (if (or (null search-ring)
710 (not (string= string (car search-ring))))
711 (progn
712 (setq search-ring (cons string search-ring))
713 (if (> (length search-ring) search-ring-max)
714 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
715
716 ;;; Switching buffers should first terminate isearch-mode.
717 ;;; ;; For Emacs 19, the frame switch event is handled.
718 ;;; (defun isearch-switch-frame-handler ()
719 ;;; (interactive) ;; Is this necessary?
720 ;;; ;; First terminate isearch-mode.
721 ;;; (isearch-done)
722 ;;; (isearch-clean-overlays)
723 ;;; (handle-switch-frame (car (cdr last-command-char))))
724
725 \f
726 ;; Commands active while inside of the isearch minor mode.
727
728 (defun isearch-exit ()
729 "Exit search normally.
730 However, if this is the first command after starting incremental
731 search and `search-nonincremental-instead' is non-nil, do a
732 nonincremental search instead via `isearch-edit-string'."
733 (interactive)
734 (if (and search-nonincremental-instead
735 (= 0 (length isearch-string)))
736 (let ((isearch-nonincremental t))
737 (isearch-edit-string)))
738 (isearch-done)
739 (isearch-clean-overlays))
740
741
742 (defun isearch-edit-string ()
743 "Edit the search string in the minibuffer.
744 The following additional command keys are active while editing.
745 \\<minibuffer-local-isearch-map>
746 \\[exit-minibuffer] to resume incremental searching with the edited string.
747 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
748 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
749 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
750 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
751 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
752 \\[isearch-complete-edit] to complete the search string using the search ring.
753 \\<isearch-mode-map>
754 If first char entered is \\[isearch-yank-word], then do word search instead."
755
756 ;; This code is very hairy for several reasons, explained in the code.
757 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
758 ;; If there were a way to catch any change of buffer from the minibuffer,
759 ;; this could be simplified greatly.
760 ;; Editing doesn't back up the search point. Should it?
761 (interactive)
762 (condition-case err
763 (progn
764 (let ((isearch-nonincremental isearch-nonincremental)
765
766 ;; Locally bind all isearch global variables to protect them
767 ;; from recursive isearching.
768 ;; isearch-string -message and -forward are not bound
769 ;; so they may be changed. Instead, save the values.
770 (isearch-new-string isearch-string)
771 (isearch-new-message isearch-message)
772 (isearch-new-forward isearch-forward)
773 (isearch-new-word isearch-word)
774
775 (isearch-regexp isearch-regexp)
776 (isearch-op-fun isearch-op-fun)
777 (isearch-cmds isearch-cmds)
778 (isearch-success isearch-success)
779 (isearch-wrapped isearch-wrapped)
780 (isearch-barrier isearch-barrier)
781 (isearch-adjusted isearch-adjusted)
782 (isearch-yank-flag isearch-yank-flag)
783 (isearch-invalid-regexp isearch-invalid-regexp)
784 (isearch-within-brackets isearch-within-brackets)
785 ;;; Don't bind this. We want isearch-search, below, to set it.
786 ;;; And the old value won't matter after that.
787 ;;; (isearch-other-end isearch-other-end)
788 ;;; Perhaps some of these other variables should be bound for a
789 ;;; shorter period, ending before the next isearch-search.
790 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
791 (isearch-opoint isearch-opoint)
792 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
793 (isearch-small-window isearch-small-window)
794 (isearch-recursive-edit isearch-recursive-edit)
795 ;; Save current configuration so we can restore it here.
796 (isearch-window-configuration (current-window-configuration))
797
798 ;; Temporarily restore `minibuffer-message-timeout'.
799 (minibuffer-message-timeout
800 isearch-original-minibuffer-message-timeout)
801 (isearch-original-minibuffer-message-timeout
802 isearch-original-minibuffer-message-timeout)
803 )
804
805 ;; Actually terminate isearching until editing is done.
806 ;; This is so that the user can do anything without failure,
807 ;; like switch buffers and start another isearch, and return.
808 (condition-case err
809 (isearch-done t t)
810 (exit nil)) ; was recursive editing
811
812 (isearch-message) ;; for read-char
813 (unwind-protect
814 (let* (;; Why does following read-char echo?
815 ;;(echo-keystrokes 0) ;; not needed with above message
816 (e (let ((cursor-in-echo-area t))
817 (read-event)))
818 ;; Binding minibuffer-history-symbol to nil is a work-around
819 ;; for some incompatibility with gmhist.
820 (minibuffer-history-symbol)
821 (message-log-max nil))
822 ;; If the first character the user types when we prompt them
823 ;; for a string is the yank-word character, then go into
824 ;; word-search mode. Otherwise unread that character and
825 ;; read a key the normal way.
826 ;; Word search does not apply (yet) to regexp searches,
827 ;; no check is made here.
828 (message (isearch-message-prefix nil nil t))
829 (if (eq 'isearch-yank-word
830 (lookup-key isearch-mode-map (vector e)))
831 (setq isearch-word t;; so message-prefix is right
832 isearch-new-word t)
833 (cancel-kbd-macro-events)
834 (isearch-unread e))
835 (setq cursor-in-echo-area nil)
836 (setq isearch-new-string
837 (let (junk-ring)
838 (read-from-minibuffer
839 (isearch-message-prefix nil nil isearch-nonincremental)
840 isearch-string
841 minibuffer-local-isearch-map nil
842 'junk-ring))
843 isearch-new-message
844 (mapconcat 'isearch-text-char-description
845 isearch-new-string "")))
846 ;; Always resume isearching by restarting it.
847 (isearch-mode isearch-forward
848 isearch-regexp
849 isearch-op-fun
850 nil
851 isearch-word)
852
853 ;; Copy new local values to isearch globals
854 (setq isearch-string isearch-new-string
855 isearch-message isearch-new-message
856 isearch-forward isearch-new-forward
857 isearch-word isearch-new-word))
858
859 ;; Empty isearch-string means use default.
860 (if (= 0 (length isearch-string))
861 (setq isearch-string (or (car (if isearch-regexp
862 regexp-search-ring
863 search-ring))
864 ""))
865 ;; This used to set the last search string,
866 ;; but I think it is not right to do that here.
867 ;; Only the string actually used should be saved.
868 ))
869
870 ;; Push the state as of before this C-s.
871 (isearch-push-state)
872
873 ;; Reinvoke the pending search.
874 (isearch-search)
875 (isearch-update)
876 (if isearch-nonincremental
877 (progn
878 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
879 (isearch-done))))
880
881 (quit ; handle abort-recursive-edit
882 (isearch-abort) ;; outside of let to restore outside global values
883 )))
884
885 (defun isearch-nonincremental-exit-minibuffer ()
886 (interactive)
887 (setq isearch-nonincremental t)
888 (exit-minibuffer))
889
890 (defun isearch-forward-exit-minibuffer ()
891 (interactive)
892 (setq isearch-new-forward t)
893 (exit-minibuffer))
894
895 (defun isearch-reverse-exit-minibuffer ()
896 (interactive)
897 (setq isearch-new-forward nil)
898 (exit-minibuffer))
899
900 (defun isearch-cancel ()
901 "Terminate the search and go back to the starting point."
902 (interactive)
903 (goto-char isearch-opoint)
904 (isearch-done t)
905 (isearch-clean-overlays)
906 (signal 'quit nil)) ; and pass on quit signal
907
908 (defun isearch-abort ()
909 "Abort incremental search mode if searching is successful, signaling quit.
910 Otherwise, revert to previous successful search and continue searching.
911 Use `isearch-exit' to quit without signaling."
912 (interactive)
913 ;; (ding) signal instead below, if quitting
914 (discard-input)
915 (if isearch-success
916 ;; If search is successful, move back to starting point
917 ;; and really do quit.
918 (progn (goto-char isearch-opoint)
919 (setq isearch-success nil)
920 (isearch-done t) ; exit isearch
921 (isearch-clean-overlays)
922 (signal 'quit nil)) ; and pass on quit signal
923 ;; If search is failing, or has an incomplete regexp,
924 ;; rub out until it is once more successful.
925 (while (or (not isearch-success) isearch-invalid-regexp)
926 (isearch-pop-state))
927 (isearch-update)))
928
929 (defun isearch-repeat (direction)
930 ;; Utility for isearch-repeat-forward and -backward.
931 (if (eq isearch-forward (eq direction 'forward))
932 ;; C-s in forward or C-r in reverse.
933 (if (equal isearch-string "")
934 ;; If search string is empty, use last one.
935 (setq isearch-string
936 (or (if isearch-regexp
937 (car regexp-search-ring)
938 (car search-ring))
939 "")
940 isearch-message
941 (mapconcat 'isearch-text-char-description
942 isearch-string ""))
943 ;; If already have what to search for, repeat it.
944 (or isearch-success
945 (progn
946 (goto-char (if isearch-forward (point-min) (point-max)))
947 (setq isearch-wrapped t))))
948 ;; C-s in reverse or C-r in forward, change direction.
949 (setq isearch-forward (not isearch-forward)))
950
951 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
952
953 (if (equal isearch-string "")
954 (setq isearch-success t)
955 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
956 (not isearch-just-started))
957 ;; If repeating a search that found
958 ;; an empty string, ensure we advance.
959 (if (if isearch-forward (eobp) (bobp))
960 ;; If there's nowhere to advance to, fail (and wrap next time).
961 (progn
962 (setq isearch-success nil)
963 (ding))
964 (forward-char (if isearch-forward 1 -1))
965 (isearch-search))
966 (isearch-search)))
967
968 (isearch-push-state)
969 (isearch-update))
970
971 (defun isearch-repeat-forward ()
972 "Repeat incremental search forwards."
973 (interactive)
974 (isearch-repeat 'forward))
975
976 (defun isearch-repeat-backward ()
977 "Repeat incremental search backwards."
978 (interactive)
979 (isearch-repeat 'backward))
980
981 (defun isearch-toggle-regexp ()
982 "Toggle regexp searching on or off."
983 ;; The status stack is left unchanged.
984 (interactive)
985 (setq isearch-regexp (not isearch-regexp))
986 (if isearch-regexp (setq isearch-word nil))
987 (isearch-update))
988
989 (defun isearch-toggle-case-fold ()
990 "Toggle case folding in searching on or off."
991 (interactive)
992 (setq isearch-case-fold-search
993 (if isearch-case-fold-search nil 'yes))
994 (let ((message-log-max nil))
995 (message "%s%s [case %ssensitive]"
996 (isearch-message-prefix nil nil isearch-nonincremental)
997 isearch-message
998 (if isearch-case-fold-search "in" "")))
999 (setq isearch-adjusted t)
1000 (sit-for 1)
1001 (isearch-update))
1002
1003 (defun isearch-delete-char ()
1004 "Discard last input item and move point back.
1005 If no previous match was done, just beep."
1006 (interactive)
1007 (if (null (cdr isearch-cmds))
1008 (ding)
1009 (isearch-pop-state))
1010 (isearch-update))
1011
1012
1013 (defun isearch-yank-string (string)
1014 "Pull STRING into search string."
1015 ;; Downcase the string if not supposed to case-fold yanked strings.
1016 (if (and isearch-case-fold-search
1017 (eq 'not-yanks search-upper-case))
1018 (setq string (downcase string)))
1019 (if isearch-regexp (setq string (regexp-quote string)))
1020 (setq isearch-string (concat isearch-string string)
1021 isearch-message
1022 (concat isearch-message
1023 (mapconcat 'isearch-text-char-description
1024 string ""))
1025 ;; Don't move cursor in reverse search.
1026 isearch-yank-flag t)
1027 (isearch-search-and-update))
1028
1029 (defun isearch-yank-kill ()
1030 "Pull string from kill ring into search string."
1031 (interactive)
1032 (isearch-yank-string (current-kill 0)))
1033
1034 (defun isearch-yank-x-selection ()
1035 "Pull current X selection into search string."
1036 (interactive)
1037 (isearch-yank-string (x-get-selection)))
1038
1039 (defun isearch-mouse-yank (click arg)
1040 "Yank with the mouse in Isearch mode.
1041 For a click in the echo area, invoke `isearch-yank-x-selection'.
1042 Otherwise invoke `mouse-yank-at-click'."
1043 (interactive "e\nP")
1044 (let ((w (posn-window (event-start click))))
1045 (if (and (window-minibuffer-p w)
1046 (not (minibuffer-window-active-p w))) ; in echo area
1047 (isearch-yank-x-selection)
1048 (mouse-yank-at-click click arg))))
1049
1050 (defun isearch-yank-word ()
1051 "Pull next word from buffer into search string."
1052 (interactive)
1053 (isearch-yank-string
1054 (save-excursion
1055 (and (not isearch-forward) isearch-other-end
1056 (goto-char isearch-other-end))
1057 (buffer-substring-no-properties
1058 (point) (progn (forward-word 1) (point))))))
1059
1060 (defun isearch-yank-line ()
1061 "Pull rest of line from buffer into search string."
1062 (interactive)
1063 (isearch-yank-string
1064 (save-excursion
1065 (and (not isearch-forward) isearch-other-end
1066 (goto-char isearch-other-end))
1067 (buffer-substring-no-properties (point) (line-end-position)))))
1068
1069
1070 (defun isearch-search-and-update ()
1071 ;; Do the search and update the display.
1072 (if (and (not isearch-success)
1073 ;; unsuccessful regexp search may become
1074 ;; successful by addition of characters which
1075 ;; make isearch-string valid
1076 (not isearch-regexp))
1077 nil
1078 ;; In reverse search, adding stuff at
1079 ;; the end may cause zero or many more chars to be
1080 ;; matched, in the string following point.
1081 ;; Allow all those possibilities without moving point as
1082 ;; long as the match does not extend past search origin.
1083 (if (and (not isearch-forward) (not isearch-adjusted)
1084 (condition-case ()
1085 (let ((case-fold-search isearch-case-fold-search))
1086 (if (and (eq case-fold-search t) search-upper-case)
1087 (setq case-fold-search
1088 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1089 (looking-at (if isearch-regexp isearch-string
1090 (regexp-quote isearch-string))))
1091 (error nil))
1092 (or isearch-yank-flag
1093 (<= (match-end 0)
1094 (min isearch-opoint isearch-barrier))))
1095 (progn
1096 (setq isearch-success t
1097 isearch-invalid-regexp nil
1098 isearch-within-brackets nil
1099 isearch-other-end (match-end 0))
1100 (if (and (eq isearch-case-fold-search t) search-upper-case)
1101 (setq isearch-case-fold-search
1102 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1103 ;; Not regexp, not reverse, or no match at point.
1104 (if (and isearch-other-end (not isearch-adjusted))
1105 (goto-char (if isearch-forward isearch-other-end
1106 (min isearch-opoint
1107 isearch-barrier
1108 (1+ isearch-other-end)))))
1109 (isearch-search)
1110 ))
1111 (isearch-push-state)
1112 (if isearch-op-fun (funcall isearch-op-fun))
1113 (isearch-update))
1114
1115
1116 ;; *, ?, and | chars can make a regexp more liberal.
1117 ;; They can make a regexp match sooner or make it succeed instead of failing.
1118 ;; So go back to place last successful search started
1119 ;; or to the last ^S/^R (barrier), whichever is nearer.
1120 ;; + needs no special handling because the string must match at least once.
1121
1122 (defun isearch-*-char ()
1123 "Handle * and ? specially in regexps."
1124 (interactive)
1125 (if isearch-regexp
1126 (let ((idx (length isearch-string)))
1127 (while (and (> idx 0)
1128 (eq (aref isearch-string (1- idx)) ?\\))
1129 (setq idx (1- idx)))
1130 (when (= (mod (- (length isearch-string) idx) 2) 0)
1131 (setq isearch-adjusted t)
1132 ;; Get the isearch-other-end from before the last search.
1133 ;; We want to start from there,
1134 ;; so that we don't retreat farther than that.
1135 ;; (car isearch-cmds) is after last search;
1136 ;; (car (cdr isearch-cmds)) is from before it.
1137 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1138 (setq cs (or cs isearch-barrier))
1139 (goto-char
1140 (if isearch-forward
1141 (max cs isearch-barrier)
1142 (min cs isearch-barrier)))))))
1143 (isearch-process-search-char last-command-char))
1144
1145
1146 (defun isearch-|-char ()
1147 "If in regexp search, jump to the barrier."
1148 (interactive)
1149 (if isearch-regexp
1150 (progn
1151 (setq isearch-adjusted t)
1152 (goto-char isearch-barrier)))
1153 (isearch-process-search-char last-command-char))
1154
1155
1156 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1157
1158 (defun isearch-other-meta-char ()
1159 "Exit the search normally and reread this key sequence.
1160 But only if `search-exit-option' is non-nil, the default.
1161 If it is the symbol `edit', the search string is edited in the minibuffer
1162 and the meta character is unread so that it applies to editing the string."
1163 (interactive)
1164 (let* ((key (this-command-keys))
1165 (main-event (aref key 0))
1166 (keylist (listify-key-sequence key)))
1167 (cond ((and (= (length key) 1)
1168 (let ((lookup (lookup-key function-key-map key)))
1169 (not (or (null lookup) (integerp lookup)
1170 (keymapp lookup)))))
1171 ;; Handle a function key that translates into something else.
1172 ;; If the key has a global definition too,
1173 ;; exit and unread the key itself, so its global definition runs.
1174 ;; Otherwise, unread the translation,
1175 ;; so that the translated key takes effect within isearch.
1176 (cancel-kbd-macro-events)
1177 (if (lookup-key global-map key)
1178 (progn
1179 (isearch-done)
1180 (apply 'isearch-unread keylist))
1181 (setq keylist
1182 (listify-key-sequence (lookup-key function-key-map key)))
1183 (while keylist
1184 (setq key (car keylist))
1185 ;; If KEY is a printing char, we handle it here
1186 ;; directly to avoid the input method and keyboard
1187 ;; coding system translating it.
1188 (if (and (integerp key)
1189 (>= key ?\ ) (/= key 127) (< key 256))
1190 (progn
1191 (isearch-process-search-char key)
1192 (setq keylist (cdr keylist)))
1193 ;; As the remaining keys in KEYLIST can't be handled
1194 ;; here, we must reread them.
1195 (apply 'isearch-unread keylist)
1196 (setq keylist nil)))))
1197 (
1198 ;; Handle an undefined shifted control character
1199 ;; by downshifting it if that makes it defined.
1200 ;; (As read-key-sequence would normally do,
1201 ;; if we didn't have a default definition.)
1202 (let ((mods (event-modifiers main-event)))
1203 (and (integerp main-event)
1204 (memq 'shift mods)
1205 (memq 'control mods)
1206 (lookup-key isearch-mode-map
1207 (let ((copy (copy-sequence key)))
1208 (aset copy 0
1209 (- main-event (- ?\C-\S-a ?\C-a)))
1210 copy)
1211 nil)))
1212 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1213 (cancel-kbd-macro-events)
1214 (apply 'isearch-unread keylist))
1215 ((eq search-exit-option 'edit)
1216 (apply 'isearch-unread keylist)
1217 (isearch-edit-string))
1218 (search-exit-option
1219 (let (window)
1220 (cancel-kbd-macro-events)
1221 (apply 'isearch-unread keylist)
1222 ;; Properly handle scroll-bar and mode-line clicks
1223 ;; for which a dummy prefix event was generated as (aref key 0).
1224 (and (> (length key) 1)
1225 (symbolp (aref key 0))
1226 (listp (aref key 1))
1227 (not (numberp (posn-point (event-start (aref key 1)))))
1228 ;; Convert the event back into its raw form,
1229 ;; with the dummy prefix implicit in the mouse event,
1230 ;; so it will get split up once again.
1231 (progn (setq unread-command-events
1232 (cdr unread-command-events))
1233 (setq main-event (car unread-command-events))
1234 (setcar (cdr (event-start main-event))
1235 (car (nth 1 (event-start main-event))))))
1236 ;; If we got a mouse click, maybe it was read with the buffer
1237 ;; it was clicked on. If so, that buffer, not the current one,
1238 ;; is in isearch mode. So end the search in that buffer.
1239 (if (and (listp main-event)
1240 (setq window (posn-window (event-start main-event)))
1241 (windowp window)
1242 (or (> (minibuffer-depth) 0)
1243 (not (window-minibuffer-p window))))
1244 (save-excursion
1245 (set-buffer (window-buffer window))
1246 (isearch-done)
1247 (isearch-clean-overlays))
1248 (isearch-done)
1249 (isearch-clean-overlays))))
1250 (t;; otherwise nil
1251 (isearch-process-search-string key key)))))
1252
1253 (defun isearch-quote-char ()
1254 "Quote special characters for incremental search."
1255 (interactive)
1256 (let ((char (read-quoted-char (isearch-message t))))
1257 ;; Assume character codes 0200 - 0377 stand for characters in some
1258 ;; single-byte character set, and convert them to Emacs
1259 ;; characters.
1260 (and enable-multibyte-characters
1261 (>= char ?\200)
1262 (<= char ?\377)
1263 (setq char (unibyte-char-to-multibyte char)))
1264 (isearch-process-search-char char)))
1265
1266 (defun isearch-return-char ()
1267 "Convert return into newline for incremental search.
1268 Obsolete."
1269 (interactive)
1270 (isearch-process-search-char ?\n))
1271
1272 (defun isearch-printing-char ()
1273 "Add this ordinary printing character to the search string and search."
1274 (interactive)
1275 (let ((char last-command-char))
1276 (if (= char ?\S-\ )
1277 (setq char ?\ ))
1278 (if (and enable-multibyte-characters
1279 (>= char ?\200)
1280 (<= char ?\377))
1281 (if (keyboard-coding-system)
1282 (isearch-process-search-multibyte-characters char)
1283 (isearch-process-search-char (unibyte-char-to-multibyte char)))
1284 (if current-input-method
1285 (isearch-process-search-multibyte-characters char)
1286 (isearch-process-search-char char)))))
1287
1288 (defun isearch-whitespace-chars ()
1289 "Match all whitespace chars, if in regexp mode.
1290 If you want to search for just a space, type \\[quoted-insert] SPC."
1291 (interactive)
1292 (if isearch-regexp
1293 (if (and search-whitespace-regexp (not isearch-within-brackets)
1294 (not isearch-invalid-regexp))
1295 (isearch-process-search-string search-whitespace-regexp " ")
1296 (isearch-printing-char))
1297 (progn
1298 ;; This way of doing word search doesn't correctly extend current search.
1299 ;; (setq isearch-word t)
1300 ;; (setq isearch-adjusted t)
1301 ;; (goto-char isearch-barrier)
1302 (isearch-printing-char))))
1303
1304 (defun isearch-process-search-char (char)
1305 ;; Append the char to the search string, update the message and re-search.
1306 (isearch-process-search-string
1307 (char-to-string char)
1308 (if (>= char ?\200)
1309 (char-to-string char)
1310 (isearch-text-char-description char))))
1311
1312 (defun isearch-process-search-string (string message)
1313 (setq isearch-string (concat isearch-string string)
1314 isearch-message (concat isearch-message message))
1315 (isearch-search-and-update))
1316
1317 \f
1318 ;; Search Ring
1319
1320 (defun isearch-ring-adjust1 (advance)
1321 ;; Helper for isearch-ring-adjust
1322 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1323 (length (length ring))
1324 (yank-pointer-name (if isearch-regexp
1325 'regexp-search-ring-yank-pointer
1326 'search-ring-yank-pointer))
1327 (yank-pointer (eval yank-pointer-name)))
1328 (if (zerop length)
1329 ()
1330 (set yank-pointer-name
1331 (setq yank-pointer
1332 (mod (+ (or yank-pointer 0)
1333 (if advance -1 1))
1334 length)))
1335 (setq isearch-string (nth yank-pointer ring)
1336 isearch-message (mapconcat 'isearch-text-char-description
1337 isearch-string "")))))
1338
1339 (defun isearch-ring-adjust (advance)
1340 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1341 (isearch-ring-adjust1 advance)
1342 (if search-ring-update
1343 (progn
1344 (isearch-search)
1345 (isearch-update))
1346 (isearch-edit-string)
1347 )
1348 (isearch-push-state))
1349
1350 (defun isearch-ring-advance ()
1351 "Advance to the next search string in the ring."
1352 ;; This could be more general to handle a prefix arg, but who would use it.
1353 (interactive)
1354 (isearch-ring-adjust 'advance))
1355
1356 (defun isearch-ring-retreat ()
1357 "Retreat to the previous search string in the ring."
1358 (interactive)
1359 (isearch-ring-adjust nil))
1360
1361 (defun isearch-ring-advance-edit (n)
1362 "Insert the next element of the search history into the minibuffer.
1363 With prefix arg N, insert the Nth element."
1364 (interactive "p")
1365 (let* ((yank-pointer-name (if isearch-regexp
1366 'regexp-search-ring-yank-pointer
1367 'search-ring-yank-pointer))
1368 (yank-pointer (eval yank-pointer-name))
1369 (ring (if isearch-regexp regexp-search-ring search-ring))
1370 (length (length ring)))
1371 (if (zerop length)
1372 ()
1373 (set yank-pointer-name
1374 (setq yank-pointer
1375 (mod (- (or yank-pointer 0) n)
1376 length)))
1377
1378 (delete-field)
1379 (insert (nth yank-pointer ring))
1380 (goto-char (point-max)))))
1381
1382 (defun isearch-ring-retreat-edit (n)
1383 "Insert the previous element of the search history into the minibuffer.
1384 With prefix arg N, insert the Nth element."
1385 (interactive "p")
1386 (isearch-ring-advance-edit (- n)))
1387
1388 ;;(defun isearch-ring-adjust-edit (advance)
1389 ;; "Use the next or previous search string in the ring while in minibuffer."
1390 ;; (isearch-ring-adjust1 advance)
1391 ;; (erase-buffer)
1392 ;; (insert isearch-string))
1393
1394 ;;(defun isearch-ring-advance-edit ()
1395 ;; (interactive)
1396 ;; (isearch-ring-adjust-edit 'advance))
1397
1398 ;;(defun isearch-ring-retreat-edit ()
1399 ;; "Retreat to the previous search string in the ring while in the minibuffer."
1400 ;; (interactive)
1401 ;; (isearch-ring-adjust-edit nil))
1402
1403
1404 (defun isearch-complete1 ()
1405 ;; Helper for isearch-complete and isearch-complete-edit
1406 ;; Return t if completion OK, nil if no completion exists.
1407 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1408 (alist (mapcar (function (lambda (string) (list string))) ring))
1409 (completion-ignore-case case-fold-search)
1410 (completion (try-completion isearch-string alist)))
1411 (cond
1412 ((eq completion t)
1413 ;; isearch-string stays the same
1414 t)
1415 ((or completion ; not nil, must be a string
1416 (= 0 (length isearch-string))) ; shouldn't have to say this
1417 (if (equal completion isearch-string) ;; no extension?
1418 (progn
1419 (if completion-auto-help
1420 (with-output-to-temp-buffer "*Isearch completions*"
1421 (display-completion-list
1422 (all-completions isearch-string alist))))
1423 t)
1424 (and completion
1425 (setq isearch-string completion))))
1426 (t
1427 (message "No completion") ; waits a second if in minibuffer
1428 nil))))
1429
1430 (defun isearch-complete ()
1431 "Complete the search string from the strings on the search ring.
1432 The completed string is then editable in the minibuffer.
1433 If there is no completion possible, say so and continue searching."
1434 (interactive)
1435 (if (isearch-complete1)
1436 (isearch-edit-string)
1437 ;; else
1438 (sit-for 1)
1439 (isearch-update)))
1440
1441 (defun isearch-complete-edit ()
1442 "Same as `isearch-complete' except in the minibuffer."
1443 (interactive)
1444 (setq isearch-string (buffer-string))
1445 (if (isearch-complete1)
1446 (progn
1447 (delete-field)
1448 (insert isearch-string))))
1449
1450 \f
1451 ;; The search status stack (and isearch window-local variables, not used).
1452 ;; Need a structure for this.
1453
1454 (defun isearch-top-state ()
1455 (let ((cmd (car isearch-cmds)))
1456 (setq isearch-string (car cmd)
1457 isearch-message (car (cdr cmd))
1458 isearch-success (nth 3 cmd)
1459 isearch-forward (nth 4 cmd)
1460 isearch-other-end (nth 5 cmd)
1461 isearch-word (nth 6 cmd)
1462 isearch-invalid-regexp (nth 7 cmd)
1463 isearch-wrapped (nth 8 cmd)
1464 isearch-barrier (nth 9 cmd)
1465 isearch-within-brackets (nth 10 cmd)
1466 isearch-case-fold-search (nth 11 cmd))
1467 (goto-char (car (cdr (cdr cmd))))))
1468
1469 (defun isearch-pop-state ()
1470 (setq isearch-cmds (cdr isearch-cmds))
1471 (isearch-top-state))
1472
1473 (defun isearch-push-state ()
1474 (setq isearch-cmds
1475 (cons (list isearch-string isearch-message (point)
1476 isearch-success isearch-forward isearch-other-end
1477 isearch-word
1478 isearch-invalid-regexp isearch-wrapped isearch-barrier
1479 isearch-within-brackets isearch-case-fold-search)
1480 isearch-cmds)))
1481
1482 \f
1483 ;; Message string
1484
1485 (defun isearch-message (&optional c-q-hack ellipsis)
1486 ;; Generate and print the message string.
1487 (let ((cursor-in-echo-area ellipsis)
1488 (m (concat
1489 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
1490 isearch-message
1491 (isearch-message-suffix c-q-hack ellipsis)
1492 )))
1493 (if c-q-hack
1494 m
1495 (let ((message-log-max nil))
1496 (message "%s" m)))))
1497
1498 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
1499 ;; If about to search, and previous search regexp was invalid,
1500 ;; check that it still is. If it is valid now,
1501 ;; let the message we display while searching say that it is valid.
1502 (and isearch-invalid-regexp ellipsis
1503 (condition-case ()
1504 (progn (re-search-forward isearch-string (point) t)
1505 (setq isearch-invalid-regexp nil
1506 isearch-within-brackets nil))
1507 (error nil)))
1508 ;; If currently failing, display no ellipsis.
1509 (or isearch-success (setq ellipsis nil))
1510 (let ((m (concat (if isearch-success "" "failing ")
1511 (if (and isearch-wrapped
1512 (if isearch-forward
1513 (> (point) isearch-opoint)
1514 (< (point) isearch-opoint)))
1515 "over")
1516 (if isearch-wrapped "wrapped ")
1517 (if isearch-word "word " "")
1518 (if isearch-regexp "regexp " "")
1519 (if nonincremental "search" "I-search")
1520 (if isearch-forward "" " backward")
1521 (if current-input-method
1522 (concat " [" current-input-method-title "]: ")
1523 ": ")
1524 )))
1525 (concat (upcase (substring m 0 1)) (substring m 1))))
1526
1527
1528 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
1529 (concat (if c-q-hack "^Q" "")
1530 (if isearch-invalid-regexp
1531 (concat " [" isearch-invalid-regexp "]")
1532 "")))
1533
1534 \f
1535 ;;; Searching
1536
1537 (defun isearch-search ()
1538 ;; Do the search with the current search string.
1539 (isearch-message nil t)
1540 (if (and (eq isearch-case-fold-search t) search-upper-case)
1541 (setq isearch-case-fold-search
1542 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1543 (condition-case lossage
1544 (let ((inhibit-point-motion-hooks search-invisible)
1545 (inhibit-quit nil)
1546 (case-fold-search isearch-case-fold-search)
1547 (retry t))
1548 (if isearch-regexp (setq isearch-invalid-regexp nil))
1549 (setq isearch-within-brackets nil)
1550 (while retry
1551 (setq isearch-success
1552 (funcall
1553 (cond (isearch-word
1554 (if isearch-forward
1555 'word-search-forward 'word-search-backward))
1556 (isearch-regexp
1557 (if isearch-forward
1558 're-search-forward 're-search-backward))
1559 (t
1560 (if isearch-forward 'search-forward 'search-backward)))
1561 isearch-string nil t))
1562 ;; Clear RETRY unless we matched some invisible text
1563 ;; and we aren't supposed to do that.
1564 (if (or (eq search-invisible t)
1565 (not isearch-success)
1566 (bobp) (eobp)
1567 (= (match-beginning 0) (match-end 0))
1568 (not (isearch-range-invisible
1569 (match-beginning 0) (match-end 0))))
1570 (setq retry nil)))
1571 (setq isearch-just-started nil)
1572 (if isearch-success
1573 (setq isearch-other-end
1574 (if isearch-forward (match-beginning 0) (match-end 0)))))
1575
1576 (quit (isearch-unread ?\C-g)
1577 (setq isearch-success nil))
1578
1579 (invalid-regexp
1580 (setq isearch-invalid-regexp (car (cdr lossage)))
1581 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1582 isearch-invalid-regexp))
1583 (if (string-match
1584 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1585 isearch-invalid-regexp)
1586 (setq isearch-invalid-regexp "incomplete input")))
1587 (error
1588 ;; stack overflow in regexp search.
1589 (setq isearch-invalid-regexp (car (cdr lossage)))))
1590
1591 (if isearch-success
1592 nil
1593 ;; Ding if failed this time after succeeding last time.
1594 (and (nth 3 (car isearch-cmds))
1595 (ding))
1596 (goto-char (nth 2 (car isearch-cmds)))))
1597
1598
1599 ;;; Called when opening an overlay, and we are still in isearch.
1600 (defun isearch-open-overlay-temporary (ov)
1601 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1602 ;; Some modes would want to open the overlays temporary during
1603 ;; isearch in their own way, they should set the
1604 ;; `isearch-open-invisible-temporary' to a function doing this.
1605 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1606 ;; Store the values for the `invisible' and `intangible'
1607 ;; properties, and then set them to nil. This way the text hidden
1608 ;; by this overlay becomes visible.
1609
1610 ;; Do we really need to set the `intangible' property to t? Can we
1611 ;; have the point inside an overlay with an `intangible' property?
1612 ;; In 19.34 this does not exist so I cannot test it.
1613 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1614 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1615 (overlay-put ov 'invisible nil)
1616 (overlay-put ov 'intangible nil)))
1617
1618
1619 ;;; This is called at the end of isearch. It will open the overlays
1620 ;;; that contain the latest match. Obviously in case of a C-g the
1621 ;;; point returns to the original location which surely is not contain
1622 ;;; in any of these overlays, se we are safe in this case too.
1623 (defun isearch-open-necessary-overlays (ov)
1624 (let ((inside-overlay (and (> (point) (overlay-start ov))
1625 (< (point) (overlay-end ov))))
1626 ;; If this exists it means that the overlay was opened using
1627 ;; this function, not by us tweaking the overlay properties.
1628 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1629 (when (or inside-overlay (not fct-temp))
1630 ;; restore the values for the `invisible' and `intangible'
1631 ;; properties
1632 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1633 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1634 (overlay-put ov 'isearch-invisible nil)
1635 (overlay-put ov 'isearch-intangible nil))
1636 (if inside-overlay
1637 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1638 (if fct-temp
1639 (funcall fct-temp ov t)))))
1640
1641 ;;; This is called when exiting isearch. It closes the temporary
1642 ;;; opened overlays, except the ones that contain the latest match.
1643 (defun isearch-clean-overlays ()
1644 (when isearch-opened-overlays
1645 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
1646 (setq isearch-opened-overlays nil)))
1647
1648 ;;; Verify if the current match is outside of each element of
1649 ;;; `isearch-opened-overlays', if so close that overlay.
1650 (defun isearch-close-unecessary-overlays (begin end)
1651 (let ((ov-list isearch-opened-overlays)
1652 ov
1653 inside-overlay
1654 fct-temp)
1655 (setq isearch-opened-overlays nil)
1656 (while ov-list
1657 (setq ov (car ov-list))
1658 (setq ov-list (cdr ov-list))
1659 (setq inside-overlay (or (and (> begin (overlay-start ov))
1660 (< begin (overlay-end ov)))
1661 (and (> end (overlay-start ov))
1662 (< end (overlay-end ov)))))
1663 ;; If this exists it means that the overlay was opened using
1664 ;; this function, not by us tweaking the overlay properties.
1665 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1666 (if inside-overlay
1667 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1668 (if fct-temp
1669 (funcall fct-temp ov t)
1670 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1671 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1672 (overlay-put ov 'isearch-invisible nil)
1673 (overlay-put ov 'isearch-intangible nil))))))
1674
1675 (defun isearch-range-invisible (beg end)
1676 "Return t if all the text from BEG to END is invisible."
1677 (and (/= beg end)
1678 ;; Check that invisibility runs up to END.
1679 (save-excursion
1680 (goto-char beg)
1681 (let (
1682 ;; can-be-opened keeps track if we can open some overlays.
1683 (can-be-opened (eq search-invisible 'open))
1684 ;; the list of overlays that could be opened
1685 (crt-overlays nil))
1686 (when (and can-be-opened isearch-hide-immediately)
1687 (isearch-close-unecessary-overlays beg end))
1688 ;; If the following character is currently invisible,
1689 ;; skip all characters with that same `invisible' property value.
1690 ;; Do that over and over.
1691 (while (and (< (point) end)
1692 (let ((prop
1693 (get-char-property (point) 'invisible)))
1694 (if (eq buffer-invisibility-spec t)
1695 prop
1696 (or (memq prop buffer-invisibility-spec)
1697 (assq prop buffer-invisibility-spec)))))
1698 (if (get-text-property (point) 'invisible)
1699 (progn
1700 (goto-char (next-single-property-change (point) 'invisible
1701 nil end))
1702 ;; if text is hidden by an `invisible' text property
1703 ;; we cannot open it at all.
1704 (setq can-be-opened nil))
1705 (unless (null can-be-opened)
1706 (let ((overlays (overlays-at (point)))
1707 ov-list
1708 o
1709 invis-prop)
1710 (while overlays
1711 (setq o (car overlays)
1712 invis-prop (overlay-get o 'invisible))
1713 (if (if (eq buffer-invisibility-spec t)
1714 invis-prop
1715 (or (memq invis-prop buffer-invisibility-spec)
1716 (assq invis-prop buffer-invisibility-spec)))
1717 (if (overlay-get o 'isearch-open-invisible)
1718 (setq ov-list (cons o ov-list))
1719 ;; We found one overlay that cannot be
1720 ;; opened, that means the whole chunk
1721 ;; cannot be opened.
1722 (setq can-be-opened nil)))
1723 (setq overlays (cdr overlays)))
1724 (if can-be-opened
1725 ;; It makes sense to append to the open
1726 ;; overlays list only if we know that this is
1727 ;; t.
1728 (setq crt-overlays (append ov-list crt-overlays)))))
1729 (goto-char (next-overlay-change (point)))))
1730 ;; See if invisibility reaches up thru END.
1731 (if (>= (point) end)
1732 (if (and (not (null can-be-opened)) (consp crt-overlays))
1733 (progn
1734 (setq isearch-opened-overlays
1735 (append isearch-opened-overlays crt-overlays))
1736 (mapc 'isearch-open-overlay-temporary crt-overlays)
1737 nil)
1738 t))))))
1739
1740 \f
1741 ;;; Highlighting
1742
1743 (defvar isearch-overlay nil)
1744
1745 (defsubst isearch-set-lazy-highlight-faces-at (pos face)
1746 "Set the face property of isearch lazy highlight overlays at POS to FACE.
1747 If POS is nil, nothing is done."
1748 (unless (null pos)
1749 (dolist (ov (overlays-at pos))
1750 (when (and (not (eq ov isearch-overlay))
1751 (memq ov isearch-lazy-highlight-overlays)
1752 (not (eq (overlay-get ov 'face) face)))
1753 (overlay-put ov 'face face)))))
1754
1755 (defun isearch-highlight (beg end)
1756 (unless (or (null search-highlight) (null (display-color-p)))
1757 (cond (isearch-overlay
1758 ;; Overlay already exists, just move it.
1759
1760 ;; Check to see if there are any lazy-isearch overlays at
1761 ;; the same position with their face property suppressed
1762 ;; (to avoid face clashes), and if so, give them their face
1763 ;; back.
1764 (isearch-set-lazy-highlight-faces-at (overlay-start isearch-overlay)
1765 isearch-lazy-highlight-face)
1766
1767 (move-overlay isearch-overlay beg end (current-buffer)))
1768
1769 (t
1770 ;; Overlay doesn't exist, create it.
1771 (setq isearch-overlay (make-overlay beg end))
1772 (overlay-put isearch-overlay 'face isearch)))
1773
1774 ;; Suppress the faces of any lazy-isearch overlays at the new position
1775 (isearch-set-lazy-highlight-faces-at beg nil)))
1776
1777 (defun isearch-dehighlight (totally)
1778 (when isearch-overlay
1779 ;; Check to see if there are any lazy-isearch overlays at the same
1780 ;; position with their face property suppressed (to avoid face
1781 ;; clashes), and if so, give them their face back.
1782 (isearch-set-lazy-highlight-faces-at (overlay-start isearch-overlay)
1783 isearch-lazy-highlight-face)
1784 (delete-overlay isearch-overlay)))
1785
1786
1787 ;;; General utilities
1788
1789
1790 (defun isearch-no-upper-case-p (string regexp-flag)
1791 "Return t if there are no upper case chars in STRING.
1792 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
1793 since they have special meaning in a regexp."
1794 (let (quote-flag (i 0) (len (length string)) found)
1795 (while (and (not found) (< i len))
1796 (let ((char (aref string i)))
1797 (if (and regexp-flag (eq char ?\\))
1798 (setq quote-flag (not quote-flag))
1799 (if (and (not quote-flag) (not (eq char (downcase char))))
1800 (setq found t))))
1801 (setq i (1+ i)))
1802 (not found)))
1803
1804 ;; Portability functions to support various Emacs versions.
1805
1806 (defun isearch-text-char-description (c)
1807 (cond
1808 ((< c ?\ ) (format "^%c" (+ c 64)))
1809 ((= c ?\^?) "^?")
1810 (t (char-to-string c))))
1811
1812 ;; General function to unread characters or events.
1813 ;; Also insert them in a keyboard macro being defined.
1814 (defun isearch-unread (&rest char-or-events)
1815 (mapc 'store-kbd-macro-event char-or-events)
1816 (setq unread-command-events
1817 (append char-or-events unread-command-events)))
1818
1819 \f
1820 ;;; isearch-lazy-highlight feature
1821 ;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1822
1823 ;;; When active, *every* match for the current search string is
1824 ;;; highlighted: the current one using the normal isearch match color
1825 ;;; and all the others using the unobtrusive `secondary-selection'
1826 ;;; color. The extra highlighting makes it easier to anticipate where
1827 ;;; the cursor will land each time you press C-s or C-r to repeat a
1828 ;;; pending search. Highlighting of these additional matches happens
1829 ;;; in a deferred fashion using "idle timers," so the cycles needed do
1830 ;;; not rob isearch of its usual snappy response.
1831
1832 ;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1833 ;;; Specifically:
1834 ;;; - `isearch-update' is expected to be called (at least) every time
1835 ;;; the search string changes;
1836 ;;; - `isearch-string' is expected to contain the current search
1837 ;;; string as entered by the user;
1838 ;;; - `isearch-overlay' is expected to contain the overlay used for
1839 ;;; primary isearch match-highlighting;
1840 ;;; - `isearch-opoint' is expected to contain the location where the
1841 ;;; current search began;
1842 ;;; - the type of the current search is expected to be given by
1843 ;;; `isearch-word' and `isearch-regexp';
1844 ;;; - the direction of the current search is expected to be given by
1845 ;;; `isearch-forward';
1846 ;;; - the variable `isearch-invalid-regexp' is expected to be true
1847 ;;; iff `isearch-string' is an invalid regexp.
1848
1849 (require 'timer)
1850
1851 (defgroup isearch-lazy-highlight nil
1852 "Lazy highlighting feature for incremental search."
1853 :prefix "isearch-lazy-highlight-"
1854 :group 'isearch)
1855
1856 (defcustom isearch-lazy-highlight t
1857 "*Controls the lazy-highlighting during incremental searches.
1858 When non-nil, all text in the buffer matching the current search
1859 string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1860 and `isearch-lazy-highlight-interval')."
1861 :type 'boolean
1862 :group 'isearch-lazy-highlight)
1863
1864 (defcustom isearch-lazy-highlight-cleanup t
1865 "*Controls whether to remove extra highlighting after a search.
1866 If this is nil, extra highlighting can be \"manually\" removed with
1867 \\[isearch-lazy-highlight-cleanup]."
1868 :type 'boolean
1869 :group 'isearch-lazy-highlight)
1870
1871 (defcustom isearch-lazy-highlight-initial-delay 0.25
1872 "*Seconds to wait before beginning to lazily highlight all matches."
1873 :type 'number
1874 :group 'isearch-lazy-highlight)
1875
1876 (defcustom isearch-lazy-highlight-interval 0.0625
1877 "*Seconds between lazily highlighting successive matches."
1878 :type 'number
1879 :group 'isearch-lazy-highlight)
1880
1881 (defcustom isearch-lazy-highlight-max 20
1882 "*Maximum number of matches to highlight."
1883 :type '(choice (const :tag "All" nil)
1884 (integer :tag "Some"))
1885 :group 'isearch-lazy-highlight)
1886
1887 (defgroup isearch-faces nil
1888 "Lazy highlighting feature for incremental search."
1889 :version "21.1"
1890 :group 'isearch)
1891
1892 (defface isearch
1893 '((t (:inherit region)))
1894 "Face for highlighting matches."
1895 :group 'isearch-faces)
1896 (defvar isearch 'isearch)
1897
1898 (defface isearch-lazy-highlight-face
1899 '((t (:inherit secondary-selection)))
1900 "Face for lazy highlighting of matches."
1901 :group 'isearch-faces)
1902 (defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
1903
1904 (defvar isearch-lazy-highlight-overlays nil)
1905 (defvar isearch-lazy-highlight-window nil)
1906 (defvar isearch-lazy-highlight-start nil)
1907 (defvar isearch-lazy-highlight-end nil)
1908 (defvar isearch-lazy-highlight-timer nil)
1909 (defvar isearch-lazy-highlight-last-string nil)
1910
1911 (defun isearch-lazy-highlight-cleanup (&optional remove)
1912 "Stop lazy highlighting and maybe remove existing highlighting.
1913 REMOVE non-nil means remove all the existing lazy highlighting.
1914
1915 This function is called when exiting an incremental search."
1916 (interactive '(t))
1917 (if remove
1918 (isearch-lazy-highlight-remove-overlays))
1919 (if isearch-lazy-highlight-timer
1920 (progn
1921 (cancel-timer isearch-lazy-highlight-timer)
1922 (setq isearch-lazy-highlight-timer nil))))
1923
1924 (defun isearch-lazy-highlight-remove-overlays (&optional keep-start keep-end)
1925 "Remove lazy highlight overlays from the current buffer.
1926 With optional arguments KEEP-START and KEEP-END,
1927 preserve any overlays in that range."
1928 (let ((tem isearch-lazy-highlight-overlays))
1929 (while tem
1930 (if (or (null keep-start)
1931 (let ((pos (overlay-start (car tem))))
1932 (or (< pos keep-start) (> pos keep-end))))
1933 (progn
1934 (delete-overlay (car tem))
1935 (setq isearch-lazy-highlight-overlays
1936 (delq (car tem) isearch-lazy-highlight-overlays))))
1937 (setq tem (cdr tem)))))
1938
1939 (defun isearch-lazy-highlight-new-loop ()
1940 "Clear obsolete highlighting, and queue up to do new highlighting.
1941 This happens when `isearch-update' is invoked (which can cause the
1942 search string to change)."
1943 (when (and isearch-lazy-highlight
1944 (not isearch-invalid-regexp)
1945 (not (equal isearch-string "")))
1946
1947 ;; If the search string has changed, remove all old overlays.
1948 (unless (equal isearch-string isearch-lazy-highlight-last-string)
1949 (isearch-lazy-highlight-remove-overlays)
1950 (setq isearch-lazy-highlight-window nil))
1951
1952 (if (and isearch-overlay
1953 (not (overlay-get isearch-overlay 'priority)))
1954 ;; Make sure the isearch-overlay takes priority
1955 ;; over any other matches.
1956 (overlay-put isearch-overlay 'priority 1))
1957
1958 ;; Queue up to display other matches after a short pause.
1959 (setq isearch-lazy-highlight-timer
1960 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
1961 'isearch-lazy-highlight-update))))
1962
1963 (defun isearch-lazy-highlight-update ()
1964 "Update highlighting of possible other matches for isearch."
1965 (unless (and (eq isearch-lazy-highlight-window (selected-window))
1966 (equal isearch-lazy-highlight-start (window-start)))
1967
1968 ;; The search string or the visible window has changed.
1969
1970 (setq isearch-lazy-highlight-window (selected-window)
1971 isearch-lazy-highlight-start (window-start)
1972 isearch-lazy-highlight-end (window-end nil t)
1973 isearch-lazy-highlight-last-string isearch-string)
1974
1975 ;; If the string is the same, the old overlays are still usable
1976 ;; if they are still visible in the window.
1977 (isearch-lazy-highlight-remove-overlays (window-start)
1978 (window-end nil t))
1979
1980 (when (or (null isearch-lazy-highlight-max)
1981 (< (length isearch-lazy-highlight-overlays)
1982 isearch-lazy-highlight-max))
1983 (save-excursion
1984 (save-match-data
1985 (let (found)
1986 (goto-char isearch-lazy-highlight-start)
1987 (while (let ((case-fold-search isearch-case-fold-search))
1988 (funcall (cond (isearch-word 'word-search-forward)
1989 (isearch-regexp 're-search-forward)
1990 (t 'search-forward))
1991 isearch-string
1992 isearch-lazy-highlight-end
1993 t))
1994 ;; Found the next match.
1995 (let ((ov (make-overlay (match-beginning 0)
1996 (match-end 0))))
1997 ;; If OV overlaps the current isearch overlay, suppress
1998 ;; its face property; otherwise, we sometimes get odd
1999 ;; looking face combinations.
2000 (unless (memq isearch-overlay
2001 (overlays-at (match-beginning 0)))
2002 (overlay-put ov 'face isearch-lazy-highlight-face))
2003
2004 (overlay-put ov 'priority 0)
2005 ;; Don't highlight on any other windows.
2006 (overlay-put ov 'window isearch-lazy-highlight-window)
2007
2008 (push ov isearch-lazy-highlight-overlays)))))))))
2009
2010 (defun isearch-resume (search regexp word forward message case-fold)
2011 "Resume an incremental search.
2012 SEARCH is the string or regexp searched for.
2013 REGEXP non-nil means the resumed search was a regexp search.
2014 WORD non-nil means resume a word search.
2015 FORWARD non-nil means resume a forward search.
2016 MESSAGE is the echo-area message recorded for the search resumed.
2017 CASE-FOLD non-nil means the search was case-insensitive."
2018 (isearch-mode forward regexp nil nil word)
2019 (setq isearch-string search
2020 isearch-message message
2021 isearch-case-fold-search case-fold)
2022 (isearch-search))
2023
2024 ;;; isearch.el ends here