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