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