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