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