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