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