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