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