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