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