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