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