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