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