1 ;;; isearch.el --- incremental search commands
4 ;; Last-Modified: 30 Jun 1992
6 ;; Copyright (C) 1985, 1986 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
26 (defvar search-last-string
"" "\
27 Last string search for by a non-regexp search command.
28 This does not include direct calls to the primitive search functions,
29 and does not include searches that are aborted.")
31 (defvar search-last-regexp
"" "\
32 Last string searched for by a regexp search command.
33 This does not include direct calls to the primitive search functions,
34 and does not include searches that are aborted.")
37 (defconst search-repeat-char ?\C-s
"\
38 *Character to repeat incremental search forwards.")
39 (defconst search-reverse-char ?\C-r
"\
40 *Character to repeat incremental search backwards.")
41 (defconst search-exit-char ?\C-m
"\
42 *Character to exit incremental search.")
43 (defconst search-delete-char ?
\177 "\
44 *Character to delete from incremental search string.")
45 (defconst search-quote-char ?\C-q
"\
46 *Character to quote special characters for incremental search.")
47 (defconst search-yank-word-char ?\C-w
"\
48 *Character to pull next word from buffer into search string.")
49 (defconst search-yank-line-char ?\C-y
"\
50 *Character to pull rest of line from buffer into search string.")
51 (defconst search-ring-advance-char ?\M-n
"\
52 *Character to pull next (more recent) search string from the ring of same.")
53 (defconst search-ring-retreat-char ?\M-p
"\
54 *Character to pull previous (older) search string from the ring of same.")
56 (defconst search-exit-option t
"\
57 *Non-nil means random control characters terminate incremental search.")
59 (defvar search-slow-window-lines
1 "\
60 *Number of lines in slow search display windows.
61 These are the short windows used during incremental search on slow terminals.
62 Negative means put the slow search window at the top (normally it's at bottom)
63 and the value is minus the number of lines.")
65 (defvar search-slow-speed
1200 "\
66 *Highest terminal speed at which to use \"slow\" style incremental search.
67 This is the style where a one-line window is created to show the line
68 that the search has reached.")
70 (defconst search-upper-case t
71 "*Non-nil means an upper-case letter as search input means case-sensitive.
72 Any upper-case letter given explicitly as input to the incremental search
73 has the effect of turning off `case-fold-search' for the rest of this search.
74 Deleting the letter from the search string cancels the effect.")
76 (fset 'search-forward-regexp
're-search-forward
)
77 (fset 'search-backward-regexp
're-search-backward
)
79 (defvar search-ring nil
80 "List of recent non-regexp incremental searches.
81 Each element is a cons cell of the form (STRING . UPPERCASE-FLAG).")
83 (defvar regexp-search-ring nil
84 "List of recent regexp incremental searches.
85 Each element is a cons cell of the form (STRING . UPPERCASE-FLAG).")
87 (defconst search-ring-max
16
88 "*Maximum length of search ring before oldest elements are thrown away.")
90 (defvar search-ring-yank-pointer nil
91 "The tail of the search ring whose car is the last thing searched for.")
93 (defvar regexp-search-ring-yank-pointer nil
94 "The tail of the regular expression search ring whose car is the last
98 (defun isearch-forward ()
99 "Do incremental search forward.
100 As you type characters, they add to the search string and are found.
101 Type Delete to cancel characters from end of search string.
102 Type RET to exit, leaving point at location found.
103 Type C-s to search again forward, C-r to search again backward.
104 Type C-w to yank word from buffer onto end of search string and search for it.
105 Type C-y to yank rest of line onto end of search string, etc.
106 Type C-q to quote control character to search for it.
107 Other control and meta characters terminate the search
108 and are then executed normally.
109 The above special characters are mostly controlled by parameters;
110 do M-x apropos on search-.*-char to find them.
111 C-g while searching or when search has failed
112 cancels input back to what has been found successfully.
113 C-g when search is successful aborts and moves point to starting point."
116 (define-key global-map
"\C-s" 'isearch-forward
)
118 (defun isearch-forward-regexp ()
119 "Do incremental search forward for regular expression.
120 Like ordinary incremental search except that your input
121 is treated as a regexp. See \\[isearch-forward] for more info."
124 (define-key esc-map
"\C-s" 'isearch-forward-regexp
)
126 (defun isearch-backward ()
127 "Do incremental search backward.
128 See \\[isearch-forward] for more information."
131 (define-key global-map
"\C-r" 'isearch-backward
)
133 (defun isearch-backward-regexp ()
134 "Do incremental search backward for regular expression.
135 Like ordinary incremental search except that your input
136 is treated as a regexp. See \\[isearch-forward] for more info."
139 (define-key esc-map
"\C-r" 'isearch-backward-regexp
)
142 ;; This function does all the work of incremental search.
143 ;; The functions attached to ^R and ^S are trivial,
144 ;; merely calling this one, but they are always loaded by default
145 ;; whereas this file can optionally be autoloadable.
146 ;; This is the only entry point in this file.
148 ;; OP-FUN is a function to be called after each input character is processed.
149 ;; (It is not called after characters that exit the search.)
151 (defun isearch (forward &optional regexp op-fun
)
152 (let ((search-string "")
154 ;; List of previous states during this search.
156 ;; t means search is currently successful.
158 ;; Set once the search has wrapped around the end of the buffer.
160 ;; Nominal starting point for searching
161 ;; Usually this is the same as the opoint,
162 ;; but it is changed by wrapping
163 ;; and also by repeating the search.
165 ;; Set temporarily when adding a character to a regexp
166 ;; enables it to match more rather than fewer places in the buffer.
168 ;; Set temporarily by yanking text into the search string.
171 ;; non-nil means an explicit uppercase letter seen in the input
173 ;; Non-nil means start using a small window
174 ;; if the search moves outside what is currently on the frame.
175 (slow-terminal-mode (and (<= baud-rate search-slow-speed
)
177 (* 4 search-slow-window-lines
))))
178 ;; t means a small window is currently in use.
179 (small-window nil
) ;if t, using a small window
180 ;; These variables preserve information from the small window
181 ;; through exit from the save-window-excursion.
184 ;; Point is at one end of the last match.
185 ;; This variable records the other end of that match.
187 ;; Value of point at start of search,
188 ;; for moving the cursor back on quitting.
190 (inhibit-quit t
) ;Prevent ^G from quitting, so we can read it.
191 ;; The frame we're working on; if this changes, we exit isearch.
192 (frame (if (fboundp 'selected-frame
) (selected-frame))))
195 (save-window-excursion
198 (or (and (numberp unread-command-char
) (>= unread-command-char
0))
200 (or (input-pending-p)
202 (if (and slow-terminal-mode
203 (not (or small-window
(pos-visible-in-window-p))))
205 (setq small-window t
)
206 (setq found-point
(point))
207 (move-to-window-line 0)
208 (let ((window-min-height 1))
209 (split-window nil
(if (< search-slow-window-lines
0)
210 (1+ (- search-slow-window-lines
))
212 (1+ search-slow-window-lines
)))))
213 (if (< search-slow-window-lines
0)
214 (progn (vertical-motion (- 1 search-slow-window-lines
))
215 (set-window-start (next-window) (point))
216 (set-window-hscroll (next-window)
218 (set-window-hscroll (selected-window) 0))
220 (goto-char found-point
)))))
221 (let ((char (if quit-flag
224 (setq quit-flag nil liberalized nil yank-flag nil
)
225 (cond ((and (or (not (integerp char
))
227 (not (= char search-ring-advance-char
))
228 (not (= char search-ring-retreat-char
))))
230 (setq unread-command-char char
)
231 (throw 'search-done t
))
233 ;; If the user switches to a different frame, exit.
234 ((not (eq frame last-event-frame
))
235 (setq unread-command-char char
)
236 (throw 'search-done t
))
238 ((eq char search-exit-char
)
239 ;; RET means exit search normally.
240 ;; Except, if first thing typed, it means do nonincremental
241 (if (= 0 (length search-string
))
242 (nonincremental-search forward regexp
))
243 (throw 'search-done t
))
245 ;; ^G means the user tried to quit.
249 ;; If search is successful, move back to starting point
250 ;; and really do quit.
251 (progn (goto-char opoint
)
253 ;; If search is failing, rub out until it is once more
255 (while (not success
) (isearch-pop))))
256 ((or (eq char search-repeat-char
)
257 (eq char search-reverse-char
))
258 (if (eq forward
(eq char search-repeat-char
))
259 ;; C-s in forward or C-r in reverse.
260 (if (equal search-string
"")
261 ;; If search string is empty, use last one.
262 (isearch-get-string-from-ring)
263 ;; If already have what to search for, repeat it.
265 (progn (goto-char (if forward
(point-min) (point-max)))
267 ;; C-s in reverse or C-r in forward, change direction.
268 (setq forward
(not forward
)))
269 (setq barrier
(point)) ; For subsequent \| if regexp.
271 (or (equal search-string
"")
273 ;; If repeating a search that found an empty string,
274 ;; ensure we advance. Test history to make sure we
275 ;; actually have done a search already; otherwise,
276 ;; the match data will be random.
277 (if (and (cdr history
)
278 (= (match-end 0) (match-beginning 0)))
279 (forward-char (if forward
1 -
1)))
281 (isearch-push-state))
282 ((= char search-delete-char
)
283 ;; Rubout means discard last input item and move point
284 ;; back. If buffer is empty, just beep.
285 (if (null (cdr history
))
288 ((= char search-ring-advance-char
)
291 (let ((length (length regexp-search-ring
)))
294 (setq regexp-search-ring-yank-pointer
295 (nthcdr (%
(+ 1 (- length
(length regexp-search-ring-yank-pointer
)))
298 (isearch-get-string-from-ring)))
299 (let ((length (length search-ring
)))
302 (setq search-ring-yank-pointer
303 (nthcdr (%
(+ 1 (- length
(length search-ring-yank-pointer
)))
306 (isearch-get-string-from-ring))))
309 ((= char search-ring-retreat-char
)
312 (let ((length (length regexp-search-ring
)))
315 (setq regexp-search-ring-yank-pointer
316 (nthcdr (%
(+ (- length
(length regexp-search-ring-yank-pointer
))
320 (isearch-get-string-from-ring)))
321 (let ((length (length search-ring
)))
324 (setq search-ring-yank-pointer
325 (nthcdr (%
(+ (- length
(length search-ring-yank-pointer
))
329 (isearch-get-string-from-ring))))
333 (cond ((or (eq char search-yank-word-char
)
334 (eq char search-yank-line-char
))
335 ;; ^W means gobble next word from buffer.
336 ;; ^Y means gobble rest of line from buffer.
337 (let ((word (save-excursion
338 (and (not forward
) other-end
339 (goto-char other-end
))
343 (if (eq char search-yank-line-char
)
348 (setq word
(regexp-quote word
)))
349 (setq search-string
(concat search-string word
)
351 (concat search-message
352 (mapconcat 'text-char-description
354 ;; Don't move cursor in reverse search.
356 ;; Any other control char =>
357 ;; unread it and exit the search normally.
358 ((and search-exit-option
359 (/= char search-quote-char
)
364 (setq unread-command-char char
)
365 (throw 'search-done t
))
367 ;; Any other character => add it to the
368 ;; search string and search.
369 (cond ((= char search-quote-char
)
370 (setq char
(read-quoted-char
371 (isearch-message t
))))
373 ;; RET translates to newline.
375 (setq search-string
(concat search-string
376 (char-to-string char
))
377 search-message
(concat search-message
378 (text-char-description char
))
379 uppercase-flag
(or uppercase-flag
380 (not (= char
(downcase char
)))))))
381 (if (and (not success
)
382 ;; unsuccessful regexp search may become
383 ;; successful by addition of characters which
384 ;; make search-string valid
387 ;; Check for chars that can make a regexp more liberal.
388 ;; They can make a regexp match sooner
389 ;; or make it succeed instead of failing.
390 ;; So go back to place last successful search started
391 ;; or to the last ^S/^R (barrier), whichever is nearer.
393 (cond ((and (memq char
'(?
* ??
))
394 ;; Don't treat *, ? as special
395 ;; within [] or after \.
396 (not (nth 6 (car history
))))
398 ;; This used to use element 2
399 ;; in a reverse search, but it seems that 5
400 ;; (which is the end of the old match)
401 ;; is better in that case too.
402 (let ((cs (nth 5 ; old other-end.
403 (car (cdr history
)))))
404 ;; (car history) is after last search;
405 ;; (car (cdr history)) is from before it.
406 (setq cs
(or cs barrier
))
413 (goto-char barrier
))))
414 ;; Turn off case-sensitivity if string requests it.
415 (let ((case-fold-search
416 (and case-fold-search
417 (not (and uppercase-flag
418 search-upper-case
)))))
419 ;; In reverse search, adding stuff at
420 ;; the end may cause zero or many more chars to be
421 ;; matched, in the string following point.
422 ;; Allow all those possibilities without moving point as
423 ;; long as the match does not extend past search origin.
424 (if (and (not forward
) (not liberalized
)
426 (looking-at (if regexp search-string
427 (regexp-quote search-string
)))
430 ;; Used to have (min opoint barrier)
431 ;; instead of barrier.
432 ;; This lost when wrapping.
433 (<= (match-end 0) barrier
)))
434 (setq success t invalid-regexp nil
435 other-end
(match-end 0))
436 ;; Not regexp, not reverse, or no match at point.
437 (if (and other-end
(not liberalized
))
438 (goto-char (if forward other-end
439 ;; Used to have opoint inside the min.
440 ;; This lost when wrapping.
441 (min barrier
(1+ other-end
)))))
443 (isearch-push-state))))
444 (if op-fun
(funcall op-fun
))))
445 (setq found-start
(window-start (selected-window)))
446 (setq found-point
(point)))
447 (if (> (length search-string
) 0)
448 (if (and regexp
(not (member search-string regexp-search-ring
)))
450 (setq regexp-search-ring
(cons (cons search-string uppercase-flag
)
452 regexp-search-ring-yank-pointer regexp-search-ring
)
453 (if (> (length regexp-search-ring
) regexp-search-ring-max
)
454 (setcdr (nthcdr (1- search-ring-max
) regexp-search-ring
) nil
)))
455 (if (not (member search-string search-ring
))
457 (setq search-ring
(cons (cons search-string uppercase-flag
)
459 search-ring-yank-pointer search-ring
)
460 (if (> (length search-ring
) search-ring-max
)
461 (setcdr (nthcdr (1- search-ring-max
) search-ring
) nil
))))))
462 ;; If we displayed a single-line window, set point in this window.
464 (goto-char found-point
))
465 ;; If there was movement, mark the starting position.
466 ;; Maybe should test difference between and set mark iff > threshold.
467 (if (/= (point) opoint
)
471 ;; Exiting the save-window-excursion clobbers this; restore it.
472 (set-window-start (selected-window) found-start t
))))
474 (defun isearch-message (&optional c-q-hack ellipsis
)
475 ;; If about to search, and previous search regexp was invalid,
476 ;; check that it still is. If it is valid now,
477 ;; let the message we display while searching say that it is valid.
478 (and invalid-regexp ellipsis
480 (progn (re-search-forward search-string
(point) t
)
481 (setq invalid-regexp nil
))
483 ;; If currently failing, display no ellipsis.
484 (or success
(setq ellipsis nil
))
485 (let ((m (concat (if success
"" "failing ")
486 (if wrapped
"wrapped ")
487 (if (or (not case-fold-search
)
488 (and uppercase-flag search-upper-case
))
490 (if regexp
"regexp " "")
492 (if forward
": " " backward: ")
494 (if c-q-hack
"^Q" "")
496 (concat " [" invalid-regexp
"]")
498 (aset m
0 (upcase (aref m
0)))
499 (let ((cursor-in-echo-area ellipsis
))
500 (if c-q-hack m
(message "%s" m
)))))
502 ;; Get the search string from the "front" of the ring of previous searches.
503 (defun isearch-get-string-from-ring ()
504 (let ((elt (car (if regexp
505 (or regexp-search-ring-yank-pointer regexp-search-ring
)
506 (or search-ring-yank-pointer search-ring
)))))
507 ;; ELT describes the most recent search or where we have rotated the ring.
509 (setq search-string
(car elt
)
510 uppercase-flag
(cdr elt
))
511 (setq search-string
"" uppercase-flag nil
)))
512 ;; Let's give this one the benefit of the doubt.
513 (setq invalid-regexp nil
)
514 (setq search-message
(mapconcat 'text-char-description search-string
"")))
516 (defun isearch-pop ()
517 (setq history
(cdr history
))
518 (let ((cmd (car history
)))
519 (setq search-string
(car cmd
)
520 search-message
(car (cdr cmd
))
523 other-end
(nth 5 cmd
)
524 invalid-regexp
(nth 6 cmd
)
527 uppercase-flag
(nth 9 cmd
))
528 (goto-char (car (cdr (cdr cmd
))))))
530 (defun isearch-push-state ()
531 (setq history
(cons (list search-string search-message
(point)
532 success forward other-end invalid-regexp
533 wrapped barrier uppercase-flag
)
536 (defun isearch-search ()
537 (let ((case-fold-search
538 (and case-fold-search
539 (not (and uppercase-flag
540 search-upper-case
)))))
541 (isearch-message nil t
)
542 (condition-case lossage
543 (let ((inhibit-quit nil
))
544 (if regexp
(setq invalid-regexp nil
))
548 (if forward
're-search-forward
're-search-backward
)
549 (if forward
'search-forward
'search-backward
))
550 search-string nil t
))
553 (if forward
(match-beginning 0) (match-end 0)))))
554 (quit (setq unread-command-char ?\C-g
)
556 (invalid-regexp (setq invalid-regexp
(car (cdr lossage
)))
557 (if (string-match "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
559 (setq invalid-regexp
"incomplete input"))))
562 ;; Ding if failed this time after succeeding last time.
563 (and (nth 3 (car history
))
565 (goto-char (nth 2 (car history
))))))
567 ;; This is called from incremental-search
568 ;; if the first input character is the exit character.
569 ;; The interactive-arg-reader uses free variables `forward' and `regexp'
570 ;; which are bound by `incremental-search'.
572 ;; We store the search string in `search-string'
573 ;; which has been bound already by `incremental-search'
574 ;; so that, when we exit, it is copied into `search-last-string'.
576 (defun nonincremental-search (forward regexp
)
577 (let (message char function string inhibit-quit
)
578 (let ((cursor-in-echo-area t
))
579 ;; Prompt assuming not word search,
580 (setq message
(if regexp
581 (if forward
"Regexp search: "
582 "Regexp search backward: ")
583 (if forward
"Search: " "Search backward: ")))
584 (message "%s" message
)
585 ;; Read 1 char and switch to word search if it is ^W.
586 (setq char
(read-event)))
587 (if (and (numberp char
) (eq char search-yank-word-char
))
588 (setq message
(if forward
"Word search: " "Word search backward: "))
589 ;; Otherwise let that 1 char be part of the search string.
590 (setq unread-command-char char
))
592 (if (eq char search-yank-word-char
)
593 (if forward
'word-search-forward
'word-search-backward
)
595 (if forward
're-search-forward
're-search-backward
)
596 (if forward
'search-forward
'search-backward
))))
597 ;; Read the search string with corrected prompt.
598 (setq string
(read-string message
))
599 ;; Empty means use default.
600 (if (= 0 (length string
))
601 (setq string search-last-string
)
602 ;; Set last search string now so it is set even if we fail.
603 (setq search-last-string string
))
604 ;; Since we used the minibuffer, we should be available for redo.
605 (setq command-history
(cons (list function string
) command-history
))
606 ;; Go ahead and search.
607 (funcall function string
)))
609 ;;; isearch.el ends here