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