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