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