Merge from emacs-24; up to 2013-01-03T02:31:36Z!rgm@gnu.org
[emacs.git] / lisp / isearch.el
blob131ab5893d4442245c17e93559cca203255b6552
1 ;;; isearch.el --- incremental search minor mode
3 ;; Copyright (C) 1992-1997, 1999-2013 Free Software Foundation, Inc.
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6 ;; Maintainer: FSF
7 ;; Keywords: matching
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Instructions
29 ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
30 ;; isearch-mode behaves modally and does not return until the search
31 ;; is completed. It uses a recursive-edit to behave this way.
33 ;; The key bindings active within isearch-mode are defined below in
34 ;; `isearch-mode-map' which is given bindings close to the default
35 ;; characters of the original isearch.el. With `isearch-mode',
36 ;; however, you can bind multi-character keys and it should be easier
37 ;; to add new commands. One bug though: keys with meta-prefix cannot
38 ;; be longer than two chars. Also see minibuffer-local-isearch-map
39 ;; for bindings active during `isearch-edit-string'.
41 ;; isearch-mode should work even if you switch windows with the mouse,
42 ;; in which case isearch-mode is terminated automatically before the
43 ;; switch.
45 ;; The search ring and completion commands automatically put you in
46 ;; the minibuffer to edit the string. This gives you a chance to
47 ;; modify the search string before executing the search. There are
48 ;; three commands to terminate the editing: C-s and C-r exit the
49 ;; minibuffer and search forward and reverse respectively, while C-m
50 ;; exits and searches in the last search direction.
52 ;; Exiting immediately from isearch uses isearch-edit-string instead
53 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
54 ;; The name of this option should probably be changed if we decide to
55 ;; keep the behavior. No point in forcing nonincremental search until
56 ;; the last possible moment.
58 ;;; Code:
60 (eval-when-compile (require 'cl-lib))
62 ;; Some additional options and constants.
64 (defgroup isearch nil
65 "Incremental search minor mode."
66 :link '(emacs-commentary-link "isearch")
67 :link '(custom-manual "(emacs)Incremental Search")
68 :prefix "isearch-"
69 :prefix "search-"
70 :group 'matching)
73 (defcustom search-exit-option t
74 "Non-nil means random control characters terminate incremental search."
75 :type 'boolean
76 :group 'isearch)
78 (defcustom search-slow-window-lines 1
79 "Number of lines in slow search display windows.
80 These are the short windows used during incremental search on slow terminals.
81 Negative means put the slow search window at the top (normally it's at bottom)
82 and the value is minus the number of lines."
83 :type 'integer
84 :group 'isearch)
86 (defcustom search-slow-speed 1200
87 "Highest terminal speed at which to use \"slow\" style incremental search.
88 This is the style where a one-line window is created to show the line
89 that the search has reached."
90 :type 'integer
91 :group 'isearch)
93 (defcustom search-upper-case 'not-yanks
94 "If non-nil, upper case chars disable case fold searching.
95 That is, upper and lower case chars must match exactly.
96 This applies no matter where the chars come from, but does not
97 apply to chars in regexps that are prefixed with `\\'.
98 If this value is `not-yanks', text yanked into the search string
99 in Isearch mode is always downcased."
100 :type '(choice (const :tag "off" nil)
101 (const not-yanks)
102 (other :tag "on" t))
103 :group 'isearch)
105 (defcustom search-nonincremental-instead t
106 "If non-nil, do a nonincremental search instead of exiting immediately.
107 Actually, `isearch-edit-string' is called to let you enter the search
108 string, and RET terminates editing and does a nonincremental search."
109 :type 'boolean
110 :group 'isearch)
112 (defcustom search-whitespace-regexp (purecopy "\\s-+")
113 "If non-nil, regular expression to match a sequence of whitespace chars.
114 When you enter a space or spaces in the incremental search, it
115 will match any sequence matched by this regexp. As an exception,
116 spaces are treated normally in regexp incremental search if they
117 occur in a regexp construct like [...] or *, + or ?.
119 If the value is a string, it applies to both ordinary and
120 regexp incremental search. If the value is nil, or
121 `isearch-lax-whitespace' is nil for ordinary incremental search, or
122 `isearch-regexp-lax-whitespace' is nil for regexp incremental search,
123 then each space you type matches literally, against one space.
125 You might want to use something like \"[ \\t\\r\\n]+\" instead.
126 In the Customization buffer, that is `[' followed by a space,
127 a tab, a carriage return (control-M), a newline, and `]+'."
128 :type '(choice (const :tag "Match Spaces Literally" nil)
129 regexp)
130 :group 'isearch
131 :version "24.3")
133 (defcustom search-invisible 'open
134 "If t incremental search can match hidden text.
135 A nil value means don't match invisible text.
136 When the value is `open', if the text matched is made invisible by
137 an overlay having an `invisible' property and that overlay has a property
138 `isearch-open-invisible', then incremental search will show the contents.
139 \(This applies when using `outline.el' and `hideshow.el'.)
140 See also `reveal-mode' if you want overlays to automatically be opened
141 whenever point is in one of them."
142 :type '(choice (const :tag "Match hidden text" t)
143 (const :tag "Open overlays" open)
144 (const :tag "Don't match hidden text" nil))
145 :group 'isearch)
147 (defcustom isearch-hide-immediately t
148 "If non-nil, re-hide an invisible match right away.
149 This variable makes a difference when `search-invisible' is set to `open'.
150 It means that after search makes some invisible text visible
151 to show the match, it makes the text invisible again when the match moves.
152 Ordinarily the text becomes invisible again at the end of the search."
153 :type 'boolean
154 :group 'isearch)
156 (defcustom isearch-resume-in-command-history nil
157 "If non-nil, `isearch-resume' commands are added to the command history.
158 This allows you to resume earlier Isearch sessions through the
159 command history."
160 :type 'boolean
161 :group 'isearch)
163 (defvar isearch-mode-hook nil
164 "Function(s) to call after starting up an incremental search.")
166 (defvar isearch-update-post-hook nil
167 "Function(s) to call after isearch has found matches in the buffer.")
169 (defvar isearch-mode-end-hook nil
170 "Function(s) to call after terminating an incremental search.
171 When these functions are called, `isearch-mode-end-hook-quit'
172 is non-nil if the user quits the search.")
174 (defvar isearch-mode-end-hook-quit nil
175 "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
177 (defvar isearch-message-function nil
178 "Function to call to display the search prompt.
179 If nil, use function `isearch-message'.")
181 (defvar isearch-wrap-function nil
182 "Function to call to wrap the search when search is failed.
183 If nil, move point to the beginning of the buffer for a forward search,
184 or to the end of the buffer for a backward search.")
186 (defvar isearch-push-state-function nil
187 "Function to save a function restoring the mode-specific Isearch state
188 to the search status stack.")
190 (defvar isearch-filter-predicate #'isearch-filter-visible
191 "Predicate that filter the search hits that would normally be available.
192 Search hits that dissatisfy the predicate are skipped. The function
193 has two arguments: the positions of start and end of text matched by
194 the search. If this function returns nil, continue searching without
195 stopping at this match.
196 If you use `add-function' to modify this variable, you can use the
197 `isearch-message-prefix' advice property to specify the prefix string
198 displayed in the search message.")
200 ;; Search ring.
202 (defvar search-ring nil
203 "List of search string sequences.")
204 (defvar regexp-search-ring nil
205 "List of regular expression search string sequences.")
207 (defcustom search-ring-max 16
208 "Maximum length of search ring before oldest elements are thrown away."
209 :type 'integer
210 :group 'isearch)
211 (defcustom regexp-search-ring-max 16
212 "Maximum length of regexp search ring before oldest elements are thrown away."
213 :type 'integer
214 :group 'isearch)
216 (defvar search-ring-yank-pointer nil
217 "Index in `search-ring' of last string reused.
218 It is nil if none yet.")
219 (defvar regexp-search-ring-yank-pointer nil
220 "Index in `regexp-search-ring' of last string reused.
221 It is nil if none yet.")
223 (defcustom search-ring-update nil
224 "Non-nil if advancing or retreating in the search ring should cause search.
225 Default value, nil, means edit the string instead."
226 :type 'boolean
227 :group 'isearch)
229 ;;; isearch highlight customization.
231 (defcustom search-highlight t
232 "Non-nil means incremental search highlights the current match."
233 :type 'boolean
234 :group 'isearch)
236 (defface isearch
237 '((((class color) (min-colors 88) (background light))
238 ;; The background must not be too dark, for that means
239 ;; the character is hard to see when the cursor is there.
240 (:background "magenta3" :foreground "lightskyblue1"))
241 (((class color) (min-colors 88) (background dark))
242 (:background "palevioletred2" :foreground "brown4"))
243 (((class color) (min-colors 16))
244 (:background "magenta4" :foreground "cyan1"))
245 (((class color) (min-colors 8))
246 (:background "magenta4" :foreground "cyan1"))
247 (t (:inverse-video t)))
248 "Face for highlighting Isearch matches."
249 :group 'isearch
250 :group 'basic-faces)
251 (defvar isearch-face 'isearch)
253 (defface isearch-fail
254 '((((class color) (min-colors 88) (background light))
255 (:background "RosyBrown1"))
256 (((class color) (min-colors 88) (background dark))
257 (:background "red4"))
258 (((class color) (min-colors 16))
259 (:background "red"))
260 (((class color) (min-colors 8))
261 (:background "red"))
262 (((class color grayscale))
263 :foreground "grey")
264 (t (:inverse-video t)))
265 "Face for highlighting failed part in Isearch echo-area message."
266 :version "23.1"
267 :group 'isearch)
269 (defcustom isearch-lazy-highlight t
270 "Controls the lazy-highlighting during incremental search.
271 When non-nil, all text in the buffer matching the current search
272 string is highlighted lazily (see `lazy-highlight-initial-delay'
273 and `lazy-highlight-interval')."
274 :type 'boolean
275 :group 'lazy-highlight
276 :group 'isearch)
278 ;;; Lazy highlight customization.
280 (defgroup lazy-highlight nil
281 "Lazy highlighting feature for matching strings."
282 :prefix "lazy-highlight-"
283 :version "21.1"
284 :group 'isearch
285 :group 'matching)
287 (define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
288 'lazy-highlight-cleanup
289 "22.1")
291 (defcustom lazy-highlight-cleanup t
292 "Controls whether to remove extra highlighting after a search.
293 If this is nil, extra highlighting can be \"manually\" removed with
294 \\[lazy-highlight-cleanup]."
295 :type 'boolean
296 :group 'lazy-highlight)
298 (define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
299 'lazy-highlight-initial-delay
300 "22.1")
302 (defcustom lazy-highlight-initial-delay 0.25
303 "Seconds to wait before beginning to lazily highlight all matches."
304 :type 'number
305 :group 'lazy-highlight)
307 (define-obsolete-variable-alias 'isearch-lazy-highlight-interval
308 'lazy-highlight-interval
309 "22.1")
311 (defcustom lazy-highlight-interval 0 ; 0.0625
312 "Seconds between lazily highlighting successive matches."
313 :type 'number
314 :group 'lazy-highlight)
316 (define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
317 'lazy-highlight-max-at-a-time
318 "22.1")
320 (defcustom lazy-highlight-max-at-a-time 20
321 "Maximum matches to highlight at a time (for `lazy-highlight').
322 Larger values may reduce Isearch's responsiveness to user input;
323 smaller values make matches highlight slowly.
324 A value of nil means highlight all matches."
325 :type '(choice (const :tag "All" nil)
326 (integer :tag "Some"))
327 :group 'lazy-highlight)
329 (defface lazy-highlight
330 '((((class color) (min-colors 88) (background light))
331 (:background "paleturquoise"))
332 (((class color) (min-colors 88) (background dark))
333 (:background "paleturquoise4"))
334 (((class color) (min-colors 16))
335 (:background "turquoise3"))
336 (((class color) (min-colors 8))
337 (:background "turquoise3"))
338 (t (:underline t)))
339 "Face for lazy highlighting of matches other than the current one."
340 :group 'lazy-highlight
341 :group 'basic-faces)
342 (define-obsolete-face-alias 'isearch-lazy-highlight-face 'lazy-highlight "22.1")
343 (define-obsolete-variable-alias 'isearch-lazy-highlight-face
344 'lazy-highlight-face
345 "22.1")
346 (defvar lazy-highlight-face 'lazy-highlight)
348 ;; Define isearch help map.
350 (defvar isearch-help-map
351 (let ((map (make-sparse-keymap)))
352 (define-key map [t] 'isearch-other-control-char)
353 (define-key map (char-to-string help-char) 'isearch-help-for-help)
354 (define-key map [help] 'isearch-help-for-help)
355 (define-key map [f1] 'isearch-help-for-help)
356 (define-key map "?" 'isearch-help-for-help)
357 (define-key map "b" 'isearch-describe-bindings)
358 (define-key map "k" 'isearch-describe-key)
359 (define-key map "m" 'isearch-describe-mode)
360 (define-key map "q" 'help-quit)
361 map)
362 "Keymap for characters following the Help key for Isearch mode.")
364 (eval-when-compile (require 'help-macro))
366 (make-help-screen isearch-help-for-help-internal
367 (purecopy "Type a help option: [bkm] or ?")
368 "You have typed %THIS-KEY%, the help character. Type a Help option:
369 \(Type \\<help-map>\\[help-quit] to exit the Help command.)
371 b Display all Isearch key bindings.
372 k KEYS Display full documentation of Isearch key sequence.
373 m Display documentation of Isearch mode.
375 You can't type here other help keys available in the global help map,
376 but outside of this help window when you type them in Isearch mode,
377 they exit Isearch mode before displaying global help."
378 isearch-help-map)
380 (defvar isearch--display-help-action '(nil (inhibit-same-window . t)))
382 (defun isearch-help-for-help ()
383 "Display Isearch help menu."
384 (interactive)
385 (let ((display-buffer-overriding-action isearch--display-help-action))
386 (isearch-help-for-help-internal))
387 (isearch-update))
389 (defun isearch-describe-bindings ()
390 "Show a list of all keys defined in Isearch mode, and their definitions.
391 This is like `describe-bindings', but displays only Isearch keys."
392 (interactive)
393 (let ((display-buffer-overriding-action isearch--display-help-action))
394 (with-help-window "*Help*"
395 (with-current-buffer standard-output
396 (princ "Isearch Mode Bindings:\n")
397 (princ (substitute-command-keys "\\{isearch-mode-map}"))))))
399 (defun isearch-describe-key ()
400 "Display documentation of the function invoked by isearch key."
401 (interactive)
402 (let ((display-buffer-overriding-action isearch--display-help-action))
403 (call-interactively 'describe-key))
404 (isearch-update))
406 (defun isearch-describe-mode ()
407 "Display documentation of Isearch mode."
408 (interactive)
409 (let ((display-buffer-overriding-action isearch--display-help-action))
410 (describe-function 'isearch-forward))
411 (isearch-update))
413 (defalias 'isearch-mode-help 'isearch-describe-mode)
416 ;; Define isearch-mode keymap.
418 (defvar isearch-mode-map
419 (let ((i 0)
420 (map (make-keymap)))
421 (or (char-table-p (nth 1 map))
422 (error "The initialization of isearch-mode-map must be updated"))
423 ;; Make all multibyte characters search for themselves.
424 (set-char-table-range (nth 1 map) (cons #x100 (max-char))
425 'isearch-printing-char)
426 ;; Make function keys, etc, which aren't bound to a scrolling-function
427 ;; exit the search.
428 (define-key map [t] 'isearch-other-control-char)
430 ;; Single-byte printing chars extend the search string by default.
431 (setq i ?\s)
432 (while (< i 256)
433 (define-key map (vector i) 'isearch-printing-char)
434 (setq i (1+ i)))
436 ;; To handle local bindings with meta char prefix keys, define
437 ;; another full keymap. This must be done for any other prefix
438 ;; keys as well, one full keymap per char of the prefix key. It
439 ;; would be simpler to disable the global keymap, and/or have a
440 ;; default local key binding for any key not otherwise bound.
441 (let ((meta-map (make-sparse-keymap)))
442 (define-key map (char-to-string meta-prefix-char) meta-map)
443 (define-key map [escape] meta-map)
444 (define-key meta-map [t] 'isearch-other-meta-char))
446 ;; Several non-printing chars change the searching behavior.
447 (define-key map "\C-s" 'isearch-repeat-forward)
448 (define-key map "\C-r" 'isearch-repeat-backward)
449 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
450 ;; combinations can be used to repeat regexp isearches that can
451 ;; be used to start these searches.
452 (define-key map "\M-\C-s" 'isearch-repeat-forward)
453 (define-key map "\M-\C-r" 'isearch-repeat-backward)
454 (define-key map "\177" 'isearch-delete-char)
455 (define-key map "\C-g" 'isearch-abort)
457 ;; This assumes \e is the meta-prefix-char.
458 (or (= ?\e meta-prefix-char)
459 (error "Inconsistency in isearch.el"))
460 (define-key map "\e\e\e" 'isearch-cancel)
461 (define-key map [escape escape escape] 'isearch-cancel)
463 (define-key map "\C-q" 'isearch-quote-char)
465 (define-key map "\r" 'isearch-exit)
466 (define-key map "\C-j" 'isearch-printing-char)
467 (define-key map "\t" 'isearch-printing-char)
468 (define-key map [?\S-\ ] 'isearch-printing-char)
470 (define-key map "\C-w" 'isearch-yank-word-or-char)
471 (define-key map "\M-\C-w" 'isearch-del-char)
472 (define-key map "\M-\C-y" 'isearch-yank-char)
473 (define-key map "\C-y" 'isearch-yank-kill)
474 (define-key map "\M-s\C-e" 'isearch-yank-line)
476 (define-key map (char-to-string help-char) isearch-help-map)
477 (define-key map [help] isearch-help-map)
478 (define-key map [f1] isearch-help-map)
480 (define-key map "\M-n" 'isearch-ring-advance)
481 (define-key map "\M-p" 'isearch-ring-retreat)
482 (define-key map "\M-y" 'isearch-yank-pop)
484 (define-key map "\M-\t" 'isearch-complete)
486 ;; Pass frame events transparently so they won't exit the search.
487 ;; In particular, if we have more than one display open, then a
488 ;; switch-frame might be generated by someone typing at another keyboard.
489 (define-key map [switch-frame] nil)
490 (define-key map [delete-frame] nil)
491 (define-key map [iconify-frame] nil)
492 (define-key map [make-frame-visible] nil)
493 (define-key map [mouse-movement] nil)
494 (define-key map [language-change] nil)
496 ;; For searching multilingual text.
497 (define-key map "\C-\\" 'isearch-toggle-input-method)
498 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
500 ;; People expect to be able to paste with the mouse.
501 (define-key map [mouse-2] #'isearch-mouse-2)
502 (define-key map [down-mouse-2] nil)
504 ;; Some bindings you may want to put in your isearch-mode-hook.
505 ;; Suggest some alternates...
506 (define-key map "\M-c" 'isearch-toggle-case-fold)
507 (define-key map "\M-r" 'isearch-toggle-regexp)
508 (define-key map "\M-e" 'isearch-edit-string)
510 (define-key map "\M-sc" 'isearch-toggle-case-fold)
511 (define-key map "\M-si" 'isearch-toggle-invisible)
512 (define-key map "\M-sr" 'isearch-toggle-regexp)
513 (define-key map "\M-sw" 'isearch-toggle-word)
514 (define-key map "\M-s_" 'isearch-toggle-symbol)
515 (define-key map "\M-s " 'isearch-toggle-lax-whitespace)
517 (define-key map [?\M-%] 'isearch-query-replace)
518 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
519 (define-key map "\M-so" 'isearch-occur)
520 (define-key map "\M-shr" 'isearch-highlight-regexp)
522 ;; The key translations defined in the C-x 8 prefix should add
523 ;; characters to the search string. See iso-transl.el.
524 (define-key map "\C-x" nil)
525 (define-key map [?\C-x t] 'isearch-other-control-char)
526 (define-key map "\C-x8" nil)
527 (define-key map "\C-x8\r" 'isearch-char-by-name)
529 map)
530 "Keymap for `isearch-mode'.")
532 (defvar minibuffer-local-isearch-map
533 (let ((map (make-sparse-keymap)))
534 (set-keymap-parent map minibuffer-local-map)
535 (define-key map "\r" 'exit-minibuffer)
536 (define-key map "\M-\t" 'isearch-complete-edit)
537 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
538 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
539 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
540 (define-key map [right] 'isearch-yank-char-in-minibuffer)
541 map)
542 "Keymap for editing Isearch strings in the minibuffer.")
544 ;; Internal variables declared globally for byte-compiler.
545 ;; These are all set with setq while isearching
546 ;; and bound locally while editing the search string.
548 (defvar isearch-forward nil) ; Searching in the forward direction.
549 (defvar isearch-regexp nil) ; Searching for a regexp.
550 (defvar isearch-word nil
551 "Regexp-based search mode for words/symbols.
552 If t, do incremental search for a sequence of words, ignoring punctuation.
553 If the value is a function (e.g. `isearch-symbol-regexp'), it is called to
554 convert the search string to a regexp used by regexp search functions.
555 The property `isearch-message-prefix' put on this function specifies the
556 prefix string displayed in the search message.")
558 (defvar isearch-lax-whitespace t
559 "If non-nil, a space will match a sequence of whitespace chars.
560 When you enter a space or spaces in ordinary incremental search, it
561 will match any sequence matched by the regexp defined by the variable
562 `search-whitespace-regexp'. If the value is nil, each space you type
563 matches literally, against one space. You can toggle the value of this
564 variable by the command `isearch-toggle-lax-whitespace'.")
566 (defvar isearch-regexp-lax-whitespace nil
567 "If non-nil, a space will match a sequence of whitespace chars.
568 When you enter a space or spaces in regexp incremental search, it
569 will match any sequence matched by the regexp defined by the variable
570 `search-whitespace-regexp'. If the value is nil, each space you type
571 matches literally, against one space. You can toggle the value of this
572 variable by the command `isearch-toggle-lax-whitespace'.")
574 (defvar isearch-cmds nil
575 "Stack of search status sets.
576 Each set is a vector of the form:
577 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
578 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
580 (defvar isearch-string "") ; The current search string.
581 (defvar isearch-message "") ; text-char-description version of isearch-string
583 (defvar isearch-message-prefix-add nil) ; Additional text for the message prefix
584 (defvar isearch-message-suffix-add nil) ; Additional text for the message suffix
586 (defvar isearch-success t) ; Searching is currently successful.
587 (defvar isearch-error nil) ; Error message for failed search.
588 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
589 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
590 (defvar isearch-barrier 0
591 "Recorded minimum/maximal point for the current search.")
592 (defvar isearch-just-started nil)
593 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
595 ;; case-fold-search while searching.
596 ;; either nil, t, or 'yes. 'yes means the same as t except that mixed
597 ;; case in the search string is ignored.
598 (defvar isearch-case-fold-search nil)
600 ;; search-invisible while searching.
601 ;; either nil, t, or 'open. 'open means the same as t except that
602 ;; opens hidden overlays.
603 (defvar isearch-invisible search-invisible)
605 (defvar isearch-last-case-fold-search nil)
607 ;; Used to save default value while isearch is active
608 (defvar isearch-original-minibuffer-message-timeout nil)
610 (defvar isearch-adjusted nil)
611 (defvar isearch-slow-terminal-mode nil)
612 ;; If t, using a small window.
613 (defvar isearch-small-window nil)
614 (defvar isearch-opoint 0)
615 ;; The window configuration active at the beginning of the search.
616 (defvar isearch-window-configuration nil)
618 ;; Flag to indicate a yank occurred, so don't move the cursor.
619 (defvar isearch-yank-flag nil)
621 ;; A function to be called after each input character is processed.
622 ;; (It is not called after characters that exit the search.)
623 ;; It is only set from an optional argument to `isearch-mode'.
624 (defvar isearch-op-fun nil)
626 ;; Is isearch-mode in a recursive edit for modal searching.
627 (defvar isearch-recursive-edit nil)
629 ;; Should isearch be terminated after doing one search?
630 (defvar isearch-nonincremental nil)
632 ;; New value of isearch-forward after isearch-edit-string.
633 (defvar isearch-new-forward nil)
635 ;; Accumulate here the overlays opened during searching.
636 (defvar isearch-opened-overlays nil)
638 ;; Non-nil if the string exists but is invisible.
639 (defvar isearch-hidden nil)
641 ;; The value of input-method-function when isearch is invoked.
642 (defvar isearch-input-method-function nil)
644 ;; A flag to tell if input-method-function is locally bound when
645 ;; isearch is invoked.
646 (defvar isearch-input-method-local-p nil)
648 ;; Minor-mode-alist changes - kind of redundant with the
649 ;; echo area, but if isearching in multiple windows, it can be useful.
651 (or (assq 'isearch-mode minor-mode-alist)
652 (nconc minor-mode-alist
653 (list '(isearch-mode isearch-mode))))
655 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
656 (make-variable-buffer-local 'isearch-mode)
658 (define-key global-map "\C-s" 'isearch-forward)
659 (define-key esc-map "\C-s" 'isearch-forward-regexp)
660 (define-key global-map "\C-r" 'isearch-backward)
661 (define-key esc-map "\C-r" 'isearch-backward-regexp)
662 (define-key search-map "w" 'isearch-forward-word)
663 (define-key search-map "_" 'isearch-forward-symbol)
664 (define-key search-map "." 'isearch-forward-symbol-at-point)
666 ;; Entry points to isearch-mode.
668 (defun isearch-forward (&optional regexp-p no-recursive-edit)
670 Do incremental search forward.
671 With a prefix argument, do an incremental regular expression search instead.
672 \\<isearch-mode-map>
673 As you type characters, they add to the search string and are found.
674 The following non-printing keys are bound in `isearch-mode-map'.
676 Type \\[isearch-delete-char] to cancel last input item from end of search string.
677 Type \\[isearch-exit] to exit, leaving point at location found.
678 Type LFD (C-j) to match end of line.
679 Type \\[isearch-repeat-forward] to search again forward,\
680 \\[isearch-repeat-backward] to search again backward.
681 Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
682 onto the end of the search string, and search for it.
683 Type \\[isearch-del-char] to delete character from end of search string.
684 Type \\[isearch-yank-char] to yank char from buffer onto end of search\
685 string and search for it.
686 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
687 and search for it.
688 Type \\[isearch-yank-kill] to yank the last string of killed text.
689 Type \\[isearch-yank-pop] to replace string just yanked into search prompt
690 with string killed before it.
691 Type \\[isearch-quote-char] to quote control character to search for it.
692 Type \\[isearch-char-by-name] to add a character to search by Unicode name,\
693 with completion.
694 \\[isearch-abort] while searching or when search has failed cancels input\
695 back to what has
696 been found successfully.
697 \\[isearch-abort] when search is successful aborts and moves point to\
698 starting point.
700 If you try to exit with the search string still empty, it invokes
701 nonincremental search.
703 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
704 Type \\[isearch-toggle-invisible] to toggle search in invisible text.
705 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
706 Type \\[isearch-toggle-word] to toggle word mode.
707 Type \\[isearch-toggle-symbol] to toggle symbol mode.
709 Type \\[isearch-toggle-lax-whitespace] to toggle whitespace matching.
710 In incremental searches, a space or spaces normally matches any whitespace
711 defined by the variable `search-whitespace-regexp'; see also the variables
712 `isearch-lax-whitespace' and `isearch-regexp-lax-whitespace'.
714 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
716 Also supported is a search ring of the previous 16 search strings.
717 Type \\[isearch-ring-advance] to search for the next item in the search ring.
718 Type \\[isearch-ring-retreat] to search for the previous item in the search\
719 ring.
720 Type \\[isearch-complete] to complete the search string using the search ring.
722 Type \\[isearch-query-replace] to run `query-replace' with string to\
723 replace from last search string.
724 Type \\[isearch-query-replace-regexp] to run `query-replace-regexp'\
725 with the last search string.
726 Type \\[isearch-occur] to run `occur' that shows\
727 the last search string.
728 Type \\[isearch-highlight-regexp] to run `highlight-regexp'\
729 that highlights the last search string.
731 Type \\[isearch-describe-bindings] to display all Isearch key bindings.
732 Type \\[isearch-describe-key] to display documentation of Isearch key.
733 Type \\[isearch-describe-mode] to display documentation of Isearch mode.
735 If an input method is turned on in the current buffer, that input
736 method is also active while you are typing characters to search.
737 To toggle the input method, type \\[isearch-toggle-input-method]. \
738 It also toggles the input
739 method in the current buffer.
741 To use a different input method for searching, type \
742 \\[isearch-toggle-specified-input-method],
743 and specify an input method you want to use.
745 The above keys, bound in `isearch-mode-map', are often controlled by
746 options; do \\[apropos] on search-.* to find them.
747 Other control and meta characters terminate the search
748 and are then executed normally (depending on `search-exit-option').
749 Likewise for function keys and mouse button events.
751 If this function is called non-interactively with a nil NO-RECURSIVE-EDIT,
752 it does not return to the calling function until the search is done.
753 See the function `isearch-mode' for more information."
755 (interactive "P\np")
756 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
758 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
759 "Do incremental search forward for regular expression.
760 With a prefix argument, do a regular string search instead.
761 Like ordinary incremental search except that your input is treated
762 as a regexp. See the command `isearch-forward' for more information.
764 In incremental searches, a space or spaces normally matches any
765 whitespace defined by the variable `search-whitespace-regexp'.
766 To search for a literal space and nothing else, enter C-q SPC.
767 To toggle whitespace matching, use `isearch-toggle-lax-whitespace'."
768 (interactive "P\np")
769 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
771 (defun isearch-forward-word (&optional not-word no-recursive-edit)
772 "Do incremental search forward for a sequence of words.
773 With a prefix argument, do a regular string search instead.
774 Like ordinary incremental search except that your input is treated
775 as a sequence of words without regard to how the words are separated.
776 See the command `isearch-forward' for more information."
777 (interactive "P\np")
778 (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
780 (defun isearch-forward-symbol (&optional _not-symbol no-recursive-edit)
781 "Do incremental search forward for a symbol.
782 The prefix argument is currently unused.
783 Like ordinary incremental search except that your input is treated
784 as a symbol surrounded by symbol boundary constructs \\_< and \\_>.
785 See the command `isearch-forward' for more information."
786 (interactive "P\np")
787 (isearch-mode t nil nil (not no-recursive-edit) 'isearch-symbol-regexp))
789 (defun isearch-backward (&optional regexp-p no-recursive-edit)
790 "Do incremental search backward.
791 With a prefix argument, do a regular expression search instead.
792 See the command `isearch-forward' for more information."
793 (interactive "P\np")
794 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
796 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
797 "Do incremental search backward for regular expression.
798 With a prefix argument, do a regular string search instead.
799 Like ordinary incremental search except that your input is treated
800 as a regexp. See the command `isearch-forward' for more information."
801 (interactive "P\np")
802 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
804 (defun isearch-forward-symbol-at-point ()
805 "Do incremental search forward for a symbol found near point.
806 Like ordinary incremental search except that the symbol found at point
807 is added to the search string initially as a regexp surrounded
808 by symbol boundary constructs \\_< and \\_>.
809 See the command `isearch-forward-symbol' for more information."
810 (interactive)
811 (isearch-forward-symbol nil 1)
812 (let ((bounds (find-tag-default-bounds)))
813 (cond
814 (bounds
815 (when (< (car bounds) (point))
816 (goto-char (car bounds)))
817 (isearch-yank-string
818 (buffer-substring-no-properties (car bounds) (cdr bounds))))
820 (setq isearch-error "No symbol at point")
821 (isearch-update)))))
824 ;; isearch-mode only sets up incremental search for the minor mode.
825 ;; All the work is done by the isearch-mode commands.
827 ;; Not used yet:
828 ;;(defvar isearch-commands '(isearch-forward isearch-backward
829 ;; isearch-forward-regexp isearch-backward-regexp)
830 ;; "List of commands for which isearch-mode does not recursive-edit.")
833 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word)
834 "Start Isearch minor mode.
835 It is called by the function `isearch-forward' and other related functions.
837 The non-nil arg FORWARD means searching in the forward direction.
839 The non-nil arg REGEXP does an incremental regular expression search.
841 The arg OP-FUN is a function to be called after each input character
842 is processed. (It is not called after characters that exit the search.)
844 When the arg RECURSIVE-EDIT is non-nil, this function behaves modally and
845 does not return to the calling function until the search is completed.
846 To behave this way it enters a recursive-edit and exits it when done
847 isearching.
849 The arg WORD, if t, does incremental search for a sequence of words,
850 ignoring punctuation. If the value is a function, it is called to
851 convert the search string to a regexp used by regexp search functions."
853 ;; Initialize global vars.
854 (setq isearch-forward forward
855 isearch-regexp regexp
856 isearch-word word
857 isearch-op-fun op-fun
858 isearch-last-case-fold-search isearch-case-fold-search
859 isearch-case-fold-search case-fold-search
860 isearch-invisible search-invisible
861 isearch-string ""
862 isearch-message ""
863 isearch-cmds nil
864 isearch-success t
865 isearch-wrapped nil
866 isearch-barrier (point)
867 isearch-adjusted nil
868 isearch-yank-flag nil
869 isearch-error nil
870 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
871 (> (window-height)
872 (* 4
873 (abs search-slow-window-lines))))
874 isearch-other-end nil
875 isearch-small-window nil
876 isearch-just-started t
877 isearch-start-hscroll (window-hscroll)
879 isearch-opoint (point)
880 search-ring-yank-pointer nil
881 isearch-opened-overlays nil
882 isearch-input-method-function input-method-function
883 isearch-input-method-local-p (local-variable-p 'input-method-function)
884 regexp-search-ring-yank-pointer nil
886 ;; Save the original value of `minibuffer-message-timeout', and
887 ;; set it to nil so that isearch's messages don't get timed out.
888 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
889 minibuffer-message-timeout nil)
891 ;; We must bypass input method while reading key. When a user type
892 ;; printable character, appropriate input method is turned on in
893 ;; minibuffer to read multibyte characters.
894 (or isearch-input-method-local-p
895 (make-local-variable 'input-method-function))
896 (setq input-method-function nil)
898 (looking-at "")
899 (setq isearch-window-configuration
900 (if isearch-slow-terminal-mode (current-window-configuration) nil))
902 ;; Maybe make minibuffer frame visible and/or raise it.
903 (let ((frame (window-frame (minibuffer-window))))
904 (unless (memq (frame-live-p frame) '(nil t))
905 (unless (frame-visible-p frame)
906 (make-frame-visible frame))
907 (if minibuffer-auto-raise
908 (raise-frame frame))))
910 (setq isearch-mode " Isearch") ;; forward? regexp?
911 (force-mode-line-update)
913 (setq overriding-terminal-local-map isearch-mode-map)
914 (run-hooks 'isearch-mode-hook)
916 ;; Pushing the initial state used to be before running isearch-mode-hook,
917 ;; but a hook might set `isearch-push-state-function' used in
918 ;; `isearch-push-state' to save mode-specific initial state. (Bug#4994)
919 (isearch-push-state)
921 (isearch-update)
923 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
924 (add-hook 'kbd-macro-termination-hook 'isearch-done)
926 ;; isearch-mode can be made modal (in the sense of not returning to
927 ;; the calling function until searching is completed) by entering
928 ;; a recursive-edit and exiting it when done isearching.
929 (if recursive-edit
930 (let ((isearch-recursive-edit t))
931 (recursive-edit)))
932 isearch-success)
935 ;; Some high level utilities. Others below.
937 (defun isearch-update ()
938 "This is called after every isearch command to update the display.
939 The last thing it does is to run `isearch-update-post-hook'."
940 (if (and (null unread-command-events)
941 (null executing-kbd-macro))
942 (progn
943 (if (not (input-pending-p))
944 (if isearch-message-function
945 (funcall isearch-message-function)
946 (isearch-message)))
947 (if (and isearch-slow-terminal-mode
948 (not (or isearch-small-window
949 (pos-visible-in-window-p))))
950 (let ((found-point (point)))
951 (setq isearch-small-window t)
952 (move-to-window-line 0)
953 (let ((window-min-height 1))
954 (split-window nil (if (< search-slow-window-lines 0)
955 (1+ (- search-slow-window-lines))
956 (- (window-height)
957 (1+ search-slow-window-lines)))))
958 (if (< search-slow-window-lines 0)
959 (progn (vertical-motion (- 1 search-slow-window-lines))
960 (set-window-start (next-window) (point))
961 (set-window-hscroll (next-window)
962 (window-hscroll))
963 (set-window-hscroll (selected-window) 0))
964 (other-window 1))
965 (goto-char found-point))
966 ;; Keep same hscrolling as at the start of the search when possible
967 (let ((current-scroll (window-hscroll)))
968 (set-window-hscroll (selected-window) isearch-start-hscroll)
969 (unless (pos-visible-in-window-p)
970 (set-window-hscroll (selected-window) current-scroll))))
971 (if isearch-other-end
972 (if (< isearch-other-end (point)) ; isearch-forward?
973 (isearch-highlight isearch-other-end (point))
974 (isearch-highlight (point) isearch-other-end))
975 (isearch-dehighlight))))
976 (setq ;; quit-flag nil not for isearch-mode
977 isearch-adjusted nil
978 isearch-yank-flag nil)
979 (when isearch-lazy-highlight
980 (isearch-lazy-highlight-new-loop))
981 ;; We must prevent the point moving to the end of composition when a
982 ;; part of the composition has just been searched.
983 (setq disable-point-adjustment t)
984 (run-hooks 'isearch-update-post-hook))
986 (defun isearch-done (&optional nopush edit)
987 "Exit Isearch mode.
988 For successful search, pass no args.
989 For a failing search, NOPUSH is t.
990 For going to the minibuffer to edit the search string,
991 NOPUSH is t and EDIT is t."
993 (if isearch-resume-in-command-history
994 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
995 ,isearch-word ,isearch-forward
996 ,isearch-message
997 ',isearch-case-fold-search)))
998 (unless (equal (car command-history) command)
999 (setq command-history (cons command command-history)))))
1001 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
1002 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
1003 (setq isearch-lazy-highlight-start nil)
1005 ;; Called by all commands that terminate isearch-mode.
1006 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
1007 (setq overriding-terminal-local-map nil)
1008 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
1009 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
1010 (isearch-dehighlight)
1011 (lazy-highlight-cleanup lazy-highlight-cleanup)
1012 (let ((found-start (window-start))
1013 (found-point (point)))
1014 (when isearch-window-configuration
1015 (set-window-configuration isearch-window-configuration)
1016 (if isearch-small-window
1017 (goto-char found-point)
1018 ;; set-window-configuration clobbers window-start; restore it.
1019 ;; This has an annoying side effect of clearing the last_modiff
1020 ;; field of the window, which can cause unwanted scrolling,
1021 ;; so don't do it unless truly necessary.
1022 (set-window-start (selected-window) found-start t))))
1024 (setq isearch-mode nil)
1025 (if isearch-input-method-local-p
1026 (setq input-method-function isearch-input-method-function)
1027 (kill-local-variable 'input-method-function))
1029 (force-mode-line-update)
1031 ;; If we ended in the middle of some intangible text,
1032 ;; move to the further end of that intangible text.
1033 (let ((after (if (eobp) nil
1034 (get-text-property (point) 'intangible)))
1035 (before (if (bobp) nil
1036 (get-text-property (1- (point)) 'intangible))))
1037 (when (and before after (eq before after))
1038 (goto-char
1039 (if isearch-forward
1040 (next-single-property-change (point) 'intangible)
1041 (previous-single-property-change (point) 'intangible)))))
1043 (if (and (> (length isearch-string) 0) (not nopush))
1044 ;; Update the ring data.
1045 (isearch-update-ring isearch-string isearch-regexp))
1047 (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
1048 (run-hooks 'isearch-mode-end-hook))
1050 ;; If there was movement, mark the starting position.
1051 ;; Maybe should test difference between and set mark only if > threshold.
1052 (if (/= (point) isearch-opoint)
1053 (or (and transient-mark-mode mark-active)
1054 (progn
1055 (push-mark isearch-opoint t)
1056 (or executing-kbd-macro (> (minibuffer-depth) 0) edit
1057 (message "Mark saved where search started")))))
1059 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
1061 (defun isearch-update-ring (string &optional regexp)
1062 "Add STRING to the beginning of the search ring.
1063 REGEXP if non-nil says use the regexp search ring."
1064 (add-to-history
1065 (if regexp 'regexp-search-ring 'search-ring)
1066 string
1067 (if regexp regexp-search-ring-max search-ring-max)))
1069 ;; Switching buffers should first terminate isearch-mode.
1070 ;; ;; For Emacs 19, the frame switch event is handled.
1071 ;; (defun isearch-switch-frame-handler ()
1072 ;; (interactive) ;; Is this necessary?
1073 ;; ;; First terminate isearch-mode.
1074 ;; (isearch-done)
1075 ;; (isearch-clean-overlays)
1076 ;; (handle-switch-frame (car (cdr last-command-event))))
1079 ;; The search status structure and stack.
1081 (cl-defstruct (isearch--state
1082 (:constructor nil)
1083 (:copier nil)
1084 (:constructor isearch--get-state
1085 (&aux
1086 (string isearch-string)
1087 (message isearch-message)
1088 (point (point))
1089 (success isearch-success)
1090 (forward isearch-forward)
1091 (other-end isearch-other-end)
1092 (word isearch-word)
1093 (error isearch-error)
1094 (wrapped isearch-wrapped)
1095 (barrier isearch-barrier)
1096 (case-fold-search isearch-case-fold-search)
1097 (pop-fun (if isearch-push-state-function
1098 (funcall isearch-push-state-function))))))
1099 (string :read-only t)
1100 (message :read-only t)
1101 (point :read-only t)
1102 (success :read-only t)
1103 (forward :read-only t)
1104 (other-end :read-only t)
1105 (word :read-only t)
1106 (error :read-only t)
1107 (wrapped :read-only t)
1108 (barrier :read-only t)
1109 (case-fold-search :read-only t)
1110 (pop-fun :read-only t))
1112 (defun isearch--set-state (cmd)
1113 (setq isearch-string (isearch--state-string cmd)
1114 isearch-message (isearch--state-message cmd)
1115 isearch-success (isearch--state-success cmd)
1116 isearch-forward (isearch--state-forward cmd)
1117 isearch-other-end (isearch--state-other-end cmd)
1118 isearch-word (isearch--state-word cmd)
1119 isearch-error (isearch--state-error cmd)
1120 isearch-wrapped (isearch--state-wrapped cmd)
1121 isearch-barrier (isearch--state-barrier cmd)
1122 isearch-case-fold-search (isearch--state-case-fold-search cmd))
1123 (if (functionp (isearch--state-pop-fun cmd))
1124 (funcall (isearch--state-pop-fun cmd) cmd))
1125 (goto-char (isearch--state-point cmd)))
1127 (defun isearch-pop-state ()
1128 (setq isearch-cmds (cdr isearch-cmds))
1129 (isearch--set-state (car isearch-cmds)))
1131 (defun isearch-push-state ()
1132 (push (isearch--get-state) isearch-cmds))
1135 ;; Commands active while inside of the isearch minor mode.
1137 (defun isearch-exit ()
1138 "Exit search normally.
1139 However, if this is the first command after starting incremental
1140 search and `search-nonincremental-instead' is non-nil, do a
1141 nonincremental search instead via `isearch-edit-string'."
1142 (interactive)
1143 (if (and search-nonincremental-instead
1144 (= 0 (length isearch-string)))
1145 (let ((isearch-nonincremental t))
1146 (isearch-edit-string)))
1147 (isearch-done)
1148 (isearch-clean-overlays))
1150 (defvar minibuffer-history-symbol) ;; from external package gmhist.el
1152 (defun isearch-fail-pos (&optional msg)
1153 "Return position of first mismatch in search string, or nil if none.
1154 If MSG is non-nil, use variable `isearch-message', otherwise `isearch-string'."
1155 (let ((cmds isearch-cmds)
1156 (curr-msg (if msg isearch-message isearch-string))
1157 succ-msg)
1158 (when (or (not isearch-success) isearch-error)
1159 (while (and cmds
1160 (or (not (isearch--state-success (car cmds)))
1161 (isearch--state-error (car cmds))))
1162 (pop cmds))
1163 (setq succ-msg (and cmds (if msg (isearch--state-message (car cmds))
1164 (isearch--state-string (car cmds)))))
1165 (if (and (stringp succ-msg)
1166 (< (length succ-msg) (length curr-msg))
1167 (equal succ-msg
1168 (substring curr-msg 0 (length succ-msg))))
1169 (length succ-msg)
1170 0))))
1172 (defmacro with-isearch-suspended (&rest body)
1173 "Exit Isearch mode, run BODY, and reinvoke the pending search.
1174 You can update the global isearch variables by setting new values to
1175 `isearch-new-string', `isearch-new-message', `isearch-new-forward',
1176 `isearch-new-word', `isearch-new-case-fold'."
1177 ;; This code is very hairy for several reasons, explained in the code.
1178 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
1179 ;; If there were a way to catch any change of buffer from the minibuffer,
1180 ;; this could be simplified greatly.
1181 ;; Editing doesn't back up the search point. Should it?
1182 `(condition-case nil
1183 (progn
1184 (let ((isearch-nonincremental isearch-nonincremental)
1186 ;; Locally bind all isearch global variables to protect them
1187 ;; from recursive isearching.
1188 ;; isearch-string -message and -forward are not bound
1189 ;; so they may be changed. Instead, save the values.
1190 (isearch-new-string isearch-string)
1191 (isearch-new-message isearch-message)
1192 (isearch-new-forward isearch-forward)
1193 (isearch-new-word isearch-word)
1194 (isearch-new-case-fold isearch-case-fold-search)
1196 (isearch-regexp isearch-regexp)
1197 (isearch-op-fun isearch-op-fun)
1198 (isearch-cmds isearch-cmds)
1199 (isearch-success isearch-success)
1200 (isearch-wrapped isearch-wrapped)
1201 (isearch-barrier isearch-barrier)
1202 (isearch-adjusted isearch-adjusted)
1203 (isearch-yank-flag isearch-yank-flag)
1204 (isearch-error isearch-error)
1205 ;;; Don't bind this. We want isearch-search, below, to set it.
1206 ;;; And the old value won't matter after that.
1207 ;;; (isearch-other-end isearch-other-end)
1208 ;;; Perhaps some of these other variables should be bound for a
1209 ;;; shorter period, ending before the next isearch-search.
1210 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
1211 (isearch-opoint isearch-opoint)
1212 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
1213 (isearch-small-window isearch-small-window)
1214 (isearch-recursive-edit isearch-recursive-edit)
1215 ;; Save current configuration so we can restore it here.
1216 (isearch-window-configuration (current-window-configuration))
1218 ;; This could protect the index of the search rings,
1219 ;; but we can't reliably count the number of typed M-p
1220 ;; in `read-from-minibuffer' to adjust the index accordingly.
1221 ;; So when the following is commented out, `isearch-mode'
1222 ;; below resets the index to the predictable value nil.
1223 ;; (search-ring-yank-pointer search-ring-yank-pointer)
1224 ;; (regexp-search-ring-yank-pointer regexp-search-ring-yank-pointer)
1226 ;; Temporarily restore `minibuffer-message-timeout'.
1227 (minibuffer-message-timeout
1228 isearch-original-minibuffer-message-timeout)
1229 (isearch-original-minibuffer-message-timeout
1230 isearch-original-minibuffer-message-timeout)
1231 old-point old-other-end)
1233 ;; Actually terminate isearching until editing is done.
1234 ;; This is so that the user can do anything without failure,
1235 ;; like switch buffers and start another isearch, and return.
1236 (condition-case nil
1237 (isearch-done t t)
1238 (exit nil)) ; was recursive editing
1240 ;; Save old point and isearch-other-end before reading from minibuffer
1241 ;; that can change their values.
1242 (setq old-point (point) old-other-end isearch-other-end)
1244 (unwind-protect
1245 (progn ,@body)
1247 ;; Set point at the start (end) of old match if forward (backward),
1248 ;; so after exiting minibuffer isearch resumes at the start (end)
1249 ;; of this match and can find it again.
1250 (if (and old-other-end (eq old-point (point))
1251 (eq isearch-forward isearch-new-forward))
1252 (goto-char old-other-end))
1254 ;; Always resume isearching by restarting it.
1255 (isearch-mode isearch-forward
1256 isearch-regexp
1257 isearch-op-fun
1259 isearch-word)
1261 ;; Copy new local values to isearch globals
1262 (setq isearch-string isearch-new-string
1263 isearch-message isearch-new-message
1264 isearch-forward isearch-new-forward
1265 isearch-word isearch-new-word
1266 isearch-case-fold-search isearch-new-case-fold))
1268 ;; Empty isearch-string means use default.
1269 (when (= 0 (length isearch-string))
1270 (setq isearch-string (or (car (if isearch-regexp
1271 regexp-search-ring
1272 search-ring))
1275 isearch-message
1276 (mapconcat 'isearch-text-char-description
1277 isearch-string ""))
1278 ;; After taking the last element, adjust ring to previous one.
1279 (isearch-ring-adjust1 nil)))
1281 ;; This used to push the state as of before this C-s, but it adds
1282 ;; an inconsistent state where part of variables are from the
1283 ;; previous search (e.g. `isearch-success'), and part of variables
1284 ;; are just entered from the minibuffer (e.g. `isearch-string').
1285 ;; (isearch-push-state)
1287 ;; Reinvoke the pending search.
1288 (isearch-search)
1289 (isearch-push-state) ; this pushes the correct state
1290 (isearch-update)
1291 (if isearch-nonincremental
1292 (progn
1293 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
1294 (isearch-done)
1295 ;; The search done message is confusing when the string
1296 ;; is empty, so erase it.
1297 (if (equal isearch-string "")
1298 (message "")))))
1300 (quit ; handle abort-recursive-edit
1301 (isearch-abort) ;; outside of let to restore outside global values
1304 (defun isearch-edit-string ()
1305 "Edit the search string in the minibuffer.
1306 The following additional command keys are active while editing.
1307 \\<minibuffer-local-isearch-map>
1308 \\[exit-minibuffer] to resume incremental searching with the edited string.
1309 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
1310 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
1311 \\[isearch-complete-edit] to complete the search string using the search ring."
1312 (interactive)
1313 (with-isearch-suspended
1314 (let* ((message-log-max nil)
1315 ;; Don't add a new search string to the search ring here
1316 ;; in `read-from-minibuffer'. It should be added only
1317 ;; by `isearch-update-ring' called from `isearch-done'.
1318 (history-add-new-input nil)
1319 ;; Binding minibuffer-history-symbol to nil is a work-around
1320 ;; for some incompatibility with gmhist.
1321 (minibuffer-history-symbol))
1322 (setq isearch-new-string
1323 (read-from-minibuffer
1324 (isearch-message-prefix nil isearch-nonincremental)
1325 (cons isearch-string (1+ (or (isearch-fail-pos)
1326 (length isearch-string))))
1327 minibuffer-local-isearch-map nil
1328 (if isearch-regexp
1329 (cons 'regexp-search-ring
1330 (1+ (or regexp-search-ring-yank-pointer -1)))
1331 (cons 'search-ring
1332 (1+ (or search-ring-yank-pointer -1))))
1333 nil t)
1334 isearch-new-message
1335 (mapconcat 'isearch-text-char-description
1336 isearch-new-string "")))))
1338 (defun isearch-nonincremental-exit-minibuffer ()
1339 (interactive)
1340 (setq isearch-nonincremental t)
1341 (exit-minibuffer))
1342 ;; Changing the value of `isearch-nonincremental' has no effect here,
1343 ;; because `isearch-edit-string' ignores this change. Thus marked as obsolete.
1344 (make-obsolete 'isearch-nonincremental-exit-minibuffer 'exit-minibuffer "24.4")
1346 (defun isearch-forward-exit-minibuffer ()
1347 "Resume isearching forward from the minibuffer that edits the search string."
1348 (interactive)
1349 (setq isearch-new-forward t)
1350 (exit-minibuffer))
1352 (defun isearch-reverse-exit-minibuffer ()
1353 "Resume isearching backward from the minibuffer that edits the search string."
1354 (interactive)
1355 (setq isearch-new-forward nil)
1356 (exit-minibuffer))
1358 (defun isearch-cancel ()
1359 "Terminate the search and go back to the starting point."
1360 (interactive)
1361 (if (and isearch-push-state-function isearch-cmds)
1362 ;; For defined push-state function, restore the first state.
1363 ;; This calls pop-state function and restores original point.
1364 (let ((isearch-cmds (last isearch-cmds)))
1365 (isearch--set-state (car isearch-cmds)))
1366 (goto-char isearch-opoint))
1367 (isearch-done t) ; Exit isearch..
1368 (isearch-clean-overlays)
1369 (signal 'quit nil)) ; ..and pass on quit signal.
1371 (defun isearch-abort ()
1372 "Abort incremental search mode if searching is successful, signaling quit.
1373 Otherwise, revert to previous successful search and continue searching.
1374 Use `isearch-exit' to quit without signaling."
1375 (interactive)
1376 ;; (ding) signal instead below, if quitting
1377 (discard-input)
1378 (if (and isearch-success (not isearch-error))
1379 ;; If search is successful and has no incomplete regexp,
1380 ;; move back to starting point and really do quit.
1381 (progn
1382 (setq isearch-success nil)
1383 (isearch-cancel))
1384 ;; If search is failing, or has an incomplete regexp,
1385 ;; rub out until it is once more successful.
1386 (while (or (not isearch-success) isearch-error)
1387 (isearch-pop-state))
1388 (isearch-update)))
1390 (defun isearch-repeat (direction)
1391 ;; Utility for isearch-repeat-forward and -backward.
1392 (if (eq isearch-forward (eq direction 'forward))
1393 ;; C-s in forward or C-r in reverse.
1394 (if (equal isearch-string "")
1395 ;; If search string is empty, use last one.
1396 (if (null (if isearch-regexp regexp-search-ring search-ring))
1397 (setq isearch-error "No previous search string")
1398 (setq isearch-string
1399 (car (if isearch-regexp regexp-search-ring search-ring))
1400 isearch-message
1401 (mapconcat 'isearch-text-char-description
1402 isearch-string "")
1403 isearch-case-fold-search isearch-last-case-fold-search)
1404 ;; After taking the last element, adjust ring to previous one.
1405 (isearch-ring-adjust1 nil))
1406 ;; If already have what to search for, repeat it.
1407 (or isearch-success
1408 (progn
1409 ;; Set isearch-wrapped before calling isearch-wrap-function
1410 (setq isearch-wrapped t)
1411 (if isearch-wrap-function
1412 (funcall isearch-wrap-function)
1413 (goto-char (if isearch-forward (point-min) (point-max)))))))
1414 ;; C-s in reverse or C-r in forward, change direction.
1415 (setq isearch-forward (not isearch-forward)
1416 isearch-success t))
1418 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1420 (if (equal isearch-string "")
1421 (setq isearch-success t)
1422 (if (and isearch-success
1423 (equal (point) isearch-other-end)
1424 (not isearch-just-started))
1425 ;; If repeating a search that found
1426 ;; an empty string, ensure we advance.
1427 (if (if isearch-forward (eobp) (bobp))
1428 ;; If there's nowhere to advance to, fail (and wrap next time).
1429 (progn
1430 (setq isearch-success nil)
1431 (ding))
1432 (forward-char (if isearch-forward 1 -1))
1433 (isearch-search))
1434 (isearch-search)))
1436 (isearch-push-state)
1437 (isearch-update))
1439 (defun isearch-repeat-forward ()
1440 "Repeat incremental search forwards."
1441 (interactive)
1442 (isearch-repeat 'forward))
1444 (defun isearch-repeat-backward ()
1445 "Repeat incremental search backwards."
1446 (interactive)
1447 (isearch-repeat 'backward))
1449 (defun isearch-toggle-regexp ()
1450 "Toggle regexp searching on or off."
1451 ;; The status stack is left unchanged.
1452 (interactive)
1453 (setq isearch-regexp (not isearch-regexp))
1454 (if isearch-regexp (setq isearch-word nil))
1455 (setq isearch-success t isearch-adjusted t)
1456 (isearch-update))
1458 (defun isearch-toggle-word ()
1459 "Toggle word searching on or off."
1460 ;; The status stack is left unchanged.
1461 (interactive)
1462 (setq isearch-word (not isearch-word))
1463 (if isearch-word (setq isearch-regexp nil))
1464 (setq isearch-success t isearch-adjusted t)
1465 (isearch-update))
1467 (defun isearch-toggle-symbol ()
1468 "Toggle symbol searching on or off."
1469 (interactive)
1470 (setq isearch-word (unless (eq isearch-word 'isearch-symbol-regexp)
1471 'isearch-symbol-regexp))
1472 (if isearch-word (setq isearch-regexp nil))
1473 (setq isearch-success t isearch-adjusted t)
1474 (isearch-update))
1476 (defun isearch-toggle-lax-whitespace ()
1477 "Toggle whitespace matching in searching on or off.
1478 In ordinary search, toggles the value of the variable
1479 `isearch-lax-whitespace'. In regexp search, toggles the
1480 value of the variable `isearch-regexp-lax-whitespace'."
1481 (interactive)
1482 (if isearch-regexp
1483 (setq isearch-regexp-lax-whitespace (not isearch-regexp-lax-whitespace))
1484 (setq isearch-lax-whitespace (not isearch-lax-whitespace)))
1485 (let ((message-log-max nil))
1486 (message "%s%s [%s]"
1487 (isearch-message-prefix nil isearch-nonincremental)
1488 isearch-message
1489 (if (if isearch-regexp
1490 isearch-regexp-lax-whitespace
1491 isearch-lax-whitespace)
1492 "match spaces loosely"
1493 "match spaces literally")))
1494 (setq isearch-success t isearch-adjusted t)
1495 (sit-for 1)
1496 (isearch-update))
1498 (defun isearch-toggle-case-fold ()
1499 "Toggle case folding in searching on or off.
1500 Toggles the value of the variable `isearch-case-fold-search'."
1501 (interactive)
1502 (setq isearch-case-fold-search
1503 (if isearch-case-fold-search nil 'yes))
1504 (let ((message-log-max nil))
1505 (message "%s%s [case %ssensitive]"
1506 (isearch-message-prefix nil isearch-nonincremental)
1507 isearch-message
1508 (if isearch-case-fold-search "in" "")))
1509 (setq isearch-success t isearch-adjusted t)
1510 (sit-for 1)
1511 (isearch-update))
1513 (defun isearch-toggle-invisible ()
1514 "Toggle searching in invisible text on or off.
1515 Toggles the variable `isearch-invisible' between values
1516 nil and a non-nil value of the option `search-invisible'
1517 \(or `open' if `search-invisible' is nil)."
1518 (interactive)
1519 (setq isearch-invisible
1520 (if isearch-invisible nil (or search-invisible 'open)))
1521 (let ((message-log-max nil))
1522 (message "%s%s [match %svisible text]"
1523 (isearch-message-prefix nil isearch-nonincremental)
1524 isearch-message
1525 (if isearch-invisible "in" "")))
1526 (setq isearch-success t isearch-adjusted t)
1527 (sit-for 1)
1528 (isearch-update))
1531 ;; Word search
1533 (defun word-search-regexp (string &optional lax)
1534 "Return a regexp which matches words, ignoring punctuation.
1535 Given STRING, a string of words separated by word delimiters,
1536 compute a regexp that matches those exact words separated by
1537 arbitrary punctuation. If the string begins or ends in whitespace,
1538 the beginning or the end of the string matches arbitrary whitespace.
1539 Otherwise if LAX is non-nil, the beginning or the end of the string
1540 need not match a word boundary.
1542 Used in `word-search-forward', `word-search-backward',
1543 `word-search-forward-lax', `word-search-backward-lax'."
1544 (cond
1545 ((equal string "") "")
1546 ((string-match-p "\\`\\W+\\'" string) "\\W+")
1547 (t (concat
1548 (if (string-match-p "\\`\\W" string) "\\W+"
1549 (unless lax "\\<"))
1550 (mapconcat 'regexp-quote (split-string string "\\W+" t) "\\W+")
1551 (if (string-match-p "\\W\\'" string) "\\W+"
1552 (unless lax "\\>"))))))
1554 (defun word-search-backward (string &optional bound noerror count)
1555 "Search backward from point for STRING, ignoring differences in punctuation.
1556 Set point to the beginning of the occurrence found, and return point.
1557 An optional second argument bounds the search; it is a buffer position.
1558 The match found must not extend before that position.
1559 Optional third argument, if t, means if fail just return nil (no error).
1560 If not nil and not t, move to limit of search and return nil.
1561 Optional fourth argument is repeat count--search for successive occurrences.
1563 Relies on the function `word-search-regexp' to convert a sequence
1564 of words in STRING to a regexp used to search words without regard
1565 to punctuation."
1566 (interactive "sWord search backward: ")
1567 (re-search-backward (word-search-regexp string nil) bound noerror count))
1569 (defun word-search-forward (string &optional bound noerror count)
1570 "Search forward from point for STRING, ignoring differences in punctuation.
1571 Set point to the end of the occurrence found, and return point.
1572 An optional second argument bounds the search; it is a buffer position.
1573 The match found must not extend after that position.
1574 Optional third argument, if t, means if fail just return nil (no error).
1575 If not nil and not t, move to limit of search and return nil.
1576 Optional fourth argument is repeat count--search for successive occurrences.
1578 Relies on the function `word-search-regexp' to convert a sequence
1579 of words in STRING to a regexp used to search words without regard
1580 to punctuation."
1581 (interactive "sWord search: ")
1582 (re-search-forward (word-search-regexp string nil) bound noerror count))
1584 (defun word-search-backward-lax (string &optional bound noerror count)
1585 "Search backward from point for STRING, ignoring differences in punctuation.
1586 Set point to the beginning of the occurrence found, and return point.
1588 Unlike `word-search-backward', the end of STRING need not match a word
1589 boundary, unless STRING ends in whitespace.
1591 An optional second argument bounds the search; it is a buffer position.
1592 The match found must not extend before that position.
1593 Optional third argument, if t, means if fail just return nil (no error).
1594 If not nil and not t, move to limit of search and return nil.
1595 Optional fourth argument is repeat count--search for successive occurrences.
1597 Relies on the function `word-search-regexp' to convert a sequence
1598 of words in STRING to a regexp used to search words without regard
1599 to punctuation."
1600 (interactive "sWord search backward: ")
1601 (re-search-backward (word-search-regexp string t) bound noerror count))
1603 (defun word-search-forward-lax (string &optional bound noerror count)
1604 "Search forward from point for STRING, ignoring differences in punctuation.
1605 Set point to the end of the occurrence found, and return point.
1607 Unlike `word-search-forward', the end of STRING need not match a word
1608 boundary, unless STRING ends in whitespace.
1610 An optional second argument bounds the search; it is a buffer position.
1611 The match found must not extend after that position.
1612 Optional third argument, if t, means if fail just return nil (no error).
1613 If not nil and not t, move to limit of search and return nil.
1614 Optional fourth argument is repeat count--search for successive occurrences.
1616 Relies on the function `word-search-regexp' to convert a sequence
1617 of words in STRING to a regexp used to search words without regard
1618 to punctuation."
1619 (interactive "sWord search: ")
1620 (re-search-forward (word-search-regexp string t) bound noerror count))
1622 ;; Symbol search
1624 (defun isearch-symbol-regexp (string &optional lax)
1625 "Return a regexp which matches STRING as a symbol.
1626 Creates a regexp where STRING is surrounded by symbol delimiters \\_< and \\_>.
1627 If there are more than one symbol, then compute a regexp that matches
1628 those exact symbols separated by non-symbol characters. If the string
1629 begins or ends in whitespace, the beginning or the end of the string
1630 matches arbitrary non-symbol whitespace. Otherwise if LAX is non-nil,
1631 the beginning or the end of the string need not match a symbol boundary."
1632 (let ((not-word-symbol-re
1633 ;; This regexp matches all syntaxes except word and symbol syntax.
1634 ;; FIXME: Replace it with something shorter if possible (bug#14602).
1635 "\\(?:\\s-\\|\\s.\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s/\\|\\s$\\|\\s'\\|\\s<\\|\\s>\\|\\s@\\|\\s!\\|\\s|\\)+"))
1636 (cond
1637 ((equal string "") "")
1638 ((string-match-p (format "\\`%s\\'" not-word-symbol-re) string) not-word-symbol-re)
1639 (t (concat
1640 (if (string-match-p (format "\\`%s" not-word-symbol-re) string) not-word-symbol-re
1641 (unless lax "\\_<"))
1642 (mapconcat 'regexp-quote (split-string string not-word-symbol-re t) not-word-symbol-re)
1643 (if (string-match-p (format "%s\\'" not-word-symbol-re) string) not-word-symbol-re
1644 (unless lax "\\_>")))))))
1646 (put 'isearch-symbol-regexp 'isearch-message-prefix "symbol ")
1648 ;; Search with lax whitespace
1650 (defun search-forward-lax-whitespace (string &optional bound noerror count)
1651 "Search forward for STRING, matching a sequence of whitespace chars."
1652 (let ((search-spaces-regexp search-whitespace-regexp))
1653 (re-search-forward (regexp-quote string) bound noerror count)))
1655 (defun search-backward-lax-whitespace (string &optional bound noerror count)
1656 "Search backward for STRING, matching a sequence of whitespace chars."
1657 (let ((search-spaces-regexp search-whitespace-regexp))
1658 (re-search-backward (regexp-quote string) bound noerror count)))
1660 (defun re-search-forward-lax-whitespace (regexp &optional bound noerror count)
1661 "Search forward for REGEXP, matching a sequence of whitespace chars."
1662 (let ((search-spaces-regexp search-whitespace-regexp))
1663 (re-search-forward regexp bound noerror count)))
1665 (defun re-search-backward-lax-whitespace (regexp &optional bound noerror count)
1666 "Search backward for REGEXP, matching a sequence of whitespace chars."
1667 (let ((search-spaces-regexp search-whitespace-regexp))
1668 (re-search-backward regexp bound noerror count)))
1671 (defun isearch-query-replace (&optional delimited regexp-flag)
1672 "Start `query-replace' with string to replace from last search string.
1673 The arg DELIMITED (prefix arg if interactive), if non-nil, means replace
1674 only matches surrounded by word boundaries. Note that using the prefix arg
1675 is possible only when `isearch-allow-scroll' is non-nil or
1676 `isearch-allow-prefix' is non-nil, and it doesn't always provide the
1677 correct matches for `query-replace', so the preferred way to run word
1678 replacements from Isearch is `M-s w ... M-%'."
1679 (interactive
1680 (list current-prefix-arg))
1681 (barf-if-buffer-read-only)
1682 (if regexp-flag (setq isearch-regexp t))
1683 (let ((case-fold-search isearch-case-fold-search)
1684 ;; set `search-upper-case' to nil to not call
1685 ;; `isearch-no-upper-case-p' in `perform-replace'
1686 (search-upper-case nil)
1687 (search-invisible isearch-invisible)
1688 (replace-lax-whitespace
1689 isearch-lax-whitespace)
1690 (replace-regexp-lax-whitespace
1691 isearch-regexp-lax-whitespace)
1692 ;; Set `isearch-recursive-edit' to nil to prevent calling
1693 ;; `exit-recursive-edit' in `isearch-done' that terminates
1694 ;; the execution of this command when it is non-nil.
1695 ;; We call `exit-recursive-edit' explicitly at the end below.
1696 (isearch-recursive-edit nil))
1697 (isearch-done nil t)
1698 (isearch-clean-overlays)
1699 (if (and isearch-other-end
1700 (< isearch-other-end (point))
1701 (not (and transient-mark-mode mark-active
1702 (< (mark) (point)))))
1703 (goto-char isearch-other-end))
1704 (set query-replace-from-history-variable
1705 (cons isearch-string
1706 (symbol-value query-replace-from-history-variable)))
1707 (perform-replace
1708 isearch-string
1709 (query-replace-read-to
1710 isearch-string
1711 (concat "Query replace"
1712 (if (or delimited isearch-word)
1713 (let* ((symbol (or delimited isearch-word))
1714 (string (and symbol (symbolp symbol)
1715 (get symbol 'isearch-message-prefix))))
1716 (if (stringp string)
1717 ;; Move space from the end to the beginning.
1718 (replace-regexp-in-string "\\(.*\\) \\'" " \\1" string)
1719 " word"))
1721 (if isearch-regexp " regexp" "")
1722 (if (and transient-mark-mode mark-active) " in region" ""))
1723 isearch-regexp)
1724 t isearch-regexp (or delimited isearch-word) nil nil
1725 (if (and transient-mark-mode mark-active) (region-beginning))
1726 (if (and transient-mark-mode mark-active) (region-end))))
1727 (and isearch-recursive-edit (exit-recursive-edit)))
1729 (defun isearch-query-replace-regexp (&optional delimited)
1730 "Start `query-replace-regexp' with string to replace from last search string.
1731 See `isearch-query-replace' for more information."
1732 (interactive
1733 (list current-prefix-arg))
1734 (isearch-query-replace delimited t))
1736 (defun isearch-occur (regexp &optional nlines)
1737 "Run `occur' using the last search string as the regexp.
1738 Interactively, REGEXP is constructed using the search string from the
1739 last search command. NLINES has the same meaning as in `occur'.
1741 If the last search command was a word search, REGEXP is computed from
1742 the search words, ignoring punctuation. If the last search
1743 command was a regular expression search, REGEXP is the regular
1744 expression used in that search. If the last search command searched
1745 for a literal string, REGEXP is constructed by quoting all the special
1746 characters in that string."
1747 (interactive
1748 (let* ((perform-collect (consp current-prefix-arg))
1749 (regexp (cond
1750 ((functionp isearch-word)
1751 (funcall isearch-word isearch-string))
1752 (isearch-word (word-search-regexp isearch-string))
1753 (isearch-regexp isearch-string)
1754 (t (regexp-quote isearch-string)))))
1755 (list regexp
1756 (if perform-collect
1757 ;; Perform collect operation
1758 (if (zerop (regexp-opt-depth regexp))
1759 ;; No subexpression so collect the entire match.
1760 "\\&"
1761 ;; Get the regexp for collection pattern.
1762 (let ((default (car occur-collect-regexp-history))
1763 regexp-collect)
1764 (with-isearch-suspended
1765 (setq regexp-collect
1766 (read-regexp
1767 (format "Regexp to collect (default %s): " default)
1768 default 'occur-collect-regexp-history)))
1769 regexp-collect))
1770 ;; Otherwise normal occur takes numerical prefix argument.
1771 (when current-prefix-arg
1772 (prefix-numeric-value current-prefix-arg))))))
1773 (let ((case-fold-search isearch-case-fold-search)
1774 ;; Set `search-upper-case' to nil to not call
1775 ;; `isearch-no-upper-case-p' in `occur-1'.
1776 (search-upper-case nil)
1777 (search-spaces-regexp
1778 (if (if isearch-regexp
1779 isearch-regexp-lax-whitespace
1780 isearch-lax-whitespace)
1781 search-whitespace-regexp)))
1782 (occur regexp nlines)))
1784 (declare-function hi-lock-read-face-name "hi-lock" ())
1786 (defun isearch-highlight-regexp ()
1787 "Run `highlight-regexp' with regexp from the current search string.
1788 It exits Isearch mode and calls `hi-lock-face-buffer' with its regexp
1789 argument from the last search regexp or a quoted search string,
1790 and reads its face argument using `hi-lock-read-face-name'."
1791 (interactive)
1792 (let (
1793 ;; Set `isearch-recursive-edit' to nil to prevent calling
1794 ;; `exit-recursive-edit' in `isearch-done' that terminates
1795 ;; the execution of this command when it is non-nil.
1796 ;; We call `exit-recursive-edit' explicitly at the end below.
1797 (isearch-recursive-edit nil))
1798 (isearch-done nil t)
1799 (isearch-clean-overlays))
1800 (require 'hi-lock nil t)
1801 (let ((regexp (cond ((functionp isearch-word)
1802 (funcall isearch-word isearch-string))
1803 (isearch-word (word-search-regexp isearch-string))
1804 (isearch-regexp isearch-string)
1805 ((if (and (eq isearch-case-fold-search t)
1806 search-upper-case)
1807 (isearch-no-upper-case-p
1808 isearch-string isearch-regexp)
1809 isearch-case-fold-search)
1810 ;; Turn isearch-string into a case-insensitive
1811 ;; regexp.
1812 (mapconcat
1813 (lambda (c)
1814 (let ((s (string c)))
1815 (if (string-match "[[:alpha:]]" s)
1816 (format "[%s%s]" (upcase s) (downcase s))
1817 (regexp-quote s))))
1818 isearch-string ""))
1819 (t (regexp-quote isearch-string)))))
1820 (hi-lock-face-buffer regexp (hi-lock-read-face-name)))
1821 (and isearch-recursive-edit (exit-recursive-edit)))
1824 (defun isearch-delete-char ()
1825 "Discard last input item and move point back.
1826 Last input means the last character or the last isearch command
1827 that added or deleted characters from the search string,
1828 moved point, toggled regexp mode or case-sensitivity, etc.
1829 If no previous match was done, just beep."
1830 (interactive)
1831 (if (null (cdr isearch-cmds))
1832 (ding)
1833 (isearch-pop-state))
1834 (isearch-update))
1836 (defun isearch-del-char (&optional arg)
1837 "Delete character from end of search string and search again.
1838 Unlike `isearch-delete-char', it only deletes the last character,
1839 but doesn't cancel the effect of other isearch command.
1840 If search string is empty, just beep."
1841 (interactive "p")
1842 (if (= 0 (length isearch-string))
1843 (ding)
1844 (setq isearch-string (substring isearch-string 0
1845 (- (min (or arg 1)
1846 (length isearch-string))))
1847 isearch-message (mapconcat 'isearch-text-char-description
1848 isearch-string "")))
1849 ;; Use the isearch-other-end as new starting point to be able
1850 ;; to find the remaining part of the search string again.
1851 ;; This is like what `isearch-search-and-update' does,
1852 ;; but currently it doesn't support deletion of characters
1853 ;; for the case where unsuccessful search may become successful
1854 ;; by deletion of characters.
1855 (if isearch-other-end (goto-char isearch-other-end))
1856 (isearch-search)
1857 (isearch-push-state)
1858 (isearch-update))
1860 (defun isearch-yank-string (string)
1861 "Pull STRING into search string."
1862 ;; Downcase the string if not supposed to case-fold yanked strings.
1863 (if (and isearch-case-fold-search
1864 (eq 'not-yanks search-upper-case))
1865 (setq string (downcase string)))
1866 (if isearch-regexp (setq string (regexp-quote string)))
1867 ;; Don't move cursor in reverse search.
1868 (setq isearch-yank-flag t)
1869 (isearch-process-search-string
1870 string (mapconcat 'isearch-text-char-description string "")))
1872 (defun isearch-yank-kill ()
1873 "Pull string from kill ring into search string."
1874 (interactive)
1875 (isearch-yank-string (current-kill 0)))
1877 (defun isearch-yank-pop ()
1878 "Replace just-yanked search string with previously killed string."
1879 (interactive)
1880 (if (not (memq last-command '(isearch-yank-kill isearch-yank-pop)))
1881 ;; Fall back on `isearch-yank-kill' for the benefits of people
1882 ;; who are used to the old behavior of `M-y' in isearch mode. In
1883 ;; future, this fallback may be changed if we ever change
1884 ;; `yank-pop' to do something like the kill-ring-browser.
1885 (isearch-yank-kill)
1886 (isearch-pop-state)
1887 (isearch-yank-string (current-kill 1))))
1889 (defun isearch-yank-x-selection ()
1890 "Pull current X selection into search string."
1891 (interactive)
1892 (isearch-yank-string (x-get-selection))
1893 ;; If `x-get-selection' returned the text from the active region,
1894 ;; then it "used" the mark which we should hence deactivate.
1895 (when select-active-regions (deactivate-mark)))
1898 (defun isearch-mouse-2 (click)
1899 "Handle mouse-2 in Isearch mode.
1900 For a click in the echo area, invoke `isearch-yank-x-selection'.
1901 Otherwise invoke whatever the calling mouse-2 command sequence
1902 is bound to outside of Isearch."
1903 (interactive "e")
1904 (let* ((w (posn-window (event-start click)))
1905 (overriding-terminal-local-map nil)
1906 (binding (key-binding (this-command-keys-vector) t)))
1907 (if (and (window-minibuffer-p w)
1908 (not (minibuffer-window-active-p w))) ; in echo area
1909 (isearch-yank-x-selection)
1910 (when (functionp binding)
1911 (call-interactively binding)))))
1913 (defun isearch-yank-internal (jumpform)
1914 "Pull the text from point to the point reached by JUMPFORM.
1915 JUMPFORM is a lambda expression that takes no arguments and returns
1916 a buffer position, possibly having moved point to that position.
1917 For example, it might move point forward by a word and return point,
1918 or it might return the position of the end of the line."
1919 (isearch-yank-string
1920 (save-excursion
1921 (and (not isearch-forward) isearch-other-end
1922 (goto-char isearch-other-end))
1923 (buffer-substring-no-properties (point) (funcall jumpform)))))
1925 (defun isearch-yank-char-in-minibuffer (&optional arg)
1926 "Pull next character from buffer into end of search string in minibuffer."
1927 (interactive "p")
1928 (if (eobp)
1929 (insert
1930 (with-current-buffer (cadr (buffer-list))
1931 (buffer-substring-no-properties
1932 (point) (progn (forward-char arg) (point)))))
1933 (forward-char arg)))
1935 (defun isearch-yank-char (&optional arg)
1936 "Pull next character from buffer into search string."
1937 (interactive "p")
1938 (isearch-yank-internal (lambda () (forward-char arg) (point))))
1940 (declare-function subword-forward "subword" (&optional arg))
1941 (defun isearch-yank-word-or-char ()
1942 "Pull next character, subword or word from buffer into search string.
1943 Subword is used when `subword-mode' is activated. "
1944 (interactive)
1945 (isearch-yank-internal
1946 (lambda ()
1947 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1948 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1949 (if (and (boundp 'subword-mode) subword-mode)
1950 (subword-forward 1)
1951 (forward-word 1))
1952 (forward-char 1)) (point))))
1954 (defun isearch-yank-word (&optional arg)
1955 "Pull next word from buffer into search string."
1956 (interactive "p")
1957 (isearch-yank-internal (lambda () (forward-word arg) (point))))
1959 (defun isearch-yank-line (&optional arg)
1960 "Pull rest of line from buffer into search string."
1961 (interactive "p")
1962 (isearch-yank-internal
1963 (lambda () (let ((inhibit-field-text-motion t))
1964 (line-end-position (if (eolp) (1+ arg) arg))))))
1966 (defun isearch-char-by-name (&optional count)
1967 "Read a character by its Unicode name and add it to the search string.
1968 Completion is available like in `read-char-by-name' used by `insert-char'.
1969 With argument, add COUNT copies of the character."
1970 (interactive "p")
1971 (with-isearch-suspended
1972 (let ((char (read-char-by-name "Add character to search (Unicode name or hex): ")))
1973 (when char
1974 (let ((string (if (and (integerp count) (> count 1))
1975 (make-string count char)
1976 (char-to-string char))))
1977 (setq isearch-new-string (concat isearch-string string)
1978 isearch-new-message (concat isearch-message
1979 (mapconcat 'isearch-text-char-description
1980 string ""))))))))
1982 (defun isearch-search-and-update ()
1983 ;; Do the search and update the display.
1984 (when (or isearch-success
1985 ;; Unsuccessful regexp search may become successful by
1986 ;; addition of characters which make isearch-string valid
1987 isearch-regexp
1988 ;; If the string was found but was completely invisible,
1989 ;; it might now be partly visible, so try again.
1990 (prog1 isearch-hidden (setq isearch-hidden nil)))
1991 ;; In reverse search, adding stuff at
1992 ;; the end may cause zero or many more chars to be
1993 ;; matched, in the string following point.
1994 ;; Allow all those possibilities without moving point as
1995 ;; long as the match does not extend past search origin.
1996 (if (and (not isearch-forward) (not isearch-adjusted)
1997 (condition-case ()
1998 (let ((case-fold-search isearch-case-fold-search))
1999 (if (and (eq case-fold-search t) search-upper-case)
2000 (setq case-fold-search
2001 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2002 (looking-at (cond
2003 ((functionp isearch-word)
2004 (funcall isearch-word isearch-string t))
2005 (isearch-word (word-search-regexp isearch-string t))
2006 (isearch-regexp isearch-string)
2007 (t (regexp-quote isearch-string)))))
2008 (error nil))
2009 (or isearch-yank-flag
2010 (<= (match-end 0)
2011 (min isearch-opoint isearch-barrier))))
2012 (progn
2013 (setq isearch-success t
2014 isearch-error nil
2015 isearch-other-end (match-end 0))
2016 (if (and (eq isearch-case-fold-search t) search-upper-case)
2017 (setq isearch-case-fold-search
2018 (isearch-no-upper-case-p isearch-string isearch-regexp))))
2019 ;; Not regexp, not reverse, or no match at point.
2020 (if (and isearch-other-end (not isearch-adjusted))
2021 (goto-char (if isearch-forward isearch-other-end
2022 (min isearch-opoint
2023 isearch-barrier
2024 (1+ isearch-other-end)))))
2025 (isearch-search)
2027 (isearch-push-state)
2028 (if isearch-op-fun (funcall isearch-op-fun))
2029 (isearch-update))
2032 ;; *, ?, }, and | chars can make a regexp more liberal.
2033 ;; They can make a regexp match sooner or make it succeed instead of failing.
2034 ;; So go back to place last successful search started
2035 ;; or to the last ^S/^R (barrier), whichever is nearer.
2036 ;; + needs no special handling because the string must match at least once.
2038 (defun isearch-backslash (str)
2039 "Return t if STR ends in an odd number of backslashes."
2040 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
2042 (defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
2043 "Return point to previous successful match to allow regexp liberalization.
2044 \\<isearch-mode-map>
2045 Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by \
2046 stopping at `isearch-barrier' as needed.
2048 Do nothing if a backslash is escaping the liberalizing character.
2049 If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
2051 Do nothing if regexp has recently been invalid unless optional
2052 ALLOW-INVALID non-nil.
2054 If optional TO-BARRIER non-nil, ignore previous matches and go exactly
2055 to the barrier."
2056 ;; (eq (not a) (not b)) makes all non-nil values equivalent
2057 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
2058 (not want-backslash))
2059 ;; We have to check 2 stack frames because the last might be
2060 ;; invalid just because of a backslash.
2061 (or (not isearch-error)
2062 (not (isearch--state-error (cadr isearch-cmds)))
2063 allow-invalid))
2064 (if to-barrier
2065 (progn (goto-char isearch-barrier)
2066 (setq isearch-adjusted t))
2067 (let* ((stack isearch-cmds)
2068 (previous (cdr stack)) ; lookbelow in the stack
2069 (frame (car stack)))
2070 ;; Walk down the stack looking for a valid regexp (as of course only
2071 ;; they can be the previous successful match); this conveniently
2072 ;; removes all bracket-sets and groups that might be in the way, as
2073 ;; well as partial \{\} constructs that the code below leaves behind.
2074 ;; Also skip over postfix operators -- though horrid,
2075 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
2076 (while (and previous
2077 (or (isearch--state-error frame)
2078 (let* ((string (isearch--state-string frame))
2079 (lchar (aref string (1- (length string)))))
2080 ;; The operators aren't always operators; check
2081 ;; backslashes. This doesn't handle the case of
2082 ;; operators at the beginning of the regexp not
2083 ;; being special, but then we should fall back to
2084 ;; the barrier anyway because it's all optional.
2085 (if (isearch-backslash
2086 (isearch--state-string (car previous)))
2087 (eq lchar ?\})
2088 (memq lchar '(?* ?? ?+))))))
2089 (setq stack previous previous (cdr previous) frame (car stack)))
2090 (when stack
2091 ;; `stack' now refers the most recent valid regexp that is not at
2092 ;; all optional in its last term. Now dig one level deeper and find
2093 ;; what matched before that.
2094 (let ((last-other-end
2095 (or (and (car previous)
2096 (isearch--state-other-end (car previous)))
2097 isearch-barrier)))
2098 (goto-char (if isearch-forward
2099 (max last-other-end isearch-barrier)
2100 (min last-other-end isearch-barrier)))
2101 (setq isearch-adjusted t)))))))
2103 (defun isearch-unread-key-sequence (keylist)
2104 "Unread the given key-sequence KEYLIST.
2105 Scroll-bar or mode-line events are processed appropriately."
2106 (cancel-kbd-macro-events)
2107 (apply 'isearch-unread keylist)
2108 ;; If the event was a scroll-bar or mode-line click, the event will have
2109 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
2110 ;; it here, because this symbol will be attached to the event again next
2111 ;; time it gets read by read-key-sequence.
2113 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
2114 ;; modify the event anymore in 21 because read_key_sequence no longer
2115 ;; modifies events to produce fake prefix keys.")
2116 (if (and (> (length keylist) 1)
2117 (symbolp (car keylist))
2118 (listp (cadr keylist))
2119 (not (numberp (posn-point
2120 (event-start (cadr keylist) )))))
2121 (pop unread-command-events)))
2123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2124 ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
2126 ;; The idea here is that certain vertical scrolling commands (like C-l
2127 ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
2128 ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
2129 ;; tamper with isearch's state, or move point. It is unacceptable for the
2130 ;; search string to be scrolled out of the current window. If a command
2131 ;; attempts this, we scroll the text back again.
2133 ;; We implement this feature with a property called `isearch-scroll'.
2134 ;; If a command's symbol has the value t for this property or for the
2135 ;; `scroll-command' property, it is a scrolling command. The feature
2136 ;; needs to be enabled by setting the customizable variable
2137 ;; `isearch-allow-scroll' to a non-nil value.
2139 ;; The universal argument commands (e.g. C-u) in simple.el are marked
2140 ;; as scrolling commands, and isearch.el has been amended to allow
2141 ;; prefix arguments to be passed through to scrolling commands. Thus
2142 ;; M-0 C-l will scroll point to the top of the window.
2144 ;; Horizontal scrolling commands are currently not catered for.
2145 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2147 ;; Set the isearch-scroll property on some standard functions:
2148 ;; Scroll-bar functions:
2149 (if (fboundp 'scroll-bar-toolkit-scroll)
2150 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
2151 (if (fboundp 'w32-handle-scroll-bar-event)
2152 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
2154 ;; Commands which scroll the window (some scroll commands
2155 ;; already have the `scroll-command' property on them):
2156 (put 'recenter 'isearch-scroll t)
2157 (put 'recenter-top-bottom 'isearch-scroll t)
2158 (put 'reposition-window 'isearch-scroll t)
2160 ;; Commands which act on the other window
2161 (put 'list-buffers 'isearch-scroll t)
2162 (put 'scroll-other-window 'isearch-scroll t)
2163 (put 'scroll-other-window-down 'isearch-scroll t)
2164 (put 'beginning-of-buffer-other-window 'isearch-scroll t)
2165 (put 'end-of-buffer-other-window 'isearch-scroll t)
2167 ;; Commands which change the window layout
2168 (put 'delete-other-windows 'isearch-scroll t)
2169 (put 'balance-windows 'isearch-scroll t)
2170 (put 'split-window-right 'isearch-scroll t)
2171 (put 'split-window-below 'isearch-scroll t)
2172 (put 'enlarge-window 'isearch-scroll t)
2174 ;; Aliases for split-window-*
2175 (put 'split-window-vertically 'isearch-scroll t)
2176 (put 'split-window-horizontally 'isearch-scroll t)
2178 ;; Universal argument commands
2179 (put 'universal-argument 'isearch-scroll t)
2180 (put 'negative-argument 'isearch-scroll t)
2181 (put 'digit-argument 'isearch-scroll t)
2183 (defcustom isearch-allow-scroll nil
2184 "Whether scrolling is allowed during incremental search.
2185 If non-nil, scrolling commands can be used in Isearch mode.
2186 However, the current match will never scroll offscreen.
2187 If nil, scrolling commands will first cancel Isearch mode."
2188 :type 'boolean
2189 :group 'isearch)
2191 (defcustom isearch-allow-prefix t
2192 "Whether prefix arguments are allowed during incremental search.
2193 If non-nil, entering a prefix argument will not terminate the
2194 search. This option is ignored \(presumed t) when
2195 `isearch-allow-scroll' is set."
2196 :version "24.4"
2197 :type 'boolean
2198 :group 'isearch)
2200 (defun isearch-string-out-of-window (isearch-point)
2201 "Test whether the search string is currently outside of the window.
2202 Return nil if it's completely visible, or if point is visible,
2203 together with as much of the search string as will fit; the symbol
2204 `above' if we need to scroll the text downwards; the symbol `below',
2205 if upwards."
2206 (let ((w-start (window-start))
2207 (w-end (window-end nil t))
2208 (w-L1 (save-excursion (move-to-window-line 1) (point)))
2209 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
2210 start end) ; start and end of search string in buffer
2211 (if isearch-forward
2212 (setq end isearch-point start (or isearch-other-end isearch-point))
2213 (setq start isearch-point end (or isearch-other-end isearch-point)))
2214 (cond ((or (and (>= start w-start) (<= end w-end))
2215 (if isearch-forward
2216 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
2217 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
2218 nil)
2219 ((and (< start w-start)
2220 (< isearch-point w-L-1))
2221 'above)
2222 (t 'below))))
2224 (defun isearch-back-into-window (above isearch-point)
2225 "Scroll the window to bring the search string back into view.
2226 Restore point to ISEARCH-POINT in the process. ABOVE is t when the
2227 search string is above the top of the window, nil when it is beneath
2228 the bottom."
2229 (let (start end)
2230 (if isearch-forward
2231 (setq end isearch-point start (or isearch-other-end isearch-point))
2232 (setq start isearch-point end (or isearch-other-end isearch-point)))
2233 (if above
2234 (progn
2235 (goto-char start)
2236 (recenter 0)
2237 (when (>= isearch-point (window-end nil t))
2238 (goto-char isearch-point)
2239 (recenter -1)))
2240 (goto-char end)
2241 (recenter -1)
2242 (when (< isearch-point (window-start))
2243 (goto-char isearch-point)
2244 (recenter 0))))
2245 (goto-char isearch-point))
2247 (defun isearch-reread-key-sequence-naturally (keylist)
2248 "Reread key sequence KEYLIST with an inactive Isearch-mode keymap.
2249 Return the key sequence as a string/vector."
2250 (isearch-unread-key-sequence keylist)
2251 (let (overriding-terminal-local-map)
2252 ;; This will go through function-key-map, if nec.
2253 ;; The arg DONT-DOWNCASE-LAST prevents premature shift-translation.
2254 (read-key-sequence nil nil t)))
2256 (defun isearch-lookup-scroll-key (key-seq)
2257 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
2258 Otherwise return nil."
2259 (let* ((overriding-terminal-local-map nil)
2260 (binding (key-binding key-seq)))
2261 (and binding (symbolp binding) (commandp binding)
2262 (or (eq (get binding 'isearch-scroll) t)
2263 (eq (get binding 'scroll-command) t))
2264 binding)))
2266 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
2268 (defun isearch-other-meta-char (&optional arg)
2269 "Process a miscellaneous key sequence in Isearch mode.
2271 Try to convert the current key-sequence to something usable in Isearch
2272 mode, either by converting it with `function-key-map', downcasing a
2273 key with C-<upper case>, or finding a \"scrolling command\" bound to
2274 it. \(In the last case, we may have to read more events.) If so,
2275 either unread the converted sequence or execute the command.
2277 Otherwise, if `search-exit-option' is non-nil (the default) unread the
2278 key-sequence and exit the search normally. If it is the symbol
2279 `edit', the search string is edited in the minibuffer and the meta
2280 character is unread so that it applies to editing the string.
2282 ARG is the prefix argument. It will be transmitted through to the
2283 scrolling command or to the command whose key-sequence exits
2284 Isearch mode."
2285 (interactive "P")
2286 (let* ((key (if current-prefix-arg ; not nec the same as ARG
2287 (substring (this-command-keys) universal-argument-num-events)
2288 (this-command-keys)))
2289 (main-event (aref key 0))
2290 (keylist (listify-key-sequence key))
2291 scroll-command isearch-point)
2292 (cond ((and (= (length key) 1)
2293 (let ((lookup (lookup-key local-function-key-map key)))
2294 (not (or (null lookup) (integerp lookup)
2295 (keymapp lookup)))))
2296 ;; Handle a function key that translates into something else.
2297 ;; If the key has a global definition too,
2298 ;; exit and unread the key itself, so its global definition runs.
2299 ;; Otherwise, unread the translation,
2300 ;; so that the translated key takes effect within isearch.
2301 (cancel-kbd-macro-events)
2302 (if (lookup-key global-map key)
2303 (progn
2304 (isearch-done)
2305 (setq prefix-arg arg)
2306 (apply 'isearch-unread keylist))
2307 (setq keylist
2308 (listify-key-sequence
2309 (lookup-key local-function-key-map key)))
2310 (while keylist
2311 (setq key (car keylist))
2312 ;; Handle an undefined shifted printing character
2313 ;; by downshifting it if that makes it printing.
2314 ;; (As read-key-sequence would normally do,
2315 ;; if we didn't have a default definition.)
2316 (if (and (integerp key)
2317 (memq 'shift (event-modifiers key))
2318 (>= key (+ ?\s (- ?\S-a ?a)))
2319 (/= key (+ 127 (- ?\S-a ?a)))
2320 (< key (+ 256 (- ?\S-a ?a))))
2321 (setq key (- key (- ?\S-a ?a))))
2322 ;; If KEY is a printing char, we handle it here
2323 ;; directly to avoid the input method and keyboard
2324 ;; coding system translating it.
2325 (if (and (integerp key)
2326 (>= key ?\s) (/= key 127) (< key 256))
2327 (progn
2328 ;; Ensure that the processed char is recorded in
2329 ;; the keyboard macro, if any (Bug#4894)
2330 (store-kbd-macro-event key)
2331 (isearch-process-search-char key)
2332 (setq keylist (cdr keylist)))
2333 ;; As the remaining keys in KEYLIST can't be handled
2334 ;; here, we must reread them.
2335 (setq prefix-arg arg)
2336 (apply 'isearch-unread keylist)
2337 (setq keylist nil)))))
2339 ;; Handle an undefined shifted control character
2340 ;; by downshifting it if that makes it defined.
2341 ;; (As read-key-sequence would normally do,
2342 ;; if we didn't have a default definition.)
2343 (let ((mods (event-modifiers main-event)))
2344 (and (integerp main-event)
2345 (memq 'shift mods)
2346 (memq 'control mods)
2347 (not (memq (lookup-key isearch-mode-map
2348 (let ((copy (copy-sequence key)))
2349 (aset copy 0
2350 (- main-event
2351 (- ?\C-\S-a ?\C-a)))
2352 copy)
2353 nil)
2354 '(nil
2355 isearch-other-control-char)))))
2356 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
2357 (cancel-kbd-macro-events)
2358 (setq prefix-arg arg)
2359 (apply 'isearch-unread keylist))
2360 ((eq search-exit-option 'edit)
2361 (setq prefix-arg arg)
2362 (apply 'isearch-unread keylist)
2363 (isearch-edit-string))
2364 ;; Handle a scrolling function or prefix argument.
2365 ((progn
2366 (setq key (isearch-reread-key-sequence-naturally keylist)
2367 keylist (listify-key-sequence key)
2368 main-event (aref key 0))
2369 (or (and isearch-allow-scroll
2370 (setq scroll-command (isearch-lookup-scroll-key key)))
2371 (and isearch-allow-prefix
2372 (let (overriding-terminal-local-map)
2373 (setq scroll-command (key-binding key))
2374 (memq scroll-command
2375 '(universal-argument
2376 negative-argument digit-argument))))))
2377 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
2378 ;; complete key sequence, possibly as modified by function-key-map,
2379 ;; not merely the one or two event fragment which invoked
2380 ;; isearch-other-meta-char in the first place.
2381 (setq isearch-point (point))
2382 (setq prefix-arg arg)
2383 (command-execute scroll-command)
2384 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
2385 (if ab-bel
2386 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
2387 (goto-char isearch-point)))
2388 (isearch-update))
2389 ;; A mouse click on the isearch message starts editing the search string
2390 ((and (eq (car-safe main-event) 'down-mouse-1)
2391 (window-minibuffer-p (posn-window (event-start main-event))))
2392 ;; Swallow the up-event.
2393 (read-event)
2394 (isearch-edit-string))
2395 (search-exit-option
2396 (let (window)
2397 (setq prefix-arg arg)
2398 (isearch-unread-key-sequence keylist)
2399 (setq main-event (car unread-command-events))
2401 ;; Don't store special commands in the keyboard macro.
2402 (let (overriding-terminal-local-map)
2403 (when (memq (key-binding key)
2404 '(kmacro-start-macro
2405 kmacro-end-macro kmacro-end-and-call-macro))
2406 (cancel-kbd-macro-events)))
2408 ;; If we got a mouse click event, that event contains the
2409 ;; window clicked on. maybe it was read with the buffer
2410 ;; it was clicked on. If so, that buffer, not the current one,
2411 ;; is in isearch mode. So end the search in that buffer.
2413 ;; ??? I have no idea what this if checks for, but it's
2414 ;; obviously wrong for the case that a down-mouse event
2415 ;; on another window invokes this function. The event
2416 ;; will contain the window clicked on and that window's
2417 ;; buffer is certainly not always in Isearch mode.
2419 ;; Leave the code in, but check for current buffer not
2420 ;; being in Isearch mode for now, until someone tells
2421 ;; what it's really supposed to do.
2423 ;; --gerd 2001-08-10.
2425 (if (and (not isearch-mode)
2426 (listp main-event)
2427 (setq window (posn-window (event-start main-event)))
2428 (windowp window)
2429 (or (> (minibuffer-depth) 0)
2430 (not (window-minibuffer-p window))))
2431 (with-current-buffer (window-buffer window)
2432 (isearch-done)
2433 (isearch-clean-overlays))
2434 (isearch-done)
2435 (isearch-clean-overlays)
2436 (setq prefix-arg arg))))
2437 (t;; otherwise nil
2438 (isearch-process-search-string key key)))))
2440 (defun isearch-quote-char (&optional count)
2441 "Quote special characters for incremental search.
2442 With argument, add COUNT copies of the character."
2443 (interactive "p")
2444 (let ((char (read-quoted-char (isearch-message t))))
2445 ;; Assume character codes 0200 - 0377 stand for characters in some
2446 ;; single-byte character set, and convert them to Emacs
2447 ;; characters.
2448 (if (and isearch-regexp isearch-regexp-lax-whitespace (= char ?\s))
2449 (if (subregexp-context-p isearch-string (length isearch-string))
2450 (isearch-process-search-string "[ ]" " ")
2451 (isearch-process-search-char char count))
2452 ;; This used to assume character codes 0240 - 0377 stand for
2453 ;; characters in some single-byte character set, and converted them
2454 ;; to Emacs characters. But in 23.1 this feature is deprecated
2455 ;; in favor of inserting the corresponding Unicode characters.
2456 ;; (and enable-multibyte-characters
2457 ;; (>= char ?\200)
2458 ;; (<= char ?\377)
2459 ;; (setq char (unibyte-char-to-multibyte char)))
2460 (isearch-process-search-char char count))))
2462 (defun isearch-printing-char (&optional char count)
2463 "Add this ordinary printing CHAR to the search string and search.
2464 With argument, add COUNT copies of the character."
2465 (interactive (list last-command-event
2466 (prefix-numeric-value current-prefix-arg)))
2467 (let ((char (or char last-command-event)))
2468 (if (= char ?\S-\ )
2469 (setq char ?\s))
2470 (if current-input-method
2471 (isearch-process-search-multibyte-characters char count)
2472 (isearch-process-search-char char count))))
2474 (defun isearch-process-search-char (char &optional count)
2475 ;; * and ? are special in regexps when not preceded by \.
2476 ;; } and | are special in regexps when preceded by \.
2477 ;; Nothing special for + because it matches at least once.
2478 (cond
2479 ((memq char '(?* ??)) (isearch-fallback nil))
2480 ((eq char ?\}) (isearch-fallback t t))
2481 ((eq char ?|) (isearch-fallback t nil t)))
2483 ;; Append the char(s) to the search string,
2484 ;; update the message and re-search.
2485 (let* ((string (if (and (integerp count) (> count 1))
2486 (make-string count char)
2487 (char-to-string char)))
2488 (message (if (>= char ?\200)
2489 string
2490 (mapconcat 'isearch-text-char-description string ""))))
2491 (isearch-process-search-string string message)))
2493 (defun isearch-process-search-string (string message)
2494 (setq isearch-string (concat isearch-string string)
2495 isearch-message (concat isearch-message message))
2496 (isearch-search-and-update))
2499 ;; Search Ring
2501 (defun isearch-ring-adjust1 (advance)
2502 ;; Helper for isearch-ring-adjust
2503 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
2504 (length (length ring))
2505 (yank-pointer-name (if isearch-regexp
2506 'regexp-search-ring-yank-pointer
2507 'search-ring-yank-pointer))
2508 (yank-pointer (eval yank-pointer-name)))
2509 (if (zerop length)
2511 (set yank-pointer-name
2512 (setq yank-pointer
2513 (mod (+ (or yank-pointer (if advance 0 -1))
2514 (if advance -1 1))
2515 length)))
2516 (setq isearch-string (nth yank-pointer ring)
2517 isearch-message (mapconcat 'isearch-text-char-description
2518 isearch-string "")))))
2520 (defun isearch-ring-adjust (advance)
2521 ;; Helper for isearch-ring-advance and isearch-ring-retreat
2522 (isearch-ring-adjust1 advance)
2523 (if search-ring-update
2524 (progn
2525 (isearch-search)
2526 (isearch-push-state)
2527 (isearch-update))
2528 ;; Otherwise, edit the search string instead. Note that there is
2529 ;; no need to push the search state after isearch-edit-string here
2530 ;; since isearch-edit-string already pushes its state
2531 (isearch-edit-string)))
2533 (defun isearch-ring-advance ()
2534 "Advance to the next search string in the ring."
2535 ;; This could be more general to handle a prefix arg, but who would use it.
2536 (interactive)
2537 (isearch-ring-adjust 'advance))
2539 (defun isearch-ring-retreat ()
2540 "Retreat to the previous search string in the ring."
2541 (interactive)
2542 (isearch-ring-adjust nil))
2544 (defun isearch-complete1 ()
2545 ;; Helper for isearch-complete and isearch-complete-edit
2546 ;; Return t if completion OK, nil if no completion exists.
2547 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
2548 (completion-ignore-case case-fold-search)
2549 (completion (try-completion isearch-string ring)))
2550 (cond
2551 ((eq completion t)
2552 ;; isearch-string stays the same
2554 ((or completion ; not nil, must be a string
2555 (= 0 (length isearch-string))) ; shouldn't have to say this
2556 (if (equal completion isearch-string) ;; no extension?
2557 (progn
2558 (if completion-auto-help
2559 (with-output-to-temp-buffer "*Isearch completions*"
2560 (display-completion-list
2561 (all-completions isearch-string ring))))
2563 (and completion
2564 (setq isearch-string completion))))
2566 (message "No completion") ; waits a second if in minibuffer
2567 nil))))
2569 (defun isearch-complete ()
2570 "Complete the search string from the strings on the search ring.
2571 The completed string is then editable in the minibuffer.
2572 If there is no completion possible, say so and continue searching."
2573 (interactive)
2574 (if (isearch-complete1)
2575 (progn (setq isearch-message
2576 (mapconcat 'isearch-text-char-description
2577 isearch-string ""))
2578 (isearch-edit-string))
2579 ;; else
2580 (sit-for 1)
2581 (isearch-update)))
2583 (defun isearch-complete-edit ()
2584 "Same as `isearch-complete' except in the minibuffer."
2585 (interactive)
2586 (setq isearch-string (field-string))
2587 (if (isearch-complete1)
2588 (progn
2589 (delete-field)
2590 (insert isearch-string))))
2593 ;; Message string
2595 (defun isearch-message (&optional c-q-hack ellipsis)
2596 ;; Generate and print the message string.
2597 (let ((cursor-in-echo-area ellipsis)
2598 (m isearch-message)
2599 (fail-pos (isearch-fail-pos t)))
2600 ;; Highlight failed part
2601 (when fail-pos
2602 (setq m (copy-sequence m))
2603 (add-text-properties fail-pos (length m) '(face isearch-fail) m)
2604 ;; Highlight failed trailing whitespace
2605 (when (string-match " +$" m)
2606 (add-text-properties (match-beginning 0) (match-end 0)
2607 '(face trailing-whitespace) m)))
2608 (setq m (concat
2609 (isearch-message-prefix ellipsis isearch-nonincremental)
2611 (isearch-message-suffix c-q-hack)))
2612 (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
2614 (defun isearch-message-prefix (&optional ellipsis nonincremental)
2615 ;; If about to search, and previous search regexp was invalid,
2616 ;; check that it still is. If it is valid now,
2617 ;; let the message we display while searching say that it is valid.
2618 (and isearch-error ellipsis
2619 (condition-case ()
2620 (progn (re-search-forward isearch-string (point) t)
2621 (setq isearch-error nil))
2622 (error nil)))
2623 ;; If currently failing, display no ellipsis.
2624 (or isearch-success (setq ellipsis nil))
2625 (let ((m (concat (if isearch-success "" "failing ")
2626 (if isearch-adjusted "pending " "")
2627 (if (and isearch-wrapped
2628 (not isearch-wrap-function)
2629 (if isearch-forward
2630 (> (point) isearch-opoint)
2631 (< (point) isearch-opoint)))
2632 "over")
2633 (if isearch-wrapped "wrapped ")
2634 (let ((prefix ""))
2635 (advice-function-mapc
2636 (lambda (_ props)
2637 (let ((np (cdr (assq 'isearch-message-prefix props))))
2638 (if np (setq prefix (concat np prefix)))))
2639 isearch-filter-predicate)
2640 prefix)
2641 (if isearch-word
2642 (or (and (symbolp isearch-word)
2643 (get isearch-word 'isearch-message-prefix))
2644 "word ")
2646 (if isearch-regexp "regexp " "")
2647 (if multi-isearch-next-buffer-current-function "multi " "")
2648 (or isearch-message-prefix-add "")
2649 (if nonincremental "search" "I-search")
2650 (if isearch-forward "" " backward")
2651 (if current-input-method
2652 ;; Input methods for RTL languages use RTL
2653 ;; characters for their title, and that messes
2654 ;; up the display of search text after the prompt.
2655 (bidi-string-mark-left-to-right
2656 (concat " [" current-input-method-title "]: "))
2657 ": ")
2659 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
2660 'face 'minibuffer-prompt)))
2662 (defun isearch-message-suffix (&optional c-q-hack)
2663 (concat (if c-q-hack "^Q" "")
2664 (if isearch-error
2665 (concat " [" isearch-error "]")
2667 (or isearch-message-suffix-add "")))
2670 ;; Searching
2672 (defvar isearch-search-fun-function 'isearch-search-fun-default
2673 "Non-default value overrides the behavior of `isearch-search-fun-default'.
2674 This variable's value should be a function, which will be called
2675 with no arguments, and should return a function that takes three
2676 arguments: STRING, BOUND, and NOERROR.
2678 This returned function will be used by `isearch-search-string' to
2679 search for the first occurrence of STRING or its translation.")
2681 (defun isearch-search-fun ()
2682 "Return the function to use for the search.
2683 Can be changed via `isearch-search-fun-function' for special needs."
2684 (funcall (or isearch-search-fun-function 'isearch-search-fun-default)))
2686 (defun isearch-search-fun-default ()
2687 "Return default functions to use for the search."
2688 (cond
2689 (isearch-word
2690 (lambda (string &optional bound noerror count)
2691 ;; Use lax versions to not fail at the end of the word while
2692 ;; the user adds and removes characters in the search string
2693 ;; (or when using nonincremental word isearch)
2694 (let ((lax (not (or isearch-nonincremental
2695 (null (car isearch-cmds))
2696 (eq (length isearch-string)
2697 (length (isearch--state-string
2698 (car isearch-cmds))))))))
2699 (funcall
2700 (if isearch-forward #'re-search-forward #'re-search-backward)
2701 (if (functionp isearch-word)
2702 (funcall isearch-word string lax)
2703 (word-search-regexp string lax))
2704 bound noerror count))))
2705 ((and isearch-regexp isearch-regexp-lax-whitespace
2706 search-whitespace-regexp)
2707 (if isearch-forward
2708 're-search-forward-lax-whitespace
2709 're-search-backward-lax-whitespace))
2710 (isearch-regexp
2711 (if isearch-forward 're-search-forward 're-search-backward))
2712 ((and isearch-lax-whitespace search-whitespace-regexp)
2713 (if isearch-forward
2714 'search-forward-lax-whitespace
2715 'search-backward-lax-whitespace))
2717 (if isearch-forward 'search-forward 'search-backward))))
2719 (defun isearch-search-string (string bound noerror)
2720 "Search for the first occurrence of STRING or its translation.
2721 If found, move point to the end of the occurrence,
2722 update the match data, and return point."
2723 (let* ((func (isearch-search-fun))
2724 (pos1 (save-excursion (funcall func string bound noerror)))
2725 pos2)
2726 (when (and
2727 ;; Avoid "obsolete" warnings for translation-table-for-input.
2728 (with-no-warnings
2729 (char-table-p translation-table-for-input))
2730 (multibyte-string-p string)
2731 ;; Minor optimization.
2732 (string-match-p "[^[:ascii:]]" string))
2733 (let ((translated
2734 (apply 'string
2735 (mapcar (lambda (c)
2737 ;; Avoid "obsolete" warnings for
2738 ;; translation-table-for-input.
2739 (with-no-warnings
2740 (aref translation-table-for-input c))
2742 string)))
2743 match-data)
2744 (when translated
2745 (save-match-data
2746 (save-excursion
2747 (if (setq pos2 (funcall func translated bound noerror))
2748 (setq match-data (match-data t)))))
2749 (when (and pos2
2750 (or (not pos1)
2751 (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
2752 (setq pos1 pos2)
2753 (set-match-data match-data)))))
2754 (when pos1
2755 ;; When using multiple buffers isearch, switch to the new buffer here,
2756 ;; because `save-excursion' above doesn't allow doing it inside funcall.
2757 (if (and multi-isearch-next-buffer-current-function
2758 (buffer-live-p multi-isearch-current-buffer))
2759 (switch-to-buffer multi-isearch-current-buffer))
2760 (goto-char pos1)
2761 pos1)))
2763 (defun isearch-search ()
2764 ;; Do the search with the current search string.
2765 (if isearch-message-function
2766 (funcall isearch-message-function nil t)
2767 (isearch-message nil t))
2768 (if (and (eq isearch-case-fold-search t) search-upper-case)
2769 (setq isearch-case-fold-search
2770 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2771 (condition-case lossage
2772 (let ((inhibit-point-motion-hooks isearch-invisible)
2773 (inhibit-quit nil)
2774 (case-fold-search isearch-case-fold-search)
2775 (search-invisible isearch-invisible)
2776 (retry t))
2777 (setq isearch-error nil)
2778 (while retry
2779 (setq isearch-success
2780 (isearch-search-string isearch-string nil t))
2781 ;; Clear RETRY unless the search predicate says
2782 ;; to skip this search hit.
2783 (if (or (not isearch-success)
2784 (bobp) (eobp)
2785 (= (match-beginning 0) (match-end 0))
2786 (funcall isearch-filter-predicate
2787 (match-beginning 0) (match-end 0)))
2788 (setq retry nil)))
2789 (setq isearch-just-started nil)
2790 (if isearch-success
2791 (setq isearch-other-end
2792 (if isearch-forward (match-beginning 0) (match-end 0)))))
2794 (quit (isearch-unread ?\C-g)
2795 (setq isearch-success nil))
2797 (invalid-regexp
2798 (setq isearch-error (car (cdr lossage)))
2799 (cond
2800 ((string-match
2801 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
2802 isearch-error)
2803 (setq isearch-error "incomplete input"))
2804 ((and (not isearch-regexp)
2805 (string-match "\\`Regular expression too big" isearch-error))
2806 (cond
2807 (isearch-word
2808 (setq isearch-error "Too many words"))
2809 ((and isearch-lax-whitespace search-whitespace-regexp)
2810 (setq isearch-error "Too many spaces for whitespace matching"))))))
2812 (search-failed
2813 (setq isearch-success nil)
2814 (setq isearch-error (nth 2 lossage)))
2816 (error
2817 ;; stack overflow in regexp search.
2818 (setq isearch-error (format "%s" lossage))))
2820 (if isearch-success
2822 ;; Ding if failed this time after succeeding last time.
2823 (and (isearch--state-success (car isearch-cmds))
2824 (ding))
2825 (if (functionp (isearch--state-pop-fun (car isearch-cmds)))
2826 (funcall (isearch--state-pop-fun (car isearch-cmds))
2827 (car isearch-cmds)))
2828 (goto-char (isearch--state-point (car isearch-cmds)))))
2831 ;; Called when opening an overlay, and we are still in isearch.
2832 (defun isearch-open-overlay-temporary (ov)
2833 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
2834 ;; Some modes would want to open the overlays temporary during
2835 ;; isearch in their own way, they should set the
2836 ;; `isearch-open-invisible-temporary' to a function doing this.
2837 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2838 ;; Store the values for the `invisible' and `intangible'
2839 ;; properties, and then set them to nil. This way the text hidden
2840 ;; by this overlay becomes visible.
2842 ;; Do we really need to set the `intangible' property to t? Can we
2843 ;; have the point inside an overlay with an `intangible' property?
2844 ;; In 19.34 this does not exist so I cannot test it.
2845 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2846 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
2847 (overlay-put ov 'invisible nil)
2848 (overlay-put ov 'intangible nil)))
2851 ;; This is called at the end of isearch. It will open the overlays
2852 ;; that contain the latest match. Obviously in case of a C-g the
2853 ;; point returns to the original location which surely is not contain
2854 ;; in any of these overlays, se we are safe in this case too.
2855 (defun isearch-open-necessary-overlays (ov)
2856 (let ((inside-overlay (and (> (point) (overlay-start ov))
2857 (< (point) (overlay-end ov))))
2858 ;; If this exists it means that the overlay was opened using
2859 ;; this function, not by us tweaking the overlay properties.
2860 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2861 (when (or inside-overlay (not fct-temp))
2862 ;; restore the values for the `invisible' and `intangible'
2863 ;; properties
2864 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2865 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2866 (overlay-put ov 'isearch-invisible nil)
2867 (overlay-put ov 'isearch-intangible nil))
2868 (if inside-overlay
2869 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2870 (if fct-temp
2871 (funcall fct-temp ov t)))))
2873 ;; This is called when exiting isearch. It closes the temporary
2874 ;; opened overlays, except the ones that contain the latest match.
2875 (defun isearch-clean-overlays ()
2876 (when isearch-opened-overlays
2877 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
2878 (setq isearch-opened-overlays nil)))
2881 (defun isearch-intersects-p (start0 end0 start1 end1)
2882 "Return t if regions START0..END0 and START1..END1 intersect."
2883 (or (and (>= start0 start1) (< start0 end1))
2884 (and (> end0 start1) (<= end0 end1))
2885 (and (>= start1 start0) (< start1 end0))
2886 (and (> end1 start0) (<= end1 end0))))
2889 ;; Verify if the current match is outside of each element of
2890 ;; `isearch-opened-overlays', if so close that overlay.
2892 (defun isearch-close-unnecessary-overlays (begin end)
2893 (let ((overlays isearch-opened-overlays))
2894 (setq isearch-opened-overlays nil)
2895 (dolist (ov overlays)
2896 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
2897 (push ov isearch-opened-overlays)
2898 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2899 (if fct-temp
2900 ;; If this exists it means that the overlay was opened
2901 ;; using this function, not by us tweaking the overlay
2902 ;; properties.
2903 (funcall fct-temp ov t)
2904 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2905 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2906 (overlay-put ov 'isearch-invisible nil)
2907 (overlay-put ov 'isearch-intangible nil)))))))
2910 (defun isearch-range-invisible (beg end)
2911 "Return t if all the text from BEG to END is invisible."
2912 (when (/= beg end)
2913 ;; Check that invisibility runs up to END.
2914 (save-excursion
2915 (goto-char beg)
2916 (let (;; can-be-opened keeps track if we can open some overlays.
2917 (can-be-opened (eq search-invisible 'open))
2918 ;; the list of overlays that could be opened
2919 (crt-overlays nil))
2920 (when (and can-be-opened isearch-hide-immediately)
2921 (isearch-close-unnecessary-overlays beg end))
2922 ;; If the following character is currently invisible,
2923 ;; skip all characters with that same `invisible' property value.
2924 ;; Do that over and over.
2925 (while (and (< (point) end) (invisible-p (point)))
2926 (if (invisible-p (get-text-property (point) 'invisible))
2927 (progn
2928 (goto-char (next-single-property-change (point) 'invisible
2929 nil end))
2930 ;; if text is hidden by an `invisible' text property
2931 ;; we cannot open it at all.
2932 (setq can-be-opened nil))
2933 (when can-be-opened
2934 (let ((overlays (overlays-at (point)))
2935 ov-list
2937 invis-prop)
2938 (while overlays
2939 (setq o (car overlays)
2940 invis-prop (overlay-get o 'invisible))
2941 (if (invisible-p invis-prop)
2942 (if (overlay-get o 'isearch-open-invisible)
2943 (setq ov-list (cons o ov-list))
2944 ;; We found one overlay that cannot be
2945 ;; opened, that means the whole chunk
2946 ;; cannot be opened.
2947 (setq can-be-opened nil)))
2948 (setq overlays (cdr overlays)))
2949 (if can-be-opened
2950 ;; It makes sense to append to the open
2951 ;; overlays list only if we know that this is
2952 ;; t.
2953 (setq crt-overlays (append ov-list crt-overlays)))))
2954 (goto-char (next-overlay-change (point)))))
2955 ;; See if invisibility reaches up thru END.
2956 (if (>= (point) end)
2957 (if (and can-be-opened (consp crt-overlays))
2958 (progn
2959 (setq isearch-opened-overlays
2960 (append isearch-opened-overlays crt-overlays))
2961 (mapc 'isearch-open-overlay-temporary crt-overlays)
2962 nil)
2963 (setq isearch-hidden t)))))))
2965 (defun isearch-filter-visible (beg end)
2966 "Test whether the current search hit is visible at least partially.
2967 Return non-nil if the text from BEG to END is visible to Isearch as
2968 determined by `isearch-range-invisible' unless invisible text can be
2969 searched too when `search-invisible' is t."
2970 (or (eq search-invisible t)
2971 (not (isearch-range-invisible beg end))))
2974 ;; General utilities
2976 (defun isearch-no-upper-case-p (string regexp-flag)
2977 "Return t if there are no upper case chars in STRING.
2978 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
2979 since they have special meaning in a regexp."
2980 (let (quote-flag (i 0) (len (length string)) found)
2981 (while (and (not found) (< i len))
2982 (let ((char (aref string i)))
2983 (if (and regexp-flag (eq char ?\\))
2984 (setq quote-flag (not quote-flag))
2985 (if (and (not quote-flag) (not (eq char (downcase char))))
2986 (setq found t))
2987 (setq quote-flag nil)))
2988 (setq i (1+ i)))
2989 (not (or found
2990 ;; Even if there's no uppercase char, we want to detect the use
2991 ;; of [:upper:] or [:lower:] char-class, which indicates
2992 ;; clearly that the user cares about case distinction.
2993 (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
2994 (condition-case err
2995 (progn
2996 (string-match (substring string 0 (match-beginning 0))
2998 nil)
2999 (invalid-regexp
3000 (equal "Unmatched [ or [^" (cadr err)))))))))
3002 ;; Portability functions to support various Emacs versions.
3004 (defun isearch-text-char-description (c)
3005 (cond
3006 ((< c ?\s) (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph))
3007 ((= c ?\^?) (propertize "^?" 'face 'escape-glyph))
3008 (t (char-to-string c))))
3010 ;; General function to unread characters or events.
3011 ;; Also insert them in a keyboard macro being defined.
3012 (defun isearch-unread (&rest char-or-events)
3013 (mapc 'store-kbd-macro-event char-or-events)
3014 (setq unread-command-events
3015 (append char-or-events unread-command-events)))
3018 ;; Highlighting
3020 (defvar isearch-overlay nil)
3022 (defun isearch-highlight (beg end)
3023 (if search-highlight
3024 (if isearch-overlay
3025 ;; Overlay already exists, just move it.
3026 (move-overlay isearch-overlay beg end (current-buffer))
3027 ;; Overlay doesn't exist, create it.
3028 (setq isearch-overlay (make-overlay beg end))
3029 ;; 1001 is higher than lazy's 1000 and ediff's 100+
3030 (overlay-put isearch-overlay 'priority 1001)
3031 (overlay-put isearch-overlay 'face isearch-face))))
3033 (defun isearch-dehighlight ()
3034 (when isearch-overlay
3035 (delete-overlay isearch-overlay)))
3037 ;; isearch-lazy-highlight feature
3038 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
3040 ;; When active, *every* match for the current search string is
3041 ;; highlighted: the current one using the normal isearch match color
3042 ;; and all the others using `isearch-lazy-highlight'. The extra
3043 ;; highlighting makes it easier to anticipate where the cursor will
3044 ;; land each time you press C-s or C-r to repeat a pending search.
3045 ;; Highlighting of these additional matches happens in a deferred
3046 ;; fashion using "idle timers," so the cycles needed do not rob
3047 ;; isearch of its usual snappy response.
3049 ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
3050 ;; Specifically:
3051 ;; - `isearch-update' is expected to be called (at least) every time
3052 ;; the search string or window-start changes;
3053 ;; - `isearch-string' is expected to contain the current search
3054 ;; string as entered by the user;
3055 ;; - the type of the current search is expected to be given by
3056 ;; `isearch-word' and `isearch-regexp';
3057 ;; - the direction of the current search is expected to be given by
3058 ;; `isearch-forward';
3059 ;; - the variable `isearch-error' is expected to be true
3060 ;; only if `isearch-string' is an invalid regexp.
3062 (defvar isearch-lazy-highlight-overlays nil)
3063 (defvar isearch-lazy-highlight-wrapped nil)
3064 (defvar isearch-lazy-highlight-start-limit nil)
3065 (defvar isearch-lazy-highlight-end-limit nil)
3066 (defvar isearch-lazy-highlight-start nil)
3067 (defvar isearch-lazy-highlight-end nil)
3068 (defvar isearch-lazy-highlight-timer nil)
3069 (defvar isearch-lazy-highlight-last-string nil)
3070 (defvar isearch-lazy-highlight-window nil)
3071 (defvar isearch-lazy-highlight-window-start nil)
3072 (defvar isearch-lazy-highlight-window-end nil)
3073 (defvar isearch-lazy-highlight-case-fold-search nil)
3074 (defvar isearch-lazy-highlight-regexp nil)
3075 (defvar isearch-lazy-highlight-lax-whitespace nil)
3076 (defvar isearch-lazy-highlight-regexp-lax-whitespace nil)
3077 (defvar isearch-lazy-highlight-word nil)
3078 (defvar isearch-lazy-highlight-forward nil)
3079 (defvar isearch-lazy-highlight-error nil)
3081 (defun lazy-highlight-cleanup (&optional force)
3082 "Stop lazy highlighting and remove extra highlighting from current buffer.
3083 FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
3084 is nil. This function is called when exiting an incremental search if
3085 `lazy-highlight-cleanup' is non-nil."
3086 (interactive '(t))
3087 (if (or force lazy-highlight-cleanup)
3088 (while isearch-lazy-highlight-overlays
3089 (delete-overlay (car isearch-lazy-highlight-overlays))
3090 (setq isearch-lazy-highlight-overlays
3091 (cdr isearch-lazy-highlight-overlays))))
3092 (when isearch-lazy-highlight-timer
3093 (cancel-timer isearch-lazy-highlight-timer)
3094 (setq isearch-lazy-highlight-timer nil)))
3096 (define-obsolete-function-alias 'isearch-lazy-highlight-cleanup
3097 'lazy-highlight-cleanup
3098 "22.1")
3100 (defun isearch-lazy-highlight-new-loop (&optional beg end)
3101 "Cleanup any previous `lazy-highlight' loop and begin a new one.
3102 BEG and END specify the bounds within which highlighting should occur.
3103 This is called when `isearch-update' is invoked (which can cause the
3104 search string to change or the window to scroll). It is also used
3105 by other Emacs features."
3106 (when (and (null executing-kbd-macro)
3107 (sit-for 0) ;make sure (window-start) is credible
3108 (or (not (equal isearch-string
3109 isearch-lazy-highlight-last-string))
3110 (not (eq (selected-window)
3111 isearch-lazy-highlight-window))
3112 (not (eq isearch-lazy-highlight-case-fold-search
3113 isearch-case-fold-search))
3114 (not (eq isearch-lazy-highlight-regexp
3115 isearch-regexp))
3116 (not (eq isearch-lazy-highlight-word
3117 isearch-word))
3118 (not (eq isearch-lazy-highlight-lax-whitespace
3119 isearch-lax-whitespace))
3120 (not (eq isearch-lazy-highlight-regexp-lax-whitespace
3121 isearch-regexp-lax-whitespace))
3122 (not (= (window-start)
3123 isearch-lazy-highlight-window-start))
3124 (not (= (window-end) ; Window may have been split/joined.
3125 isearch-lazy-highlight-window-end))
3126 (not (eq isearch-forward
3127 isearch-lazy-highlight-forward))
3128 ;; In case we are recovering from an error.
3129 (not (equal isearch-error
3130 isearch-lazy-highlight-error))))
3131 ;; something important did indeed change
3132 (lazy-highlight-cleanup t) ;kill old loop & remove overlays
3133 (setq isearch-lazy-highlight-error isearch-error)
3134 ;; It used to check for `(not isearch-error)' here, but actually
3135 ;; lazy-highlighting might find matches to highlight even when
3136 ;; `isearch-error' is non-nil. (Bug#9918)
3137 (setq isearch-lazy-highlight-start-limit beg
3138 isearch-lazy-highlight-end-limit end)
3139 (setq isearch-lazy-highlight-window (selected-window)
3140 isearch-lazy-highlight-window-start (window-start)
3141 isearch-lazy-highlight-window-end (window-end)
3142 ;; Start lazy-highlighting at the beginning of the found
3143 ;; match (`isearch-other-end'). If no match, use point.
3144 ;; One of the next two variables (depending on search direction)
3145 ;; is used to define the starting position of lazy-highlighting
3146 ;; and also to remember the current position of point between
3147 ;; calls of `isearch-lazy-highlight-update', and another variable
3148 ;; is used to define where the wrapped search must stop.
3149 isearch-lazy-highlight-start (or isearch-other-end (point))
3150 isearch-lazy-highlight-end (or isearch-other-end (point))
3151 isearch-lazy-highlight-wrapped nil
3152 isearch-lazy-highlight-last-string isearch-string
3153 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
3154 isearch-lazy-highlight-regexp isearch-regexp
3155 isearch-lazy-highlight-lax-whitespace isearch-lax-whitespace
3156 isearch-lazy-highlight-regexp-lax-whitespace isearch-regexp-lax-whitespace
3157 isearch-lazy-highlight-word isearch-word
3158 isearch-lazy-highlight-forward isearch-forward)
3159 (unless (equal isearch-string "")
3160 (setq isearch-lazy-highlight-timer
3161 (run-with-idle-timer lazy-highlight-initial-delay nil
3162 'isearch-lazy-highlight-update)))))
3164 (defun isearch-lazy-highlight-search ()
3165 "Search ahead for the next or previous match, for lazy highlighting.
3166 Attempt to do the search exactly the way the pending Isearch would."
3167 (condition-case nil
3168 (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
3169 (isearch-regexp isearch-lazy-highlight-regexp)
3170 (isearch-word isearch-lazy-highlight-word)
3171 (isearch-lax-whitespace
3172 isearch-lazy-highlight-lax-whitespace)
3173 (isearch-regexp-lax-whitespace
3174 isearch-lazy-highlight-regexp-lax-whitespace)
3175 (isearch-forward isearch-lazy-highlight-forward)
3176 (search-invisible nil) ; don't match invisible text
3177 (retry t)
3178 (success nil)
3179 (bound (if isearch-lazy-highlight-forward
3180 (min (or isearch-lazy-highlight-end-limit (point-max))
3181 (if isearch-lazy-highlight-wrapped
3182 isearch-lazy-highlight-start
3183 (window-end)))
3184 (max (or isearch-lazy-highlight-start-limit (point-min))
3185 (if isearch-lazy-highlight-wrapped
3186 isearch-lazy-highlight-end
3187 (window-start))))))
3188 ;; Use a loop like in `isearch-search'.
3189 (while retry
3190 (setq success (isearch-search-string
3191 isearch-lazy-highlight-last-string bound t))
3192 ;; Clear RETRY unless the search predicate says
3193 ;; to skip this search hit.
3194 (if (or (not success)
3195 (= (point) bound) ; like (bobp) (eobp) in `isearch-search'.
3196 (= (match-beginning 0) (match-end 0))
3197 (funcall isearch-filter-predicate
3198 (match-beginning 0) (match-end 0)))
3199 (setq retry nil)))
3200 success)
3201 (error nil)))
3203 (defun isearch-lazy-highlight-update ()
3204 "Update highlighting of other matches for current search."
3205 (let ((max lazy-highlight-max-at-a-time)
3206 (looping t)
3207 nomore)
3208 (with-local-quit
3209 (save-selected-window
3210 (if (and (window-live-p isearch-lazy-highlight-window)
3211 (not (eq (selected-window) isearch-lazy-highlight-window)))
3212 (select-window isearch-lazy-highlight-window))
3213 (save-excursion
3214 (save-match-data
3215 (goto-char (if isearch-lazy-highlight-forward
3216 isearch-lazy-highlight-end
3217 isearch-lazy-highlight-start))
3218 (while looping
3219 (let ((found (isearch-lazy-highlight-search)))
3220 (when max
3221 (setq max (1- max))
3222 (if (<= max 0)
3223 (setq looping nil)))
3224 (if found
3225 (let ((mb (match-beginning 0))
3226 (me (match-end 0)))
3227 (if (= mb me) ;zero-length match
3228 (if isearch-lazy-highlight-forward
3229 (if (= mb (if isearch-lazy-highlight-wrapped
3230 isearch-lazy-highlight-start
3231 (window-end)))
3232 (setq found nil)
3233 (forward-char 1))
3234 (if (= mb (if isearch-lazy-highlight-wrapped
3235 isearch-lazy-highlight-end
3236 (window-start)))
3237 (setq found nil)
3238 (forward-char -1)))
3240 ;; non-zero-length match
3241 (let ((ov (make-overlay mb me)))
3242 (push ov isearch-lazy-highlight-overlays)
3243 ;; 1000 is higher than ediff's 100+,
3244 ;; but lower than isearch main overlay's 1001
3245 (overlay-put ov 'priority 1000)
3246 (overlay-put ov 'face lazy-highlight-face)
3247 (overlay-put ov 'window (selected-window))))
3248 ;; Remember the current position of point for
3249 ;; the next call of `isearch-lazy-highlight-update'
3250 ;; when `lazy-highlight-max-at-a-time' is too small.
3251 (if isearch-lazy-highlight-forward
3252 (setq isearch-lazy-highlight-end (point))
3253 (setq isearch-lazy-highlight-start (point)))))
3255 ;; not found or zero-length match at the search bound
3256 (if (not found)
3257 (if isearch-lazy-highlight-wrapped
3258 (setq looping nil
3259 nomore t)
3260 (setq isearch-lazy-highlight-wrapped t)
3261 (if isearch-lazy-highlight-forward
3262 (progn
3263 (setq isearch-lazy-highlight-end (window-start))
3264 (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
3265 (window-start))))
3266 (setq isearch-lazy-highlight-start (window-end))
3267 (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
3268 (window-end))))))))
3269 (unless nomore
3270 (setq isearch-lazy-highlight-timer
3271 (run-at-time lazy-highlight-interval nil
3272 'isearch-lazy-highlight-update)))))))))
3274 (defun isearch-resume (string regexp word forward message case-fold)
3275 "Resume an incremental search.
3276 STRING is the string or regexp searched for.
3277 REGEXP non-nil means the resumed search was a regexp search.
3278 WORD non-nil means resume a word search.
3279 FORWARD non-nil means resume a forward search.
3280 MESSAGE is the echo-area message recorded for the search resumed.
3281 CASE-FOLD non-nil means the search was case-insensitive."
3282 (isearch-mode forward regexp nil nil word)
3283 (setq isearch-string string
3284 isearch-message message
3285 isearch-case-fold-search case-fold)
3286 (isearch-search)
3287 (isearch-update))
3289 ;;; isearch.el ends here