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