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