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