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