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