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