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