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