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