*** empty log message ***
[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)
44336afb 622 (isearch-lazy-highlight-new-loop))
8e1cae6d 623
0daa17f3 624(defun isearch-done (&optional nopush edit)
bfe2d334 625 (remove-hook 'mouse-leave-buffer-hook 'isearch-done)
8e1cae6d 626 ;; Called by all commands that terminate isearch-mode.
35a4d143 627 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
c7955222 628 (setq overriding-terminal-local-map nil)
08200510
RS
629 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
630 (isearch-dehighlight t)
44336afb 631 (isearch-lazy-highlight-cleanup)
08200510
RS
632 (let ((found-start (window-start (selected-window)))
633 (found-point (point)))
70418205
RS
634 (if isearch-window-configuration
635 (set-window-configuration isearch-window-configuration))
08200510 636
d49b6338
RS
637 (if isearch-small-window
638 (goto-char found-point)
639 ;; Exiting the save-window-excursion clobbers window-start; restore it.
9821535a 640 (set-window-start (selected-window) found-start t))
d49b6338 641
08200510
RS
642 ;; If there was movement, mark the starting position.
643 ;; Maybe should test difference between and set mark iff > threshold.
644 (if (/= (point) isearch-opoint)
9821535a
RS
645 (or (and transient-mark-mode mark-active)
646 (progn
647 (push-mark isearch-opoint t)
f3acf6f5 648 (or executing-kbd-macro (> (minibuffer-depth) 0)
9821535a 649 (message "Mark saved where search started"))))))
08200510
RS
650
651 (setq isearch-mode nil)
99848db0
KH
652 (if isearch-input-method-local-p
653 (setq input-method-function isearch-input-method-function)
654 (kill-local-variable 'input-method-function))
655
1cd3732c 656 (force-mode-line-update)
8e1cae6d 657
df01192b
RS
658 ;; If we ended in the middle of some intangible text,
659 ;; move to the further end of that intangible text.
660 (let ((after (if (eobp) nil
661 (get-text-property (point) 'intangible)))
662 (before (if (bobp) nil
663 (get-text-property (1- (point)) 'intangible))))
664 (when (and before after (eq before after))
665 (if isearch-forward
666 (goto-char (next-single-property-change (point) 'intangible))
667 (goto-char (previous-single-property-change (point) 'intangible)))))
668
35a4d143 669 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d 670 ;; Update the ring data.
73d2bf95 671 (isearch-update-ring isearch-string isearch-regexp))
8e1cae6d 672
8e1cae6d 673 (run-hooks 'isearch-mode-end-hook)
0daa17f3 674 (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
08200510 675
73d2bf95
RS
676(defun isearch-update-ring (string &optional regexp)
677 "Add STRING to the beginning of the search ring.
678REGEXP says which ring to use."
679 (if regexp
680 (if (or (null regexp-search-ring)
fd89878d 681 (not (string= string (car regexp-search-ring))))
73d2bf95
RS
682 (progn
683 (setq regexp-search-ring
fd89878d 684 (cons string regexp-search-ring))
73d2bf95
RS
685 (if (> (length regexp-search-ring) regexp-search-ring-max)
686 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
687 nil))))
688 (if (or (null search-ring)
fd89878d 689 (not (string= string (car search-ring))))
73d2bf95 690 (progn
fd89878d 691 (setq search-ring (cons string search-ring))
73d2bf95
RS
692 (if (> (length search-ring) search-ring-max)
693 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
694
08200510 695;;; Switching buffers should first terminate isearch-mode.
117132a6
DL
696;;; ;; For Emacs 19, the frame switch event is handled.
697;;; (defun isearch-switch-frame-handler ()
698;;; (interactive) ;; Is this necessary?
699;;; ;; First terminate isearch-mode.
700;;; (isearch-done)
701;;; (isearch-clean-overlays)
702;;; (handle-switch-frame (car (cdr last-command-char))))
08200510 703
8e1cae6d 704\f
8e1cae6d
JB
705;; Commands active while inside of the isearch minor mode.
706
707(defun isearch-exit ()
708 "Exit search normally.
709However, if this is the first command after starting incremental
710search and `search-nonincremental-instead' is non-nil, do a
08200510 711nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
712 (interactive)
713 (if (and search-nonincremental-instead
714 (= 0 (length isearch-string)))
08200510
RS
715 (let ((isearch-nonincremental t))
716 (isearch-edit-string)))
0352b205
RS
717 (isearch-done)
718 (isearch-clean-overlays))
8e1cae6d
JB
719
720
721(defun isearch-edit-string ()
08200510
RS
722 "Edit the search string in the minibuffer.
723The following additional command keys are active while editing.
724\\<minibuffer-local-isearch-map>
725\\[exit-minibuffer] to resume incremental searching with the edited string.
726\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
727\\[isearch-forward-exit-minibuffer] to resume isearching forward.
8eb40e74 728\\[isearch-reverse-exit-minibuffer] to resume isearching backward.
08200510 729\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 730\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510 731\\[isearch-complete-edit] to complete the search string using the search ring.
8eb40e74 732\\<isearch-mode-map>
08200510
RS
733If first char entered is \\[isearch-yank-word], then do word search instead."
734
735 ;; This code is very hairy for several reasons, explained in the code.
736 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
737 ;; If there were a way to catch any change of buffer from the minibuffer,
738 ;; this could be simplified greatly.
eb8c3be9 739 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 740 (interactive)
08200510 741 (condition-case err
e900ced4
RS
742 (progn
743 (let ((isearch-nonincremental isearch-nonincremental)
744
745 ;; Locally bind all isearch global variables to protect them
746 ;; from recursive isearching.
747 ;; isearch-string -message and -forward are not bound
748 ;; so they may be changed. Instead, save the values.
749 (isearch-new-string isearch-string)
750 (isearch-new-message isearch-message)
751 (isearch-new-forward isearch-forward)
752 (isearch-new-word isearch-word)
753
754 (isearch-regexp isearch-regexp)
755 (isearch-op-fun isearch-op-fun)
756 (isearch-cmds isearch-cmds)
757 (isearch-success isearch-success)
758 (isearch-wrapped isearch-wrapped)
759 (isearch-barrier isearch-barrier)
760 (isearch-adjusted isearch-adjusted)
761 (isearch-yank-flag isearch-yank-flag)
762 (isearch-invalid-regexp isearch-invalid-regexp)
763 (isearch-within-brackets isearch-within-brackets)
764 ;;; Don't bind this. We want isearch-search, below, to set it.
765 ;;; And the old value won't matter after that.
766 ;;; (isearch-other-end isearch-other-end)
767 ;;; Perhaps some of these other variables should be bound for a
768 ;;; shorter period, ending before the next isearch-search.
769 ;;; But there doesn't seem to be a real bug, so let's not risk it now.
770 (isearch-opoint isearch-opoint)
771 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
772 (isearch-small-window isearch-small-window)
773 (isearch-recursive-edit isearch-recursive-edit)
774 ;; Save current configuration so we can restore it here.
775 (isearch-window-configuration (current-window-configuration))
776 )
777
778 ;; Actually terminate isearching until editing is done.
779 ;; This is so that the user can do anything without failure,
780 ;; like switch buffers and start another isearch, and return.
781 (condition-case err
782 (isearch-done t t)
783 (exit nil)) ; was recursive editing
784
785 (isearch-message) ;; for read-char
786 (unwind-protect
787 (let* (;; Why does following read-char echo?
788 ;;(echo-keystrokes 0) ;; not needed with above message
789 (e (let ((cursor-in-echo-area t))
790 (read-event)))
791 ;; Binding minibuffer-history-symbol to nil is a work-around
792 ;; for some incompatibility with gmhist.
793 (minibuffer-history-symbol)
794 (message-log-max nil))
795 ;; If the first character the user types when we prompt them
796 ;; for a string is the yank-word character, then go into
797 ;; word-search mode. Otherwise unread that character and
798 ;; read a key the normal way.
799 ;; Word search does not apply (yet) to regexp searches,
800 ;; no check is made here.
801 (message (isearch-message-prefix nil nil t))
802 (if (eq 'isearch-yank-word
803 (lookup-key isearch-mode-map (vector e)))
804 (setq isearch-word t;; so message-prefix is right
805 isearch-new-word t)
806 (cancel-kbd-macro-events)
807 (isearch-unread e))
808 (setq cursor-in-echo-area nil)
809 (setq isearch-new-string
810 (let (junk-ring)
811 (read-from-minibuffer
812 (isearch-message-prefix nil nil isearch-nonincremental)
813 isearch-string
814 minibuffer-local-isearch-map nil
815 'junk-ring))
816 isearch-new-message
817 (mapconcat 'isearch-text-char-description
818 isearch-new-string "")))
819 ;; Always resume isearching by restarting it.
820 (isearch-mode isearch-forward
821 isearch-regexp
822 isearch-op-fun
823 nil
824 isearch-word)
825
826 ;; Copy new local values to isearch globals
827 (setq isearch-string isearch-new-string
828 isearch-message isearch-new-message
829 isearch-forward isearch-new-forward
830 isearch-word isearch-new-word))
831
832 ;; Empty isearch-string means use default.
833 (if (= 0 (length isearch-string))
834 (setq isearch-string (or (car (if isearch-regexp
835 regexp-search-ring
836 search-ring))
837 ""))
838 ;; This used to set the last search string,
839 ;; but I think it is not right to do that here.
840 ;; Only the string actually used should be saved.
841 ))
842
843 ;; Push the state as of before this C-s.
844 (isearch-push-state)
08200510
RS
845
846 ;; Reinvoke the pending search.
08200510
RS
847 (isearch-search)
848 (isearch-update)
849 (if isearch-nonincremental
850 (progn
851 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
852 (isearch-done))))
8e1cae6d 853
08200510
RS
854 (quit ; handle abort-recursive-edit
855 (isearch-abort) ;; outside of let to restore outside global values
856 )))
857
858(defun isearch-nonincremental-exit-minibuffer ()
859 (interactive)
860 (setq isearch-nonincremental t)
861 (exit-minibuffer))
862
863(defun isearch-forward-exit-minibuffer ()
864 (interactive)
865 (setq isearch-new-forward t)
866 (exit-minibuffer))
8e1cae6d 867
08200510 868(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 869 (interactive)
08200510
RS
870 (setq isearch-new-forward nil)
871 (exit-minibuffer))
872
19993c54
RS
873(defun isearch-cancel ()
874 "Terminate the search and go back to the starting point."
875 (interactive)
876 (goto-char isearch-opoint)
877 (isearch-done t)
0352b205 878 (isearch-clean-overlays)
19993c54 879 (signal 'quit nil)) ; and pass on quit signal
08200510
RS
880
881(defun isearch-abort ()
b7852303 882 "Abort incremental search mode if searching is successful, signaling quit.
08200510 883Otherwise, revert to previous successful search and continue searching.
b7852303 884Use `isearch-exit' to quit without signaling."
08200510 885 (interactive)
eb8c3be9 886;; (ding) signal instead below, if quitting
8e1cae6d
JB
887 (discard-input)
888 (if isearch-success
889 ;; If search is successful, move back to starting point
890 ;; and really do quit.
891 (progn (goto-char isearch-opoint)
509ed182 892 (setq isearch-success nil)
35a4d143 893 (isearch-done t) ; exit isearch
0352b205 894 (isearch-clean-overlays)
a5dcf63f 895 (signal 'quit nil)) ; and pass on quit signal
925a67ca
RS
896 ;; If search is failing, or has an incomplete regexp,
897 ;; rub out until it is once more successful.
898 (while (or (not isearch-success) isearch-invalid-regexp)
899 (isearch-pop-state))
8e1cae6d
JB
900 (isearch-update)))
901
8e1cae6d
JB
902(defun isearch-repeat (direction)
903 ;; Utility for isearch-repeat-forward and -backward.
904 (if (eq isearch-forward (eq direction 'forward))
905 ;; C-s in forward or C-r in reverse.
906 (if (equal isearch-string "")
907 ;; If search string is empty, use last one.
908 (setq isearch-string
8e1cae6d 909 (or (if isearch-regexp
a5dcf63f
RS
910 (car regexp-search-ring)
911 (car search-ring))
8e1cae6d
JB
912 "")
913 isearch-message
08200510 914 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
915 isearch-string ""))
916 ;; If already have what to search for, repeat it.
917 (or isearch-success
918 (progn
8e1cae6d
JB
919 (goto-char (if isearch-forward (point-min) (point-max)))
920 (setq isearch-wrapped t))))
921 ;; C-s in reverse or C-r in forward, change direction.
922 (setq isearch-forward (not isearch-forward)))
923
924 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
925
926 (if (equal isearch-string "")
927 (setq isearch-success t)
99ae9e9f
KH
928 (if (and isearch-success (equal (match-end 0) (match-beginning 0))
929 (not isearch-just-started))
8e1cae6d
JB
930 ;; If repeating a search that found
931 ;; an empty string, ensure we advance.
6fefdc4b
RS
932 (if (if isearch-forward (eobp) (bobp))
933 ;; If there's nowhere to advance to, fail (and wrap next time).
934 (progn
935 (setq isearch-success nil)
936 (ding))
937 (forward-char (if isearch-forward 1 -1))
938 (isearch-search))
939 (isearch-search)))
940
8e1cae6d
JB
941 (isearch-push-state)
942 (isearch-update))
943
944(defun isearch-repeat-forward ()
945 "Repeat incremental search forwards."
946 (interactive)
947 (isearch-repeat 'forward))
948
949(defun isearch-repeat-backward ()
950 "Repeat incremental search backwards."
951 (interactive)
952 (isearch-repeat 'backward))
953
954(defun isearch-toggle-regexp ()
955 "Toggle regexp searching on or off."
956 ;; The status stack is left unchanged.
957 (interactive)
958 (setq isearch-regexp (not isearch-regexp))
08200510 959 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
960 (isearch-update))
961
4453091d
RS
962(defun isearch-toggle-case-fold ()
963 "Toggle case folding in searching on or off."
964 (interactive)
965 (setq isearch-case-fold-search
966 (if isearch-case-fold-search nil 'yes))
a56687f1
KH
967 (let ((message-log-max nil))
968 (message "%s%s [case %ssensitive]"
969 (isearch-message-prefix nil nil isearch-nonincremental)
970 isearch-message
971 (if isearch-case-fold-search "in" "")))
4453091d
RS
972 (setq isearch-adjusted t)
973 (sit-for 1)
974 (isearch-update))
975
8e1cae6d
JB
976(defun isearch-delete-char ()
977 "Discard last input item and move point back.
978If no previous match was done, just beep."
979 (interactive)
980 (if (null (cdr isearch-cmds))
981 (ding)
982 (isearch-pop-state))
983 (isearch-update))
984
985
9cf081fa
KH
986(defun isearch-yank-string (string)
987 "Pull STRING into search string."
988 ;; Downcase the string if not supposed to case-fold yanked strings.
989 (if (and isearch-case-fold-search
990 (eq 'not-yanks search-upper-case))
991 (setq string (downcase string)))
992 (if isearch-regexp (setq string (regexp-quote string)))
993 (setq isearch-string (concat isearch-string string)
994 isearch-message
995 (concat isearch-message
996 (mapconcat 'isearch-text-char-description
997 string ""))
998 ;; Don't move cursor in reverse search.
999 isearch-yank-flag t)
8e1cae6d
JB
1000 (isearch-search-and-update))
1001
30d47262
RS
1002(defun isearch-yank-kill ()
1003 "Pull string from kill ring into search string."
1004 (interactive)
9cf081fa
KH
1005 (isearch-yank-string (current-kill 0)))
1006
1007(defun isearch-yank-x-selection ()
117132a6 1008 "Pull current X selection into search string."
9cf081fa
KH
1009 (interactive)
1010 (isearch-yank-string (x-get-selection)))
8e1cae6d 1011
117132a6
DL
1012(defun isearch-mouse-yank (click arg)
1013 "Yank with the mouse in Isearch mode.
1014For a click in the echo area, invoke `isearch-yank-x-selection'.
1015Otherwise invoke `mouse-yank-at-click'."
1016 (interactive "e\nP")
1017 (let ((w (posn-window (event-start click))))
1018 (if (and (window-minibuffer-p w)
1019 (not (minibuffer-window-active-p w))) ; in echo area
1020 (isearch-yank-x-selection)
1021 (mouse-yank-at-click click arg))))
1022
8e1cae6d
JB
1023(defun isearch-yank-word ()
1024 "Pull next word from buffer into search string."
1025 (interactive)
9cf081fa
KH
1026 (isearch-yank-string
1027 (save-excursion
1028 (and (not isearch-forward) isearch-other-end
1029 (goto-char isearch-other-end))
1030 (buffer-substring (point) (progn (forward-word 1) (point))))))
8e1cae6d
JB
1031
1032(defun isearch-yank-line ()
1033 "Pull rest of line from buffer into search string."
1034 (interactive)
9cf081fa
KH
1035 (isearch-yank-string
1036 (save-excursion
1037 (and (not isearch-forward) isearch-other-end
1038 (goto-char isearch-other-end))
1039 (buffer-substring (point) (line-end-position)))))
8e1cae6d
JB
1040
1041
1042(defun isearch-search-and-update ()
1043 ;; Do the search and update the display.
1044 (if (and (not isearch-success)
1045 ;; unsuccessful regexp search may become
1046 ;; successful by addition of characters which
1047 ;; make isearch-string valid
1048 (not isearch-regexp))
1049 nil
1050 ;; In reverse search, adding stuff at
1051 ;; the end may cause zero or many more chars to be
1052 ;; matched, in the string following point.
1053 ;; Allow all those possibilities without moving point as
1054 ;; long as the match does not extend past search origin.
1055 (if (and (not isearch-forward) (not isearch-adjusted)
1056 (condition-case ()
99ae9e9f 1057 (let ((case-fold-search isearch-case-fold-search))
ec0a2f45
KH
1058 (if (and (eq case-fold-search t) search-upper-case)
1059 (setq case-fold-search
1060 (isearch-no-upper-case-p isearch-string isearch-regexp)))
99ae9e9f
KH
1061 (looking-at (if isearch-regexp isearch-string
1062 (regexp-quote isearch-string))))
8e1cae6d 1063 (error nil))
bd6a8414
RS
1064 (or isearch-yank-flag
1065 (<= (match-end 0)
1066 (min isearch-opoint isearch-barrier))))
d64b1d96
RS
1067 (progn
1068 (setq isearch-success t
1069 isearch-invalid-regexp nil
1070 isearch-within-brackets nil
1071 isearch-other-end (match-end 0))
1072 (if (and (eq isearch-case-fold-search t) search-upper-case)
1073 (setq isearch-case-fold-search
1074 (isearch-no-upper-case-p isearch-string isearch-regexp))))
8e1cae6d
JB
1075 ;; Not regexp, not reverse, or no match at point.
1076 (if (and isearch-other-end (not isearch-adjusted))
1077 (goto-char (if isearch-forward isearch-other-end
1078 (min isearch-opoint
1079 isearch-barrier
1080 (1+ isearch-other-end)))))
1081 (isearch-search)
1082 ))
1083 (isearch-push-state)
1084 (if isearch-op-fun (funcall isearch-op-fun))
1085 (isearch-update))
1086
1087
1088;; *, ?, and | chars can make a regexp more liberal.
08200510 1089;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
1090;; So go back to place last successful search started
1091;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 1092;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
1093
1094(defun isearch-*-char ()
1095 "Handle * and ? specially in regexps."
1096 (interactive)
1097 (if isearch-regexp
bd6a8414
RS
1098 (let ((idx (length isearch-string)))
1099 (while (and (> idx 0)
1100 (eq (aref isearch-string (1- idx)) ?\\))
1101 (setq idx (1- idx)))
1102 (when (= (mod (- (length isearch-string) idx) 2) 0)
1103 (setq isearch-adjusted t)
1104 ;; Get the isearch-other-end from before the last search.
1105 ;; We want to start from there,
1106 ;; so that we don't retreat farther than that.
1107 ;; (car isearch-cmds) is after last search;
1108 ;; (car (cdr isearch-cmds)) is from before it.
1109 (let ((cs (nth 5 (car (cdr isearch-cmds)))))
1110 (setq cs (or cs isearch-barrier))
1111 (goto-char
1112 (if isearch-forward
1113 (max cs isearch-barrier)
1114 (min cs isearch-barrier)))))))
117132a6 1115 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1116
1117
8e1cae6d
JB
1118(defun isearch-|-char ()
1119 "If in regexp search, jump to the barrier."
1120 (interactive)
1121 (if isearch-regexp
1122 (progn
1123 (setq isearch-adjusted t)
1124 (goto-char isearch-barrier)))
117132a6 1125 (isearch-process-search-char last-command-char))
8e1cae6d
JB
1126
1127
b457cc3b 1128(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1129
1130(defun isearch-other-meta-char ()
8f90f594 1131 "Exit the search normally and reread this key sequence.
35a4d143
RS
1132But only if `search-exit-option' is non-nil, the default.
1133If it is the symbol `edit', the search string is edited in the minibuffer
1134and the meta character is unread so that it applies to editing the string."
8e1cae6d 1135 (interactive)
c6431f12
KH
1136 (let* ((key (this-command-keys))
1137 (main-event (aref key 0))
1138 (keylist (listify-key-sequence key)))
24b5caec 1139 (cond ((and (= (length key) 1)
d94eb6eb 1140 (let ((lookup (lookup-key function-key-map key)))
3f866b53
KH
1141 (not (or (null lookup) (integerp lookup)
1142 (keymapp lookup)))))
24b5caec
RS
1143 ;; Handle a function key that translates into something else.
1144 ;; If the key has a global definition too,
1145 ;; exit and unread the key itself, so its global definition runs.
1146 ;; Otherwise, unread the translation,
1147 ;; so that the translated key takes effect within isearch.
d94eb6eb 1148 (cancel-kbd-macro-events)
24b5caec
RS
1149 (if (lookup-key global-map key)
1150 (progn
1151 (isearch-done)
1152 (apply 'isearch-unread keylist))
1153 (apply 'isearch-unread
1154 (listify-key-sequence (lookup-key function-key-map key)))))
1155 (
c6431f12
KH
1156 ;; Handle an undefined shifted control character
1157 ;; by downshifting it if that makes it defined.
1158 ;; (As read-key-sequence would normally do,
1159 ;; if we didn't have a default definition.)
1160 (let ((mods (event-modifiers main-event)))
1161 (and (integerp main-event)
1162 (memq 'shift mods)
1163 (memq 'control mods)
1164 (lookup-key isearch-mode-map
1165 (let ((copy (copy-sequence key)))
1166 (aset copy 0
1167 (- main-event (- ?\C-\S-a ?\C-a)))
1168 copy)
1169 nil)))
1170 (setcar keylist (- main-event (- ?\C-\S-a ?\C-a)))
d94eb6eb 1171 (cancel-kbd-macro-events)
c6431f12
KH
1172 (apply 'isearch-unread keylist))
1173 ((eq search-exit-option 'edit)
1174 (apply 'isearch-unread keylist)
1175 (isearch-edit-string))
1176 (search-exit-option
1177 (let (window)
d94eb6eb 1178 (cancel-kbd-macro-events)
c6431f12
KH
1179 (apply 'isearch-unread keylist)
1180 ;; Properly handle scroll-bar and mode-line clicks
1181 ;; for which a dummy prefix event was generated as (aref key 0).
1182 (and (> (length key) 1)
1183 (symbolp (aref key 0))
1184 (listp (aref key 1))
1185 (not (numberp (posn-point (event-start (aref key 1)))))
1186 ;; Convert the event back into its raw form,
1187 ;; with the dummy prefix implicit in the mouse event,
1188 ;; so it will get split up once again.
1189 (progn (setq unread-command-events
1190 (cdr unread-command-events))
1191 (setq main-event (car unread-command-events))
1192 (setcar (cdr (event-start main-event))
1193 (car (nth 1 (event-start main-event))))))
1194 ;; If we got a mouse click, maybe it was read with the buffer
1195 ;; it was clicked on. If so, that buffer, not the current one,
1196 ;; is in isearch mode. So end the search in that buffer.
1197 (if (and (listp main-event)
1198 (setq window (posn-window (event-start main-event)))
4fd017e7
RS
1199 (windowp window)
1200 (or (> (minibuffer-depth) 0)
1201 (not (window-minibuffer-p window))))
c6431f12
KH
1202 (save-excursion
1203 (set-buffer (window-buffer window))
0352b205
RS
1204 (isearch-done)
1205 (isearch-clean-overlays))
1206 (isearch-done)
1207 (isearch-clean-overlays))))
c6431f12
KH
1208 (t;; otherwise nil
1209 (isearch-process-search-string key key)))))
8e1cae6d 1210
8e1cae6d
JB
1211(defun isearch-quote-char ()
1212 "Quote special characters for incremental search."
1213 (interactive)
8bc15fa8 1214 (let ((char (read-quoted-char (isearch-message t))))
3c75023f 1215 ;; Assume character codes 0200 - 0377 stand for characters in some
c20d35d5
KH
1216 ;; single-byte character set, and convert them to Emacs
1217 ;; characters.
8bc15fa8 1218 (and enable-multibyte-characters
3c75023f 1219 (>= char ?\200)
8bc15fa8 1220 (<= char ?\377)
c20d35d5 1221 (setq char (unibyte-char-to-multibyte char)))
8bc15fa8 1222 (isearch-process-search-char char)))
8e1cae6d 1223
8e1cae6d
JB
1224(defun isearch-return-char ()
1225 "Convert return into newline for incremental search.
1226Obsolete."
1227 (interactive)
1228 (isearch-process-search-char ?\n))
1229
8e1cae6d 1230(defun isearch-printing-char ()
b68c164d 1231 "Add this ordinary printing character to the search string and search."
8e1cae6d 1232 (interactive)
117132a6 1233 (let ((char last-command-char))
45b94eb2
KH
1234 (if (= char ?\S-\ )
1235 (setq char ?\ ))
af56433d
RS
1236 (if (and enable-multibyte-characters
1237 (>= char ?\200)
1238 (<= char ?\377))
3c75023f 1239 (isearch-process-search-char (unibyte-char-to-multibyte char))
af56433d
RS
1240 (if current-input-method
1241 (isearch-process-search-multibyte-characters char)
1242 (isearch-process-search-char char)))))
8e1cae6d
JB
1243
1244(defun isearch-whitespace-chars ()
08200510 1245 "Match all whitespace chars, if in regexp mode.
117132a6 1246If you want to search for just a space, type \\[quoted-insert] SPC."
8e1cae6d 1247 (interactive)
08200510 1248 (if isearch-regexp
9da6682f
RS
1249 (if (and search-whitespace-regexp (not isearch-within-brackets)
1250 (not isearch-invalid-regexp))
08200510
RS
1251 (isearch-process-search-string search-whitespace-regexp " ")
1252 (isearch-printing-char))
1253 (progn
eb8c3be9 1254 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1255 ;; (setq isearch-word t)
1256 ;; (setq isearch-adjusted t)
1257 ;; (goto-char isearch-barrier)
1258 (isearch-printing-char))))
8e1cae6d
JB
1259
1260(defun isearch-process-search-char (char)
1261 ;; Append the char to the search string, update the message and re-search.
08200510 1262 (isearch-process-search-string
117132a6 1263 (char-to-string char)
8f3c1d76 1264 (if (>= char ?\200)
01a6ef79
RS
1265 (char-to-string char)
1266 (isearch-text-char-description char))))
8e1cae6d
JB
1267
1268(defun isearch-process-search-string (string message)
1269 (setq isearch-string (concat isearch-string string)
1270 isearch-message (concat isearch-message message))
1271 (isearch-search-and-update))
1272
1273\f
8e1cae6d
JB
1274;; Search Ring
1275
08200510
RS
1276(defun isearch-ring-adjust1 (advance)
1277 ;; Helper for isearch-ring-adjust
1278 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1279 (length (length ring))
1280 (yank-pointer-name (if isearch-regexp
08200510 1281 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1282 'search-ring-yank-pointer))
1283 (yank-pointer (eval yank-pointer-name)))
1284 (if (zerop length)
1285 ()
1286 (set yank-pointer-name
1287 (setq yank-pointer
ffbc30b2
PE
1288 (mod (+ (or yank-pointer 0)
1289 (if advance -1 1))
1290 length)))
a5dcf63f 1291 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1292 isearch-message (mapconcat 'isearch-text-char-description
1293 isearch-string "")))))
1294
1295(defun isearch-ring-adjust (advance)
1296 ;; Helper for isearch-ring-advance and isearch-ring-retreat
08200510 1297 (isearch-ring-adjust1 advance)
08200510
RS
1298 (if search-ring-update
1299 (progn
1300 (isearch-search)
1301 (isearch-update))
1302 (isearch-edit-string)
2ff20b7e
RS
1303 )
1304 (isearch-push-state))
8e1cae6d
JB
1305
1306(defun isearch-ring-advance ()
1307 "Advance to the next search string in the ring."
08200510 1308 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1309 (interactive)
1310 (isearch-ring-adjust 'advance))
1311
1312(defun isearch-ring-retreat ()
1313 "Retreat to the previous search string in the ring."
1314 (interactive)
1315 (isearch-ring-adjust nil))
1316
a5dcf63f 1317(defun isearch-ring-advance-edit (n)
117132a6
DL
1318 "Insert the next element of the search history into the minibuffer.
1319With prefix arg N, insert the Nth element."
a5dcf63f
RS
1320 (interactive "p")
1321 (let* ((yank-pointer-name (if isearch-regexp
1322 'regexp-search-ring-yank-pointer
1323 'search-ring-yank-pointer))
1324 (yank-pointer (eval yank-pointer-name))
1325 (ring (if isearch-regexp regexp-search-ring search-ring))
1326 (length (length ring)))
1327 (if (zerop length)
1328 ()
1329 (set yank-pointer-name
1330 (setq yank-pointer
ffbc30b2
PE
1331 (mod (- (or yank-pointer 0) n)
1332 length)))
a5dcf63f 1333
b7b66466 1334 (delete-field)
35a4d143 1335 (insert (nth yank-pointer ring))
49c13105 1336 (goto-char (point-max)))))
a5dcf63f
RS
1337
1338(defun isearch-ring-retreat-edit (n)
117132a6
DL
1339 "Insert the previous element of the search history into the minibuffer.
1340With prefix arg N, insert the Nth element."
a5dcf63f
RS
1341 (interactive "p")
1342 (isearch-ring-advance-edit (- n)))
1343
1344;;(defun isearch-ring-adjust-edit (advance)
1345;; "Use the next or previous search string in the ring while in minibuffer."
1346;; (isearch-ring-adjust1 advance)
1347;; (erase-buffer)
1348;; (insert isearch-string))
1349
1350;;(defun isearch-ring-advance-edit ()
1351;; (interactive)
1352;; (isearch-ring-adjust-edit 'advance))
1353
1354;;(defun isearch-ring-retreat-edit ()
1355;; "Retreat to the previous search string in the ring while in the minibuffer."
1356;; (interactive)
1357;; (isearch-ring-adjust-edit nil))
08200510
RS
1358
1359
1360(defun isearch-complete1 ()
1361 ;; Helper for isearch-complete and isearch-complete-edit
1362 ;; Return t if completion OK, nil if no completion exists.
1363 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1364 (alist (mapcar (function (lambda (string) (list string))) ring))
1365 (completion-ignore-case case-fold-search)
1366 (completion (try-completion isearch-string alist)))
1367 (cond
1368 ((eq completion t)
1369 ;; isearch-string stays the same
1370 t)
1371 ((or completion ; not nil, must be a string
b7852303 1372 (= 0 (length isearch-string))) ; shouldn't have to say this
08200510 1373 (if (equal completion isearch-string) ;; no extension?
36bcac3f
RS
1374 (progn
1375 (if completion-auto-help
1376 (with-output-to-temp-buffer "*Isearch completions*"
1377 (display-completion-list
1378 (all-completions isearch-string alist))))
1379 t)
1380 (and completion
1381 (setq isearch-string completion))))
08200510
RS
1382 (t
1383 (message "No completion") ; waits a second if in minibuffer
1384 nil))))
1385
1386(defun isearch-complete ()
1387 "Complete the search string from the strings on the search ring.
1388The completed string is then editable in the minibuffer.
1389If there is no completion possible, say so and continue searching."
1390 (interactive)
1391 (if (isearch-complete1)
1392 (isearch-edit-string)
1393 ;; else
1394 (sit-for 1)
1395 (isearch-update)))
8e1cae6d 1396
08200510
RS
1397(defun isearch-complete-edit ()
1398 "Same as `isearch-complete' except in the minibuffer."
1399 (interactive)
1400 (setq isearch-string (buffer-string))
1401 (if (isearch-complete1)
8e1cae6d 1402 (progn
b7b66466 1403 (delete-field)
08200510 1404 (insert isearch-string))))
8e1cae6d
JB
1405
1406\f
8e1cae6d 1407;; The search status stack (and isearch window-local variables, not used).
08200510 1408;; Need a structure for this.
8e1cae6d
JB
1409
1410(defun isearch-top-state ()
8e1cae6d
JB
1411 (let ((cmd (car isearch-cmds)))
1412 (setq isearch-string (car cmd)
1413 isearch-message (car (cdr cmd))
1414 isearch-success (nth 3 cmd)
1415 isearch-forward (nth 4 cmd)
1416 isearch-other-end (nth 5 cmd)
08200510
RS
1417 isearch-word (nth 6 cmd)
1418 isearch-invalid-regexp (nth 7 cmd)
1419 isearch-wrapped (nth 8 cmd)
c5f6b356 1420 isearch-barrier (nth 9 cmd)
f551b97d
RS
1421 isearch-within-brackets (nth 10 cmd)
1422 isearch-case-fold-search (nth 11 cmd))
8e1cae6d
JB
1423 (goto-char (car (cdr (cdr cmd))))))
1424
1425(defun isearch-pop-state ()
8e1cae6d
JB
1426 (setq isearch-cmds (cdr isearch-cmds))
1427 (isearch-top-state)
1428 )
1429
1430(defun isearch-push-state ()
1431 (setq isearch-cmds
1432 (cons (list isearch-string isearch-message (point)
1433 isearch-success isearch-forward isearch-other-end
08200510 1434 isearch-word
c5f6b356 1435 isearch-invalid-regexp isearch-wrapped isearch-barrier
f551b97d 1436 isearch-within-brackets isearch-case-fold-search)
8e1cae6d
JB
1437 isearch-cmds)))
1438
8e1cae6d 1439\f
8e1cae6d
JB
1440;; Message string
1441
1442(defun isearch-message (&optional c-q-hack ellipsis)
1443 ;; Generate and print the message string.
1444 (let ((cursor-in-echo-area ellipsis)
1445 (m (concat
08200510 1446 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1447 isearch-message
1448 (isearch-message-suffix c-q-hack ellipsis)
1449 )))
a56687f1
KH
1450 (if c-q-hack
1451 m
1452 (let ((message-log-max nil))
1453 (message "%s" m)))))
8e1cae6d 1454
08200510 1455(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1456 ;; If about to search, and previous search regexp was invalid,
1457 ;; check that it still is. If it is valid now,
1458 ;; let the message we display while searching say that it is valid.
1459 (and isearch-invalid-regexp ellipsis
1460 (condition-case ()
1461 (progn (re-search-forward isearch-string (point) t)
8eb40e74
KH
1462 (setq isearch-invalid-regexp nil
1463 isearch-within-brackets nil))
8e1cae6d
JB
1464 (error nil)))
1465 ;; If currently failing, display no ellipsis.
1466 (or isearch-success (setq ellipsis nil))
1467 (let ((m (concat (if isearch-success "" "failing ")
7badea30
RS
1468 (if (and isearch-wrapped
1469 (if isearch-forward
1470 (> (point) isearch-opoint)
1471 (< (point) isearch-opoint)))
1472 "over")
8e1cae6d 1473 (if isearch-wrapped "wrapped ")
08200510 1474 (if isearch-word "word " "")
8e1cae6d 1475 (if isearch-regexp "regexp " "")
08200510 1476 (if nonincremental "search" "I-search")
f46d5091 1477 (if isearch-forward "" " backward")
5b56d4e4
KH
1478 (if current-input-method
1479 (concat " [" current-input-method-title "]: ")
f46d5091 1480 ": ")
8e1cae6d
JB
1481 )))
1482 (aset m 0 (upcase (aref m 0)))
1483 m))
1484
1485
1486(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1487 (concat (if c-q-hack "^Q" "")
1488 (if isearch-invalid-regexp
1489 (concat " [" isearch-invalid-regexp "]")
1490 "")))
1491
1492\f
8e1cae6d
JB
1493;;; Searching
1494
1495(defun isearch-search ()
1496 ;; Do the search with the current search string.
1497 (isearch-message nil t)
4453091d 1498 (if (and (eq isearch-case-fold-search t) search-upper-case)
b78559b0
RS
1499 (setq isearch-case-fold-search
1500 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d 1501 (condition-case lossage
79c7a4fa
RS
1502 (let ((inhibit-point-motion-hooks search-invisible)
1503 (inhibit-quit nil)
86bfaffe
RS
1504 (case-fold-search isearch-case-fold-search)
1505 (retry t))
8e1cae6d 1506 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1507 (setq isearch-within-brackets nil)
86bfaffe
RS
1508 (while retry
1509 (setq isearch-success
1510 (funcall
1511 (cond (isearch-word
1512 (if isearch-forward
1513 'word-search-forward 'word-search-backward))
1514 (isearch-regexp
1515 (if isearch-forward
1516 're-search-forward 're-search-backward))
1517 (t
1518 (if isearch-forward 'search-forward 'search-backward)))
1519 isearch-string nil t))
1520 ;; Clear RETRY unless we matched some invisible text
1521 ;; and we aren't supposed to do that.
0352b205 1522 (if (or (eq search-invisible t)
86bfaffe
RS
1523 (not isearch-success)
1524 (bobp) (eobp)
1525 (= (match-beginning 0) (match-end 0))
1526 (not (isearch-range-invisible
1527 (match-beginning 0) (match-end 0))))
1528 (setq retry nil)))
99ae9e9f 1529 (setq isearch-just-started nil)
8e1cae6d
JB
1530 (if isearch-success
1531 (setq isearch-other-end
1532 (if isearch-forward (match-beginning 0) (match-end 0)))))
1533
d32696d4 1534 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1535 (setq isearch-success nil))
1536
1537 (invalid-regexp
1538 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1539 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1540 isearch-invalid-regexp))
8e1cae6d
JB
1541 (if (string-match
1542 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1543 isearch-invalid-regexp)
f1c03125
RS
1544 (setq isearch-invalid-regexp "incomplete input")))
1545 (error
1546 ;; stack overflow in regexp search.
1547 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1548
1549 (if isearch-success
1550 nil
1551 ;; Ding if failed this time after succeeding last time.
1552 (and (nth 3 (car isearch-cmds))
1553 (ding))
1554 (goto-char (nth 2 (car isearch-cmds)))))
1555
0352b205
RS
1556
1557;;; Called when opening an overlay, and we are still in isearch.
1558(defun isearch-open-overlay-temporary (ov)
0352b205
RS
1559 (if (not (null (overlay-get ov 'isearch-open-invisible-temporary)))
1560 ;; Some modes would want to open the overlays temporary during
1561 ;; isearch in their own way, they should set the
1562 ;; `isearch-open-invisible-temporary' to a function doing this.
1563 (funcall (overlay-get ov 'isearch-open-invisible-temporary) ov nil)
1564 ;; Store the values for the `invisible' and `intangible'
1565 ;; properties, and then set them to nil. This way the text hidden
1566 ;; by this overlay becomes visible.
1567
1568 ;; Do we realy need to set the `intangible' property to t? Can we
1569 ;; have the point inside an overlay with an `intangible' property?
1570 ;; In 19.34 this does not exist so I cannot test it.
1571 (overlay-put ov 'isearch-invisible (overlay-get ov 'invisible))
1572 (overlay-put ov 'isearch-intangible (overlay-get ov 'intangible))
1573 (overlay-put ov 'invisible nil)
1574 (overlay-put ov 'intangible nil)))
1575
1576
117132a6
DL
1577;;; This is called at the end of isearch. It will open the overlays
1578;;; that contain the latest match. Obviously in case of a C-g the
0352b205
RS
1579;;; point returns to the original location which surely is not contain
1580;;; in any of these overlays, se we are safe in this case too.
1581(defun isearch-open-necessary-overlays (ov)
1582 (let ((inside-overlay (and (> (point) (overlay-start ov))
1583 (< (point) (overlay-end ov))))
1584 ;; If this exists it means that the overlay was opened using
1585 ;; this function, not by us tweaking the overlay properties.
1586 (fct-temp (overlay-get ov 'isearch-open-invisible-temporary)))
1587 (when (or inside-overlay (not fct-temp))
1588 ;; restore the values for the `invisible' and `intangible'
1589 ;; properties
1590 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1591 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1592 (overlay-put ov 'isearch-invisible nil)
1593 (overlay-put ov 'isearch-intangible nil))
1594 (if inside-overlay
1595 (funcall (overlay-get ov 'isearch-open-invisible) ov)
1596 (if fct-temp
1597 (funcall fct-temp ov t)))))
1598
1599;;; This is called when exiting isearch. It closes the temporary
1600;;; opened overlays, except the ones that contain the latest match.
1601(defun isearch-clean-overlays ()
1602 (when isearch-opened-overlays
1603 ;; Use a cycle instead of a mapcar here?
1604 (mapcar
1605 (function isearch-open-necessary-overlays) isearch-opened-overlays)
1606 (setq isearch-opened-overlays nil)))
1607
1608;;; Verify if the current match is outside of each element of
1609;;; `isearch-opened-overlays', if so close that overlay.
1610(defun isearch-close-unecessary-overlays (begin end)
1611 (let ((ov-list isearch-opened-overlays)
1612 ov
1613 inside-overlay
1614 fct-temp)
1615 (setq isearch-opened-overlays nil)
1616 (while ov-list
1617 (setq ov (car ov-list))
1618 (setq ov-list (cdr ov-list))
1619 (setq inside-overlay (or (and (> begin (overlay-start ov))
1620 (< begin (overlay-end ov)))
1621 (and (> end (overlay-start ov))
1622 (< end (overlay-end ov)))))
1623 ;; If this exists it means that the overlay was opened using
1624 ;; this function, not by us tweaking the overlay properties.
1625 (setq fct-temp (overlay-get ov 'isearch-open-invisible-temporary))
1626 (if inside-overlay
1627 (setq isearch-opened-overlays (cons ov isearch-opened-overlays))
1628 (if fct-temp
1629 (funcall fct-temp ov t)
1630 (overlay-put ov 'invisible (overlay-get ov 'isearch-invisible))
1631 (overlay-put ov 'intangible (overlay-get ov 'isearch-intangible))
1632 (overlay-put ov 'isearch-invisible nil)
1633 (overlay-put ov 'isearch-intangible nil))))))
1634
86bfaffe 1635(defun isearch-range-invisible (beg end)
79c7a4fa 1636 "Return t if all the text from BEG to END is invisible."
86bfaffe
RS
1637 (and (/= beg end)
1638 ;; Check that invisibility runs up to END.
1639 (save-excursion
1640 (goto-char beg)
df01192b
RS
1641 (let (
1642 ;; can-be-opened keeps track if we can open some overlays.
1643 (can-be-opened (eq search-invisible 'open))
1644 ;; the list of overlays that could be opened
1645 (crt-overlays nil))
0352b205 1646 (when (and can-be-opened isearch-hide-immediately)
df01192b 1647 (isearch-close-unecessary-overlays beg end))
0352b205
RS
1648 ;; If the following character is currently invisible,
1649 ;; skip all characters with that same `invisible' property value.
1650 ;; Do that over and over.
1651 (while (and (< (point) end)
1652 (let ((prop
1653 (get-char-property (point) 'invisible)))
1654 (if (eq buffer-invisibility-spec t)
1655 prop
1656 (or (memq prop buffer-invisibility-spec)
1657 (assq prop buffer-invisibility-spec)))))
1658 (if (get-text-property (point) 'invisible)
1659 (progn
1660 (goto-char (next-single-property-change (point) 'invisible
1661 nil end))
1662 ;; if text is hidden by an `invisible' text property
1663 ;; we cannot open it at all.
1664 (setq can-be-opened nil))
1665 (unless (null can-be-opened)
1666 (let ((overlays (overlays-at (point)))
1667 ov-list
1668 o
1669 invis-prop)
1670 (while overlays
1671 (setq o (car overlays)
1672 invis-prop (overlay-get o 'invisible))
de207f5a
KH
1673 (if (if (eq buffer-invisibility-spec t)
1674 invis-prop
1675 (or (memq invis-prop buffer-invisibility-spec)
1676 (assq invis-prop buffer-invisibility-spec)))
0352b205
RS
1677 (if (overlay-get o 'isearch-open-invisible)
1678 (setq ov-list (cons o ov-list))
1679 ;; We found one overlay that cannot be
1680 ;; opened, that means the whole chunk
1681 ;; cannot be opened.
1682 (setq can-be-opened nil)))
1683 (setq overlays (cdr overlays)))
1684 (if can-be-opened
1685 ;; It makes sense to append to the open
1686 ;; overlays list only if we know that this is
1687 ;; t.
7e97482b
RS
1688 (setq crt-overlays (append ov-list crt-overlays)))))
1689 (goto-char (next-overlay-change (point)))))
86bfaffe 1690 ;; See if invisibility reaches up thru END.
0352b205
RS
1691 (if (>= (point) end)
1692 (if (and (not (null can-be-opened)) (consp crt-overlays))
1693 (progn
1694 (setq isearch-opened-overlays
1695 (append isearch-opened-overlays crt-overlays))
1696 ;; maybe use a cycle instead of mapcar?
1697 (mapcar (function isearch-open-overlay-temporary)
1698 crt-overlays)
1699 nil)
1700 t))))))
08200510
RS
1701
1702\f
08200510
RS
1703;;; Highlighting
1704
b78559b0 1705(defvar isearch-overlay nil)
08200510 1706
b78559b0 1707(defun isearch-highlight (beg end)
3cf07305 1708 (if (or (null search-highlight) (null (display-color-p)))
08200510 1709 nil
b78559b0
RS
1710 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1711 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1712 (overlay-put isearch-overlay 'face
1713 (if (internal-find-face 'isearch nil)
1714 'isearch 'region))))
b78559b0
RS
1715
1716(defun isearch-dehighlight (totally)
1717 (if isearch-overlay
1718 (delete-overlay isearch-overlay)))
08200510 1719
08200510
RS
1720;;; General utilities
1721
08200510 1722
b78559b0
RS
1723(defun isearch-no-upper-case-p (string regexp-flag)
1724 "Return t if there are no upper case chars in STRING.
b7852303 1725If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
b78559b0 1726since they have special meaning in a regexp."
59057198
RS
1727 (let (quote-flag (i 0) (len (length string)) found)
1728 (while (and (not found) (< i len))
1729 (let ((char (aref string i)))
1730 (if (and regexp-flag (eq char ?\\))
1731 (setq quote-flag (not quote-flag))
1732 (if (and (not quote-flag) (not (eq char (downcase char))))
1733 (setq found t))))
1734 (setq i (1+ i)))
1735 (not found)))
08200510 1736
b78559b0 1737;; Portability functions to support various Emacs versions.
8e1cae6d 1738
08200510 1739(defun isearch-text-char-description (c)
9d78ff8b
RS
1740 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1741 (text-char-description c)
117132a6 1742 (char-to-string c)))
08200510 1743
bd1bd125 1744;; General function to unread characters or events.
99ae9e9f 1745;; Also insert them in a keyboard macro being defined.
8f90f594 1746(defun isearch-unread (&rest char-or-events)
99ae9e9f 1747 (mapcar 'store-kbd-macro-event char-or-events)
bd1bd125
RS
1748 (setq unread-command-events
1749 (append char-or-events unread-command-events)))
08200510 1750
44336afb
GM
1751\f
1752;;; isearch-lazy-highlight feature
1753;;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
1754
1755;;; When active, *every* match for the current search string is
1756;;; highlighted: the current one using the normal isearch match color
1757;;; and all the others using the unobtrusive `secondary-selection'
1758;;; color. The extra highlighting makes it easier to anticipate where
1759;;; the cursor will land each time you press C-s or C-r to repeat a
1760;;; pending search. Highlighting of these additional matches happens
1761;;; in a deferred fashion using "idle timers," so the cycles needed do
1762;;; not rob isearch of its usual snappy response.
1763
1764;;; IMPLEMENTATION NOTE: This depends on some isearch internals.
1765;;; Specifically:
1766;;; - `isearch-update' is expected to be called (at least) every time
1767;;; the search string changes;
1768;;; - `isearch-string' is expected to contain the current search
1769;;; string as entered by the user;
1770;;; - `isearch-overlay' is expected to contain the overlay used for
1771;;; primary isearch match-highlighting;
1772;;; - `isearch-opoint' is expected to contain the location where the
1773;;; current search began;
1774;;; - the type of the current search is expected to be given by
1775;;; `isearch-word' and `isearch-regexp';
1776;;; - the direction of the current search is expected to be given by
1777;;; `isearch-forward';
1778;;; - the variable `isearch-invalid-regexp' is expected to be true
1779;;; iff `isearch-string' is an invalid regexp.
1780
1781(require 'timer)
1782
1783(defgroup isearch-lazy-highlight nil
1784 "Lazy highlighting feature for incremental search."
1785 :prefix "isearch-lazy-highlight-"
1786 :group 'isearch)
1787
1788(defcustom isearch-lazy-highlight t
1789 "*Controls the lazy-highlighting during incremental searches.
1790When non-nil, all text in the buffer matching the current search
1791string is highlighted lazily (see `isearch-lazy-highlight-initial-delay'
1792and `isearch-lazy-highlight-interval')."
1793 :type 'boolean
1794 :group 'isearch-lazy-highlight)
1795
1796(defcustom isearch-lazy-highlight-cleanup t
1797 "*Controls whether to remove extra highlighting after a search.
1798If this is nil, extra highlighting can be \"manually\" removed with
1799\\[isearch-lazy-highlight-cleanup]."
1800 :type 'boolean
1801 :group 'isearch-lazy-highlight)
1802
1803(defcustom isearch-lazy-highlight-initial-delay 0.25
1804 "*Seconds to wait before beginning to lazily highlight all matches."
1805 :type 'number
1806 :group 'isearch-lazy-highlight)
1807
1808(defcustom isearch-lazy-highlight-interval 0.0625
1809 "*Seconds between lazily highlighting successive matches."
1810 :type 'number
1811 :group 'isearch-lazy-highlight)
1812
1813(defcustom isearch-lazy-highlight-face 'secondary-selection
1814 "*Face to use for lazily highlighting all matches."
1815 :type 'face
1816 :group 'isearch-lazy-highlight)
1817
1818(defvar isearch-lazy-highlight-overlays nil)
1819(defvar isearch-lazy-highlight-wrapped nil)
1820(defvar isearch-lazy-highlight-start nil)
1821(defvar isearch-lazy-highlight-end nil)
1822(defvar isearch-lazy-highlight-timer nil)
1823(defvar isearch-lazy-highlight-last-string nil)
1824
1825(defun isearch-lazy-highlight-cleanup (&optional force)
1826 "Stop lazy highlighting and remove extra highlighting from current buffer.
1827FORCE non-nil means do it whether or not `isearch-lazy-highlight-cleanup'
1828is nil. This function is called when exiting an incremental search if
1829`isearch-lazy-highlight-cleanup' is non-nil."
1830 (interactive '(t))
1831 (if (or force isearch-lazy-highlight-cleanup)
1832 (isearch-lazy-highlight-remove-overlays))
1833 (if isearch-lazy-highlight-timer
1834 (progn
1835 (cancel-timer isearch-lazy-highlight-timer)
1836 (setq isearch-lazy-highlight-timer nil))))
1837
1838(defun isearch-lazy-highlight-remove-overlays ()
1839 "Remove lazy highlight overlays from the current buffer."
1840 (while isearch-lazy-highlight-overlays
1841 (delete-overlay (car isearch-lazy-highlight-overlays))
1842 (setq isearch-lazy-highlight-overlays
1843 (cdr isearch-lazy-highlight-overlays))))
1844
1845(defun isearch-lazy-highlight-new-loop ()
1846 "Cleanup any previous isearch-lazy-highlight loop and begin a new one.
1847This happens when `isearch-update' is invoked (which can cause the
1848search string to change)."
1849 (if (and isearch-lazy-highlight
1850 (not (equal isearch-string isearch-lazy-highlight-last-string)))
1851 ;; the search string did indeed change
1852 (progn
1853 (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
1854 (if (and isearch-overlay
1855 (not (overlay-get isearch-overlay 'priority)))
1856 ;; make sure the isearch-overlay takes priority
1857 (overlay-put isearch-overlay 'priority 1))
1858 (setq isearch-lazy-highlight-start isearch-opoint
1859 isearch-lazy-highlight-end isearch-opoint
1860 isearch-lazy-highlight-last-string isearch-string
1861 isearch-lazy-highlight-wrapped nil)
1862 (setq isearch-lazy-highlight-timer
1863 (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
1864 'isearch-lazy-highlight-update)))))
1865
1866(defun isearch-lazy-highlight-search ()
1867 "Search ahead for the next or previous match, for lazy highlighting.
1868Attempt to do the search exactly the way the pending isearch would."
1869 (let ((case-fold-search isearch-case-fold-search))
1870 (funcall (cond (isearch-word (if isearch-forward
1871 'word-search-forward
1872 'word-search-backward))
1873 (isearch-regexp (if isearch-forward
1874 're-search-forward
1875 're-search-backward))
1876 (t (if isearch-forward
1877 'search-forward
1878 'search-backward)))
1879 isearch-string
1880 (if isearch-forward
1881 (if isearch-lazy-highlight-wrapped
1882 isearch-lazy-highlight-start
1883 nil)
1884 (if isearch-lazy-highlight-wrapped
1885 isearch-lazy-highlight-end
1886 nil))
1887 t)))
1888
1889(defun isearch-lazy-highlight-update ()
1890 "Find and highlight the next match in the lazy highlighting loop."
1891 (when (not isearch-invalid-regexp)
1892 (save-excursion
1893 (save-match-data
1894 (goto-char (if isearch-forward
1895 isearch-lazy-highlight-end
1896 isearch-lazy-highlight-start))
1897 (let ((found (isearch-lazy-highlight-search))) ;do search
1898 (if found
1899 ;; found the next match
1900 (let ((ov (make-overlay (match-beginning 0)
1901 (match-end 0))))
1902 (overlay-put ov 'face isearch-lazy-highlight-face)
1903 (overlay-put ov 'priority 0)
1904 (setq isearch-lazy-highlight-overlays
1905 (cons ov isearch-lazy-highlight-overlays))
1906 (setq isearch-lazy-highlight-timer
1907 (run-at-time isearch-lazy-highlight-interval nil
1908 'isearch-lazy-highlight-update))
1909 (if isearch-forward
1910 (setq isearch-lazy-highlight-end (point))
1911 (setq isearch-lazy-highlight-start (point))))
1912 ;; found no next match
1913 (when (not isearch-lazy-highlight-wrapped)
1914 ;; let's try wrapping around the end of the buffer
1915 (setq isearch-lazy-highlight-wrapped t)
1916 (setq isearch-lazy-highlight-timer
1917 (run-at-time isearch-lazy-highlight-interval nil
1918 'isearch-lazy-highlight-update))
1919 (if isearch-forward
1920 (setq isearch-lazy-highlight-end (point-min))
1921 (setq isearch-lazy-highlight-start (point-max))))))))))
1922
3b1e4dd1 1923;;; isearch.el ends here