Follow coding conventions.
[emacs.git] / lisp / isearch.el
blobffb00d3af0eea0bca1c19ccf6c6fbd29ba19b221
1 ;;; isearch.el --- incremental search minor mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: matching
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
29 ;; Instructions
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.
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'.
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.
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.
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.
60 ;; TODO
61 ;; - Integrate the emacs 19 generalized command history.
62 ;; - Think about incorporating query-replace.
63 ;; - Hooks and options for failed search.
65 ;;; Change Log:
67 ;; Changes before those recorded in ChangeLog:
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.
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.
92 ;; Also see variables search-caps-disable-folding,
93 ;; search-nonincremental-instead, search-whitespace-regexp, and
94 ;; commands isearch-toggle-regexp, isearch-edit-string.
96 ;; semi-modal isearching is supported.
98 ;; Changes for 1.1
99 ;; 3/18/92 Fixed invalid-regexp.
100 ;; 3/18/92 Fixed yanking in regexps.
102 ;;; Code:
105 ;;; Some additional options and constants.
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)
116 (defcustom search-exit-option t
117 "*Non-nil means random control characters terminate incremental search."
118 :type 'boolean
119 :group 'isearch)
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)
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)
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)
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)
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)
163 (defcustom search-highlight t
164 "*Non-nil means incremental search highlights the current match."
165 :type 'boolean
166 :group 'isearch)
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)
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)
189 (defcustom isearch-resume-enabled t
190 "*If non-nil, `isearch-resume' commands are added to the command history."
191 :type 'boolean
192 :group 'isearch)
194 (defvar isearch-mode-hook nil
195 "Function(s) to call after starting up an incremental search.")
197 (defvar isearch-mode-end-hook nil
198 "Function(s) to call after terminating an incremental search.")
200 ;;; Search ring.
202 (defvar search-ring nil
203 "List of search string sequences.")
204 (defvar regexp-search-ring nil
205 "List of regular expression search string sequences.")
207 (defcustom search-ring-max 16
208 "*Maximum length of search ring before oldest elements are thrown away."
209 :type 'integer
210 :group 'isearch)
211 (defcustom regexp-search-ring-max 16
212 "*Maximum length of regexp search ring before oldest elements are thrown away."
213 :type 'integer
214 :group 'isearch)
216 (defvar search-ring-yank-pointer nil
217 "Index in `search-ring' of last string reused.
218 nil if none yet.")
219 (defvar regexp-search-ring-yank-pointer nil
220 "Index in `regexp-search-ring' of last string reused.
221 nil if none yet.")
223 (defcustom search-ring-update nil
224 "*Non-nil if advancing or retreating in the search ring should cause search.
225 Default value, nil, means edit the string instead."
226 :type 'boolean
227 :group 'isearch)
229 ;;; Define isearch-mode keymap.
231 (defvar isearch-mode-map
232 (let* ((i 0)
233 (map (make-keymap)))
234 (or (vectorp (nth 1 map))
235 (char-table-p (nth 1 map))
236 (error "The initialization of isearch-mode-map must be updated"))
237 ;; Make all multibyte characters search for themselves.
238 (let ((l (generic-character-list))
239 (table (nth 1 map)))
240 (while l
241 (set-char-table-default table (car l) 'isearch-printing-char)
242 (setq l (cdr l))))
243 ;; Make function keys, etc, exit the search.
244 (define-key map [t] 'isearch-other-control-char)
245 ;; Control chars, by default, end isearch mode transparently.
246 ;; We need these explicit definitions because, in a dense keymap,
247 ;; the binding for t does not affect characters.
248 ;; We use a dense keymap to save space.
249 (while (< i ?\ )
250 (define-key map (make-string 1 i) 'isearch-other-control-char)
251 (setq i (1+ i)))
253 ;; Single-byte printing chars extend the search string by default.
254 (setq i ?\ )
255 (while (< i 256)
256 (define-key map (vector i) 'isearch-printing-char)
257 (setq i (1+ i)))
259 ;; To handle local bindings with meta char prefix keys, define
260 ;; another full keymap. This must be done for any other prefix
261 ;; keys as well, one full keymap per char of the prefix key. It
262 ;; would be simpler to disable the global keymap, and/or have a
263 ;; default local key binding for any key not otherwise bound.
264 (let ((meta-map (make-sparse-keymap)))
265 (define-key map (char-to-string meta-prefix-char) meta-map)
266 (define-key map [escape] meta-map))
267 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
269 ;; Several non-printing chars change the searching behavior.
270 (define-key map "\C-s" 'isearch-repeat-forward)
271 (define-key map "\C-r" 'isearch-repeat-backward)
272 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
273 ;; combinations can be used to repeat regexp isearches that can
274 ;; be used to start these searches.
275 (define-key map "\M-\C-s" 'isearch-repeat-forward)
276 (define-key map "\M-\C-r" 'isearch-repeat-backward)
277 (define-key map "\177" 'isearch-delete-char)
278 (define-key map "\C-g" 'isearch-abort)
280 ;; This assumes \e is the meta-prefix-char.
281 (or (= ?\e meta-prefix-char)
282 (error "Inconsistency in isearch.el"))
283 (define-key map "\e\e\e" 'isearch-cancel)
284 (define-key map [escape escape escape] 'isearch-cancel)
286 (define-key map "\C-q" 'isearch-quote-char)
288 (define-key map "\r" 'isearch-exit)
289 (define-key map "\C-j" 'isearch-printing-char)
290 (define-key map "\t" 'isearch-printing-char)
291 (define-key map " " 'isearch-whitespace-chars)
292 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
294 (define-key map "\C-w" 'isearch-yank-word-or-char)
295 (define-key map "\C-y" 'isearch-yank-line)
297 ;; Define keys for regexp chars * ? |.
298 ;; Nothing special for + because it matches at least once.
299 (define-key map "*" 'isearch-*-char)
300 (define-key map "?" 'isearch-*-char)
301 (define-key map "|" 'isearch-|-char)
303 ;;; Turned off because I find I expect to get the global definition--rms.
304 ;;; ;; Instead bind C-h to special help command for isearch-mode.
305 ;;; (define-key map "\C-h" 'isearch-mode-help)
307 (define-key map "\M-n" 'isearch-ring-advance)
308 (define-key map "\M-p" 'isearch-ring-retreat)
309 (define-key map "\M-y" 'isearch-yank-kill)
311 (define-key map "\M-\t" 'isearch-complete)
313 ;; Pass frame events transparently so they won't exit the search.
314 ;; In particular, if we have more than one display open, then a
315 ;; switch-frame might be generated by someone typing at another keyboard.
316 (define-key map [switch-frame] nil)
317 (define-key map [delete-frame] nil)
318 (define-key map [iconify-frame] nil)
319 (define-key map [make-frame-visible] nil)
320 (define-key map [mouse-movement] nil)
321 ;; For searching multilingual text.
322 (define-key map "\C-\\" 'isearch-toggle-input-method)
323 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
325 ;; People expect to be able to paste with the mouse.
326 (define-key map [mouse-2] #'isearch-mouse-2)
327 (define-key map [down-mouse-2] nil)
329 ;; Some bindings you may want to put in your isearch-mode-hook.
330 ;; Suggest some alternates...
331 (define-key map "\M-c" 'isearch-toggle-case-fold)
332 (define-key map "\M-r" 'isearch-toggle-regexp)
333 (define-key map "\M-e" 'isearch-edit-string)
335 map)
336 "Keymap for `isearch-mode'.")
338 (defvar minibuffer-local-isearch-map
339 (let ((map (make-sparse-keymap)))
340 (set-keymap-parent map minibuffer-local-map)
341 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
342 (define-key map "\M-n" 'isearch-ring-advance-edit)
343 (define-key map "\M-p" 'isearch-ring-retreat-edit)
344 (define-key map "\M-\t" 'isearch-complete-edit)
345 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
346 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
347 map)
348 "Keymap for editing isearch strings in the minibuffer.")
350 ;; Internal variables declared globally for byte-compiler.
351 ;; These are all set with setq while isearching
352 ;; and bound locally while editing the search string.
354 (defvar isearch-forward nil) ; Searching in the forward direction.
355 (defvar isearch-regexp nil) ; Searching for a regexp.
356 (defvar isearch-word nil) ; Searching for words.
358 (defvar isearch-cmds nil) ; Stack of search status sets.
359 (defvar isearch-string "") ; The current search string.
360 (defvar isearch-message "") ; text-char-description version of isearch-string
362 (defvar isearch-success t) ; Searching is currently successful.
363 (defvar isearch-invalid-regexp nil) ; Regexp not well formed.
364 (defvar isearch-within-brackets nil) ; Regexp has unclosed [.
365 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
366 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
367 (defvar isearch-barrier 0)
368 (defvar isearch-just-started nil)
369 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
371 ; case-fold-search while searching.
372 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
373 ; case in the search string is ignored.
374 (defvar isearch-case-fold-search nil)
376 (defvar isearch-last-case-fold-search nil)
378 ;; Used to save default value while isearch is active
379 (defvar isearch-original-minibuffer-message-timeout nil)
381 (defvar isearch-adjusted nil)
382 (defvar isearch-slow-terminal-mode nil)
383 ;;; If t, using a small window.
384 (defvar isearch-small-window nil)
385 (defvar isearch-opoint 0)
386 ;;; The window configuration active at the beginning of the search.
387 (defvar isearch-window-configuration nil)
389 ;; Flag to indicate a yank occurred, so don't move the cursor.
390 (defvar isearch-yank-flag nil)
392 ;;; A function to be called after each input character is processed.
393 ;;; (It is not called after characters that exit the search.)
394 ;;; It is only set from an optional argument to `isearch-mode'.
395 (defvar isearch-op-fun nil)
397 ;;; Is isearch-mode in a recursive edit for modal searching.
398 (defvar isearch-recursive-edit nil)
400 ;;; Should isearch be terminated after doing one search?
401 (defvar isearch-nonincremental nil)
403 ;; New value of isearch-forward after isearch-edit-string.
404 (defvar isearch-new-forward nil)
406 ;; Accumulate here the overlays opened during searching.
407 (defvar isearch-opened-overlays nil)
409 ;; The value of input-method-function when isearch is invoked.
410 (defvar isearch-input-method-function nil)
412 ;; A flag to tell if input-method-function is locally bound when
413 ;; isearch is invoked.
414 (defvar isearch-input-method-local-p nil)
416 ;; Minor-mode-alist changes - kind of redundant with the
417 ;; echo area, but if isearching in multiple windows, it can be useful.
419 (or (assq 'isearch-mode minor-mode-alist)
420 (nconc minor-mode-alist
421 (list '(isearch-mode isearch-mode))))
423 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
424 (make-variable-buffer-local 'isearch-mode)
426 (define-key global-map "\C-s" 'isearch-forward)
427 (define-key esc-map "\C-s" 'isearch-forward-regexp)
428 (define-key global-map "\C-r" 'isearch-backward)
429 (define-key esc-map "\C-r" 'isearch-backward-regexp)
431 ;;; Entry points to isearch-mode.
433 (defun isearch-forward (&optional regexp-p no-recursive-edit)
435 Do incremental search forward.
436 With a prefix argument, do an incremental regular expression search instead.
437 \\<isearch-mode-map>
438 As you type characters, they add to the search string and are found.
439 The following non-printing keys are bound in `isearch-mode-map'.
441 Type \\[isearch-delete-char] to cancel characters from end of search string.
442 Type \\[isearch-exit] to exit, leaving point at location found.
443 Type LFD (C-j) to match end of line.
444 Type \\[isearch-repeat-forward] to search again forward,\
445 \\[isearch-repeat-backward] to search again backward.
446 Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
447 string and search for it.
448 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
449 and search for it.
450 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
451 and search for it.
452 Type \\[isearch-quote-char] to quote control character to search for it.
453 \\[isearch-abort] while searching or when search has failed cancels input\
454 back to what has
455 been found successfully.
456 \\[isearch-abort] when search is successful aborts and moves point to\
457 starting point.
459 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
460 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
461 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
463 Also supported is a search ring of the previous 16 search strings.
464 Type \\[isearch-ring-advance] to search for the next item in the search ring.
465 Type \\[isearch-ring-retreat] to search for the previous item in the search\
466 ring.
467 Type \\[isearch-complete] to complete the search string using the search ring.
469 If an input method is turned on in the current buffer, that input
470 method is also active while you are typing a characters to search. To
471 toggle the input method, type \\[isearch-toggle-input-method]. It
472 also toggles the input method in the current buffer.
474 To use a different input method for searching, type
475 \\[isearch-toggle-specified-input-method], and specify an input method
476 you want to use.
478 The above keys, bound in `isearch-mode-map', are often controlled by
479 options; do M-x apropos on search-.* to find them.
480 Other control and meta characters terminate the search
481 and are then executed normally (depending on `search-exit-option').
482 Likewise for function keys and mouse button events.
484 If this function is called non-interactively, it does not return to
485 the calling function until the search is done."
487 (interactive "P\np")
488 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
490 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
492 Do incremental search forward for regular expression.
493 With a prefix argument, do a regular string search instead.
494 Like ordinary incremental search except that your input
495 is treated as a regexp. See \\[isearch-forward] for more info."
496 (interactive "P\np")
497 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
499 (defun isearch-backward (&optional regexp-p no-recursive-edit)
501 Do incremental search backward.
502 With a prefix argument, do a regular expression search instead.
503 See \\[isearch-forward] for more information."
504 (interactive "P\np")
505 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
507 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
509 Do incremental search backward for regular expression.
510 With a prefix argument, do a regular string search instead.
511 Like ordinary incremental search except that your input
512 is treated as a regexp. See \\[isearch-forward] for more info."
513 (interactive "P\np")
514 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
517 (defun isearch-mode-help ()
518 (interactive)
519 (describe-function 'isearch-forward)
520 (isearch-update))
523 ;; isearch-mode only sets up incremental search for the minor mode.
524 ;; All the work is done by the isearch-mode commands.
526 ;; Not used yet:
527 ;;(defvar isearch-commands '(isearch-forward isearch-backward
528 ;; isearch-forward-regexp isearch-backward-regexp)
529 ;; "List of commands for which isearch-mode does not recursive-edit.")
532 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
533 "Start isearch minor mode. Called by `isearch-forward', etc.
535 \\{isearch-mode-map}"
537 ;; Initialize global vars.
538 (setq isearch-forward forward
539 isearch-regexp regexp
540 isearch-word word-p
541 isearch-op-fun op-fun
542 isearch-last-case-fold-search isearch-case-fold-search
543 isearch-case-fold-search case-fold-search
544 isearch-string ""
545 isearch-message ""
546 isearch-cmds nil
547 isearch-success t
548 isearch-wrapped nil
549 isearch-barrier (point)
550 isearch-adjusted nil
551 isearch-yank-flag nil
552 isearch-invalid-regexp nil
553 isearch-within-brackets nil
554 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
555 (> (window-height)
556 (* 4
557 (abs search-slow-window-lines))))
558 isearch-other-end nil
559 isearch-small-window nil
560 isearch-just-started t
561 isearch-start-hscroll (window-hscroll)
563 isearch-opoint (point)
564 search-ring-yank-pointer nil
565 isearch-opened-overlays nil
566 isearch-input-method-function input-method-function
567 isearch-input-method-local-p (local-variable-p 'input-method-function)
568 regexp-search-ring-yank-pointer nil
570 ;; Save the original value of `minibuffer-message-timeout', and
571 ;; set it to nil so that isearch's messages don't get timed out.
572 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
573 minibuffer-message-timeout nil)
575 ;; We must bypass input method while reading key. When a user type
576 ;; printable character, appropriate input method is turned on in
577 ;; minibuffer to read multibyte characters.
578 (or isearch-input-method-local-p
579 (make-local-variable 'input-method-function))
580 (setq input-method-function nil)
582 (looking-at "")
583 (setq isearch-window-configuration
584 (if isearch-slow-terminal-mode (current-window-configuration) nil))
586 ;; Maybe make minibuffer frame visible and/or raise it.
587 (let ((frame (window-frame (minibuffer-window))))
588 (unless (memq (frame-live-p frame) '(nil t))
589 (unless (frame-visible-p frame)
590 (make-frame-visible frame))
591 (if minibuffer-auto-raise
592 (raise-frame frame))))
594 (setq isearch-mode " Isearch") ;; forward? regexp?
595 (force-mode-line-update)
597 (isearch-push-state)
599 (setq overriding-terminal-local-map isearch-mode-map)
600 (isearch-update)
601 (run-hooks 'isearch-mode-hook)
603 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
604 (add-hook 'kbd-macro-termination-hook 'isearch-done)
606 ;; isearch-mode can be made modal (in the sense of not returning to
607 ;; the calling function until searching is completed) by entering
608 ;; a recursive-edit and exiting it when done isearching.
609 (if recursive-edit
610 (let ((isearch-recursive-edit t))
611 (recursive-edit)))
612 isearch-success)
615 ;; Some high level utilities. Others below.
617 (defun isearch-update ()
618 ;; Called after each command to update the display.
619 (if (and (null unread-command-events)
620 (null executing-kbd-macro))
621 (progn
622 (if (not (input-pending-p))
623 (isearch-message))
624 (if (and isearch-slow-terminal-mode
625 (not (or isearch-small-window
626 (pos-visible-in-window-p))))
627 (let ((found-point (point)))
628 (setq isearch-small-window t)
629 (move-to-window-line 0)
630 (let ((window-min-height 1))
631 (split-window nil (if (< search-slow-window-lines 0)
632 (1+ (- search-slow-window-lines))
633 (- (window-height)
634 (1+ search-slow-window-lines)))))
635 (if (< search-slow-window-lines 0)
636 (progn (vertical-motion (- 1 search-slow-window-lines))
637 (set-window-start (next-window) (point))
638 (set-window-hscroll (next-window)
639 (window-hscroll))
640 (set-window-hscroll (selected-window) 0))
641 (other-window 1))
642 (goto-char found-point))
643 ;; Keep same hscrolling as at the start of the search when possible
644 (let ((current-scroll (window-hscroll)))
645 (set-window-hscroll (selected-window) isearch-start-hscroll)
646 (unless (pos-visible-in-window-p)
647 (set-window-hscroll (selected-window) current-scroll))))
648 (if isearch-other-end
649 (if (< isearch-other-end (point)) ; isearch-forward?
650 (isearch-highlight isearch-other-end (point))
651 (isearch-highlight (point) isearch-other-end))
652 (isearch-dehighlight nil))
654 (setq ;; quit-flag nil not for isearch-mode
655 isearch-adjusted nil
656 isearch-yank-flag nil)
657 (isearch-lazy-highlight-new-loop)
658 ;; We must prevent the point moving to the end of composition when a
659 ;; part of the composition has just been searched.
660 (setq disable-point-adjustment t))
662 (defun isearch-done (&optional nopush edit)
663 (if isearch-resume-enabled
664 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
665 ,isearch-word ,isearch-forward
666 ,isearch-message
667 ',isearch-case-fold-search)))
668 (unless (equal (car command-history) command)
669 (setq command-history (cons command command-history)))))
671 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
672 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
673 (setq isearch-lazy-highlight-start nil)
675 ;; Called by all commands that terminate isearch-mode.
676 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
677 (setq overriding-terminal-local-map nil)
678 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
679 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
680 (isearch-dehighlight t)
681 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
682 (let ((found-start (window-start (selected-window)))
683 (found-point (point)))
684 (if isearch-window-configuration
685 (set-window-configuration isearch-window-configuration))
687 (if isearch-small-window
688 (goto-char found-point)
689 ;; Exiting the save-window-excursion clobbers window-start; restore it.
690 (set-window-start (selected-window) found-start t))
692 ;; If there was movement, mark the starting position.
693 ;; Maybe should test difference between and set mark iff > threshold.
694 (if (/= (point) isearch-opoint)
695 (or (and transient-mark-mode mark-active)
696 (progn
697 (push-mark isearch-opoint t)
698 (or executing-kbd-macro (> (minibuffer-depth) 0)
699 (message "Mark saved where search started"))))))
701 (setq isearch-mode nil)
702 (if isearch-input-method-local-p
703 (setq input-method-function isearch-input-method-function)
704 (kill-local-variable 'input-method-function))
706 (force-mode-line-update)
708 ;; If we ended in the middle of some intangible text,
709 ;; move to the further end of that intangible text.
710 (let ((after (if (eobp) nil
711 (get-text-property (point) 'intangible)))
712 (before (if (bobp) nil
713 (get-text-property (1- (point)) 'intangible))))
714 (when (and before after (eq before after))
715 (if isearch-forward
716 (goto-char (next-single-property-change (point) 'intangible))
717 (goto-char (previous-single-property-change (point) 'intangible)))))
719 (if (and (> (length isearch-string) 0) (not nopush))
720 ;; Update the ring data.
721 (isearch-update-ring isearch-string isearch-regexp))
723 (run-hooks 'isearch-mode-end-hook)
724 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
726 (defun isearch-update-ring (string &optional regexp)
727 "Add STRING to the beginning of the search ring.
728 REGEXP says which ring to use."
729 (if regexp
730 (if (or (null regexp-search-ring)
731 (not (string= string (car regexp-search-ring))))
732 (progn
733 (setq regexp-search-ring
734 (cons string regexp-search-ring))
735 (if (> (length regexp-search-ring) regexp-search-ring-max)
736 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
737 nil))))
738 (if (or (null search-ring)
739 (not (string= string (car search-ring))))
740 (progn
741 (setq search-ring (cons string search-ring))
742 (if (> (length search-ring) search-ring-max)
743 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
745 ;;; Switching buffers should first terminate isearch-mode.
746 ;;; ;; For Emacs 19, the frame switch event is handled.
747 ;;; (defun isearch-switch-frame-handler ()
748 ;;; (interactive) ;; Is this necessary?
749 ;;; ;; First terminate isearch-mode.
750 ;;; (isearch-done)
751 ;;; (isearch-clean-overlays)
752 ;;; (handle-switch-frame (car (cdr last-command-char))))
755 ;; Commands active while inside of the isearch minor mode.
757 (defun isearch-exit ()
758 "Exit search normally.
759 However, if this is the first command after starting incremental
760 search and `search-nonincremental-instead' is non-nil, do a
761 nonincremental search instead via `isearch-edit-string'."
762 (interactive)
763 (if (and search-nonincremental-instead
764 (= 0 (length isearch-string)))
765 (let ((isearch-nonincremental t))
766 (isearch-edit-string)))
767 (isearch-done)
768 (isearch-clean-overlays))
771 (defun isearch-edit-string ()
772 "Edit the search string in the minibuffer.
773 The following additional command keys are active while editing.
774 \\<minibuffer-local-isearch-map>
775 \\[exit-minibuffer] to resume incremental searching with the edited string.
776 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
777 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
778 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
779 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
780 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
781 \\[isearch-complete-edit] to complete the search string using the search ring.
782 \\<isearch-mode-map>
783 If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
785 ;; This code is very hairy for several reasons, explained in the code.
786 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
787 ;; If there were a way to catch any change of buffer from the minibuffer,
788 ;; this could be simplified greatly.
789 ;; Editing doesn't back up the search point. Should it?
790 (interactive)
791 (condition-case err
792 (progn
793 (let ((isearch-nonincremental isearch-nonincremental)
795 ;; Locally bind all isearch global variables to protect them
796 ;; from recursive isearching.
797 ;; isearch-string -message and -forward are not bound
798 ;; so they may be changed. Instead, save the values.
799 (isearch-new-string isearch-string)
800 (isearch-new-message isearch-message)
801 (isearch-new-forward isearch-forward)
802 (isearch-new-word isearch-word)
804 (isearch-regexp isearch-regexp)
805 (isearch-op-fun isearch-op-fun)
806 (isearch-cmds isearch-cmds)
807 (isearch-success isearch-success)
808 (isearch-wrapped isearch-wrapped)
809 (isearch-barrier isearch-barrier)
810 (isearch-adjusted isearch-adjusted)
811 (isearch-yank-flag isearch-yank-flag)
812 (isearch-invalid-regexp isearch-invalid-regexp)
813 (isearch-within-brackets isearch-within-brackets)
814 ;;; Don't bind this. We want isearch-search, below, to set it.
815 ;;; And the old value won't matter after that.
816 ;;; (isearch-other-end isearch-other-end)
817 ;;; Perhaps some of these other variables should be bound for a
818 ;;; shorter period, ending before the next isearch-search.
819 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
820 (isearch-opoint isearch-opoint)
821 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
822 (isearch-small-window isearch-small-window)
823 (isearch-recursive-edit isearch-recursive-edit)
824 ;; Save current configuration so we can restore it here.
825 (isearch-window-configuration (current-window-configuration))
827 ;; Temporarily restore `minibuffer-message-timeout'.
828 (minibuffer-message-timeout
829 isearch-original-minibuffer-message-timeout)
830 (isearch-original-minibuffer-message-timeout
831 isearch-original-minibuffer-message-timeout)
834 ;; Actually terminate isearching until editing is done.
835 ;; This is so that the user can do anything without failure,
836 ;; like switch buffers and start another isearch, and return.
837 (condition-case err
838 (isearch-done t t)
839 (exit nil)) ; was recursive editing
841 (isearch-message) ;; for read-char
842 (unwind-protect
843 (let* (;; Why does following read-char echo?
844 ;;(echo-keystrokes 0) ;; not needed with above message
845 (e (let ((cursor-in-echo-area t))
846 (read-event)))
847 ;; Binding minibuffer-history-symbol to nil is a work-around
848 ;; for some incompatibility with gmhist.
849 (minibuffer-history-symbol)
850 (message-log-max nil))
851 ;; If the first character the user types when we prompt them
852 ;; for a string is the yank-word character, then go into
853 ;; word-search mode. Otherwise unread that character and
854 ;; read a key the normal way.
855 ;; Word search does not apply (yet) to regexp searches,
856 ;; no check is made here.
857 (message (isearch-message-prefix nil nil t))
858 (if (memq (lookup-key isearch-mode-map (vector e))
859 '(isearch-yank-word
860 isearch-yank-word-or-char))
861 (setq isearch-word t;; so message-prefix is right
862 isearch-new-word t)
863 (cancel-kbd-macro-events)
864 (isearch-unread e))
865 (setq cursor-in-echo-area nil)
866 (setq isearch-new-string
867 (let (junk-ring)
868 (read-from-minibuffer
869 (isearch-message-prefix nil nil isearch-nonincremental)
870 isearch-string
871 minibuffer-local-isearch-map nil
872 'junk-ring))
873 isearch-new-message
874 (mapconcat 'isearch-text-char-description
875 isearch-new-string "")))
876 ;; Always resume isearching by restarting it.
877 (isearch-mode isearch-forward
878 isearch-regexp
879 isearch-op-fun
881 isearch-word)
883 ;; Copy new local values to isearch globals
884 (setq isearch-string isearch-new-string
885 isearch-message isearch-new-message
886 isearch-forward isearch-new-forward
887 isearch-word isearch-new-word))
889 ;; Empty isearch-string means use default.
890 (if (= 0 (length isearch-string))
891 (setq isearch-string (or (car (if isearch-regexp
892 regexp-search-ring
893 search-ring))
896 isearch-message
897 (mapconcat 'isearch-text-char-description
898 isearch-string ""))
899 ;; This used to set the last search string,
900 ;; but I think it is not right to do that here.
901 ;; Only the string actually used should be saved.
904 ;; Push the state as of before this C-s.
905 (isearch-push-state)
907 ;; Reinvoke the pending search.
908 (isearch-search)
909 (isearch-update)
910 (if isearch-nonincremental
911 (progn
912 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
913 (isearch-done))))
915 (quit ; handle abort-recursive-edit
916 (isearch-abort) ;; outside of let to restore outside global values
919 (defun isearch-nonincremental-exit-minibuffer ()
920 (interactive)
921 (setq isearch-nonincremental t)
922 (exit-minibuffer))
924 (defun isearch-forward-exit-minibuffer ()
925 (interactive)
926 (setq isearch-new-forward t)
927 (exit-minibuffer))
929 (defun isearch-reverse-exit-minibuffer ()
930 (interactive)
931 (setq isearch-new-forward nil)
932 (exit-minibuffer))
934 (defun isearch-cancel ()
935 "Terminate the search and go back to the starting point."
936 (interactive)
937 (goto-char isearch-opoint)
938 (isearch-done t)
939 (isearch-clean-overlays)
940 (signal 'quit nil)) ; and pass on quit signal
942 (defun isearch-abort ()
943 "Abort incremental search mode if searching is successful, signaling quit.
944 Otherwise, revert to previous successful search and continue searching.
945 Use `isearch-exit' to quit without signaling."
946 (interactive)
947 ;; (ding) signal instead below, if quitting
948 (discard-input)
949 (if isearch-success
950 ;; If search is successful, move back to starting point
951 ;; and really do quit.
952 (progn (goto-char isearch-opoint)
953 (setq isearch-success nil)
954 (isearch-done t) ; exit isearch
955 (isearch-clean-overlays)
956 (signal 'quit nil)) ; and pass on quit signal
957 ;; If search is failing, or has an incomplete regexp,
958 ;; rub out until it is once more successful.
959 (while (or (not isearch-success) isearch-invalid-regexp)
960 (isearch-pop-state))
961 (isearch-update)))
963 (defun isearch-repeat (direction)
964 ;; Utility for isearch-repeat-forward and -backward.
965 (if (eq isearch-forward (eq direction 'forward))
966 ;; C-s in forward or C-r in reverse.
967 (if (equal isearch-string "")
968 ;; If search string is empty, use last one.
969 (setq isearch-string
970 (or (if isearch-regexp
971 (car regexp-search-ring)
972 (car search-ring))
974 isearch-message
975 (mapconcat 'isearch-text-char-description
976 isearch-string "")
977 isearch-case-fold-search isearch-last-case-fold-search)
978 ;; If already have what to search for, repeat it.
979 (or isearch-success
980 (progn
981 (goto-char (if isearch-forward (point-min) (point-max)))
982 (setq isearch-wrapped t))))
983 ;; C-s in reverse or C-r in forward, change direction.
984 (setq isearch-forward (not isearch-forward)))
986 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
988 (if (equal isearch-string "")
989 (setq isearch-success t)
990 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
991 (not isearch-just-started))
992 ;; If repeating a search that found
993 ;; an empty string, ensure we advance.
994 (if (if isearch-forward (eobp) (bobp))
995 ;; If there's nowhere to advance to, fail (and wrap next time).
996 (progn
997 (setq isearch-success nil)
998 (ding))
999 (forward-char (if isearch-forward 1 -1))
1000 (isearch-search))
1001 (isearch-search)))
1003 (isearch-push-state)
1004 (isearch-update))
1006 (defun isearch-repeat-forward ()
1007 "Repeat incremental search forwards."
1008 (interactive)
1009 (isearch-repeat 'forward))
1011 (defun isearch-repeat-backward ()
1012 "Repeat incremental search backwards."
1013 (interactive)
1014 (isearch-repeat 'backward))
1016 (defun isearch-toggle-regexp ()
1017 "Toggle regexp searching on or off."
1018 ;; The status stack is left unchanged.
1019 (interactive)
1020 (setq isearch-regexp (not isearch-regexp))
1021 (if isearch-regexp (setq isearch-word nil))
1022 (isearch-update))
1024 (defun isearch-toggle-case-fold ()
1025 "Toggle case folding in searching on or off."
1026 (interactive)
1027 (setq isearch-case-fold-search
1028 (if isearch-case-fold-search nil 'yes))
1029 (let ((message-log-max nil))
1030 (message "%s%s [case %ssensitive]"
1031 (isearch-message-prefix nil nil isearch-nonincremental)
1032 isearch-message
1033 (if isearch-case-fold-search "in" "")))
1034 (setq isearch-adjusted t)
1035 (sit-for 1)
1036 (isearch-update))
1038 (defun isearch-delete-char ()
1039 "Discard last input item and move point back.
1040 If no previous match was done, just beep."
1041 (interactive)
1042 (if (null (cdr isearch-cmds))
1043 (ding)
1044 (isearch-pop-state))
1045 (isearch-update))
1048 (defun isearch-yank-string (string)
1049 "Pull STRING into search string."
1050 ;; Downcase the string if not supposed to case-fold yanked strings.
1051 (if (and isearch-case-fold-search
1052 (eq 'not-yanks search-upper-case))
1053 (setq string (downcase string)))
1054 (if isearch-regexp (setq string (regexp-quote string)))
1055 (setq isearch-string (concat isearch-string string)
1056 isearch-message
1057 (concat isearch-message
1058 (mapconcat 'isearch-text-char-description
1059 string ""))
1060 ;; Don't move cursor in reverse search.
1061 isearch-yank-flag t)
1062 (isearch-search-and-update))
1064 (defun isearch-yank-kill ()
1065 "Pull string from kill ring into search string."
1066 (interactive)
1067 (isearch-yank-string (current-kill 0)))
1069 (defun isearch-yank-x-selection ()
1070 "Pull current X selection into search string."
1071 (interactive)
1072 (isearch-yank-string (x-get-selection)))
1075 (defun isearch-mouse-2 (click arg)
1076 "Handle mouse-2 in Isearch mode.
1077 For a click in the echo area, invoke `isearch-yank-x-selection'.
1078 Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
1079 (interactive "e\nP")
1080 (let* ((w (posn-window (event-start click)))
1081 (overriding-terminal-local-map nil)
1082 (key (vector (event-basic-type click)))
1083 (binding (key-binding key)))
1084 (if (and (window-minibuffer-p w)
1085 (not (minibuffer-window-active-p w))) ; in echo area
1086 (isearch-yank-x-selection)
1087 (when binding
1088 ;; Kluge to allow passing ARG to functions that support it,
1089 ;; like mouse-yank-at-click.
1090 (if (equal (cadr (interactive-form binding)) "e\nP")
1091 (funcall binding click arg)
1092 (funcall binding click))))))
1095 (defun isearch-yank-internal (jumpform)
1096 "Pull the text from point to the point reached by JUMPFORM.
1097 JUMPFORM is a lambda expression that takes no arguments and returns a
1098 buffer position, possibly having moved point to that position. For
1099 example, it might move point forward by a word and return point, or it
1100 might return the position of the end of the line."
1101 (isearch-yank-string
1102 (save-excursion
1103 (and (not isearch-forward) isearch-other-end
1104 (goto-char isearch-other-end))
1105 (buffer-substring-no-properties (point) (funcall jumpform)))))
1107 (defun isearch-yank-char ()
1108 "Pull next letter from buffer into search string."
1109 (interactive)
1110 (isearch-yank-internal (lambda () (forward-char 1) (point))))
1112 (defun isearch-yank-word-or-char ()
1113 "Pull next character or word from buffer into search string."
1114 (interactive)
1115 (isearch-yank-internal
1116 (lambda ()
1117 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1118 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1119 (forward-word 1)
1120 (forward-char 1)) (point))))
1122 (defun isearch-yank-word ()
1123 "Pull next word from buffer into search string."
1124 (interactive)
1125 (isearch-yank-internal (lambda () (forward-word 1) (point))))
1127 (defun isearch-yank-line ()
1128 "Pull rest of line from buffer into search string."
1129 (interactive)
1130 (isearch-yank-internal (lambda () (line-end-position))))
1133 (defun isearch-search-and-update ()
1134 ;; Do the search and update the display.
1135 (if (and (not isearch-success)
1136 ;; unsuccessful regexp search may become
1137 ;; successful by addition of characters which
1138 ;; make isearch-string valid
1139 (not isearch-regexp))
1141 ;; In reverse search, adding stuff at
1142 ;; the end may cause zero or many more chars to be
1143 ;; matched, in the string following point.
1144 ;; Allow all those possibilities without moving point as
1145 ;; long as the match does not extend past search origin.
1146 (if (and (not isearch-forward) (not isearch-adjusted)
1147 (condition-case ()
1148 (let ((case-fold-search isearch-case-fold-search))
1149 (if (and (eq case-fold-search t) search-upper-case)
1150 (setq case-fold-search
1151 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1152 (looking-at (if isearch-regexp isearch-string
1153 (regexp-quote isearch-string))))
1154 (error nil))
1155 (or isearch-yank-flag
1156 (<= (match-end 0)
1157 (min isearch-opoint isearch-barrier))))
1158 (progn
1159 (setq isearch-success t
1160 isearch-invalid-regexp nil
1161 isearch-within-brackets nil
1162 isearch-other-end (match-end 0))
1163 (if (and (eq isearch-case-fold-search t) search-upper-case)
1164 (setq isearch-case-fold-search
1165 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1166 ;; Not regexp, not reverse, or no match at point.
1167 (if (and isearch-other-end (not isearch-adjusted))
1168 (goto-char (if isearch-forward isearch-other-end
1169 (min isearch-opoint
1170 isearch-barrier
1171 (1+ isearch-other-end)))))
1172 (isearch-search)
1174 (isearch-push-state)
1175 (if isearch-op-fun (funcall isearch-op-fun))
1176 (isearch-update))
1179 ;; *, ?, and | chars can make a regexp more liberal.
1180 ;; They can make a regexp match sooner or make it succeed instead of failing.
1181 ;; So go back to place last successful search started
1182 ;; or to the last ^S/^R (barrier), whichever is nearer.
1183 ;; + needs no special handling because the string must match at least once.
1185 (defun isearch-*-char ()
1186 "Handle * and ? specially in regexps."
1187 (interactive)
1188 (if isearch-regexp
1189 (let ((idx (length isearch-string)))
1190 (while (and (> idx 0)
1191 (eq (aref isearch-string (1- idx)) ?\\))
1192 (setq idx (1- idx)))
1193 (when (= (mod (- (length isearch-string) idx) 2) 0)
1194 (setq isearch-adjusted t)
1195 ;; Get the isearch-other-end from before the last search.
1196 ;; We want to start from there,
1197 ;; so that we don't retreat farther than that.
1198 ;; (car isearch-cmds) is after last search;
1199 ;; (car (cdr isearch-cmds)) is from before it.
1200 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1201 (setq cs (or cs isearch-barrier))
1202 (goto-char
1203 (if isearch-forward
1204 (max cs isearch-barrier)
1205 (min cs isearch-barrier)))))))
1206 (isearch-process-search-char last-command-char))
1209 (defun isearch-|-char ()
1210 "If in regexp search, jump to the barrier."
1211 (interactive)
1212 (if isearch-regexp
1213 (progn
1214 (setq isearch-adjusted t)
1215 (goto-char isearch-barrier)))
1216 (isearch-process-search-char last-command-char))
1219 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1221 (defun isearch-other-meta-char ()
1222 "Exit the search normally and reread this key sequence.
1223 But only if `search-exit-option' is non-nil, the default.
1224 If it is the symbol `edit', the search string is edited in the minibuffer
1225 and the meta character is unread so that it applies to editing the string."
1226 (interactive)
1227 (let* ((key (this-command-keys))
1228 (main-event (aref key 0))
1229 (keylist (listify-key-sequence key)))
1230 (cond ((and (= (length key) 1)
1231 (let ((lookup (lookup-key function-key-map key)))
1232 (not (or (null lookup) (integerp lookup)
1233 (keymapp lookup)))))
1234 ;; Handle a function key that translates into something else.
1235 ;; If the key has a global definition too,
1236 ;; exit and unread the key itself, so its global definition runs.
1237 ;; Otherwise, unread the translation,
1238 ;; so that the translated key takes effect within isearch.
1239 (cancel-kbd-macro-events)
1240 (if (lookup-key global-map key)
1241 (progn
1242 (isearch-done)
1243 (apply 'isearch-unread keylist))
1244 (setq keylist
1245 (listify-key-sequence (lookup-key function-key-map key)))
1246 (while keylist
1247 (setq key (car keylist))
1248 ;; If KEY is a printing char, we handle it here
1249 ;; directly to avoid the input method and keyboard
1250 ;; coding system translating it.
1251 (if (and (integerp key)
1252 (>= key ?\ ) (/= key 127) (< key 256))
1253 (progn
1254 (isearch-process-search-char key)
1255 (setq keylist (cdr keylist)))
1256 ;; As the remaining keys in KEYLIST can't be handled
1257 ;; here, we must reread them.
1258 (apply 'isearch-unread keylist)
1259 (setq keylist nil)))))
1261 ;; Handle an undefined shifted control character
1262 ;; by downshifting it if that makes it defined.
1263 ;; (As read-key-sequence would normally do,
1264 ;; if we didn't have a default definition.)
1265 (let ((mods (event-modifiers main-event)))
1266 (and (integerp main-event)
1267 (memq 'shift mods)
1268 (memq 'control mods)
1269 (lookup-key isearch-mode-map
1270 (let ((copy (copy-sequence key)))
1271 (aset copy 0
1272 (- main-event (- ?\C-\S-a ?\C-a)))
1273 copy)
1274 nil)))
1275 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1276 (cancel-kbd-macro-events)
1277 (apply 'isearch-unread keylist))
1278 ((eq search-exit-option 'edit)
1279 (apply 'isearch-unread keylist)
1280 (isearch-edit-string))
1281 (search-exit-option
1282 (let (window)
1283 (cancel-kbd-macro-events)
1284 (apply 'isearch-unread keylist)
1286 ;; Properly handle scroll-bar and mode-line clicks for
1287 ;; which a dummy prefix event was generated as (aref key
1288 ;; 0). Note that we don't have to modify the event
1289 ;; anymore in 21 because read_key_sequence no longer modifies
1290 ;; events to produce fake prefix keys.
1291 (when (and (> (length key) 1)
1292 (symbolp (aref key 0))
1293 (listp (aref key 1))
1294 (not (numberp (posn-point
1295 (event-start (aref key 1))))))
1296 (pop unread-command-events)
1297 (setq main-event (car unread-command-events)))
1299 ;; If we got a mouse click event, that event contains the
1300 ;; window clicked on. maybe it was read with the buffer
1301 ;; it was clicked on. If so, that buffer, not the current one,
1302 ;; is in isearch mode. So end the search in that buffer.
1304 ;; ??? I have no idea what this if checks for, but it's
1305 ;; obviously wrong for the case that a down-mouse event
1306 ;; on another window invokes this function. The event
1307 ;; will contain the window clicked on and that window's
1308 ;; buffer is certainaly not always in Isearch mode.
1310 ;; Leave the code in, but check for current buffer not
1311 ;; being in Isearch mode for now, until someone tells
1312 ;; what it's really supposed to do.
1314 ;; --gerd 2001-08-10.
1316 (if (and (not isearch-mode)
1317 (listp main-event)
1318 (setq window (posn-window (event-start main-event)))
1319 (windowp window)
1320 (or (> (minibuffer-depth) 0)
1321 (not (window-minibuffer-p window))))
1322 (save-excursion
1323 (set-buffer (window-buffer window))
1324 (isearch-done)
1325 (isearch-clean-overlays))
1326 (isearch-done)
1327 (isearch-clean-overlays))))
1328 (t;; otherwise nil
1329 (isearch-process-search-string key key)))))
1331 (defun isearch-quote-char ()
1332 "Quote special characters for incremental search."
1333 (interactive)
1334 (let ((char (read-quoted-char (isearch-message t))))
1335 ;; Assume character codes 0200 - 0377 stand for characters in some
1336 ;; single-byte character set, and convert them to Emacs
1337 ;; characters.
1338 (and enable-multibyte-characters
1339 (>= char ?\200)
1340 (<= char ?\377)
1341 (setq char (unibyte-char-to-multibyte char)))
1342 (isearch-process-search-char char)))
1344 (defun isearch-return-char ()
1345 "Convert return into newline for incremental search.
1346 Obsolete."
1347 (interactive)
1348 (isearch-process-search-char ?\n))
1350 (defun isearch-printing-char ()
1351 "Add this ordinary printing character to the search string and search."
1352 (interactive)
1353 (let ((char last-command-char))
1354 (if (= char ?\S-\ )
1355 (setq char ?\ ))
1356 (if (and enable-multibyte-characters
1357 (>= char ?\200)
1358 (<= char ?\377))
1359 (if (keyboard-coding-system)
1360 (isearch-process-search-multibyte-characters char)
1361 (isearch-process-search-char (unibyte-char-to-multibyte char)))
1362 (if current-input-method
1363 (isearch-process-search-multibyte-characters char)
1364 (isearch-process-search-char char)))))
1366 (defun isearch-whitespace-chars ()
1367 "Match all whitespace chars, if in regexp mode.
1368 If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
1369 (interactive)
1370 (if isearch-regexp
1371 (if (and search-whitespace-regexp (not isearch-within-brackets)
1372 (not isearch-invalid-regexp))
1373 (isearch-process-search-string search-whitespace-regexp " ")
1374 (isearch-printing-char))
1375 (progn
1376 ;; This way of doing word search doesn't correctly extend current search.
1377 ;; (setq isearch-word t)
1378 ;; (setq isearch-adjusted t)
1379 ;; (goto-char isearch-barrier)
1380 (isearch-printing-char))))
1382 (defun isearch-process-search-char (char)
1383 ;; Append the char to the search string, update the message and re-search.
1384 (isearch-process-search-string
1385 (char-to-string char)
1386 (if (>= char ?\200)
1387 (char-to-string char)
1388 (isearch-text-char-description char))))
1390 (defun isearch-process-search-string (string message)
1391 (setq isearch-string (concat isearch-string string)
1392 isearch-message (concat isearch-message message))
1393 (isearch-search-and-update))
1396 ;; Search Ring
1398 (defun isearch-ring-adjust1 (advance)
1399 ;; Helper for isearch-ring-adjust
1400 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1401 (length (length ring))
1402 (yank-pointer-name (if isearch-regexp
1403 'regexp-search-ring-yank-pointer
1404 'search-ring-yank-pointer))
1405 (yank-pointer (eval yank-pointer-name)))
1406 (if (zerop length)
1408 (set yank-pointer-name
1409 (setq yank-pointer
1410 (mod (+ (or yank-pointer 0)
1411 (if advance -1 1))
1412 length)))
1413 (setq isearch-string (nth yank-pointer ring)
1414 isearch-message (mapconcat 'isearch-text-char-description
1415 isearch-string "")))))
1417 (defun isearch-ring-adjust (advance)
1418 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1419 (isearch-ring-adjust1 advance)
1420 (if search-ring-update
1421 (progn
1422 (isearch-search)
1423 (isearch-update))
1424 (isearch-edit-string)
1426 (isearch-push-state))
1428 (defun isearch-ring-advance ()
1429 "Advance to the next search string in the ring."
1430 ;; This could be more general to handle a prefix arg, but who would use it.
1431 (interactive)
1432 (isearch-ring-adjust 'advance))
1434 (defun isearch-ring-retreat ()
1435 "Retreat to the previous search string in the ring."
1436 (interactive)
1437 (isearch-ring-adjust nil))
1439 (defun isearch-ring-advance-edit (n)
1440 "Insert the next element of the search history into the minibuffer.
1441 With prefix arg N, insert the Nth element."
1442 (interactive "p")
1443 (let* ((yank-pointer-name (if isearch-regexp
1444 'regexp-search-ring-yank-pointer
1445 'search-ring-yank-pointer))
1446 (yank-pointer (eval yank-pointer-name))
1447 (ring (if isearch-regexp regexp-search-ring search-ring))
1448 (length (length ring)))
1449 (if (zerop length)
1451 (set yank-pointer-name
1452 (setq yank-pointer
1453 (mod (- (or yank-pointer 0) n)
1454 length)))
1456 (delete-field)
1457 (insert (nth yank-pointer ring))
1458 (goto-char (point-max)))))
1460 (defun isearch-ring-retreat-edit (n)
1461 "Insert the previous element of the search history into the minibuffer.
1462 With prefix arg N, insert the Nth element."
1463 (interactive "p")
1464 (isearch-ring-advance-edit (- n)))
1466 ;;(defun isearch-ring-adjust-edit (advance)
1467 ;; "Use the next or previous search string in the ring while in minibuffer."
1468 ;; (isearch-ring-adjust1 advance)
1469 ;; (erase-buffer)
1470 ;; (insert isearch-string))
1472 ;;(defun isearch-ring-advance-edit ()
1473 ;; (interactive)
1474 ;; (isearch-ring-adjust-edit 'advance))
1476 ;;(defun isearch-ring-retreat-edit ()
1477 ;; "Retreat to the previous search string in the ring while in the minibuffer."
1478 ;; (interactive)
1479 ;; (isearch-ring-adjust-edit nil))
1482 (defun isearch-complete1 ()
1483 ;; Helper for isearch-complete and isearch-complete-edit
1484 ;; Return t if completion OK, nil if no completion exists.
1485 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1486 (alist (mapcar (function (lambda (string) (list string))) ring))
1487 (completion-ignore-case case-fold-search)
1488 (completion (try-completion isearch-string alist)))
1489 (cond
1490 ((eq completion t)
1491 ;; isearch-string stays the same
1493 ((or completion ; not nil, must be a string
1494 (= 0 (length isearch-string))) ; shouldn't have to say this
1495 (if (equal completion isearch-string) ;; no extension?
1496 (progn
1497 (if completion-auto-help
1498 (with-output-to-temp-buffer "*Isearch completions*"
1499 (display-completion-list
1500 (all-completions isearch-string alist))))
1502 (and completion
1503 (setq isearch-string completion))))
1505 (message "No completion") ; waits a second if in minibuffer
1506 nil))))
1508 (defun isearch-complete ()
1509 "Complete the search string from the strings on the search ring.
1510 The completed string is then editable in the minibuffer.
1511 If there is no completion possible, say so and continue searching."
1512 (interactive)
1513 (if (isearch-complete1)
1514 (isearch-edit-string)
1515 ;; else
1516 (sit-for 1)
1517 (isearch-update)))
1519 (defun isearch-complete-edit ()
1520 "Same as `isearch-complete' except in the minibuffer."
1521 (interactive)
1522 (setq isearch-string (buffer-string))
1523 (if (isearch-complete1)
1524 (progn
1525 (delete-field)
1526 (insert isearch-string))))
1529 ;; The search status stack (and isearch window-local variables, not used).
1530 ;; Need a structure for this.
1532 (defun isearch-top-state ()
1533 (let ((cmd (car isearch-cmds)))
1534 (setq isearch-string (car cmd)
1535 isearch-message (car (cdr cmd))
1536 isearch-success (nth 3 cmd)
1537 isearch-forward (nth 4 cmd)
1538 isearch-other-end (nth 5 cmd)
1539 isearch-word (nth 6 cmd)
1540 isearch-invalid-regexp (nth 7 cmd)
1541 isearch-wrapped (nth 8 cmd)
1542 isearch-barrier (nth 9 cmd)
1543 isearch-within-brackets (nth 10 cmd)
1544 isearch-case-fold-search (nth 11 cmd))
1545 (goto-char (car (cdr (cdr cmd))))))
1547 (defun isearch-pop-state ()
1548 (setq isearch-cmds (cdr isearch-cmds))
1549 (isearch-top-state))
1551 (defun isearch-push-state ()
1552 (setq isearch-cmds
1553 (cons (list isearch-string isearch-message (point)
1554 isearch-success isearch-forward isearch-other-end
1555 isearch-word
1556 isearch-invalid-regexp isearch-wrapped isearch-barrier
1557 isearch-within-brackets isearch-case-fold-search)
1558 isearch-cmds)))
1561 ;; Message string
1563 (defun isearch-message (&optional c-q-hack ellipsis)
1564 ;; Generate and print the message string.
1565 (let ((cursor-in-echo-area ellipsis)
1566 (m (concat
1567 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
1568 isearch-message
1569 (isearch-message-suffix c-q-hack ellipsis)
1571 (if c-q-hack
1573 (let ((message-log-max nil))
1574 (message "%s" m)))))
1576 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
1577 ;; If about to search, and previous search regexp was invalid,
1578 ;; check that it still is. If it is valid now,
1579 ;; let the message we display while searching say that it is valid.
1580 (and isearch-invalid-regexp ellipsis
1581 (condition-case ()
1582 (progn (re-search-forward isearch-string (point) t)
1583 (setq isearch-invalid-regexp nil
1584 isearch-within-brackets nil))
1585 (error nil)))
1586 ;; If currently failing, display no ellipsis.
1587 (or isearch-success (setq ellipsis nil))
1588 (let ((m (concat (if isearch-success "" "failing ")
1589 (if (and isearch-wrapped
1590 (if isearch-forward
1591 (> (point) isearch-opoint)
1592 (< (point) isearch-opoint)))
1593 "over")
1594 (if isearch-wrapped "wrapped ")
1595 (if isearch-word "word " "")
1596 (if isearch-regexp "regexp " "")
1597 (if nonincremental "search" "I-search")
1598 (if isearch-forward "" " backward")
1599 (if current-input-method
1600 (concat " [" current-input-method-title "]: ")
1601 ": ")
1603 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
1604 'face 'minibuffer-prompt)))
1606 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
1607 (concat (if c-q-hack "^Q" "")
1608 (if isearch-invalid-regexp
1609 (concat " [" isearch-invalid-regexp "]")
1610 "")))
1613 ;;; Searching
1615 (defun isearch-search ()
1616 ;; Do the search with the current search string.
1617 (isearch-message nil t)
1618 (if (and (eq isearch-case-fold-search t) search-upper-case)
1619 (setq isearch-case-fold-search
1620 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1621 (condition-case lossage
1622 (let ((inhibit-point-motion-hooks search-invisible)
1623 (inhibit-quit nil)
1624 (case-fold-search isearch-case-fold-search)
1625 (retry t))
1626 (if isearch-regexp (setq isearch-invalid-regexp nil))
1627 (setq isearch-within-brackets nil)
1628 (while retry
1629 (setq isearch-success
1630 (funcall
1631 (cond (isearch-word
1632 (if isearch-forward
1633 'word-search-forward 'word-search-backward))
1634 (isearch-regexp
1635 (if isearch-forward
1636 're-search-forward 're-search-backward))
1638 (if isearch-forward 'search-forward 'search-backward)))
1639 isearch-string nil t))
1640 ;; Clear RETRY unless we matched some invisible text
1641 ;; and we aren't supposed to do that.
1642 (if (or (eq search-invisible t)
1643 (not isearch-success)
1644 (bobp) (eobp)
1645 (= (match-beginning 0) (match-end 0))
1646 (not (isearch-range-invisible
1647 (match-beginning 0) (match-end 0))))
1648 (setq retry nil)))
1649 (setq isearch-just-started nil)
1650 (if isearch-success
1651 (setq isearch-other-end
1652 (if isearch-forward (match-beginning 0) (match-end 0)))))
1654 (quit (isearch-unread ?\C-g)
1655 (setq isearch-success nil))
1657 (invalid-regexp
1658 (setq isearch-invalid-regexp (car (cdr lossage)))
1659 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1660 isearch-invalid-regexp))
1661 (if (string-match
1662 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1663 isearch-invalid-regexp)
1664 (setq isearch-invalid-regexp "incomplete input")))
1665 (error
1666 ;; stack overflow in regexp search.
1667 (setq isearch-invalid-regexp (format "%s" lossage))))
1669 (if isearch-success
1671 ;; Ding if failed this time after succeeding last time.
1672 (and (nth 3 (car isearch-cmds))
1673 (ding))
1674 (goto-char (nth 2 (car isearch-cmds)))))
1677 ;;; Called when opening an overlay, and we are still in isearch.
1678 (defun isearch-open-overlay-temporary (ov)
1679 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1680 ;; Some modes would want to open the overlays temporary during
1681 ;; isearch in their own way, they should set the
1682 ;; `isearch-open-invisible-temporary' to a function doing this.
1683 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1684 ;; Store the values for the `invisible' and `intangible'
1685 ;; properties, and then set them to nil. This way the text hidden
1686 ;; by this overlay becomes visible.
1688 ;; Do we really need to set the `intangible' property to t? Can we
1689 ;; have the point inside an overlay with an `intangible' property?
1690 ;; In 19.34 this does not exist so I cannot test it.
1691 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1692 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1693 (overlay-put ov 'invisible nil)
1694 (overlay-put ov 'intangible nil)))
1697 ;;; This is called at the end of isearch. It will open the overlays
1698 ;;; that contain the latest match. Obviously in case of a C-g the
1699 ;;; point returns to the original location which surely is not contain
1700 ;;; in any of these overlays, se we are safe in this case too.
1701 (defun isearch-open-necessary-overlays (ov)
1702 (let ((inside-overlay (and (> (point) (overlay-start ov))
1703 (< (point) (overlay-end ov))))
1704 ;; If this exists it means that the overlay was opened using
1705 ;; this function, not by us tweaking the overlay properties.
1706 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1707 (when (or inside-overlay (not fct-temp))
1708 ;; restore the values for the `invisible' and `intangible'
1709 ;; properties
1710 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1711 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1712 (overlay-put ov 'isearch-invisible nil)
1713 (overlay-put ov 'isearch-intangible nil))
1714 (if inside-overlay
1715 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1716 (if fct-temp
1717 (funcall fct-temp ov t)))))
1719 ;;; This is called when exiting isearch. It closes the temporary
1720 ;;; opened overlays, except the ones that contain the latest match.
1721 (defun isearch-clean-overlays ()
1722 (when isearch-opened-overlays
1723 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
1724 (setq isearch-opened-overlays nil)))
1727 (defun isearch-intersects-p (start0 end0 start1 end1)
1728 "Return t if regions START0..END0 and START1..END1 intersect."
1729 (or (and (>= start0 start1) (< start0 end1))
1730 (and (> end0 start1) (<= end0 end1))
1731 (and (>= start1 start0) (< start1 end0))
1732 (and (> end1 start0) (<= end1 end0))))
1735 ;;; Verify if the current match is outside of each element of
1736 ;;; `isearch-opened-overlays', if so close that overlay.
1738 (defun isearch-close-unnecessary-overlays (begin end)
1739 (let ((overlays isearch-opened-overlays))
1740 (setq isearch-opened-overlays nil)
1741 (dolist (ov overlays)
1742 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
1743 (push ov isearch-opened-overlays)
1744 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1745 (if fct-temp
1746 ;; If this exists it means that the overlay was opened
1747 ;; using this function, not by us tweaking the overlay
1748 ;; properties.
1749 (funcall fct-temp ov t)
1750 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1751 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1752 (overlay-put ov 'isearch-invisible nil)
1753 (overlay-put ov 'isearch-intangible nil)))))))
1756 (defun isearch-range-invisible (beg end)
1757 "Return t if all the text from BEG to END is invisible."
1758 (and (/= beg end)
1759 ;; Check that invisibility runs up to END.
1760 (save-excursion
1761 (goto-char beg)
1762 (let (
1763 ;; can-be-opened keeps track if we can open some overlays.
1764 (can-be-opened (eq search-invisible 'open))
1765 ;; the list of overlays that could be opened
1766 (crt-overlays nil))
1767 (when (and can-be-opened isearch-hide-immediately)
1768 (isearch-close-unnecessary-overlays beg end))
1769 ;; If the following character is currently invisible,
1770 ;; skip all characters with that same `invisible' property value.
1771 ;; Do that over and over.
1772 (while (and (< (point) end)
1773 (let ((prop
1774 (get-char-property (point) 'invisible)))
1775 (if (eq buffer-invisibility-spec t)
1776 prop
1777 (or (memq prop buffer-invisibility-spec)
1778 (assq prop buffer-invisibility-spec)))))
1779 (if (get-text-property (point) 'invisible)
1780 (progn
1781 (goto-char (next-single-property-change (point) 'invisible
1782 nil end))
1783 ;; if text is hidden by an `invisible' text property
1784 ;; we cannot open it at all.
1785 (setq can-be-opened nil))
1786 (unless (null can-be-opened)
1787 (let ((overlays (overlays-at (point)))
1788 ov-list
1790 invis-prop)
1791 (while overlays
1792 (setq o (car overlays)
1793 invis-prop (overlay-get o 'invisible))
1794 (if (if (eq buffer-invisibility-spec t)
1795 invis-prop
1796 (or (memq invis-prop buffer-invisibility-spec)
1797 (assq invis-prop buffer-invisibility-spec)))
1798 (if (overlay-get o 'isearch-open-invisible)
1799 (setq ov-list (cons o ov-list))
1800 ;; We found one overlay that cannot be
1801 ;; opened, that means the whole chunk
1802 ;; cannot be opened.
1803 (setq can-be-opened nil)))
1804 (setq overlays (cdr overlays)))
1805 (if can-be-opened
1806 ;; It makes sense to append to the open
1807 ;; overlays list only if we know that this is
1808 ;; t.
1809 (setq crt-overlays (append ov-list crt-overlays)))))
1810 (goto-char (next-overlay-change (point)))))
1811 ;; See if invisibility reaches up thru END.
1812 (if (>= (point) end)
1813 (if (and (not (null can-be-opened)) (consp crt-overlays))
1814 (progn
1815 (setq isearch-opened-overlays
1816 (append isearch-opened-overlays crt-overlays))
1817 (mapc 'isearch-open-overlay-temporary crt-overlays)
1818 nil)
1819 t))))))
1822 ;;; Highlighting
1824 (defvar isearch-overlay nil)
1826 (defun isearch-highlight (beg end)
1827 (unless (null search-highlight)
1828 (cond (isearch-overlay
1829 ;; Overlay already exists, just move it.
1830 (move-overlay isearch-overlay beg end (current-buffer)))
1833 ;; Overlay doesn't exist, create it.
1834 (setq isearch-overlay (make-overlay beg end))
1835 (overlay-put isearch-overlay 'face isearch)
1836 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
1837 ))))
1839 (defun isearch-dehighlight (totally)
1840 (when isearch-overlay
1841 (delete-overlay isearch-overlay)))
1844 ;;; General utilities
1847 (defun isearch-no-upper-case-p (string regexp-flag)
1848 "Return t if there are no upper case chars in STRING.
1849 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
1850 since they have special meaning in a regexp."
1851 (let (quote-flag (i 0) (len (length string)) found)
1852 (while (and (not found) (< i len))
1853 (let ((char (aref string i)))
1854 (if (and regexp-flag (eq char ?\\))
1855 (setq quote-flag (not quote-flag))
1856 (if (and (not quote-flag) (not (eq char (downcase char))))
1857 (setq found t))))
1858 (setq i (1+ i)))
1859 (not found)))
1861 ;; Portability functions to support various Emacs versions.
1863 (defun isearch-text-char-description (c)
1864 (cond
1865 ((< c ?\ ) (format "^%c" (+ c 64)))
1866 ((= c ?\^?) "^?")
1867 (t (char-to-string c))))
1869 ;; General function to unread characters or events.
1870 ;; Also insert them in a keyboard macro being defined.
1871 (defun isearch-unread (&rest char-or-events)
1872 (mapc 'store-kbd-macro-event char-or-events)
1873 (setq unread-command-events
1874 (append char-or-events unread-command-events)))
1877 ;;; isearch-lazy-highlight feature
1878 ;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1880 ;;; When active, *every* match for the current search string is
1881 ;;; highlighted: the current one using the normal isearch match color
1882 ;;; and all the others using `isearch-lazy-highlight-face'. The extra
1883 ;;; highlighting makes it easier to anticipate where the cursor will
1884 ;;; land each time you press C-s or C-r to repeat a pending search.
1885 ;;; Highlighting of these additional matches happens in a deferred
1886 ;;; fashion using "idle timers," so the cycles needed do not rob
1887 ;;; isearch of its usual snappy response.
1889 ;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1890 ;;; Specifically:
1891 ;;; - `isearch-update' is expected to be called (at least) every time
1892 ;;; the search string or window-start changes;
1893 ;;; - `isearch-string' is expected to contain the current search
1894 ;;; string as entered by the user;
1895 ;;; - the type of the current search is expected to be given by
1896 ;;; `isearch-word' and `isearch-regexp';
1897 ;;; - the direction of the current search is expected to be given by
1898 ;;; `isearch-forward';
1899 ;;; - the variable `isearch-invalid-regexp' is expected to be true
1900 ;;; iff `isearch-string' is an invalid regexp.
1902 (defgroup isearch-lazy-highlight nil
1903 "Lazy highlighting feature for incremental search."
1904 :prefix "isearch-lazy-highlight-"
1905 :version "21.1"
1906 :group 'isearch)
1908 (defcustom isearch-lazy-highlight t
1909 "*Controls the lazy-highlighting during incremental searches.
1910 When non-nil, all text in the buffer matching the current search
1911 string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1912 and `isearch-lazy-highlight-interval')."
1913 :type 'boolean
1914 :group 'isearch-lazy-highlight)
1916 (defcustom isearch-lazy-highlight-cleanup t
1917 "*Controls whether to remove extra highlighting after a search.
1918 If this is nil, extra highlighting can be \"manually\" removed with
1919 \\[isearch-lazy-highlight-cleanup]."
1920 :type 'boolean
1921 :group 'isearch-lazy-highlight)
1923 (defcustom isearch-lazy-highlight-initial-delay 0.25
1924 "*Seconds to wait before beginning to lazily highlight all matches."
1925 :type 'number
1926 :group 'isearch-lazy-highlight)
1928 (defcustom isearch-lazy-highlight-interval 0 ; 0.0625
1929 "*Seconds between lazily highlighting successive matches."
1930 :type 'number
1931 :group 'isearch-lazy-highlight)
1933 (defcustom isearch-lazy-highlight-max-at-a-time 20
1934 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
1935 Larger values may reduce isearch's responsiveness to user input;
1936 smaller values make matches highlight slowly.
1937 A value of nil means highlight all matches."
1938 :type '(choice (const :tag "All" nil)
1939 (integer :tag "Some"))
1940 :group 'isearch-lazy-highlight)
1942 (defgroup isearch-faces nil
1943 "Lazy highlighting feature for incremental search."
1944 :version "21.1"
1945 :group 'isearch)
1947 (defface isearch
1948 '((((type tty pc) (class color))
1949 (:background "magenta4" :foreground "cyan1"))
1950 (((class color) (background light))
1951 ;; The background must not be too dark, for that means
1952 ;; the character is hard to see when the cursor is there.
1953 (:background "magenta2" :foreground "lightskyblue1"))
1954 (((class color) (background dark))
1955 (:background "palevioletred2" :foreground "brown4"))
1956 (t (:inverse-video t)))
1957 "Face for highlighting Isearch matches."
1958 :group 'isearch-faces)
1959 (defvar isearch 'isearch)
1961 (defface isearch-lazy-highlight-face
1962 '((((type tty pc) (class color))
1963 (:background "turquoise3"))
1964 (((class color) (background light))
1965 (:background "paleturquoise"))
1966 (((class color) (background dark))
1967 (:background "paleturquoise4"))
1968 (t (:underline t)))
1969 "Face for lazy highlighting of Isearch matches other than the current one."
1970 :group 'isearch-faces)
1971 (defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
1973 (defvar isearch-lazy-highlight-overlays nil)
1974 (defvar isearch-lazy-highlight-wrapped nil)
1975 (defvar isearch-lazy-highlight-start nil)
1976 (defvar isearch-lazy-highlight-end nil)
1977 (defvar isearch-lazy-highlight-timer nil)
1978 (defvar isearch-lazy-highlight-last-string nil)
1979 (defvar isearch-lazy-highlight-window nil)
1980 (defvar isearch-lazy-highlight-window-start nil)
1981 (defvar isearch-lazy-highlight-case-fold-search nil)
1982 (defvar isearch-lazy-highlight-regexp nil)
1984 (defun isearch-lazy-highlight-cleanup (&optional force)
1985 "Stop lazy highlighting and remove extra highlighting from current buffer.
1986 FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
1987 is nil. This function is called when exiting an incremental search if
1988 `isearch-lazy-highlight-cleanup' is non-nil."
1989 (interactive '(t))
1990 (if (or force isearch-lazy-highlight-cleanup)
1991 (while isearch-lazy-highlight-overlays
1992 (delete-overlay (car isearch-lazy-highlight-overlays))
1993 (setq isearch-lazy-highlight-overlays
1994 (cdr isearch-lazy-highlight-overlays))))
1995 (when isearch-lazy-highlight-timer
1996 (cancel-timer isearch-lazy-highlight-timer)
1997 (setq isearch-lazy-highlight-timer nil)))
1999 (defun isearch-lazy-highlight-new-loop ()
2000 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
2001 This happens when `isearch-update' is invoked (which can cause the
2002 search string to change or the window to scroll)."
2003 (when (and isearch-lazy-highlight
2004 (null executing-kbd-macro)
2005 (sit-for 0) ;make sure (window-start) is credible
2006 (or (not (equal isearch-string
2007 isearch-lazy-highlight-last-string))
2008 (not (eq (selected-window)
2009 isearch-lazy-highlight-window))
2010 (not (eq isearch-lazy-highlight-case-fold-search
2011 isearch-case-fold-search))
2012 (not (eq isearch-lazy-highlight-regexp
2013 isearch-regexp))
2014 (not (= (window-start)
2015 isearch-lazy-highlight-window-start))))
2016 ;; something important did indeed change
2017 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
2018 (when (not isearch-invalid-regexp)
2019 (setq isearch-lazy-highlight-window (selected-window)
2020 isearch-lazy-highlight-window-start (window-start)
2021 isearch-lazy-highlight-start (point)
2022 isearch-lazy-highlight-end (point)
2023 isearch-lazy-highlight-last-string isearch-string
2024 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2025 isearch-lazy-highlight-regexp isearch-regexp
2026 isearch-lazy-highlight-wrapped nil)
2027 (setq isearch-lazy-highlight-timer
2028 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
2029 'isearch-lazy-highlight-update)))))
2031 (defun isearch-lazy-highlight-search ()
2032 "Search ahead for the next or previous match, for lazy highlighting.
2033 Attempt to do the search exactly the way the pending isearch would."
2034 (let ((case-fold-search isearch-case-fold-search)
2035 (choices (cond (isearch-word
2036 '(word-search-forward . word-search-backward))
2037 (isearch-regexp
2038 '(re-search-forward . re-search-backward))
2040 '(search-forward . search-backward)))))
2041 (funcall (if isearch-forward
2042 (car choices)
2043 (cdr choices))
2044 isearch-string
2045 (if isearch-forward
2046 (if isearch-lazy-highlight-wrapped
2047 isearch-lazy-highlight-start
2048 (window-end))
2049 (if isearch-lazy-highlight-wrapped
2050 isearch-lazy-highlight-end
2051 (window-start)))
2052 t)))
2054 (defun isearch-lazy-highlight-update ()
2055 "Update highlighting of other matches for current search."
2056 (let ((max isearch-lazy-highlight-max-at-a-time)
2057 (looping t)
2058 nomore)
2059 (save-excursion
2060 (save-match-data
2061 (goto-char (if isearch-forward
2062 isearch-lazy-highlight-end
2063 isearch-lazy-highlight-start))
2064 (while looping
2065 (let ((found (isearch-lazy-highlight-search)))
2066 (when max
2067 (setq max (1- max))
2068 (if (<= max 0)
2069 (setq looping nil)))
2070 (if found
2071 (let ((mb (match-beginning 0))
2072 (me (match-end 0)))
2073 (if (= mb me) ;zero-length match
2074 (forward-char 1)
2076 ;; non-zero-length match
2077 (let ((ov (make-overlay mb me)))
2078 (overlay-put ov 'face isearch-lazy-highlight-face)
2079 (overlay-put ov 'priority 0) ;lower than main overlay
2080 (overlay-put ov 'window (selected-window))
2081 (push ov isearch-lazy-highlight-overlays)))
2082 (if isearch-forward
2083 (setq isearch-lazy-highlight-end (point))
2084 (setq isearch-lazy-highlight-start (point))))
2086 ;; not found
2087 (if isearch-lazy-highlight-wrapped
2088 (setq looping nil
2089 nomore t)
2090 (setq isearch-lazy-highlight-wrapped t)
2091 (if isearch-forward
2092 (progn
2093 (setq isearch-lazy-highlight-end (window-start))
2094 (goto-char (window-start)))
2095 (setq isearch-lazy-highlight-start (window-end))
2096 (goto-char (window-end)))))))
2097 (unless nomore
2098 (setq isearch-lazy-highlight-timer
2099 (run-at-time isearch-lazy-highlight-interval nil
2100 'isearch-lazy-highlight-update)))))))
2102 (defun isearch-resume (search regexp word forward message case-fold)
2103 "Resume an incremental search.
2104 SEARCH is the string or regexp searched for.
2105 REGEXP non-nil means the resumed search was a regexp search.
2106 WORD non-nil means resume a word search.
2107 FORWARD non-nil means resume a forward search.
2108 MESSAGE is the echo-area message recorded for the search resumed.
2109 CASE-FOLD non-nil means the search was case-insensitive."
2110 (isearch-mode forward regexp nil nil word)
2111 (setq isearch-string search
2112 isearch-message message
2113 isearch-case-fold-search case-fold)
2114 (isearch-search))
2116 ;;; isearch.el ends here