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