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