automatically generated from GPLed version
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
76550a57 1;;; isearch.el --- incremental search minor mode.
3b1e4dd1 2
9c1db929 3;; Copyright (C) 1992, 93, 94, 95, 96, 1997 Free Software Foundation, Inc.
3a801d0c 4
3b1e4dd1 5;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
0f09b616 6;; Maintainer: FSF
8e1cae6d 7
54d2ecd3 8;; This file is part of GNU Emacs.
8e1cae6d 9
59243403
RS
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
8e1cae6d 15;; GNU Emacs is distributed in the hope that it will be useful,
59243403
RS
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
b578f267
EN
21;; along with GNU Emacs; see the file COPYING. If not, write to the
22;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23;; Boston, MA 02111-1307, USA.
8e1cae6d 24
3b1e4dd1
ER
25;;; Commentary:
26
8e1cae6d
JB
27;; Instructions
28
08200510
RS
29;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
30;; isearch-mode behaves modally and does not return until the search
31;; is completed. It uses a recursive-edit to behave this way. Note:
32;; gnus does it wrong: (call-interactively 'isearch-forward).
33
8e1cae6d 34;; The key bindings active within isearch-mode are defined below in
08200510
RS
35;; `isearch-mode-map' which is given bindings close to the default
36;; characters of the original isearch.el. With `isearch-mode',
37;; however, you can bind multi-character keys and it should be easier
38;; to add new commands. One bug though: keys with meta-prefix cannot
39;; be longer than two chars. Also see minibuffer-local-isearch-map
40;; for bindings active during `isearch-edit-string'.
41
42;; Note to emacs version 19 users: isearch-mode should work even if
43;; you switch windows with the mouse, in which case isearch-mode is
44;; terminated automatically before the switch. This is true of lemacs
45;; too, with a few more cleanups I've neglected in this release.
46;; No one has supplied patches for epoch yet.
47
48;; The search ring and completion commands automatically put you in
49;; the minibuffer to edit the string. This gives you a chance to
50;; modify the search string before executing the search. There are
51;; three commands to terminate the editing: C-s and C-r exit the
52;; minibuffer and search forward and reverse respectively, while C-m
53;; exits and does a nonincremental search.
54
55;; Exiting immediately from isearch uses isearch-edit-string instead
56;; of nonincremental-search, if search-nonincremental-instead is non-nil.
57;; The name of this option should probably be changed if we decide to
58;; keep the behavior. No point in forcing nonincremental search until
59;; the last possible moment.
60
61;; TODO
eb8c3be9 62;; - Integrate the emacs 19 generalized command history.
08200510
RS
63;; - Think about incorporating query-replace.
64;; - Hooks and options for failed search.
282d89c0 65
3b1e4dd1
ER
66;;; Change Log:
67
b578f267
EN
68;; Changes before those recorded in ChangeLog:
69
70;; Revision 1.4 92/09/14 16:26:02 liberte
71;; Added prefix args to isearch-forward, etc. to switch between
72;; string and regular expression searching.
73;; Added some support for lemacs.
74;; Added general isearch-highlight option - but only for lemacs so far.
75;; Added support for frame switching in emacs 19.
76;; Added word search option to isearch-edit-string.
77;; Renamed isearch-quit to isearch-abort.
78;; Numerous changes to comments and doc strings.
79;;
80;; Revision 1.3 92/06/29 13:10:08 liberte
81;; Moved modal isearch-mode handling into isearch-mode.
82;; Got rid of buffer-local isearch variables.
83;; isearch-edit-string used by ring adjustments, completion, and
84;; nonincremental searching. C-s and C-r are additional exit commands.
85;; Renamed all regex to regexp.
86;; Got rid of found-start and found-point globals.
87;; Generalized handling of upper-case chars.
88
89;; Revision 1.2 92/05/27 11:33:57 liberte
90;; Emacs version 19 has a search ring, which is supported here.
91;; Other fixes found in the version 19 isearch are included here.
92;;
93;; Also see variables search-caps-disable-folding,
94;; search-nonincremental-instead, search-whitespace-regexp, and
95;; commands isearch-toggle-regexp, isearch-edit-string.
96;;
97;; semi-modal isearching is supported.
98
99;; Changes for 1.1
100;; 3/18/92 Fixed invalid-regexp.
101;; 3/18/92 Fixed yanking in regexps.
282d89c0 102
3b1e4dd1 103;;; Code:
08200510
RS
104
105\f
58451992 106;;; Some additional options and constants.
08200510 107
9c1db929
RS
108(defgroup isearch nil
109 "Incremental search minor mode."
110 :prefix "isearch-"
111 :prefix "search-"
112 :group 'matching)
fdf5afa4 113
9c1db929
RS
114
115(defcustom search-exit-option t
116 "*Non-nil means random control characters terminate incremental search."
117 :type 'boolean
118 :group 'isearch)
119
120(defcustom search-slow-window-lines 1
fdf5afa4
RS
121 "*Number of lines in slow search display windows.
122These are the short windows used during incremental search on slow terminals.
123Negative means put the slow search window at the top (normally it's at bottom)
9c1db929
RS
124and the value is minus the number of lines."
125 :type 'integer
126 :group 'isearch)
fdf5afa4 127
9c1db929 128(defcustom search-slow-speed 1200
fdf5afa4
RS
129 "*Highest terminal speed at which to use \"slow\" style incremental search.
130This is the style where a one-line window is created to show the line
9c1db929
RS
131that the search has reached."
132 :type 'integer
133 :group 'isearch)
8e1cae6d 134
9c1db929 135(defcustom search-upper-case 'not-yanks
8e1cae6d 136 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
137That is, upper and lower case chars must match exactly.
138This applies no matter where the chars come from, but does not
fdf5afa4 139apply to chars in regexps that are prefixed with `\\'.
9c1db929
RS
140If this value is `not-yanks', yanked text is always downcased."
141 :type '(choice (const :tag "off" nil)
142 (const not-yanks)
143 (sexp :tag "on" :format "%t\n" t))
144 :group 'isearch)
8e1cae6d 145
9c1db929 146(defcustom search-nonincremental-instead t
8e1cae6d 147 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510 148Actually, `isearch-edit-string' is called to let you enter the search
9c1db929
RS
149string, and RET terminates editing and does a nonincremental search."
150 :type 'boolean
151 :group 'isearch)
8e1cae6d 152
9c1db929 153(defcustom search-whitespace-regexp "\\s-+"
8e1cae6d 154 "*If non-nil, regular expression to match a sequence of whitespace chars.
9c1db929
RS
155You might want to use something like \"[ \\t\\r\\n]+\" instead."
156 :type 'regexp
157 :group 'isearch)
8e1cae6d 158
30178dde 159(defcustom search-highlight t
9c1db929
RS
160 "*Non-nil means incremental search highlights the current match."
161 :type 'boolean
162 :group 'isearch)
08200510 163
0352b205
RS
164(defcustom search-invisible 'open
165 "If t incremental search can match hidden text.
166nil means don't match invisible text.
167If the value is `open', if the text matched is made invisible by
168an overlay having an `invisible' property and that overlay has a property
169`isearch-open-invisible', then incremental search will show the contents.
170\(This applies when using `outline.el' and `hideshow.el'.)"
171 :type '(choice (const :tag "Match hidden text" t)
172 (const :tag "Open overlays" open)
173 (const :tag "Don't match hidden text" t))
174 :group 'isearch)
175
176(defcustom isearch-hide-immediately t
177 "If t hide the previous match if needed.
178This has efect iff `search-invisible' is set to `open' and it means
179that if the current match is out of one of the previously shown
180regions hide is right away, as opposed to hiding it at the end of
181isearch."
182 :type 'boolean
183 :group 'isearch)
86bfaffe 184
08200510
RS
185(defvar isearch-mode-hook nil
186 "Function(s) to call after starting up an incremental search.")
187
188(defvar isearch-mode-end-hook nil
189 "Function(s) to call after terminating an incremental search.")
190
8e1cae6d 191;;; Search ring.
8e1cae6d
JB
192
193(defvar search-ring nil
194 "List of search string sequences.")
08200510 195(defvar regexp-search-ring nil
8e1cae6d
JB
196 "List of regular expression search string sequences.")
197
9c1db929
RS
198(defcustom search-ring-max 16
199 "*Maximum length of search ring before oldest elements are thrown away."
200 :type 'integer
201 :group 'isearch)
202(defcustom regexp-search-ring-max 16
203 "*Maximum length of regexp search ring before oldest elements are thrown away."
204 :type 'integer
205 :group 'isearch)
8e1cae6d
JB
206
207(defvar search-ring-yank-pointer nil
a5dcf63f
RS
208 "Index in `search-ring' of last string reused.
209nil if none yet.")
08200510 210(defvar regexp-search-ring-yank-pointer nil
a5dcf63f
RS
211 "Index in `regexp-search-ring' of last string reused.
212nil if none yet.")
8e1cae6d 213
9c1db929 214(defcustom search-ring-update nil
08200510 215 "*Non-nil if advancing or retreating in the search ring should cause search.
9c1db929
RS
216Default value, nil, means edit the string instead."
217 :type 'boolean
218 :group 'isearch)
08200510 219
8e1cae6d
JB
220;;; Define isearch-mode keymap.
221
222(defvar isearch-mode-map nil
223 "Keymap for isearch-mode.")
224
08200510
RS
225(or isearch-mode-map
226 (let* ((i 0)
fdf5afa4 227 (map (make-keymap)))
fcf8ba15 228 (or (vectorp (nth 1 map))
280e8e04 229 (char-table-p (nth 1 map))
fcf8ba15 230 (error "The initialization of isearch-mode-map must be updated"))
01a6ef79 231 ;; Make Latin-1, Latin-2, Latin-3 and Latin-4 characters
280e8e04 232 ;; search for themselves.
01a6ef79
RS
233 (aset (nth 1 map) (make-char 'latin-iso8859-1) 'isearch-printing-char)
234 (aset (nth 1 map) (make-char 'latin-iso8859-2) 'isearch-printing-char)
235 (aset (nth 1 map) (make-char 'latin-iso8859-3) 'isearch-printing-char)
236 (aset (nth 1 map) (make-char 'latin-iso8859-4) 'isearch-printing-char)
d64b1d96 237 (aset (nth 1 map) (make-char 'latin-iso8859-9) 'isearch-printing-char)
ac689515
RS
238 ;; Make function keys, etc, exit the search.
239 (define-key map [t] 'isearch-other-control-char)
08200510 240 ;; Control chars, by default, end isearch mode transparently.
ac689515
RS
241 ;; We need these explicit definitions because, in a dense keymap,
242 ;; the binding for t does not affect characters.
243 ;; We use a dense keymap to save space.
08200510
RS
244 (while (< i ?\ )
245 (define-key map (make-string 1 i) 'isearch-other-control-char)
246 (setq i (1+ i)))
247
8cb2ceaa 248 ;; Printing chars extend the search string by default.
ac689515 249 (setq i ?\ )
8d7e4e80 250 (while (< i (length (nth 1 map)))
8cb2ceaa 251 (define-key map (vector i) 'isearch-printing-char)
08200510
RS
252 (setq i (1+ i)))
253
ec121bad
RS
254 ;; To handle local bindings with meta char prefix keys, define
255 ;; another full keymap. This must be done for any other prefix
256 ;; keys as well, one full keymap per char of the prefix key. It
257 ;; would be simpler to disable the global keymap, and/or have a
258 ;; default local key binding for any key not otherwise bound.
259 (let ((meta-map (make-sparse-keymap)))
260 (define-key map (char-to-string meta-prefix-char) meta-map)
261 (define-key map [escape] meta-map))
262 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
263
08200510
RS
264 ;; Several non-printing chars change the searching behavior.
265 (define-key map "\C-s" 'isearch-repeat-forward)
266 (define-key map "\C-r" 'isearch-repeat-backward)
267 (define-key map "\177" 'isearch-delete-char)
268 (define-key map "\C-g" 'isearch-abort)
ec121bad
RS
269 ;; This assumes \e is the meta-prefix-char.
270 (or (= ?\e meta-prefix-char)
271 (error "Inconsistency in isearch.el"))
19993c54
RS
272 (define-key map "\e\e\e" 'isearch-cancel)
273 (define-key map [escape escape escape] 'isearch-cancel)
8e1cae6d 274
08200510
RS
275 (define-key map "\C-q" 'isearch-quote-char)
276
08200510
RS
277 (define-key map "\r" 'isearch-exit)
278 (define-key map "\C-j" 'isearch-printing-char)
279 (define-key map "\t" 'isearch-printing-char)
280 (define-key map " " 'isearch-whitespace-chars)
8e1cae6d 281
08200510
RS
282 (define-key map "\C-w" 'isearch-yank-word)
283 (define-key map "\C-y" 'isearch-yank-line)
284
285 ;; Define keys for regexp chars * ? |.
286 ;; Nothing special for + because it matches at least once.
287 (define-key map "*" 'isearch-*-char)
288 (define-key map "?" 'isearch-*-char)
289 (define-key map "|" 'isearch-|-char)
290
58f1634a
RS
291;;; Turned off because I find I expect to get the global definition--rms.
292;;; ;; Instead bind C-h to special help command for isearch-mode.
293;;; (define-key map "\C-h" 'isearch-mode-help)
08200510 294
08200510
RS
295 (define-key map "\M-n" 'isearch-ring-advance)
296 (define-key map "\M-p" 'isearch-ring-retreat)
30d47262 297 (define-key map "\M-y" 'isearch-yank-kill)
1a3a6707 298
08200510
RS
299 (define-key map "\M-\t" 'isearch-complete)
300
5f48fc17
KH
301 ;; Pass frame events transparently so they won't exit the search.
302 ;; In particular, if we have more than one display open, then a
303 ;; switch-frame might be generated by someone typing at another keyboard.
304 (define-key map [switch-frame] nil)
305 (define-key map [delete-frame] nil)
306 (define-key map [iconify-frame] nil)
307 (define-key map [make-frame-visible] nil)
f46d5091
KH
308 ;; For searching multilingual text.
309 (define-key map "\C-\\" 'isearch-toggle-input-method)
310 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
5f48fc17 311
08200510
RS
312 (setq isearch-mode-map map)
313 ))
8e1cae6d 314
08200510
RS
315;; Some bindings you may want to put in your isearch-mode-hook.
316;; Suggest some alternates...
4453091d 317;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-case-fold)
08200510
RS
318;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp)
319;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string)
8e1cae6d 320
8e1cae6d 321
08200510
RS
322(defvar minibuffer-local-isearch-map nil
323 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 324
08200510
RS
325(or minibuffer-local-isearch-map
326 (let ((map (copy-keymap minibuffer-local-map)))
327 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
35a4d143
RS
328 (define-key map "\M-n" 'isearch-ring-advance-edit)
329 (define-key map "\M-p" 'isearch-ring-retreat-edit)
08200510
RS
330 (define-key map "\M-\t" 'isearch-complete-edit)
331 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
332 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
333 (setq minibuffer-local-isearch-map map)
334 ))
8e1cae6d 335
8e1cae6d 336;; Internal variables declared globally for byte-compiler.
08200510
RS
337;; These are all set with setq while isearching
338;; and bound locally while editing the search string.
339
340(defvar isearch-forward nil) ; Searching in the forward direction.
341(defvar isearch-regexp nil) ; Searching for a regexp.
342(defvar isearch-word nil) ; Searching for words.
343
344(defvar isearch-cmds nil) ; Stack of search status sets.
345(defvar isearch-string "") ; The current search string.
346(defvar isearch-message "") ; text-char-description version of isearch-string
347
348(defvar isearch-success t) ; Searching is currently successful.
349(defvar isearch-invalid-regexp nil) ; Regexp not well formed.
c5f6b356 350(defvar isearch-within-brackets nil) ; Regexp has unclosed [.
08200510
RS
351(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
352(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d 353(defvar isearch-barrier 0)
99ae9e9f 354(defvar isearch-just-started nil)
8e1cae6d 355
4453091d
RS
356; case-fold-search while searching.
357; either nil, t, or 'yes. 'yes means the same as t except that mixed
358; case in the search string is ignored.
359(defvar isearch-case-fold-search nil)
8e1cae6d
JB
360
361(defvar isearch-adjusted nil)
362(defvar isearch-slow-terminal-mode nil)
08200510
RS
363;;; If t, using a small window.
364(defvar isearch-small-window nil)
8e1cae6d 365(defvar isearch-opoint 0)
08200510
RS
366;;; The window configuration active at the beginning of the search.
367(defvar isearch-window-configuration nil)
8e1cae6d 368
08200510
RS
369;; Flag to indicate a yank occurred, so don't move the cursor.
370(defvar isearch-yank-flag nil)
8e1cae6d 371
f46d5091
KH
372;; Flag to indicate that we are searching multibyte characaters.
373(defvar isearch-multibyte-characters-flag nil)
374
08200510
RS
375;;; A function to be called after each input character is processed.
376;;; (It is not called after characters that exit the search.)
377;;; It is only set from an optional argument to `isearch-mode'.
378(defvar isearch-op-fun nil)
8e1cae6d 379
08200510
RS
380;;; Is isearch-mode in a recursive edit for modal searching.
381(defvar isearch-recursive-edit nil)
8e1cae6d 382
08200510
RS
383;;; Should isearch be terminated after doing one search?
384(defvar isearch-nonincremental nil)
385
386;; New value of isearch-forward after isearch-edit-string.
387(defvar isearch-new-forward nil)
8e1cae6d 388
0352b205
RS
389;; Accumulate here the overlays opened during searching.
390(defvar isearch-opened-overlays nil)
8e1cae6d 391
8e1cae6d
JB
392;; Minor-mode-alist changes - kind of redundant with the
393;; echo area, but if isearching in multiple windows, it can be useful.
394
395(or (assq 'isearch-mode minor-mode-alist)
396 (nconc minor-mode-alist
397 (list '(isearch-mode isearch-mode))))
398
08200510 399(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
400(make-variable-buffer-local 'isearch-mode)
401
fdf5afa4
RS
402(define-key global-map "\C-s" 'isearch-forward)
403(define-key esc-map "\C-s" 'isearch-forward-regexp)
404(define-key global-map "\C-r" 'isearch-backward)
405(define-key esc-map "\C-r" 'isearch-backward-regexp)
406
8e1cae6d 407;;; Entry points to isearch-mode.
08200510 408;;; These four functions should replace those in loaddefs.el
b457cc3b 409;;; An alternative is to defalias isearch-forward etc to isearch-mode,
08200510 410;;; and look at this-command to set the options accordingly.
8e1cae6d 411
eceee2c0 412(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
413 "\
414Do incremental search forward.
08200510
RS
415With a prefix argument, do an incremental regular expression search instead.
416\\<isearch-mode-map>
8e1cae6d 417As you type characters, they add to the search string and are found.
08200510 418The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 419
08200510 420Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 421Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
422Type LFD (C-j) to match end of line.
423Type \\[isearch-repeat-forward] to search again forward,\
424 \\[isearch-repeat-backward] to search again backward.
425Type \\[isearch-yank-word] to yank word from buffer onto end of search\
426 string and search for it.
427Type \\[isearch-yank-line] to yank rest of line onto end of search string\
428 and search for it.
de40ed89 429Type \\[isearch-yank-kill] to yank the last string of killed text.
8e1cae6d 430Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
431\\[isearch-abort] while searching or when search has failed cancels input\
432 back to what has
433 been found successfully.
434\\[isearch-abort] when search is successful aborts and moves point to\
435 starting point.
8e1cae6d
JB
436
437Also supported is a search ring of the previous 16 search strings.
438Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
439Type \\[isearch-ring-retreat] to search for the previous item in the search\
440 ring.
441Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 442
08200510
RS
443The above keys, bound in `isearch-mode-map', are often controlled by
444 options; do M-x apropos on search-.* to find them.
8e1cae6d 445Other control and meta characters terminate the search
08200510 446 and are then executed normally (depending on `search-exit-option').
fd49e05c 447Likewise for function keys and mouse button events.
8e1cae6d 448
08200510
RS
449If this function is called non-interactively, it does not return to
450the calling function until the search is done."
8e1cae6d 451
eceee2c0 452 (interactive "P\np")
4ae7d00a 453 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 454
eceee2c0 455(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
456 "\
457Do incremental search forward for regular expression.
08200510 458With a prefix argument, do a regular string search instead.
8e1cae6d
JB
459Like ordinary incremental search except that your input
460is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 461 (interactive "P\np")
4ae7d00a 462 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 463
eceee2c0 464(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
465 "\
466Do incremental search backward.
08200510 467With a prefix argument, do a regular expression search instead.
8e1cae6d 468See \\[isearch-forward] for more information."
eceee2c0 469 (interactive "P\np")
4ae7d00a 470 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 471
eceee2c0 472(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
473 "\
474Do incremental search backward for regular expression.
08200510 475With a prefix argument, do a regular string search instead.
8e1cae6d
JB
476Like ordinary incremental search except that your input
477is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 478 (interactive "P\np")
4ae7d00a 479 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
480
481
482(defun isearch-mode-help ()
483 (interactive)
484 (describe-function 'isearch-forward)
485 (isearch-update))
8e1cae6d
JB
486
487\f
8e1cae6d
JB
488;; isearch-mode only sets up incremental search for the minor mode.
489;; All the work is done by the isearch-mode commands.
490
08200510 491;; Not used yet:
d7fa5aa2 492;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
493;; isearch-forward-regexp isearch-backward-regexp)
494;; "List of commands for which isearch-mode does not recursive-edit.")
495
496
497(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
498 "Start isearch minor mode. Called by `isearch-forward', etc.
499
500\\{isearch-mode-map}"
8e1cae6d
JB
501
502 ;; Initialize global vars.
503 (setq isearch-forward forward
504 isearch-regexp regexp
08200510 505 isearch-word word-p
8e1cae6d
JB
506 isearch-op-fun op-fun
507 isearch-case-fold-search case-fold-search
508 isearch-string ""
509 isearch-message ""
510 isearch-cmds nil
511 isearch-success t
512 isearch-wrapped nil
513 isearch-barrier (point)
514 isearch-adjusted nil
515 isearch-yank-flag nil
516 isearch-invalid-regexp nil
c5f6b356 517 isearch-within-brackets nil
0cabad13 518 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d
JB
519 (> (window-height)
520 (* 4 search-slow-window-lines)))
521 isearch-other-end nil
522 isearch-small-window nil
99ae9e9f 523 isearch-just-started t
f46d5091 524 isearch-multibyte-characters-flag nil
8c5d9782 525 isearch-input-method nil
8e1cae6d 526
8e1cae6d 527 isearch-opoint (point)
a5dcf63f 528 search-ring-yank-pointer nil
0352b205 529 isearch-opened-overlays nil
a5dcf63f 530 regexp-search-ring-yank-pointer nil)
99ae9e9f 531 (looking-at "")
292a8dff
RS
532 (setq isearch-window-configuration
533 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 534
18ce7555
RS
535 ;; Maybe make minibuffer frame visible and/or raise it.
536 (let ((frame (window-frame (minibuffer-window))))
537 (if (not (memq (frame-live-p frame) '(nil t)))
538 (progn
539 (make-frame-visible frame)
540 (if minibuffer-auto-raise
541 (raise-frame frame)))))
542
8e1cae6d 543 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 544 (force-mode-line-update)
8e1cae6d
JB
545
546 (isearch-push-state)
547
c7955222 548 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
549 (isearch-update)
550 (run-hooks 'isearch-mode-hook)
08200510 551
bfe2d334 552 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
6e5cd0ae 553
08200510
RS
554 ;; isearch-mode can be made modal (in the sense of not returning to
555 ;; the calling function until searching is completed) by entering
556 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
557 (if recursive-edit
558 (let ((isearch-recursive-edit t))
559 (recursive-edit)))
0daa17f3 560 isearch-success)
8e1cae6d
JB
561
562
8e1cae6d
JB
563;; Some high level utilities. Others below.
564
565(defun isearch-update ()
566 ;; Called after each command to update the display.
58451992 567 (if (null unread-command-events)
8e1cae6d
JB
568 (progn
569 (if (not (input-pending-p))
570 (isearch-message))
571 (if (and isearch-slow-terminal-mode
572 (not (or isearch-small-window
573 (pos-visible-in-window-p))))
08200510 574 (let ((found-point (point)))
8e1cae6d 575 (setq isearch-small-window t)
8e1cae6d
JB
576 (move-to-window-line 0)
577 (let ((window-min-height 1))
578 (split-window nil (if (< search-slow-window-lines 0)
579 (1+ (- search-slow-window-lines))
580 (- (window-height)
581 (1+ search-slow-window-lines)))))
582 (if (< search-slow-window-lines 0)
583 (progn (vertical-motion (- 1 search-slow-window-lines))
584 (set-window-start (next-window) (point))
585 (set-window-hscroll (next-window)
586 (window-hscroll))
587 (set-window-hscroll (selected-window) 0))
588 (other-window 1))
08200510
RS
589 (goto-char found-point)))
590 (if isearch-other-end
591 (if (< isearch-other-end (point)) ; isearch-forward?
592 (isearch-highlight isearch-other-end (point))
99afb671
RS
593 (isearch-highlight (point) isearch-other-end))
594 (isearch-dehighlight nil))
08200510 595 ))
8e1cae6d
JB
596 (setq ;; quit-flag nil not for isearch-mode
597 isearch-adjusted nil
598 isearch-yank-flag nil)
599 )
600
0daa17f3 601(defun isearch-done (&optional nopush edit)
bfe2d334 602 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
8e1cae6d 603 ;; Called by all commands that terminate isearch-mode.
35a4d143 604 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 605 (setq overriding-terminal-local-map nil)
08200510
RS
606 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
607 (isearch-dehighlight t)
608 (let ((found-start (window-start (selected-window)))
609 (found-point (point)))
70418205
RS
610 (if isearch-window-configuration
611 (set-window-configuration isearch-window-configuration))
08200510 612
d49b6338
RS
613 (if isearch-small-window
614 (goto-char found-point)
615 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 616 (set-window-start (selected-window) found-start t))
d49b6338 617
08200510
RS
618 ;; If there was movement, mark the starting position.
619 ;; Maybe should test difference between and set mark iff > threshold.
620 (if (/= (point) isearch-opoint)
9821535a
RS
621 (or (and transient-mark-mode mark-active)
622 (progn
623 (push-mark isearch-opoint t)
f3acf6f5 624 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 625 (message "Mark saved where search started"))))))
08200510
RS
626
627 (setq isearch-mode nil)
1cd3732c 628 (force-mode-line-update)
8e1cae6d 629
35a4d143 630 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 631 ;; Update the ring data.
73d2bf95 632 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 633
8e1cae6d 634 (run-hooks 'isearch-mode-end-hook)
0daa17f3 635 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 636
73d2bf95
RS
637(defun isearch-update-ring (string &optional regexp)
638 "Add STRING to the beginning of the search ring.
639REGEXP says which ring to use."
640 (if regexp
641 (if (or (null regexp-search-ring)
fd89878d 642 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
643 (progn
644 (setq regexp-search-ring
fd89878d 645 (cons string regexp-search-ring))
73d2bf95
RS
646 (if (> (length regexp-search-ring) regexp-search-ring-max)
647 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
648 nil))))
649 (if (or (null search-ring)
fd89878d 650 (not (string= string (car search-ring))))
73d2bf95 651 (progn
fd89878d 652 (setq search-ring (cons string search-ring))
73d2bf95
RS
653 (if (> (length search-ring) search-ring-max)
654 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
655
08200510 656;;; Switching buffers should first terminate isearch-mode.
eb8c3be9 657;;; This is done quite differently for each variant of emacs.
08200510
RS
658;;; For lemacs, see Exiting in lemacs below
659
660;; For Emacs 19, the frame switch event is handled.
661(defun isearch-switch-frame-handler ()
662 (interactive) ;; Is this necessary?
663 ;; First terminate isearch-mode.
664 (isearch-done)
0352b205 665 (isearch-clean-overlays)
dba1ec55 666 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510 667
8e1cae6d 668\f
8e1cae6d
JB
669;; Commands active while inside of the isearch minor mode.
670
671(defun isearch-exit ()
672 "Exit search normally.
673However, if this is the first command after starting incremental
674search and `search-nonincremental-instead' is non-nil, do a
08200510 675nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
676 (interactive)
677 (if (and search-nonincremental-instead
678 (= 0 (length isearch-string)))
08200510
RS
679 (let ((isearch-nonincremental t))
680 (isearch-edit-string)))
0352b205
RS
681 (isearch-done)
682 (isearch-clean-overlays))
8e1cae6d
JB
683
684
685(defun isearch-edit-string ()
08200510
RS
686 "Edit the search string in the minibuffer.
687The following additional command keys are active while editing.
688\\<minibuffer-local-isearch-map>
689\\[exit-minibuffer] to resume incremental searching with the edited string.
690\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
691\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 692\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 693\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 694\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 695\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 696\\<isearch-mode-map>
08200510
RS
697If first char entered is \\[isearch-yank-word], then do word search instead."
698
699 ;; This code is very hairy for several reasons, explained in the code.
700 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
701 ;; If there were a way to catch any change of buffer from the minibuffer,
702 ;; this could be simplified greatly.
eb8c3be9 703 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 704 (interactive)
08200510 705 (condition-case err
e900ced4
RS
706 (progn
707 (let ((isearch-nonincremental isearch-nonincremental)
708
709 ;; Locally bind all isearch global variables to protect them
710 ;; from recursive isearching.
711 ;; isearch-string -message and -forward are not bound
712 ;; so they may be changed. Instead, save the values.
713 (isearch-new-string isearch-string)
714 (isearch-new-message isearch-message)
715 (isearch-new-forward isearch-forward)
716 (isearch-new-word isearch-word)
717
718 (isearch-regexp isearch-regexp)
719 (isearch-op-fun isearch-op-fun)
720 (isearch-cmds isearch-cmds)
721 (isearch-success isearch-success)
722 (isearch-wrapped isearch-wrapped)
723 (isearch-barrier isearch-barrier)
724 (isearch-adjusted isearch-adjusted)
725 (isearch-yank-flag isearch-yank-flag)
726 (isearch-invalid-regexp isearch-invalid-regexp)
727 (isearch-within-brackets isearch-within-brackets)
728 ;;; Don't bind this. We want isearch-search, below, to set it.
729 ;;; And the old value won't matter after that.
730 ;;; (isearch-other-end isearch-other-end)
731 ;;; Perhaps some of these other variables should be bound for a
732 ;;; shorter period, ending before the next isearch-search.
733 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
734 (isearch-opoint isearch-opoint)
735 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
736 (isearch-small-window isearch-small-window)
737 (isearch-recursive-edit isearch-recursive-edit)
738 ;; Save current configuration so we can restore it here.
739 (isearch-window-configuration (current-window-configuration))
740 )
741
742 ;; Actually terminate isearching until editing is done.
743 ;; This is so that the user can do anything without failure,
744 ;; like switch buffers and start another isearch, and return.
745 (condition-case err
746 (isearch-done t t)
747 (exit nil)) ; was recursive editing
748
749 (isearch-message) ;; for read-char
750 (unwind-protect
751 (let* (;; Why does following read-char echo?
752 ;;(echo-keystrokes 0) ;; not needed with above message
753 (e (let ((cursor-in-echo-area t))
754 (read-event)))
755 ;; Binding minibuffer-history-symbol to nil is a work-around
756 ;; for some incompatibility with gmhist.
757 (minibuffer-history-symbol)
758 (message-log-max nil))
759 ;; If the first character the user types when we prompt them
760 ;; for a string is the yank-word character, then go into
761 ;; word-search mode. Otherwise unread that character and
762 ;; read a key the normal way.
763 ;; Word search does not apply (yet) to regexp searches,
764 ;; no check is made here.
765 (message (isearch-message-prefix nil nil t))
766 (if (eq 'isearch-yank-word
767 (lookup-key isearch-mode-map (vector e)))
768 (setq isearch-word t;; so message-prefix is right
769 isearch-new-word t)
770 (cancel-kbd-macro-events)
771 (isearch-unread e))
772 (setq cursor-in-echo-area nil)
773 (setq isearch-new-string
774 (let (junk-ring)
775 (read-from-minibuffer
776 (isearch-message-prefix nil nil isearch-nonincremental)
777 isearch-string
778 minibuffer-local-isearch-map nil
779 'junk-ring))
780 isearch-new-message
781 (mapconcat 'isearch-text-char-description
782 isearch-new-string "")))
783 ;; Always resume isearching by restarting it.
784 (isearch-mode isearch-forward
785 isearch-regexp
786 isearch-op-fun
787 nil
788 isearch-word)
789
790 ;; Copy new local values to isearch globals
791 (setq isearch-string isearch-new-string
792 isearch-message isearch-new-message
793 isearch-forward isearch-new-forward
794 isearch-word isearch-new-word))
795
796 ;; Empty isearch-string means use default.
797 (if (= 0 (length isearch-string))
798 (setq isearch-string (or (car (if isearch-regexp
799 regexp-search-ring
800 search-ring))
801 ""))
802 ;; This used to set the last search string,
803 ;; but I think it is not right to do that here.
804 ;; Only the string actually used should be saved.
805 ))
806
807 ;; Push the state as of before this C-s.
808 (isearch-push-state)
08200510
RS
809
810 ;; Reinvoke the pending search.
08200510
RS
811 (isearch-search)
812 (isearch-update)
813 (if isearch-nonincremental
814 (progn
815 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
816 (isearch-done))))
8e1cae6d 817
08200510
RS
818 (quit ; handle abort-recursive-edit
819 (isearch-abort) ;; outside of let to restore outside global values
820 )))
821
822(defun isearch-nonincremental-exit-minibuffer ()
823 (interactive)
824 (setq isearch-nonincremental t)
825 (exit-minibuffer))
826
827(defun isearch-forward-exit-minibuffer ()
828 (interactive)
829 (setq isearch-new-forward t)
830 (exit-minibuffer))
8e1cae6d 831
08200510 832(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 833 (interactive)
08200510
RS
834 (setq isearch-new-forward nil)
835 (exit-minibuffer))
836
19993c54
RS
837(defun isearch-cancel ()
838 "Terminate the search and go back to the starting point."
839 (interactive)
840 (goto-char isearch-opoint)
841 (isearch-done t)
0352b205 842 (isearch-clean-overlays)
19993c54 843 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
844
845(defun isearch-abort ()
b7852303 846 "Abort incremental search mode if searching is successful, signaling quit.
08200510 847Otherwise, revert to previous successful search and continue searching.
b7852303 848Use `isearch-exit' to quit without signaling."
08200510 849 (interactive)
eb8c3be9 850;; (ding) signal instead below, if quitting
8e1cae6d
JB
851 (discard-input)
852 (if isearch-success
853 ;; If search is successful, move back to starting point
854 ;; and really do quit.
855 (progn (goto-char isearch-opoint)
509ed182 856 (setq isearch-success nil)
35a4d143 857 (isearch-done t) ; exit isearch
0352b205 858 (isearch-clean-overlays)
a5dcf63f 859 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
860 ;; If search is failing, or has an incomplete regexp,
861 ;; rub out until it is once more successful.
862 (while (or (not isearch-success) isearch-invalid-regexp)
863 (isearch-pop-state))
8e1cae6d
JB
864 (isearch-update)))
865
8e1cae6d
JB
866(defun isearch-repeat (direction)
867 ;; Utility for isearch-repeat-forward and -backward.
868 (if (eq isearch-forward (eq direction 'forward))
869 ;; C-s in forward or C-r in reverse.
870 (if (equal isearch-string "")
871 ;; If search string is empty, use last one.
872 (setq isearch-string
8e1cae6d 873 (or (if isearch-regexp
a5dcf63f
RS
874 (car regexp-search-ring)
875 (car search-ring))
8e1cae6d
JB
876 "")
877 isearch-message
08200510 878 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
879 isearch-string ""))
880 ;; If already have what to search for, repeat it.
881 (or isearch-success
882 (progn
8e1cae6d
JB
883 (goto-char (if isearch-forward (point-min) (point-max)))
884 (setq isearch-wrapped t))))
885 ;; C-s in reverse or C-r in forward, change direction.
886 (setq isearch-forward (not isearch-forward)))
887
888 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
889
890 (if (equal isearch-string "")
891 (setq isearch-success t)
99ae9e9f
KH
892 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
893 (not isearch-just-started))
8e1cae6d
JB
894 ;; If repeating a search that found
895 ;; an empty string, ensure we advance.
6fefdc4b
RS
896 (if (if isearch-forward (eobp) (bobp))
897 ;; If there's nowhere to advance to, fail (and wrap next time).
898 (progn
899 (setq isearch-success nil)
900 (ding))
901 (forward-char (if isearch-forward 1 -1))
902 (isearch-search))
903 (isearch-search)))
904
8e1cae6d
JB
905 (isearch-push-state)
906 (isearch-update))
907
908(defun isearch-repeat-forward ()
909 "Repeat incremental search forwards."
910 (interactive)
911 (isearch-repeat 'forward))
912
913(defun isearch-repeat-backward ()
914 "Repeat incremental search backwards."
915 (interactive)
916 (isearch-repeat 'backward))
917
918(defun isearch-toggle-regexp ()
919 "Toggle regexp searching on or off."
920 ;; The status stack is left unchanged.
921 (interactive)
922 (setq isearch-regexp (not isearch-regexp))
08200510 923 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
924 (isearch-update))
925
4453091d
RS
926(defun isearch-toggle-case-fold ()
927 "Toggle case folding in searching on or off."
928 (interactive)
929 (setq isearch-case-fold-search
930 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
931 (let ((message-log-max nil))
932 (message "%s%s [case %ssensitive]"
933 (isearch-message-prefix nil nil isearch-nonincremental)
934 isearch-message
935 (if isearch-case-fold-search "in" "")))
4453091d
RS
936 (setq isearch-adjusted t)
937 (sit-for 1)
938 (isearch-update))
939
8e1cae6d
JB
940(defun isearch-delete-char ()
941 "Discard last input item and move point back.
942If no previous match was done, just beep."
943 (interactive)
944 (if (null (cdr isearch-cmds))
945 (ding)
946 (isearch-pop-state))
947 (isearch-update))
948
949
950(defun isearch-yank (chunk)
951 ;; Helper for isearch-yank-word and isearch-yank-line
fd543be5 952 ;; CHUNK should be word, line, kill, or x-sel.
30d47262
RS
953 (let ((string (cond
954 ((eq chunk 'kill)
955 (current-kill 0))
fd543be5
KH
956 ((eq chunk 'x-sel)
957 (x-get-selection))
30d47262
RS
958 (t
959 (save-excursion
960 (and (not isearch-forward) isearch-other-end
961 (goto-char isearch-other-end))
962 (buffer-substring
963 (point)
964 (save-excursion
965 (cond
966 ((eq chunk 'word)
967 (forward-word 1))
968 ((eq chunk 'line)
969 (end-of-line)))
970 (point))))))))
08200510
RS
971 ;; Downcase the string if not supposed to case-fold yanked strings.
972 (if (and isearch-case-fold-search
fdf5afa4 973 (eq 'not-yanks search-upper-case))
08200510
RS
974 (setq string (downcase string)))
975 (if isearch-regexp (setq string (regexp-quote string)))
976 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
977 isearch-message
978 (concat isearch-message
08200510
RS
979 (mapconcat 'isearch-text-char-description
980 string ""))
8e1cae6d
JB
981 ;; Don't move cursor in reverse search.
982 isearch-yank-flag t))
983 (isearch-search-and-update))
984
30d47262
RS
985(defun isearch-yank-kill ()
986 "Pull string from kill ring into search string."
987 (interactive)
988 (isearch-yank 'kill))
8e1cae6d
JB
989
990(defun isearch-yank-word ()
991 "Pull next word from buffer into search string."
992 (interactive)
993 (isearch-yank 'word))
994
995(defun isearch-yank-line ()
996 "Pull rest of line from buffer into search string."
997 (interactive)
998 (isearch-yank 'line))
999
1000
1001(defun isearch-search-and-update ()
1002 ;; Do the search and update the display.
1003 (if (and (not isearch-success)
1004 ;; unsuccessful regexp search may become
1005 ;; successful by addition of characters which
1006 ;; make isearch-string valid
1007 (not isearch-regexp))
1008 nil
1009 ;; In reverse search, adding stuff at
1010 ;; the end may cause zero or many more chars to be
1011 ;; matched, in the string following point.
1012 ;; Allow all those possibilities without moving point as
1013 ;; long as the match does not extend past search origin.
1014 (if (and (not isearch-forward) (not isearch-adjusted)
1015 (condition-case ()
99ae9e9f
KH
1016 (let ((case-fold-search isearch-case-fold-search))
1017 (looking-at (if isearch-regexp isearch-string
1018 (regexp-quote isearch-string))))
8e1cae6d
JB
1019 (error nil))
1020 (or isearch-yank-flag
1021 (<= (match-end 0)
1022 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1023 (progn
1024 (setq isearch-success t
1025 isearch-invalid-regexp nil
1026 isearch-within-brackets nil
1027 isearch-other-end (match-end 0))
1028 (if (and (eq isearch-case-fold-search t) search-upper-case)
1029 (setq isearch-case-fold-search
1030 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1031 ;; Not regexp, not reverse, or no match at point.
1032 (if (and isearch-other-end (not isearch-adjusted))
1033 (goto-char (if isearch-forward isearch-other-end
1034 (min isearch-opoint
1035 isearch-barrier
1036 (1+ isearch-other-end)))))
1037 (isearch-search)
1038 ))
1039 (isearch-push-state)
1040 (if isearch-op-fun (funcall isearch-op-fun))
1041 (isearch-update))
1042
1043
1044;; *, ?, and | chars can make a regexp more liberal.
08200510 1045;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1046;; So go back to place last successful search started
1047;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1048;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1049
1050(defun isearch-*-char ()
1051 "Handle * and ? specially in regexps."
1052 (interactive)
1053 (if isearch-regexp
1054
1055 (progn
1056 (setq isearch-adjusted t)
149d4fe5
RS
1057 ;; Get the isearch-other-end from before the last search.
1058 ;; We want to start from there,
1059 ;; so that we don't retreat farther than that.
1060 ;; (car isearch-cmds) is after last search;
1061 ;; (car (cdr isearch-cmds)) is from before it.
1062 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
8e1cae6d
JB
1063 (setq cs (or cs isearch-barrier))
1064 (goto-char
1065 (if isearch-forward
1066 (max cs isearch-barrier)
1067 (min cs isearch-barrier))))))
08200510 1068 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1069
1070
8e1cae6d
JB
1071(defun isearch-|-char ()
1072 "If in regexp search, jump to the barrier."
1073 (interactive)
1074 (if isearch-regexp
1075 (progn
1076 (setq isearch-adjusted t)
1077 (goto-char isearch-barrier)))
08200510 1078 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1079
1080
b457cc3b 1081(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1082
1083(defun isearch-other-meta-char ()
8f90f594 1084 "Exit the search normally and reread this key sequence.
35a4d143
RS
1085But only if `search-exit-option' is non-nil, the default.
1086If it is the symbol `edit', the search string is edited in the minibuffer
1087and the meta character is unread so that it applies to editing the string."
8e1cae6d 1088 (interactive)
c6431f12
KH
1089 (let* ((key (this-command-keys))
1090 (main-event (aref key 0))
1091 (keylist (listify-key-sequence key)))
24b5caec 1092 (cond ((and (= (length key) 1)
d94eb6eb 1093 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1094 (not (or (null lookup) (integerp lookup)
1095 (keymapp lookup)))))
24b5caec
RS
1096 ;; Handle a function key that translates into something else.
1097 ;; If the key has a global definition too,
1098 ;; exit and unread the key itself, so its global definition runs.
1099 ;; Otherwise, unread the translation,
1100 ;; so that the translated key takes effect within isearch.
d94eb6eb 1101 (cancel-kbd-macro-events)
24b5caec
RS
1102 (if (lookup-key global-map key)
1103 (progn
1104 (isearch-done)
1105 (apply 'isearch-unread keylist))
1106 (apply 'isearch-unread
1107 (listify-key-sequence (lookup-key function-key-map key)))))
1108 (
c6431f12
KH
1109 ;; Handle an undefined shifted control character
1110 ;; by downshifting it if that makes it defined.
1111 ;; (As read-key-sequence would normally do,
1112 ;; if we didn't have a default definition.)
1113 (let ((mods (event-modifiers main-event)))
1114 (and (integerp main-event)
1115 (memq 'shift mods)
1116 (memq 'control mods)
1117 (lookup-key isearch-mode-map
1118 (let ((copy (copy-sequence key)))
1119 (aset copy 0
1120 (- main-event (- ?\C-\S-a ?\C-a)))
1121 copy)
1122 nil)))
1123 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1124 (cancel-kbd-macro-events)
c6431f12
KH
1125 (apply 'isearch-unread keylist))
1126 ((eq search-exit-option 'edit)
1127 (apply 'isearch-unread keylist)
1128 (isearch-edit-string))
1129 (search-exit-option
1130 (let (window)
d94eb6eb 1131 (cancel-kbd-macro-events)
c6431f12
KH
1132 (apply 'isearch-unread keylist)
1133 ;; Properly handle scroll-bar and mode-line clicks
1134 ;; for which a dummy prefix event was generated as (aref key 0).
1135 (and (> (length key) 1)
1136 (symbolp (aref key 0))
1137 (listp (aref key 1))
1138 (not (numberp (posn-point (event-start (aref key 1)))))
1139 ;; Convert the event back into its raw form,
1140 ;; with the dummy prefix implicit in the mouse event,
1141 ;; so it will get split up once again.
1142 (progn (setq unread-command-events
1143 (cdr unread-command-events))
1144 (setq main-event (car unread-command-events))
1145 (setcar (cdr (event-start main-event))
1146 (car (nth 1 (event-start main-event))))))
1147 ;; If we got a mouse click, maybe it was read with the buffer
1148 ;; it was clicked on. If so, that buffer, not the current one,
1149 ;; is in isearch mode. So end the search in that buffer.
1150 (if (and (listp main-event)
1151 (setq window (posn-window (event-start main-event)))
1152 (windowp window))
1153 (save-excursion
1154 (set-buffer (window-buffer window))
0352b205
RS
1155 (isearch-done)
1156 (isearch-clean-overlays))
1157 (isearch-done)
1158 (isearch-clean-overlays))))
c6431f12
KH
1159 (t;; otherwise nil
1160 (isearch-process-search-string key key)))))
8e1cae6d 1161
8e1cae6d
JB
1162(defun isearch-quote-char ()
1163 "Quote special characters for incremental search."
1164 (interactive)
1165 (isearch-process-search-char (read-quoted-char (isearch-message t))))
1166
8e1cae6d
JB
1167(defun isearch-return-char ()
1168 "Convert return into newline for incremental search.
1169Obsolete."
1170 (interactive)
1171 (isearch-process-search-char ?\n))
1172
8e1cae6d 1173(defun isearch-printing-char ()
b68c164d 1174 "Add this ordinary printing character to the search string and search."
8e1cae6d 1175 (interactive)
8c5d9782 1176 (if isearch-input-method
f46d5091
KH
1177 (isearch-process-search-multibyte-characters (isearch-last-command-char))
1178 (isearch-process-search-char (isearch-last-command-char))))
8e1cae6d
JB
1179
1180(defun isearch-whitespace-chars ()
08200510 1181 "Match all whitespace chars, if in regexp mode.
b68c164d 1182If you want to search for just a space, type C-q SPC."
8e1cae6d 1183 (interactive)
08200510 1184 (if isearch-regexp
9da6682f
RS
1185 (if (and search-whitespace-regexp (not isearch-within-brackets)
1186 (not isearch-invalid-regexp))
08200510
RS
1187 (isearch-process-search-string search-whitespace-regexp " ")
1188 (isearch-printing-char))
1189 (progn
eb8c3be9 1190 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1191 ;; (setq isearch-word t)
1192 ;; (setq isearch-adjusted t)
1193 ;; (goto-char isearch-barrier)
1194 (isearch-printing-char))))
8e1cae6d
JB
1195
1196(defun isearch-process-search-char (char)
1197 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1198 (isearch-process-search-string
1199 (isearch-char-to-string char)
01a6ef79
RS
1200 (if (>= char 0200)
1201 (char-to-string char)
1202 (isearch-text-char-description char))))
8e1cae6d
JB
1203
1204(defun isearch-process-search-string (string message)
1205 (setq isearch-string (concat isearch-string string)
1206 isearch-message (concat isearch-message message))
1207 (isearch-search-and-update))
1208
1209\f
8e1cae6d
JB
1210;; Search Ring
1211
08200510
RS
1212(defun isearch-ring-adjust1 (advance)
1213 ;; Helper for isearch-ring-adjust
1214 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1215 (length (length ring))
1216 (yank-pointer-name (if isearch-regexp
08200510 1217 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1218 'search-ring-yank-pointer))
1219 (yank-pointer (eval yank-pointer-name)))
1220 (if (zerop length)
1221 ()
1222 (set yank-pointer-name
1223 (setq yank-pointer
ffbc30b2
PE
1224 (mod (+ (or yank-pointer 0)
1225 (if advance -1 1))
1226 length)))
a5dcf63f 1227 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1228 isearch-message (mapconcat 'isearch-text-char-description
1229 isearch-string "")))))
1230
1231(defun isearch-ring-adjust (advance)
1232 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1233 (isearch-ring-adjust1 advance)
08200510
RS
1234 (if search-ring-update
1235 (progn
1236 (isearch-search)
1237 (isearch-update))
1238 (isearch-edit-string)
2ff20b7e
RS
1239 )
1240 (isearch-push-state))
8e1cae6d
JB
1241
1242(defun isearch-ring-advance ()
1243 "Advance to the next search string in the ring."
08200510 1244 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1245 (interactive)
1246 (isearch-ring-adjust 'advance))
1247
1248(defun isearch-ring-retreat ()
1249 "Retreat to the previous search string in the ring."
1250 (interactive)
1251 (isearch-ring-adjust nil))
1252
a5dcf63f
RS
1253(defun isearch-ring-advance-edit (n)
1254 "Insert the next element of the search history into the minibuffer."
1255 (interactive "p")
1256 (let* ((yank-pointer-name (if isearch-regexp
1257 'regexp-search-ring-yank-pointer
1258 'search-ring-yank-pointer))
1259 (yank-pointer (eval yank-pointer-name))
1260 (ring (if isearch-regexp regexp-search-ring search-ring))
1261 (length (length ring)))
1262 (if (zerop length)
1263 ()
1264 (set yank-pointer-name
1265 (setq yank-pointer
ffbc30b2
PE
1266 (mod (- (or yank-pointer 0) n)
1267 length)))
a5dcf63f 1268
a5dcf63f 1269 (erase-buffer)
35a4d143 1270 (insert (nth yank-pointer ring))
49c13105 1271 (goto-char (point-max)))))
a5dcf63f
RS
1272
1273(defun isearch-ring-retreat-edit (n)
1274 "Inserts the previous element of the search history into the minibuffer."
1275 (interactive "p")
1276 (isearch-ring-advance-edit (- n)))
1277
1278;;(defun isearch-ring-adjust-edit (advance)
1279;; "Use the next or previous search string in the ring while in minibuffer."
1280;; (isearch-ring-adjust1 advance)
1281;; (erase-buffer)
1282;; (insert isearch-string))
1283
1284;;(defun isearch-ring-advance-edit ()
1285;; (interactive)
1286;; (isearch-ring-adjust-edit 'advance))
1287
1288;;(defun isearch-ring-retreat-edit ()
1289;; "Retreat to the previous search string in the ring while in the minibuffer."
1290;; (interactive)
1291;; (isearch-ring-adjust-edit nil))
08200510
RS
1292
1293
1294(defun isearch-complete1 ()
1295 ;; Helper for isearch-complete and isearch-complete-edit
1296 ;; Return t if completion OK, nil if no completion exists.
1297 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1298 (alist (mapcar (function (lambda (string) (list string))) ring))
1299 (completion-ignore-case case-fold-search)
1300 (completion (try-completion isearch-string alist)))
1301 (cond
1302 ((eq completion t)
1303 ;; isearch-string stays the same
1304 t)
1305 ((or completion ; not nil, must be a string
b7852303 1306 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1307 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1308 (progn
1309 (if completion-auto-help
1310 (with-output-to-temp-buffer "*Isearch completions*"
1311 (display-completion-list
1312 (all-completions isearch-string alist))))
1313 t)
1314 (and completion
1315 (setq isearch-string completion))))
08200510
RS
1316 (t
1317 (message "No completion") ; waits a second if in minibuffer
1318 nil))))
1319
1320(defun isearch-complete ()
1321 "Complete the search string from the strings on the search ring.
1322The completed string is then editable in the minibuffer.
1323If there is no completion possible, say so and continue searching."
1324 (interactive)
1325 (if (isearch-complete1)
1326 (isearch-edit-string)
1327 ;; else
1328 (sit-for 1)
1329 (isearch-update)))
8e1cae6d 1330
08200510
RS
1331(defun isearch-complete-edit ()
1332 "Same as `isearch-complete' except in the minibuffer."
1333 (interactive)
1334 (setq isearch-string (buffer-string))
1335 (if (isearch-complete1)
8e1cae6d 1336 (progn
08200510
RS
1337 (erase-buffer)
1338 (insert isearch-string))))
8e1cae6d
JB
1339
1340\f
8e1cae6d 1341;; The search status stack (and isearch window-local variables, not used).
08200510 1342;; Need a structure for this.
8e1cae6d
JB
1343
1344(defun isearch-top-state ()
8e1cae6d
JB
1345 (let ((cmd (car isearch-cmds)))
1346 (setq isearch-string (car cmd)
1347 isearch-message (car (cdr cmd))
1348 isearch-success (nth 3 cmd)
1349 isearch-forward (nth 4 cmd)
1350 isearch-other-end (nth 5 cmd)
08200510
RS
1351 isearch-word (nth 6 cmd)
1352 isearch-invalid-regexp (nth 7 cmd)
1353 isearch-wrapped (nth 8 cmd)
c5f6b356 1354 isearch-barrier (nth 9 cmd)
f551b97d
RS
1355 isearch-within-brackets (nth 10 cmd)
1356 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1357 (goto-char (car (cdr (cdr cmd))))))
1358
1359(defun isearch-pop-state ()
8e1cae6d
JB
1360 (setq isearch-cmds (cdr isearch-cmds))
1361 (isearch-top-state)
1362 )
1363
1364(defun isearch-push-state ()
1365 (setq isearch-cmds
1366 (cons (list isearch-string isearch-message (point)
1367 isearch-success isearch-forward isearch-other-end
08200510 1368 isearch-word
c5f6b356 1369 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1370 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1371 isearch-cmds)))
1372
8e1cae6d 1373\f
8e1cae6d
JB
1374;; Message string
1375
1376(defun isearch-message (&optional c-q-hack ellipsis)
1377 ;; Generate and print the message string.
1378 (let ((cursor-in-echo-area ellipsis)
1379 (m (concat
08200510 1380 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1381 isearch-message
1382 (isearch-message-suffix c-q-hack ellipsis)
1383 )))
a56687f1
KH
1384 (if c-q-hack
1385 m
1386 (let ((message-log-max nil))
1387 (message "%s" m)))))
8e1cae6d 1388
08200510 1389(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1390 ;; If about to search, and previous search regexp was invalid,
1391 ;; check that it still is. If it is valid now,
1392 ;; let the message we display while searching say that it is valid.
1393 (and isearch-invalid-regexp ellipsis
1394 (condition-case ()
1395 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1396 (setq isearch-invalid-regexp nil
1397 isearch-within-brackets nil))
8e1cae6d
JB
1398 (error nil)))
1399 ;; If currently failing, display no ellipsis.
1400 (or isearch-success (setq ellipsis nil))
1401 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1402 (if (and isearch-wrapped
1403 (if isearch-forward
1404 (> (point) isearch-opoint)
1405 (< (point) isearch-opoint)))
1406 "over")
8e1cae6d 1407 (if isearch-wrapped "wrapped ")
08200510 1408 (if isearch-word "word " "")
8e1cae6d 1409 (if isearch-regexp "regexp " "")
08200510 1410 (if nonincremental "search" "I-search")
f46d5091 1411 (if isearch-forward "" " backward")
8c5d9782
KH
1412 (if isearch-input-method
1413 (concat " [" isearch-input-method-title "]: ")
f46d5091 1414 ": ")
8e1cae6d
JB
1415 )))
1416 (aset m 0 (upcase (aref m 0)))
1417 m))
1418
1419
1420(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1421 (concat (if c-q-hack "^Q" "")
1422 (if isearch-invalid-regexp
1423 (concat " [" isearch-invalid-regexp "]")
1424 "")))
1425
1426\f
8e1cae6d
JB
1427;;; Searching
1428
1429(defun isearch-search ()
1430 ;; Do the search with the current search string.
1431 (isearch-message nil t)
4453091d 1432 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1433 (setq isearch-case-fold-search
1434 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d
JB
1435 (condition-case lossage
1436 (let ((inhibit-quit nil)
86bfaffe
RS
1437 (case-fold-search isearch-case-fold-search)
1438 (retry t))
8e1cae6d 1439 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1440 (setq isearch-within-brackets nil)
86bfaffe
RS
1441 (while retry
1442 (setq isearch-success
1443 (funcall
1444 (cond (isearch-word
1445 (if isearch-forward
1446 'word-search-forward 'word-search-backward))
1447 (isearch-regexp
1448 (if isearch-forward
1449 're-search-forward 're-search-backward))
1450 (t
1451 (if isearch-forward 'search-forward 'search-backward)))
1452 isearch-string nil t))
1453 ;; Clear RETRY unless we matched some invisible text
1454 ;; and we aren't supposed to do that.
0352b205 1455 (if (or (eq search-invisible t)
86bfaffe
RS
1456 (not isearch-success)
1457 (bobp) (eobp)
1458 (= (match-beginning 0) (match-end 0))
1459 (not (isearch-range-invisible
1460 (match-beginning 0) (match-end 0))))
1461 (setq retry nil)))
99ae9e9f 1462 (setq isearch-just-started nil)
8e1cae6d
JB
1463 (if isearch-success
1464 (setq isearch-other-end
1465 (if isearch-forward (match-beginning 0) (match-end 0)))))
1466
d32696d4 1467 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1468 (setq isearch-success nil))
1469
1470 (invalid-regexp
1471 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1472 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1473 isearch-invalid-regexp))
8e1cae6d
JB
1474 (if (string-match
1475 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1476 isearch-invalid-regexp)
f1c03125
RS
1477 (setq isearch-invalid-regexp "incomplete input")))
1478 (error
1479 ;; stack overflow in regexp search.
1480 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1481
1482 (if isearch-success
1483 nil
1484 ;; Ding if failed this time after succeeding last time.
1485 (and (nth 3 (car isearch-cmds))
1486 (ding))
1487 (goto-char (nth 2 (car isearch-cmds)))))
1488
0352b205
RS
1489
1490;;; Called when opening an overlay, and we are still in isearch.
1491(defun isearch-open-overlay-temporary (ov)
1492 (message "temporary called")
1493 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1494 ;; Some modes would want to open the overlays temporary during
1495 ;; isearch in their own way, they should set the
1496 ;; `isearch-open-invisible-temporary' to a function doing this.
1497 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1498 ;; Store the values for the `invisible' and `intangible'
1499 ;; properties, and then set them to nil. This way the text hidden
1500 ;; by this overlay becomes visible.
1501
1502 ;; Do we realy need to set the `intangible' property to t? Can we
1503 ;; have the point inside an overlay with an `intangible' property?
1504 ;; In 19.34 this does not exist so I cannot test it.
1505 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1506 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1507 (overlay-put ov 'invisible nil)
1508 (overlay-put ov 'intangible nil)))
1509
1510
1511;;; This is called at the end of isearch. I will open the overlays
1512;;; that contain the latest match. Obviously in case of a C-g the
1513;;; point returns to the original location which surely is not contain
1514;;; in any of these overlays, se we are safe in this case too.
1515(defun isearch-open-necessary-overlays (ov)
1516 (let ((inside-overlay (and (> (point) (overlay-start ov))
1517 (< (point) (overlay-end ov))))
1518 ;; If this exists it means that the overlay was opened using
1519 ;; this function, not by us tweaking the overlay properties.
1520 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1521 (when (or inside-overlay (not fct-temp))
1522 ;; restore the values for the `invisible' and `intangible'
1523 ;; properties
1524 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1525 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1526 (overlay-put ov 'isearch-invisible nil)
1527 (overlay-put ov 'isearch-intangible nil))
1528 (if inside-overlay
1529 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1530 (if fct-temp
1531 (funcall fct-temp ov t)))))
1532
1533;;; This is called when exiting isearch. It closes the temporary
1534;;; opened overlays, except the ones that contain the latest match.
1535(defun isearch-clean-overlays ()
1536 (when isearch-opened-overlays
1537 ;; Use a cycle instead of a mapcar here?
1538 (mapcar
1539 (function isearch-open-necessary-overlays) isearch-opened-overlays)
1540 (setq isearch-opened-overlays nil)))
1541
1542;;; Verify if the current match is outside of each element of
1543;;; `isearch-opened-overlays', if so close that overlay.
1544(defun isearch-close-unecessary-overlays (begin end)
1545 (let ((ov-list isearch-opened-overlays)
1546 ov
1547 inside-overlay
1548 fct-temp)
1549 (setq isearch-opened-overlays nil)
1550 (while ov-list
1551 (setq ov (car ov-list))
1552 (setq ov-list (cdr ov-list))
1553 (setq inside-overlay (or (and (> begin (overlay-start ov))
1554 (< begin (overlay-end ov)))
1555 (and (> end (overlay-start ov))
1556 (< end (overlay-end ov)))))
1557 ;; If this exists it means that the overlay was opened using
1558 ;; this function, not by us tweaking the overlay properties.
1559 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1560 (if inside-overlay
1561 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1562 (if fct-temp
1563 (funcall fct-temp ov t)
1564 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1565 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1566 (overlay-put ov 'isearch-invisible nil)
1567 (overlay-put ov 'isearch-intangible nil))))))
1568
86bfaffe
RS
1569(defun isearch-range-invisible (beg end)
1570 "Return t if all the bext from BEG to END is invisible."
1571 (and (/= beg end)
1572 ;; Check that invisibility runs up to END.
1573 (save-excursion
1574 (goto-char beg)
0352b205
RS
1575 (let
1576 ;; can-be-opened keeps track if we can open some overlays.
1577 ((can-be-opened (eq search-invisible 'open))
1578 ;; the list of overlays that could be opened
1579 (crt-overlays nil))
1580 (when (and can-be-opened isearch-hide-immediately)
1581 (isearch-close-unecessary-overlays beg end))
1582 ;; If the following character is currently invisible,
1583 ;; skip all characters with that same `invisible' property value.
1584 ;; Do that over and over.
1585 (while (and (< (point) end)
1586 (let ((prop
1587 (get-char-property (point) 'invisible)))
1588 (if (eq buffer-invisibility-spec t)
1589 prop
1590 (or (memq prop buffer-invisibility-spec)
1591 (assq prop buffer-invisibility-spec)))))
1592 (if (get-text-property (point) 'invisible)
1593 (progn
1594 (goto-char (next-single-property-change (point) 'invisible
1595 nil end))
1596 ;; if text is hidden by an `invisible' text property
1597 ;; we cannot open it at all.
1598 (setq can-be-opened nil))
1599 (unless (null can-be-opened)
1600 (let ((overlays (overlays-at (point)))
1601 ov-list
1602 o
1603 invis-prop)
1604 (while overlays
1605 (setq o (car overlays)
1606 invis-prop (overlay-get o 'invisible))
1607 (if (or (memq invis-prop buffer-invisibility-spec)
1608 (assq invis-prop buffer-invisibility-spec))
1609 (if (overlay-get o 'isearch-open-invisible)
1610 (setq ov-list (cons o ov-list))
1611 ;; We found one overlay that cannot be
1612 ;; opened, that means the whole chunk
1613 ;; cannot be opened.
1614 (setq can-be-opened nil)))
1615 (setq overlays (cdr overlays)))
1616 (if can-be-opened
1617 ;; It makes sense to append to the open
1618 ;; overlays list only if we know that this is
1619 ;; t.
1620 (setq crt-overlays (append ov-list crt-overlays))))
1621 (goto-char (next-overlay-change (point))))))
86bfaffe 1622 ;; See if invisibility reaches up thru END.
0352b205
RS
1623 (if (>= (point) end)
1624 (if (and (not (null can-be-opened)) (consp crt-overlays))
1625 (progn
1626 (setq isearch-opened-overlays
1627 (append isearch-opened-overlays crt-overlays))
1628 ;; maybe use a cycle instead of mapcar?
1629 (mapcar (function isearch-open-overlay-temporary)
1630 crt-overlays)
1631 nil)
1632 t))))))
08200510
RS
1633
1634\f
08200510
RS
1635;;; Highlighting
1636
b78559b0 1637(defvar isearch-overlay nil)
08200510 1638
b78559b0 1639(defun isearch-highlight (beg end)
f5f6a944 1640 (if (or (null search-highlight) (null window-system))
08200510 1641 nil
b78559b0
RS
1642 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1643 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1644 (overlay-put isearch-overlay 'face
1645 (if (internal-find-face 'isearch nil)
1646 'isearch 'region))))
b78559b0
RS
1647
1648(defun isearch-dehighlight (totally)
1649 (if isearch-overlay
1650 (delete-overlay isearch-overlay)))
08200510 1651
08200510
RS
1652;;; General utilities
1653
08200510 1654
b78559b0
RS
1655(defun isearch-no-upper-case-p (string regexp-flag)
1656 "Return t if there are no upper case chars in STRING.
b7852303 1657If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1658since they have special meaning in a regexp."
59057198
RS
1659 (let (quote-flag (i 0) (len (length string)) found)
1660 (while (and (not found) (< i len))
1661 (let ((char (aref string i)))
1662 (if (and regexp-flag (eq char ?\\))
1663 (setq quote-flag (not quote-flag))
1664 (if (and (not quote-flag) (not (eq char (downcase char))))
1665 (setq found t))))
1666 (setq i (1+ i)))
1667 (not found)))
08200510 1668
b78559b0 1669;; Portability functions to support various Emacs versions.
8e1cae6d 1670
08200510 1671(defun isearch-char-to-string (c)
0cabad13 1672 (make-string 1 c))
08200510
RS
1673
1674(defun isearch-text-char-description (c)
9d78ff8b
RS
1675 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1676 (text-char-description c)
1677 (isearch-char-to-string c)))
08200510 1678
bd1bd125 1679;; General function to unread characters or events.
99ae9e9f 1680;; Also insert them in a keyboard macro being defined.
8f90f594 1681(defun isearch-unread (&rest char-or-events)
99ae9e9f 1682 (mapcar 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1683 (setq unread-command-events
1684 (append char-or-events unread-command-events)))
08200510
RS
1685
1686(defun isearch-last-command-char ()
1687 ;; General function to return the last command character.
58451992 1688 last-command-char)
08200510 1689
3b1e4dd1 1690;;; isearch.el ends here