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