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