* isearch.el (isearch-highlight-regexp): Fix case of highlighted
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
55535639 1;;; isearch.el --- incremental search minor mode
3b1e4dd1 2
0d30b337 3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
409cc4a3 4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
59243403 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
59243403 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
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
8e1cae6d 24
3b1e4dd1
ER
25;;; Commentary:
26
8e1cae6d
JB
27;; Instructions
28
08200510
RS
29;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
30;; isearch-mode behaves modally and does not return until the search
117132a6 31;; is completed. It uses a recursive-edit to behave this way.
08200510 32
8e1cae6d 33;; The key bindings active within isearch-mode are defined below in
08200510
RS
34;; `isearch-mode-map' which is given bindings close to the default
35;; characters of the original isearch.el. With `isearch-mode',
36;; however, you can bind multi-character keys and it should be easier
37;; to add new commands. One bug though: keys with meta-prefix cannot
38;; be longer than two chars. Also see minibuffer-local-isearch-map
39;; for bindings active during `isearch-edit-string'.
40
117132a6
DL
41;; isearch-mode should work even if you switch windows with the mouse,
42;; in which case isearch-mode is terminated automatically before the
43;; switch.
08200510
RS
44
45;; The search ring and completion commands automatically put you in
46;; the minibuffer to edit the string. This gives you a chance to
47;; modify the search string before executing the search. There are
48;; three commands to terminate the editing: C-s and C-r exit the
49;; minibuffer and search forward and reverse respectively, while C-m
50;; exits and does a nonincremental search.
51
52;; Exiting immediately from isearch uses isearch-edit-string instead
53;; of nonincremental-search, if search-nonincremental-instead is non-nil.
54;; The name of this option should probably be changed if we decide to
55;; keep the behavior. No point in forcing nonincremental search until
56;; the last possible moment.
57
3b1e4dd1 58;;; Code:
08200510
RS
59
60\f
191f025a 61;; Some additional options and constants.
08200510 62
9c1db929
RS
63(defgroup isearch nil
64 "Incremental search minor mode."
117132a6
DL
65 :link '(emacs-commentary-link "isearch")
66 :link '(custom-manual "(emacs)Incremental Search")
9c1db929
RS
67 :prefix "isearch-"
68 :prefix "search-"
69 :group 'matching)
fdf5afa4 70
9c1db929
RS
71
72(defcustom search-exit-option t
73 "*Non-nil means random control characters terminate incremental search."
74 :type 'boolean
75 :group 'isearch)
76
77(defcustom search-slow-window-lines 1
fdf5afa4
RS
78 "*Number of lines in slow search display windows.
79These are the short windows used during incremental search on slow terminals.
80Negative means put the slow search window at the top (normally it's at bottom)
9c1db929
RS
81and the value is minus the number of lines."
82 :type 'integer
83 :group 'isearch)
fdf5afa4 84
9c1db929 85(defcustom search-slow-speed 1200
fdf5afa4
RS
86 "*Highest terminal speed at which to use \"slow\" style incremental search.
87This is the style where a one-line window is created to show the line
9c1db929
RS
88that the search has reached."
89 :type 'integer
90 :group 'isearch)
8e1cae6d 91
9c1db929 92(defcustom search-upper-case 'not-yanks
8e1cae6d 93 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
94That is, upper and lower case chars must match exactly.
95This applies no matter where the chars come from, but does not
fdf5afa4 96apply to chars in regexps that are prefixed with `\\'.
6e3057bb
JL
97If this value is `not-yanks', text yanked into the search string
98in Isearch mode is always downcased."
9c1db929
RS
99 :type '(choice (const :tag "off" nil)
100 (const not-yanks)
5786b3ed 101 (other :tag "on" t))
9c1db929 102 :group 'isearch)
8e1cae6d 103
9c1db929 104(defcustom search-nonincremental-instead t
8e1cae6d 105 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510 106Actually, `isearch-edit-string' is called to let you enter the search
9c1db929
RS
107string, and RET terminates editing and does a nonincremental search."
108 :type 'boolean
109 :group 'isearch)
8e1cae6d 110
ab67e8b6 111(defcustom search-whitespace-regexp "\\s-+"
8e1cae6d 112 "*If non-nil, regular expression to match a sequence of whitespace chars.
e2b992cb 113This applies to regular expression incremental search.
ab67e8b6
RS
114When you put a space or spaces in the incremental regexp, it stands for
115this, unless it is inside of a regexp construct such as [...] or *, + or ?.
116You might want to use something like \"[ \\t\\r\\n]+\" instead.
117In the Customization buffer, that is `[' followed by a space,
51a77cba
RS
118a tab, a carriage return (control-M), a newline, and `]+'.
119
120When this is nil, each space you type matches literally, against one space."
121 :type '(choice (const :tag "Find Spaces Literally" nil)
122 regexp)
9c1db929 123 :group 'isearch)
8e1cae6d 124
0352b205
RS
125(defcustom search-invisible 'open
126 "If t incremental search can match hidden text.
6480c508
JB
127A nil value means don't match invisible text.
128When the value is `open', if the text matched is made invisible by
0352b205
RS
129an overlay having an `invisible' property and that overlay has a property
130`isearch-open-invisible', then incremental search will show the contents.
cd59ea72
SM
131\(This applies when using `outline.el' and `hideshow.el'.)
132See also `reveal-mode' if you want overlays to automatically be opened
133whenever point is in one of them."
0352b205
RS
134 :type '(choice (const :tag "Match hidden text" t)
135 (const :tag "Open overlays" open)
79c7a4fa 136 (const :tag "Don't match hidden text" nil))
0352b205
RS
137 :group 'isearch)
138
139(defcustom isearch-hide-immediately t
68c18d6d
RS
140 "If non-nil, re-hide an invisible match right away.
141This variable makes a difference when `search-invisible' is set to `open'.
142It means that after search makes some invisible text visible
143to show the match, it makes the text invisible again when the match moves.
b48ca14f
JB
144Ordinarily the text becomes invisible again at the end of the search."
145 :type 'boolean
0352b205 146 :group 'isearch)
86bfaffe 147
00098b01
KS
148(defcustom isearch-resume-in-command-history nil
149 "*If non-nil, `isearch-resume' commands are added to the command history.
150This allows you to resume earlier isearch sessions through the
151command history."
6848c9f1
KS
152 :type 'boolean
153 :group 'isearch)
154
08200510
RS
155(defvar isearch-mode-hook nil
156 "Function(s) to call after starting up an incremental search.")
157
158(defvar isearch-mode-end-hook nil
87e97673
RS
159 "Function(s) to call after terminating an incremental search.
160When these functions are called, `isearch-mode-end-hook-quit'
4974aba8 161is non-nil if the user quits the search.")
87e97673
RS
162
163(defvar isearch-mode-end-hook-quit nil
4974aba8 164 "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
08200510 165
ac475d3a
JL
166(defvar isearch-message-function nil
167 "Function to call to display the search prompt.
168If nil, use `isearch-message'.")
169
6a18e4e7
JL
170(defvar isearch-wrap-function nil
171 "Function to call to wrap the search when search is failed.
172If nil, move point to the beginning of the buffer for a forward search,
173or to the end of the buffer for a backward search.")
174
175(defvar isearch-push-state-function nil
176 "Function to save a function restoring the mode-specific isearch state
177to the search status stack.")
178
5e189f39
JL
179(defvar isearch-success-function 'isearch-success-function-default
180 "Function to report whether the new search match is considered successful.
181The function has two arguments: the positions of start and end of text
b92368b4 182matched by the search. If this function returns nil, continue
5e189f39
JL
183searching without stopping at this match.")
184
191f025a 185;; Search ring.
8e1cae6d
JB
186
187(defvar search-ring nil
188 "List of search string sequences.")
08200510 189(defvar regexp-search-ring nil
8e1cae6d
JB
190 "List of regular expression search string sequences.")
191
9c1db929
RS
192(defcustom search-ring-max 16
193 "*Maximum length of search ring before oldest elements are thrown away."
194 :type 'integer
195 :group 'isearch)
196(defcustom regexp-search-ring-max 16
197 "*Maximum length of regexp search ring before oldest elements are thrown away."
198 :type 'integer
199 :group 'isearch)
8e1cae6d
JB
200
201(defvar search-ring-yank-pointer nil
a5dcf63f 202 "Index in `search-ring' of last string reused.
6480c508 203It is nil if none yet.")
08200510 204(defvar regexp-search-ring-yank-pointer nil
a5dcf63f 205 "Index in `regexp-search-ring' of last string reused.
6480c508 206It is nil if none yet.")
8e1cae6d 207
9c1db929 208(defcustom search-ring-update nil
08200510 209 "*Non-nil if advancing or retreating in the search ring should cause search.
9c1db929
RS
210Default value, nil, means edit the string instead."
211 :type 'boolean
212 :group 'isearch)
08200510 213
bca92193
JL
214;;; isearch highlight customization.
215
216(defcustom search-highlight t
217 "*Non-nil means incremental search highlights the current match."
218 :type 'boolean
219 :group 'isearch)
220
221(defface isearch
222 '((((class color) (min-colors 88) (background light))
223 ;; The background must not be too dark, for that means
224 ;; the character is hard to see when the cursor is there.
a49ff341 225 (:background "magenta3" :foreground "lightskyblue1"))
bca92193
JL
226 (((class color) (min-colors 88) (background dark))
227 (:background "palevioletred2" :foreground "brown4"))
228 (((class color) (min-colors 16))
229 (:background "magenta4" :foreground "cyan1"))
230 (((class color) (min-colors 8))
231 (:background "magenta4" :foreground "cyan1"))
232 (t (:inverse-video t)))
233 "Face for highlighting Isearch matches."
92cc4a30
JL
234 :group 'isearch
235 :group 'basic-faces)
bca92193
JL
236(defvar isearch 'isearch)
237
d8891294
JL
238(defface isearch-fail
239 '((((class color) (min-colors 88) (background light))
240 (:background "RosyBrown1"))
241 (((class color) (min-colors 88) (background dark))
242 (:background "red4"))
243 (((class color) (min-colors 16))
244 (:background "red"))
245 (((class color) (min-colors 8))
246 (:background "red"))
247 (((class color grayscale))
248 :foreground "grey")
249 (t (:inverse-video t)))
723e5b84 250 "Face for highlighting failed part in Isearch echo-area message."
d8891294 251 :version "23.1"
723e5b84
BG
252 :group 'isearch)
253
bca92193
JL
254(defcustom isearch-lazy-highlight t
255 "*Controls the lazy-highlighting during incremental search.
256When non-nil, all text in the buffer matching the current search
257string is highlighted lazily (see `lazy-highlight-initial-delay'
258and `lazy-highlight-interval')."
259 :type 'boolean
260 :group 'lazy-highlight
261 :group 'isearch)
262
c1bc6bb6 263;;; Lazy highlight customization.
bca92193 264
c1bc6bb6
RS
265(defgroup lazy-highlight nil
266 "Lazy highlighting feature for matching strings."
267 :prefix "lazy-highlight-"
268 :version "21.1"
269 :group 'isearch
bca92193 270 :group 'matching)
c1bc6bb6
RS
271
272(defcustom lazy-highlight-cleanup t
273 "*Controls whether to remove extra highlighting after a search.
274If this is nil, extra highlighting can be \"manually\" removed with
46fe9018 275\\[lazy-highlight-cleanup]."
c1bc6bb6
RS
276 :type 'boolean
277 :group 'lazy-highlight)
6480c508
JB
278(define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
279 'lazy-highlight-cleanup
280 "22.1")
c1bc6bb6
RS
281
282(defcustom lazy-highlight-initial-delay 0.25
283 "*Seconds to wait before beginning to lazily highlight all matches."
284 :type 'number
285 :group 'lazy-highlight)
6480c508
JB
286(define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
287 'lazy-highlight-initial-delay
288 "22.1")
c1bc6bb6
RS
289
290(defcustom lazy-highlight-interval 0 ; 0.0625
291 "*Seconds between lazily highlighting successive matches."
292 :type 'number
293 :group 'lazy-highlight)
6480c508
JB
294(define-obsolete-variable-alias 'isearch-lazy-highlight-interval
295 'lazy-highlight-interval
296 "22.1")
c1bc6bb6
RS
297
298(defcustom lazy-highlight-max-at-a-time 20
299 "*Maximum matches to highlight at a time (for `lazy-highlight').
300Larger values may reduce isearch's responsiveness to user input;
301smaller values make matches highlight slowly.
302A value of nil means highlight all matches."
303 :type '(choice (const :tag "All" nil)
304 (integer :tag "Some"))
305 :group 'lazy-highlight)
6480c508
JB
306(define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
307 'lazy-highlight-max-at-a-time
308 "22.1")
c1bc6bb6 309
e3cde0c7 310(defface lazy-highlight
c1bc6bb6
RS
311 '((((class color) (min-colors 88) (background light))
312 (:background "paleturquoise"))
313 (((class color) (min-colors 88) (background dark))
314 (:background "paleturquoise4"))
315 (((class color) (min-colors 16))
316 (:background "turquoise3"))
317 (((class color) (min-colors 8))
318 (:background "turquoise3"))
319 (t (:underline t)))
320 "Face for lazy highlighting of matches other than the current one."
92cc4a30
JL
321 :group 'lazy-highlight
322 :group 'basic-faces)
bca92193
JL
323(put 'isearch-lazy-highlight-face 'face-alias 'lazy-highlight)
324(defvar lazy-highlight-face 'lazy-highlight)
6480c508
JB
325(define-obsolete-variable-alias 'isearch-lazy-highlight-face
326 'lazy-highlight-face
327 "22.1")
c1bc6bb6 328\f
b92368b4
JL
329;; Define isearch help map.
330
331(defvar isearch-help-map
332 (let ((i 0)
333 (map (make-sparse-keymap)))
334 (define-key map [t] 'isearch-other-control-char)
335 (define-key map (char-to-string help-char) 'isearch-help-for-help)
336 (define-key map [help] 'isearch-help-for-help)
337 (define-key map [f1] 'isearch-help-for-help)
338 (define-key map "?" 'isearch-help-for-help)
339 (define-key map "b" 'isearch-describe-bindings)
340 (define-key map "k" 'isearch-describe-key)
341 (define-key map "m" 'isearch-describe-mode)
342 (define-key map "q" 'help-quit)
343 map)
344 "Keymap for characters following the Help key for isearch mode.")
345
346(eval-when-compile (require 'help-macro))
347
348(make-help-screen isearch-help-for-help-internal
349 "Type a help option: [bkm] or ?"
350 "You have typed %THIS-KEY%, the help character. Type a Help option:
351\(Type \\<help-map>\\[help-quit] to exit the Help command.)
352
353b Display all isearch key bindings.
354k KEYS Display full documentation of isearch key sequence.
355m Display documentation of isearch mode.
356
357You can't type here other help keys available in the global help map,
358but outise of this help window when you type them in isearch mode,
359they exit isearch mode before displaying global help."
360 isearch-help-map)
361
362(defun isearch-help-for-help ()
363 "Display isearch help menu."
364 (interactive)
365 (let (same-window-buffer-names same-window-regexps)
366 (isearch-help-for-help-internal))
367 (isearch-update))
368
369(defun isearch-describe-bindings ()
370 "Show a list of all keys defined in isearch mode, and their definitions.
371This is like `describe-bindings', but displays only isearch keys."
372 (interactive)
373 (let (same-window-buffer-names same-window-regexps)
374 (with-help-window "*Help*"
375 (with-current-buffer standard-output
376 (princ "Isearch Mode Bindings:\n")
377 (princ (substitute-command-keys "\\{isearch-mode-map}"))))))
378
379(defun isearch-describe-key ()
380 "Display documentation of the function invoked by isearch key."
381 (interactive)
382 (let (same-window-buffer-names same-window-regexps)
383 (call-interactively 'describe-key))
384 (isearch-update))
385
386(defun isearch-describe-mode ()
387 "Display documentation of isearch mode."
388 (interactive)
389 (let (same-window-buffer-names same-window-regexps)
390 (describe-function 'isearch-forward))
391 (isearch-update))
392
393(defalias 'isearch-mode-help 'isearch-describe-mode)
394
395\f
191f025a 396;; Define isearch-mode keymap.
8e1cae6d 397
02b2f510
SM
398(defvar isearch-mode-map
399 (let* ((i 0)
400 (map (make-keymap)))
8f924df7 401 (or (char-table-p (nth 1 map))
02b2f510
SM
402 (error "The initialization of isearch-mode-map must be updated"))
403 ;; Make all multibyte characters search for themselves.
6b61353c 404 (set-char-table-range (nth 1 map) (cons #x100 (max-char))
5760cdc1 405 'isearch-printing-char)
6b61353c
KH
406 ;; Make function keys, etc, which aren't bound to a scrolling-function
407 ;; exit the search.
02b2f510
SM
408 (define-key map [t] 'isearch-other-control-char)
409 ;; Control chars, by default, end isearch mode transparently.
b48ca14f 410 ;; We need these explicit definitions because, in a dense keymap,
02b2f510
SM
411 ;; the binding for t does not affect characters.
412 ;; We use a dense keymap to save space.
6480c508 413 (while (< i ?\s)
02b2f510
SM
414 (define-key map (make-string 1 i) 'isearch-other-control-char)
415 (setq i (1+ i)))
416
417 ;; Single-byte printing chars extend the search string by default.
6480c508 418 (setq i ?\s)
02b2f510
SM
419 (while (< i 256)
420 (define-key map (vector i) 'isearch-printing-char)
421 (setq i (1+ i)))
422
423 ;; To handle local bindings with meta char prefix keys, define
424 ;; another full keymap. This must be done for any other prefix
425 ;; keys as well, one full keymap per char of the prefix key. It
426 ;; would be simpler to disable the global keymap, and/or have a
427 ;; default local key binding for any key not otherwise bound.
428 (let ((meta-map (make-sparse-keymap)))
429 (define-key map (char-to-string meta-prefix-char) meta-map)
430 (define-key map [escape] meta-map))
431 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
432
433 ;; Several non-printing chars change the searching behavior.
434 (define-key map "\C-s" 'isearch-repeat-forward)
435 (define-key map "\C-r" 'isearch-repeat-backward)
436 ;; Define M-C-s and M-C-r like C-s and C-r so that the same key
437 ;; combinations can be used to repeat regexp isearches that can
438 ;; be used to start these searches.
439 (define-key map "\M-\C-s" 'isearch-repeat-forward)
440 (define-key map "\M-\C-r" 'isearch-repeat-backward)
441 (define-key map "\177" 'isearch-delete-char)
442 (define-key map "\C-g" 'isearch-abort)
1f4139d5 443
02b2f510
SM
444 ;; This assumes \e is the meta-prefix-char.
445 (or (= ?\e meta-prefix-char)
446 (error "Inconsistency in isearch.el"))
447 (define-key map "\e\e\e" 'isearch-cancel)
448 (define-key map [escape escape escape] 'isearch-cancel)
b48ca14f 449
02b2f510 450 (define-key map "\C-q" 'isearch-quote-char)
08200510 451
02b2f510
SM
452 (define-key map "\r" 'isearch-exit)
453 (define-key map "\C-j" 'isearch-printing-char)
454 (define-key map "\t" 'isearch-printing-char)
ec06d344 455 (define-key map [?\S-\ ] 'isearch-printing-char)
b48ca14f 456
74820eb5
JL
457 (define-key map "\C-w" 'isearch-yank-word-or-char)
458 (define-key map "\M-\C-w" 'isearch-del-char)
459 (define-key map "\M-\C-y" 'isearch-yank-char)
460 (define-key map "\C-y" 'isearch-yank-line)
08200510 461
b92368b4 462 (define-key map "\C-h" isearch-help-map)
08200510 463
02b2f510
SM
464 (define-key map "\M-n" 'isearch-ring-advance)
465 (define-key map "\M-p" 'isearch-ring-retreat)
466 (define-key map "\M-y" 'isearch-yank-kill)
467
468 (define-key map "\M-\t" 'isearch-complete)
469
470 ;; Pass frame events transparently so they won't exit the search.
471 ;; In particular, if we have more than one display open, then a
472 ;; switch-frame might be generated by someone typing at another keyboard.
473 (define-key map [switch-frame] nil)
474 (define-key map [delete-frame] nil)
475 (define-key map [iconify-frame] nil)
476 (define-key map [make-frame-visible] nil)
3f482bc0 477 (define-key map [mouse-movement] nil)
76197e28
JR
478 (define-key map [language-change] nil)
479
02b2f510
SM
480 ;; For searching multilingual text.
481 (define-key map "\C-\\" 'isearch-toggle-input-method)
482 (define-key map "\C-^" 'isearch-toggle-specified-input-method)
483
484 ;; People expect to be able to paste with the mouse.
e4af1426 485 (define-key map [mouse-2] #'isearch-mouse-2)
02b2f510
SM
486 (define-key map [down-mouse-2] nil)
487
488 ;; Some bindings you may want to put in your isearch-mode-hook.
489 ;; Suggest some alternates...
490 (define-key map "\M-c" 'isearch-toggle-case-fold)
491 (define-key map "\M-r" 'isearch-toggle-regexp)
492 (define-key map "\M-e" 'isearch-edit-string)
493
70bc5268
JL
494 (define-key map "\M-sr" 'isearch-toggle-regexp)
495 (define-key map "\M-sw" 'isearch-toggle-word)
496
81e898ea
SM
497 (define-key map [?\M-%] 'isearch-query-replace)
498 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
6e3057bb 499 (define-key map "\M-so" 'isearch-occur)
11c9f489 500 (define-key map "\M-shr" 'isearch-highlight-regexp)
74820eb5 501
02b2f510
SM
502 map)
503 "Keymap for `isearch-mode'.")
504
505(defvar minibuffer-local-isearch-map
506 (let ((map (make-sparse-keymap)))
507 (set-keymap-parent map minibuffer-local-map)
74820eb5 508 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
02b2f510 509 (define-key map "\M-\t" 'isearch-complete-edit)
74820eb5
JL
510 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
511 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
c16e87ee 512 (define-key map "\C-w" 'isearch-edit-string-set-word)
74820eb5
JL
513 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
514 (define-key map [right] 'isearch-yank-char-in-minibuffer)
02b2f510 515 map)
08200510 516 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 517
8e1cae6d 518;; Internal variables declared globally for byte-compiler.
08200510
RS
519;; These are all set with setq while isearching
520;; and bound locally while editing the search string.
521
522(defvar isearch-forward nil) ; Searching in the forward direction.
523(defvar isearch-regexp nil) ; Searching for a regexp.
524(defvar isearch-word nil) ; Searching for words.
cd59ea72 525(defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
08200510 526
191f025a
SM
527(defvar isearch-cmds nil
528 "Stack of search status sets.
08e3de69
EZ
529Each set is a vector of the form:
530 [STRING MESSAGE POINT SUCCESS FORWARD OTHER-END WORD
531 INVALID-REGEXP WRAPPED BARRIER WITHIN-BRACKETS CASE-FOLD-SEARCH]")
191f025a 532
08200510
RS
533(defvar isearch-string "") ; The current search string.
534(defvar isearch-message "") ; text-char-description version of isearch-string
535
ba653a53
JL
536(defvar isearch-success t) ; Searching is currently successful.
537(defvar isearch-error nil) ; Error message for failed search.
08200510
RS
538(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
539(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d 540(defvar isearch-barrier 0)
99ae9e9f 541(defvar isearch-just-started nil)
ddbe3d5f 542(defvar isearch-start-hscroll 0) ; hscroll when starting the search.
8e1cae6d 543
4453091d
RS
544; case-fold-search while searching.
545; either nil, t, or 'yes. 'yes means the same as t except that mixed
546; case in the search string is ignored.
547(defvar isearch-case-fold-search nil)
8e1cae6d 548
67085aba
GM
549(defvar isearch-last-case-fold-search nil)
550
ae1a21c6
MB
551;; Used to save default value while isearch is active
552(defvar isearch-original-minibuffer-message-timeout nil)
553
8e1cae6d
JB
554(defvar isearch-adjusted nil)
555(defvar isearch-slow-terminal-mode nil)
191f025a 556;; If t, using a small window.
08200510 557(defvar isearch-small-window nil)
8e1cae6d 558(defvar isearch-opoint 0)
191f025a 559;; The window configuration active at the beginning of the search.
08200510 560(defvar isearch-window-configuration nil)
8e1cae6d 561
08200510
RS
562;; Flag to indicate a yank occurred, so don't move the cursor.
563(defvar isearch-yank-flag nil)
8e1cae6d 564
191f025a
SM
565;; A function to be called after each input character is processed.
566;; (It is not called after characters that exit the search.)
567;; It is only set from an optional argument to `isearch-mode'.
08200510 568(defvar isearch-op-fun nil)
8e1cae6d 569
191f025a 570;; Is isearch-mode in a recursive edit for modal searching.
08200510 571(defvar isearch-recursive-edit nil)
8e1cae6d 572
191f025a 573;; Should isearch be terminated after doing one search?
08200510
RS
574(defvar isearch-nonincremental nil)
575
576;; New value of isearch-forward after isearch-edit-string.
577(defvar isearch-new-forward nil)
8e1cae6d 578
0352b205
RS
579;; Accumulate here the overlays opened during searching.
580(defvar isearch-opened-overlays nil)
8e1cae6d 581
99848db0
KH
582;; The value of input-method-function when isearch is invoked.
583(defvar isearch-input-method-function nil)
584
585;; A flag to tell if input-method-function is locally bound when
586;; isearch is invoked.
587(defvar isearch-input-method-local-p nil)
588
8e1cae6d
JB
589;; Minor-mode-alist changes - kind of redundant with the
590;; echo area, but if isearching in multiple windows, it can be useful.
591
592(or (assq 'isearch-mode minor-mode-alist)
593 (nconc minor-mode-alist
594 (list '(isearch-mode isearch-mode))))
595
08200510 596(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
597(make-variable-buffer-local 'isearch-mode)
598
fdf5afa4
RS
599(define-key global-map "\C-s" 'isearch-forward)
600(define-key esc-map "\C-s" 'isearch-forward-regexp)
601(define-key global-map "\C-r" 'isearch-backward)
602(define-key esc-map "\C-r" 'isearch-backward-regexp)
70bc5268 603(define-key search-map "w" 'isearch-forward-word)
fdf5afa4 604
191f025a 605;; Entry points to isearch-mode.
8e1cae6d 606
eceee2c0 607(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
608 "\
609Do incremental search forward.
08200510
RS
610With a prefix argument, do an incremental regular expression search instead.
611\\<isearch-mode-map>
8e1cae6d 612As you type characters, they add to the search string and are found.
b48ca14f 613The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 614
35904fd3 615Type \\[isearch-delete-char] to cancel last input item from end of search string.
8e1cae6d 616Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
617Type LFD (C-j) to match end of line.
618Type \\[isearch-repeat-forward] to search again forward,\
619 \\[isearch-repeat-backward] to search again backward.
0ae85960
RS
620Type \\[isearch-yank-word-or-char] to yank next word or character in buffer
621 onto the end of the search string, and search for it.
74820eb5
JL
622Type \\[isearch-del-char] to delete character from end of search string.
623Type \\[isearch-yank-char] to yank char from buffer onto end of search\
08200510
RS
624 string and search for it.
625Type \\[isearch-yank-line] to yank rest of line onto end of search string\
626 and search for it.
75a07f2c 627Type \\[isearch-yank-kill] to yank the last string of killed text.
8e1cae6d 628Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
629\\[isearch-abort] while searching or when search has failed cancels input\
630 back to what has
631 been found successfully.
632\\[isearch-abort] when search is successful aborts and moves point to\
633 starting point.
8e1cae6d 634
b3612973
RS
635If you try to exit with the search string still empty, it invokes
636 nonincremental search.
637
6c551d4e
EZ
638Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
639Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
70bc5268 640Type \\[isearch-toggle-word] to toggle word mode.
6c551d4e
EZ
641Type \\[isearch-edit-string] to edit the search string in the minibuffer.
642
8e1cae6d
JB
643Also supported is a search ring of the previous 16 search strings.
644Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
645Type \\[isearch-ring-retreat] to search for the previous item in the search\
646 ring.
647Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 648
70bc5268
JL
649Type \\[isearch-query-replace] to run `query-replace' with string to\
650 replace from last search string.
651Type \\[isearch-query-replace-regexp] to run `query-replace-regexp'\
652 with the last search string.
653Type \\[isearch-occur] to run `occur' that shows\
654 the last search string.
655Type \\[isearch-highlight-regexp] to run `highlight-regexp'\
656 that highlights the last search string.
657
b92368b4
JL
658Type \\[isearch-describe-bindings] to display all isearch key bindings.
659Type \\[isearch-describe-key] to display documentation of isearch key.
660Type \\[isearch-describe-mode] to display documentation of isearch mode.
661
1e14b095 662If an input method is turned on in the current buffer, that input
b48ca14f 663method is also active while you are typing characters to search. To
37b20c9b
KH
664toggle the input method, type \\[isearch-toggle-input-method]. It
665also toggles the input method in the current buffer.
666
667To use a different input method for searching, type
1e14b095 668\\[isearch-toggle-specified-input-method], and specify an input method
37b20c9b
KH
669you want to use.
670
b48ca14f 671The above keys, bound in `isearch-mode-map', are often controlled by
35904fd3 672 options; do \\[apropos] on search-.* to find them.
8e1cae6d 673Other control and meta characters terminate the search
08200510 674 and are then executed normally (depending on `search-exit-option').
fd49e05c 675Likewise for function keys and mouse button events.
8e1cae6d 676
08200510
RS
677If this function is called non-interactively, it does not return to
678the calling function until the search is done."
8e1cae6d 679
eceee2c0 680 (interactive "P\np")
4ae7d00a 681 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 682
eceee2c0 683(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
684 "\
685Do incremental search forward for regular expression.
08200510 686With a prefix argument, do a regular string search instead.
8938c0bc
JL
687Like ordinary incremental search except that your input is treated
688as a regexp. See the command `isearch-forward' for more information.
ab67e8b6
RS
689
690In regexp incremental searches, a space or spaces normally matches
691any whitespace (the variable `search-whitespace-regexp' controls
692precisely what that means). If you want to search for a literal space
1b1fb2ef 693and nothing else, enter C-q SPC."
eceee2c0 694 (interactive "P\np")
4ae7d00a 695 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 696
70bc5268
JL
697(defun isearch-forward-word (&optional not-word no-recursive-edit)
698 "\
699Do incremental search forward for a sequence of words.
700With a prefix argument, do a regular string search instead.
8938c0bc
JL
701Like ordinary incremental search except that your input is treated
702as a sequence of words without regard to how the words are separated.
703See the command `isearch-forward' for more information."
70bc5268
JL
704 (interactive "P\np")
705 (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
706
eceee2c0 707(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
708 "\
709Do incremental search backward.
08200510 710With a prefix argument, do a regular expression search instead.
8938c0bc 711See the command `isearch-forward' for more information."
eceee2c0 712 (interactive "P\np")
4ae7d00a 713 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 714
eceee2c0 715(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
716 "\
717Do incremental search backward for regular expression.
08200510 718With a prefix argument, do a regular string search instead.
8938c0bc
JL
719Like ordinary incremental search except that your input is treated
720as a regexp. See the command `isearch-forward' for more information."
eceee2c0 721 (interactive "P\np")
4ae7d00a 722 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510 723
8e1cae6d 724\f
8e1cae6d
JB
725;; isearch-mode only sets up incremental search for the minor mode.
726;; All the work is done by the isearch-mode commands.
727
08200510 728;; Not used yet:
d7fa5aa2 729;;(defvar isearch-commands '(isearch-forward isearch-backward
08200510
RS
730;; isearch-forward-regexp isearch-backward-regexp)
731;; "List of commands for which isearch-mode does not recursive-edit.")
b48ca14f 732
08200510
RS
733
734(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
b92368b4
JL
735 "Start isearch minor mode.
736It is called by the function `isearch-forward' and other related functions."
8e1cae6d
JB
737
738 ;; Initialize global vars.
739 (setq isearch-forward forward
740 isearch-regexp regexp
08200510 741 isearch-word word-p
8e1cae6d 742 isearch-op-fun op-fun
67085aba 743 isearch-last-case-fold-search isearch-case-fold-search
8e1cae6d
JB
744 isearch-case-fold-search case-fold-search
745 isearch-string ""
746 isearch-message ""
747 isearch-cmds nil
748 isearch-success t
749 isearch-wrapped nil
750 isearch-barrier (point)
751 isearch-adjusted nil
752 isearch-yank-flag nil
ba653a53 753 isearch-error nil
0cabad13 754 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
8e1cae6d 755 (> (window-height)
fad241d3
RS
756 (* 4
757 (abs search-slow-window-lines))))
8e1cae6d
JB
758 isearch-other-end nil
759 isearch-small-window nil
99ae9e9f 760 isearch-just-started t
ddbe3d5f 761 isearch-start-hscroll (window-hscroll)
8e1cae6d 762
8e1cae6d 763 isearch-opoint (point)
a5dcf63f 764 search-ring-yank-pointer nil
0352b205 765 isearch-opened-overlays nil
99848db0
KH
766 isearch-input-method-function input-method-function
767 isearch-input-method-local-p (local-variable-p 'input-method-function)
ae1a21c6
MB
768 regexp-search-ring-yank-pointer nil
769
770 ;; Save the original value of `minibuffer-message-timeout', and
771 ;; set it to nil so that isearch's messages don't get timed out.
772 isearch-original-minibuffer-message-timeout minibuffer-message-timeout
773 minibuffer-message-timeout nil)
99848db0
KH
774
775 ;; We must bypass input method while reading key. When a user type
776 ;; printable character, appropriate input method is turned on in
380866a2 777 ;; minibuffer to read multibyte characters.
99848db0
KH
778 (or isearch-input-method-local-p
779 (make-local-variable 'input-method-function))
780 (setq input-method-function nil)
781
99ae9e9f 782 (looking-at "")
292a8dff
RS
783 (setq isearch-window-configuration
784 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 785
18ce7555
RS
786 ;; Maybe make minibuffer frame visible and/or raise it.
787 (let ((frame (window-frame (minibuffer-window))))
360e0dd5
RS
788 (unless (memq (frame-live-p frame) '(nil t))
789 (unless (frame-visible-p frame)
790 (make-frame-visible frame))
791 (if minibuffer-auto-raise
792 (raise-frame frame))))
18ce7555 793
8e1cae6d 794 (setq isearch-mode " Isearch") ;; forward? regexp?
1cd3732c 795 (force-mode-line-update)
8e1cae6d
JB
796
797 (isearch-push-state)
798
c7955222 799 (setq overriding-terminal-local-map isearch-mode-map)
8e1cae6d
JB
800 (isearch-update)
801 (run-hooks 'isearch-mode-hook)
08200510 802
bfe2d334 803 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 804 (add-hook 'kbd-macro-termination-hook 'isearch-done)
6e5cd0ae 805
b48ca14f
JB
806 ;; isearch-mode can be made modal (in the sense of not returning to
807 ;; the calling function until searching is completed) by entering
08200510 808 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
809 (if recursive-edit
810 (let ((isearch-recursive-edit t))
811 (recursive-edit)))
0daa17f3 812 isearch-success)
8e1cae6d
JB
813
814
8e1cae6d
JB
815;; Some high level utilities. Others below.
816
817(defun isearch-update ()
191f025a 818 ;; Called after each command to update the display.
4c01d4bd
RS
819 (if (and (null unread-command-events)
820 (null executing-kbd-macro))
8e1cae6d 821 (progn
c982ab21 822 (if (not (input-pending-p))
ac475d3a
JL
823 (if isearch-message-function
824 (funcall isearch-message-function)
825 (isearch-message)))
c982ab21 826 (if (and isearch-slow-terminal-mode
b48ca14f 827 (not (or isearch-small-window
c982ab21
GM
828 (pos-visible-in-window-p))))
829 (let ((found-point (point)))
830 (setq isearch-small-window t)
831 (move-to-window-line 0)
832 (let ((window-min-height 1))
833 (split-window nil (if (< search-slow-window-lines 0)
834 (1+ (- search-slow-window-lines))
835 (- (window-height)
836 (1+ search-slow-window-lines)))))
837 (if (< search-slow-window-lines 0)
838 (progn (vertical-motion (- 1 search-slow-window-lines))
839 (set-window-start (next-window) (point))
840 (set-window-hscroll (next-window)
841 (window-hscroll))
842 (set-window-hscroll (selected-window) 0))
843 (other-window 1))
ddbe3d5f
RS
844 (goto-char found-point))
845 ;; Keep same hscrolling as at the start of the search when possible
846 (let ((current-scroll (window-hscroll)))
847 (set-window-hscroll (selected-window) isearch-start-hscroll)
848 (unless (pos-visible-in-window-p)
849 (set-window-hscroll (selected-window) current-scroll))))
850 (if isearch-other-end
c982ab21
GM
851 (if (< isearch-other-end (point)) ; isearch-forward?
852 (isearch-highlight isearch-other-end (point))
853 (isearch-highlight (point) isearch-other-end))
4dbbcb46 854 (isearch-dehighlight))
c982ab21 855 ))
8e1cae6d
JB
856 (setq ;; quit-flag nil not for isearch-mode
857 isearch-adjusted nil
858 isearch-yank-flag nil)
f9114cec 859 (when isearch-lazy-highlight
d9dd1f33 860 (isearch-lazy-highlight-new-loop))
a5cc922e
KH
861 ;; We must prevent the point moving to the end of composition when a
862 ;; part of the composition has just been searched.
863 (setq disable-point-adjustment t))
8e1cae6d 864
0daa17f3 865(defun isearch-done (&optional nopush edit)
87e97673
RS
866 "Exit Isearch mode.
867For successful search, pass no args.
868For a failing search, NOPUSH is t.
869For going to the minibuffer to edit the search string,
870NOPUSH is t and EDIT is t."
871
00098b01 872 (if isearch-resume-in-command-history
6848c9f1
KS
873 (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
874 ,isearch-word ,isearch-forward
875 ,isearch-message
876 ',isearch-case-fold-search)))
877 (unless (equal (car command-history) command)
878 (setq command-history (cons command command-history)))))
c40a4de1 879
bfe2d334 880 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
7e56ea04 881 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
d196f58d
GM
882 (setq isearch-lazy-highlight-start nil)
883
8e1cae6d 884 ;; Called by all commands that terminate isearch-mode.
35a4d143 885 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 886 (setq overriding-terminal-local-map nil)
08200510 887 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
ae1a21c6 888 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
4dbbcb46 889 (isearch-dehighlight)
46fe9018 890 (lazy-highlight-cleanup lazy-highlight-cleanup)
08200510
RS
891 (let ((found-start (window-start (selected-window)))
892 (found-point (point)))
d8a4fc44
RS
893 (when isearch-window-configuration
894 (set-window-configuration isearch-window-configuration)
895 (if isearch-small-window
896 (goto-char found-point)
897 ;; set-window-configuration clobbers window-start; restore it.
898 ;; This has an annoying side effect of clearing the last_modiff
899 ;; field of the window, which can cause unwanted scrolling,
900 ;; so don't do it unless truly necessary.
901 (set-window-start (selected-window) found-start t))))
08200510
RS
902
903 (setq isearch-mode nil)
99848db0
KH
904 (if isearch-input-method-local-p
905 (setq input-method-function isearch-input-method-function)
906 (kill-local-variable 'input-method-function))
907
1cd3732c 908 (force-mode-line-update)
8e1cae6d 909
df01192b
RS
910 ;; If we ended in the middle of some intangible text,
911 ;; move to the further end of that intangible text.
912 (let ((after (if (eobp) nil
913 (get-text-property (point) 'intangible)))
914 (before (if (bobp) nil
915 (get-text-property (1- (point)) 'intangible))))
916 (when (and before after (eq before after))
917 (if isearch-forward
918 (goto-char (next-single-property-change (point) 'intangible))
919 (goto-char (previous-single-property-change (point) 'intangible)))))
920
35a4d143 921 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 922 ;; Update the ring data.
73d2bf95 923 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 924
87e97673
RS
925 (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
926 (run-hooks 'isearch-mode-end-hook))
071fdd66
JL
927
928 ;; If there was movement, mark the starting position.
4837b516 929 ;; Maybe should test difference between and set mark only if > threshold.
071fdd66
JL
930 (if (/= (point) isearch-opoint)
931 (or (and transient-mark-mode mark-active)
932 (progn
933 (push-mark isearch-opoint t)
934 (or executing-kbd-macro (> (minibuffer-depth) 0)
935 (message "Mark saved where search started")))))
936
0daa17f3 937 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 938
73d2bf95
RS
939(defun isearch-update-ring (string &optional regexp)
940 "Add STRING to the beginning of the search ring.
a2b7dcc7 941REGEXP if non-nil says use the regexp search ring."
77e5aef9
KS
942 (add-to-history
943 (if regexp 'regexp-search-ring 'search-ring)
944 string
945 (if regexp regexp-search-ring-max search-ring-max)))
73d2bf95 946
191f025a
SM
947;; Switching buffers should first terminate isearch-mode.
948;; ;; For Emacs 19, the frame switch event is handled.
949;; (defun isearch-switch-frame-handler ()
950;; (interactive) ;; Is this necessary?
951;; ;; First terminate isearch-mode.
952;; (isearch-done)
953;; (isearch-clean-overlays)
954;; (handle-switch-frame (car (cdr last-command-char))))
08200510 955
8e1cae6d 956\f
08e3de69
EZ
957;; The search status structure and stack.
958
1a699acf 959(defsubst isearch-string-state (frame)
08e3de69 960 "Return the search string in FRAME."
3eec7894 961 (aref frame 0))
1a699acf 962(defsubst isearch-message-state (frame)
08e3de69 963 "Return the search string to display to the user in FRAME."
3eec7894 964 (aref frame 1))
1a699acf 965(defsubst isearch-point-state (frame)
08e3de69 966 "Return the point in FRAME."
3eec7894 967 (aref frame 2))
1a699acf 968(defsubst isearch-success-state (frame)
08e3de69 969 "Return the success flag in FRAME."
3eec7894 970 (aref frame 3))
1a699acf 971(defsubst isearch-forward-state (frame)
08e3de69 972 "Return the searching-forward flag in FRAME."
3eec7894 973 (aref frame 4))
1a699acf 974(defsubst isearch-other-end-state (frame)
08e3de69 975 "Return the other end of the match in FRAME."
3eec7894 976 (aref frame 5))
1a699acf 977(defsubst isearch-word-state (frame)
08e3de69 978 "Return the search-by-word flag in FRAME."
3eec7894 979 (aref frame 6))
ba653a53 980(defsubst isearch-error-state (frame)
08e3de69 981 "Return the regexp error message in FRAME, or nil if its regexp is valid."
3eec7894 982 (aref frame 7))
1a699acf 983(defsubst isearch-wrapped-state (frame)
08e3de69 984 "Return the search-wrapped flag in FRAME."
3eec7894 985 (aref frame 8))
1a699acf 986(defsubst isearch-barrier-state (frame)
08e3de69 987 "Return the barrier value in FRAME."
3eec7894 988 (aref frame 9))
1a699acf 989(defsubst isearch-case-fold-search-state (frame)
08e3de69 990 "Return the case-folding flag in FRAME."
ba653a53 991 (aref frame 10))
6a18e4e7
JL
992(defsubst isearch-pop-fun-state (frame)
993 "Return the function restoring the mode-specific isearch state in FRAME."
ba653a53 994 (aref frame 11))
08e3de69
EZ
995
996(defun isearch-top-state ()
997 (let ((cmd (car isearch-cmds)))
1a699acf
JL
998 (setq isearch-string (isearch-string-state cmd)
999 isearch-message (isearch-message-state cmd)
1000 isearch-success (isearch-success-state cmd)
1001 isearch-forward (isearch-forward-state cmd)
1002 isearch-other-end (isearch-other-end-state cmd)
1003 isearch-word (isearch-word-state cmd)
ba653a53 1004 isearch-error (isearch-error-state cmd)
1a699acf
JL
1005 isearch-wrapped (isearch-wrapped-state cmd)
1006 isearch-barrier (isearch-barrier-state cmd)
1a699acf 1007 isearch-case-fold-search (isearch-case-fold-search-state cmd))
6a18e4e7
JL
1008 (if (functionp (isearch-pop-fun-state cmd))
1009 (funcall (isearch-pop-fun-state cmd) cmd))
1a699acf 1010 (goto-char (isearch-point-state cmd))))
08e3de69
EZ
1011
1012(defun isearch-pop-state ()
1013 (setq isearch-cmds (cdr isearch-cmds))
1014 (isearch-top-state))
1015
1016(defun isearch-push-state ()
1017 (setq isearch-cmds
1018 (cons (vector isearch-string isearch-message (point)
1019 isearch-success isearch-forward isearch-other-end
1020 isearch-word
ba653a53
JL
1021 isearch-error isearch-wrapped isearch-barrier
1022 isearch-case-fold-search
6a18e4e7
JL
1023 (if isearch-push-state-function
1024 (funcall isearch-push-state-function)))
08e3de69
EZ
1025 isearch-cmds)))
1026
1027\f
8e1cae6d
JB
1028;; Commands active while inside of the isearch minor mode.
1029
1030(defun isearch-exit ()
1031 "Exit search normally.
1032However, if this is the first command after starting incremental
1033search and `search-nonincremental-instead' is non-nil, do a
08200510 1034nonincremental search instead via `isearch-edit-string'."
8e1cae6d 1035 (interactive)
b48ca14f 1036 (if (and search-nonincremental-instead
8e1cae6d 1037 (= 0 (length isearch-string)))
08200510
RS
1038 (let ((isearch-nonincremental t))
1039 (isearch-edit-string)))
0352b205
RS
1040 (isearch-done)
1041 (isearch-clean-overlays))
8e1cae6d
JB
1042
1043
1044(defun isearch-edit-string ()
08200510
RS
1045 "Edit the search string in the minibuffer.
1046The following additional command keys are active while editing.
1047\\<minibuffer-local-isearch-map>
1048\\[exit-minibuffer] to resume incremental searching with the edited string.
1049\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
1050\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 1051\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 1052\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 1053\\<isearch-mode-map>
74820eb5 1054If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
08200510
RS
1055
1056 ;; This code is very hairy for several reasons, explained in the code.
1057 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
1058 ;; If there were a way to catch any change of buffer from the minibuffer,
1059 ;; this could be simplified greatly.
eb8c3be9 1060 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 1061 (interactive)
08200510 1062 (condition-case err
e900ced4
RS
1063 (progn
1064 (let ((isearch-nonincremental isearch-nonincremental)
1065
1066 ;; Locally bind all isearch global variables to protect them
1067 ;; from recursive isearching.
1068 ;; isearch-string -message and -forward are not bound
1069 ;; so they may be changed. Instead, save the values.
1070 (isearch-new-string isearch-string)
1071 (isearch-new-message isearch-message)
1072 (isearch-new-forward isearch-forward)
1073 (isearch-new-word isearch-word)
1074
1075 (isearch-regexp isearch-regexp)
1076 (isearch-op-fun isearch-op-fun)
1077 (isearch-cmds isearch-cmds)
1078 (isearch-success isearch-success)
1079 (isearch-wrapped isearch-wrapped)
1080 (isearch-barrier isearch-barrier)
1081 (isearch-adjusted isearch-adjusted)
1082 (isearch-yank-flag isearch-yank-flag)
ba653a53 1083 (isearch-error isearch-error)
e900ced4
RS
1084 ;;; Don't bind this. We want isearch-search, below, to set it.
1085 ;;; And the old value won't matter after that.
1086 ;;; (isearch-other-end isearch-other-end)
1087 ;;; Perhaps some of these other variables should be bound for a
1088 ;;; shorter period, ending before the next isearch-search.
1089 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
1090 (isearch-opoint isearch-opoint)
1091 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
1092 (isearch-small-window isearch-small-window)
1093 (isearch-recursive-edit isearch-recursive-edit)
1094 ;; Save current configuration so we can restore it here.
1095 (isearch-window-configuration (current-window-configuration))
ae1a21c6
MB
1096
1097 ;; Temporarily restore `minibuffer-message-timeout'.
1098 (minibuffer-message-timeout
1099 isearch-original-minibuffer-message-timeout)
1100 (isearch-original-minibuffer-message-timeout
1101 isearch-original-minibuffer-message-timeout)
02b99a17 1102 old-point old-other-end)
e900ced4
RS
1103
1104 ;; Actually terminate isearching until editing is done.
b48ca14f 1105 ;; This is so that the user can do anything without failure,
e900ced4
RS
1106 ;; like switch buffers and start another isearch, and return.
1107 (condition-case err
1108 (isearch-done t t)
1109 (exit nil)) ; was recursive editing
1110
02b99a17
JL
1111 ;; Save old point and isearch-other-end before reading from minibuffer
1112 ;; that can change their values.
1113 (setq old-point (point) old-other-end isearch-other-end)
1114
e900ced4 1115 (unwind-protect
c16e87ee 1116 (let* ((message-log-max nil)
e900ced4
RS
1117 ;; Binding minibuffer-history-symbol to nil is a work-around
1118 ;; for some incompatibility with gmhist.
c16e87ee 1119 (minibuffer-history-symbol))
e900ced4 1120 (setq isearch-new-string
e5feeb31
SM
1121 (read-from-minibuffer
1122 (isearch-message-prefix nil nil isearch-nonincremental)
1123 isearch-string
1124 minibuffer-local-isearch-map nil
363de02e
JL
1125 (if isearch-regexp
1126 (cons 'regexp-search-ring
1127 (1+ (or regexp-search-ring-yank-pointer -1)))
1128 (cons 'search-ring
1129 (1+ (or search-ring-yank-pointer -1))))
e5feeb31 1130 nil t)
e900ced4
RS
1131 isearch-new-message
1132 (mapconcat 'isearch-text-char-description
1133 isearch-new-string "")))
02b99a17
JL
1134
1135 ;; Set point at the start (end) of old match if forward (backward),
1136 ;; so after exiting minibuffer isearch resumes at the start (end)
1137 ;; of this match and can find it again.
1138 (if (and old-other-end (eq old-point (point))
1139 (eq isearch-forward isearch-new-forward))
1140 (goto-char old-other-end))
1141
e900ced4 1142 ;; Always resume isearching by restarting it.
b48ca14f
JB
1143 (isearch-mode isearch-forward
1144 isearch-regexp
1145 isearch-op-fun
e900ced4
RS
1146 nil
1147 isearch-word)
1148
1149 ;; Copy new local values to isearch globals
1150 (setq isearch-string isearch-new-string
1151 isearch-message isearch-new-message
1152 isearch-forward isearch-new-forward
1153 isearch-word isearch-new-word))
1154
1155 ;; Empty isearch-string means use default.
1156 (if (= 0 (length isearch-string))
1157 (setq isearch-string (or (car (if isearch-regexp
1158 regexp-search-ring
1159 search-ring))
8cc3c821
RS
1160 "")
1161
1162 isearch-message
1163 (mapconcat 'isearch-text-char-description
1164 isearch-string ""))
e900ced4
RS
1165 ;; This used to set the last search string,
1166 ;; but I think it is not right to do that here.
1167 ;; Only the string actually used should be saved.
1168 ))
1169
a71a98cf
JL
1170 ;; This used to push the state as of before this C-s, but it adds
1171 ;; an inconsistent state where part of variables are from the
1172 ;; previous search (e.g. `isearch-success'), and part of variables
1173 ;; are just entered from the minibuffer (e.g. `isearch-string').
1174 ;; (isearch-push-state)
08200510
RS
1175
1176 ;; Reinvoke the pending search.
08200510 1177 (isearch-search)
a71a98cf 1178 (isearch-push-state) ; this pushes the correct state
08200510 1179 (isearch-update)
b48ca14f 1180 (if isearch-nonincremental
08200510
RS
1181 (progn
1182 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
40c7f256
RS
1183 (isearch-done)
1184 ;; The search done message is confusing when the string
1185 ;; is empty, so erase it.
1186 (if (equal isearch-string "")
1187 (message "")))))
8e1cae6d 1188
08200510
RS
1189 (quit ; handle abort-recursive-edit
1190 (isearch-abort) ;; outside of let to restore outside global values
1191 )))
1192
c16e87ee
JL
1193;; Obsolete usage of `C-s M-e C-w'. Remove after 23.1.
1194(defvar isearch-new-word)
1195(defun isearch-edit-string-set-word ()
1196 "Do word search after exiting `isearch-edit-string'."
1197 (interactive)
1198 (message "This feature is obsolete since 23.1; use `M-s w' instead.")
1199 (setq isearch-word t isearch-new-word t))
1200
1201
08200510
RS
1202(defun isearch-nonincremental-exit-minibuffer ()
1203 (interactive)
1204 (setq isearch-nonincremental t)
1205 (exit-minibuffer))
1206
1207(defun isearch-forward-exit-minibuffer ()
1208 (interactive)
1209 (setq isearch-new-forward t)
1210 (exit-minibuffer))
8e1cae6d 1211
08200510 1212(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 1213 (interactive)
08200510
RS
1214 (setq isearch-new-forward nil)
1215 (exit-minibuffer))
1216
19993c54
RS
1217(defun isearch-cancel ()
1218 "Terminate the search and go back to the starting point."
1219 (interactive)
6a18e4e7
JL
1220 (if (functionp (isearch-pop-fun-state (car (last isearch-cmds))))
1221 (funcall (isearch-pop-fun-state (car (last isearch-cmds)))
1222 (car (last isearch-cmds))))
19993c54 1223 (goto-char isearch-opoint)
6a18e4e7 1224 (isearch-done t) ; exit isearch
0352b205 1225 (isearch-clean-overlays)
6a18e4e7 1226 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
1227
1228(defun isearch-abort ()
b7852303 1229 "Abort incremental search mode if searching is successful, signaling quit.
08200510 1230Otherwise, revert to previous successful search and continue searching.
b7852303 1231Use `isearch-exit' to quit without signaling."
08200510 1232 (interactive)
eb8c3be9 1233;; (ding) signal instead below, if quitting
8e1cae6d
JB
1234 (discard-input)
1235 (if isearch-success
1236 ;; If search is successful, move back to starting point
1237 ;; and really do quit.
6a18e4e7
JL
1238 (progn
1239 (setq isearch-success nil)
1240 (isearch-cancel))
925a67ca
RS
1241 ;; If search is failing, or has an incomplete regexp,
1242 ;; rub out until it is once more successful.
ba653a53 1243 (while (or (not isearch-success) isearch-error)
925a67ca 1244 (isearch-pop-state))
8e1cae6d
JB
1245 (isearch-update)))
1246
8e1cae6d
JB
1247(defun isearch-repeat (direction)
1248 ;; Utility for isearch-repeat-forward and -backward.
1249 (if (eq isearch-forward (eq direction 'forward))
1250 ;; C-s in forward or C-r in reverse.
1251 (if (equal isearch-string "")
1252 ;; If search string is empty, use last one.
ece75c05
JL
1253 (if (null (if isearch-regexp regexp-search-ring search-ring))
1254 (setq isearch-error "No previous search string")
1255 (setq isearch-string
1256 (if isearch-regexp
1257 (car regexp-search-ring)
1258 (car search-ring))
1259 isearch-message
1260 (mapconcat 'isearch-text-char-description
1261 isearch-string "")
1262 isearch-case-fold-search isearch-last-case-fold-search))
8e1cae6d
JB
1263 ;; If already have what to search for, repeat it.
1264 (or isearch-success
02b2f510 1265 (progn
ba653a53
JL
1266 ;; Set isearch-wrapped before calling isearch-wrap-function
1267 (setq isearch-wrapped t)
6a18e4e7
JL
1268 (if isearch-wrap-function
1269 (funcall isearch-wrap-function)
ba653a53 1270 (goto-char (if isearch-forward (point-min) (point-max)))))))
8e1cae6d 1271 ;; C-s in reverse or C-r in forward, change direction.
f4c49513
RS
1272 (setq isearch-forward (not isearch-forward)
1273 isearch-success t))
8e1cae6d
JB
1274
1275 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
1276
1277 (if (equal isearch-string "")
1278 (setq isearch-success t)
512bfd85
RS
1279 (if (and isearch-success
1280 (equal (point) isearch-other-end)
99ae9e9f 1281 (not isearch-just-started))
8e1cae6d
JB
1282 ;; If repeating a search that found
1283 ;; an empty string, ensure we advance.
6fefdc4b
RS
1284 (if (if isearch-forward (eobp) (bobp))
1285 ;; If there's nowhere to advance to, fail (and wrap next time).
1286 (progn
1287 (setq isearch-success nil)
1288 (ding))
1289 (forward-char (if isearch-forward 1 -1))
1290 (isearch-search))
1291 (isearch-search)))
1292
8e1cae6d
JB
1293 (isearch-push-state)
1294 (isearch-update))
1295
1296(defun isearch-repeat-forward ()
1297 "Repeat incremental search forwards."
1298 (interactive)
1299 (isearch-repeat 'forward))
1300
1301(defun isearch-repeat-backward ()
1302 "Repeat incremental search backwards."
1303 (interactive)
1304 (isearch-repeat 'backward))
1305
1306(defun isearch-toggle-regexp ()
1307 "Toggle regexp searching on or off."
1308 ;; The status stack is left unchanged.
1309 (interactive)
1310 (setq isearch-regexp (not isearch-regexp))
08200510 1311 (if isearch-regexp (setq isearch-word nil))
d85519bb 1312 (setq isearch-success t isearch-adjusted t)
8e1cae6d
JB
1313 (isearch-update))
1314
70bc5268
JL
1315(defun isearch-toggle-word ()
1316 "Toggle word searching on or off."
1317 (interactive)
1318 (setq isearch-word (not isearch-word))
1319 (setq isearch-success t isearch-adjusted t)
1320 (isearch-update))
1321
4453091d
RS
1322(defun isearch-toggle-case-fold ()
1323 "Toggle case folding in searching on or off."
1324 (interactive)
1325 (setq isearch-case-fold-search
1326 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
1327 (let ((message-log-max nil))
1328 (message "%s%s [case %ssensitive]"
1329 (isearch-message-prefix nil nil isearch-nonincremental)
1330 isearch-message
1331 (if isearch-case-fold-search "in" "")))
d85519bb 1332 (setq isearch-success t isearch-adjusted t)
4453091d
RS
1333 (sit-for 1)
1334 (isearch-update))
1335
d85519bb 1336(defun isearch-query-replace (&optional regexp-flag)
6e3057bb 1337 "Start `query-replace' with string to replace from last search string."
74820eb5 1338 (interactive)
81e898ea 1339 (barf-if-buffer-read-only)
d85519bb 1340 (if regexp-flag (setq isearch-regexp t))
6e3057bb
JL
1341 (let ((case-fold-search isearch-case-fold-search)
1342 ;; set `search-upper-case' to nil to not call
1343 ;; `isearch-no-upper-case-p' in `perform-replace'
1344 (search-upper-case nil))
74820eb5
JL
1345 (isearch-done)
1346 (isearch-clean-overlays)
e7e4ea21
JL
1347 (if (and isearch-other-end
1348 (< isearch-other-end (point))
d85519bb 1349 (not (and transient-mark-mode mark-active
fa81f010 1350 (< (mark) (point)))))
d85519bb
JL
1351 (goto-char isearch-other-end))
1352 (set query-replace-from-history-variable
1353 (cons isearch-string
1354 (symbol-value query-replace-from-history-variable)))
81e898ea
SM
1355 (perform-replace
1356 isearch-string
d85519bb
JL
1357 (query-replace-read-to
1358 isearch-string
1359 (if isearch-regexp "Query replace regexp" "Query replace")
1360 isearch-regexp)
1361 t isearch-regexp isearch-word nil nil
1362 (if (and transient-mark-mode mark-active) (region-beginning))
1363 (if (and transient-mark-mode mark-active) (region-end)))))
74820eb5
JL
1364
1365(defun isearch-query-replace-regexp ()
6e3057bb 1366 "Start `query-replace-regexp' with string to replace from last search string."
74820eb5 1367 (interactive)
d85519bb 1368 (isearch-query-replace t))
74820eb5 1369
6e3057bb
JL
1370(defun isearch-occur (regexp &optional nlines)
1371 "Run `occur' with regexp to search from the current search string.
1372Interactively, REGEXP is the current search regexp or a quoted search
1373string. NLINES has the same meaning as in `occur'."
1374 (interactive
1375 (list
1376 (if isearch-regexp isearch-string (regexp-quote isearch-string))
1377 (if current-prefix-arg (prefix-numeric-value current-prefix-arg))))
1378 (let ((case-fold-search isearch-case-fold-search)
1379 ;; set `search-upper-case' to nil to not call
1380 ;; `isearch-no-upper-case-p' in `occur-1'
1381 (search-upper-case nil))
1382 (occur regexp nlines)))
1383
11c9f489
JL
1384(declare-function hi-lock-regexp-okay "hi-lock" (regexp))
1385(declare-function hi-lock-read-face-name "hi-lock" ())
1386
8938c0bc 1387(defun isearch-highlight-regexp ()
11c9f489 1388 "Run `highlight-regexp' with regexp from the current search string.
8938c0bc
JL
1389It exits Isearch mode and calls `hi-lock-face-buffer' with its regexp
1390argument from the last search regexp or a quoted search string,
1391and reads its face argument using `hi-lock-read-face-name'."
1392 (interactive)
11c9f489
JL
1393 (isearch-done)
1394 (isearch-clean-overlays)
8938c0bc 1395 (require 'hi-lock nil t)
11c9f489 1396 ;; (add-to-history 'hi-lock-regexp-history regexp)
8938c0bc
JL
1397 (let ((case-fold-search isearch-case-fold-search)
1398 ;; TODO: add `search-upper-case' as in `isearch-occur'
1399 )
1400 (hi-lock-face-buffer
1401 (hi-lock-regexp-okay
1402 (if isearch-regexp isearch-string (regexp-quote isearch-string)))
1403 (hi-lock-read-face-name))))
11c9f489 1404
74820eb5 1405\f
8e1cae6d 1406(defun isearch-delete-char ()
d6b8a1c0 1407 "Discard last input item and move point back.
8e1cae6d
JB
1408If no previous match was done, just beep."
1409 (interactive)
1410 (if (null (cdr isearch-cmds))
1411 (ding)
1412 (isearch-pop-state))
1413 (isearch-update))
1414
74820eb5
JL
1415(defun isearch-del-char (&optional arg)
1416 "Delete character from end of search string and search again.
1417If search string is empty, just beep."
1418 (interactive "p")
1419 (if (= 0 (length isearch-string))
35904fd3 1420 (ding)
74820eb5 1421 (setq isearch-string (substring isearch-string 0 (- (or arg 1)))
35904fd3 1422 isearch-message (mapconcat 'isearch-text-char-description
02b99a17
JL
1423 isearch-string "")))
1424 ;; Use the isearch-other-end as new starting point to be able
1425 ;; to find the remaining part of the search string again.
1426 (if isearch-other-end (goto-char isearch-other-end))
1427 (isearch-search)
1428 (isearch-push-state)
1429 (isearch-update))
8e1cae6d 1430
9cf081fa
KH
1431(defun isearch-yank-string (string)
1432 "Pull STRING into search string."
1433 ;; Downcase the string if not supposed to case-fold yanked strings.
1434 (if (and isearch-case-fold-search
1435 (eq 'not-yanks search-upper-case))
1436 (setq string (downcase string)))
1437 (if isearch-regexp (setq string (regexp-quote string)))
1438 (setq isearch-string (concat isearch-string string)
1439 isearch-message
1440 (concat isearch-message
1441 (mapconcat 'isearch-text-char-description
1442 string ""))
1443 ;; Don't move cursor in reverse search.
1444 isearch-yank-flag t)
8e1cae6d
JB
1445 (isearch-search-and-update))
1446
30d47262
RS
1447(defun isearch-yank-kill ()
1448 "Pull string from kill ring into search string."
1449 (interactive)
9cf081fa
KH
1450 (isearch-yank-string (current-kill 0)))
1451
1452(defun isearch-yank-x-selection ()
117132a6 1453 "Pull current X selection into search string."
9cf081fa
KH
1454 (interactive)
1455 (isearch-yank-string (x-get-selection)))
8e1cae6d 1456
e4af1426 1457
191f025a 1458(defun isearch-mouse-2 (click)
e4af1426 1459 "Handle mouse-2 in Isearch mode.
117132a6 1460For a click in the echo area, invoke `isearch-yank-x-selection'.
f3b5dd74
DK
1461Otherwise invoke whatever the calling mouse-2 command sequence
1462is bound to outside of Isearch."
191f025a 1463 (interactive "e")
e4af1426
GM
1464 (let* ((w (posn-window (event-start click)))
1465 (overriding-terminal-local-map nil)
f3b5dd74 1466 (binding (key-binding (this-command-keys-vector) t)))
117132a6
DL
1467 (if (and (window-minibuffer-p w)
1468 (not (minibuffer-window-active-p w))) ; in echo area
1469 (isearch-yank-x-selection)
cd59ea72 1470 (when (functionp binding)
191f025a 1471 (call-interactively binding)))))
e4af1426 1472
29e53a0a
KF
1473(defun isearch-yank-internal (jumpform)
1474 "Pull the text from point to the point reached by JUMPFORM.
1475JUMPFORM is a lambda expression that takes no arguments and returns a
1476buffer position, possibly having moved point to that position. For
1477example, it might move point forward by a word and return point, or it
1478might return the position of the end of the line."
9cf081fa
KH
1479 (isearch-yank-string
1480 (save-excursion
1481 (and (not isearch-forward) isearch-other-end
1482 (goto-char isearch-other-end))
29e53a0a
KF
1483 (buffer-substring-no-properties (point) (funcall jumpform)))))
1484
74820eb5
JL
1485(defun isearch-yank-char-in-minibuffer (&optional arg)
1486 "Pull next character from buffer into end of search string in minibuffer."
1487 (interactive "p")
1488 (if (eobp)
1489 (insert
1490 (save-excursion
1491 (set-buffer (cadr (buffer-list)))
1492 (buffer-substring-no-properties
1493 (point) (progn (forward-char arg) (point)))))
1494 (forward-char arg)))
1495
1496(defun isearch-yank-char (&optional arg)
35904fd3 1497 "Pull next character from buffer into search string."
74820eb5
JL
1498 (interactive "p")
1499 (isearch-yank-internal (lambda () (forward-char arg) (point))))
29e53a0a 1500
868bf43a
RS
1501(defun isearch-yank-word-or-char ()
1502 "Pull next character or word from buffer into search string."
1503 (interactive)
21d90805 1504 (isearch-yank-internal
b48ca14f 1505 (lambda ()
21d90805
KF
1506 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1507 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1508 (forward-word 1)
1509 (forward-char 1)) (point))))
868bf43a 1510
29e53a0a
KF
1511(defun isearch-yank-word ()
1512 "Pull next word from buffer into search string."
1513 (interactive)
1514 (isearch-yank-internal (lambda () (forward-word 1) (point))))
8e1cae6d
JB
1515
1516(defun isearch-yank-line ()
1517 "Pull rest of line from buffer into search string."
1518 (interactive)
a1883913 1519 (isearch-yank-internal
933f8467
RF
1520 (lambda () (let ((inhibit-field-text-motion t))
1521 (line-end-position (if (eolp) 2 1))))))
8e1cae6d
JB
1522
1523(defun isearch-search-and-update ()
1524 ;; Do the search and update the display.
191f025a 1525 (when (or isearch-success
35904fd3
JL
1526 ;; Unsuccessful regexp search may become successful by
1527 ;; addition of characters which make isearch-string valid
cd59ea72
SM
1528 isearch-regexp
1529 ;; If the string was found but was completely invisible,
1530 ;; it might now be partly visible, so try again.
1531 (prog1 isearch-hidden (setq isearch-hidden nil)))
8e1cae6d
JB
1532 ;; In reverse search, adding stuff at
1533 ;; the end may cause zero or many more chars to be
1534 ;; matched, in the string following point.
1535 ;; Allow all those possibilities without moving point as
1536 ;; long as the match does not extend past search origin.
1537 (if (and (not isearch-forward) (not isearch-adjusted)
1538 (condition-case ()
99ae9e9f 1539 (let ((case-fold-search isearch-case-fold-search))
ec0a2f45
KH
1540 (if (and (eq case-fold-search t) search-upper-case)
1541 (setq case-fold-search
1542 (isearch-no-upper-case-p isearch-string isearch-regexp)))
99ae9e9f
KH
1543 (looking-at (if isearch-regexp isearch-string
1544 (regexp-quote isearch-string))))
8e1cae6d 1545 (error nil))
bd6a8414 1546 (or isearch-yank-flag
191f025a 1547 (<= (match-end 0)
bd6a8414 1548 (min isearch-opoint isearch-barrier))))
d64b1d96 1549 (progn
191f025a 1550 (setq isearch-success t
ba653a53 1551 isearch-error nil
d64b1d96
RS
1552 isearch-other-end (match-end 0))
1553 (if (and (eq isearch-case-fold-search t) search-upper-case)
1554 (setq isearch-case-fold-search
1555 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1556 ;; Not regexp, not reverse, or no match at point.
1557 (if (and isearch-other-end (not isearch-adjusted))
1558 (goto-char (if isearch-forward isearch-other-end
191f025a
SM
1559 (min isearch-opoint
1560 isearch-barrier
8e1cae6d
JB
1561 (1+ isearch-other-end)))))
1562 (isearch-search)
1563 ))
1564 (isearch-push-state)
1565 (if isearch-op-fun (funcall isearch-op-fun))
1566 (isearch-update))
1567
1568
08e3de69 1569;; *, ?, }, and | chars can make a regexp more liberal.
08200510 1570;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1571;; So go back to place last successful search started
1572;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1573;; + needs no special handling because the string must match at least once.
8e1cae6d 1574
08e3de69
EZ
1575(defun isearch-backslash (str)
1576 "Return t if STR ends in an odd number of backslashes."
1577 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
1578
1579(defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
1580 "Return point to previous successful match to allow regexp liberalization.
1581\\<isearch-mode-map>
1582Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by
1583stopping at `isearch-barrier' as needed.
1584
6f2df0f4
JL
1585Do nothing if a backslash is escaping the liberalizing character.
1586If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
08e3de69 1587
6f2df0f4
JL
1588Do nothing if regexp has recently been invalid unless optional
1589ALLOW-INVALID non-nil.
08e3de69 1590
6f2df0f4
JL
1591If optional TO-BARRIER non-nil, ignore previous matches and go exactly
1592to the barrier."
08e3de69
EZ
1593 ;; (eq (not a) (not b)) makes all non-nil values equivalent
1594 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
1595 (not want-backslash))
1596 ;; We have to check 2 stack frames because the last might be
1597 ;; invalid just because of a backslash.
ba653a53
JL
1598 (or (not isearch-error)
1599 (not (isearch-error-state (cadr isearch-cmds)))
08e3de69
EZ
1600 allow-invalid))
1601 (if to-barrier
1602 (progn (goto-char isearch-barrier)
1603 (setq isearch-adjusted t))
1604 (let* ((stack isearch-cmds)
1605 (previous (cdr stack)) ; lookbelow in the stack
1606 (frame (car stack)))
1607 ;; Walk down the stack looking for a valid regexp (as of course only
1608 ;; they can be the previous successful match); this conveniently
1609 ;; removes all bracket-sets and groups that might be in the way, as
1610 ;; well as partial \{\} constructs that the code below leaves behind.
1611 ;; Also skip over postfix operators -- though horrid,
9b9a4122 1612 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
08e3de69 1613 (while (and previous
ba653a53 1614 (or (isearch-error-state frame)
1a699acf 1615 (let* ((string (isearch-string-state frame))
08e3de69
EZ
1616 (lchar (aref string (1- (length string)))))
1617 ;; The operators aren't always operators; check
1618 ;; backslashes. This doesn't handle the case of
1619 ;; operators at the beginning of the regexp not
1620 ;; being special, but then we should fall back to
1621 ;; the barrier anyway because it's all optional.
1622 (if (isearch-backslash
1a699acf 1623 (isearch-string-state (car previous)))
08e3de69
EZ
1624 (eq lchar ?\})
1625 (memq lchar '(?* ?? ?+))))))
1626 (setq stack previous previous (cdr previous) frame (car stack)))
1627 (when stack
1628 ;; `stack' now refers the most recent valid regexp that is not at
1629 ;; all optional in its last term. Now dig one level deeper and find
1630 ;; what matched before that.
5a1f9fcf
JL
1631 (let ((last-other-end
1632 (or (and (car previous)
1633 (isearch-other-end-state (car previous)))
1634 isearch-barrier)))
08e3de69
EZ
1635 (goto-char (if isearch-forward
1636 (max last-other-end isearch-barrier)
1637 (min last-other-end isearch-barrier)))
6f2df0f4 1638 (setq isearch-adjusted t)))))))
8e1cae6d 1639
6b61353c
KH
1640(defun isearch-unread-key-sequence (keylist)
1641 "Unread the given key-sequence KEYLIST.
1642Scroll-bar or mode-line events are processed appropriately."
1643 (cancel-kbd-macro-events)
1644 (apply 'isearch-unread keylist)
1645 ;; If the event was a scroll-bar or mode-line click, the event will have
1646 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
1647 ;; it here, because this symbol will be attached to the event again next
1648 ;; time it gets read by read-key-sequence.
1649 ;;
1650 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
1651 ;; modify the event anymore in 21 because read_key_sequence no longer
1652 ;; modifies events to produce fake prefix keys.")
1653 (if (and (> (length keylist) 1)
1654 (symbolp (car keylist))
1655 (listp (cadr keylist))
1656 (not (numberp (posn-point
1657 (event-start (cadr keylist) )))))
1658 (pop unread-command-events)))
1659
1660;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1661;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
1662;;
1663;; The idea here is that certain vertical scrolling commands (like C-l
1664;; `recenter') should be usable WITHIN Isearch mode. For a command to be
1665;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
1666;; tamper with isearch's state, or move point. It is unacceptable for the
1667;; search string to be scrolled out of the current window. If a command
1668;; attempts this, we scroll the text back again.
1669;;
1670;; We implement this feature with a property called `isearch-scroll'.
1671;; If a command's symbol has the value t for this property it is a
1672;; scrolling command. The feature needs to be enabled by setting the
1673;; customizable variable `isearch-allow-scroll' to a non-nil value.
1674;;
1675;; The universal argument commands (e.g. C-u) in simple.el are marked
1676;; as scrolling commands, and isearch.el has been amended to allow
1677;; prefix arguments to be passed through to scrolling commands. Thus
1678;; M-0 C-l will scroll point to the top of the window.
1679;;
1680;; Horizontal scrolling commands are currently not catered for.
1681;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1682
1683;; Set the isearch-scroll property on some standard functions:
1684;; Scroll-bar functions:
1685(if (fboundp 'scroll-bar-toolkit-scroll)
1686 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
6b61353c
KH
1687(if (fboundp 'w32-handle-scroll-bar-event)
1688 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
1689
1690;; Commands which scroll the window:
1691(put 'recenter 'isearch-scroll t)
21d9ed8b 1692(put 'recenter-top-bottom 'isearch-scroll t)
6b61353c
KH
1693(put 'reposition-window 'isearch-scroll t)
1694(put 'scroll-up 'isearch-scroll t)
1695(put 'scroll-down 'isearch-scroll t)
1696
1697;; Commands which act on the other window
1698(put 'list-buffers 'isearch-scroll t)
1699(put 'scroll-other-window 'isearch-scroll t)
1700(put 'scroll-other-window-down 'isearch-scroll t)
1701(put 'beginning-of-buffer-other-window 'isearch-scroll t)
1702(put 'end-of-buffer-other-window 'isearch-scroll t)
1703
1704;; Commands which change the window layout
1705(put 'delete-other-windows 'isearch-scroll t)
1706(put 'balance-windows 'isearch-scroll t)
1707(put 'split-window-vertically 'isearch-scroll t)
7f55d3b7 1708(put 'split-window-horizontally 'isearch-scroll t)
6b61353c
KH
1709(put 'enlarge-window 'isearch-scroll t)
1710
1711;; Universal argument commands
1712(put 'universal-argument 'isearch-scroll t)
1713(put 'negative-argument 'isearch-scroll t)
1714(put 'digit-argument 'isearch-scroll t)
1715
1716(defcustom isearch-allow-scroll nil
1717 "If non-nil, scrolling commands are allowed during incremental search."
1718 :type 'boolean
1719 :group 'isearch)
1720
1721(defun isearch-string-out-of-window (isearch-point)
1722 "Test whether the search string is currently outside of the window.
1723Return nil if it's completely visible, or if point is visible,
1724together with as much of the search string as will fit; the symbol
1725`above' if we need to scroll the text downwards; the symbol `below',
1726if upwards."
1727 (let ((w-start (window-start))
1728 (w-end (window-end nil t))
1729 (w-L1 (save-excursion (move-to-window-line 1) (point)))
1730 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
1731 start end) ; start and end of search string in buffer
1732 (if isearch-forward
1733 (setq end isearch-point start (or isearch-other-end isearch-point))
1734 (setq start isearch-point end (or isearch-other-end isearch-point)))
1735 (cond ((or (and (>= start w-start) (<= end w-end))
1736 (if isearch-forward
1737 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
1738 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
1739 nil)
1740 ((and (< start w-start)
1741 (< isearch-point w-L-1))
1742 'above)
1743 (t 'below))))
1744
1745(defun isearch-back-into-window (above isearch-point)
1746 "Scroll the window to bring the search string back into view.
1747Restore point to ISEARCH-POINT in the process. ABOVE is t when the
1748search string is above the top of the window, nil when it is beneath
1749the bottom."
1750 (let (start end)
1751 (if isearch-forward
1752 (setq end isearch-point start (or isearch-other-end isearch-point))
1753 (setq start isearch-point end (or isearch-other-end isearch-point)))
1754 (if above
1755 (progn
1756 (goto-char start)
1757 (recenter 0)
1758 (when (>= isearch-point (window-end nil t))
1759 (goto-char isearch-point)
1760 (recenter -1)))
1761 (goto-char end)
1762 (recenter -1)
1763 (when (< isearch-point (window-start))
1764 (goto-char isearch-point)
1765 (recenter 0))))
1766 (goto-char isearch-point))
1767
1768(defun isearch-reread-key-sequence-naturally (keylist)
1769 "Reread key sequence KEYLIST with Isearch mode's keymap deactivated.
1770Return the key sequence as a string/vector."
1771 (isearch-unread-key-sequence keylist)
1772 (let (overriding-terminal-local-map)
1773 (read-key-sequence nil))) ; This will go through function-key-map, if nec.
1774
1775(defun isearch-lookup-scroll-key (key-seq)
1776 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
1777Otherwise return nil."
1778 (let* ((overriding-terminal-local-map nil)
1779 (binding (key-binding key-seq)))
1780 (and binding (symbolp binding) (commandp binding)
1781 (eq (get binding 'isearch-scroll) t)
1782 binding)))
8e1cae6d 1783
b457cc3b 1784(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d 1785
6b61353c
KH
1786(defun isearch-other-meta-char (&optional arg)
1787 "Process a miscellaneous key sequence in Isearch mode.
1788
1789Try to convert the current key-sequence to something usable in Isearch
1790mode, either by converting it with `function-key-map', downcasing a
1791key with C-<upper case>, or finding a \"scrolling command\" bound to
1792it. \(In the last case, we may have to read more events.) If so,
1793either unread the converted sequence or execute the command.
1794
1795Otherwise, if `search-exit-option' is non-nil (the default) unread the
1796key-sequence and exit the search normally. If it is the symbol
1797`edit', the search string is edited in the minibuffer and the meta
1798character is unread so that it applies to editing the string.
1799
1800ARG is the prefix argument. It will be transmitted through to the
1801scrolling command or to the command whose key-sequence exits
1802Isearch mode."
1803 (interactive "P")
1804 (let* ((key (if current-prefix-arg ; not nec the same as ARG
1805 (substring (this-command-keys) universal-argument-num-events)
1806 (this-command-keys)))
c6431f12 1807 (main-event (aref key 0))
6b61353c
KH
1808 (keylist (listify-key-sequence key))
1809 scroll-command isearch-point)
24b5caec 1810 (cond ((and (= (length key) 1)
c40bb1ba 1811 (let ((lookup (lookup-key local-function-key-map key)))
3f866b53
KH
1812 (not (or (null lookup) (integerp lookup)
1813 (keymapp lookup)))))
24b5caec
RS
1814 ;; Handle a function key that translates into something else.
1815 ;; If the key has a global definition too,
1816 ;; exit and unread the key itself, so its global definition runs.
1817 ;; Otherwise, unread the translation,
1818 ;; so that the translated key takes effect within isearch.
d94eb6eb 1819 (cancel-kbd-macro-events)
24b5caec 1820 (if (lookup-key global-map key)
02b2f510 1821 (progn
24b5caec
RS
1822 (isearch-done)
1823 (apply 'isearch-unread keylist))
a5cc922e 1824 (setq keylist
c40bb1ba 1825 (listify-key-sequence (lookup-key local-function-key-map key)))
a5cc922e
KH
1826 (while keylist
1827 (setq key (car keylist))
1828 ;; If KEY is a printing char, we handle it here
1829 ;; directly to avoid the input method and keyboard
1830 ;; coding system translating it.
1831 (if (and (integerp key)
6480c508 1832 (>= key ?\s) (/= key 127) (< key 256))
a5cc922e
KH
1833 (progn
1834 (isearch-process-search-char key)
1835 (setq keylist (cdr keylist)))
1836 ;; As the remaining keys in KEYLIST can't be handled
1837 ;; here, we must reread them.
1838 (apply 'isearch-unread keylist)
1839 (setq keylist nil)))))
24b5caec 1840 (
c6431f12
KH
1841 ;; Handle an undefined shifted control character
1842 ;; by downshifting it if that makes it defined.
1843 ;; (As read-key-sequence would normally do,
1844 ;; if we didn't have a default definition.)
1845 (let ((mods (event-modifiers main-event)))
1846 (and (integerp main-event)
1847 (memq 'shift mods)
1848 (memq 'control mods)
f438bf5c
EZ
1849 (not (memq (lookup-key isearch-mode-map
1850 (let ((copy (copy-sequence key)))
1851 (aset copy 0
1852 (- main-event
1853 (- ?\C-\S-a ?\C-a)))
1854 copy)
1855 nil)
1856 '(nil
1857 isearch-other-control-char)))))
c6431f12 1858 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1859 (cancel-kbd-macro-events)
c6431f12
KH
1860 (apply 'isearch-unread keylist))
1861 ((eq search-exit-option 'edit)
1862 (apply 'isearch-unread keylist)
1863 (isearch-edit-string))
6b61353c
KH
1864 ;; Handle a scrolling function.
1865 ((and isearch-allow-scroll
1866 (progn (setq key (isearch-reread-key-sequence-naturally keylist))
1867 (setq keylist (listify-key-sequence key))
1868 (setq main-event (aref key 0))
1869 (setq scroll-command (isearch-lookup-scroll-key key))))
1870 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
1871 ;; complete key sequence, possibly as modified by function-key-map,
1872 ;; not merely the one or two event fragment which invoked
1873 ;; isearch-other-meta-char in the first place.
1874 (setq isearch-point (point))
1875 (setq prefix-arg arg)
1876 (command-execute scroll-command)
1877 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
1878 (if ab-bel
35904fd3 1879 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
d85519bb 1880 (goto-char isearch-point)))
6b61353c 1881 (isearch-update))
72a69d7f
JL
1882 ;; A mouse click on the isearch message starts editing the search string
1883 ((and (eq (car-safe main-event) 'down-mouse-1)
1884 (window-minibuffer-p (posn-window (event-start main-event))))
1885 ;; Swallow the up-event.
1886 (read-event)
1887 (isearch-edit-string))
c6431f12
KH
1888 (search-exit-option
1889 (let (window)
6b61353c
KH
1890 (isearch-unread-key-sequence keylist)
1891 (setq main-event (car unread-command-events))
3fb01f36 1892
346f18dc
GM
1893 ;; If we got a mouse click event, that event contains the
1894 ;; window clicked on. maybe it was read with the buffer
c6431f12
KH
1895 ;; it was clicked on. If so, that buffer, not the current one,
1896 ;; is in isearch mode. So end the search in that buffer.
346f18dc
GM
1897
1898 ;; ??? I have no idea what this if checks for, but it's
1899 ;; obviously wrong for the case that a down-mouse event
1900 ;; on another window invokes this function. The event
1901 ;; will contain the window clicked on and that window's
b48ca14f 1902 ;; buffer is certainly not always in Isearch mode.
346f18dc
GM
1903 ;;
1904 ;; Leave the code in, but check for current buffer not
1905 ;; being in Isearch mode for now, until someone tells
1906 ;; what it's really supposed to do.
1907 ;;
1908 ;; --gerd 2001-08-10.
1909
1910 (if (and (not isearch-mode)
1911 (listp main-event)
c6431f12 1912 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1913 (windowp window)
1914 (or (> (minibuffer-depth) 0)
1915 (not (window-minibuffer-p window))))
c6431f12
KH
1916 (save-excursion
1917 (set-buffer (window-buffer window))
0352b205
RS
1918 (isearch-done)
1919 (isearch-clean-overlays))
1920 (isearch-done)
6b61353c
KH
1921 (isearch-clean-overlays)
1922 (setq prefix-arg arg))))
1923 (t;; otherwise nil
c6431f12 1924 (isearch-process-search-string key key)))))
8e1cae6d 1925
8e1cae6d
JB
1926(defun isearch-quote-char ()
1927 "Quote special characters for incremental search."
1928 (interactive)
8bc15fa8 1929 (let ((char (read-quoted-char (isearch-message t))))
3c75023f 1930 ;; Assume character codes 0200 - 0377 stand for characters in some
c20d35d5
KH
1931 ;; single-byte character set, and convert them to Emacs
1932 ;; characters.
6480c508 1933 (if (and isearch-regexp (= char ?\s))
be02a7ed 1934 (if (subregexp-context-p isearch-string (length isearch-string))
30bb1443
SM
1935 (isearch-process-search-string "[ ]" " ")
1936 (isearch-process-search-char char))
ab67e8b6
RS
1937 (and enable-multibyte-characters
1938 (>= char ?\200)
1939 (<= char ?\377)
1940 (setq char (unibyte-char-to-multibyte char)))
1941 (isearch-process-search-char char))))
8e1cae6d 1942
8e1cae6d 1943(defun isearch-return-char ()
6f2df0f4 1944 "Convert return into newline for incremental search."
8e1cae6d
JB
1945 (interactive)
1946 (isearch-process-search-char ?\n))
13701709 1947(make-obsolete 'isearch-return-char 'isearch-printing-char "19.7")
8e1cae6d 1948
8e1cae6d 1949(defun isearch-printing-char ()
b68c164d 1950 "Add this ordinary printing character to the search string and search."
8e1cae6d 1951 (interactive)
117132a6 1952 (let ((char last-command-char))
45b94eb2 1953 (if (= char ?\S-\ )
6480c508 1954 (setq char ?\s))
758710cb
KH
1955 (if current-input-method
1956 (isearch-process-search-multibyte-characters char)
1957 (isearch-process-search-char char))))
8e1cae6d 1958
8e1cae6d 1959(defun isearch-process-search-char (char)
6f2df0f4
JL
1960 ;; * and ? are special in regexps when not preceded by \.
1961 ;; } and | are special in regexps when preceded by \.
1962 ;; Nothing special for + because it matches at least once.
1963 (cond
1964 ((memq char '(?* ??)) (isearch-fallback nil))
1965 ((eq char ?\}) (isearch-fallback t t))
1966 ((eq char ?|) (isearch-fallback t nil t)))
1967
8e1cae6d 1968 ;; Append the char to the search string, update the message and re-search.
191f025a
SM
1969 (isearch-process-search-string
1970 (char-to-string char)
8f3c1d76 1971 (if (>= char ?\200)
01a6ef79
RS
1972 (char-to-string char)
1973 (isearch-text-char-description char))))
8e1cae6d
JB
1974
1975(defun isearch-process-search-string (string message)
1976 (setq isearch-string (concat isearch-string string)
1977 isearch-message (concat isearch-message message))
1978 (isearch-search-and-update))
1979
1980\f
8e1cae6d
JB
1981;; Search Ring
1982
08200510
RS
1983(defun isearch-ring-adjust1 (advance)
1984 ;; Helper for isearch-ring-adjust
1985 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1986 (length (length ring))
1987 (yank-pointer-name (if isearch-regexp
08200510 1988 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1989 'search-ring-yank-pointer))
1990 (yank-pointer (eval yank-pointer-name)))
1991 (if (zerop length)
1992 ()
1993 (set yank-pointer-name
1994 (setq yank-pointer
ffbc30b2
PE
1995 (mod (+ (or yank-pointer 0)
1996 (if advance -1 1))
1997 length)))
a5dcf63f 1998 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1999 isearch-message (mapconcat 'isearch-text-char-description
2000 isearch-string "")))))
2001
2002(defun isearch-ring-adjust (advance)
2003 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 2004 (isearch-ring-adjust1 advance)
08200510
RS
2005 (if search-ring-update
2006 (progn
2007 (isearch-search)
a71a98cf 2008 (isearch-push-state)
08200510 2009 (isearch-update))
a71a98cf
JL
2010 ;; Otherwise, edit the search string instead. Note that there is
2011 ;; no need to push the search state after isearch-edit-string here
2012 ;; since isearch-edit-string already pushes its state
2013 (isearch-edit-string)))
8e1cae6d
JB
2014
2015(defun isearch-ring-advance ()
2016 "Advance to the next search string in the ring."
08200510 2017 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
2018 (interactive)
2019 (isearch-ring-adjust 'advance))
2020
2021(defun isearch-ring-retreat ()
2022 "Retreat to the previous search string in the ring."
2023 (interactive)
2024 (isearch-ring-adjust nil))
2025
08200510
RS
2026(defun isearch-complete1 ()
2027 ;; Helper for isearch-complete and isearch-complete-edit
2028 ;; Return t if completion OK, nil if no completion exists.
2029 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
08200510 2030 (completion-ignore-case case-fold-search)
c789e608 2031 (completion (try-completion isearch-string ring)))
08200510
RS
2032 (cond
2033 ((eq completion t)
2034 ;; isearch-string stays the same
2035 t)
2036 ((or completion ; not nil, must be a string
b7852303 2037 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 2038 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
2039 (progn
2040 (if completion-auto-help
2041 (with-output-to-temp-buffer "*Isearch completions*"
b48ca14f 2042 (display-completion-list
c789e608 2043 (all-completions isearch-string ring))))
36bcac3f
RS
2044 t)
2045 (and completion
2046 (setq isearch-string completion))))
08200510
RS
2047 (t
2048 (message "No completion") ; waits a second if in minibuffer
2049 nil))))
2050
2051(defun isearch-complete ()
2052 "Complete the search string from the strings on the search ring.
2053The completed string is then editable in the minibuffer.
2054If there is no completion possible, say so and continue searching."
2055 (interactive)
2056 (if (isearch-complete1)
3ae4c509
RS
2057 (progn (setq isearch-message
2058 (mapconcat 'isearch-text-char-description
2059 isearch-string ""))
2060 (isearch-edit-string))
08200510
RS
2061 ;; else
2062 (sit-for 1)
2063 (isearch-update)))
8e1cae6d 2064
08200510
RS
2065(defun isearch-complete-edit ()
2066 "Same as `isearch-complete' except in the minibuffer."
2067 (interactive)
c789e608 2068 (setq isearch-string (field-string))
08200510 2069 (if (isearch-complete1)
8e1cae6d 2070 (progn
b7b66466 2071 (delete-field)
08200510 2072 (insert isearch-string))))
8e1cae6d
JB
2073
2074\f
8e1cae6d
JB
2075;; Message string
2076
2077(defun isearch-message (&optional c-q-hack ellipsis)
2078 ;; Generate and print the message string.
2079 (let ((cursor-in-echo-area ellipsis)
d8891294
JL
2080 (m isearch-message)
2081 (cmds isearch-cmds)
2082 succ-msg)
2083 (when (or (not isearch-success) isearch-error)
2084 ;; Highlight failed part
2085 (while (or (not (isearch-success-state (car cmds)))
2086 (isearch-error-state (car cmds)))
2087 (pop cmds))
2088 (setq succ-msg (and cmds (isearch-message-state (car cmds)))
2089 m (copy-sequence m))
a71a98cf
JL
2090 (add-text-properties
2091 (if (and (stringp succ-msg)
2092 (< (length succ-msg) (length m))
2093 (equal succ-msg (substring m 0 (length succ-msg))))
2094 (length succ-msg)
2095 0)
2096 (length m) '(face isearch-fail) m)
d8891294
JL
2097 ;; Highlight failed trailing whitespace
2098 (when (string-match " +$" m)
2099 (add-text-properties (match-beginning 0) (match-end 0)
2100 '(face trailing-whitespace) m)))
2101 (setq m (concat
2102 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
2103 m
2104 (isearch-message-suffix c-q-hack ellipsis)))
2105 (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
8e1cae6d 2106
08200510 2107(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
2108 ;; If about to search, and previous search regexp was invalid,
2109 ;; check that it still is. If it is valid now,
2110 ;; let the message we display while searching say that it is valid.
ba653a53 2111 (and isearch-error ellipsis
8e1cae6d
JB
2112 (condition-case ()
2113 (progn (re-search-forward isearch-string (point) t)
ba653a53 2114 (setq isearch-error nil))
8e1cae6d
JB
2115 (error nil)))
2116 ;; If currently failing, display no ellipsis.
2117 (or isearch-success (setq ellipsis nil))
2118 (let ((m (concat (if isearch-success "" "failing ")
d85519bb 2119 (if isearch-adjusted "pending " "")
7badea30 2120 (if (and isearch-wrapped
6a18e4e7 2121 (not isearch-wrap-function)
7badea30
RS
2122 (if isearch-forward
2123 (> (point) isearch-opoint)
2124 (< (point) isearch-opoint)))
2125 "over")
8e1cae6d 2126 (if isearch-wrapped "wrapped ")
08200510 2127 (if isearch-word "word " "")
8e1cae6d 2128 (if isearch-regexp "regexp " "")
3c0aa5e6 2129 (if multi-isearch-next-buffer-current-function "multi " "")
08200510 2130 (if nonincremental "search" "I-search")
f46d5091 2131 (if isearch-forward "" " backward")
5b56d4e4
KH
2132 (if current-input-method
2133 (concat " [" current-input-method-title "]: ")
f46d5091 2134 ": ")
8e1cae6d 2135 )))
aec11aff
KS
2136 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
2137 'face 'minibuffer-prompt)))
8e1cae6d
JB
2138
2139(defun isearch-message-suffix (&optional c-q-hack ellipsis)
2140 (concat (if c-q-hack "^Q" "")
ba653a53
JL
2141 (if isearch-error
2142 (concat " [" isearch-error "]")
8e1cae6d
JB
2143 "")))
2144
2145\f
191f025a
SM
2146;; Searching
2147
512bfd85
RS
2148(defvar isearch-search-fun-function nil
2149 "Override `isearch-search-fun'.
2150This function should return the search function for isearch to use.
2151It will call this function with three arguments
2152as if it were `search-forward'.")
191f025a
SM
2153
2154(defun isearch-search-fun ()
2155 "Return the function to use for the search.
2156Can be changed via `isearch-search-fun-function' for special needs."
2157 (if isearch-search-fun-function
2158 (funcall isearch-search-fun-function)
2159 (cond
2160 (isearch-word
2161 (if isearch-forward 'word-search-forward 'word-search-backward))
2162 (isearch-regexp
2163 (if isearch-forward 're-search-forward 're-search-backward))
2164 (t
2165 (if isearch-forward 'search-forward 'search-backward)))))
8e1cae6d 2166
6cf157df
KH
2167(defun isearch-search-string (string bound noerror)
2168 ;; Search for the first occurance of STRING or its translation. If
2169 ;; found, move point to the end of the occurance, update
2170 ;; isearch-match-beg and isearch-match-end, and return point.
2171 (let ((func (isearch-search-fun))
2172 (len (length string))
2173 pos1 pos2)
2174 (setq pos1 (save-excursion (funcall func string bound noerror)))
2175 (if (and (char-table-p translation-table-for-input)
4f3b3482
SM
2176 (multibyte-string-p string)
2177 ;; Minor optimization.
2178 (string-match-p "[^[:ascii:]]" string))
2179 (let ((translated
2180 (apply 'string
2181 (mapcar (lambda (c)
2182 (or (aref translation-table-for-input c) c))
2183 string)))
2184 match-data)
6cf157df
KH
2185 (when translated
2186 (save-match-data
2187 (save-excursion
2188 (if (setq pos2 (funcall func translated bound noerror))
2189 (setq match-data (match-data t)))))
2190 (when (and pos2
2191 (or (not pos1)
2192 (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
2193 (setq pos1 pos2)
2194 (set-match-data match-data)))))
d240f431
JL
2195 (when pos1
2196 ;; When using multiple buffers isearch, switch to the new buffer here,
2197 ;; because `save-excursion' above doesn't allow doing it inside funcall.
3c0aa5e6
JL
2198 (if (and multi-isearch-next-buffer-current-function
2199 (buffer-live-p multi-isearch-current-buffer))
2200 (switch-to-buffer multi-isearch-current-buffer))
d240f431 2201 (goto-char pos1))
6cf157df
KH
2202 pos1))
2203
8e1cae6d
JB
2204(defun isearch-search ()
2205 ;; Do the search with the current search string.
ac475d3a
JL
2206 (if isearch-message-function
2207 (funcall isearch-message-function nil t)
2208 (isearch-message nil t))
4453091d 2209 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
2210 (setq isearch-case-fold-search
2211 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 2212 (condition-case lossage
5e189f39
JL
2213 (let ((inhibit-point-motion-hooks
2214 (and (eq isearch-success-function 'isearch-success-function-default)
2215 search-invisible))
79c7a4fa 2216 (inhibit-quit nil)
86bfaffe 2217 (case-fold-search isearch-case-fold-search)
ab67e8b6 2218 (search-spaces-regexp search-whitespace-regexp)
86bfaffe 2219 (retry t))
ba653a53 2220 (setq isearch-error nil)
86bfaffe
RS
2221 (while retry
2222 (setq isearch-success
6cf157df 2223 (isearch-search-string isearch-string nil t))
86bfaffe
RS
2224 ;; Clear RETRY unless we matched some invisible text
2225 ;; and we aren't supposed to do that.
5e189f39 2226 (if (or (not isearch-success)
86bfaffe
RS
2227 (bobp) (eobp)
2228 (= (match-beginning 0) (match-end 0))
5e189f39
JL
2229 (funcall isearch-success-function
2230 (match-beginning 0) (match-end 0)))
86bfaffe 2231 (setq retry nil)))
99ae9e9f 2232 (setq isearch-just-started nil)
8e1cae6d
JB
2233 (if isearch-success
2234 (setq isearch-other-end
2235 (if isearch-forward (match-beginning 0) (match-end 0)))))
2236
d32696d4 2237 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
2238 (setq isearch-success nil))
2239
191f025a 2240 (invalid-regexp
ba653a53 2241 (setq isearch-error (car (cdr lossage)))
8e1cae6d
JB
2242 (if (string-match
2243 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
ba653a53
JL
2244 isearch-error)
2245 (setq isearch-error "incomplete input")))
2246
2247 (search-failed
2248 (setq isearch-success nil)
2249 (setq isearch-error (nth 2 lossage)))
2250
f1c03125 2251 (error
c982ab21 2252 ;; stack overflow in regexp search.
ba653a53 2253 (setq isearch-error (format "%s" lossage))))
8e1cae6d
JB
2254
2255 (if isearch-success
2256 nil
2257 ;; Ding if failed this time after succeeding last time.
1a699acf 2258 (and (isearch-success-state (car isearch-cmds))
8e1cae6d 2259 (ding))
6a18e4e7
JL
2260 (if (functionp (isearch-pop-fun-state (car isearch-cmds)))
2261 (funcall (isearch-pop-fun-state (car isearch-cmds)) (car isearch-cmds)))
1a699acf 2262 (goto-char (isearch-point-state (car isearch-cmds)))))
8e1cae6d 2263
0352b205 2264
191f025a 2265;; Called when opening an overlay, and we are still in isearch.
0352b205 2266(defun isearch-open-overlay-temporary (ov)
b48ca14f 2267 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
0352b205
RS
2268 ;; Some modes would want to open the overlays temporary during
2269 ;; isearch in their own way, they should set the
2270 ;; `isearch-open-invisible-temporary' to a function doing this.
2271 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2272 ;; Store the values for the `invisible' and `intangible'
2273 ;; properties, and then set them to nil. This way the text hidden
2274 ;; by this overlay becomes visible.
2275
380866a2 2276 ;; Do we really need to set the `intangible' property to t? Can we
0352b205
RS
2277 ;; have the point inside an overlay with an `intangible' property?
2278 ;; In 19.34 this does not exist so I cannot test it.
2279 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2280 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
2281 (overlay-put ov 'invisible nil)
2282 (overlay-put ov 'intangible nil)))
2283
2284
191f025a
SM
2285;; This is called at the end of isearch. It will open the overlays
2286;; that contain the latest match. Obviously in case of a C-g the
2287;; point returns to the original location which surely is not contain
2288;; in any of these overlays, se we are safe in this case too.
0352b205 2289(defun isearch-open-necessary-overlays (ov)
191f025a 2290 (let ((inside-overlay (and (> (point) (overlay-start ov))
0352b205
RS
2291 (< (point) (overlay-end ov))))
2292 ;; If this exists it means that the overlay was opened using
2293 ;; this function, not by us tweaking the overlay properties.
2294 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2295 (when (or inside-overlay (not fct-temp))
2296 ;; restore the values for the `invisible' and `intangible'
2297 ;; properties
2298 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2299 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2300 (overlay-put ov 'isearch-invisible nil)
2301 (overlay-put ov 'isearch-intangible nil))
2302 (if inside-overlay
2303 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2304 (if fct-temp
2305 (funcall fct-temp ov t)))))
2306
191f025a
SM
2307;; This is called when exiting isearch. It closes the temporary
2308;; opened overlays, except the ones that contain the latest match.
0352b205
RS
2309(defun isearch-clean-overlays ()
2310 (when isearch-opened-overlays
02b2f510 2311 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
0352b205
RS
2312 (setq isearch-opened-overlays nil)))
2313
fc0eccfc
GM
2314
2315(defun isearch-intersects-p (start0 end0 start1 end1)
2316 "Return t if regions START0..END0 and START1..END1 intersect."
eaa493df
GM
2317 (or (and (>= start0 start1) (< start0 end1))
2318 (and (> end0 start1) (<= end0 end1))
2319 (and (>= start1 start0) (< start1 end0))
2320 (and (> end1 start0) (<= end1 end0))))
fc0eccfc
GM
2321
2322
191f025a
SM
2323;; Verify if the current match is outside of each element of
2324;; `isearch-opened-overlays', if so close that overlay.
fc0eccfc
GM
2325
2326(defun isearch-close-unnecessary-overlays (begin end)
2327 (let ((overlays isearch-opened-overlays))
0352b205 2328 (setq isearch-opened-overlays nil)
fc0eccfc
GM
2329 (dolist (ov overlays)
2330 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
2331 (push ov isearch-opened-overlays)
2332 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2333 (if fct-temp
2334 ;; If this exists it means that the overlay was opened
2335 ;; using this function, not by us tweaking the overlay
2336 ;; properties.
2337 (funcall fct-temp ov t)
2338 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2339 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2340 (overlay-put ov 'isearch-invisible nil)
2341 (overlay-put ov 'isearch-intangible nil)))))))
2342
0352b205 2343
86bfaffe 2344(defun isearch-range-invisible (beg end)
79c7a4fa 2345 "Return t if all the text from BEG to END is invisible."
cd59ea72
SM
2346 (when (/= beg end)
2347 ;; Check that invisibility runs up to END.
2348 (save-excursion
2349 (goto-char beg)
2350 (let (;; can-be-opened keeps track if we can open some overlays.
2351 (can-be-opened (eq search-invisible 'open))
2352 ;; the list of overlays that could be opened
2353 (crt-overlays nil))
2354 (when (and can-be-opened isearch-hide-immediately)
2355 (isearch-close-unnecessary-overlays beg end))
2356 ;; If the following character is currently invisible,
2357 ;; skip all characters with that same `invisible' property value.
2358 ;; Do that over and over.
2359 (while (and (< (point) end)
2360 (let ((prop
2361 (get-char-property (point) 'invisible)))
2362 (if (eq buffer-invisibility-spec t)
2363 prop
2364 (or (memq prop buffer-invisibility-spec)
2365 (assq prop buffer-invisibility-spec)))))
2366 (if (get-text-property (point) 'invisible)
2367 (progn
2368 (goto-char (next-single-property-change (point) 'invisible
2369 nil end))
2370 ;; if text is hidden by an `invisible' text property
2371 ;; we cannot open it at all.
2372 (setq can-be-opened nil))
2373 (when can-be-opened
2374 (let ((overlays (overlays-at (point)))
2375 ov-list
2376 o
2377 invis-prop)
2378 (while overlays
2379 (setq o (car overlays)
2380 invis-prop (overlay-get o 'invisible))
2381 (if (if (eq buffer-invisibility-spec t)
2382 invis-prop
2383 (or (memq invis-prop buffer-invisibility-spec)
2384 (assq invis-prop buffer-invisibility-spec)))
2385 (if (overlay-get o 'isearch-open-invisible)
2386 (setq ov-list (cons o ov-list))
2387 ;; We found one overlay that cannot be
2388 ;; opened, that means the whole chunk
2389 ;; cannot be opened.
2390 (setq can-be-opened nil)))
2391 (setq overlays (cdr overlays)))
2392 (if can-be-opened
2393 ;; It makes sense to append to the open
2394 ;; overlays list only if we know that this is
2395 ;; t.
2396 (setq crt-overlays (append ov-list crt-overlays)))))
2397 (goto-char (next-overlay-change (point)))))
2398 ;; See if invisibility reaches up thru END.
2399 (if (>= (point) end)
2400 (if (and can-be-opened (consp crt-overlays))
2401 (progn
2402 (setq isearch-opened-overlays
2403 (append isearch-opened-overlays crt-overlays))
2404 (mapc 'isearch-open-overlay-temporary crt-overlays)
2405 nil)
2406 (setq isearch-hidden t)))))))
08200510 2407
5e189f39
JL
2408(defun isearch-success-function-default (beg end)
2409 "Default function to report if the new search match is successful.
2410Returns t if search can match hidden text, or otherwise checks if some
2411text from BEG to END is visible."
2412 (or (eq search-invisible t)
2413 (not (isearch-range-invisible beg end))))
2414
08200510 2415\f
191f025a 2416;; General utilities
08200510 2417
b78559b0
RS
2418(defun isearch-no-upper-case-p (string regexp-flag)
2419 "Return t if there are no upper case chars in STRING.
b7852303 2420If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 2421since they have special meaning in a regexp."
b48ca14f 2422 (let (quote-flag (i 0) (len (length string)) found)
59057198
RS
2423 (while (and (not found) (< i len))
2424 (let ((char (aref string i)))
2425 (if (and regexp-flag (eq char ?\\))
2426 (setq quote-flag (not quote-flag))
2427 (if (and (not quote-flag) (not (eq char (downcase char))))
f4be3f89
RS
2428 (setq found t))
2429 (setq quote-flag nil)))
59057198 2430 (setq i (1+ i)))
c5f847b6
SM
2431 (not (or found
2432 ;; Even if there's no uppercase char, we want to detect the use
2433 ;; of [:upper:] or [:lower:] char-class, which indicates
2434 ;; clearly that the user cares about case distinction.
2435 (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
2436 (condition-case err
2437 (progn
2438 (string-match (substring string 0 (match-beginning 0))
2439 "")
2440 nil)
2441 (invalid-regexp
2442 (equal "Unmatched [ or [^" (cadr err)))))))))
08200510 2443
b78559b0 2444;; Portability functions to support various Emacs versions.
8e1cae6d 2445
08200510 2446(defun isearch-text-char-description (c)
14373677 2447 (cond
6480c508 2448 ((< c ?\s) (format "^%c" (+ c 64)))
14373677
SM
2449 ((= c ?\^?) "^?")
2450 (t (char-to-string c))))
08200510 2451
bd1bd125 2452;; General function to unread characters or events.
99ae9e9f 2453;; Also insert them in a keyboard macro being defined.
8f90f594 2454(defun isearch-unread (&rest char-or-events)
02b2f510 2455 (mapc 'store-kbd-macro-event char-or-events)
bd1bd125
RS
2456 (setq unread-command-events
2457 (append char-or-events unread-command-events)))
08200510 2458
44336afb 2459\f
bca92193
JL
2460;; Highlighting
2461
2462(defvar isearch-overlay nil)
2463
2464(defun isearch-highlight (beg end)
704d3ae7
JL
2465 (if search-highlight
2466 (if isearch-overlay
2467 ;; Overlay already exists, just move it.
2468 (move-overlay isearch-overlay beg end (current-buffer))
2469 ;; Overlay doesn't exist, create it.
2470 (setq isearch-overlay (make-overlay beg end))
2471 ;; 1001 is higher than lazy's 1000 and ediff's 100+
2472 (overlay-put isearch-overlay 'priority 1001)
2473 (overlay-put isearch-overlay 'face isearch))))
bca92193
JL
2474
2475(defun isearch-dehighlight ()
2476 (when isearch-overlay
2477 (delete-overlay isearch-overlay)))
2478\f
191f025a
SM
2479;; isearch-lazy-highlight feature
2480;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
2481
2482;; When active, *every* match for the current search string is
2483;; highlighted: the current one using the normal isearch match color
e3cde0c7 2484;; and all the others using `isearch-lazy-highlight'. The extra
191f025a
SM
2485;; highlighting makes it easier to anticipate where the cursor will
2486;; land each time you press C-s or C-r to repeat a pending search.
2487;; Highlighting of these additional matches happens in a deferred
2488;; fashion using "idle timers," so the cycles needed do not rob
2489;; isearch of its usual snappy response.
2490
2491;; IMPLEMENTATION NOTE: This depends on some isearch internals.
2492;; Specifically:
2493;; - `isearch-update' is expected to be called (at least) every time
2494;; the search string or window-start changes;
2495;; - `isearch-string' is expected to contain the current search
2496;; string as entered by the user;
2497;; - the type of the current search is expected to be given by
2498;; `isearch-word' and `isearch-regexp';
2499;; - the direction of the current search is expected to be given by
2500;; `isearch-forward';
ba653a53 2501;; - the variable `isearch-error' is expected to be true
4837b516 2502;; only if `isearch-string' is an invalid regexp.
44336afb 2503
44336afb 2504(defvar isearch-lazy-highlight-overlays nil)
c982ab21 2505(defvar isearch-lazy-highlight-wrapped nil)
f9114cec
RS
2506(defvar isearch-lazy-highlight-start-limit nil)
2507(defvar isearch-lazy-highlight-end-limit nil)
44336afb
GM
2508(defvar isearch-lazy-highlight-start nil)
2509(defvar isearch-lazy-highlight-end nil)
2510(defvar isearch-lazy-highlight-timer nil)
2511(defvar isearch-lazy-highlight-last-string nil)
c982ab21
GM
2512(defvar isearch-lazy-highlight-window nil)
2513(defvar isearch-lazy-highlight-window-start nil)
6b61353c 2514(defvar isearch-lazy-highlight-window-end nil)
46daf6c7
GM
2515(defvar isearch-lazy-highlight-case-fold-search nil)
2516(defvar isearch-lazy-highlight-regexp nil)
9a193944 2517(defvar isearch-lazy-highlight-space-regexp nil)
44336afb 2518
46fe9018 2519(defun lazy-highlight-cleanup (&optional force)
c982ab21 2520 "Stop lazy highlighting and remove extra highlighting from current buffer.
c1bc6bb6 2521FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
c982ab21 2522is nil. This function is called when exiting an incremental search if
c1bc6bb6 2523`lazy-highlight-cleanup' is non-nil."
44336afb 2524 (interactive '(t))
c1bc6bb6 2525 (if (or force lazy-highlight-cleanup)
c982ab21
GM
2526 (while isearch-lazy-highlight-overlays
2527 (delete-overlay (car isearch-lazy-highlight-overlays))
2528 (setq isearch-lazy-highlight-overlays
2529 (cdr isearch-lazy-highlight-overlays))))
2530 (when isearch-lazy-highlight-timer
2531 (cancel-timer isearch-lazy-highlight-timer)
2532 (setq isearch-lazy-highlight-timer nil)))
44336afb 2533
6480c508
JB
2534(define-obsolete-function-alias 'isearch-lazy-highlight-cleanup
2535 'lazy-highlight-cleanup
2536 "22.1")
46fe9018 2537
d9dd1f33 2538(defun isearch-lazy-highlight-new-loop (&optional beg end)
c1bc6bb6 2539 "Cleanup any previous `lazy-highlight' loop and begin a new one.
f9114cec
RS
2540BEG and END specify the bounds within which highlighting should occur.
2541This is called when `isearch-update' is invoked (which can cause the
2542search string to change or the window to scroll). It is also used
2543by other Emacs features."
da79720c 2544 (when (and (null executing-kbd-macro)
c982ab21
GM
2545 (sit-for 0) ;make sure (window-start) is credible
2546 (or (not (equal isearch-string
2547 isearch-lazy-highlight-last-string))
2548 (not (eq (selected-window)
2549 isearch-lazy-highlight-window))
46daf6c7
GM
2550 (not (eq isearch-lazy-highlight-case-fold-search
2551 isearch-case-fold-search))
2552 (not (eq isearch-lazy-highlight-regexp
2553 isearch-regexp))
c982ab21 2554 (not (= (window-start)
6b61353c
KH
2555 isearch-lazy-highlight-window-start))
2556 (not (= (window-end) ; Window may have been split/joined.
2557 isearch-lazy-highlight-window-end))))
c982ab21 2558 ;; something important did indeed change
46fe9018 2559 (lazy-highlight-cleanup t) ;kill old loop & remove overlays
ba653a53 2560 (when (not isearch-error)
f9114cec
RS
2561 (setq isearch-lazy-highlight-start-limit beg
2562 isearch-lazy-highlight-end-limit end)
c982ab21
GM
2563 (setq isearch-lazy-highlight-window (selected-window)
2564 isearch-lazy-highlight-window-start (window-start)
6b61353c 2565 isearch-lazy-highlight-window-end (window-end)
c982ab21
GM
2566 isearch-lazy-highlight-start (point)
2567 isearch-lazy-highlight-end (point)
2568 isearch-lazy-highlight-last-string isearch-string
46daf6c7
GM
2569 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2570 isearch-lazy-highlight-regexp isearch-regexp
9a193944
CY
2571 isearch-lazy-highlight-wrapped nil
2572 isearch-lazy-highlight-space-regexp search-whitespace-regexp)
5f3a57c9
RS
2573 (unless (equal isearch-string "")
2574 (setq isearch-lazy-highlight-timer
c1bc6bb6 2575 (run-with-idle-timer lazy-highlight-initial-delay nil
5f3a57c9 2576 'isearch-lazy-highlight-update))))))
c982ab21
GM
2577
2578(defun isearch-lazy-highlight-search ()
2579 "Search ahead for the next or previous match, for lazy highlighting.
2580Attempt to do the search exactly the way the pending isearch would."
d9dd1f33
JL
2581 (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
2582 (isearch-regexp isearch-lazy-highlight-regexp)
9a193944 2583 (search-spaces-regexp isearch-lazy-highlight-space-regexp))
ba653a53 2584 (condition-case nil
6cf157df 2585 (isearch-search-string
d9dd1f33 2586 isearch-lazy-highlight-last-string
ba653a53
JL
2587 (if isearch-forward
2588 (min (or isearch-lazy-highlight-end-limit (point-max))
2589 (if isearch-lazy-highlight-wrapped
2590 isearch-lazy-highlight-start
2591 (window-end)))
2592 (max (or isearch-lazy-highlight-start-limit (point-min))
2593 (if isearch-lazy-highlight-wrapped
2594 isearch-lazy-highlight-end
2595 (window-start))))
2596 t)
2597 (error nil))))
44336afb
GM
2598
2599(defun isearch-lazy-highlight-update ()
c982ab21 2600 "Update highlighting of other matches for current search."
c1bc6bb6 2601 (let ((max lazy-highlight-max-at-a-time)
c982ab21
GM
2602 (looping t)
2603 nomore)
6ee8e621
JL
2604 (with-local-quit
2605 (save-selected-window
2606 (if (and (window-live-p isearch-lazy-highlight-window)
2607 (not (eq (selected-window) isearch-lazy-highlight-window)))
2608 (select-window isearch-lazy-highlight-window))
2609 (save-excursion
2610 (save-match-data
2611 (goto-char (if isearch-forward
2612 isearch-lazy-highlight-end
2613 isearch-lazy-highlight-start))
2614 (while looping
2615 (let ((found (isearch-lazy-highlight-search)))
2616 (when max
2617 (setq max (1- max))
2618 (if (<= max 0)
2619 (setq looping nil)))
2620 (if found
2621 (let ((mb (match-beginning 0))
2622 (me (match-end 0)))
2623 (if (= mb me) ;zero-length match
2624 (if isearch-forward
2625 (if (= mb (if isearch-lazy-highlight-wrapped
2626 isearch-lazy-highlight-start
2627 (window-end)))
2628 (setq found nil)
2629 (forward-char 1))
2630 (if (= mb (if isearch-lazy-highlight-wrapped
2631 isearch-lazy-highlight-end
2632 (window-start)))
2633 (setq found nil)
2634 (forward-char -1)))
2635
2636 ;; non-zero-length match
2637 (let ((ov (make-overlay mb me)))
2638 (push ov isearch-lazy-highlight-overlays)
704d3ae7
JL
2639 ;; 1000 is higher than ediff's 100+,
2640 ;; but lower than isearch main overlay's 1001
2641 (overlay-put ov 'priority 1000)
46fe9018 2642 (overlay-put ov 'face lazy-highlight-face)
6ee8e621
JL
2643 (overlay-put ov 'window (selected-window))))
2644 (if isearch-forward
2645 (setq isearch-lazy-highlight-end (point))
2646 (setq isearch-lazy-highlight-start (point)))))
2647
2648 ;; not found or zero-length match at the search bound
2649 (if (not found)
2650 (if isearch-lazy-highlight-wrapped
2651 (setq looping nil
2652 nomore t)
2653 (setq isearch-lazy-highlight-wrapped t)
5ee5cd10 2654 (if isearch-forward
6ee8e621
JL
2655 (progn
2656 (setq isearch-lazy-highlight-end (window-start))
f9114cec
RS
2657 (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
2658 (window-start))))
6ee8e621 2659 (setq isearch-lazy-highlight-start (window-end))
f9114cec
RS
2660 (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
2661 (window-end))))))))
6ee8e621
JL
2662 (unless nomore
2663 (setq isearch-lazy-highlight-timer
2c987fc2 2664 (run-at-time lazy-highlight-interval nil
6ee8e621 2665 'isearch-lazy-highlight-update)))))))))
44336afb 2666
72a69d7f 2667(defun isearch-resume (string regexp word forward message case-fold)
c40a4de1 2668 "Resume an incremental search.
72a69d7f 2669STRING is the string or regexp searched for.
c40a4de1
GM
2670REGEXP non-nil means the resumed search was a regexp search.
2671WORD non-nil means resume a word search.
2672FORWARD non-nil means resume a forward search.
2673MESSAGE is the echo-area message recorded for the search resumed.
2674CASE-FOLD non-nil means the search was case-insensitive."
2675 (isearch-mode forward regexp nil nil word)
72a69d7f 2676 (setq isearch-string string
c40a4de1
GM
2677 isearch-message message
2678 isearch-case-fold-search case-fold)
72a69d7f
JL
2679 (isearch-search)
2680 (isearch-update))
191f025a 2681
81e898ea 2682;; arch-tag: 74850515-f7d8-43a6-8a2c-ca90a4c1e675
3b1e4dd1 2683;;; isearch.el ends here