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