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