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