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