(filesets-data): Add another defvar.
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
55535639 1;;; isearch.el --- incremental search minor mode
3b1e4dd1 2
c789e608 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 01, 2003
1ea1fb7e 4;; 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
14;; the Free Software Foundation; either version 2, or (at your option)
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
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, 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
60;; TODO
eb8c3be9 61;; - Integrate the emacs 19 generalized command history.
08200510
RS
62;; - Think about incorporating query-replace.
63;; - Hooks and options for failed search.
282d89c0 64
3b1e4dd1
ER
65;;; Change Log:
66
b578f267
EN
67;; Changes before those recorded in ChangeLog:
68
69;; Revision 1.4 92/09/14 16:26:02 liberte
70;; Added prefix args to isearch-forward, etc. to switch between
71;; string and regular expression searching.
72;; Added some support for lemacs.
73;; Added general isearch-highlight option - but only for lemacs so far.
74;; Added support for frame switching in emacs 19.
75;; Added word search option to isearch-edit-string.
76;; Renamed isearch-quit to isearch-abort.
77;; Numerous changes to comments and doc strings.
b48ca14f 78;;
b578f267
EN
79;; Revision 1.3 92/06/29 13:10:08 liberte
80;; Moved modal isearch-mode handling into isearch-mode.
81;; Got rid of buffer-local isearch variables.
82;; isearch-edit-string used by ring adjustments, completion, and
83;; nonincremental searching. C-s and C-r are additional exit commands.
84;; Renamed all regex to regexp.
85;; Got rid of found-start and found-point globals.
86;; Generalized handling of upper-case chars.
87
88;; Revision 1.2 92/05/27 11:33:57 liberte
89;; Emacs version 19 has a search ring, which is supported here.
90;; Other fixes found in the version 19 isearch are included here.
91;;
92;; Also see variables search-caps-disable-folding,
93;; search-nonincremental-instead, search-whitespace-regexp, and
94;; commands isearch-toggle-regexp, isearch-edit-string.
95;;
96;; semi-modal isearching is supported.
97
98;; Changes for 1.1
99;; 3/18/92 Fixed invalid-regexp.
100;; 3/18/92 Fixed yanking in regexps.
282d89c0 101
3b1e4dd1 102;;; Code:
08200510
RS
103
104\f
191f025a 105;; Some additional options and constants.
08200510 106
9c1db929
RS
107(defgroup isearch nil
108 "Incremental search minor mode."
117132a6
DL
109 :link '(emacs-commentary-link "isearch")
110 :link '(custom-manual "(emacs)Incremental Search")
9c1db929
RS
111 :prefix "isearch-"
112 :prefix "search-"
113 :group 'matching)
fdf5afa4 114
9c1db929
RS
115
116(defcustom search-exit-option t
117 "*Non-nil means random control characters terminate incremental search."
118 :type 'boolean
119 :group 'isearch)
120
121(defcustom search-slow-window-lines 1
fdf5afa4
RS
122 "*Number of lines in slow search display windows.
123These are the short windows used during incremental search on slow terminals.
124Negative means put the slow search window at the top (normally it's at bottom)
9c1db929
RS
125and the value is minus the number of lines."
126 :type 'integer
127 :group 'isearch)
fdf5afa4 128
9c1db929 129(defcustom search-slow-speed 1200
fdf5afa4
RS
130 "*Highest terminal speed at which to use \"slow\" style incremental search.
131This is the style where a one-line window is created to show the line
9c1db929
RS
132that the search has reached."
133 :type 'integer
134 :group 'isearch)
8e1cae6d 135
9c1db929 136(defcustom search-upper-case 'not-yanks
8e1cae6d 137 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
138That is, upper and lower case chars must match exactly.
139This applies no matter where the chars come from, but does not
fdf5afa4 140apply to chars in regexps that are prefixed with `\\'.
9c1db929
RS
141If this value is `not-yanks', yanked text is always downcased."
142 :type '(choice (const :tag "off" nil)
143 (const not-yanks)
5786b3ed 144 (other :tag "on" t))
9c1db929 145 :group 'isearch)
8e1cae6d 146
9c1db929 147(defcustom search-nonincremental-instead t
8e1cae6d 148 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510 149Actually, `isearch-edit-string' is called to let you enter the search
9c1db929
RS
150string, and RET terminates editing and does a nonincremental search."
151 :type 'boolean
152 :group 'isearch)
8e1cae6d 153
9c1db929 154(defcustom search-whitespace-regexp "\\s-+"
8e1cae6d 155 "*If non-nil, regular expression to match a sequence of whitespace chars.
e2b992cb 156This applies to regular expression incremental search.
dcb6bf2b
RS
157You might want to use something like \"[ \\t\\r\\n]+\" instead.
158In the Customization buffer, that is `[' followed by a space,
159a tab, a carriage return (control-M), a newline, and `]+'."
9c1db929
RS
160 :type 'regexp
161 :group 'isearch)
8e1cae6d 162
30178dde 163(defcustom search-highlight t
9c1db929
RS
164 "*Non-nil means incremental search highlights the current match."
165 :type 'boolean
166 :group 'isearch)
08200510 167
0352b205
RS
168(defcustom search-invisible 'open
169 "If t incremental search can match hidden text.
170nil means don't match invisible text.
171If the value is `open', if the text matched is made invisible by
172an overlay having an `invisible' property and that overlay has a property
173`isearch-open-invisible', then incremental search will show the contents.
cd59ea72
SM
174\(This applies when using `outline.el' and `hideshow.el'.)
175See also `reveal-mode' if you want overlays to automatically be opened
176whenever point is in one of them."
0352b205
RS
177 :type '(choice (const :tag "Match hidden text" t)
178 (const :tag "Open overlays" open)
79c7a4fa 179 (const :tag "Don't match hidden text" nil))
0352b205
RS
180 :group 'isearch)
181
182(defcustom isearch-hide-immediately t
68c18d6d
RS
183 "If non-nil, re-hide an invisible match right away.
184This variable makes a difference when `search-invisible' is set to `open'.
185It means that after search makes some invisible text visible
186to show the match, it makes the text invisible again when the match moves.
b48ca14f
JB
187Ordinarily the text becomes invisible again at the end of the search."
188 :type 'boolean
0352b205 189 :group 'isearch)
86bfaffe 190
6848c9f1
KS
191(defcustom isearch-resume-enabled t
192 "*If non-nil, `isearch-resume' commands are added to the command history."
193 :type 'boolean
194 :group 'isearch)
195
08200510
RS
196(defvar isearch-mode-hook nil
197 "Function(s) to call after starting up an incremental search.")
198
199(defvar isearch-mode-end-hook nil
200 "Function(s) to call after terminating an incremental search.")
201
191f025a 202;; Search ring.
8e1cae6d
JB
203
204(defvar search-ring nil
205 "List of search string sequences.")
08200510 206(defvar regexp-search-ring nil
8e1cae6d
JB
207 "List of regular expression search string sequences.")
208
9c1db929
RS
209(defcustom search-ring-max 16
210 "*Maximum length of search ring before oldest elements are thrown away."
211 :type 'integer
212 :group 'isearch)
213(defcustom regexp-search-ring-max 16
214 "*Maximum length of regexp search ring before oldest elements are thrown away."
215 :type 'integer
216 :group 'isearch)
8e1cae6d
JB
217
218(defvar search-ring-yank-pointer nil
a5dcf63f
RS
219 "Index in `search-ring' of last string reused.
220nil if none yet.")
08200510 221(defvar regexp-search-ring-yank-pointer nil
a5dcf63f
RS
222 "Index in `regexp-search-ring' of last string reused.
223nil if none yet.")
8e1cae6d 224
9c1db929 225(defcustom search-ring-update nil
08200510 226 "*Non-nil if advancing or retreating in the search ring should cause search.
9c1db929
RS
227Default value, nil, means edit the string instead."
228 :type 'boolean
229 :group 'isearch)
08200510 230
191f025a 231;; Define isearch-mode keymap.
8e1cae6d 232
02b2f510
SM
233(defvar isearch-mode-map
234 (let* ((i 0)
235 (map (make-keymap)))
236 (or (vectorp (nth 1 map))
237 (char-table-p (nth 1 map))
238 (error "The initialization of isearch-mode-map must be updated"))
239 ;; Make all multibyte characters search for themselves.
240 (let ((l (generic-character-list))
241 (table (nth 1 map)))
242 (while l
243 (set-char-table-default table (car l) 'isearch-printing-char)
244 (setq l (cdr l))))
245 ;; Make function keys, etc, exit the search.
246 (define-key map [t] 'isearch-other-control-char)
247 ;; Control chars, by default, end isearch mode transparently.
b48ca14f 248 ;; We need these explicit definitions because, in a dense keymap,
02b2f510
SM
249 ;; the binding for t does not affect characters.
250 ;; We use a dense keymap to save space.
251 (while (< i ?\ )
252 (define-key map (make-string 1 i) 'isearch-other-control-char)
253 (setq i (1+ i)))
254
255 ;; Single-byte printing chars extend the search string by default.
256 (setq i ?\ )
257 (while (< i 256)
258 (define-key map (vector i) 'isearch-printing-char)
259 (setq i (1+ i)))
260
261 ;; To handle local bindings with meta char prefix keys, define
262 ;; another full keymap. This must be done for any other prefix
263 ;; keys as well, one full keymap per char of the prefix key. It
264 ;; would be simpler to disable the global keymap, and/or have a
265 ;; default local key binding for any key not otherwise bound.
266 (let ((meta-map (make-sparse-keymap)))
267 (define-key map (char-to-string meta-prefix-char) meta-map)
268 (define-key map [escape] meta-map))
269 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
270
271 ;; Several non-printing chars change the searching behavior.
272 (define-key map "\C-s" 'isearch-repeat-forward)
273 (define-key map "\C-r" 'isearch-repeat-backward)
274 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
275 ;; combinations can be used to repeat regexp isearches that can
276 ;; be used to start these searches.
277 (define-key map "\M-\C-s" 'isearch-repeat-forward)
278 (define-key map "\M-\C-r" 'isearch-repeat-backward)
279 (define-key map "\177" 'isearch-delete-char)
280 (define-key map "\C-g" 'isearch-abort)
1f4139d5 281
02b2f510
SM
282 ;; This assumes \e is the meta-prefix-char.
283 (or (= ?\e meta-prefix-char)
284 (error "Inconsistency in isearch.el"))
285 (define-key map "\e\e\e" 'isearch-cancel)
286 (define-key map [escape escape escape] 'isearch-cancel)
b48ca14f 287
02b2f510 288 (define-key map "\C-q" 'isearch-quote-char)
08200510 289
02b2f510
SM
290 (define-key map "\r" 'isearch-exit)
291 (define-key map "\C-j" 'isearch-printing-char)
292 (define-key map "\t" 'isearch-printing-char)
293 (define-key map " " 'isearch-whitespace-chars)
294 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
b48ca14f 295
868bf43a 296 (define-key map "\C-w" 'isearch-yank-word-or-char)
02b2f510 297 (define-key map "\C-y" 'isearch-yank-line)
08200510 298
02b2f510
SM
299 ;; Define keys for regexp chars * ? |.
300 ;; Nothing special for + because it matches at least once.
301 (define-key map "*" 'isearch-*-char)
302 (define-key map "?" 'isearch-*-char)
303 (define-key map "|" 'isearch-|-char)
08200510 304
191f025a
SM
305 ;; Turned off because I find I expect to get the global definition--rms.
306 ;; ;; Instead bind C-h to special help command for isearch-mode.
307 ;; (define-key map "\C-h" 'isearch-mode-help)
08200510 308
02b2f510
SM
309 (define-key map "\M-n" 'isearch-ring-advance)
310 (define-key map "\M-p" 'isearch-ring-retreat)
311 (define-key map "\M-y" 'isearch-yank-kill)
312
313 (define-key map "\M-\t" 'isearch-complete)
314
315 ;; Pass frame events transparently so they won't exit the search.
316 ;; In particular, if we have more than one display open, then a
317 ;; switch-frame might be generated by someone typing at another keyboard.
318 (define-key map [switch-frame] nil)
319 (define-key map [delete-frame] nil)
320 (define-key map [iconify-frame] nil)
321 (define-key map [make-frame-visible] nil)
3f482bc0 322 (define-key map [mouse-movement] nil)
02b2f510
SM
323 ;; For searching multilingual text.
324 (define-key map "\C-\\" 'isearch-toggle-input-method)
325 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
326
327 ;; People expect to be able to paste with the mouse.
e4af1426 328 (define-key map [mouse-2] #'isearch-mouse-2)
02b2f510
SM
329 (define-key map [down-mouse-2] nil)
330
331 ;; Some bindings you may want to put in your isearch-mode-hook.
332 ;; Suggest some alternates...
333 (define-key map "\M-c" 'isearch-toggle-case-fold)
334 (define-key map "\M-r" 'isearch-toggle-regexp)
335 (define-key map "\M-e" 'isearch-edit-string)
336
337 map)
338 "Keymap for `isearch-mode'.")
339
340(defvar minibuffer-local-isearch-map
341 (let ((map (make-sparse-keymap)))
342 (set-keymap-parent map minibuffer-local-map)
343 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
344 (define-key map "\M-n" 'isearch-ring-advance-edit)
345 (define-key map "\M-p" 'isearch-ring-retreat-edit)
346 (define-key map "\M-\t" 'isearch-complete-edit)
347 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
348 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
349 map)
08200510 350 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 351
8e1cae6d 352;; Internal variables declared globally for byte-compiler.
08200510
RS
353;; These are all set with setq while isearching
354;; and bound locally while editing the search string.
355
356(defvar isearch-forward nil) ; Searching in the forward direction.
357(defvar isearch-regexp nil) ; Searching for a regexp.
358(defvar isearch-word nil) ; Searching for words.
cd59ea72 359(defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
08200510 360
191f025a
SM
361(defvar isearch-cmds nil
362 "Stack of search status sets.
363Each set is a list of the form:
364 (STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
365 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH)")
366
08200510
RS
367(defvar isearch-string "") ; The current search string.
368(defvar isearch-message "") ; text-char-description version of isearch-string
369
370(defvar isearch-success t) ; Searching is currently successful.
371(defvar isearch-invalid-regexp nil) ; Regexp not well formed.
c5f6b356 372(defvar isearch-within-brackets nil) ; Regexp has unclosed [.
08200510
RS
373(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
374(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d 375(defvar isearch-barrier 0)
99ae9e9f 376(defvar isearch-just-started nil)
ddbe3d5f 377(defvar isearch-start-hscroll 0) ; hscroll when starting the search.
8e1cae6d 378
4453091d
RS
379; case-fold-search while searching.
380; either nil, t, or 'yes. 'yes means the same as t except that mixed
381; case in the search string is ignored.
382(defvar isearch-case-fold-search nil)
8e1cae6d 383
67085aba
GM
384(defvar isearch-last-case-fold-search nil)
385
ae1a21c6
MB
386;; Used to save default value while isearch is active
387(defvar isearch-original-minibuffer-message-timeout nil)
388
8e1cae6d
JB
389(defvar isearch-adjusted nil)
390(defvar isearch-slow-terminal-mode nil)
191f025a 391;; If t, using a small window.
08200510 392(defvar isearch-small-window nil)
8e1cae6d 393(defvar isearch-opoint 0)
191f025a 394;; The window configuration active at the beginning of the search.
08200510 395(defvar isearch-window-configuration nil)
8e1cae6d 396
08200510
RS
397;; Flag to indicate a yank occurred, so don't move the cursor.
398(defvar isearch-yank-flag nil)
8e1cae6d 399
191f025a
SM
400;; A function to be called after each input character is processed.
401;; (It is not called after characters that exit the search.)
402;; It is only set from an optional argument to `isearch-mode'.
08200510 403(defvar isearch-op-fun nil)
8e1cae6d 404
191f025a 405;; Is isearch-mode in a recursive edit for modal searching.
08200510 406(defvar isearch-recursive-edit nil)
8e1cae6d 407
191f025a 408;; Should isearch be terminated after doing one search?
08200510
RS
409(defvar isearch-nonincremental nil)
410
411;; New value of isearch-forward after isearch-edit-string.
412(defvar isearch-new-forward nil)
8e1cae6d 413
0352b205
RS
414;; Accumulate here the overlays opened during searching.
415(defvar isearch-opened-overlays nil)
8e1cae6d 416
99848db0
KH
417;; The value of input-method-function when isearch is invoked.
418(defvar isearch-input-method-function nil)
419
420;; A flag to tell if input-method-function is locally bound when
421;; isearch is invoked.
422(defvar isearch-input-method-local-p nil)
423
8e1cae6d
JB
424;; Minor-mode-alist changes - kind of redundant with the
425;; echo area, but if isearching in multiple windows, it can be useful.
426
427(or (assq 'isearch-mode minor-mode-alist)
428 (nconc minor-mode-alist
429 (list '(isearch-mode isearch-mode))))
430
08200510 431(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
432(make-variable-buffer-local 'isearch-mode)
433
fdf5afa4
RS
434(define-key global-map "\C-s" 'isearch-forward)
435(define-key esc-map "\C-s" 'isearch-forward-regexp)
436(define-key global-map "\C-r" 'isearch-backward)
437(define-key esc-map "\C-r" 'isearch-backward-regexp)
438
191f025a 439;; Entry points to isearch-mode.
8e1cae6d 440
eceee2c0 441(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
442 "\
443Do incremental search forward.
08200510
RS
444With a prefix argument, do an incremental regular expression search instead.
445\\<isearch-mode-map>
8e1cae6d 446As you type characters, they add to the search string and are found.
b48ca14f 447The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 448
08200510 449Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 450Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
451Type LFD (C-j) to match end of line.
452Type \\[isearch-repeat-forward] to search again forward,\
453 \\[isearch-repeat-backward] to search again backward.
21d90805 454Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
08200510
RS
455 string and search for it.
456Type \\[isearch-yank-line] to yank rest of line onto end of search string\
457 and search for it.
8617e346
KH
458Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
459 and search for it.
8e1cae6d 460Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
461\\[isearch-abort] while searching or when search has failed cancels input\
462 back to what has
463 been found successfully.
464\\[isearch-abort] when search is successful aborts and moves point to\
465 starting point.
8e1cae6d 466
6c551d4e
EZ
467Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
468Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
469Type \\[isearch-edit-string] to edit the search string in the minibuffer.
470
8e1cae6d
JB
471Also supported is a search ring of the previous 16 search strings.
472Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
473Type \\[isearch-ring-retreat] to search for the previous item in the search\
474 ring.
475Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 476
1e14b095 477If an input method is turned on in the current buffer, that input
b48ca14f 478method is also active while you are typing characters to search. To
37b20c9b
KH
479toggle the input method, type \\[isearch-toggle-input-method]. It
480also toggles the input method in the current buffer.
481
482To use a different input method for searching, type
1e14b095 483\\[isearch-toggle-specified-input-method], and specify an input method
37b20c9b
KH
484you want to use.
485
b48ca14f 486The above keys, bound in `isearch-mode-map', are often controlled by
08200510 487 options; do M-x apropos on search-.* to find them.
8e1cae6d 488Other control and meta characters terminate the search
08200510 489 and are then executed normally (depending on `search-exit-option').
fd49e05c 490Likewise for function keys and mouse button events.
8e1cae6d 491
08200510
RS
492If this function is called non-interactively, it does not return to
493the calling function until the search is done."
8e1cae6d 494
eceee2c0 495 (interactive "P\np")
4ae7d00a 496 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 497
eceee2c0 498(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
499 "\
500Do incremental search forward for regular expression.
08200510 501With a prefix argument, do a regular string search instead.
8e1cae6d
JB
502Like ordinary incremental search except that your input
503is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 504 (interactive "P\np")
4ae7d00a 505 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 506
eceee2c0 507(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
508 "\
509Do incremental search backward.
08200510 510With a prefix argument, do a regular expression search instead.
8e1cae6d 511See \\[isearch-forward] for more information."
eceee2c0 512 (interactive "P\np")
4ae7d00a 513 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 514
eceee2c0 515(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
516 "\
517Do incremental search backward for regular expression.
08200510 518With a prefix argument, do a regular string search instead.
8e1cae6d
JB
519Like ordinary incremental search except that your input
520is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 521 (interactive "P\np")
4ae7d00a 522 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
523
524
525(defun isearch-mode-help ()
526 (interactive)
527 (describe-function 'isearch-forward)
528 (isearch-update))
8e1cae6d
JB
529
530\f
8e1cae6d
JB
531;; isearch-mode only sets up incremental search for the minor mode.
532;; All the work is done by the isearch-mode commands.
533
08200510 534;; Not used yet:
d7fa5aa2 535;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
536;; isearch-forward-regexp isearch-backward-regexp)
537;; "List of commands for which isearch-mode does not recursive-edit.")
b48ca14f 538
08200510
RS
539
540(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
541 "Start isearch minor mode. Called by `isearch-forward', etc.
542
543\\{isearch-mode-map}"
8e1cae6d
JB
544
545 ;; Initialize global vars.
546 (setq isearch-forward forward
547 isearch-regexp regexp
08200510 548 isearch-word word-p
8e1cae6d 549 isearch-op-fun op-fun
67085aba 550 isearch-last-case-fold-search isearch-case-fold-search
8e1cae6d
JB
551 isearch-case-fold-search case-fold-search
552 isearch-string ""
553 isearch-message ""
554 isearch-cmds nil
555 isearch-success t
556 isearch-wrapped nil
557 isearch-barrier (point)
558 isearch-adjusted nil
559 isearch-yank-flag nil
560 isearch-invalid-regexp nil
c5f6b356 561 isearch-within-brackets nil
0cabad13 562 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d 563 (> (window-height)
fad241d3
RS
564 (* 4
565 (abs search-slow-window-lines))))
8e1cae6d
JB
566 isearch-other-end nil
567 isearch-small-window nil
99ae9e9f 568 isearch-just-started t
ddbe3d5f 569 isearch-start-hscroll (window-hscroll)
8e1cae6d 570
8e1cae6d 571 isearch-opoint (point)
a5dcf63f 572 search-ring-yank-pointer nil
0352b205 573 isearch-opened-overlays nil
99848db0
KH
574 isearch-input-method-function input-method-function
575 isearch-input-method-local-p (local-variable-p 'input-method-function)
ae1a21c6
MB
576 regexp-search-ring-yank-pointer nil
577
578 ;; Save the original value of `minibuffer-message-timeout', and
579 ;; set it to nil so that isearch's messages don't get timed out.
580 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
581 minibuffer-message-timeout nil)
99848db0
KH
582
583 ;; We must bypass input method while reading key. When a user type
584 ;; printable character, appropriate input method is turned on in
380866a2 585 ;; minibuffer to read multibyte characters.
99848db0
KH
586 (or isearch-input-method-local-p
587 (make-local-variable 'input-method-function))
588 (setq input-method-function nil)
589
99ae9e9f 590 (looking-at "")
292a8dff
RS
591 (setq isearch-window-configuration
592 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 593
18ce7555
RS
594 ;; Maybe make minibuffer frame visible and/or raise it.
595 (let ((frame (window-frame (minibuffer-window))))
360e0dd5
RS
596 (unless (memq (frame-live-p frame) '(nil t))
597 (unless (frame-visible-p frame)
598 (make-frame-visible frame))
599 (if minibuffer-auto-raise
600 (raise-frame frame))))
18ce7555 601
8e1cae6d 602 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 603 (force-mode-line-update)
8e1cae6d
JB
604
605 (isearch-push-state)
606
c7955222 607 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
608 (isearch-update)
609 (run-hooks 'isearch-mode-hook)
08200510 610
bfe2d334 611 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 612 (add-hook 'kbd-macro-termination-hook 'isearch-done)
6e5cd0ae 613
b48ca14f
JB
614 ;; isearch-mode can be made modal (in the sense of not returning to
615 ;; the calling function until searching is completed) by entering
08200510 616 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
617 (if recursive-edit
618 (let ((isearch-recursive-edit t))
619 (recursive-edit)))
0daa17f3 620 isearch-success)
8e1cae6d
JB
621
622
8e1cae6d
JB
623;; Some high level utilities. Others below.
624
625(defun isearch-update ()
191f025a 626 ;; Called after each command to update the display.
4c01d4bd
RS
627 (if (and (null unread-command-events)
628 (null executing-kbd-macro))
8e1cae6d 629 (progn
c982ab21
GM
630 (if (not (input-pending-p))
631 (isearch-message))
632 (if (and isearch-slow-terminal-mode
b48ca14f 633 (not (or isearch-small-window
c982ab21
GM
634 (pos-visible-in-window-p))))
635 (let ((found-point (point)))
636 (setq isearch-small-window t)
637 (move-to-window-line 0)
638 (let ((window-min-height 1))
639 (split-window nil (if (< search-slow-window-lines 0)
640 (1+ (- search-slow-window-lines))
641 (- (window-height)
642 (1+ search-slow-window-lines)))))
643 (if (< search-slow-window-lines 0)
644 (progn (vertical-motion (- 1 search-slow-window-lines))
645 (set-window-start (next-window) (point))
646 (set-window-hscroll (next-window)
647 (window-hscroll))
648 (set-window-hscroll (selected-window) 0))
649 (other-window 1))
ddbe3d5f
RS
650 (goto-char found-point))
651 ;; Keep same hscrolling as at the start of the search when possible
652 (let ((current-scroll (window-hscroll)))
653 (set-window-hscroll (selected-window) isearch-start-hscroll)
654 (unless (pos-visible-in-window-p)
655 (set-window-hscroll (selected-window) current-scroll))))
656 (if isearch-other-end
c982ab21
GM
657 (if (< isearch-other-end (point)) ; isearch-forward?
658 (isearch-highlight isearch-other-end (point))
659 (isearch-highlight (point) isearch-other-end))
660 (isearch-dehighlight nil))
661 ))
8e1cae6d
JB
662 (setq ;; quit-flag nil not for isearch-mode
663 isearch-adjusted nil
664 isearch-yank-flag nil)
a5cc922e
KH
665 (isearch-lazy-highlight-new-loop)
666 ;; We must prevent the point moving to the end of composition when a
667 ;; part of the composition has just been searched.
668 (setq disable-point-adjustment t))
8e1cae6d 669
0daa17f3 670(defun isearch-done (&optional nopush edit)
6848c9f1
KS
671 (if isearch-resume-enabled
672 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
673 ,isearch-word ,isearch-forward
674 ,isearch-message
675 ',isearch-case-fold-search)))
676 (unless (equal (car command-history) command)
677 (setq command-history (cons command command-history)))))
c40a4de1 678
bfe2d334 679 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 680 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
d196f58d
GM
681 (setq isearch-lazy-highlight-start nil)
682
8e1cae6d 683 ;; Called by all commands that terminate isearch-mode.
35a4d143 684 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 685 (setq overriding-terminal-local-map nil)
08200510 686 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
ae1a21c6 687 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
08200510 688 (isearch-dehighlight t)
67ecad4e 689 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
08200510
RS
690 (let ((found-start (window-start (selected-window)))
691 (found-point (point)))
70418205
RS
692 (if isearch-window-configuration
693 (set-window-configuration isearch-window-configuration))
08200510 694
d49b6338
RS
695 (if isearch-small-window
696 (goto-char found-point)
697 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 698 (set-window-start (selected-window) found-start t))
d49b6338 699
08200510
RS
700 ;; If there was movement, mark the starting position.
701 ;; Maybe should test difference between and set mark iff > threshold.
702 (if (/= (point) isearch-opoint)
9821535a
RS
703 (or (and transient-mark-mode mark-active)
704 (progn
705 (push-mark isearch-opoint t)
f3acf6f5 706 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 707 (message "Mark saved where search started"))))))
08200510
RS
708
709 (setq isearch-mode nil)
99848db0
KH
710 (if isearch-input-method-local-p
711 (setq input-method-function isearch-input-method-function)
712 (kill-local-variable 'input-method-function))
713
1cd3732c 714 (force-mode-line-update)
8e1cae6d 715
df01192b
RS
716 ;; If we ended in the middle of some intangible text,
717 ;; move to the further end of that intangible text.
718 (let ((after (if (eobp) nil
719 (get-text-property (point) 'intangible)))
720 (before (if (bobp) nil
721 (get-text-property (1- (point)) 'intangible))))
722 (when (and before after (eq before after))
723 (if isearch-forward
724 (goto-char (next-single-property-change (point) 'intangible))
725 (goto-char (previous-single-property-change (point) 'intangible)))))
726
35a4d143 727 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 728 ;; Update the ring data.
73d2bf95 729 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 730
8e1cae6d 731 (run-hooks 'isearch-mode-end-hook)
0daa17f3 732 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 733
73d2bf95
RS
734(defun isearch-update-ring (string &optional regexp)
735 "Add STRING to the beginning of the search ring.
736REGEXP says which ring to use."
b48ca14f 737 (if regexp
73d2bf95 738 (if (or (null regexp-search-ring)
fd89878d 739 (not (string= string (car regexp-search-ring))))
73d2bf95 740 (progn
c789e608 741 (push string regexp-search-ring)
73d2bf95
RS
742 (if (> (length regexp-search-ring) regexp-search-ring-max)
743 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
744 nil))))
745 (if (or (null search-ring)
fd89878d 746 (not (string= string (car search-ring))))
73d2bf95 747 (progn
c789e608 748 (push string search-ring)
73d2bf95
RS
749 (if (> (length search-ring) search-ring-max)
750 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
751
191f025a
SM
752;; Switching buffers should first terminate isearch-mode.
753;; ;; For Emacs 19, the frame switch event is handled.
754;; (defun isearch-switch-frame-handler ()
755;; (interactive) ;; Is this necessary?
756;; ;; First terminate isearch-mode.
757;; (isearch-done)
758;; (isearch-clean-overlays)
759;; (handle-switch-frame (car (cdr last-command-char))))
08200510 760
8e1cae6d 761\f
8e1cae6d
JB
762;; Commands active while inside of the isearch minor mode.
763
764(defun isearch-exit ()
765 "Exit search normally.
766However, if this is the first command after starting incremental
767search and `search-nonincremental-instead' is non-nil, do a
08200510 768nonincremental search instead via `isearch-edit-string'."
8e1cae6d 769 (interactive)
b48ca14f 770 (if (and search-nonincremental-instead
8e1cae6d 771 (= 0 (length isearch-string)))
08200510
RS
772 (let ((isearch-nonincremental t))
773 (isearch-edit-string)))
0352b205
RS
774 (isearch-done)
775 (isearch-clean-overlays))
8e1cae6d
JB
776
777
778(defun isearch-edit-string ()
08200510
RS
779 "Edit the search string in the minibuffer.
780The following additional command keys are active while editing.
781\\<minibuffer-local-isearch-map>
782\\[exit-minibuffer] to resume incremental searching with the edited string.
783\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
784\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 785\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 786\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 787\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 788\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 789\\<isearch-mode-map>
21d90805 790If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
08200510
RS
791
792 ;; This code is very hairy for several reasons, explained in the code.
793 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
794 ;; If there were a way to catch any change of buffer from the minibuffer,
795 ;; this could be simplified greatly.
eb8c3be9 796 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 797 (interactive)
08200510 798 (condition-case err
e900ced4
RS
799 (progn
800 (let ((isearch-nonincremental isearch-nonincremental)
801
802 ;; Locally bind all isearch global variables to protect them
803 ;; from recursive isearching.
804 ;; isearch-string -message and -forward are not bound
805 ;; so they may be changed. Instead, save the values.
806 (isearch-new-string isearch-string)
807 (isearch-new-message isearch-message)
808 (isearch-new-forward isearch-forward)
809 (isearch-new-word isearch-word)
810
811 (isearch-regexp isearch-regexp)
812 (isearch-op-fun isearch-op-fun)
813 (isearch-cmds isearch-cmds)
814 (isearch-success isearch-success)
815 (isearch-wrapped isearch-wrapped)
816 (isearch-barrier isearch-barrier)
817 (isearch-adjusted isearch-adjusted)
818 (isearch-yank-flag isearch-yank-flag)
819 (isearch-invalid-regexp isearch-invalid-regexp)
820 (isearch-within-brackets isearch-within-brackets)
821 ;;; Don't bind this. We want isearch-search, below, to set it.
822 ;;; And the old value won't matter after that.
823 ;;; (isearch-other-end isearch-other-end)
824 ;;; Perhaps some of these other variables should be bound for a
825 ;;; shorter period, ending before the next isearch-search.
826 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
827 (isearch-opoint isearch-opoint)
828 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
829 (isearch-small-window isearch-small-window)
830 (isearch-recursive-edit isearch-recursive-edit)
831 ;; Save current configuration so we can restore it here.
832 (isearch-window-configuration (current-window-configuration))
ae1a21c6
MB
833
834 ;; Temporarily restore `minibuffer-message-timeout'.
835 (minibuffer-message-timeout
836 isearch-original-minibuffer-message-timeout)
837 (isearch-original-minibuffer-message-timeout
838 isearch-original-minibuffer-message-timeout)
e900ced4
RS
839 )
840
841 ;; Actually terminate isearching until editing is done.
b48ca14f 842 ;; This is so that the user can do anything without failure,
e900ced4
RS
843 ;; like switch buffers and start another isearch, and return.
844 (condition-case err
845 (isearch-done t t)
846 (exit nil)) ; was recursive editing
847
848 (isearch-message) ;; for read-char
849 (unwind-protect
b48ca14f 850 (let* (;; Why does following read-char echo?
e900ced4
RS
851 ;;(echo-keystrokes 0) ;; not needed with above message
852 (e (let ((cursor-in-echo-area t))
853 (read-event)))
854 ;; Binding minibuffer-history-symbol to nil is a work-around
855 ;; for some incompatibility with gmhist.
856 (minibuffer-history-symbol)
857 (message-log-max nil))
858 ;; If the first character the user types when we prompt them
859 ;; for a string is the yank-word character, then go into
860 ;; word-search mode. Otherwise unread that character and
861 ;; read a key the normal way.
862 ;; Word search does not apply (yet) to regexp searches,
863 ;; no check is made here.
864 (message (isearch-message-prefix nil nil t))
88715f05
RS
865 (if (memq (lookup-key isearch-mode-map (vector e))
866 '(isearch-yank-word
867 isearch-yank-word-or-char))
e900ced4
RS
868 (setq isearch-word t;; so message-prefix is right
869 isearch-new-word t)
870 (cancel-kbd-macro-events)
871 (isearch-unread e))
872 (setq cursor-in-echo-area nil)
873 (setq isearch-new-string
874 (let (junk-ring)
875 (read-from-minibuffer
876 (isearch-message-prefix nil nil isearch-nonincremental)
877 isearch-string
878 minibuffer-local-isearch-map nil
879 'junk-ring))
880 isearch-new-message
881 (mapconcat 'isearch-text-char-description
882 isearch-new-string "")))
883 ;; Always resume isearching by restarting it.
b48ca14f
JB
884 (isearch-mode isearch-forward
885 isearch-regexp
886 isearch-op-fun
e900ced4
RS
887 nil
888 isearch-word)
889
890 ;; Copy new local values to isearch globals
891 (setq isearch-string isearch-new-string
892 isearch-message isearch-new-message
893 isearch-forward isearch-new-forward
894 isearch-word isearch-new-word))
895
896 ;; Empty isearch-string means use default.
897 (if (= 0 (length isearch-string))
898 (setq isearch-string (or (car (if isearch-regexp
899 regexp-search-ring
900 search-ring))
8cc3c821
RS
901 "")
902
903 isearch-message
904 (mapconcat 'isearch-text-char-description
905 isearch-string ""))
e900ced4
RS
906 ;; This used to set the last search string,
907 ;; but I think it is not right to do that here.
908 ;; Only the string actually used should be saved.
909 ))
910
911 ;; Push the state as of before this C-s.
912 (isearch-push-state)
08200510
RS
913
914 ;; Reinvoke the pending search.
08200510
RS
915 (isearch-search)
916 (isearch-update)
b48ca14f 917 (if isearch-nonincremental
08200510
RS
918 (progn
919 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
920 (isearch-done))))
8e1cae6d 921
08200510
RS
922 (quit ; handle abort-recursive-edit
923 (isearch-abort) ;; outside of let to restore outside global values
924 )))
925
926(defun isearch-nonincremental-exit-minibuffer ()
927 (interactive)
928 (setq isearch-nonincremental t)
929 (exit-minibuffer))
930
931(defun isearch-forward-exit-minibuffer ()
932 (interactive)
933 (setq isearch-new-forward t)
934 (exit-minibuffer))
8e1cae6d 935
08200510 936(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 937 (interactive)
08200510
RS
938 (setq isearch-new-forward nil)
939 (exit-minibuffer))
940
19993c54
RS
941(defun isearch-cancel ()
942 "Terminate the search and go back to the starting point."
943 (interactive)
944 (goto-char isearch-opoint)
945 (isearch-done t)
0352b205 946 (isearch-clean-overlays)
19993c54 947 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
948
949(defun isearch-abort ()
b7852303 950 "Abort incremental search mode if searching is successful, signaling quit.
08200510 951Otherwise, revert to previous successful search and continue searching.
b7852303 952Use `isearch-exit' to quit without signaling."
08200510 953 (interactive)
eb8c3be9 954;; (ding) signal instead below, if quitting
8e1cae6d
JB
955 (discard-input)
956 (if isearch-success
957 ;; If search is successful, move back to starting point
958 ;; and really do quit.
959 (progn (goto-char isearch-opoint)
509ed182 960 (setq isearch-success nil)
35a4d143 961 (isearch-done t) ; exit isearch
0352b205 962 (isearch-clean-overlays)
a5dcf63f 963 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
964 ;; If search is failing, or has an incomplete regexp,
965 ;; rub out until it is once more successful.
966 (while (or (not isearch-success) isearch-invalid-regexp)
967 (isearch-pop-state))
8e1cae6d
JB
968 (isearch-update)))
969
8e1cae6d
JB
970(defun isearch-repeat (direction)
971 ;; Utility for isearch-repeat-forward and -backward.
972 (if (eq isearch-forward (eq direction 'forward))
973 ;; C-s in forward or C-r in reverse.
974 (if (equal isearch-string "")
975 ;; If search string is empty, use last one.
976 (setq isearch-string
8e1cae6d 977 (or (if isearch-regexp
a5dcf63f
RS
978 (car regexp-search-ring)
979 (car search-ring))
135f293b 980 (error "No previous search string"))
8e1cae6d 981 isearch-message
08200510 982 (mapconcat 'isearch-text-char-description
67085aba
GM
983 isearch-string "")
984 isearch-case-fold-search isearch-last-case-fold-search)
8e1cae6d
JB
985 ;; If already have what to search for, repeat it.
986 (or isearch-success
02b2f510 987 (progn
8e1cae6d
JB
988 (goto-char (if isearch-forward (point-min) (point-max)))
989 (setq isearch-wrapped t))))
990 ;; C-s in reverse or C-r in forward, change direction.
991 (setq isearch-forward (not isearch-forward)))
992
993 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
994
995 (if (equal isearch-string "")
996 (setq isearch-success t)
99ae9e9f
KH
997 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
998 (not isearch-just-started))
8e1cae6d
JB
999 ;; If repeating a search that found
1000 ;; an empty string, ensure we advance.
6fefdc4b
RS
1001 (if (if isearch-forward (eobp) (bobp))
1002 ;; If there's nowhere to advance to, fail (and wrap next time).
1003 (progn
1004 (setq isearch-success nil)
1005 (ding))
1006 (forward-char (if isearch-forward 1 -1))
1007 (isearch-search))
1008 (isearch-search)))
1009
8e1cae6d
JB
1010 (isearch-push-state)
1011 (isearch-update))
1012
1013(defun isearch-repeat-forward ()
1014 "Repeat incremental search forwards."
1015 (interactive)
1016 (isearch-repeat 'forward))
1017
1018(defun isearch-repeat-backward ()
1019 "Repeat incremental search backwards."
1020 (interactive)
1021 (isearch-repeat 'backward))
1022
1023(defun isearch-toggle-regexp ()
1024 "Toggle regexp searching on or off."
1025 ;; The status stack is left unchanged.
1026 (interactive)
1027 (setq isearch-regexp (not isearch-regexp))
08200510 1028 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
1029 (isearch-update))
1030
4453091d
RS
1031(defun isearch-toggle-case-fold ()
1032 "Toggle case folding in searching on or off."
1033 (interactive)
1034 (setq isearch-case-fold-search
1035 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
1036 (let ((message-log-max nil))
1037 (message "%s%s [case %ssensitive]"
1038 (isearch-message-prefix nil nil isearch-nonincremental)
1039 isearch-message
1040 (if isearch-case-fold-search "in" "")))
4453091d
RS
1041 (setq isearch-adjusted t)
1042 (sit-for 1)
1043 (isearch-update))
1044
8e1cae6d 1045(defun isearch-delete-char ()
d6b8a1c0 1046 "Discard last input item and move point back.
8e1cae6d
JB
1047If no previous match was done, just beep."
1048 (interactive)
1049 (if (null (cdr isearch-cmds))
1050 (ding)
1051 (isearch-pop-state))
1052 (isearch-update))
1053
1054
9cf081fa
KH
1055(defun isearch-yank-string (string)
1056 "Pull STRING into search string."
1057 ;; Downcase the string if not supposed to case-fold yanked strings.
1058 (if (and isearch-case-fold-search
1059 (eq 'not-yanks search-upper-case))
1060 (setq string (downcase string)))
1061 (if isearch-regexp (setq string (regexp-quote string)))
1062 (setq isearch-string (concat isearch-string string)
1063 isearch-message
1064 (concat isearch-message
1065 (mapconcat 'isearch-text-char-description
1066 string ""))
1067 ;; Don't move cursor in reverse search.
1068 isearch-yank-flag t)
8e1cae6d
JB
1069 (isearch-search-and-update))
1070
30d47262
RS
1071(defun isearch-yank-kill ()
1072 "Pull string from kill ring into search string."
1073 (interactive)
9cf081fa
KH
1074 (isearch-yank-string (current-kill 0)))
1075
1076(defun isearch-yank-x-selection ()
117132a6 1077 "Pull current X selection into search string."
9cf081fa
KH
1078 (interactive)
1079 (isearch-yank-string (x-get-selection)))
8e1cae6d 1080
e4af1426 1081
191f025a 1082(defun isearch-mouse-2 (click)
e4af1426 1083 "Handle mouse-2 in Isearch mode.
117132a6 1084For a click in the echo area, invoke `isearch-yank-x-selection'.
e4af1426 1085Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
191f025a 1086 (interactive "e")
e4af1426
GM
1087 (let* ((w (posn-window (event-start click)))
1088 (overriding-terminal-local-map nil)
1089 (key (vector (event-basic-type click)))
191f025a
SM
1090 ;; FIXME: `key-binding' should accept an event as argument
1091 ;; and do all the overlay/text-properties lookup etc...
1092 (binding (with-current-buffer
1093 (if (window-live-p w) (window-buffer w) (current-buffer))
1094 (key-binding key))))
117132a6
DL
1095 (if (and (window-minibuffer-p w)
1096 (not (minibuffer-window-active-p w))) ; in echo area
1097 (isearch-yank-x-selection)
cd59ea72 1098 (when (functionp binding)
191f025a 1099 (call-interactively binding)))))
e4af1426 1100
117132a6 1101
29e53a0a
KF
1102(defun isearch-yank-internal (jumpform)
1103 "Pull the text from point to the point reached by JUMPFORM.
1104JUMPFORM is a lambda expression that takes no arguments and returns a
1105buffer position, possibly having moved point to that position. For
1106example, it might move point forward by a word and return point, or it
1107might return the position of the end of the line."
9cf081fa
KH
1108 (isearch-yank-string
1109 (save-excursion
1110 (and (not isearch-forward) isearch-other-end
1111 (goto-char isearch-other-end))
29e53a0a
KF
1112 (buffer-substring-no-properties (point) (funcall jumpform)))))
1113
1114(defun isearch-yank-char ()
1115 "Pull next letter from buffer into search string."
1116 (interactive)
1117 (isearch-yank-internal (lambda () (forward-char 1) (point))))
1118
868bf43a
RS
1119(defun isearch-yank-word-or-char ()
1120 "Pull next character or word from buffer into search string."
1121 (interactive)
21d90805 1122 (isearch-yank-internal
b48ca14f 1123 (lambda ()
21d90805
KF
1124 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1125 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1126 (forward-word 1)
1127 (forward-char 1)) (point))))
868bf43a 1128
29e53a0a
KF
1129(defun isearch-yank-word ()
1130 "Pull next word from buffer into search string."
1131 (interactive)
1132 (isearch-yank-internal (lambda () (forward-word 1) (point))))
8e1cae6d
JB
1133
1134(defun isearch-yank-line ()
1135 "Pull rest of line from buffer into search string."
1136 (interactive)
8fd4d684 1137 (isearch-yank-internal 'line-end-position))
8e1cae6d
JB
1138
1139
1140(defun isearch-search-and-update ()
1141 ;; Do the search and update the display.
191f025a
SM
1142 (when (or isearch-success
1143 ;; unsuccessful regexp search may become
1144 ;; successful by addition of characters which
1145 ;; make isearch-string valid
cd59ea72
SM
1146 isearch-regexp
1147 ;; If the string was found but was completely invisible,
1148 ;; it might now be partly visible, so try again.
1149 (prog1 isearch-hidden (setq isearch-hidden nil)))
8e1cae6d
JB
1150 ;; In reverse search, adding stuff at
1151 ;; the end may cause zero or many more chars to be
1152 ;; matched, in the string following point.
1153 ;; Allow all those possibilities without moving point as
1154 ;; long as the match does not extend past search origin.
1155 (if (and (not isearch-forward) (not isearch-adjusted)
1156 (condition-case ()
99ae9e9f 1157 (let ((case-fold-search isearch-case-fold-search))
ec0a2f45
KH
1158 (if (and (eq case-fold-search t) search-upper-case)
1159 (setq case-fold-search
1160 (isearch-no-upper-case-p isearch-string isearch-regexp)))
99ae9e9f
KH
1161 (looking-at (if isearch-regexp isearch-string
1162 (regexp-quote isearch-string))))
8e1cae6d 1163 (error nil))
bd6a8414 1164 (or isearch-yank-flag
191f025a 1165 (<= (match-end 0)
bd6a8414 1166 (min isearch-opoint isearch-barrier))))
d64b1d96 1167 (progn
191f025a 1168 (setq isearch-success t
d64b1d96
RS
1169 isearch-invalid-regexp nil
1170 isearch-within-brackets nil
1171 isearch-other-end (match-end 0))
1172 (if (and (eq isearch-case-fold-search t) search-upper-case)
1173 (setq isearch-case-fold-search
1174 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1175 ;; Not regexp, not reverse, or no match at point.
1176 (if (and isearch-other-end (not isearch-adjusted))
1177 (goto-char (if isearch-forward isearch-other-end
191f025a
SM
1178 (min isearch-opoint
1179 isearch-barrier
8e1cae6d
JB
1180 (1+ isearch-other-end)))))
1181 (isearch-search)
1182 ))
1183 (isearch-push-state)
1184 (if isearch-op-fun (funcall isearch-op-fun))
1185 (isearch-update))
1186
1187
1188;; *, ?, and | chars can make a regexp more liberal.
08200510 1189;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1190;; So go back to place last successful search started
1191;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1192;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1193
1194(defun isearch-*-char ()
1195 "Handle * and ? specially in regexps."
1196 (interactive)
191f025a 1197 (if isearch-regexp
bd6a8414
RS
1198 (let ((idx (length isearch-string)))
1199 (while (and (> idx 0)
1200 (eq (aref isearch-string (1- idx)) ?\\))
1201 (setq idx (1- idx)))
1202 (when (= (mod (- (length isearch-string) idx) 2) 0)
1203 (setq isearch-adjusted t)
1204 ;; Get the isearch-other-end from before the last search.
1205 ;; We want to start from there,
1206 ;; so that we don't retreat farther than that.
1207 ;; (car isearch-cmds) is after last search;
1208 ;; (car (cdr isearch-cmds)) is from before it.
1209 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1210 (setq cs (or cs isearch-barrier))
1211 (goto-char
1212 (if isearch-forward
1213 (max cs isearch-barrier)
1214 (min cs isearch-barrier)))))))
117132a6 1215 (isearch-process-search-char last-command-char))
b48ca14f 1216
8e1cae6d 1217
8e1cae6d
JB
1218(defun isearch-|-char ()
1219 "If in regexp search, jump to the barrier."
1220 (interactive)
1221 (if isearch-regexp
1222 (progn
1223 (setq isearch-adjusted t)
1224 (goto-char isearch-barrier)))
117132a6 1225 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1226
1227
b457cc3b 1228(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1229
1230(defun isearch-other-meta-char ()
8f90f594 1231 "Exit the search normally and reread this key sequence.
35a4d143
RS
1232But only if `search-exit-option' is non-nil, the default.
1233If it is the symbol `edit', the search string is edited in the minibuffer
1234and the meta character is unread so that it applies to editing the string."
8e1cae6d 1235 (interactive)
c6431f12
KH
1236 (let* ((key (this-command-keys))
1237 (main-event (aref key 0))
1238 (keylist (listify-key-sequence key)))
24b5caec 1239 (cond ((and (= (length key) 1)
d94eb6eb 1240 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1241 (not (or (null lookup) (integerp lookup)
1242 (keymapp lookup)))))
24b5caec
RS
1243 ;; Handle a function key that translates into something else.
1244 ;; If the key has a global definition too,
1245 ;; exit and unread the key itself, so its global definition runs.
1246 ;; Otherwise, unread the translation,
1247 ;; so that the translated key takes effect within isearch.
d94eb6eb 1248 (cancel-kbd-macro-events)
24b5caec 1249 (if (lookup-key global-map key)
02b2f510 1250 (progn
24b5caec
RS
1251 (isearch-done)
1252 (apply 'isearch-unread keylist))
a5cc922e
KH
1253 (setq keylist
1254 (listify-key-sequence (lookup-key function-key-map key)))
1255 (while keylist
1256 (setq key (car keylist))
1257 ;; If KEY is a printing char, we handle it here
1258 ;; directly to avoid the input method and keyboard
1259 ;; coding system translating it.
1260 (if (and (integerp key)
ed5c183b 1261 (>= key ?\ ) (/= key 127) (< key 256))
a5cc922e
KH
1262 (progn
1263 (isearch-process-search-char key)
1264 (setq keylist (cdr keylist)))
1265 ;; As the remaining keys in KEYLIST can't be handled
1266 ;; here, we must reread them.
1267 (apply 'isearch-unread keylist)
1268 (setq keylist nil)))))
24b5caec 1269 (
c6431f12
KH
1270 ;; Handle an undefined shifted control character
1271 ;; by downshifting it if that makes it defined.
1272 ;; (As read-key-sequence would normally do,
1273 ;; if we didn't have a default definition.)
1274 (let ((mods (event-modifiers main-event)))
1275 (and (integerp main-event)
1276 (memq 'shift mods)
1277 (memq 'control mods)
1278 (lookup-key isearch-mode-map
1279 (let ((copy (copy-sequence key)))
1280 (aset copy 0
1281 (- main-event (- ?\C-\S-a ?\C-a)))
1282 copy)
1283 nil)))
1284 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1285 (cancel-kbd-macro-events)
c6431f12
KH
1286 (apply 'isearch-unread keylist))
1287 ((eq search-exit-option 'edit)
1288 (apply 'isearch-unread keylist)
1289 (isearch-edit-string))
1290 (search-exit-option
1291 (let (window)
d94eb6eb 1292 (cancel-kbd-macro-events)
c6431f12 1293 (apply 'isearch-unread keylist)
3fb01f36
GM
1294
1295 ;; Properly handle scroll-bar and mode-line clicks for
1296 ;; which a dummy prefix event was generated as (aref key
1297 ;; 0). Note that we don't have to modify the event
1298 ;; anymore in 21 because read_key_sequence no longer modifies
1299 ;; events to produce fake prefix keys.
1300 (when (and (> (length key) 1)
1301 (symbolp (aref key 0))
1302 (listp (aref key 1))
191f025a 1303 (not (numberp (posn-point
3fb01f36 1304 (event-start (aref key 1))))))
17635d50
GM
1305 (pop unread-command-events)
1306 (setq main-event (car unread-command-events)))
3fb01f36 1307
346f18dc
GM
1308 ;; If we got a mouse click event, that event contains the
1309 ;; window clicked on. maybe it was read with the buffer
c6431f12
KH
1310 ;; it was clicked on. If so, that buffer, not the current one,
1311 ;; is in isearch mode. So end the search in that buffer.
346f18dc
GM
1312
1313 ;; ??? I have no idea what this if checks for, but it's
1314 ;; obviously wrong for the case that a down-mouse event
1315 ;; on another window invokes this function. The event
1316 ;; will contain the window clicked on and that window's
b48ca14f 1317 ;; buffer is certainly not always in Isearch mode.
346f18dc
GM
1318 ;;
1319 ;; Leave the code in, but check for current buffer not
1320 ;; being in Isearch mode for now, until someone tells
1321 ;; what it's really supposed to do.
1322 ;;
1323 ;; --gerd 2001-08-10.
1324
1325 (if (and (not isearch-mode)
1326 (listp main-event)
c6431f12 1327 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1328 (windowp window)
1329 (or (> (minibuffer-depth) 0)
1330 (not (window-minibuffer-p window))))
c6431f12
KH
1331 (save-excursion
1332 (set-buffer (window-buffer window))
0352b205
RS
1333 (isearch-done)
1334 (isearch-clean-overlays))
1335 (isearch-done)
1336 (isearch-clean-overlays))))
c6431f12
KH
1337 (t;; otherwise nil
1338 (isearch-process-search-string key key)))))
8e1cae6d 1339
8e1cae6d
JB
1340(defun isearch-quote-char ()
1341 "Quote special characters for incremental search."
1342 (interactive)
8bc15fa8 1343 (let ((char (read-quoted-char (isearch-message t))))
3c75023f 1344 ;; Assume character codes 0200 - 0377 stand for characters in some
c20d35d5
KH
1345 ;; single-byte character set, and convert them to Emacs
1346 ;; characters.
8bc15fa8 1347 (and enable-multibyte-characters
3c75023f 1348 (>= char ?\200)
8bc15fa8 1349 (<= char ?\377)
c20d35d5 1350 (setq char (unibyte-char-to-multibyte char)))
8bc15fa8 1351 (isearch-process-search-char char)))
8e1cae6d 1352
8e1cae6d
JB
1353(defun isearch-return-char ()
1354 "Convert return into newline for incremental search.
1355Obsolete."
1356 (interactive)
1357 (isearch-process-search-char ?\n))
1358
8e1cae6d 1359(defun isearch-printing-char ()
b68c164d 1360 "Add this ordinary printing character to the search string and search."
8e1cae6d 1361 (interactive)
117132a6 1362 (let ((char last-command-char))
45b94eb2
KH
1363 (if (= char ?\S-\ )
1364 (setq char ?\ ))
af56433d
RS
1365 (if (and enable-multibyte-characters
1366 (>= char ?\200)
1367 (<= char ?\377))
3c487103
KH
1368 (if (keyboard-coding-system)
1369 (isearch-process-search-multibyte-characters char)
1370 (isearch-process-search-char (unibyte-char-to-multibyte char)))
af56433d
RS
1371 (if current-input-method
1372 (isearch-process-search-multibyte-characters char)
1373 (isearch-process-search-char char)))))
8e1cae6d
JB
1374
1375(defun isearch-whitespace-chars ()
08200510 1376 "Match all whitespace chars, if in regexp mode.
fad241d3 1377If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
8e1cae6d 1378 (interactive)
b48ca14f 1379 (if isearch-regexp
9da6682f
RS
1380 (if (and search-whitespace-regexp (not isearch-within-brackets)
1381 (not isearch-invalid-regexp))
08200510
RS
1382 (isearch-process-search-string search-whitespace-regexp " ")
1383 (isearch-printing-char))
1384 (progn
eb8c3be9 1385 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1386 ;; (setq isearch-word t)
1387 ;; (setq isearch-adjusted t)
1388 ;; (goto-char isearch-barrier)
1389 (isearch-printing-char))))
8e1cae6d
JB
1390
1391(defun isearch-process-search-char (char)
1392 ;; Append the char to the search string, update the message and re-search.
191f025a
SM
1393 (isearch-process-search-string
1394 (char-to-string char)
8f3c1d76 1395 (if (>= char ?\200)
01a6ef79
RS
1396 (char-to-string char)
1397 (isearch-text-char-description char))))
8e1cae6d
JB
1398
1399(defun isearch-process-search-string (string message)
1400 (setq isearch-string (concat isearch-string string)
1401 isearch-message (concat isearch-message message))
1402 (isearch-search-and-update))
1403
1404\f
8e1cae6d
JB
1405;; Search Ring
1406
08200510
RS
1407(defun isearch-ring-adjust1 (advance)
1408 ;; Helper for isearch-ring-adjust
1409 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1410 (length (length ring))
1411 (yank-pointer-name (if isearch-regexp
08200510 1412 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1413 'search-ring-yank-pointer))
1414 (yank-pointer (eval yank-pointer-name)))
1415 (if (zerop length)
1416 ()
1417 (set yank-pointer-name
1418 (setq yank-pointer
ffbc30b2
PE
1419 (mod (+ (or yank-pointer 0)
1420 (if advance -1 1))
1421 length)))
a5dcf63f 1422 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1423 isearch-message (mapconcat 'isearch-text-char-description
1424 isearch-string "")))))
1425
1426(defun isearch-ring-adjust (advance)
1427 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1428 (isearch-ring-adjust1 advance)
08200510
RS
1429 (if search-ring-update
1430 (progn
1431 (isearch-search)
1432 (isearch-update))
1433 (isearch-edit-string)
2ff20b7e
RS
1434 )
1435 (isearch-push-state))
8e1cae6d
JB
1436
1437(defun isearch-ring-advance ()
1438 "Advance to the next search string in the ring."
08200510 1439 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1440 (interactive)
1441 (isearch-ring-adjust 'advance))
1442
1443(defun isearch-ring-retreat ()
1444 "Retreat to the previous search string in the ring."
1445 (interactive)
1446 (isearch-ring-adjust nil))
1447
a5dcf63f 1448(defun isearch-ring-advance-edit (n)
117132a6
DL
1449 "Insert the next element of the search history into the minibuffer.
1450With prefix arg N, insert the Nth element."
a5dcf63f
RS
1451 (interactive "p")
1452 (let* ((yank-pointer-name (if isearch-regexp
1453 'regexp-search-ring-yank-pointer
1454 'search-ring-yank-pointer))
1455 (yank-pointer (eval yank-pointer-name))
1456 (ring (if isearch-regexp regexp-search-ring search-ring))
1457 (length (length ring)))
1458 (if (zerop length)
1459 ()
1460 (set yank-pointer-name
1461 (setq yank-pointer
ffbc30b2
PE
1462 (mod (- (or yank-pointer 0) n)
1463 length)))
a5dcf63f 1464
b7b66466 1465 (delete-field)
35a4d143 1466 (insert (nth yank-pointer ring))
49c13105 1467 (goto-char (point-max)))))
a5dcf63f
RS
1468
1469(defun isearch-ring-retreat-edit (n)
117132a6
DL
1470 "Insert the previous element of the search history into the minibuffer.
1471With prefix arg N, insert the Nth element."
a5dcf63f
RS
1472 (interactive "p")
1473 (isearch-ring-advance-edit (- n)))
1474
1475;;(defun isearch-ring-adjust-edit (advance)
1476;; "Use the next or previous search string in the ring while in minibuffer."
1477;; (isearch-ring-adjust1 advance)
1478;; (erase-buffer)
1479;; (insert isearch-string))
1480
1481;;(defun isearch-ring-advance-edit ()
1482;; (interactive)
1483;; (isearch-ring-adjust-edit 'advance))
1484
1485;;(defun isearch-ring-retreat-edit ()
1486;; "Retreat to the previous search string in the ring while in the minibuffer."
1487;; (interactive)
1488;; (isearch-ring-adjust-edit nil))
08200510
RS
1489
1490
1491(defun isearch-complete1 ()
1492 ;; Helper for isearch-complete and isearch-complete-edit
1493 ;; Return t if completion OK, nil if no completion exists.
1494 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
08200510 1495 (completion-ignore-case case-fold-search)
c789e608 1496 (completion (try-completion isearch-string ring)))
08200510
RS
1497 (cond
1498 ((eq completion t)
1499 ;; isearch-string stays the same
1500 t)
1501 ((or completion ; not nil, must be a string
b7852303 1502 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1503 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1504 (progn
1505 (if completion-auto-help
1506 (with-output-to-temp-buffer "*Isearch completions*"
b48ca14f 1507 (display-completion-list
c789e608 1508 (all-completions isearch-string ring))))
36bcac3f
RS
1509 t)
1510 (and completion
1511 (setq isearch-string completion))))
08200510
RS
1512 (t
1513 (message "No completion") ; waits a second if in minibuffer
1514 nil))))
1515
1516(defun isearch-complete ()
1517 "Complete the search string from the strings on the search ring.
1518The completed string is then editable in the minibuffer.
1519If there is no completion possible, say so and continue searching."
1520 (interactive)
1521 (if (isearch-complete1)
1522 (isearch-edit-string)
1523 ;; else
1524 (sit-for 1)
1525 (isearch-update)))
8e1cae6d 1526
08200510
RS
1527(defun isearch-complete-edit ()
1528 "Same as `isearch-complete' except in the minibuffer."
1529 (interactive)
c789e608 1530 (setq isearch-string (field-string))
08200510 1531 (if (isearch-complete1)
8e1cae6d 1532 (progn
b7b66466 1533 (delete-field)
08200510 1534 (insert isearch-string))))
8e1cae6d
JB
1535
1536\f
8e1cae6d 1537;; The search status stack (and isearch window-local variables, not used).
08200510 1538;; Need a structure for this.
8e1cae6d
JB
1539
1540(defun isearch-top-state ()
8e1cae6d
JB
1541 (let ((cmd (car isearch-cmds)))
1542 (setq isearch-string (car cmd)
1543 isearch-message (car (cdr cmd))
1544 isearch-success (nth 3 cmd)
1545 isearch-forward (nth 4 cmd)
1546 isearch-other-end (nth 5 cmd)
08200510
RS
1547 isearch-word (nth 6 cmd)
1548 isearch-invalid-regexp (nth 7 cmd)
1549 isearch-wrapped (nth 8 cmd)
c5f6b356 1550 isearch-barrier (nth 9 cmd)
f551b97d
RS
1551 isearch-within-brackets (nth 10 cmd)
1552 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1553 (goto-char (car (cdr (cdr cmd))))))
1554
1555(defun isearch-pop-state ()
8e1cae6d 1556 (setq isearch-cmds (cdr isearch-cmds))
1f4139d5 1557 (isearch-top-state))
8e1cae6d
JB
1558
1559(defun isearch-push-state ()
b48ca14f 1560 (setq isearch-cmds
8e1cae6d 1561 (cons (list isearch-string isearch-message (point)
b48ca14f 1562 isearch-success isearch-forward isearch-other-end
08200510 1563 isearch-word
c5f6b356 1564 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1565 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1566 isearch-cmds)))
1567
8e1cae6d 1568\f
8e1cae6d
JB
1569;; Message string
1570
1571(defun isearch-message (&optional c-q-hack ellipsis)
1572 ;; Generate and print the message string.
1573 (let ((cursor-in-echo-area ellipsis)
1574 (m (concat
08200510 1575 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1576 isearch-message
1577 (isearch-message-suffix c-q-hack ellipsis)
1578 )))
a56687f1
KH
1579 (if c-q-hack
1580 m
1581 (let ((message-log-max nil))
1582 (message "%s" m)))))
8e1cae6d 1583
08200510 1584(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1585 ;; If about to search, and previous search regexp was invalid,
1586 ;; check that it still is. If it is valid now,
1587 ;; let the message we display while searching say that it is valid.
1588 (and isearch-invalid-regexp ellipsis
1589 (condition-case ()
1590 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1591 (setq isearch-invalid-regexp nil
1592 isearch-within-brackets nil))
8e1cae6d
JB
1593 (error nil)))
1594 ;; If currently failing, display no ellipsis.
1595 (or isearch-success (setq ellipsis nil))
1596 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1597 (if (and isearch-wrapped
1598 (if isearch-forward
1599 (> (point) isearch-opoint)
1600 (< (point) isearch-opoint)))
1601 "over")
8e1cae6d 1602 (if isearch-wrapped "wrapped ")
08200510 1603 (if isearch-word "word " "")
8e1cae6d 1604 (if isearch-regexp "regexp " "")
08200510 1605 (if nonincremental "search" "I-search")
f46d5091 1606 (if isearch-forward "" " backward")
5b56d4e4
KH
1607 (if current-input-method
1608 (concat " [" current-input-method-title "]: ")
f46d5091 1609 ": ")
8e1cae6d 1610 )))
360e0dd5
RS
1611 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
1612 'face 'minibuffer-prompt)))
8e1cae6d
JB
1613
1614(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1615 (concat (if c-q-hack "^Q" "")
1616 (if isearch-invalid-regexp
1617 (concat " [" isearch-invalid-regexp "]")
1618 "")))
1619
1620\f
191f025a
SM
1621;; Searching
1622
1623(defvar isearch-search-fun-function nil "Override `isearch-function-fun'.")
1624
1625(defun isearch-search-fun ()
1626 "Return the function to use for the search.
1627Can be changed via `isearch-search-fun-function' for special needs."
1628 (if isearch-search-fun-function
1629 (funcall isearch-search-fun-function)
1630 (cond
1631 (isearch-word
1632 (if isearch-forward 'word-search-forward 'word-search-backward))
1633 (isearch-regexp
1634 (if isearch-forward 're-search-forward 're-search-backward))
1635 (t
1636 (if isearch-forward 'search-forward 'search-backward)))))
8e1cae6d
JB
1637
1638(defun isearch-search ()
1639 ;; Do the search with the current search string.
1640 (isearch-message nil t)
4453091d 1641 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1642 (setq isearch-case-fold-search
1643 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1644 (condition-case lossage
79c7a4fa
RS
1645 (let ((inhibit-point-motion-hooks search-invisible)
1646 (inhibit-quit nil)
86bfaffe
RS
1647 (case-fold-search isearch-case-fold-search)
1648 (retry t))
8e1cae6d 1649 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1650 (setq isearch-within-brackets nil)
86bfaffe
RS
1651 (while retry
1652 (setq isearch-success
1653 (funcall
191f025a 1654 (isearch-search-fun)
86bfaffe
RS
1655 isearch-string nil t))
1656 ;; Clear RETRY unless we matched some invisible text
1657 ;; and we aren't supposed to do that.
0352b205 1658 (if (or (eq search-invisible t)
86bfaffe
RS
1659 (not isearch-success)
1660 (bobp) (eobp)
1661 (= (match-beginning 0) (match-end 0))
1662 (not (isearch-range-invisible
1663 (match-beginning 0) (match-end 0))))
1664 (setq retry nil)))
99ae9e9f 1665 (setq isearch-just-started nil)
8e1cae6d
JB
1666 (if isearch-success
1667 (setq isearch-other-end
1668 (if isearch-forward (match-beginning 0) (match-end 0)))))
1669
d32696d4 1670 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1671 (setq isearch-success nil))
1672
191f025a 1673 (invalid-regexp
8e1cae6d 1674 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1675 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1676 isearch-invalid-regexp))
8e1cae6d
JB
1677 (if (string-match
1678 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1679 isearch-invalid-regexp)
f1c03125
RS
1680 (setq isearch-invalid-regexp "incomplete input")))
1681 (error
c982ab21 1682 ;; stack overflow in regexp search.
c5348233 1683 (setq isearch-invalid-regexp (format "%s" lossage))))
8e1cae6d
JB
1684
1685 (if isearch-success
1686 nil
1687 ;; Ding if failed this time after succeeding last time.
1688 (and (nth 3 (car isearch-cmds))
1689 (ding))
1690 (goto-char (nth 2 (car isearch-cmds)))))
1691
0352b205 1692
191f025a 1693;; Called when opening an overlay, and we are still in isearch.
0352b205 1694(defun isearch-open-overlay-temporary (ov)
b48ca14f 1695 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
0352b205
RS
1696 ;; Some modes would want to open the overlays temporary during
1697 ;; isearch in their own way, they should set the
1698 ;; `isearch-open-invisible-temporary' to a function doing this.
1699 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1700 ;; Store the values for the `invisible' and `intangible'
1701 ;; properties, and then set them to nil. This way the text hidden
1702 ;; by this overlay becomes visible.
1703
380866a2 1704 ;; Do we really need to set the `intangible' property to t? Can we
0352b205
RS
1705 ;; have the point inside an overlay with an `intangible' property?
1706 ;; In 19.34 this does not exist so I cannot test it.
1707 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1708 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1709 (overlay-put ov 'invisible nil)
1710 (overlay-put ov 'intangible nil)))
1711
1712
191f025a
SM
1713;; This is called at the end of isearch. It will open the overlays
1714;; that contain the latest match. Obviously in case of a C-g the
1715;; point returns to the original location which surely is not contain
1716;; in any of these overlays, se we are safe in this case too.
0352b205 1717(defun isearch-open-necessary-overlays (ov)
191f025a 1718 (let ((inside-overlay (and (> (point) (overlay-start ov))
0352b205
RS
1719 (< (point) (overlay-end ov))))
1720 ;; If this exists it means that the overlay was opened using
1721 ;; this function, not by us tweaking the overlay properties.
1722 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1723 (when (or inside-overlay (not fct-temp))
1724 ;; restore the values for the `invisible' and `intangible'
1725 ;; properties
1726 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1727 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1728 (overlay-put ov 'isearch-invisible nil)
1729 (overlay-put ov 'isearch-intangible nil))
1730 (if inside-overlay
1731 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1732 (if fct-temp
1733 (funcall fct-temp ov t)))))
1734
191f025a
SM
1735;; This is called when exiting isearch. It closes the temporary
1736;; opened overlays, except the ones that contain the latest match.
0352b205
RS
1737(defun isearch-clean-overlays ()
1738 (when isearch-opened-overlays
02b2f510 1739 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
0352b205
RS
1740 (setq isearch-opened-overlays nil)))
1741
fc0eccfc
GM
1742
1743(defun isearch-intersects-p (start0 end0 start1 end1)
1744 "Return t if regions START0..END0 and START1..END1 intersect."
eaa493df
GM
1745 (or (and (>= start0 start1) (< start0 end1))
1746 (and (> end0 start1) (<= end0 end1))
1747 (and (>= start1 start0) (< start1 end0))
1748 (and (> end1 start0) (<= end1 end0))))
fc0eccfc
GM
1749
1750
191f025a
SM
1751;; Verify if the current match is outside of each element of
1752;; `isearch-opened-overlays', if so close that overlay.
fc0eccfc
GM
1753
1754(defun isearch-close-unnecessary-overlays (begin end)
1755 (let ((overlays isearch-opened-overlays))
0352b205 1756 (setq isearch-opened-overlays nil)
fc0eccfc
GM
1757 (dolist (ov overlays)
1758 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
1759 (push ov isearch-opened-overlays)
1760 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1761 (if fct-temp
1762 ;; If this exists it means that the overlay was opened
1763 ;; using this function, not by us tweaking the overlay
1764 ;; properties.
1765 (funcall fct-temp ov t)
1766 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1767 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1768 (overlay-put ov 'isearch-invisible nil)
1769 (overlay-put ov 'isearch-intangible nil)))))))
1770
0352b205 1771
86bfaffe 1772(defun isearch-range-invisible (beg end)
79c7a4fa 1773 "Return t if all the text from BEG to END is invisible."
cd59ea72
SM
1774 (when (/= beg end)
1775 ;; Check that invisibility runs up to END.
1776 (save-excursion
1777 (goto-char beg)
1778 (let (;; can-be-opened keeps track if we can open some overlays.
1779 (can-be-opened (eq search-invisible 'open))
1780 ;; the list of overlays that could be opened
1781 (crt-overlays nil))
1782 (when (and can-be-opened isearch-hide-immediately)
1783 (isearch-close-unnecessary-overlays beg end))
1784 ;; If the following character is currently invisible,
1785 ;; skip all characters with that same `invisible' property value.
1786 ;; Do that over and over.
1787 (while (and (< (point) end)
1788 (let ((prop
1789 (get-char-property (point) 'invisible)))
1790 (if (eq buffer-invisibility-spec t)
1791 prop
1792 (or (memq prop buffer-invisibility-spec)
1793 (assq prop buffer-invisibility-spec)))))
1794 (if (get-text-property (point) 'invisible)
1795 (progn
1796 (goto-char (next-single-property-change (point) 'invisible
1797 nil end))
1798 ;; if text is hidden by an `invisible' text property
1799 ;; we cannot open it at all.
1800 (setq can-be-opened nil))
1801 (when can-be-opened
1802 (let ((overlays (overlays-at (point)))
1803 ov-list
1804 o
1805 invis-prop)
1806 (while overlays
1807 (setq o (car overlays)
1808 invis-prop (overlay-get o 'invisible))
1809 (if (if (eq buffer-invisibility-spec t)
1810 invis-prop
1811 (or (memq invis-prop buffer-invisibility-spec)
1812 (assq invis-prop buffer-invisibility-spec)))
1813 (if (overlay-get o 'isearch-open-invisible)
1814 (setq ov-list (cons o ov-list))
1815 ;; We found one overlay that cannot be
1816 ;; opened, that means the whole chunk
1817 ;; cannot be opened.
1818 (setq can-be-opened nil)))
1819 (setq overlays (cdr overlays)))
1820 (if can-be-opened
1821 ;; It makes sense to append to the open
1822 ;; overlays list only if we know that this is
1823 ;; t.
1824 (setq crt-overlays (append ov-list crt-overlays)))))
1825 (goto-char (next-overlay-change (point)))))
1826 ;; See if invisibility reaches up thru END.
1827 (if (>= (point) end)
1828 (if (and can-be-opened (consp crt-overlays))
1829 (progn
1830 (setq isearch-opened-overlays
1831 (append isearch-opened-overlays crt-overlays))
1832 (mapc 'isearch-open-overlay-temporary crt-overlays)
1833 nil)
1834 (setq isearch-hidden t)))))))
08200510
RS
1835
1836\f
191f025a 1837;; Highlighting
08200510 1838
b78559b0 1839(defvar isearch-overlay nil)
08200510 1840
b78559b0 1841(defun isearch-highlight (beg end)
8005142f 1842 (unless (null search-highlight)
4fb2ad98
MB
1843 (cond (isearch-overlay
1844 ;; Overlay already exists, just move it.
4fb2ad98
MB
1845 (move-overlay isearch-overlay beg end (current-buffer)))
1846
1847 (t
1848 ;; Overlay doesn't exist, create it.
1849 (setq isearch-overlay (make-overlay beg end))
c982ab21
GM
1850 (overlay-put isearch-overlay 'face isearch)
1851 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
1852 ))))
b78559b0
RS
1853
1854(defun isearch-dehighlight (totally)
4fb2ad98 1855 (when isearch-overlay
4fb2ad98
MB
1856 (delete-overlay isearch-overlay)))
1857
08200510 1858
191f025a 1859;; General utilities
08200510 1860
08200510 1861
b78559b0
RS
1862(defun isearch-no-upper-case-p (string regexp-flag)
1863 "Return t if there are no upper case chars in STRING.
b7852303 1864If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1865since they have special meaning in a regexp."
b48ca14f 1866 (let (quote-flag (i 0) (len (length string)) found)
59057198
RS
1867 (while (and (not found) (< i len))
1868 (let ((char (aref string i)))
1869 (if (and regexp-flag (eq char ?\\))
1870 (setq quote-flag (not quote-flag))
1871 (if (and (not quote-flag) (not (eq char (downcase char))))
f4be3f89
RS
1872 (setq found t))
1873 (setq quote-flag nil)))
59057198
RS
1874 (setq i (1+ i)))
1875 (not found)))
08200510 1876
b78559b0 1877;; Portability functions to support various Emacs versions.
8e1cae6d 1878
08200510 1879(defun isearch-text-char-description (c)
14373677
SM
1880 (cond
1881 ((< c ?\ ) (format "^%c" (+ c 64)))
1882 ((= c ?\^?) "^?")
1883 (t (char-to-string c))))
08200510 1884
bd1bd125 1885;; General function to unread characters or events.
99ae9e9f 1886;; Also insert them in a keyboard macro being defined.
8f90f594 1887(defun isearch-unread (&rest char-or-events)
02b2f510 1888 (mapc 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1889 (setq unread-command-events
1890 (append char-or-events unread-command-events)))
08200510 1891
44336afb 1892\f
191f025a
SM
1893;; isearch-lazy-highlight feature
1894;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1895
1896;; When active, *every* match for the current search string is
1897;; highlighted: the current one using the normal isearch match color
1898;; and all the others using `isearch-lazy-highlight-face'. The extra
1899;; highlighting makes it easier to anticipate where the cursor will
1900;; land each time you press C-s or C-r to repeat a pending search.
1901;; Highlighting of these additional matches happens in a deferred
1902;; fashion using "idle timers," so the cycles needed do not rob
1903;; isearch of its usual snappy response.
1904
1905;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1906;; Specifically:
1907;; - `isearch-update' is expected to be called (at least) every time
1908;; the search string or window-start changes;
1909;; - `isearch-string' is expected to contain the current search
1910;; string as entered by the user;
1911;; - the type of the current search is expected to be given by
1912;; `isearch-word' and `isearch-regexp';
1913;; - the direction of the current search is expected to be given by
1914;; `isearch-forward';
1915;; - the variable `isearch-invalid-regexp' is expected to be true
1916;; iff `isearch-string' is an invalid regexp.
44336afb 1917
44336afb
GM
1918(defgroup isearch-lazy-highlight nil
1919 "Lazy highlighting feature for incremental search."
1920 :prefix "isearch-lazy-highlight-"
39f2174e 1921 :version "21.1"
44336afb
GM
1922 :group 'isearch)
1923
1924(defcustom isearch-lazy-highlight t
1925 "*Controls the lazy-highlighting during incremental searches.
1926When non-nil, all text in the buffer matching the current search
1927string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1928and `isearch-lazy-highlight-interval')."
1929 :type 'boolean
1930 :group 'isearch-lazy-highlight)
1931
1932(defcustom isearch-lazy-highlight-cleanup t
1933 "*Controls whether to remove extra highlighting after a search.
1934If this is nil, extra highlighting can be \"manually\" removed with
1935\\[isearch-lazy-highlight-cleanup]."
1936 :type 'boolean
1937 :group 'isearch-lazy-highlight)
1938
871e0439 1939(defcustom isearch-lazy-highlight-initial-delay 0.25
44336afb
GM
1940 "*Seconds to wait before beginning to lazily highlight all matches."
1941 :type 'number
1942 :group 'isearch-lazy-highlight)
1943
b8fbd474 1944(defcustom isearch-lazy-highlight-interval 0 ; 0.0625
44336afb
GM
1945 "*Seconds between lazily highlighting successive matches."
1946 :type 'number
1947 :group 'isearch-lazy-highlight)
1948
c982ab21
GM
1949(defcustom isearch-lazy-highlight-max-at-a-time 20
1950 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
1951Larger values may reduce isearch's responsiveness to user input;
1952smaller values make matches highlight slowly.
1953A value of nil means highlight all matches."
68ebbb95
GM
1954 :type '(choice (const :tag "All" nil)
1955 (integer :tag "Some"))
1956 :group 'isearch-lazy-highlight)
1957
18ca20aa
GM
1958(defgroup isearch-faces nil
1959 "Lazy highlighting feature for incremental search."
1960 :version "21.1"
1961 :group 'isearch)
1962
1963(defface isearch
29910493
EZ
1964 '((((type tty pc) (class color))
1965 (:background "magenta4" :foreground "cyan1"))
1966 (((class color) (background light))
b4d7a2ec
RS
1967 ;; The background must not be too dark, for that means
1968 ;; the character is hard to see when the cursor is there.
5bcf0054 1969 (:background "magenta2" :foreground "lightskyblue1"))
29910493
EZ
1970 (((class color) (background dark))
1971 (:background "palevioletred2" :foreground "brown4"))
1972 (t (:inverse-video t)))
1973 "Face for highlighting Isearch matches."
18ca20aa
GM
1974 :group 'isearch-faces)
1975(defvar isearch 'isearch)
1976
1977(defface isearch-lazy-highlight-face
29910493
EZ
1978 '((((type tty pc) (class color))
1979 (:background "turquoise3"))
1980 (((class color) (background light))
1981 (:background "paleturquoise"))
1982 (((class color) (background dark))
1983 (:background "paleturquoise4"))
1984 (t (:underline t)))
1985 "Face for lazy highlighting of Isearch matches other than the current one."
18ca20aa
GM
1986 :group 'isearch-faces)
1987(defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
44336afb
GM
1988
1989(defvar isearch-lazy-highlight-overlays nil)
c982ab21 1990(defvar isearch-lazy-highlight-wrapped nil)
44336afb
GM
1991(defvar isearch-lazy-highlight-start nil)
1992(defvar isearch-lazy-highlight-end nil)
1993(defvar isearch-lazy-highlight-timer nil)
1994(defvar isearch-lazy-highlight-last-string nil)
c982ab21
GM
1995(defvar isearch-lazy-highlight-window nil)
1996(defvar isearch-lazy-highlight-window-start nil)
46daf6c7
GM
1997(defvar isearch-lazy-highlight-case-fold-search nil)
1998(defvar isearch-lazy-highlight-regexp nil)
44336afb 1999
c982ab21
GM
2000(defun isearch-lazy-highlight-cleanup (&optional force)
2001 "Stop lazy highlighting and remove extra highlighting from current buffer.
2002FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
2003is nil. This function is called when exiting an incremental search if
2004`isearch-lazy-highlight-cleanup' is non-nil."
44336afb 2005 (interactive '(t))
c982ab21
GM
2006 (if (or force isearch-lazy-highlight-cleanup)
2007 (while isearch-lazy-highlight-overlays
2008 (delete-overlay (car isearch-lazy-highlight-overlays))
2009 (setq isearch-lazy-highlight-overlays
2010 (cdr isearch-lazy-highlight-overlays))))
2011 (when isearch-lazy-highlight-timer
2012 (cancel-timer isearch-lazy-highlight-timer)
2013 (setq isearch-lazy-highlight-timer nil)))
44336afb
GM
2014
2015(defun isearch-lazy-highlight-new-loop ()
c982ab21 2016 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
44336afb 2017This happens when `isearch-update' is invoked (which can cause the
c982ab21 2018search string to change or the window to scroll)."
67ecad4e 2019 (when (and isearch-lazy-highlight
4c01d4bd 2020 (null executing-kbd-macro)
c982ab21
GM
2021 (sit-for 0) ;make sure (window-start) is credible
2022 (or (not (equal isearch-string
2023 isearch-lazy-highlight-last-string))
2024 (not (eq (selected-window)
2025 isearch-lazy-highlight-window))
46daf6c7
GM
2026 (not (eq isearch-lazy-highlight-case-fold-search
2027 isearch-case-fold-search))
2028 (not (eq isearch-lazy-highlight-regexp
2029 isearch-regexp))
c982ab21
GM
2030 (not (= (window-start)
2031 isearch-lazy-highlight-window-start))))
2032 ;; something important did indeed change
2033 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
2034 (when (not isearch-invalid-regexp)
2035 (setq isearch-lazy-highlight-window (selected-window)
2036 isearch-lazy-highlight-window-start (window-start)
2037 isearch-lazy-highlight-start (point)
2038 isearch-lazy-highlight-end (point)
2039 isearch-lazy-highlight-last-string isearch-string
46daf6c7
GM
2040 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2041 isearch-lazy-highlight-regexp isearch-regexp
c982ab21
GM
2042 isearch-lazy-highlight-wrapped nil)
2043 (setq isearch-lazy-highlight-timer
2044 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
2045 'isearch-lazy-highlight-update)))))
2046
2047(defun isearch-lazy-highlight-search ()
2048 "Search ahead for the next or previous match, for lazy highlighting.
2049Attempt to do the search exactly the way the pending isearch would."
191f025a
SM
2050 (let ((case-fold-search isearch-case-fold-search))
2051 (funcall (isearch-search-fun)
c982ab21
GM
2052 isearch-string
2053 (if isearch-forward
2054 (if isearch-lazy-highlight-wrapped
2055 isearch-lazy-highlight-start
2056 (window-end))
2057 (if isearch-lazy-highlight-wrapped
2058 isearch-lazy-highlight-end
2059 (window-start)))
2060 t)))
44336afb
GM
2061
2062(defun isearch-lazy-highlight-update ()
c982ab21
GM
2063 "Update highlighting of other matches for current search."
2064 (let ((max isearch-lazy-highlight-max-at-a-time)
2065 (looping t)
2066 nomore)
7742ee19
RS
2067 (save-excursion
2068 (save-match-data
c982ab21
GM
2069 (goto-char (if isearch-forward
2070 isearch-lazy-highlight-end
2071 isearch-lazy-highlight-start))
2072 (while looping
2073 (let ((found (isearch-lazy-highlight-search)))
2074 (when max
2075 (setq max (1- max))
2076 (if (<= max 0)
2077 (setq looping nil)))
2078 (if found
2079 (let ((mb (match-beginning 0))
2080 (me (match-end 0)))
2081 (if (= mb me) ;zero-length match
2082 (forward-char 1)
2083
2084 ;; non-zero-length match
2085 (let ((ov (make-overlay mb me)))
2086 (overlay-put ov 'face isearch-lazy-highlight-face)
2087 (overlay-put ov 'priority 0) ;lower than main overlay
2088 (overlay-put ov 'window (selected-window))
2089 (push ov isearch-lazy-highlight-overlays)))
2090 (if isearch-forward
2091 (setq isearch-lazy-highlight-end (point))
2092 (setq isearch-lazy-highlight-start (point))))
2093
2094 ;; not found
2095 (if isearch-lazy-highlight-wrapped
2096 (setq looping nil
2097 nomore t)
2098 (setq isearch-lazy-highlight-wrapped t)
2099 (if isearch-forward
2100 (progn
2101 (setq isearch-lazy-highlight-end (window-start))
2102 (goto-char (window-start)))
2103 (setq isearch-lazy-highlight-start (window-end))
2104 (goto-char (window-end)))))))
2105 (unless nomore
2106 (setq isearch-lazy-highlight-timer
2107 (run-at-time isearch-lazy-highlight-interval nil
2108 'isearch-lazy-highlight-update)))))))
44336afb 2109
c40a4de1
GM
2110(defun isearch-resume (search regexp word forward message case-fold)
2111 "Resume an incremental search.
2112SEARCH is the string or regexp searched for.
2113REGEXP non-nil means the resumed search was a regexp search.
2114WORD non-nil means resume a word search.
2115FORWARD non-nil means resume a forward search.
2116MESSAGE is the echo-area message recorded for the search resumed.
2117CASE-FOLD non-nil means the search was case-insensitive."
2118 (isearch-mode forward regexp nil nil word)
2119 (setq isearch-string search
2120 isearch-message message
2121 isearch-case-fold-search case-fold)
2122 (isearch-search))
191f025a 2123
3b1e4dd1 2124;;; isearch.el ends here