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