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