isearch.el 92.5 KB
Newer Older
1
;;; isearch.el --- incremental search minor mode
2

3 4
;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999,
;;   2000, 2001, 2003, 2004  Free Software Foundation, Inc.
Eric S. Raymond's avatar
Eric S. Raymond committed
5

6
;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7
;; Maintainer: FSF
Dave Love's avatar
Dave Love committed
8
;; Keywords: matching
Jim Blandy's avatar
Jim Blandy committed
9

David J. MacKenzie's avatar
David J. MacKenzie committed
10
;; This file is part of GNU Emacs.
Jim Blandy's avatar
Jim Blandy committed
11

12 13 14 15 16
;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

Jim Blandy's avatar
Jim Blandy committed
17
;; GNU Emacs is distributed in the hope that it will be useful,
18 19 20 21 22
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Erik Naggum's avatar
Erik Naggum committed
23 24 25
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Jim Blandy's avatar
Jim Blandy committed
26

27 28
;;; Commentary:

Jim Blandy's avatar
Jim Blandy committed
29 30
;; Instructions

31 32
;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
;; isearch-mode behaves modally and does not return until the search
Dave Love's avatar
Dave Love committed
33
;; is completed.  It uses a recursive-edit to behave this way.
34

Jim Blandy's avatar
Jim Blandy committed
35
;; The key bindings active within isearch-mode are defined below in
36 37 38 39 40 41 42
;; `isearch-mode-map' which is given bindings close to the default
;; characters of the original isearch.el.  With `isearch-mode',
;; however, you can bind multi-character keys and it should be easier
;; to add new commands.  One bug though: keys with meta-prefix cannot
;; be longer than two chars.  Also see minibuffer-local-isearch-map
;; for bindings active during `isearch-edit-string'.

Dave Love's avatar
Dave Love committed
43 44 45
;; isearch-mode should work even if you switch windows with the mouse,
;; in which case isearch-mode is terminated automatically before the
;; switch.
46 47 48 49 50 51 52 53 54 55 56 57 58 59

;; The search ring and completion commands automatically put you in
;; the minibuffer to edit the string.  This gives you a chance to
;; modify the search string before executing the search.  There are
;; three commands to terminate the editing: C-s and C-r exit the
;; minibuffer and search forward and reverse respectively, while C-m
;; exits and does a nonincremental search.

;; Exiting immediately from isearch uses isearch-edit-string instead
;; of nonincremental-search, if search-nonincremental-instead is non-nil.
;; The name of this option should probably be changed if we decide to
;; keep the behavior.  No point in forcing nonincremental search until
;; the last possible moment.

60
;;; Code:
61 62


63
;; Some additional options and constants.
64

65 66
(defgroup isearch nil
  "Incremental search minor mode."
Dave Love's avatar
Dave Love committed
67 68
  :link '(emacs-commentary-link "isearch")
  :link '(custom-manual "(emacs)Incremental Search")
69 70 71
  :prefix "isearch-"
  :prefix "search-"
  :group 'matching)
72

73 74 75 76 77 78 79

(defcustom search-exit-option t
  "*Non-nil means random control characters terminate incremental search."
  :type 'boolean
  :group 'isearch)

(defcustom search-slow-window-lines 1
80 81 82
  "*Number of lines in slow search display windows.
These are the short windows used during incremental search on slow terminals.
Negative means put the slow search window at the top (normally it's at bottom)
83 84 85
and the value is minus the number of lines."
  :type 'integer
  :group 'isearch)
86

87
(defcustom search-slow-speed 1200
88 89
  "*Highest terminal speed at which to use \"slow\" style incremental search.
This is the style where a one-line window is created to show the line
90 91 92
that the search has reached."
  :type 'integer
  :group 'isearch)
Jim Blandy's avatar
Jim Blandy committed
93

94
(defcustom search-upper-case 'not-yanks
Jim Blandy's avatar
Jim Blandy committed
95
  "*If non-nil, upper case chars disable case fold searching.
96 97
That is, upper and lower case chars must match exactly.
This applies no matter where the chars come from, but does not
98
apply to chars in regexps that are prefixed with `\\'.
99 100 101
If this value is `not-yanks', yanked text is always downcased."
  :type '(choice (const :tag "off" nil)
		 (const not-yanks)
102
		 (other :tag "on" t))
103
  :group 'isearch)
Jim Blandy's avatar
Jim Blandy committed
104

105
(defcustom search-nonincremental-instead t
Jim Blandy's avatar
Jim Blandy committed
106
  "*If non-nil, do a nonincremental search instead if exiting immediately.
107
Actually, `isearch-edit-string' is called to let you enter the search
108 109 110
string, and RET terminates editing and does a nonincremental search."
  :type 'boolean
  :group 'isearch)
Jim Blandy's avatar
Jim Blandy committed
111

112
(defcustom search-whitespace-regexp "\\(?:\\s-+\\)"
Jim Blandy's avatar
Jim Blandy committed
113
  "*If non-nil, regular expression to match a sequence of whitespace chars.
114
This applies to regular expression incremental search.
115 116 117
You might want to use something like \"\\\\(?:[ \\t\\r\\n]+\\\\)\" instead.
In the Customization buffer, that is `\\(?:[' followed by a space,
a tab, a carriage return (control-M), a newline, and `]+\\)'."
118 119
  :type 'regexp
  :group 'isearch)
Jim Blandy's avatar
Jim Blandy committed
120

