(OLDXMENU_OPTIONS): Add quotations.
[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
ecc99658 7;; |$Date: 1993/05/19 04:44:42 $|$Revision: 1.36 $
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.
527 (if recursive-edit (recursive-edit))
8e1cae6d
JB
528 )
529
530
531;;;====================================================
532;; Some high level utilities. Others below.
533
534(defun isearch-update ()
535 ;; Called after each command to update the display.
a5dcf63f 536 (if (if isearch-event-data-type
08200510 537 (null unread-command-event)
1a3a6707 538 (if isearch-gnu-emacs-events
dbc4e1c1 539 (null unread-command-events)
08200510 540 (< unread-command-char 0)))
8e1cae6d
JB
541 (progn
542 (if (not (input-pending-p))
543 (isearch-message))
544 (if (and isearch-slow-terminal-mode
545 (not (or isearch-small-window
546 (pos-visible-in-window-p))))
08200510 547 (let ((found-point (point)))
8e1cae6d 548 (setq isearch-small-window t)
8e1cae6d
JB
549 (move-to-window-line 0)
550 (let ((window-min-height 1))
551 (split-window nil (if (< search-slow-window-lines 0)
552 (1+ (- search-slow-window-lines))
553 (- (window-height)
554 (1+ search-slow-window-lines)))))
555 (if (< search-slow-window-lines 0)
556 (progn (vertical-motion (- 1 search-slow-window-lines))
557 (set-window-start (next-window) (point))
558 (set-window-hscroll (next-window)
559 (window-hscroll))
560 (set-window-hscroll (selected-window) 0))
561 (other-window 1))
08200510
RS
562 (goto-char found-point)))
563 (if isearch-other-end
564 (if (< isearch-other-end (point)) ; isearch-forward?
565 (isearch-highlight isearch-other-end (point))
566 (isearch-highlight (point) isearch-other-end)))
567 ))
8e1cae6d
JB
568 (setq ;; quit-flag nil not for isearch-mode
569 isearch-adjusted nil
570 isearch-yank-flag nil)
571 )
572
573
35a4d143 574(defun isearch-done (&optional nopush)
8e1cae6d 575 ;; Called by all commands that terminate isearch-mode.
35a4d143 576 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
8e1cae6d 577 (use-local-map isearch-old-local-map)
08200510
RS
578 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
579 (isearch-dehighlight t)
580 (let ((found-start (window-start (selected-window)))
581 (found-point (point)))
582 (set-window-configuration isearch-window-configuration)
583
584 ;; If there was movement, mark the starting position.
585 ;; Maybe should test difference between and set mark iff > threshold.
586 (if (/= (point) isearch-opoint)
7872ee4d
RS
587 (progn
588 (push-mark isearch-opoint t)
20f3a745
RS
589 (if transient-mark-mode
590 (setq mark-active nil))
7872ee4d
RS
591 (or executing-macro (> (minibuffer-depth) 0)
592 (message "Mark saved where search started")))
08200510
RS
593 ;; (message "") why is this needed?
594 )
595 (if isearch-small-window
596 (goto-char found-point)
597 ;; Exiting the save-window-excursion clobbers window-start; restore it.
598 (set-window-start (selected-window) found-start t)))
599
600 (setq isearch-mode nil)
601 (set-buffer-modified-p (buffer-modified-p)) ;; update modeline
8e1cae6d 602
35a4d143 603 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d
JB
604 ;; Update the ring data.
605 (if isearch-regexp
a8f783b2
RS
606 (if (or (null regexp-search-ring)
607 (not (string= isearch-string (car regexp-search-ring))))
8e1cae6d 608 (progn
08200510 609 (setq regexp-search-ring
a5dcf63f 610 (cons isearch-string regexp-search-ring))
08200510
RS
611 (if (> (length regexp-search-ring) regexp-search-ring-max)
612 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
8e1cae6d 613 nil))))
a8f783b2
RS
614 (if (or (null search-ring)
615 (not (string= isearch-string (car search-ring))))
8e1cae6d 616 (progn
a5dcf63f 617 (setq search-ring (cons isearch-string search-ring))
8e1cae6d
JB
618 (if (> (length search-ring) search-ring-max)
619 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
620
8e1cae6d 621 (run-hooks 'isearch-mode-end-hook)
08200510
RS
622 (if isearch-recursive-edit (exit-recursive-edit)))
623
624;;;=======================================================
625;;; Switching buffers should first terminate isearch-mode.
626;;; This is done quite differently for each varient of emacs.
627;;; For lemacs, see Exiting in lemacs below
628
629;; For Emacs 19, the frame switch event is handled.
630(defun isearch-switch-frame-handler ()
631 (interactive) ;; Is this necessary?
632 ;; First terminate isearch-mode.
633 (isearch-done)
dba1ec55 634 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510
RS
635
636;;;========================================================
8e1cae6d
JB
637
638\f
639;;;====================================================
640;; Commands active while inside of the isearch minor mode.
641
642(defun isearch-exit ()
643 "Exit search normally.
644However, if this is the first command after starting incremental
645search and `search-nonincremental-instead' is non-nil, do a
08200510 646nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
647 (interactive)
648 (if (and search-nonincremental-instead
649 (= 0 (length isearch-string)))
08200510
RS
650 (let ((isearch-nonincremental t))
651 (isearch-edit-string)))
8e1cae6d
JB
652 (isearch-done))
653
654
655(defun isearch-edit-string ()
08200510
RS
656 "Edit the search string in the minibuffer.
657The following additional command keys are active while editing.
658\\<minibuffer-local-isearch-map>
659\\[exit-minibuffer] to resume incremental searching with the edited string.
660\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
661\\[isearch-forward-exit-minibuffer] to resume isearching forward.
662\\[isearch-backward-exit-minibuffer] to resume isearching backward.
663\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
664\\[isearch-ring-retreat-edit] to replace the search string with the previou item in the search ring.
665\\[isearch-complete-edit] to complete the search string using the search ring.
666
667If first char entered is \\[isearch-yank-word], then do word search instead."
668
669 ;; This code is very hairy for several reasons, explained in the code.
670 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
671 ;; If there were a way to catch any change of buffer from the minibuffer,
672 ;; this could be simplified greatly.
673 ;; Editing doesnt back up the search point. Should it?
8e1cae6d 674 (interactive)
08200510 675 (condition-case err
a5dcf63f 676 (let (isearch-nonincremental ; should search nonincrementally?
08200510
RS
677
678 ;; Locally bind all isearch global variables to protect them
679 ;; from recursive isearching.
680 ;; isearch-string -message and -forward are not bound
681 ;; so they may be changed. Instead, save the values.
682 (isearch-new-string isearch-string)
683 (isearch-new-message isearch-message)
684 (isearch-new-forward isearch-forward)
685 (isearch-new-word isearch-word)
686
687 (isearch-regexp isearch-regexp)
688 (isearch-op-fun isearch-op-fun)
689 (isearch-cmds isearch-cmds)
690 (isearch-success isearch-success)
691 (isearch-wrapped isearch-wrapped)
692 (isearch-barrier isearch-barrier)
693 (isearch-adjusted isearch-adjusted)
694 (isearch-yank-flag isearch-yank-flag)
695 (isearch-invalid-regexp isearch-invalid-regexp)
696 (isearch-other-end isearch-other-end)
697 (isearch-opoint isearch-opoint)
698 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
699 (isearch-small-window isearch-small-window)
700 (isearch-recursive-edit isearch-recursive-edit)
701 ;; Save current configuration so we can restore it here.
702 (isearch-window-configuration (current-window-configuration))
703 )
704
705 ;; Actually terminate isearching until editing is done.
706 ;; This is so that the user can do anything without failure,
707 ;; like switch buffers and start another isearch, and return.
708 (condition-case err
35a4d143 709 (isearch-done t)
08200510
RS
710 (exit nil)) ; was recursive editing
711
712 (isearch-message) ;; for read-char
713 (unwind-protect
714 (let* (;; Why does following read-char echo?
715 ;;(echo-keystrokes 0) ;; not needed with above message
d94307ee
RS
716 (e (let ((cursor-in-echo-area t))
717 (if isearch-event-data-type
718 (allocate-event) (read-char))))
08200510
RS
719 ;; Binding minibuffer-history-symbol to nil is a work-around
720 ;; for some incompatibility with gmhist.
721 (minibuffer-history-symbol))
722 ;; If the first character the user types when we prompt them
723 ;; for a string is the yank-word character, then go into
724 ;; word-search mode. Otherwise unread that character and
725 ;; read a key the normal way.
726 ;; Word search does not apply (yet) to regexp searches,
727 ;; no check is made here.
728 (message (isearch-message-prefix nil nil t))
729 (if (eq 'isearch-yank-word
730 (lookup-key
731 isearch-mode-map
732 (char-to-string
a5dcf63f 733 (if isearch-event-data-type
08200510
RS
734 (or (event-to-character (next-command-event e)) 0)
735 e))))
736 (setq isearch-word t ;; so message-prefix is right
737 isearch-new-word t)
738 (isearch-unread e))
a5dcf63f
RS
739 (setq cursor-in-echo-area nil)
740 (setq isearch-new-string
35a4d143 741 (let (junk-ring)
a5dcf63f
RS
742 (read-from-minibuffer (isearch-message-prefix)
743 isearch-string
744 minibuffer-local-isearch-map nil
35a4d143 745 'junk-ring))
08200510
RS
746 isearch-new-message (mapconcat 'text-char-description
747 isearch-new-string "")))
748 ;; Always resume isearching by restarting it.
749 (isearch-mode isearch-forward
750 isearch-regexp
751 isearch-op-fun
752 isearch-recursive-edit
753 isearch-word)
754
755 ;; Copy new local values to isearch globals
756 (setq isearch-string isearch-new-string
757 isearch-message isearch-new-message
758 isearch-forward isearch-new-forward
759 isearch-word isearch-new-word))
760
761 ;; Empty isearch-string means use default.
762 (if (= 0 (length isearch-string))
a8f783b2
RS
763 (setq isearch-string (car (if isearch-regexp regexp-search-ring
764 search-ring)))
765 ;; This used to set the last search string,
766 ;; but I think it is not right to do that here.
767 ;; Only the string actually used should be saved.
768 )
08200510
RS
769
770 ;; Reinvoke the pending search.
771 (isearch-push-state)
772 (isearch-search)
773 (isearch-update)
774 (if isearch-nonincremental
775 (progn
776 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
777 (isearch-done))))
8e1cae6d 778
08200510
RS
779 (quit ; handle abort-recursive-edit
780 (isearch-abort) ;; outside of let to restore outside global values
781 )))
782
783(defun isearch-nonincremental-exit-minibuffer ()
784 (interactive)
785 (setq isearch-nonincremental t)
786 (exit-minibuffer))
787
788(defun isearch-forward-exit-minibuffer ()
789 (interactive)
790 (setq isearch-new-forward t)
791 (exit-minibuffer))
8e1cae6d 792
08200510 793(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 794 (interactive)
08200510
RS
795 (setq isearch-new-forward nil)
796 (exit-minibuffer))
797
798
799(defun isearch-abort ()
800 "Abort incremental search mode if searching is successful, signalling quit.
801Otherwise, revert to previous successful search and continue searching.
802Use `isearch-exit' to quit without signalling."
803 (interactive)
804;; (ding) signal instead below, if quiting
8e1cae6d
JB
805 (discard-input)
806 (if isearch-success
807 ;; If search is successful, move back to starting point
808 ;; and really do quit.
809 (progn (goto-char isearch-opoint)
35a4d143 810 (isearch-done t) ; exit isearch
a5dcf63f 811 (signal 'quit nil)) ; and pass on quit signal
08200510 812 ;; If search is failing, rub out until it is once more successful.
8e1cae6d
JB
813 (while (not isearch-success) (isearch-pop-state))
814 (isearch-update)))
815
816
817(defun isearch-repeat (direction)
818 ;; Utility for isearch-repeat-forward and -backward.
819 (if (eq isearch-forward (eq direction 'forward))
820 ;; C-s in forward or C-r in reverse.
821 (if (equal isearch-string "")
822 ;; If search string is empty, use last one.
823 (setq isearch-string
8e1cae6d 824 (or (if isearch-regexp
a5dcf63f
RS
825 (car regexp-search-ring)
826 (car search-ring))
8e1cae6d
JB
827 "")
828 isearch-message
08200510 829 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
830 isearch-string ""))
831 ;; If already have what to search for, repeat it.
832 (or isearch-success
833 (progn
834
835 (goto-char (if isearch-forward (point-min) (point-max)))
836 (setq isearch-wrapped t))))
837 ;; C-s in reverse or C-r in forward, change direction.
838 (setq isearch-forward (not isearch-forward)))
839
840 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
841 (setq isearch-success t)
842 (or (equal isearch-string "")
843 (progn
844 ;; If repeating a search that found
845 ;; an empty string, ensure we advance.
846 (if (equal (match-end 0) (match-beginning 0))
847 (forward-char (if isearch-forward 1 -1)))
848 (isearch-search)))
849 (isearch-push-state)
850 (isearch-update))
851
852(defun isearch-repeat-forward ()
853 "Repeat incremental search forwards."
854 (interactive)
855 (isearch-repeat 'forward))
856
857(defun isearch-repeat-backward ()
858 "Repeat incremental search backwards."
859 (interactive)
860 (isearch-repeat 'backward))
861
862(defun isearch-toggle-regexp ()
863 "Toggle regexp searching on or off."
864 ;; The status stack is left unchanged.
865 (interactive)
866 (setq isearch-regexp (not isearch-regexp))
08200510 867 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
868 (isearch-update))
869
870(defun isearch-delete-char ()
871 "Discard last input item and move point back.
872If no previous match was done, just beep."
873 (interactive)
874 (if (null (cdr isearch-cmds))
875 (ding)
876 (isearch-pop-state))
877 (isearch-update))
878
879
880(defun isearch-yank (chunk)
881 ;; Helper for isearch-yank-word and isearch-yank-line
08200510
RS
882 (let ((string (save-excursion
883 (and (not isearch-forward) isearch-other-end
884 (goto-char isearch-other-end))
885 (buffer-substring
886 (point)
887 (save-excursion
888 (cond
889 ((eq chunk 'word)
890 (forward-word 1))
891 ((eq chunk 'line)
892 (end-of-line)))
893 (point))))))
894 ;; Downcase the string if not supposed to case-fold yanked strings.
895 (if (and isearch-case-fold-search
fdf5afa4 896 (eq 'not-yanks search-upper-case))
08200510
RS
897 (setq string (downcase string)))
898 (if isearch-regexp (setq string (regexp-quote string)))
899 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
900 isearch-message
901 (concat isearch-message
08200510
RS
902 (mapconcat 'isearch-text-char-description
903 string ""))
8e1cae6d
JB
904 ;; Don't move cursor in reverse search.
905 isearch-yank-flag t))
906 (isearch-search-and-update))
907
908
909(defun isearch-yank-word ()
910 "Pull next word from buffer into search string."
911 (interactive)
912 (isearch-yank 'word))
913
914(defun isearch-yank-line ()
915 "Pull rest of line from buffer into search string."
916 (interactive)
917 (isearch-yank 'line))
918
919
920(defun isearch-search-and-update ()
921 ;; Do the search and update the display.
922 (if (and (not isearch-success)
923 ;; unsuccessful regexp search may become
924 ;; successful by addition of characters which
925 ;; make isearch-string valid
926 (not isearch-regexp))
927 nil
928 ;; In reverse search, adding stuff at
929 ;; the end may cause zero or many more chars to be
930 ;; matched, in the string following point.
931 ;; Allow all those possibilities without moving point as
932 ;; long as the match does not extend past search origin.
933 (if (and (not isearch-forward) (not isearch-adjusted)
934 (condition-case ()
935 (looking-at (if isearch-regexp isearch-string
936 (regexp-quote isearch-string)))
937 (error nil))
938 (or isearch-yank-flag
939 (<= (match-end 0)
940 (min isearch-opoint isearch-barrier))))
941 (setq isearch-success t
942 isearch-invalid-regexp nil
943 isearch-other-end (match-end 0))
944 ;; Not regexp, not reverse, or no match at point.
945 (if (and isearch-other-end (not isearch-adjusted))
946 (goto-char (if isearch-forward isearch-other-end
947 (min isearch-opoint
948 isearch-barrier
949 (1+ isearch-other-end)))))
950 (isearch-search)
951 ))
952 (isearch-push-state)
953 (if isearch-op-fun (funcall isearch-op-fun))
954 (isearch-update))
955
956
957;; *, ?, and | chars can make a regexp more liberal.
08200510 958;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
959;; So go back to place last successful search started
960;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 961;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
962
963(defun isearch-*-char ()
964 "Handle * and ? specially in regexps."
965 (interactive)
966 (if isearch-regexp
967
968 (progn
969 (setq isearch-adjusted t)
970 (let ((cs (nth (if isearch-forward
971 5 ; isearch-other-end
972 2) ; saved (point)
973 (car (cdr isearch-cmds)))))
974 ;; (car isearch-cmds) is after last search;
975 ;; (car (cdr isearch-cmds)) is from before it.
976 (setq cs (or cs isearch-barrier))
977 (goto-char
978 (if isearch-forward
979 (max cs isearch-barrier)
980 (min cs isearch-barrier))))))
08200510 981 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
982
983
8e1cae6d
JB
984(defun isearch-|-char ()
985 "If in regexp search, jump to the barrier."
986 (interactive)
987 (if isearch-regexp
988 (progn
989 (setq isearch-adjusted t)
990 (goto-char isearch-barrier)))
08200510 991 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
992
993
b457cc3b 994(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
995
996(defun isearch-other-meta-char ()
8f90f594 997 "Exit the search normally and reread this key sequence.
35a4d143
RS
998But only if `search-exit-option' is non-nil, the default.
999If it is the symbol `edit', the search string is edited in the minibuffer
1000and the meta character is unread so that it applies to editing the string."
8e1cae6d 1001 (interactive)
35a4d143
RS
1002 (cond ((eq search-exit-option 'edit)
1003 (let ((key (this-command-keys)))
e7cb0106 1004 (apply 'isearch-unread (listify-key-sequence key)))
35a4d143
RS
1005 (isearch-edit-string))
1006 (search-exit-option
1007 (let ((key (this-command-keys)))
e7cb0106 1008 (apply 'isearch-unread (listify-key-sequence key)))
35a4d143
RS
1009 (isearch-done))
1010 (t;; otherwise nil
1011 (isearch-process-search-string (this-command-keys)
1012 (this-command-keys)))))
8e1cae6d
JB
1013
1014
1015(defun isearch-quote-char ()
1016 "Quote special characters for incremental search."
1017 (interactive)
1018 (isearch-process-search-char (read-quoted-char (isearch-message t))))
1019
8e1cae6d
JB
1020(defun isearch-return-char ()
1021 "Convert return into newline for incremental search.
1022Obsolete."
1023 (interactive)
1024 (isearch-process-search-char ?\n))
1025
8e1cae6d 1026(defun isearch-printing-char ()
b68c164d 1027 "Add this ordinary printing character to the search string and search."
8e1cae6d 1028 (interactive)
08200510 1029 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1030
1031(defun isearch-whitespace-chars ()
08200510 1032 "Match all whitespace chars, if in regexp mode.
b68c164d 1033If you want to search for just a space, type C-q SPC."
8e1cae6d 1034 (interactive)
08200510
RS
1035 (if isearch-regexp
1036 (if search-whitespace-regexp
1037 (isearch-process-search-string search-whitespace-regexp " ")
1038 (isearch-printing-char))
1039 (progn
1040 ;; This way of doing word search doesnt correctly extend current search.
1041 ;; (setq isearch-word t)
1042 ;; (setq isearch-adjusted t)
1043 ;; (goto-char isearch-barrier)
1044 (isearch-printing-char))))
8e1cae6d
JB
1045
1046(defun isearch-process-search-char (char)
1047 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1048 (isearch-process-search-string
1049 (isearch-char-to-string char)
1050 (isearch-text-char-description char)))
8e1cae6d
JB
1051
1052(defun isearch-process-search-string (string message)
1053 (setq isearch-string (concat isearch-string string)
1054 isearch-message (concat isearch-message message))
1055 (isearch-search-and-update))
1056
1057\f
1058;;===========================================================
1059;; Search Ring
1060
08200510
RS
1061(defun isearch-ring-adjust1 (advance)
1062 ;; Helper for isearch-ring-adjust
1063 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1064 (length (length ring))
1065 (yank-pointer-name (if isearch-regexp
08200510 1066 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1067 'search-ring-yank-pointer))
1068 (yank-pointer (eval yank-pointer-name)))
1069 (if (zerop length)
1070 ()
1071 (set yank-pointer-name
1072 (setq yank-pointer
a5dcf63f
RS
1073 (% (+ (or yank-pointer 0)
1074 (if advance (1- length) 1))
1075 length)))
1076 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1077 isearch-message (mapconcat 'isearch-text-char-description
1078 isearch-string "")))))
1079
1080(defun isearch-ring-adjust (advance)
1081 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1082 (if (cdr isearch-cmds) ;; is there more than one thing on stack?
1083 (isearch-pop-state))
1084 (isearch-ring-adjust1 advance)
8e1cae6d 1085 (isearch-push-state)
08200510
RS
1086 (if search-ring-update
1087 (progn
1088 (isearch-search)
1089 (isearch-update))
1090 (isearch-edit-string)
1091 ))
8e1cae6d
JB
1092
1093(defun isearch-ring-advance ()
1094 "Advance to the next search string in the ring."
08200510 1095 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1096 (interactive)
1097 (isearch-ring-adjust 'advance))
1098
1099(defun isearch-ring-retreat ()
1100 "Retreat to the previous search string in the ring."
1101 (interactive)
1102 (isearch-ring-adjust nil))
1103
a5dcf63f
RS
1104(defun isearch-ring-advance-edit (n)
1105 "Insert the next element of the search history into the minibuffer."
1106 (interactive "p")
1107 (let* ((yank-pointer-name (if isearch-regexp
1108 'regexp-search-ring-yank-pointer
1109 'search-ring-yank-pointer))
1110 (yank-pointer (eval yank-pointer-name))
1111 (ring (if isearch-regexp regexp-search-ring search-ring))
1112 (length (length ring)))
1113 (if (zerop length)
1114 ()
1115 (set yank-pointer-name
1116 (setq yank-pointer
1117 (% (+ (or yank-pointer 0)
35a4d143
RS
1118 ;; Add LENGTH here to ensure a positive result.
1119 length
1120 (% (- n) length))
a5dcf63f
RS
1121 length)))
1122
a5dcf63f 1123 (erase-buffer)
35a4d143 1124 (insert (nth yank-pointer ring))
49c13105 1125 (goto-char (point-max)))))
a5dcf63f
RS
1126
1127(defun isearch-ring-retreat-edit (n)
1128 "Inserts the previous element of the search history into the minibuffer."
1129 (interactive "p")
1130 (isearch-ring-advance-edit (- n)))
1131
1132;;(defun isearch-ring-adjust-edit (advance)
1133;; "Use the next or previous search string in the ring while in minibuffer."
1134;; (isearch-ring-adjust1 advance)
1135;; (erase-buffer)
1136;; (insert isearch-string))
1137
1138;;(defun isearch-ring-advance-edit ()
1139;; (interactive)
1140;; (isearch-ring-adjust-edit 'advance))
1141
1142;;(defun isearch-ring-retreat-edit ()
1143;; "Retreat to the previous search string in the ring while in the minibuffer."
1144;; (interactive)
1145;; (isearch-ring-adjust-edit nil))
08200510
RS
1146
1147
1148(defun isearch-complete1 ()
1149 ;; Helper for isearch-complete and isearch-complete-edit
1150 ;; Return t if completion OK, nil if no completion exists.
1151 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1152 (alist (mapcar (function (lambda (string) (list string))) ring))
1153 (completion-ignore-case case-fold-search)
1154 (completion (try-completion isearch-string alist)))
1155 (cond
1156 ((eq completion t)
1157 ;; isearch-string stays the same
1158 t)
1159 ((or completion ; not nil, must be a string
1160 (= 0 (length isearch-string))) ; shouldnt have to say this
1161 (if (equal completion isearch-string) ;; no extension?
1162 (if completion-auto-help
1163 (with-output-to-temp-buffer "*Isearch completions*"
1164 (display-completion-list
1165 (all-completions isearch-string alist))))
1166 (setq isearch-string completion))
1167 t)
1168 (t
1169 (message "No completion") ; waits a second if in minibuffer
1170 nil))))
1171
1172(defun isearch-complete ()
1173 "Complete the search string from the strings on the search ring.
1174The completed string is then editable in the minibuffer.
1175If there is no completion possible, say so and continue searching."
1176 (interactive)
1177 (if (isearch-complete1)
1178 (isearch-edit-string)
1179 ;; else
1180 (sit-for 1)
1181 (isearch-update)))
8e1cae6d 1182
08200510
RS
1183(defun isearch-complete-edit ()
1184 "Same as `isearch-complete' except in the minibuffer."
1185 (interactive)
1186 (setq isearch-string (buffer-string))
1187 (if (isearch-complete1)
8e1cae6d 1188 (progn
08200510
RS
1189 (erase-buffer)
1190 (insert isearch-string))))
8e1cae6d
JB
1191
1192\f
1193;;;==============================================================
1194;; The search status stack (and isearch window-local variables, not used).
08200510 1195;; Need a structure for this.
8e1cae6d
JB
1196
1197(defun isearch-top-state ()
8e1cae6d
JB
1198 (let ((cmd (car isearch-cmds)))
1199 (setq isearch-string (car cmd)
1200 isearch-message (car (cdr cmd))
1201 isearch-success (nth 3 cmd)
1202 isearch-forward (nth 4 cmd)
1203 isearch-other-end (nth 5 cmd)
08200510
RS
1204 isearch-word (nth 6 cmd)
1205 isearch-invalid-regexp (nth 7 cmd)
1206 isearch-wrapped (nth 8 cmd)
1207 isearch-barrier (nth 9 cmd))
8e1cae6d
JB
1208 (goto-char (car (cdr (cdr cmd))))))
1209
1210(defun isearch-pop-state ()
8e1cae6d
JB
1211 (setq isearch-cmds (cdr isearch-cmds))
1212 (isearch-top-state)
1213 )
1214
1215(defun isearch-push-state ()
1216 (setq isearch-cmds
1217 (cons (list isearch-string isearch-message (point)
1218 isearch-success isearch-forward isearch-other-end
08200510 1219 isearch-word
8e1cae6d
JB
1220 isearch-invalid-regexp isearch-wrapped isearch-barrier)
1221 isearch-cmds)))
1222
8e1cae6d
JB
1223\f
1224;;;==================================================================
1225;; Message string
1226
1227(defun isearch-message (&optional c-q-hack ellipsis)
1228 ;; Generate and print the message string.
1229 (let ((cursor-in-echo-area ellipsis)
1230 (m (concat
08200510 1231 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1232 isearch-message
1233 (isearch-message-suffix c-q-hack ellipsis)
1234 )))
1235 (if c-q-hack m (message "%s" m))))
1236
08200510 1237(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1238 ;; If about to search, and previous search regexp was invalid,
1239 ;; check that it still is. If it is valid now,
1240 ;; let the message we display while searching say that it is valid.
1241 (and isearch-invalid-regexp ellipsis
1242 (condition-case ()
1243 (progn (re-search-forward isearch-string (point) t)
1244 (setq isearch-invalid-regexp nil))
1245 (error nil)))
1246 ;; If currently failing, display no ellipsis.
1247 (or isearch-success (setq ellipsis nil))
1248 (let ((m (concat (if isearch-success "" "failing ")
1249 (if isearch-wrapped "wrapped ")
08200510 1250 (if isearch-word "word " "")
8e1cae6d 1251 (if isearch-regexp "regexp " "")
08200510 1252 (if nonincremental "search" "I-search")
8e1cae6d
JB
1253 (if isearch-forward ": " " backward: ")
1254 )))
1255 (aset m 0 (upcase (aref m 0)))
1256 m))
1257
1258
1259(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1260 (concat (if c-q-hack "^Q" "")
1261 (if isearch-invalid-regexp
1262 (concat " [" isearch-invalid-regexp "]")
1263 "")))
1264
1265\f
1266;;;========================================================
1267;;; Searching
1268
1269(defun isearch-search ()
1270 ;; Do the search with the current search string.
1271 (isearch-message nil t)
fdf5afa4 1272 (if search-upper-case
08200510 1273 (setq isearch-case-fold-search (isearch-no-upper-case-p isearch-string)))
8e1cae6d
JB
1274 (condition-case lossage
1275 (let ((inhibit-quit nil)
1276 (case-fold-search isearch-case-fold-search))
1277 (if isearch-regexp (setq isearch-invalid-regexp nil))
1278 (setq isearch-success
1279 (funcall
08200510
RS
1280 (cond (isearch-word
1281 (if isearch-forward
1282 'word-search-forward 'word-search-backward))
1283 (isearch-regexp
1284 (if isearch-forward
1285 're-search-forward 're-search-backward))
1286 (t
1287 (if isearch-forward 'search-forward 'search-backward)))
8e1cae6d
JB
1288 isearch-string nil t))
1289 (if isearch-success
1290 (setq isearch-other-end
1291 (if isearch-forward (match-beginning 0) (match-end 0)))))
1292
08200510 1293 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1294 (setq isearch-success nil))
1295
1296 (invalid-regexp
1297 (setq isearch-invalid-regexp (car (cdr lossage)))
1298 (if (string-match
1299 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1300 isearch-invalid-regexp)
f1c03125
RS
1301 (setq isearch-invalid-regexp "incomplete input")))
1302 (error
1303 ;; stack overflow in regexp search.
1304 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1305
1306 (if isearch-success
1307 nil
1308 ;; Ding if failed this time after succeeding last time.
1309 (and (nth 3 (car isearch-cmds))
1310 (ding))
1311 (goto-char (nth 2 (car isearch-cmds)))))
1312
08200510
RS
1313
1314\f
1315;;;========================================================
1316;;; Highlighting
1317
1318(defun isearch-highlight (begin end))
1319(defun isearch-dehighlight (totally))
1320
1321;; lemacs uses faces
1322'(progn
1323(defvar isearch-extent nil)
1324
1325(or (find-face 'isearch) ;; this face is initialized by x-faces.el
1326 (make-face 'isearch)) ;; since isearch is preloaded
1327
1328(defun isearch-lemacs-highlight (begin end)
1329 (if (null isearch-highlight)
1330 nil
1331 (if (and (extentp isearch-extent)
1332 (eq (extent-buffer isearch-extent) (current-buffer)))
1333 (set-extent-endpoints isearch-extent begin end)
1334 (if (and (extentp isearch-extent)
1335 (bufferp (extent-buffer isearch-extent))
1336 (buffer-name (extent-buffer isearch-extent)))
1337 (delete-extent isearch-extent))
1338 (setq isearch-extent (make-extent begin end (current-buffer))))
1339 (set-extent-face isearch-extent 'isearch)))
1340
1341(defun isearch-lemacs-dehighlight (totally)
1342 (if (and isearch-highlight isearch-extent)
1343 (if totally
1344 (let ((inhibit-quit t))
1345 (if (and (extentp isearch-extent)
1346 (bufferp (extent-buffer isearch-extent))
1347 (buffer-name (extent-buffer isearch-extent)))
1348 (delete-extent isearch-extent))
1349 (setq isearch-extent nil))
1350 (if (and (extentp isearch-extent)
1351 (bufferp (extent-buffer isearch-extent))
1352 (buffer-name (extent-buffer isearch-extent)))
1353 (set-extent-face isearch-extent 'default)
1354 (isearch-dehighlight t)))))
1355
b457cc3b
ER
1356(defalias 'isearch-highlight (symbol-function 'isearch-lemacs-highlight))
1357(defalias 'isearch-dehighlight (symbol-function 'isearch-lemacs-dehighlight))
08200510
RS
1358)
1359
1360;;;===========================================================
1361;;; General utilities
1362
b457cc3b 1363;; (defalias 'isearch-member-equal (symbol-function 'member)) ; for emacs 19
08200510
RS
1364
1365(defun isearch-member-equal (item list)
1366 "Return non-nil if ITEM is `equal' to some item in LIST.
1367Actually return the list whose car is that item."
1368 (while (and list (not (equal item (car list))))
1369 (setq list (cdr list)))
1370 list)
1371
1372
1373(defun isearch-no-upper-case-p (string)
1374 "Return t if there are no upper case chars in string.
1375But upper case chars preceeded by \\ (but not \\\\) do not count since they
1376have special meaning in a regexp."
1377 (let ((case-fold-search nil))
1378 (not (string-match "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]" string))))
1379
1380
8e1cae6d 1381;;;=================================================
08200510 1382;;; Special functions for lemacs events.
8e1cae6d 1383
08200510
RS
1384;; To quiet the byte-compiler.
1385(defvar unread-command-event)
dbc4e1c1 1386(defvar unread-command-events)
08200510 1387(defvar last-command-event)
8e1cae6d 1388
08200510
RS
1389(defun isearch-char-to-string (c)
1390 (if (integerp c)
1391 (make-string 1 c)
8d7e4e80
RS
1392 (if (and (symbolp c) (get c 'ascii-character))
1393 (make-string 1 (get c 'ascii-character))
1394 (make-string 1 (event-to-character c)))))
08200510
RS
1395
1396(defun isearch-text-char-description (c)
1397 (isearch-char-to-string c))
1398
8f90f594 1399(defun isearch-unread (&rest char-or-events)
8f90f594
RS
1400 ;; General function to unread characters or events.
1401 (if isearch-gnu-emacs-events
e7cb0106
RS
1402 (setq unread-command-events
1403 (append char-or-events unread-command-events))
8f90f594 1404 (let ((char (if (cdr char-or-events)
75e86b2e
RS
1405 (progn
1406 (while (cdr char-or-events)
1407 (setq char-or-events (cdr char-or-events)))
1408 (+ 128 (car char-or-events)))
8f90f594
RS
1409 (car char-or-events))))
1410 (if isearch-event-data-type
1411 (setq unread-command-event char)
1412 (setq unread-command-char char)))))
08200510
RS
1413
1414(defun isearch-last-command-char ()
1415 ;; General function to return the last command character.
a5dcf63f 1416 (if isearch-event-data-type
08200510
RS
1417 last-command-event
1418 last-command-char))
1419
1420
1421
1422\f
1423;;;========================================================
1424;;; Exiting in lemacs
1425
1426;; This is a large amount of code to support automatic termination of
1427;; isearch-mode when a command (however it is invoked) is not an
1428;; isearch command, or the buffer is switched out from under
1429;; isearch-mode. Only later versions of lemacs have the pre-command-hook.
1430
1431;;(if isearch-pre-command-hook-exists
1432;;(progn
1433
1434;;;; This list must be modified whenever the available commands are modified.
1435;;(mapcar (function (lambda (command)
1436;; (put command 'isearch-command t)))
1437;; '(isearch-printing-char
1438;; isearch-return-char
1439;; isearch-repeat-forward
1440;; isearch-repeat-backward
1441;; isearch-delete-char
1442;; isearch-abort
1443;; isearch-quote-char
1444;; isearch-exit
1445;; isearch-printing-char
1446;; isearch-printing-char
1447;; isearch-yank-word
1448;; isearch-yank-line
1449;; isearch-*-char
1450;; isearch-*-char
1451;; isearch-|-char
1452;; isearch-toggle-regexp
1453;; isearch-edit-string
1454;; isearch-mode-help
1455;; isearch-ring-advance
1456;; isearch-ring-retreat
1457;; isearch-ring-advance-edit
1458;; isearch-ring-retreat-edit
1459;; isearch-whitespace-chars
1460;; isearch-complete
1461;; isearch-complete-edit
1462;; isearch-edit-string
1463;; isearch-toggle-regexp
1464;; ;; The following may not be needed since isearch-mode is off already.
1465;; isearch-forward-exit-minibuffer
1466;; isearch-reverse-exit-minibuffer
1467;; isearch-nonincremental-exit-minibuffer))
1468
1469;;(defun isearch-pre-command-hook ()
1470;; ;;
1471;; ;; For use as the value of `pre-command-hook' when isearch-mode is active.
1472;; ;; If the command about to be executed is not one of the isearch commands,
1473;; ;; then isearch-mode is turned off before that command is executed.
1474;; ;;
1475;; ;; If the command about to be executed is self-insert-command, or is a
1476;; ;; keyboard macro of a single key sequence which is bound to self-insert-
1477;; ;; command, then we add those chars to the search ring instead of inserting
1478;; ;; them in the buffer. In this way, the set of self-searching characters
1479;; ;; need not be exhaustively enumerated, but is derived from other maps.
1480;; ;;
1481;; (isearch-maybe-frob-keyboard-macros)
1482;; (if (and (symbolp this-command)
1483;; (get this-command 'isearch-command))
1484;; nil
1485;; (isearch-done)))
1486
1487;;(defun isearch-maybe-frob-keyboard-macros ()
1488;; ;;
1489;; ;; If the command about to be executed is `self-insert-command' then change
1490;; ;; the command to `isearch-printing-char' instead, meaning add the last-
1491;; ;; typed character to the search string.
1492;; ;;
1493;; ;; If `this-command' is a string or a vector (that is, a keyboard macro)
1494;; ;; and it contains only one command, which is bound to self-insert-command,
1495;; ;; then do the same thing as for self-inserting commands: arrange for that
1496;; ;; character to be added to the search string. If we didn't do this, then
1497;; ;; typing a compose sequence (a la x-compose.el) would terminate the search
1498;; ;; and insert the character, instead of searching for that character.
1499;; ;;
1500;; (cond ((eq this-command 'self-insert-command)
1501;; (setq this-command 'isearch-printing-char))
1502;; ((and (stringp this-command)
1503;; (eq (key-binding this-command) 'self-insert-command))
1504;; (setq last-command-char (aref this-command 0)
1505;; last-command-event (character-to-event last-command-char)
1506;; this-command 'isearch-printing-char))
1507;; ((and (vectorp this-command)
1508;; (eq (key-binding this-command) 'self-insert-command))
1509;; (let* ((desc (aref this-command 0))
1510;; (code (cond ((integerp desc) desc)
1511;; ((symbolp desc) (get desc character-set-property))
1512;; ((consp desc)
1513;; (and (null (cdr desc))
1514;; (get (car desc) character-set-property)))
1515;; (t nil))))
1516;; (if code
1517;; (setq last-command-char code
1518;; last-command-event (character-to-event last-command-char)
1519;; this-command 'isearch-printing-char))))
1520;; ))
1521
1522;;))
4ae7d00a 1523
3b1e4dd1 1524;;; isearch.el ends here