(Text from Minibuffer): Lower-case Word to word.
[bpt/emacs.git] / lisp / isearch.el
... / ...
CommitLineData
1;;; isearch.el --- incremental search minor mode
2
3;; Copyright (C) 1992, 1993, 1994, 1995, 1996, 1997, 1999, 2000,
4;; 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
7;; Maintainer: FSF
8;; Keywords: matching
9
10;; This file is part of GNU Emacs.
11
12;; GNU Emacs is free software: you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
16
17;; GNU Emacs is distributed in the hope that it will be useful,
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
23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25;;; Commentary:
26
27;; Instructions
28
29;; For programmed use of isearch-mode, e.g. calling (isearch-forward),
30;; isearch-mode behaves modally and does not return until the search
31;; is completed. It uses a recursive-edit to behave this way.
32
33;; The key bindings active within isearch-mode are defined below in
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
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.
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
58;;; Code:
59
60\f
61;; Some additional options and constants.
62
63(defgroup isearch nil
64 "Incremental search minor mode."
65 :link '(emacs-commentary-link "isearch")
66 :link '(custom-manual "(emacs)Incremental Search")
67 :prefix "isearch-"
68 :prefix "search-"
69 :group 'matching)
70
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
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)
81and the value is minus the number of lines."
82 :type 'integer
83 :group 'isearch)
84
85(defcustom search-slow-speed 1200
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
88that the search has reached."
89 :type 'integer
90 :group 'isearch)
91
92(defcustom search-upper-case 'not-yanks
93 "*If non-nil, upper case chars disable case fold searching.
94That is, upper and lower case chars must match exactly.
95This applies no matter where the chars come from, but does not
96apply to chars in regexps that are prefixed with `\\'.
97If this value is `not-yanks', text yanked into the search string
98in Isearch mode is always downcased."
99 :type '(choice (const :tag "off" nil)
100 (const not-yanks)
101 (other :tag "on" t))
102 :group 'isearch)
103
104(defcustom search-nonincremental-instead t
105 "*If non-nil, do a nonincremental search instead if exiting immediately.
106Actually, `isearch-edit-string' is called to let you enter the search
107string, and RET terminates editing and does a nonincremental search."
108 :type 'boolean
109 :group 'isearch)
110
111(defcustom search-whitespace-regexp "\\s-+"
112 "*If non-nil, regular expression to match a sequence of whitespace chars.
113This applies to regular expression incremental search.
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,
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)
123 :group 'isearch)
124
125(defcustom search-invisible 'open
126 "If t incremental search can match hidden text.
127A nil value means don't match invisible text.
128When the value is `open', if the text matched is made invisible by
129an overlay having an `invisible' property and that overlay has a property
130`isearch-open-invisible', then incremental search will show the contents.
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."
134 :type '(choice (const :tag "Match hidden text" t)
135 (const :tag "Open overlays" open)
136 (const :tag "Don't match hidden text" nil))
137 :group 'isearch)
138
139(defcustom isearch-hide-immediately t
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.
144Ordinarily the text becomes invisible again at the end of the search."
145 :type 'boolean
146 :group 'isearch)
147
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."
152 :type 'boolean
153 :group 'isearch)
154
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
159 "Function(s) to call after terminating an incremental search.
160When these functions are called, `isearch-mode-end-hook-quit'
161is non-nil if the user quits the search.")
162
163(defvar isearch-mode-end-hook-quit nil
164 "Non-nil while running `isearch-mode-end-hook' if the user quits the search.")
165
166(defvar isearch-message-function nil
167 "Function to call to display the search prompt.
168If nil, use `isearch-message'.")
169
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
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
182matched by the search. If this function returns nil, continue
183searching without stopping at this match.")
184
185;; Search ring.
186
187(defvar search-ring nil
188 "List of search string sequences.")
189(defvar regexp-search-ring nil
190 "List of regular expression search string sequences.")
191
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)
200
201(defvar search-ring-yank-pointer nil
202 "Index in `search-ring' of last string reused.
203It is nil if none yet.")
204(defvar regexp-search-ring-yank-pointer nil
205 "Index in `regexp-search-ring' of last string reused.
206It is nil if none yet.")
207
208(defcustom search-ring-update nil
209 "*Non-nil if advancing or retreating in the search ring should cause search.
210Default value, nil, means edit the string instead."
211 :type 'boolean
212 :group 'isearch)
213
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.
225 (:background "magenta3" :foreground "lightskyblue1"))
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."
234 :group 'isearch
235 :group 'basic-faces)
236(defvar isearch 'isearch)
237
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)))
250 "Face for highlighting failed part in Isearch echo-area message."
251 :version "23.1"
252 :group 'isearch)
253
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
263;;; Lazy highlight customization.
264
265(defgroup lazy-highlight nil
266 "Lazy highlighting feature for matching strings."
267 :prefix "lazy-highlight-"
268 :version "21.1"
269 :group 'isearch
270 :group 'matching)
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
275\\[lazy-highlight-cleanup]."
276 :type 'boolean
277 :group 'lazy-highlight)
278(define-obsolete-variable-alias 'isearch-lazy-highlight-cleanup
279 'lazy-highlight-cleanup
280 "22.1")
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)
286(define-obsolete-variable-alias 'isearch-lazy-highlight-initial-delay
287 'lazy-highlight-initial-delay
288 "22.1")
289
290(defcustom lazy-highlight-interval 0 ; 0.0625
291 "*Seconds between lazily highlighting successive matches."
292 :type 'number
293 :group 'lazy-highlight)
294(define-obsolete-variable-alias 'isearch-lazy-highlight-interval
295 'lazy-highlight-interval
296 "22.1")
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)
306(define-obsolete-variable-alias 'isearch-lazy-highlight-max-at-a-time
307 'lazy-highlight-max-at-a-time
308 "22.1")
309
310(defface lazy-highlight
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."
321 :group 'lazy-highlight
322 :group 'basic-faces)
323(put 'isearch-lazy-highlight-face 'face-alias 'lazy-highlight)
324(defvar lazy-highlight-face 'lazy-highlight)
325(define-obsolete-variable-alias 'isearch-lazy-highlight-face
326 'lazy-highlight-face
327 "22.1")
328\f
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
396;; Define isearch-mode keymap.
397
398(defvar isearch-mode-map
399 (let* ((i 0)
400 (map (make-keymap)))
401 (or (char-table-p (nth 1 map))
402 (error "The initialization of isearch-mode-map must be updated"))
403 ;; Make all multibyte characters search for themselves.
404 (set-char-table-range (nth 1 map) (cons #x100 (max-char))
405 'isearch-printing-char)
406 ;; Make function keys, etc, which aren't bound to a scrolling-function
407 ;; exit the search.
408 (define-key map [t] 'isearch-other-control-char)
409 ;; Control chars, by default, end isearch mode transparently.
410 ;; We need these explicit definitions because, in a dense keymap,
411 ;; the binding for t does not affect characters.
412 ;; We use a dense keymap to save space.
413 (while (< i ?\s)
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.
418 (setq i ?\s)
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)
443
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)
449
450 (define-key map "\C-q" 'isearch-quote-char)
451
452 (define-key map "\r" 'isearch-exit)
453 (define-key map "\C-j" 'isearch-printing-char)
454 (define-key map "\t" 'isearch-printing-char)
455 (define-key map [?\S-\ ] 'isearch-printing-char)
456
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)
461
462 (define-key map "\C-h" isearch-help-map)
463
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)
477 (define-key map [mouse-movement] nil)
478 (define-key map [language-change] nil)
479
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.
485 (define-key map [mouse-2] #'isearch-mouse-2)
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
494 (define-key map "\M-sr" 'isearch-toggle-regexp)
495 (define-key map "\M-sw" 'isearch-toggle-word)
496
497 (define-key map [?\M-%] 'isearch-query-replace)
498 (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
499 (define-key map "\M-so" 'isearch-occur)
500 (define-key map "\M-shr" 'isearch-highlight-regexp)
501
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)
508 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
509 (define-key map "\M-\t" 'isearch-complete-edit)
510 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
511 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
512 (define-key map "\C-w" 'isearch-edit-string-set-word)
513 (define-key map "\C-f" 'isearch-yank-char-in-minibuffer)
514 (define-key map [right] 'isearch-yank-char-in-minibuffer)
515 map)
516 "Keymap for editing isearch strings in the minibuffer.")
517
518;; Internal variables declared globally for byte-compiler.
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.
525(defvar isearch-hidden nil) ; Non-nil if the string exists but is invisible.
526
527(defvar isearch-cmds nil
528 "Stack of search status sets.
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]")
532
533(defvar isearch-string "") ; The current search string.
534(defvar isearch-message "") ; text-char-description version of isearch-string
535
536(defvar isearch-success t) ; Searching is currently successful.
537(defvar isearch-error nil) ; Error message for failed search.
538(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
539(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
540(defvar isearch-barrier 0)
541(defvar isearch-just-started nil)
542(defvar isearch-start-hscroll 0) ; hscroll when starting the search.
543
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)
548
549(defvar isearch-last-case-fold-search nil)
550
551;; Used to save default value while isearch is active
552(defvar isearch-original-minibuffer-message-timeout nil)
553
554(defvar isearch-adjusted nil)
555(defvar isearch-slow-terminal-mode nil)
556;; If t, using a small window.
557(defvar isearch-small-window nil)
558(defvar isearch-opoint 0)
559;; The window configuration active at the beginning of the search.
560(defvar isearch-window-configuration nil)
561
562;; Flag to indicate a yank occurred, so don't move the cursor.
563(defvar isearch-yank-flag nil)
564
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'.
568(defvar isearch-op-fun nil)
569
570;; Is isearch-mode in a recursive edit for modal searching.
571(defvar isearch-recursive-edit nil)
572
573;; Should isearch be terminated after doing one search?
574(defvar isearch-nonincremental nil)
575
576;; New value of isearch-forward after isearch-edit-string.
577(defvar isearch-new-forward nil)
578
579;; Accumulate here the overlays opened during searching.
580(defvar isearch-opened-overlays nil)
581
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
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
596(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
597(make-variable-buffer-local 'isearch-mode)
598
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)
603(define-key search-map "w" 'isearch-forward-word)
604
605;; Entry points to isearch-mode.
606
607(defun isearch-forward (&optional regexp-p no-recursive-edit)
608 "\
609Do incremental search forward.
610With a prefix argument, do an incremental regular expression search instead.
611\\<isearch-mode-map>
612As you type characters, they add to the search string and are found.
613The following non-printing keys are bound in `isearch-mode-map'.
614
615Type \\[isearch-delete-char] to cancel last input item from end of search string.
616Type \\[isearch-exit] to exit, leaving point at location found.
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.
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.
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\
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.
627Type \\[isearch-yank-kill] to yank the last string of killed text.
628Type \\[isearch-quote-char] to quote control character to search for it.
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.
634
635If you try to exit with the search string still empty, it invokes
636 nonincremental search.
637
638Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
639Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
640Type \\[isearch-toggle-word] to toggle word mode.
641Type \\[isearch-edit-string] to edit the search string in the minibuffer.
642
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.
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.
648
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
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
662If an input method is turned on in the current buffer, that input
663method is also active while you are typing characters to search. To
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
668\\[isearch-toggle-specified-input-method], and specify an input method
669you want to use.
670
671The above keys, bound in `isearch-mode-map', are often controlled by
672 options; do \\[apropos] on search-.* to find them.
673Other control and meta characters terminate the search
674 and are then executed normally (depending on `search-exit-option').
675Likewise for function keys and mouse button events.
676
677If this function is called non-interactively, it does not return to
678the calling function until the search is done."
679
680 (interactive "P\np")
681 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
682
683(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
684 "\
685Do incremental search forward for regular expression.
686With a prefix argument, do a regular string search instead.
687Like ordinary incremental search except that your input is treated
688as a regexp. See the command `isearch-forward' for more information.
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
693and nothing else, enter C-q SPC."
694 (interactive "P\np")
695 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
696
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.
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."
704 (interactive "P\np")
705 (isearch-mode t nil nil (not no-recursive-edit) (null not-word)))
706
707(defun isearch-backward (&optional regexp-p no-recursive-edit)
708 "\
709Do incremental search backward.
710With a prefix argument, do a regular expression search instead.
711See the command `isearch-forward' for more information."
712 (interactive "P\np")
713 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
714
715(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
716 "\
717Do incremental search backward for regular expression.
718With a prefix argument, do a regular string search instead.
719Like ordinary incremental search except that your input is treated
720as a regexp. See the command `isearch-forward' for more information."
721 (interactive "P\np")
722 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
723
724\f
725;; isearch-mode only sets up incremental search for the minor mode.
726;; All the work is done by the isearch-mode commands.
727
728;; Not used yet:
729;;(defvar isearch-commands '(isearch-forward isearch-backward
730;; isearch-forward-regexp isearch-backward-regexp)
731;; "List of commands for which isearch-mode does not recursive-edit.")
732
733
734(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
735 "Start isearch minor mode.
736It is called by the function `isearch-forward' and other related functions."
737
738 ;; Initialize global vars.
739 (setq isearch-forward forward
740 isearch-regexp regexp
741 isearch-word word-p
742 isearch-op-fun op-fun
743 isearch-last-case-fold-search isearch-case-fold-search
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
753 isearch-error nil
754 isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
755 (> (window-height)
756 (* 4
757 (abs search-slow-window-lines))))
758 isearch-other-end nil
759 isearch-small-window nil
760 isearch-just-started t
761 isearch-start-hscroll (window-hscroll)
762
763 isearch-opoint (point)
764 search-ring-yank-pointer nil
765 isearch-opened-overlays nil
766 isearch-input-method-function input-method-function
767 isearch-input-method-local-p (local-variable-p 'input-method-function)
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)
774
775 ;; We must bypass input method while reading key. When a user type
776 ;; printable character, appropriate input method is turned on in
777 ;; minibuffer to read multibyte characters.
778 (or isearch-input-method-local-p
779 (make-local-variable 'input-method-function))
780 (setq input-method-function nil)
781
782 (looking-at "")
783 (setq isearch-window-configuration
784 (if isearch-slow-terminal-mode (current-window-configuration) nil))
785
786 ;; Maybe make minibuffer frame visible and/or raise it.
787 (let ((frame (window-frame (minibuffer-window))))
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))))
793
794 (setq isearch-mode " Isearch") ;; forward? regexp?
795 (force-mode-line-update)
796
797 (isearch-push-state)
798
799 (setq overriding-terminal-local-map isearch-mode-map)
800 (isearch-update)
801 (run-hooks 'isearch-mode-hook)
802
803 (add-hook 'mouse-leave-buffer-hook 'isearch-done)
804 (add-hook 'kbd-macro-termination-hook 'isearch-done)
805
806 ;; isearch-mode can be made modal (in the sense of not returning to
807 ;; the calling function until searching is completed) by entering
808 ;; a recursive-edit and exiting it when done isearching.
809 (if recursive-edit
810 (let ((isearch-recursive-edit t))
811 (recursive-edit)))
812 isearch-success)
813
814
815;; Some high level utilities. Others below.
816
817(defun isearch-update ()
818 ;; Called after each command to update the display.
819 (if (and (null unread-command-events)
820 (null executing-kbd-macro))
821 (progn
822 (if (not (input-pending-p))
823 (if isearch-message-function
824 (funcall isearch-message-function)
825 (isearch-message)))
826 (if (and isearch-slow-terminal-mode
827 (not (or isearch-small-window
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))
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
851 (if (< isearch-other-end (point)) ; isearch-forward?
852 (isearch-highlight isearch-other-end (point))
853 (isearch-highlight (point) isearch-other-end))
854 (isearch-dehighlight))
855 ))
856 (setq ;; quit-flag nil not for isearch-mode
857 isearch-adjusted nil
858 isearch-yank-flag nil)
859 (when isearch-lazy-highlight
860 (isearch-lazy-highlight-new-loop))
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))
864
865(defun isearch-done (&optional nopush edit)
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
872 (if isearch-resume-in-command-history
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)))))
879
880 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
881 (remove-hook 'kbd-macro-termination-hook 'isearch-done)
882 (setq isearch-lazy-highlight-start nil)
883
884 ;; Called by all commands that terminate isearch-mode.
885 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
886 (setq overriding-terminal-local-map nil)
887 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
888 (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
889 (isearch-dehighlight)
890 (lazy-highlight-cleanup lazy-highlight-cleanup)
891 (let ((found-start (window-start (selected-window)))
892 (found-point (point)))
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))))
902
903 (setq isearch-mode nil)
904 (if isearch-input-method-local-p
905 (setq input-method-function isearch-input-method-function)
906 (kill-local-variable 'input-method-function))
907
908 (force-mode-line-update)
909
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
921 (if (and (> (length isearch-string) 0) (not nopush))
922 ;; Update the ring data.
923 (isearch-update-ring isearch-string isearch-regexp))
924
925 (let ((isearch-mode-end-hook-quit (and nopush (not edit))))
926 (run-hooks 'isearch-mode-end-hook))
927
928 ;; If there was movement, mark the starting position.
929 ;; Maybe should test difference between and set mark only if > threshold.
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
937 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
938
939(defun isearch-update-ring (string &optional regexp)
940 "Add STRING to the beginning of the search ring.
941REGEXP if non-nil says use the regexp search ring."
942 (add-to-history
943 (if regexp 'regexp-search-ring 'search-ring)
944 string
945 (if regexp regexp-search-ring-max search-ring-max)))
946
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))))
955
956\f
957;; The search status structure and stack.
958
959(defsubst isearch-string-state (frame)
960 "Return the search string in FRAME."
961 (aref frame 0))
962(defsubst isearch-message-state (frame)
963 "Return the search string to display to the user in FRAME."
964 (aref frame 1))
965(defsubst isearch-point-state (frame)
966 "Return the point in FRAME."
967 (aref frame 2))
968(defsubst isearch-success-state (frame)
969 "Return the success flag in FRAME."
970 (aref frame 3))
971(defsubst isearch-forward-state (frame)
972 "Return the searching-forward flag in FRAME."
973 (aref frame 4))
974(defsubst isearch-other-end-state (frame)
975 "Return the other end of the match in FRAME."
976 (aref frame 5))
977(defsubst isearch-word-state (frame)
978 "Return the search-by-word flag in FRAME."
979 (aref frame 6))
980(defsubst isearch-error-state (frame)
981 "Return the regexp error message in FRAME, or nil if its regexp is valid."
982 (aref frame 7))
983(defsubst isearch-wrapped-state (frame)
984 "Return the search-wrapped flag in FRAME."
985 (aref frame 8))
986(defsubst isearch-barrier-state (frame)
987 "Return the barrier value in FRAME."
988 (aref frame 9))
989(defsubst isearch-case-fold-search-state (frame)
990 "Return the case-folding flag in FRAME."
991 (aref frame 10))
992(defsubst isearch-pop-fun-state (frame)
993 "Return the function restoring the mode-specific isearch state in FRAME."
994 (aref frame 11))
995
996(defun isearch-top-state ()
997 (let ((cmd (car isearch-cmds)))
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)
1004 isearch-error (isearch-error-state cmd)
1005 isearch-wrapped (isearch-wrapped-state cmd)
1006 isearch-barrier (isearch-barrier-state cmd)
1007 isearch-case-fold-search (isearch-case-fold-search-state cmd))
1008 (if (functionp (isearch-pop-fun-state cmd))
1009 (funcall (isearch-pop-fun-state cmd) cmd))
1010 (goto-char (isearch-point-state cmd))))
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
1021 isearch-error isearch-wrapped isearch-barrier
1022 isearch-case-fold-search
1023 (if isearch-push-state-function
1024 (funcall isearch-push-state-function)))
1025 isearch-cmds)))
1026
1027\f
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
1034nonincremental search instead via `isearch-edit-string'."
1035 (interactive)
1036 (if (and search-nonincremental-instead
1037 (= 0 (length isearch-string)))
1038 (let ((isearch-nonincremental t))
1039 (isearch-edit-string)))
1040 (isearch-done)
1041 (isearch-clean-overlays))
1042
1043
1044(defun isearch-edit-string ()
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.
1051\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
1052\\[isearch-complete-edit] to complete the search string using the search ring.
1053\\<isearch-mode-map>
1054If first char entered is \\[isearch-yank-word-or-char], then do word search instead."
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.
1060 ;; Editing doesn't back up the search point. Should it?
1061 (interactive)
1062 (condition-case err
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)
1083 (isearch-error isearch-error)
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))
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)
1102 old-point old-other-end)
1103
1104 ;; Actually terminate isearching until editing is done.
1105 ;; This is so that the user can do anything without failure,
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
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
1115 (unwind-protect
1116 (let* ((message-log-max nil)
1117 ;; Binding minibuffer-history-symbol to nil is a work-around
1118 ;; for some incompatibility with gmhist.
1119 (minibuffer-history-symbol))
1120 (setq isearch-new-string
1121 (read-from-minibuffer
1122 (isearch-message-prefix nil nil isearch-nonincremental)
1123 isearch-string
1124 minibuffer-local-isearch-map nil
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))))
1130 nil t)
1131 isearch-new-message
1132 (mapconcat 'isearch-text-char-description
1133 isearch-new-string "")))
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
1142 ;; Always resume isearching by restarting it.
1143 (isearch-mode isearch-forward
1144 isearch-regexp
1145 isearch-op-fun
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))
1160 "")
1161
1162 isearch-message
1163 (mapconcat 'isearch-text-char-description
1164 isearch-string ""))
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
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)
1175
1176 ;; Reinvoke the pending search.
1177 (isearch-search)
1178 (isearch-push-state) ; this pushes the correct state
1179 (isearch-update)
1180 (if isearch-nonincremental
1181 (progn
1182 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
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 "")))))
1188
1189 (quit ; handle abort-recursive-edit
1190 (isearch-abort) ;; outside of let to restore outside global values
1191 )))
1192
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
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))
1211
1212(defun isearch-reverse-exit-minibuffer ()
1213 (interactive)
1214 (setq isearch-new-forward nil)
1215 (exit-minibuffer))
1216
1217(defun isearch-cancel ()
1218 "Terminate the search and go back to the starting point."
1219 (interactive)
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))))
1223 (goto-char isearch-opoint)
1224 (isearch-done t) ; exit isearch
1225 (isearch-clean-overlays)
1226 (signal 'quit nil)) ; and pass on quit signal
1227
1228(defun isearch-abort ()
1229 "Abort incremental search mode if searching is successful, signaling quit.
1230Otherwise, revert to previous successful search and continue searching.
1231Use `isearch-exit' to quit without signaling."
1232 (interactive)
1233;; (ding) signal instead below, if quitting
1234 (discard-input)
1235 (if isearch-success
1236 ;; If search is successful, move back to starting point
1237 ;; and really do quit.
1238 (progn
1239 (setq isearch-success nil)
1240 (isearch-cancel))
1241 ;; If search is failing, or has an incomplete regexp,
1242 ;; rub out until it is once more successful.
1243 (while (or (not isearch-success) isearch-error)
1244 (isearch-pop-state))
1245 (isearch-update)))
1246
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.
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))
1263 ;; If already have what to search for, repeat it.
1264 (or isearch-success
1265 (progn
1266 ;; Set isearch-wrapped before calling isearch-wrap-function
1267 (setq isearch-wrapped t)
1268 (if isearch-wrap-function
1269 (funcall isearch-wrap-function)
1270 (goto-char (if isearch-forward (point-min) (point-max)))))))
1271 ;; C-s in reverse or C-r in forward, change direction.
1272 (setq isearch-forward (not isearch-forward)
1273 isearch-success t))
1274
1275 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
1276
1277 (if (equal isearch-string "")
1278 (setq isearch-success t)
1279 (if (and isearch-success
1280 (equal (point) isearch-other-end)
1281 (not isearch-just-started))
1282 ;; If repeating a search that found
1283 ;; an empty string, ensure we advance.
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
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))
1311 (if isearch-regexp (setq isearch-word nil))
1312 (setq isearch-success t isearch-adjusted t)
1313 (isearch-update))
1314
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
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))
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" "")))
1332 (setq isearch-success t isearch-adjusted t)
1333 (sit-for 1)
1334 (isearch-update))
1335
1336(defun isearch-query-replace (&optional regexp-flag)
1337 "Start `query-replace' with string to replace from last search string."
1338 (interactive)
1339 (barf-if-buffer-read-only)
1340 (if regexp-flag (setq isearch-regexp t))
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))
1345 (isearch-done)
1346 (isearch-clean-overlays)
1347 (if (and isearch-other-end
1348 (< isearch-other-end (point))
1349 (not (and transient-mark-mode mark-active
1350 (< (mark) (point)))))
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)))
1355 (perform-replace
1356 isearch-string
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)))))
1364
1365(defun isearch-query-replace-regexp ()
1366 "Start `query-replace-regexp' with string to replace from last search string."
1367 (interactive)
1368 (isearch-query-replace t))
1369
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
1384(declare-function hi-lock-regexp-okay "hi-lock" (regexp))
1385(declare-function hi-lock-read-face-name "hi-lock" ())
1386
1387(defun isearch-highlight-regexp ()
1388 "Run `highlight-regexp' with regexp from the current search string.
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)
1393 (isearch-done)
1394 (isearch-clean-overlays)
1395 (require 'hi-lock nil t)
1396 (let ((string (cond (isearch-regexp isearch-string)
1397 ((if (and (eq isearch-case-fold-search t)
1398 search-upper-case)
1399 (isearch-no-upper-case-p
1400 isearch-string isearch-regexp)
1401 isearch-case-fold-search)
1402 ;; Turn isearch-string into a case-insensitive
1403 ;; regexp.
1404 (mapconcat
1405 (lambda (c)
1406 (let ((s (string c)))
1407 (if (string-match "[[:alpha:]]" s)
1408 (format "[%s%s]" (upcase s) (downcase s))
1409 (regexp-quote s))))
1410 isearch-string ""))
1411 (t (regexp-quote isearch-string)))))
1412 (hi-lock-face-buffer string (hi-lock-read-face-name))))
1413
1414\f
1415(defun isearch-delete-char ()
1416 "Discard last input item and move point back.
1417If no previous match was done, just beep."
1418 (interactive)
1419 (if (null (cdr isearch-cmds))
1420 (ding)
1421 (isearch-pop-state))
1422 (isearch-update))
1423
1424(defun isearch-del-char (&optional arg)
1425 "Delete character from end of search string and search again.
1426If search string is empty, just beep."
1427 (interactive "p")
1428 (if (= 0 (length isearch-string))
1429 (ding)
1430 (setq isearch-string (substring isearch-string 0 (- (or arg 1)))
1431 isearch-message (mapconcat 'isearch-text-char-description
1432 isearch-string "")))
1433 ;; Use the isearch-other-end as new starting point to be able
1434 ;; to find the remaining part of the search string again.
1435 (if isearch-other-end (goto-char isearch-other-end))
1436 (isearch-search)
1437 (isearch-push-state)
1438 (isearch-update))
1439
1440(defun isearch-yank-string (string)
1441 "Pull STRING into search string."
1442 ;; Downcase the string if not supposed to case-fold yanked strings.
1443 (if (and isearch-case-fold-search
1444 (eq 'not-yanks search-upper-case))
1445 (setq string (downcase string)))
1446 (if isearch-regexp (setq string (regexp-quote string)))
1447 (setq isearch-string (concat isearch-string string)
1448 isearch-message
1449 (concat isearch-message
1450 (mapconcat 'isearch-text-char-description
1451 string ""))
1452 ;; Don't move cursor in reverse search.
1453 isearch-yank-flag t)
1454 (isearch-search-and-update))
1455
1456(defun isearch-yank-kill ()
1457 "Pull string from kill ring into search string."
1458 (interactive)
1459 (isearch-yank-string (current-kill 0)))
1460
1461(defun isearch-yank-x-selection ()
1462 "Pull current X selection into search string."
1463 (interactive)
1464 (isearch-yank-string (x-get-selection)))
1465
1466
1467(defun isearch-mouse-2 (click)
1468 "Handle mouse-2 in Isearch mode.
1469For a click in the echo area, invoke `isearch-yank-x-selection'.
1470Otherwise invoke whatever the calling mouse-2 command sequence
1471is bound to outside of Isearch."
1472 (interactive "e")
1473 (let* ((w (posn-window (event-start click)))
1474 (overriding-terminal-local-map nil)
1475 (binding (key-binding (this-command-keys-vector) t)))
1476 (if (and (window-minibuffer-p w)
1477 (not (minibuffer-window-active-p w))) ; in echo area
1478 (isearch-yank-x-selection)
1479 (when (functionp binding)
1480 (call-interactively binding)))))
1481
1482(defun isearch-yank-internal (jumpform)
1483 "Pull the text from point to the point reached by JUMPFORM.
1484JUMPFORM is a lambda expression that takes no arguments and returns a
1485buffer position, possibly having moved point to that position. For
1486example, it might move point forward by a word and return point, or it
1487might return the position of the end of the line."
1488 (isearch-yank-string
1489 (save-excursion
1490 (and (not isearch-forward) isearch-other-end
1491 (goto-char isearch-other-end))
1492 (buffer-substring-no-properties (point) (funcall jumpform)))))
1493
1494(defun isearch-yank-char-in-minibuffer (&optional arg)
1495 "Pull next character from buffer into end of search string in minibuffer."
1496 (interactive "p")
1497 (if (eobp)
1498 (insert
1499 (save-excursion
1500 (set-buffer (cadr (buffer-list)))
1501 (buffer-substring-no-properties
1502 (point) (progn (forward-char arg) (point)))))
1503 (forward-char arg)))
1504
1505(defun isearch-yank-char (&optional arg)
1506 "Pull next character from buffer into search string."
1507 (interactive "p")
1508 (isearch-yank-internal (lambda () (forward-char arg) (point))))
1509
1510(defun isearch-yank-word-or-char ()
1511 "Pull next character or word from buffer into search string."
1512 (interactive)
1513 (isearch-yank-internal
1514 (lambda ()
1515 (if (or (= (char-syntax (or (char-after) 0)) ?w)
1516 (= (char-syntax (or (char-after (1+ (point))) 0)) ?w))
1517 (forward-word 1)
1518 (forward-char 1)) (point))))
1519
1520(defun isearch-yank-word ()
1521 "Pull next word from buffer into search string."
1522 (interactive)
1523 (isearch-yank-internal (lambda () (forward-word 1) (point))))
1524
1525(defun isearch-yank-line ()
1526 "Pull rest of line from buffer into search string."
1527 (interactive)
1528 (isearch-yank-internal
1529 (lambda () (let ((inhibit-field-text-motion t))
1530 (line-end-position (if (eolp) 2 1))))))
1531
1532(defun isearch-search-and-update ()
1533 ;; Do the search and update the display.
1534 (when (or isearch-success
1535 ;; Unsuccessful regexp search may become successful by
1536 ;; addition of characters which make isearch-string valid
1537 isearch-regexp
1538 ;; If the string was found but was completely invisible,
1539 ;; it might now be partly visible, so try again.
1540 (prog1 isearch-hidden (setq isearch-hidden nil)))
1541 ;; In reverse search, adding stuff at
1542 ;; the end may cause zero or many more chars to be
1543 ;; matched, in the string following point.
1544 ;; Allow all those possibilities without moving point as
1545 ;; long as the match does not extend past search origin.
1546 (if (and (not isearch-forward) (not isearch-adjusted)
1547 (condition-case ()
1548 (let ((case-fold-search isearch-case-fold-search))
1549 (if (and (eq case-fold-search t) search-upper-case)
1550 (setq case-fold-search
1551 (isearch-no-upper-case-p isearch-string isearch-regexp)))
1552 (looking-at (if isearch-regexp isearch-string
1553 (regexp-quote isearch-string))))
1554 (error nil))
1555 (or isearch-yank-flag
1556 (<= (match-end 0)
1557 (min isearch-opoint isearch-barrier))))
1558 (progn
1559 (setq isearch-success t
1560 isearch-error nil
1561 isearch-other-end (match-end 0))
1562 (if (and (eq isearch-case-fold-search t) search-upper-case)
1563 (setq isearch-case-fold-search
1564 (isearch-no-upper-case-p isearch-string isearch-regexp))))
1565 ;; Not regexp, not reverse, or no match at point.
1566 (if (and isearch-other-end (not isearch-adjusted))
1567 (goto-char (if isearch-forward isearch-other-end
1568 (min isearch-opoint
1569 isearch-barrier
1570 (1+ isearch-other-end)))))
1571 (isearch-search)
1572 ))
1573 (isearch-push-state)
1574 (if isearch-op-fun (funcall isearch-op-fun))
1575 (isearch-update))
1576
1577
1578;; *, ?, }, and | chars can make a regexp more liberal.
1579;; They can make a regexp match sooner or make it succeed instead of failing.
1580;; So go back to place last successful search started
1581;; or to the last ^S/^R (barrier), whichever is nearer.
1582;; + needs no special handling because the string must match at least once.
1583
1584(defun isearch-backslash (str)
1585 "Return t if STR ends in an odd number of backslashes."
1586 (= (mod (- (length str) (string-match "\\\\*\\'" str)) 2) 1))
1587
1588(defun isearch-fallback (want-backslash &optional allow-invalid to-barrier)
1589 "Return point to previous successful match to allow regexp liberalization.
1590\\<isearch-mode-map>
1591Respects \\[isearch-repeat-forward] and \\[isearch-repeat-backward] by
1592stopping at `isearch-barrier' as needed.
1593
1594Do nothing if a backslash is escaping the liberalizing character.
1595If WANT-BACKSLASH is non-nil, invert this behavior (for \\} and \\|).
1596
1597Do nothing if regexp has recently been invalid unless optional
1598ALLOW-INVALID non-nil.
1599
1600If optional TO-BARRIER non-nil, ignore previous matches and go exactly
1601to the barrier."
1602 ;; (eq (not a) (not b)) makes all non-nil values equivalent
1603 (when (and isearch-regexp (eq (not (isearch-backslash isearch-string))
1604 (not want-backslash))
1605 ;; We have to check 2 stack frames because the last might be
1606 ;; invalid just because of a backslash.
1607 (or (not isearch-error)
1608 (not (isearch-error-state (cadr isearch-cmds)))
1609 allow-invalid))
1610 (if to-barrier
1611 (progn (goto-char isearch-barrier)
1612 (setq isearch-adjusted t))
1613 (let* ((stack isearch-cmds)
1614 (previous (cdr stack)) ; lookbelow in the stack
1615 (frame (car stack)))
1616 ;; Walk down the stack looking for a valid regexp (as of course only
1617 ;; they can be the previous successful match); this conveniently
1618 ;; removes all bracket-sets and groups that might be in the way, as
1619 ;; well as partial \{\} constructs that the code below leaves behind.
1620 ;; Also skip over postfix operators -- though horrid,
1621 ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly valid.
1622 (while (and previous
1623 (or (isearch-error-state frame)
1624 (let* ((string (isearch-string-state frame))
1625 (lchar (aref string (1- (length string)))))
1626 ;; The operators aren't always operators; check
1627 ;; backslashes. This doesn't handle the case of
1628 ;; operators at the beginning of the regexp not
1629 ;; being special, but then we should fall back to
1630 ;; the barrier anyway because it's all optional.
1631 (if (isearch-backslash
1632 (isearch-string-state (car previous)))
1633 (eq lchar ?\})
1634 (memq lchar '(?* ?? ?+))))))
1635 (setq stack previous previous (cdr previous) frame (car stack)))
1636 (when stack
1637 ;; `stack' now refers the most recent valid regexp that is not at
1638 ;; all optional in its last term. Now dig one level deeper and find
1639 ;; what matched before that.
1640 (let ((last-other-end
1641 (or (and (car previous)
1642 (isearch-other-end-state (car previous)))
1643 isearch-barrier)))
1644 (goto-char (if isearch-forward
1645 (max last-other-end isearch-barrier)
1646 (min last-other-end isearch-barrier)))
1647 (setq isearch-adjusted t)))))))
1648
1649(defun isearch-unread-key-sequence (keylist)
1650 "Unread the given key-sequence KEYLIST.
1651Scroll-bar or mode-line events are processed appropriately."
1652 (cancel-kbd-macro-events)
1653 (apply 'isearch-unread keylist)
1654 ;; If the event was a scroll-bar or mode-line click, the event will have
1655 ;; been prefixed by a symbol such as vertical-scroll-bar. We must remove
1656 ;; it here, because this symbol will be attached to the event again next
1657 ;; time it gets read by read-key-sequence.
1658 ;;
1659 ;; (Old comment from isearch-other-meta-char: "Note that we don't have to
1660 ;; modify the event anymore in 21 because read_key_sequence no longer
1661 ;; modifies events to produce fake prefix keys.")
1662 (if (and (> (length keylist) 1)
1663 (symbolp (car keylist))
1664 (listp (cadr keylist))
1665 (not (numberp (posn-point
1666 (event-start (cadr keylist) )))))
1667 (pop unread-command-events)))
1668
1669;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1670;; scrolling within Isearch mode. Alan Mackenzie (acm@muc.de), 2003/2/24
1671;;
1672;; The idea here is that certain vertical scrolling commands (like C-l
1673;; `recenter') should be usable WITHIN Isearch mode. For a command to be
1674;; suitable, it must NOT alter the buffer, swap to another buffer or frame,
1675;; tamper with isearch's state, or move point. It is unacceptable for the
1676;; search string to be scrolled out of the current window. If a command
1677;; attempts this, we scroll the text back again.
1678;;
1679;; We implement this feature with a property called `isearch-scroll'.
1680;; If a command's symbol has the value t for this property it is a
1681;; scrolling command. The feature needs to be enabled by setting the
1682;; customizable variable `isearch-allow-scroll' to a non-nil value.
1683;;
1684;; The universal argument commands (e.g. C-u) in simple.el are marked
1685;; as scrolling commands, and isearch.el has been amended to allow
1686;; prefix arguments to be passed through to scrolling commands. Thus
1687;; M-0 C-l will scroll point to the top of the window.
1688;;
1689;; Horizontal scrolling commands are currently not catered for.
1690;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1691
1692;; Set the isearch-scroll property on some standard functions:
1693;; Scroll-bar functions:
1694(if (fboundp 'scroll-bar-toolkit-scroll)
1695 (put 'scroll-bar-toolkit-scroll 'isearch-scroll t))
1696(if (fboundp 'w32-handle-scroll-bar-event)
1697 (put 'w32-handle-scroll-bar-event 'isearch-scroll t))
1698
1699;; Commands which scroll the window:
1700(put 'recenter 'isearch-scroll t)
1701(put 'recenter-top-bottom 'isearch-scroll t)
1702(put 'reposition-window 'isearch-scroll t)
1703(put 'scroll-up 'isearch-scroll t)
1704(put 'scroll-down 'isearch-scroll t)
1705
1706;; Commands which act on the other window
1707(put 'list-buffers 'isearch-scroll t)
1708(put 'scroll-other-window 'isearch-scroll t)
1709(put 'scroll-other-window-down 'isearch-scroll t)
1710(put 'beginning-of-buffer-other-window 'isearch-scroll t)
1711(put 'end-of-buffer-other-window 'isearch-scroll t)
1712
1713;; Commands which change the window layout
1714(put 'delete-other-windows 'isearch-scroll t)
1715(put 'balance-windows 'isearch-scroll t)
1716(put 'split-window-vertically 'isearch-scroll t)
1717(put 'split-window-horizontally 'isearch-scroll t)
1718(put 'enlarge-window 'isearch-scroll t)
1719
1720;; Universal argument commands
1721(put 'universal-argument 'isearch-scroll t)
1722(put 'negative-argument 'isearch-scroll t)
1723(put 'digit-argument 'isearch-scroll t)
1724
1725(defcustom isearch-allow-scroll nil
1726 "If non-nil, scrolling commands are allowed during incremental search."
1727 :type 'boolean
1728 :group 'isearch)
1729
1730(defun isearch-string-out-of-window (isearch-point)
1731 "Test whether the search string is currently outside of the window.
1732Return nil if it's completely visible, or if point is visible,
1733together with as much of the search string as will fit; the symbol
1734`above' if we need to scroll the text downwards; the symbol `below',
1735if upwards."
1736 (let ((w-start (window-start))
1737 (w-end (window-end nil t))
1738 (w-L1 (save-excursion (move-to-window-line 1) (point)))
1739 (w-L-1 (save-excursion (move-to-window-line -1) (point)))
1740 start end) ; start and end of search string in buffer
1741 (if isearch-forward
1742 (setq end isearch-point start (or isearch-other-end isearch-point))
1743 (setq start isearch-point end (or isearch-other-end isearch-point)))
1744 (cond ((or (and (>= start w-start) (<= end w-end))
1745 (if isearch-forward
1746 (and (>= isearch-point w-L-1) (< isearch-point w-end)) ; point on Line -1
1747 (and (>= isearch-point w-start) (< isearch-point w-L1)))) ; point on Line 0
1748 nil)
1749 ((and (< start w-start)
1750 (< isearch-point w-L-1))
1751 'above)
1752 (t 'below))))
1753
1754(defun isearch-back-into-window (above isearch-point)
1755 "Scroll the window to bring the search string back into view.
1756Restore point to ISEARCH-POINT in the process. ABOVE is t when the
1757search string is above the top of the window, nil when it is beneath
1758the bottom."
1759 (let (start end)
1760 (if isearch-forward
1761 (setq end isearch-point start (or isearch-other-end isearch-point))
1762 (setq start isearch-point end (or isearch-other-end isearch-point)))
1763 (if above
1764 (progn
1765 (goto-char start)
1766 (recenter 0)
1767 (when (>= isearch-point (window-end nil t))
1768 (goto-char isearch-point)
1769 (recenter -1)))
1770 (goto-char end)
1771 (recenter -1)
1772 (when (< isearch-point (window-start))
1773 (goto-char isearch-point)
1774 (recenter 0))))
1775 (goto-char isearch-point))
1776
1777(defun isearch-reread-key-sequence-naturally (keylist)
1778 "Reread key sequence KEYLIST with Isearch mode's keymap deactivated.
1779Return the key sequence as a string/vector."
1780 (isearch-unread-key-sequence keylist)
1781 (let (overriding-terminal-local-map)
1782 (read-key-sequence nil))) ; This will go through function-key-map, if nec.
1783
1784(defun isearch-lookup-scroll-key (key-seq)
1785 "If KEY-SEQ is bound to a scrolling command, return it as a symbol.
1786Otherwise return nil."
1787 (let* ((overriding-terminal-local-map nil)
1788 (binding (key-binding key-seq)))
1789 (and binding (symbolp binding) (commandp binding)
1790 (eq (get binding 'isearch-scroll) t)
1791 binding)))
1792
1793(defalias 'isearch-other-control-char 'isearch-other-meta-char)
1794
1795(defun isearch-other-meta-char (&optional arg)
1796 "Process a miscellaneous key sequence in Isearch mode.
1797
1798Try to convert the current key-sequence to something usable in Isearch
1799mode, either by converting it with `function-key-map', downcasing a
1800key with C-<upper case>, or finding a \"scrolling command\" bound to
1801it. \(In the last case, we may have to read more events.) If so,
1802either unread the converted sequence or execute the command.
1803
1804Otherwise, if `search-exit-option' is non-nil (the default) unread the
1805key-sequence and exit the search normally. If it is the symbol
1806`edit', the search string is edited in the minibuffer and the meta
1807character is unread so that it applies to editing the string.
1808
1809ARG is the prefix argument. It will be transmitted through to the
1810scrolling command or to the command whose key-sequence exits
1811Isearch mode."
1812 (interactive "P")
1813 (let* ((key (if current-prefix-arg ; not nec the same as ARG
1814 (substring (this-command-keys) universal-argument-num-events)
1815 (this-command-keys)))
1816 (main-event (aref key 0))
1817 (keylist (listify-key-sequence key))
1818 scroll-command isearch-point)
1819 (cond ((and (= (length key) 1)
1820 (let ((lookup (lookup-key local-function-key-map key)))
1821 (not (or (null lookup) (integerp lookup)
1822 (keymapp lookup)))))
1823 ;; Handle a function key that translates into something else.
1824 ;; If the key has a global definition too,
1825 ;; exit and unread the key itself, so its global definition runs.
1826 ;; Otherwise, unread the translation,
1827 ;; so that the translated key takes effect within isearch.
1828 (cancel-kbd-macro-events)
1829 (if (lookup-key global-map key)
1830 (progn
1831 (isearch-done)
1832 (apply 'isearch-unread keylist))
1833 (setq keylist
1834 (listify-key-sequence (lookup-key local-function-key-map key)))
1835 (while keylist
1836 (setq key (car keylist))
1837 ;; If KEY is a printing char, we handle it here
1838 ;; directly to avoid the input method and keyboard
1839 ;; coding system translating it.
1840 (if (and (integerp key)
1841 (>= key ?\s) (/= key 127) (< key 256))
1842 (progn
1843 (isearch-process-search-char key)
1844 (setq keylist (cdr keylist)))
1845 ;; As the remaining keys in KEYLIST can't be handled
1846 ;; here, we must reread them.
1847 (apply 'isearch-unread keylist)
1848 (setq keylist nil)))))
1849 (
1850 ;; Handle an undefined shifted control character
1851 ;; by downshifting it if that makes it defined.
1852 ;; (As read-key-sequence would normally do,
1853 ;; if we didn't have a default definition.)
1854 (let ((mods (event-modifiers main-event)))
1855 (and (integerp main-event)
1856 (memq 'shift mods)
1857 (memq 'control mods)
1858 (not (memq (lookup-key isearch-mode-map
1859 (let ((copy (copy-sequence key)))
1860 (aset copy 0
1861 (- main-event
1862 (- ?\C-\S-a ?\C-a)))
1863 copy)
1864 nil)
1865 '(nil
1866 isearch-other-control-char)))))
1867 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
1868 (cancel-kbd-macro-events)
1869 (apply 'isearch-unread keylist))
1870 ((eq search-exit-option 'edit)
1871 (apply 'isearch-unread keylist)
1872 (isearch-edit-string))
1873 ;; Handle a scrolling function.
1874 ((and isearch-allow-scroll
1875 (progn (setq key (isearch-reread-key-sequence-naturally keylist))
1876 (setq keylist (listify-key-sequence key))
1877 (setq main-event (aref key 0))
1878 (setq scroll-command (isearch-lookup-scroll-key key))))
1879 ;; From this point onwards, KEY, KEYLIST and MAIN-EVENT hold a
1880 ;; complete key sequence, possibly as modified by function-key-map,
1881 ;; not merely the one or two event fragment which invoked
1882 ;; isearch-other-meta-char in the first place.
1883 (setq isearch-point (point))
1884 (setq prefix-arg arg)
1885 (command-execute scroll-command)
1886 (let ((ab-bel (isearch-string-out-of-window isearch-point)))
1887 (if ab-bel
1888 (isearch-back-into-window (eq ab-bel 'above) isearch-point)
1889 (goto-char isearch-point)))
1890 (isearch-update))
1891 ;; A mouse click on the isearch message starts editing the search string
1892 ((and (eq (car-safe main-event) 'down-mouse-1)
1893 (window-minibuffer-p (posn-window (event-start main-event))))
1894 ;; Swallow the up-event.
1895 (read-event)
1896 (isearch-edit-string))
1897 (search-exit-option
1898 (let (window)
1899 (isearch-unread-key-sequence keylist)
1900 (setq main-event (car unread-command-events))
1901
1902 ;; If we got a mouse click event, that event contains the
1903 ;; window clicked on. maybe it was read with the buffer
1904 ;; it was clicked on. If so, that buffer, not the current one,
1905 ;; is in isearch mode. So end the search in that buffer.
1906
1907 ;; ??? I have no idea what this if checks for, but it's
1908 ;; obviously wrong for the case that a down-mouse event
1909 ;; on another window invokes this function. The event
1910 ;; will contain the window clicked on and that window's
1911 ;; buffer is certainly not always in Isearch mode.
1912 ;;
1913 ;; Leave the code in, but check for current buffer not
1914 ;; being in Isearch mode for now, until someone tells
1915 ;; what it's really supposed to do.
1916 ;;
1917 ;; --gerd 2001-08-10.
1918
1919 (if (and (not isearch-mode)
1920 (listp main-event)
1921 (setq window (posn-window (event-start main-event)))
1922 (windowp window)
1923 (or (> (minibuffer-depth) 0)
1924 (not (window-minibuffer-p window))))
1925 (save-excursion
1926 (set-buffer (window-buffer window))
1927 (isearch-done)
1928 (isearch-clean-overlays))
1929 (isearch-done)
1930 (isearch-clean-overlays)
1931 (setq prefix-arg arg))))
1932 (t;; otherwise nil
1933 (isearch-process-search-string key key)))))
1934
1935(defun isearch-quote-char ()
1936 "Quote special characters for incremental search."
1937 (interactive)
1938 (let ((char (read-quoted-char (isearch-message t))))
1939 ;; Assume character codes 0200 - 0377 stand for characters in some
1940 ;; single-byte character set, and convert them to Emacs
1941 ;; characters.
1942 (if (and isearch-regexp (= char ?\s))
1943 (if (subregexp-context-p isearch-string (length isearch-string))
1944 (isearch-process-search-string "[ ]" " ")
1945 (isearch-process-search-char char))
1946 (and enable-multibyte-characters
1947 (>= char ?\200)
1948 (<= char ?\377)
1949 (setq char (unibyte-char-to-multibyte char)))
1950 (isearch-process-search-char char))))
1951
1952(defun isearch-return-char ()
1953 "Convert return into newline for incremental search."
1954 (interactive)
1955 (isearch-process-search-char ?\n))
1956(make-obsolete 'isearch-return-char 'isearch-printing-char "19.7")
1957
1958(defun isearch-printing-char ()
1959 "Add this ordinary printing character to the search string and search."
1960 (interactive)
1961 (let ((char last-command-char))
1962 (if (= char ?\S-\ )
1963 (setq char ?\s))
1964 (if current-input-method
1965 (isearch-process-search-multibyte-characters char)
1966 (isearch-process-search-char char))))
1967
1968(defun isearch-process-search-char (char)
1969 ;; * and ? are special in regexps when not preceded by \.
1970 ;; } and | are special in regexps when preceded by \.
1971 ;; Nothing special for + because it matches at least once.
1972 (cond
1973 ((memq char '(?* ??)) (isearch-fallback nil))
1974 ((eq char ?\}) (isearch-fallback t t))
1975 ((eq char ?|) (isearch-fallback t nil t)))
1976
1977 ;; Append the char to the search string, update the message and re-search.
1978 (isearch-process-search-string
1979 (char-to-string char)
1980 (if (>= char ?\200)
1981 (char-to-string char)
1982 (isearch-text-char-description char))))
1983
1984(defun isearch-process-search-string (string message)
1985 (setq isearch-string (concat isearch-string string)
1986 isearch-message (concat isearch-message message))
1987 (isearch-search-and-update))
1988
1989\f
1990;; Search Ring
1991
1992(defun isearch-ring-adjust1 (advance)
1993 ;; Helper for isearch-ring-adjust
1994 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1995 (length (length ring))
1996 (yank-pointer-name (if isearch-regexp
1997 'regexp-search-ring-yank-pointer
1998 'search-ring-yank-pointer))
1999 (yank-pointer (eval yank-pointer-name)))
2000 (if (zerop length)
2001 ()
2002 (set yank-pointer-name
2003 (setq yank-pointer
2004 (mod (+ (or yank-pointer 0)
2005 (if advance -1 1))
2006 length)))
2007 (setq isearch-string (nth yank-pointer ring)
2008 isearch-message (mapconcat 'isearch-text-char-description
2009 isearch-string "")))))
2010
2011(defun isearch-ring-adjust (advance)
2012 ;; Helper for isearch-ring-advance and isearch-ring-retreat
2013 (isearch-ring-adjust1 advance)
2014 (if search-ring-update
2015 (progn
2016 (isearch-search)
2017 (isearch-push-state)
2018 (isearch-update))
2019 ;; Otherwise, edit the search string instead. Note that there is
2020 ;; no need to push the search state after isearch-edit-string here
2021 ;; since isearch-edit-string already pushes its state
2022 (isearch-edit-string)))
2023
2024(defun isearch-ring-advance ()
2025 "Advance to the next search string in the ring."
2026 ;; This could be more general to handle a prefix arg, but who would use it.
2027 (interactive)
2028 (isearch-ring-adjust 'advance))
2029
2030(defun isearch-ring-retreat ()
2031 "Retreat to the previous search string in the ring."
2032 (interactive)
2033 (isearch-ring-adjust nil))
2034
2035(defun isearch-complete1 ()
2036 ;; Helper for isearch-complete and isearch-complete-edit
2037 ;; Return t if completion OK, nil if no completion exists.
2038 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
2039 (completion-ignore-case case-fold-search)
2040 (completion (try-completion isearch-string ring)))
2041 (cond
2042 ((eq completion t)
2043 ;; isearch-string stays the same
2044 t)
2045 ((or completion ; not nil, must be a string
2046 (= 0 (length isearch-string))) ; shouldn't have to say this
2047 (if (equal completion isearch-string) ;; no extension?
2048 (progn
2049 (if completion-auto-help
2050 (with-output-to-temp-buffer "*Isearch completions*"
2051 (display-completion-list
2052 (all-completions isearch-string ring))))
2053 t)
2054 (and completion
2055 (setq isearch-string completion))))
2056 (t
2057 (message "No completion") ; waits a second if in minibuffer
2058 nil))))
2059
2060(defun isearch-complete ()
2061 "Complete the search string from the strings on the search ring.
2062The completed string is then editable in the minibuffer.
2063If there is no completion possible, say so and continue searching."
2064 (interactive)
2065 (if (isearch-complete1)
2066 (progn (setq isearch-message
2067 (mapconcat 'isearch-text-char-description
2068 isearch-string ""))
2069 (isearch-edit-string))
2070 ;; else
2071 (sit-for 1)
2072 (isearch-update)))
2073
2074(defun isearch-complete-edit ()
2075 "Same as `isearch-complete' except in the minibuffer."
2076 (interactive)
2077 (setq isearch-string (field-string))
2078 (if (isearch-complete1)
2079 (progn
2080 (delete-field)
2081 (insert isearch-string))))
2082
2083\f
2084;; Message string
2085
2086(defun isearch-message (&optional c-q-hack ellipsis)
2087 ;; Generate and print the message string.
2088 (let ((cursor-in-echo-area ellipsis)
2089 (m isearch-message)
2090 (cmds isearch-cmds)
2091 succ-msg)
2092 (when (or (not isearch-success) isearch-error)
2093 ;; Highlight failed part
2094 (while (or (not (isearch-success-state (car cmds)))
2095 (isearch-error-state (car cmds)))
2096 (pop cmds))
2097 (setq succ-msg (and cmds (isearch-message-state (car cmds)))
2098 m (copy-sequence m))
2099 (add-text-properties
2100 (if (and (stringp succ-msg)
2101 (< (length succ-msg) (length m))
2102 (equal succ-msg (substring m 0 (length succ-msg))))
2103 (length succ-msg)
2104 0)
2105 (length m) '(face isearch-fail) m)
2106 ;; Highlight failed trailing whitespace
2107 (when (string-match " +$" m)
2108 (add-text-properties (match-beginning 0) (match-end 0)
2109 '(face trailing-whitespace) m)))
2110 (setq m (concat
2111 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
2112 m
2113 (isearch-message-suffix c-q-hack ellipsis)))
2114 (if c-q-hack m (let ((message-log-max nil)) (message "%s" m)))))
2115
2116(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
2117 ;; If about to search, and previous search regexp was invalid,
2118 ;; check that it still is. If it is valid now,
2119 ;; let the message we display while searching say that it is valid.
2120 (and isearch-error ellipsis
2121 (condition-case ()
2122 (progn (re-search-forward isearch-string (point) t)
2123 (setq isearch-error nil))
2124 (error nil)))
2125 ;; If currently failing, display no ellipsis.
2126 (or isearch-success (setq ellipsis nil))
2127 (let ((m (concat (if isearch-success "" "failing ")
2128 (if isearch-adjusted "pending " "")
2129 (if (and isearch-wrapped
2130 (not isearch-wrap-function)
2131 (if isearch-forward
2132 (> (point) isearch-opoint)
2133 (< (point) isearch-opoint)))
2134 "over")
2135 (if isearch-wrapped "wrapped ")
2136 (if isearch-word "word " "")
2137 (if isearch-regexp "regexp " "")
2138 (if multi-isearch-next-buffer-current-function "multi " "")
2139 (if nonincremental "search" "I-search")
2140 (if isearch-forward "" " backward")
2141 (if current-input-method
2142 (concat " [" current-input-method-title "]: ")
2143 ": ")
2144 )))
2145 (propertize (concat (upcase (substring m 0 1)) (substring m 1))
2146 'face 'minibuffer-prompt)))
2147
2148(defun isearch-message-suffix (&optional c-q-hack ellipsis)
2149 (concat (if c-q-hack "^Q" "")
2150 (if isearch-error
2151 (concat " [" isearch-error "]")
2152 "")))
2153
2154\f
2155;; Searching
2156
2157(defvar isearch-search-fun-function nil
2158 "Override `isearch-search-fun'.
2159This function should return the search function for isearch to use.
2160It will call this function with three arguments
2161as if it were `search-forward'.")
2162
2163(defun isearch-search-fun ()
2164 "Return the function to use for the search.
2165Can be changed via `isearch-search-fun-function' for special needs."
2166 (if isearch-search-fun-function
2167 (funcall isearch-search-fun-function)
2168 (cond
2169 (isearch-word
2170 (if isearch-forward 'word-search-forward-lax 'word-search-backward-lax))
2171 (isearch-regexp
2172 (if isearch-forward 're-search-forward 're-search-backward))
2173 (t
2174 (if isearch-forward 'search-forward 'search-backward)))))
2175
2176(defun isearch-search-string (string bound noerror)
2177 ;; Search for the first occurance of STRING or its translation. If
2178 ;; found, move point to the end of the occurance, update
2179 ;; isearch-match-beg and isearch-match-end, and return point.
2180 (let ((func (isearch-search-fun))
2181 (len (length string))
2182 pos1 pos2)
2183 (setq pos1 (save-excursion (funcall func string bound noerror)))
2184 (if (and (char-table-p translation-table-for-input)
2185 (multibyte-string-p string)
2186 ;; Minor optimization.
2187 (string-match-p "[^[:ascii:]]" string))
2188 (let ((translated
2189 (apply 'string
2190 (mapcar (lambda (c)
2191 (or (aref translation-table-for-input c) c))
2192 string)))
2193 match-data)
2194 (when translated
2195 (save-match-data
2196 (save-excursion
2197 (if (setq pos2 (funcall func translated bound noerror))
2198 (setq match-data (match-data t)))))
2199 (when (and pos2
2200 (or (not pos1)
2201 (if isearch-forward (< pos2 pos1) (> pos2 pos1))))
2202 (setq pos1 pos2)
2203 (set-match-data match-data)))))
2204 (when pos1
2205 ;; When using multiple buffers isearch, switch to the new buffer here,
2206 ;; because `save-excursion' above doesn't allow doing it inside funcall.
2207 (if (and multi-isearch-next-buffer-current-function
2208 (buffer-live-p multi-isearch-current-buffer))
2209 (switch-to-buffer multi-isearch-current-buffer))
2210 (goto-char pos1))
2211 pos1))
2212
2213(defun isearch-search ()
2214 ;; Do the search with the current search string.
2215 (if isearch-message-function
2216 (funcall isearch-message-function nil t)
2217 (isearch-message nil t))
2218 (if (and (eq isearch-case-fold-search t) search-upper-case)
2219 (setq isearch-case-fold-search
2220 (isearch-no-upper-case-p isearch-string isearch-regexp)))
2221 (condition-case lossage
2222 (let ((inhibit-point-motion-hooks
2223 (and (eq isearch-success-function 'isearch-success-function-default)
2224 search-invisible))
2225 (inhibit-quit nil)
2226 (case-fold-search isearch-case-fold-search)
2227 (search-spaces-regexp search-whitespace-regexp)
2228 (retry t))
2229 (setq isearch-error nil)
2230 (while retry
2231 (setq isearch-success
2232 (isearch-search-string isearch-string nil t))
2233 ;; Clear RETRY unless we matched some invisible text
2234 ;; and we aren't supposed to do that.
2235 (if (or (not isearch-success)
2236 (bobp) (eobp)
2237 (= (match-beginning 0) (match-end 0))
2238 (funcall isearch-success-function
2239 (match-beginning 0) (match-end 0)))
2240 (setq retry nil)))
2241 (setq isearch-just-started nil)
2242 (if isearch-success
2243 (setq isearch-other-end
2244 (if isearch-forward (match-beginning 0) (match-end 0)))))
2245
2246 (quit (isearch-unread ?\C-g)
2247 (setq isearch-success nil))
2248
2249 (invalid-regexp
2250 (setq isearch-error (car (cdr lossage)))
2251 (if (string-match
2252 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
2253 isearch-error)
2254 (setq isearch-error "incomplete input")))
2255
2256 (search-failed
2257 (setq isearch-success nil)
2258 (setq isearch-error (nth 2 lossage)))
2259
2260 (error
2261 ;; stack overflow in regexp search.
2262 (setq isearch-error (format "%s" lossage))))
2263
2264 (if isearch-success
2265 nil
2266 ;; Ding if failed this time after succeeding last time.
2267 (and (isearch-success-state (car isearch-cmds))
2268 (ding))
2269 (if (functionp (isearch-pop-fun-state (car isearch-cmds)))
2270 (funcall (isearch-pop-fun-state (car isearch-cmds)) (car isearch-cmds)))
2271 (goto-char (isearch-point-state (car isearch-cmds)))))
2272
2273
2274;; Called when opening an overlay, and we are still in isearch.
2275(defun isearch-open-overlay-temporary (ov)
2276 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
2277 ;; Some modes would want to open the overlays temporary during
2278 ;; isearch in their own way, they should set the
2279 ;; `isearch-open-invisible-temporary' to a function doing this.
2280 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
2281 ;; Store the values for the `invisible' and `intangible'
2282 ;; properties, and then set them to nil. This way the text hidden
2283 ;; by this overlay becomes visible.
2284
2285 ;; Do we really need to set the `intangible' property to t? Can we
2286 ;; have the point inside an overlay with an `intangible' property?
2287 ;; In 19.34 this does not exist so I cannot test it.
2288 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
2289 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
2290 (overlay-put ov 'invisible nil)
2291 (overlay-put ov 'intangible nil)))
2292
2293
2294;; This is called at the end of isearch. It will open the overlays
2295;; that contain the latest match. Obviously in case of a C-g the
2296;; point returns to the original location which surely is not contain
2297;; in any of these overlays, se we are safe in this case too.
2298(defun isearch-open-necessary-overlays (ov)
2299 (let ((inside-overlay (and (> (point) (overlay-start ov))
2300 (< (point) (overlay-end ov))))
2301 ;; If this exists it means that the overlay was opened using
2302 ;; this function, not by us tweaking the overlay properties.
2303 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2304 (when (or inside-overlay (not fct-temp))
2305 ;; restore the values for the `invisible' and `intangible'
2306 ;; properties
2307 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2308 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2309 (overlay-put ov 'isearch-invisible nil)
2310 (overlay-put ov 'isearch-intangible nil))
2311 (if inside-overlay
2312 (funcall (overlay-get ov 'isearch-open-invisible) ov)
2313 (if fct-temp
2314 (funcall fct-temp ov t)))))
2315
2316;; This is called when exiting isearch. It closes the temporary
2317;; opened overlays, except the ones that contain the latest match.
2318(defun isearch-clean-overlays ()
2319 (when isearch-opened-overlays
2320 (mapc 'isearch-open-necessary-overlays isearch-opened-overlays)
2321 (setq isearch-opened-overlays nil)))
2322
2323
2324(defun isearch-intersects-p (start0 end0 start1 end1)
2325 "Return t if regions START0..END0 and START1..END1 intersect."
2326 (or (and (>= start0 start1) (< start0 end1))
2327 (and (> end0 start1) (<= end0 end1))
2328 (and (>= start1 start0) (< start1 end0))
2329 (and (> end1 start0) (<= end1 end0))))
2330
2331
2332;; Verify if the current match is outside of each element of
2333;; `isearch-opened-overlays', if so close that overlay.
2334
2335(defun isearch-close-unnecessary-overlays (begin end)
2336 (let ((overlays isearch-opened-overlays))
2337 (setq isearch-opened-overlays nil)
2338 (dolist (ov overlays)
2339 (if (isearch-intersects-p begin end (overlay-start ov) (overlay-end ov))
2340 (push ov isearch-opened-overlays)
2341 (let ((fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
2342 (if fct-temp
2343 ;; If this exists it means that the overlay was opened
2344 ;; using this function, not by us tweaking the overlay
2345 ;; properties.
2346 (funcall fct-temp ov t)
2347 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
2348 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
2349 (overlay-put ov 'isearch-invisible nil)
2350 (overlay-put ov 'isearch-intangible nil)))))))
2351
2352
2353(defun isearch-range-invisible (beg end)
2354 "Return t if all the text from BEG to END is invisible."
2355 (when (/= beg end)
2356 ;; Check that invisibility runs up to END.
2357 (save-excursion
2358 (goto-char beg)
2359 (let (;; can-be-opened keeps track if we can open some overlays.
2360 (can-be-opened (eq search-invisible 'open))
2361 ;; the list of overlays that could be opened
2362 (crt-overlays nil))
2363 (when (and can-be-opened isearch-hide-immediately)
2364 (isearch-close-unnecessary-overlays beg end))
2365 ;; If the following character is currently invisible,
2366 ;; skip all characters with that same `invisible' property value.
2367 ;; Do that over and over.
2368 (while (and (< (point) end)
2369 (let ((prop
2370 (get-char-property (point) 'invisible)))
2371 (if (eq buffer-invisibility-spec t)
2372 prop
2373 (or (memq prop buffer-invisibility-spec)
2374 (assq prop buffer-invisibility-spec)))))
2375 (if (get-text-property (point) 'invisible)
2376 (progn
2377 (goto-char (next-single-property-change (point) 'invisible
2378 nil end))
2379 ;; if text is hidden by an `invisible' text property
2380 ;; we cannot open it at all.
2381 (setq can-be-opened nil))
2382 (when can-be-opened
2383 (let ((overlays (overlays-at (point)))
2384 ov-list
2385 o
2386 invis-prop)
2387 (while overlays
2388 (setq o (car overlays)
2389 invis-prop (overlay-get o 'invisible))
2390 (if (if (eq buffer-invisibility-spec t)
2391 invis-prop
2392 (or (memq invis-prop buffer-invisibility-spec)
2393 (assq invis-prop buffer-invisibility-spec)))
2394 (if (overlay-get o 'isearch-open-invisible)
2395 (setq ov-list (cons o ov-list))
2396 ;; We found one overlay that cannot be
2397 ;; opened, that means the whole chunk
2398 ;; cannot be opened.
2399 (setq can-be-opened nil)))
2400 (setq overlays (cdr overlays)))
2401 (if can-be-opened
2402 ;; It makes sense to append to the open
2403 ;; overlays list only if we know that this is
2404 ;; t.
2405 (setq crt-overlays (append ov-list crt-overlays)))))
2406 (goto-char (next-overlay-change (point)))))
2407 ;; See if invisibility reaches up thru END.
2408 (if (>= (point) end)
2409 (if (and can-be-opened (consp crt-overlays))
2410 (progn
2411 (setq isearch-opened-overlays
2412 (append isearch-opened-overlays crt-overlays))
2413 (mapc 'isearch-open-overlay-temporary crt-overlays)
2414 nil)
2415 (setq isearch-hidden t)))))))
2416
2417(defun isearch-success-function-default (beg end)
2418 "Default function to report if the new search match is successful.
2419Returns t if search can match hidden text, or otherwise checks if some
2420text from BEG to END is visible."
2421 (or (eq search-invisible t)
2422 (not (isearch-range-invisible beg end))))
2423
2424\f
2425;; General utilities
2426
2427(defun isearch-no-upper-case-p (string regexp-flag)
2428 "Return t if there are no upper case chars in STRING.
2429If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
2430since they have special meaning in a regexp."
2431 (let (quote-flag (i 0) (len (length string)) found)
2432 (while (and (not found) (< i len))
2433 (let ((char (aref string i)))
2434 (if (and regexp-flag (eq char ?\\))
2435 (setq quote-flag (not quote-flag))
2436 (if (and (not quote-flag) (not (eq char (downcase char))))
2437 (setq found t))
2438 (setq quote-flag nil)))
2439 (setq i (1+ i)))
2440 (not (or found
2441 ;; Even if there's no uppercase char, we want to detect the use
2442 ;; of [:upper:] or [:lower:] char-class, which indicates
2443 ;; clearly that the user cares about case distinction.
2444 (and regexp-flag (string-match "\\[:\\(upp\\|low\\)er:]" string)
2445 (condition-case err
2446 (progn
2447 (string-match (substring string 0 (match-beginning 0))
2448 "")
2449 nil)
2450 (invalid-regexp
2451 (equal "Unmatched [ or [^" (cadr err)))))))))
2452
2453;; Portability functions to support various Emacs versions.
2454
2455(defun isearch-text-char-description (c)
2456 (cond
2457 ((< c ?\s) (format "^%c" (+ c 64)))
2458 ((= c ?\^?) "^?")
2459 (t (char-to-string c))))
2460
2461;; General function to unread characters or events.
2462;; Also insert them in a keyboard macro being defined.
2463(defun isearch-unread (&rest char-or-events)
2464 (mapc 'store-kbd-macro-event char-or-events)
2465 (setq unread-command-events
2466 (append char-or-events unread-command-events)))
2467
2468\f
2469;; Highlighting
2470
2471(defvar isearch-overlay nil)
2472
2473(defun isearch-highlight (beg end)
2474 (if search-highlight
2475 (if isearch-overlay
2476 ;; Overlay already exists, just move it.
2477 (move-overlay isearch-overlay beg end (current-buffer))
2478 ;; Overlay doesn't exist, create it.
2479 (setq isearch-overlay (make-overlay beg end))
2480 ;; 1001 is higher than lazy's 1000 and ediff's 100+
2481 (overlay-put isearch-overlay 'priority 1001)
2482 (overlay-put isearch-overlay 'face isearch))))
2483
2484(defun isearch-dehighlight ()
2485 (when isearch-overlay
2486 (delete-overlay isearch-overlay)))
2487\f
2488;; isearch-lazy-highlight feature
2489;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
2490
2491;; When active, *every* match for the current search string is
2492;; highlighted: the current one using the normal isearch match color
2493;; and all the others using `isearch-lazy-highlight'. The extra
2494;; highlighting makes it easier to anticipate where the cursor will
2495;; land each time you press C-s or C-r to repeat a pending search.
2496;; Highlighting of these additional matches happens in a deferred
2497;; fashion using "idle timers," so the cycles needed do not rob
2498;; isearch of its usual snappy response.
2499
2500;; IMPLEMENTATION NOTE: This depends on some isearch internals.
2501;; Specifically:
2502;; - `isearch-update' is expected to be called (at least) every time
2503;; the search string or window-start changes;
2504;; - `isearch-string' is expected to contain the current search
2505;; string as entered by the user;
2506;; - the type of the current search is expected to be given by
2507;; `isearch-word' and `isearch-regexp';
2508;; - the direction of the current search is expected to be given by
2509;; `isearch-forward';
2510;; - the variable `isearch-error' is expected to be true
2511;; only if `isearch-string' is an invalid regexp.
2512
2513(defvar isearch-lazy-highlight-overlays nil)
2514(defvar isearch-lazy-highlight-wrapped nil)
2515(defvar isearch-lazy-highlight-start-limit nil)
2516(defvar isearch-lazy-highlight-end-limit nil)
2517(defvar isearch-lazy-highlight-start nil)
2518(defvar isearch-lazy-highlight-end nil)
2519(defvar isearch-lazy-highlight-timer nil)
2520(defvar isearch-lazy-highlight-last-string nil)
2521(defvar isearch-lazy-highlight-window nil)
2522(defvar isearch-lazy-highlight-window-start nil)
2523(defvar isearch-lazy-highlight-window-end nil)
2524(defvar isearch-lazy-highlight-case-fold-search nil)
2525(defvar isearch-lazy-highlight-regexp nil)
2526(defvar isearch-lazy-highlight-space-regexp nil)
2527
2528(defun lazy-highlight-cleanup (&optional force)
2529 "Stop lazy highlighting and remove extra highlighting from current buffer.
2530FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
2531is nil. This function is called when exiting an incremental search if
2532`lazy-highlight-cleanup' is non-nil."
2533 (interactive '(t))
2534 (if (or force lazy-highlight-cleanup)
2535 (while isearch-lazy-highlight-overlays
2536 (delete-overlay (car isearch-lazy-highlight-overlays))
2537 (setq isearch-lazy-highlight-overlays
2538 (cdr isearch-lazy-highlight-overlays))))
2539 (when isearch-lazy-highlight-timer
2540 (cancel-timer isearch-lazy-highlight-timer)
2541 (setq isearch-lazy-highlight-timer nil)))
2542
2543(define-obsolete-function-alias 'isearch-lazy-highlight-cleanup
2544 'lazy-highlight-cleanup
2545 "22.1")
2546
2547(defun isearch-lazy-highlight-new-loop (&optional beg end)
2548 "Cleanup any previous `lazy-highlight' loop and begin a new one.
2549BEG and END specify the bounds within which highlighting should occur.
2550This is called when `isearch-update' is invoked (which can cause the
2551search string to change or the window to scroll). It is also used
2552by other Emacs features."
2553 (when (and (null executing-kbd-macro)
2554 (sit-for 0) ;make sure (window-start) is credible
2555 (or (not (equal isearch-string
2556 isearch-lazy-highlight-last-string))
2557 (not (eq (selected-window)
2558 isearch-lazy-highlight-window))
2559 (not (eq isearch-lazy-highlight-case-fold-search
2560 isearch-case-fold-search))
2561 (not (eq isearch-lazy-highlight-regexp
2562 isearch-regexp))
2563 (not (= (window-start)
2564 isearch-lazy-highlight-window-start))
2565 (not (= (window-end) ; Window may have been split/joined.
2566 isearch-lazy-highlight-window-end))))
2567 ;; something important did indeed change
2568 (lazy-highlight-cleanup t) ;kill old loop & remove overlays
2569 (when (not isearch-error)
2570 (setq isearch-lazy-highlight-start-limit beg
2571 isearch-lazy-highlight-end-limit end)
2572 (setq isearch-lazy-highlight-window (selected-window)
2573 isearch-lazy-highlight-window-start (window-start)
2574 isearch-lazy-highlight-window-end (window-end)
2575 isearch-lazy-highlight-start (point)
2576 isearch-lazy-highlight-end (point)
2577 isearch-lazy-highlight-last-string isearch-string
2578 isearch-lazy-highlight-case-fold-search isearch-case-fold-search
2579 isearch-lazy-highlight-regexp isearch-regexp
2580 isearch-lazy-highlight-wrapped nil
2581 isearch-lazy-highlight-space-regexp search-whitespace-regexp)
2582 (unless (equal isearch-string "")
2583 (setq isearch-lazy-highlight-timer
2584 (run-with-idle-timer lazy-highlight-initial-delay nil
2585 'isearch-lazy-highlight-update))))))
2586
2587(defun isearch-lazy-highlight-search ()
2588 "Search ahead for the next or previous match, for lazy highlighting.
2589Attempt to do the search exactly the way the pending isearch would."
2590 (let ((case-fold-search isearch-lazy-highlight-case-fold-search)
2591 (isearch-regexp isearch-lazy-highlight-regexp)
2592 (search-spaces-regexp isearch-lazy-highlight-space-regexp))
2593 (condition-case nil
2594 (isearch-search-string
2595 isearch-lazy-highlight-last-string
2596 (if isearch-forward
2597 (min (or isearch-lazy-highlight-end-limit (point-max))
2598 (if isearch-lazy-highlight-wrapped
2599 isearch-lazy-highlight-start
2600 (window-end)))
2601 (max (or isearch-lazy-highlight-start-limit (point-min))
2602 (if isearch-lazy-highlight-wrapped
2603 isearch-lazy-highlight-end
2604 (window-start))))
2605 t)
2606 (error nil))))
2607
2608(defun isearch-lazy-highlight-update ()
2609 "Update highlighting of other matches for current search."
2610 (let ((max lazy-highlight-max-at-a-time)
2611 (looping t)
2612 nomore)
2613 (with-local-quit
2614 (save-selected-window
2615 (if (and (window-live-p isearch-lazy-highlight-window)
2616 (not (eq (selected-window) isearch-lazy-highlight-window)))
2617 (select-window isearch-lazy-highlight-window))
2618 (save-excursion
2619 (save-match-data
2620 (goto-char (if isearch-forward
2621 isearch-lazy-highlight-end
2622 isearch-lazy-highlight-start))
2623 (while looping
2624 (let ((found (isearch-lazy-highlight-search)))
2625 (when max
2626 (setq max (1- max))
2627 (if (<= max 0)
2628 (setq looping nil)))
2629 (if found
2630 (let ((mb (match-beginning 0))
2631 (me (match-end 0)))
2632 (if (= mb me) ;zero-length match
2633 (if isearch-forward
2634 (if (= mb (if isearch-lazy-highlight-wrapped
2635 isearch-lazy-highlight-start
2636 (window-end)))
2637 (setq found nil)
2638 (forward-char 1))
2639 (if (= mb (if isearch-lazy-highlight-wrapped
2640 isearch-lazy-highlight-end
2641 (window-start)))
2642 (setq found nil)
2643 (forward-char -1)))
2644
2645 ;; non-zero-length match
2646 (let ((ov (make-overlay mb me)))
2647 (push ov isearch-lazy-highlight-overlays)
2648 ;; 1000 is higher than ediff's 100+,
2649 ;; but lower than isearch main overlay's 1001
2650 (overlay-put ov 'priority 1000)
2651 (overlay-put ov 'face lazy-highlight-face)
2652 (overlay-put ov 'window (selected-window))))
2653 (if isearch-forward
2654 (setq isearch-lazy-highlight-end (point))
2655 (setq isearch-lazy-highlight-start (point)))))
2656
2657 ;; not found or zero-length match at the search bound
2658 (if (not found)
2659 (if isearch-lazy-highlight-wrapped
2660 (setq looping nil
2661 nomore t)
2662 (setq isearch-lazy-highlight-wrapped t)
2663 (if isearch-forward
2664 (progn
2665 (setq isearch-lazy-highlight-end (window-start))
2666 (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
2667 (window-start))))
2668 (setq isearch-lazy-highlight-start (window-end))
2669 (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
2670 (window-end))))))))
2671 (unless nomore
2672 (setq isearch-lazy-highlight-timer
2673 (run-at-time lazy-highlight-interval nil
2674 'isearch-lazy-highlight-update)))))))))
2675
2676(defun isearch-resume (string regexp word forward message case-fold)
2677 "Resume an incremental search.
2678STRING is the string or regexp searched for.
2679REGEXP non-nil means the resumed search was a regexp search.
2680WORD non-nil means resume a word search.
2681FORWARD non-nil means resume a forward search.
2682MESSAGE is the echo-area message recorded for the search resumed.
2683CASE-FOLD non-nil means the search was case-insensitive."
2684 (isearch-mode forward regexp nil nil word)
2685 (setq isearch-string string
2686 isearch-message message
2687 isearch-case-fold-search case-fold)
2688 (isearch-search)
2689 (isearch-update))
2690
2691;; arch-tag: 74850515-f7d8-43a6-8a2c-ca90a4c1e675
2692;;; isearch.el ends here