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