(iswitchb-visit-buffer): Use `select-frame-set-input-focus'.
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
55535639 1;;; isearch.el --- incremental search minor mode
3b1e4dd1 2
1f4139d5 3;; Copyright (C) 1992, 93, 94, 95, 96, 97, 1999, 2000, 2001
1ea1fb7e 4;; Free Software Foundation, Inc.
3a801d0c 5
3b1e4dd1 6;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
0f09b616 7;; Maintainer: FSF
117132a6 8;; Keywords: matching
8e1cae6d 9
54d2ecd3 10;; This file is part of GNU Emacs.
8e1cae6d 11
59243403
RS
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; any later version.
16
8e1cae6d 17;; GNU Emacs is distributed in the hope that it will be useful,
59243403
RS
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b578f267
EN
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
8e1cae6d 26
3b1e4dd1
ER
27;;; Commentary:
28
8e1cae6d
JB
29;; Instructions
30
08200510
RS
31;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
32;; isearch-mode behaves modally and does not return until the search
117132a6 33;; is completed. It uses a recursive-edit to behave this way.
08200510 34
8e1cae6d 35;; The key bindings active within isearch-mode are defined below in
08200510
RS
36;; `isearch-mode-map' which is given bindings close to the default
37;; characters of the original isearch.el. With `isearch-mode',
38;; however, you can bind multi-character keys and it should be easier
39;; to add new commands. One bug though: keys with meta-prefix cannot
40;; be longer than two chars. Also see minibuffer-local-isearch-map
41;; for bindings active during `isearch-edit-string'.
42
117132a6
DL
43;; isearch-mode should work even if you switch windows with the mouse,
44;; in which case isearch-mode is terminated automatically before the
45;; switch.
08200510
RS
46
47;; The search ring and completion commands automatically put you in
48;; the minibuffer to edit the string. This gives you a chance to
49;; modify the search string before executing the search. There are
50;; three commands to terminate the editing: C-s and C-r exit the
51;; minibuffer and search forward and reverse respectively, while C-m
52;; exits and does a nonincremental search.
53
54;; Exiting immediately from isearch uses isearch-edit-string instead
55;; of nonincremental-search, if search-nonincremental-instead is non-nil.
56;; The name of this option should probably be changed if we decide to
57;; keep the behavior. No point in forcing nonincremental search until
58;; the last possible moment.
59
60;; TODO
eb8c3be9 61;; - Integrate the emacs 19 generalized command history.
08200510
RS
62;; - Think about incorporating query-replace.
63;; - Hooks and options for failed search.
282d89c0 64
3b1e4dd1
ER
65;;; Change Log:
66
b578f267
EN
67;; Changes before those recorded in ChangeLog:
68
69;; Revision 1.4 92/09/14 16:26:02 liberte
70;; Added prefix args to isearch-forward, etc. to switch between
71;; string and regular expression searching.
72;; Added some support for lemacs.
73;; Added general isearch-highlight option - but only for lemacs so far.
74;; Added support for frame switching in emacs 19.
75;; Added word search option to isearch-edit-string.
76;; Renamed isearch-quit to isearch-abort.
77;; Numerous changes to comments and doc strings.
78;;
79;; Revision 1.3 92/06/29 13:10:08 liberte
80;; Moved modal isearch-mode handling into isearch-mode.
81;; Got rid of buffer-local isearch variables.
82;; isearch-edit-string used by ring adjustments, completion, and
83;; nonincremental searching. C-s and C-r are additional exit commands.
84;; Renamed all regex to regexp.
85;; Got rid of found-start and found-point globals.
86;; Generalized handling of upper-case chars.
87
88;; Revision 1.2 92/05/27 11:33:57 liberte
89;; Emacs version 19 has a search ring, which is supported here.
90;; Other fixes found in the version 19 isearch are included here.
91;;
92;; Also see variables search-caps-disable-folding,
93;; search-nonincremental-instead, search-whitespace-regexp, and
94;; commands isearch-toggle-regexp, isearch-edit-string.
95;;
96;; semi-modal isearching is supported.
97
98;; Changes for 1.1
99;; 3/18/92 Fixed invalid-regexp.
100;; 3/18/92 Fixed yanking in regexps.
282d89c0 101
3b1e4dd1 102;;; Code:
08200510
RS
103
104\f
58451992 105;;; Some additional options and constants.
08200510 106
9c1db929
RS
107(defgroup isearch nil
108 "Incremental search minor mode."
117132a6
DL
109 :link '(emacs-commentary-link "isearch")
110 :link '(custom-manual "(emacs)Incremental Search")
9c1db929
RS
111 :prefix "isearch-"
112 :prefix "search-"
113 :group 'matching)
fdf5afa4 114
9c1db929
RS
115
116(defcustom search-exit-option t
117 "*Non-nil means random control characters terminate incremental search."
118 :type 'boolean
119 :group 'isearch)
120
121(defcustom search-slow-window-lines 1
fdf5afa4
RS
122 "*Number of lines in slow search display windows.
123These are the short windows used during incremental search on slow terminals.
124Negative means put the slow search window at the top (normally it's at bottom)
9c1db929
RS
125and the value is minus the number of lines."
126 :type 'integer
127 :group 'isearch)
fdf5afa4 128
9c1db929 129(defcustom search-slow-speed 1200
fdf5afa4
RS
130 "*Highest terminal speed at which to use \"slow\" style incremental search.
131This is the style where a one-line window is created to show the line
9c1db929
RS
132that the search has reached."
133 :type 'integer
134 :group 'isearch)
8e1cae6d 135
9c1db929 136(defcustom search-upper-case 'not-yanks
8e1cae6d 137 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
138That is, upper and lower case chars must match exactly.
139This applies no matter where the chars come from, but does not
fdf5afa4 140apply to chars in regexps that are prefixed with `\\'.
9c1db929
RS
141If this value is `not-yanks', yanked text is always downcased."
142 :type '(choice (const :tag "off" nil)
143 (const not-yanks)
5786b3ed 144 (other :tag "on" t))
9c1db929 145 :group 'isearch)
8e1cae6d 146
9c1db929 147(defcustom search-nonincremental-instead t
8e1cae6d 148 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510 149Actually, `isearch-edit-string' is called to let you enter the search
9c1db929
RS
150string, and RET terminates editing and does a nonincremental search."
151 :type 'boolean
152 :group 'isearch)
8e1cae6d 153
9c1db929 154(defcustom search-whitespace-regexp "\\s-+"
8e1cae6d 155 "*If non-nil, regular expression to match a sequence of whitespace chars.
e2b992cb 156This applies to regular expression incremental search.
dcb6bf2b
RS
157You might want to use something like \"[ \\t\\r\\n]+\" instead.
158In the Customization buffer, that is `[' followed by a space,
159a tab, a carriage return (control-M), a newline, and `]+'."
9c1db929
RS
160 :type 'regexp
161 :group 'isearch)
8e1cae6d 162
30178dde 163(defcustom search-highlight t
9c1db929
RS
164 "*Non-nil means incremental search highlights the current match."
165 :type 'boolean
166 :group 'isearch)
08200510 167
0352b205
RS
168(defcustom search-invisible 'open
169 "If t incremental search can match hidden text.
170nil means don't match invisible text.
171If the value is `open', if the text matched is made invisible by
172an overlay having an `invisible' property and that overlay has a property
173`isearch-open-invisible', then incremental search will show the contents.
174\(This applies when using `outline.el' and `hideshow.el'.)"
175 :type '(choice (const :tag "Match hidden text" t)
176 (const :tag "Open overlays" open)
79c7a4fa 177 (const :tag "Don't match hidden text" nil))
0352b205
RS
178 :group 'isearch)
179
180(defcustom isearch-hide-immediately t
68c18d6d
RS
181 "If non-nil, re-hide an invisible match right away.
182This variable makes a difference when `search-invisible' is set to `open'.
183It means that after search makes some invisible text visible
184to show the match, it makes the text invisible again when the match moves.
185Ordinarily the text becomes invisible again at the end of the search."
0352b205
RS
186 :type 'boolean
187 :group 'isearch)
86bfaffe 188
6848c9f1
KS
189(defcustom isearch-resume-enabled t
190 "*If non-nil, `isearch-resume' commands are added to the command history."
191 :type 'boolean
192 :group 'isearch)
193
08200510
RS
194(defvar isearch-mode-hook nil
195 "Function(s) to call after starting up an incremental search.")
196
197(defvar isearch-mode-end-hook nil
198 "Function(s) to call after terminating an incremental search.")
199
8e1cae6d 200;;; Search ring.
8e1cae6d
JB
201
202(defvar search-ring nil
203 "List of search string sequences.")
08200510 204(defvar regexp-search-ring nil
8e1cae6d
JB
205 "List of regular expression search string sequences.")
206
9c1db929
RS
207(defcustom search-ring-max 16
208 "*Maximum length of search ring before oldest elements are thrown away."
209 :type 'integer
210 :group 'isearch)
211(defcustom regexp-search-ring-max 16
212 "*Maximum length of regexp search ring before oldest elements are thrown away."
213 :type 'integer
214 :group 'isearch)
8e1cae6d
JB
215
216(defvar search-ring-yank-pointer nil
a5dcf63f
RS
217 "Index in `search-ring' of last string reused.
218nil if none yet.")
08200510 219(defvar regexp-search-ring-yank-pointer nil
a5dcf63f
RS
220 "Index in `regexp-search-ring' of last string reused.
221nil if none yet.")
8e1cae6d 222
9c1db929 223(defcustom search-ring-update nil
08200510 224 "*Non-nil if advancing or retreating in the search ring should cause search.
9c1db929
RS
225Default value, nil, means edit the string instead."
226 :type 'boolean
227 :group 'isearch)
08200510 228
8e1cae6d
JB
229;;; Define isearch-mode keymap.
230
02b2f510
SM
231(defvar isearch-mode-map
232 (let* ((i 0)
233 (map (make-keymap)))
234 (or (vectorp (nth 1 map))
235 (char-table-p (nth 1 map))
236 (error "The initialization of isearch-mode-map must be updated"))
237 ;; Make all multibyte characters search for themselves.
238 (let ((l (generic-character-list))
239 (table (nth 1 map)))
240 (while l
241 (set-char-table-default table (car l) 'isearch-printing-char)
242 (setq l (cdr l))))
243 ;; Make function keys, etc, exit the search.
244 (define-key map [t] 'isearch-other-control-char)
245 ;; Control chars, by default, end isearch mode transparently.
246 ;; We need these explicit definitions because, in a dense keymap,
247 ;; the binding for t does not affect characters.
248 ;; We use a dense keymap to save space.
249 (while (< i ?\ )
250 (define-key map (make-string 1 i) 'isearch-other-control-char)
251 (setq i (1+ i)))
252
253 ;; Single-byte printing chars extend the search string by default.
254 (setq i ?\ )
255 (while (< i 256)
256 (define-key map (vector i) 'isearch-printing-char)
257 (setq i (1+ i)))
258
259 ;; To handle local bindings with meta char prefix keys, define
260 ;; another full keymap. This must be done for any other prefix
261 ;; keys as well, one full keymap per char of the prefix key. It
262 ;; would be simpler to disable the global keymap, and/or have a
263 ;; default local key binding for any key not otherwise bound.
264 (let ((meta-map (make-sparse-keymap)))
265 (define-key map (char-to-string meta-prefix-char) meta-map)
266 (define-key map [escape] meta-map))
267 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
268
269 ;; Several non-printing chars change the searching behavior.
270 (define-key map "\C-s" 'isearch-repeat-forward)
271 (define-key map "\C-r" 'isearch-repeat-backward)
272 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
273 ;; combinations can be used to repeat regexp isearches that can
274 ;; be used to start these searches.
275 (define-key map "\M-\C-s" 'isearch-repeat-forward)
276 (define-key map "\M-\C-r" 'isearch-repeat-backward)
277 (define-key map "\177" 'isearch-delete-char)
278 (define-key map "\C-g" 'isearch-abort)
1f4139d5 279
02b2f510
SM
280 ;; This assumes \e is the meta-prefix-char.
281 (or (= ?\e meta-prefix-char)
282 (error "Inconsistency in isearch.el"))
283 (define-key map "\e\e\e" 'isearch-cancel)
284 (define-key map [escape escape escape] 'isearch-cancel)
8e1cae6d 285
02b2f510 286 (define-key map "\C-q" 'isearch-quote-char)
08200510 287
02b2f510
SM
288 (define-key map "\r" 'isearch-exit)
289 (define-key map "\C-j" 'isearch-printing-char)
290 (define-key map "\t" 'isearch-printing-char)
291 (define-key map " " 'isearch-whitespace-chars)
292 (define-key map [?\S-\ ] 'isearch-whitespace-chars)
8e1cae6d 293
02b2f510
SM
294 (define-key map "\C-w" 'isearch-yank-word)
295 (define-key map "\C-y" 'isearch-yank-line)
08200510 296
02b2f510
SM
297 ;; Define keys for regexp chars * ? |.
298 ;; Nothing special for + because it matches at least once.
299 (define-key map "*" 'isearch-*-char)
300 (define-key map "?" 'isearch-*-char)
301 (define-key map "|" 'isearch-|-char)
08200510 302
58f1634a
RS
303;;; Turned off because I find I expect to get the global definition--rms.
304;;; ;; Instead bind C-h to special help command for isearch-mode.
305;;; (define-key map "\C-h" 'isearch-mode-help)
08200510 306
02b2f510
SM
307 (define-key map "\M-n" 'isearch-ring-advance)
308 (define-key map "\M-p" 'isearch-ring-retreat)
309 (define-key map "\M-y" 'isearch-yank-kill)
310
311 (define-key map "\M-\t" 'isearch-complete)
312
313 ;; Pass frame events transparently so they won't exit the search.
314 ;; In particular, if we have more than one display open, then a
315 ;; switch-frame might be generated by someone typing at another keyboard.
316 (define-key map [switch-frame] nil)
317 (define-key map [delete-frame] nil)
318 (define-key map [iconify-frame] nil)
319 (define-key map [make-frame-visible] nil)
3f482bc0 320 (define-key map [mouse-movement] nil)
02b2f510
SM
321 ;; For searching multilingual text.
322 (define-key map "\C-\\" 'isearch-toggle-input-method)
323 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
324
325 ;; People expect to be able to paste with the mouse.
e4af1426 326 (define-key map [mouse-2] #'isearch-mouse-2)
02b2f510
SM
327 (define-key map [down-mouse-2] nil)
328
329 ;; Some bindings you may want to put in your isearch-mode-hook.
330 ;; Suggest some alternates...
331 (define-key map "\M-c" 'isearch-toggle-case-fold)
332 (define-key map "\M-r" 'isearch-toggle-regexp)
333 (define-key map "\M-e" 'isearch-edit-string)
334
335 map)
336 "Keymap for `isearch-mode'.")
337
338(defvar minibuffer-local-isearch-map
339 (let ((map (make-sparse-keymap)))
340 (set-keymap-parent map minibuffer-local-map)
341 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
342 (define-key map "\M-n" 'isearch-ring-advance-edit)
343 (define-key map "\M-p" 'isearch-ring-retreat-edit)
344 (define-key map "\M-\t" 'isearch-complete-edit)
345 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
346 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
347 map)
08200510 348 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 349
8e1cae6d 350;; Internal variables declared globally for byte-compiler.
08200510
RS
351;; These are all set with setq while isearching
352;; and bound locally while editing the search string.
353
354(defvar isearch-forward nil) ; Searching in the forward direction.
355(defvar isearch-regexp nil) ; Searching for a regexp.
356(defvar isearch-word nil) ; Searching for words.
357
358(defvar isearch-cmds nil) ; Stack of search status sets.
359(defvar isearch-string "") ; The current search string.
360(defvar isearch-message "") ; text-char-description version of isearch-string
361
362(defvar isearch-success t) ; Searching is currently successful.
363(defvar isearch-invalid-regexp nil) ; Regexp not well formed.
c5f6b356 364(defvar isearch-within-brackets nil) ; Regexp has unclosed [.
08200510
RS
365(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
366(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d 367(defvar isearch-barrier 0)
99ae9e9f 368(defvar isearch-just-started nil)
8e1cae6d 369
4453091d
RS
370; case-fold-search while searching.
371; either nil, t, or 'yes. 'yes means the same as t except that mixed
372; case in the search string is ignored.
373(defvar isearch-case-fold-search nil)
8e1cae6d 374
67085aba
GM
375(defvar isearch-last-case-fold-search nil)
376
ae1a21c6
MB
377;; Used to save default value while isearch is active
378(defvar isearch-original-minibuffer-message-timeout nil)
379
8e1cae6d
JB
380(defvar isearch-adjusted nil)
381(defvar isearch-slow-terminal-mode nil)
08200510
RS
382;;; If t, using a small window.
383(defvar isearch-small-window nil)
8e1cae6d 384(defvar isearch-opoint 0)
08200510
RS
385;;; The window configuration active at the beginning of the search.
386(defvar isearch-window-configuration nil)
8e1cae6d 387
08200510
RS
388;; Flag to indicate a yank occurred, so don't move the cursor.
389(defvar isearch-yank-flag nil)
8e1cae6d 390
08200510
RS
391;;; A function to be called after each input character is processed.
392;;; (It is not called after characters that exit the search.)
393;;; It is only set from an optional argument to `isearch-mode'.
394(defvar isearch-op-fun nil)
8e1cae6d 395
08200510
RS
396;;; Is isearch-mode in a recursive edit for modal searching.
397(defvar isearch-recursive-edit nil)
8e1cae6d 398
08200510
RS
399;;; Should isearch be terminated after doing one search?
400(defvar isearch-nonincremental nil)
401
402;; New value of isearch-forward after isearch-edit-string.
403(defvar isearch-new-forward nil)
8e1cae6d 404
0352b205
RS
405;; Accumulate here the overlays opened during searching.
406(defvar isearch-opened-overlays nil)
8e1cae6d 407
99848db0
KH
408;; The value of input-method-function when isearch is invoked.
409(defvar isearch-input-method-function nil)
410
411;; A flag to tell if input-method-function is locally bound when
412;; isearch is invoked.
413(defvar isearch-input-method-local-p nil)
414
8e1cae6d
JB
415;; Minor-mode-alist changes - kind of redundant with the
416;; echo area, but if isearching in multiple windows, it can be useful.
417
418(or (assq 'isearch-mode minor-mode-alist)
419 (nconc minor-mode-alist
420 (list '(isearch-mode isearch-mode))))
421
08200510 422(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
423(make-variable-buffer-local 'isearch-mode)
424
fdf5afa4
RS
425(define-key global-map "\C-s" 'isearch-forward)
426(define-key esc-map "\C-s" 'isearch-forward-regexp)
427(define-key global-map "\C-r" 'isearch-backward)
428(define-key esc-map "\C-r" 'isearch-backward-regexp)
429
8e1cae6d 430;;; Entry points to isearch-mode.
8e1cae6d 431
eceee2c0 432(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
433 "\
434Do incremental search forward.
08200510
RS
435With a prefix argument, do an incremental regular expression search instead.
436\\<isearch-mode-map>
8e1cae6d 437As you type characters, they add to the search string and are found.
08200510 438The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 439
08200510 440Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 441Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
442Type LFD (C-j) to match end of line.
443Type \\[isearch-repeat-forward] to search again forward,\
444 \\[isearch-repeat-backward] to search again backward.
445Type \\[isearch-yank-word] to yank word from buffer onto end of search\
446 string and search for it.
447Type \\[isearch-yank-line] to yank rest of line onto end of search string\
448 and search for it.
8617e346
KH
449Type \\[isearch-yank-kill] to yank last killed text onto end of search string\
450 and search for it.
8e1cae6d 451Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
452\\[isearch-abort] while searching or when search has failed cancels input\
453 back to what has
454 been found successfully.
455\\[isearch-abort] when search is successful aborts and moves point to\
456 starting point.
8e1cae6d 457
6c551d4e
EZ
458Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
459Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
460Type \\[isearch-edit-string] to edit the search string in the minibuffer.
461
8e1cae6d
JB
462Also supported is a search ring of the previous 16 search strings.
463Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
464Type \\[isearch-ring-retreat] to search for the previous item in the search\
465 ring.
466Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 467
1e14b095 468If an input method is turned on in the current buffer, that input
37b20c9b
KH
469method is also active while you are typing a characters to search. To
470toggle the input method, type \\[isearch-toggle-input-method]. It
471also toggles the input method in the current buffer.
472
473To use a different input method for searching, type
1e14b095 474\\[isearch-toggle-specified-input-method], and specify an input method
37b20c9b
KH
475you want to use.
476
08200510
RS
477The above keys, bound in `isearch-mode-map', are often controlled by
478 options; do M-x apropos on search-.* to find them.
8e1cae6d 479Other control and meta characters terminate the search
08200510 480 and are then executed normally (depending on `search-exit-option').
fd49e05c 481Likewise for function keys and mouse button events.
8e1cae6d 482
08200510
RS
483If this function is called non-interactively, it does not return to
484the calling function until the search is done."
8e1cae6d 485
eceee2c0 486 (interactive "P\np")
4ae7d00a 487 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 488
eceee2c0 489(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
490 "\
491Do incremental search forward for regular expression.
08200510 492With a prefix argument, do a regular string search instead.
8e1cae6d
JB
493Like ordinary incremental search except that your input
494is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 495 (interactive "P\np")
4ae7d00a 496 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 497
eceee2c0 498(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
499 "\
500Do incremental search backward.
08200510 501With a prefix argument, do a regular expression search instead.
8e1cae6d 502See \\[isearch-forward] for more information."
eceee2c0 503 (interactive "P\np")
4ae7d00a 504 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 505
eceee2c0 506(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
507 "\
508Do incremental search backward for regular expression.
08200510 509With a prefix argument, do a regular string search instead.
8e1cae6d
JB
510Like ordinary incremental search except that your input
511is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 512 (interactive "P\np")
4ae7d00a 513 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
514
515
516(defun isearch-mode-help ()
517 (interactive)
518 (describe-function 'isearch-forward)
519 (isearch-update))
8e1cae6d
JB
520
521\f
8e1cae6d
JB
522;; isearch-mode only sets up incremental search for the minor mode.
523;; All the work is done by the isearch-mode commands.
524
08200510 525;; Not used yet:
d7fa5aa2 526;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
527;; isearch-forward-regexp isearch-backward-regexp)
528;; "List of commands for which isearch-mode does not recursive-edit.")
529
530
531(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
79ce455d
RS
532 "Start isearch minor mode. Called by `isearch-forward', etc.
533
534\\{isearch-mode-map}"
8e1cae6d
JB
535
536 ;; Initialize global vars.
537 (setq isearch-forward forward
538 isearch-regexp regexp
08200510 539 isearch-word word-p
8e1cae6d 540 isearch-op-fun op-fun
67085aba 541 isearch-last-case-fold-search isearch-case-fold-search
8e1cae6d
JB
542 isearch-case-fold-search case-fold-search
543 isearch-string ""
544 isearch-message ""
545 isearch-cmds nil
546 isearch-success t
547 isearch-wrapped nil
548 isearch-barrier (point)
549 isearch-adjusted nil
550 isearch-yank-flag nil
551 isearch-invalid-regexp nil
c5f6b356 552 isearch-within-brackets nil
0cabad13 553 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d 554 (> (window-height)
fad241d3
RS
555 (* 4
556 (abs search-slow-window-lines))))
8e1cae6d
JB
557 isearch-other-end nil
558 isearch-small-window nil
99ae9e9f 559 isearch-just-started t
8e1cae6d 560
8e1cae6d 561 isearch-opoint (point)
a5dcf63f 562 search-ring-yank-pointer nil
0352b205 563 isearch-opened-overlays nil
99848db0
KH
564 isearch-input-method-function input-method-function
565 isearch-input-method-local-p (local-variable-p 'input-method-function)
ae1a21c6
MB
566 regexp-search-ring-yank-pointer nil
567
568 ;; Save the original value of `minibuffer-message-timeout', and
569 ;; set it to nil so that isearch's messages don't get timed out.
570 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
571 minibuffer-message-timeout nil)
99848db0
KH
572
573 ;; We must bypass input method while reading key. When a user type
574 ;; printable character, appropriate input method is turned on in
380866a2 575 ;; minibuffer to read multibyte characters.
99848db0
KH
576 (or isearch-input-method-local-p
577 (make-local-variable 'input-method-function))
578 (setq input-method-function nil)
579
99ae9e9f 580 (looking-at "")
292a8dff
RS
581 (setq isearch-window-configuration
582 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 583
18ce7555
RS
584 ;; Maybe make minibuffer frame visible and/or raise it.
585 (let ((frame (window-frame (minibuffer-window))))
586 (if (not (memq (frame-live-p frame) '(nil t)))
587 (progn
588 (make-frame-visible frame)
589 (if minibuffer-auto-raise
590 (raise-frame frame)))))
591
8e1cae6d 592 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 593 (force-mode-line-update)
8e1cae6d
JB
594
595 (isearch-push-state)
596
c7955222 597 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
598 (isearch-update)
599 (run-hooks 'isearch-mode-hook)
08200510 600
bfe2d334 601 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 602 (add-hook 'kbd-macro-termination-hook 'isearch-done)
6e5cd0ae 603
08200510
RS
604 ;; isearch-mode can be made modal (in the sense of not returning to
605 ;; the calling function until searching is completed) by entering
606 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
607 (if recursive-edit
608 (let ((isearch-recursive-edit t))
609 (recursive-edit)))
0daa17f3 610 isearch-success)
8e1cae6d
JB
611
612
8e1cae6d
JB
613;; Some high level utilities. Others below.
614
615(defun isearch-update ()
616 ;; Called after each command to update the display.
58451992 617 (if (null unread-command-events)
8e1cae6d 618 (progn
c982ab21
GM
619 (if (not (input-pending-p))
620 (isearch-message))
621 (if (and isearch-slow-terminal-mode
622 (not (or isearch-small-window
623 (pos-visible-in-window-p))))
624 (let ((found-point (point)))
625 (setq isearch-small-window t)
626 (move-to-window-line 0)
627 (let ((window-min-height 1))
628 (split-window nil (if (< search-slow-window-lines 0)
629 (1+ (- search-slow-window-lines))
630 (- (window-height)
631 (1+ search-slow-window-lines)))))
632 (if (< search-slow-window-lines 0)
633 (progn (vertical-motion (- 1 search-slow-window-lines))
634 (set-window-start (next-window) (point))
635 (set-window-hscroll (next-window)
636 (window-hscroll))
637 (set-window-hscroll (selected-window) 0))
638 (other-window 1))
639 (goto-char found-point)))
640 (if isearch-other-end
641 (if (< isearch-other-end (point)) ; isearch-forward?
642 (isearch-highlight isearch-other-end (point))
643 (isearch-highlight (point) isearch-other-end))
644 (isearch-dehighlight nil))
645 ))
8e1cae6d
JB
646 (setq ;; quit-flag nil not for isearch-mode
647 isearch-adjusted nil
648 isearch-yank-flag nil)
a5cc922e
KH
649 (isearch-lazy-highlight-new-loop)
650 ;; We must prevent the point moving to the end of composition when a
651 ;; part of the composition has just been searched.
652 (setq disable-point-adjustment t))
8e1cae6d 653
0daa17f3 654(defun isearch-done (&optional nopush edit)
6848c9f1
KS
655 (if isearch-resume-enabled
656 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
657 ,isearch-word ,isearch-forward
658 ,isearch-message
659 ',isearch-case-fold-search)))
660 (unless (equal (car command-history) command)
661 (setq command-history (cons command command-history)))))
c40a4de1 662
bfe2d334 663 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 664 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
d196f58d
GM
665 (setq isearch-lazy-highlight-start nil)
666
8e1cae6d 667 ;; Called by all commands that terminate isearch-mode.
35a4d143 668 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 669 (setq overriding-terminal-local-map nil)
08200510 670 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
ae1a21c6 671 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
08200510 672 (isearch-dehighlight t)
67ecad4e 673 (isearch-lazy-highlight-cleanup isearch-lazy-highlight-cleanup)
08200510
RS
674 (let ((found-start (window-start (selected-window)))
675 (found-point (point)))
70418205
RS
676 (if isearch-window-configuration
677 (set-window-configuration isearch-window-configuration))
08200510 678
d49b6338
RS
679 (if isearch-small-window
680 (goto-char found-point)
681 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 682 (set-window-start (selected-window) found-start t))
d49b6338 683
08200510
RS
684 ;; If there was movement, mark the starting position.
685 ;; Maybe should test difference between and set mark iff > threshold.
686 (if (/= (point) isearch-opoint)
9821535a
RS
687 (or (and transient-mark-mode mark-active)
688 (progn
689 (push-mark isearch-opoint t)
f3acf6f5 690 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 691 (message "Mark saved where search started"))))))
08200510
RS
692
693 (setq isearch-mode nil)
99848db0
KH
694 (if isearch-input-method-local-p
695 (setq input-method-function isearch-input-method-function)
696 (kill-local-variable 'input-method-function))
697
1cd3732c 698 (force-mode-line-update)
8e1cae6d 699
df01192b
RS
700 ;; If we ended in the middle of some intangible text,
701 ;; move to the further end of that intangible text.
702 (let ((after (if (eobp) nil
703 (get-text-property (point) 'intangible)))
704 (before (if (bobp) nil
705 (get-text-property (1- (point)) 'intangible))))
706 (when (and before after (eq before after))
707 (if isearch-forward
708 (goto-char (next-single-property-change (point) 'intangible))
709 (goto-char (previous-single-property-change (point) 'intangible)))))
710
35a4d143 711 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 712 ;; Update the ring data.
73d2bf95 713 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 714
8e1cae6d 715 (run-hooks 'isearch-mode-end-hook)
0daa17f3 716 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 717
73d2bf95
RS
718(defun isearch-update-ring (string &optional regexp)
719 "Add STRING to the beginning of the search ring.
720REGEXP says which ring to use."
721 (if regexp
722 (if (or (null regexp-search-ring)
fd89878d 723 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
724 (progn
725 (setq regexp-search-ring
fd89878d 726 (cons string regexp-search-ring))
73d2bf95
RS
727 (if (> (length regexp-search-ring) regexp-search-ring-max)
728 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
729 nil))))
730 (if (or (null search-ring)
fd89878d 731 (not (string= string (car search-ring))))
73d2bf95 732 (progn
fd89878d 733 (setq search-ring (cons string search-ring))
73d2bf95
RS
734 (if (> (length search-ring) search-ring-max)
735 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
736
08200510 737;;; Switching buffers should first terminate isearch-mode.
117132a6
DL
738;;; ;; For Emacs 19, the frame switch event is handled.
739;;; (defun isearch-switch-frame-handler ()
740;;; (interactive) ;; Is this necessary?
741;;; ;; First terminate isearch-mode.
742;;; (isearch-done)
743;;; (isearch-clean-overlays)
744;;; (handle-switch-frame (car (cdr last-command-char))))
08200510 745
8e1cae6d 746\f
8e1cae6d
JB
747;; Commands active while inside of the isearch minor mode.
748
749(defun isearch-exit ()
750 "Exit search normally.
751However, if this is the first command after starting incremental
752search and `search-nonincremental-instead' is non-nil, do a
08200510 753nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
754 (interactive)
755 (if (and search-nonincremental-instead
756 (= 0 (length isearch-string)))
08200510
RS
757 (let ((isearch-nonincremental t))
758 (isearch-edit-string)))
0352b205
RS
759 (isearch-done)
760 (isearch-clean-overlays))
8e1cae6d
JB
761
762
763(defun isearch-edit-string ()
08200510
RS
764 "Edit the search string in the minibuffer.
765The following additional command keys are active while editing.
766\\<minibuffer-local-isearch-map>
767\\[exit-minibuffer] to resume incremental searching with the edited string.
768\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
769\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 770\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 771\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 772\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 773\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 774\\<isearch-mode-map>
08200510
RS
775If first char entered is \\[isearch-yank-word], then do word search instead."
776
777 ;; This code is very hairy for several reasons, explained in the code.
778 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
779 ;; If there were a way to catch any change of buffer from the minibuffer,
780 ;; this could be simplified greatly.
eb8c3be9 781 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 782 (interactive)
08200510 783 (condition-case err
e900ced4
RS
784 (progn
785 (let ((isearch-nonincremental isearch-nonincremental)
786
787 ;; Locally bind all isearch global variables to protect them
788 ;; from recursive isearching.
789 ;; isearch-string -message and -forward are not bound
790 ;; so they may be changed. Instead, save the values.
791 (isearch-new-string isearch-string)
792 (isearch-new-message isearch-message)
793 (isearch-new-forward isearch-forward)
794 (isearch-new-word isearch-word)
795
796 (isearch-regexp isearch-regexp)
797 (isearch-op-fun isearch-op-fun)
798 (isearch-cmds isearch-cmds)
799 (isearch-success isearch-success)
800 (isearch-wrapped isearch-wrapped)
801 (isearch-barrier isearch-barrier)
802 (isearch-adjusted isearch-adjusted)
803 (isearch-yank-flag isearch-yank-flag)
804 (isearch-invalid-regexp isearch-invalid-regexp)
805 (isearch-within-brackets isearch-within-brackets)
806 ;;; Don't bind this. We want isearch-search, below, to set it.
807 ;;; And the old value won't matter after that.
808 ;;; (isearch-other-end isearch-other-end)
809 ;;; Perhaps some of these other variables should be bound for a
810 ;;; shorter period, ending before the next isearch-search.
811 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
812 (isearch-opoint isearch-opoint)
813 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
814 (isearch-small-window isearch-small-window)
815 (isearch-recursive-edit isearch-recursive-edit)
816 ;; Save current configuration so we can restore it here.
817 (isearch-window-configuration (current-window-configuration))
ae1a21c6
MB
818
819 ;; Temporarily restore `minibuffer-message-timeout'.
820 (minibuffer-message-timeout
821 isearch-original-minibuffer-message-timeout)
822 (isearch-original-minibuffer-message-timeout
823 isearch-original-minibuffer-message-timeout)
e900ced4
RS
824 )
825
826 ;; Actually terminate isearching until editing is done.
827 ;; This is so that the user can do anything without failure,
828 ;; like switch buffers and start another isearch, and return.
829 (condition-case err
830 (isearch-done t t)
831 (exit nil)) ; was recursive editing
832
833 (isearch-message) ;; for read-char
834 (unwind-protect
835 (let* (;; Why does following read-char echo?
836 ;;(echo-keystrokes 0) ;; not needed with above message
837 (e (let ((cursor-in-echo-area t))
838 (read-event)))
839 ;; Binding minibuffer-history-symbol to nil is a work-around
840 ;; for some incompatibility with gmhist.
841 (minibuffer-history-symbol)
842 (message-log-max nil))
843 ;; If the first character the user types when we prompt them
844 ;; for a string is the yank-word character, then go into
845 ;; word-search mode. Otherwise unread that character and
846 ;; read a key the normal way.
847 ;; Word search does not apply (yet) to regexp searches,
848 ;; no check is made here.
849 (message (isearch-message-prefix nil nil t))
850 (if (eq 'isearch-yank-word
851 (lookup-key isearch-mode-map (vector e)))
852 (setq isearch-word t;; so message-prefix is right
853 isearch-new-word t)
854 (cancel-kbd-macro-events)
855 (isearch-unread e))
856 (setq cursor-in-echo-area nil)
857 (setq isearch-new-string
858 (let (junk-ring)
859 (read-from-minibuffer
860 (isearch-message-prefix nil nil isearch-nonincremental)
861 isearch-string
862 minibuffer-local-isearch-map nil
863 'junk-ring))
864 isearch-new-message
865 (mapconcat 'isearch-text-char-description
866 isearch-new-string "")))
867 ;; Always resume isearching by restarting it.
868 (isearch-mode isearch-forward
869 isearch-regexp
870 isearch-op-fun
871 nil
872 isearch-word)
873
874 ;; Copy new local values to isearch globals
875 (setq isearch-string isearch-new-string
876 isearch-message isearch-new-message
877 isearch-forward isearch-new-forward
878 isearch-word isearch-new-word))
879
880 ;; Empty isearch-string means use default.
881 (if (= 0 (length isearch-string))
882 (setq isearch-string (or (car (if isearch-regexp
883 regexp-search-ring
884 search-ring))
885 ""))
886 ;; This used to set the last search string,
887 ;; but I think it is not right to do that here.
888 ;; Only the string actually used should be saved.
889 ))
890
891 ;; Push the state as of before this C-s.
892 (isearch-push-state)
08200510
RS
893
894 ;; Reinvoke the pending search.
08200510
RS
895 (isearch-search)
896 (isearch-update)
897 (if isearch-nonincremental
898 (progn
899 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
900 (isearch-done))))
8e1cae6d 901
08200510
RS
902 (quit ; handle abort-recursive-edit
903 (isearch-abort) ;; outside of let to restore outside global values
904 )))
905
906(defun isearch-nonincremental-exit-minibuffer ()
907 (interactive)
908 (setq isearch-nonincremental t)
909 (exit-minibuffer))
910
911(defun isearch-forward-exit-minibuffer ()
912 (interactive)
913 (setq isearch-new-forward t)
914 (exit-minibuffer))
8e1cae6d 915
08200510 916(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 917 (interactive)
08200510
RS
918 (setq isearch-new-forward nil)
919 (exit-minibuffer))
920
19993c54
RS
921(defun isearch-cancel ()
922 "Terminate the search and go back to the starting point."
923 (interactive)
924 (goto-char isearch-opoint)
925 (isearch-done t)
0352b205 926 (isearch-clean-overlays)
19993c54 927 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
928
929(defun isearch-abort ()
b7852303 930 "Abort incremental search mode if searching is successful, signaling quit.
08200510 931Otherwise, revert to previous successful search and continue searching.
b7852303 932Use `isearch-exit' to quit without signaling."
08200510 933 (interactive)
eb8c3be9 934;; (ding) signal instead below, if quitting
8e1cae6d
JB
935 (discard-input)
936 (if isearch-success
937 ;; If search is successful, move back to starting point
938 ;; and really do quit.
939 (progn (goto-char isearch-opoint)
509ed182 940 (setq isearch-success nil)
35a4d143 941 (isearch-done t) ; exit isearch
0352b205 942 (isearch-clean-overlays)
a5dcf63f 943 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
944 ;; If search is failing, or has an incomplete regexp,
945 ;; rub out until it is once more successful.
946 (while (or (not isearch-success) isearch-invalid-regexp)
947 (isearch-pop-state))
8e1cae6d
JB
948 (isearch-update)))
949
8e1cae6d
JB
950(defun isearch-repeat (direction)
951 ;; Utility for isearch-repeat-forward and -backward.
952 (if (eq isearch-forward (eq direction 'forward))
953 ;; C-s in forward or C-r in reverse.
954 (if (equal isearch-string "")
955 ;; If search string is empty, use last one.
956 (setq isearch-string
8e1cae6d 957 (or (if isearch-regexp
a5dcf63f
RS
958 (car regexp-search-ring)
959 (car search-ring))
8e1cae6d
JB
960 "")
961 isearch-message
08200510 962 (mapconcat 'isearch-text-char-description
67085aba
GM
963 isearch-string "")
964 isearch-case-fold-search isearch-last-case-fold-search)
8e1cae6d
JB
965 ;; If already have what to search for, repeat it.
966 (or isearch-success
02b2f510 967 (progn
8e1cae6d
JB
968 (goto-char (if isearch-forward (point-min) (point-max)))
969 (setq isearch-wrapped t))))
970 ;; C-s in reverse or C-r in forward, change direction.
971 (setq isearch-forward (not isearch-forward)))
972
973 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
974
975 (if (equal isearch-string "")
976 (setq isearch-success t)
99ae9e9f
KH
977 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
978 (not isearch-just-started))
8e1cae6d
JB
979 ;; If repeating a search that found
980 ;; an empty string, ensure we advance.
6fefdc4b
RS
981 (if (if isearch-forward (eobp) (bobp))
982 ;; If there's nowhere to advance to, fail (and wrap next time).
983 (progn
984 (setq isearch-success nil)
985 (ding))
986 (forward-char (if isearch-forward 1 -1))
987 (isearch-search))
988 (isearch-search)))
989
8e1cae6d
JB
990 (isearch-push-state)
991 (isearch-update))
992
993(defun isearch-repeat-forward ()
994 "Repeat incremental search forwards."
995 (interactive)
996 (isearch-repeat 'forward))
997
998(defun isearch-repeat-backward ()
999 "Repeat incremental search backwards."
1000 (interactive)
1001 (isearch-repeat 'backward))
1002
1003(defun isearch-toggle-regexp ()
1004 "Toggle regexp searching on or off."
1005 ;; The status stack is left unchanged.
1006 (interactive)
1007 (setq isearch-regexp (not isearch-regexp))
08200510 1008 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
1009 (isearch-update))
1010
4453091d
RS
1011(defun isearch-toggle-case-fold ()
1012 "Toggle case folding in searching on or off."
1013 (interactive)
1014 (setq isearch-case-fold-search
1015 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
1016 (let ((message-log-max nil))
1017 (message "%s%s [case %ssensitive]"
1018 (isearch-message-prefix nil nil isearch-nonincremental)
1019 isearch-message
1020 (if isearch-case-fold-search "in" "")))
4453091d
RS
1021 (setq isearch-adjusted t)
1022 (sit-for 1)
1023 (isearch-update))
1024
8e1cae6d 1025(defun isearch-delete-char ()
d6b8a1c0 1026 "Discard last input item and move point back.
8e1cae6d
JB
1027If no previous match was done, just beep."
1028 (interactive)
1029 (if (null (cdr isearch-cmds))
1030 (ding)
1031 (isearch-pop-state))
1032 (isearch-update))
1033
1034
9cf081fa
KH
1035(defun isearch-yank-string (string)
1036 "Pull STRING into search string."
1037 ;; Downcase the string if not supposed to case-fold yanked strings.
1038 (if (and isearch-case-fold-search
1039 (eq 'not-yanks search-upper-case))
1040 (setq string (downcase string)))
1041 (if isearch-regexp (setq string (regexp-quote string)))
1042 (setq isearch-string (concat isearch-string string)
1043 isearch-message
1044 (concat isearch-message
1045 (mapconcat 'isearch-text-char-description
1046 string ""))
1047 ;; Don't move cursor in reverse search.
1048 isearch-yank-flag t)
8e1cae6d
JB
1049 (isearch-search-and-update))
1050
30d47262
RS
1051(defun isearch-yank-kill ()
1052 "Pull string from kill ring into search string."
1053 (interactive)
9cf081fa
KH
1054 (isearch-yank-string (current-kill 0)))
1055
1056(defun isearch-yank-x-selection ()
117132a6 1057 "Pull current X selection into search string."
9cf081fa
KH
1058 (interactive)
1059 (isearch-yank-string (x-get-selection)))
8e1cae6d 1060
e4af1426
GM
1061
1062(defun isearch-mouse-2 (click arg)
1063 "Handle mouse-2 in Isearch mode.
117132a6 1064For a click in the echo area, invoke `isearch-yank-x-selection'.
e4af1426 1065Otherwise invoke whatever mouse-2 is bound to outside of Isearch."
117132a6 1066 (interactive "e\nP")
e4af1426
GM
1067 (let* ((w (posn-window (event-start click)))
1068 (overriding-terminal-local-map nil)
1069 (key (vector (event-basic-type click)))
1070 (binding (key-binding key)))
117132a6
DL
1071 (if (and (window-minibuffer-p w)
1072 (not (minibuffer-window-active-p w))) ; in echo area
1073 (isearch-yank-x-selection)
e4af1426
GM
1074 (when binding
1075 ;; Kluge to allow passing ARG to functions that support it,
1076 ;; like mouse-yank-at-click.
1077 (if (equal (cadr (interactive-form binding)) "e\nP")
1078 (funcall binding click arg)
1079 (funcall binding click))))))
1080
117132a6 1081
29e53a0a
KF
1082(defun isearch-yank-internal (jumpform)
1083 "Pull the text from point to the point reached by JUMPFORM.
1084JUMPFORM is a lambda expression that takes no arguments and returns a
1085buffer position, possibly having moved point to that position. For
1086example, it might move point forward by a word and return point, or it
1087might return the position of the end of the line."
9cf081fa
KH
1088 (isearch-yank-string
1089 (save-excursion
1090 (and (not isearch-forward) isearch-other-end
1091 (goto-char isearch-other-end))
29e53a0a
KF
1092 (buffer-substring-no-properties (point) (funcall jumpform)))))
1093
1094(defun isearch-yank-char ()
1095 "Pull next letter from buffer into search string."
1096 (interactive)
1097 (isearch-yank-internal (lambda () (forward-char 1) (point))))
1098
1099(defun isearch-yank-word ()
1100 "Pull next word from buffer into search string."
1101 (interactive)
1102 (isearch-yank-internal (lambda () (forward-word 1) (point))))
8e1cae6d
JB
1103
1104(defun isearch-yank-line ()
1105 "Pull rest of line from buffer into search string."
1106 (interactive)
29e53a0a 1107 (isearch-yank-internal (lambda () (line-end-position))))
8e1cae6d
JB
1108
1109
1110(defun isearch-search-and-update ()
1111 ;; Do the search and update the display.
1112 (if (and (not isearch-success)
1113 ;; unsuccessful regexp search may become
1114 ;; successful by addition of characters which
1115 ;; make isearch-string valid
1116 (not isearch-regexp))
1117 nil
1118 ;; In reverse search, adding stuff at
1119 ;; the end may cause zero or many more chars to be
1120 ;; matched, in the string following point.
1121 ;; Allow all those possibilities without moving point as
1122 ;; long as the match does not extend past search origin.
1123 (if (and (not isearch-forward) (not isearch-adjusted)
1124 (condition-case ()
99ae9e9f 1125 (let ((case-fold-search isearch-case-fold-search))
ec0a2f45
KH
1126 (if (and (eq case-fold-search t) search-upper-case)
1127 (setq case-fold-search
1128 (isearch-no-upper-case-p isearch-string isearch-regexp)))
99ae9e9f
KH
1129 (looking-at (if isearch-regexp isearch-string
1130 (regexp-quote isearch-string))))
8e1cae6d 1131 (error nil))
bd6a8414
RS
1132 (or isearch-yank-flag
1133 (<= (match-end 0)
1134 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1135 (progn
1136 (setq isearch-success t
1137 isearch-invalid-regexp nil
1138 isearch-within-brackets nil
1139 isearch-other-end (match-end 0))
1140 (if (and (eq isearch-case-fold-search t) search-upper-case)
1141 (setq isearch-case-fold-search
1142 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1143 ;; Not regexp, not reverse, or no match at point.
1144 (if (and isearch-other-end (not isearch-adjusted))
1145 (goto-char (if isearch-forward isearch-other-end
1146 (min isearch-opoint
1147 isearch-barrier
1148 (1+ isearch-other-end)))))
1149 (isearch-search)
1150 ))
1151 (isearch-push-state)
1152 (if isearch-op-fun (funcall isearch-op-fun))
1153 (isearch-update))
1154
1155
1156;; *, ?, and | chars can make a regexp more liberal.
08200510 1157;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1158;; So go back to place last successful search started
1159;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1160;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1161
1162(defun isearch-*-char ()
1163 "Handle * and ? specially in regexps."
1164 (interactive)
1165 (if isearch-regexp
bd6a8414
RS
1166 (let ((idx (length isearch-string)))
1167 (while (and (> idx 0)
1168 (eq (aref isearch-string (1- idx)) ?\\))
1169 (setq idx (1- idx)))
1170 (when (= (mod (- (length isearch-string) idx) 2) 0)
1171 (setq isearch-adjusted t)
1172 ;; Get the isearch-other-end from before the last search.
1173 ;; We want to start from there,
1174 ;; so that we don't retreat farther than that.
1175 ;; (car isearch-cmds) is after last search;
1176 ;; (car (cdr isearch-cmds)) is from before it.
1177 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1178 (setq cs (or cs isearch-barrier))
1179 (goto-char
1180 (if isearch-forward
1181 (max cs isearch-barrier)
1182 (min cs isearch-barrier)))))))
117132a6 1183 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1184
1185
8e1cae6d
JB
1186(defun isearch-|-char ()
1187 "If in regexp search, jump to the barrier."
1188 (interactive)
1189 (if isearch-regexp
1190 (progn
1191 (setq isearch-adjusted t)
1192 (goto-char isearch-barrier)))
117132a6 1193 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1194
1195
b457cc3b 1196(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1197
1198(defun isearch-other-meta-char ()
8f90f594 1199 "Exit the search normally and reread this key sequence.
35a4d143
RS
1200But only if `search-exit-option' is non-nil, the default.
1201If it is the symbol `edit', the search string is edited in the minibuffer
1202and the meta character is unread so that it applies to editing the string."
8e1cae6d 1203 (interactive)
c6431f12
KH
1204 (let* ((key (this-command-keys))
1205 (main-event (aref key 0))
1206 (keylist (listify-key-sequence key)))
24b5caec 1207 (cond ((and (= (length key) 1)
d94eb6eb 1208 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1209 (not (or (null lookup) (integerp lookup)
1210 (keymapp lookup)))))
24b5caec
RS
1211 ;; Handle a function key that translates into something else.
1212 ;; If the key has a global definition too,
1213 ;; exit and unread the key itself, so its global definition runs.
1214 ;; Otherwise, unread the translation,
1215 ;; so that the translated key takes effect within isearch.
d94eb6eb 1216 (cancel-kbd-macro-events)
24b5caec 1217 (if (lookup-key global-map key)
02b2f510 1218 (progn
24b5caec
RS
1219 (isearch-done)
1220 (apply 'isearch-unread keylist))
a5cc922e
KH
1221 (setq keylist
1222 (listify-key-sequence (lookup-key function-key-map key)))
1223 (while keylist
1224 (setq key (car keylist))
1225 ;; If KEY is a printing char, we handle it here
1226 ;; directly to avoid the input method and keyboard
1227 ;; coding system translating it.
1228 (if (and (integerp key)
ed5c183b 1229 (>= key ?\ ) (/= key 127) (< key 256))
a5cc922e
KH
1230 (progn
1231 (isearch-process-search-char key)
1232 (setq keylist (cdr keylist)))
1233 ;; As the remaining keys in KEYLIST can't be handled
1234 ;; here, we must reread them.
1235 (apply 'isearch-unread keylist)
1236 (setq keylist nil)))))
24b5caec 1237 (
c6431f12
KH
1238 ;; Handle an undefined shifted control character
1239 ;; by downshifting it if that makes it defined.
1240 ;; (As read-key-sequence would normally do,
1241 ;; if we didn't have a default definition.)
1242 (let ((mods (event-modifiers main-event)))
1243 (and (integerp main-event)
1244 (memq 'shift mods)
1245 (memq 'control mods)
1246 (lookup-key isearch-mode-map
1247 (let ((copy (copy-sequence key)))
1248 (aset copy 0
1249 (- main-event (- ?\C-\S-a ?\C-a)))
1250 copy)
1251 nil)))
1252 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1253 (cancel-kbd-macro-events)
c6431f12
KH
1254 (apply 'isearch-unread keylist))
1255 ((eq search-exit-option 'edit)
1256 (apply 'isearch-unread keylist)
1257 (isearch-edit-string))
1258 (search-exit-option
1259 (let (window)
d94eb6eb 1260 (cancel-kbd-macro-events)
c6431f12 1261 (apply 'isearch-unread keylist)
3fb01f36
GM
1262
1263 ;; Properly handle scroll-bar and mode-line clicks for
1264 ;; which a dummy prefix event was generated as (aref key
1265 ;; 0). Note that we don't have to modify the event
1266 ;; anymore in 21 because read_key_sequence no longer modifies
1267 ;; events to produce fake prefix keys.
1268 (when (and (> (length key) 1)
1269 (symbolp (aref key 0))
1270 (listp (aref key 1))
1271 (not (numberp (posn-point
1272 (event-start (aref key 1))))))
17635d50
GM
1273 (pop unread-command-events)
1274 (setq main-event (car unread-command-events)))
3fb01f36 1275
346f18dc
GM
1276 ;; If we got a mouse click event, that event contains the
1277 ;; window clicked on. maybe it was read with the buffer
c6431f12
KH
1278 ;; it was clicked on. If so, that buffer, not the current one,
1279 ;; is in isearch mode. So end the search in that buffer.
346f18dc
GM
1280
1281 ;; ??? I have no idea what this if checks for, but it's
1282 ;; obviously wrong for the case that a down-mouse event
1283 ;; on another window invokes this function. The event
1284 ;; will contain the window clicked on and that window's
1285 ;; buffer is certainaly not always in Isearch mode.
1286 ;;
1287 ;; Leave the code in, but check for current buffer not
1288 ;; being in Isearch mode for now, until someone tells
1289 ;; what it's really supposed to do.
1290 ;;
1291 ;; --gerd 2001-08-10.
1292
1293 (if (and (not isearch-mode)
1294 (listp main-event)
c6431f12 1295 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1296 (windowp window)
1297 (or (> (minibuffer-depth) 0)
1298 (not (window-minibuffer-p window))))
c6431f12
KH
1299 (save-excursion
1300 (set-buffer (window-buffer window))
0352b205
RS
1301 (isearch-done)
1302 (isearch-clean-overlays))
1303 (isearch-done)
1304 (isearch-clean-overlays))))
c6431f12
KH
1305 (t;; otherwise nil
1306 (isearch-process-search-string key key)))))
8e1cae6d 1307
8e1cae6d
JB
1308(defun isearch-quote-char ()
1309 "Quote special characters for incremental search."
1310 (interactive)
8bc15fa8 1311 (let ((char (read-quoted-char (isearch-message t))))
3c75023f 1312 ;; Assume character codes 0200 - 0377 stand for characters in some
c20d35d5
KH
1313 ;; single-byte character set, and convert them to Emacs
1314 ;; characters.
8bc15fa8 1315 (and enable-multibyte-characters
3c75023f 1316 (>= char ?\200)
8bc15fa8 1317 (<= char ?\377)
c20d35d5 1318 (setq char (unibyte-char-to-multibyte char)))
8bc15fa8 1319 (isearch-process-search-char char)))
8e1cae6d 1320
8e1cae6d
JB
1321(defun isearch-return-char ()
1322 "Convert return into newline for incremental search.
1323Obsolete."
1324 (interactive)
1325 (isearch-process-search-char ?\n))
1326
8e1cae6d 1327(defun isearch-printing-char ()
b68c164d 1328 "Add this ordinary printing character to the search string and search."
8e1cae6d 1329 (interactive)
117132a6 1330 (let ((char last-command-char))
45b94eb2
KH
1331 (if (= char ?\S-\ )
1332 (setq char ?\ ))
af56433d
RS
1333 (if (and enable-multibyte-characters
1334 (>= char ?\200)
1335 (<= char ?\377))
3c487103
KH
1336 (if (keyboard-coding-system)
1337 (isearch-process-search-multibyte-characters char)
1338 (isearch-process-search-char (unibyte-char-to-multibyte char)))
af56433d
RS
1339 (if current-input-method
1340 (isearch-process-search-multibyte-characters char)
1341 (isearch-process-search-char char)))))
8e1cae6d
JB
1342
1343(defun isearch-whitespace-chars ()
08200510 1344 "Match all whitespace chars, if in regexp mode.
fad241d3 1345If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
8e1cae6d 1346 (interactive)
08200510 1347 (if isearch-regexp
9da6682f
RS
1348 (if (and search-whitespace-regexp (not isearch-within-brackets)
1349 (not isearch-invalid-regexp))
08200510
RS
1350 (isearch-process-search-string search-whitespace-regexp " ")
1351 (isearch-printing-char))
1352 (progn
eb8c3be9 1353 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1354 ;; (setq isearch-word t)
1355 ;; (setq isearch-adjusted t)
1356 ;; (goto-char isearch-barrier)
1357 (isearch-printing-char))))
8e1cae6d
JB
1358
1359(defun isearch-process-search-char (char)
1360 ;; Append the char to the search string, update the message and re-search.
08200510 1361 (isearch-process-search-string
117132a6 1362 (char-to-string char)
8f3c1d76 1363 (if (>= char ?\200)
01a6ef79
RS
1364 (char-to-string char)
1365 (isearch-text-char-description char))))
8e1cae6d
JB
1366
1367(defun isearch-process-search-string (string message)
1368 (setq isearch-string (concat isearch-string string)
1369 isearch-message (concat isearch-message message))
1370 (isearch-search-and-update))
1371
1372\f
8e1cae6d
JB
1373;; Search Ring
1374
08200510
RS
1375(defun isearch-ring-adjust1 (advance)
1376 ;; Helper for isearch-ring-adjust
1377 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1378 (length (length ring))
1379 (yank-pointer-name (if isearch-regexp
08200510 1380 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1381 'search-ring-yank-pointer))
1382 (yank-pointer (eval yank-pointer-name)))
1383 (if (zerop length)
1384 ()
1385 (set yank-pointer-name
1386 (setq yank-pointer
ffbc30b2
PE
1387 (mod (+ (or yank-pointer 0)
1388 (if advance -1 1))
1389 length)))
a5dcf63f 1390 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1391 isearch-message (mapconcat 'isearch-text-char-description
1392 isearch-string "")))))
1393
1394(defun isearch-ring-adjust (advance)
1395 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1396 (isearch-ring-adjust1 advance)
08200510
RS
1397 (if search-ring-update
1398 (progn
1399 (isearch-search)
1400 (isearch-update))
1401 (isearch-edit-string)
2ff20b7e
RS
1402 )
1403 (isearch-push-state))
8e1cae6d
JB
1404
1405(defun isearch-ring-advance ()
1406 "Advance to the next search string in the ring."
08200510 1407 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1408 (interactive)
1409 (isearch-ring-adjust 'advance))
1410
1411(defun isearch-ring-retreat ()
1412 "Retreat to the previous search string in the ring."
1413 (interactive)
1414 (isearch-ring-adjust nil))
1415
a5dcf63f 1416(defun isearch-ring-advance-edit (n)
117132a6
DL
1417 "Insert the next element of the search history into the minibuffer.
1418With prefix arg N, insert the Nth element."
a5dcf63f
RS
1419 (interactive "p")
1420 (let* ((yank-pointer-name (if isearch-regexp
1421 'regexp-search-ring-yank-pointer
1422 'search-ring-yank-pointer))
1423 (yank-pointer (eval yank-pointer-name))
1424 (ring (if isearch-regexp regexp-search-ring search-ring))
1425 (length (length ring)))
1426 (if (zerop length)
1427 ()
1428 (set yank-pointer-name
1429 (setq yank-pointer
ffbc30b2
PE
1430 (mod (- (or yank-pointer 0) n)
1431 length)))
a5dcf63f 1432
b7b66466 1433 (delete-field)
35a4d143 1434 (insert (nth yank-pointer ring))
49c13105 1435 (goto-char (point-max)))))
a5dcf63f
RS
1436
1437(defun isearch-ring-retreat-edit (n)
117132a6
DL
1438 "Insert the previous element of the search history into the minibuffer.
1439With prefix arg N, insert the Nth element."
a5dcf63f
RS
1440 (interactive "p")
1441 (isearch-ring-advance-edit (- n)))
1442
1443;;(defun isearch-ring-adjust-edit (advance)
1444;; "Use the next or previous search string in the ring while in minibuffer."
1445;; (isearch-ring-adjust1 advance)
1446;; (erase-buffer)
1447;; (insert isearch-string))
1448
1449;;(defun isearch-ring-advance-edit ()
1450;; (interactive)
1451;; (isearch-ring-adjust-edit 'advance))
1452
1453;;(defun isearch-ring-retreat-edit ()
1454;; "Retreat to the previous search string in the ring while in the minibuffer."
1455;; (interactive)
1456;; (isearch-ring-adjust-edit nil))
08200510
RS
1457
1458
1459(defun isearch-complete1 ()
1460 ;; Helper for isearch-complete and isearch-complete-edit
1461 ;; Return t if completion OK, nil if no completion exists.
1462 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1463 (alist (mapcar (function (lambda (string) (list string))) ring))
1464 (completion-ignore-case case-fold-search)
1465 (completion (try-completion isearch-string alist)))
1466 (cond
1467 ((eq completion t)
1468 ;; isearch-string stays the same
1469 t)
1470 ((or completion ; not nil, must be a string
b7852303 1471 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1472 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1473 (progn
1474 (if completion-auto-help
1475 (with-output-to-temp-buffer "*Isearch completions*"
1476 (display-completion-list
1477 (all-completions isearch-string alist))))
1478 t)
1479 (and completion
1480 (setq isearch-string completion))))
08200510
RS
1481 (t
1482 (message "No completion") ; waits a second if in minibuffer
1483 nil))))
1484
1485(defun isearch-complete ()
1486 "Complete the search string from the strings on the search ring.
1487The completed string is then editable in the minibuffer.
1488If there is no completion possible, say so and continue searching."
1489 (interactive)
1490 (if (isearch-complete1)
1491 (isearch-edit-string)
1492 ;; else
1493 (sit-for 1)
1494 (isearch-update)))
8e1cae6d 1495
08200510
RS
1496(defun isearch-complete-edit ()
1497 "Same as `isearch-complete' except in the minibuffer."
1498 (interactive)
1499 (setq isearch-string (buffer-string))
1500 (if (isearch-complete1)
8e1cae6d 1501 (progn
b7b66466 1502 (delete-field)
08200510 1503 (insert isearch-string))))
8e1cae6d
JB
1504
1505\f
8e1cae6d 1506;; The search status stack (and isearch window-local variables, not used).
08200510 1507;; Need a structure for this.
8e1cae6d
JB
1508
1509(defun isearch-top-state ()
8e1cae6d
JB
1510 (let ((cmd (car isearch-cmds)))
1511 (setq isearch-string (car cmd)
1512 isearch-message (car (cdr cmd))
1513 isearch-success (nth 3 cmd)
1514 isearch-forward (nth 4 cmd)
1515 isearch-other-end (nth 5 cmd)
08200510
RS
1516 isearch-word (nth 6 cmd)
1517 isearch-invalid-regexp (nth 7 cmd)
1518 isearch-wrapped (nth 8 cmd)
c5f6b356 1519 isearch-barrier (nth 9 cmd)
f551b97d
RS
1520 isearch-within-brackets (nth 10 cmd)
1521 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1522 (goto-char (car (cdr (cdr cmd))))))
1523
1524(defun isearch-pop-state ()
8e1cae6d 1525 (setq isearch-cmds (cdr isearch-cmds))
1f4139d5 1526 (isearch-top-state))
8e1cae6d
JB
1527
1528(defun isearch-push-state ()
1529 (setq isearch-cmds
1530 (cons (list isearch-string isearch-message (point)
1531 isearch-success isearch-forward isearch-other-end
08200510 1532 isearch-word
c5f6b356 1533 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1534 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1535 isearch-cmds)))
1536
8e1cae6d 1537\f
8e1cae6d
JB
1538;; Message string
1539
1540(defun isearch-message (&optional c-q-hack ellipsis)
1541 ;; Generate and print the message string.
1542 (let ((cursor-in-echo-area ellipsis)
1543 (m (concat
08200510 1544 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1545 isearch-message
1546 (isearch-message-suffix c-q-hack ellipsis)
1547 )))
a56687f1
KH
1548 (if c-q-hack
1549 m
1550 (let ((message-log-max nil))
1551 (message "%s" m)))))
8e1cae6d 1552
08200510 1553(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1554 ;; If about to search, and previous search regexp was invalid,
1555 ;; check that it still is. If it is valid now,
1556 ;; let the message we display while searching say that it is valid.
1557 (and isearch-invalid-regexp ellipsis
1558 (condition-case ()
1559 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1560 (setq isearch-invalid-regexp nil
1561 isearch-within-brackets nil))
8e1cae6d
JB
1562 (error nil)))
1563 ;; If currently failing, display no ellipsis.
1564 (or isearch-success (setq ellipsis nil))
1565 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1566 (if (and isearch-wrapped
1567 (if isearch-forward
1568 (> (point) isearch-opoint)
1569 (< (point) isearch-opoint)))
1570 "over")
8e1cae6d 1571 (if isearch-wrapped "wrapped ")
08200510 1572 (if isearch-word "word " "")
8e1cae6d 1573 (if isearch-regexp "regexp " "")
08200510 1574 (if nonincremental "search" "I-search")
f46d5091 1575 (if isearch-forward "" " backward")
5b56d4e4
KH
1576 (if current-input-method
1577 (concat " [" current-input-method-title "]: ")
f46d5091 1578 ": ")
8e1cae6d 1579 )))
c5def0db 1580 (concat (upcase (substring m 0 1)) (substring m 1))))
8e1cae6d
JB
1581
1582
1583(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1584 (concat (if c-q-hack "^Q" "")
1585 (if isearch-invalid-regexp
1586 (concat " [" isearch-invalid-regexp "]")
1587 "")))
1588
1589\f
8e1cae6d
JB
1590;;; Searching
1591
1592(defun isearch-search ()
1593 ;; Do the search with the current search string.
1594 (isearch-message nil t)
4453091d 1595 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1596 (setq isearch-case-fold-search
1597 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1598 (condition-case lossage
79c7a4fa
RS
1599 (let ((inhibit-point-motion-hooks search-invisible)
1600 (inhibit-quit nil)
86bfaffe
RS
1601 (case-fold-search isearch-case-fold-search)
1602 (retry t))
8e1cae6d 1603 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1604 (setq isearch-within-brackets nil)
86bfaffe
RS
1605 (while retry
1606 (setq isearch-success
1607 (funcall
1608 (cond (isearch-word
1609 (if isearch-forward
1610 'word-search-forward 'word-search-backward))
1611 (isearch-regexp
1612 (if isearch-forward
1613 're-search-forward 're-search-backward))
1614 (t
1615 (if isearch-forward 'search-forward 'search-backward)))
1616 isearch-string nil t))
1617 ;; Clear RETRY unless we matched some invisible text
1618 ;; and we aren't supposed to do that.
0352b205 1619 (if (or (eq search-invisible t)
86bfaffe
RS
1620 (not isearch-success)
1621 (bobp) (eobp)
1622 (= (match-beginning 0) (match-end 0))
1623 (not (isearch-range-invisible
1624 (match-beginning 0) (match-end 0))))
1625 (setq retry nil)))
99ae9e9f 1626 (setq isearch-just-started nil)
8e1cae6d
JB
1627 (if isearch-success
1628 (setq isearch-other-end
1629 (if isearch-forward (match-beginning 0) (match-end 0)))))
1630
d32696d4 1631 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1632 (setq isearch-success nil))
1633
1634 (invalid-regexp
1635 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1636 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1637 isearch-invalid-regexp))
8e1cae6d
JB
1638 (if (string-match
1639 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1640 isearch-invalid-regexp)
f1c03125
RS
1641 (setq isearch-invalid-regexp "incomplete input")))
1642 (error
c982ab21 1643 ;; stack overflow in regexp search.
c5348233 1644 (setq isearch-invalid-regexp (format "%s" lossage))))
8e1cae6d
JB
1645
1646 (if isearch-success
1647 nil
1648 ;; Ding if failed this time after succeeding last time.
1649 (and (nth 3 (car isearch-cmds))
1650 (ding))
1651 (goto-char (nth 2 (car isearch-cmds)))))
1652
0352b205
RS
1653
1654;;; Called when opening an overlay, and we are still in isearch.
1655(defun isearch-open-overlay-temporary (ov)
0352b205
RS
1656 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1657 ;; Some modes would want to open the overlays temporary during
1658 ;; isearch in their own way, they should set the
1659 ;; `isearch-open-invisible-temporary' to a function doing this.
1660 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1661 ;; Store the values for the `invisible' and `intangible'
1662 ;; properties, and then set them to nil. This way the text hidden
1663 ;; by this overlay becomes visible.
1664
380866a2 1665 ;; Do we really need to set the `intangible' property to t? Can we
0352b205
RS
1666 ;; have the point inside an overlay with an `intangible' property?
1667 ;; In 19.34 this does not exist so I cannot test it.
1668 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1669 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1670 (overlay-put ov 'invisible nil)
1671 (overlay-put ov 'intangible nil)))
1672
1673
117132a6
DL
1674;;; This is called at the end of isearch. It will open the overlays
1675;;; that contain the latest match. Obviously in case of a C-g the
0352b205
RS
1676;;; point returns to the original location which surely is not contain
1677;;; in any of these overlays, se we are safe in this case too.
1678(defun isearch-open-necessary-overlays (ov)
1679 (let ((inside-overlay (and (> (point) (overlay-start ov))
1680 (< (point) (overlay-end ov))))
1681 ;; If this exists it means that the overlay was opened using
1682 ;; this function, not by us tweaking the overlay properties.
1683 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1684 (when (or inside-overlay (not fct-temp))
1685 ;; restore the values for the `invisible' and `intangible'
1686 ;; properties
1687 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1688 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1689 (overlay-put ov 'isearch-invisible nil)
1690 (overlay-put ov 'isearch-intangible nil))
1691 (if inside-overlay
1692 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1693 (if fct-temp
1694 (funcall fct-temp ov t)))))
1695
1696;;; This is called when exiting isearch. It closes the temporary
1697;;; opened overlays, except the ones that contain the latest match.
1698(defun isearch-clean-overlays ()
1699 (when isearch-opened-overlays
02b2f510 1700 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
0352b205
RS
1701 (setq isearch-opened-overlays nil)))
1702
fc0eccfc
GM
1703
1704(defun isearch-intersects-p (start0 end0 start1 end1)
1705 "Return t if regions START0..END0 and START1..END1 intersect."
eaa493df
GM
1706 (or (and (>= start0 start1) (< start0 end1))
1707 (and (> end0 start1) (<= end0 end1))
1708 (and (>= start1 start0) (< start1 end0))
1709 (and (> end1 start0) (<= end1 end0))))
fc0eccfc
GM
1710
1711
0352b205
RS
1712;;; Verify if the current match is outside of each element of
1713;;; `isearch-opened-overlays', if so close that overlay.
fc0eccfc
GM
1714
1715(defun isearch-close-unnecessary-overlays (begin end)
1716 (let ((overlays isearch-opened-overlays))
0352b205 1717 (setq isearch-opened-overlays nil)
fc0eccfc
GM
1718 (dolist (ov overlays)
1719 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
1720 (push ov isearch-opened-overlays)
1721 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1722 (if fct-temp
1723 ;; If this exists it means that the overlay was opened
1724 ;; using this function, not by us tweaking the overlay
1725 ;; properties.
1726 (funcall fct-temp ov t)
1727 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1728 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1729 (overlay-put ov 'isearch-invisible nil)
1730 (overlay-put ov 'isearch-intangible nil)))))))
1731
0352b205 1732
86bfaffe 1733(defun isearch-range-invisible (beg end)
79c7a4fa 1734 "Return t if all the text from BEG to END is invisible."
86bfaffe
RS
1735 (and (/= beg end)
1736 ;; Check that invisibility runs up to END.
1737 (save-excursion
1738 (goto-char beg)
df01192b
RS
1739 (let (
1740 ;; can-be-opened keeps track if we can open some overlays.
1741 (can-be-opened (eq search-invisible 'open))
1742 ;; the list of overlays that could be opened
1743 (crt-overlays nil))
0352b205 1744 (when (and can-be-opened isearch-hide-immediately)
fc0eccfc 1745 (isearch-close-unnecessary-overlays beg end))
0352b205
RS
1746 ;; If the following character is currently invisible,
1747 ;; skip all characters with that same `invisible' property value.
1748 ;; Do that over and over.
1749 (while (and (< (point) end)
1750 (let ((prop
1751 (get-char-property (point) 'invisible)))
1752 (if (eq buffer-invisibility-spec t)
1753 prop
1754 (or (memq prop buffer-invisibility-spec)
1755 (assq prop buffer-invisibility-spec)))))
1756 (if (get-text-property (point) 'invisible)
1757 (progn
1758 (goto-char (next-single-property-change (point) 'invisible
1759 nil end))
1760 ;; if text is hidden by an `invisible' text property
1761 ;; we cannot open it at all.
1762 (setq can-be-opened nil))
1763 (unless (null can-be-opened)
1764 (let ((overlays (overlays-at (point)))
1765 ov-list
1766 o
1767 invis-prop)
1768 (while overlays
1769 (setq o (car overlays)
1770 invis-prop (overlay-get o 'invisible))
de207f5a
KH
1771 (if (if (eq buffer-invisibility-spec t)
1772 invis-prop
1773 (or (memq invis-prop buffer-invisibility-spec)
1774 (assq invis-prop buffer-invisibility-spec)))
0352b205
RS
1775 (if (overlay-get o 'isearch-open-invisible)
1776 (setq ov-list (cons o ov-list))
1777 ;; We found one overlay that cannot be
1778 ;; opened, that means the whole chunk
1779 ;; cannot be opened.
1780 (setq can-be-opened nil)))
1781 (setq overlays (cdr overlays)))
1782 (if can-be-opened
1783 ;; It makes sense to append to the open
1784 ;; overlays list only if we know that this is
1785 ;; t.
7e97482b
RS
1786 (setq crt-overlays (append ov-list crt-overlays)))))
1787 (goto-char (next-overlay-change (point)))))
86bfaffe 1788 ;; See if invisibility reaches up thru END.
0352b205
RS
1789 (if (>= (point) end)
1790 (if (and (not (null can-be-opened)) (consp crt-overlays))
1791 (progn
1792 (setq isearch-opened-overlays
1793 (append isearch-opened-overlays crt-overlays))
02b2f510 1794 (mapc 'isearch-open-overlay-temporary crt-overlays)
0352b205
RS
1795 nil)
1796 t))))))
08200510
RS
1797
1798\f
08200510
RS
1799;;; Highlighting
1800
b78559b0 1801(defvar isearch-overlay nil)
08200510 1802
b78559b0 1803(defun isearch-highlight (beg end)
8005142f 1804 (unless (null search-highlight)
4fb2ad98
MB
1805 (cond (isearch-overlay
1806 ;; Overlay already exists, just move it.
4fb2ad98
MB
1807 (move-overlay isearch-overlay beg end (current-buffer)))
1808
1809 (t
1810 ;; Overlay doesn't exist, create it.
1811 (setq isearch-overlay (make-overlay beg end))
c982ab21
GM
1812 (overlay-put isearch-overlay 'face isearch)
1813 (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
1814 ))))
b78559b0
RS
1815
1816(defun isearch-dehighlight (totally)
4fb2ad98 1817 (when isearch-overlay
4fb2ad98
MB
1818 (delete-overlay isearch-overlay)))
1819
08200510 1820
08200510
RS
1821;;; General utilities
1822
08200510 1823
b78559b0
RS
1824(defun isearch-no-upper-case-p (string regexp-flag)
1825 "Return t if there are no upper case chars in STRING.
b7852303 1826If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1827since they have special meaning in a regexp."
59057198
RS
1828 (let (quote-flag (i 0) (len (length string)) found)
1829 (while (and (not found) (< i len))
1830 (let ((char (aref string i)))
1831 (if (and regexp-flag (eq char ?\\))
1832 (setq quote-flag (not quote-flag))
1833 (if (and (not quote-flag) (not (eq char (downcase char))))
1834 (setq found t))))
1835 (setq i (1+ i)))
1836 (not found)))
08200510 1837
b78559b0 1838;; Portability functions to support various Emacs versions.
8e1cae6d 1839
08200510 1840(defun isearch-text-char-description (c)
14373677
SM
1841 (cond
1842 ((< c ?\ ) (format "^%c" (+ c 64)))
1843 ((= c ?\^?) "^?")
1844 (t (char-to-string c))))
08200510 1845
bd1bd125 1846;; General function to unread characters or events.
99ae9e9f 1847;; Also insert them in a keyboard macro being defined.
8f90f594 1848(defun isearch-unread (&rest char-or-events)
02b2f510 1849 (mapc 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1850 (setq unread-command-events
1851 (append char-or-events unread-command-events)))
08200510 1852
44336afb
GM
1853\f
1854;;; isearch-lazy-highlight feature
1855;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1856
1857;;; When active, *every* match for the current search string is
1858;;; highlighted: the current one using the normal isearch match color
c982ab21
GM
1859;;; and all the others using `isearch-lazy-highlight-face'. The extra
1860;;; highlighting makes it easier to anticipate where the cursor will
1861;;; land each time you press C-s or C-r to repeat a pending search.
1862;;; Highlighting of these additional matches happens in a deferred
1863;;; fashion using "idle timers," so the cycles needed do not rob
1864;;; isearch of its usual snappy response.
44336afb
GM
1865
1866;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1867;;; Specifically:
1868;;; - `isearch-update' is expected to be called (at least) every time
c982ab21 1869;;; the search string or window-start changes;
44336afb
GM
1870;;; - `isearch-string' is expected to contain the current search
1871;;; string as entered by the user;
44336afb
GM
1872;;; - the type of the current search is expected to be given by
1873;;; `isearch-word' and `isearch-regexp';
1874;;; - the direction of the current search is expected to be given by
1875;;; `isearch-forward';
1876;;; - the variable `isearch-invalid-regexp' is expected to be true
1877;;; iff `isearch-string' is an invalid regexp.
1878
1879(require 'timer)
1880
1881(defgroup isearch-lazy-highlight nil
1882 "Lazy highlighting feature for incremental search."
1883 :prefix "isearch-lazy-highlight-"
39f2174e 1884 :version "21.1"
44336afb
GM
1885 :group 'isearch)
1886
1887(defcustom isearch-lazy-highlight t
1888 "*Controls the lazy-highlighting during incremental searches.
1889When non-nil, all text in the buffer matching the current search
1890string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1891and `isearch-lazy-highlight-interval')."
1892 :type 'boolean
1893 :group 'isearch-lazy-highlight)
1894
1895(defcustom isearch-lazy-highlight-cleanup t
1896 "*Controls whether to remove extra highlighting after a search.
1897If this is nil, extra highlighting can be \"manually\" removed with
1898\\[isearch-lazy-highlight-cleanup]."
1899 :type 'boolean
1900 :group 'isearch-lazy-highlight)
1901
871e0439 1902(defcustom isearch-lazy-highlight-initial-delay 0.25
44336afb
GM
1903 "*Seconds to wait before beginning to lazily highlight all matches."
1904 :type 'number
1905 :group 'isearch-lazy-highlight)
1906
b8fbd474 1907(defcustom isearch-lazy-highlight-interval 0 ; 0.0625
44336afb
GM
1908 "*Seconds between lazily highlighting successive matches."
1909 :type 'number
1910 :group 'isearch-lazy-highlight)
1911
c982ab21
GM
1912(defcustom isearch-lazy-highlight-max-at-a-time 20
1913 "*Maximum matches to highlight at a time (for `isearch-lazy-highlight').
1914Larger values may reduce isearch's responsiveness to user input;
1915smaller values make matches highlight slowly.
1916A value of nil means highlight all matches."
68ebbb95
GM
1917 :type '(choice (const :tag "All" nil)
1918 (integer :tag "Some"))
1919 :group 'isearch-lazy-highlight)
1920
18ca20aa
GM
1921(defgroup isearch-faces nil
1922 "Lazy highlighting feature for incremental search."
1923 :version "21.1"
1924 :group 'isearch)
1925
1926(defface isearch
29910493
EZ
1927 '((((type tty pc) (class color))
1928 (:background "magenta4" :foreground "cyan1"))
1929 (((class color) (background light))
b4d7a2ec
RS
1930 ;; The background must not be too dark, for that means
1931 ;; the character is hard to see when the cursor is there.
5bcf0054 1932 (:background "magenta2" :foreground "lightskyblue1"))
29910493
EZ
1933 (((class color) (background dark))
1934 (:background "palevioletred2" :foreground "brown4"))
1935 (t (:inverse-video t)))
1936 "Face for highlighting Isearch matches."
18ca20aa
GM
1937 :group 'isearch-faces)
1938(defvar isearch 'isearch)
1939
1940(defface isearch-lazy-highlight-face
29910493
EZ
1941 '((((type tty pc) (class color))
1942 (:background "turquoise3"))
1943 (((class color) (background light))
1944 (:background "paleturquoise"))
1945 (((class color) (background dark))
1946 (:background "paleturquoise4"))
1947 (t (:underline t)))
1948 "Face for lazy highlighting of Isearch matches other than the current one."
18ca20aa
GM
1949 :group 'isearch-faces)
1950(defvar isearch-lazy-highlight-face 'isearch-lazy-highlight-face)
44336afb
GM
1951
1952(defvar isearch-lazy-highlight-overlays nil)
c982ab21 1953(defvar isearch-lazy-highlight-wrapped nil)
44336afb
GM
1954(defvar isearch-lazy-highlight-start nil)
1955(defvar isearch-lazy-highlight-end nil)
1956(defvar isearch-lazy-highlight-timer nil)
1957(defvar isearch-lazy-highlight-last-string nil)
c982ab21
GM
1958(defvar isearch-lazy-highlight-window nil)
1959(defvar isearch-lazy-highlight-window-start nil)
46daf6c7
GM
1960(defvar isearch-lazy-highlight-case-fold-search nil)
1961(defvar isearch-lazy-highlight-regexp nil)
44336afb 1962
c982ab21
GM
1963(defun isearch-lazy-highlight-cleanup (&optional force)
1964 "Stop lazy highlighting and remove extra highlighting from current buffer.
1965FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
1966is nil. This function is called when exiting an incremental search if
1967`isearch-lazy-highlight-cleanup' is non-nil."
44336afb 1968 (interactive '(t))
c982ab21
GM
1969 (if (or force isearch-lazy-highlight-cleanup)
1970 (while isearch-lazy-highlight-overlays
1971 (delete-overlay (car isearch-lazy-highlight-overlays))
1972 (setq isearch-lazy-highlight-overlays
1973 (cdr isearch-lazy-highlight-overlays))))
1974 (when isearch-lazy-highlight-timer
1975 (cancel-timer isearch-lazy-highlight-timer)
1976 (setq isearch-lazy-highlight-timer nil)))
44336afb
GM
1977
1978(defun isearch-lazy-highlight-new-loop ()
c982ab21 1979 "Cleanup any previous `isearch-lazy-highlight' loop and begin a new one.
44336afb 1980This happens when `isearch-update' is invoked (which can cause the
c982ab21 1981search string to change or the window to scroll)."
67ecad4e 1982 (when (and isearch-lazy-highlight
c982ab21
GM
1983 (sit-for 0) ;make sure (window-start) is credible
1984 (or (not (equal isearch-string
1985 isearch-lazy-highlight-last-string))
1986 (not (eq (selected-window)
1987 isearch-lazy-highlight-window))
46daf6c7
GM
1988 (not (eq isearch-lazy-highlight-case-fold-search
1989 isearch-case-fold-search))
1990 (not (eq isearch-lazy-highlight-regexp
1991 isearch-regexp))
c982ab21
GM
1992 (not (= (window-start)
1993 isearch-lazy-highlight-window-start))))
1994 ;; something important did indeed change
1995 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
1996 (when (not isearch-invalid-regexp)
1997 (setq isearch-lazy-highlight-window (selected-window)
1998 isearch-lazy-highlight-window-start (window-start)
1999 isearch-lazy-highlight-start (point)
2000 isearch-lazy-highlight-end (point)
2001 isearch-lazy-highlight-last-string isearch-string
46daf6c7
GM
2002 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2003 isearch-lazy-highlight-regexp isearch-regexp
c982ab21
GM
2004 isearch-lazy-highlight-wrapped nil)
2005 (setq isearch-lazy-highlight-timer
2006 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
2007 'isearch-lazy-highlight-update)))))
2008
2009(defun isearch-lazy-highlight-search ()
2010 "Search ahead for the next or previous match, for lazy highlighting.
2011Attempt to do the search exactly the way the pending isearch would."
2012 (let ((case-fold-search isearch-case-fold-search)
2013 (choices (cond (isearch-word
2014 '(word-search-forward . word-search-backward))
2015 (isearch-regexp
2016 '(re-search-forward . re-search-backward))
2017 (t
2018 '(search-forward . search-backward)))))
2019 (funcall (if isearch-forward
2020 (car choices)
2021 (cdr choices))
2022 isearch-string
2023 (if isearch-forward
2024 (if isearch-lazy-highlight-wrapped
2025 isearch-lazy-highlight-start
2026 (window-end))
2027 (if isearch-lazy-highlight-wrapped
2028 isearch-lazy-highlight-end
2029 (window-start)))
2030 t)))
44336afb
GM
2031
2032(defun isearch-lazy-highlight-update ()
c982ab21
GM
2033 "Update highlighting of other matches for current search."
2034 (let ((max isearch-lazy-highlight-max-at-a-time)
2035 (looping t)
2036 nomore)
7742ee19
RS
2037 (save-excursion
2038 (save-match-data
c982ab21
GM
2039 (goto-char (if isearch-forward
2040 isearch-lazy-highlight-end
2041 isearch-lazy-highlight-start))
2042 (while looping
2043 (let ((found (isearch-lazy-highlight-search)))
2044 (when max
2045 (setq max (1- max))
2046 (if (<= max 0)
2047 (setq looping nil)))
2048 (if found
2049 (let ((mb (match-beginning 0))
2050 (me (match-end 0)))
2051 (if (= mb me) ;zero-length match
2052 (forward-char 1)
2053
2054 ;; non-zero-length match
2055 (let ((ov (make-overlay mb me)))
2056 (overlay-put ov 'face isearch-lazy-highlight-face)
2057 (overlay-put ov 'priority 0) ;lower than main overlay
2058 (overlay-put ov 'window (selected-window))
2059 (push ov isearch-lazy-highlight-overlays)))
2060 (if isearch-forward
2061 (setq isearch-lazy-highlight-end (point))
2062 (setq isearch-lazy-highlight-start (point))))
2063
2064 ;; not found
2065 (if isearch-lazy-highlight-wrapped
2066 (setq looping nil
2067 nomore t)
2068 (setq isearch-lazy-highlight-wrapped t)
2069 (if isearch-forward
2070 (progn
2071 (setq isearch-lazy-highlight-end (window-start))
2072 (goto-char (window-start)))
2073 (setq isearch-lazy-highlight-start (window-end))
2074 (goto-char (window-end)))))))
2075 (unless nomore
2076 (setq isearch-lazy-highlight-timer
2077 (run-at-time isearch-lazy-highlight-interval nil
2078 'isearch-lazy-highlight-update)))))))
44336afb 2079
c40a4de1
GM
2080(defun isearch-resume (search regexp word forward message case-fold)
2081 "Resume an incremental search.
2082SEARCH is the string or regexp searched for.
2083REGEXP non-nil means the resumed search was a regexp search.
2084WORD non-nil means resume a word search.
2085FORWARD non-nil means resume a forward search.
2086MESSAGE is the echo-area message recorded for the search resumed.
2087CASE-FOLD non-nil means the search was case-insensitive."
2088 (isearch-mode forward regexp nil nil word)
2089 (setq isearch-string search
2090 isearch-message message
2091 isearch-case-fold-search case-fold)
2092 (isearch-search))
2093
3b1e4dd1 2094;;; isearch.el ends here