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