Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / isearch.el
1 ;;; isearch.el --- incremental search minor mode
2
3 ;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4 ;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7 ;; Maintainer: FSF
8 ;; Keywords: matching
9
10 ;; This file is part of GNU Emacs.
11
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
16
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; Instructions
28
29 ;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
30 ;; isearch-mode behaves modally and does not return until the search
31 ;; is completed. It uses a recursive-edit to behave this way.
32
33 ;; The key bindings active within isearch-mode are defined below in
34 ;; `isearch-mode-map' which is given bindings close to the default
35 ;; characters of the original isearch.el. With `isearch-mode',
36 ;; however, you can bind multi-character keys and it should be easier
37 ;; to add new commands. One bug though: keys with meta-prefix cannot
38 ;; be longer than two chars. Also see minibuffer-local-isearch-map
39 ;; for bindings active during `isearch-edit-string'.
40
41 ;; isearch-mode should work even if you switch windows with the mouse,
42 ;; in which case isearch-mode is terminated automatically before the
43 ;; switch.
44
45 ;; The search ring and completion commands automatically put you in
46 ;; the minibuffer to edit the string. This gives you a chance to
47 ;; modify the search string before executing the search. There are
48 ;; three commands to terminate the editing: C-s and C-r exit the
49 ;; minibuffer and search forward and reverse respectively, while C-m
50 ;; exits and does a nonincremental search.
51
52 ;; Exiting immediately from isearch uses isearch-edit-string instead
53 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
54 ;; The name of this option should probably be changed if we decide to
55 ;; keep the behavior. No point in forcing nonincremental search until
56 ;; the last possible moment.
57
58 ;;; Code:
59
60 \f
61 ;; Some additional options and constants.
62
63 (defgroup isearch nil
64 "Incremental search minor mode."
65 :link '(emacs-commentary-link "isearch")
66 :link '(custom-manual "(emacs)Incremental Search")
67 :prefix "isearch-"
68 :prefix "search-"
69 :group 'matching)
70
71
72 (defcustom search-exit-option t
73 "*Non-nil means random control characters terminate incremental search."
74 :type 'boolean
75 :group 'isearch)
76
77 (defcustom search-slow-window-lines 1
78 "*Number of lines in slow search display windows.
79 These are the short windows used during incremental search on slow terminals.
80 Negative means put the slow search window at the top (normally it's at bottom)
81 and the value is minus the number of lines."
82 :type 'integer
83 :group 'isearch)
84
85 (defcustom search-slow-speed 1200
86 "*Highest terminal speed at which to use \"slow\" style incremental search.
87 This is the style where a one-line window is created to show the line
88 that the search has reached."
89 :type 'integer
90 :group 'isearch)
91
92 (defcustom search-upper-case 'not-yanks
93 "*If non-nil, upper case chars disable case fold searching.
94 That is, upper and lower case chars must match exactly.
95 This applies no matter where the chars come from, but does not
96 apply to chars in regexps that are prefixed with `\\'.
97 If this value is `not-yanks', text yanked into the search string
98 in Isearch mode is always downcased."
99 :type '(choice (const :tag "off" nil)
100 (const not-yanks)
101 (other :tag "on" t))
102 :group 'isearch)
103
104 (defcustom search-nonincremental-instead t
105 "*If non-nil, do a nonincremental search instead if exiting immediately.
106 Actually, `isearch-edit-string' is called to let you enter the search
107 string, and RET terminates editing and does a nonincremental search."
108 :type 'boolean
109 :group 'isearch)
110
111 (defcustom search-whitespace-regexp "\\s-+"
112 "*If non-nil, regular expression to match a sequence of whitespace chars.
113 This applies to regular expression incremental search.
114 When you put a space or spaces in the incremental regexp, it stands for
115 this, unless it is inside of a regexp construct such as [...] or *, + or ?.
116 You might want to use something like \"[ \\t\\r\\n]+\" instead.
117 In the Customization buffer, that is `[' followed by a space,
118 a tab, a carriage return (control-M), a newline, and `]+'.
119
120 When this is nil, each space you type matches literally, against one space."
121 :type '(choice (const :tag "Find Spaces Literally" nil)
122 regexp)
123 :group 'isearch)
124
125 (defcustom search-invisible 'open
126 "If t incremental search can match hidden text.
127 A nil value means don't match invisible text.
128 When the value is `open', if the text matched is made invisible by
129 an overlay having an `invisible' property and that overlay has a property
130 `isearch-open-invisible', then incremental search will show the contents.
131 \(This applies when using `outline.el' and `hideshow.el'.)
132 See also `reveal-mode' if you want overlays to automatically be opened
133 whenever point is in one of them."
134 :type '(choice (const :tag "Match hidden text" t)
135 (const :tag "Open overlays" open)
136 (const :tag "Don't match hidden text" nil))
137 :group 'isearch)
138
139 (defcustom isearch-hide-immediately t
140 "If non-nil, re-hide an invisible match right away.
141 This variable makes a difference when `search-invisible' is set to `open'.
142 It means that after search makes some invisible text visible
143 to show the match, it makes the text invisible again when the match moves.
144 Ordinarily the text becomes invisible again at the end of the search."
145 :type 'boolean
146 :group 'isearch)
147
148 (defcustom isearch-resume-in-command-history nil
149 "*If non-nil, `isearch-resume' commands are added to the command history.
150 This allows you to resume earlier isearch sessions through the
151 command history."
152 :type 'boolean
153 :group 'isearch)
154
155 (defvar isearch-mode-hook nil
156 "Function(s) to call after starting up an incremental search.")
157
158 (defvar isearch-mode-end-hook nil
159 "Function(s) to call after terminating an incremental search.
160 When these functions are called, `isearch-mode-end-hook-quit'
161 is non-nil if the user quits the search.")
162
163 (defvar isearch-mode-end-hook-quit nil
164 "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
165
166 (defvar isearch-message-function nil
167 "Function to call to display the search prompt.
168 If nil, use `isearch-message'.")
169
170 (defvar isearch-wrap-function nil
171 "Function to call to wrap the search when search is failed.
172 If nil, move point to the beginning of the buffer for a forward search,
173 or to the end of the buffer for a backward search.")
174
175 (defvar isearch-push-state-function nil
176 "Function to save a function restoring the mode-specific isearch state
177 to the search status stack.")
178
179 (defvar isearch-success-function 'isearch-success-function-default
180 "Function to report whether the new search match is considered successful.
181 The function has two arguments: the positions of start and end of text
182 matched by the search. If this function returns nil, continue
183 searching without stopping at this match.")
184
185 ;; Search ring.
186
187 (defvar search-ring nil
188 "List of search string sequences.")
189 (defvar regexp-search-ring nil
190 "List of regular expression search string sequences.")
191
192 (defcustom search-ring-max 16
193 "*Maximum length of search ring before oldest elements are thrown away."
194 :type 'integer
195 :group 'isearch)
196 (defcustom regexp-search-ring-max 16
197 "*Maximum length of regexp search ring before oldest elements are thrown away."
198 :type 'integer
199 :group 'isearch)
200
201 (defvar search-ring-yank-pointer nil
202 "Index in `search-ring' of last string reused.
203 It is nil if none yet.")
204 (defvar regexp-search-ring-yank-pointer nil
205 "Index in `regexp-search-ring' of last string reused.
206 It is nil if none yet.")
207
208 (defcustom search-ring-update nil
209 "*Non-nil if advancing or retreating in the search ring should cause search.
210 Default value, nil, means edit the string instead."
211 :type 'boolean
212 :group 'isearch)
213
214 ;;; isearch highlight customization.
215
216 (defcustom search-highlight t
217 "*Non-nil means incremental search highlights the current match."
218 :type 'boolean
219 :group 'isearch)
220
221 (defface isearch
222 '((((class color) (min-colors 88) (background light))
223 ;; The background must not be too dark, for that means
224 ;; the character is hard to see when the cursor is there.
225 (:background "magenta3" :foreground "lightskyblue1"))
226 (((class color) (min-colors 88) (background dark))
227 (:background "palevioletred2" :foreground "brown4"))
228 (((class color) (min-colors 16))
229 (:background "magenta4" :foreground "cyan1"))
230 (((class color) (min-colors 8))
231 (:background "magenta4" :foreground "cyan1"))
232 (t (:inverse-video t)))
233 "Face for highlighting Isearch matches."
234 :group 'isearch
235 :group 'basic-faces)
236 (defvar isearch 'isearch)
237
238 (defface isearch-fail
239 '((((class color) (min-colors 88) (background light))
240 (:background "RosyBrown1"))
241 (((class color) (min-colors 88) (background dark))
242 (:background "red4"))
243 (((class color) (min-colors 16))
244 (:background "red"))
245 (((class color) (min-colors 8))
246 (:background "red"))
247 (((class color grayscale))
248 :foreground "grey")
249 (t (:inverse-video t)))
250 "Face for highlighting failed part in Isearch echo-area message."
251 :version "23.1"
252 :group 'isearch)
253
254 (defcustom isearch-lazy-highlight t
255 "*Controls the lazy-highlighting during incremental search.
256 When non-nil, all text in the buffer matching the current search
257 string is highlighted lazily (see `lazy-highlight-initial-delay'
258 and `lazy-highlight-interval')."
259 :type 'boolean
260 :group 'lazy-highlight
261 :group 'isearch)
262
263 ;;; Lazy highlight customization.
264
265 (defgroup lazy-highlight nil
266 "Lazy highlighting feature for matching strings."
267 :prefix "lazy-highlight-"
268 :version "21.1"
269 :group 'isearch
270 :group 'matching)
271
272 (defcustom lazy-highlight-cleanup t
273 "*Controls whether to remove extra highlighting after a search.
274 If this is nil, extra highlighting can be \"manually\" removed with
275 \\[lazy-highlight-cleanup]."
276 :type 'boolean
277 :group 'lazy-highlight)
278 (define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
279 'lazy-highlight-cleanup
280 "22.1")
281
282 (defcustom lazy-highlight-initial-delay 0.25
283 "*Seconds to wait before beginning to lazily highlight all matches."
284 :type 'number
285 :group 'lazy-highlight)
286 (define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
287 'lazy-highlight-initial-delay
288 "22.1")
289
290 (defcustom lazy-highlight-interval 0 ; 0.0625
291 "*Seconds between lazily highlighting successive matches."
292 :type 'number
293 :group 'lazy-highlight)
294 (define-obsolete-variable-alias 'isearch-lazy-highlight-interval
295 'lazy-highlight-interval
296 "22.1")
297
298 (defcustom lazy-highlight-max-at-a-time 20
299 "*Maximum matches to highlight at a time (for `lazy-highlight').
300 Larger values may reduce isearch's responsiveness to user input;
301 smaller values make matches highlight slowly.
302 A value of nil means highlight all matches."
303 :type '(choice (const :tag "All" nil)
304 (integer :tag "Some"))
305 :group 'lazy-highlight)
306 (define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
307 'lazy-highlight-max-at-a-time
308 "22.1")
309
310 (defface lazy-highlight
311 '((((class color) (min-colors 88) (background light))
312 (:background "paleturquoise"))
313 (((class color) (min-colors 88) (background dark))
314 (:background "paleturquoise4"))
315 (((class color) (min-colors 16))
316 (:background "turquoise3"))
317 (((class color) (min-colors 8))
318 (:background "turquoise3"))
319 (t (:underline t)))
320 "Face for lazy highlighting of matches other than the current one."
321 :group 'lazy-highlight
322 :group 'basic-faces)
323 (put 'isearch-lazy-highlight-face 'face-alias 'lazy-highlight)
324 (defvar lazy-highlight-face 'lazy-highlight)
325 (define-obsolete-variable-alias 'isearch-lazy-highlight-face
326 'lazy-highlight-face
327 "22.1")
328 \f
329 ;; Define isearch help map.
330
331 (defvar isearch-help-map
332 (let ((i 0)
333 (map (make-sparse-keymap)))
334 (define-key map [t] 'isearch-other-control-char)
335 (define-key map (char-to-string help-char) 'isearch-help-for-help)
336 (define-key map [help] 'isearch-help-for-help)
337 (define-key map [f1] 'isearch-help-for-help)
338 (define-key map "?" 'isearch-help-for-help)
339 (define-key map "b" 'isearch-describe-bindings)
340 (define-key map "k" 'isearch-describe-key)
341 (define-key map "m" 'isearch-describe-mode)
342 (define-key map "q" 'help-quit)
343 map)
344 "Keymap for characters following the Help key for isearch mode.")
345
346 (eval-when-compile (require 'help-macro))
347
348 (make-help-screen isearch-help-for-help-internal
349 "Type a help option: [bkm] or ?"
350 "You have typed %THIS-KEY%, the help character. Type a Help option:
351 \(Type \\<help-map>\\[help-quit] to exit the Help command.)
352
353 b Display all isearch key bindings.
354 k KEYS Display full documentation of isearch key sequence.
355 m Display documentation of isearch mode.
356
357 You can't type here other help keys available in the global help map,
358 but outise of this help window when you type them in isearch mode,
359 they exit isearch mode before displaying global help."
360 isearch-help-map)
361
362 (defun isearch-help-for-help ()
363 "Display isearch help menu."
364 (interactive)
365 (let (same-window-buffer-names same-window-regexps)
366 (isearch-help-for-help-internal))
367 (isearch-update))
368
369 (defun isearch-describe-bindings ()
370 "Show a list of all keys defined in isearch mode, and their definitions.
371 This is like `describe-bindings', but displays only isearch keys."
372 (interactive)
373 (let (same-window-buffer-names same-window-regexps)
374 (with-help-window "*Help*"
375 (with-current-buffer standard-output
376 (princ "Isearch Mode Bindings:\n")
377 (princ (substitute-command-keys "\\{isearch-mode-map}"))))))
378
379 (defun isearch-describe-key ()
380 "Display documentation of the function invoked by isearch key."
381 (interactive)
382 (let (same-window-buffer-names same-window-regexps)
383 (call-interactively 'describe-key))
384 (isearch-update))
385
386 (defun isearch-describe-mode ()
387 "Display documentation of isearch mode."
388 (interactive)
389 (let (same-window-buffer-names same-window-regexps)
390 (describe-function 'isearch-forward))
391 (isearch-update))
392
393 (defalias 'isearch-mode-help 'isearch-describe-mode)
394
395 \f
396 ;; Define isearch-mode keymap.
397
398 (defvar isearch-mode-map
399 (let* ((i 0)
400 (map (make-keymap)))
401 (or (char-table-p (nth 1 map))
402 (error "The initialization of isearch-mode-map must be updated"))
403 ;; Make all multibyte characters search for themselves.
404 (set-char-table-range (nth 1 map) (cons #x100 (max-char))
405 'isearch-printing-char)
406 ;; Make function keys, etc, which aren't bound to a scrolling-function
407 ;; exit the search.
408 (define-key map [t] 'isearch-other-control-char)
409 ;; Control chars, by default, end isearch mode transparently.
410 ;; We need these explicit definitions because, in a dense keymap,
411 ;; the binding for t does not affect characters.
412 ;; We use a dense keymap to save space.
413 (while (< i ?\s)
414 (define-key map (make-string 1 i) 'isearch-other-control-char)
415 (setq i (1+ i)))
416
417 ;; Single-byte printing chars extend the search string by default.
418 (setq i ?\s)
419 (while (< i 256)
420 (define-key map (vector i) 'isearch-printing-char)
421 (setq i (1+ i)))
422
423 ;; To handle local bindings with meta char prefix keys, define
424 ;; another full keymap. This must be done for any other prefix
425 ;; keys as well, one full keymap per char of the prefix key. It
426 ;; would be simpler to disable the global keymap, and/or have a
427 ;; default local key binding for any key not otherwise bound.
428 (let ((meta-map (make-sparse-keymap)))
429 (define-key map (char-to-string meta-prefix-char) meta-map)
430 (define-key map [escape] meta-map))
431 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
432
433 ;; Several non-printing chars change the searching behavior.
434 (define-key map "\C-s" 'isearch-repeat-forward)
435 (define-key map "\C-r" 'isearch-repeat-backward)
436 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
437 ;; combinations can be used to repeat regexp isearches that can
438 ;; be used to start these searches.
439 (define-key map "\M-\C-s" 'isearch-repeat-forward)
440 (define-key map "\M-\C-r" 'isearch-repeat-backward)
441 (define-key map "\177" 'isearch-delete-char)
442 (define-key map "\C-g" 'isearch-abort)
443
444 ;; This assumes \e is the meta-prefix-char.
445 (or (= ?\e meta-prefix-char)
446 (error "Inconsistency in isearch.el"))
447 (define-key map "\e\e\e" 'isearch-cancel)
448 (define-key map [escape escape escape] 'isearch-cancel)
449
450 (define-key map "\C-q" 'isearch-quote-char)
451
452 (define-key map "\r" 'isearch-exit)
453 (define-key map "\C-j" 'isearch-printing-char)
454 (define-key map "\t" 'isearch-printing-char)
455 (define-key map [?\S-\ ] 'isearch-printing-char)
456
457 (define-key map "\C-w" 'isearch-yank-word-or-char)
458 (define-key map "\M-\C-w" 'isearch-del-char)
459 (define-key map "\M-\C-y" 'isearch-yank-char)
460 (define-key map "\C-y" 'isearch-yank-line)
461
462 (define-key map "\C-h" isearch-help-map)
463
464 (define-key map "\M-n" 'isearch-ring-advance)
465 (define-key map "\M-p" 'isearch-ring-retreat)
466 (define-key map "\M-y" 'isearch-yank-kill)
467
468 (define-key map "\M-\t" 'isearch-complete)
469
470 ;; Pass frame events transparently so they won't exit the search.
471 ;; In particular, if we have more than one display open, then a
472 ;; switch-frame might be generated by someone typing at another keyboard.
473 (define-key map [switch-frame] nil)
474 (define-key map [delete-frame] nil)
475 (define-key map [iconify-frame] nil)
476 (define-key map [make-frame-visible] nil)
477 (define-key map [mouse-movement] nil)
478 (define-key map [language-change] nil)
479
480 ;; For searching multilingual text.
481 (define-key map "\C-\\" 'isearch-toggle-input-method)
482 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
483
484 ;; People expect to be able to paste with the mouse.
485 (define-key map [mouse-2] #'isearch-mouse-2)
486 (define-key map [down-mouse-2] nil)
487
488 ;; Some bindings you may want to put in your isearch-mode-hook.
489 ;; Suggest some alternates...
490 (define-key map "\M-c" 'isearch-toggle-case-fold)
491 (define-key map "\M-r" 'isearch-toggle-regexp)
492 (define-key map "\M-e" 'isearch-edit-string)
493
494 (define-key map [?\M-%] 'isearch-query-replace)
495 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
496 (define-key map "\M-so" 'isearch-occur)
497
498 map)
499 "Keymap for `isearch-mode'.")
500
501 (defvar minibuffer-local-isearch-map
502 (let ((map (make-sparse-keymap)))
503 (set-keymap-parent map minibuffer-local-map)
504 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
505 (define-key map "\M-\t" 'isearch-complete-edit)
506 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
507 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
508 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
509 (define-key map [right] 'isearch-yank-char-in-minibuffer)
510 map)
511 "Keymap for editing isearch strings in the minibuffer.")
512
513 ;; Internal variables declared globally for byte-compiler.
514 ;; These are all set with setq while isearching
515 ;; and bound locally while editing the search string.
516
517 (defvar isearch-forward nil) ; Searching in the forward direction.
518 (defvar isearch-regexp nil) ; Searching for a regexp.
519 (defvar isearch-word nil) ; Searching for words.
520 (defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
521
522 (defvar isearch-cmds nil
523 "Stack of search status sets.
524 Each set is a vector of the form:
525 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
526 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
527
528 (defvar isearch-string "") ; The current search string.
529 (defvar isearch-message "") ; text-char-description version of isearch-string
530
531 (defvar isearch-success t) ; Searching is currently successful.
532 (defvar isearch-error nil) ; Error message for failed search.
533 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
534 (defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
535 (defvar isearch-barrier 0)
536 (defvar isearch-just-started nil)
537 (defvar isearch-start-hscroll 0) ; hscroll when starting the search.
538
539 ; case-fold-search while searching.
540 ; either nil, t, or 'yes. 'yes means the same as t except that mixed
541 ; case in the search string is ignored.
542 (defvar isearch-case-fold-search nil)
543
544 (defvar isearch-last-case-fold-search nil)
545
546 ;; Used to save default value while isearch is active
547 (defvar isearch-original-minibuffer-message-timeout nil)
548
549 (defvar isearch-adjusted nil)
550 (defvar isearch-slow-terminal-mode nil)
551 ;; If t, using a small window.
552 (defvar isearch-small-window nil)
553 (defvar isearch-opoint 0)
554 ;; The window configuration active at the beginning of the search.
555 (defvar isearch-window-configuration nil)
556
557 ;; Flag to indicate a yank occurred, so don't move the cursor.
558 (defvar isearch-yank-flag nil)
559
560 ;; A function to be called after each input character is processed.
561 ;; (It is not called after characters that exit the search.)
562 ;; It is only set from an optional argument to `isearch-mode'.
563 (defvar isearch-op-fun nil)
564
565 ;; Is isearch-mode in a recursive edit for modal searching.
566 (defvar isearch-recursive-edit nil)
567
568 ;; Should isearch be terminated after doing one search?
569 (defvar isearch-nonincremental nil)
570
571 ;; New value of isearch-forward after isearch-edit-string.
572 (defvar isearch-new-forward nil)
573
574 ;; Accumulate here the overlays opened during searching.
575 (defvar isearch-opened-overlays nil)
576
577 ;; The value of input-method-function when isearch is invoked.
578 (defvar isearch-input-method-function nil)
579
580 ;; A flag to tell if input-method-function is locally bound when
581 ;; isearch is invoked.
582 (defvar isearch-input-method-local-p nil)
583
584 ;; Minor-mode-alist changes - kind of redundant with the
585 ;; echo area, but if isearching in multiple windows, it can be useful.
586
587 (or (assq 'isearch-mode minor-mode-alist)
588 (nconc minor-mode-alist
589 (list '(isearch-mode isearch-mode))))
590
591 (defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
592 (make-variable-buffer-local 'isearch-mode)
593
594 (define-key global-map "\C-s" 'isearch-forward)
595 (define-key esc-map "\C-s" 'isearch-forward-regexp)
596 (define-key global-map "\C-r" 'isearch-backward)
597 (define-key esc-map "\C-r" 'isearch-backward-regexp)
598
599 ;; Entry points to isearch-mode.
600
601 (defun isearch-forward (&optional regexp-p no-recursive-edit)
602 "\
603 Do incremental search forward.
604 With a prefix argument, do an incremental regular expression search instead.
605 \\<isearch-mode-map>
606 As you type characters, they add to the search string and are found.
607 The following non-printing keys are bound in `isearch-mode-map'.
608
609 Type \\[isearch-delete-char] to cancel last input item from end of search string.
610 Type \\[isearch-exit] to exit, leaving point at location found.
611 Type LFD (C-j) to match end of line.
612 Type \\[isearch-repeat-forward] to search again forward,\
613 \\[isearch-repeat-backward] to search again backward.
614 Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
615 onto the end of the search string, and search for it.
616 Type \\[isearch-del-char] to delete character from end of search string.
617 Type \\[isearch-yank-char] to yank char from buffer onto end of search\
618 string and search for it.
619 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
620 and search for it.
621 Type \\[isearch-yank-kill] to yank the last string of killed text.
622 Type \\[isearch-quote-char] to quote control character to search for it.
623 \\[isearch-abort] while searching or when search has failed cancels input\
624 back to what has
625 been found successfully.
626 \\[isearch-abort] when search is successful aborts and moves point to\
627 starting point.
628
629 If you try to exit with the search string still empty, it invokes
630 nonincremental search.
631
632 Type \\[isearch-query-replace] to start `query-replace' with string to\
633 replace from last search string.
634 Type \\[isearch-query-replace-regexp] to start `query-replace-regexp'\
635 with string to replace from last search string.
636
637 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
638 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
639 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
640
641 Also supported is a search ring of the previous 16 search strings.
642 Type \\[isearch-ring-advance] to search for the next item in the search ring.
643 Type \\[isearch-ring-retreat] to search for the previous item in the search\
644 ring.
645 Type \\[isearch-complete] to complete the search string using the search ring.
646
647 Type \\[isearch-describe-bindings] to display all isearch key bindings.
648 Type \\[isearch-describe-key] to display documentation of isearch key.
649 Type \\[isearch-describe-mode] to display documentation of isearch mode.
650
651 If an input method is turned on in the current buffer, that input
652 method is also active while you are typing characters to search. To
653 toggle the input method, type \\[isearch-toggle-input-method]. It
654 also toggles the input method in the current buffer.
655
656 To use a different input method for searching, type
657 \\[isearch-toggle-specified-input-method], and specify an input method
658 you want to use.
659
660 The above keys, bound in `isearch-mode-map', are often controlled by
661 options; do \\[apropos] on search-.* to find them.
662 Other control and meta characters terminate the search
663 and are then executed normally (depending on `search-exit-option').
664 Likewise for function keys and mouse button events.
665
666 If this function is called non-interactively, it does not return to
667 the calling function until the search is done."
668
669 (interactive "P\np")
670 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
671
672 (defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
673 "\
674 Do incremental search forward for regular expression.
675 With a prefix argument, do a regular string search instead.
676 Like ordinary incremental search except that your input
677 is treated as a regexp. See \\[isearch-forward] for more info.
678
679 In regexp incremental searches, a space or spaces normally matches
680 any whitespace (the variable `search-whitespace-regexp' controls
681 precisely what that means). If you want to search for a literal space
682 and nothing else, enter C-q SPC."
683 (interactive "P\np")
684 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
685
686 (defun isearch-backward (&optional regexp-p no-recursive-edit)
687 "\
688 Do incremental search backward.
689 With a prefix argument, do a regular expression search instead.
690 See \\[isearch-forward] for more information."
691 (interactive "P\np")
692 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
693
694 (defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
695 "\
696 Do incremental search backward for regular expression.
697 With a prefix argument, do a regular string search instead.
698 Like ordinary incremental search except that your input
699 is treated as a regexp. See \\[isearch-forward] for more info."
700 (interactive "P\np")
701 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
702
703 \f
704 ;; isearch-mode only sets up incremental search for the minor mode.
705 ;; All the work is done by the isearch-mode commands.
706
707 ;; Not used yet:
708 ;;(defvar isearch-commands '(isearch-forward isearch-backward
709 ;; isearch-forward-regexp isearch-backward-regexp)
710 ;; "List of commands for which isearch-mode does not recursive-edit.")
711
712
713 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
714 "Start isearch minor mode.
715 It is called by the function `isearch-forward' and other related functions."
716
717 ;; Initialize global vars.
718 (setq isearch-forward forward
719 isearch-regexp regexp
720 isearch-word word-p
721 isearch-op-fun op-fun
722 isearch-last-case-fold-search isearch-case-fold-search
723 isearch-case-fold-search case-fold-search
724 isearch-string ""
725 isearch-message ""
726 isearch-cmds nil
727 isearch-success t
728 isearch-wrapped nil
729 isearch-barrier (point)
730 isearch-adjusted nil
731 isearch-yank-flag nil
732 isearch-error nil
733 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
734 (> (window-height)
735 (* 4
736 (abs search-slow-window-lines))))
737 isearch-other-end nil
738 isearch-small-window nil
739 isearch-just-started t
740 isearch-start-hscroll (window-hscroll)
741
742 isearch-opoint (point)
743 search-ring-yank-pointer nil
744 isearch-opened-overlays nil
745 isearch-input-method-function input-method-function
746 isearch-input-method-local-p (local-variable-p 'input-method-function)
747 regexp-search-ring-yank-pointer nil
748
749 ;; Save the original value of `minibuffer-message-timeout', and
750 ;; set it to nil so that isearch's messages don't get timed out.
751 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
752 minibuffer-message-timeout nil)
753
754 ;; We must bypass input method while reading key. When a user type
755 ;; printable character, appropriate input method is turned on in
756 ;; minibuffer to read multibyte characters.
757 (or isearch-input-method-local-p
758 (make-local-variable 'input-method-function))
759 (setq input-method-function nil)
760
761 (looking-at "")
762 (setq isearch-window-configuration
763 (if isearch-slow-terminal-mode (current-window-configuration) nil))
764
765 ;; Maybe make minibuffer frame visible and/or raise it.
766 (let ((frame (window-frame (minibuffer-window))))
767 (unless (memq (frame-live-p frame) '(nil t))
768 (unless (frame-visible-p frame)
769 (make-frame-visible frame))
770 (if minibuffer-auto-raise
771 (raise-frame frame))))
772
773 (setq isearch-mode " Isearch") ;; forward? regexp?
774 (force-mode-line-update)
775
776 (isearch-push-state)
777
778 (setq overriding-terminal-local-map isearch-mode-map)
779 (isearch-update)
780 (run-hooks 'isearch-mode-hook)
781
782 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
783 (add-hook 'kbd-macro-termination-hook 'isearch-done)
784
785 ;; isearch-mode can be made modal (in the sense of not returning to
786 ;; the calling function until searching is completed) by entering
787 ;; a recursive-edit and exiting it when done isearching.
788 (if recursive-edit
789 (let ((isearch-recursive-edit t))
790 (recursive-edit)))
791 isearch-success)
792
793
794 ;; Some high level utilities. Others below.
795
796 (defun isearch-update ()
797 ;; Called after each command to update the display.
798 (if (and (null unread-command-events)
799 (null executing-kbd-macro))
800 (progn
801 (if (not (input-pending-p))
802 (if isearch-message-function
803 (funcall isearch-message-function)
804 (isearch-message)))
805 (if (and isearch-slow-terminal-mode
806 (not (or isearch-small-window
807 (pos-visible-in-window-p))))
808 (let ((found-point (point)))
809 (setq isearch-small-window t)
810 (move-to-window-line 0)
811 (let ((window-min-height 1))
812 (split-window nil (if (< search-slow-window-lines 0)
813 (1+ (- search-slow-window-lines))
814 (- (window-height)
815 (1+ search-slow-window-lines)))))
816 (if (< search-slow-window-lines 0)
817 (progn (vertical-motion (- 1 search-slow-window-lines))
818 (set-window-start (next-window) (point))
819 (set-window-hscroll (next-window)
820 (window-hscroll))
821 (set-window-hscroll (selected-window) 0))
822 (other-window 1))
823 (goto-char found-point))
824 ;; Keep same hscrolling as at the start of the search when possible
825 (let ((current-scroll (window-hscroll)))
826 (set-window-hscroll (selected-window) isearch-start-hscroll)
827 (unless (pos-visible-in-window-p)
828 (set-window-hscroll (selected-window) current-scroll))))
829 (if isearch-other-end
830 (if (< isearch-other-end (point)) ; isearch-forward?
831 (isearch-highlight isearch-other-end (point))
832 (isearch-highlight (point) isearch-other-end))
833 (isearch-dehighlight))
834 ))
835 (setq ;; quit-flag nil not for isearch-mode
836 isearch-adjusted nil
837 isearch-yank-flag nil)
838 (when isearch-lazy-highlight
839 (isearch-lazy-highlight-new-loop))
840 ;; We must prevent the point moving to the end of composition when a
841 ;; part of the composition has just been searched.
842 (setq disable-point-adjustment t))
843
844 (defun isearch-done (&optional nopush edit)
845 "Exit Isearch mode.
846 For successful search, pass no args.
847 For a failing search, NOPUSH is t.
848 For going to the minibuffer to edit the search string,
849 NOPUSH is t and EDIT is t."
850
851 (if isearch-resume-in-command-history
852 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
853 ,isearch-word ,isearch-forward
854 ,isearch-message
855 ',isearch-case-fold-search)))
856 (unless (equal (car command-history) command)
857 (setq command-history (cons command command-history)))))
858
859 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
860 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
861 (setq isearch-lazy-highlight-start nil)
862
863 ;; Called by all commands that terminate isearch-mode.
864 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
865 (setq overriding-terminal-local-map nil)
866 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
867 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
868 (isearch-dehighlight)
869 (lazy-highlight-cleanup lazy-highlight-cleanup)
870 (let ((found-start (window-start (selected-window)))
871 (found-point (point)))
872 (when isearch-window-configuration
873 (set-window-configuration isearch-window-configuration)
874 (if isearch-small-window
875 (goto-char found-point)
876 ;; set-window-configuration clobbers window-start; restore it.
877 ;; This has an annoying side effect of clearing the last_modiff
878 ;; field of the window, which can cause unwanted scrolling,
879 ;; so don't do it unless truly necessary.
880 (set-window-start (selected-window) found-start t))))
881
882 (setq isearch-mode nil)
883 (if isearch-input-method-local-p
884 (setq input-method-function isearch-input-method-function)
885 (kill-local-variable 'input-method-function))
886
887 (force-mode-line-update)
888
889 ;; If we ended in the middle of some intangible text,
890 ;; move to the further end of that intangible text.
891 (let ((after (if (eobp) nil
892 (get-text-property (point) 'intangible)))
893 (before (if (bobp) nil
894 (get-text-property (1- (point)) 'intangible))))
895 (when (and before after (eq before after))
896 (if isearch-forward
897 (goto-char (next-single-property-change (point) 'intangible))
898 (goto-char (previous-single-property-change (point) 'intangible)))))
899
900 (if (and (> (length isearch-string) 0) (not nopush))
901 ;; Update the ring data.
902 (isearch-update-ring isearch-string isearch-regexp))
903
904 (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
905 (run-hooks 'isearch-mode-end-hook))
906
907 ;; If there was movement, mark the starting position.
908 ;; Maybe should test difference between and set mark only if > threshold.
909 (if (/= (point) isearch-opoint)
910 (or (and transient-mark-mode mark-active)
911 (progn
912 (push-mark isearch-opoint t)
913 (or executing-kbd-macro (> (minibuffer-depth) 0)
914 (message "Mark saved where search started")))))
915
916 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
917
918 (defun isearch-update-ring (string &optional regexp)
919 "Add STRING to the beginning of the search ring.
920 REGEXP if non-nil says use the regexp search ring."
921 (add-to-history
922 (if regexp 'regexp-search-ring 'search-ring)
923 string
924 (if regexp regexp-search-ring-max search-ring-max)))
925
926 ;; Switching buffers should first terminate isearch-mode.
927 ;; ;; For Emacs 19, the frame switch event is handled.
928 ;; (defun isearch-switch-frame-handler ()
929 ;; (interactive) ;; Is this necessary?
930 ;; ;; First terminate isearch-mode.
931 ;; (isearch-done)
932 ;; (isearch-clean-overlays)
933 ;; (handle-switch-frame (car (cdr last-command-char))))
934
935 \f
936 ;; The search status structure and stack.
937
938 (defsubst isearch-string-state (frame)
939 "Return the search string in FRAME."
940 (aref frame 0))
941 (defsubst isearch-message-state (frame)
942 "Return the search string to display to the user in FRAME."
943 (aref frame 1))
944 (defsubst isearch-point-state (frame)
945 "Return the point in FRAME."
946 (aref frame 2))
947 (defsubst isearch-success-state (frame)
948 "Return the success flag in FRAME."
949 (aref frame 3))
950 (defsubst isearch-forward-state (frame)
951 "Return the searching-forward flag in FRAME."
952 (aref frame 4))
953 (defsubst isearch-other-end-state (frame)
954 "Return the other end of the match in FRAME."
955 (aref frame 5))
956 (defsubst isearch-word-state (frame)
957 "Return the search-by-word flag in FRAME."
958 (aref frame 6))
959 (defsubst isearch-error-state (frame)
960 "Return the regexp error message in FRAME, or nil if its regexp is valid."
961 (aref frame 7))
962 (defsubst isearch-wrapped-state (frame)
963 "Return the search-wrapped flag in FRAME."
964 (aref frame 8))
965 (defsubst isearch-barrier-state (frame)
966 "Return the barrier value in FRAME."
967 (aref frame 9))
968 (defsubst isearch-case-fold-search-state (frame)
969 "Return the case-folding flag in FRAME."
970 (aref frame 10))
971 (defsubst isearch-pop-fun-state (frame)
972 "Return the function restoring the mode-specific isearch state in FRAME."
973 (aref frame 11))
974
975 (defun isearch-top-state ()
976 (let ((cmd (car isearch-cmds)))
977 (setq isearch-string (isearch-string-state cmd)
978 isearch-message (isearch-message-state cmd)
979 isearch-success (isearch-success-state cmd)
980 isearch-forward (isearch-forward-state cmd)
981 isearch-other-end (isearch-other-end-state cmd)
982 isearch-word (isearch-word-state cmd)
983 isearch-error (isearch-error-state cmd)
984 isearch-wrapped (isearch-wrapped-state cmd)
985 isearch-barrier (isearch-barrier-state cmd)
986 isearch-case-fold-search (isearch-case-fold-search-state cmd))
987 (if (functionp (isearch-pop-fun-state cmd))
988 (funcall (isearch-pop-fun-state cmd) cmd))
989 (goto-char (isearch-point-state cmd))))
990
991 (defun isearch-pop-state ()
992 (setq isearch-cmds (cdr isearch-cmds))
993 (isearch-top-state))
994
995 (defun isearch-push-state ()
996 (setq isearch-cmds
997 (cons (vector isearch-string isearch-message (point)
998 isearch-success isearch-forward isearch-other-end
999 isearch-word
1000 isearch-error isearch-wrapped isearch-barrier
1001 isearch-case-fold-search
1002 (if isearch-push-state-function
1003 (funcall isearch-push-state-function)))
1004 isearch-cmds)))
1005
1006 \f
1007 ;; Commands active while inside of the isearch minor mode.
1008
1009 (defun isearch-exit ()
1010 "Exit search normally.
1011 However, if this is the first command after starting incremental
1012 search and `search-nonincremental-instead' is non-nil, do a
1013 nonincremental search instead via `isearch-edit-string'."
1014 (interactive)
1015 (if (and search-nonincremental-instead
1016 (= 0 (length isearch-string)))
1017 (let ((isearch-nonincremental t))
1018 (isearch-edit-string)))
1019 (isearch-done)
1020 (isearch-clean-overlays))
1021
1022
1023 (defun isearch-edit-string ()
1024 "Edit the search string in the minibuffer.
1025 The following additional command keys are active while editing.
1026 \\<minibuffer-local-isearch-map>
1027 \\[exit-minibuffer] to resume incremental searching with the edited string.
1028 \\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
1029 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
1030 \\[isearch-reverse-exit-minibuffer] to resume isearching backward.
1031 \\[isearch-complete-edit] to complete the search string using the search ring.
1032 \\<isearch-mode-map>
1033 If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
1034
1035 ;; This code is very hairy for several reasons, explained in the code.
1036 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
1037 ;; If there were a way to catch any change of buffer from the minibuffer,
1038 ;; this could be simplified greatly.
1039 ;; Editing doesn't back up the search point. Should it?
1040 (interactive)
1041 (condition-case err
1042 (progn
1043 (let ((isearch-nonincremental isearch-nonincremental)
1044
1045 ;; Locally bind all isearch global variables to protect them
1046 ;; from recursive isearching.
1047 ;; isearch-string -message and -forward are not bound
1048 ;; so they may be changed. Instead, save the values.
1049 (isearch-new-string isearch-string)
1050 (isearch-new-message isearch-message)
1051 (isearch-new-forward isearch-forward)
1052 (isearch-new-word isearch-word)
1053
1054 (isearch-regexp isearch-regexp)
1055 (isearch-op-fun isearch-op-fun)
1056 (isearch-cmds isearch-cmds)
1057 (isearch-success isearch-success)
1058 (isearch-wrapped isearch-wrapped)
1059 (isearch-barrier isearch-barrier)
1060 (isearch-adjusted isearch-adjusted)
1061 (isearch-yank-flag isearch-yank-flag)
1062 (isearch-error isearch-error)
1063 ;;; Don't bind this. We want isearch-search, below, to set it.
1064 ;;; And the old value won't matter after that.
1065 ;;; (isearch-other-end isearch-other-end)
1066 ;;; Perhaps some of these other variables should be bound for a
1067 ;;; shorter period, ending before the next isearch-search.
1068 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
1069 (isearch-opoint isearch-opoint)
1070 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
1071 (isearch-small-window isearch-small-window)
1072 (isearch-recursive-edit isearch-recursive-edit)
1073 ;; Save current configuration so we can restore it here.
1074 (isearch-window-configuration (current-window-configuration))
1075
1076 ;; Temporarily restore `minibuffer-message-timeout'.
1077 (minibuffer-message-timeout
1078 isearch-original-minibuffer-message-timeout)
1079 (isearch-original-minibuffer-message-timeout
1080 isearch-original-minibuffer-message-timeout)
1081 old-point old-other-end)
1082
1083 ;; Actually terminate isearching until editing is done.
1084 ;; This is so that the user can do anything without failure,
1085 ;; like switch buffers and start another isearch, and return.
1086 (condition-case err
1087 (isearch-done t t)
1088 (exit nil)) ; was recursive editing
1089
1090 ;; Save old point and isearch-other-end before reading from minibuffer
1091 ;; that can change their values.
1092 (setq old-point (point) old-other-end isearch-other-end)
1093
1094 (isearch-message) ;; for read-char
1095 (unwind-protect
1096 (let* (;; Why does following read-char echo?
1097 ;;(echo-keystrokes 0) ;; not needed with above message
1098 (e (let ((cursor-in-echo-area t))
1099 (read-event)))
1100 ;; Binding minibuffer-history-symbol to nil is a work-around
1101 ;; for some incompatibility with gmhist.
1102 (minibuffer-history-symbol)
1103 (message-log-max nil))
1104 ;; If the first character the user types when we prompt them
1105 ;; for a string is the yank-word character, then go into
1106 ;; word-search mode. Otherwise unread that character and
1107 ;; read a key the normal way.
1108 ;; Word search does not apply (yet) to regexp searches,
1109 ;; no check is made here.
1110 (message "%s" (isearch-message-prefix nil nil t))
1111 (if (memq (lookup-key isearch-mode-map (vector e))
1112 '(isearch-yank-word
1113 isearch-yank-word-or-char))
1114 (setq isearch-word t;; so message-prefix is right
1115 isearch-new-word t)
1116 (cancel-kbd-macro-events)
1117 (isearch-unread e))
1118 (setq cursor-in-echo-area nil)
1119 (setq isearch-new-string
1120 (read-from-minibuffer
1121 (isearch-message-prefix nil nil isearch-nonincremental)
1122 isearch-string
1123 minibuffer-local-isearch-map nil
1124 (if isearch-regexp
1125 (cons 'regexp-search-ring
1126 (1+ (or regexp-search-ring-yank-pointer -1)))
1127 (cons 'search-ring
1128 (1+ (or search-ring-yank-pointer -1))))
1129 nil t)
1130 isearch-new-message
1131 (mapconcat 'isearch-text-char-description
1132 isearch-new-string "")))
1133
1134 ;; Set point at the start (end) of old match if forward (backward),
1135 ;; so after exiting minibuffer isearch resumes at the start (end)
1136 ;; of this match and can find it again.
1137 (if (and old-other-end (eq old-point (point))
1138 (eq isearch-forward isearch-new-forward))
1139 (goto-char old-other-end))
1140
1141 ;; Always resume isearching by restarting it.
1142 (isearch-mode isearch-forward
1143 isearch-regexp
1144 isearch-op-fun
1145 nil
1146 isearch-word)
1147
1148 ;; Copy new local values to isearch globals
1149 (setq isearch-string isearch-new-string
1150 isearch-message isearch-new-message
1151 isearch-forward isearch-new-forward
1152 isearch-word isearch-new-word))
1153
1154 ;; Empty isearch-string means use default.
1155 (if (= 0 (length isearch-string))
1156 (setq isearch-string (or (car (if isearch-regexp
1157 regexp-search-ring
1158 search-ring))
1159 "")
1160
1161 isearch-message
1162 (mapconcat 'isearch-text-char-description
1163 isearch-string ""))
1164 ;; This used to set the last search string,
1165 ;; but I think it is not right to do that here.
1166 ;; Only the string actually used should be saved.
1167 ))
1168
1169 ;; This used to push the state as of before this C-s, but it adds
1170 ;; an inconsistent state where part of variables are from the
1171 ;; previous search (e.g. `isearch-success'), and part of variables
1172 ;; are just entered from the minibuffer (e.g. `isearch-string').
1173 ;; (isearch-push-state)
1174
1175 ;; Reinvoke the pending search.
1176 (isearch-search)
1177 (isearch-push-state) ; this pushes the correct state
1178 (isearch-update)
1179 (if isearch-nonincremental
1180 (progn
1181 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
1182 (isearch-done)
1183 ;; The search done message is confusing when the string
1184 ;; is empty, so erase it.
1185 (if (equal isearch-string "")
1186 (message "")))))
1187
1188 (quit ; handle abort-recursive-edit
1189 (isearch-abort) ;; outside of let to restore outside global values
1190 )))
1191
1192 (defun isearch-nonincremental-exit-minibuffer ()
1193 (interactive)
1194 (setq isearch-nonincremental t)
1195 (exit-minibuffer))
1196
1197 (defun isearch-forward-exit-minibuffer ()
1198 (interactive)
1199 (setq isearch-new-forward t)
1200 (exit-minibuffer))
1201
1202 (defun isearch-reverse-exit-minibuffer ()
1203 (interactive)
1204 (setq isearch-new-forward nil)
1205 (exit-minibuffer))
1206
1207 (defun isearch-cancel ()
1208 "Terminate the search and go back to the starting point."
1209 (interactive)
1210 (if (functionp (isearch-pop-fun-state (car (last isearch-cmds))))
1211 (funcall (isearch-pop-fun-state (car (last isearch-cmds)))
1212 (car (last isearch-cmds))))
1213 (goto-char isearch-opoint)
1214 (isearch-done t) ; exit isearch
1215 (isearch-clean-overlays)
1216 (signal 'quit nil)) ; and pass on quit signal
1217
1218 (defun isearch-abort ()
1219 "Abort incremental search mode if searching is successful, signaling quit.
1220 Otherwise, revert to previous successful search and continue searching.
1221 Use `isearch-exit' to quit without signaling."
1222 (interactive)
1223 ;; (ding) signal instead below, if quitting
1224 (discard-input)
1225 (if isearch-success
1226 ;; If search is successful, move back to starting point
1227 ;; and really do quit.
1228 (progn
1229 (setq isearch-success nil)
1230 (isearch-cancel))
1231 ;; If search is failing, or has an incomplete regexp,
1232 ;; rub out until it is once more successful.
1233 (while (or (not isearch-success) isearch-error)
1234 (isearch-pop-state))
1235 (isearch-update)))
1236
1237 (defun isearch-repeat (direction)
1238 ;; Utility for isearch-repeat-forward and -backward.
1239 (if (eq isearch-forward (eq direction 'forward))
1240 ;; C-s in forward or C-r in reverse.
1241 (if (equal isearch-string "")
1242 ;; If search string is empty, use last one.
1243 (if (null (if isearch-regexp regexp-search-ring search-ring))
1244 (setq isearch-error "No previous search string")
1245 (setq isearch-string
1246 (if isearch-regexp
1247 (car regexp-search-ring)
1248 (car search-ring))
1249 isearch-message
1250 (mapconcat 'isearch-text-char-description
1251 isearch-string "")
1252 isearch-case-fold-search isearch-last-case-fold-search))
1253 ;; If already have what to search for, repeat it.
1254 (or isearch-success
1255 (progn
1256 ;; Set isearch-wrapped before calling isearch-wrap-function
1257 (setq isearch-wrapped t)
1258 (if isearch-wrap-function
1259 (funcall isearch-wrap-function)
1260 (goto-char (if isearch-forward (point-min) (point-max)))))))
1261 ;; C-s in reverse or C-r in forward, change direction.
1262 (setq isearch-forward (not isearch-forward)
1263 isearch-success t))
1264
1265 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1266
1267 (if (equal isearch-string "")
1268 (setq isearch-success t)
1269 (if (and isearch-success
1270 (equal (point) isearch-other-end)
1271 (not isearch-just-started))
1272 ;; If repeating a search that found
1273 ;; an empty string, ensure we advance.
1274 (if (if isearch-forward (eobp) (bobp))
1275 ;; If there's nowhere to advance to, fail (and wrap next time).
1276 (progn
1277 (setq isearch-success nil)
1278 (ding))
1279 (forward-char (if isearch-forward 1 -1))
1280 (isearch-search))
1281 (isearch-search)))
1282
1283 (isearch-push-state)
1284 (isearch-update))
1285
1286 (defun isearch-repeat-forward ()
1287 "Repeat incremental search forwards."
1288 (interactive)
1289 (isearch-repeat 'forward))
1290
1291 (defun isearch-repeat-backward ()
1292 "Repeat incremental search backwards."
1293 (interactive)
1294 (isearch-repeat 'backward))
1295
1296 (defun isearch-toggle-regexp ()
1297 "Toggle regexp searching on or off."
1298 ;; The status stack is left unchanged.
1299 (interactive)
1300 (setq isearch-regexp (not isearch-regexp))
1301 (if isearch-regexp (setq isearch-word nil))
1302 (setq isearch-success t isearch-adjusted t)
1303 (isearch-update))
1304
1305 (defun isearch-toggle-case-fold ()
1306 "Toggle case folding in searching on or off."
1307 (interactive)
1308 (setq isearch-case-fold-search
1309 (if isearch-case-fold-search nil 'yes))
1310 (let ((message-log-max nil))
1311 (message "%s%s [case %ssensitive]"
1312 (isearch-message-prefix nil nil isearch-nonincremental)
1313 isearch-message
1314 (if isearch-case-fold-search "in" "")))
1315 (setq isearch-success t isearch-adjusted t)
1316 (sit-for 1)
1317 (isearch-update))
1318
1319 (defun isearch-query-replace (&optional regexp-flag)
1320 "Start `query-replace' with string to replace from last search string."
1321 (interactive)
1322 (barf-if-buffer-read-only)
1323 (if regexp-flag (setq isearch-regexp t))
1324 (let ((case-fold-search isearch-case-fold-search)
1325 ;; set `search-upper-case' to nil to not call
1326 ;; `isearch-no-upper-case-p' in `perform-replace'
1327 (search-upper-case nil))
1328 (isearch-done)
1329 (isearch-clean-overlays)
1330 (if (and isearch-other-end
1331 (< isearch-other-end (point))
1332 (not (and transient-mark-mode mark-active
1333 (< (mark) (point)))))
1334 (goto-char isearch-other-end))
1335 (set query-replace-from-history-variable
1336 (cons isearch-string
1337 (symbol-value query-replace-from-history-variable)))
1338 (perform-replace
1339 isearch-string
1340 (query-replace-read-to
1341 isearch-string
1342 (if isearch-regexp "Query replace regexp" "Query replace")
1343 isearch-regexp)
1344 t isearch-regexp isearch-word nil nil
1345 (if (and transient-mark-mode mark-active) (region-beginning))
1346 (if (and transient-mark-mode mark-active) (region-end)))))
1347
1348 (defun isearch-query-replace-regexp ()
1349 "Start `query-replace-regexp' with string to replace from last search string."
1350 (interactive)
1351 (isearch-query-replace t))
1352
1353 (defun isearch-occur (regexp &optional nlines)
1354 "Run `occur' with regexp to search from the current search string.
1355 Interactively, REGEXP is the current search regexp or a quoted search
1356 string. NLINES has the same meaning as in `occur'."
1357 (interactive
1358 (list
1359 (if isearch-regexp isearch-string (regexp-quote isearch-string))
1360 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))))
1361 (let ((case-fold-search isearch-case-fold-search)
1362 ;; set `search-upper-case' to nil to not call
1363 ;; `isearch-no-upper-case-p' in `occur-1'
1364 (search-upper-case nil))
1365 (occur regexp nlines)))
1366
1367 \f
1368 (defun isearch-delete-char ()
1369 "Discard last input item and move point back.
1370 If no previous match was done, just beep."
1371 (interactive)
1372 (if (null (cdr isearch-cmds))
1373 (ding)
1374 (isearch-pop-state))
1375 (isearch-update))
1376
1377 (defun isearch-del-char (&optional arg)
1378 "Delete character from end of search string and search again.
1379 If search string is empty, just beep."
1380 (interactive "p")
1381 (if (= 0 (length isearch-string))
1382 (ding)
1383 (setq isearch-string (substring isearch-string 0 (- (or arg 1)))
1384 isearch-message (mapconcat 'isearch-text-char-description
1385 isearch-string "")))
1386 ;; Use the isearch-other-end as new starting point to be able
1387 ;; to find the remaining part of the search string again.
1388 (if isearch-other-end (goto-char isearch-other-end))
1389 (isearch-search)
1390 (isearch-push-state)
1391 (isearch-update))
1392
1393 (defun isearch-yank-string (string)
1394 "Pull STRING into search string."
1395 ;; Downcase the string if not supposed to case-fold yanked strings.
1396 (if (and isearch-case-fold-search
1397 (eq 'not-yanks search-upper-case))
1398 (setq string (downcase string)))
1399 (if isearch-regexp (setq string (regexp-quote string)))
1400 (setq isearch-string (concat isearch-string string)
1401 isearch-message
1402 (concat isearch-message
1403 (mapconcat 'isearch-text-char-description
1404 string ""))
1405 ;; Don't move cursor in reverse search.
1406 isearch-yank-flag t)
1407 (isearch-search-and-update))
1408
1409 (defun isearch-yank-kill ()
1410 "Pull string from kill ring into search string."
1411 (interactive)
1412 (isearch-yank-string (current-kill 0)))
1413
1414 (defun isearch-yank-x-selection ()
1415 "Pull current X selection into search string."
1416 (interactive)
1417 (isearch-yank-string (x-get-selection)))
1418
1419
1420 (defun isearch-mouse-2 (click)
1421 "Handle mouse-2 in Isearch mode.
1422 For a click in the echo area, invoke `isearch-yank-x-selection'.
1423 Otherwise invoke whatever the calling mouse-2 command sequence
1424 is bound to outside of Isearch."
1425 (interactive "e")
1426 (let* ((w (posn-window (event-start click)))
1427 (overriding-terminal-local-map nil)
1428 (binding (key-binding (this-command-keys-vector) t)))
1429 (if (and (window-minibuffer-p w)
1430 (not (minibuffer-window-active-p w))) ; in echo area
1431 (isearch-yank-x-selection)
1432 (when (functionp binding)
1433 (call-interactively binding)))))
1434
1435 (defun isearch-yank-internal (jumpform)
1436 "Pull the text from point to the point reached by JUMPFORM.
1437 JUMPFORM is a lambda expression that takes no arguments and returns a
1438 buffer position, possibly having moved point to that position. For
1439 example, it might move point forward by a word and return point, or it
1440 might return the position of the end of the line."
1441 (isearch-yank-string
1442 (save-excursion
1443 (and (not isearch-forward) isearch-other-end
1444 (goto-char isearch-other-end))
1445 (buffer-substring-no-properties (point) (funcall jumpform)))))
1446
1447 (defun isearch-yank-char-in-minibuffer (&optional arg)
1448 "Pull next character from buffer into end of search string in minibuffer."
1449 (interactive "p")
1450 (if (eobp)
1451 (insert
1452 (save-excursion
1453 (set-buffer (cadr (buffer-list)))
1454 (buffer-substring-no-properties
1455 (point) (progn (forward-char arg) (point)))))
1456 (forward-char arg)))
1457
1458 (defun isearch-yank-char (&optional arg)
1459 "Pull next character from buffer into search string."
1460 (interactive "p")
1461 (isearch-yank-internal (lambda () (forward-char arg) (point))))
1462
1463 (defun isearch-yank-word-or-char ()
1464 "Pull next character or word from buffer into search string."
1465 (interactive)
1466 (isearch-yank-internal
1467 (lambda ()
1468 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1469 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1470 (forward-word 1)
1471 (forward-char 1)) (point))))
1472
1473 (defun isearch-yank-word ()
1474 "Pull next word from buffer into search string."
1475 (interactive)
1476 (isearch-yank-internal (lambda () (forward-word 1) (point))))
1477
1478 (defun isearch-yank-line ()
1479 "Pull rest of line from buffer into search string."
1480 (interactive)
1481 (isearch-yank-internal
1482 (lambda () (let ((inhibit-field-text-motion t))
1483 (line-end-position (if (eolp) 2 1))))))
1484
1485 (defun isearch-search-and-update ()
1486 ;; Do the search and update the display.
1487 (when (or isearch-success
1488 ;; Unsuccessful regexp search may become successful by
1489 ;; addition of characters which make isearch-string valid
1490 isearch-regexp
1491 ;; If the string was found but was completely invisible,
1492 ;; it might now be partly visible, so try again.
1493 (prog1 isearch-hidden (setq isearch-hidden nil)))
1494 ;; In reverse search, adding stuff at
1495 ;; the end may cause zero or many more chars to be
1496 ;; matched, in the string following point.
1497 ;; Allow all those possibilities without moving point as
1498 ;; long as the match does not extend past search origin.
1499 (if (and (not isearch-forward) (not isearch-adjusted)
1500 (condition-case ()
1501 (let ((case-fold-search isearch-case-fold-search))
1502 (if (and (eq case-fold-search t) search-upper-case)
1503 (setq case-fold-search
1504 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1505 (looking-at (if isearch-regexp isearch-string
1506 (regexp-quote isearch-string))))
1507 (error nil))
1508 (or isearch-yank-flag
1509 (<= (match-end 0)
1510 (min isearch-opoint isearch-barrier))))
1511 (progn
1512 (setq isearch-success t
1513 isearch-error nil
1514 isearch-other-end (match-end 0))
1515 (if (and (eq isearch-case-fold-search t) search-upper-case)
1516 (setq isearch-case-fold-search
1517 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1518 ;; Not regexp, not reverse, or no match at point.
1519 (if (and isearch-other-end (not isearch-adjusted))
1520 (goto-char (if isearch-forward isearch-other-end
1521 (min isearch-opoint
1522 isearch-barrier
1523 (1+ isearch-other-end)))))
1524 (isearch-search)
1525 ))
1526 (isearch-push-state)
1527 (if isearch-op-fun (funcall isearch-op-fun))
1528 (isearch-update))
1529
1530
1531 ;; *, ?, }, and | chars can make a regexp more liberal.
1532 ;; They can make a regexp match sooner or make it succeed instead of failing.
1533 ;; So go back to place last successful search started
1534 ;; or to the last ^S/^R (barrier), whichever is nearer.
1535 ;; + needs no special handling because the string must match at least once.
1536
1537 (defun isearch-backslash (str)
1538 "Return t if STR ends in an odd number of backslashes."
1539 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
1540
1541 (defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
1542 "Return point to previous successful match to allow regexp liberalization.
1543 \\<isearch-mode-map>
1544 Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by
1545 stopping at `isearch-barrier' as needed.
1546
1547 Do nothing if a backslash is escaping the liberalizing character.
1548 If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
1549
1550 Do nothing if regexp has recently been invalid unless optional
1551 ALLOW-INVALID non-nil.
1552
1553 If optional TO-BARRIER non-nil, ignore previous matches and go exactly
1554 to the barrier."
1555 ;; (eq (not a) (not b)) makes all non-nil values equivalent
1556 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
1557 (not want-backslash))
1558 ;; We have to check 2 stack frames because the last might be
1559 ;; invalid just because of a backslash.
1560 (or (not isearch-error)
1561 (not (isearch-error-state (cadr isearch-cmds)))
1562 allow-invalid))
1563 (if to-barrier
1564 (progn (goto-char isearch-barrier)
1565 (setq isearch-adjusted t))
1566 (let* ((stack isearch-cmds)
1567 (previous (cdr stack)) ; lookbelow in the stack
1568 (frame (car stack)))
1569 ;; Walk down the stack looking for a valid regexp (as of course only
1570 ;; they can be the previous successful match); this conveniently
1571 ;; removes all bracket-sets and groups that might be in the way, as
1572 ;; well as partial \{\} constructs that the code below leaves behind.
1573 ;; Also skip over postfix operators -- though horrid,
1574 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
1575 (while (and previous
1576 (or (isearch-error-state frame)
1577 (let* ((string (isearch-string-state frame))
1578 (lchar (aref string (1- (length string)))))
1579 ;; The operators aren't always operators; check
1580 ;; backslashes. This doesn't handle the case of
1581 ;; operators at the beginning of the regexp not
1582 ;; being special, but then we should fall back to
1583 ;; the barrier anyway because it's all optional.
1584 (if (isearch-backslash
1585 (isearch-string-state (car previous)))
1586 (eq lchar ?\})
1587 (memq lchar '(?* ?? ?+))))))
1588 (setq stack previous previous (cdr previous) frame (car stack)))
1589 (when stack
1590 ;; `stack' now refers the most recent valid regexp that is not at
1591 ;; all optional in its last term. Now dig one level deeper and find
1592 ;; what matched before that.
1593 (let ((last-other-end
1594 (or (and (car previous)
1595 (isearch-other-end-state (car previous)))
1596 isearch-barrier)))
1597 (goto-char (if isearch-forward
1598 (max last-other-end isearch-barrier)
1599 (min last-other-end isearch-barrier)))
1600 (setq isearch-adjusted t)))))))
1601
1602 (defun isearch-unread-key-sequence (keylist)
1603 "Unread the given key-sequence KEYLIST.
1604 Scroll-bar or mode-line events are processed appropriately."
1605 (cancel-kbd-macro-events)
1606 (apply 'isearch-unread keylist)
1607 ;; If the event was a scroll-bar or mode-line click, the event will have
1608 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
1609 ;; it here, because this symbol will be attached to the event again next
1610 ;; time it gets read by read-key-sequence.
1611 ;;
1612 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
1613 ;; modify the event anymore in 21 because read_key_sequence no longer
1614 ;; modifies events to produce fake prefix keys.")
1615 (if (and (> (length keylist) 1)
1616 (symbolp (car keylist))
1617 (listp (cadr keylist))
1618 (not (numberp (posn-point
1619 (event-start (cadr keylist) )))))
1620 (pop unread-command-events)))
1621
1622 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1623 ;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
1624 ;;
1625 ;; The idea here is that certain vertical scrolling commands (like C-l
1626 ;; `recenter') should be usable WITHIN Isearch mode. For a command to be
1627 ;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
1628 ;; tamper with isearch's state, or move point. It is unacceptable for the
1629 ;; search string to be scrolled out of the current window. If a command
1630 ;; attempts this, we scroll the text back again.
1631 ;;
1632 ;; We implement this feature with a property called `isearch-scroll'.
1633 ;; If a command's symbol has the value t for this property it is a
1634 ;; scrolling command. The feature needs to be enabled by setting the
1635 ;; customizable variable `isearch-allow-scroll' to a non-nil value.
1636 ;;
1637 ;; The universal argument commands (e.g. C-u) in simple.el are marked
1638 ;; as scrolling commands, and isearch.el has been amended to allow
1639 ;; prefix arguments to be passed through to scrolling commands. Thus
1640 ;; M-0 C-l will scroll point to the top of the window.
1641 ;;
1642 ;; Horizontal scrolling commands are currently not catered for.
1643 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1644
1645 ;; Set the isearch-scroll property on some standard functions:
1646 ;; Scroll-bar functions:
1647 (if (fboundp 'scroll-bar-toolkit-scroll)
1648 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
1649 (if (fboundp 'mac-handle-scroll-bar-event)
1650 (put 'mac-handle-scroll-bar-event 'isearch-scroll t))
1651 (if (fboundp 'w32-handle-scroll-bar-event)
1652 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
1653
1654 ;; Commands which scroll the window:
1655 (put 'recenter 'isearch-scroll t)
1656 (put 'reposition-window 'isearch-scroll t)
1657 (put 'scroll-up 'isearch-scroll t)
1658 (put 'scroll-down 'isearch-scroll t)
1659
1660 ;; Commands which act on the other window
1661 (put 'list-buffers 'isearch-scroll t)
1662 (put 'scroll-other-window 'isearch-scroll t)
1663 (put 'scroll-other-window-down 'isearch-scroll t)
1664 (put 'beginning-of-buffer-other-window 'isearch-scroll t)
1665 (put 'end-of-buffer-other-window 'isearch-scroll t)
1666
1667 ;; Commands which change the window layout
1668 (put 'delete-other-windows 'isearch-scroll t)
1669 (put 'balance-windows 'isearch-scroll t)
1670 (put 'split-window-vertically 'isearch-scroll t)
1671 (put 'split-window-horizontally 'isearch-scroll t)
1672 (put 'enlarge-window 'isearch-scroll t)
1673
1674 ;; Universal argument commands
1675 (put 'universal-argument 'isearch-scroll t)
1676 (put 'negative-argument 'isearch-scroll t)
1677 (put 'digit-argument 'isearch-scroll t)
1678
1679 (defcustom isearch-allow-scroll nil
1680 "If non-nil, scrolling commands are allowed during incremental search."
1681 :type 'boolean
1682 :group 'isearch)
1683
1684 (defun isearch-string-out-of-window (isearch-point)
1685 "Test whether the search string is currently outside of the window.
1686 Return nil if it's completely visible, or if point is visible,
1687 together with as much of the search string as will fit; the symbol
1688 `above' if we need to scroll the text downwards; the symbol `below',
1689 if upwards."
1690 (let ((w-start (window-start))
1691 (w-end (window-end nil t))
1692 (w-L1 (save-excursion (move-to-window-line 1) (point)))
1693 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
1694 start end) ; start and end of search string in buffer
1695 (if isearch-forward
1696 (setq end isearch-point start (or isearch-other-end isearch-point))
1697 (setq start isearch-point end (or isearch-other-end isearch-point)))
1698 (cond ((or (and (>= start w-start) (<= end w-end))
1699 (if isearch-forward
1700 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
1701 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
1702 nil)
1703 ((and (< start w-start)
1704 (< isearch-point w-L-1))
1705 'above)
1706 (t 'below))))
1707
1708 (defun isearch-back-into-window (above isearch-point)
1709 "Scroll the window to bring the search string back into view.
1710 Restore point to ISEARCH-POINT in the process. ABOVE is t when the
1711 search string is above the top of the window, nil when it is beneath
1712 the bottom."
1713 (let (start end)
1714 (if isearch-forward
1715 (setq end isearch-point start (or isearch-other-end isearch-point))
1716 (setq start isearch-point end (or isearch-other-end isearch-point)))
1717 (if above
1718 (progn
1719 (goto-char start)
1720 (recenter 0)
1721 (when (>= isearch-point (window-end nil t))
1722 (goto-char isearch-point)
1723 (recenter -1)))
1724 (goto-char end)
1725 (recenter -1)
1726 (when (< isearch-point (window-start))
1727 (goto-char isearch-point)
1728 (recenter 0))))
1729 (goto-char isearch-point))
1730
1731 (defun isearch-reread-key-sequence-naturally (keylist)
1732 "Reread key sequence KEYLIST with Isearch mode's keymap deactivated.
1733 Return the key sequence as a string/vector."
1734 (isearch-unread-key-sequence keylist)
1735 (let (overriding-terminal-local-map)
1736 (read-key-sequence nil))) ; This will go through function-key-map, if nec.
1737
1738 (defun isearch-lookup-scroll-key (key-seq)
1739 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
1740 Otherwise return nil."
1741 (let* ((overriding-terminal-local-map nil)
1742 (binding (key-binding key-seq)))
1743 (and binding (symbolp binding) (commandp binding)
1744 (eq (get binding 'isearch-scroll) t)
1745 binding)))
1746
1747 (defalias 'isearch-other-control-char 'isearch-other-meta-char)
1748
1749 (defun isearch-other-meta-char (&optional arg)
1750 "Process a miscellaneous key sequence in Isearch mode.
1751
1752 Try to convert the current key-sequence to something usable in Isearch
1753 mode, either by converting it with `function-key-map', downcasing a
1754 key with C-<upper case>, or finding a \"scrolling command\" bound to
1755 it. \(In the last case, we may have to read more events.) If so,
1756 either unread the converted sequence or execute the command.
1757
1758 Otherwise, if `search-exit-option' is non-nil (the default) unread the
1759 key-sequence and exit the search normally. If it is the symbol
1760 `edit', the search string is edited in the minibuffer and the meta
1761 character is unread so that it applies to editing the string.
1762
1763 ARG is the prefix argument. It will be transmitted through to the
1764 scrolling command or to the command whose key-sequence exits
1765 Isearch mode."
1766 (interactive "P")
1767 (let* ((key (if current-prefix-arg ; not nec the same as ARG
1768 (substring (this-command-keys) universal-argument-num-events)
1769 (this-command-keys)))
1770 (main-event (aref key 0))
1771 (keylist (listify-key-sequence key))
1772 scroll-command isearch-point)
1773 (cond ((and (= (length key) 1)
1774 (let ((lookup (lookup-key local-function-key-map key)))
1775 (not (or (null lookup) (integerp lookup)
1776 (keymapp lookup)))))
1777 ;; Handle a function key that translates into something else.
1778 ;; If the key has a global definition too,
1779 ;; exit and unread the key itself, so its global definition runs.
1780 ;; Otherwise, unread the translation,
1781 ;; so that the translated key takes effect within isearch.
1782 (cancel-kbd-macro-events)
1783 (if (lookup-key global-map key)
1784 (progn
1785 (isearch-done)
1786 (apply 'isearch-unread keylist))
1787 (setq keylist
1788 (listify-key-sequence (lookup-key local-function-key-map key)))
1789 (while keylist
1790 (setq key (car keylist))
1791 ;; If KEY is a printing char, we handle it here
1792 ;; directly to avoid the input method and keyboard
1793 ;; coding system translating it.
1794 (if (and (integerp key)
1795 (>= key ?\s) (/= key 127) (< key 256))
1796 (progn
1797 (isearch-process-search-char key)
1798 (setq keylist (cdr keylist)))
1799 ;; As the remaining keys in KEYLIST can't be handled
1800 ;; here, we must reread them.
1801 (apply 'isearch-unread keylist)
1802 (setq keylist nil)))))
1803 (
1804 ;; Handle an undefined shifted control character
1805 ;; by downshifting it if that makes it defined.
1806 ;; (As read-key-sequence would normally do,
1807 ;; if we didn't have a default definition.)
1808 (let ((mods (event-modifiers main-event)))
1809 (and (integerp main-event)
1810 (memq 'shift mods)
1811 (memq 'control mods)
1812 (not (memq (lookup-key isearch-mode-map
1813 (let ((copy (copy-sequence key)))
1814 (aset copy 0
1815 (- main-event
1816 (- ?\C-\S-a ?\C-a)))
1817 copy)
1818 nil)
1819 '(nil
1820 isearch-other-control-char)))))
1821 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1822 (cancel-kbd-macro-events)
1823 (apply 'isearch-unread keylist))
1824 ((eq search-exit-option 'edit)
1825 (apply 'isearch-unread keylist)
1826 (isearch-edit-string))
1827 ;; Handle a scrolling function.
1828 ((and isearch-allow-scroll
1829 (progn (setq key (isearch-reread-key-sequence-naturally keylist))
1830 (setq keylist (listify-key-sequence key))
1831 (setq main-event (aref key 0))
1832 (setq scroll-command (isearch-lookup-scroll-key key))))
1833 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
1834 ;; complete key sequence, possibly as modified by function-key-map,
1835 ;; not merely the one or two event fragment which invoked
1836 ;; isearch-other-meta-char in the first place.
1837 (setq isearch-point (point))
1838 (setq prefix-arg arg)
1839 (command-execute scroll-command)
1840 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
1841 (if ab-bel
1842 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
1843 (goto-char isearch-point)))
1844 (isearch-update))
1845 ;; A mouse click on the isearch message starts editing the search string
1846 ((and (eq (car-safe main-event) 'down-mouse-1)
1847 (window-minibuffer-p (posn-window (event-start main-event))))
1848 ;; Swallow the up-event.
1849 (read-event)
1850 (isearch-edit-string))
1851 (search-exit-option
1852 (let (window)
1853 (isearch-unread-key-sequence keylist)
1854 (setq main-event (car unread-command-events))
1855
1856 ;; If we got a mouse click event, that event contains the
1857 ;; window clicked on. maybe it was read with the buffer
1858 ;; it was clicked on. If so, that buffer, not the current one,
1859 ;; is in isearch mode. So end the search in that buffer.
1860
1861 ;; ??? I have no idea what this if checks for, but it's
1862 ;; obviously wrong for the case that a down-mouse event
1863 ;; on another window invokes this function. The event
1864 ;; will contain the window clicked on and that window's
1865 ;; buffer is certainly not always in Isearch mode.
1866 ;;
1867 ;; Leave the code in, but check for current buffer not
1868 ;; being in Isearch mode for now, until someone tells
1869 ;; what it's really supposed to do.
1870 ;;
1871 ;; --gerd 2001-08-10.
1872
1873 (if (and (not isearch-mode)
1874 (listp main-event)
1875 (setq window (posn-window (event-start main-event)))
1876 (windowp window)
1877 (or (> (minibuffer-depth) 0)
1878 (not (window-minibuffer-p window))))
1879 (save-excursion
1880 (set-buffer (window-buffer window))
1881 (isearch-done)
1882 (isearch-clean-overlays))
1883 (isearch-done)
1884 (isearch-clean-overlays)
1885 (setq prefix-arg arg))))
1886 (t;; otherwise nil
1887 (isearch-process-search-string key key)))))
1888
1889 (defun isearch-quote-char ()
1890 "Quote special characters for incremental search."
1891 (interactive)
1892 (let ((char (read-quoted-char (isearch-message t))))
1893 ;; Assume character codes 0200 - 0377 stand for characters in some
1894 ;; single-byte character set, and convert them to Emacs
1895 ;; characters.
1896 (if (and isearch-regexp (= char ?\s))
1897 (if (subregexp-context-p isearch-string (length isearch-string))
1898 (isearch-process-search-string "[ ]" " ")
1899 (isearch-process-search-char char))
1900 (and enable-multibyte-characters
1901 (>= char ?\200)
1902 (<= char ?\377)
1903 (setq char (unibyte-char-to-multibyte char)))
1904 (isearch-process-search-char char))))
1905
1906 (defun isearch-return-char ()
1907 "Convert return into newline for incremental search."
1908 (interactive)
1909 (isearch-process-search-char ?\n))
1910 (make-obsolete 'isearch-return-char 'isearch-printing-char "19.7")
1911
1912 (defun isearch-printing-char ()
1913 "Add this ordinary printing character to the search string and search."
1914 (interactive)
1915 (let ((char last-command-char))
1916 (if (= char ?\S-\ )
1917 (setq char ?\s))
1918 (if current-input-method
1919 (isearch-process-search-multibyte-characters char)
1920 (isearch-process-search-char char))))
1921
1922 (defun isearch-process-search-char (char)
1923 ;; * and ? are special in regexps when not preceded by \.
1924 ;; } and | are special in regexps when preceded by \.
1925 ;; Nothing special for + because it matches at least once.
1926 (cond
1927 ((memq char '(?* ??)) (isearch-fallback nil))
1928 ((eq char ?\}) (isearch-fallback t t))
1929 ((eq char ?|) (isearch-fallback t nil t)))
1930
1931 ;; Append the char to the search string, update the message and re-search.
1932 (isearch-process-search-string
1933 (char-to-string char)
1934 (if (>= char ?\200)
1935 (char-to-string char)
1936 (isearch-text-char-description char))))
1937
1938 (defun isearch-process-search-string (string message)
1939 (setq isearch-string (concat isearch-string string)
1940 isearch-message (concat isearch-message message))
1941 (isearch-search-and-update))
1942
1943 \f
1944 ;; Search Ring
1945
1946 (defun isearch-ring-adjust1 (advance)
1947 ;; Helper for isearch-ring-adjust
1948 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1949 (length (length ring))
1950 (yank-pointer-name (if isearch-regexp
1951 'regexp-search-ring-yank-pointer
1952 'search-ring-yank-pointer))
1953 (yank-pointer (eval yank-pointer-name)))
1954 (if (zerop length)
1955 ()
1956 (set yank-pointer-name
1957 (setq yank-pointer
1958 (mod (+ (or yank-pointer 0)
1959 (if advance -1 1))
1960 length)))
1961 (setq isearch-string (nth yank-pointer ring)
1962 isearch-message (mapconcat 'isearch-text-char-description
1963 isearch-string "")))))
1964
1965 (defun isearch-ring-adjust (advance)
1966 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1967 (isearch-ring-adjust1 advance)
1968 (if search-ring-update
1969 (progn
1970 (isearch-search)
1971 (isearch-push-state)
1972 (isearch-update))
1973 ;; Otherwise, edit the search string instead. Note that there is
1974 ;; no need to push the search state after isearch-edit-string here
1975 ;; since isearch-edit-string already pushes its state
1976 (isearch-edit-string)))
1977
1978 (defun isearch-ring-advance ()
1979 "Advance to the next search string in the ring."
1980 ;; This could be more general to handle a prefix arg, but who would use it.
1981 (interactive)
1982 (isearch-ring-adjust 'advance))
1983
1984 (defun isearch-ring-retreat ()
1985 "Retreat to the previous search string in the ring."
1986 (interactive)
1987 (isearch-ring-adjust nil))
1988
1989 (defun isearch-complete1 ()
1990 ;; Helper for isearch-complete and isearch-complete-edit
1991 ;; Return t if completion OK, nil if no completion exists.
1992 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1993 (completion-ignore-case case-fold-search)
1994 (completion (try-completion isearch-string ring)))
1995 (cond
1996 ((eq completion t)
1997 ;; isearch-string stays the same
1998 t)
1999 ((or completion ; not nil, must be a string
2000 (= 0 (length isearch-string))) ; shouldn't have to say this
2001 (if (equal completion isearch-string) ;; no extension?
2002 (progn
2003 (if completion-auto-help
2004 (with-output-to-temp-buffer "*Isearch completions*"
2005 (display-completion-list
2006 (all-completions isearch-string ring))))
2007 t)
2008 (and completion
2009 (setq isearch-string completion))))
2010 (t
2011 (message "No completion") ; waits a second if in minibuffer
2012 nil))))
2013
2014 (defun isearch-complete ()
2015 "Complete the search string from the strings on the search ring.
2016 The completed string is then editable in the minibuffer.
2017 If there is no completion possible, say so and continue searching."
2018 (interactive)
2019 (if (isearch-complete1)
2020 (progn (setq isearch-message
2021 (mapconcat 'isearch-text-char-description
2022 isearch-string ""))
2023 (isearch-edit-string))
2024 ;; else
2025 (sit-for 1)
2026 (isearch-update)))
2027
2028 (defun isearch-complete-edit ()
2029 "Same as `isearch-complete' except in the minibuffer."
2030 (interactive)
2031 (setq isearch-string (field-string))
2032 (if (isearch-complete1)
2033 (progn
2034 (delete-field)
2035 (insert isearch-string))))
2036
2037 \f
2038 ;; Message string
2039
2040 (defun isearch-message (&optional c-q-hack ellipsis)
2041 ;; Generate and print the message string.
2042 (let ((cursor-in-echo-area ellipsis)
2043 (m isearch-message)
2044 (cmds isearch-cmds)
2045 succ-msg)
2046 (when (or (not isearch-success) isearch-error)
2047 ;; Highlight failed part
2048 (while (or (not (isearch-success-state (car cmds)))
2049 (isearch-error-state (car cmds)))
2050 (pop cmds))
2051 (setq succ-msg (and cmds (isearch-message-state (car cmds)))
2052 m (copy-sequence m))
2053 (add-text-properties
2054 (if (and (stringp succ-msg)
2055 (< (length succ-msg) (length m))
2056 (equal succ-msg (substring m 0 (length succ-msg))))
2057 (length succ-msg)
2058 0)
2059 (length m) '(face isearch-fail) m)
2060 ;; Highlight failed trailing whitespace
2061 (when (string-match " +$" m)
2062 (add-text-properties (match-beginning 0) (match-end 0)
2063 '(face trailing-whitespace) m)))
2064 (setq m (concat
2065 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
2066 m
2067 (isearch-message-suffix c-q-hack ellipsis)))
2068 (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
2069
2070 (defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
2071 ;; If about to search, and previous search regexp was invalid,
2072 ;; check that it still is. If it is valid now,
2073 ;; let the message we display while searching say that it is valid.
2074 (and isearch-error ellipsis
2075 (condition-case ()
2076 (progn (re-search-forward isearch-string (point) t)
2077 (setq isearch-error nil))
2078 (error nil)))
2079 ;; If currently failing, display no ellipsis.
2080 (or isearch-success (setq ellipsis nil))
2081 (let ((m (concat (if isearch-success "" "failing ")
2082 (if isearch-adjusted "pending " "")
2083 (if (and isearch-wrapped
2084 (not isearch-wrap-function)
2085 (if isearch-forward
2086 (> (point) isearch-opoint)
2087 (< (point) isearch-opoint)))
2088 "over")
2089 (if isearch-wrapped "wrapped ")
2090 (if isearch-word "word " "")
2091 (if isearch-regexp "regexp " "")
2092 (if nonincremental "search" "I-search")
2093 (if isearch-forward "" " backward")
2094 (if current-input-method
2095 (concat " [" current-input-method-title "]: ")
2096 ": ")
2097 )))
2098 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
2099 'face 'minibuffer-prompt)))
2100
2101 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
2102 (concat (if c-q-hack "^Q" "")
2103 (if isearch-error
2104 (concat " [" isearch-error "]")
2105 "")))
2106
2107 \f
2108 ;; Searching
2109
2110 (defvar isearch-search-fun-function nil
2111 "Override `isearch-search-fun'.
2112 This function should return the search function for isearch to use.
2113 It will call this function with three arguments
2114 as if it were `search-forward'.")
2115
2116 (defun isearch-search-fun ()
2117 "Return the function to use for the search.
2118 Can be changed via `isearch-search-fun-function' for special needs."
2119 (if isearch-search-fun-function
2120 (funcall isearch-search-fun-function)
2121 (cond
2122 (isearch-word
2123 (if isearch-forward 'word-search-forward 'word-search-backward))
2124 (isearch-regexp
2125 (if isearch-forward 're-search-forward 're-search-backward))
2126 (t
2127 (if isearch-forward 'search-forward 'search-backward)))))
2128
2129 (defun isearch-search-string (string bound noerror)
2130 ;; Search for the first occurance of STRING or its translation. If
2131 ;; found, move point to the end of the occurance, update
2132 ;; isearch-match-beg and isearch-match-end, and return point.
2133 (let ((func (isearch-search-fun))
2134 (len (length string))
2135 pos1 pos2)
2136 (setq pos1 (save-excursion (funcall func string bound noerror)))
2137 (if (and (char-table-p translation-table-for-input)
2138 (multibyte-string-p string)
2139 ;; Minor optimization.
2140 (string-match-p "[^[:ascii:]]" string))
2141 (let ((translated
2142 (apply 'string
2143 (mapcar (lambda (c)
2144 (or (aref translation-table-for-input c) c))
2145 string)))
2146 match-data)
2147 (when translated
2148 (save-match-data
2149 (save-excursion
2150 (if (setq pos2 (funcall func translated bound noerror))
2151 (setq match-data (match-data t)))))
2152 (when (and pos2
2153 (or (not pos1)
2154 (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
2155 (setq pos1 pos2)
2156 (set-match-data match-data)))))
2157 (when pos1
2158 ;; When using multiple buffers isearch, switch to the new buffer here,
2159 ;; because `save-excursion' above doesn't allow doing it inside funcall.
2160 (if (and isearch-buffers-next-buffer-function
2161 (buffer-live-p isearch-buffers-current-buffer))
2162 (switch-to-buffer isearch-buffers-current-buffer))
2163 (goto-char pos1))
2164 pos1))
2165
2166 (defun isearch-search ()
2167 ;; Do the search with the current search string.
2168 (if isearch-message-function
2169 (funcall isearch-message-function nil t)
2170 (isearch-message nil t))
2171 (if (and (eq isearch-case-fold-search t) search-upper-case)
2172 (setq isearch-case-fold-search
2173 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2174 (condition-case lossage
2175 (let ((inhibit-point-motion-hooks
2176 (and (eq isearch-success-function 'isearch-success-function-default)
2177 search-invisible))
2178 (inhibit-quit nil)
2179 (case-fold-search isearch-case-fold-search)
2180 (search-spaces-regexp search-whitespace-regexp)
2181 (retry t))
2182 (setq isearch-error nil)
2183 (while retry
2184 (setq isearch-success
2185 (isearch-search-string isearch-string nil t))
2186 ;; Clear RETRY unless we matched some invisible text
2187 ;; and we aren't supposed to do that.
2188 (if (or (not isearch-success)
2189 (bobp) (eobp)
2190 (= (match-beginning 0) (match-end 0))
2191 (funcall isearch-success-function
2192 (match-beginning 0) (match-end 0)))
2193 (setq retry nil)))
2194 (setq isearch-just-started nil)
2195 (if isearch-success
2196 (setq isearch-other-end
2197 (if isearch-forward (match-beginning 0) (match-end 0)))))
2198
2199 (quit (isearch-unread ?\C-g)
2200 (setq isearch-success nil))
2201
2202 (invalid-regexp
2203 (setq isearch-error (car (cdr lossage)))
2204 (if (string-match
2205 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
2206 isearch-error)
2207 (setq isearch-error "incomplete input")))
2208
2209 (search-failed
2210 (setq isearch-success nil)
2211 (setq isearch-error (nth 2 lossage)))
2212
2213 (error
2214 ;; stack overflow in regexp search.
2215 (setq isearch-error (format "%s" lossage))))
2216
2217 (if isearch-success
2218 nil
2219 ;; Ding if failed this time after succeeding last time.
2220 (and (isearch-success-state (car isearch-cmds))
2221 (ding))
2222 (if (functionp (isearch-pop-fun-state (car isearch-cmds)))
2223 (funcall (isearch-pop-fun-state (car isearch-cmds)) (car isearch-cmds)))
2224 (goto-char (isearch-point-state (car isearch-cmds)))))
2225
2226
2227 ;; Called when opening an overlay, and we are still in isearch.
2228 (defun isearch-open-overlay-temporary (ov)
2229 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
2230 ;; Some modes would want to open the overlays temporary during
2231 ;; isearch in their own way, they should set the
2232 ;; `isearch-open-invisible-temporary' to a function doing this.
2233 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2234 ;; Store the values for the `invisible' and `intangible'
2235 ;; properties, and then set them to nil. This way the text hidden
2236 ;; by this overlay becomes visible.
2237
2238 ;; Do we really need to set the `intangible' property to t? Can we
2239 ;; have the point inside an overlay with an `intangible' property?
2240 ;; In 19.34 this does not exist so I cannot test it.
2241 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2242 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
2243 (overlay-put ov 'invisible nil)
2244 (overlay-put ov 'intangible nil)))
2245
2246
2247 ;; This is called at the end of isearch. It will open the overlays
2248 ;; that contain the latest match. Obviously in case of a C-g the
2249 ;; point returns to the original location which surely is not contain
2250 ;; in any of these overlays, se we are safe in this case too.
2251 (defun isearch-open-necessary-overlays (ov)
2252 (let ((inside-overlay (and (> (point) (overlay-start ov))
2253 (< (point) (overlay-end ov))))
2254 ;; If this exists it means that the overlay was opened using
2255 ;; this function, not by us tweaking the overlay properties.
2256 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2257 (when (or inside-overlay (not fct-temp))
2258 ;; restore the values for the `invisible' and `intangible'
2259 ;; properties
2260 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2261 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2262 (overlay-put ov 'isearch-invisible nil)
2263 (overlay-put ov 'isearch-intangible nil))
2264 (if inside-overlay
2265 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2266 (if fct-temp
2267 (funcall fct-temp ov t)))))
2268
2269 ;; This is called when exiting isearch. It closes the temporary
2270 ;; opened overlays, except the ones that contain the latest match.
2271 (defun isearch-clean-overlays ()
2272 (when isearch-opened-overlays
2273 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
2274 (setq isearch-opened-overlays nil)))
2275
2276
2277 (defun isearch-intersects-p (start0 end0 start1 end1)
2278 "Return t if regions START0..END0 and START1..END1 intersect."
2279 (or (and (>= start0 start1) (< start0 end1))
2280 (and (> end0 start1) (<= end0 end1))
2281 (and (>= start1 start0) (< start1 end0))
2282 (and (> end1 start0) (<= end1 end0))))
2283
2284
2285 ;; Verify if the current match is outside of each element of
2286 ;; `isearch-opened-overlays', if so close that overlay.
2287
2288 (defun isearch-close-unnecessary-overlays (begin end)
2289 (let ((overlays isearch-opened-overlays))
2290 (setq isearch-opened-overlays nil)
2291 (dolist (ov overlays)
2292 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
2293 (push ov isearch-opened-overlays)
2294 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2295 (if fct-temp
2296 ;; If this exists it means that the overlay was opened
2297 ;; using this function, not by us tweaking the overlay
2298 ;; properties.
2299 (funcall fct-temp ov t)
2300 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2301 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2302 (overlay-put ov 'isearch-invisible nil)
2303 (overlay-put ov 'isearch-intangible nil)))))))
2304
2305
2306 (defun isearch-range-invisible (beg end)
2307 "Return t if all the text from BEG to END is invisible."
2308 (when (/= beg end)
2309 ;; Check that invisibility runs up to END.
2310 (save-excursion
2311 (goto-char beg)
2312 (let (;; can-be-opened keeps track if we can open some overlays.
2313 (can-be-opened (eq search-invisible 'open))
2314 ;; the list of overlays that could be opened
2315 (crt-overlays nil))
2316 (when (and can-be-opened isearch-hide-immediately)
2317 (isearch-close-unnecessary-overlays beg end))
2318 ;; If the following character is currently invisible,
2319 ;; skip all characters with that same `invisible' property value.
2320 ;; Do that over and over.
2321 (while (and (< (point) end)
2322 (let ((prop
2323 (get-char-property (point) 'invisible)))
2324 (if (eq buffer-invisibility-spec t)
2325 prop
2326 (or (memq prop buffer-invisibility-spec)
2327 (assq prop buffer-invisibility-spec)))))
2328 (if (get-text-property (point) 'invisible)
2329 (progn
2330 (goto-char (next-single-property-change (point) 'invisible
2331 nil end))
2332 ;; if text is hidden by an `invisible' text property
2333 ;; we cannot open it at all.
2334 (setq can-be-opened nil))
2335 (when can-be-opened
2336 (let ((overlays (overlays-at (point)))
2337 ov-list
2338 o
2339 invis-prop)
2340 (while overlays
2341 (setq o (car overlays)
2342 invis-prop (overlay-get o 'invisible))
2343 (if (if (eq buffer-invisibility-spec t)
2344 invis-prop
2345 (or (memq invis-prop buffer-invisibility-spec)
2346 (assq invis-prop buffer-invisibility-spec)))
2347 (if (overlay-get o 'isearch-open-invisible)
2348 (setq ov-list (cons o ov-list))
2349 ;; We found one overlay that cannot be
2350 ;; opened, that means the whole chunk
2351 ;; cannot be opened.
2352 (setq can-be-opened nil)))
2353 (setq overlays (cdr overlays)))
2354 (if can-be-opened
2355 ;; It makes sense to append to the open
2356 ;; overlays list only if we know that this is
2357 ;; t.
2358 (setq crt-overlays (append ov-list crt-overlays)))))
2359 (goto-char (next-overlay-change (point)))))
2360 ;; See if invisibility reaches up thru END.
2361 (if (>= (point) end)
2362 (if (and can-be-opened (consp crt-overlays))
2363 (progn
2364 (setq isearch-opened-overlays
2365 (append isearch-opened-overlays crt-overlays))
2366 (mapc 'isearch-open-overlay-temporary crt-overlays)
2367 nil)
2368 (setq isearch-hidden t)))))))
2369
2370 (defun isearch-success-function-default (beg end)
2371 "Default function to report if the new search match is successful.
2372 Returns t if search can match hidden text, or otherwise checks if some
2373 text from BEG to END is visible."
2374 (or (eq search-invisible t)
2375 (not (isearch-range-invisible beg end))))
2376
2377 \f
2378 ;; General utilities
2379
2380 (defun isearch-no-upper-case-p (string regexp-flag)
2381 "Return t if there are no upper case chars in STRING.
2382 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
2383 since they have special meaning in a regexp."
2384 (let (quote-flag (i 0) (len (length string)) found)
2385 (while (and (not found) (< i len))
2386 (let ((char (aref string i)))
2387 (if (and regexp-flag (eq char ?\\))
2388 (setq quote-flag (not quote-flag))
2389 (if (and (not quote-flag) (not (eq char (downcase char))))
2390 (setq found t))
2391 (setq quote-flag nil)))
2392 (setq i (1+ i)))
2393 (not (or found
2394 ;; Even if there's no uppercase char, we want to detect the use
2395 ;; of [:upper:] or [:lower:] char-class, which indicates
2396 ;; clearly that the user cares about case distinction.
2397 (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
2398 (condition-case err
2399 (progn
2400 (string-match (substring string 0 (match-beginning 0))
2401 "")
2402 nil)
2403 (invalid-regexp
2404 (equal "Unmatched [ or [^" (cadr err)))))))))
2405
2406 ;; Portability functions to support various Emacs versions.
2407
2408 (defun isearch-text-char-description (c)
2409 (cond
2410 ((< c ?\s) (format "^%c" (+ c 64)))
2411 ((= c ?\^?) "^?")
2412 (t (char-to-string c))))
2413
2414 ;; General function to unread characters or events.
2415 ;; Also insert them in a keyboard macro being defined.
2416 (defun isearch-unread (&rest char-or-events)
2417 (mapc 'store-kbd-macro-event char-or-events)
2418 (setq unread-command-events
2419 (append char-or-events unread-command-events)))
2420
2421 \f
2422 ;; Highlighting
2423
2424 (defvar isearch-overlay nil)
2425
2426 (defun isearch-highlight (beg end)
2427 (if search-highlight
2428 (if isearch-overlay
2429 ;; Overlay already exists, just move it.
2430 (move-overlay isearch-overlay beg end (current-buffer))
2431 ;; Overlay doesn't exist, create it.
2432 (setq isearch-overlay (make-overlay beg end))
2433 ;; 1001 is higher than lazy's 1000 and ediff's 100+
2434 (overlay-put isearch-overlay 'priority 1001)
2435 (overlay-put isearch-overlay 'face isearch))))
2436
2437 (defun isearch-dehighlight ()
2438 (when isearch-overlay
2439 (delete-overlay isearch-overlay)))
2440 \f
2441 ;; isearch-lazy-highlight feature
2442 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
2443
2444 ;; When active, *every* match for the current search string is
2445 ;; highlighted: the current one using the normal isearch match color
2446 ;; and all the others using `isearch-lazy-highlight'. The extra
2447 ;; highlighting makes it easier to anticipate where the cursor will
2448 ;; land each time you press C-s or C-r to repeat a pending search.
2449 ;; Highlighting of these additional matches happens in a deferred
2450 ;; fashion using "idle timers," so the cycles needed do not rob
2451 ;; isearch of its usual snappy response.
2452
2453 ;; IMPLEMENTATION NOTE: This depends on some isearch internals.
2454 ;; Specifically:
2455 ;; - `isearch-update' is expected to be called (at least) every time
2456 ;; the search string or window-start changes;
2457 ;; - `isearch-string' is expected to contain the current search
2458 ;; string as entered by the user;
2459 ;; - the type of the current search is expected to be given by
2460 ;; `isearch-word' and `isearch-regexp';
2461 ;; - the direction of the current search is expected to be given by
2462 ;; `isearch-forward';
2463 ;; - the variable `isearch-error' is expected to be true
2464 ;; only if `isearch-string' is an invalid regexp.
2465
2466 (defvar isearch-lazy-highlight-overlays nil)
2467 (defvar isearch-lazy-highlight-wrapped nil)
2468 (defvar isearch-lazy-highlight-start-limit nil)
2469 (defvar isearch-lazy-highlight-end-limit nil)
2470 (defvar isearch-lazy-highlight-start nil)
2471 (defvar isearch-lazy-highlight-end nil)
2472 (defvar isearch-lazy-highlight-timer nil)
2473 (defvar isearch-lazy-highlight-last-string nil)
2474 (defvar isearch-lazy-highlight-window nil)
2475 (defvar isearch-lazy-highlight-window-start nil)
2476 (defvar isearch-lazy-highlight-window-end nil)
2477 (defvar isearch-lazy-highlight-case-fold-search nil)
2478 (defvar isearch-lazy-highlight-regexp nil)
2479 (defvar isearch-lazy-highlight-space-regexp nil)
2480
2481 (defun lazy-highlight-cleanup (&optional force)
2482 "Stop lazy highlighting and remove extra highlighting from current buffer.
2483 FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
2484 is nil. This function is called when exiting an incremental search if
2485 `lazy-highlight-cleanup' is non-nil."
2486 (interactive '(t))
2487 (if (or force lazy-highlight-cleanup)
2488 (while isearch-lazy-highlight-overlays
2489 (delete-overlay (car isearch-lazy-highlight-overlays))
2490 (setq isearch-lazy-highlight-overlays
2491 (cdr isearch-lazy-highlight-overlays))))
2492 (when isearch-lazy-highlight-timer
2493 (cancel-timer isearch-lazy-highlight-timer)
2494 (setq isearch-lazy-highlight-timer nil)))
2495
2496 (define-obsolete-function-alias 'isearch-lazy-highlight-cleanup
2497 'lazy-highlight-cleanup
2498 "22.1")
2499
2500 (defun isearch-lazy-highlight-new-loop (&optional beg end)
2501 "Cleanup any previous `lazy-highlight' loop and begin a new one.
2502 BEG and END specify the bounds within which highlighting should occur.
2503 This is called when `isearch-update' is invoked (which can cause the
2504 search string to change or the window to scroll). It is also used
2505 by other Emacs features."
2506 (when (and (null executing-kbd-macro)
2507 (sit-for 0) ;make sure (window-start) is credible
2508 (or (not (equal isearch-string
2509 isearch-lazy-highlight-last-string))
2510 (not (eq (selected-window)
2511 isearch-lazy-highlight-window))
2512 (not (eq isearch-lazy-highlight-case-fold-search
2513 isearch-case-fold-search))
2514 (not (eq isearch-lazy-highlight-regexp
2515 isearch-regexp))
2516 (not (= (window-start)
2517 isearch-lazy-highlight-window-start))
2518 (not (= (window-end) ; Window may have been split/joined.
2519 isearch-lazy-highlight-window-end))))
2520 ;; something important did indeed change
2521 (lazy-highlight-cleanup t) ;kill old loop & remove overlays
2522 (when (not isearch-error)
2523 (setq isearch-lazy-highlight-start-limit beg
2524 isearch-lazy-highlight-end-limit end)
2525 (setq isearch-lazy-highlight-window (selected-window)
2526 isearch-lazy-highlight-window-start (window-start)
2527 isearch-lazy-highlight-window-end (window-end)
2528 isearch-lazy-highlight-start (point)
2529 isearch-lazy-highlight-end (point)
2530 isearch-lazy-highlight-last-string isearch-string
2531 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2532 isearch-lazy-highlight-regexp isearch-regexp
2533 isearch-lazy-highlight-wrapped nil
2534 isearch-lazy-highlight-space-regexp search-whitespace-regexp)
2535 (unless (equal isearch-string "")
2536 (setq isearch-lazy-highlight-timer
2537 (run-with-idle-timer lazy-highlight-initial-delay nil
2538 'isearch-lazy-highlight-update))))))
2539
2540 (defun isearch-lazy-highlight-search ()
2541 "Search ahead for the next or previous match, for lazy highlighting.
2542 Attempt to do the search exactly the way the pending isearch would."
2543 (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
2544 (isearch-regexp isearch-lazy-highlight-regexp)
2545 (search-spaces-regexp isearch-lazy-highlight-space-regexp))
2546 (condition-case nil
2547 (isearch-search-string
2548 isearch-lazy-highlight-last-string
2549 (if isearch-forward
2550 (min (or isearch-lazy-highlight-end-limit (point-max))
2551 (if isearch-lazy-highlight-wrapped
2552 isearch-lazy-highlight-start
2553 (window-end)))
2554 (max (or isearch-lazy-highlight-start-limit (point-min))
2555 (if isearch-lazy-highlight-wrapped
2556 isearch-lazy-highlight-end
2557 (window-start))))
2558 t)
2559 (error nil))))
2560
2561 (defun isearch-lazy-highlight-update ()
2562 "Update highlighting of other matches for current search."
2563 (let ((max lazy-highlight-max-at-a-time)
2564 (looping t)
2565 nomore)
2566 (with-local-quit
2567 (save-selected-window
2568 (if (and (window-live-p isearch-lazy-highlight-window)
2569 (not (eq (selected-window) isearch-lazy-highlight-window)))
2570 (select-window isearch-lazy-highlight-window))
2571 (save-excursion
2572 (save-match-data
2573 (goto-char (if isearch-forward
2574 isearch-lazy-highlight-end
2575 isearch-lazy-highlight-start))
2576 (while looping
2577 (let ((found (isearch-lazy-highlight-search)))
2578 (when max
2579 (setq max (1- max))
2580 (if (<= max 0)
2581 (setq looping nil)))
2582 (if found
2583 (let ((mb (match-beginning 0))
2584 (me (match-end 0)))
2585 (if (= mb me) ;zero-length match
2586 (if isearch-forward
2587 (if (= mb (if isearch-lazy-highlight-wrapped
2588 isearch-lazy-highlight-start
2589 (window-end)))
2590 (setq found nil)
2591 (forward-char 1))
2592 (if (= mb (if isearch-lazy-highlight-wrapped
2593 isearch-lazy-highlight-end
2594 (window-start)))
2595 (setq found nil)
2596 (forward-char -1)))
2597
2598 ;; non-zero-length match
2599 (let ((ov (make-overlay mb me)))
2600 (push ov isearch-lazy-highlight-overlays)
2601 ;; 1000 is higher than ediff's 100+,
2602 ;; but lower than isearch main overlay's 1001
2603 (overlay-put ov 'priority 1000)
2604 (overlay-put ov 'face lazy-highlight-face)
2605 (overlay-put ov 'window (selected-window))))
2606 (if isearch-forward
2607 (setq isearch-lazy-highlight-end (point))
2608 (setq isearch-lazy-highlight-start (point)))))
2609
2610 ;; not found or zero-length match at the search bound
2611 (if (not found)
2612 (if isearch-lazy-highlight-wrapped
2613 (setq looping nil
2614 nomore t)
2615 (setq isearch-lazy-highlight-wrapped t)
2616 (if isearch-forward
2617 (progn
2618 (setq isearch-lazy-highlight-end (window-start))
2619 (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
2620 (window-start))))
2621 (setq isearch-lazy-highlight-start (window-end))
2622 (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
2623 (window-end))))))))
2624 (unless nomore
2625 (setq isearch-lazy-highlight-timer
2626 (run-at-time lazy-highlight-interval nil
2627 'isearch-lazy-highlight-update)))))))))
2628
2629 (defun isearch-resume (string regexp word forward message case-fold)
2630 "Resume an incremental search.
2631 STRING is the string or regexp searched for.
2632 REGEXP non-nil means the resumed search was a regexp search.
2633 WORD non-nil means resume a word search.
2634 FORWARD non-nil means resume a forward search.
2635 MESSAGE is the echo-area message recorded for the search resumed.
2636 CASE-FOLD non-nil means the search was case-insensitive."
2637 (isearch-mode forward regexp nil nil word)
2638 (setq isearch-string string
2639 isearch-message message
2640 isearch-case-fold-search case-fold)
2641 (isearch-search)
2642 (isearch-update))
2643
2644 ;; arch-tag: 74850515-f7d8-43a6-8a2c-ca90a4c1e675
2645 ;;; isearch.el ends here