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