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