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