121
(defcustom search-highlight t
122 123 124
  "*Non-nil means incremental search highlights the current match."
  :type 'boolean
  :group 'isearch)
125

126 127 128 129 130 131
(defcustom search-invisible 'open
  "If t incremental search can match hidden text.
nil means don't match invisible text.
If the value is `open', if the text matched is made invisible by
an overlay having an `invisible' property and that overlay has a property
`isearch-open-invisible', then incremental search will show the contents.
Stefan Monnier's avatar
Stefan Monnier committed
132 133 134
\(This applies when using `outline.el' and `hideshow.el'.)
See also `reveal-mode' if you want overlays to automatically be opened
whenever point is in one of them."
135 136
  :type '(choice (const :tag "Match hidden text" t)
		 (const :tag "Open overlays" open)
137
		 (const :tag "Don't match hidden text" nil))
138 139 140
  :group 'isearch)

(defcustom isearch-hide-immediately t
141 142 143 144
  "If non-nil, re-hide an invisible match right away.
This variable makes a difference when `search-invisible' is set to `open'.
It means that after search makes some invisible text visible
to show the match, it makes the text invisible again when the match moves.
145 146
Ordinarily the text becomes invisible again at the end of the search."
  :type 'boolean
147
  :group 'isearch)
148

149 150 151 152 153
(defcustom isearch-resume-enabled t
  "*If non-nil, `isearch-resume' commands are added to the command history."
  :type 'boolean
  :group 'isearch)

154 155 156 157 158 159
(defvar isearch-mode-hook nil
  "Function(s) to call after starting up an incremental search.")

(defvar isearch-mode-end-hook nil
  "Function(s) to call after terminating an incremental search.")

160 161 162 163 164 165 166 167 168
(defvar isearch-wrap-function nil
  "Function to call to wrap the search when search is failed.
If nil, move point to the beginning of the buffer for a forward search,
or to the end of the buffer for a backward search.")

(defvar isearch-push-state-function nil
  "Function to save a function restoring the mode-specific isearch state
to the search status stack.")

169
;; Search ring.
Jim Blandy's avatar
Jim Blandy committed
170 171 172

(defvar search-ring nil
  "List of search string sequences.")
173
(defvar regexp-search-ring nil
Jim Blandy's avatar
Jim Blandy committed
174 175
  "List of regular expression search string sequences.")

176 177 178 179 180 181 182 183
(defcustom search-ring-max 16
  "*Maximum length of search ring before oldest elements are thrown away."
  :type 'integer
  :group 'isearch)
(defcustom regexp-search-ring-max 16
  "*Maximum length of regexp search ring before oldest elements are thrown away."
  :type 'integer
  :group 'isearch)
Jim Blandy's avatar
Jim Blandy committed
184 185

(defvar search-ring-yank-pointer nil
186 187
  "Index in `search-ring' of last string reused.
nil if none yet.")
188
(defvar regexp-search-ring-yank-pointer nil
189 190
  "Index in `regexp-search-ring' of last string reused.
nil if none yet.")
Jim Blandy's avatar
Jim Blandy committed
191

192
(defcustom search-ring-update nil
193
  "*Non-nil if advancing or retreating in the search ring should cause search.
194 195 196
Default value, nil, means edit the string instead."
  :type 'boolean
  :group 'isearch)
197

198
;; Define isearch-mode keymap.
Jim Blandy's avatar
Jim Blandy committed
199

200 201 202 203 204 205 206 207 208 209 210 211
(defvar isearch-mode-map
  (let* ((i 0)
	 (map (make-keymap)))
    (or (vectorp (nth 1 map))
	(char-table-p (nth 1 map))
	(error "The initialization of isearch-mode-map must be updated"))
    ;; Make all multibyte characters search for themselves.
    (let ((l (generic-character-list))
	  (table (nth 1 map)))
      (while l
	(set-char-table-default table (car l) 'isearch-printing-char)
	(setq l (cdr l))))
212 213
    ;; Make function keys, etc, which aren't bound to a scrolling-function
    ;; exit the search.
214 215
    (define-key map [t] 'isearch-other-control-char)
    ;; Control chars, by default, end isearch mode transparently.
216
    ;; We need these explicit definitions because, in a dense keymap,
217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248
    ;; the binding for t does not affect characters.
    ;; We use a dense keymap to save space.
    (while (< i ?\ )
      (define-key map (make-string 1 i) 'isearch-other-control-char)
      (setq i (1+ i)))

    ;; Single-byte printing chars extend the search string by default.
    (setq i ?\ )
    (while (< i 256)
      (define-key map (vector i) 'isearch-printing-char)
      (setq i (1+ i)))

    ;; To handle local bindings with meta char prefix keys, define
    ;; another full keymap.  This must be done for any other prefix
    ;; keys as well, one full keymap per char of the prefix key.  It
    ;; would be simpler to disable the global keymap, and/or have a
    ;; default local key binding for any key not otherwise bound.
    (let ((meta-map (make-sparse-keymap)))
      (define-key map (char-to-string meta-prefix-char) meta-map)
      (define-key map [escape] meta-map))
    (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)

    ;; Several non-printing chars change the searching behavior.
    (define-key map "\C-s" 'isearch-repeat-forward)
    (define-key map "\C-r" 'isearch-repeat-backward)
    ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
    ;; combinations can be used to repeat regexp isearches that can
    ;; be used to start these searches.
    (define-key map "\M-\C-s" 'isearch-repeat-forward)
    (define-key map "\M-\C-r" 'isearch-repeat-backward)
    (define-key map "\177" 'isearch-delete-char)
    (define-key map "\C-g" 'isearch-abort)
249

250 251 252 253 254
    ;; This assumes \e is the meta-prefix-char.
    (or (= ?\e meta-prefix-char)
	(error "Inconsistency in isearch.el"))
    (define-key map "\e\e\e" 'isearch-cancel)
    (define-key map  [escape escape escape] 'isearch-cancel)
255

256
    (define-key map "\C-q" 'isearch-quote-char)
257

258 259 260 261 262
    (define-key map "\r" 'isearch-exit)
    (define-key map "\C-j" 'isearch-printing-char)
    (define-key map "\t" 'isearch-printing-char)
    (define-key map " " 'isearch-whitespace-chars)
    (define-key map [?\S-\ ] 'isearch-whitespace-chars)
263

264 265 266 267
    (define-key map    "\C-w" 'isearch-yank-word-or-char)
    (define-key map "\M-\C-w" 'isearch-del-char)
    (define-key map "\M-\C-y" 'isearch-yank-char)
    (define-key map    "\C-y" 'isearch-yank-line)
268

269
    ;; Define keys for regexp chars * ? } |.
270 271 272
    ;; Nothing special for + because it matches at least once.
    (define-key map "*" 'isearch-*-char)
    (define-key map "?" 'isearch-*-char)
273
    (define-key map "}" 'isearch-}-char)
274
    (define-key map "|" 'isearch-|-char)
275

276 277 278
    ;; Turned off because I find I expect to get the global definition--rms.
    ;; ;; Instead bind C-h to special help command for isearch-mode.
    ;; (define-key map "\C-h" 'isearch-mode-help)
279

280 281 282 283 284 285 286 287 288 289 290 291 292
    (define-key map "\M-n" 'isearch-ring-advance)
    (define-key map "\M-p" 'isearch-ring-retreat)
    (define-key map "\M-y" 'isearch-yank-kill)

    (define-key map "\M-\t" 'isearch-complete)

    ;; Pass frame events transparently so they won't exit the search.
    ;; In particular, if we have more than one display open, then a
    ;; switch-frame might be generated by someone typing at another keyboard.
    (define-key map [switch-frame] nil)
    (define-key map [delete-frame] nil)
    (define-key map [iconify-frame] nil)
    (define-key map [make-frame-visible] nil)
293
    (define-key map [mouse-movement] nil)
294 295 296 297 298
    ;; For searching multilingual text.
    (define-key map "\C-\\" 'isearch-toggle-input-method)
    (define-key map "\C-^" 'isearch-toggle-specified-input-method)

    ;; People expect to be able to paste with the mouse.
299
    (define-key map [mouse-2] #'isearch-mouse-2)
300 301 302 303 304 305 306 307
    (define-key map [down-mouse-2] nil)

    ;; Some bindings you may want to put in your isearch-mode-hook.
    ;; Suggest some alternates...
    (define-key map "\M-c" 'isearch-toggle-case-fold)
    (define-key map "\M-r" 'isearch-toggle-regexp)
    (define-key map "\M-e" 'isearch-edit-string)

308 309
    (define-key map [?\M-%] 'isearch-query-replace)
    (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
310

311 312 313 314 315 316
    map)
  "Keymap for `isearch-mode'.")

(defvar minibuffer-local-isearch-map
  (let ((map (make-sparse-keymap)))
    (set-keymap-parent map minibuffer-local-map)
317 318 319 320 321 322 323
    (define-key map "\r"    'isearch-nonincremental-exit-minibuffer)
    (define-key map "\M-n"  'isearch-ring-advance-edit)
    (define-key map [next]  'isearch-ring-advance-edit)
    (define-key map [down]  'isearch-ring-advance-edit)
    (define-key map "\M-p"  'isearch-ring-retreat-edit)
    (define-key map [prior] 'isearch-ring-retreat-edit)
    (define-key map [up]    'isearch-ring-retreat-edit)
324
    (define-key map "\M-\t" 'isearch-complete-edit)
325 326 327 328
    (define-key map "\C-s"  'isearch-forward-exit-minibuffer)
    (define-key map "\C-r"  'isearch-reverse-exit-minibuffer)
    (define-key map "\C-f"  'isearch-yank-char-in-minibuffer)
    (define-key map [right] 'isearch-yank-char-in-minibuffer)
329
    map)
330
  "Keymap for editing isearch strings in the minibuffer.")
Jim Blandy's avatar
Jim Blandy committed
331 332

;; Internal variables declared globally for byte-compiler.
333 334 335 336 337 338
;; These are all set with setq while isearching
;; and bound locally while editing the search string.

(defvar isearch-forward nil)	; Searching in the forward direction.
(defvar isearch-regexp nil)	; Searching for a regexp.
(defvar isearch-word nil)	; Searching for words.
Stefan Monnier's avatar
Stefan Monnier committed
339
(defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
340

341 342
(defvar isearch-cmds nil
  "Stack of search status sets.
343 344 345
Each set is a vector of the form:
 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
  INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
346

347 348 349 350 351
(defvar isearch-string "")  ; The current search string.
(defvar isearch-message "") ; text-char-description version of isearch-string

(defvar isearch-success t)		; Searching is currently successful.
(defvar isearch-invalid-regexp nil)	; Regexp not well formed.
352
(defvar isearch-within-brackets nil)	; Regexp has unclosed [.
353 354
(defvar isearch-other-end nil)	; Start (end) of match if forward (backward).
(defvar isearch-wrapped nil)	; Searching restarted from the top (bottom).
Jim Blandy's avatar
Jim Blandy committed
355
(defvar isearch-barrier 0)
356
(defvar isearch-just-started nil)
357
(defvar isearch-start-hscroll 0)	; hscroll when starting the search.
Jim Blandy's avatar
Jim Blandy committed
358

359 360 361 362
; case-fold-search while searching.
;   either nil, t, or 'yes.  'yes means the same as t except that mixed
;   case in the search string is ignored.
(defvar isearch-case-fold-search nil)
Jim Blandy's avatar
Jim Blandy committed
363

364 365
(defvar isearch-last-case-fold-search nil)

366 367 368
;; Used to save default value while isearch is active
(defvar isearch-original-minibuffer-message-timeout nil)

Jim Blandy's avatar
Jim Blandy committed
369 370
(defvar isearch-adjusted nil)
(defvar isearch-slow-terminal-mode nil)
371
;; If t, using a small window.
372
(defvar isearch-small-window nil)
Jim Blandy's avatar
Jim Blandy committed
373
(defvar isearch-opoint 0)
374
;; The window configuration active at the beginning of the search.
375
(defvar isearch-window-configuration nil)
Jim Blandy's avatar
Jim Blandy committed
376

377 378
;; Flag to indicate a yank occurred, so don't move the cursor.
(defvar isearch-yank-flag nil)
Jim Blandy's avatar
Jim Blandy committed
379

380 381 382
;; A function to be called after each input character is processed.
;; (It is not called after characters that exit the search.)
;; It is only set from an optional argument to `isearch-mode'.
383
(defvar isearch-op-fun nil)
Jim Blandy's avatar
Jim Blandy committed
384

385
;;  Is isearch-mode in a recursive edit for modal searching.
386
(defvar isearch-recursive-edit nil)
Jim Blandy's avatar
Jim Blandy committed
387

388
;; Should isearch be terminated after doing one search?
389 390 391 392
(defvar isearch-nonincremental nil)

;; New value of isearch-forward after isearch-edit-string.
(defvar isearch-new-forward nil)
Jim Blandy's avatar
Jim Blandy committed
393

394 395
;; Accumulate here the overlays opened during searching.
(defvar isearch-opened-overlays nil)
Jim Blandy's avatar
Jim Blandy committed
396

397 398 399 400 401 402 403
;; The value of input-method-function when isearch is invoked.
(defvar isearch-input-method-function nil)

;; A flag to tell if input-method-function is locally bound when
;; isearch is invoked.
(defvar isearch-input-method-local-p nil)

Jim Blandy's avatar
Jim Blandy committed
404 405 406 407 408 409 410
;; Minor-mode-alist changes - kind of redundant with the
;; echo area, but if isearching in multiple windows, it can be useful.

(or (assq 'isearch-mode minor-mode-alist)
    (nconc minor-mode-alist
	   (list '(isearch-mode isearch-mode))))

411
(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
Jim Blandy's avatar
Jim Blandy committed
412 413
(make-variable-buffer-local 'isearch-mode)

414 415 416 417 418
(define-key global-map "\C-s" 'isearch-forward)
(define-key esc-map "\C-s" 'isearch-forward-regexp)
(define-key global-map "\C-r" 'isearch-backward)
(define-key esc-map "\C-r" 'isearch-backward-regexp)

419
;; Entry points to isearch-mode.
Jim Blandy's avatar
Jim Blandy committed
420

421
(defun isearch-forward (&optional regexp-p no-recursive-edit)
Jim Blandy's avatar
Jim Blandy committed
422 423
  "\
Do incremental search forward.
424 425
With a prefix argument, do an incremental regular expression search instead.
\\<isearch-mode-map>
Jim Blandy's avatar
Jim Blandy committed
426
As you type characters, they add to the search string and are found.
427
The following non-printing keys are bound in `isearch-mode-map'.
Jim Blandy's avatar
Jim Blandy committed
428

429
Type \\[isearch-delete-char] to cancel last input item from end of search string.
Jim Blandy's avatar
Jim Blandy committed
430
Type \\[isearch-exit] to exit, leaving point at location found.
431 432 433
Type LFD (C-j) to match end of line.
Type \\[isearch-repeat-forward] to search again forward,\
 \\[isearch-repeat-backward] to search again backward.
434
Type \\[isearch-yank-word-or-char] to yank word from buffer onto end of search\
435
 string and search for it.
436 437
Type \\[isearch-del-char] to delete character from end of search string.
Type \\[isearch-yank-char] to yank char from buffer onto end of search\
438 439 440
 string and search for it.
Type \\[isearch-yank-line] to yank rest of line onto end of search string\
 and search for it.
Karl Heuer's avatar
Karl Heuer committed
441 442
Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
 and search for it.
Jim Blandy's avatar
Jim Blandy committed
443
Type \\[isearch-quote-char] to quote control character to search for it.
444 445 446 447 448
\\[isearch-abort] while searching or when search has failed cancels input\
 back to what has
 been found successfully.
\\[isearch-abort] when search is successful aborts and moves point to\
 starting point.
Jim Blandy's avatar
Jim Blandy committed
449

450 451 452 453
Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
Type \\[isearch-edit-string] to edit the search string in the minibuffer.

Jim Blandy's avatar
Jim Blandy committed
454 455
Also supported is a search ring of the previous 16 search strings.
Type \\[isearch-ring-advance] to search for the next item in the search ring.
456 457 458
Type \\[isearch-ring-retreat] to search for the previous item in the search\
 ring.
Type \\[isearch-complete] to complete the search string using the search ring.
Jim Blandy's avatar
Jim Blandy committed
459

Eli Zaretskii's avatar
Eli Zaretskii committed
460
If an input method is turned on in the current buffer, that input
461
method is also active while you are typing characters to search.  To
462 463 464 465
toggle the input method, type \\[isearch-toggle-input-method].  It
also toggles the input method in the current buffer.

To use a different input method for searching, type
Eli Zaretskii's avatar
Eli Zaretskii committed
466
\\[isearch-toggle-specified-input-method], and specify an input method
467 468
you want to use.

469
The above keys, bound in `isearch-mode-map', are often controlled by
470
 options; do \\[apropos] on search-.* to find them.
Jim Blandy's avatar
Jim Blandy committed
471
Other control and meta characters terminate the search
472
 and are then executed normally (depending on `search-exit-option').
473
Likewise for function keys and mouse button events.
Jim Blandy's avatar
Jim Blandy committed
474

475 476
If this function is called non-interactively, it does not return to
the calling function until the search is done."
Jim Blandy's avatar
Jim Blandy committed
477

478
  (interactive "P\np")
479
  (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
Jim Blandy's avatar
Jim Blandy committed
480

481
(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
Jim Blandy's avatar
Jim Blandy committed
482 483
  "\
Do incremental search forward for regular expression.
484
With a prefix argument, do a regular string search instead.
Jim Blandy's avatar
Jim Blandy committed
485 486
Like ordinary incremental search except that your input
is treated as a regexp.  See \\[isearch-forward] for more info."
487
  (interactive "P\np")
488
  (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
Jim Blandy's avatar
Jim Blandy committed
489

490
(defun isearch-backward (&optional regexp-p no-recursive-edit)
Jim Blandy's avatar
Jim Blandy committed
491 492
  "\
Do incremental search backward.
493
With a prefix argument, do a regular expression search instead.
Jim Blandy's avatar
Jim Blandy committed
494
See \\[isearch-forward] for more information."
495
  (interactive "P\np")
496
  (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
Jim Blandy's avatar
Jim Blandy committed
497

498
(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
Jim Blandy's avatar
Jim Blandy committed
499 500
  "\
Do incremental search backward for regular expression.
501
With a prefix argument, do a regular string search instead.
Jim Blandy's avatar
Jim Blandy committed
502 503
Like ordinary incremental search except that your input
is treated as a regexp.  See \\[isearch-forward] for more info."
504
  (interactive "P\np")
505
  (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
506 507 508 509 510 511


(defun isearch-mode-help ()
  (interactive)
  (describe-function 'isearch-forward)
  (isearch-update))
Jim Blandy's avatar
Jim Blandy committed
512 513 514 515 516


;; isearch-mode only sets up incremental search for the minor mode.
;; All the work is done by the isearch-mode commands.

517
;; Not used yet:
518
;;(defvar isearch-commands '(isearch-forward isearch-backward
519 520
;;			     isearch-forward-regexp isearch-backward-regexp)
;;  "List of commands for which isearch-mode does not recursive-edit.")
521

522 523

(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
Richard M. Stallman's avatar
Richard M. Stallman committed
524 525 526
  "Start isearch minor mode.  Called by `isearch-forward', etc.

\\{isearch-mode-map}"
Jim Blandy's avatar
Jim Blandy committed
527 528 529 530

  ;; Initialize global vars.
  (setq isearch-forward forward
	isearch-regexp regexp
531
	isearch-word word-p
Jim Blandy's avatar
Jim Blandy committed
532
	isearch-op-fun op-fun
533
	isearch-last-case-fold-search isearch-case-fold-search
Jim Blandy's avatar
Jim Blandy committed
534 535 536 537 538 539 540 541 542 543
	isearch-case-fold-search case-fold-search
	isearch-string ""
	isearch-message ""
	isearch-cmds nil
	isearch-success t
	isearch-wrapped nil
	isearch-barrier (point)
	isearch-adjusted nil
	isearch-yank-flag nil
	isearch-invalid-regexp nil
544
	isearch-within-brackets nil
545
	isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
Jim Blandy's avatar
Jim Blandy committed
546
					(> (window-height)
547 548
					   (* 4
					      (abs search-slow-window-lines))))
Jim Blandy's avatar
Jim Blandy committed
549 550
	isearch-other-end nil
	isearch-small-window nil
551
	isearch-just-started t
552
	isearch-start-hscroll (window-hscroll)
Jim Blandy's avatar
Jim Blandy committed
553 554

	isearch-opoint (point)
555
	search-ring-yank-pointer nil
556
	isearch-opened-overlays nil
557 558
	isearch-input-method-function input-method-function
	isearch-input-method-local-p (local-variable-p 'input-method-function)
559 560 561 562 563 564
	regexp-search-ring-yank-pointer nil

	;; Save the original value of `minibuffer-message-timeout', and
	;; set it to nil so that isearch's messages don't get timed out.
	isearch-original-minibuffer-message-timeout minibuffer-message-timeout
	minibuffer-message-timeout nil)
565 566 567

  ;; We must bypass input method while reading key.  When a user type
  ;; printable character, appropriate input method is turned on in
568
  ;; minibuffer to read multibyte characters.
569 570 571 572
  (or isearch-input-method-local-p
      (make-local-variable 'input-method-function))
  (setq input-method-function nil)

573
  (looking-at "")
574 575
  (setq isearch-window-configuration
	(if isearch-slow-terminal-mode (current-window-configuration) nil))
576

577 578
  ;; Maybe make minibuffer frame visible and/or raise it.
  (let ((frame (window-frame (minibuffer-window))))
579 580 581 582 583
    (unless (memq (frame-live-p frame) '(nil t))
      (unless (frame-visible-p frame)
	(make-frame-visible frame))
      (if minibuffer-auto-raise
	  (raise-frame frame))))
584

Jim Blandy's avatar
Jim Blandy committed
585
  (setq	isearch-mode " Isearch")  ;; forward? regexp?
586
  (force-mode-line-update)
Jim Blandy's avatar
Jim Blandy committed
587 588 589

  (isearch-push-state)

590
  (setq overriding-terminal-local-map isearch-mode-map)
Jim Blandy's avatar
Jim Blandy committed
591 592
  (isearch-update)
  (run-hooks 'isearch-mode-hook)
593

594
  (add-hook 'mouse-leave-buffer-hook 'isearch-done)
595
  (add-hook 'kbd-macro-termination-hook 'isearch-done)
596

597 598
  ;; isearch-mode can be made modal (in the sense of not returning to
  ;; the calling function until searching is completed) by entering
599
  ;; a recursive-edit and exiting it when done isearching.
600 601 602
  (if recursive-edit
      (let ((isearch-recursive-edit t))
	(recursive-edit)))
603
  isearch-success)
Jim Blandy's avatar
Jim Blandy committed
604 605 606 607 608


;; Some high level utilities.  Others below.

(defun isearch-update ()
609
  ;; Called after each command to update the display.
610 611
  (if (and (null unread-command-events)
	   (null executing-kbd-macro))
Jim Blandy's avatar
Jim Blandy committed
612
      (progn
613 614 615
        (if (not (input-pending-p))
            (isearch-message))
        (if (and isearch-slow-terminal-mode
616
                 (not (or isearch-small-window
617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
                          (pos-visible-in-window-p))))
            (let ((found-point (point)))
              (setq isearch-small-window t)
              (move-to-window-line 0)
              (let ((window-min-height 1))
                (split-window nil (if (< search-slow-window-lines 0)
                                      (1+ (- search-slow-window-lines))
                                    (- (window-height)
                                       (1+ search-slow-window-lines)))))
              (if (< search-slow-window-lines 0)
                  (progn (vertical-motion (- 1 search-slow-window-lines))
                         (set-window-start (next-window) (point))
                         (set-window-hscroll (next-window)
                                             (window-hscroll))
                         (set-window-hscroll (selected-window) 0))
                (other-window 1))
633 634 635 636 637 638 639
              (goto-char found-point))
	  ;; Keep same hscrolling as at the start of the search when possible
	  (let ((current-scroll (window-hscroll)))
	    (set-window-hscroll (selected-window) isearch-start-hscroll)
	    (unless (pos-visible-in-window-p)
	      (set-window-hscroll (selected-window) current-scroll))))
	(if isearch-other-end
640 641 642 643 644
            (if (< isearch-other-end (point)) ; isearch-forward?
                (isearch-highlight isearch-other-end (point))
              (isearch-highlight (point) isearch-other-end))
          (isearch-dehighlight nil))
        ))
Jim Blandy's avatar
Jim Blandy committed
645 646 647
  (setq ;; quit-flag nil  not for isearch-mode
   isearch-adjusted nil
   isearch-yank-flag nil)
648 649 650 651
  (isearch-lazy-highlight-new-loop)
  ;; We must prevent the point moving to the end of composition when a
  ;; part of the composition has just been searched.
  (setq disable-point-adjustment t))
Jim Blandy's avatar
Jim Blandy committed
652

653
(defun isearch-done (&optional nopush edit)
654 655 656 657 658 659 660
  (if isearch-resume-enabled
      (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
				      ,isearch-word ,isearch-forward
				      ,isearch-message
				      ',isearch-case-fold-search)))
	(unless (equal (car command-history) command)
	  (setq command-history (cons command command-history)))))
661

662
  (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
663
  (remove-hook 'kbd-macro-termination-hook 'isearch-done)
664 665
  (setq isearch-lazy-highlight-start nil)

Jim Blandy's avatar
Jim Blandy committed
666
  ;; Called by all commands that terminate isearch-mode.
Richard M. Stallman's avatar
Richard M. Stallman committed
667
  ;; If NOPUSH is non-nil, we don't push the string on the search ring.
668
  (setq overriding-terminal-local-map nil)
669
  ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
670
  (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
671
  (isearch-dehighlight t)
672
  (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
673 674
  (let ((found-start (window-start (selected-window)))
	(found-point (point)))
675 676
    (if isearch-window-configuration
	(set-window-configuration isearch-window-configuration))
677

678 679 680
    (if isearch-small-window
	(goto-char found-point)
      ;; Exiting the save-window-excursion clobbers window-start; restore it.
681
      (set-window-start (selected-window) found-start t))
682

683 684 685
    ;; If there was movement, mark the starting position.
    ;; Maybe should test difference between and set mark iff > threshold.
    (if (/= (point) isearch-opoint)
686 687 688
	(or (and transient-mark-mode mark-active)
	    (progn
	      (push-mark isearch-opoint t)
689
	      (or executing-kbd-macro (> (minibuffer-depth) 0)
690
		  (message "Mark saved where search started"))))))
691 692

  (setq isearch-mode nil)
693 694 695 696
  (if isearch-input-method-local-p
      (setq input-method-function isearch-input-method-function)
    (kill-local-variable 'input-method-function))

697
  (force-mode-line-update)
Jim Blandy's avatar
Jim Blandy committed
698

699 700 701 702 703 704 705 706 707 708 709
  ;; If we ended in the middle of some intangible text,
  ;; move to the further end of that intangible text.
  (let ((after (if (eobp) nil
		 (get-text-property (point) 'intangible)))
	(before (if (bobp) nil
		  (get-text-property (1- (point)) 'intangible))))
    (when (and before after (eq before after))
      (if isearch-forward
	  (goto-char (next-single-property-change (point) 'intangible))
	(goto-char (previous-single-property-change (point) 'intangible)))))

Richard M. Stallman's avatar
Richard M. Stallman committed
710
  (if (and (> (length isearch-string) 0) (not nopush))
Jim Blandy's avatar
Jim Blandy committed
711
      ;; Update the ring data.
712
      (isearch-update-ring isearch-string isearch-regexp))
Jim Blandy's avatar
Jim Blandy committed
713 714

  (run-hooks 'isearch-mode-end-hook)
715
  (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
716

717 718 719
(defun isearch-update-ring (string &optional regexp)
  "Add STRING to the beginning of the search ring.
REGEXP says which ring to use."
720
  (if regexp
721
      (if (or (null regexp-search-ring)
722
	      (not (string= string (car regexp-search-ring))))
723
	  (progn
724
	    (push string regexp-search-ring)
725 726 727 728
	    (if (> (length regexp-search-ring) regexp-search-ring-max)
		(setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
			nil))))
    (if (or (null search-ring)
729
	    (not (string= string (car search-ring))))
730
	(progn
731
	  (push string search-ring)
732 733 734
	  (if (> (length search-ring) search-ring-max)
	      (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))

735 736 737 738 739 740 741 742
;; Switching buffers should first terminate isearch-mode.
;; ;; For Emacs 19, the frame switch event is handled.
;; (defun isearch-switch-frame-handler ()
;;   (interactive) ;; Is this necessary?
;;   ;; First terminate isearch-mode.
;;   (isearch-done)
;;   (isearch-clean-overlays)
;;   (handle-switch-frame (car (cdr last-command-char))))
743

744 745 746

;; The search status structure and stack.

747
(defsubst isearch-string-state (frame)
748
  "Return the search string in FRAME."
749
  (aref frame 0))
750
(defsubst isearch-message-state (frame)
751
  "Return the search string to display to the user in FRAME."
752
  (aref frame 1))
753
(defsubst isearch-point-state (frame)
754
  "Return the point in FRAME."
755
  (aref frame 2))
756
(defsubst isearch-success-state (frame)
757
  "Return the success flag in FRAME."
758
  (aref frame 3))
759
(defsubst isearch-forward-state (frame)
760
  "Return the searching-forward flag in FRAME."
761
  (aref frame 4))
762
(defsubst isearch-other-end-state (frame)
763
  "Return the other end of the match in FRAME."
764
  (aref frame 5))
765
(defsubst isearch-word-state (frame)
766
  "Return the search-by-word flag in FRAME."
767
  (aref frame 6))
768
(defsubst isearch-invalid-regexp-state (frame)
769
  "Return the regexp error message in FRAME, or nil if its regexp is valid."
770
  (aref frame 7))
771
(defsubst isearch-wrapped-state (frame)
772
  "Return the search-wrapped flag in FRAME."
773
  (aref frame 8))
774
(defsubst isearch-barrier-state (frame)
775
  "Return the barrier value in FRAME."
776
  (aref frame 9))
777
(defsubst isearch-within-brackets-state (frame)
778
  "Return the in-character-class flag in FRAME."
779
  (aref frame 10))
780
(defsubst isearch-case-fold-search-state (frame)
781
  "Return the case-folding flag in FRAME."
782
  (aref frame 11))
783 784 785
(defsubst isearch-pop-fun-state (frame)
  "Return the function restoring the mode-specific isearch state in FRAME."
  (aref frame 12))
786 787 788

(defun isearch-top-state ()
  (let ((cmd (car isearch-cmds)))
789 790 791 792 793 794 795 796 797 798 799
    (setq isearch-string (isearch-string-state cmd)
	  isearch-message (isearch-message-state cmd)
	  isearch-success (isearch-success-state cmd)
	  isearch-forward (isearch-forward-state cmd)
	  isearch-other-end (isearch-other-end-state cmd)
	  isearch-word (isearch-word-state cmd)
	  isearch-invalid-regexp (isearch-invalid-regexp-state cmd)
	  isearch-wrapped (isearch-wrapped-state cmd)
	  isearch-barrier (isearch-barrier-state cmd)
	  isearch-within-brackets (isearch-within-brackets-state cmd)
	  isearch-case-fold-search (isearch-case-fold-search-state cmd))
800 801
    (if (functionp (isearch-pop-fun-state cmd))
	(funcall (isearch-pop-fun-state cmd) cmd))
802
    (goto-char (isearch-point-state cmd))))
803 804 805 806 807 808 809 810 811 812 813

(defun isearch-pop-state ()
  (setq isearch-cmds (cdr isearch-cmds))
  (isearch-top-state))

(defun isearch-push-state ()
  (setq isearch-cmds
	(cons (vector isearch-string isearch-message (point)
		      isearch-success isearch-forward isearch-other-end
		      isearch-word
		      isearch-invalid-regexp isearch-wrapped isearch-barrier
814 815 816
		      isearch-within-brackets isearch-case-fold-search
		      (if isearch-push-state-function
			  (funcall isearch-push-state-function)))
817 818
	      isearch-cmds)))

Jim Blandy's avatar
Jim Blandy committed
819 820 821 822 823 824 825

;; Commands active while inside of the isearch minor mode.

(defun isearch-exit ()
  "Exit search normally.
However, if this is the first command after starting incremental
search and `search-nonincremental-instead' is non-nil, do a
826
nonincremental search instead via `isearch-edit-string'."
Jim Blandy's avatar
Jim Blandy committed
827
  (interactive)
828
  (if (and search-nonincremental-instead
Jim Blandy's avatar
Jim Blandy committed
829
	   (= 0 (length isearch-string)))
830 831
      (let ((isearch-nonincremental t))
	(isearch-edit-string)))
832 833
  (isearch-done)
  (isearch-clean-overlays))
Jim Blandy's avatar
Jim Blandy committed
834 835 836


(defun isearch-edit-string ()
837 838 839 840 841 842
  "Edit the search string in the minibuffer.
The following additional command keys are active while editing.
\\<minibuffer-local-isearch-map>
\\[exit-minibuffer] to resume incremental searching with the edited string.
\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
\\[isearch-forward-exit-minibuffer] to resume isearching forward.
Karl Heuer's avatar
Karl Heuer committed
843
\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
844
\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
845
\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
846
\\[isearch-complete-edit] to complete the search string using the search ring.
Karl Heuer's avatar
Karl Heuer committed
847
\\<isearch-mode-map>
848
If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
849 850 851 852 853

  ;; This code is very hairy for several reasons, explained in the code.
  ;; Mainly, isearch-mode must be terminated while editing and then restarted.
  ;; If there were a way to catch any change of buffer from the minibuffer,
  ;; this could be simplified greatly.
854
  ;; Editing doesn't back up the search point.  Should it?
Jim Blandy's avatar
Jim Blandy committed
855
  (interactive)
856
  (condition-case err
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890
      (progn
	(let ((isearch-nonincremental isearch-nonincremental)

	      ;; Locally bind all isearch global variables to protect them
	      ;; from recursive isearching.
	      ;; isearch-string -message and -forward are not bound
	      ;; so they may be changed.  Instead, save the values.
	      (isearch-new-string isearch-string)
	      (isearch-new-message isearch-message)
	      (isearch-new-forward isearch-forward)
	      (isearch-new-word isearch-word)

	      (isearch-regexp isearch-regexp)
	      (isearch-op-fun isearch-op-fun)
	      (isearch-cmds isearch-cmds)
	      (isearch-success isearch-success)
	      (isearch-wrapped isearch-wrapped)
	      (isearch-barrier isearch-barrier)
	      (isearch-adjusted isearch-adjusted)
	      (isearch-yank-flag isearch-yank-flag)
	      (isearch-invalid-regexp isearch-invalid-regexp)
	      (isearch-within-brackets isearch-within-brackets)
  ;;; Don't bind this.  We want isearch-search, below, to set it.
  ;;; And the old value won't matter after that.
  ;;;	    (isearch-other-end isearch-other-end)
  ;;; Perhaps some of these other variables should be bound for a
  ;;; shorter period, ending before the next isearch-search.
  ;;; But there doesn't seem to be a real bug, so let's not risk it now.
	      (isearch-opoint isearch-opoint)
	      (isearch-slow-terminal-mode isearch-slow-terminal-mode)
	      (isearch-small-window isearch-small-window)
	      (isearch-recursive-edit isearch-recursive-edit)
	      ;; Save current configuration so we can restore it here.
	      (isearch-window-configuration (current-window-configuration))
891 892 893 894 895 896

	      ;; Temporarily restore `minibuffer-message-timeout'.
	      (minibuffer-message-timeout
	       isearch-original-minibuffer-message-timeout)
	      (isearch-original-minibuffer-message-timeout
	       isearch-original-minibuffer-message-timeout)
897 898 899
	      )

	  ;; Actually terminate isearching until editing is done.
900
	  ;; This is so that the user can do anything without failure,
901 902 903 904 905 906 907
	  ;; like switch buffers and start another isearch, and return.
	  (condition-case err
	      (isearch-done t t)
	    (exit nil))			; was recursive editing

	  (isearch-message) ;; for read-char
	  (unwind-protect
908
	      (let* (;; Why does following read-char echo?
909 910 911 912 913 914 915 916 917 918 919 920 921 922
		     ;;(echo-keystrokes 0) ;; not needed with above message
		     (e (let ((cursor-in-echo-area t))
			  (read-event)))
		     ;; Binding minibuffer-history-symbol to nil is a work-around
		     ;; for some incompatibility with gmhist.
		     (minibuffer-history-symbol)
		     (message-log-max nil))
		;; If the first character the user types when we prompt them
		;; for a string is the yank-word character, then go into
		;; word-search mode.  Otherwise unread that character and
		;; read a key the normal way.
		;; Word search does not apply (yet) to regexp searches,
		;; no check is made here.
		(message (isearch-message-prefix nil nil t))
Richard M. Stallman's avatar
Richard M. Stallman committed
923 924 925
		(if (memq (lookup-key isearch-mode-map (vector e))
			  '(isearch-yank-word
			    isearch-yank-word-or-char))
926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941
		    (setq isearch-word t;; so message-prefix is right
			  isearch-new-word t)
		  (cancel-kbd-macro-events)
		  (isearch-unread e))
		(setq cursor-in-echo-area nil)
		(setq isearch-new-string
		      (let (junk-ring)
			(read-from-minibuffer
			 (isearch-message-prefix nil nil isearch-nonincremental)
			 isearch-string
			 minibuffer-local-isearch-map nil
			 'junk-ring))
		      isearch-new-message
		      (mapconcat 'isearch-text-char-description
				 isearch-new-string "")))
	    ;; Always resume isearching by restarting it.
942 943 944
	    (isearch-mode isearch-forward
			  isearch-regexp
			  isearch-op-fun
945 946 947 948 949 950 951 952 953 954 955 956 957 958
			  nil
			  isearch-word)

	    ;; Copy new local values to isearch globals
	    (setq isearch-string isearch-new-string
		  isearch-message isearch-new-message
		  isearch-forward isearch-new-forward
		  isearch-word isearch-new-word))

	  ;; Empty isearch-string means use default.
	  (if (= 0 (length isearch-string))
	      (setq isearch-string (or (car (if isearch-regexp
						regexp-search-ring
					      search-ring))
959 960 961 962 963
				       "")

		    isearch-message
		    (mapconcat 'isearch-text-char-description
			       isearch-string ""))
964 965 966 967 968 969 970
	    ;; This used to set the last search string,
	    ;; but I think it is not right to do that here.
	    ;; Only the string actually used should be saved.
	    ))

	;; Push the state as of before this C-s.
	(isearch-push-state)
971 972 973 974

	;; Reinvoke the pending search.
	(isearch-search)
	(isearch-update)
975
	(if isearch-nonincremental
976 977 978
	    (progn
	      ;; (sit-for 1) ;; needed if isearch-done does: (message "")
	      (isearch-done))))
Jim Blandy's avatar
Jim Blandy committed
979

980 981 982 983 984 985 986 987 988 989 990 991 992
    (quit  ; handle abort-recursive-edit
     (isearch-abort)  ;; outside of let to restore outside global values
     )))

(defun isearch-nonincremental-exit-minibuffer ()
  (interactive)
  (setq isearch-nonincremental t)
  (exit-minibuffer))

(defun isearch-forward-exit-minibuffer ()
  (interactive)
  (setq isearch-new-forward t)
  (exit-minibuffer))
Jim Blandy's avatar
Jim Blandy committed
993

994
(defun isearch-reverse-exit-minibuffer ()
Jim Blandy's avatar
Jim Blandy committed
995
  (interactive)
996 997 998
  (setq isearch-new-forward nil)
  (exit-minibuffer))

999 1000 1001
(defun isearch-cancel ()
  "Terminate the search and go back to the starting point."
  (interactive)
1002 1003 1004
  (if (functionp (isearch-pop-fun-state (car (last isearch-cmds))))
      (funcall (isearch-pop-fun-state (car (last isearch-cmds)))
               (car (last isearch-cmds))))
1005
  (goto-char isearch-opoint)
1006
  (isearch-done t)                      ; exit isearch
1007
  (isearch-clean-overlays)
1008
  (signal 'quit nil))                   ; and pass on quit signal
1009 1010

(defun isearch-abort ()
1011
  "Abort incremental search mode if searching is successful, signaling quit.
1012
Otherwise, revert to previous successful search and continue searching.
1013
Use `isearch-exit' to quit without signaling."
1014
  (interactive)
1015
;;  (ding)  signal instead below, if quitting
Jim Blandy's avatar
Jim Blandy committed
1016 1017 1018 1019
  (discard-input)
  (if isearch-success
      ;; If search is successful, move back to starting point
      ;; and really do quit.
1020 1021 1022
      (progn
        (setq isearch-success nil)
        (isearch-cancel))
1023 1024 1025 1026
    ;; If search is failing, or has an incomplete regexp,
    ;; rub out until it is once more successful.
    (while (or (not isearch-success) isearch-invalid-regexp)
      (isearch-pop-state))
Jim Blandy's avatar
Jim Blandy committed
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
    (isearch-update)))

(defun isearch-repeat (direction)
  ;; Utility for isearch-repeat-forward and -backward.
  (if (eq isearch-forward (eq direction 'forward))
      ;; C-s in forward or C-r in reverse.
      (if (equal isearch-string "")
	  ;; If search string is empty, use last one.
	  (setq isearch-string
		(or (if isearch-regexp
1037 1038
			(car regexp-search-ring)
		      (car search-ring))