(imenu-always-use-completion-buffer-p): Use `other'
[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 ()
57ff063d
RS
969 "Pull current X selection into search string.
970Some users like to put this command on Mouse-2.
971To do that, evaluate these expressions:
972 (define-key isearch-mode-map [down-mouse-2] nil)
973 (define-key isearch-mode-map [mouse-2] 'isearch-yank-x-selection)"
9cf081fa
KH
974 (interactive)
975 (isearch-yank-string (x-get-selection)))
8e1cae6d
JB
976
977(defun isearch-yank-word ()
978 "Pull next word from buffer into search string."
979 (interactive)
9cf081fa
KH
980 (isearch-yank-string
981 (save-excursion
982 (and (not isearch-forward) isearch-other-end
983 (goto-char isearch-other-end))
984 (buffer-substring (point) (progn (forward-word 1) (point))))))
8e1cae6d
JB
985
986(defun isearch-yank-line ()
987 "Pull rest of line from buffer into search string."
988 (interactive)
9cf081fa
KH
989 (isearch-yank-string
990 (save-excursion
991 (and (not isearch-forward) isearch-other-end
992 (goto-char isearch-other-end))
993 (buffer-substring (point) (line-end-position)))))
8e1cae6d
JB
994
995
996(defun isearch-search-and-update ()
997 ;; Do the search and update the display.
998 (if (and (not isearch-success)
999 ;; unsuccessful regexp search may become
1000 ;; successful by addition of characters which
1001 ;; make isearch-string valid
1002 (not isearch-regexp))
1003 nil
1004 ;; In reverse search, adding stuff at
1005 ;; the end may cause zero or many more chars to be
1006 ;; matched, in the string following point.
1007 ;; Allow all those possibilities without moving point as
1008 ;; long as the match does not extend past search origin.
1009 (if (and (not isearch-forward) (not isearch-adjusted)
1010 (condition-case ()
99ae9e9f
KH
1011 (let ((case-fold-search isearch-case-fold-search))
1012 (looking-at (if isearch-regexp isearch-string
1013 (regexp-quote isearch-string))))
8e1cae6d 1014 (error nil))
bd6a8414
RS
1015 (or isearch-yank-flag
1016 (<= (match-end 0)
1017 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1018 (progn
1019 (setq isearch-success t
1020 isearch-invalid-regexp nil
1021 isearch-within-brackets nil
1022 isearch-other-end (match-end 0))
1023 (if (and (eq isearch-case-fold-search t) search-upper-case)
1024 (setq isearch-case-fold-search
1025 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1026 ;; Not regexp, not reverse, or no match at point.
1027 (if (and isearch-other-end (not isearch-adjusted))
1028 (goto-char (if isearch-forward isearch-other-end
1029 (min isearch-opoint
1030 isearch-barrier
1031 (1+ isearch-other-end)))))
1032 (isearch-search)
1033 ))
1034 (isearch-push-state)
1035 (if isearch-op-fun (funcall isearch-op-fun))
1036 (isearch-update))
1037
1038
1039;; *, ?, and | chars can make a regexp more liberal.
08200510 1040;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1041;; So go back to place last successful search started
1042;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1043;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1044
1045(defun isearch-*-char ()
1046 "Handle * and ? specially in regexps."
1047 (interactive)
1048 (if isearch-regexp
bd6a8414
RS
1049 (let ((idx (length isearch-string)))
1050 (while (and (> idx 0)
1051 (eq (aref isearch-string (1- idx)) ?\\))
1052 (setq idx (1- idx)))
1053 (when (= (mod (- (length isearch-string) idx) 2) 0)
1054 (setq isearch-adjusted t)
1055 ;; Get the isearch-other-end from before the last search.
1056 ;; We want to start from there,
1057 ;; so that we don't retreat farther than that.
1058 ;; (car isearch-cmds) is after last search;
1059 ;; (car (cdr isearch-cmds)) is from before it.
1060 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1061 (setq cs (or cs isearch-barrier))
1062 (goto-char
1063 (if isearch-forward
1064 (max cs isearch-barrier)
1065 (min cs isearch-barrier)))))))
08200510 1066 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1067
1068
8e1cae6d
JB
1069(defun isearch-|-char ()
1070 "If in regexp search, jump to the barrier."
1071 (interactive)
1072 (if isearch-regexp
1073 (progn
1074 (setq isearch-adjusted t)
1075 (goto-char isearch-barrier)))
08200510 1076 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1077
1078
b457cc3b 1079(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1080
1081(defun isearch-other-meta-char ()
8f90f594 1082 "Exit the search normally and reread this key sequence.
35a4d143
RS
1083But only if `search-exit-option' is non-nil, the default.
1084If it is the symbol `edit', the search string is edited in the minibuffer
1085and the meta character is unread so that it applies to editing the string."
8e1cae6d 1086 (interactive)
c6431f12
KH
1087 (let* ((key (this-command-keys))
1088 (main-event (aref key 0))
1089 (keylist (listify-key-sequence key)))
24b5caec 1090 (cond ((and (= (length key) 1)
d94eb6eb 1091 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1092 (not (or (null lookup) (integerp lookup)
1093 (keymapp lookup)))))
24b5caec
RS
1094 ;; Handle a function key that translates into something else.
1095 ;; If the key has a global definition too,
1096 ;; exit and unread the key itself, so its global definition runs.
1097 ;; Otherwise, unread the translation,
1098 ;; so that the translated key takes effect within isearch.
d94eb6eb 1099 (cancel-kbd-macro-events)
24b5caec
RS
1100 (if (lookup-key global-map key)
1101 (progn
1102 (isearch-done)
1103 (apply 'isearch-unread keylist))
1104 (apply 'isearch-unread
1105 (listify-key-sequence (lookup-key function-key-map key)))))
1106 (
c6431f12
KH
1107 ;; Handle an undefined shifted control character
1108 ;; by downshifting it if that makes it defined.
1109 ;; (As read-key-sequence would normally do,
1110 ;; if we didn't have a default definition.)
1111 (let ((mods (event-modifiers main-event)))
1112 (and (integerp main-event)
1113 (memq 'shift mods)
1114 (memq 'control mods)
1115 (lookup-key isearch-mode-map
1116 (let ((copy (copy-sequence key)))
1117 (aset copy 0
1118 (- main-event (- ?\C-\S-a ?\C-a)))
1119 copy)
1120 nil)))
1121 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1122 (cancel-kbd-macro-events)
c6431f12
KH
1123 (apply 'isearch-unread keylist))
1124 ((eq search-exit-option 'edit)
1125 (apply 'isearch-unread keylist)
1126 (isearch-edit-string))
1127 (search-exit-option
1128 (let (window)
d94eb6eb 1129 (cancel-kbd-macro-events)
c6431f12
KH
1130 (apply 'isearch-unread keylist)
1131 ;; Properly handle scroll-bar and mode-line clicks
1132 ;; for which a dummy prefix event was generated as (aref key 0).
1133 (and (> (length key) 1)
1134 (symbolp (aref key 0))
1135 (listp (aref key 1))
1136 (not (numberp (posn-point (event-start (aref key 1)))))
1137 ;; Convert the event back into its raw form,
1138 ;; with the dummy prefix implicit in the mouse event,
1139 ;; so it will get split up once again.
1140 (progn (setq unread-command-events
1141 (cdr unread-command-events))
1142 (setq main-event (car unread-command-events))
1143 (setcar (cdr (event-start main-event))
1144 (car (nth 1 (event-start main-event))))))
1145 ;; If we got a mouse click, maybe it was read with the buffer
1146 ;; it was clicked on. If so, that buffer, not the current one,
1147 ;; is in isearch mode. So end the search in that buffer.
1148 (if (and (listp main-event)
1149 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1150 (windowp window)
1151 (or (> (minibuffer-depth) 0)
1152 (not (window-minibuffer-p window))))
c6431f12
KH
1153 (save-excursion
1154 (set-buffer (window-buffer window))
0352b205
RS
1155 (isearch-done)
1156 (isearch-clean-overlays))
1157 (isearch-done)
1158 (isearch-clean-overlays))))
c6431f12
KH
1159 (t;; otherwise nil
1160 (isearch-process-search-string key key)))))
8e1cae6d 1161
8e1cae6d
JB
1162(defun isearch-quote-char ()
1163 "Quote special characters for incremental search."
1164 (interactive)
8bc15fa8
RS
1165 (let ((char (read-quoted-char (isearch-message t))))
1166 ;; Assume character codes 0200 - 0377 stand for
1167 ;; European characters in Latin-1, and convert them
1168 ;; to Emacs characters.
1169 (and enable-multibyte-characters
1170 (>= char ?\200)
1171 (<= char ?\377)
1172 (setq char (+ char nonascii-insert-offset)))
1173 (isearch-process-search-char char)))
8e1cae6d 1174
8e1cae6d
JB
1175(defun isearch-return-char ()
1176 "Convert return into newline for incremental search.
1177Obsolete."
1178 (interactive)
1179 (isearch-process-search-char ?\n))
1180
8e1cae6d 1181(defun isearch-printing-char ()
b68c164d 1182 "Add this ordinary printing character to the search string and search."
8e1cae6d 1183 (interactive)
af56433d 1184 (let ((char (isearch-last-command-char)))
45b94eb2
KH
1185 (if (= char ?\S-\ )
1186 (setq char ?\ ))
af56433d
RS
1187 (if (and enable-multibyte-characters
1188 (>= char ?\200)
1189 (<= char ?\377))
1190 (isearch-process-search-char (+ char nonascii-insert-offset))
1191 (if current-input-method
1192 (isearch-process-search-multibyte-characters char)
1193 (isearch-process-search-char char)))))
8e1cae6d
JB
1194
1195(defun isearch-whitespace-chars ()
08200510 1196 "Match all whitespace chars, if in regexp mode.
b68c164d 1197If you want to search for just a space, type C-q SPC."
8e1cae6d 1198 (interactive)
08200510 1199 (if isearch-regexp
9da6682f
RS
1200 (if (and search-whitespace-regexp (not isearch-within-brackets)
1201 (not isearch-invalid-regexp))
08200510
RS
1202 (isearch-process-search-string search-whitespace-regexp " ")
1203 (isearch-printing-char))
1204 (progn
eb8c3be9 1205 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1206 ;; (setq isearch-word t)
1207 ;; (setq isearch-adjusted t)
1208 ;; (goto-char isearch-barrier)
1209 (isearch-printing-char))))
8e1cae6d
JB
1210
1211(defun isearch-process-search-char (char)
1212 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1213 (isearch-process-search-string
1214 (isearch-char-to-string char)
01a6ef79
RS
1215 (if (>= char 0200)
1216 (char-to-string char)
1217 (isearch-text-char-description char))))
8e1cae6d
JB
1218
1219(defun isearch-process-search-string (string message)
1220 (setq isearch-string (concat isearch-string string)
1221 isearch-message (concat isearch-message message))
1222 (isearch-search-and-update))
1223
1224\f
8e1cae6d
JB
1225;; Search Ring
1226
08200510
RS
1227(defun isearch-ring-adjust1 (advance)
1228 ;; Helper for isearch-ring-adjust
1229 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1230 (length (length ring))
1231 (yank-pointer-name (if isearch-regexp
08200510 1232 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1233 'search-ring-yank-pointer))
1234 (yank-pointer (eval yank-pointer-name)))
1235 (if (zerop length)
1236 ()
1237 (set yank-pointer-name
1238 (setq yank-pointer
ffbc30b2
PE
1239 (mod (+ (or yank-pointer 0)
1240 (if advance -1 1))
1241 length)))
a5dcf63f 1242 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1243 isearch-message (mapconcat 'isearch-text-char-description
1244 isearch-string "")))))
1245
1246(defun isearch-ring-adjust (advance)
1247 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1248 (isearch-ring-adjust1 advance)
08200510
RS
1249 (if search-ring-update
1250 (progn
1251 (isearch-search)
1252 (isearch-update))
1253 (isearch-edit-string)
2ff20b7e
RS
1254 )
1255 (isearch-push-state))
8e1cae6d
JB
1256
1257(defun isearch-ring-advance ()
1258 "Advance to the next search string in the ring."
08200510 1259 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1260 (interactive)
1261 (isearch-ring-adjust 'advance))
1262
1263(defun isearch-ring-retreat ()
1264 "Retreat to the previous search string in the ring."
1265 (interactive)
1266 (isearch-ring-adjust nil))
1267
a5dcf63f
RS
1268(defun isearch-ring-advance-edit (n)
1269 "Insert the next element of the search history into the minibuffer."
1270 (interactive "p")
1271 (let* ((yank-pointer-name (if isearch-regexp
1272 'regexp-search-ring-yank-pointer
1273 'search-ring-yank-pointer))
1274 (yank-pointer (eval yank-pointer-name))
1275 (ring (if isearch-regexp regexp-search-ring search-ring))
1276 (length (length ring)))
1277 (if (zerop length)
1278 ()
1279 (set yank-pointer-name
1280 (setq yank-pointer
ffbc30b2
PE
1281 (mod (- (or yank-pointer 0) n)
1282 length)))
a5dcf63f 1283
a5dcf63f 1284 (erase-buffer)
35a4d143 1285 (insert (nth yank-pointer ring))
49c13105 1286 (goto-char (point-max)))))
a5dcf63f
RS
1287
1288(defun isearch-ring-retreat-edit (n)
1289 "Inserts the previous element of the search history into the minibuffer."
1290 (interactive "p")
1291 (isearch-ring-advance-edit (- n)))
1292
1293;;(defun isearch-ring-adjust-edit (advance)
1294;; "Use the next or previous search string in the ring while in minibuffer."
1295;; (isearch-ring-adjust1 advance)
1296;; (erase-buffer)
1297;; (insert isearch-string))
1298
1299;;(defun isearch-ring-advance-edit ()
1300;; (interactive)
1301;; (isearch-ring-adjust-edit 'advance))
1302
1303;;(defun isearch-ring-retreat-edit ()
1304;; "Retreat to the previous search string in the ring while in the minibuffer."
1305;; (interactive)
1306;; (isearch-ring-adjust-edit nil))
08200510
RS
1307
1308
1309(defun isearch-complete1 ()
1310 ;; Helper for isearch-complete and isearch-complete-edit
1311 ;; Return t if completion OK, nil if no completion exists.
1312 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1313 (alist (mapcar (function (lambda (string) (list string))) ring))
1314 (completion-ignore-case case-fold-search)
1315 (completion (try-completion isearch-string alist)))
1316 (cond
1317 ((eq completion t)
1318 ;; isearch-string stays the same
1319 t)
1320 ((or completion ; not nil, must be a string
b7852303 1321 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1322 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1323 (progn
1324 (if completion-auto-help
1325 (with-output-to-temp-buffer "*Isearch completions*"
1326 (display-completion-list
1327 (all-completions isearch-string alist))))
1328 t)
1329 (and completion
1330 (setq isearch-string completion))))
08200510
RS
1331 (t
1332 (message "No completion") ; waits a second if in minibuffer
1333 nil))))
1334
1335(defun isearch-complete ()
1336 "Complete the search string from the strings on the search ring.
1337The completed string is then editable in the minibuffer.
1338If there is no completion possible, say so and continue searching."
1339 (interactive)
1340 (if (isearch-complete1)
1341 (isearch-edit-string)
1342 ;; else
1343 (sit-for 1)
1344 (isearch-update)))
8e1cae6d 1345
08200510
RS
1346(defun isearch-complete-edit ()
1347 "Same as `isearch-complete' except in the minibuffer."
1348 (interactive)
1349 (setq isearch-string (buffer-string))
1350 (if (isearch-complete1)
8e1cae6d 1351 (progn
08200510
RS
1352 (erase-buffer)
1353 (insert isearch-string))))
8e1cae6d
JB
1354
1355\f
8e1cae6d 1356;; The search status stack (and isearch window-local variables, not used).
08200510 1357;; Need a structure for this.
8e1cae6d
JB
1358
1359(defun isearch-top-state ()
8e1cae6d
JB
1360 (let ((cmd (car isearch-cmds)))
1361 (setq isearch-string (car cmd)
1362 isearch-message (car (cdr cmd))
1363 isearch-success (nth 3 cmd)
1364 isearch-forward (nth 4 cmd)
1365 isearch-other-end (nth 5 cmd)
08200510
RS
1366 isearch-word (nth 6 cmd)
1367 isearch-invalid-regexp (nth 7 cmd)
1368 isearch-wrapped (nth 8 cmd)
c5f6b356 1369 isearch-barrier (nth 9 cmd)
f551b97d
RS
1370 isearch-within-brackets (nth 10 cmd)
1371 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1372 (goto-char (car (cdr (cdr cmd))))))
1373
1374(defun isearch-pop-state ()
8e1cae6d
JB
1375 (setq isearch-cmds (cdr isearch-cmds))
1376 (isearch-top-state)
1377 )
1378
1379(defun isearch-push-state ()
1380 (setq isearch-cmds
1381 (cons (list isearch-string isearch-message (point)
1382 isearch-success isearch-forward isearch-other-end
08200510 1383 isearch-word
c5f6b356 1384 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1385 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1386 isearch-cmds)))
1387
8e1cae6d 1388\f
8e1cae6d
JB
1389;; Message string
1390
1391(defun isearch-message (&optional c-q-hack ellipsis)
1392 ;; Generate and print the message string.
1393 (let ((cursor-in-echo-area ellipsis)
1394 (m (concat
08200510 1395 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1396 isearch-message
1397 (isearch-message-suffix c-q-hack ellipsis)
1398 )))
a56687f1
KH
1399 (if c-q-hack
1400 m
1401 (let ((message-log-max nil))
1402 (message "%s" m)))))
8e1cae6d 1403
08200510 1404(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1405 ;; If about to search, and previous search regexp was invalid,
1406 ;; check that it still is. If it is valid now,
1407 ;; let the message we display while searching say that it is valid.
1408 (and isearch-invalid-regexp ellipsis
1409 (condition-case ()
1410 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1411 (setq isearch-invalid-regexp nil
1412 isearch-within-brackets nil))
8e1cae6d
JB
1413 (error nil)))
1414 ;; If currently failing, display no ellipsis.
1415 (or isearch-success (setq ellipsis nil))
1416 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1417 (if (and isearch-wrapped
1418 (if isearch-forward
1419 (> (point) isearch-opoint)
1420 (< (point) isearch-opoint)))
1421 "over")
8e1cae6d 1422 (if isearch-wrapped "wrapped ")
08200510 1423 (if isearch-word "word " "")
8e1cae6d 1424 (if isearch-regexp "regexp " "")
08200510 1425 (if nonincremental "search" "I-search")
f46d5091 1426 (if isearch-forward "" " backward")
5b56d4e4
KH
1427 (if current-input-method
1428 (concat " [" current-input-method-title "]: ")
f46d5091 1429 ": ")
8e1cae6d
JB
1430 )))
1431 (aset m 0 (upcase (aref m 0)))
1432 m))
1433
1434
1435(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1436 (concat (if c-q-hack "^Q" "")
1437 (if isearch-invalid-regexp
1438 (concat " [" isearch-invalid-regexp "]")
1439 "")))
1440
1441\f
8e1cae6d
JB
1442;;; Searching
1443
1444(defun isearch-search ()
1445 ;; Do the search with the current search string.
1446 (isearch-message nil t)
4453091d 1447 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1448 (setq isearch-case-fold-search
1449 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1450 (condition-case lossage
79c7a4fa
RS
1451 (let ((inhibit-point-motion-hooks search-invisible)
1452 (inhibit-quit nil)
86bfaffe
RS
1453 (case-fold-search isearch-case-fold-search)
1454 (retry t))
8e1cae6d 1455 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1456 (setq isearch-within-brackets nil)
86bfaffe
RS
1457 (while retry
1458 (setq isearch-success
1459 (funcall
1460 (cond (isearch-word
1461 (if isearch-forward
1462 'word-search-forward 'word-search-backward))
1463 (isearch-regexp
1464 (if isearch-forward
1465 're-search-forward 're-search-backward))
1466 (t
1467 (if isearch-forward 'search-forward 'search-backward)))
1468 isearch-string nil t))
1469 ;; Clear RETRY unless we matched some invisible text
1470 ;; and we aren't supposed to do that.
0352b205 1471 (if (or (eq search-invisible t)
86bfaffe
RS
1472 (not isearch-success)
1473 (bobp) (eobp)
1474 (= (match-beginning 0) (match-end 0))
1475 (not (isearch-range-invisible
1476 (match-beginning 0) (match-end 0))))
1477 (setq retry nil)))
99ae9e9f 1478 (setq isearch-just-started nil)
8e1cae6d
JB
1479 (if isearch-success
1480 (setq isearch-other-end
1481 (if isearch-forward (match-beginning 0) (match-end 0)))))
1482
d32696d4 1483 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1484 (setq isearch-success nil))
1485
1486 (invalid-regexp
1487 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1488 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1489 isearch-invalid-regexp))
8e1cae6d
JB
1490 (if (string-match
1491 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1492 isearch-invalid-regexp)
f1c03125
RS
1493 (setq isearch-invalid-regexp "incomplete input")))
1494 (error
1495 ;; stack overflow in regexp search.
1496 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1497
1498 (if isearch-success
1499 nil
1500 ;; Ding if failed this time after succeeding last time.
1501 (and (nth 3 (car isearch-cmds))
1502 (ding))
1503 (goto-char (nth 2 (car isearch-cmds)))))
1504
0352b205
RS
1505
1506;;; Called when opening an overlay, and we are still in isearch.
1507(defun isearch-open-overlay-temporary (ov)
0352b205
RS
1508 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1509 ;; Some modes would want to open the overlays temporary during
1510 ;; isearch in their own way, they should set the
1511 ;; `isearch-open-invisible-temporary' to a function doing this.
1512 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1513 ;; Store the values for the `invisible' and `intangible'
1514 ;; properties, and then set them to nil. This way the text hidden
1515 ;; by this overlay becomes visible.
1516
1517 ;; Do we realy need to set the `intangible' property to t? Can we
1518 ;; have the point inside an overlay with an `intangible' property?
1519 ;; In 19.34 this does not exist so I cannot test it.
1520 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1521 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1522 (overlay-put ov 'invisible nil)
1523 (overlay-put ov 'intangible nil)))
1524
1525
1526;;; This is called at the end of isearch. I will open the overlays
1527;;; that contain the latest match. Obviously in case of a C-g the
1528;;; point returns to the original location which surely is not contain
1529;;; in any of these overlays, se we are safe in this case too.
1530(defun isearch-open-necessary-overlays (ov)
1531 (let ((inside-overlay (and (> (point) (overlay-start ov))
1532 (< (point) (overlay-end ov))))
1533 ;; If this exists it means that the overlay was opened using
1534 ;; this function, not by us tweaking the overlay properties.
1535 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1536 (when (or inside-overlay (not fct-temp))
1537 ;; restore the values for the `invisible' and `intangible'
1538 ;; properties
1539 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1540 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1541 (overlay-put ov 'isearch-invisible nil)
1542 (overlay-put ov 'isearch-intangible nil))
1543 (if inside-overlay
1544 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1545 (if fct-temp
1546 (funcall fct-temp ov t)))))
1547
1548;;; This is called when exiting isearch. It closes the temporary
1549;;; opened overlays, except the ones that contain the latest match.
1550(defun isearch-clean-overlays ()
1551 (when isearch-opened-overlays
1552 ;; Use a cycle instead of a mapcar here?
1553 (mapcar
1554 (function isearch-open-necessary-overlays) isearch-opened-overlays)
1555 (setq isearch-opened-overlays nil)))
1556
1557;;; Verify if the current match is outside of each element of
1558;;; `isearch-opened-overlays', if so close that overlay.
1559(defun isearch-close-unecessary-overlays (begin end)
1560 (let ((ov-list isearch-opened-overlays)
1561 ov
1562 inside-overlay
1563 fct-temp)
1564 (setq isearch-opened-overlays nil)
1565 (while ov-list
1566 (setq ov (car ov-list))
1567 (setq ov-list (cdr ov-list))
1568 (setq inside-overlay (or (and (> begin (overlay-start ov))
1569 (< begin (overlay-end ov)))
1570 (and (> end (overlay-start ov))
1571 (< end (overlay-end ov)))))
1572 ;; If this exists it means that the overlay was opened using
1573 ;; this function, not by us tweaking the overlay properties.
1574 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1575 (if inside-overlay
1576 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1577 (if fct-temp
1578 (funcall fct-temp ov t)
1579 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1580 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1581 (overlay-put ov 'isearch-invisible nil)
1582 (overlay-put ov 'isearch-intangible nil))))))
1583
86bfaffe 1584(defun isearch-range-invisible (beg end)
79c7a4fa 1585 "Return t if all the text from BEG to END is invisible."
86bfaffe
RS
1586 (and (/= beg end)
1587 ;; Check that invisibility runs up to END.
1588 (save-excursion
1589 (goto-char beg)
0352b205
RS
1590 (let
1591 ;; can-be-opened keeps track if we can open some overlays.
1592 ((can-be-opened (eq search-invisible 'open))
1593 ;; the list of overlays that could be opened
1594 (crt-overlays nil))
1595 (when (and can-be-opened isearch-hide-immediately)
1596 (isearch-close-unecessary-overlays beg end))
1597 ;; If the following character is currently invisible,
1598 ;; skip all characters with that same `invisible' property value.
1599 ;; Do that over and over.
1600 (while (and (< (point) end)
1601 (let ((prop
1602 (get-char-property (point) 'invisible)))
1603 (if (eq buffer-invisibility-spec t)
1604 prop
1605 (or (memq prop buffer-invisibility-spec)
1606 (assq prop buffer-invisibility-spec)))))
1607 (if (get-text-property (point) 'invisible)
1608 (progn
1609 (goto-char (next-single-property-change (point) 'invisible
1610 nil end))
1611 ;; if text is hidden by an `invisible' text property
1612 ;; we cannot open it at all.
1613 (setq can-be-opened nil))
1614 (unless (null can-be-opened)
1615 (let ((overlays (overlays-at (point)))
1616 ov-list
1617 o
1618 invis-prop)
1619 (while overlays
1620 (setq o (car overlays)
1621 invis-prop (overlay-get o 'invisible))
de207f5a
KH
1622 (if (if (eq buffer-invisibility-spec t)
1623 invis-prop
1624 (or (memq invis-prop buffer-invisibility-spec)
1625 (assq invis-prop buffer-invisibility-spec)))
0352b205
RS
1626 (if (overlay-get o 'isearch-open-invisible)
1627 (setq ov-list (cons o ov-list))
1628 ;; We found one overlay that cannot be
1629 ;; opened, that means the whole chunk
1630 ;; cannot be opened.
1631 (setq can-be-opened nil)))
1632 (setq overlays (cdr overlays)))
1633 (if can-be-opened
1634 ;; It makes sense to append to the open
1635 ;; overlays list only if we know that this is
1636 ;; t.
7e97482b
RS
1637 (setq crt-overlays (append ov-list crt-overlays)))))
1638 (goto-char (next-overlay-change (point)))))
86bfaffe 1639 ;; See if invisibility reaches up thru END.
0352b205
RS
1640 (if (>= (point) end)
1641 (if (and (not (null can-be-opened)) (consp crt-overlays))
1642 (progn
1643 (setq isearch-opened-overlays
1644 (append isearch-opened-overlays crt-overlays))
1645 ;; maybe use a cycle instead of mapcar?
1646 (mapcar (function isearch-open-overlay-temporary)
1647 crt-overlays)
1648 nil)
1649 t))))))
08200510
RS
1650
1651\f
08200510
RS
1652;;; Highlighting
1653
b78559b0 1654(defvar isearch-overlay nil)
08200510 1655
b78559b0 1656(defun isearch-highlight (beg end)
f5f6a944 1657 (if (or (null search-highlight) (null window-system))
08200510 1658 nil
b78559b0
RS
1659 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1660 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1661 (overlay-put isearch-overlay 'face
1662 (if (internal-find-face 'isearch nil)
1663 'isearch 'region))))
b78559b0
RS
1664
1665(defun isearch-dehighlight (totally)
1666 (if isearch-overlay
1667 (delete-overlay isearch-overlay)))
08200510 1668
08200510
RS
1669;;; General utilities
1670
08200510 1671
b78559b0
RS
1672(defun isearch-no-upper-case-p (string regexp-flag)
1673 "Return t if there are no upper case chars in STRING.
b7852303 1674If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1675since they have special meaning in a regexp."
59057198
RS
1676 (let (quote-flag (i 0) (len (length string)) found)
1677 (while (and (not found) (< i len))
1678 (let ((char (aref string i)))
1679 (if (and regexp-flag (eq char ?\\))
1680 (setq quote-flag (not quote-flag))
1681 (if (and (not quote-flag) (not (eq char (downcase char))))
1682 (setq found t))))
1683 (setq i (1+ i)))
1684 (not found)))
08200510 1685
b78559b0 1686;; Portability functions to support various Emacs versions.
8e1cae6d 1687
08200510 1688(defun isearch-char-to-string (c)
92d874e6 1689 (char-to-string c))
08200510
RS
1690
1691(defun isearch-text-char-description (c)
9d78ff8b
RS
1692 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1693 (text-char-description c)
1694 (isearch-char-to-string c)))
08200510 1695
bd1bd125 1696;; General function to unread characters or events.
99ae9e9f 1697;; Also insert them in a keyboard macro being defined.
8f90f594 1698(defun isearch-unread (&rest char-or-events)
99ae9e9f 1699 (mapcar 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1700 (setq unread-command-events
1701 (append char-or-events unread-command-events)))
08200510
RS
1702
1703(defun isearch-last-command-char ()
1704 ;; General function to return the last command character.
58451992 1705 last-command-char)
08200510 1706
3b1e4dd1 1707;;; isearch.el ends here