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