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