1 ;;; isearch.el --- incremental search minor mode
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 ;; 2000, 2001, 2003, 2004 Free Software Foundation, Inc.
6 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
10 ;; This file is part of GNU Emacs.
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)
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.
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.
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.
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'.
43 ;; isearch-mode should work even if you switch windows with the mouse,
44 ;; in which case isearch-mode is terminated automatically before the
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.
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.
61 ;; - Integrate the emacs 19 generalized command history.
62 ;; - Hooks and options for failed search.
66 ;; Changes before those recorded in ChangeLog:
68 ;; Revision 1.4 92/09/14 16:26:02 liberte
69 ;; Added prefix args to isearch-forward, etc. to switch between
70 ;; string and regular expression searching.
71 ;; Added some support for lemacs.
72 ;; Added general isearch-highlight option - but only for lemacs so far.
73 ;; Added support for frame switching in emacs 19.
74 ;; Added word search option to isearch-edit-string.
75 ;; Renamed isearch-quit to isearch-abort.
76 ;; Numerous changes to comments and doc strings.
78 ;; Revision 1.3 92/06/29 13:10:08 liberte
79 ;; Moved modal isearch-mode handling into isearch-mode.
80 ;; Got rid of buffer-local isearch variables.
81 ;; isearch-edit-string used by ring adjustments, completion, and
82 ;; nonincremental searching. C-s and C-r are additional exit commands.
83 ;; Renamed all regex to regexp.
84 ;; Got rid of found-start and found-point globals.
85 ;; Generalized handling of upper-case chars.
87 ;; Revision 1.2 92/05/27 11:33:57 liberte
88 ;; Emacs version 19 has a search ring, which is supported here.
89 ;; Other fixes found in the version 19 isearch are included here.
91 ;; Also see variables search-caps-disable-folding,
92 ;; search-nonincremental-instead, search-whitespace-regexp, and
93 ;; commands isearch-toggle-regexp, isearch-edit-string.
95 ;; semi-modal isearching is supported.
98 ;; 3/18/92 Fixed invalid-regexp.
99 ;; 3/18/92 Fixed yanking in regexps.
104 ;; Some additional options and constants.
106 (defgroup isearch nil
107 "Incremental search minor mode."
108 :link '(emacs-commentary-link "isearch")
109 :link '(custom-manual "(emacs)Incremental Search")
115 (defcustom search-exit-option t
116 "*Non-nil means random control characters terminate incremental search."
120 (defcustom search-slow-window-lines 1
121 "*Number of lines in slow search display windows.
122 These are the short windows used during incremental search on slow terminals.
123 Negative means put the slow search window at the top (normally it's at bottom)
124 and the value is minus the number of lines."
128 (defcustom search-slow-speed 1200
129 "*Highest terminal speed at which to use \"slow\" style incremental search.
130 This is the style where a one-line window is created to show the line
131 that the search has reached."
135 (defcustom search-upper-case 'not-yanks
136 "*If non-nil, upper case chars disable case fold searching.
137 That is, upper and lower case chars must match exactly.
138 This applies no matter where the chars come from, but does not
139 apply to chars in regexps that are prefixed with `\\'.
140 If this value is `not-yanks', yanked text is always downcased."
141 :type '(choice (const :tag "off" nil)
146 (defcustom search-nonincremental-instead t
147 "*If non-nil, do a nonincremental search instead if exiting immediately.
148 Actually, `isearch-edit-string' is called to let you enter the search
149 string, and RET terminates editing and does a nonincremental search."
153 (defcustom search-whitespace-regexp "\\(?:\\s-+\\)"
154 "*If non-nil, regular expression to match a sequence of whitespace chars.
155 This applies to regular expression incremental search.
156 You might want to use something like \"\\\\(?:[ \\t\\r\\n]+\\\\)\" instead.
157 In the Customization buffer, that is `\\(?:[' followed by a space,
158 a tab, a carriage return (control-M), a newline, and `]+\\)'."
162 (defcustom search-highlight t
163 "*Non-nil means incremental search highlights the current match."
167 (defcustom search-invisible 'open
168 "If t incremental search can match hidden text.
169 nil means don't match invisible text.
170 If the value is `open', if the text matched is made invisible by
171 an overlay having an `invisible' property and that overlay has a property
172 `isearch-open-invisible', then incremental search will show the contents.
173 \(This applies when using `outline.el' and `hideshow.el'.)
174 See also `reveal-mode' if you want overlays to automatically be opened
175 whenever point is in one of them."
176 :type '(choice (const :tag "Match hidden text" t)
177 (const :tag "Open overlays" open)
178 (const :tag "Don't match hidden text" nil))
181 (defcustom isearch-hide-immediately t
182 "If non-nil, re-hide an invisible match right away.
183 This variable makes a difference when `search-invisible' is set to `open'.
184 It means that after search makes some invisible text visible
185 to show the match, it makes the text invisible again when the match moves.
186 Ordinarily the text becomes invisible again at the end of the search."
190 (defcustom isearch-resume-enabled t
191 "*If non-nil, `isearch-resume' commands are added to the command history."
195 (defvar isearch-mode-hook nil
196 "Function(s) to call after starting up an incremental search.")
198 (defvar isearch-mode-end-hook nil
199 "Function(s) to call after terminating an incremental search.")
203 (defvar search-ring nil
204 "List of search string sequences.")
205 (defvar regexp-search-ring nil
206 "List of regular expression search string sequences.")
208 (defcustom search-ring-max 16
209 "*Maximum length of search ring before oldest elements are thrown away."
212 (defcustom regexp-search-ring-max 16
213 "*Maximum length of regexp search ring before oldest elements are thrown away."
217 (defvar search-ring-yank-pointer nil
218 "Index in `search-ring' of last string reused.
220 (defvar regexp-search-ring-yank-pointer nil
221 "Index in `regexp-search-ring' of last string reused.
224 (defcustom search-ring-update nil
225 "*Non-nil if advancing or retreating in the search ring should cause search.
226 Default value, nil, means edit the string instead."
230 ;; Define isearch-mode keymap.
232 (defvar isearch-mode-map
235 (or (vectorp (nth 1 map))
236 (char-table-p (nth 1 map))
237 (error "The initialization of isearch-mode-map must be updated"))
238 ;; Make all multibyte characters search for themselves.
239 (let ((l (generic-character-list))
242 (set-char-table-default table (car l) 'isearch-printing-char)
244 ;; Make function keys, etc, which aren't bound to a scrolling-function
246 (define-key map [t] 'isearch-other-control-char)
247 ;; Control chars, by default, end isearch mode transparently.
248 ;; We need these explicit definitions because, in a dense keymap,
249 ;; the binding for t does not affect characters.
250 ;; We use a dense keymap to save space.
252 (define-key map (make-string 1 i) 'isearch-other-control-char)
255 ;; Single-byte printing chars extend the search string by default.
258 (define-key map (vector i) 'isearch-printing-char)
261 ;; To handle local bindings with meta char prefix keys, define
262 ;; another full keymap. This must be done for any other prefix
263 ;; keys as well, one full keymap per char of the prefix key. It
264 ;; would be simpler to disable the global keymap, and/or have a
265 ;; default local key binding for any key not otherwise bound.
266 (let ((meta-map (make-sparse-keymap)))
267 (define-key map (char-to-string meta-prefix-char) meta-map)
268 (define-key map [escape] meta-map))
269 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
271 ;; Several non-printing chars change the searching behavior.
272 (define-key map "\C-s" 'isearch-repeat-forward)
273 (define-key map "\C-r" 'isearch-repeat-backward)
274 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
275 ;; combinations can be used to repeat regexp isearches that can
276 ;; be used to start these searches.
277 (define-key map "\M-\C-s" 'isearch-repeat-forward)
278 (define-key map "\M-\C-r" 'isearch-repeat-backward)
279 (define-key map "\177" 'isearch-delete-char)
280 (define-key map "\C-g" 'isearch-abort)
282 ;; This assumes \e is the meta-prefix-char.
283 (or (= ?\e meta-prefix-char)
284 (error "Inconsistency in isearch.el"))
285 (define-key map "\e\e\e" 'isearch-cancel)
286 (define-key map [escape escape escape] 'isearch-cancel)
288 (define-key map "\C-q" 'isearch-quote-char)
290 (define-key map "\r" 'isearch-exit)
291 (define-key map "\C-j" 'isearch-printing-char)
292 (define-key map "\t" 'isearch-printing-char)
293 (define-key map " " 'isearch-whitespace-chars)
294 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
296 (define-key map "\C-w" 'isearch-yank-word-or-char)
297 (define-key map "\M-\C-w" 'isearch-del-char)
298 (define-key map "\M-\C-y" 'isearch-yank-char)
299 (define-key map "\C-y" 'isearch-yank-line)
301 ;; Define keys for regexp chars * ? } |.
302 ;; Nothing special for + because it matches at least once.
303 (define-key map "*" 'isearch-*-char)
304 (define-key map "?" 'isearch-*-char)
305 (define-key map "}" 'isearch-}-char)
306 (define-key map "|" 'isearch-|-char)
308 ;; Turned off because I find I expect to get the global definition--rms.
309 ;; ;; Instead bind C-h to special help command for isearch-mode.
310 ;; (define-key map "\C-h" 'isearch-mode-help)
312 (define-key map "\M-n" 'isearch-ring-advance)
313 (define-key map "\M-p" 'isearch-ring-retreat)
314 (define-key map "\M-y" 'isearch-yank-kill)
316 (define-key map "\M-\t" 'isearch-complete)
318 ;; Pass frame events transparently so they won't exit the search.
319 ;; In particular, if we have more than one display open, then a
320 ;; switch-frame might be generated by someone typing at another keyboard.
321 (define-key map [switch-frame] nil)
322 (define-key map [delete-frame] nil)
323 (define-key map [iconify-frame] nil)
324 (define-key map [make-frame-visible] nil)
325 (define-key map [mouse-movement] nil)
326 ;; For searching multilingual text.
327 (define-key map "\C-\\" 'isearch-toggle-input-method)
328 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
330 ;; People expect to be able to paste with the mouse.
331 (define-key map [mouse-2] #'isearch-mouse-2)
332 (define-key map [down-mouse-2] nil)
334 ;; Some bindings you may want to put in your isearch-mode-hook.
335 ;; Suggest some alternates...
336 (define-key map "\M-c" 'isearch-toggle-case-fold)
337 (define-key map "\M-r" 'isearch-toggle-regexp)
338 (define-key map "\M-e" 'isearch-edit-string)
340 (define-key map [?\M-%] 'isearch-query-replace)
341 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
344 "Keymap for `isearch-mode'.")
346 (defvar minibuffer-local-isearch-map
347 (let ((map (make-sparse-keymap)))
348 (set-keymap-parent map minibuffer-local-map)
349 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
350 (define-key map "\M-n" 'isearch-ring-advance-edit)
351 (define-key map [next] 'isearch-ring-advance-edit)
352 (define-key map [down] 'isearch-ring-advance-edit)
353 (define-key map "\M-p" 'isearch-ring-retreat-edit)
354 (define-key map [prior] 'isearch-ring-retreat-edit)
355 (define-key map [up] 'isearch-ring-retreat-edit)
356 (define-key map "\M-\t" 'isearch-complete-edit)
357 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
358 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
359 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
360 (define-key map [right] 'isearch-yank-char-in-minibuffer)
362 "Keymap for editing isearch strings in the minibuffer.")
364 ;; Internal variables declared globally for byte-compiler.
365 ;; These are all set with setq while isearching
366 ;; and bound locally while editing the search string.
368 (defvar isearch-forward nil) ; Searching in the forward direction.
369 (defvar isearch-regexp nil) ; Searching for a regexp.
370 (defvar isearch-word nil) ; Searching for words.
371 (defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
373 (defvar isearch-cmds nil
374 "Stack of search status sets.
375 Each set is a vector of the form:
376 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
377 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
379 (defvar isearch-string "") ; The current search string.
380 (defvar isearch-message "") ; text-char-description version of isearch-string
382 (defvar isearch-success t) ; Searching is currently successful.
383 (defvar isearch-invalid-regexp nil) ; Regexp not well formed.
384 (defvar isearch-within-brackets nil) ; Regexp has unclosed [.
385 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
386 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
387 (defvar isearch-barrier 0)
388 (defvar isearch-just-started nil)
389 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
391 ; case-fold-search while searching.
392 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
393 ; case in the search string is ignored.
394 (defvar isearch-case-fold-search nil)
396 (defvar isearch-last-case-fold-search nil)
398 ;; Used to save default value while isearch is active
399 (defvar isearch-original-minibuffer-message-timeout nil)
401 (defvar isearch-adjusted nil)
402 (defvar isearch-slow-terminal-mode nil)
403 ;; If t, using a small window.
404 (defvar isearch-small-window nil)
405 (defvar isearch-opoint 0)
406 ;; The window configuration active at the beginning of the search.
407 (defvar isearch-window-configuration nil)
409 ;; Flag to indicate a yank occurred, so don't move the cursor.
410 (defvar isearch-yank-flag nil)
412 ;; A function to be called after each input character is processed.
413 ;; (It is not called after characters that exit the search.)
414 ;; It is only set from an optional argument to `isearch-mode'.
415 (defvar isearch-op-fun nil)
417 ;; Is isearch-mode in a recursive edit for modal searching.
418 (defvar isearch-recursive-edit nil)
420 ;; Should isearch be terminated after doing one search?
421 (defvar isearch-nonincremental nil)
423 ;; New value of isearch-forward after isearch-edit-string.
424 (defvar isearch-new-forward nil)
426 ;; Accumulate here the overlays opened during searching.
427 (defvar isearch-opened-overlays nil)
429 ;; The value of input-method-function when isearch is invoked.
430 (defvar isearch-input-method-function nil)
432 ;; A flag to tell if input-method-function is locally bound when
433 ;; isearch is invoked.
434 (defvar isearch-input-method-local-p nil)
436 ;; Minor-mode-alist changes - kind of redundant with the
437 ;; echo area, but if isearching in multiple windows, it can be useful.
439 (or (assq 'isearch-mode minor-mode-alist)
440 (nconc minor-mode-alist
441 (list '(isearch-mode isearch-mode))))
443 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
444 (make-variable-buffer-local 'isearch-mode)
446 (define-key global-map "\C-s" 'isearch-forward)
447 (define-key esc-map "\C-s" 'isearch-forward-regexp)
448 (define-key global-map "\C-r" 'isearch-backward)
449 (define-key esc-map "\C-r" 'isearch-backward-regexp)
451 ;; Entry points to isearch-mode.
453 (defun isearch-forward (&optional regexp-p no-recursive-edit)
455 Do incremental search forward.
456 With a prefix argument, do an incremental regular expression search instead.
458 As you type characters, they add to the search string and are found.
459 The following non-printing keys are bound in `isearch-mode-map'.
461 Type \\[isearch-delete-char] to cancel last input item from end of search string.
462 Type \\[isearch-exit] to exit, leaving point at location found.
463 Type LFD (C-j) to match end of line.
464 Type \\[isearch-repeat-forward] to search again forward,\
465 \\[isearch-repeat-backward] to search again backward.
466 Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
467 string and search for it.
468 Type \\[isearch-del-char] to delete character from end of search string.
469 Type \\[isearch-yank-char] to yank char from buffer onto end of search\
470 string and search for it.
471 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
473 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
475 Type \\[isearch-quote-char] to quote control character to search for it.
476 \\[isearch-abort] while searching or when search has failed cancels input\
478 been found successfully.
479 \\[isearch-abort] when search is successful aborts and moves point to\
482 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
483 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
484 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
486 Also supported is a search ring of the previous 16 search strings.
487 Type \\[isearch-ring-advance] to search for the next item in the search ring.
488 Type \\[isearch-ring-retreat] to search for the previous item in the search\
490 Type \\[isearch-complete] to complete the search string using the search ring.
492 If an input method is turned on in the current buffer, that input
493 method is also active while you are typing characters to search. To
494 toggle the input method, type \\[isearch-toggle-input-method]. It
495 also toggles the input method in the current buffer.
497 To use a different input method for searching, type
498 \\[isearch-toggle-specified-input-method], and specify an input method
501 The above keys, bound in `isearch-mode-map', are often controlled by
502 options; do \\[apropos] on search-.* to find them.
503 Other control and meta characters terminate the search
504 and are then executed normally (depending on `search-exit-option').
505 Likewise for function keys and mouse button events.
507 If this function is called non-interactively, it does not return to
508 the calling function until the search is done."
511 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
513 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
515 Do incremental search forward for regular expression.
516 With a prefix argument, do a regular string search instead.
517 Like ordinary incremental search except that your input
518 is treated as a regexp. See \\[isearch-forward] for more info."
520 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
522 (defun isearch-backward (&optional regexp-p no-recursive-edit)
524 Do incremental search backward.
525 With a prefix argument, do a regular expression search instead.
526 See \\[isearch-forward] for more information."
528 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
530 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
532 Do incremental search backward for regular expression.
533 With a prefix argument, do a regular string search instead.
534 Like ordinary incremental search except that your input
535 is treated as a regexp. See \\[isearch-forward] for more info."
537 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
540 (defun isearch-mode-help ()
542 (describe-function 'isearch-forward)
546 ;; isearch-mode only sets up incremental search for the minor mode.
547 ;; All the work is done by the isearch-mode commands.
550 ;;(defvar isearch-commands '(isearch-forward isearch-backward
551 ;; isearch-forward-regexp isearch-backward-regexp)
552 ;; "List of commands for which isearch-mode does not recursive-edit.")
555 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
556 "Start isearch minor mode. Called by `isearch-forward', etc.
558 \\{isearch-mode-map}"
560 ;; Initialize global vars.
561 (setq isearch-forward forward
562 isearch-regexp regexp
564 isearch-op-fun op-fun
565 isearch-last-case-fold-search isearch-case-fold-search
566 isearch-case-fold-search case-fold-search
572 isearch-barrier (point)
574 isearch-yank-flag nil
575 isearch-invalid-regexp nil
576 isearch-within-brackets nil
577 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
580 (abs search-slow-window-lines))))
581 isearch-other-end nil
582 isearch-small-window nil
583 isearch-just-started t
584 isearch-start-hscroll (window-hscroll)
586 isearch-opoint (point)
587 search-ring-yank-pointer nil
588 isearch-opened-overlays nil
589 isearch-input-method-function input-method-function
590 isearch-input-method-local-p (local-variable-p 'input-method-function)
591 regexp-search-ring-yank-pointer nil
593 ;; Save the original value of `minibuffer-message-timeout', and
594 ;; set it to nil so that isearch's messages don't get timed out.
595 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
596 minibuffer-message-timeout nil)
598 ;; We must bypass input method while reading key. When a user type
599 ;; printable character, appropriate input method is turned on in
600 ;; minibuffer to read multibyte characters.
601 (or isearch-input-method-local-p
602 (make-local-variable 'input-method-function))
603 (setq input-method-function nil)
606 (setq isearch-window-configuration
607 (if isearch-slow-terminal-mode (current-window-configuration) nil))
609 ;; Maybe make minibuffer frame visible and/or raise it.
610 (let ((frame (window-frame (minibuffer-window))))
611 (unless (memq (frame-live-p frame) '(nil t))
612 (unless (frame-visible-p frame)
613 (make-frame-visible frame))
614 (if minibuffer-auto-raise
615 (raise-frame frame))))
617 (setq isearch-mode " Isearch") ;; forward? regexp?
618 (force-mode-line-update)
622 (setq overriding-terminal-local-map isearch-mode-map)
624 (run-hooks 'isearch-mode-hook)
626 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
627 (add-hook 'kbd-macro-termination-hook 'isearch-done)
629 ;; isearch-mode can be made modal (in the sense of not returning to
630 ;; the calling function until searching is completed) by entering
631 ;; a recursive-edit and exiting it when done isearching.
633 (let ((isearch-recursive-edit t))
638 ;; Some high level utilities. Others below.
640 (defun isearch-update ()
641 ;; Called after each command to update the display.
642 (if (and (null unread-command-events)
643 (null executing-kbd-macro))
645 (if (not (input-pending-p))
647 (if (and isearch-slow-terminal-mode
648 (not (or isearch-small-window
649 (pos-visible-in-window-p))))
650 (let ((found-point (point)))
651 (setq isearch-small-window t)
652 (move-to-window-line 0)
653 (let ((window-min-height 1))
654 (split-window nil (if (< search-slow-window-lines 0)
655 (1+ (- search-slow-window-lines))
657 (1+ search-slow-window-lines)))))
658 (if (< search-slow-window-lines 0)
659 (progn (vertical-motion (- 1 search-slow-window-lines))
660 (set-window-start (next-window) (point))
661 (set-window-hscroll (next-window)
663 (set-window-hscroll (selected-window) 0))
665 (goto-char found-point))
666 ;; Keep same hscrolling as at the start of the search when possible
667 (let ((current-scroll (window-hscroll)))
668 (set-window-hscroll (selected-window) isearch-start-hscroll)
669 (unless (pos-visible-in-window-p)
670 (set-window-hscroll (selected-window) current-scroll))))
671 (if isearch-other-end
672 (if (< isearch-other-end (point)) ; isearch-forward?
673 (isearch-highlight isearch-other-end (point))
674 (isearch-highlight (point) isearch-other-end))
675 (isearch-dehighlight nil))
677 (setq ;; quit-flag nil not for isearch-mode
679 isearch-yank-flag nil)
680 (isearch-lazy-highlight-new-loop)
681 ;; We must prevent the point moving to the end of composition when a
682 ;; part of the composition has just been searched.
683 (setq disable-point-adjustment t))
685 (defun isearch-done (&optional nopush edit)
686 (if isearch-resume-enabled
687 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
688 ,isearch-word ,isearch-forward
690 ',isearch-case-fold-search)))
691 (unless (equal (car command-history) command)
692 (setq command-history (cons command command-history)))))
694 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
695 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
696 (setq isearch-lazy-highlight-start nil)
698 ;; Called by all commands that terminate isearch-mode.
699 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
700 (setq overriding-terminal-local-map nil)
701 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
702 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
703 (isearch-dehighlight t)
704 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
705 (let ((found-start (window-start (selected-window)))
706 (found-point (point)))
707 (if isearch-window-configuration
708 (set-window-configuration isearch-window-configuration))
710 (if isearch-small-window
711 (goto-char found-point)
712 ;; Exiting the save-window-excursion clobbers window-start; restore it.
713 (set-window-start (selected-window) found-start t))
715 ;; If there was movement, mark the starting position.
716 ;; Maybe should test difference between and set mark iff > threshold.
717 (if (/= (point) isearch-opoint)
718 (or (and transient-mark-mode mark-active)
720 (push-mark isearch-opoint t)
721 (or executing-kbd-macro (> (minibuffer-depth) 0)
722 (message "Mark saved where search started"))))))
724 (setq isearch-mode nil)
725 (if isearch-input-method-local-p
726 (setq input-method-function isearch-input-method-function)
727 (kill-local-variable 'input-method-function))
729 (force-mode-line-update)
731 ;; If we ended in the middle of some intangible text,
732 ;; move to the further end of that intangible text.
733 (let ((after (if (eobp) nil
734 (get-text-property (point) 'intangible)))
735 (before (if (bobp) nil
736 (get-text-property (1- (point)) 'intangible))))
737 (when (and before after (eq before after))
739 (goto-char (next-single-property-change (point) 'intangible))
740 (goto-char (previous-single-property-change (point) 'intangible)))))
742 (if (and (> (length isearch-string) 0) (not nopush))
743 ;; Update the ring data.
744 (isearch-update-ring isearch-string isearch-regexp))
746 (run-hooks 'isearch-mode-end-hook)
747 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
749 (defun isearch-update-ring (string &optional regexp)
750 "Add STRING to the beginning of the search ring.
751 REGEXP says which ring to use."
753 (if (or (null regexp-search-ring)
754 (not (string= string (car regexp-search-ring))))
756 (push string regexp-search-ring)
757 (if (> (length regexp-search-ring) regexp-search-ring-max)
758 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
760 (if (or (null search-ring)
761 (not (string= string (car search-ring))))
763 (push string search-ring)
764 (if (> (length search-ring) search-ring-max)
765 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
767 ;; Switching buffers should first terminate isearch-mode.
768 ;; ;; For Emacs 19, the frame switch event is handled.
769 ;; (defun isearch-switch-frame-handler ()
770 ;; (interactive) ;; Is this necessary?
771 ;; ;; First terminate isearch-mode.
773 ;; (isearch-clean-overlays)
774 ;; (handle-switch-frame (car (cdr last-command-char))))
777 ;; The search status structure and stack.
779 (defsubst isearch-string (frame)
780 "Return the search string in FRAME."
782 (defsubst isearch-message-string (frame)
783 "Return the search string to display to the user in FRAME."
785 (defsubst isearch-point (frame)
786 "Return the point in FRAME."
788 (defsubst isearch-success (frame)
789 "Return the success flag in FRAME."
791 (defsubst isearch-forward-flag (frame)
792 "Return the searching-forward flag in FRAME."
794 (defsubst isearch-other-end (frame)
795 "Return the other end of the match in FRAME."
797 (defsubst isearch-word (frame)
798 "Return the search-by-word flag in FRAME."
800 (defsubst isearch-invalid-regexp (frame)
801 "Return the regexp error message in FRAME, or nil if its regexp is valid."
803 (defsubst isearch-wrapped (frame)
804 "Return the search-wrapped flag in FRAME."
806 (defsubst isearch-barrier (frame)
807 "Return the barrier value in FRAME."
809 (defsubst isearch-within-brackets (frame)
810 "Return the in-character-class flag in FRAME."
812 (defsubst isearch-case-fold-search (frame)
813 "Return the case-folding flag in FRAME."
816 (defun isearch-top-state ()
817 (let ((cmd (car isearch-cmds)))
818 (setq isearch-string (isearch-string cmd)
819 isearch-message (isearch-message-string cmd)
820 isearch-success (isearch-success cmd)
821 isearch-forward (isearch-forward-flag cmd)
822 isearch-other-end (isearch-other-end cmd)
823 isearch-word (isearch-word cmd)
824 isearch-invalid-regexp (isearch-invalid-regexp cmd)
825 isearch-wrapped (isearch-wrapped cmd)
826 isearch-barrier (isearch-barrier cmd)
827 isearch-within-brackets (isearch-within-brackets cmd)
828 isearch-case-fold-search (isearch-case-fold-search cmd))
829 (goto-char (isearch-point cmd))))
831 (defun isearch-pop-state ()
832 (setq isearch-cmds (cdr isearch-cmds))
835 (defun isearch-push-state ()
837 (cons (vector isearch-string isearch-message (point)
838 isearch-success isearch-forward isearch-other-end
840 isearch-invalid-regexp isearch-wrapped isearch-barrier
841 isearch-within-brackets isearch-case-fold-search)
845 ;; Commands active while inside of the isearch minor mode.
847 (defun isearch-exit ()
848 "Exit search normally.
849 However, if this is the first command after starting incremental
850 search and `search-nonincremental-instead' is non-nil, do a
851 nonincremental search instead via `isearch-edit-string'."
853 (if (and search-nonincremental-instead
854 (= 0 (length isearch-string)))
855 (let ((isearch-nonincremental t))
856 (isearch-edit-string)))
858 (isearch-clean-overlays))
861 (defun isearch-edit-string ()
862 "Edit the search string in the minibuffer.
863 The following additional command keys are active while editing.
864 \\<minibuffer-local-isearch-map>
865 \\[exit-minibuffer] to resume incremental searching with the edited string.
866 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
867 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
868 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
869 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
870 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
871 \\[isearch-complete-edit] to complete the search string using the search ring.
873 If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
875 ;; This code is very hairy for several reasons, explained in the code.
876 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
877 ;; If there were a way to catch any change of buffer from the minibuffer,
878 ;; this could be simplified greatly.
879 ;; Editing doesn't back up the search point. Should it?
883 (let ((isearch-nonincremental isearch-nonincremental)
885 ;; Locally bind all isearch global variables to protect them
886 ;; from recursive isearching.
887 ;; isearch-string -message and -forward are not bound
888 ;; so they may be changed. Instead, save the values.
889 (isearch-new-string isearch-string)
890 (isearch-new-message isearch-message)
891 (isearch-new-forward isearch-forward)
892 (isearch-new-word isearch-word)
894 (isearch-regexp isearch-regexp)
895 (isearch-op-fun isearch-op-fun)
896 (isearch-cmds isearch-cmds)
897 (isearch-success isearch-success)
898 (isearch-wrapped isearch-wrapped)
899 (isearch-barrier isearch-barrier)
900 (isearch-adjusted isearch-adjusted)
901 (isearch-yank-flag isearch-yank-flag)
902 (isearch-invalid-regexp isearch-invalid-regexp)
903 (isearch-within-brackets isearch-within-brackets)
904 ;;; Don't bind this. We want isearch-search, below, to set it.
905 ;;; And the old value won't matter after that.
906 ;;; (isearch-other-end isearch-other-end)
907 ;;; Perhaps some of these other variables should be bound for a
908 ;;; shorter period, ending before the next isearch-search.
909 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
910 (isearch-opoint isearch-opoint)
911 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
912 (isearch-small-window isearch-small-window)
913 (isearch-recursive-edit isearch-recursive-edit)
914 ;; Save current configuration so we can restore it here.
915 (isearch-window-configuration (current-window-configuration))
917 ;; Temporarily restore `minibuffer-message-timeout'.
918 (minibuffer-message-timeout
919 isearch-original-minibuffer-message-timeout)
920 (isearch-original-minibuffer-message-timeout
921 isearch-original-minibuffer-message-timeout)
924 ;; Actually terminate isearching until editing is done.
925 ;; This is so that the user can do anything without failure,
926 ;; like switch buffers and start another isearch, and return.
929 (exit nil)) ; was recursive editing
931 (isearch-message) ;; for read-char
933 (let* (;; Why does following read-char echo?
934 ;;(echo-keystrokes 0) ;; not needed with above message
935 (e (let ((cursor-in-echo-area t))
937 ;; Binding minibuffer-history-symbol to nil is a work-around
938 ;; for some incompatibility with gmhist.
939 (minibuffer-history-symbol)
940 (message-log-max nil))
941 ;; If the first character the user types when we prompt them
942 ;; for a string is the yank-word character, then go into
943 ;; word-search mode. Otherwise unread that character and
944 ;; read a key the normal way.
945 ;; Word search does not apply (yet) to regexp searches,
946 ;; no check is made here.
947 (message (isearch-message-prefix nil nil t))
948 (if (memq (lookup-key isearch-mode-map (vector e))
950 isearch-yank-word-or-char))
951 (setq isearch-word t;; so message-prefix is right
953 (cancel-kbd-macro-events)
955 (setq cursor-in-echo-area nil)
956 (setq isearch-new-string
958 (read-from-minibuffer
959 (isearch-message-prefix nil nil isearch-nonincremental)
961 minibuffer-local-isearch-map nil
964 (mapconcat 'isearch-text-char-description
965 isearch-new-string "")))
966 ;; Always resume isearching by restarting it.
967 (isearch-mode isearch-forward
973 ;; Copy new local values to isearch globals
974 (setq isearch-string isearch-new-string
975 isearch-message isearch-new-message
976 isearch-forward isearch-new-forward
977 isearch-word isearch-new-word))
979 ;; Empty isearch-string means use default.
980 (if (= 0 (length isearch-string))
981 (setq isearch-string (or (car (if isearch-regexp
987 (mapconcat 'isearch-text-char-description
989 ;; This used to set the last search string,
990 ;; but I think it is not right to do that here.
991 ;; Only the string actually used should be saved.
994 ;; Push the state as of before this C-s.
997 ;; Reinvoke the pending search.
1000 (if isearch-nonincremental
1002 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
1005 (quit ; handle abort-recursive-edit
1006 (isearch-abort) ;; outside of let to restore outside global values
1009 (defun isearch-nonincremental-exit-minibuffer ()
1011 (setq isearch-nonincremental t)
1014 (defun isearch-forward-exit-minibuffer ()
1016 (setq isearch-new-forward t)
1019 (defun isearch-reverse-exit-minibuffer ()
1021 (setq isearch-new-forward nil)
1024 (defun isearch-cancel ()
1025 "Terminate the search and go back to the starting point."
1027 (goto-char isearch-opoint)
1029 (isearch-clean-overlays)
1030 (signal 'quit nil)) ; and pass on quit signal
1032 (defun isearch-abort ()
1033 "Abort incremental search mode if searching is successful, signaling quit.
1034 Otherwise, revert to previous successful search and continue searching.
1035 Use `isearch-exit' to quit without signaling."
1037 ;; (ding) signal instead below, if quitting
1040 ;; If search is successful, move back to starting point
1041 ;; and really do quit.
1042 (progn (goto-char isearch-opoint)
1043 (setq isearch-success nil)
1044 (isearch-done t) ; exit isearch
1045 (isearch-clean-overlays)
1046 (signal 'quit nil)) ; and pass on quit signal
1047 ;; If search is failing, or has an incomplete regexp,
1048 ;; rub out until it is once more successful.
1049 (while (or (not isearch-success) isearch-invalid-regexp)
1050 (isearch-pop-state))
1053 (defun isearch-repeat (direction)
1054 ;; Utility for isearch-repeat-forward and -backward.
1055 (if (eq isearch-forward (eq direction 'forward))
1056 ;; C-s in forward or C-r in reverse.
1057 (if (equal isearch-string "")
1058 ;; If search string is empty, use last one.
1059 (setq isearch-string
1060 (or (if isearch-regexp
1061 (car regexp-search-ring)
1063 (error "No previous search string"))
1065 (mapconcat 'isearch-text-char-description
1067 isearch-case-fold-search isearch-last-case-fold-search)
1068 ;; If already have what to search for, repeat it.
1071 (goto-char (if isearch-forward (point-min) (point-max)))
1072 (setq isearch-wrapped t))))
1073 ;; C-s in reverse or C-r in forward, change direction.
1074 (setq isearch-forward (not isearch-forward)))
1076 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1078 (if (equal isearch-string "")
1079 (setq isearch-success t)
1080 (if (and isearch-success
1081 (equal (point) isearch-other-end)
1082 (not isearch-just-started))
1083 ;; If repeating a search that found
1084 ;; an empty string, ensure we advance.
1085 (if (if isearch-forward (eobp) (bobp))
1086 ;; If there's nowhere to advance to, fail (and wrap next time).
1088 (setq isearch-success nil)
1090 (forward-char (if isearch-forward 1 -1))
1094 (isearch-push-state)
1097 (defun isearch-repeat-forward ()
1098 "Repeat incremental search forwards."
1100 (isearch-repeat 'forward))
1102 (defun isearch-repeat-backward ()
1103 "Repeat incremental search backwards."
1105 (isearch-repeat 'backward))
1107 (defun isearch-toggle-regexp ()
1108 "Toggle regexp searching on or off."
1109 ;; The status stack is left unchanged.
1111 (setq isearch-regexp (not isearch-regexp))
1112 (if isearch-regexp (setq isearch-word nil))
1115 (defun isearch-toggle-case-fold ()
1116 "Toggle case folding in searching on or off."
1118 (setq isearch-case-fold-search
1119 (if isearch-case-fold-search nil 'yes))
1120 (let ((message-log-max nil))
1121 (message "%s%s [case %ssensitive]"
1122 (isearch-message-prefix nil nil isearch-nonincremental)
1124 (if isearch-case-fold-search "in" "")))
1125 (setq isearch-adjusted t)
1129 (defun isearch-query-replace ()
1130 "Start query-replace with string to replace from last search string."
1132 (barf-if-buffer-read-only)
1133 (let ((case-fold-search isearch-case-fold-search))
1135 (isearch-clean-overlays)
1136 (and isearch-forward isearch-other-end (goto-char isearch-other-end))
1139 (query-replace-read-to isearch-string "Query replace" isearch-regexp)
1140 t isearch-regexp isearch-word)))
1142 (defun isearch-query-replace-regexp ()
1143 "Start query-replace-regexp with string to replace from last search string."
1145 (let ((query-replace-interactive t)
1146 (case-fold-search isearch-case-fold-search))
1147 ;; Put search string into the right ring
1148 (setq isearch-regexp t)
1150 (isearch-clean-overlays)
1151 (and isearch-forward isearch-other-end (goto-char isearch-other-end))
1152 (call-interactively 'query-replace-regexp)))
1155 (defun isearch-delete-char ()
1156 "Discard last input item and move point back.
1157 If no previous match was done, just beep."
1159 (if (null (cdr isearch-cmds))
1161 (isearch-pop-state))
1164 (defun isearch-del-char (&optional arg)
1165 "Delete character from end of search string and search again.
1166 If search string is empty, just beep."
1168 (if (= 0 (length isearch-string))
1170 (setq isearch-string (substring isearch-string 0 (- (or arg 1)))
1171 isearch-message (mapconcat 'isearch-text-char-description
1173 ;; Don't move cursor in reverse search.
1174 isearch-yank-flag t))
1175 (isearch-search-and-update))
1177 (defun isearch-yank-string (string)
1178 "Pull STRING into search string."
1179 ;; Downcase the string if not supposed to case-fold yanked strings.
1180 (if (and isearch-case-fold-search
1181 (eq 'not-yanks search-upper-case))
1182 (setq string (downcase string)))
1183 (if isearch-regexp (setq string (regexp-quote string)))
1184 (setq isearch-string (concat isearch-string string)
1186 (concat isearch-message
1187 (mapconcat 'isearch-text-char-description
1189 ;; Don't move cursor in reverse search.
1190 isearch-yank-flag t)
1191 (isearch-search-and-update))
1193 (defun isearch-yank-kill ()
1194 "Pull string from kill ring into search string."
1196 (isearch-yank-string (current-kill 0)))
1198 (defun isearch-yank-x-selection ()
1199 "Pull current X selection into search string."
1201 (isearch-yank-string (x-get-selection)))
1204 (defun isearch-mouse-2 (click)
1205 "Handle mouse-2 in Isearch mode.
1206 For a click in the echo area, invoke `isearch-yank-x-selection'.
1207 Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
1209 (let* ((w (posn-window (event-start click)))
1210 (overriding-terminal-local-map nil)
1211 (key (vector (event-basic-type click)))
1212 ;; FIXME: `key-binding' should accept an event as argument
1213 ;; and do all the overlay/text-properties lookup etc...
1214 (binding (with-current-buffer
1215 (if (window-live-p w) (window-buffer w) (current-buffer))
1216 (key-binding key))))
1217 (if (and (window-minibuffer-p w)
1218 (not (minibuffer-window-active-p w))) ; in echo area
1219 (isearch-yank-x-selection)
1220 (when (functionp binding)
1221 (call-interactively binding)))))
1224 (defun isearch-yank-internal (jumpform)
1225 "Pull the text from point to the point reached by JUMPFORM.
1226 JUMPFORM is a lambda expression that takes no arguments and returns a
1227 buffer position, possibly having moved point to that position. For
1228 example, it might move point forward by a word and return point, or it
1229 might return the position of the end of the line."
1230 (isearch-yank-string
1232 (and (not isearch-forward) isearch-other-end
1233 (goto-char isearch-other-end))
1234 (buffer-substring-no-properties (point) (funcall jumpform)))))
1236 (defun isearch-yank-char-in-minibuffer (&optional arg)
1237 "Pull next character from buffer into end of search string in minibuffer."
1242 (set-buffer (cadr (buffer-list)))
1243 (buffer-substring-no-properties
1244 (point) (progn (forward-char arg) (point)))))
1245 (forward-char arg)))
1247 (defun isearch-yank-char (&optional arg)
1248 "Pull next character from buffer into search string."
1250 (isearch-yank-internal (lambda () (forward-char arg) (point))))
1252 (defun isearch-yank-word-or-char ()
1253 "Pull next character or word from buffer into search string."
1255 (isearch-yank-internal
1257 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1258 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1260 (forward-char 1)) (point))))
1262 (defun isearch-yank-word ()
1263 "Pull next word from buffer into search string."
1265 (isearch-yank-internal (lambda () (forward-word 1) (point))))
1267 (defun isearch-yank-line ()
1268 "Pull rest of line from buffer into search string."
1270 (isearch-yank-internal 'line-end-position))
1273 (defun isearch-search-and-update ()
1274 ;; Do the search and update the display.
1275 (when (or isearch-success
1276 ;; Unsuccessful regexp search may become successful by
1277 ;; addition of characters which make isearch-string valid
1279 ;; If the string was found but was completely invisible,
1280 ;; it might now be partly visible, so try again.
1281 (prog1 isearch-hidden (setq isearch-hidden nil)))
1282 ;; In reverse search, adding stuff at
1283 ;; the end may cause zero or many more chars to be
1284 ;; matched, in the string following point.
1285 ;; Allow all those possibilities without moving point as
1286 ;; long as the match does not extend past search origin.
1287 (if (and (not isearch-forward) (not isearch-adjusted)
1289 (let ((case-fold-search isearch-case-fold-search))
1290 (if (and (eq case-fold-search t) search-upper-case)
1291 (setq case-fold-search
1292 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1293 (looking-at (if isearch-regexp isearch-string
1294 (regexp-quote isearch-string))))
1296 (or isearch-yank-flag
1298 (min isearch-opoint isearch-barrier))))
1300 (setq isearch-success t
1301 isearch-invalid-regexp nil
1302 isearch-within-brackets nil
1303 isearch-other-end (match-end 0))
1304 (if (and (eq isearch-case-fold-search t) search-upper-case)
1305 (setq isearch-case-fold-search
1306 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1307 ;; Not regexp, not reverse, or no match at point.
1308 (if (and isearch-other-end (not isearch-adjusted))
1309 (goto-char (if isearch-forward isearch-other-end
1312 (1+ isearch-other-end)))))
1315 (isearch-push-state)
1316 (if isearch-op-fun (funcall isearch-op-fun))
1320 ;; *, ?, }, and | chars can make a regexp more liberal.
1321 ;; They can make a regexp match sooner or make it succeed instead of failing.
1322 ;; So go back to place last successful search started
1323 ;; or to the last ^S/^R (barrier), whichever is nearer.
1324 ;; + needs no special handling because the string must match at least once.
1326 (defun isearch-backslash (str)
1327 "Return t if STR ends in an odd number of backslashes."
1328 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
1330 (defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
1331 "Return point to previous successful match to allow regexp liberalization.
1332 \\<isearch-mode-map>
1333 Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by
1334 stopping at `isearch-barrier' as needed.
1336 Do nothing if a backslash is escaping the liberalizing character. If
1337 WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
1339 Do nothing if regexp has recently been invalid unless optional ALLOW-INVALID
1342 If optional TO-BARRIER non-nil, ignore previous matches and go exactly to the
1344 ;; (eq (not a) (not b)) makes all non-nil values equivalent
1345 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
1346 (not want-backslash))
1347 ;; We have to check 2 stack frames because the last might be
1348 ;; invalid just because of a backslash.
1349 (or (not isearch-invalid-regexp)
1350 (not (isearch-invalid-regexp (cadr isearch-cmds)))
1353 (progn (goto-char isearch-barrier)
1354 (setq isearch-adjusted t))
1355 (let* ((stack isearch-cmds)
1356 (previous (cdr stack)) ; lookbelow in the stack
1357 (frame (car stack)))
1358 ;; Walk down the stack looking for a valid regexp (as of course only
1359 ;; they can be the previous successful match); this conveniently
1360 ;; removes all bracket-sets and groups that might be in the way, as
1361 ;; well as partial \{\} constructs that the code below leaves behind.
1362 ;; Also skip over postfix operators -- though horrid,
1363 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly legal.
1364 (while (and previous
1365 (or (isearch-invalid-regexp frame)
1366 (let* ((string (isearch-string frame))
1367 (lchar (aref string (1- (length string)))))
1368 ;; The operators aren't always operators; check
1369 ;; backslashes. This doesn't handle the case of
1370 ;; operators at the beginning of the regexp not
1371 ;; being special, but then we should fall back to
1372 ;; the barrier anyway because it's all optional.
1373 (if (isearch-backslash
1374 (isearch-string (car previous)))
1376 (memq lchar '(?* ?? ?+))))))
1377 (setq stack previous previous (cdr previous) frame (car stack)))
1379 ;; `stack' now refers the most recent valid regexp that is not at
1380 ;; all optional in its last term. Now dig one level deeper and find
1381 ;; what matched before that.
1382 (let ((last-other-end (or (isearch-other-end (car previous))
1384 (goto-char (if isearch-forward
1385 (max last-other-end isearch-barrier)
1386 (min last-other-end isearch-barrier)))
1387 (setq isearch-adjusted t))))))
1388 (isearch-process-search-char last-command-char))
1390 ;; * and ? are special when not preceded by \.
1391 (defun isearch-*-char ()
1392 "Maybe back up to handle * and ? specially in regexps."
1394 (isearch-fallback nil))
1396 ;; } is special when it is preceded by \.
1397 (defun isearch-}-char ()
1398 "Handle \\} specially in regexps."
1400 (isearch-fallback t t))
1402 ;; | is special when it is preceded by \.
1403 (defun isearch-|-char ()
1404 "If in regexp search, jump to the barrier unless in a group."
1406 (isearch-fallback t nil t))
1408 (defun isearch-unread-key-sequence (keylist)
1409 "Unread the given key-sequence KEYLIST.
1410 Scroll-bar or mode-line events are processed appropriately."
1411 (cancel-kbd-macro-events)
1412 (apply 'isearch-unread keylist)
1413 ;; If the event was a scroll-bar or mode-line click, the event will have
1414 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
1415 ;; it here, because this symbol will be attached to the event again next
1416 ;; time it gets read by read-key-sequence.
1418 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
1419 ;; modify the event anymore in 21 because read_key_sequence no longer
1420 ;; modifies events to produce fake prefix keys.")
1421 (if (and (> (length keylist) 1)
1422 (symbolp (car keylist))
1423 (listp (cadr keylist))
1424 (not (numberp (posn-point
1425 (event-start (cadr keylist) )))))
1426 (pop unread-command-events)))
1428 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1429 ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
1431 ;; The idea here is that certain vertical scrolling commands (like C-l
1432 ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
1433 ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
1434 ;; tamper with isearch's state, or move point. It is unacceptable for the
1435 ;; search string to be scrolled out of the current window. If a command
1436 ;; attempts this, we scroll the text back again.
1438 ;; We implement this feature with a property called `isearch-scroll'.
1439 ;; If a command's symbol has the value t for this property it is a
1440 ;; scrolling command. The feature needs to be enabled by setting the
1441 ;; customizable variable `isearch-allow-scroll' to a non-nil value.
1443 ;; The universal argument commands (e.g. C-u) in simple.el are marked
1444 ;; as scrolling commands, and isearch.el has been amended to allow
1445 ;; prefix arguments to be passed through to scrolling commands. Thus
1446 ;; M-0 C-l will scroll point to the top of the window.
1448 ;; Horizontal scrolling commands are currently not catered for.
1449 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1451 ;; Set the isearch-scroll property on some standard functions:
1452 ;; Scroll-bar functions:
1453 (if (fboundp 'scroll-bar-toolkit-scroll)
1454 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
1455 (if (fboundp 'mac-handle-scroll-bar-event)
1456 (put 'mac-handle-scroll-bar-event 'isearch-scroll t))
1457 (if (fboundp 'w32-handle-scroll-bar-event)
1458 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
1460 ;; Commands which scroll the window:
1461 (put 'recenter 'isearch-scroll t)
1462 (put 'reposition-window 'isearch-scroll t)
1463 (put 'scroll-up 'isearch-scroll t)
1464 (put 'scroll-down 'isearch-scroll t)
1466 ;; Commands which act on the other window
1467 (put 'list-buffers 'isearch-scroll t)
1468 (put 'scroll-other-window 'isearch-scroll t)
1469 (put 'scroll-other-window-down 'isearch-scroll t)
1470 (put 'beginning-of-buffer-other-window 'isearch-scroll t)
1471 (put 'end-of-buffer-other-window 'isearch-scroll t)
1473 ;; Commands which change the window layout
1474 (put 'delete-other-windows 'isearch-scroll t)
1475 (put 'balance-windows 'isearch-scroll t)
1476 (put 'split-window-vertically 'isearch-scroll t)
1477 (put 'enlarge-window 'isearch-scroll t)
1479 ;; Universal argument commands
1480 (put 'universal-argument 'isearch-scroll t)
1481 (put 'negative-argument 'isearch-scroll t)
1482 (put 'digit-argument 'isearch-scroll t)
1484 (defcustom isearch-allow-scroll nil
1485 "If non-nil, scrolling commands are allowed during incremental search."
1489 (defun isearch-string-out-of-window (isearch-point)
1490 "Test whether the search string is currently outside of the window.
1491 Return nil if it's completely visible, or if point is visible,
1492 together with as much of the search string as will fit; the symbol
1493 `above' if we need to scroll the text downwards; the symbol `below',
1495 (let ((w-start (window-start))
1496 (w-end (window-end nil t))
1497 (w-L1 (save-excursion (move-to-window-line 1) (point)))
1498 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
1499 start end) ; start and end of search string in buffer
1501 (setq end isearch-point start (or isearch-other-end isearch-point))
1502 (setq start isearch-point end (or isearch-other-end isearch-point)))
1503 (cond ((or (and (>= start w-start) (<= end w-end))
1505 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
1506 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
1508 ((and (< start w-start)
1509 (< isearch-point w-L-1))
1513 (defun isearch-back-into-window (above isearch-point)
1514 "Scroll the window to bring the search string back into view.
1515 Restore point to ISEARCH-POINT in the process. ABOVE is t when the
1516 search string is above the top of the window, nil when it is beneath
1520 (setq end isearch-point start (or isearch-other-end isearch-point))
1521 (setq start isearch-point end (or isearch-other-end isearch-point)))
1526 (when (>= isearch-point (window-end nil t))
1527 (goto-char isearch-point)
1531 (when (< isearch-point (window-start))
1532 (goto-char isearch-point)
1534 (goto-char isearch-point))
1536 (defun isearch-reread-key-sequence-naturally (keylist)
1537 "Reread key sequence KEYLIST with Isearch mode's keymap deactivated.
1538 Return the key sequence as a string/vector."
1539 (isearch-unread-key-sequence keylist)
1540 (let (overriding-terminal-local-map)
1541 (read-key-sequence nil))) ; This will go through function-key-map, if nec.
1543 (defun isearch-lookup-scroll-key (key-seq)
1544 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
1545 Otherwise return nil."
1546 (let* ((overriding-terminal-local-map nil)
1547 (binding (key-binding key-seq)))
1548 (and binding (symbolp binding) (commandp binding)
1549 (eq (get binding 'isearch-scroll) t)
1552 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1554 (defun isearch-other-meta-char (&optional arg)
1555 "Process a miscellaneous key sequence in Isearch mode.
1557 Try to convert the current key-sequence to something usable in Isearch
1558 mode, either by converting it with `function-key-map', downcasing a
1559 key with C-<upper case>, or finding a \"scrolling command\" bound to
1560 it. \(In the last case, we may have to read more events.) If so,
1561 either unread the converted sequence or execute the command.
1563 Otherwise, if `search-exit-option' is non-nil (the default) unread the
1564 key-sequence and exit the search normally. If it is the symbol
1565 `edit', the search string is edited in the minibuffer and the meta
1566 character is unread so that it applies to editing the string.
1568 ARG is the prefix argument. It will be transmitted through to the
1569 scrolling command or to the command whose key-sequence exits
1572 (let* ((key (if current-prefix-arg ; not nec the same as ARG
1573 (substring (this-command-keys) universal-argument-num-events)
1574 (this-command-keys)))
1575 (main-event (aref key 0))
1576 (keylist (listify-key-sequence key))
1577 scroll-command isearch-point)
1578 (cond ((and (= (length key) 1)
1579 (let ((lookup (lookup-key function-key-map key)))
1580 (not (or (null lookup) (integerp lookup)
1581 (keymapp lookup)))))
1582 ;; Handle a function key that translates into something else.
1583 ;; If the key has a global definition too,
1584 ;; exit and unread the key itself, so its global definition runs.
1585 ;; Otherwise, unread the translation,
1586 ;; so that the translated key takes effect within isearch.
1587 (cancel-kbd-macro-events)
1588 (if (lookup-key global-map key)
1591 (apply 'isearch-unread keylist))
1593 (listify-key-sequence (lookup-key function-key-map key)))
1595 (setq key (car keylist))
1596 ;; If KEY is a printing char, we handle it here
1597 ;; directly to avoid the input method and keyboard
1598 ;; coding system translating it.
1599 (if (and (integerp key)
1600 (>= key ?\ ) (/= key 127) (< key 256))
1602 (isearch-process-search-char key)
1603 (setq keylist (cdr keylist)))
1604 ;; As the remaining keys in KEYLIST can't be handled
1605 ;; here, we must reread them.
1606 (apply 'isearch-unread keylist)
1607 (setq keylist nil)))))
1609 ;; Handle an undefined shifted control character
1610 ;; by downshifting it if that makes it defined.
1611 ;; (As read-key-sequence would normally do,
1612 ;; if we didn't have a default definition.)
1613 (let ((mods (event-modifiers main-event)))
1614 (and (integerp main-event)
1616 (memq 'control mods)
1617 (lookup-key isearch-mode-map
1618 (let ((copy (copy-sequence key)))
1620 (- main-event (- ?\C-\S-a ?\C-a)))
1623 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1624 (cancel-kbd-macro-events)
1625 (apply 'isearch-unread keylist))
1626 ((eq search-exit-option 'edit)
1627 (apply 'isearch-unread keylist)
1628 (isearch-edit-string))
1629 ;; Handle a scrolling function.
1630 ((and isearch-allow-scroll
1631 (progn (setq key (isearch-reread-key-sequence-naturally keylist))
1632 (setq keylist (listify-key-sequence key))
1633 (setq main-event (aref key 0))
1634 (setq scroll-command (isearch-lookup-scroll-key key))))
1635 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
1636 ;; complete key sequence, possibly as modified by function-key-map,
1637 ;; not merely the one or two event fragment which invoked
1638 ;; isearch-other-meta-char in the first place.
1639 (setq isearch-point (point))
1640 (setq prefix-arg arg)
1641 (command-execute scroll-command)
1642 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
1644 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
1645 (or (eq (point) isearch-point)
1646 (goto-char isearch-point))))
1650 (isearch-unread-key-sequence keylist)
1651 (setq main-event (car unread-command-events))
1653 ;; If we got a mouse click event, that event contains the
1654 ;; window clicked on. maybe it was read with the buffer
1655 ;; it was clicked on. If so, that buffer, not the current one,
1656 ;; is in isearch mode. So end the search in that buffer.
1658 ;; ??? I have no idea what this if checks for, but it's
1659 ;; obviously wrong for the case that a down-mouse event
1660 ;; on another window invokes this function. The event
1661 ;; will contain the window clicked on and that window's
1662 ;; buffer is certainly not always in Isearch mode.
1664 ;; Leave the code in, but check for current buffer not
1665 ;; being in Isearch mode for now, until someone tells
1666 ;; what it's really supposed to do.
1668 ;; --gerd 2001-08-10.
1670 (if (and (not isearch-mode)
1672 (setq window (posn-window (event-start main-event)))
1674 (or (> (minibuffer-depth) 0)
1675 (not (window-minibuffer-p window))))
1677 (set-buffer (window-buffer window))
1679 (isearch-clean-overlays))
1681 (isearch-clean-overlays)
1682 (setq prefix-arg arg))))
1684 (isearch-process-search-string key key)))))
1686 (defun isearch-quote-char ()
1687 "Quote special characters for incremental search."
1689 (let ((char (read-quoted-char (isearch-message t))))
1690 ;; Assume character codes 0200 - 0377 stand for characters in some
1691 ;; single-byte character set, and convert them to Emacs
1693 (and enable-multibyte-characters
1696 (setq char (unibyte-char-to-multibyte char)))
1697 (isearch-process-search-char char)))
1699 (defun isearch-return-char ()
1700 "Convert return into newline for incremental search.
1703 (isearch-process-search-char ?\n))
1705 (defun isearch-printing-char ()
1706 "Add this ordinary printing character to the search string and search."
1708 (let ((char last-command-char))
1711 (if (and enable-multibyte-characters
1714 (if (keyboard-coding-system)
1715 (isearch-process-search-multibyte-characters char)
1716 (isearch-process-search-char (unibyte-char-to-multibyte char)))
1717 (if current-input-method
1718 (isearch-process-search-multibyte-characters char)
1719 (isearch-process-search-char char)))))
1721 (defun isearch-whitespace-chars ()
1722 "Match all whitespace chars, if in regexp mode.
1723 If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
1726 (if (and search-whitespace-regexp (not isearch-within-brackets)
1727 (not isearch-invalid-regexp))
1728 (isearch-process-search-string search-whitespace-regexp " ")
1729 (isearch-printing-char))
1731 ;; This way of doing word search doesn't correctly extend current search.
1732 ;; (setq isearch-word t)
1733 ;; (setq isearch-adjusted t)
1734 ;; (goto-char isearch-barrier)
1735 (isearch-printing-char))))
1737 (defun isearch-process-search-char (char)
1738 ;; Append the char to the search string, update the message and re-search.
1739 (isearch-process-search-string
1740 (char-to-string char)
1742 (char-to-string char)
1743 (isearch-text-char-description char))))
1745 (defun isearch-process-search-string (string message)
1746 (setq isearch-string (concat isearch-string string)
1747 isearch-message (concat isearch-message message))
1748 (isearch-search-and-update))
1753 (defun isearch-ring-adjust1 (advance)
1754 ;; Helper for isearch-ring-adjust
1755 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1756 (length (length ring))
1757 (yank-pointer-name (if isearch-regexp
1758 'regexp-search-ring-yank-pointer
1759 'search-ring-yank-pointer))
1760 (yank-pointer (eval yank-pointer-name)))
1763 (set yank-pointer-name
1765 (mod (+ (or yank-pointer 0)
1768 (setq isearch-string (nth yank-pointer ring)
1769 isearch-message (mapconcat 'isearch-text-char-description
1770 isearch-string "")))))
1772 (defun isearch-ring-adjust (advance)
1773 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1774 (isearch-ring-adjust1 advance)
1775 (if search-ring-update
1779 (isearch-edit-string)
1781 (isearch-push-state))
1783 (defun isearch-ring-advance ()
1784 "Advance to the next search string in the ring."
1785 ;; This could be more general to handle a prefix arg, but who would use it.
1787 (isearch-ring-adjust 'advance))
1789 (defun isearch-ring-retreat ()
1790 "Retreat to the previous search string in the ring."
1792 (isearch-ring-adjust nil))
1794 (defun isearch-ring-advance-edit (n)
1795 "Insert the next element of the search history into the minibuffer.
1796 With prefix arg N, insert the Nth element."
1798 (let* ((yank-pointer-name (if isearch-regexp
1799 'regexp-search-ring-yank-pointer
1800 'search-ring-yank-pointer))
1801 (yank-pointer (eval yank-pointer-name))
1802 (ring (if isearch-regexp regexp-search-ring search-ring))
1803 (length (length ring)))
1806 (set yank-pointer-name
1808 (mod (- (or yank-pointer 0) n)
1812 (insert (nth yank-pointer ring))
1813 (goto-char (point-max)))))
1815 (defun isearch-ring-retreat-edit (n)
1816 "Insert the previous element of the search history into the minibuffer.
1817 With prefix arg N, insert the Nth element."
1819 (isearch-ring-advance-edit (- n)))
1821 ;;(defun isearch-ring-adjust-edit (advance)
1822 ;; "Use the next or previous search string in the ring while in minibuffer."
1823 ;; (isearch-ring-adjust1 advance)
1825 ;; (insert isearch-string))
1827 ;;(defun isearch-ring-advance-edit ()
1829 ;; (isearch-ring-adjust-edit 'advance))
1831 ;;(defun isearch-ring-retreat-edit ()
1832 ;; "Retreat to the previous search string in the ring while in the minibuffer."
1834 ;; (isearch-ring-adjust-edit nil))
1837 (defun isearch-complete1 ()
1838 ;; Helper for isearch-complete and isearch-complete-edit
1839 ;; Return t if completion OK, nil if no completion exists.
1840 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1841 (completion-ignore-case case-fold-search)
1842 (completion (try-completion isearch-string ring)))
1845 ;; isearch-string stays the same
1847 ((or completion ; not nil, must be a string
1848 (= 0 (length isearch-string))) ; shouldn't have to say this
1849 (if (equal completion isearch-string) ;; no extension?
1851 (if completion-auto-help
1852 (with-output-to-temp-buffer "*Isearch completions*"
1853 (display-completion-list
1854 (all-completions isearch-string ring))))
1857 (setq isearch-string completion))))
1859 (message "No completion") ; waits a second if in minibuffer
1862 (defun isearch-complete ()
1863 "Complete the search string from the strings on the search ring.
1864 The completed string is then editable in the minibuffer.
1865 If there is no completion possible, say so and continue searching."
1867 (if (isearch-complete1)
1868 (progn (setq isearch-message
1869 (mapconcat 'isearch-text-char-description
1871 (isearch-edit-string))
1876 (defun isearch-complete-edit ()
1877 "Same as `isearch-complete' except in the minibuffer."
1879 (setq isearch-string (field-string))
1880 (if (isearch-complete1)
1883 (insert isearch-string))))
1888 (defun isearch-message (&optional c-q-hack ellipsis)
1889 ;; Generate and print the message string.
1890 (let ((cursor-in-echo-area ellipsis)
1892 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
1893 (if (and (not isearch-success)
1894 (string-match " +$" isearch-message))
1896 (substring isearch-message 0 (match-beginning 0))
1897 (propertize (substring isearch-message (match-beginning 0))
1898 'face 'trailing-whitespace))
1900 (isearch-message-suffix c-q-hack ellipsis)
1904 (let ((message-log-max nil))
1905 (message "%s" m)))))
1907 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
1908 ;; If about to search, and previous search regexp was invalid,
1909 ;; check that it still is. If it is valid now,
1910 ;; let the message we display while searching say that it is valid.
1911 (and isearch-invalid-regexp ellipsis
1913 (progn (re-search-forward isearch-string (point) t)
1914 (setq isearch-invalid-regexp nil
1915 isearch-within-brackets nil))
1917 ;; If currently failing, display no ellipsis.
1918 (or isearch-success (setq ellipsis nil))
1919 (let ((m (concat (if isearch-success "" "failing ")
1920 (if (and isearch-wrapped
1922 (> (point) isearch-opoint)
1923 (< (point) isearch-opoint)))
1925 (if isearch-wrapped "wrapped ")
1926 (if isearch-word "word " "")
1927 (if isearch-regexp "regexp " "")
1928 (if nonincremental "search" "I-search")
1929 (if isearch-forward "" " backward")
1930 (if current-input-method
1931 (concat " [" current-input-method-title "]: ")
1934 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
1935 'face 'minibuffer-prompt)))
1937 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
1938 (concat (if c-q-hack "^Q" "")
1939 (if isearch-invalid-regexp
1940 (concat " [" isearch-invalid-regexp "]")
1946 (defvar isearch-search-fun-function nil
1947 "Override `isearch-search-fun'.
1948 This function should return the search function for isearch to use.
1949 It will call this function with three arguments
1950 as if it were `search-forward'.")
1952 (defun isearch-search-fun ()
1953 "Return the function to use for the search.
1954 Can be changed via `isearch-search-fun-function' for special needs."
1955 (if isearch-search-fun-function
1956 (funcall isearch-search-fun-function)
1959 (if isearch-forward 'word-search-forward 'word-search-backward))
1961 (if isearch-forward 're-search-forward 're-search-backward))
1963 (if isearch-forward 'search-forward 'search-backward)))))
1965 (defun isearch-search ()
1966 ;; Do the search with the current search string.
1967 (isearch-message nil t)
1968 (if (and (eq isearch-case-fold-search t) search-upper-case)
1969 (setq isearch-case-fold-search
1970 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1971 (condition-case lossage
1972 (let ((inhibit-point-motion-hooks search-invisible)
1974 (case-fold-search isearch-case-fold-search)
1976 (if isearch-regexp (setq isearch-invalid-regexp nil))
1977 (setq isearch-within-brackets nil)
1979 (setq isearch-success
1981 (isearch-search-fun)
1982 isearch-string nil t))
1983 ;; Clear RETRY unless we matched some invisible text
1984 ;; and we aren't supposed to do that.
1985 (if (or (eq search-invisible t)
1986 (not isearch-success)
1988 (= (match-beginning 0) (match-end 0))
1989 (not (isearch-range-invisible
1990 (match-beginning 0) (match-end 0))))
1992 (setq isearch-just-started nil)
1994 (setq isearch-other-end
1995 (if isearch-forward (match-beginning 0) (match-end 0)))))
1997 (quit (isearch-unread ?\C-g)
1998 (setq isearch-success nil))
2001 (setq isearch-invalid-regexp (car (cdr lossage)))
2002 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
2003 isearch-invalid-regexp))
2005 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
2006 isearch-invalid-regexp)
2007 (setq isearch-invalid-regexp "incomplete input")))
2009 ;; stack overflow in regexp search.
2010 (setq isearch-invalid-regexp (format "%s" lossage))))
2014 ;; Ding if failed this time after succeeding last time.
2015 (and (isearch-success (car isearch-cmds))
2017 (goto-char (isearch-point (car isearch-cmds)))))
2020 ;; Called when opening an overlay, and we are still in isearch.
2021 (defun isearch-open-overlay-temporary (ov)
2022 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
2023 ;; Some modes would want to open the overlays temporary during
2024 ;; isearch in their own way, they should set the
2025 ;; `isearch-open-invisible-temporary' to a function doing this.
2026 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2027 ;; Store the values for the `invisible' and `intangible'
2028 ;; properties, and then set them to nil. This way the text hidden
2029 ;; by this overlay becomes visible.
2031 ;; Do we really need to set the `intangible' property to t? Can we
2032 ;; have the point inside an overlay with an `intangible' property?
2033 ;; In 19.34 this does not exist so I cannot test it.
2034 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2035 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
2036 (overlay-put ov 'invisible nil)
2037 (overlay-put ov 'intangible nil)))
2040 ;; This is called at the end of isearch. It will open the overlays
2041 ;; that contain the latest match. Obviously in case of a C-g the
2042 ;; point returns to the original location which surely is not contain
2043 ;; in any of these overlays, se we are safe in this case too.
2044 (defun isearch-open-necessary-overlays (ov)
2045 (let ((inside-overlay (and (> (point) (overlay-start ov))
2046 (< (point) (overlay-end ov))))
2047 ;; If this exists it means that the overlay was opened using
2048 ;; this function, not by us tweaking the overlay properties.
2049 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2050 (when (or inside-overlay (not fct-temp))
2051 ;; restore the values for the `invisible' and `intangible'
2053 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2054 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2055 (overlay-put ov 'isearch-invisible nil)
2056 (overlay-put ov 'isearch-intangible nil))
2058 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2060 (funcall fct-temp ov t)))))
2062 ;; This is called when exiting isearch. It closes the temporary
2063 ;; opened overlays, except the ones that contain the latest match.
2064 (defun isearch-clean-overlays ()
2065 (when isearch-opened-overlays
2066 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
2067 (setq isearch-opened-overlays nil)))
2070 (defun isearch-intersects-p (start0 end0 start1 end1)
2071 "Return t if regions START0..END0 and START1..END1 intersect."
2072 (or (and (>= start0 start1) (< start0 end1))
2073 (and (> end0 start1) (<= end0 end1))
2074 (and (>= start1 start0) (< start1 end0))
2075 (and (> end1 start0) (<= end1 end0))))
2078 ;; Verify if the current match is outside of each element of
2079 ;; `isearch-opened-overlays', if so close that overlay.
2081 (defun isearch-close-unnecessary-overlays (begin end)
2082 (let ((overlays isearch-opened-overlays))
2083 (setq isearch-opened-overlays nil)
2084 (dolist (ov overlays)
2085 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
2086 (push ov isearch-opened-overlays)
2087 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2089 ;; If this exists it means that the overlay was opened
2090 ;; using this function, not by us tweaking the overlay
2092 (funcall fct-temp ov t)
2093 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2094 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2095 (overlay-put ov 'isearch-invisible nil)
2096 (overlay-put ov 'isearch-intangible nil)))))))
2099 (defun isearch-range-invisible (beg end)
2100 "Return t if all the text from BEG to END is invisible."
2102 ;; Check that invisibility runs up to END.
2105 (let (;; can-be-opened keeps track if we can open some overlays.
2106 (can-be-opened (eq search-invisible 'open))
2107 ;; the list of overlays that could be opened
2109 (when (and can-be-opened isearch-hide-immediately)
2110 (isearch-close-unnecessary-overlays beg end))
2111 ;; If the following character is currently invisible,
2112 ;; skip all characters with that same `invisible' property value.
2113 ;; Do that over and over.
2114 (while (and (< (point) end)
2116 (get-char-property (point) 'invisible)))
2117 (if (eq buffer-invisibility-spec t)
2119 (or (memq prop buffer-invisibility-spec)
2120 (assq prop buffer-invisibility-spec)))))
2121 (if (get-text-property (point) 'invisible)
2123 (goto-char (next-single-property-change (point) 'invisible
2125 ;; if text is hidden by an `invisible' text property
2126 ;; we cannot open it at all.
2127 (setq can-be-opened nil))
2129 (let ((overlays (overlays-at (point)))
2134 (setq o (car overlays)
2135 invis-prop (overlay-get o 'invisible))
2136 (if (if (eq buffer-invisibility-spec t)
2138 (or (memq invis-prop buffer-invisibility-spec)
2139 (assq invis-prop buffer-invisibility-spec)))
2140 (if (overlay-get o 'isearch-open-invisible)
2141 (setq ov-list (cons o ov-list))
2142 ;; We found one overlay that cannot be
2143 ;; opened, that means the whole chunk
2144 ;; cannot be opened.
2145 (setq can-be-opened nil)))
2146 (setq overlays (cdr overlays)))
2148 ;; It makes sense to append to the open
2149 ;; overlays list only if we know that this is
2151 (setq crt-overlays (append ov-list crt-overlays)))))
2152 (goto-char (next-overlay-change (point)))))
2153 ;; See if invisibility reaches up thru END.
2154 (if (>= (point) end)
2155 (if (and can-be-opened (consp crt-overlays))
2157 (setq isearch-opened-overlays
2158 (append isearch-opened-overlays crt-overlays))
2159 (mapc 'isearch-open-overlay-temporary crt-overlays)
2161 (setq isearch-hidden t)))))))
2166 (defvar isearch-overlay nil)
2168 (defun isearch-highlight (beg end)
2169 (unless (null search-highlight)
2170 (cond (isearch-overlay
2171 ;; Overlay already exists, just move it.
2172 (move-overlay isearch-overlay beg end (current-buffer)))
2175 ;; Overlay doesn't exist, create it.
2176 (setq isearch-overlay (make-overlay beg end))
2177 (overlay-put isearch-overlay 'face isearch)
2178 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
2181 (defun isearch-dehighlight (totally)
2182 (when isearch-overlay
2183 (delete-overlay isearch-overlay)))
2186 ;; General utilities
2189 (defun isearch-no-upper-case-p (string regexp-flag)
2190 "Return t if there are no upper case chars in STRING.
2191 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
2192 since they have special meaning in a regexp."
2193 (let (quote-flag (i 0) (len (length string)) found)
2194 (while (and (not found) (< i len))
2195 (let ((char (aref string i)))
2196 (if (and regexp-flag (eq char ?\\))
2197 (setq quote-flag (not quote-flag))
2198 (if (and (not quote-flag) (not (eq char (downcase char))))
2200 (setq quote-flag nil)))
2204 ;; Portability functions to support various Emacs versions.
2206 (defun isearch-text-char-description (c)
2208 ((< c ?\ ) (format "^%c" (+ c 64)))
2210 (t (char-to-string c))))
2212 ;; General function to unread characters or events.
2213 ;; Also insert them in a keyboard macro being defined.
2214 (defun isearch-unread (&rest char-or-events)
2215 (mapc 'store-kbd-macro-event char-or-events)
2216 (setq unread-command-events
2217 (append char-or-events unread-command-events)))
2220 ;; isearch-lazy-highlight feature
2221 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
2223 ;; When active, *every* match for the current search string is
2224 ;; highlighted: the current one using the normal isearch match color
2225 ;; and all the others using `isearch-lazy-highlight-face'. The extra
2226 ;; highlighting makes it easier to anticipate where the cursor will
2227 ;; land each time you press C-s or C-r to repeat a pending search.
2228 ;; Highlighting of these additional matches happens in a deferred
2229 ;; fashion using "idle timers," so the cycles needed do not rob
2230 ;; isearch of its usual snappy response.
2232 ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
2234 ;; - `isearch-update' is expected to be called (at least) every time
2235 ;; the search string or window-start changes;
2236 ;; - `isearch-string' is expected to contain the current search
2237 ;; string as entered by the user;
2238 ;; - the type of the current search is expected to be given by
2239 ;; `isearch-word' and `isearch-regexp';
2240 ;; - the direction of the current search is expected to be given by
2241 ;; `isearch-forward';
2242 ;; - the variable `isearch-invalid-regexp' is expected to be true
2243 ;; iff `isearch-string' is an invalid regexp.
2245 (defgroup isearch-lazy-highlight nil
2246 "Lazy highlighting feature for incremental search."
2247 :prefix "isearch-lazy-highlight-"
2251 (defcustom isearch-lazy-highlight t
2252 "*Controls the lazy-highlighting during incremental searches.
2253 When non-nil, all text in the buffer matching the current search
2254 string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
2255 and `isearch-lazy-highlight-interval')."
2257 :group 'isearch-lazy-highlight)
2259 (defcustom isearch-lazy-highlight-cleanup t
2260 "*Controls whether to remove extra highlighting after a search.
2261 If this is nil, extra highlighting can be \"manually\" removed with
2262 \\[isearch-lazy-highlight-cleanup]."
2264 :group 'isearch-lazy-highlight)
2266 (defcustom isearch-lazy-highlight-initial-delay 0.25
2267 "*Seconds to wait before beginning to lazily highlight all matches."
2269 :group 'isearch-lazy-highlight)
2271 (defcustom isearch-lazy-highlight-interval 0 ; 0.0625
2272 "*Seconds between lazily highlighting successive matches."
2274 :group 'isearch-lazy-highlight)
2276 (defcustom isearch-lazy-highlight-max-at-a-time 20
2277 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
2278 Larger values may reduce isearch's responsiveness to user input;
2279 smaller values make matches highlight slowly.
2280 A value of nil means highlight all matches."
2281 :type '(choice (const :tag "All" nil)
2282 (integer :tag "Some"))
2283 :group 'isearch-lazy-highlight)
2285 (defgroup isearch-faces nil
2286 "Lazy highlighting feature for incremental search."
2291 '((((class color) (min-colors 88) (background light))
2292 ;; The background must not be too dark, for that means
2293 ;; the character is hard to see when the cursor is there.
2294 (:background "magenta2" :foreground "lightskyblue1"))
2295 (((class color) (min-colors 88) (background dark))
2296 (:background "palevioletred2" :foreground "brown4"))
2297 (((class color) (min-colors 16))
2298 (:background "magenta4" :foreground "cyan1"))
2299 (((class color) (min-colors 8))
2300 (:background "magenta4" :foreground "cyan1"))
2301 (t (:inverse-video t)))
2302 "Face for highlighting Isearch matches."
2303 :group 'isearch-faces)
2304 (defvar isearch 'isearch)
2306 (defface isearch-lazy-highlight-face
2307 '((((class color) (min-colors 88) (background light))
2308 (:background "paleturquoise"))
2309 (((class color) (min-colors 88) (background dark))
2310 (:background "paleturquoise4"))
2311 (((class color) (min-colors 16))
2312 (:background "turquoise3"))
2313 (((class color) (min-colors 8))
2314 (:background "turquoise3"))
2316 "Face for lazy highlighting of Isearch matches other than the current one."
2317 :group 'isearch-faces)
2318 (defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
2320 (defvar isearch-lazy-highlight-overlays nil)
2321 (defvar isearch-lazy-highlight-wrapped nil)
2322 (defvar isearch-lazy-highlight-start nil)
2323 (defvar isearch-lazy-highlight-end nil)
2324 (defvar isearch-lazy-highlight-timer nil)
2325 (defvar isearch-lazy-highlight-last-string nil)
2326 (defvar isearch-lazy-highlight-window nil)
2327 (defvar isearch-lazy-highlight-window-start nil)
2328 (defvar isearch-lazy-highlight-window-end nil)
2329 (defvar isearch-lazy-highlight-case-fold-search nil)
2330 (defvar isearch-lazy-highlight-regexp nil)
2332 (defun isearch-lazy-highlight-cleanup (&optional force)
2333 "Stop lazy highlighting and remove extra highlighting from current buffer.
2334 FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
2335 is nil. This function is called when exiting an incremental search if
2336 `isearch-lazy-highlight-cleanup' is non-nil."
2338 (if (or force isearch-lazy-highlight-cleanup)
2339 (while isearch-lazy-highlight-overlays
2340 (delete-overlay (car isearch-lazy-highlight-overlays))
2341 (setq isearch-lazy-highlight-overlays
2342 (cdr isearch-lazy-highlight-overlays))))
2343 (when isearch-lazy-highlight-timer
2344 (cancel-timer isearch-lazy-highlight-timer)
2345 (setq isearch-lazy-highlight-timer nil)))
2347 (defun isearch-lazy-highlight-new-loop ()
2348 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
2349 This happens when `isearch-update' is invoked (which can cause the
2350 search string to change or the window to scroll)."
2351 (when (and isearch-lazy-highlight
2352 (null executing-kbd-macro)
2353 (sit-for 0) ;make sure (window-start) is credible
2354 (or (not (equal isearch-string
2355 isearch-lazy-highlight-last-string))
2356 (not (eq (selected-window)
2357 isearch-lazy-highlight-window))
2358 (not (eq isearch-lazy-highlight-case-fold-search
2359 isearch-case-fold-search))
2360 (not (eq isearch-lazy-highlight-regexp
2362 (not (= (window-start)
2363 isearch-lazy-highlight-window-start))
2364 (not (= (window-end) ; Window may have been split/joined.
2365 isearch-lazy-highlight-window-end))))
2366 ;; something important did indeed change
2367 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
2368 (when (not isearch-invalid-regexp)
2369 (setq isearch-lazy-highlight-window (selected-window)
2370 isearch-lazy-highlight-window-start (window-start)
2371 isearch-lazy-highlight-window-end (window-end)
2372 isearch-lazy-highlight-start (point)
2373 isearch-lazy-highlight-end (point)
2374 isearch-lazy-highlight-last-string isearch-string
2375 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2376 isearch-lazy-highlight-regexp isearch-regexp
2377 isearch-lazy-highlight-wrapped nil)
2378 (setq isearch-lazy-highlight-timer
2379 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
2380 'isearch-lazy-highlight-update)))))
2382 (defun isearch-lazy-highlight-search ()
2383 "Search ahead for the next or previous match, for lazy highlighting.
2384 Attempt to do the search exactly the way the pending isearch would."
2385 (let ((case-fold-search isearch-case-fold-search))
2386 (funcall (isearch-search-fun)
2389 (if isearch-lazy-highlight-wrapped
2390 isearch-lazy-highlight-start
2392 (if isearch-lazy-highlight-wrapped
2393 isearch-lazy-highlight-end
2397 (defun isearch-lazy-highlight-update ()
2398 "Update highlighting of other matches for current search."
2399 (let ((max isearch-lazy-highlight-max-at-a-time)
2404 (goto-char (if isearch-forward
2405 isearch-lazy-highlight-end
2406 isearch-lazy-highlight-start))
2408 (let ((found (isearch-lazy-highlight-search)))
2412 (setq looping nil)))
2414 (let ((mb (match-beginning 0))
2416 (if (= mb me) ;zero-length match
2419 ;; non-zero-length match
2420 (let ((ov (make-overlay mb me)))
2421 (overlay-put ov 'face isearch-lazy-highlight-face)
2422 (overlay-put ov 'priority 0) ;lower than main overlay
2423 (overlay-put ov 'window (selected-window))
2424 (push ov isearch-lazy-highlight-overlays)))
2426 (setq isearch-lazy-highlight-end (point))
2427 (setq isearch-lazy-highlight-start (point))))
2430 (if isearch-lazy-highlight-wrapped
2433 (setq isearch-lazy-highlight-wrapped t)
2436 (setq isearch-lazy-highlight-end (window-start))
2437 (goto-char (window-start)))
2438 (setq isearch-lazy-highlight-start (window-end))
2439 (goto-char (window-end)))))))
2441 (setq isearch-lazy-highlight-timer
2442 (run-at-time isearch-lazy-highlight-interval nil
2443 'isearch-lazy-highlight-update)))))))
2445 (defun isearch-resume (search regexp word forward message case-fold)
2446 "Resume an incremental search.
2447 SEARCH is the string or regexp searched for.
2448 REGEXP non-nil means the resumed search was a regexp search.
2449 WORD non-nil means resume a word search.
2450 FORWARD non-nil means resume a forward search.
2451 MESSAGE is the echo-area message recorded for the search resumed.
2452 CASE-FOLD non-nil means the search was case-insensitive."
2453 (isearch-mode forward regexp nil nil word)
2454 (setq isearch-string search
2455 isearch-message message
2456 isearch-case-fold-search case-fold)
2459 ;; arch-tag: 74850515-f7d8-43a6-8a2c-ca90a4c1e675
2460 ;;; isearch.el ends here