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