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