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