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