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