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