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