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