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