(Voverriding_local_map): New variable.
[bpt/emacs.git] / lisp / isearch.el
CommitLineData
76550a57 1;;; isearch.el --- incremental search minor mode.
3b1e4dd1 2
b78559b0 3;; Copyright (C) 1992, 1993 Free Software Foundation, Inc.
3a801d0c 4
3b1e4dd1
ER
5;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6
c5f6b356 7;; |$Date: 1994/01/10 22:27:52 $|$Revision: 1.60 $
8e1cae6d 8
54d2ecd3 9;; This file is part of GNU Emacs.
8e1cae6d
JB
10
11;; GNU Emacs is distributed in the hope that it will be useful,
12;; but WITHOUT ANY WARRANTY. No author or distributor
13;; accepts responsibility to anyone for the consequences of using it
14;; or for whether it serves any particular purpose or works at all,
15;; unless he says so in writing. Refer to the GNU Emacs General Public
16;; License for full details.
17
18;; Everyone is granted permission to copy, modify and redistribute
19;; GNU Emacs, but only under the conditions described in the
20;; GNU Emacs General Public License. A copy of this license is
21;; supposed to have been given to you along with GNU Emacs so you
22;; can know your rights and responsibilities. It should be in a
23;; file named COPYING. Among other things, the copyright notice
24;; and this notice must be preserved on all copies.
25
3b1e4dd1
ER
26;;; Commentary:
27
8e1cae6d
JB
28;;;====================================================================
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
33;; is completed. It uses a recursive-edit to behave this way. Note:
34;; gnus does it wrong: (call-interactively 'isearch-forward).
35
8e1cae6d 36;; The key bindings active within isearch-mode are defined below in
08200510
RS
37;; `isearch-mode-map' which is given bindings close to the default
38;; characters of the original isearch.el. With `isearch-mode',
39;; however, you can bind multi-character keys and it should be easier
40;; to add new commands. One bug though: keys with meta-prefix cannot
41;; be longer than two chars. Also see minibuffer-local-isearch-map
42;; for bindings active during `isearch-edit-string'.
43
44;; Note to emacs version 19 users: isearch-mode should work even if
45;; you switch windows with the mouse, in which case isearch-mode is
46;; terminated automatically before the switch. This is true of lemacs
47;; too, with a few more cleanups I've neglected in this release.
48;; No one has supplied patches for epoch yet.
49
50;; The search ring and completion commands automatically put you in
51;; the minibuffer to edit the string. This gives you a chance to
52;; modify the search string before executing the search. There are
53;; three commands to terminate the editing: C-s and C-r exit the
54;; minibuffer and search forward and reverse respectively, while C-m
55;; exits and does a nonincremental search.
56
57;; Exiting immediately from isearch uses isearch-edit-string instead
58;; of nonincremental-search, if search-nonincremental-instead is non-nil.
59;; The name of this option should probably be changed if we decide to
60;; keep the behavior. No point in forcing nonincremental search until
61;; the last possible moment.
62
63;; TODO
eb8c3be9 64;; - Integrate the emacs 19 generalized command history.
08200510
RS
65;; - Think about incorporating query-replace.
66;; - Hooks and options for failed search.
282d89c0 67
3b1e4dd1
ER
68;;; Change Log:
69
8cb2ceaa
RS
70;;; Changes before those recorded in ChangeLog:
71
08200510
RS
72;;; Revision 1.4 92/09/14 16:26:02 liberte
73;;; Added prefix args to isearch-forward, etc. to switch between
74;;; string and regular expression searching.
75;;; Added some support for lemacs.
76;;; Added general isearch-highlight option - but only for lemacs so far.
77;;; Added support for frame switching in emacs 19.
78;;; Added word search option to isearch-edit-string.
79;;; Renamed isearch-quit to isearch-abort.
80;;; Numerous changes to comments and doc strings.
282d89c0 81;;;
08200510
RS
82;;; Revision 1.3 92/06/29 13:10:08 liberte
83;;; Moved modal isearch-mode handling into isearch-mode.
84;;; Got rid of buffer-local isearch variables.
85;;; isearch-edit-string used by ring adjustments, completion, and
86;;; nonincremental searching. C-s and C-r are additional exit commands.
87;;; Renamed all regex to regexp.
88;;; Got rid of found-start and found-point globals.
89;;; Generalized handling of upper-case chars.
90
91;;; Revision 1.2 92/05/27 11:33:57 liberte
92;;; Emacs version 19 has a search ring, which is supported here.
93;;; Other fixes found in the version 19 isearch are included here.
94;;;
95;;; Also see variables search-caps-disable-folding,
96;;; search-nonincremental-instead, search-whitespace-regexp, and
97;;; commands isearch-toggle-regexp, isearch-edit-string.
98;;;
99;;; semi-modal isearching is supported.
282d89c0
ER
100
101;;; Changes for 1.1
102;;; 3/18/92 Fixed invalid-regexp.
103;;; 3/18/92 Fixed yanking in regexps.
104
3b1e4dd1 105;;; Code:
08200510
RS
106
107\f
108;;;=========================================================================
109;;; Emacs features
110
111;; isearch-mode takes advantage of the features provided by several
112;; different versions of emacs. Rather than testing the version of
113;; emacs, several constants are defined, one for each of the features.
114;; Each of the tests below must work on any version of emacs.
115;; (Perhaps provide and featurep could be used for this purpose.)
116
1a3a6707 117(defconst isearch-gnu-emacs-events (fboundp 'set-frame-height)) ;; emacs 19
08200510 118(defconst isearch-pre-command-hook-exists (boundp 'pre-command-hook)) ;; lemacs
a5dcf63f 119(defconst isearch-event-data-type nil) ;; lemacs
08200510 120
fdf5afa4 121(defconst search-exit-option t
a8f783b2 122 "*Non-nil means random control characters terminate incremental search.")
fdf5afa4
RS
123
124(defvar search-slow-window-lines 1
125 "*Number of lines in slow search display windows.
126These are the short windows used during incremental search on slow terminals.
127Negative means put the slow search window at the top (normally it's at bottom)
128and the value is minus the number of lines.")
129
130(defvar search-slow-speed 1200
131 "*Highest terminal speed at which to use \"slow\" style incremental search.
132This is the style where a one-line window is created to show the line
133that the search has reached.")
8e1cae6d
JB
134
135;;;========================================================================
136;;; Some additional options and constants.
137
b78559b0 138(defvar search-upper-case 'not-yanks
8e1cae6d 139 "*If non-nil, upper case chars disable case fold searching.
08200510
RS
140That is, upper and lower case chars must match exactly.
141This applies no matter where the chars come from, but does not
fdf5afa4
RS
142apply to chars in regexps that are prefixed with `\\'.
143If this value is `not-yanks', yanked text is always downcased.")
8e1cae6d
JB
144
145(defvar search-nonincremental-instead t
146 "*If non-nil, do a nonincremental search instead if exiting immediately.
08200510
RS
147Actually, `isearch-edit-string' is called to let you enter the search
148string, and RET terminates editing and does a nonincremental search.")
8e1cae6d
JB
149
150(defconst search-whitespace-regexp "\\s-+"
151 "*If non-nil, regular expression to match a sequence of whitespace chars.
152You might want to use something like \"[ \\t\\r\\n]+\" instead.")
153
a8f783b2
RS
154;; I removed the * from the doc string because highlighting is not
155;; currently a clean thing to do. Once highlighting is made clean,
156;; this feature can be re-enabled and advertised.
08200510 157(defvar search-highlight nil
b78559b0 158 "*Non-nil means incremental search highlights the current match.")
08200510
RS
159
160(defvar isearch-mode-hook nil
161 "Function(s) to call after starting up an incremental search.")
162
163(defvar isearch-mode-end-hook nil
164 "Function(s) to call after terminating an incremental search.")
165
8e1cae6d
JB
166;;;==================================================================
167;;; Search ring.
8e1cae6d
JB
168
169(defvar search-ring nil
170 "List of search string sequences.")
08200510 171(defvar regexp-search-ring nil
8e1cae6d
JB
172 "List of regular expression search string sequences.")
173
174(defconst search-ring-max 16
175 "*Maximum length of search ring before oldest elements are thrown away.")
08200510
RS
176(defconst regexp-search-ring-max 16
177 "*Maximum length of regexp search ring before oldest elements are thrown away.")
8e1cae6d
JB
178
179(defvar search-ring-yank-pointer nil
a5dcf63f
RS
180 "Index in `search-ring' of last string reused.
181nil if none yet.")
08200510 182(defvar regexp-search-ring-yank-pointer nil
a5dcf63f
RS
183 "Index in `regexp-search-ring' of last string reused.
184nil if none yet.")
8e1cae6d 185
08200510
RS
186(defvar search-ring-update nil
187 "*Non-nil if advancing or retreating in the search ring should cause search.
188Default value, nil, means edit the string instead.")
189
8e1cae6d
JB
190;;;====================================================
191;;; Define isearch-mode keymap.
192
193(defvar isearch-mode-map nil
194 "Keymap for isearch-mode.")
195
08200510
RS
196(or isearch-mode-map
197 (let* ((i 0)
fdf5afa4 198 (map (make-keymap)))
fcf8ba15
RS
199 (or (vectorp (nth 1 map))
200 (error "The initialization of isearch-mode-map must be updated"))
201 ;; Give this map a vector 256 long, for dense binding
202 ;; of a larger range of ordinary characters.
203 (setcar (cdr map) (make-vector 256 nil))
08200510 204
ac689515
RS
205 ;; Make function keys, etc, exit the search.
206 (define-key map [t] 'isearch-other-control-char)
08200510 207 ;; Control chars, by default, end isearch mode transparently.
ac689515
RS
208 ;; We need these explicit definitions because, in a dense keymap,
209 ;; the binding for t does not affect characters.
210 ;; We use a dense keymap to save space.
08200510
RS
211 (while (< i ?\ )
212 (define-key map (make-string 1 i) 'isearch-other-control-char)
213 (setq i (1+ i)))
214
8cb2ceaa 215 ;; Printing chars extend the search string by default.
ac689515 216 (setq i ?\ )
8d7e4e80 217 (while (< i (length (nth 1 map)))
8cb2ceaa 218 (define-key map (vector i) 'isearch-printing-char)
08200510
RS
219 (setq i (1+ i)))
220
221 ;; Several non-printing chars change the searching behavior.
222 (define-key map "\C-s" 'isearch-repeat-forward)
223 (define-key map "\C-r" 'isearch-repeat-backward)
224 (define-key map "\177" 'isearch-delete-char)
225 (define-key map "\C-g" 'isearch-abort)
8e1cae6d 226
08200510
RS
227 (define-key map "\C-q" 'isearch-quote-char)
228
08200510
RS
229 (define-key map "\r" 'isearch-exit)
230 (define-key map "\C-j" 'isearch-printing-char)
231 (define-key map "\t" 'isearch-printing-char)
232 (define-key map " " 'isearch-whitespace-chars)
8e1cae6d 233
08200510
RS
234 (define-key map "\C-w" 'isearch-yank-word)
235 (define-key map "\C-y" 'isearch-yank-line)
30d47262
RS
236 (define-key map [mouse-2] 'isearch-yank-kill)
237 ;; This overrides the default binding for t.
238 (define-key map [down-mouse-2] 'nil)
08200510 239
e7cb0106
RS
240 ;; Bind the ASCII-equivalent "function keys" explicitly
241 ;; if we bind their equivalents,
242 ;; since otherwise the default binding would override.
243 ;; We bind [escape] below.
244 (define-key map [tab] 'isearch-printing-char)
f7ead361
RS
245 (define-key map [kp-0] 'isearch-printing-char)
246 (define-key map [kp-1] 'isearch-printing-char)
247 (define-key map [kp-2] 'isearch-printing-char)
248 (define-key map [kp-3] 'isearch-printing-char)
249 (define-key map [kp-4] 'isearch-printing-char)
250 (define-key map [kp-5] 'isearch-printing-char)
251 (define-key map [kp-6] 'isearch-printing-char)
252 (define-key map [kp-7] 'isearch-printing-char)
253 (define-key map [kp-8] 'isearch-printing-char)
254 (define-key map [kp-9] 'isearch-printing-char)
255 (define-key map [kp-add] 'isearch-printing-char)
256 (define-key map [kp-subtract] 'isearch-printing-char)
257 (define-key map [kp-multiply] 'isearch-printing-char)
258 (define-key map [kp-divide] 'isearch-printing-char)
259 (define-key map [kp-decimal] 'isearch-printing-char)
260 (define-key map [kp-separator] 'isearch-printing-char)
261 (define-key map [kp-equal] 'isearch-printing-char)
262 (define-key map [kp-tab] 'isearch-printing-char)
263 (define-key map [kp-space] 'isearch-printing-char)
264 (define-key map [kp-enter] 'isearch-exit)
e7cb0106
RS
265 (define-key map [delete] 'isearch-delete-char)
266 (define-key map [backspace] 'isearch-delete-char)
267 (define-key map [return] 'isearch-exit)
268 (define-key map [newline] 'isearch-printing-char)
269
08200510
RS
270 ;; Define keys for regexp chars * ? |.
271 ;; Nothing special for + because it matches at least once.
272 (define-key map "*" 'isearch-*-char)
273 (define-key map "?" 'isearch-*-char)
274 (define-key map "|" 'isearch-|-char)
275
58f1634a
RS
276;;; Turned off because I find I expect to get the global definition--rms.
277;;; ;; Instead bind C-h to special help command for isearch-mode.
278;;; (define-key map "\C-h" 'isearch-mode-help)
08200510
RS
279
280 ;; To handle local bindings with meta char prefix keys, define
281 ;; another full keymap. This must be done for any other prefix
282 ;; keys as well, one full keymap per char of the prefix key. It
283 ;; would be simpler to disable the global keymap, and/or have a
284 ;; default local key binding for any key not otherwise bound.
e7cb0106
RS
285 (let ((meta-map (make-sparse-keymap)))
286 (define-key map (char-to-string meta-prefix-char) meta-map)
287 (define-key map [escape] meta-map))
ac689515 288 (define-key map (vector meta-prefix-char t) 'isearch-other-meta-char)
08200510
RS
289
290 (define-key map "\M-n" 'isearch-ring-advance)
291 (define-key map "\M-p" 'isearch-ring-retreat)
30d47262 292 (define-key map "\M-y" 'isearch-yank-kill)
1a3a6707 293
08200510
RS
294 (define-key map "\M-\t" 'isearch-complete)
295
296 ;; For emacs 19, switching frames should terminate isearch-mode
1a3a6707 297 (if isearch-gnu-emacs-events
08200510
RS
298 (define-key map [switch-frame] 'isearch-switch-frame-handler))
299
300 (setq isearch-mode-map map)
301 ))
8e1cae6d 302
08200510
RS
303;; Some bindings you may want to put in your isearch-mode-hook.
304;; Suggest some alternates...
305;; (define-key isearch-mode-map "\C-t" 'isearch-toggle-regexp)
306;; (define-key isearch-mode-map "\C-^" 'isearch-edit-string)
8e1cae6d 307
8e1cae6d 308
08200510
RS
309(defvar minibuffer-local-isearch-map nil
310 "Keymap for editing isearch strings in the minibuffer.")
8e1cae6d 311
08200510
RS
312(or minibuffer-local-isearch-map
313 (let ((map (copy-keymap minibuffer-local-map)))
314 (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
35a4d143
RS
315 (define-key map "\M-n" 'isearch-ring-advance-edit)
316 (define-key map "\M-p" 'isearch-ring-retreat-edit)
08200510
RS
317 (define-key map "\M-\t" 'isearch-complete-edit)
318 (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
319 (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
320 (setq minibuffer-local-isearch-map map)
321 ))
8e1cae6d
JB
322
323;;;========================================================
324;; Internal variables declared globally for byte-compiler.
08200510
RS
325;; These are all set with setq while isearching
326;; and bound locally while editing the search string.
327
328(defvar isearch-forward nil) ; Searching in the forward direction.
329(defvar isearch-regexp nil) ; Searching for a regexp.
330(defvar isearch-word nil) ; Searching for words.
331
332(defvar isearch-cmds nil) ; Stack of search status sets.
333(defvar isearch-string "") ; The current search string.
334(defvar isearch-message "") ; text-char-description version of isearch-string
335
336(defvar isearch-success t) ; Searching is currently successful.
337(defvar isearch-invalid-regexp nil) ; Regexp not well formed.
c5f6b356 338(defvar isearch-within-brackets nil) ; Regexp has unclosed [.
08200510
RS
339(defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
340(defvar isearch-wrapped nil) ; Searching restarted from the top (bottom).
8e1cae6d
JB
341(defvar isearch-barrier 0)
342
08200510 343(defvar isearch-case-fold-search nil) ; case-fold-search while searching.
8e1cae6d
JB
344
345(defvar isearch-adjusted nil)
346(defvar isearch-slow-terminal-mode nil)
08200510
RS
347;;; If t, using a small window.
348(defvar isearch-small-window nil)
8e1cae6d 349(defvar isearch-opoint 0)
08200510
RS
350;;; The window configuration active at the beginning of the search.
351(defvar isearch-window-configuration nil)
352(defvar isearch-old-local-map nil)
8e1cae6d 353
08200510
RS
354;; Flag to indicate a yank occurred, so don't move the cursor.
355(defvar isearch-yank-flag nil)
8e1cae6d 356
08200510
RS
357;;; A function to be called after each input character is processed.
358;;; (It is not called after characters that exit the search.)
359;;; It is only set from an optional argument to `isearch-mode'.
360(defvar isearch-op-fun nil)
8e1cae6d 361
08200510
RS
362;;; Is isearch-mode in a recursive edit for modal searching.
363(defvar isearch-recursive-edit nil)
8e1cae6d 364
08200510
RS
365;;; Should isearch be terminated after doing one search?
366(defvar isearch-nonincremental nil)
367
368;; New value of isearch-forward after isearch-edit-string.
369(defvar isearch-new-forward nil)
8e1cae6d 370
8e1cae6d
JB
371
372;;;==============================================================
373;; Minor-mode-alist changes - kind of redundant with the
374;; echo area, but if isearching in multiple windows, it can be useful.
375
376(or (assq 'isearch-mode minor-mode-alist)
377 (nconc minor-mode-alist
378 (list '(isearch-mode isearch-mode))))
379
08200510 380(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
381(make-variable-buffer-local 'isearch-mode)
382
fdf5afa4
RS
383(define-key global-map "\C-s" 'isearch-forward)
384(define-key esc-map "\C-s" 'isearch-forward-regexp)
385(define-key global-map "\C-r" 'isearch-backward)
386(define-key esc-map "\C-r" 'isearch-backward-regexp)
387
8e1cae6d
JB
388;;;===============================================================
389;;; Entry points to isearch-mode.
08200510 390;;; These four functions should replace those in loaddefs.el
b457cc3b 391;;; An alternative is to defalias isearch-forward etc to isearch-mode,
08200510 392;;; and look at this-command to set the options accordingly.
8e1cae6d 393
eceee2c0 394(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
395 "\
396Do incremental search forward.
08200510
RS
397With a prefix argument, do an incremental regular expression search instead.
398\\<isearch-mode-map>
8e1cae6d 399As you type characters, they add to the search string and are found.
08200510 400The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 401
08200510 402Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 403Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
404Type LFD (C-j) to match end of line.
405Type \\[isearch-repeat-forward] to search again forward,\
406 \\[isearch-repeat-backward] to search again backward.
407Type \\[isearch-yank-word] to yank word from buffer onto end of search\
408 string and search for it.
409Type \\[isearch-yank-line] to yank rest of line onto end of search string\
410 and search for it.
8e1cae6d 411Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
412\\[isearch-abort] while searching or when search has failed cancels input\
413 back to what has
414 been found successfully.
415\\[isearch-abort] when search is successful aborts and moves point to\
416 starting point.
8e1cae6d
JB
417
418Also supported is a search ring of the previous 16 search strings.
419Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
420Type \\[isearch-ring-retreat] to search for the previous item in the search\
421 ring.
422Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 423
08200510
RS
424The above keys, bound in `isearch-mode-map', are often controlled by
425 options; do M-x apropos on search-.* to find them.
8e1cae6d 426Other control and meta characters terminate the search
08200510 427 and are then executed normally (depending on `search-exit-option').
8e1cae6d 428
08200510
RS
429If this function is called non-interactively, it does not return to
430the calling function until the search is done."
8e1cae6d 431
eceee2c0 432 (interactive "P\np")
4ae7d00a 433 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 434
eceee2c0 435(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
436 "\
437Do incremental search forward for regular expression.
08200510 438With a prefix argument, do a regular string search instead.
8e1cae6d
JB
439Like ordinary incremental search except that your input
440is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 441 (interactive "P\np")
4ae7d00a 442 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 443
eceee2c0 444(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
445 "\
446Do incremental search backward.
08200510 447With a prefix argument, do a regular expression search instead.
8e1cae6d 448See \\[isearch-forward] for more information."
eceee2c0 449 (interactive "P\np")
4ae7d00a 450 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 451
eceee2c0 452(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
453 "\
454Do incremental search backward for regular expression.
08200510 455With a prefix argument, do a regular string search instead.
8e1cae6d
JB
456Like ordinary incremental search except that your input
457is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 458 (interactive "P\np")
4ae7d00a 459 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
460
461
462(defun isearch-mode-help ()
463 (interactive)
464 (describe-function 'isearch-forward)
465 (isearch-update))
8e1cae6d
JB
466
467\f
468;;;==================================================================
469;; isearch-mode only sets up incremental search for the minor mode.
470;; All the work is done by the isearch-mode commands.
471
08200510
RS
472;; Not used yet:
473;;(defconst isearch-commands '(isearch-forward isearch-backward
474;; isearch-forward-regexp isearch-backward-regexp)
475;; "List of commands for which isearch-mode does not recursive-edit.")
476
477
478(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
8e1cae6d 479 "Start isearch minor mode. Called by isearch-forward, etc."
8e1cae6d
JB
480
481 ;; Initialize global vars.
482 (setq isearch-forward forward
483 isearch-regexp regexp
08200510 484 isearch-word word-p
8e1cae6d
JB
485 isearch-op-fun op-fun
486 isearch-case-fold-search case-fold-search
487 isearch-string ""
488 isearch-message ""
489 isearch-cmds nil
490 isearch-success t
491 isearch-wrapped nil
492 isearch-barrier (point)
493 isearch-adjusted nil
494 isearch-yank-flag nil
495 isearch-invalid-regexp nil
c5f6b356 496 isearch-within-brackets nil
dd492f04
JA
497 ;; Use (baud-rate) for now, for sake of other versions.
498 isearch-slow-terminal-mode (and (<= (baud-rate) search-slow-speed)
8e1cae6d
JB
499 (> (window-height)
500 (* 4 search-slow-window-lines)))
501 isearch-other-end nil
502 isearch-small-window nil
8e1cae6d 503
8e1cae6d 504 isearch-opoint (point)
a5dcf63f
RS
505 isearch-old-local-map (current-local-map)
506 search-ring-yank-pointer nil
507 regexp-search-ring-yank-pointer nil)
292a8dff
RS
508 (setq isearch-window-configuration
509 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 510
2ed45c8b
RS
511;; This was for Lucid Emacs. But now that we have pre-command-hook,
512;; it causes trouble.
513;; (if isearch-pre-command-hook-exists
514;; (add-hook 'pre-command-hook 'isearch-pre-command-hook))
8e1cae6d
JB
515 (setq isearch-mode " Isearch") ;; forward? regexp?
516 (set-buffer-modified-p (buffer-modified-p)) ; update modeline
517
7492a21e
RS
518 ;; It is ugly to show region highlighting while the search
519 ;; is going on. And we don't want the mark active at the end either.
520 (setq deactivate-mark t)
521
8e1cae6d
JB
522 (isearch-push-state)
523
524 (use-local-map isearch-mode-map)
525 (isearch-update)
526 (run-hooks 'isearch-mode-hook)
08200510
RS
527
528 ;; isearch-mode can be made modal (in the sense of not returning to
529 ;; the calling function until searching is completed) by entering
530 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
531 (if recursive-edit
532 (let ((isearch-recursive-edit t))
533 (recursive-edit)))
8e1cae6d
JB
534 )
535
536
537;;;====================================================
538;; Some high level utilities. Others below.
539
540(defun isearch-update ()
541 ;; Called after each command to update the display.
a5dcf63f 542 (if (if isearch-event-data-type
08200510 543 (null unread-command-event)
1a3a6707 544 (if isearch-gnu-emacs-events
dbc4e1c1 545 (null unread-command-events)
08200510 546 (< unread-command-char 0)))
8e1cae6d
JB
547 (progn
548 (if (not (input-pending-p))
549 (isearch-message))
550 (if (and isearch-slow-terminal-mode
551 (not (or isearch-small-window
552 (pos-visible-in-window-p))))
08200510 553 (let ((found-point (point)))
8e1cae6d 554 (setq isearch-small-window t)
8e1cae6d
JB
555 (move-to-window-line 0)
556 (let ((window-min-height 1))
557 (split-window nil (if (< search-slow-window-lines 0)
558 (1+ (- search-slow-window-lines))
559 (- (window-height)
560 (1+ search-slow-window-lines)))))
561 (if (< search-slow-window-lines 0)
562 (progn (vertical-motion (- 1 search-slow-window-lines))
563 (set-window-start (next-window) (point))
564 (set-window-hscroll (next-window)
565 (window-hscroll))
566 (set-window-hscroll (selected-window) 0))
567 (other-window 1))
08200510
RS
568 (goto-char found-point)))
569 (if isearch-other-end
570 (if (< isearch-other-end (point)) ; isearch-forward?
571 (isearch-highlight isearch-other-end (point))
99afb671
RS
572 (isearch-highlight (point) isearch-other-end))
573 (isearch-dehighlight nil))
08200510 574 ))
8e1cae6d
JB
575 (setq ;; quit-flag nil not for isearch-mode
576 isearch-adjusted nil
577 isearch-yank-flag nil)
578 )
579
580
35a4d143 581(defun isearch-done (&optional nopush)
8e1cae6d 582 ;; Called by all commands that terminate isearch-mode.
35a4d143 583 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
8e1cae6d 584 (use-local-map isearch-old-local-map)
08200510
RS
585 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
586 (isearch-dehighlight t)
587 (let ((found-start (window-start (selected-window)))
588 (found-point (point)))
70418205
RS
589 (if isearch-window-configuration
590 (set-window-configuration isearch-window-configuration))
08200510 591
d49b6338
RS
592 (if isearch-small-window
593 (goto-char found-point)
594 ;; Exiting the save-window-excursion clobbers window-start; restore it.
595 (set-window-start (selected-window) found-start t)))
596
08200510
RS
597 ;; If there was movement, mark the starting position.
598 ;; Maybe should test difference between and set mark iff > threshold.
599 (if (/= (point) isearch-opoint)
7872ee4d
RS
600 (progn
601 (push-mark isearch-opoint t)
67cbb73f 602 (deactivate-mark)
7872ee4d
RS
603 (or executing-macro (> (minibuffer-depth) 0)
604 (message "Mark saved where search started")))
08200510
RS
605 ;; (message "") why is this needed?
606 )
08200510
RS
607
608 (setq isearch-mode nil)
609 (set-buffer-modified-p (buffer-modified-p)) ;; update modeline
8e1cae6d 610
35a4d143 611 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d
JB
612 ;; Update the ring data.
613 (if isearch-regexp
a8f783b2
RS
614 (if (or (null regexp-search-ring)
615 (not (string= isearch-string (car regexp-search-ring))))
8e1cae6d 616 (progn
08200510 617 (setq regexp-search-ring
a5dcf63f 618 (cons isearch-string regexp-search-ring))
08200510
RS
619 (if (> (length regexp-search-ring) regexp-search-ring-max)
620 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
8e1cae6d 621 nil))))
a8f783b2
RS
622 (if (or (null search-ring)
623 (not (string= isearch-string (car search-ring))))
8e1cae6d 624 (progn
a5dcf63f 625 (setq search-ring (cons isearch-string search-ring))
8e1cae6d
JB
626 (if (> (length search-ring) search-ring-max)
627 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
628
8e1cae6d 629 (run-hooks 'isearch-mode-end-hook)
08200510
RS
630 (if isearch-recursive-edit (exit-recursive-edit)))
631
632;;;=======================================================
633;;; Switching buffers should first terminate isearch-mode.
eb8c3be9 634;;; This is done quite differently for each variant of emacs.
08200510
RS
635;;; For lemacs, see Exiting in lemacs below
636
637;; For Emacs 19, the frame switch event is handled.
638(defun isearch-switch-frame-handler ()
639 (interactive) ;; Is this necessary?
640 ;; First terminate isearch-mode.
641 (isearch-done)
dba1ec55 642 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510
RS
643
644;;;========================================================
8e1cae6d
JB
645
646\f
647;;;====================================================
648;; Commands active while inside of the isearch minor mode.
649
650(defun isearch-exit ()
651 "Exit search normally.
652However, if this is the first command after starting incremental
653search and `search-nonincremental-instead' is non-nil, do a
08200510 654nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
655 (interactive)
656 (if (and search-nonincremental-instead
657 (= 0 (length isearch-string)))
08200510
RS
658 (let ((isearch-nonincremental t))
659 (isearch-edit-string)))
8e1cae6d
JB
660 (isearch-done))
661
662
663(defun isearch-edit-string ()
08200510
RS
664 "Edit the search string in the minibuffer.
665The following additional command keys are active while editing.
666\\<minibuffer-local-isearch-map>
667\\[exit-minibuffer] to resume incremental searching with the edited string.
668\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
669\\[isearch-forward-exit-minibuffer] to resume isearching forward.
670\\[isearch-backward-exit-minibuffer] to resume isearching backward.
671\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 672\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510
RS
673\\[isearch-complete-edit] to complete the search string using the search ring.
674
675If first char entered is \\[isearch-yank-word], then do word search instead."
676
677 ;; This code is very hairy for several reasons, explained in the code.
678 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
679 ;; If there were a way to catch any change of buffer from the minibuffer,
680 ;; this could be simplified greatly.
eb8c3be9 681 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 682 (interactive)
08200510 683 (condition-case err
a9a5a9fa 684 (let ((isearch-nonincremental isearch-nonincremental)
08200510
RS
685
686 ;; Locally bind all isearch global variables to protect them
687 ;; from recursive isearching.
688 ;; isearch-string -message and -forward are not bound
689 ;; so they may be changed. Instead, save the values.
690 (isearch-new-string isearch-string)
691 (isearch-new-message isearch-message)
692 (isearch-new-forward isearch-forward)
693 (isearch-new-word isearch-word)
694
695 (isearch-regexp isearch-regexp)
696 (isearch-op-fun isearch-op-fun)
697 (isearch-cmds isearch-cmds)
698 (isearch-success isearch-success)
699 (isearch-wrapped isearch-wrapped)
700 (isearch-barrier isearch-barrier)
701 (isearch-adjusted isearch-adjusted)
702 (isearch-yank-flag isearch-yank-flag)
703 (isearch-invalid-regexp isearch-invalid-regexp)
c5f6b356 704 (isearch-within-brackets isearch-within-brackets)
08200510
RS
705 (isearch-other-end isearch-other-end)
706 (isearch-opoint isearch-opoint)
707 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
708 (isearch-small-window isearch-small-window)
709 (isearch-recursive-edit isearch-recursive-edit)
710 ;; Save current configuration so we can restore it here.
711 (isearch-window-configuration (current-window-configuration))
712 )
713
714 ;; Actually terminate isearching until editing is done.
715 ;; This is so that the user can do anything without failure,
716 ;; like switch buffers and start another isearch, and return.
717 (condition-case err
35a4d143 718 (isearch-done t)
08200510
RS
719 (exit nil)) ; was recursive editing
720
721 (isearch-message) ;; for read-char
722 (unwind-protect
723 (let* (;; Why does following read-char echo?
724 ;;(echo-keystrokes 0) ;; not needed with above message
d94307ee 725 (e (let ((cursor-in-echo-area t))
235beb32 726 (read-event)))
08200510
RS
727 ;; Binding minibuffer-history-symbol to nil is a work-around
728 ;; for some incompatibility with gmhist.
729 (minibuffer-history-symbol))
730 ;; If the first character the user types when we prompt them
731 ;; for a string is the yank-word character, then go into
732 ;; word-search mode. Otherwise unread that character and
733 ;; read a key the normal way.
734 ;; Word search does not apply (yet) to regexp searches,
735 ;; no check is made here.
736 (message (isearch-message-prefix nil nil t))
737 (if (eq 'isearch-yank-word
235beb32 738 (lookup-key isearch-mode-map (vector e)))
08200510
RS
739 (setq isearch-word t ;; so message-prefix is right
740 isearch-new-word t)
741 (isearch-unread e))
a5dcf63f
RS
742 (setq cursor-in-echo-area nil)
743 (setq isearch-new-string
35a4d143 744 (let (junk-ring)
a5dcf63f
RS
745 (read-from-minibuffer (isearch-message-prefix)
746 isearch-string
747 minibuffer-local-isearch-map nil
35a4d143 748 'junk-ring))
08200510
RS
749 isearch-new-message (mapconcat 'text-char-description
750 isearch-new-string "")))
751 ;; Always resume isearching by restarting it.
752 (isearch-mode isearch-forward
753 isearch-regexp
754 isearch-op-fun
755 isearch-recursive-edit
756 isearch-word)
757
758 ;; Copy new local values to isearch globals
759 (setq isearch-string isearch-new-string
760 isearch-message isearch-new-message
761 isearch-forward isearch-new-forward
762 isearch-word isearch-new-word))
763
764 ;; Empty isearch-string means use default.
765 (if (= 0 (length isearch-string))
a8f783b2
RS
766 (setq isearch-string (car (if isearch-regexp regexp-search-ring
767 search-ring)))
768 ;; This used to set the last search string,
769 ;; but I think it is not right to do that here.
770 ;; Only the string actually used should be saved.
771 )
08200510
RS
772
773 ;; Reinvoke the pending search.
774 (isearch-push-state)
775 (isearch-search)
776 (isearch-update)
777 (if isearch-nonincremental
778 (progn
779 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
780 (isearch-done))))
8e1cae6d 781
08200510
RS
782 (quit ; handle abort-recursive-edit
783 (isearch-abort) ;; outside of let to restore outside global values
784 )))
785
786(defun isearch-nonincremental-exit-minibuffer ()
787 (interactive)
788 (setq isearch-nonincremental t)
789 (exit-minibuffer))
790
791(defun isearch-forward-exit-minibuffer ()
792 (interactive)
793 (setq isearch-new-forward t)
794 (exit-minibuffer))
8e1cae6d 795
08200510 796(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 797 (interactive)
08200510
RS
798 (setq isearch-new-forward nil)
799 (exit-minibuffer))
800
801
802(defun isearch-abort ()
803 "Abort incremental search mode if searching is successful, signalling quit.
804Otherwise, revert to previous successful search and continue searching.
805Use `isearch-exit' to quit without signalling."
806 (interactive)
eb8c3be9 807;; (ding) signal instead below, if quitting
8e1cae6d
JB
808 (discard-input)
809 (if isearch-success
810 ;; If search is successful, move back to starting point
811 ;; and really do quit.
812 (progn (goto-char isearch-opoint)
35a4d143 813 (isearch-done t) ; exit isearch
a5dcf63f 814 (signal 'quit nil)) ; and pass on quit signal
08200510 815 ;; If search is failing, rub out until it is once more successful.
8e1cae6d
JB
816 (while (not isearch-success) (isearch-pop-state))
817 (isearch-update)))
818
819
820(defun isearch-repeat (direction)
821 ;; Utility for isearch-repeat-forward and -backward.
822 (if (eq isearch-forward (eq direction 'forward))
823 ;; C-s in forward or C-r in reverse.
824 (if (equal isearch-string "")
825 ;; If search string is empty, use last one.
826 (setq isearch-string
8e1cae6d 827 (or (if isearch-regexp
a5dcf63f
RS
828 (car regexp-search-ring)
829 (car search-ring))
8e1cae6d
JB
830 "")
831 isearch-message
08200510 832 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
833 isearch-string ""))
834 ;; If already have what to search for, repeat it.
835 (or isearch-success
836 (progn
837
838 (goto-char (if isearch-forward (point-min) (point-max)))
839 (setq isearch-wrapped t))))
840 ;; C-s in reverse or C-r in forward, change direction.
841 (setq isearch-forward (not isearch-forward)))
842
843 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
844
845 (if (equal isearch-string "")
846 (setq isearch-success t)
847 (if (and isearch-success (equal (match-end 0) (match-beginning 0)))
8e1cae6d
JB
848 ;; If repeating a search that found
849 ;; an empty string, ensure we advance.
6fefdc4b
RS
850 (if (if isearch-forward (eobp) (bobp))
851 ;; If there's nowhere to advance to, fail (and wrap next time).
852 (progn
853 (setq isearch-success nil)
854 (ding))
855 (forward-char (if isearch-forward 1 -1))
856 (isearch-search))
857 (isearch-search)))
858
8e1cae6d
JB
859 (isearch-push-state)
860 (isearch-update))
861
862(defun isearch-repeat-forward ()
863 "Repeat incremental search forwards."
864 (interactive)
865 (isearch-repeat 'forward))
866
867(defun isearch-repeat-backward ()
868 "Repeat incremental search backwards."
869 (interactive)
870 (isearch-repeat 'backward))
871
872(defun isearch-toggle-regexp ()
873 "Toggle regexp searching on or off."
874 ;; The status stack is left unchanged.
875 (interactive)
876 (setq isearch-regexp (not isearch-regexp))
08200510 877 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
878 (isearch-update))
879
880(defun isearch-delete-char ()
881 "Discard last input item and move point back.
882If no previous match was done, just beep."
883 (interactive)
884 (if (null (cdr isearch-cmds))
885 (ding)
886 (isearch-pop-state))
887 (isearch-update))
888
889
890(defun isearch-yank (chunk)
891 ;; Helper for isearch-yank-word and isearch-yank-line
30d47262
RS
892 ;; CHUNK should be word, line or kill.
893 (let ((string (cond
894 ((eq chunk 'kill)
895 (current-kill 0))
896 (t
897 (save-excursion
898 (and (not isearch-forward) isearch-other-end
899 (goto-char isearch-other-end))
900 (buffer-substring
901 (point)
902 (save-excursion
903 (cond
904 ((eq chunk 'word)
905 (forward-word 1))
906 ((eq chunk 'line)
907 (end-of-line)))
908 (point))))))))
08200510
RS
909 ;; Downcase the string if not supposed to case-fold yanked strings.
910 (if (and isearch-case-fold-search
fdf5afa4 911 (eq 'not-yanks search-upper-case))
08200510
RS
912 (setq string (downcase string)))
913 (if isearch-regexp (setq string (regexp-quote string)))
914 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
915 isearch-message
916 (concat isearch-message
08200510
RS
917 (mapconcat 'isearch-text-char-description
918 string ""))
8e1cae6d
JB
919 ;; Don't move cursor in reverse search.
920 isearch-yank-flag t))
921 (isearch-search-and-update))
922
30d47262
RS
923(defun isearch-yank-kill ()
924 "Pull string from kill ring into search string."
925 (interactive)
926 (isearch-yank 'kill))
8e1cae6d
JB
927
928(defun isearch-yank-word ()
929 "Pull next word from buffer into search string."
930 (interactive)
931 (isearch-yank 'word))
932
933(defun isearch-yank-line ()
934 "Pull rest of line from buffer into search string."
935 (interactive)
936 (isearch-yank 'line))
937
938
939(defun isearch-search-and-update ()
940 ;; Do the search and update the display.
941 (if (and (not isearch-success)
942 ;; unsuccessful regexp search may become
943 ;; successful by addition of characters which
944 ;; make isearch-string valid
945 (not isearch-regexp))
946 nil
947 ;; In reverse search, adding stuff at
948 ;; the end may cause zero or many more chars to be
949 ;; matched, in the string following point.
950 ;; Allow all those possibilities without moving point as
951 ;; long as the match does not extend past search origin.
952 (if (and (not isearch-forward) (not isearch-adjusted)
953 (condition-case ()
954 (looking-at (if isearch-regexp isearch-string
955 (regexp-quote isearch-string)))
956 (error nil))
957 (or isearch-yank-flag
958 (<= (match-end 0)
959 (min isearch-opoint isearch-barrier))))
960 (setq isearch-success t
961 isearch-invalid-regexp nil
c5f6b356 962 isearch-within-brackets nil
8e1cae6d
JB
963 isearch-other-end (match-end 0))
964 ;; Not regexp, not reverse, or no match at point.
965 (if (and isearch-other-end (not isearch-adjusted))
966 (goto-char (if isearch-forward isearch-other-end
967 (min isearch-opoint
968 isearch-barrier
969 (1+ isearch-other-end)))))
970 (isearch-search)
971 ))
972 (isearch-push-state)
973 (if isearch-op-fun (funcall isearch-op-fun))
974 (isearch-update))
975
976
977;; *, ?, and | chars can make a regexp more liberal.
08200510 978;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
979;; So go back to place last successful search started
980;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 981;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
982
983(defun isearch-*-char ()
984 "Handle * and ? specially in regexps."
985 (interactive)
986 (if isearch-regexp
987
988 (progn
989 (setq isearch-adjusted t)
990 (let ((cs (nth (if isearch-forward
991 5 ; isearch-other-end
992 2) ; saved (point)
993 (car (cdr isearch-cmds)))))
994 ;; (car isearch-cmds) is after last search;
995 ;; (car (cdr isearch-cmds)) is from before it.
996 (setq cs (or cs isearch-barrier))
997 (goto-char
998 (if isearch-forward
999 (max cs isearch-barrier)
1000 (min cs isearch-barrier))))))
08200510 1001 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1002
1003
8e1cae6d
JB
1004(defun isearch-|-char ()
1005 "If in regexp search, jump to the barrier."
1006 (interactive)
1007 (if isearch-regexp
1008 (progn
1009 (setq isearch-adjusted t)
1010 (goto-char isearch-barrier)))
08200510 1011 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1012
1013
b457cc3b 1014(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1015
1016(defun isearch-other-meta-char ()
8f90f594 1017 "Exit the search normally and reread this key sequence.
35a4d143
RS
1018But only if `search-exit-option' is non-nil, the default.
1019If it is the symbol `edit', the search string is edited in the minibuffer
1020and the meta character is unread so that it applies to editing the string."
8e1cae6d 1021 (interactive)
35a4d143
RS
1022 (cond ((eq search-exit-option 'edit)
1023 (let ((key (this-command-keys)))
e7cb0106 1024 (apply 'isearch-unread (listify-key-sequence key)))
35a4d143
RS
1025 (isearch-edit-string))
1026 (search-exit-option
70418205 1027 (let ((key (this-command-keys))
285eb5ef 1028 (index 0)
70418205
RS
1029 window)
1030 (apply 'isearch-unread (listify-key-sequence key))
285eb5ef
RS
1031 ;; Properly handle scroll-bar and mode-line clicks
1032 ;; for which a dummy prefix event was generated as (aref key 0).
1033 (and (> (length key) 1)
1034 (symbolp (aref key 0))
1035 (listp (aref key 1))
4b43d418
RS
1036 ;; These events now have a symbol; they used to have a list.
1037 ;; Accept either one. Other events have a number here.
1038 (not (numberp (posn-point (event-start (aref key 1)))))
285eb5ef 1039 (setq index 1))
70418205
RS
1040 ;; If we got a mouse click, maybe it was read with the buffer
1041 ;; it was clicked on. If so, that buffer, not the current one,
1042 ;; is in isearch mode. So end the search in that buffer.
285eb5ef
RS
1043 (if (and (listp (aref key index))
1044 (setq window (posn-window (event-start (aref key index))))
70418205
RS
1045 (windowp window))
1046 (save-excursion
1047 (set-buffer (window-buffer window))
1048 (isearch-done))
1049 (isearch-done))))
35a4d143
RS
1050 (t;; otherwise nil
1051 (isearch-process-search-string (this-command-keys)
1052 (this-command-keys)))))
8e1cae6d 1053
8e1cae6d
JB
1054(defun isearch-quote-char ()
1055 "Quote special characters for incremental search."
1056 (interactive)
1057 (isearch-process-search-char (read-quoted-char (isearch-message t))))
1058
8e1cae6d
JB
1059(defun isearch-return-char ()
1060 "Convert return into newline for incremental search.
1061Obsolete."
1062 (interactive)
1063 (isearch-process-search-char ?\n))
1064
8e1cae6d 1065(defun isearch-printing-char ()
b68c164d 1066 "Add this ordinary printing character to the search string and search."
8e1cae6d 1067 (interactive)
08200510 1068 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1069
1070(defun isearch-whitespace-chars ()
08200510 1071 "Match all whitespace chars, if in regexp mode.
b68c164d 1072If you want to search for just a space, type C-q SPC."
8e1cae6d 1073 (interactive)
08200510 1074 (if isearch-regexp
c5f6b356 1075 (if (and search-whitespace-regexp (not isearch-within-brackets))
08200510
RS
1076 (isearch-process-search-string search-whitespace-regexp " ")
1077 (isearch-printing-char))
1078 (progn
eb8c3be9 1079 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1080 ;; (setq isearch-word t)
1081 ;; (setq isearch-adjusted t)
1082 ;; (goto-char isearch-barrier)
1083 (isearch-printing-char))))
8e1cae6d
JB
1084
1085(defun isearch-process-search-char (char)
1086 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1087 (isearch-process-search-string
1088 (isearch-char-to-string char)
1089 (isearch-text-char-description char)))
8e1cae6d
JB
1090
1091(defun isearch-process-search-string (string message)
1092 (setq isearch-string (concat isearch-string string)
1093 isearch-message (concat isearch-message message))
1094 (isearch-search-and-update))
1095
1096\f
1097;;===========================================================
1098;; Search Ring
1099
08200510
RS
1100(defun isearch-ring-adjust1 (advance)
1101 ;; Helper for isearch-ring-adjust
1102 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1103 (length (length ring))
1104 (yank-pointer-name (if isearch-regexp
08200510 1105 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1106 'search-ring-yank-pointer))
1107 (yank-pointer (eval yank-pointer-name)))
1108 (if (zerop length)
1109 ()
1110 (set yank-pointer-name
1111 (setq yank-pointer
ffbc30b2
PE
1112 (mod (+ (or yank-pointer 0)
1113 (if advance -1 1))
1114 length)))
a5dcf63f 1115 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1116 isearch-message (mapconcat 'isearch-text-char-description
1117 isearch-string "")))))
1118
1119(defun isearch-ring-adjust (advance)
1120 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1121 (if (cdr isearch-cmds) ;; is there more than one thing on stack?
1122 (isearch-pop-state))
1123 (isearch-ring-adjust1 advance)
8e1cae6d 1124 (isearch-push-state)
08200510
RS
1125 (if search-ring-update
1126 (progn
1127 (isearch-search)
1128 (isearch-update))
1129 (isearch-edit-string)
1130 ))
8e1cae6d
JB
1131
1132(defun isearch-ring-advance ()
1133 "Advance to the next search string in the ring."
08200510 1134 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1135 (interactive)
1136 (isearch-ring-adjust 'advance))
1137
1138(defun isearch-ring-retreat ()
1139 "Retreat to the previous search string in the ring."
1140 (interactive)
1141 (isearch-ring-adjust nil))
1142
a5dcf63f
RS
1143(defun isearch-ring-advance-edit (n)
1144 "Insert the next element of the search history into the minibuffer."
1145 (interactive "p")
1146 (let* ((yank-pointer-name (if isearch-regexp
1147 'regexp-search-ring-yank-pointer
1148 'search-ring-yank-pointer))
1149 (yank-pointer (eval yank-pointer-name))
1150 (ring (if isearch-regexp regexp-search-ring search-ring))
1151 (length (length ring)))
1152 (if (zerop length)
1153 ()
1154 (set yank-pointer-name
1155 (setq yank-pointer
ffbc30b2
PE
1156 (mod (- (or yank-pointer 0) n)
1157 length)))
a5dcf63f 1158
a5dcf63f 1159 (erase-buffer)
35a4d143 1160 (insert (nth yank-pointer ring))
49c13105 1161 (goto-char (point-max)))))
a5dcf63f
RS
1162
1163(defun isearch-ring-retreat-edit (n)
1164 "Inserts the previous element of the search history into the minibuffer."
1165 (interactive "p")
1166 (isearch-ring-advance-edit (- n)))
1167
1168;;(defun isearch-ring-adjust-edit (advance)
1169;; "Use the next or previous search string in the ring while in minibuffer."
1170;; (isearch-ring-adjust1 advance)
1171;; (erase-buffer)
1172;; (insert isearch-string))
1173
1174;;(defun isearch-ring-advance-edit ()
1175;; (interactive)
1176;; (isearch-ring-adjust-edit 'advance))
1177
1178;;(defun isearch-ring-retreat-edit ()
1179;; "Retreat to the previous search string in the ring while in the minibuffer."
1180;; (interactive)
1181;; (isearch-ring-adjust-edit nil))
08200510
RS
1182
1183
1184(defun isearch-complete1 ()
1185 ;; Helper for isearch-complete and isearch-complete-edit
1186 ;; Return t if completion OK, nil if no completion exists.
1187 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1188 (alist (mapcar (function (lambda (string) (list string))) ring))
1189 (completion-ignore-case case-fold-search)
1190 (completion (try-completion isearch-string alist)))
1191 (cond
1192 ((eq completion t)
1193 ;; isearch-string stays the same
1194 t)
1195 ((or completion ; not nil, must be a string
1196 (= 0 (length isearch-string))) ; shouldnt have to say this
1197 (if (equal completion isearch-string) ;; no extension?
1198 (if completion-auto-help
1199 (with-output-to-temp-buffer "*Isearch completions*"
1200 (display-completion-list
1201 (all-completions isearch-string alist))))
1202 (setq isearch-string completion))
1203 t)
1204 (t
1205 (message "No completion") ; waits a second if in minibuffer
1206 nil))))
1207
1208(defun isearch-complete ()
1209 "Complete the search string from the strings on the search ring.
1210The completed string is then editable in the minibuffer.
1211If there is no completion possible, say so and continue searching."
1212 (interactive)
1213 (if (isearch-complete1)
1214 (isearch-edit-string)
1215 ;; else
1216 (sit-for 1)
1217 (isearch-update)))
8e1cae6d 1218
08200510
RS
1219(defun isearch-complete-edit ()
1220 "Same as `isearch-complete' except in the minibuffer."
1221 (interactive)
1222 (setq isearch-string (buffer-string))
1223 (if (isearch-complete1)
8e1cae6d 1224 (progn
08200510
RS
1225 (erase-buffer)
1226 (insert isearch-string))))
8e1cae6d
JB
1227
1228\f
1229;;;==============================================================
1230;; The search status stack (and isearch window-local variables, not used).
08200510 1231;; Need a structure for this.
8e1cae6d
JB
1232
1233(defun isearch-top-state ()
8e1cae6d
JB
1234 (let ((cmd (car isearch-cmds)))
1235 (setq isearch-string (car cmd)
1236 isearch-message (car (cdr cmd))
1237 isearch-success (nth 3 cmd)
1238 isearch-forward (nth 4 cmd)
1239 isearch-other-end (nth 5 cmd)
08200510
RS
1240 isearch-word (nth 6 cmd)
1241 isearch-invalid-regexp (nth 7 cmd)
1242 isearch-wrapped (nth 8 cmd)
c5f6b356
RS
1243 isearch-barrier (nth 9 cmd)
1244 isearch-within-brackets (nth 10 cmd))
8e1cae6d
JB
1245 (goto-char (car (cdr (cdr cmd))))))
1246
1247(defun isearch-pop-state ()
8e1cae6d
JB
1248 (setq isearch-cmds (cdr isearch-cmds))
1249 (isearch-top-state)
1250 )
1251
1252(defun isearch-push-state ()
1253 (setq isearch-cmds
1254 (cons (list isearch-string isearch-message (point)
1255 isearch-success isearch-forward isearch-other-end
08200510 1256 isearch-word
c5f6b356
RS
1257 isearch-invalid-regexp isearch-wrapped isearch-barrier
1258 isearch-within-brackets)
8e1cae6d
JB
1259 isearch-cmds)))
1260
8e1cae6d
JB
1261\f
1262;;;==================================================================
1263;; Message string
1264
1265(defun isearch-message (&optional c-q-hack ellipsis)
1266 ;; Generate and print the message string.
1267 (let ((cursor-in-echo-area ellipsis)
1268 (m (concat
08200510 1269 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1270 isearch-message
1271 (isearch-message-suffix c-q-hack ellipsis)
1272 )))
1273 (if c-q-hack m (message "%s" m))))
1274
08200510 1275(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1276 ;; If about to search, and previous search regexp was invalid,
1277 ;; check that it still is. If it is valid now,
1278 ;; let the message we display while searching say that it is valid.
1279 (and isearch-invalid-regexp ellipsis
1280 (condition-case ()
1281 (progn (re-search-forward isearch-string (point) t)
1282 (setq isearch-invalid-regexp nil))
1283 (error nil)))
1284 ;; If currently failing, display no ellipsis.
1285 (or isearch-success (setq ellipsis nil))
1286 (let ((m (concat (if isearch-success "" "failing ")
1287 (if isearch-wrapped "wrapped ")
08200510 1288 (if isearch-word "word " "")
8e1cae6d 1289 (if isearch-regexp "regexp " "")
08200510 1290 (if nonincremental "search" "I-search")
8e1cae6d
JB
1291 (if isearch-forward ": " " backward: ")
1292 )))
1293 (aset m 0 (upcase (aref m 0)))
1294 m))
1295
1296
1297(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1298 (concat (if c-q-hack "^Q" "")
1299 (if isearch-invalid-regexp
1300 (concat " [" isearch-invalid-regexp "]")
1301 "")))
1302
1303\f
1304;;;========================================================
1305;;; Searching
1306
1307(defun isearch-search ()
1308 ;; Do the search with the current search string.
1309 (isearch-message nil t)
43873131 1310 (if (and isearch-case-fold-search search-upper-case)
b78559b0
RS
1311 (setq isearch-case-fold-search
1312 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d
JB
1313 (condition-case lossage
1314 (let ((inhibit-quit nil)
1315 (case-fold-search isearch-case-fold-search))
1316 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1317 (setq isearch-within-brackets nil)
8e1cae6d
JB
1318 (setq isearch-success
1319 (funcall
08200510
RS
1320 (cond (isearch-word
1321 (if isearch-forward
1322 'word-search-forward 'word-search-backward))
1323 (isearch-regexp
1324 (if isearch-forward
1325 're-search-forward 're-search-backward))
1326 (t
1327 (if isearch-forward 'search-forward 'search-backward)))
8e1cae6d
JB
1328 isearch-string nil t))
1329 (if isearch-success
1330 (setq isearch-other-end
1331 (if isearch-forward (match-beginning 0) (match-end 0)))))
1332
d32696d4 1333 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1334 (setq isearch-success nil))
1335
1336 (invalid-regexp
1337 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1338 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1339 isearch-invalid-regexp))
8e1cae6d
JB
1340 (if (string-match
1341 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1342 isearch-invalid-regexp)
f1c03125
RS
1343 (setq isearch-invalid-regexp "incomplete input")))
1344 (error
1345 ;; stack overflow in regexp search.
1346 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1347
1348 (if isearch-success
1349 nil
1350 ;; Ding if failed this time after succeeding last time.
1351 (and (nth 3 (car isearch-cmds))
1352 (ding))
1353 (goto-char (nth 2 (car isearch-cmds)))))
1354
08200510
RS
1355
1356\f
1357;;;========================================================
1358;;; Highlighting
1359
b78559b0 1360(defvar isearch-overlay nil)
08200510 1361
b78559b0 1362(defun isearch-highlight (beg end)
82318db5 1363 (if (null search-highlight)
08200510 1364 nil
b78559b0
RS
1365 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1366 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1367 (overlay-put isearch-overlay 'face
1368 (if (internal-find-face 'isearch nil)
1369 'isearch 'region))))
b78559b0
RS
1370
1371(defun isearch-dehighlight (totally)
1372 (if isearch-overlay
1373 (delete-overlay isearch-overlay)))
08200510
RS
1374
1375;;;===========================================================
1376;;; General utilities
1377
08200510 1378
b78559b0
RS
1379(defun isearch-no-upper-case-p (string regexp-flag)
1380 "Return t if there are no upper case chars in STRING.
1381If REGEXP-FLAG is non-nil, disregard letters preceeded by `\\' (but not `\\\\')
1382since they have special meaning in a regexp."
08200510 1383 (let ((case-fold-search nil))
b78559b0
RS
1384 (not (string-match (if regexp-flag "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]"
1385 "[A-Z]")
1386 string))))
08200510
RS
1387
1388
8e1cae6d 1389;;;=================================================
b78559b0 1390;; Portability functions to support various Emacs versions.
8e1cae6d 1391
08200510
RS
1392;; To quiet the byte-compiler.
1393(defvar unread-command-event)
dbc4e1c1 1394(defvar unread-command-events)
08200510 1395(defvar last-command-event)
8e1cae6d 1396
08200510
RS
1397(defun isearch-char-to-string (c)
1398 (if (integerp c)
1399 (make-string 1 c)
8d7e4e80
RS
1400 (if (and (symbolp c) (get c 'ascii-character))
1401 (make-string 1 (get c 'ascii-character))
1402 (make-string 1 (event-to-character c)))))
08200510
RS
1403
1404(defun isearch-text-char-description (c)
9d78ff8b
RS
1405 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1406 (text-char-description c)
1407 (isearch-char-to-string c)))
08200510 1408
8f90f594 1409(defun isearch-unread (&rest char-or-events)
8f90f594
RS
1410 ;; General function to unread characters or events.
1411 (if isearch-gnu-emacs-events
e7cb0106
RS
1412 (setq unread-command-events
1413 (append char-or-events unread-command-events))
8f90f594 1414 (let ((char (if (cdr char-or-events)
75e86b2e
RS
1415 (progn
1416 (while (cdr char-or-events)
1417 (setq char-or-events (cdr char-or-events)))
1418 (+ 128 (car char-or-events)))
8f90f594
RS
1419 (car char-or-events))))
1420 (if isearch-event-data-type
1421 (setq unread-command-event char)
1422 (setq unread-command-char char)))))
08200510
RS
1423
1424(defun isearch-last-command-char ()
1425 ;; General function to return the last command character.
a5dcf63f 1426 (if isearch-event-data-type
08200510
RS
1427 last-command-event
1428 last-command-char))
1429
3b1e4dd1 1430;;; isearch.el ends here