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