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