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