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