Fix off-by-one history pruning (bug#31211)
[emacs.git] / lisp / isearch.el
blobfeadf10e8b702fb40f8d1bd4c3b8003091cadbde
1 ;;; isearch.el --- incremental search minor mode -*- lexical-binding: t -*-
3 ;; Copyright (C) 1992-1997, 1999-2018 Free Software Foundation, Inc.
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6 ;; Maintainer: emacs-devel@gnu.org
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 <https://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'. Also see minibuffer-local-isearch-map
35 ;; for bindings active during `isearch-edit-string'.
37 ;; isearch-mode should work even if you switch windows with the mouse,
38 ;; in which case isearch-mode is terminated automatically before the
39 ;; switch.
41 ;; The search ring and completion commands automatically put you in
42 ;; the minibuffer to edit the string. This gives you a chance to
43 ;; modify the search string before executing the search. There are
44 ;; three commands to terminate the editing: C-s and C-r exit the
45 ;; minibuffer and search forward and reverse respectively, while C-m
46 ;; exits and searches in the last search direction.
48 ;; Exiting immediately from isearch uses isearch-edit-string instead
49 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
50 ;; The name of this option should probably be changed if we decide to
51 ;; keep the behavior. No point in forcing nonincremental search until
52 ;; the last possible moment.
54 ;;; Code:
56 (eval-when-compile (require 'cl-lib))
58 ;; Some additional options and constants.
60 (defgroup isearch nil
61 "Incremental search minor mode."
62 :link '(emacs-commentary-link "isearch")
63 :link '(custom-manual "(emacs)Incremental Search")
64 :prefix "isearch-"
65 :prefix "search-"
66 :group 'matching)
69 (defcustom search-exit-option t
70 "Defines what control characters do in incremental search.
71 If t, random control and meta characters terminate the search
72 and are then executed normally.
73 If `edit', edit the search string instead of exiting.
74 If `move', extend the search string by motion commands
75 that have the `isearch-move' property on their symbols
76 equal to `enabled', or the shift-translated command is
77 not disabled by the value `disabled' of the same property.
78 If `shift-move', extend the search string by motion commands
79 while holding down the shift key.
80 Both `move' and `shift-move' extend the search string by yanking text
81 that ends at the new position after moving point in the current buffer.
82 If `append', the characters which you type that are not interpreted by
83 the incremental search are simply appended to the search string.
84 If nil, run the command without exiting Isearch."
85 :type '(choice (const :tag "Terminate incremental search" t)
86 (const :tag "Edit the search string" edit)
87 (const :tag "Extend the search string by motion commands" move)
88 (const :tag "Extend the search string by shifted motion keys" shift-move)
89 (const :tag "Append control characters to the search string" append)
90 (const :tag "Don't terminate incremental search" nil))
91 :version "27.1")
93 (defcustom search-slow-window-lines 1
94 "Number of lines in slow search display windows.
95 These are the short windows used during incremental search on slow terminals.
96 Negative means put the slow search window at the top (normally it's at bottom)
97 and the value is minus the number of lines."
98 :type 'integer)
100 (defcustom search-slow-speed 1200
101 "Highest terminal speed at which to use \"slow\" style incremental search.
102 This is the style where a one-line window is created to show the line
103 that the search has reached."
104 :type 'integer)
106 (defcustom search-upper-case 'not-yanks
107 "If non-nil, upper case chars disable case fold searching.
108 That is, upper and lower case chars must match exactly.
109 This applies no matter where the chars come from, but does not
110 apply to chars in regexps that are prefixed with `\\'.
111 If this value is `not-yanks', text yanked into the search string
112 in Isearch mode is always downcased."
113 :type '(choice (const :tag "off" nil)
114 (const not-yanks)
115 (other :tag "on" t)))
117 (defcustom search-nonincremental-instead t
118 "If non-nil, do a nonincremental search instead of exiting immediately.
119 This affects the behavior of `isearch-exit' and any key bound to that
120 command: if this variable is nil, `isearch-exit' always exits the search;
121 if the value is non-nil, and the search string is empty, `isearch-exit'
122 starts a nonincremental search instead. (Actually, `isearch-edit-string'
123 is called to let you enter the search string, and RET terminates editing
124 and does a nonincremental search.)"
125 :type 'boolean)
127 (defcustom search-whitespace-regexp (purecopy "\\s-+")
128 "If non-nil, regular expression to match a sequence of whitespace chars.
129 When you enter a space or spaces in the incremental search, it
130 will match any sequence matched by this regexp. As an exception,
131 spaces are treated normally in regexp incremental search if they
132 occur in a regexp construct like [...] or *, + or ?.
134 If the value is a string, it applies to both ordinary and
135 regexp incremental search. If the value is nil, or
136 `isearch-lax-whitespace' is nil for ordinary incremental search, or
137 `isearch-regexp-lax-whitespace' is nil for regexp incremental search,
138 then each space you type matches literally, against one space.
140 You might want to use something like \"[ \\t\\r\\n]+\" instead.
141 In the Customization buffer, that is `[' followed by a space,
142 a tab, a carriage return (control-M), a newline, and `]+'."
143 :type '(choice (const :tag "Match Spaces Literally" nil)
144 regexp)
145 :version "24.3")
147 (defcustom search-invisible 'open
148 "If t incremental search/query-replace can match hidden text.
149 A nil value means don't match invisible text.
150 When the value is `open', if the text matched is made invisible by
151 an overlay having a non-nil `invisible' property, and that overlay
152 has a non-nil property `isearch-open-invisible', then incremental
153 search will show the hidden text. (This applies when using `outline.el'
154 and `hideshow.el'.)
156 To temporarily change the value for an active incremental search,
157 use \\<isearch-mode-map>\\[isearch-toggle-invisible].
159 See also the related option `isearch-hide-immediately'.
161 See also `reveal-mode' if you want overlays to automatically be opened
162 whenever point is in one of them."
163 :type '(choice (const :tag "Match hidden text" t)
164 (const :tag "Open overlays" open)
165 (const :tag "Don't match hidden text" nil)))
167 (defcustom isearch-hide-immediately t
168 "If non-nil, re-hide an invisible match right away.
169 This variable makes a difference when `search-invisible' is set to `open'.
170 If non-nil, invisible matches are re-hidden as soon as the match moves
171 off the invisible text surrounding the match.
172 If nil then do not re-hide opened invisible text when the match moves.
173 Whatever the value, all opened invisible text is hidden again after exiting
174 the search, with the exception of the last successful match, if any."
175 :type 'boolean)
177 (defcustom isearch-resume-in-command-history nil
178 "If non-nil, `isearch-resume' commands are added to the command history.
179 This allows you to resume earlier Isearch sessions through the
180 command history."
181 :type 'boolean)
183 (defvar isearch-mode-hook nil
184 "Function(s) to call after starting up an incremental search.")
186 (defvar isearch-update-post-hook nil
187 "Function(s) to call after isearch has found matches in the buffer.")
189 (defvar isearch-mode-end-hook nil
190 "Function(s) to call after terminating an incremental search.
191 When these functions are called, `isearch-mode-end-hook-quit'
192 is non-nil if the user quits the search.")
194 (defvar isearch-mode-end-hook-quit nil
195 "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
197 (defvar isearch-message-function nil
198 "Function to call to display the search prompt.
199 If nil, use function `isearch-message'.")
201 (defvar isearch-wrap-function nil
202 "Function to call to wrap the search when search is failed.
203 If nil, move point to the beginning of the buffer for a forward search,
204 or to the end of the buffer for a backward search.")
206 (defvar isearch-push-state-function nil
207 "Function to save a function restoring the mode-specific Isearch state
208 to the search status stack.")
210 (defvar isearch-filter-predicate #'isearch-filter-visible
211 "Predicate that filters the search hits that would normally be available.
212 Search hits that dissatisfy the predicate are skipped. The function
213 has two arguments: the positions of start and end of text matched by
214 the search. If this function returns nil, continue searching without
215 stopping at this match.
216 If you use `add-function' to modify this variable, you can use the
217 `isearch-message-prefix' advice property to specify the prefix string
218 displayed in the search message.")
220 ;; Search ring.
222 (defvar search-ring nil
223 "List of search string sequences.")
224 (defvar regexp-search-ring nil
225 "List of regular expression search string sequences.")
227 (defcustom search-ring-max 16
228 "Maximum length of search ring before oldest elements are thrown away."
229 :type 'integer)
230 (defcustom regexp-search-ring-max 16
231 "Maximum length of regexp search ring before oldest elements are thrown away."
232 :type 'integer)
234 (defvar search-ring-yank-pointer nil
235 "Index in `search-ring' of last string reused.
236 It is nil if none yet.")
237 (defvar regexp-search-ring-yank-pointer nil
238 "Index in `regexp-search-ring' of last string reused.
239 It is nil if none yet.")
241 (defcustom search-ring-update nil
242 "Non-nil if advancing or retreating in the search ring should cause search.
243 Default value, nil, means edit the string instead."
244 :type 'boolean)
246 (autoload 'char-fold-to-regexp "char-fold")
248 (defcustom search-default-mode nil
249 "Default mode to use when starting isearch.
250 Value is nil, t, or a function.
252 If nil, default to literal searches (note that `case-fold-search'
253 and `isearch-lax-whitespace' may still be applied).\\<isearch-mode-map>
254 If t, default to regexp searches (as if typing `\\[isearch-toggle-regexp]' during
255 isearch).
257 If a function, use that function as an `isearch-regexp-function'.
258 Example functions (and the keys to toggle them during isearch)
259 are `word-search-regexp' \(`\\[isearch-toggle-word]'), `isearch-symbol-regexp'
260 \(`\\[isearch-toggle-symbol]'), and `char-fold-to-regexp' \(`\\[isearch-toggle-char-fold]')."
261 ;; :type is set below by `isearch-define-mode-toggle'.
262 :type '(choice (const :tag "Literal search" nil)
263 (const :tag "Regexp search" t)
264 (function :tag "Other"))
265 :version "25.1")
267 ;;; isearch highlight customization.
269 (defcustom search-highlight t
270 "Non-nil means incremental search highlights the current match."
271 :type 'boolean)
273 (defface isearch
274 '((((class color) (min-colors 88) (background light))
275 ;; The background must not be too dark, for that means
276 ;; the character is hard to see when the cursor is there.
277 (:background "magenta3" :foreground "lightskyblue1"))
278 (((class color) (min-colors 88) (background dark))
279 (:background "palevioletred2" :foreground "brown4"))
280 (((class color) (min-colors 16))
281 (:background "magenta4" :foreground "cyan1"))
282 (((class color) (min-colors 8))
283 (:background "magenta4" :foreground "cyan1"))
284 (t (:inverse-video t)))
285 "Face for highlighting Isearch matches."
286 :group 'isearch
287 :group 'basic-faces)
288 (defvar isearch-face 'isearch)
290 (defface isearch-fail
291 '((((class color) (min-colors 88) (background light))
292 (:background "RosyBrown1"))
293 (((class color) (min-colors 88) (background dark))
294 (:background "red4"))
295 (((class color) (min-colors 16))
296 (:background "red"))
297 (((class color) (min-colors 8))
298 (:background "red"))
299 (((class color grayscale))
300 :foreground "grey")
301 (t (:inverse-video t)))
302 "Face for highlighting failed part in Isearch echo-area message."
303 :version "23.1")
305 (defcustom isearch-lazy-highlight t
306 "Controls the lazy-highlighting during incremental search.
307 When non-nil, all text in the buffer matching the current search
308 string is highlighted lazily (see `lazy-highlight-initial-delay'
309 and `lazy-highlight-interval').
311 When multiple windows display the current buffer, the
312 highlighting is displayed only on the selected window, unless
313 this variable is set to the symbol `all-windows'."
314 :type '(choice boolean
315 (const :tag "On, and applied to all windows" all-windows))
316 :group 'lazy-highlight
317 :group 'isearch)
319 ;;; Lazy highlight customization.
321 (defgroup lazy-highlight nil
322 "Lazy highlighting feature for matching strings."
323 :prefix "lazy-highlight-"
324 :version "21.1"
325 :group 'isearch
326 :group 'matching)
328 (defcustom lazy-highlight-cleanup t
329 "Controls whether to remove extra highlighting after a search.
330 If this is nil, extra highlighting can be \"manually\" removed with
331 \\[lazy-highlight-cleanup]."
332 :type 'boolean
333 :group 'lazy-highlight)
335 (defcustom lazy-highlight-initial-delay 0.25
336 "Seconds to wait before beginning to lazily highlight all matches."
337 :type 'number
338 :group 'lazy-highlight)
340 (defcustom lazy-highlight-interval 0 ; 0.0625
341 "Seconds between lazily highlighting successive matches."
342 :type 'number
343 :group 'lazy-highlight)
345 (defcustom lazy-highlight-max-at-a-time nil ; 20 (bug#25751)
346 "Maximum matches to highlight at a time (for `lazy-highlight').
347 Larger values may reduce Isearch's responsiveness to user input;
348 smaller values make matches highlight slowly.
349 A value of nil means highlight all matches shown on the screen."
350 :type '(choice (const :tag "All" nil)
351 (integer :tag "Some"))
352 :group 'lazy-highlight)
354 (defface lazy-highlight
355 '((((class color) (min-colors 88) (background light))
356 (:background "paleturquoise"))
357 (((class color) (min-colors 88) (background dark))
358 (:background "paleturquoise4"))
359 (((class color) (min-colors 16))
360 (:background "turquoise3"))
361 (((class color) (min-colors 8))
362 (:background "turquoise3"))
363 (t (:underline t)))
364 "Face for lazy highlighting of matches other than the current one."
365 :group 'lazy-highlight
366 :group 'basic-faces)
369 ;; Define isearch help map.
371 (defvar isearch-help-map
372 (let ((map (make-sparse-keymap)))
373 (define-key map (char-to-string help-char) 'isearch-help-for-help)
374 (define-key map [help] 'isearch-help-for-help)
375 (define-key map [f1] 'isearch-help-for-help)
376 (define-key map "?" 'isearch-help-for-help)
377 (define-key map "b" 'isearch-describe-bindings)
378 (define-key map "k" 'isearch-describe-key)
379 (define-key map "m" 'isearch-describe-mode)
380 (define-key map "q" 'help-quit)
381 map)
382 "Keymap for characters following the Help key for Isearch mode.")
384 (eval-when-compile (require 'help-macro))
386 (make-help-screen isearch-help-for-help-internal
387 (purecopy "Type a help option: [bkm] or ?")
388 "You have typed %THIS-KEY%, the help character. Type a Help option:
389 \(Type \\<help-map>\\[help-quit] to exit the Help command.)
391 b Display all Isearch key bindings.
392 k KEYS Display full documentation of Isearch key sequence.
393 m Display documentation of Isearch mode.
395 You can't type here other help keys available in the global help map,
396 but outside of this help window when you type them in Isearch mode,
397 they exit Isearch mode before displaying global help."
398 isearch-help-map)
400 (defvar isearch--display-help-action '(nil (inhibit-same-window . t)))
402 (defun isearch-help-for-help ()
403 "Display Isearch help menu."
404 (interactive)
405 (let ((display-buffer-overriding-action isearch--display-help-action))
406 (isearch-help-for-help-internal))
407 (isearch-update))
409 (defun isearch-describe-bindings ()
410 "Show a list of all keys defined in Isearch mode, and their definitions.
411 This is like `describe-bindings', but displays only Isearch keys."
412 (interactive)
413 (let ((display-buffer-overriding-action isearch--display-help-action))
414 (with-help-window "*Help*"
415 (with-current-buffer standard-output
416 (princ "Isearch Mode Bindings:\n")
417 (princ (substitute-command-keys "\\{isearch-mode-map}"))))))
419 (defun isearch-describe-key ()
420 "Display documentation of the function invoked by isearch key."
421 (interactive)
422 (let ((display-buffer-overriding-action isearch--display-help-action))
423 (call-interactively 'describe-key))
424 (isearch-update))
426 (defun isearch-describe-mode ()
427 "Display documentation of Isearch mode."
428 (interactive)
429 (let ((display-buffer-overriding-action isearch--display-help-action))
430 (describe-function 'isearch-forward))
431 (isearch-update))
433 (defalias 'isearch-mode-help 'isearch-describe-mode)
436 ;; Define isearch-mode keymap.
438 (defvar isearch-mode-map
439 (let ((i 0)
440 (map (make-keymap)))
441 (or (char-table-p (nth 1 map))
442 (error "The initialization of isearch-mode-map must be updated"))
443 ;; Make all multibyte characters search for themselves.
444 (set-char-table-range (nth 1 map) (cons #x100 (max-char))
445 'isearch-printing-char)
447 ;; Single-byte printing chars extend the search string by default.
448 (setq i ?\s)
449 (while (< i 256)
450 (define-key map (vector i) 'isearch-printing-char)
451 (setq i (1+ i)))
453 ;; To handle local bindings with meta char prefix keys, define
454 ;; another full keymap. This must be done for any other prefix
455 ;; keys as well, one full keymap per char of the prefix key. It
456 ;; would be simpler to disable the global keymap, and/or have a
457 ;; default local key binding for any key not otherwise bound.
458 (let ((meta-map (make-sparse-keymap)))
459 (define-key map (char-to-string meta-prefix-char) meta-map))
461 ;; Several non-printing chars change the searching behavior.
462 (define-key map "\C-s" 'isearch-repeat-forward)
463 (define-key map "\C-r" 'isearch-repeat-backward)
464 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
465 ;; combinations can be used to repeat regexp isearches that can
466 ;; be used to start these searches.
467 (define-key map "\M-\C-s" 'isearch-repeat-forward)
468 (define-key map "\M-\C-r" 'isearch-repeat-backward)
469 (define-key map "\177" 'isearch-delete-char)
470 (define-key map [backspace] 'undefined) ;bug#20466.
471 (define-key map "\C-g" 'isearch-abort)
473 ;; This assumes \e is the meta-prefix-char.
474 (or (= ?\e meta-prefix-char)
475 (error "Inconsistency in isearch.el"))
476 (define-key map "\e\e\e" 'isearch-cancel)
478 (define-key map "\C-q" 'isearch-quote-char)
480 (define-key map "\r" 'isearch-exit)
481 (define-key map [return] 'isearch-exit)
482 (define-key map "\C-j" 'isearch-printing-char)
483 (define-key map "\t" 'isearch-printing-char)
484 (define-key map [?\S-\ ] 'isearch-printing-char)
486 (define-key map "\C-w" 'isearch-yank-word-or-char)
487 (define-key map "\M-\C-w" 'isearch-yank-symbol-or-char)
488 (define-key map "\M-\C-d" 'isearch-del-char)
489 (define-key map "\M-\C-y" 'isearch-yank-char)
490 (define-key map "\C-y" 'isearch-yank-kill)
491 (define-key map "\M-s\C-e" 'isearch-yank-line)
493 (define-key map (char-to-string help-char) isearch-help-map)
494 (define-key map [help] isearch-help-map)
495 (define-key map [f1] isearch-help-map)
497 (define-key map "\M-n" 'isearch-ring-advance)
498 (define-key map "\M-p" 'isearch-ring-retreat)
499 (define-key map "\M-y" 'isearch-yank-pop)
501 (define-key map "\M-\t" 'isearch-complete)
503 ;; Pass frame events transparently so they won't exit the search.
504 ;; In particular, if we have more than one display open, then a
505 ;; switch-frame might be generated by someone typing at another keyboard.
506 (define-key map [switch-frame] nil)
507 (define-key map [delete-frame] nil)
508 (define-key map [iconify-frame] nil)
509 (define-key map [make-frame-visible] nil)
510 (define-key map [mouse-movement] nil)
511 (define-key map [language-change] nil)
513 ;; For searching multilingual text.
514 (define-key map "\C-\\" 'isearch-toggle-input-method)
515 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
517 ;; People expect to be able to paste with the mouse.
518 (define-key map [mouse-2] #'isearch-mouse-2)
519 (define-key map [down-mouse-2] nil)
520 (define-key map [xterm-paste] #'isearch-xterm-paste)
522 ;; Some bindings you may want to put in your isearch-mode-hook.
523 ;; Suggest some alternates...
524 (define-key map "\M-c" 'isearch-toggle-case-fold)
525 (define-key map "\M-r" 'isearch-toggle-regexp)
526 (define-key map "\M-e" 'isearch-edit-string)
528 (put 'isearch-toggle-case-fold :advertised-binding "\M-sc")
529 (put 'isearch-toggle-regexp :advertised-binding "\M-sr")
530 (put 'isearch-edit-string :advertised-binding "\M-se")
532 (define-key map "\M-se" 'isearch-edit-string)
533 ;; More toggles defined by `isearch-define-mode-toggle'.
535 (define-key map [?\M-%] 'isearch-query-replace)
536 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
537 (define-key map "\M-so" 'isearch-occur)
538 (define-key map "\M-shr" 'isearch-highlight-regexp)
540 ;; The key translations defined in the C-x 8 prefix should add
541 ;; characters to the search string. See iso-transl.el.
542 (define-key map "\C-x8\r" 'isearch-char-by-name)
544 map)
545 "Keymap for `isearch-mode'.")
547 (defvar minibuffer-local-isearch-map
548 (let ((map (make-sparse-keymap)))
549 (set-keymap-parent map minibuffer-local-map)
550 (define-key map "\r" 'exit-minibuffer)
551 (define-key map "\M-\t" 'isearch-complete-edit)
552 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
553 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
554 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
555 (define-key map [right] 'isearch-yank-char-in-minibuffer)
556 map)
557 "Keymap for editing Isearch strings in the minibuffer.")
559 ;; Internal variables declared globally for byte-compiler.
560 ;; These are all set with setq while isearching
561 ;; and bound locally while editing the search string.
563 (defvar isearch-forward nil) ; Searching in the forward direction.
564 (defvar isearch-regexp nil) ; Searching for a regexp.
565 ;; We still support setting this to t for backwards compatibility.
566 (define-obsolete-variable-alias 'isearch-word
567 'isearch-regexp-function "25.1")
568 (defvar isearch-regexp-function nil
569 "Regexp-based search mode for words/symbols.
570 If the value is a function (e.g. `isearch-symbol-regexp'), it is
571 called to convert a plain search string to a regexp used by
572 regexp search functions.
573 The symbol property `isearch-message-prefix' put on this function
574 specifies the prefix string displayed in the search message.
576 This variable is set and changed during isearch. To change the
577 default behavior used for searches, see `search-default-mode'
578 instead.")
580 (defvar isearch-lax-whitespace t
581 "If non-nil, a space will match a sequence of whitespace chars.
582 When you enter a space or spaces in ordinary incremental search, it
583 will match any sequence matched by the regexp defined by the variable
584 `search-whitespace-regexp'. If the value is nil, each space you type
585 matches literally, against one space. You can toggle the value of this
586 variable by the command `isearch-toggle-lax-whitespace'.")
588 (defvar isearch-regexp-lax-whitespace nil
589 "If non-nil, a space will match a sequence of whitespace chars.
590 When you enter a space or spaces in regexp incremental search, it
591 will match any sequence matched by the regexp defined by the variable
592 `search-whitespace-regexp'. If the value is nil, each space you type
593 matches literally, against one space. You can toggle the value of this
594 variable by the command `isearch-toggle-lax-whitespace'.")
596 (defvar isearch-cmds nil
597 "Stack of search status elements.
598 Each element is an `isearch--state' struct where the slots are
599 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD/REGEXP-FUNCTION
600 ERROR WRAPPED BARRIER CASE-FOLD-SEARCH POP-FUN]")
602 (defvar isearch-string "") ; The current search string.
603 (defvar isearch-message "") ; text-char-description version of isearch-string
605 (defvar isearch-message-prefix-add nil) ; Additional text for the message prefix
606 (defvar isearch-message-suffix-add nil) ; Additional text for the message suffix
608 (defvar isearch-success t) ; Searching is currently successful.
609 (defvar isearch-error nil) ; Error message for failed search.
610 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
611 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
612 (defvar isearch-barrier 0
613 "Recorded minimum/maximal point for the current search.")
614 (defvar isearch-just-started nil)
615 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
617 ;; case-fold-search while searching.
618 ;; either nil, t, or 'yes. 'yes means the same as t except that mixed
619 ;; case in the search string is ignored.
620 (defvar isearch-case-fold-search nil)
622 ;; search-invisible while searching.
623 ;; either nil, t, or 'open. 'open means the same as t except that
624 ;; opens hidden overlays.
625 (defvar isearch-invisible search-invisible)
627 (defvar isearch-last-case-fold-search nil)
629 ;; Used to save default value while isearch is active
630 (defvar isearch-original-minibuffer-message-timeout nil)
632 (defvar isearch-adjusted nil)
633 (defvar isearch-slow-terminal-mode nil)
634 ;; If t, using a small window.
635 (defvar isearch-small-window nil)
636 (defvar isearch-opoint 0)
637 ;; The window configuration active at the beginning of the search.
638 (defvar isearch-window-configuration nil)
640 ;; Flag to indicate a yank occurred, so don't move the cursor.
641 (defvar isearch-yank-flag nil)
643 ;; A function to be called after each input character is processed.
644 ;; (It is not called after characters that exit the search.)
645 ;; It is only set from an optional argument to `isearch-mode'.
646 (defvar isearch-op-fun nil)
648 ;; Is isearch-mode in a recursive edit for modal searching.
649 (defvar isearch-recursive-edit nil)
651 ;; Should isearch be terminated after doing one search?
652 (defvar isearch-nonincremental nil)
654 ;; New value of isearch-nonincremental after isearch-edit-string.
655 (defvar isearch-new-nonincremental nil)
657 ;; New value of isearch-forward after isearch-edit-string.
658 (defvar isearch-new-forward nil)
660 ;; Accumulate here the overlays opened during searching.
661 (defvar isearch-opened-overlays nil)
663 ;; Non-nil if the string exists but is invisible.
664 (defvar isearch-hidden nil)
666 ;; The value of input-method-function when isearch is invoked.
667 (defvar isearch-input-method-function nil)
669 ;; A flag to tell if input-method-function is locally bound when
670 ;; isearch is invoked.
671 (defvar isearch-input-method-local-p nil)
673 (defvar isearch--saved-overriding-local-map nil)
675 ;; Minor-mode-alist changes - kind of redundant with the
676 ;; echo area, but if isearching in multiple windows, it can be useful.
678 (or (assq 'isearch-mode minor-mode-alist)
679 (nconc minor-mode-alist
680 (list '(isearch-mode isearch-mode))))
682 (defvar-local isearch-mode nil) ;; Name of the minor mode, if non-nil.
684 (define-key global-map "\C-s" 'isearch-forward)
685 (define-key esc-map "\C-s" 'isearch-forward-regexp)
686 (define-key global-map "\C-r" 'isearch-backward)
687 (define-key esc-map "\C-r" 'isearch-backward-regexp)
688 (define-key search-map "w" 'isearch-forward-word)
689 (define-key search-map "_" 'isearch-forward-symbol)
690 (define-key search-map "." 'isearch-forward-symbol-at-point)
692 ;; Entry points to isearch-mode.
694 (defun isearch-forward (&optional regexp-p no-recursive-edit)
696 Do incremental search forward.
697 With a prefix argument, do an incremental regular expression search instead.
698 \\<isearch-mode-map>
699 As you type characters, they add to the search string and are found.
700 The following non-printing keys are bound in `isearch-mode-map'.
702 Type \\[isearch-delete-char] to cancel last input item from end of search string.
703 Type \\[isearch-exit] to exit, leaving point at location found.
704 Type LFD (C-j) to match end of line.
705 Type \\[isearch-repeat-forward] to search again forward,\
706 \\[isearch-repeat-backward] to search again backward.
707 Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
708 onto the end of the search string, and search for it.
709 Type \\[isearch-del-char] to delete character from end of search string.
710 Type \\[isearch-yank-char] to yank char from buffer onto end of search\
711 string and search for it.
712 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
713 and search for it.
714 Type \\[isearch-yank-kill] to yank the last string of killed text.
715 Type \\[isearch-yank-pop] to replace string just yanked into search prompt
716 with string killed before it.
717 Type \\[isearch-quote-char] to quote control character to search for it.
718 Type \\[isearch-char-by-name] to add a character to search by Unicode name,\
719 with completion.
720 \\[isearch-abort] while searching or when search has failed cancels input\
721 back to what has
722 been found successfully.
723 \\[isearch-abort] when search is successful aborts and moves point to\
724 starting point.
726 If you try to exit with the search string still empty, it invokes
727 nonincremental search.
729 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
730 Type \\[isearch-toggle-invisible] to toggle search in invisible text.
731 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
732 Type \\[isearch-toggle-word] to toggle word mode.
733 Type \\[isearch-toggle-symbol] to toggle symbol mode.
734 Type \\[isearch-toggle-char-fold] to toggle character folding.
736 Type \\[isearch-toggle-lax-whitespace] to toggle whitespace matching.
737 In incremental searches, a space or spaces normally matches any whitespace
738 defined by the variable `search-whitespace-regexp'; see also the variables
739 `isearch-lax-whitespace' and `isearch-regexp-lax-whitespace'.
741 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
743 Also supported is a search ring of the previous 16 search strings.
744 Type \\[isearch-ring-advance] to search for the next item in the search ring.
745 Type \\[isearch-ring-retreat] to search for the previous item in the search\
746 ring.
747 Type \\[isearch-complete] to complete the search string using the search ring.
749 Type \\[isearch-query-replace] to run `query-replace' with string to\
750 replace from last search string.
751 Type \\[isearch-query-replace-regexp] to run `query-replace-regexp'\
752 with the last search string.
753 Type \\[isearch-occur] to run `occur' that shows\
754 the last search string.
755 Type \\[isearch-highlight-regexp] to run `highlight-regexp'\
756 that highlights the last search string.
758 Type \\[isearch-describe-bindings] to display all Isearch key bindings.
759 Type \\[isearch-describe-key] to display documentation of Isearch key.
760 Type \\[isearch-describe-mode] to display documentation of Isearch mode.
762 If an input method is turned on in the current buffer, that input
763 method is also active while you are typing characters to search.
764 To toggle the input method, type \\[isearch-toggle-input-method]. \
765 It also toggles the input
766 method in the current buffer.
768 To use a different input method for searching, type \
769 \\[isearch-toggle-specified-input-method],
770 and specify an input method you want to use.
772 The above keys, bound in `isearch-mode-map', are often controlled by
773 options; do \\[apropos] on search-.* to find them.
774 Other control and meta characters terminate the search
775 and are then executed normally (depending on `search-exit-option').
776 Likewise for function keys and mouse button events.
778 If this function is called non-interactively with a nil NO-RECURSIVE-EDIT,
779 it does not return to the calling function until the search is done.
780 See the function `isearch-mode' for more information."
782 (interactive "P\np")
783 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
785 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
786 "Do incremental search forward for regular expression.
787 With a prefix argument, do a regular string search instead.
788 Like ordinary incremental search except that your input is treated
789 as a regexp. See the command `isearch-forward' for more information.
791 In incremental searches, a space or spaces normally matches any
792 whitespace defined by the variable `search-whitespace-regexp'.
793 To search for a literal space and nothing else, enter C-q SPC.
794 To toggle whitespace matching, use `isearch-toggle-lax-whitespace'.
795 This command does not support character folding."
796 (interactive "P\np")
797 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
799 (defun isearch-forward-word (&optional not-word no-recursive-edit)
800 "Do incremental search forward for a sequence of words.
801 With a prefix argument, do a regular string search instead.
802 Like ordinary incremental search except that your input is treated
803 as a sequence of words without regard to how the words are separated.
804 See the command `isearch-forward' for more information.
805 This command does not support character folding, and lax space matching
806 has no effect on it."
807 (interactive "P\np")
808 (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
810 (defun isearch-forward-symbol (&optional _not-symbol no-recursive-edit)
811 "Do incremental search forward for a symbol.
812 The prefix argument is currently unused.
813 Like ordinary incremental search except that your input is treated
814 as a symbol surrounded by symbol boundary constructs \\_< and \\_>.
815 See the command `isearch-forward' for more information.
816 This command does not support character folding, and lax space matching
817 has no effect on it."
818 (interactive "P\np")
819 (isearch-mode t nil nil (not no-recursive-edit) 'isearch-symbol-regexp))
821 (defun isearch-backward (&optional regexp-p no-recursive-edit)
822 "Do incremental search backward.
823 With a prefix argument, do a regular expression search instead.
824 See the command `isearch-forward' for more information."
825 (interactive "P\np")
826 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
828 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
829 "Do incremental search backward for regular expression.
830 With a prefix argument, do a regular string search instead.
831 Like ordinary incremental search except that your input is treated
832 as a regexp. See the command `isearch-forward-regexp' for more information."
833 (interactive "P\np")
834 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
836 (defun isearch-forward-symbol-at-point ()
837 "Do incremental search forward for a symbol found near point.
838 Like ordinary incremental search except that the symbol found at point
839 is added to the search string initially as a regexp surrounded
840 by symbol boundary constructs \\_< and \\_>.
841 See the command `isearch-forward-symbol' for more information."
842 (interactive)
843 (isearch-forward-symbol nil 1)
844 (let ((bounds (find-tag-default-bounds)))
845 (cond
846 (bounds
847 (when (< (car bounds) (point))
848 (goto-char (car bounds)))
849 (isearch-yank-string
850 (buffer-substring-no-properties (car bounds) (cdr bounds))))
852 (setq isearch-error "No symbol at point")
853 (isearch-push-state)
854 (isearch-update)))))
857 ;; isearch-mode only sets up incremental search for the minor mode.
858 ;; All the work is done by the isearch-mode commands.
860 ;; Not used yet:
861 ;;(defvar isearch-commands '(isearch-forward isearch-backward
862 ;; isearch-forward-regexp isearch-backward-regexp)
863 ;; "List of commands for which isearch-mode does not recursive-edit.")
865 (defun isearch-mode (forward &optional regexp op-fun recursive-edit regexp-function)
866 "Start Isearch minor mode.
867 It is called by the function `isearch-forward' and other related functions.
869 The non-nil arg FORWARD means searching in the forward direction.
871 The non-nil arg REGEXP does an incremental regular expression search.
873 The arg OP-FUN is a function to be called after each input character
874 is processed. (It is not called after characters that exit the search.)
876 When the arg RECURSIVE-EDIT is non-nil, this function behaves modally and
877 does not return to the calling function until the search is completed.
878 To behave this way it enters a recursive-edit and exits it when done
879 isearching.
881 The arg REGEXP-FUNCTION, if non-nil, should be a function. It is
882 used to set the value of `isearch-regexp-function'."
884 ;; Initialize global vars.
885 (setq isearch-forward forward
886 isearch-regexp (or regexp
887 (and (not regexp-function)
888 (eq search-default-mode t)))
889 isearch-regexp-function (or regexp-function
890 (and (functionp search-default-mode)
891 (not regexp)
892 search-default-mode))
893 isearch-op-fun op-fun
894 isearch-last-case-fold-search isearch-case-fold-search
895 isearch-case-fold-search case-fold-search
896 isearch-invisible search-invisible
897 isearch-string ""
898 isearch-message ""
899 isearch-cmds nil
900 isearch-success t
901 isearch-wrapped nil
902 isearch-barrier (point)
903 isearch-adjusted nil
904 isearch-yank-flag nil
905 isearch-error nil
906 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
907 (> (window-height)
908 (* 4
909 (abs search-slow-window-lines))))
910 isearch-other-end nil
911 isearch-small-window nil
912 isearch-just-started t
913 isearch-start-hscroll (window-hscroll)
915 isearch-opoint (point)
916 search-ring-yank-pointer nil
917 isearch-opened-overlays nil
918 isearch-input-method-function input-method-function
919 isearch-input-method-local-p (local-variable-p 'input-method-function)
920 regexp-search-ring-yank-pointer nil
922 ;; Save the original value of `minibuffer-message-timeout', and
923 ;; set it to nil so that isearch's messages don't get timed out.
924 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
925 minibuffer-message-timeout nil)
927 ;; We must bypass input method while reading key. When a user type
928 ;; printable character, appropriate input method is turned on in
929 ;; minibuffer to read multibyte characters.
930 (or isearch-input-method-local-p
931 (make-local-variable 'input-method-function))
932 (setq input-method-function nil)
934 (looking-at "")
935 (setq isearch-window-configuration
936 (if isearch-slow-terminal-mode (current-window-configuration) nil))
938 ;; Maybe make minibuffer frame visible and/or raise it.
939 (let ((frame (window-frame (minibuffer-window))))
940 (unless (memq (frame-live-p frame) '(nil t))
941 (unless (frame-visible-p frame)
942 (make-frame-visible frame))
943 (if minibuffer-auto-raise
944 (raise-frame frame))))
946 (setq isearch-mode " Isearch") ;; forward? regexp?
947 (force-mode-line-update)
949 (setq overriding-terminal-local-map isearch-mode-map)
950 (run-hooks 'isearch-mode-hook)
951 ;; Remember the initial map possibly modified
952 ;; by external packages in isearch-mode-hook. (Bug#16035)
953 (setq isearch--saved-overriding-local-map overriding-terminal-local-map)
955 ;; Pushing the initial state used to be before running isearch-mode-hook,
956 ;; but a hook might set `isearch-push-state-function' used in
957 ;; `isearch-push-state' to save mode-specific initial state. (Bug#4994)
958 (isearch-push-state)
960 (isearch-update)
962 (add-hook 'pre-command-hook 'isearch-pre-command-hook)
963 (add-hook 'post-command-hook 'isearch-post-command-hook)
964 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
965 (add-hook 'kbd-macro-termination-hook 'isearch-done)
967 ;; isearch-mode can be made modal (in the sense of not returning to
968 ;; the calling function until searching is completed) by entering
969 ;; a recursive-edit and exiting it when done isearching.
970 (if recursive-edit
971 (let ((isearch-recursive-edit t))
972 (recursive-edit)))
973 isearch-success)
976 ;; Some high level utilities. Others below.
977 (defvar isearch--current-buffer nil)
979 (defun isearch-update ()
980 "This is called after every isearch command to update the display.
981 The second last thing it does is to run `isearch-update-post-hook'.
982 The last thing is to trigger a new round of lazy highlighting."
983 (unless (eq (current-buffer) isearch--current-buffer)
984 (when (buffer-live-p isearch--current-buffer)
985 (with-current-buffer isearch--current-buffer
986 (setq cursor-sensor-inhibit (delq 'isearch cursor-sensor-inhibit))))
987 (setq isearch--current-buffer (current-buffer))
988 (make-local-variable 'cursor-sensor-inhibit)
989 ;; Suspend things like cursor-intangible during Isearch so we can search
990 ;; even within intangible text.
991 (push 'isearch cursor-sensor-inhibit))
993 (if (and (null unread-command-events)
994 (null executing-kbd-macro))
995 (progn
996 (if (not (input-pending-p))
997 (funcall (or isearch-message-function #'isearch-message)))
998 (if (and isearch-slow-terminal-mode
999 (not (or isearch-small-window
1000 (pos-visible-in-window-group-p))))
1001 (let ((found-point (point)))
1002 (setq isearch-small-window t)
1003 (move-to-window-line 0)
1004 (let ((window-min-height 1))
1005 (split-window nil (if (< search-slow-window-lines 0)
1006 (1+ (- search-slow-window-lines))
1007 (- (window-height)
1008 (1+ search-slow-window-lines)))))
1009 (if (< search-slow-window-lines 0)
1010 (progn (vertical-motion (- 1 search-slow-window-lines))
1011 (set-window-start (next-window) (point))
1012 (set-window-hscroll (next-window)
1013 (window-hscroll))
1014 (set-window-hscroll (selected-window) 0))
1015 (other-window 1))
1016 (goto-char found-point))
1017 ;; Keep same hscrolling as at the start of the search when possible
1018 (let ((current-scroll (window-hscroll))
1019 visible-p)
1020 (set-window-hscroll (selected-window) isearch-start-hscroll)
1021 (setq visible-p (pos-visible-in-window-group-p nil nil t))
1022 (if (or (not visible-p)
1023 ;; When point is not visible because of hscroll,
1024 ;; pos-visible-in-window-group-p returns non-nil, but
1025 ;; the X coordinate it returns is 1 pixel beyond
1026 ;; the last visible one.
1027 (>= (car visible-p)
1028 (* (window-max-chars-per-line) (frame-char-width))))
1029 (set-window-hscroll (selected-window) current-scroll))))
1030 (if isearch-other-end
1031 (if (< isearch-other-end (point)) ; isearch-forward?
1032 (isearch-highlight isearch-other-end (point))
1033 (isearch-highlight (point) isearch-other-end))
1034 (isearch-dehighlight))))
1035 (setq ;; quit-flag nil not for isearch-mode
1036 isearch-adjusted nil
1037 isearch-yank-flag nil)
1038 ;; We must prevent the point moving to the end of composition when a
1039 ;; part of the composition has just been searched.
1040 (setq disable-point-adjustment t)
1041 (run-hooks 'isearch-update-post-hook)
1042 (when isearch-lazy-highlight
1043 (isearch-lazy-highlight-new-loop)))
1045 (defun isearch-done (&optional nopush edit)
1046 "Exit Isearch mode.
1047 For successful search, pass no args.
1048 For a failing search, NOPUSH is t.
1049 For going to the minibuffer to edit the search string,
1050 NOPUSH is t and EDIT is t."
1052 (when isearch-resume-in-command-history
1053 (add-to-history 'command-history
1054 `(isearch-resume ,isearch-string ,isearch-regexp
1055 ,isearch-regexp-function ,isearch-forward
1056 ,isearch-message
1057 ',isearch-case-fold-search)))
1059 (remove-hook 'pre-command-hook 'isearch-pre-command-hook)
1060 (remove-hook 'post-command-hook 'isearch-post-command-hook)
1061 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
1062 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
1063 (setq isearch-lazy-highlight-start nil)
1064 (when (buffer-live-p isearch--current-buffer)
1065 (with-current-buffer isearch--current-buffer
1066 (setq isearch--current-buffer nil)
1067 (setq cursor-sensor-inhibit (delq 'isearch cursor-sensor-inhibit))))
1069 ;; Called by all commands that terminate isearch-mode.
1070 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
1071 (setq overriding-terminal-local-map nil)
1072 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
1073 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
1074 (isearch-dehighlight)
1075 (lazy-highlight-cleanup lazy-highlight-cleanup)
1076 (let ((found-start (window-group-start))
1077 (found-point (point)))
1078 (when isearch-window-configuration
1079 (set-window-configuration isearch-window-configuration)
1080 (if isearch-small-window
1081 (goto-char found-point)
1082 ;; set-window-configuration clobbers window-start; restore it.
1083 ;; This has an annoying side effect of clearing the last_modiff
1084 ;; field of the window, which can cause unwanted scrolling,
1085 ;; so don't do it unless truly necessary.
1086 (set-window-group-start (selected-window) found-start t))))
1088 (setq isearch-mode nil)
1089 (if isearch-input-method-local-p
1090 (setq input-method-function isearch-input-method-function)
1091 (kill-local-variable 'input-method-function))
1093 (force-mode-line-update)
1095 ;; If we ended in the middle of some intangible text,
1096 ;; move to the further end of that intangible text.
1097 (let ((after (if (eobp) nil
1098 (get-text-property (point) 'intangible)))
1099 (before (if (bobp) nil
1100 (get-text-property (1- (point)) 'intangible))))
1101 (when (and before after (eq before after))
1102 (goto-char
1103 (if isearch-forward
1104 (next-single-property-change (point) 'intangible)
1105 (previous-single-property-change (point) 'intangible)))))
1107 (if (and (> (length isearch-string) 0) (not nopush))
1108 ;; Update the ring data.
1109 (isearch-update-ring isearch-string isearch-regexp))
1111 (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
1112 (run-hooks 'isearch-mode-end-hook))
1114 ;; If there was movement, mark the starting position.
1115 ;; Maybe should test difference between and set mark only if > threshold.
1116 (if (/= (point) isearch-opoint)
1117 (or (and transient-mark-mode mark-active)
1118 (progn
1119 (push-mark isearch-opoint t)
1120 (or executing-kbd-macro (> (minibuffer-depth) 0) edit
1121 (message "Mark saved where search started")))))
1123 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
1125 (defun isearch-update-ring (string &optional regexp)
1126 "Add STRING to the beginning of the search ring.
1127 REGEXP if non-nil says use the regexp search ring."
1128 (let ((history-delete-duplicates t))
1129 (add-to-history
1130 (if regexp 'regexp-search-ring 'search-ring)
1131 (isearch-string-propertize string)
1132 (if regexp regexp-search-ring-max search-ring-max)
1133 t)))
1135 (defun isearch-string-propertize (string &optional properties)
1136 "Add isearch properties to the isearch string."
1137 (unless properties
1138 (setq properties `(isearch-case-fold-search ,isearch-case-fold-search))
1139 (unless isearch-regexp
1140 (setq properties (append properties `(isearch-regexp-function ,isearch-regexp-function)))))
1141 (apply 'propertize string properties))
1143 (defun isearch-update-from-string-properties (string)
1144 "Update isearch properties from the isearch string"
1145 (when (plist-member (text-properties-at 0 string) 'isearch-case-fold-search)
1146 (setq isearch-case-fold-search
1147 (get-text-property 0 'isearch-case-fold-search string)))
1148 (when (plist-member (text-properties-at 0 string) 'isearch-regexp-function)
1149 (setq isearch-regexp-function
1150 (get-text-property 0 'isearch-regexp-function string))))
1153 ;; The search status structure and stack.
1155 (cl-defstruct (isearch--state
1156 (:constructor nil)
1157 (:copier nil)
1158 (:constructor isearch--get-state
1159 (&aux
1160 (string isearch-string)
1161 (message isearch-message)
1162 (point (point))
1163 (success isearch-success)
1164 (forward isearch-forward)
1165 (other-end isearch-other-end)
1166 (word isearch-regexp-function)
1167 (error isearch-error)
1168 (wrapped isearch-wrapped)
1169 (barrier isearch-barrier)
1170 (case-fold-search isearch-case-fold-search)
1171 (pop-fun (if isearch-push-state-function
1172 (funcall isearch-push-state-function))))))
1173 (string nil :read-only t)
1174 (message nil :read-only t)
1175 (point nil :read-only t)
1176 (success nil :read-only t)
1177 (forward nil :read-only t)
1178 (other-end nil :read-only t)
1179 (word nil :read-only t)
1180 (error nil :read-only t)
1181 (wrapped nil :read-only t)
1182 (barrier nil :read-only t)
1183 (case-fold-search nil :read-only t)
1184 (pop-fun nil :read-only t))
1186 (defun isearch--set-state (cmd)
1187 (setq isearch-string (isearch--state-string cmd)
1188 isearch-message (isearch--state-message cmd)
1189 isearch-success (isearch--state-success cmd)
1190 isearch-forward (isearch--state-forward cmd)
1191 isearch-other-end (isearch--state-other-end cmd)
1192 isearch-regexp-function (isearch--state-word cmd)
1193 isearch-error (isearch--state-error cmd)
1194 isearch-wrapped (isearch--state-wrapped cmd)
1195 isearch-barrier (isearch--state-barrier cmd)
1196 isearch-case-fold-search (isearch--state-case-fold-search cmd))
1197 (if (functionp (isearch--state-pop-fun cmd))
1198 (funcall (isearch--state-pop-fun cmd) cmd))
1199 (goto-char (isearch--state-point cmd)))
1201 (defun isearch-pop-state ()
1202 (setq isearch-cmds (cdr isearch-cmds))
1203 (isearch--set-state (car isearch-cmds)))
1205 (defun isearch-push-state ()
1206 (push (isearch--get-state) isearch-cmds))
1209 ;; Commands active while inside of the isearch minor mode.
1211 (defun isearch-exit ()
1212 "Exit search normally.
1213 However, if this is the first command after starting incremental
1214 search and `search-nonincremental-instead' is non-nil, do a
1215 nonincremental search instead via `isearch-edit-string'."
1216 (interactive)
1217 (if (and search-nonincremental-instead
1218 (= 0 (length isearch-string)))
1219 (let ((isearch-nonincremental t))
1220 (isearch-edit-string)) ;; this calls isearch-done as well
1221 (isearch-done))
1222 (isearch-clean-overlays))
1224 (defun isearch-fail-pos (&optional msg)
1225 "Return position of first mismatch in search string, or nil if none.
1226 If MSG is non-nil, use variable `isearch-message', otherwise `isearch-string'."
1227 (let ((cmds isearch-cmds)
1228 (curr-msg (if msg isearch-message isearch-string))
1229 succ-msg)
1230 (when (or (not isearch-success) isearch-error)
1231 (while (and cmds
1232 (or (not (isearch--state-success (car cmds)))
1233 (isearch--state-error (car cmds))))
1234 (pop cmds))
1235 (setq succ-msg (and cmds (if msg (isearch--state-message (car cmds))
1236 (isearch--state-string (car cmds)))))
1237 (if (and (stringp succ-msg)
1238 (< (length succ-msg) (length curr-msg))
1239 (equal succ-msg
1240 (substring curr-msg 0 (length succ-msg))))
1241 (length succ-msg)
1242 0))))
1244 (define-obsolete-variable-alias 'isearch-new-word
1245 'isearch-new-regexp-function "25.1")
1247 (defvar isearch-new-regexp-function nil
1248 "Holds the next `isearch-regexp-function' inside `with-isearch-suspended'.
1249 If this is set inside code wrapped by the macro
1250 `with-isearch-suspended', then the value set will be used as the
1251 `isearch-regexp-function' once isearch resumes.")
1253 (defvar isearch-suspended nil)
1255 (defmacro with-isearch-suspended (&rest body)
1256 "Exit Isearch mode, run BODY, and reinvoke the pending search.
1257 You can update the global isearch variables by setting new values to
1258 `isearch-new-string', `isearch-new-message', `isearch-new-forward',
1259 `isearch-new-regexp-function', `isearch-new-case-fold', `isearch-new-nonincremental'."
1260 ;; This code is very hairy for several reasons, explained in the code.
1261 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
1262 ;; If there were a way to catch any change of buffer from the minibuffer,
1263 ;; this could be simplified greatly.
1264 ;; Editing doesn't back up the search point. Should it?
1265 `(condition-case nil
1266 (progn
1267 (let ((isearch-new-nonincremental isearch-nonincremental)
1269 ;; Locally bind all isearch global variables to protect them
1270 ;; from recursive isearching.
1271 ;; isearch-string -message and -forward are not bound
1272 ;; so they may be changed. Instead, save the values.
1273 (isearch-new-string isearch-string)
1274 (isearch-new-message isearch-message)
1275 (isearch-new-forward isearch-forward)
1276 (isearch-new-regexp-function isearch-regexp-function)
1277 (isearch-new-case-fold isearch-case-fold-search)
1279 (isearch-regexp isearch-regexp)
1280 (isearch-op-fun isearch-op-fun)
1281 (isearch-cmds isearch-cmds)
1282 (isearch-success isearch-success)
1283 (isearch-wrapped isearch-wrapped)
1284 (isearch-barrier isearch-barrier)
1285 (isearch-adjusted isearch-adjusted)
1286 (isearch-yank-flag isearch-yank-flag)
1287 (isearch-error isearch-error)
1289 (multi-isearch-file-list-new multi-isearch-file-list)
1290 (multi-isearch-buffer-list-new multi-isearch-buffer-list)
1291 (multi-isearch-next-buffer-function multi-isearch-next-buffer-current-function)
1292 (multi-isearch-current-buffer-new multi-isearch-current-buffer)
1293 ;;; Don't bind this. We want isearch-search, below, to set it.
1294 ;;; And the old value won't matter after that.
1295 ;;; (isearch-other-end isearch-other-end)
1296 ;;; Perhaps some of these other variables should be bound for a
1297 ;;; shorter period, ending before the next isearch-search.
1298 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
1299 (isearch-opoint isearch-opoint)
1300 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
1301 (isearch-small-window isearch-small-window)
1302 (isearch-recursive-edit isearch-recursive-edit)
1303 ;; Save current configuration so we can restore it here.
1304 (isearch-window-configuration (current-window-configuration))
1306 ;; This could protect the index of the search rings,
1307 ;; but we can't reliably count the number of typed M-p
1308 ;; in `read-from-minibuffer' to adjust the index accordingly.
1309 ;; So when the following is commented out, `isearch-mode'
1310 ;; below resets the index to the predictable value nil.
1311 ;; (search-ring-yank-pointer search-ring-yank-pointer)
1312 ;; (regexp-search-ring-yank-pointer regexp-search-ring-yank-pointer)
1314 ;; Temporarily restore `minibuffer-message-timeout'.
1315 (minibuffer-message-timeout
1316 isearch-original-minibuffer-message-timeout)
1317 (isearch-original-minibuffer-message-timeout
1318 isearch-original-minibuffer-message-timeout)
1319 old-point old-other-end)
1321 (setq isearch-suspended t)
1323 ;; Actually terminate isearching until editing is done.
1324 ;; This is so that the user can do anything without failure,
1325 ;; like switch buffers and start another isearch, and return.
1326 (condition-case nil
1327 (isearch-done t t)
1328 (exit nil)) ; was recursive editing
1330 ;; Save old point and isearch-other-end before reading from minibuffer
1331 ;; that can change their values.
1332 (setq old-point (point) old-other-end isearch-other-end)
1334 (unwind-protect
1335 (progn ,@body)
1337 (setq isearch-suspended nil)
1339 ;; Always resume isearching by restarting it.
1340 (isearch-mode isearch-forward
1341 isearch-regexp
1342 isearch-op-fun
1344 isearch-regexp-function)
1346 ;; Copy new local values to isearch globals
1347 (setq isearch-string isearch-new-string
1348 isearch-message isearch-new-message
1349 isearch-forward isearch-new-forward
1350 isearch-nonincremental isearch-new-nonincremental
1351 isearch-regexp-function isearch-new-regexp-function
1352 isearch-case-fold-search isearch-new-case-fold
1353 multi-isearch-current-buffer multi-isearch-current-buffer-new
1354 multi-isearch-file-list multi-isearch-file-list-new
1355 multi-isearch-buffer-list multi-isearch-buffer-list-new)
1357 (isearch-update-from-string-properties isearch-string)
1359 ;; Restore the minibuffer message before moving point.
1360 (funcall (or isearch-message-function #'isearch-message) nil t)
1362 ;; Set point at the start (end) of old match if forward (backward),
1363 ;; so after exiting minibuffer isearch resumes at the start (end)
1364 ;; of this match and can find it again.
1365 (if (and old-other-end (eq old-point (point))
1366 (eq isearch-forward isearch-new-forward))
1367 (goto-char old-other-end)))
1369 ;; Empty isearch-string means use default.
1370 (when (= 0 (length isearch-string))
1371 (setq isearch-string (or (car (if isearch-regexp
1372 regexp-search-ring
1373 search-ring))
1376 isearch-message
1377 (mapconcat 'isearch-text-char-description
1378 isearch-string ""))
1379 ;; After taking the last element, adjust ring to previous one.
1380 (isearch-ring-adjust1 nil)))
1382 ;; This used to push the state as of before this C-s, but it adds
1383 ;; an inconsistent state where part of variables are from the
1384 ;; previous search (e.g. `isearch-success'), and part of variables
1385 ;; are just entered from the minibuffer (e.g. `isearch-string').
1386 ;; (isearch-push-state)
1388 ;; Reinvoke the pending search.
1389 (isearch-search)
1390 (isearch-push-state) ; this pushes the correct state
1391 (isearch-update)
1392 (if isearch-nonincremental
1393 (progn
1394 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
1395 (isearch-done)
1396 ;; The search done message is confusing when the string
1397 ;; is empty, so erase it.
1398 (if (equal isearch-string "")
1399 (message "")))))
1401 (quit ; handle abort-recursive-edit
1402 (setq isearch-suspended nil)
1403 (isearch-abort) ;; outside of let to restore outside global values
1406 (defvar minibuffer-history-symbol) ;; from external package gmhist.el
1408 (defun isearch-edit-string ()
1409 "Edit the search string in the minibuffer.
1410 The following additional command keys are active while editing.
1411 \\<minibuffer-local-isearch-map>
1412 \\[exit-minibuffer] to resume incremental searching with the edited string.
1413 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
1414 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
1415 \\[isearch-complete-edit] to complete the search string using the search ring."
1416 (interactive)
1417 (with-isearch-suspended
1418 (let* ((message-log-max nil)
1419 ;; Don't add a new search string to the search ring here
1420 ;; in `read-from-minibuffer'. It should be added only
1421 ;; by `isearch-update-ring' called from `isearch-done'.
1422 (history-add-new-input nil)
1423 ;; Binding minibuffer-history-symbol to nil is a work-around
1424 ;; for some incompatibility with gmhist.
1425 (minibuffer-history-symbol)
1426 ;; Search string might have meta information on text properties.
1427 (minibuffer-allow-text-properties t))
1428 (setq isearch-new-string
1429 (read-from-minibuffer
1430 (isearch-message-prefix nil isearch-nonincremental)
1431 (cons isearch-string (1+ (or (isearch-fail-pos)
1432 (length isearch-string))))
1433 minibuffer-local-isearch-map nil
1434 (if isearch-regexp
1435 (cons 'regexp-search-ring
1436 (1+ (or regexp-search-ring-yank-pointer -1)))
1437 (cons 'search-ring
1438 (1+ (or search-ring-yank-pointer -1))))
1439 nil t)
1440 isearch-new-message
1441 (mapconcat 'isearch-text-char-description
1442 isearch-new-string "")))))
1444 (defun isearch-nonincremental-exit-minibuffer ()
1445 (interactive)
1446 (setq isearch-new-nonincremental t)
1447 (exit-minibuffer))
1448 ;; It makes no sense to change the value of `isearch-new-nonincremental'
1449 ;; from nil to t during `isearch-edit-string'. Thus marked as obsolete.
1450 (make-obsolete 'isearch-nonincremental-exit-minibuffer 'exit-minibuffer "24.4")
1452 (defun isearch-forward-exit-minibuffer ()
1453 "Resume isearching forward from the minibuffer that edits the search string."
1454 (interactive)
1455 (setq isearch-new-forward t isearch-new-nonincremental nil)
1456 (exit-minibuffer))
1458 (defun isearch-reverse-exit-minibuffer ()
1459 "Resume isearching backward from the minibuffer that edits the search string."
1460 (interactive)
1461 (setq isearch-new-forward nil isearch-new-nonincremental nil)
1462 (exit-minibuffer))
1464 (defun isearch-cancel ()
1465 "Terminate the search and go back to the starting point."
1466 (interactive)
1467 (if (and isearch-push-state-function isearch-cmds)
1468 ;; For defined push-state function, restore the first state.
1469 ;; This calls pop-state function and restores original point.
1470 (let ((isearch-cmds (last isearch-cmds)))
1471 (isearch--set-state (car isearch-cmds)))
1472 (goto-char isearch-opoint))
1473 (isearch-done t) ; Exit isearch..
1474 (isearch-clean-overlays)
1475 (signal 'quit nil)) ; ..and pass on quit signal.
1477 (defun isearch-abort ()
1478 "Abort incremental search mode if searching is successful, signaling quit.
1479 Otherwise, revert to previous successful search and continue searching.
1480 Use `isearch-exit' to quit without signaling."
1481 (interactive)
1482 ;; (ding) signal instead below, if quitting
1483 (discard-input)
1484 (if (and isearch-success (not isearch-error))
1485 ;; If search is successful and has no incomplete regexp,
1486 ;; move back to starting point and really do quit.
1487 (progn
1488 (setq isearch-success nil)
1489 (isearch-cancel))
1490 ;; If search is failing, or has an incomplete regexp,
1491 ;; rub out until it is once more successful.
1492 (while (or (not isearch-success) isearch-error)
1493 (isearch-pop-state))
1494 (isearch-update)))
1496 (defun isearch-repeat (direction)
1497 ;; Utility for isearch-repeat-forward and -backward.
1498 (if (eq isearch-forward (eq direction 'forward))
1499 ;; C-s in forward or C-r in reverse.
1500 (if (equal isearch-string "")
1501 ;; If search string is empty, use last one.
1502 (if (null (if isearch-regexp regexp-search-ring search-ring))
1503 (setq isearch-error "No previous search string")
1504 (setq isearch-string
1505 (car (if isearch-regexp regexp-search-ring search-ring))
1506 isearch-message
1507 (mapconcat 'isearch-text-char-description
1508 isearch-string "")
1509 isearch-case-fold-search isearch-last-case-fold-search)
1510 ;; After taking the last element, adjust ring to previous one.
1511 (isearch-ring-adjust1 nil))
1512 ;; If already have what to search for, repeat it.
1513 (or isearch-success
1514 (progn
1515 ;; Set isearch-wrapped before calling isearch-wrap-function
1516 (setq isearch-wrapped t)
1517 (if isearch-wrap-function
1518 (funcall isearch-wrap-function)
1519 (goto-char (if isearch-forward (point-min) (point-max)))))))
1520 ;; C-s in reverse or C-r in forward, change direction.
1521 (setq isearch-forward (not isearch-forward)
1522 isearch-success t))
1524 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1526 (if (equal isearch-string "")
1527 (setq isearch-success t)
1528 (if (and isearch-success
1529 (equal (point) isearch-other-end)
1530 (not isearch-just-started))
1531 ;; If repeating a search that found
1532 ;; an empty string, ensure we advance.
1533 (if (if isearch-forward (eobp) (bobp))
1534 ;; If there's nowhere to advance to, fail (and wrap next time).
1535 (progn
1536 (setq isearch-success nil)
1537 (ding))
1538 (forward-char (if isearch-forward 1 -1))
1539 (isearch-search))
1540 (isearch-search)))
1542 (isearch-push-state)
1543 (isearch-update))
1545 (defun isearch-repeat-forward ()
1546 "Repeat incremental search forwards."
1547 (interactive)
1548 (isearch-repeat 'forward))
1550 (defun isearch-repeat-backward ()
1551 "Repeat incremental search backwards."
1552 (interactive)
1553 (isearch-repeat 'backward))
1556 ;;; Toggles for `isearch-regexp-function' and `search-default-mode'.
1557 (defmacro isearch-define-mode-toggle (mode key function &optional docstring &rest body)
1558 "Define a command called `isearch-toggle-MODE' and bind it to `M-s KEY'.
1559 The first line of the command's docstring is auto-generated, the
1560 remainder may be provided in DOCSTRING.
1561 If FUNCTION is a symbol, this command first toggles the value of
1562 `isearch-regexp-function' between nil and FUNCTION. Also set the
1563 `isearch-message-prefix' property of FUNCTION.
1564 The command then executes BODY and updates the isearch prompt."
1565 (declare (indent defun))
1566 (let ((command-name (intern (format "isearch-toggle-%s" mode)))
1567 (key (concat "\M-s" key)))
1568 `(progn
1569 (defun ,command-name ()
1570 ,(format "Toggle %s searching on or off.%s" mode
1571 (if docstring (concat "\n" docstring) ""))
1572 (interactive)
1573 ,@(when function
1574 `((setq isearch-regexp-function
1575 (unless (eq isearch-regexp-function #',function)
1576 #',function))
1577 (setq isearch-regexp nil)))
1578 ,@body
1579 (setq isearch-success t isearch-adjusted t)
1580 (isearch-update))
1581 (define-key isearch-mode-map ,key #',command-name)
1582 ,@(when (and function (symbolp function))
1583 `((put ',function 'isearch-message-prefix ,(format "%s " mode))
1584 (put ',function :advertised-binding ,key)
1585 (cl-callf (lambda (types) (cons 'choice
1586 (cons '(const :tag ,(capitalize (format "%s search" mode)) ,function)
1587 (cdr types))))
1588 (get 'search-default-mode 'custom-type)))))))
1590 (isearch-define-mode-toggle word "w" word-search-regexp "\
1591 Turning on word search turns off regexp mode.")
1592 (isearch-define-mode-toggle symbol "_" isearch-symbol-regexp "\
1593 Turning on symbol search turns off regexp mode.")
1594 (isearch-define-mode-toggle char-fold "'" char-fold-to-regexp "\
1595 Turning on character-folding turns off regexp mode.")
1597 (isearch-define-mode-toggle regexp "r" nil nil
1598 (setq isearch-regexp (not isearch-regexp))
1599 (if isearch-regexp (setq isearch-regexp-function nil)))
1601 (defun isearch--momentary-message (string)
1602 "Print STRING at the end of the isearch prompt for 1 second"
1603 (let ((message-log-max nil))
1604 (message "%s%s%s"
1605 (isearch-message-prefix nil isearch-nonincremental)
1606 isearch-message
1607 (propertize (format " [%s]" string) 'face 'minibuffer-prompt)))
1608 (sit-for 1))
1610 (isearch-define-mode-toggle lax-whitespace " " nil
1611 "In ordinary search, toggles the value of the variable
1612 `isearch-lax-whitespace'. In regexp search, toggles the
1613 value of the variable `isearch-regexp-lax-whitespace'."
1614 (isearch--momentary-message
1615 (if (if isearch-regexp
1616 (setq isearch-regexp-lax-whitespace (not isearch-regexp-lax-whitespace))
1617 (setq isearch-lax-whitespace (not isearch-lax-whitespace)))
1618 "match spaces loosely"
1619 "match spaces literally")))
1621 (isearch-define-mode-toggle case-fold "c" nil
1622 "Toggles the value of the variable `isearch-case-fold-search'."
1623 (isearch--momentary-message
1624 (if (setq isearch-case-fold-search
1625 (if isearch-case-fold-search nil 'yes))
1626 "case insensitive"
1627 "case sensitive")))
1629 (isearch-define-mode-toggle invisible "i" nil
1630 "This determines whether to search inside invisible text or not.
1631 Toggles the variable `isearch-invisible' between values
1632 nil and a non-nil value of the option `search-invisible'
1633 \(or `open' if `search-invisible' is nil)."
1634 "match %svisible text"
1635 (isearch--momentary-message
1636 (if (setq isearch-invisible
1637 (if isearch-invisible
1638 nil (or search-invisible 'open)))
1639 "match invisible text"
1640 "match visible text")))
1643 ;; Word search
1645 (defun word-search-regexp (string &optional lax)
1646 "Return a regexp which matches words, ignoring punctuation.
1647 Given STRING, a string of words separated by word delimiters,
1648 compute a regexp that matches those exact words separated by
1649 arbitrary punctuation. If the string begins or ends in whitespace,
1650 the beginning or the end of the string matches arbitrary whitespace.
1651 Otherwise if LAX is non-nil, the beginning or the end of the string
1652 need not match a word boundary.
1654 Used in `word-search-forward', `word-search-backward',
1655 `word-search-forward-lax', `word-search-backward-lax'."
1656 (cond
1657 ((equal string "") "")
1658 ((string-match-p "\\`\\W+\\'" string) "\\W+")
1659 (t (concat
1660 (if (string-match-p "\\`\\W" string) "\\W+"
1661 "\\<")
1662 (mapconcat 'regexp-quote (split-string string "\\W+" t) "\\W+")
1663 (if (string-match-p "\\W\\'" string) "\\W+"
1664 (unless lax "\\>"))))))
1666 (defun word-search-backward (string &optional bound noerror count)
1667 "Search backward from point for STRING, ignoring differences in punctuation.
1668 Set point to the beginning of the occurrence found, and return point.
1669 An optional second argument bounds the search; it is a buffer position.
1670 The match found must not begin before that position. A value of nil
1671 means search to the beginning of the accessible portion of the buffer.
1672 Optional third argument, if t, means if fail just return nil (no error).
1673 If not nil and not t, position at limit of search and return nil.
1674 Optional fourth argument COUNT, if a positive number, means to search
1675 for COUNT successive occurrences. If COUNT is negative, search
1676 forward, instead of backward, for -COUNT occurrences. A value of
1677 nil means the same as 1.
1678 With COUNT positive, the match found is the COUNTth to last one (or
1679 last, if COUNT is 1 or nil) in the buffer located entirely before
1680 the origin of the search; correspondingly with COUNT negative.
1682 Relies on the function `word-search-regexp' to convert a sequence
1683 of words in STRING to a regexp used to search words without regard
1684 to punctuation.
1685 This command does not support character folding, and lax space matching
1686 has no effect on it."
1687 (interactive "sWord search backward: ")
1688 (re-search-backward (word-search-regexp string nil) bound noerror count))
1690 (defun word-search-forward (string &optional bound noerror count)
1691 "Search forward from point for STRING, ignoring differences in punctuation.
1692 Set point to the end of the occurrence found, and return point.
1693 An optional second argument bounds the search; it is a buffer position.
1694 The match found must not end after that position. A value of nil
1695 means search to the end of the accessible portion of the buffer.
1696 Optional third argument, if t, means if fail just return nil (no error).
1697 If not nil and not t, move to limit of search and return nil.
1698 Optional fourth argument COUNT, if a positive number, means to search
1699 for COUNT successive occurrences. If COUNT is negative, search
1700 backward, instead of forward, for -COUNT occurrences. A value of
1701 nil means the same as 1.
1702 With COUNT positive, the match found is the COUNTth one (or first,
1703 if COUNT is 1 or nil) in the buffer located entirely after the
1704 origin of the search; correspondingly with COUNT negative.
1706 Relies on the function `word-search-regexp' to convert a sequence
1707 of words in STRING to a regexp used to search words without regard
1708 to punctuation.
1709 This command does not support character folding, and lax space matching
1710 has no effect on it."
1711 (interactive "sWord search: ")
1712 (re-search-forward (word-search-regexp string nil) bound noerror count))
1714 (defun word-search-backward-lax (string &optional bound noerror count)
1715 "Search backward from point for STRING, ignoring differences in punctuation.
1716 Set point to the beginning of the occurrence found, and return point.
1718 Unlike `word-search-backward', the end of STRING need not match a word
1719 boundary, unless STRING ends in whitespace.
1721 An optional second argument bounds the search; it is a buffer position.
1722 The match found must not begin before that position. A value of nil
1723 means search to the beginning of the accessible portion of the buffer.
1724 Optional third argument, if t, means if fail just return nil (no error).
1725 If not nil and not t, position at limit of search and return nil.
1726 Optional fourth argument COUNT, if a positive number, means to search
1727 for COUNT successive occurrences. If COUNT is negative, search
1728 forward, instead of backward, for -COUNT occurrences. A value of
1729 nil means the same as 1.
1730 With COUNT positive, the match found is the COUNTth to last one (or
1731 last, if COUNT is 1 or nil) in the buffer located entirely before
1732 the origin of the search; correspondingly with COUNT negative.
1734 Relies on the function `word-search-regexp' to convert a sequence
1735 of words in STRING to a regexp used to search words without regard
1736 to punctuation.
1737 This command does not support character folding, and lax space matching
1738 has no effect on it."
1739 (interactive "sWord search backward: ")
1740 (re-search-backward (word-search-regexp string t) bound noerror count))
1742 (defun word-search-forward-lax (string &optional bound noerror count)
1743 "Search forward from point for STRING, ignoring differences in punctuation.
1744 Set point to the end of the occurrence found, and return point.
1746 Unlike `word-search-forward', the end of STRING need not match a word
1747 boundary, unless STRING ends in whitespace.
1749 An optional second argument bounds the search; it is a buffer position.
1750 The match found must not end after that position. A value of nil
1751 means search to the end of the accessible portion of the buffer.
1752 Optional third argument, if t, means if fail just return nil (no error).
1753 If not nil and not t, move to limit of search and return nil.
1754 Optional fourth argument COUNT, if a positive number, means to search
1755 for COUNT successive occurrences. If COUNT is negative, search
1756 backward, instead of forward, for -COUNT occurrences. A value of
1757 nil means the same as 1.
1758 With COUNT positive, the match found is the COUNTth one (or first,
1759 if COUNT is 1 or nil) in the buffer located entirely after the
1760 origin of the search; correspondingly with COUNT negative.
1762 Relies on the function `word-search-regexp' to convert a sequence
1763 of words in STRING to a regexp used to search words without regard
1764 to punctuation.
1765 This command does not support character folding, and lax space matching
1766 has no effect on it."
1767 (interactive "sWord search: ")
1768 (re-search-forward (word-search-regexp string t) bound noerror count))
1770 ;; Symbol search
1772 (defun isearch-symbol-regexp (string &optional lax)
1773 "Return a regexp which matches STRING as a symbol.
1774 Creates a regexp where STRING is surrounded by symbol delimiters \\_< and \\_>.
1775 If there are more than one symbol, then compute a regexp that matches
1776 those exact symbols separated by non-symbol characters. If the string
1777 begins or ends in whitespace, the beginning or the end of the string
1778 matches arbitrary non-symbol whitespace. Otherwise if LAX is non-nil,
1779 the beginning or the end of the string need not match a symbol boundary."
1780 (let ((not-word-symbol-re
1781 ;; This regexp matches all syntaxes except word and symbol syntax.
1782 ;; FIXME: Replace it with something shorter if possible (bug#14602).
1783 "\\(?:\\s-\\|\\s.\\|\\s(\\|\\s)\\|\\s\"\\|\\s\\\\|\\s/\\|\\s$\\|\\s'\\|\\s<\\|\\s>\\|\\s@\\|\\s!\\|\\s|\\)+"))
1784 (cond
1785 ((equal string "") "")
1786 ((string-match-p (format "\\`%s\\'" not-word-symbol-re) string) not-word-symbol-re)
1787 (t (concat
1788 (if (string-match-p (format "\\`%s" not-word-symbol-re) string) not-word-symbol-re
1789 "\\_<")
1790 (mapconcat 'regexp-quote (split-string string not-word-symbol-re t) not-word-symbol-re)
1791 (if (string-match-p (format "%s\\'" not-word-symbol-re) string) not-word-symbol-re
1792 (unless lax "\\_>")))))))
1794 ;; Search with lax whitespace
1796 (defun search-forward-lax-whitespace (string &optional bound noerror count)
1797 "Search forward for STRING, matching a sequence of whitespace chars."
1798 (let ((search-spaces-regexp search-whitespace-regexp))
1799 (re-search-forward (regexp-quote string) bound noerror count)))
1801 (defun search-backward-lax-whitespace (string &optional bound noerror count)
1802 "Search backward for STRING, matching a sequence of whitespace chars."
1803 (let ((search-spaces-regexp search-whitespace-regexp))
1804 (re-search-backward (regexp-quote string) bound noerror count)))
1806 (defun re-search-forward-lax-whitespace (regexp &optional bound noerror count)
1807 "Search forward for REGEXP, matching a sequence of whitespace chars."
1808 (let ((search-spaces-regexp search-whitespace-regexp))
1809 (re-search-forward regexp bound noerror count)))
1811 (defun re-search-backward-lax-whitespace (regexp &optional bound noerror count)
1812 "Search backward for REGEXP, matching a sequence of whitespace chars."
1813 (let ((search-spaces-regexp search-whitespace-regexp))
1814 (re-search-backward regexp bound noerror count)))
1816 (dolist (old '(re-search-forward-lax-whitespace search-backward-lax-whitespace
1817 search-forward-lax-whitespace re-search-backward-lax-whitespace))
1818 (make-obsolete old
1819 "instead, use (let ((search-spaces-regexp search-whitespace-regexp))
1820 (re-search-... ...))"
1821 "25.1"))
1824 (defun isearch-query-replace (&optional arg regexp-flag)
1825 "Start `query-replace' with string to replace from last search string.
1826 The ARG (prefix arg if interactive), if non-nil, means replace
1827 only matches surrounded by word boundaries. A negative prefix
1828 arg means replace backward. Note that using the prefix arg
1829 is possible only when `isearch-allow-scroll' is non-nil or
1830 `isearch-allow-prefix' is non-nil, and it doesn't always provide the
1831 correct matches for `query-replace', so the preferred way to run word
1832 replacements from Isearch is `M-s w ... M-%'."
1833 (interactive
1834 (list current-prefix-arg))
1835 (barf-if-buffer-read-only)
1836 (if regexp-flag (setq isearch-regexp t))
1837 (let ((case-fold-search isearch-case-fold-search)
1838 ;; set `search-upper-case' to nil to not call
1839 ;; `isearch-no-upper-case-p' in `perform-replace'
1840 (search-upper-case nil)
1841 (search-invisible isearch-invisible)
1842 (replace-lax-whitespace
1843 isearch-lax-whitespace)
1844 (replace-regexp-lax-whitespace
1845 isearch-regexp-lax-whitespace)
1846 (delimited (and arg (not (eq arg '-))))
1847 (backward (and arg (eq arg '-)))
1848 ;; Set `isearch-recursive-edit' to nil to prevent calling
1849 ;; `exit-recursive-edit' in `isearch-done' that terminates
1850 ;; the execution of this command when it is non-nil.
1851 ;; We call `exit-recursive-edit' explicitly at the end below.
1852 (isearch-recursive-edit nil)
1853 (isearch-string-propertized
1854 (isearch-string-propertize isearch-string)))
1855 (isearch-done nil t)
1856 (isearch-clean-overlays)
1857 (if (and isearch-other-end
1858 (if backward
1859 (> isearch-other-end (point))
1860 (< isearch-other-end (point)))
1861 (not (and transient-mark-mode mark-active
1862 (if backward
1863 (> (mark) (point))
1864 (< (mark) (point))))))
1865 (goto-char isearch-other-end))
1866 (set query-replace-from-history-variable
1867 (cons isearch-string-propertized
1868 (symbol-value query-replace-from-history-variable)))
1869 (perform-replace
1870 isearch-string-propertized
1871 (query-replace-read-to
1872 isearch-string-propertized
1873 (concat "Query replace"
1874 (isearch--describe-regexp-mode (or delimited isearch-regexp-function) t)
1875 (if backward " backward" "")
1876 (if (use-region-p) " in region" ""))
1877 isearch-regexp)
1878 t isearch-regexp (or delimited isearch-regexp-function) nil nil
1879 (if (use-region-p) (region-beginning))
1880 (if (use-region-p) (region-end))
1881 backward))
1882 (and isearch-recursive-edit (exit-recursive-edit)))
1884 (defun isearch-query-replace-regexp (&optional arg)
1885 "Start `query-replace-regexp' with string to replace from last search string.
1886 See `isearch-query-replace' for more information."
1887 (interactive
1888 (list current-prefix-arg))
1889 (isearch-query-replace arg t))
1891 (defun isearch-occur (regexp &optional nlines)
1892 "Run `occur' using the last search string as the regexp.
1893 Interactively, REGEXP is constructed using the search string from the
1894 last search command. NLINES has the same meaning as in `occur'.
1896 If the last search command was a word search, REGEXP is computed from
1897 the search words, ignoring punctuation. If the last search
1898 command was a regular expression search, REGEXP is the regular
1899 expression used in that search. If the last search command searched
1900 for a literal string, REGEXP is constructed by quoting all the special
1901 characters in that string."
1902 (interactive
1903 (let* ((perform-collect (consp current-prefix-arg))
1904 (regexp (cond
1905 ((functionp isearch-regexp-function)
1906 (funcall isearch-regexp-function isearch-string))
1907 (isearch-regexp-function (word-search-regexp isearch-string))
1908 (isearch-regexp isearch-string)
1909 (t (regexp-quote isearch-string)))))
1910 (list regexp
1911 (if perform-collect
1912 ;; Perform collect operation
1913 (if (zerop (regexp-opt-depth regexp))
1914 ;; No subexpression so collect the entire match.
1915 "\\&"
1916 ;; Get the regexp for collection pattern.
1917 (let ((default (car occur-collect-regexp-history))
1918 regexp-collect)
1919 (with-isearch-suspended
1920 (setq regexp-collect
1921 (read-regexp
1922 (format "Regexp to collect (default %s): " default)
1923 default 'occur-collect-regexp-history)))
1924 regexp-collect))
1925 ;; Otherwise normal occur takes numerical prefix argument.
1926 (when current-prefix-arg
1927 (prefix-numeric-value current-prefix-arg))))))
1928 (let ((case-fold-search isearch-case-fold-search)
1929 ;; Set `search-upper-case' to nil to not call
1930 ;; `isearch-no-upper-case-p' in `occur-1'.
1931 (search-upper-case nil)
1932 (search-spaces-regexp
1933 (if (if isearch-regexp
1934 isearch-regexp-lax-whitespace
1935 isearch-lax-whitespace)
1936 search-whitespace-regexp)))
1937 (occur (if isearch-regexp-function
1938 (propertize regexp
1939 'isearch-string isearch-string
1940 'isearch-regexp-function-descr
1941 (isearch--describe-regexp-mode isearch-regexp-function))
1942 regexp)
1943 nlines
1944 (if (use-region-p) (region-bounds)))))
1946 (declare-function hi-lock-read-face-name "hi-lock" ())
1948 (defun isearch-highlight-regexp ()
1949 "Run `highlight-regexp' with regexp from the current search string.
1950 It exits Isearch mode and calls `hi-lock-face-buffer' with its regexp
1951 argument from the last search regexp or a quoted search string,
1952 and reads its face argument using `hi-lock-read-face-name'."
1953 (interactive)
1954 (let (
1955 ;; Set `isearch-recursive-edit' to nil to prevent calling
1956 ;; `exit-recursive-edit' in `isearch-done' that terminates
1957 ;; the execution of this command when it is non-nil.
1958 ;; We call `exit-recursive-edit' explicitly at the end below.
1959 (isearch-recursive-edit nil))
1960 (isearch-done nil t)
1961 (isearch-clean-overlays))
1962 (require 'hi-lock nil t)
1963 (let ((regexp (cond ((functionp isearch-regexp-function)
1964 (funcall isearch-regexp-function isearch-string))
1965 (isearch-regexp-function (word-search-regexp isearch-string))
1966 (isearch-regexp isearch-string)
1967 ((if (and (eq isearch-case-fold-search t)
1968 search-upper-case)
1969 (isearch-no-upper-case-p
1970 isearch-string isearch-regexp)
1971 isearch-case-fold-search)
1972 ;; Turn isearch-string into a case-insensitive
1973 ;; regexp.
1974 (mapconcat
1975 (lambda (c)
1976 (let ((s (string c)))
1977 (if (string-match "[[:alpha:]]" s)
1978 (format "[%s%s]" (upcase s) (downcase s))
1979 (regexp-quote s))))
1980 isearch-string ""))
1981 (t (regexp-quote isearch-string)))))
1982 (hi-lock-face-buffer regexp (hi-lock-read-face-name)))
1983 (and isearch-recursive-edit (exit-recursive-edit)))
1986 (defun isearch-delete-char ()
1987 "Discard last input item and move point back.
1988 Last input means the last character or the last isearch command
1989 that added or deleted characters from the search string,
1990 moved point, toggled regexp mode or case-sensitivity, etc.
1991 If no previous match was done, just beep."
1992 (interactive)
1993 (if (null (cdr isearch-cmds))
1994 (ding)
1995 (isearch-pop-state))
1996 (isearch-update))
1998 (defun isearch-del-char (&optional arg)
1999 "Delete character from end of search string and search again.
2000 Unlike `isearch-delete-char', it only deletes the last character,
2001 but doesn't cancel the effect of other isearch command.
2002 If search string is empty, just beep."
2003 (interactive "p")
2004 (if (= 0 (length isearch-string))
2005 (ding)
2006 (setq isearch-string (substring isearch-string 0
2007 (- (min (or arg 1)
2008 (length isearch-string))))
2009 isearch-message (mapconcat 'isearch-text-char-description
2010 isearch-string "")))
2011 ;; Do the following before moving point.
2012 (funcall (or isearch-message-function #'isearch-message) nil t)
2013 ;; Use the isearch-other-end as new starting point to be able
2014 ;; to find the remaining part of the search string again.
2015 ;; This is like what `isearch-search-and-update' does,
2016 ;; but currently it doesn't support deletion of characters
2017 ;; for the case where unsuccessful search may become successful
2018 ;; by deletion of characters.
2019 (if isearch-other-end (goto-char isearch-other-end))
2020 (isearch-search)
2021 (isearch-push-state)
2022 (isearch-update))
2024 (defun isearch-yank-string (string)
2025 "Pull STRING into search string."
2026 ;; Downcase the string if not supposed to case-fold yanked strings.
2027 (if (and isearch-case-fold-search
2028 (eq 'not-yanks search-upper-case))
2029 (setq string (downcase string)))
2030 (if isearch-regexp (setq string (regexp-quote string)))
2031 ;; Don't move cursor in reverse search.
2032 (setq isearch-yank-flag t)
2033 (isearch-process-search-string
2034 string (mapconcat 'isearch-text-char-description string "")))
2036 (defun isearch-yank-kill ()
2037 "Pull string from kill ring into search string."
2038 (interactive)
2039 (isearch-yank-string (current-kill 0)))
2041 (defun isearch-yank-pop ()
2042 "Replace just-yanked search string with previously killed string."
2043 (interactive)
2044 (if (not (memq last-command '(isearch-yank-kill isearch-yank-pop)))
2045 ;; Fall back on `isearch-yank-kill' for the benefits of people
2046 ;; who are used to the old behavior of `M-y' in isearch mode. In
2047 ;; future, this fallback may be changed if we ever change
2048 ;; `yank-pop' to do something like the kill-ring-browser.
2049 (isearch-yank-kill)
2050 (isearch-pop-state)
2051 (isearch-yank-string (current-kill 1))))
2053 (defun isearch-yank-x-selection ()
2054 "Pull current X selection into search string."
2055 (interactive)
2056 (isearch-yank-string (gui-get-selection))
2057 ;; If `gui-get-selection' returned the text from the active region,
2058 ;; then it "used" the mark which we should hence deactivate.
2059 (when select-active-regions (deactivate-mark)))
2062 (defun isearch-mouse-2 (click)
2063 "Handle mouse-2 in Isearch mode.
2064 For a click in the echo area, invoke `isearch-yank-x-selection'.
2065 Otherwise invoke whatever the calling mouse-2 command sequence
2066 is bound to outside of Isearch."
2067 (interactive "e")
2068 (let ((w (posn-window (event-start click)))
2069 (binding (let ((overriding-terminal-local-map nil))
2070 (key-binding (this-command-keys-vector) t))))
2071 (if (and (window-minibuffer-p w)
2072 (not (minibuffer-window-active-p w))) ; in echo area
2073 (isearch-yank-x-selection)
2074 (when (functionp binding)
2075 (call-interactively binding)))))
2077 (declare-function xterm--pasted-text "term/xterm" ())
2079 (defun isearch-xterm-paste ()
2080 "Pull terminal paste into search string."
2081 (interactive)
2082 (isearch-yank-string (xterm--pasted-text)))
2084 (defun isearch-yank-internal (jumpform)
2085 "Pull the text from point to the point reached by JUMPFORM.
2086 JUMPFORM is a lambda expression that takes no arguments and returns
2087 a buffer position, possibly having moved point to that position.
2088 For example, it might move point forward by a word and return point,
2089 or it might return the position of the end of the line."
2090 (isearch-yank-string
2091 (save-excursion
2092 (and (not isearch-forward) isearch-other-end
2093 (goto-char isearch-other-end))
2094 (buffer-substring-no-properties (point) (funcall jumpform)))))
2096 (defun isearch-yank-char-in-minibuffer (&optional arg)
2097 "Pull next character from buffer into end of search string in minibuffer."
2098 (interactive "p")
2099 (if (eobp)
2100 (insert
2101 (with-current-buffer (cadr (buffer-list))
2102 (buffer-substring-no-properties
2103 (point) (progn (forward-char arg) (point)))))
2104 (forward-char arg)))
2106 (defun isearch-yank-char (&optional arg)
2107 "Pull next character from buffer into search string.
2108 If optional ARG is non-nil, pull in the next ARG characters."
2109 (interactive "p")
2110 (isearch-yank-internal (lambda () (forward-char arg) (point))))
2112 (defun isearch--yank-char-or-syntax (syntax-list fn)
2113 (isearch-yank-internal
2114 (lambda ()
2115 (if (or (memq (char-syntax (or (char-after) 0)) syntax-list)
2116 (memq (char-syntax (or (char-after (1+ (point))) 0))
2117 syntax-list))
2118 (funcall fn 1)
2119 (forward-char 1))
2120 (point))))
2122 (defun isearch-yank-word-or-char ()
2123 "Pull next character or word from buffer into search string."
2124 (interactive)
2125 (isearch--yank-char-or-syntax '(?w) 'forward-word))
2127 (defun isearch-yank-symbol-or-char ()
2128 "Pull next character or symbol from buffer into search string."
2129 (interactive)
2130 (isearch--yank-char-or-syntax '(?w ?_) 'forward-symbol))
2132 (defun isearch-yank-word (&optional arg)
2133 "Pull next word from buffer into search string.
2134 If optional ARG is non-nil, pull in the next ARG words."
2135 (interactive "p")
2136 (isearch-yank-internal (lambda () (forward-word arg) (point))))
2138 (defun isearch-yank-line (&optional arg)
2139 "Pull rest of line from buffer into search string.
2140 If optional ARG is non-nil, yank the next ARG lines."
2141 (interactive "p")
2142 (isearch-yank-internal
2143 (lambda () (let ((inhibit-field-text-motion t))
2144 (line-end-position (if (eolp) (1+ arg) arg))))))
2146 (defun isearch-char-by-name (&optional count)
2147 "Read a character by its Unicode name and add it to the search string.
2148 Completion is available like in `read-char-by-name' used by `insert-char'.
2149 With argument, add COUNT copies of the character."
2150 (interactive "p")
2151 (with-isearch-suspended
2152 (let ((char (read-char-by-name "Add character to search (Unicode name or hex): ")))
2153 (when char
2154 (let ((string (if (and (integerp count) (> count 1))
2155 (make-string count char)
2156 (char-to-string char))))
2157 (setq isearch-new-string (concat isearch-string string)
2158 isearch-new-message (concat isearch-message
2159 (mapconcat 'isearch-text-char-description
2160 string ""))))))))
2162 (defun isearch-search-and-update ()
2163 ;; Do the search and update the display.
2164 (when (or isearch-success
2165 ;; Unsuccessful regexp search may become successful by
2166 ;; addition of characters which make isearch-string valid
2167 isearch-regexp
2168 ;; If the string was found but was completely invisible,
2169 ;; it might now be partly visible, so try again.
2170 (prog1 isearch-hidden (setq isearch-hidden nil)))
2171 ;; In reverse search, adding stuff at
2172 ;; the end may cause zero or many more chars to be
2173 ;; matched, in the string following point.
2174 ;; Allow all those possibilities without moving point as
2175 ;; long as the match does not extend past search origin.
2176 (if (and (not isearch-forward) (not isearch-adjusted)
2177 (condition-case ()
2178 (let ((case-fold-search isearch-case-fold-search))
2179 (if (and (eq case-fold-search t) search-upper-case)
2180 (setq case-fold-search
2181 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2182 (looking-at (cond
2183 ((functionp isearch-regexp-function)
2184 (funcall isearch-regexp-function isearch-string t))
2185 (isearch-regexp-function (word-search-regexp isearch-string t))
2186 (isearch-regexp isearch-string)
2187 (t (regexp-quote isearch-string)))))
2188 (error nil))
2189 (or isearch-yank-flag
2190 (<= (match-end 0)
2191 (min isearch-opoint isearch-barrier))))
2192 (progn
2193 (setq isearch-success t
2194 isearch-error nil
2195 isearch-other-end (match-end 0))
2196 (if (and (eq isearch-case-fold-search t) search-upper-case)
2197 (setq isearch-case-fold-search
2198 (isearch-no-upper-case-p isearch-string isearch-regexp))))
2199 ;; Not regexp, not reverse, or no match at point.
2200 ;; Do the following before moving point.
2201 (funcall (or isearch-message-function #'isearch-message) nil t)
2202 (if (and isearch-other-end (not isearch-adjusted))
2203 (goto-char (if isearch-forward isearch-other-end
2204 (min isearch-opoint
2205 isearch-barrier
2206 (1+ isearch-other-end)))))
2207 (isearch-search)
2209 (isearch-push-state)
2210 (if isearch-op-fun (funcall isearch-op-fun))
2211 (isearch-update))
2214 ;; *, ?, }, and | chars can make a regexp more liberal.
2215 ;; They can make a regexp match sooner or make it succeed instead of failing.
2216 ;; So go back to place last successful search started
2217 ;; or to the last ^S/^R (barrier), whichever is nearer.
2218 ;; + needs no special handling because the string must match at least once.
2220 (defun isearch-backslash (str)
2221 "Return t if STR ends in an odd number of backslashes."
2222 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
2224 (defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
2225 "Return point to previous successful match to allow regexp liberalization.
2226 \\<isearch-mode-map>
2227 Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by \
2228 stopping at `isearch-barrier' as needed.
2230 Do nothing if a backslash is escaping the liberalizing character.
2231 If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
2233 Do nothing if regexp has recently been invalid unless optional
2234 ALLOW-INVALID non-nil.
2236 If optional TO-BARRIER non-nil, ignore previous matches and go exactly
2237 to the barrier."
2238 ;; (eq (not a) (not b)) makes all non-nil values equivalent
2239 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
2240 (not want-backslash))
2241 ;; We have to check 2 stack frames because the last might be
2242 ;; invalid just because of a backslash.
2243 (or (not isearch-error)
2244 (not (isearch--state-error (cadr isearch-cmds)))
2245 allow-invalid))
2246 (if to-barrier
2247 (progn (goto-char isearch-barrier)
2248 (setq isearch-adjusted t))
2249 (let* ((stack isearch-cmds)
2250 (previous (cdr stack)) ; lookbelow in the stack
2251 (frame (car stack)))
2252 ;; Walk down the stack looking for a valid regexp (as of course only
2253 ;; they can be the previous successful match); this conveniently
2254 ;; removes all bracket-sets and groups that might be in the way, as
2255 ;; well as partial \{\} constructs that the code below leaves behind.
2256 ;; Also skip over postfix operators -- though horrid,
2257 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
2258 (while (and previous
2259 (or (isearch--state-error frame)
2260 (let* ((string (isearch--state-string frame))
2261 (lchar (aref string (1- (length string)))))
2262 ;; The operators aren't always operators; check
2263 ;; backslashes. This doesn't handle the case of
2264 ;; operators at the beginning of the regexp not
2265 ;; being special, but then we should fall back to
2266 ;; the barrier anyway because it's all optional.
2267 (if (isearch-backslash
2268 (isearch--state-string (car previous)))
2269 (eq lchar ?\})
2270 (memq lchar '(?* ?? ?+))))))
2271 (setq stack previous previous (cdr previous) frame (car stack)))
2272 (when stack
2273 ;; `stack' now refers the most recent valid regexp that is not at
2274 ;; all optional in its last term. Now dig one level deeper and find
2275 ;; what matched before that.
2276 (let ((last-other-end
2277 (or (and (car previous)
2278 (isearch--state-other-end (car previous)))
2279 isearch-barrier)))
2280 (goto-char (if isearch-forward
2281 (max last-other-end isearch-barrier)
2282 (min last-other-end isearch-barrier)))
2283 (setq isearch-adjusted t)))))))
2285 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2286 ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
2288 ;; The idea here is that certain vertical scrolling commands (like C-l
2289 ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
2290 ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
2291 ;; tamper with isearch's state, or move point. It is unacceptable for the
2292 ;; search string to be scrolled out of the current window. If a command
2293 ;; attempts this, we scroll the text back again.
2295 ;; We implement this feature with a property called `isearch-scroll'.
2296 ;; If a command's symbol has the value t for this property or for the
2297 ;; `scroll-command' property, it is a scrolling command. The feature
2298 ;; needs to be enabled by setting the customizable variable
2299 ;; `isearch-allow-scroll' to a non-nil value.
2301 ;; The universal argument commands (e.g. C-u) in simple.el are marked
2302 ;; as scrolling commands, and isearch.el has been amended to allow
2303 ;; prefix arguments to be passed through to scrolling commands. Thus
2304 ;; M-0 C-l will scroll point to the top of the window.
2306 ;; Horizontal scrolling commands are currently not catered for.
2307 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2309 ;; Set the isearch-scroll property on some standard functions:
2310 ;; Scroll-bar functions:
2311 (if (fboundp 'scroll-bar-toolkit-scroll)
2312 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
2313 (if (fboundp 'w32-handle-scroll-bar-event)
2314 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
2316 ;; Commands which scroll the window (some scroll commands
2317 ;; already have the `scroll-command' property on them):
2318 (put 'recenter 'isearch-scroll t)
2319 (put 'recenter-top-bottom 'isearch-scroll t)
2320 (put 'reposition-window 'isearch-scroll t)
2322 ;; Commands which act on the other window
2323 (put 'list-buffers 'isearch-scroll t)
2324 (put 'scroll-other-window 'isearch-scroll t)
2325 (put 'scroll-other-window-down 'isearch-scroll t)
2326 (put 'beginning-of-buffer-other-window 'isearch-scroll t)
2327 (put 'end-of-buffer-other-window 'isearch-scroll t)
2329 ;; Commands which change the window layout
2330 (put 'delete-other-windows 'isearch-scroll t)
2331 (put 'balance-windows 'isearch-scroll t)
2332 (put 'split-window-right 'isearch-scroll t)
2333 (put 'split-window-below 'isearch-scroll t)
2334 (put 'enlarge-window 'isearch-scroll t)
2336 ;; Aliases for split-window-*
2337 (put 'split-window-vertically 'isearch-scroll t)
2338 (put 'split-window-horizontally 'isearch-scroll t)
2340 ;; Universal argument commands
2341 (put 'universal-argument 'isearch-scroll t)
2342 (put 'universal-argument-more 'isearch-scroll t)
2343 (put 'negative-argument 'isearch-scroll t)
2344 (put 'digit-argument 'isearch-scroll t)
2346 (defcustom isearch-allow-scroll nil
2347 "Whether scrolling is allowed during incremental search.
2348 If non-nil, scrolling commands can be used in Isearch mode.
2349 However, the current match will never scroll offscreen.
2350 If nil, scrolling commands will first cancel Isearch mode."
2351 :type 'boolean
2352 :group 'isearch)
2354 (defcustom isearch-allow-prefix t
2355 "Whether prefix arguments are allowed during incremental search.
2356 If non-nil, entering a prefix argument will not terminate the
2357 search. This option is ignored \(presumed t) when
2358 `isearch-allow-scroll' is set."
2359 :version "24.4"
2360 :type 'boolean
2361 :group 'isearch)
2363 (defun isearch-string-out-of-window (isearch-point)
2364 "Test whether the search string is currently outside of the window.
2365 Return nil if it's completely visible, or if point is visible,
2366 together with as much of the search string as will fit; the symbol
2367 `above' if we need to scroll the text downwards; the symbol `below',
2368 if upwards."
2369 (let ((w-start (window-group-start))
2370 (w-end (window-group-end nil t))
2371 (w-L1 (save-excursion
2372 (save-selected-window (move-to-window-group-line 1) (point))))
2373 (w-L-1 (save-excursion
2374 (save-selected-window (move-to-window-group-line -1) (point))))
2375 start end) ; start and end of search string in buffer
2376 (if isearch-forward
2377 (setq end isearch-point start (or isearch-other-end isearch-point))
2378 (setq start isearch-point end (or isearch-other-end isearch-point)))
2379 (cond ((or (and (>= start w-start) (<= end w-end))
2380 (if isearch-forward
2381 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
2382 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
2383 nil)
2384 ((and (< start w-start)
2385 (< isearch-point w-L-1))
2386 'above)
2387 (t 'below))))
2389 (defun isearch-back-into-window (above isearch-point)
2390 "Scroll the window to bring the search string back into view.
2391 Restore point to ISEARCH-POINT in the process. ABOVE is t when the
2392 search string is above the top of the window, nil when it is beneath
2393 the bottom."
2394 (let (start end)
2395 (if isearch-forward
2396 (setq end isearch-point start (or isearch-other-end isearch-point))
2397 (setq start isearch-point end (or isearch-other-end isearch-point)))
2398 (if above
2399 (progn
2400 (goto-char start)
2401 (recenter-window-group 0)
2402 (when (>= isearch-point (window-group-end nil t))
2403 (goto-char isearch-point)
2404 (recenter-window-group -1)))
2405 (goto-char end)
2406 (recenter-window-group -1)
2407 (when (< isearch-point (window-group-start))
2408 (goto-char isearch-point)
2409 (recenter-window-group 0))))
2410 (goto-char isearch-point))
2412 (defvar isearch-pre-scroll-point nil)
2413 (defvar isearch-pre-move-point nil)
2415 (defun isearch-pre-command-hook ()
2416 "Decide whether to exit Isearch mode before executing the command.
2417 Don't exit Isearch if the key sequence that invoked this command
2418 is bound in `isearch-mode-map', or if the invoked command is
2419 a prefix argument command (when `isearch-allow-prefix' is non-nil),
2420 or it is a scrolling command (when `isearch-allow-scroll' is non-nil).
2421 Otherwise, exit Isearch (when `search-exit-option' is t)
2422 before the command is executed globally with terminated Isearch.
2423 See more for options in `search-exit-option'."
2424 (let* ((key (this-single-command-keys))
2425 (main-event (aref key 0)))
2426 (cond
2427 ;; Don't exit Isearch if we're in the middle of some
2428 ;; `set-transient-map' thingy like `universal-argument--mode'.
2429 ((not (eq overriding-terminal-local-map isearch--saved-overriding-local-map)))
2430 ;; Don't exit Isearch for isearch key bindings.
2431 ((commandp (lookup-key isearch-mode-map key nil)))
2432 ;; Optionally edit the search string instead of exiting.
2433 ((eq search-exit-option 'edit)
2434 (setq this-command 'isearch-edit-string))
2435 ;; Handle a scrolling function or prefix argument.
2436 ((or (and isearch-allow-prefix
2437 (memq this-command '(universal-argument universal-argument-more
2438 digit-argument negative-argument)))
2439 (and isearch-allow-scroll
2440 (symbolp this-command)
2441 (or (eq (get this-command 'isearch-scroll) t)
2442 (eq (get this-command 'scroll-command) t))))
2443 (when isearch-allow-scroll
2444 (setq isearch-pre-scroll-point (point))))
2445 ;; A mouse click on the isearch message starts editing the search string.
2446 ((and (eq (car-safe main-event) 'down-mouse-1)
2447 (window-minibuffer-p (posn-window (event-start main-event))))
2448 ;; Swallow the up-event.
2449 (read-event)
2450 (setq this-command 'isearch-edit-string))
2451 ;; Don't terminate the search for motion commands.
2452 ((or (and (eq search-exit-option 'move)
2453 (symbolp this-command)
2454 (or (eq (get this-command 'isearch-move) 'enabled)
2455 (and (not (eq (get this-command 'isearch-move) 'disabled))
2456 (stringp (nth 1 (interactive-form this-command)))
2457 (string-match-p "^^" (nth 1 (interactive-form this-command))))))
2458 (and (eq search-exit-option 'shift-move)
2459 this-command-keys-shift-translated))
2460 (setq this-command-keys-shift-translated nil)
2461 (setq isearch-pre-move-point (point)))
2462 ;; Append control characters to the search string
2463 ((eq search-exit-option 'append)
2464 (unless (memq nil (mapcar (lambda (k) (characterp k)) key))
2465 (isearch-process-search-string key key))
2466 (setq this-command 'ignore))
2467 ;; Other characters terminate the search and are then executed normally.
2468 (search-exit-option
2469 (isearch-done)
2470 (isearch-clean-overlays)))))
2472 (defun isearch-post-command-hook ()
2473 (cond
2474 (isearch-pre-scroll-point
2475 (let ((ab-bel (isearch-string-out-of-window isearch-pre-scroll-point)))
2476 (if ab-bel
2477 (isearch-back-into-window (eq ab-bel 'above) isearch-pre-scroll-point)
2478 (goto-char isearch-pre-scroll-point)))
2479 (setq isearch-pre-scroll-point nil)
2480 (isearch-update))
2481 ((memq search-exit-option '(move shift-move))
2482 (when (and isearch-pre-move-point
2483 (not (eq isearch-pre-move-point (point))))
2484 (let ((string (buffer-substring-no-properties
2485 (or isearch-other-end isearch-opoint) (point))))
2486 (if isearch-regexp (setq string (regexp-quote string)))
2487 (setq isearch-string string)
2488 (setq isearch-message (mapconcat 'isearch-text-char-description
2489 string ""))
2490 (setq isearch-yank-flag t)
2491 (setq isearch-forward (<= (or isearch-other-end isearch-opoint) (point)))
2492 (when isearch-forward
2493 (goto-char isearch-pre-move-point))
2494 (isearch-search-and-update)))
2495 (setq isearch-pre-move-point nil))))
2497 (defun isearch-quote-char (&optional count)
2498 "Quote special characters for incremental search.
2499 With argument, add COUNT copies of the character."
2500 (interactive "p")
2501 (let ((char (read-quoted-char (isearch-message t))))
2502 (unless (characterp char)
2503 (user-error "%s is not a valid character"
2504 (key-description (vector char))))
2505 ;; Assume character codes 0200 - 0377 stand for characters in some
2506 ;; single-byte character set, and convert them to Emacs
2507 ;; characters.
2508 (if (and isearch-regexp isearch-regexp-lax-whitespace (= char ?\s))
2509 (if (subregexp-context-p isearch-string (length isearch-string))
2510 (isearch-process-search-string "[ ]" " ")
2511 (isearch-process-search-char char count))
2512 ;; This used to assume character codes 0240 - 0377 stand for
2513 ;; characters in some single-byte character set, and converted them
2514 ;; to Emacs characters. But in 23.1 this feature is deprecated
2515 ;; in favor of inserting the corresponding Unicode characters.
2516 ;; (and enable-multibyte-characters
2517 ;; (>= char ?\200)
2518 ;; (<= char ?\377)
2519 ;; (setq char (unibyte-char-to-multibyte char)))
2520 (isearch-process-search-char char count))))
2522 (defun isearch-printing-char (&optional char count)
2523 "Add this ordinary printing CHAR to the search string and search.
2524 With argument, add COUNT copies of the character."
2525 (interactive (list last-command-event
2526 (prefix-numeric-value current-prefix-arg)))
2527 (let ((char (or char last-command-event)))
2528 (if (= char ?\S-\ )
2529 (setq char ?\s))
2530 (if current-input-method
2531 (isearch-process-search-multibyte-characters char count)
2532 (isearch-process-search-char char count))))
2534 (defun isearch-process-search-char (char &optional count)
2535 "Add CHAR to the search string, COUNT times.
2536 Search is updated accordingly."
2537 ;; * and ? are special in regexps when not preceded by \.
2538 ;; } and | are special in regexps when preceded by \.
2539 ;; Nothing special for + because it matches at least once.
2540 (cond
2541 ((memq char '(?* ??)) (isearch-fallback nil))
2542 ((eq char ?\}) (isearch-fallback t t))
2543 ((eq char ?|) (isearch-fallback t nil t)))
2545 ;; Append the char(s) to the search string,
2546 ;; update the message and re-search.
2547 (let* ((string (if (and (integerp count) (> count 1))
2548 (make-string count char)
2549 (char-to-string char)))
2550 (message (if (>= char ?\200)
2551 string
2552 (mapconcat 'isearch-text-char-description string ""))))
2553 (isearch-process-search-string string message)))
2555 (defun isearch-process-search-string (string message)
2556 (setq isearch-string (concat isearch-string string)
2557 isearch-message (concat isearch-message message))
2558 (isearch-search-and-update))
2561 ;; Search Ring
2563 (defun isearch-ring-adjust1 (advance)
2564 ;; Helper for isearch-ring-adjust
2565 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
2566 (length (length ring))
2567 (yank-pointer-name (if isearch-regexp
2568 'regexp-search-ring-yank-pointer
2569 'search-ring-yank-pointer))
2570 (yank-pointer (eval yank-pointer-name)))
2571 (if (zerop length)
2573 (set yank-pointer-name
2574 (setq yank-pointer
2575 (mod (+ (or yank-pointer (if advance 0 -1))
2576 (if advance -1 1))
2577 length)))
2578 (setq isearch-string (nth yank-pointer ring)
2579 isearch-message (mapconcat 'isearch-text-char-description
2580 isearch-string ""))
2581 (isearch-update-from-string-properties isearch-string))))
2583 (defun isearch-ring-adjust (advance)
2584 ;; Helper for isearch-ring-advance and isearch-ring-retreat
2585 (isearch-ring-adjust1 advance)
2586 (if search-ring-update
2587 (progn
2588 (funcall (or isearch-message-function #'isearch-message) nil t)
2589 (isearch-search)
2590 (isearch-push-state)
2591 (isearch-update))
2592 ;; Otherwise, edit the search string instead. Note that there is
2593 ;; no need to push the search state after isearch-edit-string here
2594 ;; since isearch-edit-string already pushes its state
2595 (isearch-edit-string)))
2597 (defun isearch-ring-advance ()
2598 "Advance to the next search string in the ring."
2599 ;; This could be more general to handle a prefix arg, but who would use it.
2600 (interactive)
2601 (isearch-ring-adjust 'advance))
2603 (defun isearch-ring-retreat ()
2604 "Retreat to the previous search string in the ring."
2605 (interactive)
2606 (isearch-ring-adjust nil))
2608 (defun isearch-complete1 ()
2609 ;; Helper for isearch-complete and isearch-complete-edit
2610 ;; Return t if completion OK, nil if no completion exists.
2611 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
2612 (completion-ignore-case case-fold-search)
2613 (completion (try-completion isearch-string ring)))
2614 (cond
2615 ((eq completion t)
2616 ;; isearch-string stays the same
2618 ((or completion ; not nil, must be a string
2619 (= 0 (length isearch-string))) ; shouldn't have to say this
2620 (if (equal completion isearch-string) ;; no extension?
2621 (progn
2622 (if completion-auto-help
2623 (with-output-to-temp-buffer "*Isearch completions*"
2624 (display-completion-list
2625 (all-completions isearch-string ring))))
2627 (and completion
2628 (setq isearch-string completion))))
2630 (message "No completion") ; waits a second if in minibuffer
2631 nil))))
2633 (defun isearch-complete ()
2634 "Complete the search string from the strings on the search ring.
2635 The completed string is then editable in the minibuffer.
2636 If there is no completion possible, say so and continue searching."
2637 (interactive)
2638 (if (isearch-complete1)
2639 (progn (setq isearch-message
2640 (mapconcat 'isearch-text-char-description
2641 isearch-string ""))
2642 (isearch-edit-string))
2643 ;; else
2644 (sit-for 1)
2645 (isearch-update)))
2647 (defun isearch-complete-edit ()
2648 "Same as `isearch-complete' except in the minibuffer."
2649 (interactive)
2650 (setq isearch-string (field-string))
2651 (if (isearch-complete1)
2652 (progn
2653 (delete-field)
2654 (insert isearch-string))))
2657 ;; Message string
2659 (defun isearch-message (&optional c-q-hack ellipsis)
2660 ;; Generate and print the message string.
2662 ;; N.B.: This function should always be called with point at the
2663 ;; search point, because in certain (rare) circumstances, undesired
2664 ;; scrolling can happen when point is elsewhere. These
2665 ;; circumstances are when follow-mode is active, the search string
2666 ;; spans two (or several) windows, and the message about to be
2667 ;; displayed will cause the echo area to expand.
2668 (let ((cursor-in-echo-area ellipsis)
2669 (m isearch-message)
2670 (fail-pos (isearch-fail-pos t)))
2671 ;; Highlight failed part
2672 (when fail-pos
2673 (setq m (copy-sequence m))
2674 (add-text-properties fail-pos (length m) '(face isearch-fail) m)
2675 ;; Highlight failed trailing whitespace
2676 (when (string-match " +$" m)
2677 (add-text-properties (match-beginning 0) (match-end 0)
2678 '(face trailing-whitespace) m)))
2679 (setq m (concat
2680 (isearch-message-prefix ellipsis isearch-nonincremental)
2682 (isearch-message-suffix c-q-hack)))
2683 (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
2685 (defun isearch--describe-regexp-mode (regexp-function &optional space-before)
2686 "Make a string for describing REGEXP-FUNCTION.
2687 If SPACE-BEFORE is non-nil, put a space before, instead of after,
2688 the word mode."
2689 (when (eq regexp-function t)
2690 (setq regexp-function #'word-search-regexp))
2691 (let ((description
2692 (cond
2693 ;; 1. Do not use a description on the default search mode,
2694 ;; but only if the default search mode is non-nil.
2695 ((and (or (and search-default-mode
2696 (equal search-default-mode regexp-function))
2697 ;; Special case where `search-default-mode' is t
2698 ;; (defaults to regexp searches).
2699 (and (eq search-default-mode t)
2700 (eq search-default-mode isearch-regexp)))
2701 ;; Also do not omit description in case of error
2702 ;; in default non-literal search.
2703 (or isearch-success (not (or regexp-function isearch-regexp))))
2705 ;; 2. Use the `isearch-message-prefix' set for
2706 ;; `regexp-function' if available.
2707 (regexp-function
2708 (and (symbolp regexp-function)
2709 (or (get regexp-function 'isearch-message-prefix)
2710 "")))
2711 ;; 3. Else if `isearch-regexp' is non-nil, set description
2712 ;; to "regexp ".
2713 (isearch-regexp "regexp ")
2714 ;; 4. Else if we're in literal mode (and if the default
2715 ;; mode is also not literal), describe it.
2716 ((functionp search-default-mode) "literal ")
2717 ;; 5. And finally, if none of the above is true, set the
2718 ;; description to an empty string.
2719 (t ""))))
2720 (if space-before
2721 ;; Move space from the end to the beginning.
2722 (replace-regexp-in-string "\\(.*\\) \\'" " \\1" description)
2723 description)))
2724 (define-obsolete-function-alias 'isearch--describe-word-mode
2725 'isearch--describe-regexp-mode "25.1")
2727 (defun isearch-message-prefix (&optional ellipsis nonincremental)
2728 ;; If about to search, and previous search regexp was invalid,
2729 ;; check that it still is. If it is valid now,
2730 ;; let the message we display while searching say that it is valid.
2731 (and isearch-error ellipsis
2732 (condition-case ()
2733 (progn (re-search-forward isearch-string (point) t)
2734 (setq isearch-error nil))
2735 (error nil)))
2736 ;; If currently failing, display no ellipsis.
2737 (or isearch-success (setq ellipsis nil))
2738 (let ((m (concat (if isearch-success "" "failing ")
2739 (if isearch-adjusted "pending " "")
2740 (if (and isearch-wrapped
2741 (not isearch-wrap-function)
2742 (if isearch-forward
2743 (> (point) isearch-opoint)
2744 (< (point) isearch-opoint)))
2745 "over")
2746 (if isearch-wrapped "wrapped ")
2747 (if (and (not isearch-success) (not isearch-case-fold-search))
2748 "case-sensitive ")
2749 (let ((prefix ""))
2750 (advice-function-mapc
2751 (lambda (_ props)
2752 (let ((np (cdr (assq 'isearch-message-prefix props))))
2753 (if np (setq prefix (concat np prefix)))))
2754 isearch-filter-predicate)
2755 prefix)
2756 (isearch--describe-regexp-mode isearch-regexp-function)
2757 (cond
2758 (multi-isearch-file-list "multi-file ")
2759 (multi-isearch-buffer-list "multi-buffer ")
2760 (t ""))
2761 (or isearch-message-prefix-add "")
2762 (if nonincremental "search" "I-search")
2763 (if isearch-forward "" " backward")
2764 (if current-input-method
2765 ;; Input methods for RTL languages use RTL
2766 ;; characters for their title, and that messes
2767 ;; up the display of search text after the prompt.
2768 (bidi-string-mark-left-to-right
2769 (concat " [" current-input-method-title "]: "))
2770 ": ")
2772 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
2773 'face 'minibuffer-prompt)))
2775 (defun isearch-message-suffix (&optional c-q-hack)
2776 (propertize (concat (if c-q-hack "^Q" "")
2777 (if isearch-error
2778 (concat " [" isearch-error "]")
2780 (or isearch-message-suffix-add ""))
2781 'face 'minibuffer-prompt))
2784 ;; Searching
2786 (defvar isearch-search-fun-function 'isearch-search-fun-default
2787 "Non-default value overrides the behavior of `isearch-search-fun-default'.
2788 This variable's value should be a function, which will be called
2789 with no arguments, and should return a function that takes three
2790 arguments: STRING, BOUND, and NOERROR. STRING is the string to
2791 be searched for. See `re-search-forward' for the meaning of
2792 BOUND and NOERROR arguments.
2794 This returned function will be used by `isearch-search-string' to
2795 search for the first occurrence of STRING.")
2797 (defun isearch-search-fun ()
2798 "Return the function to use for the search.
2799 Can be changed via `isearch-search-fun-function' for special needs."
2800 (funcall (or isearch-search-fun-function 'isearch-search-fun-default)))
2802 (defun isearch--lax-regexp-function-p ()
2803 "Non-nil if next regexp-function call should be lax."
2804 (or (memq this-command '(isearch-printing-char isearch-del-char))
2805 isearch-yank-flag))
2807 (defun isearch-search-fun-default ()
2808 "Return default functions to use for the search."
2809 (lambda (string &optional bound noerror count)
2810 ;; Use lax versions to not fail at the end of the word while
2811 ;; the user adds and removes characters in the search string
2812 ;; (or when using nonincremental word isearch)
2813 (let ((search-spaces-regexp (when (cond
2814 (isearch-regexp isearch-regexp-lax-whitespace)
2815 (t isearch-lax-whitespace))
2816 search-whitespace-regexp)))
2817 (funcall
2818 (if isearch-forward #'re-search-forward #'re-search-backward)
2819 (cond (isearch-regexp-function
2820 (let ((lax (and (not bound) (isearch--lax-regexp-function-p))))
2821 (when lax
2822 (setq isearch-adjusted t))
2823 (if (functionp isearch-regexp-function)
2824 (funcall isearch-regexp-function string lax)
2825 (word-search-regexp string lax))))
2826 (isearch-regexp string)
2827 (t (regexp-quote string)))
2828 bound noerror count))))
2830 (defun isearch-search-string (string bound noerror)
2831 "Search for the first occurrence of STRING or its translation.
2832 STRING's characters are translated using `translation-table-for-input'
2833 if that is non-nil.
2834 If found, move point to the end of the occurrence,
2835 update the match data, and return point.
2836 An optional second argument bounds the search; it is a buffer position.
2837 The match found must not extend after that position.
2838 Optional third argument, if t, means if fail just return nil (no error).
2839 If not nil and not t, move to limit of search and return nil."
2840 (let* ((func (isearch-search-fun))
2841 (pos1 (save-excursion (funcall func string bound noerror)))
2842 pos2)
2843 (when (and
2844 ;; Avoid "obsolete" warnings for translation-table-for-input.
2845 (with-no-warnings
2846 (char-table-p translation-table-for-input))
2847 (multibyte-string-p string)
2848 ;; Minor optimization.
2849 (string-match-p "[^[:ascii:]]" string))
2850 (let ((translated
2851 (apply 'string
2852 (mapcar (lambda (c)
2854 ;; Avoid "obsolete" warnings for
2855 ;; translation-table-for-input.
2856 (with-no-warnings
2857 (aref translation-table-for-input c))
2859 string)))
2860 match-data)
2861 (when translated
2862 (save-match-data
2863 (save-excursion
2864 (if (setq pos2 (funcall func translated bound noerror))
2865 (setq match-data (match-data t)))))
2866 (when (and pos2
2867 (or (not pos1)
2868 (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
2869 (setq pos1 pos2)
2870 (set-match-data match-data)))))
2871 (when pos1
2872 ;; When using multiple buffers isearch, switch to the new buffer here,
2873 ;; because `save-excursion' above doesn't allow doing it inside funcall.
2874 (if (and multi-isearch-next-buffer-current-function
2875 (buffer-live-p multi-isearch-current-buffer))
2876 (switch-to-buffer multi-isearch-current-buffer))
2877 (goto-char pos1)
2878 pos1)))
2880 (defun isearch-search ()
2881 ;; Do the search with the current search string.
2882 (if (and (eq isearch-case-fold-search t) search-upper-case)
2883 (setq isearch-case-fold-search
2884 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2885 (condition-case lossage
2886 (let ((inhibit-point-motion-hooks isearch-invisible)
2887 (inhibit-quit nil)
2888 (case-fold-search isearch-case-fold-search)
2889 (search-invisible isearch-invisible)
2890 (retry t))
2891 (setq isearch-error nil)
2892 (while retry
2893 (setq isearch-success
2894 (isearch-search-string isearch-string nil t))
2895 ;; Clear RETRY unless the search predicate says
2896 ;; to skip this search hit.
2897 (if (or (not isearch-success)
2898 (bobp) (eobp)
2899 (= (match-beginning 0) (match-end 0))
2900 (funcall isearch-filter-predicate
2901 (match-beginning 0) (match-end 0)))
2902 (setq retry nil)))
2903 (setq isearch-just-started nil)
2904 (if isearch-success
2905 (setq isearch-other-end
2906 (if isearch-forward (match-beginning 0) (match-end 0)))))
2908 (quit (isearch-unread ?\C-g)
2909 (setq isearch-success nil))
2911 (invalid-regexp
2912 (setq isearch-error (car (cdr lossage)))
2913 (cond
2914 ((string-match
2915 "\\`Premature \\|\\`Unmatched "
2916 isearch-error)
2917 (setq isearch-error "incomplete input"))
2918 ((and (not isearch-regexp)
2919 (string-match "\\`Regular expression too big" isearch-error))
2920 (cond
2921 (isearch-regexp-function
2922 (setq isearch-error "Too many words"))
2923 ((and isearch-lax-whitespace search-whitespace-regexp)
2924 (setq isearch-error "Too many spaces for whitespace matching"))))))
2926 (search-failed
2927 (setq isearch-success nil)
2928 (setq isearch-error (nth 2 lossage)))
2930 (error
2931 ;; stack overflow in regexp search.
2932 (setq isearch-error (format "%s" lossage))))
2934 (if isearch-success
2936 ;; Ding if failed this time after succeeding last time.
2937 (and (isearch--state-success (car isearch-cmds))
2938 (ding))
2939 (if (functionp (isearch--state-pop-fun (car isearch-cmds)))
2940 (funcall (isearch--state-pop-fun (car isearch-cmds))
2941 (car isearch-cmds)))
2942 (goto-char (isearch--state-point (car isearch-cmds)))))
2945 ;; Called when opening an overlay, and we are still in isearch.
2946 (defun isearch-open-overlay-temporary (ov)
2947 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
2948 ;; Some modes would want to open the overlays temporary during
2949 ;; isearch in their own way, they should set the
2950 ;; `isearch-open-invisible-temporary' to a function doing this.
2951 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2952 ;; Store the values for the `invisible' property, and then set it to nil.
2953 ;; This way the text hidden by this overlay becomes visible.
2954 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2955 (overlay-put ov 'invisible nil)))
2958 ;; This is called at the end of isearch. It will open the overlays
2959 ;; that contain the latest match. Obviously in case of a C-g the
2960 ;; point returns to the original location which surely is not contain
2961 ;; in any of these overlays, se we are safe in this case too.
2962 (defun isearch-open-necessary-overlays (ov)
2963 (let ((inside-overlay (and (> (point) (overlay-start ov))
2964 (<= (point) (overlay-end ov))))
2965 ;; If this exists it means that the overlay was opened using
2966 ;; this function, not by us tweaking the overlay properties.
2967 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2968 (when (or inside-overlay (not fct-temp))
2969 ;; restore the values for the `invisible' properties.
2970 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2971 (overlay-put ov 'isearch-invisible nil))
2972 (if inside-overlay
2973 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2974 (if fct-temp
2975 (funcall fct-temp ov t)))))
2977 ;; This is called when exiting isearch. It closes the temporary
2978 ;; opened overlays, except the ones that contain the latest match.
2979 (defun isearch-clean-overlays ()
2980 (when isearch-opened-overlays
2981 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
2982 (setq isearch-opened-overlays nil)))
2985 (defun isearch-intersects-p (start0 end0 start1 end1)
2986 "Return t if regions START0..END0 and START1..END1 intersect."
2987 (or (and (>= start0 start1) (< start0 end1))
2988 (and (> end0 start1) (<= end0 end1))
2989 (and (>= start1 start0) (< start1 end0))
2990 (and (> end1 start0) (<= end1 end0))))
2993 ;; Verify if the current match is outside of each element of
2994 ;; `isearch-opened-overlays', if so close that overlay.
2996 (defun isearch-close-unnecessary-overlays (begin end)
2997 (let ((overlays isearch-opened-overlays))
2998 (setq isearch-opened-overlays nil)
2999 (dolist (ov overlays)
3000 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
3001 (push ov isearch-opened-overlays)
3002 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
3003 (if fct-temp
3004 ;; If this exists it means that the overlay was opened
3005 ;; using this function, not by us tweaking the overlay
3006 ;; properties.
3007 (funcall fct-temp ov t)
3008 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
3009 (overlay-put ov 'isearch-invisible nil)))))))
3012 (defun isearch-range-invisible (beg end)
3013 "Return t if all the text from BEG to END is invisible."
3014 (when (/= beg end)
3015 ;; Check that invisibility runs up to END.
3016 (save-excursion
3017 (goto-char beg)
3018 (let (;; can-be-opened keeps track if we can open some overlays.
3019 (can-be-opened (eq search-invisible 'open))
3020 ;; the list of overlays that could be opened
3021 (crt-overlays nil))
3022 (when (and can-be-opened isearch-hide-immediately)
3023 (isearch-close-unnecessary-overlays beg end))
3024 ;; If the following character is currently invisible,
3025 ;; skip all characters with that same `invisible' property value.
3026 ;; Do that over and over.
3027 (while (and (< (point) end) (invisible-p (point)))
3028 (if (invisible-p (get-text-property (point) 'invisible))
3029 (progn
3030 (goto-char (next-single-property-change (point) 'invisible
3031 nil end))
3032 ;; if text is hidden by an `invisible' text property
3033 ;; we cannot open it at all.
3034 (setq can-be-opened nil))
3035 (when can-be-opened
3036 (let ((overlays (overlays-at (point)))
3037 ov-list
3039 invis-prop)
3040 (while overlays
3041 (setq o (car overlays)
3042 invis-prop (overlay-get o 'invisible))
3043 (if (invisible-p invis-prop)
3044 (if (overlay-get o 'isearch-open-invisible)
3045 (setq ov-list (cons o ov-list))
3046 ;; We found one overlay that cannot be
3047 ;; opened, that means the whole chunk
3048 ;; cannot be opened.
3049 (setq can-be-opened nil)))
3050 (setq overlays (cdr overlays)))
3051 (if can-be-opened
3052 ;; It makes sense to append to the open
3053 ;; overlays list only if we know that this is
3054 ;; t.
3055 (setq crt-overlays (append ov-list crt-overlays)))))
3056 (goto-char (next-overlay-change (point)))))
3057 ;; See if invisibility reaches up thru END.
3058 (if (>= (point) end)
3059 (if (and can-be-opened (consp crt-overlays))
3060 (progn
3061 (setq isearch-opened-overlays
3062 (append isearch-opened-overlays crt-overlays))
3063 (mapc 'isearch-open-overlay-temporary crt-overlays)
3064 nil)
3065 (setq isearch-hidden t)))))))
3067 (defun isearch-filter-visible (beg end)
3068 "Test whether the current search hit is visible at least partially.
3069 Return non-nil if the text from BEG to END is visible to Isearch as
3070 determined by `isearch-range-invisible' unless invisible text can be
3071 searched too when `search-invisible' is t."
3072 (or (eq search-invisible t)
3073 (not (isearch-range-invisible beg end))))
3076 ;; General utilities
3078 (defun isearch-no-upper-case-p (string regexp-flag)
3079 "Return t if there are no upper case chars in STRING.
3080 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
3081 since they have special meaning in a regexp."
3082 (let (quote-flag (i 0) (len (length string)) found)
3083 (while (and (not found) (< i len))
3084 (let ((char (aref string i)))
3085 (if (and regexp-flag (eq char ?\\))
3086 (setq quote-flag (not quote-flag))
3087 (if (and (not quote-flag) (not (eq char (downcase char))))
3088 (setq found t))
3089 (setq quote-flag nil)))
3090 (setq i (1+ i)))
3091 (not (or found
3092 ;; Even if there's no uppercase char, we want to detect the use
3093 ;; of [:upper:] or [:lower:] char-class, which indicates
3094 ;; clearly that the user cares about case distinction.
3095 (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
3096 (condition-case err
3097 (progn
3098 (string-match (substring string 0 (match-beginning 0))
3100 nil)
3101 (invalid-regexp
3102 (equal "Unmatched [ or [^" (cadr err)))))))))
3104 ;; Portability functions to support various Emacs versions.
3106 (defun isearch-text-char-description (c)
3107 (cond
3108 ((< c ?\s) (propertize
3109 (char-to-string c)
3110 'display (propertize (format "^%c" (+ c 64)) 'face 'escape-glyph)))
3111 ((= c ?\^?) (propertize
3112 (char-to-string c)
3113 'display (propertize "^?" 'face 'escape-glyph)))
3114 (t (char-to-string c))))
3116 ;; General function to unread characters or events.
3117 ;; Also insert them in a keyboard macro being defined.
3118 (defun isearch-unread (&rest char-or-events)
3119 (mapc 'store-kbd-macro-event char-or-events)
3120 (setq unread-command-events
3121 (append char-or-events unread-command-events)))
3124 ;; Highlighting
3126 (defvar isearch-overlay nil)
3128 (defun isearch-highlight (beg end)
3129 (if search-highlight
3130 (if isearch-overlay
3131 ;; Overlay already exists, just move it.
3132 (move-overlay isearch-overlay beg end (current-buffer))
3133 ;; Overlay doesn't exist, create it.
3134 (setq isearch-overlay (make-overlay beg end))
3135 ;; 1001 is higher than lazy's 1000 and ediff's 100+
3136 (overlay-put isearch-overlay 'priority 1001)
3137 (overlay-put isearch-overlay 'face isearch-face))))
3139 (defun isearch-dehighlight ()
3140 (when isearch-overlay
3141 (delete-overlay isearch-overlay)))
3143 ;; isearch-lazy-highlight feature
3144 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
3146 ;; When active, *every* match for the current search string is
3147 ;; highlighted: the current one using the normal isearch match color
3148 ;; and all the others using `isearch-lazy-highlight'. The extra
3149 ;; highlighting makes it easier to anticipate where the cursor will
3150 ;; land each time you press C-s or C-r to repeat a pending search.
3151 ;; Highlighting of these additional matches happens in a deferred
3152 ;; fashion using "idle timers," so the cycles needed do not rob
3153 ;; isearch of its usual snappy response.
3155 ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
3156 ;; Specifically:
3157 ;; - `isearch-update' is expected to be called (at least) every time
3158 ;; the search string or window-start changes;
3159 ;; - `isearch-string' is expected to contain the current search
3160 ;; string as entered by the user;
3161 ;; - the type of the current search is expected to be given by
3162 ;; `isearch-regexp-function' and `isearch-regexp';
3163 ;; - the direction of the current search is expected to be given by
3164 ;; `isearch-forward';
3165 ;; - the variable `isearch-error' is expected to be true
3166 ;; only if `isearch-string' is an invalid regexp.
3168 (defvar isearch-lazy-highlight-overlays nil)
3169 (defvar isearch-lazy-highlight-wrapped nil)
3170 (defvar isearch-lazy-highlight-start-limit nil)
3171 (defvar isearch-lazy-highlight-end-limit nil)
3172 (defvar isearch-lazy-highlight-start nil)
3173 (defvar isearch-lazy-highlight-end nil)
3174 (defvar isearch-lazy-highlight-timer nil)
3175 (defvar isearch-lazy-highlight-last-string nil)
3176 (defvar isearch-lazy-highlight-window nil)
3177 (defvar isearch-lazy-highlight-window-group nil)
3178 (defvar isearch-lazy-highlight-window-start nil)
3179 (defvar isearch-lazy-highlight-window-end nil)
3180 (defvar isearch-lazy-highlight-case-fold-search nil)
3181 (defvar isearch-lazy-highlight-regexp nil)
3182 (defvar isearch-lazy-highlight-lax-whitespace nil)
3183 (defvar isearch-lazy-highlight-regexp-lax-whitespace nil)
3184 (define-obsolete-variable-alias 'isearch-lazy-highlight-word
3185 'isearch-lazy-highlight-regexp-function "25.1")
3186 (defvar isearch-lazy-highlight-regexp-function nil)
3187 (defvar isearch-lazy-highlight-forward nil)
3188 (defvar isearch-lazy-highlight-error nil)
3190 (defun lazy-highlight-cleanup (&optional force procrastinate)
3191 "Stop lazy highlighting and remove extra highlighting from current buffer.
3192 FORCE non-nil means do it whether or not `lazy-highlight-cleanup' is nil.
3193 PROCRASTINATE non-nil means postpone cleanup to a later time.
3194 This function is called when exiting an incremental search if
3195 `lazy-highlight-cleanup' is non-nil."
3196 (interactive '(t))
3197 (when (and (or force lazy-highlight-cleanup) (not procrastinate))
3198 (while isearch-lazy-highlight-overlays
3199 (delete-overlay (car isearch-lazy-highlight-overlays))
3200 (setq isearch-lazy-highlight-overlays
3201 (cdr isearch-lazy-highlight-overlays))))
3202 (when isearch-lazy-highlight-timer
3203 (cancel-timer isearch-lazy-highlight-timer)
3204 (setq isearch-lazy-highlight-timer nil)))
3206 (defun isearch-lazy-highlight-new-loop (&optional beg end)
3207 "Cleanup any previous `lazy-highlight' loop and begin a new one.
3208 BEG and END specify the bounds within which highlighting should occur.
3209 This is called when `isearch-update' is invoked (which can cause the
3210 search string to change or the window to scroll). It is also used
3211 by other Emacs features."
3212 (when (and (null executing-kbd-macro)
3213 (sit-for 0) ;make sure (window-start) is credible
3214 (or (not (equal isearch-string
3215 isearch-lazy-highlight-last-string))
3216 (not (memq (selected-window)
3217 isearch-lazy-highlight-window-group))
3218 (not (eq isearch-lazy-highlight-case-fold-search
3219 isearch-case-fold-search))
3220 (not (eq isearch-lazy-highlight-regexp
3221 isearch-regexp))
3222 (not (eq isearch-lazy-highlight-regexp-function
3223 isearch-regexp-function))
3224 (not (eq isearch-lazy-highlight-lax-whitespace
3225 isearch-lax-whitespace))
3226 (not (eq isearch-lazy-highlight-regexp-lax-whitespace
3227 isearch-regexp-lax-whitespace))
3228 (not (= (window-group-start)
3229 isearch-lazy-highlight-window-start))
3230 (not (= (window-group-end) ; Window may have been split/joined.
3231 isearch-lazy-highlight-window-end))
3232 (not (eq isearch-forward
3233 isearch-lazy-highlight-forward))
3234 ;; In case we are recovering from an error.
3235 (not (equal isearch-error
3236 isearch-lazy-highlight-error))))
3237 ;; something important did indeed change
3238 (lazy-highlight-cleanup t (not (equal isearch-string ""))) ;stop old timer
3239 (setq isearch-lazy-highlight-error isearch-error)
3240 ;; It used to check for `(not isearch-error)' here, but actually
3241 ;; lazy-highlighting might find matches to highlight even when
3242 ;; `isearch-error' is non-nil. (Bug#9918)
3243 (setq isearch-lazy-highlight-start-limit beg
3244 isearch-lazy-highlight-end-limit end)
3245 (setq isearch-lazy-highlight-window (selected-window)
3246 isearch-lazy-highlight-window-group (selected-window-group)
3247 isearch-lazy-highlight-window-start (window-group-start)
3248 isearch-lazy-highlight-window-end (window-group-end)
3249 ;; Start lazy-highlighting at the beginning of the found
3250 ;; match (`isearch-other-end'). If no match, use point.
3251 ;; One of the next two variables (depending on search direction)
3252 ;; is used to define the starting position of lazy-highlighting
3253 ;; and also to remember the current position of point between
3254 ;; calls of `isearch-lazy-highlight-update', and another variable
3255 ;; is used to define where the wrapped search must stop.
3256 isearch-lazy-highlight-start (or isearch-other-end (point))
3257 isearch-lazy-highlight-end (or isearch-other-end (point))
3258 isearch-lazy-highlight-wrapped nil
3259 isearch-lazy-highlight-last-string isearch-string
3260 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
3261 isearch-lazy-highlight-regexp isearch-regexp
3262 isearch-lazy-highlight-lax-whitespace isearch-lax-whitespace
3263 isearch-lazy-highlight-regexp-lax-whitespace isearch-regexp-lax-whitespace
3264 isearch-lazy-highlight-regexp-function isearch-regexp-function
3265 isearch-lazy-highlight-forward isearch-forward)
3266 (unless (equal isearch-string "")
3267 (setq isearch-lazy-highlight-timer
3268 (run-with-idle-timer lazy-highlight-initial-delay nil
3269 'isearch-lazy-highlight-start)))))
3271 (defun isearch-lazy-highlight-search ()
3272 "Search ahead for the next or previous match, for lazy highlighting.
3273 Attempt to do the search exactly the way the pending Isearch would."
3274 (condition-case nil
3275 (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
3276 (isearch-regexp isearch-lazy-highlight-regexp)
3277 (isearch-regexp-function isearch-lazy-highlight-regexp-function)
3278 (isearch-lax-whitespace
3279 isearch-lazy-highlight-lax-whitespace)
3280 (isearch-regexp-lax-whitespace
3281 isearch-lazy-highlight-regexp-lax-whitespace)
3282 (isearch-forward isearch-lazy-highlight-forward)
3283 (search-invisible nil) ; don't match invisible text
3284 (retry t)
3285 (success nil)
3286 (bound (if isearch-lazy-highlight-forward
3287 (min (or isearch-lazy-highlight-end-limit (point-max))
3288 (if isearch-lazy-highlight-wrapped
3289 (+ isearch-lazy-highlight-start
3290 ;; Extend bound to match whole string at point
3291 (1- (length isearch-lazy-highlight-last-string)))
3292 (window-group-end)))
3293 (max (or isearch-lazy-highlight-start-limit (point-min))
3294 (if isearch-lazy-highlight-wrapped
3295 (- isearch-lazy-highlight-end
3296 ;; Extend bound to match whole string at point
3297 (1- (length isearch-lazy-highlight-last-string)))
3298 (window-group-start))))))
3299 ;; Use a loop like in `isearch-search'.
3300 (while retry
3301 (setq success (isearch-search-string
3302 isearch-lazy-highlight-last-string bound t))
3303 ;; Clear RETRY unless the search predicate says
3304 ;; to skip this search hit.
3305 (if (or (not success)
3306 (= (point) bound) ; like (bobp) (eobp) in `isearch-search'.
3307 (= (match-beginning 0) (match-end 0))
3308 (funcall isearch-filter-predicate
3309 (match-beginning 0) (match-end 0)))
3310 (setq retry nil)))
3311 success)
3312 (error nil)))
3314 (defun isearch-lazy-highlight-start ()
3315 "Start a new lazy-highlight updating loop."
3316 (lazy-highlight-cleanup t) ;remove old overlays
3317 (isearch-lazy-highlight-update))
3319 (defun isearch-lazy-highlight-update ()
3320 "Update highlighting of other matches for current search."
3321 (let ((max lazy-highlight-max-at-a-time)
3322 (looping t)
3323 nomore)
3324 (with-local-quit
3325 (save-selected-window
3326 (if (and (window-live-p isearch-lazy-highlight-window)
3327 (not (memq (selected-window) isearch-lazy-highlight-window-group)))
3328 (select-window isearch-lazy-highlight-window))
3329 (save-excursion
3330 (save-match-data
3331 (goto-char (if isearch-lazy-highlight-forward
3332 isearch-lazy-highlight-end
3333 isearch-lazy-highlight-start))
3334 (while looping
3335 (let ((found (isearch-lazy-highlight-search)))
3336 (when max
3337 (setq max (1- max))
3338 (if (<= max 0)
3339 (setq looping nil)))
3340 (if found
3341 (let ((mb (match-beginning 0))
3342 (me (match-end 0)))
3343 (if (= mb me) ;zero-length match
3344 (if isearch-lazy-highlight-forward
3345 (if (= mb (if isearch-lazy-highlight-wrapped
3346 isearch-lazy-highlight-start
3347 (window-group-end)))
3348 (setq found nil)
3349 (forward-char 1))
3350 (if (= mb (if isearch-lazy-highlight-wrapped
3351 isearch-lazy-highlight-end
3352 (window-group-start)))
3353 (setq found nil)
3354 (forward-char -1)))
3356 ;; non-zero-length match
3357 (let ((ov (make-overlay mb me)))
3358 (push ov isearch-lazy-highlight-overlays)
3359 ;; 1000 is higher than ediff's 100+,
3360 ;; but lower than isearch main overlay's 1001
3361 (overlay-put ov 'priority 1000)
3362 (overlay-put ov 'face 'lazy-highlight)
3363 (unless (eq isearch-lazy-highlight 'all-windows)
3364 (overlay-put ov 'window (selected-window)))))
3365 ;; Remember the current position of point for
3366 ;; the next call of `isearch-lazy-highlight-update'
3367 ;; when `lazy-highlight-max-at-a-time' is too small.
3368 (if isearch-lazy-highlight-forward
3369 (setq isearch-lazy-highlight-end (point))
3370 (setq isearch-lazy-highlight-start (point)))))
3372 ;; not found or zero-length match at the search bound
3373 (if (not found)
3374 (if isearch-lazy-highlight-wrapped
3375 (setq looping nil
3376 nomore t)
3377 (setq isearch-lazy-highlight-wrapped t)
3378 (if isearch-lazy-highlight-forward
3379 (progn
3380 (setq isearch-lazy-highlight-end (window-group-start))
3381 (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
3382 (window-group-start))))
3383 (setq isearch-lazy-highlight-start (window-group-end))
3384 (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
3385 (window-group-end))))))))
3386 (unless nomore
3387 (setq isearch-lazy-highlight-timer
3388 (run-at-time lazy-highlight-interval nil
3389 'isearch-lazy-highlight-update)))))))))
3391 (defun isearch-resume (string regexp word forward message case-fold)
3392 "Resume an incremental search.
3393 STRING is the string or regexp searched for.
3394 REGEXP non-nil means the resumed search was a regexp search.
3395 WORD non-nil means resume a word search.
3396 FORWARD non-nil means resume a forward search.
3397 MESSAGE is the echo-area message recorded for the search resumed.
3398 CASE-FOLD non-nil means the search was case-insensitive."
3399 (isearch-mode forward regexp nil nil word)
3400 (setq isearch-string string
3401 isearch-message message
3402 isearch-case-fold-search case-fold)
3403 (isearch-search)
3404 (isearch-update))
3406 (provide 'isearch)
3408 ;;; isearch.el ends here