(update_menu_bar): Call set_frame_menubar with proper buffer current.
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
76550a57 1;;; isearch.el --- incremental search minor mode.
3b1e4dd1 2
f8c25f1b 3;; Copyright (C) 1992, 1993, 1994, 1995 Free Software Foundation, Inc.
3a801d0c 4
3b1e4dd1
ER
5;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6
79ce455d 7;; |$Date: 1995/04/25 22:28:40 $|$Revision: 1.90 $
8e1cae6d 8
54d2ecd3 9;; This file is part of GNU Emacs.
8e1cae6d 10
59243403
RS
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
8e1cae6d 16;; GNU Emacs is distributed in the hope that it will be useful,
59243403
RS
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to
23;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
8e1cae6d 24
3b1e4dd1
ER
25;;; Commentary:
26
8e1cae6d
JB
27;;;====================================================================
28;; Instructions
29
08200510
RS
30;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
31;; isearch-mode behaves modally and does not return until the search
32;; is completed. It uses a recursive-edit to behave this way. Note:
33;; gnus does it wrong: (call-interactively 'isearch-forward).
34
8e1cae6d 35;; The key bindings active within isearch-mode are defined below in
08200510
RS
36;; `isearch-mode-map' which is given bindings close to the default
37;; characters of the original isearch.el. With `isearch-mode',
38;; however, you can bind multi-character keys and it should be easier
39;; to add new commands. One bug though: keys with meta-prefix cannot
40;; be longer than two chars. Also see minibuffer-local-isearch-map
41;; for bindings active during `isearch-edit-string'.
42
43;; Note to emacs version 19 users: isearch-mode should work even if
44;; you switch windows with the mouse, in which case isearch-mode is
45;; terminated automatically before the switch. This is true of lemacs
46;; too, with a few more cleanups I've neglected in this release.
47;; No one has supplied patches for epoch yet.
48
49;; The search ring and completion commands automatically put you in
50;; the minibuffer to edit the string. This gives you a chance to
51;; modify the search string before executing the search. There are
52;; three commands to terminate the editing: C-s and C-r exit the
53;; minibuffer and search forward and reverse respectively, while C-m
54;; exits and does a nonincremental search.
55
56;; Exiting immediately from isearch uses isearch-edit-string instead
57;; of nonincremental-search, if search-nonincremental-instead is non-nil.
58;; The name of this option should probably be changed if we decide to
59;; keep the behavior. No point in forcing nonincremental search until
60;; the last possible moment.
61
62;; TODO
eb8c3be9 63;; - Integrate the emacs 19 generalized command history.
08200510
RS
64;; - Think about incorporating query-replace.
65;; - Hooks and options for failed search.
282d89c0 66
3b1e4dd1
ER
67;;; Change Log:
68
8cb2ceaa
RS
69;;; Changes before those recorded in ChangeLog:
70
08200510
RS
71;;; Revision 1.4 92/09/14 16:26:02 liberte
72;;; Added prefix args to isearch-forward, etc. to switch between
73;;; string and regular expression searching.
74;;; Added some support for lemacs.
75;;; Added general isearch-highlight option - but only for lemacs so far.
76;;; Added support for frame switching in emacs 19.
77;;; Added word search option to isearch-edit-string.
78;;; Renamed isearch-quit to isearch-abort.
79;;; Numerous changes to comments and doc strings.
282d89c0 80;;;
08200510
RS
81;;; Revision 1.3 92/06/29 13:10:08 liberte
82;;; Moved modal isearch-mode handling into isearch-mode.
83;;; Got rid of buffer-local isearch variables.
84;;; isearch-edit-string used by ring adjustments, completion, and
85;;; nonincremental searching. C-s and C-r are additional exit commands.
86;;; Renamed all regex to regexp.
87;;; Got rid of found-start and found-point globals.
88;;; Generalized handling of upper-case chars.
89
90;;; Revision 1.2 92/05/27 11:33:57 liberte
91;;; Emacs version 19 has a search ring, which is supported here.
92;;; Other fixes found in the version 19 isearch are included here.
93;;;
94;;; Also see variables search-caps-disable-folding,
95;;; search-nonincremental-instead, search-whitespace-regexp, and
96;;; commands isearch-toggle-regexp, isearch-edit-string.
97;;;
98;;; semi-modal isearching is supported.
282d89c0
ER
99
100;;; Changes for 1.1
101;;; 3/18/92 Fixed invalid-regexp.
102;;; 3/18/92 Fixed yanking in regexps.
103
3b1e4dd1 104;;; Code:
08200510
RS
105
106\f
58451992
RS
107;;;========================================================================
108;;; Some additional options and constants.
08200510 109
fdf5afa4 110(defconst search-exit-option t
a8f783b2 111 "*Non-nil means random control characters terminate incremental search.")
fdf5afa4
RS
112
113(defvar search-slow-window-lines 1
114 "*Number of lines in slow search display windows.
115These are the short windows used during incremental search on slow terminals.
116Negative means put the slow search window at the top (normally it's at bottom)
117and the value is minus the number of lines.")
118
119(defvar search-slow-speed 1200
120 "*Highest terminal speed at which to use \"slow\" style incremental search.
121This is the style where a one-line window is created to show the line
122that the search has reached.")
8e1cae6d 123
b78559b0 124(defvar search-upper-case 'not-yanks
8e1cae6d 125 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
126That is, upper and lower case chars must match exactly.
127This applies no matter where the chars come from, but does not
fdf5afa4
RS
128apply to chars in regexps that are prefixed with `\\'.
129If this value is `not-yanks', yanked text is always downcased.")
8e1cae6d
JB
130
131(defvar search-nonincremental-instead t
132 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510
RS
133Actually, `isearch-edit-string' is called to let you enter the search
134string, and RET terminates editing and does a nonincremental search.")
8e1cae6d
JB
135
136(defconst search-whitespace-regexp "\\s-+"
137 "*If non-nil, regular expression to match a sequence of whitespace chars.
138You might want to use something like \"[ \\t\\r\\n]+\" instead.")
139
08200510 140(defvar search-highlight nil
b78559b0 141 "*Non-nil means incremental search highlights the current match.")
08200510
RS
142
143(defvar isearch-mode-hook nil
144 "Function(s) to call after starting up an incremental search.")
145
146(defvar isearch-mode-end-hook nil
147 "Function(s) to call after terminating an incremental search.")
148
8e1cae6d
JB
149;;;==================================================================
150;;; Search ring.
8e1cae6d
JB
151
152(defvar search-ring nil
153 "List of search string sequences.")
08200510 154(defvar regexp-search-ring nil
8e1cae6d
JB
155 "List of regular expression search string sequences.")
156
157(defconst search-ring-max 16
158 "*Maximum length of search ring before oldest elements are thrown away.")
08200510
RS
159(defconst regexp-search-ring-max 16
160 "*Maximum length of regexp search ring before oldest elements are thrown away.")
8e1cae6d
JB
161
162(defvar search-ring-yank-pointer nil
a5dcf63f
RS
163 "Index in `search-ring' of last string reused.
164nil if none yet.")
08200510 165(defvar regexp-search-ring-yank-pointer nil
a5dcf63f
RS
166 "Index in `regexp-search-ring' of last string reused.
167nil if none yet.")
8e1cae6d 168
08200510
RS
169(defvar search-ring-update nil
170 "*Non-nil if advancing or retreating in the search ring should cause search.
171Default value, nil, means edit the string instead.")
172
8e1cae6d
JB
173;;;====================================================
174;;; Define isearch-mode keymap.
175
176(defvar isearch-mode-map nil
177 "Keymap for isearch-mode.")
178
08200510
RS
179(or isearch-mode-map
180 (let* ((i 0)
fdf5afa4 181 (map (make-keymap)))
fcf8ba15
RS
182 (or (vectorp (nth 1 map))
183 (error "The initialization of isearch-mode-map must be updated"))
184 ;; Give this map a vector 256 long, for dense binding
185 ;; of a larger range of ordinary characters.
186 (setcar (cdr map) (make-vector 256 nil))
08200510 187
ac689515
RS
188 ;; Make function keys, etc, exit the search.
189 (define-key map [t] 'isearch-other-control-char)
08200510 190 ;; Control chars, by default, end isearch mode transparently.
ac689515
RS
191 ;; We need these explicit definitions because, in a dense keymap,
192 ;; the binding for t does not affect characters.
193 ;; We use a dense keymap to save space.
08200510
RS
194 (while (< i ?\ )
195 (define-key map (make-string 1 i) 'isearch-other-control-char)
196 (setq i (1+ i)))
197
8cb2ceaa 198 ;; Printing chars extend the search string by default.
ac689515 199 (setq i ?\ )
8d7e4e80 200 (while (< i (length (nth 1 map)))
8cb2ceaa 201 (define-key map (vector i) 'isearch-printing-char)
08200510
RS
202 (setq i (1+ i)))
203
ec121bad
RS
204 ;; To handle local bindings with meta char prefix keys, define
205 ;; another full keymap. This must be done for any other prefix
206 ;; keys as well, one full keymap per char of the prefix key. It
207 ;; would be simpler to disable the global keymap, and/or have a
208 ;; default local key binding for any key not otherwise bound.
209 (let ((meta-map (make-sparse-keymap)))
210 (define-key map (char-to-string meta-prefix-char) meta-map)
211 (define-key map [escape] meta-map))
212 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
213
08200510
RS
214 ;; Several non-printing chars change the searching behavior.
215 (define-key map "\C-s" 'isearch-repeat-forward)
216 (define-key map "\C-r" 'isearch-repeat-backward)
217 (define-key map "\177" 'isearch-delete-char)
218 (define-key map "\C-g" 'isearch-abort)
ec121bad
RS
219 ;; This assumes \e is the meta-prefix-char.
220 (or (= ?\e meta-prefix-char)
221 (error "Inconsistency in isearch.el"))
19993c54
RS
222 (define-key map "\e\e\e" 'isearch-cancel)
223 (define-key map [escape escape escape] 'isearch-cancel)
8e1cae6d 224
08200510
RS
225 (define-key map "\C-q" 'isearch-quote-char)
226
08200510
RS
227 (define-key map "\r" 'isearch-exit)
228 (define-key map "\C-j" 'isearch-printing-char)
229 (define-key map "\t" 'isearch-printing-char)
230 (define-key map " " 'isearch-whitespace-chars)
8e1cae6d 231
08200510
RS
232 (define-key map "\C-w" 'isearch-yank-word)
233 (define-key map "\C-y" 'isearch-yank-line)
234
eccd3117
RS
235 ;; Bind the ASCII-equivalent "function keys" explicitly to nil
236 ;; so that the default binding does not apply.
237 ;; As a result, these keys translate thru function-key-map
238 ;; as normal, and they have the effect of the equivalent ASCII char.
e7cb0106 239 ;; We bind [escape] below.
eccd3117
RS
240 (define-key map [tab] 'nil)
241 (define-key map [kp-0] 'nil)
242 (define-key map [kp-1] 'nil)
243 (define-key map [kp-2] 'nil)
244 (define-key map [kp-3] 'nil)
245 (define-key map [kp-4] 'nil)
246 (define-key map [kp-5] 'nil)
247 (define-key map [kp-6] 'nil)
248 (define-key map [kp-7] 'nil)
249 (define-key map [kp-8] 'nil)
250 (define-key map [kp-9] 'nil)
251 (define-key map [kp-add] 'nil)
252 (define-key map [kp-subtract] 'nil)
253 (define-key map [kp-multiply] 'nil)
254 (define-key map [kp-divide] 'nil)
255 (define-key map [kp-decimal] 'nil)
256 (define-key map [kp-separator] 'nil)
257 (define-key map [kp-equal] 'nil)
258 (define-key map [kp-tab] 'nil)
259 (define-key map [kp-space] 'nil)
260 (define-key map [kp-enter] 'nil)
261 (define-key map [delete] 'nil)
262 (define-key map [backspace] 'nil)
263 (define-key map [return] 'nil)
264 (define-key map [newline] 'nil)
e7cb0106 265
08200510
RS
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)
271
58f1634a
RS
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)
08200510 275
08200510
RS
276 (define-key map "\M-n" 'isearch-ring-advance)
277 (define-key map "\M-p" 'isearch-ring-retreat)
30d47262 278 (define-key map "\M-y" 'isearch-yank-kill)
1a3a6707 279
08200510
RS
280 (define-key map "\M-\t" 'isearch-complete)
281
58451992
RS
282 ;; Switching frames should terminate isearch-mode
283 (define-key map [switch-frame] 'isearch-switch-frame-handler)
08200510
RS
284
285 (setq isearch-mode-map map)
286 ))
8e1cae6d 287
08200510
RS
288;; Some bindings you may want to put in your isearch-mode-hook.
289;; Suggest some alternates...
4453091d 290;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-case-fold)
08200510
RS
291;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp)
292;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string)
8e1cae6d 293
8e1cae6d 294
08200510
RS
295(defvar minibuffer-local-isearch-map nil
296 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 297
08200510
RS
298(or minibuffer-local-isearch-map
299 (let ((map (copy-keymap minibuffer-local-map)))
300 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
35a4d143
RS
301 (define-key map "\M-n" 'isearch-ring-advance-edit)
302 (define-key map "\M-p" 'isearch-ring-retreat-edit)
08200510
RS
303 (define-key map "\M-\t" 'isearch-complete-edit)
304 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
305 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
306 (setq minibuffer-local-isearch-map map)
307 ))
8e1cae6d
JB
308
309;;;========================================================
310;; Internal variables declared globally for byte-compiler.
08200510
RS
311;; These are all set with setq while isearching
312;; and bound locally while editing the search string.
313
314(defvar isearch-forward nil) ; Searching in the forward direction.
315(defvar isearch-regexp nil) ; Searching for a regexp.
316(defvar isearch-word nil) ; Searching for words.
317
318(defvar isearch-cmds nil) ; Stack of search status sets.
319(defvar isearch-string "") ; The current search string.
320(defvar isearch-message "") ; text-char-description version of isearch-string
321
322(defvar isearch-success t) ; Searching is currently successful.
323(defvar isearch-invalid-regexp nil) ; Regexp not well formed.
c5f6b356 324(defvar isearch-within-brackets nil) ; Regexp has unclosed [.
08200510
RS
325(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
326(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d
JB
327(defvar isearch-barrier 0)
328
4453091d
RS
329; case-fold-search while searching.
330; either nil, t, or 'yes. 'yes means the same as t except that mixed
331; case in the search string is ignored.
332(defvar isearch-case-fold-search nil)
8e1cae6d
JB
333
334(defvar isearch-adjusted nil)
335(defvar isearch-slow-terminal-mode nil)
08200510
RS
336;;; If t, using a small window.
337(defvar isearch-small-window nil)
8e1cae6d 338(defvar isearch-opoint 0)
08200510
RS
339;;; The window configuration active at the beginning of the search.
340(defvar isearch-window-configuration nil)
8e1cae6d 341
08200510
RS
342;; Flag to indicate a yank occurred, so don't move the cursor.
343(defvar isearch-yank-flag nil)
8e1cae6d 344
08200510
RS
345;;; A function to be called after each input character is processed.
346;;; (It is not called after characters that exit the search.)
347;;; It is only set from an optional argument to `isearch-mode'.
348(defvar isearch-op-fun nil)
8e1cae6d 349
08200510
RS
350;;; Is isearch-mode in a recursive edit for modal searching.
351(defvar isearch-recursive-edit nil)
8e1cae6d 352
08200510
RS
353;;; Should isearch be terminated after doing one search?
354(defvar isearch-nonincremental nil)
355
356;; New value of isearch-forward after isearch-edit-string.
357(defvar isearch-new-forward nil)
8e1cae6d 358
8e1cae6d
JB
359
360;;;==============================================================
361;; Minor-mode-alist changes - kind of redundant with the
362;; echo area, but if isearching in multiple windows, it can be useful.
363
364(or (assq 'isearch-mode minor-mode-alist)
365 (nconc minor-mode-alist
366 (list '(isearch-mode isearch-mode))))
367
08200510 368(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
369(make-variable-buffer-local 'isearch-mode)
370
fdf5afa4
RS
371(define-key global-map "\C-s" 'isearch-forward)
372(define-key esc-map "\C-s" 'isearch-forward-regexp)
373(define-key global-map "\C-r" 'isearch-backward)
374(define-key esc-map "\C-r" 'isearch-backward-regexp)
375
8e1cae6d
JB
376;;;===============================================================
377;;; Entry points to isearch-mode.
08200510 378;;; These four functions should replace those in loaddefs.el
b457cc3b 379;;; An alternative is to defalias isearch-forward etc to isearch-mode,
08200510 380;;; and look at this-command to set the options accordingly.
8e1cae6d 381
eceee2c0 382(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
383 "\
384Do incremental search forward.
08200510
RS
385With a prefix argument, do an incremental regular expression search instead.
386\\<isearch-mode-map>
8e1cae6d 387As you type characters, they add to the search string and are found.
08200510 388The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 389
08200510 390Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 391Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
392Type LFD (C-j) to match end of line.
393Type \\[isearch-repeat-forward] to search again forward,\
394 \\[isearch-repeat-backward] to search again backward.
395Type \\[isearch-yank-word] to yank word from buffer onto end of search\
396 string and search for it.
397Type \\[isearch-yank-line] to yank rest of line onto end of search string\
398 and search for it.
8e1cae6d 399Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
400\\[isearch-abort] while searching or when search has failed cancels input\
401 back to what has
402 been found successfully.
403\\[isearch-abort] when search is successful aborts and moves point to\
404 starting point.
8e1cae6d
JB
405
406Also supported is a search ring of the previous 16 search strings.
407Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
408Type \\[isearch-ring-retreat] to search for the previous item in the search\
409 ring.
410Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 411
08200510
RS
412The above keys, bound in `isearch-mode-map', are often controlled by
413 options; do M-x apropos on search-.* to find them.
8e1cae6d 414Other control and meta characters terminate the search
08200510 415 and are then executed normally (depending on `search-exit-option').
8e1cae6d 416
08200510
RS
417If this function is called non-interactively, it does not return to
418the calling function until the search is done."
8e1cae6d 419
eceee2c0 420 (interactive "P\np")
4ae7d00a 421 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 422
eceee2c0 423(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
424 "\
425Do incremental search forward for regular expression.
08200510 426With a prefix argument, do a regular string search instead.
8e1cae6d
JB
427Like ordinary incremental search except that your input
428is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 429 (interactive "P\np")
4ae7d00a 430 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 431
eceee2c0 432(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
433 "\
434Do incremental search backward.
08200510 435With a prefix argument, do a regular expression search instead.
8e1cae6d 436See \\[isearch-forward] for more information."
eceee2c0 437 (interactive "P\np")
4ae7d00a 438 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 439
eceee2c0 440(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
441 "\
442Do incremental search backward for regular expression.
08200510 443With a prefix argument, do a regular string search instead.
8e1cae6d
JB
444Like ordinary incremental search except that your input
445is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 446 (interactive "P\np")
4ae7d00a 447 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
448
449
450(defun isearch-mode-help ()
451 (interactive)
452 (describe-function 'isearch-forward)
453 (isearch-update))
8e1cae6d
JB
454
455\f
456;;;==================================================================
457;; isearch-mode only sets up incremental search for the minor mode.
458;; All the work is done by the isearch-mode commands.
459
08200510
RS
460;; Not used yet:
461;;(defconst isearch-commands '(isearch-forward isearch-backward
462;; isearch-forward-regexp isearch-backward-regexp)
463;; "List of commands for which isearch-mode does not recursive-edit.")
464
465
466(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
467 "Start isearch minor mode. Called by `isearch-forward', etc.
468
469\\{isearch-mode-map}"
8e1cae6d
JB
470
471 ;; Initialize global vars.
472 (setq isearch-forward forward
473 isearch-regexp regexp
08200510 474 isearch-word word-p
8e1cae6d
JB
475 isearch-op-fun op-fun
476 isearch-case-fold-search case-fold-search
477 isearch-string ""
478 isearch-message ""
479 isearch-cmds nil
480 isearch-success t
481 isearch-wrapped nil
482 isearch-barrier (point)
483 isearch-adjusted nil
484 isearch-yank-flag nil
485 isearch-invalid-regexp nil
c5f6b356 486 isearch-within-brackets nil
0cabad13 487 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d
JB
488 (> (window-height)
489 (* 4 search-slow-window-lines)))
490 isearch-other-end nil
491 isearch-small-window nil
8e1cae6d 492
8e1cae6d 493 isearch-opoint (point)
a5dcf63f
RS
494 search-ring-yank-pointer nil
495 regexp-search-ring-yank-pointer nil)
292a8dff
RS
496 (setq isearch-window-configuration
497 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 498
8e1cae6d 499 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 500 (force-mode-line-update)
8e1cae6d
JB
501
502 (isearch-push-state)
503
849cfd72
RS
504 (make-local-variable 'overriding-local-map)
505 (setq overriding-local-map isearch-mode-map)
8e1cae6d
JB
506 (isearch-update)
507 (run-hooks 'isearch-mode-hook)
08200510 508
6e5cd0ae
RS
509 (setq mouse-leave-buffer-hook '(isearch-done))
510
08200510
RS
511 ;; isearch-mode can be made modal (in the sense of not returning to
512 ;; the calling function until searching is completed) by entering
513 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
514 (if recursive-edit
515 (let ((isearch-recursive-edit t))
516 (recursive-edit)))
0daa17f3 517 isearch-success)
8e1cae6d
JB
518
519
520;;;====================================================
521;; Some high level utilities. Others below.
522
523(defun isearch-update ()
524 ;; Called after each command to update the display.
58451992 525 (if (null unread-command-events)
8e1cae6d
JB
526 (progn
527 (if (not (input-pending-p))
528 (isearch-message))
529 (if (and isearch-slow-terminal-mode
530 (not (or isearch-small-window
531 (pos-visible-in-window-p))))
08200510 532 (let ((found-point (point)))
8e1cae6d 533 (setq isearch-small-window t)
8e1cae6d
JB
534 (move-to-window-line 0)
535 (let ((window-min-height 1))
536 (split-window nil (if (< search-slow-window-lines 0)
537 (1+ (- search-slow-window-lines))
538 (- (window-height)
539 (1+ search-slow-window-lines)))))
540 (if (< search-slow-window-lines 0)
541 (progn (vertical-motion (- 1 search-slow-window-lines))
542 (set-window-start (next-window) (point))
543 (set-window-hscroll (next-window)
544 (window-hscroll))
545 (set-window-hscroll (selected-window) 0))
546 (other-window 1))
08200510
RS
547 (goto-char found-point)))
548 (if isearch-other-end
549 (if (< isearch-other-end (point)) ; isearch-forward?
550 (isearch-highlight isearch-other-end (point))
99afb671
RS
551 (isearch-highlight (point) isearch-other-end))
552 (isearch-dehighlight nil))
08200510 553 ))
8e1cae6d
JB
554 (setq ;; quit-flag nil not for isearch-mode
555 isearch-adjusted nil
556 isearch-yank-flag nil)
557 )
558
0daa17f3 559(defun isearch-done (&optional nopush edit)
6e5cd0ae 560 (setq mouse-leave-buffer-hook nil)
8e1cae6d 561 ;; Called by all commands that terminate isearch-mode.
35a4d143 562 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
849cfd72 563 (setq overriding-local-map nil)
08200510
RS
564 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
565 (isearch-dehighlight t)
566 (let ((found-start (window-start (selected-window)))
567 (found-point (point)))
70418205
RS
568 (if isearch-window-configuration
569 (set-window-configuration isearch-window-configuration))
08200510 570
d49b6338
RS
571 (if isearch-small-window
572 (goto-char found-point)
573 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 574 (set-window-start (selected-window) found-start t))
d49b6338 575
08200510
RS
576 ;; If there was movement, mark the starting position.
577 ;; Maybe should test difference between and set mark iff > threshold.
578 (if (/= (point) isearch-opoint)
9821535a
RS
579 (or (and transient-mark-mode mark-active)
580 (progn
581 (push-mark isearch-opoint t)
582 (or executing-macro (> (minibuffer-depth) 0)
583 (message "Mark saved where search started"))))))
08200510
RS
584
585 (setq isearch-mode nil)
1cd3732c 586 (force-mode-line-update)
8e1cae6d 587
35a4d143 588 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 589 ;; Update the ring data.
73d2bf95 590 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 591
8e1cae6d 592 (run-hooks 'isearch-mode-end-hook)
0daa17f3 593 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 594
73d2bf95
RS
595(defun isearch-update-ring (string &optional regexp)
596 "Add STRING to the beginning of the search ring.
597REGEXP says which ring to use."
598 (if regexp
599 (if (or (null regexp-search-ring)
fd89878d 600 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
601 (progn
602 (setq regexp-search-ring
fd89878d 603 (cons string regexp-search-ring))
73d2bf95
RS
604 (if (> (length regexp-search-ring) regexp-search-ring-max)
605 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
606 nil))))
607 (if (or (null search-ring)
fd89878d 608 (not (string= string (car search-ring))))
73d2bf95 609 (progn
fd89878d 610 (setq search-ring (cons string search-ring))
73d2bf95
RS
611 (if (> (length search-ring) search-ring-max)
612 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
613
08200510
RS
614;;;=======================================================
615;;; Switching buffers should first terminate isearch-mode.
eb8c3be9 616;;; This is done quite differently for each variant of emacs.
08200510
RS
617;;; For lemacs, see Exiting in lemacs below
618
619;; For Emacs 19, the frame switch event is handled.
620(defun isearch-switch-frame-handler ()
621 (interactive) ;; Is this necessary?
622 ;; First terminate isearch-mode.
623 (isearch-done)
dba1ec55 624 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510
RS
625
626;;;========================================================
8e1cae6d
JB
627
628\f
629;;;====================================================
630;; Commands active while inside of the isearch minor mode.
631
632(defun isearch-exit ()
633 "Exit search normally.
634However, if this is the first command after starting incremental
635search and `search-nonincremental-instead' is non-nil, do a
08200510 636nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
637 (interactive)
638 (if (and search-nonincremental-instead
639 (= 0 (length isearch-string)))
08200510
RS
640 (let ((isearch-nonincremental t))
641 (isearch-edit-string)))
8e1cae6d
JB
642 (isearch-done))
643
644
645(defun isearch-edit-string ()
08200510
RS
646 "Edit the search string in the minibuffer.
647The following additional command keys are active while editing.
648\\<minibuffer-local-isearch-map>
649\\[exit-minibuffer] to resume incremental searching with the edited string.
650\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
651\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 652\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 653\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 654\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 655\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 656\\<isearch-mode-map>
08200510
RS
657If first char entered is \\[isearch-yank-word], then do word search instead."
658
659 ;; This code is very hairy for several reasons, explained in the code.
660 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
661 ;; If there were a way to catch any change of buffer from the minibuffer,
662 ;; this could be simplified greatly.
eb8c3be9 663 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 664 (interactive)
08200510 665 (condition-case err
a9a5a9fa 666 (let ((isearch-nonincremental isearch-nonincremental)
08200510
RS
667
668 ;; Locally bind all isearch global variables to protect them
669 ;; from recursive isearching.
670 ;; isearch-string -message and -forward are not bound
671 ;; so they may be changed. Instead, save the values.
672 (isearch-new-string isearch-string)
673 (isearch-new-message isearch-message)
674 (isearch-new-forward isearch-forward)
675 (isearch-new-word isearch-word)
676
677 (isearch-regexp isearch-regexp)
678 (isearch-op-fun isearch-op-fun)
679 (isearch-cmds isearch-cmds)
680 (isearch-success isearch-success)
681 (isearch-wrapped isearch-wrapped)
682 (isearch-barrier isearch-barrier)
683 (isearch-adjusted isearch-adjusted)
684 (isearch-yank-flag isearch-yank-flag)
685 (isearch-invalid-regexp isearch-invalid-regexp)
c5f6b356 686 (isearch-within-brackets isearch-within-brackets)
08200510
RS
687 (isearch-other-end isearch-other-end)
688 (isearch-opoint isearch-opoint)
689 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
690 (isearch-small-window isearch-small-window)
691 (isearch-recursive-edit isearch-recursive-edit)
692 ;; Save current configuration so we can restore it here.
693 (isearch-window-configuration (current-window-configuration))
694 )
695
696 ;; Actually terminate isearching until editing is done.
697 ;; This is so that the user can do anything without failure,
698 ;; like switch buffers and start another isearch, and return.
699 (condition-case err
0daa17f3 700 (isearch-done t t)
08200510
RS
701 (exit nil)) ; was recursive editing
702
703 (isearch-message) ;; for read-char
704 (unwind-protect
705 (let* (;; Why does following read-char echo?
706 ;;(echo-keystrokes 0) ;; not needed with above message
d94307ee 707 (e (let ((cursor-in-echo-area t))
235beb32 708 (read-event)))
08200510
RS
709 ;; Binding minibuffer-history-symbol to nil is a work-around
710 ;; for some incompatibility with gmhist.
a56687f1
KH
711 (minibuffer-history-symbol)
712 (message-log-max nil))
08200510
RS
713 ;; If the first character the user types when we prompt them
714 ;; for a string is the yank-word character, then go into
715 ;; word-search mode. Otherwise unread that character and
716 ;; read a key the normal way.
717 ;; Word search does not apply (yet) to regexp searches,
718 ;; no check is made here.
719 (message (isearch-message-prefix nil nil t))
720 (if (eq 'isearch-yank-word
235beb32 721 (lookup-key isearch-mode-map (vector e)))
08200510
RS
722 (setq isearch-word t ;; so message-prefix is right
723 isearch-new-word t)
724 (isearch-unread e))
a5dcf63f
RS
725 (setq cursor-in-echo-area nil)
726 (setq isearch-new-string
35a4d143 727 (let (junk-ring)
71e2ab54
RS
728 (read-from-minibuffer
729 (isearch-message-prefix nil nil isearch-nonincremental)
730 isearch-string
731 minibuffer-local-isearch-map nil
732 'junk-ring))
7e846b10
RS
733 isearch-new-message
734 (mapconcat 'isearch-text-char-description
735 isearch-new-string "")))
08200510
RS
736 ;; Always resume isearching by restarting it.
737 (isearch-mode isearch-forward
738 isearch-regexp
739 isearch-op-fun
0daa17f3 740 nil
08200510
RS
741 isearch-word)
742
743 ;; Copy new local values to isearch globals
744 (setq isearch-string isearch-new-string
745 isearch-message isearch-new-message
746 isearch-forward isearch-new-forward
747 isearch-word isearch-new-word))
748
749 ;; Empty isearch-string means use default.
750 (if (= 0 (length isearch-string))
a8f783b2
RS
751 (setq isearch-string (car (if isearch-regexp regexp-search-ring
752 search-ring)))
753 ;; This used to set the last search string,
754 ;; but I think it is not right to do that here.
755 ;; Only the string actually used should be saved.
756 )
08200510
RS
757
758 ;; Reinvoke the pending search.
759 (isearch-push-state)
760 (isearch-search)
761 (isearch-update)
762 (if isearch-nonincremental
763 (progn
764 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
765 (isearch-done))))
8e1cae6d 766
08200510
RS
767 (quit ; handle abort-recursive-edit
768 (isearch-abort) ;; outside of let to restore outside global values
769 )))
770
771(defun isearch-nonincremental-exit-minibuffer ()
772 (interactive)
773 (setq isearch-nonincremental t)
774 (exit-minibuffer))
775
776(defun isearch-forward-exit-minibuffer ()
777 (interactive)
778 (setq isearch-new-forward t)
779 (exit-minibuffer))
8e1cae6d 780
08200510 781(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 782 (interactive)
08200510
RS
783 (setq isearch-new-forward nil)
784 (exit-minibuffer))
785
19993c54
RS
786(defun isearch-cancel ()
787 "Terminate the search and go back to the starting point."
788 (interactive)
789 (goto-char isearch-opoint)
790 (isearch-done t)
791 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
792
793(defun isearch-abort ()
794 "Abort incremental search mode if searching is successful, signalling quit.
795Otherwise, revert to previous successful search and continue searching.
796Use `isearch-exit' to quit without signalling."
797 (interactive)
eb8c3be9 798;; (ding) signal instead below, if quitting
8e1cae6d
JB
799 (discard-input)
800 (if isearch-success
801 ;; If search is successful, move back to starting point
802 ;; and really do quit.
803 (progn (goto-char isearch-opoint)
509ed182 804 (setq isearch-success nil)
35a4d143 805 (isearch-done t) ; exit isearch
a5dcf63f 806 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
807 ;; If search is failing, or has an incomplete regexp,
808 ;; rub out until it is once more successful.
809 (while (or (not isearch-success) isearch-invalid-regexp)
810 (isearch-pop-state))
8e1cae6d
JB
811 (isearch-update)))
812
8e1cae6d
JB
813(defun isearch-repeat (direction)
814 ;; Utility for isearch-repeat-forward and -backward.
815 (if (eq isearch-forward (eq direction 'forward))
816 ;; C-s in forward or C-r in reverse.
817 (if (equal isearch-string "")
818 ;; If search string is empty, use last one.
819 (setq isearch-string
8e1cae6d 820 (or (if isearch-regexp
a5dcf63f
RS
821 (car regexp-search-ring)
822 (car search-ring))
8e1cae6d
JB
823 "")
824 isearch-message
08200510 825 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
826 isearch-string ""))
827 ;; If already have what to search for, repeat it.
828 (or isearch-success
829 (progn
830
831 (goto-char (if isearch-forward (point-min) (point-max)))
832 (setq isearch-wrapped t))))
833 ;; C-s in reverse or C-r in forward, change direction.
834 (setq isearch-forward (not isearch-forward)))
835
836 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
837
838 (if (equal isearch-string "")
839 (setq isearch-success t)
840 (if (and isearch-success (equal (match-end 0) (match-beginning 0)))
8e1cae6d
JB
841 ;; If repeating a search that found
842 ;; an empty string, ensure we advance.
6fefdc4b
RS
843 (if (if isearch-forward (eobp) (bobp))
844 ;; If there's nowhere to advance to, fail (and wrap next time).
845 (progn
846 (setq isearch-success nil)
847 (ding))
848 (forward-char (if isearch-forward 1 -1))
849 (isearch-search))
850 (isearch-search)))
851
8e1cae6d
JB
852 (isearch-push-state)
853 (isearch-update))
854
855(defun isearch-repeat-forward ()
856 "Repeat incremental search forwards."
857 (interactive)
858 (isearch-repeat 'forward))
859
860(defun isearch-repeat-backward ()
861 "Repeat incremental search backwards."
862 (interactive)
863 (isearch-repeat 'backward))
864
865(defun isearch-toggle-regexp ()
866 "Toggle regexp searching on or off."
867 ;; The status stack is left unchanged.
868 (interactive)
869 (setq isearch-regexp (not isearch-regexp))
08200510 870 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
871 (isearch-update))
872
4453091d
RS
873(defun isearch-toggle-case-fold ()
874 "Toggle case folding in searching on or off."
875 (interactive)
876 (setq isearch-case-fold-search
877 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
878 (let ((message-log-max nil))
879 (message "%s%s [case %ssensitive]"
880 (isearch-message-prefix nil nil isearch-nonincremental)
881 isearch-message
882 (if isearch-case-fold-search "in" "")))
4453091d
RS
883 (setq isearch-adjusted t)
884 (sit-for 1)
885 (isearch-update))
886
8e1cae6d
JB
887(defun isearch-delete-char ()
888 "Discard last input item and move point back.
889If no previous match was done, just beep."
890 (interactive)
891 (if (null (cdr isearch-cmds))
892 (ding)
893 (isearch-pop-state))
894 (isearch-update))
895
896
897(defun isearch-yank (chunk)
898 ;; Helper for isearch-yank-word and isearch-yank-line
30d47262
RS
899 ;; CHUNK should be word, line or kill.
900 (let ((string (cond
901 ((eq chunk 'kill)
902 (current-kill 0))
903 (t
904 (save-excursion
905 (and (not isearch-forward) isearch-other-end
906 (goto-char isearch-other-end))
907 (buffer-substring
908 (point)
909 (save-excursion
910 (cond
911 ((eq chunk 'word)
912 (forward-word 1))
913 ((eq chunk 'line)
914 (end-of-line)))
915 (point))))))))
08200510
RS
916 ;; Downcase the string if not supposed to case-fold yanked strings.
917 (if (and isearch-case-fold-search
fdf5afa4 918 (eq 'not-yanks search-upper-case))
08200510
RS
919 (setq string (downcase string)))
920 (if isearch-regexp (setq string (regexp-quote string)))
921 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
922 isearch-message
923 (concat isearch-message
08200510
RS
924 (mapconcat 'isearch-text-char-description
925 string ""))
8e1cae6d
JB
926 ;; Don't move cursor in reverse search.
927 isearch-yank-flag t))
928 (isearch-search-and-update))
929
30d47262
RS
930(defun isearch-yank-kill ()
931 "Pull string from kill ring into search string."
932 (interactive)
933 (isearch-yank 'kill))
8e1cae6d
JB
934
935(defun isearch-yank-word ()
936 "Pull next word from buffer into search string."
937 (interactive)
938 (isearch-yank 'word))
939
940(defun isearch-yank-line ()
941 "Pull rest of line from buffer into search string."
942 (interactive)
943 (isearch-yank 'line))
944
945
946(defun isearch-search-and-update ()
947 ;; Do the search and update the display.
948 (if (and (not isearch-success)
949 ;; unsuccessful regexp search may become
950 ;; successful by addition of characters which
951 ;; make isearch-string valid
952 (not isearch-regexp))
953 nil
954 ;; In reverse search, adding stuff at
955 ;; the end may cause zero or many more chars to be
956 ;; matched, in the string following point.
957 ;; Allow all those possibilities without moving point as
958 ;; long as the match does not extend past search origin.
959 (if (and (not isearch-forward) (not isearch-adjusted)
960 (condition-case ()
961 (looking-at (if isearch-regexp isearch-string
962 (regexp-quote isearch-string)))
963 (error nil))
964 (or isearch-yank-flag
965 (<= (match-end 0)
966 (min isearch-opoint isearch-barrier))))
967 (setq isearch-success t
968 isearch-invalid-regexp nil
c5f6b356 969 isearch-within-brackets nil
8e1cae6d
JB
970 isearch-other-end (match-end 0))
971 ;; Not regexp, not reverse, or no match at point.
972 (if (and isearch-other-end (not isearch-adjusted))
973 (goto-char (if isearch-forward isearch-other-end
974 (min isearch-opoint
975 isearch-barrier
976 (1+ isearch-other-end)))))
977 (isearch-search)
978 ))
979 (isearch-push-state)
980 (if isearch-op-fun (funcall isearch-op-fun))
981 (isearch-update))
982
983
984;; *, ?, and | chars can make a regexp more liberal.
08200510 985;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
986;; So go back to place last successful search started
987;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 988;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
989
990(defun isearch-*-char ()
991 "Handle * and ? specially in regexps."
992 (interactive)
993 (if isearch-regexp
994
995 (progn
996 (setq isearch-adjusted t)
997 (let ((cs (nth (if isearch-forward
998 5 ; isearch-other-end
999 2) ; saved (point)
1000 (car (cdr isearch-cmds)))))
1001 ;; (car isearch-cmds) is after last search;
1002 ;; (car (cdr isearch-cmds)) is from before it.
1003 (setq cs (or cs isearch-barrier))
1004 (goto-char
1005 (if isearch-forward
1006 (max cs isearch-barrier)
1007 (min cs isearch-barrier))))))
08200510 1008 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1009
1010
8e1cae6d
JB
1011(defun isearch-|-char ()
1012 "If in regexp search, jump to the barrier."
1013 (interactive)
1014 (if isearch-regexp
1015 (progn
1016 (setq isearch-adjusted t)
1017 (goto-char isearch-barrier)))
08200510 1018 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1019
1020
b457cc3b 1021(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1022
1023(defun isearch-other-meta-char ()
8f90f594 1024 "Exit the search normally and reread this key sequence.
35a4d143
RS
1025But only if `search-exit-option' is non-nil, the default.
1026If it is the symbol `edit', the search string is edited in the minibuffer
1027and the meta character is unread so that it applies to editing the string."
8e1cae6d 1028 (interactive)
35a4d143
RS
1029 (cond ((eq search-exit-option 'edit)
1030 (let ((key (this-command-keys)))
e7cb0106 1031 (apply 'isearch-unread (listify-key-sequence key)))
35a4d143
RS
1032 (isearch-edit-string))
1033 (search-exit-option
bd1bd125
RS
1034 (let* ((key (this-command-keys))
1035 (main-event (aref key 0))
1036 window)
70418205 1037 (apply 'isearch-unread (listify-key-sequence key))
285eb5ef
RS
1038 ;; Properly handle scroll-bar and mode-line clicks
1039 ;; for which a dummy prefix event was generated as (aref key 0).
1040 (and (> (length key) 1)
1041 (symbolp (aref key 0))
1042 (listp (aref key 1))
4b43d418 1043 (not (numberp (posn-point (event-start (aref key 1)))))
bd1bd125
RS
1044 ;; Convert the event back into its raw form,
1045 ;; with the dummy prefix implicit in the mouse event,
1046 ;; so it will get split up once again.
0cabad13 1047 (progn (setq unread-command-events
bd1bd125
RS
1048 (cdr unread-command-events))
1049 (setq main-event (car unread-command-events))
1050 (setcar (cdr (event-start main-event))
1051 (car (nth 1 (event-start main-event))))))
70418205
RS
1052 ;; If we got a mouse click, maybe it was read with the buffer
1053 ;; it was clicked on. If so, that buffer, not the current one,
1054 ;; is in isearch mode. So end the search in that buffer.
bd1bd125
RS
1055 (if (and (listp main-event)
1056 (setq window (posn-window (event-start main-event)))
70418205
RS
1057 (windowp window))
1058 (save-excursion
1059 (set-buffer (window-buffer window))
1060 (isearch-done))
1061 (isearch-done))))
35a4d143
RS
1062 (t;; otherwise nil
1063 (isearch-process-search-string (this-command-keys)
1064 (this-command-keys)))))
8e1cae6d 1065
8e1cae6d
JB
1066(defun isearch-quote-char ()
1067 "Quote special characters for incremental search."
1068 (interactive)
1069 (isearch-process-search-char (read-quoted-char (isearch-message t))))
1070
8e1cae6d
JB
1071(defun isearch-return-char ()
1072 "Convert return into newline for incremental search.
1073Obsolete."
1074 (interactive)
1075 (isearch-process-search-char ?\n))
1076
8e1cae6d 1077(defun isearch-printing-char ()
b68c164d 1078 "Add this ordinary printing character to the search string and search."
8e1cae6d 1079 (interactive)
08200510 1080 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1081
1082(defun isearch-whitespace-chars ()
08200510 1083 "Match all whitespace chars, if in regexp mode.
b68c164d 1084If you want to search for just a space, type C-q SPC."
8e1cae6d 1085 (interactive)
08200510 1086 (if isearch-regexp
c5f6b356 1087 (if (and search-whitespace-regexp (not isearch-within-brackets))
08200510
RS
1088 (isearch-process-search-string search-whitespace-regexp " ")
1089 (isearch-printing-char))
1090 (progn
eb8c3be9 1091 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1092 ;; (setq isearch-word t)
1093 ;; (setq isearch-adjusted t)
1094 ;; (goto-char isearch-barrier)
1095 (isearch-printing-char))))
8e1cae6d
JB
1096
1097(defun isearch-process-search-char (char)
1098 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1099 (isearch-process-search-string
1100 (isearch-char-to-string char)
1101 (isearch-text-char-description char)))
8e1cae6d
JB
1102
1103(defun isearch-process-search-string (string message)
1104 (setq isearch-string (concat isearch-string string)
1105 isearch-message (concat isearch-message message))
1106 (isearch-search-and-update))
1107
1108\f
1109;;===========================================================
1110;; Search Ring
1111
08200510
RS
1112(defun isearch-ring-adjust1 (advance)
1113 ;; Helper for isearch-ring-adjust
1114 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1115 (length (length ring))
1116 (yank-pointer-name (if isearch-regexp
08200510 1117 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1118 'search-ring-yank-pointer))
1119 (yank-pointer (eval yank-pointer-name)))
1120 (if (zerop length)
1121 ()
1122 (set yank-pointer-name
1123 (setq yank-pointer
ffbc30b2
PE
1124 (mod (+ (or yank-pointer 0)
1125 (if advance -1 1))
1126 length)))
a5dcf63f 1127 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1128 isearch-message (mapconcat 'isearch-text-char-description
1129 isearch-string "")))))
1130
1131(defun isearch-ring-adjust (advance)
1132 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1133 (if (cdr isearch-cmds) ;; is there more than one thing on stack?
1134 (isearch-pop-state))
1135 (isearch-ring-adjust1 advance)
8e1cae6d 1136 (isearch-push-state)
08200510
RS
1137 (if search-ring-update
1138 (progn
1139 (isearch-search)
1140 (isearch-update))
1141 (isearch-edit-string)
1142 ))
8e1cae6d
JB
1143
1144(defun isearch-ring-advance ()
1145 "Advance to the next search string in the ring."
08200510 1146 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1147 (interactive)
1148 (isearch-ring-adjust 'advance))
1149
1150(defun isearch-ring-retreat ()
1151 "Retreat to the previous search string in the ring."
1152 (interactive)
1153 (isearch-ring-adjust nil))
1154
a5dcf63f
RS
1155(defun isearch-ring-advance-edit (n)
1156 "Insert the next element of the search history into the minibuffer."
1157 (interactive "p")
1158 (let* ((yank-pointer-name (if isearch-regexp
1159 'regexp-search-ring-yank-pointer
1160 'search-ring-yank-pointer))
1161 (yank-pointer (eval yank-pointer-name))
1162 (ring (if isearch-regexp regexp-search-ring search-ring))
1163 (length (length ring)))
1164 (if (zerop length)
1165 ()
1166 (set yank-pointer-name
1167 (setq yank-pointer
ffbc30b2
PE
1168 (mod (- (or yank-pointer 0) n)
1169 length)))
a5dcf63f 1170
a5dcf63f 1171 (erase-buffer)
35a4d143 1172 (insert (nth yank-pointer ring))
49c13105 1173 (goto-char (point-max)))))
a5dcf63f
RS
1174
1175(defun isearch-ring-retreat-edit (n)
1176 "Inserts the previous element of the search history into the minibuffer."
1177 (interactive "p")
1178 (isearch-ring-advance-edit (- n)))
1179
1180;;(defun isearch-ring-adjust-edit (advance)
1181;; "Use the next or previous search string in the ring while in minibuffer."
1182;; (isearch-ring-adjust1 advance)
1183;; (erase-buffer)
1184;; (insert isearch-string))
1185
1186;;(defun isearch-ring-advance-edit ()
1187;; (interactive)
1188;; (isearch-ring-adjust-edit 'advance))
1189
1190;;(defun isearch-ring-retreat-edit ()
1191;; "Retreat to the previous search string in the ring while in the minibuffer."
1192;; (interactive)
1193;; (isearch-ring-adjust-edit nil))
08200510
RS
1194
1195
1196(defun isearch-complete1 ()
1197 ;; Helper for isearch-complete and isearch-complete-edit
1198 ;; Return t if completion OK, nil if no completion exists.
1199 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1200 (alist (mapcar (function (lambda (string) (list string))) ring))
1201 (completion-ignore-case case-fold-search)
1202 (completion (try-completion isearch-string alist)))
1203 (cond
1204 ((eq completion t)
1205 ;; isearch-string stays the same
1206 t)
1207 ((or completion ; not nil, must be a string
1208 (= 0 (length isearch-string))) ; shouldnt have to say this
1209 (if (equal completion isearch-string) ;; no extension?
1210 (if completion-auto-help
1211 (with-output-to-temp-buffer "*Isearch completions*"
1212 (display-completion-list
1213 (all-completions isearch-string alist))))
1214 (setq isearch-string completion))
1215 t)
1216 (t
1217 (message "No completion") ; waits a second if in minibuffer
1218 nil))))
1219
1220(defun isearch-complete ()
1221 "Complete the search string from the strings on the search ring.
1222The completed string is then editable in the minibuffer.
1223If there is no completion possible, say so and continue searching."
1224 (interactive)
1225 (if (isearch-complete1)
1226 (isearch-edit-string)
1227 ;; else
1228 (sit-for 1)
1229 (isearch-update)))
8e1cae6d 1230
08200510
RS
1231(defun isearch-complete-edit ()
1232 "Same as `isearch-complete' except in the minibuffer."
1233 (interactive)
1234 (setq isearch-string (buffer-string))
1235 (if (isearch-complete1)
8e1cae6d 1236 (progn
08200510
RS
1237 (erase-buffer)
1238 (insert isearch-string))))
8e1cae6d
JB
1239
1240\f
1241;;;==============================================================
1242;; The search status stack (and isearch window-local variables, not used).
08200510 1243;; Need a structure for this.
8e1cae6d
JB
1244
1245(defun isearch-top-state ()
8e1cae6d
JB
1246 (let ((cmd (car isearch-cmds)))
1247 (setq isearch-string (car cmd)
1248 isearch-message (car (cdr cmd))
1249 isearch-success (nth 3 cmd)
1250 isearch-forward (nth 4 cmd)
1251 isearch-other-end (nth 5 cmd)
08200510
RS
1252 isearch-word (nth 6 cmd)
1253 isearch-invalid-regexp (nth 7 cmd)
1254 isearch-wrapped (nth 8 cmd)
c5f6b356 1255 isearch-barrier (nth 9 cmd)
f551b97d
RS
1256 isearch-within-brackets (nth 10 cmd)
1257 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1258 (goto-char (car (cdr (cdr cmd))))))
1259
1260(defun isearch-pop-state ()
8e1cae6d
JB
1261 (setq isearch-cmds (cdr isearch-cmds))
1262 (isearch-top-state)
1263 )
1264
1265(defun isearch-push-state ()
1266 (setq isearch-cmds
1267 (cons (list isearch-string isearch-message (point)
1268 isearch-success isearch-forward isearch-other-end
08200510 1269 isearch-word
c5f6b356 1270 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1271 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1272 isearch-cmds)))
1273
8e1cae6d
JB
1274\f
1275;;;==================================================================
1276;; Message string
1277
1278(defun isearch-message (&optional c-q-hack ellipsis)
1279 ;; Generate and print the message string.
1280 (let ((cursor-in-echo-area ellipsis)
1281 (m (concat
08200510 1282 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1283 isearch-message
1284 (isearch-message-suffix c-q-hack ellipsis)
1285 )))
a56687f1
KH
1286 (if c-q-hack
1287 m
1288 (let ((message-log-max nil))
1289 (message "%s" m)))))
8e1cae6d 1290
08200510 1291(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1292 ;; If about to search, and previous search regexp was invalid,
1293 ;; check that it still is. If it is valid now,
1294 ;; let the message we display while searching say that it is valid.
1295 (and isearch-invalid-regexp ellipsis
1296 (condition-case ()
1297 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1298 (setq isearch-invalid-regexp nil
1299 isearch-within-brackets nil))
8e1cae6d
JB
1300 (error nil)))
1301 ;; If currently failing, display no ellipsis.
1302 (or isearch-success (setq ellipsis nil))
1303 (let ((m (concat (if isearch-success "" "failing ")
1304 (if isearch-wrapped "wrapped ")
08200510 1305 (if isearch-word "word " "")
8e1cae6d 1306 (if isearch-regexp "regexp " "")
08200510 1307 (if nonincremental "search" "I-search")
8e1cae6d
JB
1308 (if isearch-forward ": " " backward: ")
1309 )))
1310 (aset m 0 (upcase (aref m 0)))
1311 m))
1312
1313
1314(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1315 (concat (if c-q-hack "^Q" "")
1316 (if isearch-invalid-regexp
1317 (concat " [" isearch-invalid-regexp "]")
1318 "")))
1319
1320\f
1321;;;========================================================
1322;;; Searching
1323
1324(defun isearch-search ()
1325 ;; Do the search with the current search string.
1326 (isearch-message nil t)
4453091d 1327 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1328 (setq isearch-case-fold-search
1329 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d
JB
1330 (condition-case lossage
1331 (let ((inhibit-quit nil)
1332 (case-fold-search isearch-case-fold-search))
1333 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1334 (setq isearch-within-brackets nil)
8e1cae6d
JB
1335 (setq isearch-success
1336 (funcall
08200510
RS
1337 (cond (isearch-word
1338 (if isearch-forward
1339 'word-search-forward 'word-search-backward))
1340 (isearch-regexp
1341 (if isearch-forward
1342 're-search-forward 're-search-backward))
1343 (t
1344 (if isearch-forward 'search-forward 'search-backward)))
8e1cae6d
JB
1345 isearch-string nil t))
1346 (if isearch-success
1347 (setq isearch-other-end
1348 (if isearch-forward (match-beginning 0) (match-end 0)))))
1349
d32696d4 1350 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1351 (setq isearch-success nil))
1352
1353 (invalid-regexp
1354 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1355 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1356 isearch-invalid-regexp))
8e1cae6d
JB
1357 (if (string-match
1358 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1359 isearch-invalid-regexp)
f1c03125
RS
1360 (setq isearch-invalid-regexp "incomplete input")))
1361 (error
1362 ;; stack overflow in regexp search.
1363 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1364
1365 (if isearch-success
1366 nil
1367 ;; Ding if failed this time after succeeding last time.
1368 (and (nth 3 (car isearch-cmds))
1369 (ding))
1370 (goto-char (nth 2 (car isearch-cmds)))))
1371
08200510
RS
1372
1373\f
1374;;;========================================================
1375;;; Highlighting
1376
b78559b0 1377(defvar isearch-overlay nil)
08200510 1378
b78559b0 1379(defun isearch-highlight (beg end)
f5f6a944 1380 (if (or (null search-highlight) (null window-system))
08200510 1381 nil
b78559b0
RS
1382 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1383 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1384 (overlay-put isearch-overlay 'face
1385 (if (internal-find-face 'isearch nil)
1386 'isearch 'region))))
b78559b0
RS
1387
1388(defun isearch-dehighlight (totally)
1389 (if isearch-overlay
1390 (delete-overlay isearch-overlay)))
08200510
RS
1391
1392;;;===========================================================
1393;;; General utilities
1394
08200510 1395
b78559b0
RS
1396(defun isearch-no-upper-case-p (string regexp-flag)
1397 "Return t if there are no upper case chars in STRING.
1398If REGEXP-FLAG is non-nil, disregard letters preceeded by `\\' (but not `\\\\')
1399since they have special meaning in a regexp."
08200510 1400 (let ((case-fold-search nil))
b78559b0
RS
1401 (not (string-match (if regexp-flag "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]"
1402 "[A-Z]")
1403 string))))
08200510
RS
1404
1405
8e1cae6d 1406;;;=================================================
b78559b0 1407;; Portability functions to support various Emacs versions.
8e1cae6d 1408
08200510
RS
1409;; To quiet the byte-compiler.
1410(defvar unread-command-event)
dbc4e1c1 1411(defvar unread-command-events)
08200510 1412(defvar last-command-event)
8e1cae6d 1413
08200510 1414(defun isearch-char-to-string (c)
0cabad13 1415 (make-string 1 c))
08200510
RS
1416
1417(defun isearch-text-char-description (c)
9d78ff8b
RS
1418 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1419 (text-char-description c)
1420 (isearch-char-to-string c)))
08200510 1421
bd1bd125 1422;; General function to unread characters or events.
8f90f594 1423(defun isearch-unread (&rest char-or-events)
bd1bd125
RS
1424 (setq unread-command-events
1425 (append char-or-events unread-command-events)))
08200510
RS
1426
1427(defun isearch-last-command-char ()
1428 ;; General function to return the last command character.
58451992 1429 last-command-char)
08200510 1430
3b1e4dd1 1431;;; isearch.el ends here