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