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