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