*** empty log message ***
[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
849cfd72 7;; |$Date: 1994/01/15 16:24:47 $|$Revision: 1.61 $
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)
8e1cae6d 352
08200510
RS
353;; Flag to indicate a yank occurred, so don't move the cursor.
354(defvar isearch-yank-flag nil)
8e1cae6d 355
08200510
RS
356;;; A function to be called after each input character is processed.
357;;; (It is not called after characters that exit the search.)
358;;; It is only set from an optional argument to `isearch-mode'.
359(defvar isearch-op-fun nil)
8e1cae6d 360
08200510
RS
361;;; Is isearch-mode in a recursive edit for modal searching.
362(defvar isearch-recursive-edit nil)
8e1cae6d 363
08200510
RS
364;;; Should isearch be terminated after doing one search?
365(defvar isearch-nonincremental nil)
366
367;; New value of isearch-forward after isearch-edit-string.
368(defvar isearch-new-forward nil)
8e1cae6d 369
8e1cae6d
JB
370
371;;;==============================================================
372;; Minor-mode-alist changes - kind of redundant with the
373;; echo area, but if isearching in multiple windows, it can be useful.
374
375(or (assq 'isearch-mode minor-mode-alist)
376 (nconc minor-mode-alist
377 (list '(isearch-mode isearch-mode))))
378
08200510 379(defvar isearch-mode nil) ;; Name of the minor mode, if non-nil.
8e1cae6d
JB
380(make-variable-buffer-local 'isearch-mode)
381
fdf5afa4
RS
382(define-key global-map "\C-s" 'isearch-forward)
383(define-key esc-map "\C-s" 'isearch-forward-regexp)
384(define-key global-map "\C-r" 'isearch-backward)
385(define-key esc-map "\C-r" 'isearch-backward-regexp)
386
8e1cae6d
JB
387;;;===============================================================
388;;; Entry points to isearch-mode.
08200510 389;;; These four functions should replace those in loaddefs.el
b457cc3b 390;;; An alternative is to defalias isearch-forward etc to isearch-mode,
08200510 391;;; and look at this-command to set the options accordingly.
8e1cae6d 392
eceee2c0 393(defun isearch-forward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
394 "\
395Do incremental search forward.
08200510
RS
396With a prefix argument, do an incremental regular expression search instead.
397\\<isearch-mode-map>
8e1cae6d 398As you type characters, they add to the search string and are found.
08200510 399The following non-printing keys are bound in `isearch-mode-map'.
8e1cae6d 400
08200510 401Type \\[isearch-delete-char] to cancel characters from end of search string.
8e1cae6d 402Type \\[isearch-exit] to exit, leaving point at location found.
08200510
RS
403Type LFD (C-j) to match end of line.
404Type \\[isearch-repeat-forward] to search again forward,\
405 \\[isearch-repeat-backward] to search again backward.
406Type \\[isearch-yank-word] to yank word from buffer onto end of search\
407 string and search for it.
408Type \\[isearch-yank-line] to yank rest of line onto end of search string\
409 and search for it.
8e1cae6d 410Type \\[isearch-quote-char] to quote control character to search for it.
08200510
RS
411\\[isearch-abort] while searching or when search has failed cancels input\
412 back to what has
413 been found successfully.
414\\[isearch-abort] when search is successful aborts and moves point to\
415 starting point.
8e1cae6d
JB
416
417Also supported is a search ring of the previous 16 search strings.
418Type \\[isearch-ring-advance] to search for the next item in the search ring.
08200510
RS
419Type \\[isearch-ring-retreat] to search for the previous item in the search\
420 ring.
421Type \\[isearch-complete] to complete the search string using the search ring.
8e1cae6d 422
08200510
RS
423The above keys, bound in `isearch-mode-map', are often controlled by
424 options; do M-x apropos on search-.* to find them.
8e1cae6d 425Other control and meta characters terminate the search
08200510 426 and are then executed normally (depending on `search-exit-option').
8e1cae6d 427
08200510
RS
428If this function is called non-interactively, it does not return to
429the calling function until the search is done."
8e1cae6d 430
eceee2c0 431 (interactive "P\np")
4ae7d00a 432 (isearch-mode t (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 433
eceee2c0 434(defun isearch-forward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
435 "\
436Do incremental search forward for regular expression.
08200510 437With a prefix argument, do a regular string search instead.
8e1cae6d
JB
438Like ordinary incremental search except that your input
439is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 440 (interactive "P\np")
4ae7d00a 441 (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
8e1cae6d 442
eceee2c0 443(defun isearch-backward (&optional regexp-p no-recursive-edit)
8e1cae6d
JB
444 "\
445Do incremental search backward.
08200510 446With a prefix argument, do a regular expression search instead.
8e1cae6d 447See \\[isearch-forward] for more information."
eceee2c0 448 (interactive "P\np")
4ae7d00a 449 (isearch-mode nil (not (null regexp-p)) nil (not no-recursive-edit)))
8e1cae6d 450
eceee2c0 451(defun isearch-backward-regexp (&optional not-regexp no-recursive-edit)
8e1cae6d
JB
452 "\
453Do incremental search backward for regular expression.
08200510 454With a prefix argument, do a regular string search instead.
8e1cae6d
JB
455Like ordinary incremental search except that your input
456is treated as a regexp. See \\[isearch-forward] for more info."
eceee2c0 457 (interactive "P\np")
4ae7d00a 458 (isearch-mode nil (null not-regexp) nil (not no-recursive-edit)))
08200510
RS
459
460
461(defun isearch-mode-help ()
462 (interactive)
463 (describe-function 'isearch-forward)
464 (isearch-update))
8e1cae6d
JB
465
466\f
467;;;==================================================================
468;; isearch-mode only sets up incremental search for the minor mode.
469;; All the work is done by the isearch-mode commands.
470
08200510
RS
471;; Not used yet:
472;;(defconst isearch-commands '(isearch-forward isearch-backward
473;; isearch-forward-regexp isearch-backward-regexp)
474;; "List of commands for which isearch-mode does not recursive-edit.")
475
476
477(defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
8e1cae6d 478 "Start isearch minor mode. Called by isearch-forward, etc."
8e1cae6d
JB
479
480 ;; Initialize global vars.
481 (setq isearch-forward forward
482 isearch-regexp regexp
08200510 483 isearch-word word-p
8e1cae6d
JB
484 isearch-op-fun op-fun
485 isearch-case-fold-search case-fold-search
486 isearch-string ""
487 isearch-message ""
488 isearch-cmds nil
489 isearch-success t
490 isearch-wrapped nil
491 isearch-barrier (point)
492 isearch-adjusted nil
493 isearch-yank-flag nil
494 isearch-invalid-regexp nil
c5f6b356 495 isearch-within-brackets nil
dd492f04
JA
496 ;; Use (baud-rate) for now, for sake of other versions.
497 isearch-slow-terminal-mode (and (<= (baud-rate) search-slow-speed)
8e1cae6d
JB
498 (> (window-height)
499 (* 4 search-slow-window-lines)))
500 isearch-other-end nil
501 isearch-small-window nil
8e1cae6d 502
8e1cae6d 503 isearch-opoint (point)
a5dcf63f
RS
504 search-ring-yank-pointer nil
505 regexp-search-ring-yank-pointer nil)
292a8dff
RS
506 (setq isearch-window-configuration
507 (if isearch-slow-terminal-mode (current-window-configuration) nil))
70418205 508
2ed45c8b
RS
509;; This was for Lucid Emacs. But now that we have pre-command-hook,
510;; it causes trouble.
511;; (if isearch-pre-command-hook-exists
512;; (add-hook 'pre-command-hook 'isearch-pre-command-hook))
8e1cae6d
JB
513 (setq isearch-mode " Isearch") ;; forward? regexp?
514 (set-buffer-modified-p (buffer-modified-p)) ; update modeline
515
7492a21e
RS
516 ;; It is ugly to show region highlighting while the search
517 ;; is going on. And we don't want the mark active at the end either.
518 (setq deactivate-mark t)
519
8e1cae6d
JB
520 (isearch-push-state)
521
849cfd72
RS
522 (make-local-variable 'overriding-local-map)
523 (setq overriding-local-map isearch-mode-map)
8e1cae6d
JB
524 (isearch-update)
525 (run-hooks 'isearch-mode-hook)
08200510
RS
526
527 ;; isearch-mode can be made modal (in the sense of not returning to
528 ;; the calling function until searching is completed) by entering
529 ;; a recursive-edit and exiting it when done isearching.
130bf67c
RS
530 (if recursive-edit
531 (let ((isearch-recursive-edit t))
532 (recursive-edit)))
8e1cae6d
JB
533 )
534
535
536;;;====================================================
537;; Some high level utilities. Others below.
538
539(defun isearch-update ()
540 ;; Called after each command to update the display.
a5dcf63f 541 (if (if isearch-event-data-type
08200510 542 (null unread-command-event)
1a3a6707 543 (if isearch-gnu-emacs-events
dbc4e1c1 544 (null unread-command-events)
08200510 545 (< unread-command-char 0)))
8e1cae6d
JB
546 (progn
547 (if (not (input-pending-p))
548 (isearch-message))
549 (if (and isearch-slow-terminal-mode
550 (not (or isearch-small-window
551 (pos-visible-in-window-p))))
08200510 552 (let ((found-point (point)))
8e1cae6d 553 (setq isearch-small-window t)
8e1cae6d
JB
554 (move-to-window-line 0)
555 (let ((window-min-height 1))
556 (split-window nil (if (< search-slow-window-lines 0)
557 (1+ (- search-slow-window-lines))
558 (- (window-height)
559 (1+ search-slow-window-lines)))))
560 (if (< search-slow-window-lines 0)
561 (progn (vertical-motion (- 1 search-slow-window-lines))
562 (set-window-start (next-window) (point))
563 (set-window-hscroll (next-window)
564 (window-hscroll))
565 (set-window-hscroll (selected-window) 0))
566 (other-window 1))
08200510
RS
567 (goto-char found-point)))
568 (if isearch-other-end
569 (if (< isearch-other-end (point)) ; isearch-forward?
570 (isearch-highlight isearch-other-end (point))
99afb671
RS
571 (isearch-highlight (point) isearch-other-end))
572 (isearch-dehighlight nil))
08200510 573 ))
8e1cae6d
JB
574 (setq ;; quit-flag nil not for isearch-mode
575 isearch-adjusted nil
576 isearch-yank-flag nil)
577 )
578
579
35a4d143 580(defun isearch-done (&optional nopush)
8e1cae6d 581 ;; Called by all commands that terminate isearch-mode.
35a4d143 582 ;; If NOPUSH is non-nil, we don't push the string on the search ring.
849cfd72 583 (setq overriding-local-map nil)
08200510
RS
584 ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
585 (isearch-dehighlight t)
586 (let ((found-start (window-start (selected-window)))
587 (found-point (point)))
70418205
RS
588 (if isearch-window-configuration
589 (set-window-configuration isearch-window-configuration))
08200510 590
d49b6338
RS
591 (if isearch-small-window
592 (goto-char found-point)
593 ;; Exiting the save-window-excursion clobbers window-start; restore it.
594 (set-window-start (selected-window) found-start t)))
595
08200510
RS
596 ;; If there was movement, mark the starting position.
597 ;; Maybe should test difference between and set mark iff > threshold.
598 (if (/= (point) isearch-opoint)
7872ee4d
RS
599 (progn
600 (push-mark isearch-opoint t)
67cbb73f 601 (deactivate-mark)
7872ee4d
RS
602 (or executing-macro (> (minibuffer-depth) 0)
603 (message "Mark saved where search started")))
08200510
RS
604 ;; (message "") why is this needed?
605 )
08200510
RS
606
607 (setq isearch-mode nil)
608 (set-buffer-modified-p (buffer-modified-p)) ;; update modeline
8e1cae6d 609
35a4d143 610 (if (and (> (length isearch-string) 0) (not nopush))
8e1cae6d
JB
611 ;; Update the ring data.
612 (if isearch-regexp
a8f783b2
RS
613 (if (or (null regexp-search-ring)
614 (not (string= isearch-string (car regexp-search-ring))))
8e1cae6d 615 (progn
08200510 616 (setq regexp-search-ring
a5dcf63f 617 (cons isearch-string regexp-search-ring))
08200510
RS
618 (if (> (length regexp-search-ring) regexp-search-ring-max)
619 (setcdr (nthcdr (1- search-ring-max) regexp-search-ring)
8e1cae6d 620 nil))))
a8f783b2
RS
621 (if (or (null search-ring)
622 (not (string= isearch-string (car search-ring))))
8e1cae6d 623 (progn
a5dcf63f 624 (setq search-ring (cons isearch-string search-ring))
8e1cae6d
JB
625 (if (> (length search-ring) search-ring-max)
626 (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
627
8e1cae6d 628 (run-hooks 'isearch-mode-end-hook)
08200510
RS
629 (if isearch-recursive-edit (exit-recursive-edit)))
630
631;;;=======================================================
632;;; Switching buffers should first terminate isearch-mode.
eb8c3be9 633;;; This is done quite differently for each variant of emacs.
08200510
RS
634;;; For lemacs, see Exiting in lemacs below
635
636;; For Emacs 19, the frame switch event is handled.
637(defun isearch-switch-frame-handler ()
638 (interactive) ;; Is this necessary?
639 ;; First terminate isearch-mode.
640 (isearch-done)
dba1ec55 641 (handle-switch-frame (car (cdr (isearch-last-command-char)))))
08200510
RS
642
643;;;========================================================
8e1cae6d
JB
644
645\f
646;;;====================================================
647;; Commands active while inside of the isearch minor mode.
648
649(defun isearch-exit ()
650 "Exit search normally.
651However, if this is the first command after starting incremental
652search and `search-nonincremental-instead' is non-nil, do a
08200510 653nonincremental search instead via `isearch-edit-string'."
8e1cae6d
JB
654 (interactive)
655 (if (and search-nonincremental-instead
656 (= 0 (length isearch-string)))
08200510
RS
657 (let ((isearch-nonincremental t))
658 (isearch-edit-string)))
8e1cae6d
JB
659 (isearch-done))
660
661
662(defun isearch-edit-string ()
08200510
RS
663 "Edit the search string in the minibuffer.
664The following additional command keys are active while editing.
665\\<minibuffer-local-isearch-map>
666\\[exit-minibuffer] to resume incremental searching with the edited string.
667\\[isearch-nonincremental-exit-minibuffer] to do one nonincremental search.
668\\[isearch-forward-exit-minibuffer] to resume isearching forward.
669\\[isearch-backward-exit-minibuffer] to resume isearching backward.
670\\[isearch-ring-advance-edit] to replace the search string with the next item in the search ring.
eb8c3be9 671\\[isearch-ring-retreat-edit] to replace the search string with the previous item in the search ring.
08200510
RS
672\\[isearch-complete-edit] to complete the search string using the search ring.
673
674If first char entered is \\[isearch-yank-word], then do word search instead."
675
676 ;; This code is very hairy for several reasons, explained in the code.
677 ;; Mainly, isearch-mode must be terminated while editing and then restarted.
678 ;; If there were a way to catch any change of buffer from the minibuffer,
679 ;; this could be simplified greatly.
eb8c3be9 680 ;; Editing doesn't back up the search point. Should it?
8e1cae6d 681 (interactive)
08200510 682 (condition-case err
a9a5a9fa 683 (let ((isearch-nonincremental isearch-nonincremental)
08200510
RS
684
685 ;; Locally bind all isearch global variables to protect them
686 ;; from recursive isearching.
687 ;; isearch-string -message and -forward are not bound
688 ;; so they may be changed. Instead, save the values.
689 (isearch-new-string isearch-string)
690 (isearch-new-message isearch-message)
691 (isearch-new-forward isearch-forward)
692 (isearch-new-word isearch-word)
693
694 (isearch-regexp isearch-regexp)
695 (isearch-op-fun isearch-op-fun)
696 (isearch-cmds isearch-cmds)
697 (isearch-success isearch-success)
698 (isearch-wrapped isearch-wrapped)
699 (isearch-barrier isearch-barrier)
700 (isearch-adjusted isearch-adjusted)
701 (isearch-yank-flag isearch-yank-flag)
702 (isearch-invalid-regexp isearch-invalid-regexp)
c5f6b356 703 (isearch-within-brackets isearch-within-brackets)
08200510
RS
704 (isearch-other-end isearch-other-end)
705 (isearch-opoint isearch-opoint)
706 (isearch-slow-terminal-mode isearch-slow-terminal-mode)
707 (isearch-small-window isearch-small-window)
708 (isearch-recursive-edit isearch-recursive-edit)
709 ;; Save current configuration so we can restore it here.
710 (isearch-window-configuration (current-window-configuration))
711 )
712
713 ;; Actually terminate isearching until editing is done.
714 ;; This is so that the user can do anything without failure,
715 ;; like switch buffers and start another isearch, and return.
716 (condition-case err
35a4d143 717 (isearch-done t)
08200510
RS
718 (exit nil)) ; was recursive editing
719
720 (isearch-message) ;; for read-char
721 (unwind-protect
722 (let* (;; Why does following read-char echo?
723 ;;(echo-keystrokes 0) ;; not needed with above message
d94307ee 724 (e (let ((cursor-in-echo-area t))
235beb32 725 (read-event)))
08200510
RS
726 ;; Binding minibuffer-history-symbol to nil is a work-around
727 ;; for some incompatibility with gmhist.
728 (minibuffer-history-symbol))
729 ;; If the first character the user types when we prompt them
730 ;; for a string is the yank-word character, then go into
731 ;; word-search mode. Otherwise unread that character and
732 ;; read a key the normal way.
733 ;; Word search does not apply (yet) to regexp searches,
734 ;; no check is made here.
735 (message (isearch-message-prefix nil nil t))
736 (if (eq 'isearch-yank-word
235beb32 737 (lookup-key isearch-mode-map (vector e)))
08200510
RS
738 (setq isearch-word t ;; so message-prefix is right
739 isearch-new-word t)
740 (isearch-unread e))
a5dcf63f
RS
741 (setq cursor-in-echo-area nil)
742 (setq isearch-new-string
35a4d143 743 (let (junk-ring)
a5dcf63f
RS
744 (read-from-minibuffer (isearch-message-prefix)
745 isearch-string
746 minibuffer-local-isearch-map nil
35a4d143 747 'junk-ring))
08200510
RS
748 isearch-new-message (mapconcat 'text-char-description
749 isearch-new-string "")))
750 ;; Always resume isearching by restarting it.
751 (isearch-mode isearch-forward
752 isearch-regexp
753 isearch-op-fun
754 isearch-recursive-edit
755 isearch-word)
756
757 ;; Copy new local values to isearch globals
758 (setq isearch-string isearch-new-string
759 isearch-message isearch-new-message
760 isearch-forward isearch-new-forward
761 isearch-word isearch-new-word))
762
763 ;; Empty isearch-string means use default.
764 (if (= 0 (length isearch-string))
a8f783b2
RS
765 (setq isearch-string (car (if isearch-regexp regexp-search-ring
766 search-ring)))
767 ;; This used to set the last search string,
768 ;; but I think it is not right to do that here.
769 ;; Only the string actually used should be saved.
770 )
08200510
RS
771
772 ;; Reinvoke the pending search.
773 (isearch-push-state)
774 (isearch-search)
775 (isearch-update)
776 (if isearch-nonincremental
777 (progn
778 ;; (sit-for 1) ;; needed if isearch-done does: (message "")
779 (isearch-done))))
8e1cae6d 780
08200510
RS
781 (quit ; handle abort-recursive-edit
782 (isearch-abort) ;; outside of let to restore outside global values
783 )))
784
785(defun isearch-nonincremental-exit-minibuffer ()
786 (interactive)
787 (setq isearch-nonincremental t)
788 (exit-minibuffer))
789
790(defun isearch-forward-exit-minibuffer ()
791 (interactive)
792 (setq isearch-new-forward t)
793 (exit-minibuffer))
8e1cae6d 794
08200510 795(defun isearch-reverse-exit-minibuffer ()
8e1cae6d 796 (interactive)
08200510
RS
797 (setq isearch-new-forward nil)
798 (exit-minibuffer))
799
800
801(defun isearch-abort ()
802 "Abort incremental search mode if searching is successful, signalling quit.
803Otherwise, revert to previous successful search and continue searching.
804Use `isearch-exit' to quit without signalling."
805 (interactive)
eb8c3be9 806;; (ding) signal instead below, if quitting
8e1cae6d
JB
807 (discard-input)
808 (if isearch-success
809 ;; If search is successful, move back to starting point
810 ;; and really do quit.
811 (progn (goto-char isearch-opoint)
35a4d143 812 (isearch-done t) ; exit isearch
a5dcf63f 813 (signal 'quit nil)) ; and pass on quit signal
08200510 814 ;; If search is failing, rub out until it is once more successful.
8e1cae6d
JB
815 (while (not isearch-success) (isearch-pop-state))
816 (isearch-update)))
817
818
819(defun isearch-repeat (direction)
820 ;; Utility for isearch-repeat-forward and -backward.
821 (if (eq isearch-forward (eq direction 'forward))
822 ;; C-s in forward or C-r in reverse.
823 (if (equal isearch-string "")
824 ;; If search string is empty, use last one.
825 (setq isearch-string
8e1cae6d 826 (or (if isearch-regexp
a5dcf63f
RS
827 (car regexp-search-ring)
828 (car search-ring))
8e1cae6d
JB
829 "")
830 isearch-message
08200510 831 (mapconcat 'isearch-text-char-description
8e1cae6d
JB
832 isearch-string ""))
833 ;; If already have what to search for, repeat it.
834 (or isearch-success
835 (progn
836
837 (goto-char (if isearch-forward (point-min) (point-max)))
838 (setq isearch-wrapped t))))
839 ;; C-s in reverse or C-r in forward, change direction.
840 (setq isearch-forward (not isearch-forward)))
841
842 (setq isearch-barrier (point)) ; For subsequent \| if regexp.
6fefdc4b
RS
843
844 (if (equal isearch-string "")
845 (setq isearch-success t)
846 (if (and isearch-success (equal (match-end 0) (match-beginning 0)))
8e1cae6d
JB
847 ;; If repeating a search that found
848 ;; an empty string, ensure we advance.
6fefdc4b
RS
849 (if (if isearch-forward (eobp) (bobp))
850 ;; If there's nowhere to advance to, fail (and wrap next time).
851 (progn
852 (setq isearch-success nil)
853 (ding))
854 (forward-char (if isearch-forward 1 -1))
855 (isearch-search))
856 (isearch-search)))
857
8e1cae6d
JB
858 (isearch-push-state)
859 (isearch-update))
860
861(defun isearch-repeat-forward ()
862 "Repeat incremental search forwards."
863 (interactive)
864 (isearch-repeat 'forward))
865
866(defun isearch-repeat-backward ()
867 "Repeat incremental search backwards."
868 (interactive)
869 (isearch-repeat 'backward))
870
871(defun isearch-toggle-regexp ()
872 "Toggle regexp searching on or off."
873 ;; The status stack is left unchanged.
874 (interactive)
875 (setq isearch-regexp (not isearch-regexp))
08200510 876 (if isearch-regexp (setq isearch-word nil))
8e1cae6d
JB
877 (isearch-update))
878
879(defun isearch-delete-char ()
880 "Discard last input item and move point back.
881If no previous match was done, just beep."
882 (interactive)
883 (if (null (cdr isearch-cmds))
884 (ding)
885 (isearch-pop-state))
886 (isearch-update))
887
888
889(defun isearch-yank (chunk)
890 ;; Helper for isearch-yank-word and isearch-yank-line
30d47262
RS
891 ;; CHUNK should be word, line or kill.
892 (let ((string (cond
893 ((eq chunk 'kill)
894 (current-kill 0))
895 (t
896 (save-excursion
897 (and (not isearch-forward) isearch-other-end
898 (goto-char isearch-other-end))
899 (buffer-substring
900 (point)
901 (save-excursion
902 (cond
903 ((eq chunk 'word)
904 (forward-word 1))
905 ((eq chunk 'line)
906 (end-of-line)))
907 (point))))))))
08200510
RS
908 ;; Downcase the string if not supposed to case-fold yanked strings.
909 (if (and isearch-case-fold-search
fdf5afa4 910 (eq 'not-yanks search-upper-case))
08200510
RS
911 (setq string (downcase string)))
912 (if isearch-regexp (setq string (regexp-quote string)))
913 (setq isearch-string (concat isearch-string string)
8e1cae6d
JB
914 isearch-message
915 (concat isearch-message
08200510
RS
916 (mapconcat 'isearch-text-char-description
917 string ""))
8e1cae6d
JB
918 ;; Don't move cursor in reverse search.
919 isearch-yank-flag t))
920 (isearch-search-and-update))
921
30d47262
RS
922(defun isearch-yank-kill ()
923 "Pull string from kill ring into search string."
924 (interactive)
925 (isearch-yank 'kill))
8e1cae6d
JB
926
927(defun isearch-yank-word ()
928 "Pull next word from buffer into search string."
929 (interactive)
930 (isearch-yank 'word))
931
932(defun isearch-yank-line ()
933 "Pull rest of line from buffer into search string."
934 (interactive)
935 (isearch-yank 'line))
936
937
938(defun isearch-search-and-update ()
939 ;; Do the search and update the display.
940 (if (and (not isearch-success)
941 ;; unsuccessful regexp search may become
942 ;; successful by addition of characters which
943 ;; make isearch-string valid
944 (not isearch-regexp))
945 nil
946 ;; In reverse search, adding stuff at
947 ;; the end may cause zero or many more chars to be
948 ;; matched, in the string following point.
949 ;; Allow all those possibilities without moving point as
950 ;; long as the match does not extend past search origin.
951 (if (and (not isearch-forward) (not isearch-adjusted)
952 (condition-case ()
953 (looking-at (if isearch-regexp isearch-string
954 (regexp-quote isearch-string)))
955 (error nil))
956 (or isearch-yank-flag
957 (<= (match-end 0)
958 (min isearch-opoint isearch-barrier))))
959 (setq isearch-success t
960 isearch-invalid-regexp nil
c5f6b356 961 isearch-within-brackets nil
8e1cae6d
JB
962 isearch-other-end (match-end 0))
963 ;; Not regexp, not reverse, or no match at point.
964 (if (and isearch-other-end (not isearch-adjusted))
965 (goto-char (if isearch-forward isearch-other-end
966 (min isearch-opoint
967 isearch-barrier
968 (1+ isearch-other-end)))))
969 (isearch-search)
970 ))
971 (isearch-push-state)
972 (if isearch-op-fun (funcall isearch-op-fun))
973 (isearch-update))
974
975
976;; *, ?, and | chars can make a regexp more liberal.
08200510 977;; They can make a regexp match sooner or make it succeed instead of failing.
8e1cae6d
JB
978;; So go back to place last successful search started
979;; or to the last ^S/^R (barrier), whichever is nearer.
08200510 980;; + needs no special handling because the string must match at least once.
8e1cae6d
JB
981
982(defun isearch-*-char ()
983 "Handle * and ? specially in regexps."
984 (interactive)
985 (if isearch-regexp
986
987 (progn
988 (setq isearch-adjusted t)
989 (let ((cs (nth (if isearch-forward
990 5 ; isearch-other-end
991 2) ; saved (point)
992 (car (cdr isearch-cmds)))))
993 ;; (car isearch-cmds) is after last search;
994 ;; (car (cdr isearch-cmds)) is from before it.
995 (setq cs (or cs isearch-barrier))
996 (goto-char
997 (if isearch-forward
998 (max cs isearch-barrier)
999 (min cs isearch-barrier))))))
08200510 1000 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1001
1002
8e1cae6d
JB
1003(defun isearch-|-char ()
1004 "If in regexp search, jump to the barrier."
1005 (interactive)
1006 (if isearch-regexp
1007 (progn
1008 (setq isearch-adjusted t)
1009 (goto-char isearch-barrier)))
08200510 1010 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1011
1012
b457cc3b 1013(defalias 'isearch-other-control-char 'isearch-other-meta-char)
8e1cae6d
JB
1014
1015(defun isearch-other-meta-char ()
8f90f594 1016 "Exit the search normally and reread this key sequence.
35a4d143
RS
1017But only if `search-exit-option' is non-nil, the default.
1018If it is the symbol `edit', the search string is edited in the minibuffer
1019and the meta character is unread so that it applies to editing the string."
8e1cae6d 1020 (interactive)
35a4d143
RS
1021 (cond ((eq search-exit-option 'edit)
1022 (let ((key (this-command-keys)))
e7cb0106 1023 (apply 'isearch-unread (listify-key-sequence key)))
35a4d143
RS
1024 (isearch-edit-string))
1025 (search-exit-option
70418205 1026 (let ((key (this-command-keys))
285eb5ef 1027 (index 0)
70418205
RS
1028 window)
1029 (apply 'isearch-unread (listify-key-sequence key))
285eb5ef
RS
1030 ;; Properly handle scroll-bar and mode-line clicks
1031 ;; for which a dummy prefix event was generated as (aref key 0).
1032 (and (> (length key) 1)
1033 (symbolp (aref key 0))
1034 (listp (aref key 1))
4b43d418
RS
1035 ;; These events now have a symbol; they used to have a list.
1036 ;; Accept either one. Other events have a number here.
1037 (not (numberp (posn-point (event-start (aref key 1)))))
285eb5ef 1038 (setq index 1))
70418205
RS
1039 ;; If we got a mouse click, maybe it was read with the buffer
1040 ;; it was clicked on. If so, that buffer, not the current one,
1041 ;; is in isearch mode. So end the search in that buffer.
285eb5ef
RS
1042 (if (and (listp (aref key index))
1043 (setq window (posn-window (event-start (aref key index))))
70418205
RS
1044 (windowp window))
1045 (save-excursion
1046 (set-buffer (window-buffer window))
1047 (isearch-done))
1048 (isearch-done))))
35a4d143
RS
1049 (t;; otherwise nil
1050 (isearch-process-search-string (this-command-keys)
1051 (this-command-keys)))))
8e1cae6d 1052
8e1cae6d
JB
1053(defun isearch-quote-char ()
1054 "Quote special characters for incremental search."
1055 (interactive)
1056 (isearch-process-search-char (read-quoted-char (isearch-message t))))
1057
8e1cae6d
JB
1058(defun isearch-return-char ()
1059 "Convert return into newline for incremental search.
1060Obsolete."
1061 (interactive)
1062 (isearch-process-search-char ?\n))
1063
8e1cae6d 1064(defun isearch-printing-char ()
b68c164d 1065 "Add this ordinary printing character to the search string and search."
8e1cae6d 1066 (interactive)
08200510 1067 (isearch-process-search-char (isearch-last-command-char)))
8e1cae6d
JB
1068
1069(defun isearch-whitespace-chars ()
08200510 1070 "Match all whitespace chars, if in regexp mode.
b68c164d 1071If you want to search for just a space, type C-q SPC."
8e1cae6d 1072 (interactive)
08200510 1073 (if isearch-regexp
c5f6b356 1074 (if (and search-whitespace-regexp (not isearch-within-brackets))
08200510
RS
1075 (isearch-process-search-string search-whitespace-regexp " ")
1076 (isearch-printing-char))
1077 (progn
eb8c3be9 1078 ;; This way of doing word search doesn't correctly extend current search.
08200510
RS
1079 ;; (setq isearch-word t)
1080 ;; (setq isearch-adjusted t)
1081 ;; (goto-char isearch-barrier)
1082 (isearch-printing-char))))
8e1cae6d
JB
1083
1084(defun isearch-process-search-char (char)
1085 ;; Append the char to the search string, update the message and re-search.
08200510
RS
1086 (isearch-process-search-string
1087 (isearch-char-to-string char)
1088 (isearch-text-char-description char)))
8e1cae6d
JB
1089
1090(defun isearch-process-search-string (string message)
1091 (setq isearch-string (concat isearch-string string)
1092 isearch-message (concat isearch-message message))
1093 (isearch-search-and-update))
1094
1095\f
1096;;===========================================================
1097;; Search Ring
1098
08200510
RS
1099(defun isearch-ring-adjust1 (advance)
1100 ;; Helper for isearch-ring-adjust
1101 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
8e1cae6d
JB
1102 (length (length ring))
1103 (yank-pointer-name (if isearch-regexp
08200510 1104 'regexp-search-ring-yank-pointer
8e1cae6d
JB
1105 'search-ring-yank-pointer))
1106 (yank-pointer (eval yank-pointer-name)))
1107 (if (zerop length)
1108 ()
1109 (set yank-pointer-name
1110 (setq yank-pointer
ffbc30b2
PE
1111 (mod (+ (or yank-pointer 0)
1112 (if advance -1 1))
1113 length)))
a5dcf63f 1114 (setq isearch-string (nth yank-pointer ring)
08200510
RS
1115 isearch-message (mapconcat 'isearch-text-char-description
1116 isearch-string "")))))
1117
1118(defun isearch-ring-adjust (advance)
1119 ;; Helper for isearch-ring-advance and isearch-ring-retreat
1120 (if (cdr isearch-cmds) ;; is there more than one thing on stack?
1121 (isearch-pop-state))
1122 (isearch-ring-adjust1 advance)
8e1cae6d 1123 (isearch-push-state)
08200510
RS
1124 (if search-ring-update
1125 (progn
1126 (isearch-search)
1127 (isearch-update))
1128 (isearch-edit-string)
1129 ))
8e1cae6d
JB
1130
1131(defun isearch-ring-advance ()
1132 "Advance to the next search string in the ring."
08200510 1133 ;; This could be more general to handle a prefix arg, but who would use it.
8e1cae6d
JB
1134 (interactive)
1135 (isearch-ring-adjust 'advance))
1136
1137(defun isearch-ring-retreat ()
1138 "Retreat to the previous search string in the ring."
1139 (interactive)
1140 (isearch-ring-adjust nil))
1141
a5dcf63f
RS
1142(defun isearch-ring-advance-edit (n)
1143 "Insert the next element of the search history into the minibuffer."
1144 (interactive "p")
1145 (let* ((yank-pointer-name (if isearch-regexp
1146 'regexp-search-ring-yank-pointer
1147 'search-ring-yank-pointer))
1148 (yank-pointer (eval yank-pointer-name))
1149 (ring (if isearch-regexp regexp-search-ring search-ring))
1150 (length (length ring)))
1151 (if (zerop length)
1152 ()
1153 (set yank-pointer-name
1154 (setq yank-pointer
ffbc30b2
PE
1155 (mod (- (or yank-pointer 0) n)
1156 length)))
a5dcf63f 1157
a5dcf63f 1158 (erase-buffer)
35a4d143 1159 (insert (nth yank-pointer ring))
49c13105 1160 (goto-char (point-max)))))
a5dcf63f
RS
1161
1162(defun isearch-ring-retreat-edit (n)
1163 "Inserts the previous element of the search history into the minibuffer."
1164 (interactive "p")
1165 (isearch-ring-advance-edit (- n)))
1166
1167;;(defun isearch-ring-adjust-edit (advance)
1168;; "Use the next or previous search string in the ring while in minibuffer."
1169;; (isearch-ring-adjust1 advance)
1170;; (erase-buffer)
1171;; (insert isearch-string))
1172
1173;;(defun isearch-ring-advance-edit ()
1174;; (interactive)
1175;; (isearch-ring-adjust-edit 'advance))
1176
1177;;(defun isearch-ring-retreat-edit ()
1178;; "Retreat to the previous search string in the ring while in the minibuffer."
1179;; (interactive)
1180;; (isearch-ring-adjust-edit nil))
08200510
RS
1181
1182
1183(defun isearch-complete1 ()
1184 ;; Helper for isearch-complete and isearch-complete-edit
1185 ;; Return t if completion OK, nil if no completion exists.
1186 (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1187 (alist (mapcar (function (lambda (string) (list string))) ring))
1188 (completion-ignore-case case-fold-search)
1189 (completion (try-completion isearch-string alist)))
1190 (cond
1191 ((eq completion t)
1192 ;; isearch-string stays the same
1193 t)
1194 ((or completion ; not nil, must be a string
1195 (= 0 (length isearch-string))) ; shouldnt have to say this
1196 (if (equal completion isearch-string) ;; no extension?
1197 (if completion-auto-help
1198 (with-output-to-temp-buffer "*Isearch completions*"
1199 (display-completion-list
1200 (all-completions isearch-string alist))))
1201 (setq isearch-string completion))
1202 t)
1203 (t
1204 (message "No completion") ; waits a second if in minibuffer
1205 nil))))
1206
1207(defun isearch-complete ()
1208 "Complete the search string from the strings on the search ring.
1209The completed string is then editable in the minibuffer.
1210If there is no completion possible, say so and continue searching."
1211 (interactive)
1212 (if (isearch-complete1)
1213 (isearch-edit-string)
1214 ;; else
1215 (sit-for 1)
1216 (isearch-update)))
8e1cae6d 1217
08200510
RS
1218(defun isearch-complete-edit ()
1219 "Same as `isearch-complete' except in the minibuffer."
1220 (interactive)
1221 (setq isearch-string (buffer-string))
1222 (if (isearch-complete1)
8e1cae6d 1223 (progn
08200510
RS
1224 (erase-buffer)
1225 (insert isearch-string))))
8e1cae6d
JB
1226
1227\f
1228;;;==============================================================
1229;; The search status stack (and isearch window-local variables, not used).
08200510 1230;; Need a structure for this.
8e1cae6d
JB
1231
1232(defun isearch-top-state ()
8e1cae6d
JB
1233 (let ((cmd (car isearch-cmds)))
1234 (setq isearch-string (car cmd)
1235 isearch-message (car (cdr cmd))
1236 isearch-success (nth 3 cmd)
1237 isearch-forward (nth 4 cmd)
1238 isearch-other-end (nth 5 cmd)
08200510
RS
1239 isearch-word (nth 6 cmd)
1240 isearch-invalid-regexp (nth 7 cmd)
1241 isearch-wrapped (nth 8 cmd)
c5f6b356
RS
1242 isearch-barrier (nth 9 cmd)
1243 isearch-within-brackets (nth 10 cmd))
8e1cae6d
JB
1244 (goto-char (car (cdr (cdr cmd))))))
1245
1246(defun isearch-pop-state ()
8e1cae6d
JB
1247 (setq isearch-cmds (cdr isearch-cmds))
1248 (isearch-top-state)
1249 )
1250
1251(defun isearch-push-state ()
1252 (setq isearch-cmds
1253 (cons (list isearch-string isearch-message (point)
1254 isearch-success isearch-forward isearch-other-end
08200510 1255 isearch-word
c5f6b356
RS
1256 isearch-invalid-regexp isearch-wrapped isearch-barrier
1257 isearch-within-brackets)
8e1cae6d
JB
1258 isearch-cmds)))
1259
8e1cae6d
JB
1260\f
1261;;;==================================================================
1262;; Message string
1263
1264(defun isearch-message (&optional c-q-hack ellipsis)
1265 ;; Generate and print the message string.
1266 (let ((cursor-in-echo-area ellipsis)
1267 (m (concat
08200510 1268 (isearch-message-prefix c-q-hack ellipsis isearch-nonincremental)
8e1cae6d
JB
1269 isearch-message
1270 (isearch-message-suffix c-q-hack ellipsis)
1271 )))
1272 (if c-q-hack m (message "%s" m))))
1273
08200510 1274(defun isearch-message-prefix (&optional c-q-hack ellipsis nonincremental)
8e1cae6d
JB
1275 ;; If about to search, and previous search regexp was invalid,
1276 ;; check that it still is. If it is valid now,
1277 ;; let the message we display while searching say that it is valid.
1278 (and isearch-invalid-regexp ellipsis
1279 (condition-case ()
1280 (progn (re-search-forward isearch-string (point) t)
1281 (setq isearch-invalid-regexp nil))
1282 (error nil)))
1283 ;; If currently failing, display no ellipsis.
1284 (or isearch-success (setq ellipsis nil))
1285 (let ((m (concat (if isearch-success "" "failing ")
1286 (if isearch-wrapped "wrapped ")
08200510 1287 (if isearch-word "word " "")
8e1cae6d 1288 (if isearch-regexp "regexp " "")
08200510 1289 (if nonincremental "search" "I-search")
8e1cae6d
JB
1290 (if isearch-forward ": " " backward: ")
1291 )))
1292 (aset m 0 (upcase (aref m 0)))
1293 m))
1294
1295
1296(defun isearch-message-suffix (&optional c-q-hack ellipsis)
1297 (concat (if c-q-hack "^Q" "")
1298 (if isearch-invalid-regexp
1299 (concat " [" isearch-invalid-regexp "]")
1300 "")))
1301
1302\f
1303;;;========================================================
1304;;; Searching
1305
1306(defun isearch-search ()
1307 ;; Do the search with the current search string.
1308 (isearch-message nil t)
43873131 1309 (if (and isearch-case-fold-search search-upper-case)
b78559b0
RS
1310 (setq isearch-case-fold-search
1311 (isearch-no-upper-case-p isearch-string isearch-regexp)))
8e1cae6d
JB
1312 (condition-case lossage
1313 (let ((inhibit-quit nil)
1314 (case-fold-search isearch-case-fold-search))
1315 (if isearch-regexp (setq isearch-invalid-regexp nil))
c5f6b356 1316 (setq isearch-within-brackets nil)
8e1cae6d
JB
1317 (setq isearch-success
1318 (funcall
08200510
RS
1319 (cond (isearch-word
1320 (if isearch-forward
1321 'word-search-forward 'word-search-backward))
1322 (isearch-regexp
1323 (if isearch-forward
1324 're-search-forward 're-search-backward))
1325 (t
1326 (if isearch-forward 'search-forward 'search-backward)))
8e1cae6d
JB
1327 isearch-string nil t))
1328 (if isearch-success
1329 (setq isearch-other-end
1330 (if isearch-forward (match-beginning 0) (match-end 0)))))
1331
d32696d4 1332 (quit (isearch-unread ?\C-g)
8e1cae6d
JB
1333 (setq isearch-success nil))
1334
1335 (invalid-regexp
1336 (setq isearch-invalid-regexp (car (cdr lossage)))
c5f6b356
RS
1337 (setq isearch-within-brackets (string-match "\\`Unmatched \\["
1338 isearch-invalid-regexp))
8e1cae6d
JB
1339 (if (string-match
1340 "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1341 isearch-invalid-regexp)
f1c03125
RS
1342 (setq isearch-invalid-regexp "incomplete input")))
1343 (error
1344 ;; stack overflow in regexp search.
1345 (setq isearch-invalid-regexp (car (cdr lossage)))))
8e1cae6d
JB
1346
1347 (if isearch-success
1348 nil
1349 ;; Ding if failed this time after succeeding last time.
1350 (and (nth 3 (car isearch-cmds))
1351 (ding))
1352 (goto-char (nth 2 (car isearch-cmds)))))
1353
08200510
RS
1354
1355\f
1356;;;========================================================
1357;;; Highlighting
1358
b78559b0 1359(defvar isearch-overlay nil)
08200510 1360
b78559b0 1361(defun isearch-highlight (beg end)
82318db5 1362 (if (null search-highlight)
08200510 1363 nil
b78559b0
RS
1364 (or isearch-overlay (setq isearch-overlay (make-overlay beg end)))
1365 (move-overlay isearch-overlay beg end (current-buffer))
82318db5
RS
1366 (overlay-put isearch-overlay 'face
1367 (if (internal-find-face 'isearch nil)
1368 'isearch 'region))))
b78559b0
RS
1369
1370(defun isearch-dehighlight (totally)
1371 (if isearch-overlay
1372 (delete-overlay isearch-overlay)))
08200510
RS
1373
1374;;;===========================================================
1375;;; General utilities
1376
08200510 1377
b78559b0
RS
1378(defun isearch-no-upper-case-p (string regexp-flag)
1379 "Return t if there are no upper case chars in STRING.
1380If REGEXP-FLAG is non-nil, disregard letters preceeded by `\\' (but not `\\\\')
1381since they have special meaning in a regexp."
08200510 1382 (let ((case-fold-search nil))
b78559b0
RS
1383 (not (string-match (if regexp-flag "\\(^\\|\\\\\\\\\\|[^\\]\\)[A-Z]"
1384 "[A-Z]")
1385 string))))
08200510
RS
1386
1387
8e1cae6d 1388;;;=================================================
b78559b0 1389;; Portability functions to support various Emacs versions.
8e1cae6d 1390
08200510
RS
1391;; To quiet the byte-compiler.
1392(defvar unread-command-event)
dbc4e1c1 1393(defvar unread-command-events)
08200510 1394(defvar last-command-event)
8e1cae6d 1395
08200510
RS
1396(defun isearch-char-to-string (c)
1397 (if (integerp c)
1398 (make-string 1 c)
8d7e4e80
RS
1399 (if (and (symbolp c) (get c 'ascii-character))
1400 (make-string 1 (get c 'ascii-character))
1401 (make-string 1 (event-to-character c)))))
08200510
RS
1402
1403(defun isearch-text-char-description (c)
9d78ff8b
RS
1404 (if (and (integerp c) (or (< c ?\ ) (= c ?\^?)))
1405 (text-char-description c)
1406 (isearch-char-to-string c)))
08200510 1407
8f90f594 1408(defun isearch-unread (&rest char-or-events)
8f90f594
RS
1409 ;; General function to unread characters or events.
1410 (if isearch-gnu-emacs-events
e7cb0106
RS
1411 (setq unread-command-events
1412 (append char-or-events unread-command-events))
8f90f594 1413 (let ((char (if (cdr char-or-events)
75e86b2e
RS
1414 (progn
1415 (while (cdr char-or-events)
1416 (setq char-or-events (cdr char-or-events)))
1417 (+ 128 (car char-or-events)))
8f90f594
RS
1418 (car char-or-events))))
1419 (if isearch-event-data-type
1420 (setq unread-command-event char)
1421 (setq unread-command-char char)))))
08200510
RS
1422
1423(defun isearch-last-command-char ()
1424 ;; General function to return the last command character.
a5dcf63f 1425 (if isearch-event-data-type
08200510
RS
1426 last-command-event
1427 last-command-char))
1428
3b1e4dd1 1429;;; isearch.el ends here