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