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