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