(diff-default-read-only): Change default.
[emacs.git] / lisp / isearch.el
blob91a2c34870dab107206437d1f1f0bf2a9c0feedd
1 ;;; isearch.el --- incremental search minor mode
3 ;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 01, 2003, 2004
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.
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, which aren't bound to a scrolling-function
246 ;; exit the search.
247 (define-key map [t] 'isearch-other-control-char)
248 ;; Control chars, by default, end isearch mode transparently.
249 ;; We need these explicit definitions because, in a dense keymap,
250 ;; the binding for t does not affect characters.
251 ;; We use a dense keymap to save space.
252 (while (< i ?\ )
253 (define-key map (make-string 1 i) 'isearch-other-control-char)
254 (setq i (1+ i)))
256 ;; Single-byte printing chars extend the search string by default.
257 (setq i ?\ )
258 (while (< i 256)
259 (define-key map (vector i) 'isearch-printing-char)
260 (setq i (1+ i)))
262 ;; To handle local bindings with meta char prefix keys, define
263 ;; another full keymap. This must be done for any other prefix
264 ;; keys as well, one full keymap per char of the prefix key. It
265 ;; would be simpler to disable the global keymap, and/or have a
266 ;; default local key binding for any key not otherwise bound.
267 (let ((meta-map (make-sparse-keymap)))
268 (define-key map (char-to-string meta-prefix-char) meta-map)
269 (define-key map [escape] meta-map))
270 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
272 ;; Several non-printing chars change the searching behavior.
273 (define-key map "\C-s" 'isearch-repeat-forward)
274 (define-key map "\C-r" 'isearch-repeat-backward)
275 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
276 ;; combinations can be used to repeat regexp isearches that can
277 ;; be used to start these searches.
278 (define-key map "\M-\C-s" 'isearch-repeat-forward)
279 (define-key map "\M-\C-r" 'isearch-repeat-backward)
280 (define-key map "\177" 'isearch-delete-char)
281 (define-key map "\C-g" 'isearch-abort)
283 ;; This assumes \e is the meta-prefix-char.
284 (or (= ?\e meta-prefix-char)
285 (error "Inconsistency in isearch.el"))
286 (define-key map "\e\e\e" 'isearch-cancel)
287 (define-key map [escape escape escape] 'isearch-cancel)
289 (define-key map "\C-q" 'isearch-quote-char)
291 (define-key map "\r" 'isearch-exit)
292 (define-key map "\C-j" 'isearch-printing-char)
293 (define-key map "\t" 'isearch-printing-char)
294 (define-key map " " 'isearch-whitespace-chars)
295 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
297 (define-key map "\C-w" 'isearch-yank-word-or-char)
298 (define-key map "\C-y" 'isearch-yank-line)
300 ;; Define keys for regexp chars * ? |.
301 ;; Nothing special for + because it matches at least once.
302 (define-key map "*" 'isearch-*-char)
303 (define-key map "?" 'isearch-*-char)
304 (define-key map "{" 'isearch-{-char)
305 (define-key map "|" 'isearch-|-char)
307 ;; Turned off because I find I expect to get the global definition--rms.
308 ;; ;; Instead bind C-h to special help command for isearch-mode.
309 ;; (define-key map "\C-h" 'isearch-mode-help)
311 (define-key map "\M-n" 'isearch-ring-advance)
312 (define-key map "\M-p" 'isearch-ring-retreat)
313 (define-key map "\M-y" 'isearch-yank-kill)
315 (define-key map "\M-\t" 'isearch-complete)
317 ;; Pass frame events transparently so they won't exit the search.
318 ;; In particular, if we have more than one display open, then a
319 ;; switch-frame might be generated by someone typing at another keyboard.
320 (define-key map [switch-frame] nil)
321 (define-key map [delete-frame] nil)
322 (define-key map [iconify-frame] nil)
323 (define-key map [make-frame-visible] nil)
324 (define-key map [mouse-movement] nil)
325 ;; For searching multilingual text.
326 (define-key map "\C-\\" 'isearch-toggle-input-method)
327 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
329 ;; People expect to be able to paste with the mouse.
330 (define-key map [mouse-2] #'isearch-mouse-2)
331 (define-key map [down-mouse-2] nil)
333 ;; Some bindings you may want to put in your isearch-mode-hook.
334 ;; Suggest some alternates...
335 (define-key map "\M-c" 'isearch-toggle-case-fold)
336 (define-key map "\M-r" 'isearch-toggle-regexp)
337 (define-key map "\M-e" 'isearch-edit-string)
339 map)
340 "Keymap for `isearch-mode'.")
342 (defvar minibuffer-local-isearch-map
343 (let ((map (make-sparse-keymap)))
344 (set-keymap-parent map minibuffer-local-map)
345 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
346 (define-key map "\M-n" 'isearch-ring-advance-edit)
347 (define-key map "\M-p" 'isearch-ring-retreat-edit)
348 (define-key map "\M-\t" 'isearch-complete-edit)
349 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
350 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
351 map)
352 "Keymap for editing isearch strings in the minibuffer.")
354 ;; Internal variables declared globally for byte-compiler.
355 ;; These are all set with setq while isearching
356 ;; and bound locally while editing the search string.
358 (defvar isearch-forward nil) ; Searching in the forward direction.
359 (defvar isearch-regexp nil) ; Searching for a regexp.
360 (defvar isearch-word nil) ; Searching for words.
361 (defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
363 (defvar isearch-cmds nil
364 "Stack of search status sets.
365 Each set is a list of the form:
366 (STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
367 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH)")
369 (defvar isearch-string "") ; The current search string.
370 (defvar isearch-message "") ; text-char-description version of isearch-string
372 (defvar isearch-success t) ; Searching is currently successful.
373 (defvar isearch-invalid-regexp nil) ; Regexp not well formed.
374 (defvar isearch-within-brackets nil) ; Regexp has unclosed [.
375 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
376 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
377 (defvar isearch-barrier 0)
378 (defvar isearch-just-started nil)
379 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
381 ; case-fold-search while searching.
382 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
383 ; case in the search string is ignored.
384 (defvar isearch-case-fold-search nil)
386 (defvar isearch-last-case-fold-search nil)
388 ;; Used to save default value while isearch is active
389 (defvar isearch-original-minibuffer-message-timeout nil)
391 (defvar isearch-adjusted nil)
392 (defvar isearch-slow-terminal-mode nil)
393 ;; If t, using a small window.
394 (defvar isearch-small-window nil)
395 (defvar isearch-opoint 0)
396 ;; The window configuration active at the beginning of the search.
397 (defvar isearch-window-configuration nil)
399 ;; Flag to indicate a yank occurred, so don't move the cursor.
400 (defvar isearch-yank-flag nil)
402 ;; A function to be called after each input character is processed.
403 ;; (It is not called after characters that exit the search.)
404 ;; It is only set from an optional argument to `isearch-mode'.
405 (defvar isearch-op-fun nil)
407 ;; Is isearch-mode in a recursive edit for modal searching.
408 (defvar isearch-recursive-edit nil)
410 ;; Should isearch be terminated after doing one search?
411 (defvar isearch-nonincremental nil)
413 ;; New value of isearch-forward after isearch-edit-string.
414 (defvar isearch-new-forward nil)
416 ;; Accumulate here the overlays opened during searching.
417 (defvar isearch-opened-overlays nil)
419 ;; The value of input-method-function when isearch is invoked.
420 (defvar isearch-input-method-function nil)
422 ;; A flag to tell if input-method-function is locally bound when
423 ;; isearch is invoked.
424 (defvar isearch-input-method-local-p nil)
426 ;; Minor-mode-alist changes - kind of redundant with the
427 ;; echo area, but if isearching in multiple windows, it can be useful.
429 (or (assq 'isearch-mode minor-mode-alist)
430 (nconc minor-mode-alist
431 (list '(isearch-mode isearch-mode))))
433 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
434 (make-variable-buffer-local 'isearch-mode)
436 (define-key global-map "\C-s" 'isearch-forward)
437 (define-key esc-map "\C-s" 'isearch-forward-regexp)
438 (define-key global-map "\C-r" 'isearch-backward)
439 (define-key esc-map "\C-r" 'isearch-backward-regexp)
441 ;; Entry points to isearch-mode.
443 (defun isearch-forward (&optional regexp-p no-recursive-edit)
445 Do incremental search forward.
446 With a prefix argument, do an incremental regular expression search instead.
447 \\<isearch-mode-map>
448 As you type characters, they add to the search string and are found.
449 The following non-printing keys are bound in `isearch-mode-map'.
451 Type \\[isearch-delete-char] to cancel characters from end of search string.
452 Type \\[isearch-exit] to exit, leaving point at location found.
453 Type LFD (C-j) to match end of line.
454 Type \\[isearch-repeat-forward] to search again forward,\
455 \\[isearch-repeat-backward] to search again backward.
456 Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
457 string and search for it.
458 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
459 and search for it.
460 Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
461 and search for it.
462 Type \\[isearch-quote-char] to quote control character to search for it.
463 \\[isearch-abort] while searching or when search has failed cancels input\
464 back to what has
465 been found successfully.
466 \\[isearch-abort] when search is successful aborts and moves point to\
467 starting point.
469 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
470 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
471 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
473 Also supported is a search ring of the previous 16 search strings.
474 Type \\[isearch-ring-advance] to search for the next item in the search ring.
475 Type \\[isearch-ring-retreat] to search for the previous item in the search\
476 ring.
477 Type \\[isearch-complete] to complete the search string using the search ring.
479 If an input method is turned on in the current buffer, that input
480 method is also active while you are typing characters to search. To
481 toggle the input method, type \\[isearch-toggle-input-method]. It
482 also toggles the input method in the current buffer.
484 To use a different input method for searching, type
485 \\[isearch-toggle-specified-input-method], and specify an input method
486 you want to use.
488 The above keys, bound in `isearch-mode-map', are often controlled by
489 options; do M-x apropos on search-.* to find them.
490 Other control and meta characters terminate the search
491 and are then executed normally (depending on `search-exit-option').
492 Likewise for function keys and mouse button events.
494 If this function is called non-interactively, it does not return to
495 the calling function until the search is done."
497 (interactive "P\np")
498 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
500 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
502 Do incremental search forward for regular expression.
503 With a prefix argument, do a regular string search instead.
504 Like ordinary incremental search except that your input
505 is treated as a regexp. See \\[isearch-forward] for more info."
506 (interactive "P\np")
507 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
509 (defun isearch-backward (&optional regexp-p no-recursive-edit)
511 Do incremental search backward.
512 With a prefix argument, do a regular expression search instead.
513 See \\[isearch-forward] for more information."
514 (interactive "P\np")
515 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
517 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
519 Do incremental search backward for regular expression.
520 With a prefix argument, do a regular string search instead.
521 Like ordinary incremental search except that your input
522 is treated as a regexp. See \\[isearch-forward] for more info."
523 (interactive "P\np")
524 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
527 (defun isearch-mode-help ()
528 (interactive)
529 (describe-function 'isearch-forward)
530 (isearch-update))
533 ;; isearch-mode only sets up incremental search for the minor mode.
534 ;; All the work is done by the isearch-mode commands.
536 ;; Not used yet:
537 ;;(defvar isearch-commands '(isearch-forward isearch-backward
538 ;; isearch-forward-regexp isearch-backward-regexp)
539 ;; "List of commands for which isearch-mode does not recursive-edit.")
542 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
543 "Start isearch minor mode. Called by `isearch-forward', etc.
545 \\{isearch-mode-map}"
547 ;; Initialize global vars.
548 (setq isearch-forward forward
549 isearch-regexp regexp
550 isearch-word word-p
551 isearch-op-fun op-fun
552 isearch-last-case-fold-search isearch-case-fold-search
553 isearch-case-fold-search case-fold-search
554 isearch-string ""
555 isearch-message ""
556 isearch-cmds nil
557 isearch-success t
558 isearch-wrapped nil
559 isearch-barrier (point)
560 isearch-adjusted nil
561 isearch-yank-flag nil
562 isearch-invalid-regexp nil
563 isearch-within-brackets nil
564 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
565 (> (window-height)
566 (* 4
567 (abs search-slow-window-lines))))
568 isearch-other-end nil
569 isearch-small-window nil
570 isearch-just-started t
571 isearch-start-hscroll (window-hscroll)
573 isearch-opoint (point)
574 search-ring-yank-pointer nil
575 isearch-opened-overlays nil
576 isearch-input-method-function input-method-function
577 isearch-input-method-local-p (local-variable-p 'input-method-function)
578 regexp-search-ring-yank-pointer nil
580 ;; Save the original value of `minibuffer-message-timeout', and
581 ;; set it to nil so that isearch's messages don't get timed out.
582 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
583 minibuffer-message-timeout nil)
585 ;; We must bypass input method while reading key. When a user type
586 ;; printable character, appropriate input method is turned on in
587 ;; minibuffer to read multibyte characters.
588 (or isearch-input-method-local-p
589 (make-local-variable 'input-method-function))
590 (setq input-method-function nil)
592 (looking-at "")
593 (setq isearch-window-configuration
594 (if isearch-slow-terminal-mode (current-window-configuration) nil))
596 ;; Maybe make minibuffer frame visible and/or raise it.
597 (let ((frame (window-frame (minibuffer-window))))
598 (unless (memq (frame-live-p frame) '(nil t))
599 (unless (frame-visible-p frame)
600 (make-frame-visible frame))
601 (if minibuffer-auto-raise
602 (raise-frame frame))))
604 (setq isearch-mode " Isearch") ;; forward? regexp?
605 (force-mode-line-update)
607 (isearch-push-state)
609 (setq overriding-terminal-local-map isearch-mode-map)
610 (isearch-update)
611 (run-hooks 'isearch-mode-hook)
613 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
614 (add-hook 'kbd-macro-termination-hook 'isearch-done)
616 ;; isearch-mode can be made modal (in the sense of not returning to
617 ;; the calling function until searching is completed) by entering
618 ;; a recursive-edit and exiting it when done isearching.
619 (if recursive-edit
620 (let ((isearch-recursive-edit t))
621 (recursive-edit)))
622 isearch-success)
625 ;; Some high level utilities. Others below.
627 (defun isearch-update ()
628 ;; Called after each command to update the display.
629 (if (and (null unread-command-events)
630 (null executing-kbd-macro))
631 (progn
632 (if (not (input-pending-p))
633 (isearch-message))
634 (if (and isearch-slow-terminal-mode
635 (not (or isearch-small-window
636 (pos-visible-in-window-p))))
637 (let ((found-point (point)))
638 (setq isearch-small-window t)
639 (move-to-window-line 0)
640 (let ((window-min-height 1))
641 (split-window nil (if (< search-slow-window-lines 0)
642 (1+ (- search-slow-window-lines))
643 (- (window-height)
644 (1+ search-slow-window-lines)))))
645 (if (< search-slow-window-lines 0)
646 (progn (vertical-motion (- 1 search-slow-window-lines))
647 (set-window-start (next-window) (point))
648 (set-window-hscroll (next-window)
649 (window-hscroll))
650 (set-window-hscroll (selected-window) 0))
651 (other-window 1))
652 (goto-char found-point))
653 ;; Keep same hscrolling as at the start of the search when possible
654 (let ((current-scroll (window-hscroll)))
655 (set-window-hscroll (selected-window) isearch-start-hscroll)
656 (unless (pos-visible-in-window-p)
657 (set-window-hscroll (selected-window) current-scroll))))
658 (if isearch-other-end
659 (if (< isearch-other-end (point)) ; isearch-forward?
660 (isearch-highlight isearch-other-end (point))
661 (isearch-highlight (point) isearch-other-end))
662 (isearch-dehighlight nil))
664 (setq ;; quit-flag nil not for isearch-mode
665 isearch-adjusted nil
666 isearch-yank-flag nil)
667 (isearch-lazy-highlight-new-loop)
668 ;; We must prevent the point moving to the end of composition when a
669 ;; part of the composition has just been searched.
670 (setq disable-point-adjustment t))
672 (defun isearch-done (&optional nopush edit)
673 (if isearch-resume-enabled
674 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
675 ,isearch-word ,isearch-forward
676 ,isearch-message
677 ',isearch-case-fold-search)))
678 (unless (equal (car command-history) command)
679 (setq command-history (cons command command-history)))))
681 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
682 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
683 (setq isearch-lazy-highlight-start nil)
685 ;; Called by all commands that terminate isearch-mode.
686 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
687 (setq overriding-terminal-local-map nil)
688 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
689 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
690 (isearch-dehighlight t)
691 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
692 (let ((found-start (window-start (selected-window)))
693 (found-point (point)))
694 (if isearch-window-configuration
695 (set-window-configuration isearch-window-configuration))
697 (if isearch-small-window
698 (goto-char found-point)
699 ;; Exiting the save-window-excursion clobbers window-start; restore it.
700 (set-window-start (selected-window) found-start t))
702 ;; If there was movement, mark the starting position.
703 ;; Maybe should test difference between and set mark iff > threshold.
704 (if (/= (point) isearch-opoint)
705 (or (and transient-mark-mode mark-active)
706 (progn
707 (push-mark isearch-opoint t)
708 (or executing-kbd-macro (> (minibuffer-depth) 0)
709 (message "Mark saved where search started"))))))
711 (setq isearch-mode nil)
712 (if isearch-input-method-local-p
713 (setq input-method-function isearch-input-method-function)
714 (kill-local-variable 'input-method-function))
716 (force-mode-line-update)
718 ;; If we ended in the middle of some intangible text,
719 ;; move to the further end of that intangible text.
720 (let ((after (if (eobp) nil
721 (get-text-property (point) 'intangible)))
722 (before (if (bobp) nil
723 (get-text-property (1- (point)) 'intangible))))
724 (when (and before after (eq before after))
725 (if isearch-forward
726 (goto-char (next-single-property-change (point) 'intangible))
727 (goto-char (previous-single-property-change (point) 'intangible)))))
729 (if (and (> (length isearch-string) 0) (not nopush))
730 ;; Update the ring data.
731 (isearch-update-ring isearch-string isearch-regexp))
733 (run-hooks 'isearch-mode-end-hook)
734 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
736 (defun isearch-update-ring (string &optional regexp)
737 "Add STRING to the beginning of the search ring.
738 REGEXP says which ring to use."
739 (if regexp
740 (if (or (null regexp-search-ring)
741 (not (string= string (car regexp-search-ring))))
742 (progn
743 (push string regexp-search-ring)
744 (if (> (length regexp-search-ring) regexp-search-ring-max)
745 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
746 nil))))
747 (if (or (null search-ring)
748 (not (string= string (car search-ring))))
749 (progn
750 (push string search-ring)
751 (if (> (length search-ring) search-ring-max)
752 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
754 ;; Switching buffers should first terminate isearch-mode.
755 ;; ;; For Emacs 19, the frame switch event is handled.
756 ;; (defun isearch-switch-frame-handler ()
757 ;; (interactive) ;; Is this necessary?
758 ;; ;; First terminate isearch-mode.
759 ;; (isearch-done)
760 ;; (isearch-clean-overlays)
761 ;; (handle-switch-frame (car (cdr last-command-char))))
764 ;; Commands active while inside of the isearch minor mode.
766 (defun isearch-exit ()
767 "Exit search normally.
768 However, if this is the first command after starting incremental
769 search and `search-nonincremental-instead' is non-nil, do a
770 nonincremental search instead via `isearch-edit-string'."
771 (interactive)
772 (if (and search-nonincremental-instead
773 (= 0 (length isearch-string)))
774 (let ((isearch-nonincremental t))
775 (isearch-edit-string)))
776 (isearch-done)
777 (isearch-clean-overlays))
780 (defun isearch-edit-string ()
781 "Edit the search string in the minibuffer.
782 The following additional command keys are active while editing.
783 \\<minibuffer-local-isearch-map>
784 \\[exit-minibuffer] to resume incremental searching with the edited string.
785 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
786 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
787 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
788 \\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
789 \\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
790 \\[isearch-complete-edit] to complete the search string using the search ring.
791 \\<isearch-mode-map>
792 If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
794 ;; This code is very hairy for several reasons, explained in the code.
795 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
796 ;; If there were a way to catch any change of buffer from the minibuffer,
797 ;; this could be simplified greatly.
798 ;; Editing doesn't back up the search point. Should it?
799 (interactive)
800 (condition-case err
801 (progn
802 (let ((isearch-nonincremental isearch-nonincremental)
804 ;; Locally bind all isearch global variables to protect them
805 ;; from recursive isearching.
806 ;; isearch-string -message and -forward are not bound
807 ;; so they may be changed. Instead, save the values.
808 (isearch-new-string isearch-string)
809 (isearch-new-message isearch-message)
810 (isearch-new-forward isearch-forward)
811 (isearch-new-word isearch-word)
813 (isearch-regexp isearch-regexp)
814 (isearch-op-fun isearch-op-fun)
815 (isearch-cmds isearch-cmds)
816 (isearch-success isearch-success)
817 (isearch-wrapped isearch-wrapped)
818 (isearch-barrier isearch-barrier)
819 (isearch-adjusted isearch-adjusted)
820 (isearch-yank-flag isearch-yank-flag)
821 (isearch-invalid-regexp isearch-invalid-regexp)
822 (isearch-within-brackets isearch-within-brackets)
823 ;;; Don't bind this. We want isearch-search, below, to set it.
824 ;;; And the old value won't matter after that.
825 ;;; (isearch-other-end isearch-other-end)
826 ;;; Perhaps some of these other variables should be bound for a
827 ;;; shorter period, ending before the next isearch-search.
828 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
829 (isearch-opoint isearch-opoint)
830 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
831 (isearch-small-window isearch-small-window)
832 (isearch-recursive-edit isearch-recursive-edit)
833 ;; Save current configuration so we can restore it here.
834 (isearch-window-configuration (current-window-configuration))
836 ;; Temporarily restore `minibuffer-message-timeout'.
837 (minibuffer-message-timeout
838 isearch-original-minibuffer-message-timeout)
839 (isearch-original-minibuffer-message-timeout
840 isearch-original-minibuffer-message-timeout)
843 ;; Actually terminate isearching until editing is done.
844 ;; This is so that the user can do anything without failure,
845 ;; like switch buffers and start another isearch, and return.
846 (condition-case err
847 (isearch-done t t)
848 (exit nil)) ; was recursive editing
850 (isearch-message) ;; for read-char
851 (unwind-protect
852 (let* (;; Why does following read-char echo?
853 ;;(echo-keystrokes 0) ;; not needed with above message
854 (e (let ((cursor-in-echo-area t))
855 (read-event)))
856 ;; Binding minibuffer-history-symbol to nil is a work-around
857 ;; for some incompatibility with gmhist.
858 (minibuffer-history-symbol)
859 (message-log-max nil))
860 ;; If the first character the user types when we prompt them
861 ;; for a string is the yank-word character, then go into
862 ;; word-search mode. Otherwise unread that character and
863 ;; read a key the normal way.
864 ;; Word search does not apply (yet) to regexp searches,
865 ;; no check is made here.
866 (message (isearch-message-prefix nil nil t))
867 (if (memq (lookup-key isearch-mode-map (vector e))
868 '(isearch-yank-word
869 isearch-yank-word-or-char))
870 (setq isearch-word t;; so message-prefix is right
871 isearch-new-word t)
872 (cancel-kbd-macro-events)
873 (isearch-unread e))
874 (setq cursor-in-echo-area nil)
875 (setq isearch-new-string
876 (let (junk-ring)
877 (read-from-minibuffer
878 (isearch-message-prefix nil nil isearch-nonincremental)
879 isearch-string
880 minibuffer-local-isearch-map nil
881 'junk-ring))
882 isearch-new-message
883 (mapconcat 'isearch-text-char-description
884 isearch-new-string "")))
885 ;; Always resume isearching by restarting it.
886 (isearch-mode isearch-forward
887 isearch-regexp
888 isearch-op-fun
890 isearch-word)
892 ;; Copy new local values to isearch globals
893 (setq isearch-string isearch-new-string
894 isearch-message isearch-new-message
895 isearch-forward isearch-new-forward
896 isearch-word isearch-new-word))
898 ;; Empty isearch-string means use default.
899 (if (= 0 (length isearch-string))
900 (setq isearch-string (or (car (if isearch-regexp
901 regexp-search-ring
902 search-ring))
905 isearch-message
906 (mapconcat 'isearch-text-char-description
907 isearch-string ""))
908 ;; This used to set the last search string,
909 ;; but I think it is not right to do that here.
910 ;; Only the string actually used should be saved.
913 ;; Push the state as of before this C-s.
914 (isearch-push-state)
916 ;; Reinvoke the pending search.
917 (isearch-search)
918 (isearch-update)
919 (if isearch-nonincremental
920 (progn
921 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
922 (isearch-done))))
924 (quit ; handle abort-recursive-edit
925 (isearch-abort) ;; outside of let to restore outside global values
928 (defun isearch-nonincremental-exit-minibuffer ()
929 (interactive)
930 (setq isearch-nonincremental t)
931 (exit-minibuffer))
933 (defun isearch-forward-exit-minibuffer ()
934 (interactive)
935 (setq isearch-new-forward t)
936 (exit-minibuffer))
938 (defun isearch-reverse-exit-minibuffer ()
939 (interactive)
940 (setq isearch-new-forward nil)
941 (exit-minibuffer))
943 (defun isearch-cancel ()
944 "Terminate the search and go back to the starting point."
945 (interactive)
946 (goto-char isearch-opoint)
947 (isearch-done t)
948 (isearch-clean-overlays)
949 (signal 'quit nil)) ; and pass on quit signal
951 (defun isearch-abort ()
952 "Abort incremental search mode if searching is successful, signaling quit.
953 Otherwise, revert to previous successful search and continue searching.
954 Use `isearch-exit' to quit without signaling."
955 (interactive)
956 ;; (ding) signal instead below, if quitting
957 (discard-input)
958 (if isearch-success
959 ;; If search is successful, move back to starting point
960 ;; and really do quit.
961 (progn (goto-char isearch-opoint)
962 (setq isearch-success nil)
963 (isearch-done t) ; exit isearch
964 (isearch-clean-overlays)
965 (signal 'quit nil)) ; and pass on quit signal
966 ;; If search is failing, or has an incomplete regexp,
967 ;; rub out until it is once more successful.
968 (while (or (not isearch-success) isearch-invalid-regexp)
969 (isearch-pop-state))
970 (isearch-update)))
972 (defun isearch-repeat (direction)
973 ;; Utility for isearch-repeat-forward and -backward.
974 (if (eq isearch-forward (eq direction 'forward))
975 ;; C-s in forward or C-r in reverse.
976 (if (equal isearch-string "")
977 ;; If search string is empty, use last one.
978 (setq isearch-string
979 (or (if isearch-regexp
980 (car regexp-search-ring)
981 (car search-ring))
982 (error "No previous search string"))
983 isearch-message
984 (mapconcat 'isearch-text-char-description
985 isearch-string "")
986 isearch-case-fold-search isearch-last-case-fold-search)
987 ;; If already have what to search for, repeat it.
988 (or isearch-success
989 (progn
990 (goto-char (if isearch-forward (point-min) (point-max)))
991 (setq isearch-wrapped t))))
992 ;; C-s in reverse or C-r in forward, change direction.
993 (setq isearch-forward (not isearch-forward)))
995 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
997 (if (equal isearch-string "")
998 (setq isearch-success t)
999 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
1000 (not isearch-just-started))
1001 ;; If repeating a search that found
1002 ;; an empty string, ensure we advance.
1003 (if (if isearch-forward (eobp) (bobp))
1004 ;; If there's nowhere to advance to, fail (and wrap next time).
1005 (progn
1006 (setq isearch-success nil)
1007 (ding))
1008 (forward-char (if isearch-forward 1 -1))
1009 (isearch-search))
1010 (isearch-search)))
1012 (isearch-push-state)
1013 (isearch-update))
1015 (defun isearch-repeat-forward ()
1016 "Repeat incremental search forwards."
1017 (interactive)
1018 (isearch-repeat 'forward))
1020 (defun isearch-repeat-backward ()
1021 "Repeat incremental search backwards."
1022 (interactive)
1023 (isearch-repeat 'backward))
1025 (defun isearch-toggle-regexp ()
1026 "Toggle regexp searching on or off."
1027 ;; The status stack is left unchanged.
1028 (interactive)
1029 (setq isearch-regexp (not isearch-regexp))
1030 (if isearch-regexp (setq isearch-word nil))
1031 (isearch-update))
1033 (defun isearch-toggle-case-fold ()
1034 "Toggle case folding in searching on or off."
1035 (interactive)
1036 (setq isearch-case-fold-search
1037 (if isearch-case-fold-search nil 'yes))
1038 (let ((message-log-max nil))
1039 (message "%s%s [case %ssensitive]"
1040 (isearch-message-prefix nil nil isearch-nonincremental)
1041 isearch-message
1042 (if isearch-case-fold-search "in" "")))
1043 (setq isearch-adjusted t)
1044 (sit-for 1)
1045 (isearch-update))
1047 (defun isearch-delete-char ()
1048 "Discard last input item and move point back.
1049 If no previous match was done, just beep."
1050 (interactive)
1051 (if (null (cdr isearch-cmds))
1052 (ding)
1053 (isearch-pop-state))
1054 (isearch-update))
1057 (defun isearch-yank-string (string)
1058 "Pull STRING into search string."
1059 ;; Downcase the string if not supposed to case-fold yanked strings.
1060 (if (and isearch-case-fold-search
1061 (eq 'not-yanks search-upper-case))
1062 (setq string (downcase string)))
1063 (if isearch-regexp (setq string (regexp-quote string)))
1064 (setq isearch-string (concat isearch-string string)
1065 isearch-message
1066 (concat isearch-message
1067 (mapconcat 'isearch-text-char-description
1068 string ""))
1069 ;; Don't move cursor in reverse search.
1070 isearch-yank-flag t)
1071 (isearch-search-and-update))
1073 (defun isearch-yank-kill ()
1074 "Pull string from kill ring into search string."
1075 (interactive)
1076 (isearch-yank-string (current-kill 0)))
1078 (defun isearch-yank-x-selection ()
1079 "Pull current X selection into search string."
1080 (interactive)
1081 (isearch-yank-string (x-get-selection)))
1084 (defun isearch-mouse-2 (click)
1085 "Handle mouse-2 in Isearch mode.
1086 For a click in the echo area, invoke `isearch-yank-x-selection'.
1087 Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
1088 (interactive "e")
1089 (let* ((w (posn-window (event-start click)))
1090 (overriding-terminal-local-map nil)
1091 (key (vector (event-basic-type click)))
1092 ;; FIXME: `key-binding' should accept an event as argument
1093 ;; and do all the overlay/text-properties lookup etc...
1094 (binding (with-current-buffer
1095 (if (window-live-p w) (window-buffer w) (current-buffer))
1096 (key-binding key))))
1097 (if (and (window-minibuffer-p w)
1098 (not (minibuffer-window-active-p w))) ; in echo area
1099 (isearch-yank-x-selection)
1100 (when (functionp binding)
1101 (call-interactively binding)))))
1104 (defun isearch-yank-internal (jumpform)
1105 "Pull the text from point to the point reached by JUMPFORM.
1106 JUMPFORM is a lambda expression that takes no arguments and returns a
1107 buffer position, possibly having moved point to that position. For
1108 example, it might move point forward by a word and return point, or it
1109 might return the position of the end of the line."
1110 (isearch-yank-string
1111 (save-excursion
1112 (and (not isearch-forward) isearch-other-end
1113 (goto-char isearch-other-end))
1114 (buffer-substring-no-properties (point) (funcall jumpform)))))
1116 (defun isearch-yank-char ()
1117 "Pull next letter from buffer into search string."
1118 (interactive)
1119 (isearch-yank-internal (lambda () (forward-char 1) (point))))
1121 (defun isearch-yank-word-or-char ()
1122 "Pull next character or word from buffer into search string."
1123 (interactive)
1124 (isearch-yank-internal
1125 (lambda ()
1126 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1127 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1128 (forward-word 1)
1129 (forward-char 1)) (point))))
1131 (defun isearch-yank-word ()
1132 "Pull next word from buffer into search string."
1133 (interactive)
1134 (isearch-yank-internal (lambda () (forward-word 1) (point))))
1136 (defun isearch-yank-line ()
1137 "Pull rest of line from buffer into search string."
1138 (interactive)
1139 (isearch-yank-internal 'line-end-position))
1142 (defun isearch-search-and-update ()
1143 ;; Do the search and update the display.
1144 (when (or isearch-success
1145 ;; unsuccessful regexp search may become
1146 ;; successful by addition of characters which
1147 ;; make isearch-string valid
1148 isearch-regexp
1149 ;; If the string was found but was completely invisible,
1150 ;; it might now be partly visible, so try again.
1151 (prog1 isearch-hidden (setq isearch-hidden nil)))
1152 ;; In reverse search, adding stuff at
1153 ;; the end may cause zero or many more chars to be
1154 ;; matched, in the string following point.
1155 ;; Allow all those possibilities without moving point as
1156 ;; long as the match does not extend past search origin.
1157 (if (and (not isearch-forward) (not isearch-adjusted)
1158 (condition-case ()
1159 (let ((case-fold-search isearch-case-fold-search))
1160 (if (and (eq case-fold-search t) search-upper-case)
1161 (setq case-fold-search
1162 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1163 (looking-at (if isearch-regexp isearch-string
1164 (regexp-quote isearch-string))))
1165 (error nil))
1166 (or isearch-yank-flag
1167 (<= (match-end 0)
1168 (min isearch-opoint isearch-barrier))))
1169 (progn
1170 (setq isearch-success t
1171 isearch-invalid-regexp nil
1172 isearch-within-brackets nil
1173 isearch-other-end (match-end 0))
1174 (if (and (eq isearch-case-fold-search t) search-upper-case)
1175 (setq isearch-case-fold-search
1176 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1177 ;; Not regexp, not reverse, or no match at point.
1178 (if (and isearch-other-end (not isearch-adjusted))
1179 (goto-char (if isearch-forward isearch-other-end
1180 (min isearch-opoint
1181 isearch-barrier
1182 (1+ isearch-other-end)))))
1183 (isearch-search)
1185 (isearch-push-state)
1186 (if isearch-op-fun (funcall isearch-op-fun))
1187 (isearch-update))
1190 (defun isearch-{-char ()
1191 "Handle \{ specially in regexps."
1192 (interactive)
1193 (isearch-*-char t))
1195 ;; *, ?, and | chars can make a regexp more liberal.
1196 ;; They can make a regexp match sooner or make it succeed instead of failing.
1197 ;; So go back to place last successful search started
1198 ;; or to the last ^S/^R (barrier), whichever is nearer.
1199 ;; + needs no special handling because the string must match at least once.
1201 (defun isearch-*-char (&optional want-backslash)
1202 "Handle * and ? specially in regexps.
1203 When WANT-BACKSLASH is non-nil, do special handling for \{."
1204 (interactive)
1205 (if isearch-regexp
1206 (let ((idx (length isearch-string)))
1207 (while (and (> idx 0)
1208 (eq (aref isearch-string (1- idx)) ?\\))
1209 (setq idx (1- idx)))
1210 ;; * and ? are special when not preceded by \.
1211 ;; { is special when it is preceded by \.
1212 (when (= (mod (- (length isearch-string) idx) 2)
1213 (if want-backslash 1 0))
1214 (setq isearch-adjusted t)
1215 ;; Get the isearch-other-end from before the last search.
1216 ;; We want to start from there,
1217 ;; so that we don't retreat farther than that.
1218 ;; (car isearch-cmds) is after last search;
1219 ;; (car (cdr isearch-cmds)) is from before it.
1220 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1221 (setq cs (or cs isearch-barrier))
1222 (goto-char
1223 (if isearch-forward
1224 (max cs isearch-barrier)
1225 (min cs isearch-barrier)))))))
1226 (isearch-process-search-char last-command-char))
1229 (defun isearch-|-char ()
1230 "If in regexp search, jump to the barrier."
1231 (interactive)
1232 (if isearch-regexp
1233 (progn
1234 (setq isearch-adjusted t)
1235 (goto-char isearch-barrier)))
1236 (isearch-process-search-char last-command-char))
1238 (defun isearch-unread-key-sequence (keylist)
1239 "Unread the given key-sequence KEYLIST.
1240 Scroll-bar or mode-line events are processed appropriately."
1241 (cancel-kbd-macro-events)
1242 (apply 'isearch-unread keylist)
1243 ;; If the event was a scroll-bar or mode-line click, the event will have
1244 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
1245 ;; it here, because this symbol will be attached to the event again next
1246 ;; time it gets read by read-key-sequence.
1248 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
1249 ;; modify the event anymore in 21 because read_key_sequence no longer
1250 ;; modifies events to produce fake prefix keys.")
1251 (if (and (> (length keylist) 1)
1252 (symbolp (car keylist))
1253 (listp (cadr keylist))
1254 (not (numberp (posn-point
1255 (event-start (cadr keylist) )))))
1256 (pop unread-command-events)))
1258 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1259 ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
1261 ;; The idea here is that certain vertical scrolling commands (like C-l
1262 ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
1263 ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
1264 ;; tamper with isearch's state, or move point. It is unacceptable for the
1265 ;; search string to be scrolled out of the current window. If a command
1266 ;; attempts this, we scroll the text back again.
1268 ;; We implement this feature with a property called `isearch-scroll'.
1269 ;; If a command's symbol has the value t for this property it is a
1270 ;; scrolling command. The feature needs to be enabled by setting the
1271 ;; customizable variable `isearch-allow-scroll' to a non-nil value.
1273 ;; The universal argument commands (e.g. C-u) in simple.el are marked
1274 ;; as scrolling commands, and isearch.el has been amended to allow
1275 ;; prefix arguments to be passed through to scrolling commands. Thus
1276 ;; M-0 C-l will scroll point to the top of the window.
1278 ;; Horizontal scrolling commands are currently not catered for.
1279 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1281 ;; Set the isearch-scroll property on some standard functions:
1282 ;; Scroll-bar functions:
1283 (if (fboundp 'scroll-bar-toolkit-scroll)
1284 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
1285 (if (fboundp 'mac-handle-scroll-bar-event)
1286 (put 'mac-handle-scroll-bar-event 'isearch-scroll t))
1287 (if (fboundp 'w32-handle-scroll-bar-event)
1288 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
1290 ;; Commands which scroll the window:
1291 (put 'recenter 'isearch-scroll t)
1292 (put 'reposition-window 'isearch-scroll t)
1293 (put 'scroll-up 'isearch-scroll t)
1294 (put 'scroll-down 'isearch-scroll t)
1296 ;; Commands which act on the other window
1297 (put 'list-buffers 'isearch-scroll t)
1298 (put 'scroll-other-window 'isearch-scroll t)
1299 (put 'scroll-other-window-down 'isearch-scroll t)
1300 (put 'beginning-of-buffer-other-window 'isearch-scroll t)
1301 (put 'end-of-buffer-other-window 'isearch-scroll t)
1303 ;; Commands which change the window layout
1304 (put 'delete-other-windows 'isearch-scroll t)
1305 (put 'balance-windows 'isearch-scroll t)
1306 (put 'split-window-vertically 'isearch-scroll t)
1307 (put 'enlarge-window 'isearch-scroll t)
1309 ;; Universal argument commands
1310 (put 'universal-argument 'isearch-scroll t)
1311 (put 'negative-argument 'isearch-scroll t)
1312 (put 'digit-argument 'isearch-scroll t)
1314 (defcustom isearch-allow-scroll nil
1315 "If non-nil, scrolling commands are allowed during incremental search."
1316 :type 'boolean
1317 :group 'isearch)
1319 (defun isearch-string-out-of-window (isearch-point)
1320 "Test whether the search string is currently outside of the window.
1321 Return nil if it's completely visible, or if point is visible,
1322 together with as much of the search string as will fit; the symbol
1323 `above' if we need to scroll the text downwards; the symbol `below',
1324 if upwards."
1325 (let ((w-start (window-start))
1326 (w-end (window-end nil t))
1327 (w-L1 (save-excursion (move-to-window-line 1) (point)))
1328 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
1329 start end) ; start and end of search string in buffer
1330 (if isearch-forward
1331 (setq end isearch-point start (or isearch-other-end isearch-point))
1332 (setq start isearch-point end (or isearch-other-end isearch-point)))
1333 (cond ((or (and (>= start w-start) (<= end w-end))
1334 (if isearch-forward
1335 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
1336 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
1337 nil)
1338 ((and (< start w-start)
1339 (< isearch-point w-L-1))
1340 'above)
1341 (t 'below))))
1343 (defun isearch-back-into-window (above isearch-point)
1344 "Scroll the window to bring the search string back into view.
1345 Restore point to ISEARCH-POINT in the process. ABOVE is t when the
1346 search string is above the top of the window, nil when it is beneath
1347 the bottom."
1348 (let (start end)
1349 (if isearch-forward
1350 (setq end isearch-point start (or isearch-other-end isearch-point))
1351 (setq start isearch-point end (or isearch-other-end isearch-point)))
1352 (if above
1353 (progn
1354 (goto-char start)
1355 (recenter 0)
1356 (when (>= isearch-point (window-end nil t))
1357 (goto-char isearch-point)
1358 (recenter -1)))
1359 (goto-char end)
1360 (recenter -1)
1361 (when (< isearch-point (window-start))
1362 (goto-char isearch-point)
1363 (recenter 0))))
1364 (goto-char isearch-point))
1366 (defun isearch-reread-key-sequence-naturally (keylist)
1367 "Reread key sequence KEYLIST with Isearch mode's keymap deactivated.
1368 Return the key sequence as a string/vector."
1369 (isearch-unread-key-sequence keylist)
1370 (let (overriding-terminal-local-map)
1371 (read-key-sequence nil))) ; This will go through function-key-map, if nec.
1373 (defun isearch-lookup-scroll-key (key-seq)
1374 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
1375 Otherwise return nil."
1376 (let* ((overriding-terminal-local-map nil)
1377 (binding (key-binding key-seq)))
1378 (and binding (symbolp binding) (commandp binding)
1379 (eq (get binding 'isearch-scroll) t)
1380 binding)))
1382 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1384 (defun isearch-other-meta-char (&optional arg)
1385 "Process a miscellaneous key sequence in Isearch mode.
1387 Try to convert the current key-sequence to something usable in Isearch
1388 mode, either by converting it with `function-key-map', downcasing a
1389 key with C-<upper case>, or finding a \"scrolling command\" bound to
1390 it. \(In the last case, we may have to read more events.) If so,
1391 either unread the converted sequence or execute the command.
1393 Otherwise, if `search-exit-option' is non-nil (the default) unread the
1394 key-sequence and exit the search normally. If it is the symbol
1395 `edit', the search string is edited in the minibuffer and the meta
1396 character is unread so that it applies to editing the string.
1398 ARG is the prefix argument. It will be transmitted through to the
1399 scrolling command or to the command whose key-sequence exits
1400 Isearch mode."
1401 (interactive "P")
1402 (let* ((key (if current-prefix-arg ; not nec the same as ARG
1403 (substring (this-command-keys) universal-argument-num-events)
1404 (this-command-keys)))
1405 (main-event (aref key 0))
1406 (keylist (listify-key-sequence key))
1407 scroll-command isearch-point)
1408 (cond ((and (= (length key) 1)
1409 (let ((lookup (lookup-key function-key-map key)))
1410 (not (or (null lookup) (integerp lookup)
1411 (keymapp lookup)))))
1412 ;; Handle a function key that translates into something else.
1413 ;; If the key has a global definition too,
1414 ;; exit and unread the key itself, so its global definition runs.
1415 ;; Otherwise, unread the translation,
1416 ;; so that the translated key takes effect within isearch.
1417 (cancel-kbd-macro-events)
1418 (if (lookup-key global-map key)
1419 (progn
1420 (isearch-done)
1421 (apply 'isearch-unread keylist))
1422 (setq keylist
1423 (listify-key-sequence (lookup-key function-key-map key)))
1424 (while keylist
1425 (setq key (car keylist))
1426 ;; If KEY is a printing char, we handle it here
1427 ;; directly to avoid the input method and keyboard
1428 ;; coding system translating it.
1429 (if (and (integerp key)
1430 (>= key ?\ ) (/= key 127) (< key 256))
1431 (progn
1432 (isearch-process-search-char key)
1433 (setq keylist (cdr keylist)))
1434 ;; As the remaining keys in KEYLIST can't be handled
1435 ;; here, we must reread them.
1436 (apply 'isearch-unread keylist)
1437 (setq keylist nil)))))
1439 ;; Handle an undefined shifted control character
1440 ;; by downshifting it if that makes it defined.
1441 ;; (As read-key-sequence would normally do,
1442 ;; if we didn't have a default definition.)
1443 (let ((mods (event-modifiers main-event)))
1444 (and (integerp main-event)
1445 (memq 'shift mods)
1446 (memq 'control mods)
1447 (lookup-key isearch-mode-map
1448 (let ((copy (copy-sequence key)))
1449 (aset copy 0
1450 (- main-event (- ?\C-\S-a ?\C-a)))
1451 copy)
1452 nil)))
1453 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1454 (cancel-kbd-macro-events)
1455 (apply 'isearch-unread keylist))
1456 ((eq search-exit-option 'edit)
1457 (apply 'isearch-unread keylist)
1458 (isearch-edit-string))
1459 ;; Handle a scrolling function.
1460 ((and isearch-allow-scroll
1461 (progn (setq key (isearch-reread-key-sequence-naturally keylist))
1462 (setq keylist (listify-key-sequence key))
1463 (setq main-event (aref key 0))
1464 (setq scroll-command (isearch-lookup-scroll-key key))))
1465 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
1466 ;; complete key sequence, possibly as modified by function-key-map,
1467 ;; not merely the one or two event fragment which invoked
1468 ;; isearch-other-meta-char in the first place.
1469 (setq isearch-point (point))
1470 (setq prefix-arg arg)
1471 (command-execute scroll-command)
1472 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
1473 (if ab-bel
1474 (isearch-back-into-window (eq ab-bel 'above) isearch-point)))
1475 (isearch-update))
1476 (search-exit-option
1477 (let (window)
1478 (isearch-unread-key-sequence keylist)
1479 (setq main-event (car unread-command-events))
1481 ;; If we got a mouse click event, that event contains the
1482 ;; window clicked on. maybe it was read with the buffer
1483 ;; it was clicked on. If so, that buffer, not the current one,
1484 ;; is in isearch mode. So end the search in that buffer.
1486 ;; ??? I have no idea what this if checks for, but it's
1487 ;; obviously wrong for the case that a down-mouse event
1488 ;; on another window invokes this function. The event
1489 ;; will contain the window clicked on and that window's
1490 ;; buffer is certainly not always in Isearch mode.
1492 ;; Leave the code in, but check for current buffer not
1493 ;; being in Isearch mode for now, until someone tells
1494 ;; what it's really supposed to do.
1496 ;; --gerd 2001-08-10.
1498 (if (and (not isearch-mode)
1499 (listp main-event)
1500 (setq window (posn-window (event-start main-event)))
1501 (windowp window)
1502 (or (> (minibuffer-depth) 0)
1503 (not (window-minibuffer-p window))))
1504 (save-excursion
1505 (set-buffer (window-buffer window))
1506 (isearch-done)
1507 (isearch-clean-overlays))
1508 (isearch-done)
1509 (isearch-clean-overlays)
1510 (setq prefix-arg arg))))
1511 (t;; otherwise nil
1512 (isearch-process-search-string key key)))))
1514 (defun isearch-quote-char ()
1515 "Quote special characters for incremental search."
1516 (interactive)
1517 (let ((char (read-quoted-char (isearch-message t))))
1518 ;; Assume character codes 0200 - 0377 stand for characters in some
1519 ;; single-byte character set, and convert them to Emacs
1520 ;; characters.
1521 (and enable-multibyte-characters
1522 (>= char ?\200)
1523 (<= char ?\377)
1524 (setq char (unibyte-char-to-multibyte char)))
1525 (isearch-process-search-char char)))
1527 (defun isearch-return-char ()
1528 "Convert return into newline for incremental search.
1529 Obsolete."
1530 (interactive)
1531 (isearch-process-search-char ?\n))
1533 (defun isearch-printing-char ()
1534 "Add this ordinary printing character to the search string and search."
1535 (interactive)
1536 (let ((char last-command-char))
1537 (if (= char ?\S-\ )
1538 (setq char ?\ ))
1539 (if (and enable-multibyte-characters
1540 (>= char ?\200)
1541 (<= char ?\377))
1542 (if (keyboard-coding-system)
1543 (isearch-process-search-multibyte-characters char)
1544 (isearch-process-search-char (unibyte-char-to-multibyte char)))
1545 (if current-input-method
1546 (isearch-process-search-multibyte-characters char)
1547 (isearch-process-search-char char)))))
1549 (defun isearch-whitespace-chars ()
1550 "Match all whitespace chars, if in regexp mode.
1551 If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
1552 (interactive)
1553 (if isearch-regexp
1554 (if (and search-whitespace-regexp (not isearch-within-brackets)
1555 (not isearch-invalid-regexp))
1556 (isearch-process-search-string search-whitespace-regexp " ")
1557 (isearch-printing-char))
1558 (progn
1559 ;; This way of doing word search doesn't correctly extend current search.
1560 ;; (setq isearch-word t)
1561 ;; (setq isearch-adjusted t)
1562 ;; (goto-char isearch-barrier)
1563 (isearch-printing-char))))
1565 (defun isearch-process-search-char (char)
1566 ;; Append the char to the search string, update the message and re-search.
1567 (isearch-process-search-string
1568 (char-to-string char)
1569 (if (>= char ?\200)
1570 (char-to-string char)
1571 (isearch-text-char-description char))))
1573 (defun isearch-process-search-string (string message)
1574 (setq isearch-string (concat isearch-string string)
1575 isearch-message (concat isearch-message message))
1576 (isearch-search-and-update))
1579 ;; Search Ring
1581 (defun isearch-ring-adjust1 (advance)
1582 ;; Helper for isearch-ring-adjust
1583 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1584 (length (length ring))
1585 (yank-pointer-name (if isearch-regexp
1586 'regexp-search-ring-yank-pointer
1587 'search-ring-yank-pointer))
1588 (yank-pointer (eval yank-pointer-name)))
1589 (if (zerop length)
1591 (set yank-pointer-name
1592 (setq yank-pointer
1593 (mod (+ (or yank-pointer 0)
1594 (if advance -1 1))
1595 length)))
1596 (setq isearch-string (nth yank-pointer ring)
1597 isearch-message (mapconcat 'isearch-text-char-description
1598 isearch-string "")))))
1600 (defun isearch-ring-adjust (advance)
1601 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1602 (isearch-ring-adjust1 advance)
1603 (if search-ring-update
1604 (progn
1605 (isearch-search)
1606 (isearch-update))
1607 (isearch-edit-string)
1609 (isearch-push-state))
1611 (defun isearch-ring-advance ()
1612 "Advance to the next search string in the ring."
1613 ;; This could be more general to handle a prefix arg, but who would use it.
1614 (interactive)
1615 (isearch-ring-adjust 'advance))
1617 (defun isearch-ring-retreat ()
1618 "Retreat to the previous search string in the ring."
1619 (interactive)
1620 (isearch-ring-adjust nil))
1622 (defun isearch-ring-advance-edit (n)
1623 "Insert the next element of the search history into the minibuffer.
1624 With prefix arg N, insert the Nth element."
1625 (interactive "p")
1626 (let* ((yank-pointer-name (if isearch-regexp
1627 'regexp-search-ring-yank-pointer
1628 'search-ring-yank-pointer))
1629 (yank-pointer (eval yank-pointer-name))
1630 (ring (if isearch-regexp regexp-search-ring search-ring))
1631 (length (length ring)))
1632 (if (zerop length)
1634 (set yank-pointer-name
1635 (setq yank-pointer
1636 (mod (- (or yank-pointer 0) n)
1637 length)))
1639 (delete-field)
1640 (insert (nth yank-pointer ring))
1641 (goto-char (point-max)))))
1643 (defun isearch-ring-retreat-edit (n)
1644 "Insert the previous element of the search history into the minibuffer.
1645 With prefix arg N, insert the Nth element."
1646 (interactive "p")
1647 (isearch-ring-advance-edit (- n)))
1649 ;;(defun isearch-ring-adjust-edit (advance)
1650 ;; "Use the next or previous search string in the ring while in minibuffer."
1651 ;; (isearch-ring-adjust1 advance)
1652 ;; (erase-buffer)
1653 ;; (insert isearch-string))
1655 ;;(defun isearch-ring-advance-edit ()
1656 ;; (interactive)
1657 ;; (isearch-ring-adjust-edit 'advance))
1659 ;;(defun isearch-ring-retreat-edit ()
1660 ;; "Retreat to the previous search string in the ring while in the minibuffer."
1661 ;; (interactive)
1662 ;; (isearch-ring-adjust-edit nil))
1665 (defun isearch-complete1 ()
1666 ;; Helper for isearch-complete and isearch-complete-edit
1667 ;; Return t if completion OK, nil if no completion exists.
1668 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1669 (completion-ignore-case case-fold-search)
1670 (completion (try-completion isearch-string ring)))
1671 (cond
1672 ((eq completion t)
1673 ;; isearch-string stays the same
1675 ((or completion ; not nil, must be a string
1676 (= 0 (length isearch-string))) ; shouldn't have to say this
1677 (if (equal completion isearch-string) ;; no extension?
1678 (progn
1679 (if completion-auto-help
1680 (with-output-to-temp-buffer "*Isearch completions*"
1681 (display-completion-list
1682 (all-completions isearch-string ring))))
1684 (and completion
1685 (setq isearch-string completion))))
1687 (message "No completion") ; waits a second if in minibuffer
1688 nil))))
1690 (defun isearch-complete ()
1691 "Complete the search string from the strings on the search ring.
1692 The completed string is then editable in the minibuffer.
1693 If there is no completion possible, say so and continue searching."
1694 (interactive)
1695 (if (isearch-complete1)
1696 (progn (setq isearch-message
1697 (mapconcat 'isearch-text-char-description
1698 isearch-string ""))
1699 (isearch-edit-string))
1700 ;; else
1701 (sit-for 1)
1702 (isearch-update)))
1704 (defun isearch-complete-edit ()
1705 "Same as `isearch-complete' except in the minibuffer."
1706 (interactive)
1707 (setq isearch-string (field-string))
1708 (if (isearch-complete1)
1709 (progn
1710 (delete-field)
1711 (insert isearch-string))))
1714 ;; The search status stack (and isearch window-local variables, not used).
1715 ;; Need a structure for this.
1717 (defun isearch-top-state ()
1718 (let ((cmd (car isearch-cmds)))
1719 (setq isearch-string (car cmd)
1720 isearch-message (car (cdr cmd))
1721 isearch-success (nth 3 cmd)
1722 isearch-forward (nth 4 cmd)
1723 isearch-other-end (nth 5 cmd)
1724 isearch-word (nth 6 cmd)
1725 isearch-invalid-regexp (nth 7 cmd)
1726 isearch-wrapped (nth 8 cmd)
1727 isearch-barrier (nth 9 cmd)
1728 isearch-within-brackets (nth 10 cmd)
1729 isearch-case-fold-search (nth 11 cmd))
1730 (goto-char (car (cdr (cdr cmd))))))
1732 (defun isearch-pop-state ()
1733 (setq isearch-cmds (cdr isearch-cmds))
1734 (isearch-top-state))
1736 (defun isearch-push-state ()
1737 (setq isearch-cmds
1738 (cons (list isearch-string isearch-message (point)
1739 isearch-success isearch-forward isearch-other-end
1740 isearch-word
1741 isearch-invalid-regexp isearch-wrapped isearch-barrier
1742 isearch-within-brackets isearch-case-fold-search)
1743 isearch-cmds)))
1746 ;; Message string
1748 (defun isearch-message (&optional c-q-hack ellipsis)
1749 ;; Generate and print the message string.
1750 (let ((cursor-in-echo-area ellipsis)
1751 (m (concat
1752 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
1753 isearch-message
1754 (isearch-message-suffix c-q-hack ellipsis)
1756 (if c-q-hack
1758 (let ((message-log-max nil))
1759 (message "%s" m)))))
1761 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
1762 ;; If about to search, and previous search regexp was invalid,
1763 ;; check that it still is. If it is valid now,
1764 ;; let the message we display while searching say that it is valid.
1765 (and isearch-invalid-regexp ellipsis
1766 (condition-case ()
1767 (progn (re-search-forward isearch-string (point) t)
1768 (setq isearch-invalid-regexp nil
1769 isearch-within-brackets nil))
1770 (error nil)))
1771 ;; If currently failing, display no ellipsis.
1772 (or isearch-success (setq ellipsis nil))
1773 (let ((m (concat (if isearch-success "" "failing ")
1774 (if (and isearch-wrapped
1775 (if isearch-forward
1776 (> (point) isearch-opoint)
1777 (< (point) isearch-opoint)))
1778 "over")
1779 (if isearch-wrapped "wrapped ")
1780 (if isearch-word "word " "")
1781 (if isearch-regexp "regexp " "")
1782 (if nonincremental "search" "I-search")
1783 (if isearch-forward "" " backward")
1784 (if current-input-method
1785 (concat " [" current-input-method-title "]: ")
1786 ": ")
1788 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
1789 'face 'minibuffer-prompt)))
1791 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
1792 (concat (if c-q-hack "^Q" "")
1793 (if isearch-invalid-regexp
1794 (concat " [" isearch-invalid-regexp "]")
1795 "")))
1798 ;; Searching
1800 (defvar isearch-search-fun-function nil "Override `isearch-function-fun'.")
1802 (defun isearch-search-fun ()
1803 "Return the function to use for the search.
1804 Can be changed via `isearch-search-fun-function' for special needs."
1805 (if isearch-search-fun-function
1806 (funcall isearch-search-fun-function)
1807 (cond
1808 (isearch-word
1809 (if isearch-forward 'word-search-forward 'word-search-backward))
1810 (isearch-regexp
1811 (if isearch-forward 're-search-forward 're-search-backward))
1813 (if isearch-forward 'search-forward 'search-backward)))))
1815 (defun isearch-search ()
1816 ;; Do the search with the current search string.
1817 (isearch-message nil t)
1818 (if (and (eq isearch-case-fold-search t) search-upper-case)
1819 (setq isearch-case-fold-search
1820 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1821 (condition-case lossage
1822 (let ((inhibit-point-motion-hooks search-invisible)
1823 (inhibit-quit nil)
1824 (case-fold-search isearch-case-fold-search)
1825 (retry t))
1826 (if isearch-regexp (setq isearch-invalid-regexp nil))
1827 (setq isearch-within-brackets nil)
1828 (while retry
1829 (setq isearch-success
1830 (funcall
1831 (isearch-search-fun)
1832 isearch-string nil t))
1833 ;; Clear RETRY unless we matched some invisible text
1834 ;; and we aren't supposed to do that.
1835 (if (or (eq search-invisible t)
1836 (not isearch-success)
1837 (bobp) (eobp)
1838 (= (match-beginning 0) (match-end 0))
1839 (not (isearch-range-invisible
1840 (match-beginning 0) (match-end 0))))
1841 (setq retry nil)))
1842 (setq isearch-just-started nil)
1843 (if isearch-success
1844 (setq isearch-other-end
1845 (if isearch-forward (match-beginning 0) (match-end 0)))))
1847 (quit (isearch-unread ?\C-g)
1848 (setq isearch-success nil))
1850 (invalid-regexp
1851 (setq isearch-invalid-regexp (car (cdr lossage)))
1852 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1853 isearch-invalid-regexp))
1854 (if (string-match
1855 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1856 isearch-invalid-regexp)
1857 (setq isearch-invalid-regexp "incomplete input")))
1858 (error
1859 ;; stack overflow in regexp search.
1860 (setq isearch-invalid-regexp (format "%s" lossage))))
1862 (if isearch-success
1864 ;; Ding if failed this time after succeeding last time.
1865 (and (nth 3 (car isearch-cmds))
1866 (ding))
1867 (goto-char (nth 2 (car isearch-cmds)))))
1870 ;; Called when opening an overlay, and we are still in isearch.
1871 (defun isearch-open-overlay-temporary (ov)
1872 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1873 ;; Some modes would want to open the overlays temporary during
1874 ;; isearch in their own way, they should set the
1875 ;; `isearch-open-invisible-temporary' to a function doing this.
1876 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1877 ;; Store the values for the `invisible' and `intangible'
1878 ;; properties, and then set them to nil. This way the text hidden
1879 ;; by this overlay becomes visible.
1881 ;; Do we really need to set the `intangible' property to t? Can we
1882 ;; have the point inside an overlay with an `intangible' property?
1883 ;; In 19.34 this does not exist so I cannot test it.
1884 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1885 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1886 (overlay-put ov 'invisible nil)
1887 (overlay-put ov 'intangible nil)))
1890 ;; This is called at the end of isearch. It will open the overlays
1891 ;; that contain the latest match. Obviously in case of a C-g the
1892 ;; point returns to the original location which surely is not contain
1893 ;; in any of these overlays, se we are safe in this case too.
1894 (defun isearch-open-necessary-overlays (ov)
1895 (let ((inside-overlay (and (> (point) (overlay-start ov))
1896 (< (point) (overlay-end ov))))
1897 ;; If this exists it means that the overlay was opened using
1898 ;; this function, not by us tweaking the overlay properties.
1899 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1900 (when (or inside-overlay (not fct-temp))
1901 ;; restore the values for the `invisible' and `intangible'
1902 ;; properties
1903 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1904 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1905 (overlay-put ov 'isearch-invisible nil)
1906 (overlay-put ov 'isearch-intangible nil))
1907 (if inside-overlay
1908 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1909 (if fct-temp
1910 (funcall fct-temp ov t)))))
1912 ;; This is called when exiting isearch. It closes the temporary
1913 ;; opened overlays, except the ones that contain the latest match.
1914 (defun isearch-clean-overlays ()
1915 (when isearch-opened-overlays
1916 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
1917 (setq isearch-opened-overlays nil)))
1920 (defun isearch-intersects-p (start0 end0 start1 end1)
1921 "Return t if regions START0..END0 and START1..END1 intersect."
1922 (or (and (>= start0 start1) (< start0 end1))
1923 (and (> end0 start1) (<= end0 end1))
1924 (and (>= start1 start0) (< start1 end0))
1925 (and (> end1 start0) (<= end1 end0))))
1928 ;; Verify if the current match is outside of each element of
1929 ;; `isearch-opened-overlays', if so close that overlay.
1931 (defun isearch-close-unnecessary-overlays (begin end)
1932 (let ((overlays isearch-opened-overlays))
1933 (setq isearch-opened-overlays nil)
1934 (dolist (ov overlays)
1935 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
1936 (push ov isearch-opened-overlays)
1937 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1938 (if fct-temp
1939 ;; If this exists it means that the overlay was opened
1940 ;; using this function, not by us tweaking the overlay
1941 ;; properties.
1942 (funcall fct-temp ov t)
1943 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1944 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1945 (overlay-put ov 'isearch-invisible nil)
1946 (overlay-put ov 'isearch-intangible nil)))))))
1949 (defun isearch-range-invisible (beg end)
1950 "Return t if all the text from BEG to END is invisible."
1951 (when (/= beg end)
1952 ;; Check that invisibility runs up to END.
1953 (save-excursion
1954 (goto-char beg)
1955 (let (;; can-be-opened keeps track if we can open some overlays.
1956 (can-be-opened (eq search-invisible 'open))
1957 ;; the list of overlays that could be opened
1958 (crt-overlays nil))
1959 (when (and can-be-opened isearch-hide-immediately)
1960 (isearch-close-unnecessary-overlays beg end))
1961 ;; If the following character is currently invisible,
1962 ;; skip all characters with that same `invisible' property value.
1963 ;; Do that over and over.
1964 (while (and (< (point) end)
1965 (let ((prop
1966 (get-char-property (point) 'invisible)))
1967 (if (eq buffer-invisibility-spec t)
1968 prop
1969 (or (memq prop buffer-invisibility-spec)
1970 (assq prop buffer-invisibility-spec)))))
1971 (if (get-text-property (point) 'invisible)
1972 (progn
1973 (goto-char (next-single-property-change (point) 'invisible
1974 nil end))
1975 ;; if text is hidden by an `invisible' text property
1976 ;; we cannot open it at all.
1977 (setq can-be-opened nil))
1978 (when can-be-opened
1979 (let ((overlays (overlays-at (point)))
1980 ov-list
1982 invis-prop)
1983 (while overlays
1984 (setq o (car overlays)
1985 invis-prop (overlay-get o 'invisible))
1986 (if (if (eq buffer-invisibility-spec t)
1987 invis-prop
1988 (or (memq invis-prop buffer-invisibility-spec)
1989 (assq invis-prop buffer-invisibility-spec)))
1990 (if (overlay-get o 'isearch-open-invisible)
1991 (setq ov-list (cons o ov-list))
1992 ;; We found one overlay that cannot be
1993 ;; opened, that means the whole chunk
1994 ;; cannot be opened.
1995 (setq can-be-opened nil)))
1996 (setq overlays (cdr overlays)))
1997 (if can-be-opened
1998 ;; It makes sense to append to the open
1999 ;; overlays list only if we know that this is
2000 ;; t.
2001 (setq crt-overlays (append ov-list crt-overlays)))))
2002 (goto-char (next-overlay-change (point)))))
2003 ;; See if invisibility reaches up thru END.
2004 (if (>= (point) end)
2005 (if (and can-be-opened (consp crt-overlays))
2006 (progn
2007 (setq isearch-opened-overlays
2008 (append isearch-opened-overlays crt-overlays))
2009 (mapc 'isearch-open-overlay-temporary crt-overlays)
2010 nil)
2011 (setq isearch-hidden t)))))))
2014 ;; Highlighting
2016 (defvar isearch-overlay nil)
2018 (defun isearch-highlight (beg end)
2019 (unless (null search-highlight)
2020 (cond (isearch-overlay
2021 ;; Overlay already exists, just move it.
2022 (move-overlay isearch-overlay beg end (current-buffer)))
2025 ;; Overlay doesn't exist, create it.
2026 (setq isearch-overlay (make-overlay beg end))
2027 (overlay-put isearch-overlay 'face isearch)
2028 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
2029 ))))
2031 (defun isearch-dehighlight (totally)
2032 (when isearch-overlay
2033 (delete-overlay isearch-overlay)))
2036 ;; General utilities
2039 (defun isearch-no-upper-case-p (string regexp-flag)
2040 "Return t if there are no upper case chars in STRING.
2041 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
2042 since they have special meaning in a regexp."
2043 (let (quote-flag (i 0) (len (length string)) found)
2044 (while (and (not found) (< i len))
2045 (let ((char (aref string i)))
2046 (if (and regexp-flag (eq char ?\\))
2047 (setq quote-flag (not quote-flag))
2048 (if (and (not quote-flag) (not (eq char (downcase char))))
2049 (setq found t))
2050 (setq quote-flag nil)))
2051 (setq i (1+ i)))
2052 (not found)))
2054 ;; Portability functions to support various Emacs versions.
2056 (defun isearch-text-char-description (c)
2057 (cond
2058 ((< c ?\ ) (format "^%c" (+ c 64)))
2059 ((= c ?\^?) "^?")
2060 (t (char-to-string c))))
2062 ;; General function to unread characters or events.
2063 ;; Also insert them in a keyboard macro being defined.
2064 (defun isearch-unread (&rest char-or-events)
2065 (mapc 'store-kbd-macro-event char-or-events)
2066 (setq unread-command-events
2067 (append char-or-events unread-command-events)))
2070 ;; isearch-lazy-highlight feature
2071 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
2073 ;; When active, *every* match for the current search string is
2074 ;; highlighted: the current one using the normal isearch match color
2075 ;; and all the others using `isearch-lazy-highlight-face'. The extra
2076 ;; highlighting makes it easier to anticipate where the cursor will
2077 ;; land each time you press C-s or C-r to repeat a pending search.
2078 ;; Highlighting of these additional matches happens in a deferred
2079 ;; fashion using "idle timers," so the cycles needed do not rob
2080 ;; isearch of its usual snappy response.
2082 ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
2083 ;; Specifically:
2084 ;; - `isearch-update' is expected to be called (at least) every time
2085 ;; the search string or window-start changes;
2086 ;; - `isearch-string' is expected to contain the current search
2087 ;; string as entered by the user;
2088 ;; - the type of the current search is expected to be given by
2089 ;; `isearch-word' and `isearch-regexp';
2090 ;; - the direction of the current search is expected to be given by
2091 ;; `isearch-forward';
2092 ;; - the variable `isearch-invalid-regexp' is expected to be true
2093 ;; iff `isearch-string' is an invalid regexp.
2095 (defgroup isearch-lazy-highlight nil
2096 "Lazy highlighting feature for incremental search."
2097 :prefix "isearch-lazy-highlight-"
2098 :version "21.1"
2099 :group 'isearch)
2101 (defcustom isearch-lazy-highlight t
2102 "*Controls the lazy-highlighting during incremental searches.
2103 When non-nil, all text in the buffer matching the current search
2104 string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
2105 and `isearch-lazy-highlight-interval')."
2106 :type 'boolean
2107 :group 'isearch-lazy-highlight)
2109 (defcustom isearch-lazy-highlight-cleanup t
2110 "*Controls whether to remove extra highlighting after a search.
2111 If this is nil, extra highlighting can be \"manually\" removed with
2112 \\[isearch-lazy-highlight-cleanup]."
2113 :type 'boolean
2114 :group 'isearch-lazy-highlight)
2116 (defcustom isearch-lazy-highlight-initial-delay 0.25
2117 "*Seconds to wait before beginning to lazily highlight all matches."
2118 :type 'number
2119 :group 'isearch-lazy-highlight)
2121 (defcustom isearch-lazy-highlight-interval 0 ; 0.0625
2122 "*Seconds between lazily highlighting successive matches."
2123 :type 'number
2124 :group 'isearch-lazy-highlight)
2126 (defcustom isearch-lazy-highlight-max-at-a-time 20
2127 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
2128 Larger values may reduce isearch's responsiveness to user input;
2129 smaller values make matches highlight slowly.
2130 A value of nil means highlight all matches."
2131 :type '(choice (const :tag "All" nil)
2132 (integer :tag "Some"))
2133 :group 'isearch-lazy-highlight)
2135 (defgroup isearch-faces nil
2136 "Lazy highlighting feature for incremental search."
2137 :version "21.1"
2138 :group 'isearch)
2140 (defface isearch
2141 '((((class color) (min-colors 88) (background light))
2142 ;; The background must not be too dark, for that means
2143 ;; the character is hard to see when the cursor is there.
2144 (:background "magenta2" :foreground "lightskyblue1"))
2145 (((class color) (min-colors 88) (background dark))
2146 (:background "palevioletred2" :foreground "brown4"))
2147 (((class color) (min-colors 16))
2148 (:background "magenta4" :foreground "cyan1"))
2149 (((class color) (min-colors 8))
2150 (:background "magenta4" :foreground "cyan1"))
2151 (t (:inverse-video t)))
2152 "Face for highlighting Isearch matches."
2153 :group 'isearch-faces)
2154 (defvar isearch 'isearch)
2156 (defface isearch-lazy-highlight-face
2157 '((((class color) (min-colors 88) (background light))
2158 (:background "paleturquoise"))
2159 (((class color) (min-colors 88) (background dark))
2160 (:background "paleturquoise4"))
2161 (((class color) (min-colors 16))
2162 (:background "turquoise3"))
2163 (((class color) (min-colors 8))
2164 (:background "turquoise3"))
2165 (t (:underline t)))
2166 "Face for lazy highlighting of Isearch matches other than the current one."
2167 :group 'isearch-faces)
2168 (defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
2170 (defvar isearch-lazy-highlight-overlays nil)
2171 (defvar isearch-lazy-highlight-wrapped nil)
2172 (defvar isearch-lazy-highlight-start nil)
2173 (defvar isearch-lazy-highlight-end nil)
2174 (defvar isearch-lazy-highlight-timer nil)
2175 (defvar isearch-lazy-highlight-last-string nil)
2176 (defvar isearch-lazy-highlight-window nil)
2177 (defvar isearch-lazy-highlight-window-start nil)
2178 (defvar isearch-lazy-highlight-window-end nil)
2179 (defvar isearch-lazy-highlight-case-fold-search nil)
2180 (defvar isearch-lazy-highlight-regexp nil)
2182 (defun isearch-lazy-highlight-cleanup (&optional force)
2183 "Stop lazy highlighting and remove extra highlighting from current buffer.
2184 FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
2185 is nil. This function is called when exiting an incremental search if
2186 `isearch-lazy-highlight-cleanup' is non-nil."
2187 (interactive '(t))
2188 (if (or force isearch-lazy-highlight-cleanup)
2189 (while isearch-lazy-highlight-overlays
2190 (delete-overlay (car isearch-lazy-highlight-overlays))
2191 (setq isearch-lazy-highlight-overlays
2192 (cdr isearch-lazy-highlight-overlays))))
2193 (when isearch-lazy-highlight-timer
2194 (cancel-timer isearch-lazy-highlight-timer)
2195 (setq isearch-lazy-highlight-timer nil)))
2197 (defun isearch-lazy-highlight-new-loop ()
2198 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
2199 This happens when `isearch-update' is invoked (which can cause the
2200 search string to change or the window to scroll)."
2201 (when (and isearch-lazy-highlight
2202 (null executing-kbd-macro)
2203 (sit-for 0) ;make sure (window-start) is credible
2204 (or (not (equal isearch-string
2205 isearch-lazy-highlight-last-string))
2206 (not (eq (selected-window)
2207 isearch-lazy-highlight-window))
2208 (not (eq isearch-lazy-highlight-case-fold-search
2209 isearch-case-fold-search))
2210 (not (eq isearch-lazy-highlight-regexp
2211 isearch-regexp))
2212 (not (= (window-start)
2213 isearch-lazy-highlight-window-start))
2214 (not (= (window-end) ; Window may have been split/joined.
2215 isearch-lazy-highlight-window-end))))
2216 ;; something important did indeed change
2217 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
2218 (when (not isearch-invalid-regexp)
2219 (setq isearch-lazy-highlight-window (selected-window)
2220 isearch-lazy-highlight-window-start (window-start)
2221 isearch-lazy-highlight-window-end (window-end)
2222 isearch-lazy-highlight-start (point)
2223 isearch-lazy-highlight-end (point)
2224 isearch-lazy-highlight-last-string isearch-string
2225 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2226 isearch-lazy-highlight-regexp isearch-regexp
2227 isearch-lazy-highlight-wrapped nil)
2228 (setq isearch-lazy-highlight-timer
2229 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
2230 'isearch-lazy-highlight-update)))))
2232 (defun isearch-lazy-highlight-search ()
2233 "Search ahead for the next or previous match, for lazy highlighting.
2234 Attempt to do the search exactly the way the pending isearch would."
2235 (let ((case-fold-search isearch-case-fold-search))
2236 (funcall (isearch-search-fun)
2237 isearch-string
2238 (if isearch-forward
2239 (if isearch-lazy-highlight-wrapped
2240 isearch-lazy-highlight-start
2241 (window-end))
2242 (if isearch-lazy-highlight-wrapped
2243 isearch-lazy-highlight-end
2244 (window-start)))
2245 t)))
2247 (defun isearch-lazy-highlight-update ()
2248 "Update highlighting of other matches for current search."
2249 (let ((max isearch-lazy-highlight-max-at-a-time)
2250 (looping t)
2251 nomore)
2252 (save-excursion
2253 (save-match-data
2254 (goto-char (if isearch-forward
2255 isearch-lazy-highlight-end
2256 isearch-lazy-highlight-start))
2257 (while looping
2258 (let ((found (isearch-lazy-highlight-search)))
2259 (when max
2260 (setq max (1- max))
2261 (if (<= max 0)
2262 (setq looping nil)))
2263 (if found
2264 (let ((mb (match-beginning 0))
2265 (me (match-end 0)))
2266 (if (= mb me) ;zero-length match
2267 (forward-char 1)
2269 ;; non-zero-length match
2270 (let ((ov (make-overlay mb me)))
2271 (overlay-put ov 'face isearch-lazy-highlight-face)
2272 (overlay-put ov 'priority 0) ;lower than main overlay
2273 (overlay-put ov 'window (selected-window))
2274 (push ov isearch-lazy-highlight-overlays)))
2275 (if isearch-forward
2276 (setq isearch-lazy-highlight-end (point))
2277 (setq isearch-lazy-highlight-start (point))))
2279 ;; not found
2280 (if isearch-lazy-highlight-wrapped
2281 (setq looping nil
2282 nomore t)
2283 (setq isearch-lazy-highlight-wrapped t)
2284 (if isearch-forward
2285 (progn
2286 (setq isearch-lazy-highlight-end (window-start))
2287 (goto-char (window-start)))
2288 (setq isearch-lazy-highlight-start (window-end))
2289 (goto-char (window-end)))))))
2290 (unless nomore
2291 (setq isearch-lazy-highlight-timer
2292 (run-at-time isearch-lazy-highlight-interval nil
2293 'isearch-lazy-highlight-update)))))))
2295 (defun isearch-resume (search regexp word forward message case-fold)
2296 "Resume an incremental search.
2297 SEARCH is the string or regexp searched for.
2298 REGEXP non-nil means the resumed search was a regexp search.
2299 WORD non-nil means resume a word search.
2300 FORWARD non-nil means resume a forward search.
2301 MESSAGE is the echo-area message recorded for the search resumed.
2302 CASE-FOLD non-nil means the search was case-insensitive."
2303 (isearch-mode forward regexp nil nil word)
2304 (setq isearch-string search
2305 isearch-message message
2306 isearch-case-fold-search case-fold)
2307 (isearch-search))
2309 ;;; arch-tag: 74850515-f7d8-43a6-8a2c-ca90a4c1e675
2310 ;;; isearch.el ends here