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