(isearch-update): Set disable-point-adjustment to t
[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 ;; We must prevent the point moving to the end of composition when a
624 ;; part of the composition has just been searched.
625 (setq disable-point-adjustment t))
626
627 (defun isearch-done (&optional nopush edit)
628 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
629 ;; Called by all commands that terminate isearch-mode.
630 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
631 (setq overriding-terminal-local-map nil)
632 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
633 (isearch-dehighlight t)
634 (isearch-lazy-highlight-cleanup)
635 (let ((found-start (window-start (selected-window)))
636 (found-point (point)))
637 (if isearch-window-configuration
638 (set-window-configuration isearch-window-configuration))
639
640 (if isearch-small-window
641 (goto-char found-point)
642 ;; Exiting the save-window-excursion clobbers window-start; restore it.
643 (set-window-start (selected-window) found-start t))
644
645 ;; If there was movement, mark the starting position.
646 ;; Maybe should test difference between and set mark iff > threshold.
647 (if (/= (point) isearch-opoint)
648 (or (and transient-mark-mode mark-active)
649 (progn
650 (push-mark isearch-opoint t)
651 (or executing-kbd-macro (> (minibuffer-depth) 0)
652 (message "Mark saved where search started"))))))
653
654 (setq isearch-mode nil)
655 (if isearch-input-method-local-p
656 (setq input-method-function isearch-input-method-function)
657 (kill-local-variable 'input-method-function))
658
659 (force-mode-line-update)
660
661 ;; If we ended in the middle of some intangible text,
662 ;; move to the further end of that intangible text.
663 (let ((after (if (eobp) nil
664 (get-text-property (point) 'intangible)))
665 (before (if (bobp) nil
666 (get-text-property (1- (point)) 'intangible))))
667 (when (and before after (eq before after))
668 (if isearch-forward
669 (goto-char (next-single-property-change (point) 'intangible))
670 (goto-char (previous-single-property-change (point) 'intangible)))))
671
672 (if (and (> (length isearch-string) 0) (not nopush))
673 ;; Update the ring data.
674 (isearch-update-ring isearch-string isearch-regexp))
675
676 (run-hooks 'isearch-mode-end-hook)
677 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
678
679 (defun isearch-update-ring (string &optional regexp)
680 "Add STRING to the beginning of the search ring.
681 REGEXP says which ring to use."
682 (if regexp
683 (if (or (null regexp-search-ring)
684 (not (string= string (car regexp-search-ring))))
685 (progn
686 (setq regexp-search-ring
687 (cons string regexp-search-ring))
688 (if (> (length regexp-search-ring) regexp-search-ring-max)
689 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
690 nil))))
691 (if (or (null search-ring)
692 (not (string= string (car search-ring))))
693 (progn
694 (setq search-ring (cons string search-ring))
695 (if (> (length search-ring) search-ring-max)
696 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
697
698 ;;; Switching buffers should first terminate isearch-mode.
699 ;;; ;; For Emacs 19, the frame switch event is handled.
700 ;;; (defun isearch-switch-frame-handler ()
701 ;;; (interactive) ;; Is this necessary?
702 ;;; ;; First terminate isearch-mode.
703 ;;; (isearch-done)
704 ;;; (isearch-clean-overlays)
705 ;;; (handle-switch-frame (car (cdr last-command-char))))
706
707 \f
708 ;; Commands active while inside of the isearch minor mode.
709
710 (defun isearch-exit ()
711 "Exit search normally.
712 However, if this is the first command after starting incremental
713 search and `search-nonincremental-instead' is non-nil, do a
714 nonincremental search instead via `isearch-edit-string'."
715 (interactive)
716 (if (and search-nonincremental-instead
717 (= 0 (length isearch-string)))
718 (let ((isearch-nonincremental t))
719 (isearch-edit-string)))
720 (isearch-done)
721 (isearch-clean-overlays))
722
723
724 (defun isearch-edit-string ()
725 "Edit the search string in the minibuffer.
726 The following additional command keys are active while editing.
727 \\<minibuffer-local-isearch-map>
728 \\[exit-minibuffer] to resume incremental searching with the edited string.
729 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
730 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
731 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
732 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
733 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
734 \\[isearch-complete-edit] to complete the search string using the search ring.
735 \\<isearch-mode-map>
736 If first char entered is \\[isearch-yank-word], then do word search instead."
737
738 ;; This code is very hairy for several reasons, explained in the code.
739 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
740 ;; If there were a way to catch any change of buffer from the minibuffer,
741 ;; this could be simplified greatly.
742 ;; Editing doesn't back up the search point. Should it?
743 (interactive)
744 (condition-case err
745 (progn
746 (let ((isearch-nonincremental isearch-nonincremental)
747
748 ;; Locally bind all isearch global variables to protect them
749 ;; from recursive isearching.
750 ;; isearch-string -message and -forward are not bound
751 ;; so they may be changed. Instead, save the values.
752 (isearch-new-string isearch-string)
753 (isearch-new-message isearch-message)
754 (isearch-new-forward isearch-forward)
755 (isearch-new-word isearch-word)
756
757 (isearch-regexp isearch-regexp)
758 (isearch-op-fun isearch-op-fun)
759 (isearch-cmds isearch-cmds)
760 (isearch-success isearch-success)
761 (isearch-wrapped isearch-wrapped)
762 (isearch-barrier isearch-barrier)
763 (isearch-adjusted isearch-adjusted)
764 (isearch-yank-flag isearch-yank-flag)
765 (isearch-invalid-regexp isearch-invalid-regexp)
766 (isearch-within-brackets isearch-within-brackets)
767 ;;; Don't bind this. We want isearch-search, below, to set it.
768 ;;; And the old value won't matter after that.
769 ;;; (isearch-other-end isearch-other-end)
770 ;;; Perhaps some of these other variables should be bound for a
771 ;;; shorter period, ending before the next isearch-search.
772 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
773 (isearch-opoint isearch-opoint)
774 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
775 (isearch-small-window isearch-small-window)
776 (isearch-recursive-edit isearch-recursive-edit)
777 ;; Save current configuration so we can restore it here.
778 (isearch-window-configuration (current-window-configuration))
779 )
780
781 ;; Actually terminate isearching until editing is done.
782 ;; This is so that the user can do anything without failure,
783 ;; like switch buffers and start another isearch, and return.
784 (condition-case err
785 (isearch-done t t)
786 (exit nil)) ; was recursive editing
787
788 (isearch-message) ;; for read-char
789 (unwind-protect
790 (let* (;; Why does following read-char echo?
791 ;;(echo-keystrokes 0) ;; not needed with above message
792 (e (let ((cursor-in-echo-area t))
793 (read-event)))
794 ;; Binding minibuffer-history-symbol to nil is a work-around
795 ;; for some incompatibility with gmhist.
796 (minibuffer-history-symbol)
797 (message-log-max nil))
798 ;; If the first character the user types when we prompt them
799 ;; for a string is the yank-word character, then go into
800 ;; word-search mode. Otherwise unread that character and
801 ;; read a key the normal way.
802 ;; Word search does not apply (yet) to regexp searches,
803 ;; no check is made here.
804 (message (isearch-message-prefix nil nil t))
805 (if (eq 'isearch-yank-word
806 (lookup-key isearch-mode-map (vector e)))
807 (setq isearch-word t;; so message-prefix is right
808 isearch-new-word t)
809 (cancel-kbd-macro-events)
810 (isearch-unread e))
811 (setq cursor-in-echo-area nil)
812 (setq isearch-new-string
813 (let (junk-ring)
814 (read-from-minibuffer
815 (isearch-message-prefix nil nil isearch-nonincremental)
816 isearch-string
817 minibuffer-local-isearch-map nil
818 'junk-ring))
819 isearch-new-message
820 (mapconcat 'isearch-text-char-description
821 isearch-new-string "")))
822 ;; Always resume isearching by restarting it.
823 (isearch-mode isearch-forward
824 isearch-regexp
825 isearch-op-fun
826 nil
827 isearch-word)
828
829 ;; Copy new local values to isearch globals
830 (setq isearch-string isearch-new-string
831 isearch-message isearch-new-message
832 isearch-forward isearch-new-forward
833 isearch-word isearch-new-word))
834
835 ;; Empty isearch-string means use default.
836 (if (= 0 (length isearch-string))
837 (setq isearch-string (or (car (if isearch-regexp
838 regexp-search-ring
839 search-ring))
840 ""))
841 ;; This used to set the last search string,
842 ;; but I think it is not right to do that here.
843 ;; Only the string actually used should be saved.
844 ))
845
846 ;; Push the state as of before this C-s.
847 (isearch-push-state)
848
849 ;; Reinvoke the pending search.
850 (isearch-search)
851 (isearch-update)
852 (if isearch-nonincremental
853 (progn
854 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
855 (isearch-done))))
856
857 (quit ; handle abort-recursive-edit
858 (isearch-abort) ;; outside of let to restore outside global values
859 )))
860
861 (defun isearch-nonincremental-exit-minibuffer ()
862 (interactive)
863 (setq isearch-nonincremental t)
864 (exit-minibuffer))
865
866 (defun isearch-forward-exit-minibuffer ()
867 (interactive)
868 (setq isearch-new-forward t)
869 (exit-minibuffer))
870
871 (defun isearch-reverse-exit-minibuffer ()
872 (interactive)
873 (setq isearch-new-forward nil)
874 (exit-minibuffer))
875
876 (defun isearch-cancel ()
877 "Terminate the search and go back to the starting point."
878 (interactive)
879 (goto-char isearch-opoint)
880 (isearch-done t)
881 (isearch-clean-overlays)
882 (signal 'quit nil)) ; and pass on quit signal
883
884 (defun isearch-abort ()
885 "Abort incremental search mode if searching is successful, signaling quit.
886 Otherwise, revert to previous successful search and continue searching.
887 Use `isearch-exit' to quit without signaling."
888 (interactive)
889 ;; (ding) signal instead below, if quitting
890 (discard-input)
891 (if isearch-success
892 ;; If search is successful, move back to starting point
893 ;; and really do quit.
894 (progn (goto-char isearch-opoint)
895 (setq isearch-success nil)
896 (isearch-done t) ; exit isearch
897 (isearch-clean-overlays)
898 (signal 'quit nil)) ; and pass on quit signal
899 ;; If search is failing, or has an incomplete regexp,
900 ;; rub out until it is once more successful.
901 (while (or (not isearch-success) isearch-invalid-regexp)
902 (isearch-pop-state))
903 (isearch-update)))
904
905 (defun isearch-repeat (direction)
906 ;; Utility for isearch-repeat-forward and -backward.
907 (if (eq isearch-forward (eq direction 'forward))
908 ;; C-s in forward or C-r in reverse.
909 (if (equal isearch-string "")
910 ;; If search string is empty, use last one.
911 (setq isearch-string
912 (or (if isearch-regexp
913 (car regexp-search-ring)
914 (car search-ring))
915 "")
916 isearch-message
917 (mapconcat 'isearch-text-char-description
918 isearch-string ""))
919 ;; If already have what to search for, repeat it.
920 (or isearch-success
921 (progn
922 (goto-char (if isearch-forward (point-min) (point-max)))
923 (setq isearch-wrapped t))))
924 ;; C-s in reverse or C-r in forward, change direction.
925 (setq isearch-forward (not isearch-forward)))
926
927 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
928
929 (if (equal isearch-string "")
930 (setq isearch-success t)
931 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
932 (not isearch-just-started))
933 ;; If repeating a search that found
934 ;; an empty string, ensure we advance.
935 (if (if isearch-forward (eobp) (bobp))
936 ;; If there's nowhere to advance to, fail (and wrap next time).
937 (progn
938 (setq isearch-success nil)
939 (ding))
940 (forward-char (if isearch-forward 1 -1))
941 (isearch-search))
942 (isearch-search)))
943
944 (isearch-push-state)
945 (isearch-update))
946
947 (defun isearch-repeat-forward ()
948 "Repeat incremental search forwards."
949 (interactive)
950 (isearch-repeat 'forward))
951
952 (defun isearch-repeat-backward ()
953 "Repeat incremental search backwards."
954 (interactive)
955 (isearch-repeat 'backward))
956
957 (defun isearch-toggle-regexp ()
958 "Toggle regexp searching on or off."
959 ;; The status stack is left unchanged.
960 (interactive)
961 (setq isearch-regexp (not isearch-regexp))
962 (if isearch-regexp (setq isearch-word nil))
963 (isearch-update))
964
965 (defun isearch-toggle-case-fold ()
966 "Toggle case folding in searching on or off."
967 (interactive)
968 (setq isearch-case-fold-search
969 (if isearch-case-fold-search nil 'yes))
970 (let ((message-log-max nil))
971 (message "%s%s [case %ssensitive]"
972 (isearch-message-prefix nil nil isearch-nonincremental)
973 isearch-message
974 (if isearch-case-fold-search "in" "")))
975 (setq isearch-adjusted t)
976 (sit-for 1)
977 (isearch-update))
978
979 (defun isearch-delete-char ()
980 "Discard last input item and move point back.
981 If no previous match was done, just beep."
982 (interactive)
983 (if (null (cdr isearch-cmds))
984 (ding)
985 (isearch-pop-state))
986 (isearch-update))
987
988
989 (defun isearch-yank-string (string)
990 "Pull STRING into search string."
991 ;; Downcase the string if not supposed to case-fold yanked strings.
992 (if (and isearch-case-fold-search
993 (eq 'not-yanks search-upper-case))
994 (setq string (downcase string)))
995 (if isearch-regexp (setq string (regexp-quote string)))
996 (setq isearch-string (concat isearch-string string)
997 isearch-message
998 (concat isearch-message
999 (mapconcat 'isearch-text-char-description
1000 string ""))
1001 ;; Don't move cursor in reverse search.
1002 isearch-yank-flag t)
1003 (isearch-search-and-update))
1004
1005 (defun isearch-yank-kill ()
1006 "Pull string from kill ring into search string."
1007 (interactive)
1008 (isearch-yank-string (current-kill 0)))
1009
1010 (defun isearch-yank-x-selection ()
1011 "Pull current X selection into search string."
1012 (interactive)
1013 (isearch-yank-string (x-get-selection)))
1014
1015 (defun isearch-mouse-yank (click arg)
1016 "Yank with the mouse in Isearch mode.
1017 For a click in the echo area, invoke `isearch-yank-x-selection'.
1018 Otherwise invoke `mouse-yank-at-click'."
1019 (interactive "e\nP")
1020 (let ((w (posn-window (event-start click))))
1021 (if (and (window-minibuffer-p w)
1022 (not (minibuffer-window-active-p w))) ; in echo area
1023 (isearch-yank-x-selection)
1024 (mouse-yank-at-click click arg))))
1025
1026 (defun isearch-yank-word ()
1027 "Pull next word 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) (progn (forward-word 1) (point))))))
1034
1035 (defun isearch-yank-line ()
1036 "Pull rest of line from buffer into search string."
1037 (interactive)
1038 (isearch-yank-string
1039 (save-excursion
1040 (and (not isearch-forward) isearch-other-end
1041 (goto-char isearch-other-end))
1042 (buffer-substring (point) (line-end-position)))))
1043
1044
1045 (defun isearch-search-and-update ()
1046 ;; Do the search and update the display.
1047 (if (and (not isearch-success)
1048 ;; unsuccessful regexp search may become
1049 ;; successful by addition of characters which
1050 ;; make isearch-string valid
1051 (not isearch-regexp))
1052 nil
1053 ;; In reverse search, adding stuff at
1054 ;; the end may cause zero or many more chars to be
1055 ;; matched, in the string following point.
1056 ;; Allow all those possibilities without moving point as
1057 ;; long as the match does not extend past search origin.
1058 (if (and (not isearch-forward) (not isearch-adjusted)
1059 (condition-case ()
1060 (let ((case-fold-search isearch-case-fold-search))
1061 (if (and (eq case-fold-search t) search-upper-case)
1062 (setq case-fold-search
1063 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1064 (looking-at (if isearch-regexp isearch-string
1065 (regexp-quote isearch-string))))
1066 (error nil))
1067 (or isearch-yank-flag
1068 (<= (match-end 0)
1069 (min isearch-opoint isearch-barrier))))
1070 (progn
1071 (setq isearch-success t
1072 isearch-invalid-regexp nil
1073 isearch-within-brackets nil
1074 isearch-other-end (match-end 0))
1075 (if (and (eq isearch-case-fold-search t) search-upper-case)
1076 (setq isearch-case-fold-search
1077 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1078 ;; Not regexp, not reverse, or no match at point.
1079 (if (and isearch-other-end (not isearch-adjusted))
1080 (goto-char (if isearch-forward isearch-other-end
1081 (min isearch-opoint
1082 isearch-barrier
1083 (1+ isearch-other-end)))))
1084 (isearch-search)
1085 ))
1086 (isearch-push-state)
1087 (if isearch-op-fun (funcall isearch-op-fun))
1088 (isearch-update))
1089
1090
1091 ;; *, ?, and | chars can make a regexp more liberal.
1092 ;; They can make a regexp match sooner or make it succeed instead of failing.
1093 ;; So go back to place last successful search started
1094 ;; or to the last ^S/^R (barrier), whichever is nearer.
1095 ;; + needs no special handling because the string must match at least once.
1096
1097 (defun isearch-*-char ()
1098 "Handle * and ? specially in regexps."
1099 (interactive)
1100 (if isearch-regexp
1101 (let ((idx (length isearch-string)))
1102 (while (and (> idx 0)
1103 (eq (aref isearch-string (1- idx)) ?\\))
1104 (setq idx (1- idx)))
1105 (when (= (mod (- (length isearch-string) idx) 2) 0)
1106 (setq isearch-adjusted t)
1107 ;; Get the isearch-other-end from before the last search.
1108 ;; We want to start from there,
1109 ;; so that we don't retreat farther than that.
1110 ;; (car isearch-cmds) is after last search;
1111 ;; (car (cdr isearch-cmds)) is from before it.
1112 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1113 (setq cs (or cs isearch-barrier))
1114 (goto-char
1115 (if isearch-forward
1116 (max cs isearch-barrier)
1117 (min cs isearch-barrier)))))))
1118 (isearch-process-search-char last-command-char))
1119
1120
1121 (defun isearch-|-char ()
1122 "If in regexp search, jump to the barrier."
1123 (interactive)
1124 (if isearch-regexp
1125 (progn
1126 (setq isearch-adjusted t)
1127 (goto-char isearch-barrier)))
1128 (isearch-process-search-char last-command-char))
1129
1130
1131 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1132
1133 (defun isearch-other-meta-char ()
1134 "Exit the search normally and reread this key sequence.
1135 But only if `search-exit-option' is non-nil, the default.
1136 If it is the symbol `edit', the search string is edited in the minibuffer
1137 and the meta character is unread so that it applies to editing the string."
1138 (interactive)
1139 (let* ((key (this-command-keys))
1140 (main-event (aref key 0))
1141 (keylist (listify-key-sequence key)))
1142 (cond ((and (= (length key) 1)
1143 (let ((lookup (lookup-key function-key-map key)))
1144 (not (or (null lookup) (integerp lookup)
1145 (keymapp lookup)))))
1146 ;; Handle a function key that translates into something else.
1147 ;; If the key has a global definition too,
1148 ;; exit and unread the key itself, so its global definition runs.
1149 ;; Otherwise, unread the translation,
1150 ;; so that the translated key takes effect within isearch.
1151 (cancel-kbd-macro-events)
1152 (if (lookup-key global-map key)
1153 (progn
1154 (isearch-done)
1155 (apply 'isearch-unread keylist))
1156 (setq keylist
1157 (listify-key-sequence (lookup-key function-key-map key)))
1158 (while keylist
1159 (setq key (car keylist))
1160 ;; If KEY is a printing char, we handle it here
1161 ;; directly to avoid the input method and keyboard
1162 ;; coding system translating it.
1163 (if (and (integerp key)
1164 (>= key ?\ ) (< key 256))
1165 (progn
1166 (isearch-process-search-char key)
1167 (setq keylist (cdr keylist)))
1168 ;; As the remaining keys in KEYLIST can't be handled
1169 ;; here, we must reread them.
1170 (apply 'isearch-unread keylist)
1171 (setq keylist nil)))))
1172 (
1173 ;; Handle an undefined shifted control character
1174 ;; by downshifting it if that makes it defined.
1175 ;; (As read-key-sequence would normally do,
1176 ;; if we didn't have a default definition.)
1177 (let ((mods (event-modifiers main-event)))
1178 (and (integerp main-event)
1179 (memq 'shift mods)
1180 (memq 'control mods)
1181 (lookup-key isearch-mode-map
1182 (let ((copy (copy-sequence key)))
1183 (aset copy 0
1184 (- main-event (- ?\C-\S-a ?\C-a)))
1185 copy)
1186 nil)))
1187 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1188 (cancel-kbd-macro-events)
1189 (apply 'isearch-unread keylist))
1190 ((eq search-exit-option 'edit)
1191 (apply 'isearch-unread keylist)
1192 (isearch-edit-string))
1193 (search-exit-option
1194 (let (window)
1195 (cancel-kbd-macro-events)
1196 (apply 'isearch-unread keylist)
1197 ;; Properly handle scroll-bar and mode-line clicks
1198 ;; for which a dummy prefix event was generated as (aref key 0).
1199 (and (> (length key) 1)
1200 (symbolp (aref key 0))
1201 (listp (aref key 1))
1202 (not (numberp (posn-point (event-start (aref key 1)))))
1203 ;; Convert the event back into its raw form,
1204 ;; with the dummy prefix implicit in the mouse event,
1205 ;; so it will get split up once again.
1206 (progn (setq unread-command-events
1207 (cdr unread-command-events))
1208 (setq main-event (car unread-command-events))
1209 (setcar (cdr (event-start main-event))
1210 (car (nth 1 (event-start main-event))))))
1211 ;; If we got a mouse click, maybe it was read with the buffer
1212 ;; it was clicked on. If so, that buffer, not the current one,
1213 ;; is in isearch mode. So end the search in that buffer.
1214 (if (and (listp main-event)
1215 (setq window (posn-window (event-start main-event)))
1216 (windowp window)
1217 (or (> (minibuffer-depth) 0)
1218 (not (window-minibuffer-p window))))
1219 (save-excursion
1220 (set-buffer (window-buffer window))
1221 (isearch-done)
1222 (isearch-clean-overlays))
1223 (isearch-done)
1224 (isearch-clean-overlays))))
1225 (t;; otherwise nil
1226 (isearch-process-search-string key key)))))
1227
1228 (defun isearch-quote-char ()
1229 "Quote special characters for incremental search."
1230 (interactive)
1231 (let ((char (read-quoted-char (isearch-message t))))
1232 ;; Assume character codes 0200 - 0377 stand for characters in some
1233 ;; single-byte character set, and convert them to Emacs
1234 ;; characters.
1235 (and enable-multibyte-characters
1236 (>= char ?\200)
1237 (<= char ?\377)
1238 (setq char (unibyte-char-to-multibyte char)))
1239 (isearch-process-search-char char)))
1240
1241 (defun isearch-return-char ()
1242 "Convert return into newline for incremental search.
1243 Obsolete."
1244 (interactive)
1245 (isearch-process-search-char ?\n))
1246
1247 (defun isearch-printing-char ()
1248 "Add this ordinary printing character to the search string and search."
1249 (interactive)
1250 (let ((char last-command-char))
1251 (if (= char ?\S-\ )
1252 (setq char ?\ ))
1253 (if (and enable-multibyte-characters
1254 (>= char ?\200)
1255 (<= char ?\377))
1256 (if (keyboard-coding-system)
1257 (isearch-process-search-multibyte-characters char)
1258 (isearch-process-search-char (unibyte-char-to-multibyte char)))
1259 (if current-input-method
1260 (isearch-process-search-multibyte-characters char)
1261 (isearch-process-search-char char)))))
1262
1263 (defun isearch-whitespace-chars ()
1264 "Match all whitespace chars, if in regexp mode.
1265 If you want to search for just a space, type \\[quoted-insert] SPC."
1266 (interactive)
1267 (if isearch-regexp
1268 (if (and search-whitespace-regexp (not isearch-within-brackets)
1269 (not isearch-invalid-regexp))
1270 (isearch-process-search-string search-whitespace-regexp " ")
1271 (isearch-printing-char))
1272 (progn
1273 ;; This way of doing word search doesn't correctly extend current search.
1274 ;; (setq isearch-word t)
1275 ;; (setq isearch-adjusted t)
1276 ;; (goto-char isearch-barrier)
1277 (isearch-printing-char))))
1278
1279 (defun isearch-process-search-char (char)
1280 ;; Append the char to the search string, update the message and re-search.
1281 (isearch-process-search-string
1282 (char-to-string char)
1283 (if (>= char ?\200)
1284 (char-to-string char)
1285 (isearch-text-char-description char))))
1286
1287 (defun isearch-process-search-string (string message)
1288 (setq isearch-string (concat isearch-string string)
1289 isearch-message (concat isearch-message message))
1290 (isearch-search-and-update))
1291
1292 \f
1293 ;; Search Ring
1294
1295 (defun isearch-ring-adjust1 (advance)
1296 ;; Helper for isearch-ring-adjust
1297 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1298 (length (length ring))
1299 (yank-pointer-name (if isearch-regexp
1300 'regexp-search-ring-yank-pointer
1301 'search-ring-yank-pointer))
1302 (yank-pointer (eval yank-pointer-name)))
1303 (if (zerop length)
1304 ()
1305 (set yank-pointer-name
1306 (setq yank-pointer
1307 (mod (+ (or yank-pointer 0)
1308 (if advance -1 1))
1309 length)))
1310 (setq isearch-string (nth yank-pointer ring)
1311 isearch-message (mapconcat 'isearch-text-char-description
1312 isearch-string "")))))
1313
1314 (defun isearch-ring-adjust (advance)
1315 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1316 (isearch-ring-adjust1 advance)
1317 (if search-ring-update
1318 (progn
1319 (isearch-search)
1320 (isearch-update))
1321 (isearch-edit-string)
1322 )
1323 (isearch-push-state))
1324
1325 (defun isearch-ring-advance ()
1326 "Advance to the next search string in the ring."
1327 ;; This could be more general to handle a prefix arg, but who would use it.
1328 (interactive)
1329 (isearch-ring-adjust 'advance))
1330
1331 (defun isearch-ring-retreat ()
1332 "Retreat to the previous search string in the ring."
1333 (interactive)
1334 (isearch-ring-adjust nil))
1335
1336 (defun isearch-ring-advance-edit (n)
1337 "Insert the next element of the search history into the minibuffer.
1338 With prefix arg N, insert the Nth element."
1339 (interactive "p")
1340 (let* ((yank-pointer-name (if isearch-regexp
1341 'regexp-search-ring-yank-pointer
1342 'search-ring-yank-pointer))
1343 (yank-pointer (eval yank-pointer-name))
1344 (ring (if isearch-regexp regexp-search-ring search-ring))
1345 (length (length ring)))
1346 (if (zerop length)
1347 ()
1348 (set yank-pointer-name
1349 (setq yank-pointer
1350 (mod (- (or yank-pointer 0) n)
1351 length)))
1352
1353 (delete-field)
1354 (insert (nth yank-pointer ring))
1355 (goto-char (point-max)))))
1356
1357 (defun isearch-ring-retreat-edit (n)
1358 "Insert the previous element of the search history into the minibuffer.
1359 With prefix arg N, insert the Nth element."
1360 (interactive "p")
1361 (isearch-ring-advance-edit (- n)))
1362
1363 ;;(defun isearch-ring-adjust-edit (advance)
1364 ;; "Use the next or previous search string in the ring while in minibuffer."
1365 ;; (isearch-ring-adjust1 advance)
1366 ;; (erase-buffer)
1367 ;; (insert isearch-string))
1368
1369 ;;(defun isearch-ring-advance-edit ()
1370 ;; (interactive)
1371 ;; (isearch-ring-adjust-edit 'advance))
1372
1373 ;;(defun isearch-ring-retreat-edit ()
1374 ;; "Retreat to the previous search string in the ring while in the minibuffer."
1375 ;; (interactive)
1376 ;; (isearch-ring-adjust-edit nil))
1377
1378
1379 (defun isearch-complete1 ()
1380 ;; Helper for isearch-complete and isearch-complete-edit
1381 ;; Return t if completion OK, nil if no completion exists.
1382 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1383 (alist (mapcar (function (lambda (string) (list string))) ring))
1384 (completion-ignore-case case-fold-search)
1385 (completion (try-completion isearch-string alist)))
1386 (cond
1387 ((eq completion t)
1388 ;; isearch-string stays the same
1389 t)
1390 ((or completion ; not nil, must be a string
1391 (= 0 (length isearch-string))) ; shouldn't have to say this
1392 (if (equal completion isearch-string) ;; no extension?
1393 (progn
1394 (if completion-auto-help
1395 (with-output-to-temp-buffer "*Isearch completions*"
1396 (display-completion-list
1397 (all-completions isearch-string alist))))
1398 t)
1399 (and completion
1400 (setq isearch-string completion))))
1401 (t
1402 (message "No completion") ; waits a second if in minibuffer
1403 nil))))
1404
1405 (defun isearch-complete ()
1406 "Complete the search string from the strings on the search ring.
1407 The completed string is then editable in the minibuffer.
1408 If there is no completion possible, say so and continue searching."
1409 (interactive)
1410 (if (isearch-complete1)
1411 (isearch-edit-string)
1412 ;; else
1413 (sit-for 1)
1414 (isearch-update)))
1415
1416 (defun isearch-complete-edit ()
1417 "Same as `isearch-complete' except in the minibuffer."
1418 (interactive)
1419 (setq isearch-string (buffer-string))
1420 (if (isearch-complete1)
1421 (progn
1422 (delete-field)
1423 (insert isearch-string))))
1424
1425 \f
1426 ;; The search status stack (and isearch window-local variables, not used).
1427 ;; Need a structure for this.
1428
1429 (defun isearch-top-state ()
1430 (let ((cmd (car isearch-cmds)))
1431 (setq isearch-string (car cmd)
1432 isearch-message (car (cdr cmd))
1433 isearch-success (nth 3 cmd)
1434 isearch-forward (nth 4 cmd)
1435 isearch-other-end (nth 5 cmd)
1436 isearch-word (nth 6 cmd)
1437 isearch-invalid-regexp (nth 7 cmd)
1438 isearch-wrapped (nth 8 cmd)
1439 isearch-barrier (nth 9 cmd)
1440 isearch-within-brackets (nth 10 cmd)
1441 isearch-case-fold-search (nth 11 cmd))
1442 (goto-char (car (cdr (cdr cmd))))))
1443
1444 (defun isearch-pop-state ()
1445 (setq isearch-cmds (cdr isearch-cmds))
1446 (isearch-top-state)
1447 )
1448
1449 (defun isearch-push-state ()
1450 (setq isearch-cmds
1451 (cons (list isearch-string isearch-message (point)
1452 isearch-success isearch-forward isearch-other-end
1453 isearch-word
1454 isearch-invalid-regexp isearch-wrapped isearch-barrier
1455 isearch-within-brackets isearch-case-fold-search)
1456 isearch-cmds)))
1457
1458 \f
1459 ;; Message string
1460
1461 (defun isearch-message (&optional c-q-hack ellipsis)
1462 ;; Generate and print the message string.
1463 (let ((cursor-in-echo-area ellipsis)
1464 (m (concat
1465 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
1466 isearch-message
1467 (isearch-message-suffix c-q-hack ellipsis)
1468 )))
1469 (if c-q-hack
1470 m
1471 (let ((message-log-max nil))
1472 (message "%s" m)))))
1473
1474 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
1475 ;; If about to search, and previous search regexp was invalid,
1476 ;; check that it still is. If it is valid now,
1477 ;; let the message we display while searching say that it is valid.
1478 (and isearch-invalid-regexp ellipsis
1479 (condition-case ()
1480 (progn (re-search-forward isearch-string (point) t)
1481 (setq isearch-invalid-regexp nil
1482 isearch-within-brackets nil))
1483 (error nil)))
1484 ;; If currently failing, display no ellipsis.
1485 (or isearch-success (setq ellipsis nil))
1486 (let ((m (concat (if isearch-success "" "failing ")
1487 (if (and isearch-wrapped
1488 (if isearch-forward
1489 (> (point) isearch-opoint)
1490 (< (point) isearch-opoint)))
1491 "over")
1492 (if isearch-wrapped "wrapped ")
1493 (if isearch-word "word " "")
1494 (if isearch-regexp "regexp " "")
1495 (if nonincremental "search" "I-search")
1496 (if isearch-forward "" " backward")
1497 (if current-input-method
1498 (concat " [" current-input-method-title "]: ")
1499 ": ")
1500 )))
1501 (aset m 0 (upcase (aref m 0)))
1502 m))
1503
1504
1505 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
1506 (concat (if c-q-hack "^Q" "")
1507 (if isearch-invalid-regexp
1508 (concat " [" isearch-invalid-regexp "]")
1509 "")))
1510
1511 \f
1512 ;;; Searching
1513
1514 (defun isearch-search ()
1515 ;; Do the search with the current search string.
1516 (isearch-message nil t)
1517 (if (and (eq isearch-case-fold-search t) search-upper-case)
1518 (setq isearch-case-fold-search
1519 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1520 (condition-case lossage
1521 (let ((inhibit-point-motion-hooks search-invisible)
1522 (inhibit-quit nil)
1523 (case-fold-search isearch-case-fold-search)
1524 (retry t))
1525 (if isearch-regexp (setq isearch-invalid-regexp nil))
1526 (setq isearch-within-brackets nil)
1527 (while retry
1528 (setq isearch-success
1529 (funcall
1530 (cond (isearch-word
1531 (if isearch-forward
1532 'word-search-forward 'word-search-backward))
1533 (isearch-regexp
1534 (if isearch-forward
1535 're-search-forward 're-search-backward))
1536 (t
1537 (if isearch-forward 'search-forward 'search-backward)))
1538 isearch-string nil t))
1539 ;; Clear RETRY unless we matched some invisible text
1540 ;; and we aren't supposed to do that.
1541 (if (or (eq search-invisible t)
1542 (not isearch-success)
1543 (bobp) (eobp)
1544 (= (match-beginning 0) (match-end 0))
1545 (not (isearch-range-invisible
1546 (match-beginning 0) (match-end 0))))
1547 (setq retry nil)))
1548 (setq isearch-just-started nil)
1549 (if isearch-success
1550 (setq isearch-other-end
1551 (if isearch-forward (match-beginning 0) (match-end 0)))))
1552
1553 (quit (isearch-unread ?\C-g)
1554 (setq isearch-success nil))
1555
1556 (invalid-regexp
1557 (setq isearch-invalid-regexp (car (cdr lossage)))
1558 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1559 isearch-invalid-regexp))
1560 (if (string-match
1561 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1562 isearch-invalid-regexp)
1563 (setq isearch-invalid-regexp "incomplete input")))
1564 (error
1565 ;; stack overflow in regexp search.
1566 (setq isearch-invalid-regexp (car (cdr lossage)))))
1567
1568 (if isearch-success
1569 nil
1570 ;; Ding if failed this time after succeeding last time.
1571 (and (nth 3 (car isearch-cmds))
1572 (ding))
1573 (goto-char (nth 2 (car isearch-cmds)))))
1574
1575
1576 ;;; Called when opening an overlay, and we are still in isearch.
1577 (defun isearch-open-overlay-temporary (ov)
1578 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1579 ;; Some modes would want to open the overlays temporary during
1580 ;; isearch in their own way, they should set the
1581 ;; `isearch-open-invisible-temporary' to a function doing this.
1582 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1583 ;; Store the values for the `invisible' and `intangible'
1584 ;; properties, and then set them to nil. This way the text hidden
1585 ;; by this overlay becomes visible.
1586
1587 ;; Do we realy need to set the `intangible' property to t? Can we
1588 ;; have the point inside an overlay with an `intangible' property?
1589 ;; In 19.34 this does not exist so I cannot test it.
1590 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1591 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1592 (overlay-put ov 'invisible nil)
1593 (overlay-put ov 'intangible nil)))
1594
1595
1596 ;;; This is called at the end of isearch. It will open the overlays
1597 ;;; that contain the latest match. Obviously in case of a C-g the
1598 ;;; point returns to the original location which surely is not contain
1599 ;;; in any of these overlays, se we are safe in this case too.
1600 (defun isearch-open-necessary-overlays (ov)
1601 (let ((inside-overlay (and (> (point) (overlay-start ov))
1602 (< (point) (overlay-end ov))))
1603 ;; If this exists it means that the overlay was opened using
1604 ;; this function, not by us tweaking the overlay properties.
1605 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1606 (when (or inside-overlay (not fct-temp))
1607 ;; restore the values for the `invisible' and `intangible'
1608 ;; properties
1609 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1610 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1611 (overlay-put ov 'isearch-invisible nil)
1612 (overlay-put ov 'isearch-intangible nil))
1613 (if inside-overlay
1614 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1615 (if fct-temp
1616 (funcall fct-temp ov t)))))
1617
1618 ;;; This is called when exiting isearch. It closes the temporary
1619 ;;; opened overlays, except the ones that contain the latest match.
1620 (defun isearch-clean-overlays ()
1621 (when isearch-opened-overlays
1622 ;; Use a cycle instead of a mapcar here?
1623 (mapcar
1624 (function isearch-open-necessary-overlays) isearch-opened-overlays)
1625 (setq isearch-opened-overlays nil)))
1626
1627 ;;; Verify if the current match is outside of each element of
1628 ;;; `isearch-opened-overlays', if so close that overlay.
1629 (defun isearch-close-unecessary-overlays (begin end)
1630 (let ((ov-list isearch-opened-overlays)
1631 ov
1632 inside-overlay
1633 fct-temp)
1634 (setq isearch-opened-overlays nil)
1635 (while ov-list
1636 (setq ov (car ov-list))
1637 (setq ov-list (cdr ov-list))
1638 (setq inside-overlay (or (and (> begin (overlay-start ov))
1639 (< begin (overlay-end ov)))
1640 (and (> end (overlay-start ov))
1641 (< end (overlay-end ov)))))
1642 ;; If this exists it means that the overlay was opened using
1643 ;; this function, not by us tweaking the overlay properties.
1644 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1645 (if inside-overlay
1646 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1647 (if fct-temp
1648 (funcall fct-temp ov t)
1649 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1650 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1651 (overlay-put ov 'isearch-invisible nil)
1652 (overlay-put ov 'isearch-intangible nil))))))
1653
1654 (defun isearch-range-invisible (beg end)
1655 "Return t if all the text from BEG to END is invisible."
1656 (and (/= beg end)
1657 ;; Check that invisibility runs up to END.
1658 (save-excursion
1659 (goto-char beg)
1660 (let (
1661 ;; can-be-opened keeps track if we can open some overlays.
1662 (can-be-opened (eq search-invisible 'open))
1663 ;; the list of overlays that could be opened
1664 (crt-overlays nil))
1665 (when (and can-be-opened isearch-hide-immediately)
1666 (isearch-close-unecessary-overlays beg end))
1667 ;; If the following character is currently invisible,
1668 ;; skip all characters with that same `invisible' property value.
1669 ;; Do that over and over.
1670 (while (and (< (point) end)
1671 (let ((prop
1672 (get-char-property (point) 'invisible)))
1673 (if (eq buffer-invisibility-spec t)
1674 prop
1675 (or (memq prop buffer-invisibility-spec)
1676 (assq prop buffer-invisibility-spec)))))
1677 (if (get-text-property (point) 'invisible)
1678 (progn
1679 (goto-char (next-single-property-change (point) 'invisible
1680 nil end))
1681 ;; if text is hidden by an `invisible' text property
1682 ;; we cannot open it at all.
1683 (setq can-be-opened nil))
1684 (unless (null can-be-opened)
1685 (let ((overlays (overlays-at (point)))
1686 ov-list
1687 o
1688 invis-prop)
1689 (while overlays
1690 (setq o (car overlays)
1691 invis-prop (overlay-get o 'invisible))
1692 (if (if (eq buffer-invisibility-spec t)
1693 invis-prop
1694 (or (memq invis-prop buffer-invisibility-spec)
1695 (assq invis-prop buffer-invisibility-spec)))
1696 (if (overlay-get o 'isearch-open-invisible)
1697 (setq ov-list (cons o ov-list))
1698 ;; We found one overlay that cannot be
1699 ;; opened, that means the whole chunk
1700 ;; cannot be opened.
1701 (setq can-be-opened nil)))
1702 (setq overlays (cdr overlays)))
1703 (if can-be-opened
1704 ;; It makes sense to append to the open
1705 ;; overlays list only if we know that this is
1706 ;; t.
1707 (setq crt-overlays (append ov-list crt-overlays)))))
1708 (goto-char (next-overlay-change (point)))))
1709 ;; See if invisibility reaches up thru END.
1710 (if (>= (point) end)
1711 (if (and (not (null can-be-opened)) (consp crt-overlays))
1712 (progn
1713 (setq isearch-opened-overlays
1714 (append isearch-opened-overlays crt-overlays))
1715 ;; maybe use a cycle instead of mapcar?
1716 (mapcar (function isearch-open-overlay-temporary)
1717 crt-overlays)
1718 nil)
1719 t))))))
1720
1721 \f
1722 ;;; Highlighting
1723
1724 (defvar isearch-overlay nil)
1725
1726 (defun isearch-highlight (beg end)
1727 (if (or (null search-highlight) (null (display-color-p)))
1728 nil
1729 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1730 (move-overlay isearch-overlay beg end (current-buffer))
1731 (overlay-put isearch-overlay 'face
1732 (if (internal-find-face 'isearch nil)
1733 'isearch 'region))))
1734
1735 (defun isearch-dehighlight (totally)
1736 (if isearch-overlay
1737 (delete-overlay isearch-overlay)))
1738
1739 ;;; General utilities
1740
1741
1742 (defun isearch-no-upper-case-p (string regexp-flag)
1743 "Return t if there are no upper case chars in STRING.
1744 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
1745 since they have special meaning in a regexp."
1746 (let (quote-flag (i 0) (len (length string)) found)
1747 (while (and (not found) (< i len))
1748 (let ((char (aref string i)))
1749 (if (and regexp-flag (eq char ?\\))
1750 (setq quote-flag (not quote-flag))
1751 (if (and (not quote-flag) (not (eq char (downcase char))))
1752 (setq found t))))
1753 (setq i (1+ i)))
1754 (not found)))
1755
1756 ;; Portability functions to support various Emacs versions.
1757
1758 (defun isearch-text-char-description (c)
1759 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1760 (text-char-description c)
1761 (char-to-string c)))
1762
1763 ;; General function to unread characters or events.
1764 ;; Also insert them in a keyboard macro being defined.
1765 (defun isearch-unread (&rest char-or-events)
1766 (mapcar 'store-kbd-macro-event char-or-events)
1767 (setq unread-command-events
1768 (append char-or-events unread-command-events)))
1769
1770 \f
1771 ;;; isearch-lazy-highlight feature
1772 ;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1773
1774 ;;; When active, *every* match for the current search string is
1775 ;;; highlighted: the current one using the normal isearch match color
1776 ;;; and all the others using the unobtrusive `secondary-selection'
1777 ;;; color. The extra highlighting makes it easier to anticipate where
1778 ;;; the cursor will land each time you press C-s or C-r to repeat a
1779 ;;; pending search. Highlighting of these additional matches happens
1780 ;;; in a deferred fashion using "idle timers," so the cycles needed do
1781 ;;; not rob isearch of its usual snappy response.
1782
1783 ;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1784 ;;; Specifically:
1785 ;;; - `isearch-update' is expected to be called (at least) every time
1786 ;;; the search string changes;
1787 ;;; - `isearch-string' is expected to contain the current search
1788 ;;; string as entered by the user;
1789 ;;; - `isearch-overlay' is expected to contain the overlay used for
1790 ;;; primary isearch match-highlighting;
1791 ;;; - `isearch-opoint' is expected to contain the location where the
1792 ;;; current search began;
1793 ;;; - the type of the current search is expected to be given by
1794 ;;; `isearch-word' and `isearch-regexp';
1795 ;;; - the direction of the current search is expected to be given by
1796 ;;; `isearch-forward';
1797 ;;; - the variable `isearch-invalid-regexp' is expected to be true
1798 ;;; iff `isearch-string' is an invalid regexp.
1799
1800 (require 'timer)
1801
1802 (defgroup isearch-lazy-highlight nil
1803 "Lazy highlighting feature for incremental search."
1804 :prefix "isearch-lazy-highlight-"
1805 :group 'isearch)
1806
1807 (defcustom isearch-lazy-highlight t
1808 "*Controls the lazy-highlighting during incremental searches.
1809 When non-nil, all text in the buffer matching the current search
1810 string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1811 and `isearch-lazy-highlight-interval')."
1812 :type 'boolean
1813 :group 'isearch-lazy-highlight)
1814
1815 (defcustom isearch-lazy-highlight-cleanup t
1816 "*Controls whether to remove extra highlighting after a search.
1817 If this is nil, extra highlighting can be \"manually\" removed with
1818 \\[isearch-lazy-highlight-cleanup]."
1819 :type 'boolean
1820 :group 'isearch-lazy-highlight)
1821
1822 (defcustom isearch-lazy-highlight-initial-delay 0.25
1823 "*Seconds to wait before beginning to lazily highlight all matches."
1824 :type 'number
1825 :group 'isearch-lazy-highlight)
1826
1827 (defcustom isearch-lazy-highlight-interval 0.0625
1828 "*Seconds between lazily highlighting successive matches."
1829 :type 'number
1830 :group 'isearch-lazy-highlight)
1831
1832 (defcustom isearch-lazy-highlight-face 'secondary-selection
1833 "*Face to use for lazily highlighting all matches."
1834 :type 'face
1835 :group 'isearch-lazy-highlight)
1836
1837 (defvar isearch-lazy-highlight-overlays nil)
1838 (defvar isearch-lazy-highlight-wrapped nil)
1839 (defvar isearch-lazy-highlight-start nil)
1840 (defvar isearch-lazy-highlight-end nil)
1841 (defvar isearch-lazy-highlight-timer nil)
1842 (defvar isearch-lazy-highlight-last-string nil)
1843
1844 (defun isearch-lazy-highlight-cleanup (&optional force)
1845 "Stop lazy highlighting and remove extra highlighting from current buffer.
1846 FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
1847 is nil. This function is called when exiting an incremental search if
1848 `isearch-lazy-highlight-cleanup' is non-nil."
1849 (interactive '(t))
1850 (if (or force isearch-lazy-highlight-cleanup)
1851 (isearch-lazy-highlight-remove-overlays))
1852 (if isearch-lazy-highlight-timer
1853 (progn
1854 (cancel-timer isearch-lazy-highlight-timer)
1855 (setq isearch-lazy-highlight-timer nil))))
1856
1857 (defun isearch-lazy-highlight-remove-overlays ()
1858 "Remove lazy highlight overlays from the current buffer."
1859 (while isearch-lazy-highlight-overlays
1860 (delete-overlay (car isearch-lazy-highlight-overlays))
1861 (setq isearch-lazy-highlight-overlays
1862 (cdr isearch-lazy-highlight-overlays))))
1863
1864 (defun isearch-lazy-highlight-new-loop ()
1865 "Cleanup any previous isearch-lazy-highlight loop and begin a new one.
1866 This happens when `isearch-update' is invoked (which can cause the
1867 search string to change)."
1868 (if (and isearch-lazy-highlight
1869 (not (equal isearch-string isearch-lazy-highlight-last-string)))
1870 ;; the search string did indeed change
1871 (progn
1872 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
1873 (if (and isearch-overlay
1874 (not (overlay-get isearch-overlay 'priority)))
1875 ;; make sure the isearch-overlay takes priority
1876 (overlay-put isearch-overlay 'priority 1))
1877 (setq isearch-lazy-highlight-start isearch-opoint
1878 isearch-lazy-highlight-end isearch-opoint
1879 isearch-lazy-highlight-last-string isearch-string
1880 isearch-lazy-highlight-wrapped nil)
1881 (setq isearch-lazy-highlight-timer
1882 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
1883 'isearch-lazy-highlight-update)))))
1884
1885 (defun isearch-lazy-highlight-search ()
1886 "Search ahead for the next or previous match, for lazy highlighting.
1887 Attempt to do the search exactly the way the pending isearch would."
1888 (let ((case-fold-search isearch-case-fold-search))
1889 (funcall (cond (isearch-word (if isearch-forward
1890 'word-search-forward
1891 'word-search-backward))
1892 (isearch-regexp (if isearch-forward
1893 're-search-forward
1894 're-search-backward))
1895 (t (if isearch-forward
1896 'search-forward
1897 'search-backward)))
1898 isearch-string
1899 (if isearch-forward
1900 (if isearch-lazy-highlight-wrapped
1901 isearch-lazy-highlight-start
1902 nil)
1903 (if isearch-lazy-highlight-wrapped
1904 isearch-lazy-highlight-end
1905 nil))
1906 t)))
1907
1908 (defun isearch-lazy-highlight-update ()
1909 "Find and highlight the next match in the lazy highlighting loop."
1910 (when (not isearch-invalid-regexp)
1911 (save-excursion
1912 (save-match-data
1913 (goto-char (if isearch-forward
1914 isearch-lazy-highlight-end
1915 isearch-lazy-highlight-start))
1916 (let ((found (isearch-lazy-highlight-search))) ;do search
1917 (if found
1918 ;; found the next match
1919 (let ((ov (make-overlay (match-beginning 0)
1920 (match-end 0))))
1921 (overlay-put ov 'face isearch-lazy-highlight-face)
1922 (overlay-put ov 'priority 0)
1923 (setq isearch-lazy-highlight-overlays
1924 (cons ov isearch-lazy-highlight-overlays))
1925 (setq isearch-lazy-highlight-timer
1926 (run-at-time isearch-lazy-highlight-interval nil
1927 'isearch-lazy-highlight-update))
1928 (if isearch-forward
1929 (setq isearch-lazy-highlight-end (point))
1930 (setq isearch-lazy-highlight-start (point))))
1931 ;; found no next match
1932 (when (not isearch-lazy-highlight-wrapped)
1933 ;; let's try wrapping around the end of the buffer
1934 (setq isearch-lazy-highlight-wrapped t)
1935 (setq isearch-lazy-highlight-timer
1936 (run-at-time isearch-lazy-highlight-interval nil
1937 'isearch-lazy-highlight-update))
1938 (if isearch-forward
1939 (setq isearch-lazy-highlight-end (point-min))
1940 (setq isearch-lazy-highlight-start (point-max))))))))))
1941
1942 ;;; isearch.el ends here