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