]> code.delx.au - gnu-emacs/blob - lisp/isearch.el
(isearch-mode-map): Bind C-M-w to isearch-del-char,
[gnu-emacs] / lisp / isearch.el
1 ;;; isearch.el --- incremental search minor mode
2
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 01, 2003, 2004
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: matching
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; Instructions
30
31 ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
32 ;; isearch-mode behaves modally and does not return until the search
33 ;; is completed. It uses a recursive-edit to behave this way.
34
35 ;; The key bindings active within isearch-mode are defined below in
36 ;; `isearch-mode-map' which is given bindings close to the default
37 ;; characters of the original isearch.el. With `isearch-mode',
38 ;; however, you can bind multi-character keys and it should be easier
39 ;; to add new commands. One bug though: keys with meta-prefix cannot
40 ;; be longer than two chars. Also see minibuffer-local-isearch-map
41 ;; for bindings active during `isearch-edit-string'.
42
43 ;; isearch-mode should work even if you switch windows with the mouse,
44 ;; in which case isearch-mode is terminated automatically before the
45 ;; switch.
46
47 ;; The search ring and completion commands automatically put you in
48 ;; the minibuffer to edit the string. This gives you a chance to
49 ;; modify the search string before executing the search. There are
50 ;; three commands to terminate the editing: C-s and C-r exit the
51 ;; minibuffer and search forward and reverse respectively, while C-m
52 ;; exits and does a nonincremental search.
53
54 ;; Exiting immediately from isearch uses isearch-edit-string instead
55 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
56 ;; The name of this option should probably be changed if we decide to
57 ;; keep the behavior. No point in forcing nonincremental search until
58 ;; the last possible moment.
59
60 ;; TODO
61 ;; - Integrate the emacs 19 generalized command history.
62 ;; - Think about incorporating query-replace.
63 ;; - Hooks and options for failed search.
64
65 ;;; Change Log:
66
67 ;; Changes before those recorded in ChangeLog:
68
69 ;; Revision 1.4 92/09/14 16:26:02 liberte
70 ;; Added prefix args to isearch-forward, etc. to switch between
71 ;; string and regular expression searching.
72 ;; Added some support for lemacs.
73 ;; Added general isearch-highlight option - but only for lemacs so far.
74 ;; Added support for frame switching in emacs 19.
75 ;; Added word search option to isearch-edit-string.
76 ;; Renamed isearch-quit to isearch-abort.
77 ;; Numerous changes to comments and doc strings.
78 ;;
79 ;; Revision 1.3 92/06/29 13:10:08 liberte
80 ;; Moved modal isearch-mode handling into isearch-mode.
81 ;; Got rid of buffer-local isearch variables.
82 ;; isearch-edit-string used by ring adjustments, completion, and
83 ;; nonincremental searching. C-s and C-r are additional exit commands.
84 ;; Renamed all regex to regexp.
85 ;; Got rid of found-start and found-point globals.
86 ;; Generalized handling of upper-case chars.
87
88 ;; Revision 1.2 92/05/27 11:33:57 liberte
89 ;; Emacs version 19 has a search ring, which is supported here.
90 ;; Other fixes found in the version 19 isearch are included here.
91 ;;
92 ;; Also see variables search-caps-disable-folding,
93 ;; search-nonincremental-instead, search-whitespace-regexp, and
94 ;; commands isearch-toggle-regexp, isearch-edit-string.
95 ;;
96 ;; semi-modal isearching is supported.
97
98 ;; Changes for 1.1
99 ;; 3/18/92 Fixed invalid-regexp.
100 ;; 3/18/92 Fixed yanking in regexps.
101
102 ;;; Code:
103
104 \f
105 ;; Some additional options and constants.
106
107 (defgroup isearch nil
108 "Incremental search minor mode."
109 :link '(emacs-commentary-link "isearch")
110 :link '(custom-manual "(emacs)Incremental Search")
111 :prefix "isearch-"
112 :prefix "search-"
113 :group 'matching)
114
115
116 (defcustom search-exit-option t
117 "*Non-nil means random control characters terminate incremental search."
118 :type 'boolean
119 :group 'isearch)
120
121 (defcustom search-slow-window-lines 1
122 "*Number of lines in slow search display windows.
123 These are the short windows used during incremental search on slow terminals.
124 Negative means put the slow search window at the top (normally it's at bottom)
125 and the value is minus the number of lines."
126 :type 'integer
127 :group 'isearch)
128
129 (defcustom search-slow-speed 1200
130 "*Highest terminal speed at which to use \"slow\" style incremental search.
131 This is the style where a one-line window is created to show the line
132 that the search has reached."
133 :type 'integer
134 :group 'isearch)
135
136 (defcustom search-upper-case 'not-yanks
137 "*If non-nil, upper case chars disable case fold searching.
138 That is, upper and lower case chars must match exactly.
139 This applies no matter where the chars come from, but does not
140 apply to chars in regexps that are prefixed with `\\'.
141 If this value is `not-yanks', yanked text is always downcased."
142 :type '(choice (const :tag "off" nil)
143 (const not-yanks)
144 (other :tag "on" t))
145 :group 'isearch)
146
147 (defcustom search-nonincremental-instead t
148 "*If non-nil, do a nonincremental search instead if exiting immediately.
149 Actually, `isearch-edit-string' is called to let you enter the search
150 string, and RET terminates editing and does a nonincremental search."
151 :type 'boolean
152 :group 'isearch)
153
154 (defcustom search-whitespace-regexp "\\(?:\\s-+\\)"
155 "*If non-nil, regular expression to match a sequence of whitespace chars.
156 This applies to regular expression incremental search.
157 You might want to use something like \"[ \\t\\r\\n]+\" instead.
158 In the Customization buffer, that is `[' followed by a space,
159 a tab, a carriage return (control-M), a newline, and `]+'."
160 :type 'regexp
161 :group 'isearch)
162
163 (defcustom search-highlight t
164 "*Non-nil means incremental search highlights the current match."
165 :type 'boolean
166 :group 'isearch)
167
168 (defcustom search-invisible 'open
169 "If t incremental search can match hidden text.
170 nil means don't match invisible text.
171 If the value is `open', if the text matched is made invisible by
172 an overlay having an `invisible' property and that overlay has a property
173 `isearch-open-invisible', then incremental search will show the contents.
174 \(This applies when using `outline.el' and `hideshow.el'.)
175 See also `reveal-mode' if you want overlays to automatically be opened
176 whenever point is in one of them."
177 :type '(choice (const :tag "Match hidden text" t)
178 (const :tag "Open overlays" open)
179 (const :tag "Don't match hidden text" nil))
180 :group 'isearch)
181
182 (defcustom isearch-hide-immediately t
183 "If non-nil, re-hide an invisible match right away.
184 This variable makes a difference when `search-invisible' is set to `open'.
185 It means that after search makes some invisible text visible
186 to show the match, it makes the text invisible again when the match moves.
187 Ordinarily the text becomes invisible again at the end of the search."
188 :type 'boolean
189 :group 'isearch)
190
191 (defcustom isearch-resume-enabled t
192 "*If non-nil, `isearch-resume' commands are added to the command history."
193 :type 'boolean
194 :group 'isearch)
195
196 (defvar isearch-mode-hook nil
197 "Function(s) to call after starting up an incremental search.")
198
199 (defvar isearch-mode-end-hook nil
200 "Function(s) to call after terminating an incremental search.")
201
202 ;; Search ring.
203
204 (defvar search-ring nil
205 "List of search string sequences.")
206 (defvar regexp-search-ring nil
207 "List of regular expression search string sequences.")
208
209 (defcustom search-ring-max 16
210 "*Maximum length of search ring before oldest elements are thrown away."
211 :type 'integer
212 :group 'isearch)
213 (defcustom regexp-search-ring-max 16
214 "*Maximum length of regexp search ring before oldest elements are thrown away."
215 :type 'integer
216 :group 'isearch)
217
218 (defvar search-ring-yank-pointer nil
219 "Index in `search-ring' of last string reused.
220 nil if none yet.")
221 (defvar regexp-search-ring-yank-pointer nil
222 "Index in `regexp-search-ring' of last string reused.
223 nil if none yet.")
224
225 (defcustom search-ring-update nil
226 "*Non-nil if advancing or retreating in the search ring should cause search.
227 Default value, nil, means edit the string instead."
228 :type 'boolean
229 :group 'isearch)
230
231 ;; Define isearch-mode keymap.
232
233 (defvar isearch-mode-map
234 (let* ((i 0)
235 (map (make-keymap)))
236 (or (vectorp (nth 1 map))
237 (char-table-p (nth 1 map))
238 (error "The initialization of isearch-mode-map must be updated"))
239 ;; Make all multibyte characters search for themselves.
240 (let ((l (generic-character-list))
241 (table (nth 1 map)))
242 (while l
243 (set-char-table-default table (car l) 'isearch-printing-char)
244 (setq l (cdr l))))
245 ;; Make function keys, etc, which aren't bound to a scrolling-function
246 ;; exit the search.
247 (define-key map [t] 'isearch-other-control-char)
248 ;; Control chars, by default, end isearch mode transparently.
249 ;; We need these explicit definitions because, in a dense keymap,
250 ;; the binding for t does not affect characters.
251 ;; We use a dense keymap to save space.
252 (while (< i ?\ )
253 (define-key map (make-string 1 i) 'isearch-other-control-char)
254 (setq i (1+ i)))
255
256 ;; Single-byte printing chars extend the search string by default.
257 (setq i ?\ )
258 (while (< i 256)
259 (define-key map (vector i) 'isearch-printing-char)
260 (setq i (1+ i)))
261
262 ;; To handle local bindings with meta char prefix keys, define
263 ;; another full keymap. This must be done for any other prefix
264 ;; keys as well, one full keymap per char of the prefix key. It
265 ;; would be simpler to disable the global keymap, and/or have a
266 ;; default local key binding for any key not otherwise bound.
267 (let ((meta-map (make-sparse-keymap)))
268 (define-key map (char-to-string meta-prefix-char) meta-map)
269 (define-key map [escape] meta-map))
270 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
271
272 ;; Several non-printing chars change the searching behavior.
273 (define-key map "\C-s" 'isearch-repeat-forward)
274 (define-key map "\C-r" 'isearch-repeat-backward)
275 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
276 ;; combinations can be used to repeat regexp isearches that can
277 ;; be used to start these searches.
278 (define-key map "\M-\C-s" 'isearch-repeat-forward)
279 (define-key map "\M-\C-r" 'isearch-repeat-backward)
280 (define-key map "\177" 'isearch-delete-char)
281 (define-key map "\C-g" 'isearch-abort)
282
283 ;; This assumes \e is the meta-prefix-char.
284 (or (= ?\e meta-prefix-char)
285 (error "Inconsistency in isearch.el"))
286 (define-key map "\e\e\e" 'isearch-cancel)
287 (define-key map [escape escape escape] 'isearch-cancel)
288
289 (define-key map "\C-q" 'isearch-quote-char)
290
291 (define-key map "\r" 'isearch-exit)
292 (define-key map "\C-j" 'isearch-printing-char)
293 (define-key map "\t" 'isearch-printing-char)
294 (define-key map " " 'isearch-whitespace-chars)
295 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
296
297 (define-key map "\C-w" 'isearch-yank-word-or-char)
298 (define-key map "\M-\C-w" 'isearch-del-char)
299 (define-key map "\M-\C-y" 'isearch-yank-char)
300 (define-key map "\C-y" 'isearch-yank-line)
301
302 ;; Define keys for regexp chars * ? |.
303 ;; Nothing special for + because it matches at least once.
304 (define-key map "*" 'isearch-*-char)
305 (define-key map "?" 'isearch-*-char)
306 (define-key map "{" 'isearch-{-char)
307 (define-key map "|" 'isearch-|-char)
308
309 ;; Turned off because I find I expect to get the global definition--rms.
310 ;; ;; Instead bind C-h to special help command for isearch-mode.
311 ;; (define-key map "\C-h" 'isearch-mode-help)
312
313 (define-key map "\M-n" 'isearch-ring-advance)
314 (define-key map "\M-p" 'isearch-ring-retreat)
315 (define-key map "\M-y" 'isearch-yank-kill)
316
317 (define-key map "\M-\t" 'isearch-complete)
318
319 ;; Pass frame events transparently so they won't exit the search.
320 ;; In particular, if we have more than one display open, then a
321 ;; switch-frame might be generated by someone typing at another keyboard.
322 (define-key map [switch-frame] nil)
323 (define-key map [delete-frame] nil)
324 (define-key map [iconify-frame] nil)
325 (define-key map [make-frame-visible] nil)
326 (define-key map [mouse-movement] nil)
327 ;; For searching multilingual text.
328 (define-key map "\C-\\" 'isearch-toggle-input-method)
329 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
330
331 ;; People expect to be able to paste with the mouse.
332 (define-key map [mouse-2] #'isearch-mouse-2)
333 (define-key map [down-mouse-2] nil)
334
335 ;; Some bindings you may want to put in your isearch-mode-hook.
336 ;; Suggest some alternates...
337 (define-key map "\M-c" 'isearch-toggle-case-fold)
338 (define-key map "\M-r" 'isearch-toggle-regexp)
339 (define-key map "\M-e" 'isearch-edit-string)
340
341 (define-key map (kbd "M-%") 'isearch-query-replace)
342 (define-key map (kbd "C-M-%") 'isearch-query-replace-regexp)
343
344 map)
345 "Keymap for `isearch-mode'.")
346
347 (defvar minibuffer-local-isearch-map
348 (let ((map (make-sparse-keymap)))
349 (set-keymap-parent map minibuffer-local-map)
350 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
351 (define-key map "\M-n" 'isearch-ring-advance-edit)
352 (define-key map [next] 'isearch-ring-advance-edit)
353 (define-key map [down] 'isearch-ring-advance-edit)
354 (define-key map "\M-p" 'isearch-ring-retreat-edit)
355 (define-key map [prior] 'isearch-ring-retreat-edit)
356 (define-key map [up] 'isearch-ring-retreat-edit)
357 (define-key map "\M-\t" 'isearch-complete-edit)
358 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
359 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
360 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
361 (define-key map [right] 'isearch-yank-char-in-minibuffer)
362 map)
363 "Keymap for editing isearch strings in the minibuffer.")
364
365 ;; Internal variables declared globally for byte-compiler.
366 ;; These are all set with setq while isearching
367 ;; and bound locally while editing the search string.
368
369 (defvar isearch-forward nil) ; Searching in the forward direction.
370 (defvar isearch-regexp nil) ; Searching for a regexp.
371 (defvar isearch-word nil) ; Searching for words.
372 (defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
373
374 (defvar isearch-cmds nil
375 "Stack of search status sets.
376 Each set is a list of the form:
377 (STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
378 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH)")
379
380 (defvar isearch-string "") ; The current search string.
381 (defvar isearch-message "") ; text-char-description version of isearch-string
382
383 (defvar isearch-success t) ; Searching is currently successful.
384 (defvar isearch-invalid-regexp nil) ; Regexp not well formed.
385 (defvar isearch-within-brackets nil) ; Regexp has unclosed [.
386 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
387 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
388 (defvar isearch-barrier 0)
389 (defvar isearch-just-started nil)
390 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
391
392 ; case-fold-search while searching.
393 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
394 ; case in the search string is ignored.
395 (defvar isearch-case-fold-search nil)
396
397 (defvar isearch-last-case-fold-search nil)
398
399 ;; Used to save default value while isearch is active
400 (defvar isearch-original-minibuffer-message-timeout nil)
401
402 (defvar isearch-adjusted nil)
403 (defvar isearch-slow-terminal-mode nil)
404 ;; If t, using a small window.
405 (defvar isearch-small-window nil)
406 (defvar isearch-opoint 0)
407 ;; The window configuration active at the beginning of the search.
408 (defvar isearch-window-configuration nil)
409
410 ;; Flag to indicate a yank occurred, so don't move the cursor.
411 (defvar isearch-yank-flag nil)
412
413 ;; A function to be called after each input character is processed.
414 ;; (It is not called after characters that exit the search.)
415 ;; It is only set from an optional argument to `isearch-mode'.
416 (defvar isearch-op-fun nil)
417
418 ;; Is isearch-mode in a recursive edit for modal searching.
419 (defvar isearch-recursive-edit nil)
420
421 ;; Should isearch be terminated after doing one search?
422 (defvar isearch-nonincremental nil)
423
424 ;; New value of isearch-forward after isearch-edit-string.
425 (defvar isearch-new-forward nil)
426
427 ;; Accumulate here the overlays opened during searching.
428 (defvar isearch-opened-overlays nil)
429
430 ;; The value of input-method-function when isearch is invoked.
431 (defvar isearch-input-method-function nil)
432
433 ;; A flag to tell if input-method-function is locally bound when
434 ;; isearch is invoked.
435 (defvar isearch-input-method-local-p nil)
436
437 ;; Minor-mode-alist changes - kind of redundant with the
438 ;; echo area, but if isearching in multiple windows, it can be useful.
439
440 (or (assq 'isearch-mode minor-mode-alist)
441 (nconc minor-mode-alist
442 (list '(isearch-mode isearch-mode))))
443
444 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
445 (make-variable-buffer-local 'isearch-mode)
446
447 (define-key global-map "\C-s" 'isearch-forward)
448 (define-key esc-map "\C-s" 'isearch-forward-regexp)
449 (define-key global-map "\C-r" 'isearch-backward)
450 (define-key esc-map "\C-r" 'isearch-backward-regexp)
451
452 ;; Entry points to isearch-mode.
453
454 (defun isearch-forward (&optional regexp-p no-recursive-edit)
455 "\
456 Do incremental search forward.
457 With a prefix argument, do an incremental regular expression search instead.
458 \\<isearch-mode-map>
459 As you type characters, they add to the search string and are found.
460 The following non-printing keys are bound in `isearch-mode-map'.
461
462 Type \\[isearch-delete-char] to cancel last input item from end of search string.
463 Type \\[isearch-exit] to exit, leaving point at location found.
464 Type LFD (C-j) to match end of line.
465 Type \\[isearch-repeat-forward] to search again forward,\
466 \\[isearch-repeat-backward] to search again backward.
467 Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
468 string and search for it.
469 Type \\[isearch-del-char] to delete character from end of search string.
470 Type \\[isearch-yank-char] to yank char from buffer onto end of search\
471 string and search for it.
472 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
473 and search for it.
474 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
475 and search for it.
476 Type \\[isearch-quote-char] to quote control character to search for it.
477 \\[isearch-abort] while searching or when search has failed cancels input\
478 back to what has
479 been found successfully.
480 \\[isearch-abort] when search is successful aborts and moves point to\
481 starting point.
482
483 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
484 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
485 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
486
487 Also supported is a search ring of the previous 16 search strings.
488 Type \\[isearch-ring-advance] to search for the next item in the search ring.
489 Type \\[isearch-ring-retreat] to search for the previous item in the search\
490 ring.
491 Type \\[isearch-complete] to complete the search string using the search ring.
492
493 If an input method is turned on in the current buffer, that input
494 method is also active while you are typing characters to search. To
495 toggle the input method, type \\[isearch-toggle-input-method]. It
496 also toggles the input method in the current buffer.
497
498 To use a different input method for searching, type
499 \\[isearch-toggle-specified-input-method], and specify an input method
500 you want to use.
501
502 The above keys, bound in `isearch-mode-map', are often controlled by
503 options; do \\[apropos] on search-.* to find them.
504 Other control and meta characters terminate the search
505 and are then executed normally (depending on `search-exit-option').
506 Likewise for function keys and mouse button events.
507
508 If this function is called non-interactively, it does not return to
509 the calling function until the search is done."
510
511 (interactive "P\np")
512 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
513
514 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
515 "\
516 Do incremental search forward for regular expression.
517 With a prefix argument, do a regular string search instead.
518 Like ordinary incremental search except that your input
519 is treated as a regexp. See \\[isearch-forward] for more info."
520 (interactive "P\np")
521 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
522
523 (defun isearch-backward (&optional regexp-p no-recursive-edit)
524 "\
525 Do incremental search backward.
526 With a prefix argument, do a regular expression search instead.
527 See \\[isearch-forward] for more information."
528 (interactive "P\np")
529 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
530
531 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
532 "\
533 Do incremental search backward for regular expression.
534 With a prefix argument, do a regular string search instead.
535 Like ordinary incremental search except that your input
536 is treated as a regexp. See \\[isearch-forward] for more info."
537 (interactive "P\np")
538 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
539
540
541 (defun isearch-mode-help ()
542 (interactive)
543 (describe-function 'isearch-forward)
544 (isearch-update))
545
546 \f
547 ;; isearch-mode only sets up incremental search for the minor mode.
548 ;; All the work is done by the isearch-mode commands.
549
550 ;; Not used yet:
551 ;;(defvar isearch-commands '(isearch-forward isearch-backward
552 ;; isearch-forward-regexp isearch-backward-regexp)
553 ;; "List of commands for which isearch-mode does not recursive-edit.")
554
555
556 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
557 "Start isearch minor mode. Called by `isearch-forward', etc.
558
559 \\{isearch-mode-map}"
560
561 ;; Initialize global vars.
562 (setq isearch-forward forward
563 isearch-regexp regexp
564 isearch-word word-p
565 isearch-op-fun op-fun
566 isearch-last-case-fold-search isearch-case-fold-search
567 isearch-case-fold-search case-fold-search
568 isearch-string ""
569 isearch-message ""
570 isearch-cmds nil
571 isearch-success t
572 isearch-wrapped nil
573 isearch-barrier (point)
574 isearch-adjusted nil
575 isearch-yank-flag nil
576 isearch-invalid-regexp nil
577 isearch-within-brackets nil
578 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
579 (> (window-height)
580 (* 4
581 (abs search-slow-window-lines))))
582 isearch-other-end nil
583 isearch-small-window nil
584 isearch-just-started t
585 isearch-start-hscroll (window-hscroll)
586
587 isearch-opoint (point)
588 search-ring-yank-pointer nil
589 isearch-opened-overlays nil
590 isearch-input-method-function input-method-function
591 isearch-input-method-local-p (local-variable-p 'input-method-function)
592 regexp-search-ring-yank-pointer nil
593
594 ;; Save the original value of `minibuffer-message-timeout', and
595 ;; set it to nil so that isearch's messages don't get timed out.
596 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
597 minibuffer-message-timeout nil)
598
599 ;; We must bypass input method while reading key. When a user type
600 ;; printable character, appropriate input method is turned on in
601 ;; minibuffer to read multibyte characters.
602 (or isearch-input-method-local-p
603 (make-local-variable 'input-method-function))
604 (setq input-method-function nil)
605
606 (looking-at "")
607 (setq isearch-window-configuration
608 (if isearch-slow-terminal-mode (current-window-configuration) nil))
609
610 ;; Maybe make minibuffer frame visible and/or raise it.
611 (let ((frame (window-frame (minibuffer-window))))
612 (unless (memq (frame-live-p frame) '(nil t))
613 (unless (frame-visible-p frame)
614 (make-frame-visible frame))
615 (if minibuffer-auto-raise
616 (raise-frame frame))))
617
618 (setq isearch-mode " Isearch") ;; forward? regexp?
619 (force-mode-line-update)
620
621 (isearch-push-state)
622
623 (setq overriding-terminal-local-map isearch-mode-map)
624 (isearch-update)
625 (run-hooks 'isearch-mode-hook)
626
627 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
628 (add-hook 'kbd-macro-termination-hook 'isearch-done)
629
630 ;; isearch-mode can be made modal (in the sense of not returning to
631 ;; the calling function until searching is completed) by entering
632 ;; a recursive-edit and exiting it when done isearching.
633 (if recursive-edit
634 (let ((isearch-recursive-edit t))
635 (recursive-edit)))
636 isearch-success)
637
638
639 ;; Some high level utilities. Others below.
640
641 (defun isearch-update ()
642 ;; Called after each command to update the display.
643 (if (and (null unread-command-events)
644 (null executing-kbd-macro))
645 (progn
646 (if (not (input-pending-p))
647 (isearch-message))
648 (if (and isearch-slow-terminal-mode
649 (not (or isearch-small-window
650 (pos-visible-in-window-p))))
651 (let ((found-point (point)))
652 (setq isearch-small-window t)
653 (move-to-window-line 0)
654 (let ((window-min-height 1))
655 (split-window nil (if (< search-slow-window-lines 0)
656 (1+ (- search-slow-window-lines))
657 (- (window-height)
658 (1+ search-slow-window-lines)))))
659 (if (< search-slow-window-lines 0)
660 (progn (vertical-motion (- 1 search-slow-window-lines))
661 (set-window-start (next-window) (point))
662 (set-window-hscroll (next-window)
663 (window-hscroll))
664 (set-window-hscroll (selected-window) 0))
665 (other-window 1))
666 (goto-char found-point))
667 ;; Keep same hscrolling as at the start of the search when possible
668 (let ((current-scroll (window-hscroll)))
669 (set-window-hscroll (selected-window) isearch-start-hscroll)
670 (unless (pos-visible-in-window-p)
671 (set-window-hscroll (selected-window) current-scroll))))
672 (if isearch-other-end
673 (if (< isearch-other-end (point)) ; isearch-forward?
674 (isearch-highlight isearch-other-end (point))
675 (isearch-highlight (point) isearch-other-end))
676 (isearch-dehighlight nil))
677 ))
678 (setq ;; quit-flag nil not for isearch-mode
679 isearch-adjusted nil
680 isearch-yank-flag nil)
681 (isearch-lazy-highlight-new-loop)
682 ;; We must prevent the point moving to the end of composition when a
683 ;; part of the composition has just been searched.
684 (setq disable-point-adjustment t))
685
686 (defun isearch-done (&optional nopush edit)
687 (if isearch-resume-enabled
688 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
689 ,isearch-word ,isearch-forward
690 ,isearch-message
691 ',isearch-case-fold-search)))
692 (unless (equal (car command-history) command)
693 (setq command-history (cons command command-history)))))
694
695 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
696 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
697 (setq isearch-lazy-highlight-start nil)
698
699 ;; Called by all commands that terminate isearch-mode.
700 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
701 (setq overriding-terminal-local-map nil)
702 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
703 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
704 (isearch-dehighlight t)
705 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
706 (let ((found-start (window-start (selected-window)))
707 (found-point (point)))
708 (if isearch-window-configuration
709 (set-window-configuration isearch-window-configuration))
710
711 (if isearch-small-window
712 (goto-char found-point)
713 ;; Exiting the save-window-excursion clobbers window-start; restore it.
714 (set-window-start (selected-window) found-start t))
715
716 ;; If there was movement, mark the starting position.
717 ;; Maybe should test difference between and set mark iff > threshold.
718 (if (/= (point) isearch-opoint)
719 (or (and transient-mark-mode mark-active)
720 (progn
721 (push-mark isearch-opoint t)
722 (or executing-kbd-macro (> (minibuffer-depth) 0)
723 (message "Mark saved where search started"))))))
724
725 (setq isearch-mode nil)
726 (if isearch-input-method-local-p
727 (setq input-method-function isearch-input-method-function)
728 (kill-local-variable 'input-method-function))
729
730 (force-mode-line-update)
731
732 ;; If we ended in the middle of some intangible text,
733 ;; move to the further end of that intangible text.
734 (let ((after (if (eobp) nil
735 (get-text-property (point) 'intangible)))
736 (before (if (bobp) nil
737 (get-text-property (1- (point)) 'intangible))))
738 (when (and before after (eq before after))
739 (if isearch-forward
740 (goto-char (next-single-property-change (point) 'intangible))
741 (goto-char (previous-single-property-change (point) 'intangible)))))
742
743 (if (and (> (length isearch-string) 0) (not nopush))
744 ;; Update the ring data.
745 (isearch-update-ring isearch-string isearch-regexp))
746
747 (run-hooks 'isearch-mode-end-hook)
748 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
749
750 (defun isearch-update-ring (string &optional regexp)
751 "Add STRING to the beginning of the search ring.
752 REGEXP says which ring to use."
753 (if regexp
754 (if (or (null regexp-search-ring)
755 (not (string= string (car regexp-search-ring))))
756 (progn
757 (push string regexp-search-ring)
758 (if (> (length regexp-search-ring) regexp-search-ring-max)
759 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
760 nil))))
761 (if (or (null search-ring)
762 (not (string= string (car search-ring))))
763 (progn
764 (push string search-ring)
765 (if (> (length search-ring) search-ring-max)
766 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
767
768 ;; Switching buffers should first terminate isearch-mode.
769 ;; ;; For Emacs 19, the frame switch event is handled.
770 ;; (defun isearch-switch-frame-handler ()
771 ;; (interactive) ;; Is this necessary?
772 ;; ;; First terminate isearch-mode.
773 ;; (isearch-done)
774 ;; (isearch-clean-overlays)
775 ;; (handle-switch-frame (car (cdr last-command-char))))
776
777 \f
778 ;; Commands active while inside of the isearch minor mode.
779
780 (defun isearch-exit ()
781 "Exit search normally.
782 However, if this is the first command after starting incremental
783 search and `search-nonincremental-instead' is non-nil, do a
784 nonincremental search instead via `isearch-edit-string'."
785 (interactive)
786 (if (and search-nonincremental-instead
787 (= 0 (length isearch-string)))
788 (let ((isearch-nonincremental t))
789 (isearch-edit-string)))
790 (isearch-done)
791 (isearch-clean-overlays))
792
793
794 (defun isearch-edit-string ()
795 "Edit the search string in the minibuffer.
796 The following additional command keys are active while editing.
797 \\<minibuffer-local-isearch-map>
798 \\[exit-minibuffer] to resume incremental searching with the edited string.
799 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
800 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
801 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
802 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
803 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
804 \\[isearch-complete-edit] to complete the search string using the search ring.
805 \\<isearch-mode-map>
806 If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
807
808 ;; This code is very hairy for several reasons, explained in the code.
809 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
810 ;; If there were a way to catch any change of buffer from the minibuffer,
811 ;; this could be simplified greatly.
812 ;; Editing doesn't back up the search point. Should it?
813 (interactive)
814 (condition-case err
815 (progn
816 (let ((isearch-nonincremental isearch-nonincremental)
817
818 ;; Locally bind all isearch global variables to protect them
819 ;; from recursive isearching.
820 ;; isearch-string -message and -forward are not bound
821 ;; so they may be changed. Instead, save the values.
822 (isearch-new-string isearch-string)
823 (isearch-new-message isearch-message)
824 (isearch-new-forward isearch-forward)
825 (isearch-new-word isearch-word)
826
827 (isearch-regexp isearch-regexp)
828 (isearch-op-fun isearch-op-fun)
829 (isearch-cmds isearch-cmds)
830 (isearch-success isearch-success)
831 (isearch-wrapped isearch-wrapped)
832 (isearch-barrier isearch-barrier)
833 (isearch-adjusted isearch-adjusted)
834 (isearch-yank-flag isearch-yank-flag)
835 (isearch-invalid-regexp isearch-invalid-regexp)
836 (isearch-within-brackets isearch-within-brackets)
837 ;;; Don't bind this. We want isearch-search, below, to set it.
838 ;;; And the old value won't matter after that.
839 ;;; (isearch-other-end isearch-other-end)
840 ;;; Perhaps some of these other variables should be bound for a
841 ;;; shorter period, ending before the next isearch-search.
842 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
843 (isearch-opoint isearch-opoint)
844 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
845 (isearch-small-window isearch-small-window)
846 (isearch-recursive-edit isearch-recursive-edit)
847 ;; Save current configuration so we can restore it here.
848 (isearch-window-configuration (current-window-configuration))
849
850 ;; Temporarily restore `minibuffer-message-timeout'.
851 (minibuffer-message-timeout
852 isearch-original-minibuffer-message-timeout)
853 (isearch-original-minibuffer-message-timeout
854 isearch-original-minibuffer-message-timeout)
855 )
856
857 ;; Actually terminate isearching until editing is done.
858 ;; This is so that the user can do anything without failure,
859 ;; like switch buffers and start another isearch, and return.
860 (condition-case err
861 (isearch-done t t)
862 (exit nil)) ; was recursive editing
863
864 (isearch-message) ;; for read-char
865 (unwind-protect
866 (let* (;; Why does following read-char echo?
867 ;;(echo-keystrokes 0) ;; not needed with above message
868 (e (let ((cursor-in-echo-area t))
869 (read-event)))
870 ;; Binding minibuffer-history-symbol to nil is a work-around
871 ;; for some incompatibility with gmhist.
872 (minibuffer-history-symbol)
873 (message-log-max nil))
874 ;; If the first character the user types when we prompt them
875 ;; for a string is the yank-word character, then go into
876 ;; word-search mode. Otherwise unread that character and
877 ;; read a key the normal way.
878 ;; Word search does not apply (yet) to regexp searches,
879 ;; no check is made here.
880 (message (isearch-message-prefix nil nil t))
881 (if (memq (lookup-key isearch-mode-map (vector e))
882 '(isearch-yank-word
883 isearch-yank-word-or-char))
884 (setq isearch-word t;; so message-prefix is right
885 isearch-new-word t)
886 (cancel-kbd-macro-events)
887 (isearch-unread e))
888 (setq cursor-in-echo-area nil)
889 (setq isearch-new-string
890 (let (junk-ring)
891 (read-from-minibuffer
892 (isearch-message-prefix nil nil isearch-nonincremental)
893 isearch-string
894 minibuffer-local-isearch-map nil
895 'junk-ring))
896 isearch-new-message
897 (mapconcat 'isearch-text-char-description
898 isearch-new-string "")))
899 ;; Always resume isearching by restarting it.
900 (isearch-mode isearch-forward
901 isearch-regexp
902 isearch-op-fun
903 nil
904 isearch-word)
905
906 ;; Copy new local values to isearch globals
907 (setq isearch-string isearch-new-string
908 isearch-message isearch-new-message
909 isearch-forward isearch-new-forward
910 isearch-word isearch-new-word))
911
912 ;; Empty isearch-string means use default.
913 (if (= 0 (length isearch-string))
914 (setq isearch-string (or (car (if isearch-regexp
915 regexp-search-ring
916 search-ring))
917 "")
918
919 isearch-message
920 (mapconcat 'isearch-text-char-description
921 isearch-string ""))
922 ;; This used to set the last search string,
923 ;; but I think it is not right to do that here.
924 ;; Only the string actually used should be saved.
925 ))
926
927 ;; Push the state as of before this C-s.
928 (isearch-push-state)
929
930 ;; Reinvoke the pending search.
931 (isearch-search)
932 (isearch-update)
933 (if isearch-nonincremental
934 (progn
935 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
936 (isearch-done))))
937
938 (quit ; handle abort-recursive-edit
939 (isearch-abort) ;; outside of let to restore outside global values
940 )))
941
942 (defun isearch-nonincremental-exit-minibuffer ()
943 (interactive)
944 (setq isearch-nonincremental t)
945 (exit-minibuffer))
946
947 (defun isearch-forward-exit-minibuffer ()
948 (interactive)
949 (setq isearch-new-forward t)
950 (exit-minibuffer))
951
952 (defun isearch-reverse-exit-minibuffer ()
953 (interactive)
954 (setq isearch-new-forward nil)
955 (exit-minibuffer))
956
957 (defun isearch-cancel ()
958 "Terminate the search and go back to the starting point."
959 (interactive)
960 (goto-char isearch-opoint)
961 (isearch-done t)
962 (isearch-clean-overlays)
963 (signal 'quit nil)) ; and pass on quit signal
964
965 (defun isearch-abort ()
966 "Abort incremental search mode if searching is successful, signaling quit.
967 Otherwise, revert to previous successful search and continue searching.
968 Use `isearch-exit' to quit without signaling."
969 (interactive)
970 ;; (ding) signal instead below, if quitting
971 (discard-input)
972 (if isearch-success
973 ;; If search is successful, move back to starting point
974 ;; and really do quit.
975 (progn (goto-char isearch-opoint)
976 (setq isearch-success nil)
977 (isearch-done t) ; exit isearch
978 (isearch-clean-overlays)
979 (signal 'quit nil)) ; and pass on quit signal
980 ;; If search is failing, or has an incomplete regexp,
981 ;; rub out until it is once more successful.
982 (while (or (not isearch-success) isearch-invalid-regexp)
983 (isearch-pop-state))
984 (isearch-update)))
985
986 (defun isearch-repeat (direction)
987 ;; Utility for isearch-repeat-forward and -backward.
988 (if (eq isearch-forward (eq direction 'forward))
989 ;; C-s in forward or C-r in reverse.
990 (if (equal isearch-string "")
991 ;; If search string is empty, use last one.
992 (setq isearch-string
993 (or (if isearch-regexp
994 (car regexp-search-ring)
995 (car search-ring))
996 (error "No previous search string"))
997 isearch-message
998 (mapconcat 'isearch-text-char-description
999 isearch-string "")
1000 isearch-case-fold-search isearch-last-case-fold-search)
1001 ;; If already have what to search for, repeat it.
1002 (or isearch-success
1003 (progn
1004 (goto-char (if isearch-forward (point-min) (point-max)))
1005 (setq isearch-wrapped t))))
1006 ;; C-s in reverse or C-r in forward, change direction.
1007 (setq isearch-forward (not isearch-forward)))
1008
1009 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1010
1011 (if (equal isearch-string "")
1012 (setq isearch-success t)
1013 (if (and isearch-success
1014 (equal (point) isearch-other-end)
1015 (not isearch-just-started))
1016 ;; If repeating a search that found
1017 ;; an empty string, ensure we advance.
1018 (if (if isearch-forward (eobp) (bobp))
1019 ;; If there's nowhere to advance to, fail (and wrap next time).
1020 (progn
1021 (setq isearch-success nil)
1022 (ding))
1023 (forward-char (if isearch-forward 1 -1))
1024 (isearch-search))
1025 (isearch-search)))
1026
1027 (isearch-push-state)
1028 (isearch-update))
1029
1030 (defun isearch-repeat-forward ()
1031 "Repeat incremental search forwards."
1032 (interactive)
1033 (isearch-repeat 'forward))
1034
1035 (defun isearch-repeat-backward ()
1036 "Repeat incremental search backwards."
1037 (interactive)
1038 (isearch-repeat 'backward))
1039
1040 (defun isearch-toggle-regexp ()
1041 "Toggle regexp searching on or off."
1042 ;; The status stack is left unchanged.
1043 (interactive)
1044 (setq isearch-regexp (not isearch-regexp))
1045 (if isearch-regexp (setq isearch-word nil))
1046 (isearch-update))
1047
1048 (defun isearch-toggle-case-fold ()
1049 "Toggle case folding in searching on or off."
1050 (interactive)
1051 (setq isearch-case-fold-search
1052 (if isearch-case-fold-search nil 'yes))
1053 (let ((message-log-max nil))
1054 (message "%s%s [case %ssensitive]"
1055 (isearch-message-prefix nil nil isearch-nonincremental)
1056 isearch-message
1057 (if isearch-case-fold-search "in" "")))
1058 (setq isearch-adjusted t)
1059 (sit-for 1)
1060 (isearch-update))
1061
1062 (defun isearch-query-replace ()
1063 "Start query-replace with string to replace from last search string."
1064 (interactive)
1065 (let ((query-replace-interactive 'initial)
1066 (case-fold-search isearch-case-fold-search))
1067 ;; Put search string into the right ring
1068 (setq isearch-regexp nil)
1069 (isearch-done)
1070 (isearch-clean-overlays)
1071 (and isearch-forward isearch-other-end (goto-char isearch-other-end))
1072 (call-interactively 'query-replace)))
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 'initial)
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