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