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