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