2001-01-21 ShengHuo ZHU <zsh@cs.rochester.edu>
[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 449
6c551d4e
EZ
450Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
451Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
452Type \\[isearch-edit-string] to edit the search string in the minibuffer.
453
8e1cae6d
JB
454Also supported is a search ring of the previous 16 search strings.
455Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
456Type \\[isearch-ring-retreat] to search for the previous item in the search\
457 ring.
458Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 459
08200510
RS
460The above keys, bound in `isearch-mode-map', are often controlled by
461 options; do M-x apropos on search-.* to find them.
8e1cae6d 462Other control and meta characters terminate the search
08200510 463 and are then executed normally (depending on `search-exit-option').
fd49e05c 464Likewise for function keys and mouse button events.
8e1cae6d 465
08200510
RS
466If this function is called non-interactively, it does not return to
467the calling function until the search is done."
8e1cae6d 468
eceee2c0 469 (interactive "P\np")
4ae7d00a 470 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 471
eceee2c0 472(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
473 "\
474Do incremental search forward for regular expression.
08200510 475With a prefix argument, do a regular string search instead.
8e1cae6d
JB
476Like ordinary incremental search except that your input
477is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 478 (interactive "P\np")
4ae7d00a 479 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 480
eceee2c0 481(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
482 "\
483Do incremental search backward.
08200510 484With a prefix argument, do a regular expression search instead.
8e1cae6d 485See \\[isearch-forward] for more information."
eceee2c0 486 (interactive "P\np")
4ae7d00a 487 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 488
eceee2c0 489(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
490 "\
491Do incremental search backward for regular expression.
08200510 492With a prefix argument, do a regular string search instead.
8e1cae6d
JB
493Like ordinary incremental search except that your input
494is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 495 (interactive "P\np")
4ae7d00a 496 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
497
498
499(defun isearch-mode-help ()
500 (interactive)
501 (describe-function 'isearch-forward)
502 (isearch-update))
8e1cae6d
JB
503
504\f
8e1cae6d
JB
505;; isearch-mode only sets up incremental search for the minor mode.
506;; All the work is done by the isearch-mode commands.
507
08200510 508;; Not used yet:
d7fa5aa2 509;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
510;; isearch-forward-regexp isearch-backward-regexp)
511;; "List of commands for which isearch-mode does not recursive-edit.")
512
513
514(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
515 "Start isearch minor mode. Called by `isearch-forward', etc.
516
517\\{isearch-mode-map}"
8e1cae6d
JB
518
519 ;; Initialize global vars.
520 (setq isearch-forward forward
521 isearch-regexp regexp
08200510 522 isearch-word word-p
8e1cae6d
JB
523 isearch-op-fun op-fun
524 isearch-case-fold-search case-fold-search
525 isearch-string ""
526 isearch-message ""
527 isearch-cmds nil
528 isearch-success t
529 isearch-wrapped nil
530 isearch-barrier (point)
531 isearch-adjusted nil
532 isearch-yank-flag nil
533 isearch-invalid-regexp nil
c5f6b356 534 isearch-within-brackets nil
0cabad13 535 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d
JB
536 (> (window-height)
537 (* 4 search-slow-window-lines)))
538 isearch-other-end nil
539 isearch-small-window nil
99ae9e9f 540 isearch-just-started t
8e1cae6d 541
8e1cae6d 542 isearch-opoint (point)
a5dcf63f 543 search-ring-yank-pointer nil
0352b205 544 isearch-opened-overlays nil
99848db0
KH
545 isearch-input-method-function input-method-function
546 isearch-input-method-local-p (local-variable-p 'input-method-function)
ae1a21c6
MB
547 regexp-search-ring-yank-pointer nil
548
549 ;; Save the original value of `minibuffer-message-timeout', and
550 ;; set it to nil so that isearch's messages don't get timed out.
551 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
552 minibuffer-message-timeout nil)
99848db0
KH
553
554 ;; We must bypass input method while reading key. When a user type
555 ;; printable character, appropriate input method is turned on in
380866a2 556 ;; minibuffer to read multibyte characters.
99848db0
KH
557 (or isearch-input-method-local-p
558 (make-local-variable 'input-method-function))
559 (setq input-method-function nil)
560
99ae9e9f 561 (looking-at "")
292a8dff
RS
562 (setq isearch-window-configuration
563 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 564
18ce7555
RS
565 ;; Maybe make minibuffer frame visible and/or raise it.
566 (let ((frame (window-frame (minibuffer-window))))
567 (if (not (memq (frame-live-p frame) '(nil t)))
568 (progn
569 (make-frame-visible frame)
570 (if minibuffer-auto-raise
571 (raise-frame frame)))))
572
8e1cae6d 573 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 574 (force-mode-line-update)
8e1cae6d
JB
575
576 (isearch-push-state)
577
c7955222 578 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
579 (isearch-update)
580 (run-hooks 'isearch-mode-hook)
08200510 581
bfe2d334 582 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 583 (add-hook 'kbd-macro-termination-hook 'isearch-done)
6e5cd0ae 584
08200510
RS
585 ;; isearch-mode can be made modal (in the sense of not returning to
586 ;; the calling function until searching is completed) by entering
587 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
588 (if recursive-edit
589 (let ((isearch-recursive-edit t))
590 (recursive-edit)))
0daa17f3 591 isearch-success)
8e1cae6d
JB
592
593
8e1cae6d
JB
594;; Some high level utilities. Others below.
595
596(defun isearch-update ()
597 ;; Called after each command to update the display.
58451992 598 (if (null unread-command-events)
8e1cae6d 599 (progn
c982ab21
GM
600 (if (not (input-pending-p))
601 (isearch-message))
602 (if (and isearch-slow-terminal-mode
603 (not (or isearch-small-window
604 (pos-visible-in-window-p))))
605 (let ((found-point (point)))
606 (setq isearch-small-window t)
607 (move-to-window-line 0)
608 (let ((window-min-height 1))
609 (split-window nil (if (< search-slow-window-lines 0)
610 (1+ (- search-slow-window-lines))
611 (- (window-height)
612 (1+ search-slow-window-lines)))))
613 (if (< search-slow-window-lines 0)
614 (progn (vertical-motion (- 1 search-slow-window-lines))
615 (set-window-start (next-window) (point))
616 (set-window-hscroll (next-window)
617 (window-hscroll))
618 (set-window-hscroll (selected-window) 0))
619 (other-window 1))
620 (goto-char found-point)))
621 (if isearch-other-end
622 (if (< isearch-other-end (point)) ; isearch-forward?
623 (isearch-highlight isearch-other-end (point))
624 (isearch-highlight (point) isearch-other-end))
625 (isearch-dehighlight nil))
626 ))
8e1cae6d
JB
627 (setq ;; quit-flag nil not for isearch-mode
628 isearch-adjusted nil
629 isearch-yank-flag nil)
a5cc922e
KH
630 (isearch-lazy-highlight-new-loop)
631 ;; We must prevent the point moving to the end of composition when a
632 ;; part of the composition has just been searched.
633 (setq disable-point-adjustment t))
8e1cae6d 634
0daa17f3 635(defun isearch-done (&optional nopush edit)
c40a4de1
GM
636 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
637 ,isearch-word ,isearch-forward
638 ,isearch-message
639 ,isearch-case-fold-search)))
640 (unless (equal (car command-history) command)
641 (setq command-history (cons command command-history))))
642
bfe2d334 643 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 644 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
d196f58d
GM
645 (setq isearch-lazy-highlight-start nil)
646
8e1cae6d 647 ;; Called by all commands that terminate isearch-mode.
35a4d143 648 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 649 (setq overriding-terminal-local-map nil)
08200510 650 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
ae1a21c6 651 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
08200510 652 (isearch-dehighlight t)
67ecad4e 653 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
08200510
RS
654 (let ((found-start (window-start (selected-window)))
655 (found-point (point)))
70418205
RS
656 (if isearch-window-configuration
657 (set-window-configuration isearch-window-configuration))
08200510 658
d49b6338
RS
659 (if isearch-small-window
660 (goto-char found-point)
661 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 662 (set-window-start (selected-window) found-start t))
d49b6338 663
08200510
RS
664 ;; If there was movement, mark the starting position.
665 ;; Maybe should test difference between and set mark iff > threshold.
666 (if (/= (point) isearch-opoint)
9821535a
RS
667 (or (and transient-mark-mode mark-active)
668 (progn
669 (push-mark isearch-opoint t)
f3acf6f5 670 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 671 (message "Mark saved where search started"))))))
08200510
RS
672
673 (setq isearch-mode nil)
99848db0
KH
674 (if isearch-input-method-local-p
675 (setq input-method-function isearch-input-method-function)
676 (kill-local-variable 'input-method-function))
677
1cd3732c 678 (force-mode-line-update)
8e1cae6d 679
df01192b
RS
680 ;; If we ended in the middle of some intangible text,
681 ;; move to the further end of that intangible text.
682 (let ((after (if (eobp) nil
683 (get-text-property (point) 'intangible)))
684 (before (if (bobp) nil
685 (get-text-property (1- (point)) 'intangible))))
686 (when (and before after (eq before after))
687 (if isearch-forward
688 (goto-char (next-single-property-change (point) 'intangible))
689 (goto-char (previous-single-property-change (point) 'intangible)))))
690
35a4d143 691 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 692 ;; Update the ring data.
73d2bf95 693 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 694
8e1cae6d 695 (run-hooks 'isearch-mode-end-hook)
0daa17f3 696 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 697
73d2bf95
RS
698(defun isearch-update-ring (string &optional regexp)
699 "Add STRING to the beginning of the search ring.
700REGEXP says which ring to use."
701 (if regexp
702 (if (or (null regexp-search-ring)
fd89878d 703 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
704 (progn
705 (setq regexp-search-ring
fd89878d 706 (cons string regexp-search-ring))
73d2bf95
RS
707 (if (> (length regexp-search-ring) regexp-search-ring-max)
708 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
709 nil))))
710 (if (or (null search-ring)
fd89878d 711 (not (string= string (car search-ring))))
73d2bf95 712 (progn
fd89878d 713 (setq search-ring (cons string search-ring))
73d2bf95
RS
714 (if (> (length search-ring) search-ring-max)
715 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
716
08200510 717;;; Switching buffers should first terminate isearch-mode.
117132a6
DL
718;;; ;; For Emacs 19, the frame switch event is handled.
719;;; (defun isearch-switch-frame-handler ()
720;;; (interactive) ;; Is this necessary?
721;;; ;; First terminate isearch-mode.
722;;; (isearch-done)
723;;; (isearch-clean-overlays)
724;;; (handle-switch-frame (car (cdr last-command-char))))
08200510 725
8e1cae6d 726\f
8e1cae6d
JB
727;; Commands active while inside of the isearch minor mode.
728
729(defun isearch-exit ()
730 "Exit search normally.
731However, if this is the first command after starting incremental
732search and `search-nonincremental-instead' is non-nil, do a
08200510 733nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
734 (interactive)
735 (if (and search-nonincremental-instead
736 (= 0 (length isearch-string)))
08200510
RS
737 (let ((isearch-nonincremental t))
738 (isearch-edit-string)))
0352b205
RS
739 (isearch-done)
740 (isearch-clean-overlays))
8e1cae6d
JB
741
742
743(defun isearch-edit-string ()
08200510
RS
744 "Edit the search string in the minibuffer.
745The following additional command keys are active while editing.
746\\<minibuffer-local-isearch-map>
747\\[exit-minibuffer] to resume incremental searching with the edited string.
748\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
749\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 750\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 751\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 752\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 753\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 754\\<isearch-mode-map>
08200510
RS
755If first char entered is \\[isearch-yank-word], then do word search instead."
756
757 ;; This code is very hairy for several reasons, explained in the code.
758 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
759 ;; If there were a way to catch any change of buffer from the minibuffer,
760 ;; this could be simplified greatly.
eb8c3be9 761 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 762 (interactive)
08200510 763 (condition-case err
e900ced4
RS
764 (progn
765 (let ((isearch-nonincremental isearch-nonincremental)
766
767 ;; Locally bind all isearch global variables to protect them
768 ;; from recursive isearching.
769 ;; isearch-string -message and -forward are not bound
770 ;; so they may be changed. Instead, save the values.
771 (isearch-new-string isearch-string)
772 (isearch-new-message isearch-message)
773 (isearch-new-forward isearch-forward)
774 (isearch-new-word isearch-word)
775
776 (isearch-regexp isearch-regexp)
777 (isearch-op-fun isearch-op-fun)
778 (isearch-cmds isearch-cmds)
779 (isearch-success isearch-success)
780 (isearch-wrapped isearch-wrapped)
781 (isearch-barrier isearch-barrier)
782 (isearch-adjusted isearch-adjusted)
783 (isearch-yank-flag isearch-yank-flag)
784 (isearch-invalid-regexp isearch-invalid-regexp)
785 (isearch-within-brackets isearch-within-brackets)
786 ;;; Don't bind this. We want isearch-search, below, to set it.
787 ;;; And the old value won't matter after that.
788 ;;; (isearch-other-end isearch-other-end)
789 ;;; Perhaps some of these other variables should be bound for a
790 ;;; shorter period, ending before the next isearch-search.
791 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
792 (isearch-opoint isearch-opoint)
793 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
794 (isearch-small-window isearch-small-window)
795 (isearch-recursive-edit isearch-recursive-edit)
796 ;; Save current configuration so we can restore it here.
797 (isearch-window-configuration (current-window-configuration))
ae1a21c6
MB
798
799 ;; Temporarily restore `minibuffer-message-timeout'.
800 (minibuffer-message-timeout
801 isearch-original-minibuffer-message-timeout)
802 (isearch-original-minibuffer-message-timeout
803 isearch-original-minibuffer-message-timeout)
e900ced4
RS
804 )
805
806 ;; Actually terminate isearching until editing is done.
807 ;; This is so that the user can do anything without failure,
808 ;; like switch buffers and start another isearch, and return.
809 (condition-case err
810 (isearch-done t t)
811 (exit nil)) ; was recursive editing
812
813 (isearch-message) ;; for read-char
814 (unwind-protect
815 (let* (;; Why does following read-char echo?
816 ;;(echo-keystrokes 0) ;; not needed with above message
817 (e (let ((cursor-in-echo-area t))
818 (read-event)))
819 ;; Binding minibuffer-history-symbol to nil is a work-around
820 ;; for some incompatibility with gmhist.
821 (minibuffer-history-symbol)
822 (message-log-max nil))
823 ;; If the first character the user types when we prompt them
824 ;; for a string is the yank-word character, then go into
825 ;; word-search mode. Otherwise unread that character and
826 ;; read a key the normal way.
827 ;; Word search does not apply (yet) to regexp searches,
828 ;; no check is made here.
829 (message (isearch-message-prefix nil nil t))
830 (if (eq 'isearch-yank-word
831 (lookup-key isearch-mode-map (vector e)))
832 (setq isearch-word t;; so message-prefix is right
833 isearch-new-word t)
834 (cancel-kbd-macro-events)
835 (isearch-unread e))
836 (setq cursor-in-echo-area nil)
837 (setq isearch-new-string
838 (let (junk-ring)
839 (read-from-minibuffer
840 (isearch-message-prefix nil nil isearch-nonincremental)
841 isearch-string
842 minibuffer-local-isearch-map nil
843 'junk-ring))
844 isearch-new-message
845 (mapconcat 'isearch-text-char-description
846 isearch-new-string "")))
847 ;; Always resume isearching by restarting it.
848 (isearch-mode isearch-forward
849 isearch-regexp
850 isearch-op-fun
851 nil
852 isearch-word)
853
854 ;; Copy new local values to isearch globals
855 (setq isearch-string isearch-new-string
856 isearch-message isearch-new-message
857 isearch-forward isearch-new-forward
858 isearch-word isearch-new-word))
859
860 ;; Empty isearch-string means use default.
861 (if (= 0 (length isearch-string))
862 (setq isearch-string (or (car (if isearch-regexp
863 regexp-search-ring
864 search-ring))
865 ""))
866 ;; This used to set the last search string,
867 ;; but I think it is not right to do that here.
868 ;; Only the string actually used should be saved.
869 ))
870
871 ;; Push the state as of before this C-s.
872 (isearch-push-state)
08200510
RS
873
874 ;; Reinvoke the pending search.
08200510
RS
875 (isearch-search)
876 (isearch-update)
877 (if isearch-nonincremental
878 (progn
879 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
880 (isearch-done))))
8e1cae6d 881
08200510
RS
882 (quit ; handle abort-recursive-edit
883 (isearch-abort) ;; outside of let to restore outside global values
884 )))
885
886(defun isearch-nonincremental-exit-minibuffer ()
887 (interactive)
888 (setq isearch-nonincremental t)
889 (exit-minibuffer))
890
891(defun isearch-forward-exit-minibuffer ()
892 (interactive)
893 (setq isearch-new-forward t)
894 (exit-minibuffer))
8e1cae6d 895
08200510 896(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 897 (interactive)
08200510
RS
898 (setq isearch-new-forward nil)
899 (exit-minibuffer))
900
19993c54
RS
901(defun isearch-cancel ()
902 "Terminate the search and go back to the starting point."
903 (interactive)
904 (goto-char isearch-opoint)
905 (isearch-done t)
0352b205 906 (isearch-clean-overlays)
19993c54 907 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
908
909(defun isearch-abort ()
b7852303 910 "Abort incremental search mode if searching is successful, signaling quit.
08200510 911Otherwise, revert to previous successful search and continue searching.
b7852303 912Use `isearch-exit' to quit without signaling."
08200510 913 (interactive)
eb8c3be9 914;; (ding) signal instead below, if quitting
8e1cae6d
JB
915 (discard-input)
916 (if isearch-success
917 ;; If search is successful, move back to starting point
918 ;; and really do quit.
919 (progn (goto-char isearch-opoint)
509ed182 920 (setq isearch-success nil)
35a4d143 921 (isearch-done t) ; exit isearch
0352b205 922 (isearch-clean-overlays)
a5dcf63f 923 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
924 ;; If search is failing, or has an incomplete regexp,
925 ;; rub out until it is once more successful.
926 (while (or (not isearch-success) isearch-invalid-regexp)
927 (isearch-pop-state))
8e1cae6d
JB
928 (isearch-update)))
929
8e1cae6d
JB
930(defun isearch-repeat (direction)
931 ;; Utility for isearch-repeat-forward and -backward.
932 (if (eq isearch-forward (eq direction 'forward))
933 ;; C-s in forward or C-r in reverse.
934 (if (equal isearch-string "")
935 ;; If search string is empty, use last one.
936 (setq isearch-string
8e1cae6d 937 (or (if isearch-regexp
a5dcf63f
RS
938 (car regexp-search-ring)
939 (car search-ring))
8e1cae6d
JB
940 "")
941 isearch-message
08200510 942 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
943 isearch-string ""))
944 ;; If already have what to search for, repeat it.
945 (or isearch-success
02b2f510 946 (progn
8e1cae6d
JB
947 (goto-char (if isearch-forward (point-min) (point-max)))
948 (setq isearch-wrapped t))))
949 ;; C-s in reverse or C-r in forward, change direction.
950 (setq isearch-forward (not isearch-forward)))
951
952 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
953
954 (if (equal isearch-string "")
955 (setq isearch-success t)
99ae9e9f
KH
956 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
957 (not isearch-just-started))
8e1cae6d
JB
958 ;; If repeating a search that found
959 ;; an empty string, ensure we advance.
6fefdc4b
RS
960 (if (if isearch-forward (eobp) (bobp))
961 ;; If there's nowhere to advance to, fail (and wrap next time).
962 (progn
963 (setq isearch-success nil)
964 (ding))
965 (forward-char (if isearch-forward 1 -1))
966 (isearch-search))
967 (isearch-search)))
968
8e1cae6d
JB
969 (isearch-push-state)
970 (isearch-update))
971
972(defun isearch-repeat-forward ()
973 "Repeat incremental search forwards."
974 (interactive)
975 (isearch-repeat 'forward))
976
977(defun isearch-repeat-backward ()
978 "Repeat incremental search backwards."
979 (interactive)
980 (isearch-repeat 'backward))
981
982(defun isearch-toggle-regexp ()
983 "Toggle regexp searching on or off."
984 ;; The status stack is left unchanged.
985 (interactive)
986 (setq isearch-regexp (not isearch-regexp))
08200510 987 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
988 (isearch-update))
989
4453091d
RS
990(defun isearch-toggle-case-fold ()
991 "Toggle case folding in searching on or off."
992 (interactive)
993 (setq isearch-case-fold-search
994 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
995 (let ((message-log-max nil))
996 (message "%s%s [case %ssensitive]"
997 (isearch-message-prefix nil nil isearch-nonincremental)
998 isearch-message
999 (if isearch-case-fold-search "in" "")))
4453091d
RS
1000 (setq isearch-adjusted t)
1001 (sit-for 1)
1002 (isearch-update))
1003
8e1cae6d
JB
1004(defun isearch-delete-char ()
1005 "Discard last input item and move point back.
1006If no previous match was done, just beep."
1007 (interactive)
1008 (if (null (cdr isearch-cmds))
1009 (ding)
1010 (isearch-pop-state))
1011 (isearch-update))
1012
1013
9cf081fa
KH
1014(defun isearch-yank-string (string)
1015 "Pull STRING into search string."
1016 ;; Downcase the string if not supposed to case-fold yanked strings.
1017 (if (and isearch-case-fold-search
1018 (eq 'not-yanks search-upper-case))
1019 (setq string (downcase string)))
1020 (if isearch-regexp (setq string (regexp-quote string)))
1021 (setq isearch-string (concat isearch-string string)
1022 isearch-message
1023 (concat isearch-message
1024 (mapconcat 'isearch-text-char-description
1025 string ""))
1026 ;; Don't move cursor in reverse search.
1027 isearch-yank-flag t)
8e1cae6d
JB
1028 (isearch-search-and-update))
1029
30d47262
RS
1030(defun isearch-yank-kill ()
1031 "Pull string from kill ring into search string."
1032 (interactive)
9cf081fa
KH
1033 (isearch-yank-string (current-kill 0)))
1034
1035(defun isearch-yank-x-selection ()
117132a6 1036 "Pull current X selection into search string."
9cf081fa
KH
1037 (interactive)
1038 (isearch-yank-string (x-get-selection)))
8e1cae6d 1039
117132a6
DL
1040(defun isearch-mouse-yank (click arg)
1041 "Yank with the mouse in Isearch mode.
1042For a click in the echo area, invoke `isearch-yank-x-selection'.
1043Otherwise invoke `mouse-yank-at-click'."
1044 (interactive "e\nP")
1045 (let ((w (posn-window (event-start click))))
1046 (if (and (window-minibuffer-p w)
1047 (not (minibuffer-window-active-p w))) ; in echo area
1048 (isearch-yank-x-selection)
1049 (mouse-yank-at-click click arg))))
1050
8e1cae6d
JB
1051(defun isearch-yank-word ()
1052 "Pull next word from buffer into search string."
1053 (interactive)
9cf081fa
KH
1054 (isearch-yank-string
1055 (save-excursion
1056 (and (not isearch-forward) isearch-other-end
1057 (goto-char isearch-other-end))
c40a4de1
GM
1058 (buffer-substring-no-properties
1059 (point) (progn (forward-word 1) (point))))))
8e1cae6d
JB
1060
1061(defun isearch-yank-line ()
1062 "Pull rest of line from buffer into search string."
1063 (interactive)
9cf081fa
KH
1064 (isearch-yank-string
1065 (save-excursion
1066 (and (not isearch-forward) isearch-other-end
1067 (goto-char isearch-other-end))
c40a4de1 1068 (buffer-substring-no-properties (point) (line-end-position)))))
8e1cae6d
JB
1069
1070
1071(defun isearch-search-and-update ()
1072 ;; Do the search and update the display.
1073 (if (and (not isearch-success)
1074 ;; unsuccessful regexp search may become
1075 ;; successful by addition of characters which
1076 ;; make isearch-string valid
1077 (not isearch-regexp))
1078 nil
1079 ;; In reverse search, adding stuff at
1080 ;; the end may cause zero or many more chars to be
1081 ;; matched, in the string following point.
1082 ;; Allow all those possibilities without moving point as
1083 ;; long as the match does not extend past search origin.
1084 (if (and (not isearch-forward) (not isearch-adjusted)
1085 (condition-case ()
99ae9e9f 1086 (let ((case-fold-search isearch-case-fold-search))
ec0a2f45
KH
1087 (if (and (eq case-fold-search t) search-upper-case)
1088 (setq case-fold-search
1089 (isearch-no-upper-case-p isearch-string isearch-regexp)))
99ae9e9f
KH
1090 (looking-at (if isearch-regexp isearch-string
1091 (regexp-quote isearch-string))))
8e1cae6d 1092 (error nil))
bd6a8414
RS
1093 (or isearch-yank-flag
1094 (<= (match-end 0)
1095 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1096 (progn
1097 (setq isearch-success t
1098 isearch-invalid-regexp nil
1099 isearch-within-brackets nil
1100 isearch-other-end (match-end 0))
1101 (if (and (eq isearch-case-fold-search t) search-upper-case)
1102 (setq isearch-case-fold-search
1103 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1104 ;; Not regexp, not reverse, or no match at point.
1105 (if (and isearch-other-end (not isearch-adjusted))
1106 (goto-char (if isearch-forward isearch-other-end
1107 (min isearch-opoint
1108 isearch-barrier
1109 (1+ isearch-other-end)))))
1110 (isearch-search)
1111 ))
1112 (isearch-push-state)
1113 (if isearch-op-fun (funcall isearch-op-fun))
1114 (isearch-update))
1115
1116
1117;; *, ?, and | chars can make a regexp more liberal.
08200510 1118;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1119;; So go back to place last successful search started
1120;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1121;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1122
1123(defun isearch-*-char ()
1124 "Handle * and ? specially in regexps."
1125 (interactive)
1126 (if isearch-regexp
bd6a8414
RS
1127 (let ((idx (length isearch-string)))
1128 (while (and (> idx 0)
1129 (eq (aref isearch-string (1- idx)) ?\\))
1130 (setq idx (1- idx)))
1131 (when (= (mod (- (length isearch-string) idx) 2) 0)
1132 (setq isearch-adjusted t)
1133 ;; Get the isearch-other-end from before the last search.
1134 ;; We want to start from there,
1135 ;; so that we don't retreat farther than that.
1136 ;; (car isearch-cmds) is after last search;
1137 ;; (car (cdr isearch-cmds)) is from before it.
1138 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1139 (setq cs (or cs isearch-barrier))
1140 (goto-char
1141 (if isearch-forward
1142 (max cs isearch-barrier)
1143 (min cs isearch-barrier)))))))
117132a6 1144 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1145
1146
8e1cae6d
JB
1147(defun isearch-|-char ()
1148 "If in regexp search, jump to the barrier."
1149 (interactive)
1150 (if isearch-regexp
1151 (progn
1152 (setq isearch-adjusted t)
1153 (goto-char isearch-barrier)))
117132a6 1154 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1155
1156
b457cc3b 1157(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1158
1159(defun isearch-other-meta-char ()
8f90f594 1160 "Exit the search normally and reread this key sequence.
35a4d143
RS
1161But only if `search-exit-option' is non-nil, the default.
1162If it is the symbol `edit', the search string is edited in the minibuffer
1163and the meta character is unread so that it applies to editing the string."
8e1cae6d 1164 (interactive)
c6431f12
KH
1165 (let* ((key (this-command-keys))
1166 (main-event (aref key 0))
1167 (keylist (listify-key-sequence key)))
24b5caec 1168 (cond ((and (= (length key) 1)
d94eb6eb 1169 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1170 (not (or (null lookup) (integerp lookup)
1171 (keymapp lookup)))))
24b5caec
RS
1172 ;; Handle a function key that translates into something else.
1173 ;; If the key has a global definition too,
1174 ;; exit and unread the key itself, so its global definition runs.
1175 ;; Otherwise, unread the translation,
1176 ;; so that the translated key takes effect within isearch.
d94eb6eb 1177 (cancel-kbd-macro-events)
24b5caec 1178 (if (lookup-key global-map key)
02b2f510 1179 (progn
24b5caec
RS
1180 (isearch-done)
1181 (apply 'isearch-unread keylist))
a5cc922e
KH
1182 (setq keylist
1183 (listify-key-sequence (lookup-key function-key-map key)))
1184 (while keylist
1185 (setq key (car keylist))
1186 ;; If KEY is a printing char, we handle it here
1187 ;; directly to avoid the input method and keyboard
1188 ;; coding system translating it.
1189 (if (and (integerp key)
ed5c183b 1190 (>= key ?\ ) (/= key 127) (< key 256))
a5cc922e
KH
1191 (progn
1192 (isearch-process-search-char key)
1193 (setq keylist (cdr keylist)))
1194 ;; As the remaining keys in KEYLIST can't be handled
1195 ;; here, we must reread them.
1196 (apply 'isearch-unread keylist)
1197 (setq keylist nil)))))
24b5caec 1198 (
c6431f12
KH
1199 ;; Handle an undefined shifted control character
1200 ;; by downshifting it if that makes it defined.
1201 ;; (As read-key-sequence would normally do,
1202 ;; if we didn't have a default definition.)
1203 (let ((mods (event-modifiers main-event)))
1204 (and (integerp main-event)
1205 (memq 'shift mods)
1206 (memq 'control mods)
1207 (lookup-key isearch-mode-map
1208 (let ((copy (copy-sequence key)))
1209 (aset copy 0
1210 (- main-event (- ?\C-\S-a ?\C-a)))
1211 copy)
1212 nil)))
1213 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1214 (cancel-kbd-macro-events)
c6431f12
KH
1215 (apply 'isearch-unread keylist))
1216 ((eq search-exit-option 'edit)
1217 (apply 'isearch-unread keylist)
1218 (isearch-edit-string))
1219 (search-exit-option
1220 (let (window)
d94eb6eb 1221 (cancel-kbd-macro-events)
c6431f12
KH
1222 (apply 'isearch-unread keylist)
1223 ;; Properly handle scroll-bar and mode-line clicks
1224 ;; for which a dummy prefix event was generated as (aref key 0).
1225 (and (> (length key) 1)
1226 (symbolp (aref key 0))
1227 (listp (aref key 1))
1228 (not (numberp (posn-point (event-start (aref key 1)))))
1229 ;; Convert the event back into its raw form,
1230 ;; with the dummy prefix implicit in the mouse event,
1231 ;; so it will get split up once again.
1232 (progn (setq unread-command-events
1233 (cdr unread-command-events))
1234 (setq main-event (car unread-command-events))
1235 (setcar (cdr (event-start main-event))
1236 (car (nth 1 (event-start main-event))))))
1237 ;; If we got a mouse click, maybe it was read with the buffer
1238 ;; it was clicked on. If so, that buffer, not the current one,
1239 ;; is in isearch mode. So end the search in that buffer.
1240 (if (and (listp main-event)
1241 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1242 (windowp window)
1243 (or (> (minibuffer-depth) 0)
1244 (not (window-minibuffer-p window))))
c6431f12
KH
1245 (save-excursion
1246 (set-buffer (window-buffer window))
0352b205
RS
1247 (isearch-done)
1248 (isearch-clean-overlays))
1249 (isearch-done)
1250 (isearch-clean-overlays))))
c6431f12
KH
1251 (t;; otherwise nil
1252 (isearch-process-search-string key key)))))
8e1cae6d 1253
8e1cae6d
JB
1254(defun isearch-quote-char ()
1255 "Quote special characters for incremental search."
1256 (interactive)
8bc15fa8 1257 (let ((char (read-quoted-char (isearch-message t))))
3c75023f 1258 ;; Assume character codes 0200 - 0377 stand for characters in some
c20d35d5
KH
1259 ;; single-byte character set, and convert them to Emacs
1260 ;; characters.
8bc15fa8 1261 (and enable-multibyte-characters
3c75023f 1262 (>= char ?\200)
8bc15fa8 1263 (<= char ?\377)
c20d35d5 1264 (setq char (unibyte-char-to-multibyte char)))
8bc15fa8 1265 (isearch-process-search-char char)))
8e1cae6d 1266
8e1cae6d
JB
1267(defun isearch-return-char ()
1268 "Convert return into newline for incremental search.
1269Obsolete."
1270 (interactive)
1271 (isearch-process-search-char ?\n))
1272
8e1cae6d 1273(defun isearch-printing-char ()
b68c164d 1274 "Add this ordinary printing character to the search string and search."
8e1cae6d 1275 (interactive)
117132a6 1276 (let ((char last-command-char))
45b94eb2
KH
1277 (if (= char ?\S-\ )
1278 (setq char ?\ ))
af56433d
RS
1279 (if (and enable-multibyte-characters
1280 (>= char ?\200)
1281 (<= char ?\377))
3c487103
KH
1282 (if (keyboard-coding-system)
1283 (isearch-process-search-multibyte-characters char)
1284 (isearch-process-search-char (unibyte-char-to-multibyte char)))
af56433d
RS
1285 (if current-input-method
1286 (isearch-process-search-multibyte-characters char)
1287 (isearch-process-search-char char)))))
8e1cae6d
JB
1288
1289(defun isearch-whitespace-chars ()
08200510 1290 "Match all whitespace chars, if in regexp mode.
117132a6 1291If you want to search for just a space, type \\[quoted-insert] SPC."
8e1cae6d 1292 (interactive)
08200510 1293 (if isearch-regexp
9da6682f
RS
1294 (if (and search-whitespace-regexp (not isearch-within-brackets)
1295 (not isearch-invalid-regexp))
08200510
RS
1296 (isearch-process-search-string search-whitespace-regexp " ")
1297 (isearch-printing-char))
1298 (progn
eb8c3be9 1299 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1300 ;; (setq isearch-word t)
1301 ;; (setq isearch-adjusted t)
1302 ;; (goto-char isearch-barrier)
1303 (isearch-printing-char))))
8e1cae6d
JB
1304
1305(defun isearch-process-search-char (char)
1306 ;; Append the char to the search string, update the message and re-search.
08200510 1307 (isearch-process-search-string
117132a6 1308 (char-to-string char)
8f3c1d76 1309 (if (>= char ?\200)
01a6ef79
RS
1310 (char-to-string char)
1311 (isearch-text-char-description char))))
8e1cae6d
JB
1312
1313(defun isearch-process-search-string (string message)
1314 (setq isearch-string (concat isearch-string string)
1315 isearch-message (concat isearch-message message))
1316 (isearch-search-and-update))
1317
1318\f
8e1cae6d
JB
1319;; Search Ring
1320
08200510
RS
1321(defun isearch-ring-adjust1 (advance)
1322 ;; Helper for isearch-ring-adjust
1323 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1324 (length (length ring))
1325 (yank-pointer-name (if isearch-regexp
08200510 1326 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1327 'search-ring-yank-pointer))
1328 (yank-pointer (eval yank-pointer-name)))
1329 (if (zerop length)
1330 ()
1331 (set yank-pointer-name
1332 (setq yank-pointer
ffbc30b2
PE
1333 (mod (+ (or yank-pointer 0)
1334 (if advance -1 1))
1335 length)))
a5dcf63f 1336 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1337 isearch-message (mapconcat 'isearch-text-char-description
1338 isearch-string "")))))
1339
1340(defun isearch-ring-adjust (advance)
1341 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1342 (isearch-ring-adjust1 advance)
08200510
RS
1343 (if search-ring-update
1344 (progn
1345 (isearch-search)
1346 (isearch-update))
1347 (isearch-edit-string)
2ff20b7e
RS
1348 )
1349 (isearch-push-state))
8e1cae6d
JB
1350
1351(defun isearch-ring-advance ()
1352 "Advance to the next search string in the ring."
08200510 1353 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1354 (interactive)
1355 (isearch-ring-adjust 'advance))
1356
1357(defun isearch-ring-retreat ()
1358 "Retreat to the previous search string in the ring."
1359 (interactive)
1360 (isearch-ring-adjust nil))
1361
a5dcf63f 1362(defun isearch-ring-advance-edit (n)
117132a6
DL
1363 "Insert the next element of the search history into the minibuffer.
1364With prefix arg N, insert the Nth element."
a5dcf63f
RS
1365 (interactive "p")
1366 (let* ((yank-pointer-name (if isearch-regexp
1367 'regexp-search-ring-yank-pointer
1368 'search-ring-yank-pointer))
1369 (yank-pointer (eval yank-pointer-name))
1370 (ring (if isearch-regexp regexp-search-ring search-ring))
1371 (length (length ring)))
1372 (if (zerop length)
1373 ()
1374 (set yank-pointer-name
1375 (setq yank-pointer
ffbc30b2
PE
1376 (mod (- (or yank-pointer 0) n)
1377 length)))
a5dcf63f 1378
b7b66466 1379 (delete-field)
35a4d143 1380 (insert (nth yank-pointer ring))
49c13105 1381 (goto-char (point-max)))))
a5dcf63f
RS
1382
1383(defun isearch-ring-retreat-edit (n)
117132a6
DL
1384 "Insert the previous element of the search history into the minibuffer.
1385With prefix arg N, insert the Nth element."
a5dcf63f
RS
1386 (interactive "p")
1387 (isearch-ring-advance-edit (- n)))
1388
1389;;(defun isearch-ring-adjust-edit (advance)
1390;; "Use the next or previous search string in the ring while in minibuffer."
1391;; (isearch-ring-adjust1 advance)
1392;; (erase-buffer)
1393;; (insert isearch-string))
1394
1395;;(defun isearch-ring-advance-edit ()
1396;; (interactive)
1397;; (isearch-ring-adjust-edit 'advance))
1398
1399;;(defun isearch-ring-retreat-edit ()
1400;; "Retreat to the previous search string in the ring while in the minibuffer."
1401;; (interactive)
1402;; (isearch-ring-adjust-edit nil))
08200510
RS
1403
1404
1405(defun isearch-complete1 ()
1406 ;; Helper for isearch-complete and isearch-complete-edit
1407 ;; Return t if completion OK, nil if no completion exists.
1408 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1409 (alist (mapcar (function (lambda (string) (list string))) ring))
1410 (completion-ignore-case case-fold-search)
1411 (completion (try-completion isearch-string alist)))
1412 (cond
1413 ((eq completion t)
1414 ;; isearch-string stays the same
1415 t)
1416 ((or completion ; not nil, must be a string
b7852303 1417 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1418 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1419 (progn
1420 (if completion-auto-help
1421 (with-output-to-temp-buffer "*Isearch completions*"
1422 (display-completion-list
1423 (all-completions isearch-string alist))))
1424 t)
1425 (and completion
1426 (setq isearch-string completion))))
08200510
RS
1427 (t
1428 (message "No completion") ; waits a second if in minibuffer
1429 nil))))
1430
1431(defun isearch-complete ()
1432 "Complete the search string from the strings on the search ring.
1433The completed string is then editable in the minibuffer.
1434If there is no completion possible, say so and continue searching."
1435 (interactive)
1436 (if (isearch-complete1)
1437 (isearch-edit-string)
1438 ;; else
1439 (sit-for 1)
1440 (isearch-update)))
8e1cae6d 1441
08200510
RS
1442(defun isearch-complete-edit ()
1443 "Same as `isearch-complete' except in the minibuffer."
1444 (interactive)
1445 (setq isearch-string (buffer-string))
1446 (if (isearch-complete1)
8e1cae6d 1447 (progn
b7b66466 1448 (delete-field)
08200510 1449 (insert isearch-string))))
8e1cae6d
JB
1450
1451\f
8e1cae6d 1452;; The search status stack (and isearch window-local variables, not used).
08200510 1453;; Need a structure for this.
8e1cae6d
JB
1454
1455(defun isearch-top-state ()
8e1cae6d
JB
1456 (let ((cmd (car isearch-cmds)))
1457 (setq isearch-string (car cmd)
1458 isearch-message (car (cdr cmd))
1459 isearch-success (nth 3 cmd)
1460 isearch-forward (nth 4 cmd)
1461 isearch-other-end (nth 5 cmd)
08200510
RS
1462 isearch-word (nth 6 cmd)
1463 isearch-invalid-regexp (nth 7 cmd)
1464 isearch-wrapped (nth 8 cmd)
c5f6b356 1465 isearch-barrier (nth 9 cmd)
f551b97d
RS
1466 isearch-within-brackets (nth 10 cmd)
1467 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1468 (goto-char (car (cdr (cdr cmd))))))
1469
1470(defun isearch-pop-state ()
8e1cae6d 1471 (setq isearch-cmds (cdr isearch-cmds))
1f4139d5 1472 (isearch-top-state))
8e1cae6d
JB
1473
1474(defun isearch-push-state ()
1475 (setq isearch-cmds
1476 (cons (list isearch-string isearch-message (point)
1477 isearch-success isearch-forward isearch-other-end
08200510 1478 isearch-word
c5f6b356 1479 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1480 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1481 isearch-cmds)))
1482
8e1cae6d 1483\f
8e1cae6d
JB
1484;; Message string
1485
1486(defun isearch-message (&optional c-q-hack ellipsis)
1487 ;; Generate and print the message string.
1488 (let ((cursor-in-echo-area ellipsis)
1489 (m (concat
08200510 1490 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1491 isearch-message
1492 (isearch-message-suffix c-q-hack ellipsis)
1493 )))
a56687f1
KH
1494 (if c-q-hack
1495 m
1496 (let ((message-log-max nil))
1497 (message "%s" m)))))
8e1cae6d 1498
08200510 1499(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1500 ;; If about to search, and previous search regexp was invalid,
1501 ;; check that it still is. If it is valid now,
1502 ;; let the message we display while searching say that it is valid.
1503 (and isearch-invalid-regexp ellipsis
1504 (condition-case ()
1505 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1506 (setq isearch-invalid-regexp nil
1507 isearch-within-brackets nil))
8e1cae6d
JB
1508 (error nil)))
1509 ;; If currently failing, display no ellipsis.
1510 (or isearch-success (setq ellipsis nil))
1511 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1512 (if (and isearch-wrapped
1513 (if isearch-forward
1514 (> (point) isearch-opoint)
1515 (< (point) isearch-opoint)))
1516 "over")
8e1cae6d 1517 (if isearch-wrapped "wrapped ")
08200510 1518 (if isearch-word "word " "")
8e1cae6d 1519 (if isearch-regexp "regexp " "")
08200510 1520 (if nonincremental "search" "I-search")
f46d5091 1521 (if isearch-forward "" " backward")
5b56d4e4
KH
1522 (if current-input-method
1523 (concat " [" current-input-method-title "]: ")
f46d5091 1524 ": ")
8e1cae6d 1525 )))
c5def0db 1526 (concat (upcase (substring m 0 1)) (substring m 1))))
8e1cae6d
JB
1527
1528
1529(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1530 (concat (if c-q-hack "^Q" "")
1531 (if isearch-invalid-regexp
1532 (concat " [" isearch-invalid-regexp "]")
1533 "")))
1534
1535\f
8e1cae6d
JB
1536;;; Searching
1537
1538(defun isearch-search ()
1539 ;; Do the search with the current search string.
1540 (isearch-message nil t)
4453091d 1541 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1542 (setq isearch-case-fold-search
1543 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1544 (condition-case lossage
79c7a4fa
RS
1545 (let ((inhibit-point-motion-hooks search-invisible)
1546 (inhibit-quit nil)
86bfaffe
RS
1547 (case-fold-search isearch-case-fold-search)
1548 (retry t))
8e1cae6d 1549 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1550 (setq isearch-within-brackets nil)
86bfaffe
RS
1551 (while retry
1552 (setq isearch-success
1553 (funcall
1554 (cond (isearch-word
1555 (if isearch-forward
1556 'word-search-forward 'word-search-backward))
1557 (isearch-regexp
1558 (if isearch-forward
1559 're-search-forward 're-search-backward))
1560 (t
1561 (if isearch-forward 'search-forward 'search-backward)))
1562 isearch-string nil t))
1563 ;; Clear RETRY unless we matched some invisible text
1564 ;; and we aren't supposed to do that.
0352b205 1565 (if (or (eq search-invisible t)
86bfaffe
RS
1566 (not isearch-success)
1567 (bobp) (eobp)
1568 (= (match-beginning 0) (match-end 0))
1569 (not (isearch-range-invisible
1570 (match-beginning 0) (match-end 0))))
1571 (setq retry nil)))
99ae9e9f 1572 (setq isearch-just-started nil)
8e1cae6d
JB
1573 (if isearch-success
1574 (setq isearch-other-end
1575 (if isearch-forward (match-beginning 0) (match-end 0)))))
1576
d32696d4 1577 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1578 (setq isearch-success nil))
1579
1580 (invalid-regexp
1581 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1582 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1583 isearch-invalid-regexp))
8e1cae6d
JB
1584 (if (string-match
1585 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1586 isearch-invalid-regexp)
f1c03125
RS
1587 (setq isearch-invalid-regexp "incomplete input")))
1588 (error
c982ab21 1589 ;; stack overflow in regexp search.
c5348233 1590 (setq isearch-invalid-regexp (format "%s" lossage))))
8e1cae6d
JB
1591
1592 (if isearch-success
1593 nil
1594 ;; Ding if failed this time after succeeding last time.
1595 (and (nth 3 (car isearch-cmds))
1596 (ding))
1597 (goto-char (nth 2 (car isearch-cmds)))))
1598
0352b205
RS
1599
1600;;; Called when opening an overlay, and we are still in isearch.
1601(defun isearch-open-overlay-temporary (ov)
0352b205
RS
1602 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1603 ;; Some modes would want to open the overlays temporary during
1604 ;; isearch in their own way, they should set the
1605 ;; `isearch-open-invisible-temporary' to a function doing this.
1606 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1607 ;; Store the values for the `invisible' and `intangible'
1608 ;; properties, and then set them to nil. This way the text hidden
1609 ;; by this overlay becomes visible.
1610
380866a2 1611 ;; Do we really need to set the `intangible' property to t? Can we
0352b205
RS
1612 ;; have the point inside an overlay with an `intangible' property?
1613 ;; In 19.34 this does not exist so I cannot test it.
1614 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1615 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1616 (overlay-put ov 'invisible nil)
1617 (overlay-put ov 'intangible nil)))
1618
1619
117132a6
DL
1620;;; This is called at the end of isearch. It will open the overlays
1621;;; that contain the latest match. Obviously in case of a C-g the
0352b205
RS
1622;;; point returns to the original location which surely is not contain
1623;;; in any of these overlays, se we are safe in this case too.
1624(defun isearch-open-necessary-overlays (ov)
1625 (let ((inside-overlay (and (> (point) (overlay-start ov))
1626 (< (point) (overlay-end ov))))
1627 ;; If this exists it means that the overlay was opened using
1628 ;; this function, not by us tweaking the overlay properties.
1629 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1630 (when (or inside-overlay (not fct-temp))
1631 ;; restore the values for the `invisible' and `intangible'
1632 ;; properties
1633 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1634 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1635 (overlay-put ov 'isearch-invisible nil)
1636 (overlay-put ov 'isearch-intangible nil))
1637 (if inside-overlay
1638 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1639 (if fct-temp
1640 (funcall fct-temp ov t)))))
1641
1642;;; This is called when exiting isearch. It closes the temporary
1643;;; opened overlays, except the ones that contain the latest match.
1644(defun isearch-clean-overlays ()
1645 (when isearch-opened-overlays
02b2f510 1646 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
0352b205
RS
1647 (setq isearch-opened-overlays nil)))
1648
1649;;; Verify if the current match is outside of each element of
1650;;; `isearch-opened-overlays', if so close that overlay.
1651(defun isearch-close-unecessary-overlays (begin end)
1652 (let ((ov-list isearch-opened-overlays)
1653 ov
1654 inside-overlay
1655 fct-temp)
1656 (setq isearch-opened-overlays nil)
1657 (while ov-list
1658 (setq ov (car ov-list))
1659 (setq ov-list (cdr ov-list))
1660 (setq inside-overlay (or (and (> begin (overlay-start ov))
1661 (< begin (overlay-end ov)))
1662 (and (> end (overlay-start ov))
1663 (< end (overlay-end ov)))))
1664 ;; If this exists it means that the overlay was opened using
1665 ;; this function, not by us tweaking the overlay properties.
1666 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1667 (if inside-overlay
02b2f510 1668 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
0352b205
RS
1669 (if fct-temp
1670 (funcall fct-temp ov t)
1671 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1672 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1673 (overlay-put ov 'isearch-invisible nil)
1674 (overlay-put ov 'isearch-intangible nil))))))
1675
86bfaffe 1676(defun isearch-range-invisible (beg end)
79c7a4fa 1677 "Return t if all the text from BEG to END is invisible."
86bfaffe
RS
1678 (and (/= beg end)
1679 ;; Check that invisibility runs up to END.
1680 (save-excursion
1681 (goto-char beg)
df01192b
RS
1682 (let (
1683 ;; can-be-opened keeps track if we can open some overlays.
1684 (can-be-opened (eq search-invisible 'open))
1685 ;; the list of overlays that could be opened
1686 (crt-overlays nil))
0352b205 1687 (when (and can-be-opened isearch-hide-immediately)
df01192b 1688 (isearch-close-unecessary-overlays beg end))
0352b205
RS
1689 ;; If the following character is currently invisible,
1690 ;; skip all characters with that same `invisible' property value.
1691 ;; Do that over and over.
1692 (while (and (< (point) end)
1693 (let ((prop
1694 (get-char-property (point) 'invisible)))
1695 (if (eq buffer-invisibility-spec t)
1696 prop
1697 (or (memq prop buffer-invisibility-spec)
1698 (assq prop buffer-invisibility-spec)))))
1699 (if (get-text-property (point) 'invisible)
1700 (progn
1701 (goto-char (next-single-property-change (point) 'invisible
1702 nil end))
1703 ;; if text is hidden by an `invisible' text property
1704 ;; we cannot open it at all.
1705 (setq can-be-opened nil))
1706 (unless (null can-be-opened)
1707 (let ((overlays (overlays-at (point)))
1708 ov-list
1709 o
1710 invis-prop)
1711 (while overlays
1712 (setq o (car overlays)
1713 invis-prop (overlay-get o 'invisible))
de207f5a
KH
1714 (if (if (eq buffer-invisibility-spec t)
1715 invis-prop
1716 (or (memq invis-prop buffer-invisibility-spec)
1717 (assq invis-prop buffer-invisibility-spec)))
0352b205
RS
1718 (if (overlay-get o 'isearch-open-invisible)
1719 (setq ov-list (cons o ov-list))
1720 ;; We found one overlay that cannot be
1721 ;; opened, that means the whole chunk
1722 ;; cannot be opened.
1723 (setq can-be-opened nil)))
1724 (setq overlays (cdr overlays)))
1725 (if can-be-opened
1726 ;; It makes sense to append to the open
1727 ;; overlays list only if we know that this is
1728 ;; t.
7e97482b
RS
1729 (setq crt-overlays (append ov-list crt-overlays)))))
1730 (goto-char (next-overlay-change (point)))))
86bfaffe 1731 ;; See if invisibility reaches up thru END.
0352b205
RS
1732 (if (>= (point) end)
1733 (if (and (not (null can-be-opened)) (consp crt-overlays))
1734 (progn
1735 (setq isearch-opened-overlays
1736 (append isearch-opened-overlays crt-overlays))
02b2f510 1737 (mapc 'isearch-open-overlay-temporary crt-overlays)
0352b205
RS
1738 nil)
1739 t))))))
08200510
RS
1740
1741\f
08200510
RS
1742;;; Highlighting
1743
b78559b0 1744(defvar isearch-overlay nil)
08200510 1745
b78559b0 1746(defun isearch-highlight (beg end)
4fb2ad98
MB
1747 (unless (or (null search-highlight) (null (display-color-p)))
1748 (cond (isearch-overlay
1749 ;; Overlay already exists, just move it.
4fb2ad98
MB
1750 (move-overlay isearch-overlay beg end (current-buffer)))
1751
1752 (t
1753 ;; Overlay doesn't exist, create it.
1754 (setq isearch-overlay (make-overlay beg end))
c982ab21
GM
1755 (overlay-put isearch-overlay 'face isearch)
1756 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
1757 ))))
b78559b0
RS
1758
1759(defun isearch-dehighlight (totally)
4fb2ad98 1760 (when isearch-overlay
4fb2ad98
MB
1761 (delete-overlay isearch-overlay)))
1762
08200510 1763
08200510
RS
1764;;; General utilities
1765
08200510 1766
b78559b0
RS
1767(defun isearch-no-upper-case-p (string regexp-flag)
1768 "Return t if there are no upper case chars in STRING.
b7852303 1769If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1770since they have special meaning in a regexp."
59057198
RS
1771 (let (quote-flag (i 0) (len (length string)) found)
1772 (while (and (not found) (< i len))
1773 (let ((char (aref string i)))
1774 (if (and regexp-flag (eq char ?\\))
1775 (setq quote-flag (not quote-flag))
1776 (if (and (not quote-flag) (not (eq char (downcase char))))
1777 (setq found t))))
1778 (setq i (1+ i)))
1779 (not found)))
08200510 1780
b78559b0 1781;; Portability functions to support various Emacs versions.
8e1cae6d 1782
08200510 1783(defun isearch-text-char-description (c)
14373677
SM
1784 (cond
1785 ((< c ?\ ) (format "^%c" (+ c 64)))
1786 ((= c ?\^?) "^?")
1787 (t (char-to-string c))))
08200510 1788
bd1bd125 1789;; General function to unread characters or events.
99ae9e9f 1790;; Also insert them in a keyboard macro being defined.
8f90f594 1791(defun isearch-unread (&rest char-or-events)
02b2f510 1792 (mapc 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1793 (setq unread-command-events
1794 (append char-or-events unread-command-events)))
08200510 1795
44336afb
GM
1796\f
1797;;; isearch-lazy-highlight feature
1798;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1799
1800;;; When active, *every* match for the current search string is
1801;;; highlighted: the current one using the normal isearch match color
c982ab21
GM
1802;;; and all the others using `isearch-lazy-highlight-face'. The extra
1803;;; highlighting makes it easier to anticipate where the cursor will
1804;;; land each time you press C-s or C-r to repeat a pending search.
1805;;; Highlighting of these additional matches happens in a deferred
1806;;; fashion using "idle timers," so the cycles needed do not rob
1807;;; isearch of its usual snappy response.
44336afb
GM
1808
1809;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1810;;; Specifically:
1811;;; - `isearch-update' is expected to be called (at least) every time
c982ab21 1812;;; the search string or window-start changes;
44336afb
GM
1813;;; - `isearch-string' is expected to contain the current search
1814;;; string as entered by the user;
44336afb
GM
1815;;; - the type of the current search is expected to be given by
1816;;; `isearch-word' and `isearch-regexp';
1817;;; - the direction of the current search is expected to be given by
1818;;; `isearch-forward';
1819;;; - the variable `isearch-invalid-regexp' is expected to be true
1820;;; iff `isearch-string' is an invalid regexp.
1821
1822(require 'timer)
1823
1824(defgroup isearch-lazy-highlight nil
1825 "Lazy highlighting feature for incremental search."
1826 :prefix "isearch-lazy-highlight-"
39f2174e 1827 :version "21.1"
44336afb
GM
1828 :group 'isearch)
1829
1830(defcustom isearch-lazy-highlight t
1831 "*Controls the lazy-highlighting during incremental searches.
1832When non-nil, all text in the buffer matching the current search
1833string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1834and `isearch-lazy-highlight-interval')."
1835 :type 'boolean
1836 :group 'isearch-lazy-highlight)
1837
1838(defcustom isearch-lazy-highlight-cleanup t
1839 "*Controls whether to remove extra highlighting after a search.
1840If this is nil, extra highlighting can be \"manually\" removed with
1841\\[isearch-lazy-highlight-cleanup]."
1842 :type 'boolean
1843 :group 'isearch-lazy-highlight)
1844
b8fbd474 1845(defcustom isearch-lazy-highlight-initial-delay 0 ; 0.25
44336afb
GM
1846 "*Seconds to wait before beginning to lazily highlight all matches."
1847 :type 'number
1848 :group 'isearch-lazy-highlight)
1849
b8fbd474 1850(defcustom isearch-lazy-highlight-interval 0 ; 0.0625
44336afb
GM
1851 "*Seconds between lazily highlighting successive matches."
1852 :type 'number
1853 :group 'isearch-lazy-highlight)
1854
c982ab21
GM
1855(defcustom isearch-lazy-highlight-max-at-a-time 20
1856 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
1857Larger values may reduce isearch's responsiveness to user input;
1858smaller values make matches highlight slowly.
1859A value of nil means highlight all matches."
68ebbb95
GM
1860 :type '(choice (const :tag "All" nil)
1861 (integer :tag "Some"))
1862 :group 'isearch-lazy-highlight)
1863
18ca20aa
GM
1864(defgroup isearch-faces nil
1865 "Lazy highlighting feature for incremental search."
1866 :version "21.1"
1867 :group 'isearch)
1868
1869(defface isearch
29910493
EZ
1870 '((((type tty pc) (class color))
1871 (:background "magenta4" :foreground "cyan1"))
1872 (((class color) (background light))
1873 (:background "magenta4" :foreground "lightskyblue1"))
1874 (((class color) (background dark))
1875 (:background "palevioletred2" :foreground "brown4"))
1876 (t (:inverse-video t)))
1877 "Face for highlighting Isearch matches."
18ca20aa
GM
1878 :group 'isearch-faces)
1879(defvar isearch 'isearch)
1880
1881(defface isearch-lazy-highlight-face
29910493
EZ
1882 '((((type tty pc) (class color))
1883 (:background "turquoise3"))
1884 (((class color) (background light))
1885 (:background "paleturquoise"))
1886 (((class color) (background dark))
1887 (:background "paleturquoise4"))
1888 (t (:underline t)))
1889 "Face for lazy highlighting of Isearch matches other than the current one."
18ca20aa
GM
1890 :group 'isearch-faces)
1891(defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
44336afb
GM
1892
1893(defvar isearch-lazy-highlight-overlays nil)
c982ab21 1894(defvar isearch-lazy-highlight-wrapped nil)
44336afb
GM
1895(defvar isearch-lazy-highlight-start nil)
1896(defvar isearch-lazy-highlight-end nil)
1897(defvar isearch-lazy-highlight-timer nil)
1898(defvar isearch-lazy-highlight-last-string nil)
c982ab21
GM
1899(defvar isearch-lazy-highlight-window nil)
1900(defvar isearch-lazy-highlight-window-start nil)
44336afb 1901
c982ab21
GM
1902(defun isearch-lazy-highlight-cleanup (&optional force)
1903 "Stop lazy highlighting and remove extra highlighting from current buffer.
1904FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
1905is nil. This function is called when exiting an incremental search if
1906`isearch-lazy-highlight-cleanup' is non-nil."
44336afb 1907 (interactive '(t))
c982ab21
GM
1908 (if (or force isearch-lazy-highlight-cleanup)
1909 (while isearch-lazy-highlight-overlays
1910 (delete-overlay (car isearch-lazy-highlight-overlays))
1911 (setq isearch-lazy-highlight-overlays
1912 (cdr isearch-lazy-highlight-overlays))))
1913 (when isearch-lazy-highlight-timer
1914 (cancel-timer isearch-lazy-highlight-timer)
1915 (setq isearch-lazy-highlight-timer nil)))
44336afb
GM
1916
1917(defun isearch-lazy-highlight-new-loop ()
c982ab21 1918 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
44336afb 1919This happens when `isearch-update' is invoked (which can cause the
c982ab21 1920search string to change or the window to scroll)."
67ecad4e 1921 (when (and isearch-lazy-highlight
c982ab21
GM
1922 (sit-for 0) ;make sure (window-start) is credible
1923 (or (not (equal isearch-string
1924 isearch-lazy-highlight-last-string))
1925 (not (eq (selected-window)
1926 isearch-lazy-highlight-window))
1927 (not (= (window-start)
1928 isearch-lazy-highlight-window-start))))
1929 ;; something important did indeed change
1930 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
1931 (when (not isearch-invalid-regexp)
1932 (setq isearch-lazy-highlight-window (selected-window)
1933 isearch-lazy-highlight-window-start (window-start)
1934 isearch-lazy-highlight-start (point)
1935 isearch-lazy-highlight-end (point)
1936 isearch-lazy-highlight-last-string isearch-string
1937 isearch-lazy-highlight-wrapped nil)
1938 (setq isearch-lazy-highlight-timer
1939 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
1940 'isearch-lazy-highlight-update)))))
1941
1942(defun isearch-lazy-highlight-search ()
1943 "Search ahead for the next or previous match, for lazy highlighting.
1944Attempt to do the search exactly the way the pending isearch would."
1945 (let ((case-fold-search isearch-case-fold-search)
1946 (choices (cond (isearch-word
1947 '(word-search-forward . word-search-backward))
1948 (isearch-regexp
1949 '(re-search-forward . re-search-backward))
1950 (t
1951 '(search-forward . search-backward)))))
1952 (funcall (if isearch-forward
1953 (car choices)
1954 (cdr choices))
1955 isearch-string
1956 (if isearch-forward
1957 (if isearch-lazy-highlight-wrapped
1958 isearch-lazy-highlight-start
1959 (window-end))
1960 (if isearch-lazy-highlight-wrapped
1961 isearch-lazy-highlight-end
1962 (window-start)))
1963 t)))
44336afb
GM
1964
1965(defun isearch-lazy-highlight-update ()
c982ab21
GM
1966 "Update highlighting of other matches for current search."
1967 (let ((max isearch-lazy-highlight-max-at-a-time)
1968 (looping t)
1969 nomore)
7742ee19
RS
1970 (save-excursion
1971 (save-match-data
c982ab21
GM
1972 (goto-char (if isearch-forward
1973 isearch-lazy-highlight-end
1974 isearch-lazy-highlight-start))
1975 (while looping
1976 (let ((found (isearch-lazy-highlight-search)))
1977 (when max
1978 (setq max (1- max))
1979 (if (<= max 0)
1980 (setq looping nil)))
1981 (if found
1982 (let ((mb (match-beginning 0))
1983 (me (match-end 0)))
1984 (if (= mb me) ;zero-length match
1985 (forward-char 1)
1986
1987 ;; non-zero-length match
1988 (let ((ov (make-overlay mb me)))
1989 (overlay-put ov 'face isearch-lazy-highlight-face)
1990 (overlay-put ov 'priority 0) ;lower than main overlay
1991 (overlay-put ov 'window (selected-window))
1992 (push ov isearch-lazy-highlight-overlays)))
1993 (if isearch-forward
1994 (setq isearch-lazy-highlight-end (point))
1995 (setq isearch-lazy-highlight-start (point))))
1996
1997 ;; not found
1998 (if isearch-lazy-highlight-wrapped
1999 (setq looping nil
2000 nomore t)
2001 (setq isearch-lazy-highlight-wrapped t)
2002 (if isearch-forward
2003 (progn
2004 (setq isearch-lazy-highlight-end (window-start))
2005 (goto-char (window-start)))
2006 (setq isearch-lazy-highlight-start (window-end))
2007 (goto-char (window-end)))))))
2008 (unless nomore
2009 (setq isearch-lazy-highlight-timer
2010 (run-at-time isearch-lazy-highlight-interval nil
2011 'isearch-lazy-highlight-update)))))))
44336afb 2012
c40a4de1
GM
2013(defun isearch-resume (search regexp word forward message case-fold)
2014 "Resume an incremental search.
2015SEARCH is the string or regexp searched for.
2016REGEXP non-nil means the resumed search was a regexp search.
2017WORD non-nil means resume a word search.
2018FORWARD non-nil means resume a forward search.
2019MESSAGE is the echo-area message recorded for the search resumed.
2020CASE-FOLD non-nil means the search was case-insensitive."
2021 (isearch-mode forward regexp nil nil word)
2022 (setq isearch-string search
2023 isearch-message message
2024 isearch-case-fold-search case-fold)
2025 (isearch-search))
2026
3b1e4dd1 2027;;; isearch.el ends here