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