(read_minibuf): New arg disable_multibyte.
[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)
79c7a4fa 173 (const :tag "Don't match hidden text" nil))
0352b205
RS
174 :group 'isearch)
175
176(defcustom isearch-hide-immediately t
68c18d6d
RS
177 "If non-nil, re-hide an invisible match right away.
178This variable makes a difference when `search-invisible' is set to `open'.
179It means that after search makes some invisible text visible
180to show the match, it makes the text invisible again when the match moves.
181Ordinarily the text becomes invisible again at the end of the search."
0352b205
RS
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
08200510
RS
372;;; A function to be called after each input character is processed.
373;;; (It is not called after characters that exit the search.)
374;;; It is only set from an optional argument to `isearch-mode'.
375(defvar isearch-op-fun nil)
8e1cae6d 376
08200510
RS
377;;; Is isearch-mode in a recursive edit for modal searching.
378(defvar isearch-recursive-edit nil)
8e1cae6d 379
08200510
RS
380;;; Should isearch be terminated after doing one search?
381(defvar isearch-nonincremental nil)
382
383;; New value of isearch-forward after isearch-edit-string.
384(defvar isearch-new-forward nil)
8e1cae6d 385
0352b205
RS
386;; Accumulate here the overlays opened during searching.
387(defvar isearch-opened-overlays nil)
8e1cae6d 388
8e1cae6d
JB
389;; Minor-mode-alist changes - kind of redundant with the
390;; echo area, but if isearching in multiple windows, it can be useful.
391
392(or (assq 'isearch-mode minor-mode-alist)
393 (nconc minor-mode-alist
394 (list '(isearch-mode isearch-mode))))
395
08200510 396(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
397(make-variable-buffer-local 'isearch-mode)
398
fdf5afa4
RS
399(define-key global-map "\C-s" 'isearch-forward)
400(define-key esc-map "\C-s" 'isearch-forward-regexp)
401(define-key global-map "\C-r" 'isearch-backward)
402(define-key esc-map "\C-r" 'isearch-backward-regexp)
403
8e1cae6d 404;;; Entry points to isearch-mode.
08200510 405;;; These four functions should replace those in loaddefs.el
b457cc3b 406;;; An alternative is to defalias isearch-forward etc to isearch-mode,
08200510 407;;; and look at this-command to set the options accordingly.
8e1cae6d 408
eceee2c0 409(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
410 "\
411Do incremental search forward.
08200510
RS
412With a prefix argument, do an incremental regular expression search instead.
413\\<isearch-mode-map>
8e1cae6d 414As you type characters, they add to the search string and are found.
08200510 415The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 416
08200510 417Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 418Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
419Type LFD (C-j) to match end of line.
420Type \\[isearch-repeat-forward] to search again forward,\
421 \\[isearch-repeat-backward] to search again backward.
422Type \\[isearch-yank-word] to yank word from buffer onto end of search\
423 string and search for it.
424Type \\[isearch-yank-line] to yank rest of line onto end of search string\
425 and search for it.
de40ed89 426Type \\[isearch-yank-kill] to yank the last string of killed text.
8e1cae6d 427Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
428\\[isearch-abort] while searching or when search has failed cancels input\
429 back to what has
430 been found successfully.
431\\[isearch-abort] when search is successful aborts and moves point to\
432 starting point.
8e1cae6d
JB
433
434Also supported is a search ring of the previous 16 search strings.
435Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
436Type \\[isearch-ring-retreat] to search for the previous item in the search\
437 ring.
438Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 439
08200510
RS
440The above keys, bound in `isearch-mode-map', are often controlled by
441 options; do M-x apropos on search-.* to find them.
8e1cae6d 442Other control and meta characters terminate the search
08200510 443 and are then executed normally (depending on `search-exit-option').
fd49e05c 444Likewise for function keys and mouse button events.
8e1cae6d 445
08200510
RS
446If this function is called non-interactively, it does not return to
447the calling function until the search is done."
8e1cae6d 448
eceee2c0 449 (interactive "P\np")
4ae7d00a 450 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 451
eceee2c0 452(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
453 "\
454Do incremental search forward for regular expression.
08200510 455With a prefix argument, do a regular string search instead.
8e1cae6d
JB
456Like ordinary incremental search except that your input
457is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 458 (interactive "P\np")
4ae7d00a 459 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 460
eceee2c0 461(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
462 "\
463Do incremental search backward.
08200510 464With a prefix argument, do a regular expression search instead.
8e1cae6d 465See \\[isearch-forward] for more information."
eceee2c0 466 (interactive "P\np")
4ae7d00a 467 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 468
eceee2c0 469(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
470 "\
471Do incremental search backward for regular expression.
08200510 472With a prefix argument, do a regular string search instead.
8e1cae6d
JB
473Like ordinary incremental search except that your input
474is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 475 (interactive "P\np")
4ae7d00a 476 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
477
478
479(defun isearch-mode-help ()
480 (interactive)
481 (describe-function 'isearch-forward)
482 (isearch-update))
8e1cae6d
JB
483
484\f
8e1cae6d
JB
485;; isearch-mode only sets up incremental search for the minor mode.
486;; All the work is done by the isearch-mode commands.
487
08200510 488;; Not used yet:
d7fa5aa2 489;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
490;; isearch-forward-regexp isearch-backward-regexp)
491;; "List of commands for which isearch-mode does not recursive-edit.")
492
493
494(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
495 "Start isearch minor mode. Called by `isearch-forward', etc.
496
497\\{isearch-mode-map}"
8e1cae6d
JB
498
499 ;; Initialize global vars.
500 (setq isearch-forward forward
501 isearch-regexp regexp
08200510 502 isearch-word word-p
8e1cae6d
JB
503 isearch-op-fun op-fun
504 isearch-case-fold-search case-fold-search
505 isearch-string ""
506 isearch-message ""
507 isearch-cmds nil
508 isearch-success t
509 isearch-wrapped nil
510 isearch-barrier (point)
511 isearch-adjusted nil
512 isearch-yank-flag nil
513 isearch-invalid-regexp nil
c5f6b356 514 isearch-within-brackets nil
0cabad13 515 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d
JB
516 (> (window-height)
517 (* 4 search-slow-window-lines)))
518 isearch-other-end nil
519 isearch-small-window nil
99ae9e9f 520 isearch-just-started t
8e1cae6d 521
8e1cae6d 522 isearch-opoint (point)
a5dcf63f 523 search-ring-yank-pointer nil
0352b205 524 isearch-opened-overlays nil
a5dcf63f 525 regexp-search-ring-yank-pointer nil)
99ae9e9f 526 (looking-at "")
292a8dff
RS
527 (setq isearch-window-configuration
528 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 529
18ce7555
RS
530 ;; Maybe make minibuffer frame visible and/or raise it.
531 (let ((frame (window-frame (minibuffer-window))))
532 (if (not (memq (frame-live-p frame) '(nil t)))
533 (progn
534 (make-frame-visible frame)
535 (if minibuffer-auto-raise
536 (raise-frame frame)))))
537
8e1cae6d 538 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 539 (force-mode-line-update)
8e1cae6d
JB
540
541 (isearch-push-state)
542
c7955222 543 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
544 (isearch-update)
545 (run-hooks 'isearch-mode-hook)
08200510 546
bfe2d334 547 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
6e5cd0ae 548
08200510
RS
549 ;; isearch-mode can be made modal (in the sense of not returning to
550 ;; the calling function until searching is completed) by entering
551 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
552 (if recursive-edit
553 (let ((isearch-recursive-edit t))
554 (recursive-edit)))
0daa17f3 555 isearch-success)
8e1cae6d
JB
556
557
8e1cae6d
JB
558;; Some high level utilities. Others below.
559
560(defun isearch-update ()
561 ;; Called after each command to update the display.
58451992 562 (if (null unread-command-events)
8e1cae6d
JB
563 (progn
564 (if (not (input-pending-p))
565 (isearch-message))
566 (if (and isearch-slow-terminal-mode
567 (not (or isearch-small-window
568 (pos-visible-in-window-p))))
08200510 569 (let ((found-point (point)))
8e1cae6d 570 (setq isearch-small-window t)
8e1cae6d
JB
571 (move-to-window-line 0)
572 (let ((window-min-height 1))
573 (split-window nil (if (< search-slow-window-lines 0)
574 (1+ (- search-slow-window-lines))
575 (- (window-height)
576 (1+ search-slow-window-lines)))))
577 (if (< search-slow-window-lines 0)
578 (progn (vertical-motion (- 1 search-slow-window-lines))
579 (set-window-start (next-window) (point))
580 (set-window-hscroll (next-window)
581 (window-hscroll))
582 (set-window-hscroll (selected-window) 0))
583 (other-window 1))
08200510
RS
584 (goto-char found-point)))
585 (if isearch-other-end
586 (if (< isearch-other-end (point)) ; isearch-forward?
587 (isearch-highlight isearch-other-end (point))
99afb671
RS
588 (isearch-highlight (point) isearch-other-end))
589 (isearch-dehighlight nil))
08200510 590 ))
8e1cae6d
JB
591 (setq ;; quit-flag nil not for isearch-mode
592 isearch-adjusted nil
593 isearch-yank-flag nil)
594 )
595
0daa17f3 596(defun isearch-done (&optional nopush edit)
bfe2d334 597 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
8e1cae6d 598 ;; Called by all commands that terminate isearch-mode.
35a4d143 599 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 600 (setq overriding-terminal-local-map nil)
08200510
RS
601 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
602 (isearch-dehighlight t)
603 (let ((found-start (window-start (selected-window)))
604 (found-point (point)))
70418205
RS
605 (if isearch-window-configuration
606 (set-window-configuration isearch-window-configuration))
08200510 607
d49b6338
RS
608 (if isearch-small-window
609 (goto-char found-point)
610 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 611 (set-window-start (selected-window) found-start t))
d49b6338 612
08200510
RS
613 ;; If there was movement, mark the starting position.
614 ;; Maybe should test difference between and set mark iff > threshold.
615 (if (/= (point) isearch-opoint)
9821535a
RS
616 (or (and transient-mark-mode mark-active)
617 (progn
618 (push-mark isearch-opoint t)
f3acf6f5 619 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 620 (message "Mark saved where search started"))))))
08200510
RS
621
622 (setq isearch-mode nil)
1cd3732c 623 (force-mode-line-update)
8e1cae6d 624
35a4d143 625 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 626 ;; Update the ring data.
73d2bf95 627 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 628
8e1cae6d 629 (run-hooks 'isearch-mode-end-hook)
0daa17f3 630 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 631
73d2bf95
RS
632(defun isearch-update-ring (string &optional regexp)
633 "Add STRING to the beginning of the search ring.
634REGEXP says which ring to use."
635 (if regexp
636 (if (or (null regexp-search-ring)
fd89878d 637 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
638 (progn
639 (setq regexp-search-ring
fd89878d 640 (cons string regexp-search-ring))
73d2bf95
RS
641 (if (> (length regexp-search-ring) regexp-search-ring-max)
642 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
643 nil))))
644 (if (or (null search-ring)
fd89878d 645 (not (string= string (car search-ring))))
73d2bf95 646 (progn
fd89878d 647 (setq search-ring (cons string search-ring))
73d2bf95
RS
648 (if (> (length search-ring) search-ring-max)
649 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
650
08200510 651;;; Switching buffers should first terminate isearch-mode.
eb8c3be9 652;;; This is done quite differently for each variant of emacs.
08200510
RS
653;;; For lemacs, see Exiting in lemacs below
654
655;; For Emacs 19, the frame switch event is handled.
656(defun isearch-switch-frame-handler ()
657 (interactive) ;; Is this necessary?
658 ;; First terminate isearch-mode.
659 (isearch-done)
0352b205 660 (isearch-clean-overlays)
dba1ec55 661 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510 662
8e1cae6d 663\f
8e1cae6d
JB
664;; Commands active while inside of the isearch minor mode.
665
666(defun isearch-exit ()
667 "Exit search normally.
668However, if this is the first command after starting incremental
669search and `search-nonincremental-instead' is non-nil, do a
08200510 670nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
671 (interactive)
672 (if (and search-nonincremental-instead
673 (= 0 (length isearch-string)))
08200510
RS
674 (let ((isearch-nonincremental t))
675 (isearch-edit-string)))
0352b205
RS
676 (isearch-done)
677 (isearch-clean-overlays))
8e1cae6d
JB
678
679
680(defun isearch-edit-string ()
08200510
RS
681 "Edit the search string in the minibuffer.
682The following additional command keys are active while editing.
683\\<minibuffer-local-isearch-map>
684\\[exit-minibuffer] to resume incremental searching with the edited string.
685\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
686\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 687\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 688\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 689\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 690\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 691\\<isearch-mode-map>
08200510
RS
692If first char entered is \\[isearch-yank-word], then do word search instead."
693
694 ;; This code is very hairy for several reasons, explained in the code.
695 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
696 ;; If there were a way to catch any change of buffer from the minibuffer,
697 ;; this could be simplified greatly.
eb8c3be9 698 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 699 (interactive)
08200510 700 (condition-case err
e900ced4
RS
701 (progn
702 (let ((isearch-nonincremental isearch-nonincremental)
703
704 ;; Locally bind all isearch global variables to protect them
705 ;; from recursive isearching.
706 ;; isearch-string -message and -forward are not bound
707 ;; so they may be changed. Instead, save the values.
708 (isearch-new-string isearch-string)
709 (isearch-new-message isearch-message)
710 (isearch-new-forward isearch-forward)
711 (isearch-new-word isearch-word)
712
713 (isearch-regexp isearch-regexp)
714 (isearch-op-fun isearch-op-fun)
715 (isearch-cmds isearch-cmds)
716 (isearch-success isearch-success)
717 (isearch-wrapped isearch-wrapped)
718 (isearch-barrier isearch-barrier)
719 (isearch-adjusted isearch-adjusted)
720 (isearch-yank-flag isearch-yank-flag)
721 (isearch-invalid-regexp isearch-invalid-regexp)
722 (isearch-within-brackets isearch-within-brackets)
723 ;;; Don't bind this. We want isearch-search, below, to set it.
724 ;;; And the old value won't matter after that.
725 ;;; (isearch-other-end isearch-other-end)
726 ;;; Perhaps some of these other variables should be bound for a
727 ;;; shorter period, ending before the next isearch-search.
728 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
729 (isearch-opoint isearch-opoint)
730 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
731 (isearch-small-window isearch-small-window)
732 (isearch-recursive-edit isearch-recursive-edit)
733 ;; Save current configuration so we can restore it here.
734 (isearch-window-configuration (current-window-configuration))
735 )
736
737 ;; Actually terminate isearching until editing is done.
738 ;; This is so that the user can do anything without failure,
739 ;; like switch buffers and start another isearch, and return.
740 (condition-case err
741 (isearch-done t t)
742 (exit nil)) ; was recursive editing
743
744 (isearch-message) ;; for read-char
745 (unwind-protect
746 (let* (;; Why does following read-char echo?
747 ;;(echo-keystrokes 0) ;; not needed with above message
748 (e (let ((cursor-in-echo-area t))
749 (read-event)))
750 ;; Binding minibuffer-history-symbol to nil is a work-around
751 ;; for some incompatibility with gmhist.
752 (minibuffer-history-symbol)
753 (message-log-max nil))
754 ;; If the first character the user types when we prompt them
755 ;; for a string is the yank-word character, then go into
756 ;; word-search mode. Otherwise unread that character and
757 ;; read a key the normal way.
758 ;; Word search does not apply (yet) to regexp searches,
759 ;; no check is made here.
760 (message (isearch-message-prefix nil nil t))
761 (if (eq 'isearch-yank-word
762 (lookup-key isearch-mode-map (vector e)))
763 (setq isearch-word t;; so message-prefix is right
764 isearch-new-word t)
765 (cancel-kbd-macro-events)
766 (isearch-unread e))
767 (setq cursor-in-echo-area nil)
768 (setq isearch-new-string
769 (let (junk-ring)
770 (read-from-minibuffer
771 (isearch-message-prefix nil nil isearch-nonincremental)
772 isearch-string
773 minibuffer-local-isearch-map nil
774 'junk-ring))
775 isearch-new-message
776 (mapconcat 'isearch-text-char-description
777 isearch-new-string "")))
778 ;; Always resume isearching by restarting it.
779 (isearch-mode isearch-forward
780 isearch-regexp
781 isearch-op-fun
782 nil
783 isearch-word)
784
785 ;; Copy new local values to isearch globals
786 (setq isearch-string isearch-new-string
787 isearch-message isearch-new-message
788 isearch-forward isearch-new-forward
789 isearch-word isearch-new-word))
790
791 ;; Empty isearch-string means use default.
792 (if (= 0 (length isearch-string))
793 (setq isearch-string (or (car (if isearch-regexp
794 regexp-search-ring
795 search-ring))
796 ""))
797 ;; This used to set the last search string,
798 ;; but I think it is not right to do that here.
799 ;; Only the string actually used should be saved.
800 ))
801
802 ;; Push the state as of before this C-s.
803 (isearch-push-state)
08200510
RS
804
805 ;; Reinvoke the pending search.
08200510
RS
806 (isearch-search)
807 (isearch-update)
808 (if isearch-nonincremental
809 (progn
810 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
811 (isearch-done))))
8e1cae6d 812
08200510
RS
813 (quit ; handle abort-recursive-edit
814 (isearch-abort) ;; outside of let to restore outside global values
815 )))
816
817(defun isearch-nonincremental-exit-minibuffer ()
818 (interactive)
819 (setq isearch-nonincremental t)
820 (exit-minibuffer))
821
822(defun isearch-forward-exit-minibuffer ()
823 (interactive)
824 (setq isearch-new-forward t)
825 (exit-minibuffer))
8e1cae6d 826
08200510 827(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 828 (interactive)
08200510
RS
829 (setq isearch-new-forward nil)
830 (exit-minibuffer))
831
19993c54
RS
832(defun isearch-cancel ()
833 "Terminate the search and go back to the starting point."
834 (interactive)
835 (goto-char isearch-opoint)
836 (isearch-done t)
0352b205 837 (isearch-clean-overlays)
19993c54 838 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
839
840(defun isearch-abort ()
b7852303 841 "Abort incremental search mode if searching is successful, signaling quit.
08200510 842Otherwise, revert to previous successful search and continue searching.
b7852303 843Use `isearch-exit' to quit without signaling."
08200510 844 (interactive)
eb8c3be9 845;; (ding) signal instead below, if quitting
8e1cae6d
JB
846 (discard-input)
847 (if isearch-success
848 ;; If search is successful, move back to starting point
849 ;; and really do quit.
850 (progn (goto-char isearch-opoint)
509ed182 851 (setq isearch-success nil)
35a4d143 852 (isearch-done t) ; exit isearch
0352b205 853 (isearch-clean-overlays)
a5dcf63f 854 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
855 ;; If search is failing, or has an incomplete regexp,
856 ;; rub out until it is once more successful.
857 (while (or (not isearch-success) isearch-invalid-regexp)
858 (isearch-pop-state))
8e1cae6d
JB
859 (isearch-update)))
860
8e1cae6d
JB
861(defun isearch-repeat (direction)
862 ;; Utility for isearch-repeat-forward and -backward.
863 (if (eq isearch-forward (eq direction 'forward))
864 ;; C-s in forward or C-r in reverse.
865 (if (equal isearch-string "")
866 ;; If search string is empty, use last one.
867 (setq isearch-string
8e1cae6d 868 (or (if isearch-regexp
a5dcf63f
RS
869 (car regexp-search-ring)
870 (car search-ring))
8e1cae6d
JB
871 "")
872 isearch-message
08200510 873 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
874 isearch-string ""))
875 ;; If already have what to search for, repeat it.
876 (or isearch-success
877 (progn
8e1cae6d
JB
878 (goto-char (if isearch-forward (point-min) (point-max)))
879 (setq isearch-wrapped t))))
880 ;; C-s in reverse or C-r in forward, change direction.
881 (setq isearch-forward (not isearch-forward)))
882
883 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
884
885 (if (equal isearch-string "")
886 (setq isearch-success t)
99ae9e9f
KH
887 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
888 (not isearch-just-started))
8e1cae6d
JB
889 ;; If repeating a search that found
890 ;; an empty string, ensure we advance.
6fefdc4b
RS
891 (if (if isearch-forward (eobp) (bobp))
892 ;; If there's nowhere to advance to, fail (and wrap next time).
893 (progn
894 (setq isearch-success nil)
895 (ding))
896 (forward-char (if isearch-forward 1 -1))
897 (isearch-search))
898 (isearch-search)))
899
8e1cae6d
JB
900 (isearch-push-state)
901 (isearch-update))
902
903(defun isearch-repeat-forward ()
904 "Repeat incremental search forwards."
905 (interactive)
906 (isearch-repeat 'forward))
907
908(defun isearch-repeat-backward ()
909 "Repeat incremental search backwards."
910 (interactive)
911 (isearch-repeat 'backward))
912
913(defun isearch-toggle-regexp ()
914 "Toggle regexp searching on or off."
915 ;; The status stack is left unchanged.
916 (interactive)
917 (setq isearch-regexp (not isearch-regexp))
08200510 918 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
919 (isearch-update))
920
4453091d
RS
921(defun isearch-toggle-case-fold ()
922 "Toggle case folding in searching on or off."
923 (interactive)
924 (setq isearch-case-fold-search
925 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
926 (let ((message-log-max nil))
927 (message "%s%s [case %ssensitive]"
928 (isearch-message-prefix nil nil isearch-nonincremental)
929 isearch-message
930 (if isearch-case-fold-search "in" "")))
4453091d
RS
931 (setq isearch-adjusted t)
932 (sit-for 1)
933 (isearch-update))
934
8e1cae6d
JB
935(defun isearch-delete-char ()
936 "Discard last input item and move point back.
937If no previous match was done, just beep."
938 (interactive)
939 (if (null (cdr isearch-cmds))
940 (ding)
941 (isearch-pop-state))
942 (isearch-update))
943
944
945(defun isearch-yank (chunk)
946 ;; Helper for isearch-yank-word and isearch-yank-line
fd543be5 947 ;; CHUNK should be word, line, kill, or x-sel.
30d47262
RS
948 (let ((string (cond
949 ((eq chunk 'kill)
950 (current-kill 0))
fd543be5
KH
951 ((eq chunk 'x-sel)
952 (x-get-selection))
30d47262
RS
953 (t
954 (save-excursion
955 (and (not isearch-forward) isearch-other-end
956 (goto-char isearch-other-end))
957 (buffer-substring
958 (point)
959 (save-excursion
960 (cond
961 ((eq chunk 'word)
962 (forward-word 1))
963 ((eq chunk 'line)
964 (end-of-line)))
965 (point))))))))
08200510
RS
966 ;; Downcase the string if not supposed to case-fold yanked strings.
967 (if (and isearch-case-fold-search
fdf5afa4 968 (eq 'not-yanks search-upper-case))
08200510
RS
969 (setq string (downcase string)))
970 (if isearch-regexp (setq string (regexp-quote string)))
971 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
972 isearch-message
973 (concat isearch-message
08200510
RS
974 (mapconcat 'isearch-text-char-description
975 string ""))
8e1cae6d
JB
976 ;; Don't move cursor in reverse search.
977 isearch-yank-flag t))
978 (isearch-search-and-update))
979
30d47262
RS
980(defun isearch-yank-kill ()
981 "Pull string from kill ring into search string."
982 (interactive)
983 (isearch-yank 'kill))
8e1cae6d
JB
984
985(defun isearch-yank-word ()
986 "Pull next word from buffer into search string."
987 (interactive)
988 (isearch-yank 'word))
989
990(defun isearch-yank-line ()
991 "Pull rest of line from buffer into search string."
992 (interactive)
993 (isearch-yank 'line))
994
995
996(defun isearch-search-and-update ()
997 ;; Do the search and update the display.
998 (if (and (not isearch-success)
999 ;; unsuccessful regexp search may become
1000 ;; successful by addition of characters which
1001 ;; make isearch-string valid
1002 (not isearch-regexp))
1003 nil
1004 ;; In reverse search, adding stuff at
1005 ;; the end may cause zero or many more chars to be
1006 ;; matched, in the string following point.
1007 ;; Allow all those possibilities without moving point as
1008 ;; long as the match does not extend past search origin.
1009 (if (and (not isearch-forward) (not isearch-adjusted)
1010 (condition-case ()
99ae9e9f
KH
1011 (let ((case-fold-search isearch-case-fold-search))
1012 (looking-at (if isearch-regexp isearch-string
1013 (regexp-quote isearch-string))))
8e1cae6d
JB
1014 (error nil))
1015 (or isearch-yank-flag
1016 (<= (match-end 0)
1017 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1018 (progn
1019 (setq isearch-success t
1020 isearch-invalid-regexp nil
1021 isearch-within-brackets nil
1022 isearch-other-end (match-end 0))
1023 (if (and (eq isearch-case-fold-search t) search-upper-case)
1024 (setq isearch-case-fold-search
1025 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1026 ;; Not regexp, not reverse, or no match at point.
1027 (if (and isearch-other-end (not isearch-adjusted))
1028 (goto-char (if isearch-forward isearch-other-end
1029 (min isearch-opoint
1030 isearch-barrier
1031 (1+ isearch-other-end)))))
1032 (isearch-search)
1033 ))
1034 (isearch-push-state)
1035 (if isearch-op-fun (funcall isearch-op-fun))
1036 (isearch-update))
1037
1038
1039;; *, ?, and | chars can make a regexp more liberal.
08200510 1040;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1041;; So go back to place last successful search started
1042;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1043;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1044
1045(defun isearch-*-char ()
1046 "Handle * and ? specially in regexps."
1047 (interactive)
1048 (if isearch-regexp
1049
1050 (progn
1051 (setq isearch-adjusted t)
149d4fe5
RS
1052 ;; Get the isearch-other-end from before the last search.
1053 ;; We want to start from there,
1054 ;; so that we don't retreat farther than that.
1055 ;; (car isearch-cmds) is after last search;
1056 ;; (car (cdr isearch-cmds)) is from before it.
1057 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
8e1cae6d
JB
1058 (setq cs (or cs isearch-barrier))
1059 (goto-char
1060 (if isearch-forward
1061 (max cs isearch-barrier)
1062 (min cs isearch-barrier))))))
08200510 1063 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1064
1065
8e1cae6d
JB
1066(defun isearch-|-char ()
1067 "If in regexp search, jump to the barrier."
1068 (interactive)
1069 (if isearch-regexp
1070 (progn
1071 (setq isearch-adjusted t)
1072 (goto-char isearch-barrier)))
08200510 1073 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1074
1075
b457cc3b 1076(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1077
1078(defun isearch-other-meta-char ()
8f90f594 1079 "Exit the search normally and reread this key sequence.
35a4d143
RS
1080But only if `search-exit-option' is non-nil, the default.
1081If it is the symbol `edit', the search string is edited in the minibuffer
1082and the meta character is unread so that it applies to editing the string."
8e1cae6d 1083 (interactive)
c6431f12
KH
1084 (let* ((key (this-command-keys))
1085 (main-event (aref key 0))
1086 (keylist (listify-key-sequence key)))
24b5caec 1087 (cond ((and (= (length key) 1)
d94eb6eb 1088 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1089 (not (or (null lookup) (integerp lookup)
1090 (keymapp lookup)))))
24b5caec
RS
1091 ;; Handle a function key that translates into something else.
1092 ;; If the key has a global definition too,
1093 ;; exit and unread the key itself, so its global definition runs.
1094 ;; Otherwise, unread the translation,
1095 ;; so that the translated key takes effect within isearch.
d94eb6eb 1096 (cancel-kbd-macro-events)
24b5caec
RS
1097 (if (lookup-key global-map key)
1098 (progn
1099 (isearch-done)
1100 (apply 'isearch-unread keylist))
1101 (apply 'isearch-unread
1102 (listify-key-sequence (lookup-key function-key-map key)))))
1103 (
c6431f12
KH
1104 ;; Handle an undefined shifted control character
1105 ;; by downshifting it if that makes it defined.
1106 ;; (As read-key-sequence would normally do,
1107 ;; if we didn't have a default definition.)
1108 (let ((mods (event-modifiers main-event)))
1109 (and (integerp main-event)
1110 (memq 'shift mods)
1111 (memq 'control mods)
1112 (lookup-key isearch-mode-map
1113 (let ((copy (copy-sequence key)))
1114 (aset copy 0
1115 (- main-event (- ?\C-\S-a ?\C-a)))
1116 copy)
1117 nil)))
1118 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1119 (cancel-kbd-macro-events)
c6431f12
KH
1120 (apply 'isearch-unread keylist))
1121 ((eq search-exit-option 'edit)
1122 (apply 'isearch-unread keylist)
1123 (isearch-edit-string))
1124 (search-exit-option
1125 (let (window)
d94eb6eb 1126 (cancel-kbd-macro-events)
c6431f12
KH
1127 (apply 'isearch-unread keylist)
1128 ;; Properly handle scroll-bar and mode-line clicks
1129 ;; for which a dummy prefix event was generated as (aref key 0).
1130 (and (> (length key) 1)
1131 (symbolp (aref key 0))
1132 (listp (aref key 1))
1133 (not (numberp (posn-point (event-start (aref key 1)))))
1134 ;; Convert the event back into its raw form,
1135 ;; with the dummy prefix implicit in the mouse event,
1136 ;; so it will get split up once again.
1137 (progn (setq unread-command-events
1138 (cdr unread-command-events))
1139 (setq main-event (car unread-command-events))
1140 (setcar (cdr (event-start main-event))
1141 (car (nth 1 (event-start main-event))))))
1142 ;; If we got a mouse click, maybe it was read with the buffer
1143 ;; it was clicked on. If so, that buffer, not the current one,
1144 ;; is in isearch mode. So end the search in that buffer.
1145 (if (and (listp main-event)
1146 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1147 (windowp window)
1148 (or (> (minibuffer-depth) 0)
1149 (not (window-minibuffer-p window))))
c6431f12
KH
1150 (save-excursion
1151 (set-buffer (window-buffer window))
0352b205
RS
1152 (isearch-done)
1153 (isearch-clean-overlays))
1154 (isearch-done)
1155 (isearch-clean-overlays))))
c6431f12
KH
1156 (t;; otherwise nil
1157 (isearch-process-search-string key key)))))
8e1cae6d 1158
8e1cae6d
JB
1159(defun isearch-quote-char ()
1160 "Quote special characters for incremental search."
1161 (interactive)
8bc15fa8
RS
1162 (let ((char (read-quoted-char (isearch-message t))))
1163 ;; Assume character codes 0200 - 0377 stand for
1164 ;; European characters in Latin-1, and convert them
1165 ;; to Emacs characters.
1166 (and enable-multibyte-characters
1167 (>= char ?\200)
1168 (<= char ?\377)
1169 (setq char (+ char nonascii-insert-offset)))
1170 (isearch-process-search-char char)))
8e1cae6d 1171
8e1cae6d
JB
1172(defun isearch-return-char ()
1173 "Convert return into newline for incremental search.
1174Obsolete."
1175 (interactive)
1176 (isearch-process-search-char ?\n))
1177
8e1cae6d 1178(defun isearch-printing-char ()
b68c164d 1179 "Add this ordinary printing character to the search string and search."
8e1cae6d 1180 (interactive)
5b56d4e4 1181 (if current-input-method
f46d5091
KH
1182 (isearch-process-search-multibyte-characters (isearch-last-command-char))
1183 (isearch-process-search-char (isearch-last-command-char))))
8e1cae6d
JB
1184
1185(defun isearch-whitespace-chars ()
08200510 1186 "Match all whitespace chars, if in regexp mode.
b68c164d 1187If you want to search for just a space, type C-q SPC."
8e1cae6d 1188 (interactive)
08200510 1189 (if isearch-regexp
9da6682f
RS
1190 (if (and search-whitespace-regexp (not isearch-within-brackets)
1191 (not isearch-invalid-regexp))
08200510
RS
1192 (isearch-process-search-string search-whitespace-regexp " ")
1193 (isearch-printing-char))
1194 (progn
eb8c3be9 1195 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1196 ;; (setq isearch-word t)
1197 ;; (setq isearch-adjusted t)
1198 ;; (goto-char isearch-barrier)
1199 (isearch-printing-char))))
8e1cae6d
JB
1200
1201(defun isearch-process-search-char (char)
1202 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1203 (isearch-process-search-string
1204 (isearch-char-to-string char)
01a6ef79
RS
1205 (if (>= char 0200)
1206 (char-to-string char)
1207 (isearch-text-char-description char))))
8e1cae6d
JB
1208
1209(defun isearch-process-search-string (string message)
1210 (setq isearch-string (concat isearch-string string)
1211 isearch-message (concat isearch-message message))
1212 (isearch-search-and-update))
1213
1214\f
8e1cae6d
JB
1215;; Search Ring
1216
08200510
RS
1217(defun isearch-ring-adjust1 (advance)
1218 ;; Helper for isearch-ring-adjust
1219 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1220 (length (length ring))
1221 (yank-pointer-name (if isearch-regexp
08200510 1222 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1223 'search-ring-yank-pointer))
1224 (yank-pointer (eval yank-pointer-name)))
1225 (if (zerop length)
1226 ()
1227 (set yank-pointer-name
1228 (setq yank-pointer
ffbc30b2
PE
1229 (mod (+ (or yank-pointer 0)
1230 (if advance -1 1))
1231 length)))
a5dcf63f 1232 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1233 isearch-message (mapconcat 'isearch-text-char-description
1234 isearch-string "")))))
1235
1236(defun isearch-ring-adjust (advance)
1237 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1238 (isearch-ring-adjust1 advance)
08200510
RS
1239 (if search-ring-update
1240 (progn
1241 (isearch-search)
1242 (isearch-update))
1243 (isearch-edit-string)
2ff20b7e
RS
1244 )
1245 (isearch-push-state))
8e1cae6d
JB
1246
1247(defun isearch-ring-advance ()
1248 "Advance to the next search string in the ring."
08200510 1249 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1250 (interactive)
1251 (isearch-ring-adjust 'advance))
1252
1253(defun isearch-ring-retreat ()
1254 "Retreat to the previous search string in the ring."
1255 (interactive)
1256 (isearch-ring-adjust nil))
1257
a5dcf63f
RS
1258(defun isearch-ring-advance-edit (n)
1259 "Insert the next element of the search history into the minibuffer."
1260 (interactive "p")
1261 (let* ((yank-pointer-name (if isearch-regexp
1262 'regexp-search-ring-yank-pointer
1263 'search-ring-yank-pointer))
1264 (yank-pointer (eval yank-pointer-name))
1265 (ring (if isearch-regexp regexp-search-ring search-ring))
1266 (length (length ring)))
1267 (if (zerop length)
1268 ()
1269 (set yank-pointer-name
1270 (setq yank-pointer
ffbc30b2
PE
1271 (mod (- (or yank-pointer 0) n)
1272 length)))
a5dcf63f 1273
a5dcf63f 1274 (erase-buffer)
35a4d143 1275 (insert (nth yank-pointer ring))
49c13105 1276 (goto-char (point-max)))))
a5dcf63f
RS
1277
1278(defun isearch-ring-retreat-edit (n)
1279 "Inserts the previous element of the search history into the minibuffer."
1280 (interactive "p")
1281 (isearch-ring-advance-edit (- n)))
1282
1283;;(defun isearch-ring-adjust-edit (advance)
1284;; "Use the next or previous search string in the ring while in minibuffer."
1285;; (isearch-ring-adjust1 advance)
1286;; (erase-buffer)
1287;; (insert isearch-string))
1288
1289;;(defun isearch-ring-advance-edit ()
1290;; (interactive)
1291;; (isearch-ring-adjust-edit 'advance))
1292
1293;;(defun isearch-ring-retreat-edit ()
1294;; "Retreat to the previous search string in the ring while in the minibuffer."
1295;; (interactive)
1296;; (isearch-ring-adjust-edit nil))
08200510
RS
1297
1298
1299(defun isearch-complete1 ()
1300 ;; Helper for isearch-complete and isearch-complete-edit
1301 ;; Return t if completion OK, nil if no completion exists.
1302 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1303 (alist (mapcar (function (lambda (string) (list string))) ring))
1304 (completion-ignore-case case-fold-search)
1305 (completion (try-completion isearch-string alist)))
1306 (cond
1307 ((eq completion t)
1308 ;; isearch-string stays the same
1309 t)
1310 ((or completion ; not nil, must be a string
b7852303 1311 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1312 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1313 (progn
1314 (if completion-auto-help
1315 (with-output-to-temp-buffer "*Isearch completions*"
1316 (display-completion-list
1317 (all-completions isearch-string alist))))
1318 t)
1319 (and completion
1320 (setq isearch-string completion))))
08200510
RS
1321 (t
1322 (message "No completion") ; waits a second if in minibuffer
1323 nil))))
1324
1325(defun isearch-complete ()
1326 "Complete the search string from the strings on the search ring.
1327The completed string is then editable in the minibuffer.
1328If there is no completion possible, say so and continue searching."
1329 (interactive)
1330 (if (isearch-complete1)
1331 (isearch-edit-string)
1332 ;; else
1333 (sit-for 1)
1334 (isearch-update)))
8e1cae6d 1335
08200510
RS
1336(defun isearch-complete-edit ()
1337 "Same as `isearch-complete' except in the minibuffer."
1338 (interactive)
1339 (setq isearch-string (buffer-string))
1340 (if (isearch-complete1)
8e1cae6d 1341 (progn
08200510
RS
1342 (erase-buffer)
1343 (insert isearch-string))))
8e1cae6d
JB
1344
1345\f
8e1cae6d 1346;; The search status stack (and isearch window-local variables, not used).
08200510 1347;; Need a structure for this.
8e1cae6d
JB
1348
1349(defun isearch-top-state ()
8e1cae6d
JB
1350 (let ((cmd (car isearch-cmds)))
1351 (setq isearch-string (car cmd)
1352 isearch-message (car (cdr cmd))
1353 isearch-success (nth 3 cmd)
1354 isearch-forward (nth 4 cmd)
1355 isearch-other-end (nth 5 cmd)
08200510
RS
1356 isearch-word (nth 6 cmd)
1357 isearch-invalid-regexp (nth 7 cmd)
1358 isearch-wrapped (nth 8 cmd)
c5f6b356 1359 isearch-barrier (nth 9 cmd)
f551b97d
RS
1360 isearch-within-brackets (nth 10 cmd)
1361 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1362 (goto-char (car (cdr (cdr cmd))))))
1363
1364(defun isearch-pop-state ()
8e1cae6d
JB
1365 (setq isearch-cmds (cdr isearch-cmds))
1366 (isearch-top-state)
1367 )
1368
1369(defun isearch-push-state ()
1370 (setq isearch-cmds
1371 (cons (list isearch-string isearch-message (point)
1372 isearch-success isearch-forward isearch-other-end
08200510 1373 isearch-word
c5f6b356 1374 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1375 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1376 isearch-cmds)))
1377
8e1cae6d 1378\f
8e1cae6d
JB
1379;; Message string
1380
1381(defun isearch-message (&optional c-q-hack ellipsis)
1382 ;; Generate and print the message string.
1383 (let ((cursor-in-echo-area ellipsis)
1384 (m (concat
08200510 1385 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1386 isearch-message
1387 (isearch-message-suffix c-q-hack ellipsis)
1388 )))
a56687f1
KH
1389 (if c-q-hack
1390 m
1391 (let ((message-log-max nil))
1392 (message "%s" m)))))
8e1cae6d 1393
08200510 1394(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1395 ;; If about to search, and previous search regexp was invalid,
1396 ;; check that it still is. If it is valid now,
1397 ;; let the message we display while searching say that it is valid.
1398 (and isearch-invalid-regexp ellipsis
1399 (condition-case ()
1400 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1401 (setq isearch-invalid-regexp nil
1402 isearch-within-brackets nil))
8e1cae6d
JB
1403 (error nil)))
1404 ;; If currently failing, display no ellipsis.
1405 (or isearch-success (setq ellipsis nil))
1406 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1407 (if (and isearch-wrapped
1408 (if isearch-forward
1409 (> (point) isearch-opoint)
1410 (< (point) isearch-opoint)))
1411 "over")
8e1cae6d 1412 (if isearch-wrapped "wrapped ")
08200510 1413 (if isearch-word "word " "")
8e1cae6d 1414 (if isearch-regexp "regexp " "")
08200510 1415 (if nonincremental "search" "I-search")
f46d5091 1416 (if isearch-forward "" " backward")
5b56d4e4
KH
1417 (if current-input-method
1418 (concat " [" current-input-method-title "]: ")
f46d5091 1419 ": ")
8e1cae6d
JB
1420 )))
1421 (aset m 0 (upcase (aref m 0)))
1422 m))
1423
1424
1425(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1426 (concat (if c-q-hack "^Q" "")
1427 (if isearch-invalid-regexp
1428 (concat " [" isearch-invalid-regexp "]")
1429 "")))
1430
1431\f
8e1cae6d
JB
1432;;; Searching
1433
1434(defun isearch-search ()
1435 ;; Do the search with the current search string.
1436 (isearch-message nil t)
4453091d 1437 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1438 (setq isearch-case-fold-search
1439 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1440 (condition-case lossage
79c7a4fa
RS
1441 (let ((inhibit-point-motion-hooks search-invisible)
1442 (inhibit-quit nil)
86bfaffe
RS
1443 (case-fold-search isearch-case-fold-search)
1444 (retry t))
8e1cae6d 1445 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1446 (setq isearch-within-brackets nil)
86bfaffe
RS
1447 (while retry
1448 (setq isearch-success
1449 (funcall
1450 (cond (isearch-word
1451 (if isearch-forward
1452 'word-search-forward 'word-search-backward))
1453 (isearch-regexp
1454 (if isearch-forward
1455 're-search-forward 're-search-backward))
1456 (t
1457 (if isearch-forward 'search-forward 'search-backward)))
1458 isearch-string nil t))
1459 ;; Clear RETRY unless we matched some invisible text
1460 ;; and we aren't supposed to do that.
0352b205 1461 (if (or (eq search-invisible t)
86bfaffe
RS
1462 (not isearch-success)
1463 (bobp) (eobp)
1464 (= (match-beginning 0) (match-end 0))
1465 (not (isearch-range-invisible
1466 (match-beginning 0) (match-end 0))))
1467 (setq retry nil)))
99ae9e9f 1468 (setq isearch-just-started nil)
8e1cae6d
JB
1469 (if isearch-success
1470 (setq isearch-other-end
1471 (if isearch-forward (match-beginning 0) (match-end 0)))))
1472
d32696d4 1473 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1474 (setq isearch-success nil))
1475
1476 (invalid-regexp
1477 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1478 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1479 isearch-invalid-regexp))
8e1cae6d
JB
1480 (if (string-match
1481 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1482 isearch-invalid-regexp)
f1c03125
RS
1483 (setq isearch-invalid-regexp "incomplete input")))
1484 (error
1485 ;; stack overflow in regexp search.
1486 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1487
1488 (if isearch-success
1489 nil
1490 ;; Ding if failed this time after succeeding last time.
1491 (and (nth 3 (car isearch-cmds))
1492 (ding))
1493 (goto-char (nth 2 (car isearch-cmds)))))
1494
0352b205
RS
1495
1496;;; Called when opening an overlay, and we are still in isearch.
1497(defun isearch-open-overlay-temporary (ov)
0352b205
RS
1498 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1499 ;; Some modes would want to open the overlays temporary during
1500 ;; isearch in their own way, they should set the
1501 ;; `isearch-open-invisible-temporary' to a function doing this.
1502 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1503 ;; Store the values for the `invisible' and `intangible'
1504 ;; properties, and then set them to nil. This way the text hidden
1505 ;; by this overlay becomes visible.
1506
1507 ;; Do we realy need to set the `intangible' property to t? Can we
1508 ;; have the point inside an overlay with an `intangible' property?
1509 ;; In 19.34 this does not exist so I cannot test it.
1510 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1511 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1512 (overlay-put ov 'invisible nil)
1513 (overlay-put ov 'intangible nil)))
1514
1515
1516;;; This is called at the end of isearch. I will open the overlays
1517;;; that contain the latest match. Obviously in case of a C-g the
1518;;; point returns to the original location which surely is not contain
1519;;; in any of these overlays, se we are safe in this case too.
1520(defun isearch-open-necessary-overlays (ov)
1521 (let ((inside-overlay (and (> (point) (overlay-start ov))
1522 (< (point) (overlay-end ov))))
1523 ;; If this exists it means that the overlay was opened using
1524 ;; this function, not by us tweaking the overlay properties.
1525 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1526 (when (or inside-overlay (not fct-temp))
1527 ;; restore the values for the `invisible' and `intangible'
1528 ;; properties
1529 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1530 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1531 (overlay-put ov 'isearch-invisible nil)
1532 (overlay-put ov 'isearch-intangible nil))
1533 (if inside-overlay
1534 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1535 (if fct-temp
1536 (funcall fct-temp ov t)))))
1537
1538;;; This is called when exiting isearch. It closes the temporary
1539;;; opened overlays, except the ones that contain the latest match.
1540(defun isearch-clean-overlays ()
1541 (when isearch-opened-overlays
1542 ;; Use a cycle instead of a mapcar here?
1543 (mapcar
1544 (function isearch-open-necessary-overlays) isearch-opened-overlays)
1545 (setq isearch-opened-overlays nil)))
1546
1547;;; Verify if the current match is outside of each element of
1548;;; `isearch-opened-overlays', if so close that overlay.
1549(defun isearch-close-unecessary-overlays (begin end)
1550 (let ((ov-list isearch-opened-overlays)
1551 ov
1552 inside-overlay
1553 fct-temp)
1554 (setq isearch-opened-overlays nil)
1555 (while ov-list
1556 (setq ov (car ov-list))
1557 (setq ov-list (cdr ov-list))
1558 (setq inside-overlay (or (and (> begin (overlay-start ov))
1559 (< begin (overlay-end ov)))
1560 (and (> end (overlay-start ov))
1561 (< end (overlay-end ov)))))
1562 ;; If this exists it means that the overlay was opened using
1563 ;; this function, not by us tweaking the overlay properties.
1564 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1565 (if inside-overlay
1566 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1567 (if fct-temp
1568 (funcall fct-temp ov t)
1569 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1570 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1571 (overlay-put ov 'isearch-invisible nil)
1572 (overlay-put ov 'isearch-intangible nil))))))
1573
86bfaffe 1574(defun isearch-range-invisible (beg end)
79c7a4fa 1575 "Return t if all the text from BEG to END is invisible."
86bfaffe
RS
1576 (and (/= beg end)
1577 ;; Check that invisibility runs up to END.
1578 (save-excursion
1579 (goto-char beg)
0352b205
RS
1580 (let
1581 ;; can-be-opened keeps track if we can open some overlays.
1582 ((can-be-opened (eq search-invisible 'open))
1583 ;; the list of overlays that could be opened
1584 (crt-overlays nil))
1585 (when (and can-be-opened isearch-hide-immediately)
1586 (isearch-close-unecessary-overlays beg end))
1587 ;; If the following character is currently invisible,
1588 ;; skip all characters with that same `invisible' property value.
1589 ;; Do that over and over.
1590 (while (and (< (point) end)
1591 (let ((prop
1592 (get-char-property (point) 'invisible)))
1593 (if (eq buffer-invisibility-spec t)
1594 prop
1595 (or (memq prop buffer-invisibility-spec)
1596 (assq prop buffer-invisibility-spec)))))
1597 (if (get-text-property (point) 'invisible)
1598 (progn
1599 (goto-char (next-single-property-change (point) 'invisible
1600 nil end))
1601 ;; if text is hidden by an `invisible' text property
1602 ;; we cannot open it at all.
1603 (setq can-be-opened nil))
1604 (unless (null can-be-opened)
1605 (let ((overlays (overlays-at (point)))
1606 ov-list
1607 o
1608 invis-prop)
1609 (while overlays
1610 (setq o (car overlays)
1611 invis-prop (overlay-get o 'invisible))
1612 (if (or (memq invis-prop buffer-invisibility-spec)
1613 (assq invis-prop buffer-invisibility-spec))
1614 (if (overlay-get o 'isearch-open-invisible)
1615 (setq ov-list (cons o ov-list))
1616 ;; We found one overlay that cannot be
1617 ;; opened, that means the whole chunk
1618 ;; cannot be opened.
1619 (setq can-be-opened nil)))
1620 (setq overlays (cdr overlays)))
1621 (if can-be-opened
1622 ;; It makes sense to append to the open
1623 ;; overlays list only if we know that this is
1624 ;; t.
1625 (setq crt-overlays (append ov-list crt-overlays))))
1626 (goto-char (next-overlay-change (point))))))
86bfaffe 1627 ;; See if invisibility reaches up thru END.
0352b205
RS
1628 (if (>= (point) end)
1629 (if (and (not (null can-be-opened)) (consp crt-overlays))
1630 (progn
1631 (setq isearch-opened-overlays
1632 (append isearch-opened-overlays crt-overlays))
1633 ;; maybe use a cycle instead of mapcar?
1634 (mapcar (function isearch-open-overlay-temporary)
1635 crt-overlays)
1636 nil)
1637 t))))))
08200510
RS
1638
1639\f
08200510
RS
1640;;; Highlighting
1641
b78559b0 1642(defvar isearch-overlay nil)
08200510 1643
b78559b0 1644(defun isearch-highlight (beg end)
f5f6a944 1645 (if (or (null search-highlight) (null window-system))
08200510 1646 nil
b78559b0
RS
1647 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1648 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1649 (overlay-put isearch-overlay 'face
1650 (if (internal-find-face 'isearch nil)
1651 'isearch 'region))))
b78559b0
RS
1652
1653(defun isearch-dehighlight (totally)
1654 (if isearch-overlay
1655 (delete-overlay isearch-overlay)))
08200510 1656
08200510
RS
1657;;; General utilities
1658
08200510 1659
b78559b0
RS
1660(defun isearch-no-upper-case-p (string regexp-flag)
1661 "Return t if there are no upper case chars in STRING.
b7852303 1662If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1663since they have special meaning in a regexp."
59057198
RS
1664 (let (quote-flag (i 0) (len (length string)) found)
1665 (while (and (not found) (< i len))
1666 (let ((char (aref string i)))
1667 (if (and regexp-flag (eq char ?\\))
1668 (setq quote-flag (not quote-flag))
1669 (if (and (not quote-flag) (not (eq char (downcase char))))
1670 (setq found t))))
1671 (setq i (1+ i)))
1672 (not found)))
08200510 1673
b78559b0 1674;; Portability functions to support various Emacs versions.
8e1cae6d 1675
08200510 1676(defun isearch-char-to-string (c)
92d874e6 1677 (char-to-string c))
08200510
RS
1678
1679(defun isearch-text-char-description (c)
9d78ff8b
RS
1680 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1681 (text-char-description c)
1682 (isearch-char-to-string c)))
08200510 1683
bd1bd125 1684;; General function to unread characters or events.
99ae9e9f 1685;; Also insert them in a keyboard macro being defined.
8f90f594 1686(defun isearch-unread (&rest char-or-events)
99ae9e9f 1687 (mapcar 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1688 (setq unread-command-events
1689 (append char-or-events unread-command-events)))
08200510
RS
1690
1691(defun isearch-last-command-char ()
1692 ;; General function to return the last command character.
58451992 1693 last-command-char)
08200510 1694
3b1e4dd1 1695;;; isearch.el ends here