961a0b76f2e2229150e516d516a0a9edd17e6bd2
[chise/xemacs-chise.git.1] / lisp / isearch-mode.el
1 ;;; isearch-mode.el --- Incremental search minor mode.
2
3 ;; Copyright (C) 1992, 1993, 1997 Free Software Foundation, Inc.
4
5 ;; Author: Daniel LaLiberte <liberte@cs.uiuc.edu>
6 ;; Maintainer: XEmacs Development Team
7 ;; Keywords: extensions, dumped
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; see the file COPYING.  If not, write to the 
23 ;; Free Software Foundation, 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Synched up with: Not synched with FSF.
27
28 ;;; Commentary:
29
30 ;; LCD Archive Entry:
31 ;; isearch-mode|Daniel LaLiberte|liberte@cs.uiuc.edu
32 ;; |A minor mode replacement for isearch.el.
33
34 ;;====================================================================
35 ;; Instructions
36
37 ;; Searching with isearch-mode.el should work just like isearch.el,
38 ;; except it is done in a temporary minor mode that terminates when
39 ;; you finish searching.
40
41 ;; Semi-modal searching is supported, using a recursive edit. If
42 ;; isearching is started non-interactively by calling one of the
43 ;; isearch commands (e.g. (isearch-forward), but not like gnus does
44 ;; it: (call-interactively 'isearch-forward)), isearch-mode does not
45 ;; return until the search is completed.  You should still be able
46 ;; switch buffers, so be careful not to get things confused.
47
48 ;; The key bindings active within isearch-mode are defined below in
49 ;; `isearch-mode-map' which is given bindings close to the default
50 ;; characters of isearch.el for version 19.  With `isearch-mode',
51 ;; however, you can bind multi-character keys and it should be easier
52 ;; to add new commands.  One bug though: keys with meta-prefix cannot
53 ;; be longer than two chars.  Also see minibuffer-local-isearch-map
54 ;; for bindings active during `isearch-edit-string'.
55
56 ;; The search ring and completion commands automatically put you in
57 ;; the minibuffer to edit the string.  This gives you a chance to
58 ;; modify the search string before executing the search.  There are
59 ;; three commands to terminate the editing: C-s and C-r exit the
60 ;; minibuffer and search forward and reverse respectively, while C-m
61 ;; exits and does a nonincremental search.
62
63 ;; Exiting immediately from isearch uses isearch-edit-string instead
64 ;; of nonincremental-search, if search-nonincremental-instead is non-nil.
65 ;; The name of this option should probably be changed if we decide to
66 ;; keep the behavior.  One difference is that isearch-edit-string does
67 ;; not support word search yet; perhaps isearch-mode should support it
68 ;; even for incremental searches, but how?
69
70 ;;====================================================================
71 ;;; Change History:
72
73 ;; Header: /import/kaplan/kaplan/liberte/Isearch/RCS/isearch-mode.el,v 1.3 92/06/29 13:10:08 liberte Exp Locker: liberte 
74 ;; Log: isearch-mode.el,v 
75 ;;
76 ;; 20-aug-92  Hacked by jwz for Lucid Emacs 19.3.
77 ;;
78 ;; Revision 1.3  92/06/29  13:10:08  liberte
79 ;; Moved modal isearch-mode handling into isearch-mode.
80 ;; Got rid of buffer-local isearch variables.
81 ;; isearch-edit-string used by ring adjustments, completion, and
82 ;; nonincremental searching.  C-s and C-r are additional exit commands.
83 ;; Renamed all regex to regexp.
84 ;; Got rid of found-start and found-point globals.
85 ;; Generalized handling of upper-case chars.
86  
87 ;; Revision 1.2  92/05/27  11:33:57  liberte
88 ;; Emacs version 19 has a search ring, which is supported here.
89 ;; Other fixes found in the version 19 isearch are included here.
90 ;;
91 ;; Also see variables search-caps-disable-folding,
92 ;; search-nonincremental-instead, search-whitespace-regexp, and
93 ;; commands isearch-toggle-regexp, isearch-edit-string.
94 ;;
95 ;; semi-modal isearching is supported.
96
97 ;; Changes for 1.1
98 ;; 3/18/92 Fixed invalid-regexp.
99 ;; 3/18/92 Fixed yanking in regexps.
100
101 ;;; Code:
102
103 (defgroup isearch nil
104   "Incremental search"
105   :prefix "search-"
106   :group 'matching)
107
108
109 (defun isearch-char-to-string (c)
110   (if (eventp c) 
111       (make-string 1 (event-to-character c nil nil t))
112     (make-string 1 c)))
113
114 ;(defun isearch-text-char-description (c)
115 ;  (isearch-char-to-string c))
116
117 (define-function 'isearch-text-char-description 'text-char-description)
118
119 \f
120 ;;;=========================================================================
121 ;;; User-accessible variables
122
123 (defvar search-last-string ""
124   "Last string search for by a search command.
125 This does not include direct calls to the primitive search functions,
126 and does not include searches that are aborted.")
127
128 (defvar search-last-regexp ""
129   "Last string searched for by a regexp search command.
130 This does not include direct calls to the primitive search functions,
131 and does not include searches that are aborted.")
132
133 (defconst search-exit-option t
134   "Non-nil means random control characters terminate incremental search.")
135
136 (defcustom search-slow-window-lines 1
137   "*Number of lines in slow search display windows.
138 These are the short windows used during incremental search on slow terminals.
139 Negative means put the slow search window at the top (normally it's at bottom)
140 and the value is minus the number of lines."
141   :type 'integer
142   :group 'isearch)
143
144 (defcustom search-slow-speed 1200
145   "*Highest terminal speed at which to use \"slow\" style incremental search.
146 This is the style where a one-line window is created to show the line
147 that the search has reached."
148   :type 'integer
149   :group 'isearch)
150
151 (defcustom search-nonincremental-instead t
152   "*If non-nil, do a nonincremental search instead if exiting immediately."
153   :type 'boolean
154   :group 'isearch)
155   
156 (defcustom search-whitespace-regexp "\\(\\s \\|[\n\r]\\)+"
157   "*If non-nil, regular expression to match a sequence of whitespace chars."
158   :type 'regexp
159   :group 'isearch)
160
161 ;;;==================================================================
162 ;;; Search ring.
163
164 (defvar search-ring nil
165   "List of search string sequences.")
166 (defvar regexp-search-ring nil
167   "List of regular expression search string sequences.")
168
169 (defcustom search-ring-max 16
170   "*Maximum length of search ring before oldest elements are thrown away."
171   :type 'integer
172   :group 'isearch)
173 (defcustom regexp-search-ring-max 16
174   "*Maximum length of regexp search ring before oldest elements are thrown away."
175   :type 'integer
176   :group 'isearch)
177
178 (defvar search-ring-yank-pointer nil
179   "The tail of the search ring whose car is the last thing searched for.")
180 (defvar regexp-search-ring-yank-pointer nil
181   "The tail of the regular expression search ring whose car is the last
182 thing searched for.")
183
184 ;;;====================================================
185 ;;; Define isearch-mode keymap.
186
187 (defvar isearch-mode-map 
188   (let ((map (make-keymap)))
189     (set-keymap-name map 'isearch-mode-map)
190
191     ;; Bind all printing characters to `isearch-printing-char'.
192     ;; This isn't normally necessary, but if a printing character were 
193     ;; bound to something other than self-insert-command in global-map, 
194     ;; then it would terminate the search and be executed without this.
195     (let ((i 32)
196           (str (make-string 1 0)))
197       (while (< i 127)
198         (aset str 0 i)
199         (define-key map str 'isearch-printing-char)
200         (setq i (1+ i))))
201     (define-key map "\t" 'isearch-printing-char)
202
203     ;; Several non-printing chars change the searching behavior.
204     ;;
205     (define-key map "\C-s" 'isearch-repeat-forward)
206     (define-key map "\M-\C-s" 'isearch-repeat-forward)
207     (define-key map "\C-r" 'isearch-repeat-backward)
208     (define-key map "\C-g" 'isearch-abort)
209
210     (define-key map "\C-q" 'isearch-quote-char)
211
212     (define-key map "\C-m" 'isearch-exit)
213     (define-key map "\C-j" 'isearch-printing-char)
214     (define-key map "\t" 'isearch-printing-char)
215
216     (define-key map "\C-w" 'isearch-yank-word)
217     (define-key map "\C-y" 'isearch-yank-line)
218     (define-key map "\M-y" 'isearch-yank-kill)
219
220     ;; Define keys for regexp chars * ? |
221     (define-key map "*" 'isearch-*-char)
222     (define-key map "?" 'isearch-*-char)
223     (define-key map "|" 'isearch-|-char)
224
225     ;; Some bindings you may want to put in your isearch-mode-hook.
226     ;; Suggest some alternates...
227     ;; (define-key map "\C-t" 'isearch-toggle-regexp)
228     ;; (define-key map "\C-^" 'isearch-edit-string)
229
230     ;; delete and backspace delete backward, f1 is help, and C-h can be either
231     (define-key map 'delete 'isearch-delete-char)
232     (define-key map 'backspace 'isearch-delete-char)
233     (define-key map '(control h) 'isearch-help-or-delete-char)
234     (define-key map 'f1 'isearch-mode-help)
235     (define-key map 'help 'isearch-mode-help)
236
237     (define-key map "\M-n" 'isearch-ring-advance)
238     (define-key map "\M-p" 'isearch-ring-retreat)
239     (define-key map "\M- " 'isearch-whitespace-chars)
240     (define-key map "\M-\t" 'isearch-complete)
241
242     (define-key map 'button2 'isearch-yank-x-selection)
243
244     map)
245   "Keymap for isearch-mode.")
246
247 (defvar minibuffer-local-isearch-map 
248   (let ((map (make-sparse-keymap)))
249     ;; #### - this should also be minor-mode-ified
250     (set-keymap-parents map (list minibuffer-local-map))
251     (set-keymap-name map 'minibuffer-local-isearch-map)
252
253     ;;#### This should just arrange to use the usual Emacs minibuffer histories
254     (define-key map "\r" 'isearch-nonincremental-exit-minibuffer)
255     (define-key map "\M-n" 'isearch-ring-advance-edit)
256     (define-key map "\M-p" 'isearch-ring-retreat-edit)
257     (define-key map "\M-\t" 'isearch-complete-edit)
258     (define-key map "\C-s" 'isearch-forward-exit-minibuffer)
259     (define-key map "\C-r" 'isearch-reverse-exit-minibuffer)
260     map)
261   "Keymap for editing isearch strings in the minibuffer.")
262
263 ;;;========================================================
264 ;; Internal variables declared globally for byte-compiler.
265 ;; These are all bound locally while editing the search string.
266
267 (defvar isearch-forward nil)    ; Searching in the forward direction.
268 (defvar isearch-regexp nil)     ; Searching for a regexp.
269 (defvar isearch-word nil)       ; Searching for words.
270
271 (defvar isearch-cmds nil)   ; Stack of search status sets.
272 (defvar isearch-string "")  ; The current search string.
273 (defvar isearch-message "") ; text-char-description version of isearch-string
274
275 (defvar isearch-success t)              ; Searching is currently successful.
276 (defvar isearch-invalid-regexp nil)     ; Regexp not well formed.
277 (defvar isearch-other-end nil)  ; Start (end) of match if forward (backward).
278 (defvar isearch-wrapped nil)    ; Searching restarted from the top (bottom).
279 (defvar isearch-barrier 0)
280 (defvar isearch-just-started nil)
281 (defvar isearch-buffer nil)     ; the buffer we've frobbed the keymap of
282
283 (defvar isearch-case-fold-search nil)
284
285 (defvar isearch-adjusted nil)
286 (defvar isearch-slow-terminal-mode nil)
287 ;;; If t, using a small window.
288 (defvar isearch-small-window nil)
289 (defvar isearch-opoint 0)
290 ;;; The window configuration active at the beginning of the search.
291 (defvar isearch-window-configuration nil)
292 (defvar isearch-selected-frame nil)
293
294 ;; Flag to indicate a yank occurred, so don't move the cursor.
295 (defvar isearch-yank-flag nil)
296
297 ;;; A function to be called after each input character is processed.
298 ;;; (It is not called after characters that exit the search.)
299 ;;; It is only set from an optional argument to `isearch-mode'.
300 (defvar isearch-op-fun nil)
301
302 ;;;  Is isearch-mode in a recursive edit for modal searching.
303 (defvar isearch-recursive-edit nil)
304
305 ;;; Should isearch be terminated after doing one search?
306 (defvar isearch-nonincremental nil)
307
308 ;; New value of isearch-forward after isearch-edit-string.
309 (defvar isearch-new-forward nil)
310
311
312 (defvar isearch-mode-hook nil
313   "Function(s) to call after starting up an incremental search.")
314
315 (defvar isearch-mode-end-hook nil
316   "Function(s) to call after terminating an incremental search.")
317
318 ;;;==============================================================
319 ;; Minor-mode-alist changes - kind of redundant with the
320 ;; echo area, but if isearching in multiple windows, it can be useful.
321
322 (add-minor-mode 'isearch-mode 'isearch-mode)
323
324 (defvar isearch-mode nil)
325 (make-variable-buffer-local 'isearch-mode)
326
327 ;;;===============================================================
328 ;;; Entry points to isearch-mode.
329 ;;; These four functions should replace those in loaddefs.el
330 ;;; An alternative is to fset isearch-forward etc to isearch-mode,
331 ;;; and look at the last command to set the options accordingly.
332
333 (defun isearch-forward (&optional regexp-p)
334   "Do incremental search forward.
335 With a prefix argument, do an incremental regular expression search instead.
336 \\<isearch-mode-map>
337 As you type characters, they add to the search string and are found.
338 The following non-printing keys are bound in `isearch-mode-map'.  
339
340 Type \\[isearch-delete-char] to cancel characters from end of search string.
341 Type \\[isearch-exit] to exit, leaving point at location found.
342 Type LFD (C-j) to match end of line.
343 Type \\[isearch-repeat-forward] to search again forward,\
344  \\[isearch-repeat-backward] to search again backward.
345 Type \\[isearch-yank-word] to yank word from buffer onto end of search\
346  string and search for it.
347 Type \\[isearch-yank-line] to yank rest of line onto end of search string\
348  and search for it.
349 Type \\[isearch-quote-char] to quote control character to search for it.
350 Type \\[isearch-whitespace-chars] to match all whitespace chars in regexp.
351 \\[isearch-abort] while searching or when search has failed cancels input\
352  back to what has
353  been found successfully.
354 \\[isearch-abort] when search is successful aborts and moves point to\
355  starting point.
356
357 Also supported is a search ring of the previous 16 search strings.
358 Type \\[isearch-ring-advance] to search for the next item in the search ring.
359 Type \\[isearch-ring-retreat] to search for the previous item in the search\
360  ring.
361 Type \\[isearch-complete] to complete the search string using the search ring.
362
363 The above keys are bound in the isearch-mode-map.  To change the keys which
364  are special to isearch-mode, simply change the bindings in that map.
365
366 Other control and meta characters terminate the search
367  and are then executed normally (depending on `search-exit-option').
368
369 If this function is called non-interactively, it does not return to
370 the calling function until the search is done.
371
372 The bindings, more precisely:
373 \\{isearch-mode-map}"
374
375 ;; Non-standard bindings
376 ;; Type \\[isearch-toggle-regexp] to toggle regular expression with normal searching.
377 ;; Type \\[isearch-edit-string] to edit the search string in the minibuffer.
378 ;;  Terminate editing and return to incremental searching with CR.
379
380   (interactive "_P")
381   (isearch-mode t (not (null regexp-p)) nil (not (interactive-p))))
382
383 (defun isearch-forward-regexp ()
384   "\
385 Do incremental search forward for regular expression.
386 Like ordinary incremental search except that your input
387 is treated as a regexp.  See \\[isearch-forward] for more info."
388   (interactive "_")
389   (isearch-mode t t nil (not (interactive-p))))
390
391 (defun isearch-backward (&optional regexp-p)
392   "\
393 Do incremental search backward.
394 With a prefix argument, do an incremental regular expression search instead.
395 See \\[isearch-forward] for more information."
396   (interactive "_P")
397   (isearch-mode nil (not (null regexp-p)) nil (not (interactive-p))))
398
399 (defun isearch-backward-regexp ()
400   "\
401 Do incremental search backward for regular expression.
402 Like ordinary incremental search except that your input
403 is treated as a regexp.  See \\[isearch-forward] for more info."
404   (interactive "_")
405   (isearch-mode nil t nil (not (interactive-p))))
406
407 ;; This function is way wrong, because you can't scroll the help
408 ;; screen; as soon as you press a key, it's gone.  I don't know of a
409 ;; good way to fix it, though.  -hniksic
410 (defun isearch-mode-help ()
411   (interactive "_")
412   (let ((w (selected-window)))
413     (describe-function 'isearch-forward)
414     (select-window w))
415   (isearch-update))
416
417 \f
418 ;;;==================================================================
419 ;; isearch-mode only sets up incremental search for the minor mode.
420 ;; All the work is done by the isearch-mode commands.
421
422 (defun isearch-mode (forward &optional regexp op-fun recursive-edit word-p)
423   "Start isearch minor mode.  Called by isearch-forward, etc."
424
425   (if executing-kbd-macro (setq recursive-edit nil))
426
427   (let ((inhibit-quit t)) ; don't leave things in an inconsistent state...
428
429     ;; Initialize global vars.
430     (setq isearch-buffer (current-buffer)
431           isearch-forward forward
432           isearch-regexp regexp
433           isearch-word word-p
434           isearch-op-fun op-fun
435           isearch-case-fold-search case-fold-search
436           isearch-string ""
437           isearch-message ""
438           isearch-cmds nil
439           isearch-success t
440           isearch-wrapped nil
441           isearch-barrier (point)
442           isearch-adjusted nil
443           isearch-yank-flag nil
444           isearch-invalid-regexp nil
445           isearch-slow-terminal-mode (and (<= (device-baud-rate)
446                                               search-slow-speed)
447                                           (> (window-height)
448                                              (* 4 search-slow-window-lines)))
449           isearch-other-end nil
450           isearch-small-window nil
451           isearch-just-started t
452
453           isearch-opoint (point)
454           isearch-window-configuration (current-window-configuration)
455
456           ;; #### Should we remember the old value of
457           ;; overriding-local-map?
458           overriding-local-map isearch-mode-map
459           isearch-selected-frame (selected-frame)
460
461           isearch-mode (gettext " Isearch")
462           )
463
464     ;; XEmacs change: without clearing the match data, sometimes old values
465     ;; of isearch-other-end get used.  Don't ask me why...
466     (store-match-data nil)
467
468     (add-hook 'pre-command-hook 'isearch-pre-command-hook)
469     (set-buffer-modified-p (buffer-modified-p)) ; update modeline
470     (isearch-push-state)
471
472     ) ; inhibit-quit is t before here
473
474   (isearch-update)
475   (run-hooks 'isearch-mode-hook)
476
477   ;; isearch-mode can be made modal (in the sense of not returning to 
478   ;; the calling function until searching is completed) by entering 
479   ;; a recursive-edit and exiting it when done isearching.
480   (if recursive-edit
481       (let ((isearch-recursive-edit t))
482         (recursive-edit)))
483   )
484
485
486 ;;;====================================================
487 ;; Some high level utilities.  Others below.
488
489 (defun isearch-update ()
490   ;; Called after each command to update the display.  
491   (if (null unread-command-event)
492       (progn
493         (if (not (input-pending-p))
494             (isearch-message))
495         (if (and isearch-slow-terminal-mode
496                  (not (or isearch-small-window 
497                           (pos-visible-in-window-p))))
498             (let ((found-point (point)))
499               (setq isearch-small-window t)
500               (move-to-window-line 0)
501               (let ((window-min-height 1))
502                 (split-window nil (if (< search-slow-window-lines 0)
503                                       (1+ (- search-slow-window-lines))
504                                     (- (window-height)
505                                        (1+ search-slow-window-lines)))))
506               (if (< search-slow-window-lines 0)
507                   (progn (vertical-motion (- 1 search-slow-window-lines))
508                          (set-window-start (next-window) (point))
509                          (set-window-hscroll (next-window)
510                                              (window-hscroll))
511                          (set-window-hscroll (selected-window) 0))
512                 (other-window 1))
513               (goto-char found-point)))
514         (if isearch-other-end
515             (if (< isearch-other-end (point))
516                 (isearch-highlight isearch-other-end (point))
517               (isearch-highlight (point) isearch-other-end))
518           (if (extentp isearch-extent)
519               (isearch-dehighlight nil)))
520         ))
521   (setq ;; quit-flag nil  not for isearch-mode
522    isearch-adjusted nil
523    isearch-yank-flag nil)
524   )
525
526
527 (defun isearch-done ()
528   ;; Called by all commands that terminate isearch-mode.
529   (let ((inhibit-quit t)) ; danger danger!
530     (if (and isearch-buffer (buffer-live-p isearch-buffer))
531         (save-excursion
532           ;; Some loser process filter might have switched the
533           ;; window's buffer, so be sure to set these variables back
534           ;; in the buffer we frobbed them in.  But only if the buffer
535           ;; is still alive.
536           (set-buffer isearch-buffer)
537           ;; #### Should we restore the old value of
538           ;; overriding-local-map?
539           (setq overriding-local-map nil)
540           ;; Use remove-hook instead of just setting it to our saved value
541           ;; in case some process filter has created a buffer and modified
542           ;; the pre-command-hook in that buffer...  yeah, this is obscure,
543           ;; and yeah, I was getting screwed by it. -jwz
544           (remove-hook 'pre-command-hook 'isearch-pre-command-hook)
545           (set-keymap-parents isearch-mode-map nil)
546           (setq isearch-mode nil)
547           (set-buffer-modified-p (buffer-modified-p));; update modeline
548           (isearch-dehighlight t)))
549
550     ;; it's not critical that this be inside inhibit-quit, but leaving
551     ;; things in small-window-mode would be bad.
552     (let ((found-start (window-start (selected-window)))
553           (found-point (point)))
554       (cond ((eq (selected-frame) isearch-selected-frame)
555              (set-window-configuration isearch-window-configuration)
556
557              (if isearch-small-window
558                  (goto-char found-point)
559                ;; Exiting the save-window-excursion clobbers
560                ;; window-start; restore it.
561                (set-window-start (selected-window) found-start t))))
562       ;; If there was movement, mark the starting position.
563       ;; Maybe should test difference between and set mark iff > threshold.
564       (if (and (buffer-live-p isearch-buffer)
565                (/= (point isearch-buffer) isearch-opoint))
566           (progn
567             (push-mark isearch-opoint t nil isearch-buffer)
568             (or executing-kbd-macro (> (minibuffer-depth) 0)
569                 (display-message 'command "Mark saved where search started"))))
570         )
571     (setq isearch-buffer nil)
572     ) ; inhibit-quit is t before here
573
574   (if (> (length isearch-string) 0)
575       ;; Update the ring data.
576       (if isearch-regexp 
577           (if (not (setq regexp-search-ring-yank-pointer
578                          (member isearch-string regexp-search-ring)))
579               (progn
580                 (setq regexp-search-ring
581                       (cons isearch-string regexp-search-ring)
582                       regexp-search-ring-yank-pointer regexp-search-ring)
583                 (if (> (length regexp-search-ring) regexp-search-ring-max)
584                     (setcdr (nthcdr (1- regexp-search-ring-max) regexp-search-ring)
585                             nil))))
586         (if (not (setq search-ring-yank-pointer
587                        ;; really need equal test instead of eq.
588                        (member isearch-string search-ring)))
589             (progn
590               (setq search-ring (cons isearch-string search-ring)
591                     search-ring-yank-pointer search-ring)
592               (if (> (length search-ring) search-ring-max)
593                   (setcdr (nthcdr (1- search-ring-max) search-ring) nil))))))
594
595   (run-hooks 'isearch-mode-end-hook)
596   (if isearch-recursive-edit (exit-recursive-edit)))
597
598 \f
599 ;;;====================================================
600 ;; Commands active while inside of the isearch minor mode.
601
602 (defun isearch-exit ()
603   "Exit search normally.
604 However, if this is the first command after starting incremental
605 search and `search-nonincremental-instead' is non-nil, do an
606 incremental search via `isearch-edit-string'."
607   (interactive)
608   (if (and search-nonincremental-instead 
609            (= 0 (length isearch-string)))
610       (let ((isearch-nonincremental t))
611         (isearch-edit-string))
612     (isearch-done)))
613
614
615 (defun isearch-edit-string ()
616   "Edit the search string in the minibuffer.
617 The following additional command keys are active while editing.
618 \\<minibuffer-local-isearch-map>
619 \\[exit-minibuffer] to exit editing and resume incremental searching.
620 \\[isearch-forward-exit-minibuffer] to resume isearching forward.
621 \\[isearch-backward-exit-minibuffer] to resume isearching backward.
622 \\[isearch-ring-advance-edit] to replace the search string with the next\
623  item in the search ring.
624 \\[isearch-ring-retreat-edit] to replace the search string with the next\
625  item in the search ring.
626 \\[isearch-complete-edit] to complete the search string from the search ring."
627
628   ;; Editing doesn't back up the search point.  Should it?
629   (interactive)
630
631   (condition-case nil
632       (let ((minibuffer-local-map minibuffer-local-isearch-map)
633             isearch-nonincremental      ; should search nonincrementally?
634             isearch-new-string
635             isearch-new-message
636             (isearch-new-forward isearch-forward)
637
638             ;; Locally bind all isearch global variables to protect them
639             ;; from recursive isearching.
640             (isearch-string isearch-string)
641             (isearch-message isearch-message)
642             (isearch-forward isearch-forward) ; set by commands below.
643
644             (isearch-forward isearch-forward)
645             (isearch-regexp isearch-regexp)
646             (isearch-word isearch-word)
647             (isearch-op-fun isearch-op-fun)
648             (isearch-cmds isearch-cmds)
649             (isearch-success isearch-success)
650             (isearch-wrapped isearch-wrapped)
651             (isearch-barrier isearch-barrier)
652             (isearch-adjusted isearch-adjusted)
653             (isearch-yank-flag isearch-yank-flag)
654             (isearch-invalid-regexp isearch-invalid-regexp)
655             (isearch-other-end isearch-other-end)
656             (isearch-opoint isearch-opoint)
657             (isearch-slow-terminal-mode isearch-slow-terminal-mode)
658             (isearch-small-window isearch-small-window)
659             (isearch-recursive-edit isearch-recursive-edit)
660             (isearch-window-configuration (current-window-configuration))
661             (isearch-selected-frame (selected-frame))
662             )
663         ;; Actually terminate isearching until editing is done.
664         ;; This is so that the user can do anything without failure, 
665         ;; like switch buffers and start another isearch, and return.
666 ;;      (condition-case nil
667             (isearch-done)
668           ;;#### What does this mean?  There is no such condition!
669 ;;        (exit nil))                   ; was recursive editing
670
671         (unwind-protect
672             (let ((prompt (isearch-message-prefix nil t))
673                   event)
674               ;; If the first character the user types when we prompt them
675               ;; for a string is the yank-word character, then go into
676               ;; word-search mode.  Otherwise unread that character and
677               ;; read a string the normal way.
678               (let ((cursor-in-echo-area t))
679                 (display-message 'prompt prompt)
680                 (setq event (next-command-event))
681                 (if (eq 'isearch-yank-word
682                         (lookup-key isearch-mode-map (vector event)))
683                     (setq isearch-word t)
684                   (setq unread-command-event event)))
685               (setq isearch-new-string
686 ;;                    (if (fboundp 'gmhist-old-read-from-minibuffer)
687 ;;                        ;; Eschew gmhist crockery
688 ;;                      (gmhist-old-read-from-minibuffer prompt isearch-string)
689                       (read-string
690                        prompt isearch-string
691                        't            ;does its own history (but shouldn't)
692 ;;                     (if isearch-regexp
693 ;;                         ;; The search-rings aren't exactly minibuffer
694 ;;                         ;;  histories, but they are close enough
695 ;;                         (cons 'regexp-search-ring
696 ;;                               (- (length regexp-search-ring-yank-pointer)
697 ;;                                  (length regexp-search-ring)))
698 ;;                         (cons 'search-ring
699 ;;                               (- (length search-ring-yank-pointer)
700 ;;                                  (length search-ring))))
701                        )
702 ;;                    )
703                     isearch-new-message (mapconcat
704                                          'isearch-text-char-description
705                                          isearch-new-string ""))
706               )
707           ;; Always resume isearching by restarting it.
708           (isearch-mode isearch-forward 
709                         isearch-regexp 
710                         isearch-op-fun 
711                         isearch-recursive-edit
712                         isearch-word)
713           )
714
715         ;; Copy new values in outer locals to isearch globals
716         (setq isearch-string isearch-new-string
717               isearch-message isearch-new-message
718               isearch-forward isearch-new-forward)
719
720         ;; Empty isearch-string means use default.
721         (if (= 0 (length isearch-string))
722             (setq isearch-string (if isearch-regexp search-last-regexp
723                                    search-last-string))
724           ;; Set last search string now so it is set even if we fail.
725           (if search-last-regexp
726               (setq search-last-regexp isearch-string)
727             (setq search-last-string isearch-string)))
728
729         ;; Reinvoke the pending search.
730         (isearch-push-state)
731         (isearch-search)
732         (isearch-update)
733         (if isearch-nonincremental (isearch-done)))
734
735     (quit  ; handle abort-recursive-edit
736      (isearch-abort)  ;; outside of let to restore outside global values
737      )))
738
739 (defun isearch-nonincremental-exit-minibuffer ()
740   (interactive)
741   (setq isearch-nonincremental t)
742   (exit-minibuffer))
743
744 (defun isearch-forward-exit-minibuffer ()
745   (interactive)
746   (setq isearch-new-forward t)
747   (exit-minibuffer))
748
749 (defun isearch-reverse-exit-minibuffer ()
750   (interactive)
751   (setq isearch-new-forward nil)
752   (exit-minibuffer))
753
754
755 (defun isearch-abort ()
756   "Quit incremental search mode if searching is successful, signalling quit.
757 Otherwise, revert to previous successful search and continue searching.
758 Use `isearch-exit' to quit without signalling."
759   (interactive)
760 ;;  (ding)  signal instead below, if quiting
761   (discard-input)
762   (if isearch-success
763       ;; If search is successful, move back to starting point
764       ;; and really do quit.
765       (progn (goto-char isearch-opoint)
766              (isearch-done)   ; exit isearch
767              (signal 'quit '(isearch)))  ; and pass on quit signal
768     ;; If search is failing, rub out until it is once more successful.
769     (while (not isearch-success) (isearch-pop-state))
770     (isearch-update)))
771
772
773 (defun isearch-repeat (direction)
774   ;; Utility for isearch-repeat-forward and -backward.
775   (if (eq isearch-forward (eq direction 'forward))
776       ;; C-s in forward or C-r in reverse.
777       (if (equal isearch-string "")
778           ;; If search string is empty, use last one.
779           (setq isearch-string
780                 (or (if isearch-regexp
781                         (if regexp-search-ring-yank-pointer
782                             (car regexp-search-ring-yank-pointer)
783                           (car regexp-search-ring))
784                       (if search-ring-yank-pointer
785                           (car search-ring-yank-pointer)
786                         (car search-ring)))
787                     "")
788                 isearch-message
789                 (mapconcat 'isearch-text-char-description
790                            isearch-string ""))
791         ;; If already have what to search for, repeat it.
792         (or isearch-success
793             (progn 
794
795               (goto-char (if isearch-forward (point-min) (point-max)))
796               (setq isearch-wrapped t))))
797     ;; C-s in reverse or C-r in forward, change direction.
798     (setq isearch-forward (not isearch-forward)))
799
800   (setq isearch-barrier (point)) ; For subsequent \| if regexp.
801   (if (equal isearch-string "")
802       (setq isearch-success t)
803     (if (and (equal (match-end 0) (match-beginning 0))
804              isearch-success
805              (not isearch-just-started))
806         ;; If repeating a search that found
807         ;; an empty string, ensure we advance.
808         (if (if isearch-forward (eobp) (bobp))
809             ;; nowhere to advance to, so fail (and wrap next time)
810             (progn
811               (setq isearch-success nil)
812               (and executing-kbd-macro
813                    (not defining-kbd-macro)
814                    (isearch-done))
815               (ding nil 'isearch-failed))
816           (forward-char (if isearch-forward 1 -1))
817           (isearch-search))
818       (isearch-search)))
819   (isearch-push-state)
820   (isearch-update))
821
822 (defun isearch-repeat-forward ()
823   "Repeat incremental search forwards."
824   (interactive)
825   (isearch-repeat 'forward))
826
827 (defun isearch-repeat-backward ()
828   "Repeat incremental search backwards."
829   (interactive)
830   (isearch-repeat 'backward))
831
832 (defun isearch-toggle-regexp ()
833   "Toggle regexp searching on or off."
834   ;; The status stack is left unchanged.
835   (interactive)
836   (setq isearch-regexp (not isearch-regexp))
837   (if isearch-regexp (setq isearch-word nil))
838   (isearch-update))
839
840 (defun isearch-toggle-case-fold ()
841   "Toggle case folding in searching on or off."
842   (interactive)
843   (setq isearch-case-fold-search
844         (if isearch-case-fold-search nil 'yes))
845   (message "%s%s [case %ssensitive]"
846            (isearch-message-prefix)
847            isearch-message
848            (if isearch-case-fold-search "in" ""))
849   (setq isearch-adjusted t)
850   (sit-for 1)
851   (isearch-update))
852
853 (defun isearch-delete-char ()
854   "Discard last input item and move point back.  
855 If no previous match was done, just beep."
856   (interactive)
857   (if (null (cdr isearch-cmds))
858       (ding nil 'isearch-quit)
859     (isearch-pop-state))
860   (isearch-update))
861
862 (defun isearch-help-or-delete-char ()
863   "Show Isearch help or delete backward in the search string.
864 Deletes when `delete-key-deletes-forward' is t and C-h is used for deleting
865 backwards."
866   (interactive)
867   (if (and delete-key-deletes-forward
868            (case (device-type)
869              ('tty (eq tty-erase-char ?\C-h))
870              ('x (not (x-keysym-on-keyboard-sans-modifiers-p 'backspace)))))
871       (isearch-delete-char)
872     (isearch-mode-help)))
873
874 (defun isearch-yank (chunk)
875   ;; Helper for isearch-yank-* functions.  CHUNK can be a string or a
876   ;; function.
877   (let ((word (if (stringp chunk)
878                   chunk
879                 (save-excursion
880                   (and (not isearch-forward) isearch-other-end
881                        (goto-char isearch-other-end))
882                   (buffer-substring
883                    (point)
884                    (save-excursion
885                      (funcall chunk)
886                      (point)))))))
887     ;; if configured so that typing upper-case characters turns off case
888     ;; folding, then downcase the string so that yanking an upper-case
889     ;; word doesn't mess with case-foldedness.
890     (if (and search-caps-disable-folding isearch-case-fold-search)
891         (setq word (downcase word)))
892     (if isearch-regexp (setq word (regexp-quote word)))
893     (setq isearch-string (concat isearch-string word)
894           isearch-message
895           (concat isearch-message
896                   (mapconcat 'isearch-text-char-description
897                              word ""))
898           ;; Don't move cursor in reverse search.
899           isearch-yank-flag t))
900   (isearch-search-and-update))
901
902
903 (defun isearch-yank-word ()
904   "Pull next word from buffer into search string."
905   (interactive)
906   (isearch-yank (function (lambda () (forward-word 1)))))
907
908 (defun isearch-yank-line ()
909   "Pull rest of line from buffer into search string."
910   (interactive)
911   (isearch-yank 'end-of-line))
912
913 (defun isearch-yank-kill ()
914   "Pull rest of line from kill ring into search string."
915   (interactive)
916   (isearch-yank (current-kill 0)))
917
918 (defun isearch-yank-sexp ()
919   "Pull next expression from buffer into search string."
920   (interactive)
921   (isearch-yank 'forward-sexp))
922
923 (defun isearch-yank-x-selection ()
924   "Pull the current X selection into the search string."
925   (interactive)
926   (isearch-yank (x-get-selection)))
927
928 (defun isearch-yank-x-clipboard ()
929   "Pull the current X clipboard selection into the search string."
930   (interactive)
931   (isearch-yank (x-get-clipboard)))
932
933 (defun isearch-fix-case ()
934   (if (and isearch-case-fold-search search-caps-disable-folding)
935       (setq isearch-case-fold-search 
936             (no-upper-case-p isearch-string isearch-regexp)))
937   (setq isearch-mode (if case-fold-search
938                          (if isearch-case-fold-search
939                              " Isearch"  ;As God Intended Mode
940                            " ISeARch") ;Warn about evil case via StuDLYcAps.
941                        "Isearch"
942 ;                        (if isearch-case-fold-search
943 ;                            " isearch"    ;Presumably case-sensitive losers
944 ;                                          ;will notice this 1-char difference.
945 ;                            " Isearch")   ;Weenie mode.
946                          )))
947
948 (defun isearch-search-and-update ()
949   ;; Do the search and update the display.
950   (if (and (not isearch-success)
951            ;; unsuccessful regexp search may become
952            ;;  successful by addition of characters which
953            ;;  make isearch-string valid
954            (not isearch-regexp))
955       nil
956     ;; In reverse search, adding stuff at
957     ;; the end may cause zero or many more chars to be
958     ;; matched, in the string following point.
959     ;; Allow all those possibilities without moving point as
960     ;; long as the match does not extend past search origin.
961     (if (and (not isearch-forward) (not isearch-adjusted)
962              (condition-case ()
963                  (progn
964                    (isearch-fix-case)
965                    (let ((case-fold-search isearch-case-fold-search))
966                      (looking-at (if isearch-regexp isearch-string
967                                    (regexp-quote isearch-string)))))
968                (error nil))
969              (or isearch-yank-flag
970                  (<= (match-end 0) 
971                      (min isearch-opoint isearch-barrier))))
972         (setq isearch-success t 
973               isearch-invalid-regexp nil
974               isearch-other-end (match-end 0))
975       ;; Not regexp, not reverse, or no match at point.
976       (if (and isearch-other-end (not isearch-adjusted))
977           (goto-char (if isearch-forward isearch-other-end
978                        (min isearch-opoint 
979                             isearch-barrier 
980                             (1+ isearch-other-end)))))
981       (isearch-search)
982       ))
983   (isearch-push-state)
984   (if isearch-op-fun (funcall isearch-op-fun))
985   (isearch-update))
986
987
988 ;; *, ?, and | chars can make a regexp more liberal.
989 ;; They can make a regexp match sooner
990 ;; or make it succeed instead of failing.
991 ;; So go back to place last successful search started
992 ;; or to the last ^S/^R (barrier), whichever is nearer.
993
994 (defun isearch-*-char ()
995   "Handle * and ? specially in regexps."
996   (interactive)
997   (if isearch-regexp 
998
999       (progn
1000         (setq isearch-adjusted t)
1001         (let ((cs (nth (if isearch-forward
1002                            5            ; isearch-other-end
1003                          2)             ; saved (point)
1004                        (car (cdr isearch-cmds)))))
1005           ;; (car isearch-cmds) is after last search;
1006           ;; (car (cdr isearch-cmds)) is from before it.
1007           (setq cs (or cs isearch-barrier))
1008           (goto-char
1009            (if isearch-forward
1010                (max cs isearch-barrier)
1011              (min cs isearch-barrier))))))
1012   (isearch-process-search-char last-command-event))
1013   
1014
1015
1016 (defun isearch-|-char ()
1017   "If in regexp search, jump to the barrier."
1018   (interactive)
1019   (if isearch-regexp
1020       (progn
1021         (setq isearch-adjusted t)
1022         (goto-char isearch-barrier)))
1023   (isearch-process-search-char last-command-event))
1024
1025 (defun isearch-quote-char ()
1026   "Quote special characters for incremental search."
1027   (interactive)
1028   (isearch-process-search-char (read-quoted-char (isearch-message t))))
1029
1030
1031 (defun isearch-return-char ()
1032   "Convert return into newline for incremental search.
1033 Obsolete."
1034   (interactive)
1035   (isearch-process-search-char ?\n))
1036
1037
1038 (defun isearch-printing-char ()
1039   "Any other printing character => add it to the search string and search."
1040   (interactive)
1041   (isearch-process-search-char last-command-event))
1042
1043
1044 (defun isearch-whitespace-chars ()
1045   "Match all whitespace chars, if in regexp mode."
1046   (interactive)
1047   (if (and isearch-regexp search-whitespace-regexp)
1048       (isearch-process-search-string search-whitespace-regexp " ")
1049     (beep)
1050     (isearch-process-search-char ?\ )
1051 ;    (if isearch-word
1052 ;       nil
1053 ;      (setq isearch-word t)
1054 ;      (goto-char isearch-other-end)
1055 ;      (isearch-process-search-char ?\ ))
1056     ))
1057
1058 (defun isearch-process-search-char (char)
1059   ;; Append the char to the search string, update the message and re-search.
1060   (isearch-process-search-string (isearch-char-to-string char) 
1061                                  (isearch-text-char-description char)))
1062
1063 (defun isearch-process-search-string (string message)
1064   (setq isearch-string (concat isearch-string string)
1065         isearch-message (concat isearch-message message))
1066   (isearch-search-and-update))
1067
1068 \f
1069 ;;===========================================================
1070 ;; Search Ring
1071
1072 (defcustom search-ring-update nil
1073   "*Non-nil if advancing or retreating in the search ring should cause search.
1074 Default nil means edit the string from the search ring first."
1075   :type 'boolean
1076   :group 'isearch)
1077   
1078 (defun isearch-ring-adjust1 (advance)
1079   ;; Helper for isearch-ring-adjust
1080   (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1081          (length (length ring))
1082          (yank-pointer-name (if isearch-regexp
1083                                 'regexp-search-ring-yank-pointer
1084                               'search-ring-yank-pointer))
1085          (yank-pointer (eval yank-pointer-name)))
1086     (if (zerop length)
1087         ()
1088       (set yank-pointer-name
1089            (setq yank-pointer
1090                  (nthcdr (% (+ (- length (length yank-pointer))
1091                                (if advance (1- length) 1))
1092                             length) ring)))
1093       (setq isearch-string (car yank-pointer)
1094             isearch-message (mapconcat 'isearch-text-char-description
1095                                        isearch-string "")))))
1096
1097 (defun isearch-ring-adjust (advance)
1098   ;; Helper for isearch-ring-advance and isearch-ring-retreat
1099   (if (cdr isearch-cmds)  ;; is there more than one thing on stack?
1100       (isearch-pop-state))
1101   (isearch-ring-adjust1 advance)
1102   (isearch-push-state)
1103   (if search-ring-update
1104       (progn
1105         (isearch-search)
1106         (isearch-update))
1107     (isearch-edit-string)
1108     ))
1109
1110 (defun isearch-ring-advance ()
1111   "Advance to the next search string in the ring."
1112   ;; This could be more general to handle a prefix arg, but who would use it.
1113   (interactive)
1114   (isearch-ring-adjust 'advance))
1115
1116 (defun isearch-ring-retreat ()
1117   "Retreat to the previous search string in the ring."
1118   (interactive)
1119   (isearch-ring-adjust nil))
1120
1121 (defun isearch-ring-adjust-edit (advance)
1122   "Use the next or previous search string in the ring while in minibuffer."
1123   (isearch-ring-adjust1 advance)
1124   (erase-buffer)
1125   (insert isearch-string))
1126
1127 (defun isearch-ring-advance-edit ()
1128   (interactive)
1129   (isearch-ring-adjust-edit 'advance))
1130
1131 (defun isearch-ring-retreat-edit ()
1132   "Retreat to the previous search string in the ring while in the minibuffer."
1133   (interactive)
1134   (isearch-ring-adjust-edit nil))
1135
1136
1137 (defun isearch-complete1 ()
1138   ;; Helper for isearch-complete and isearch-complete-edit
1139   ;; Return t if completion OK, 
1140   (let* ((ring (if isearch-regexp regexp-search-ring search-ring))
1141          (alist (mapcar (function (lambda (string) (list string))) ring))
1142          (completion-ignore-case case-fold-search)
1143          (completion (try-completion isearch-string alist))
1144          )
1145     (cond
1146      ((eq completion t)
1147       ;; isearch-string stays the same
1148       t)
1149      ((or completion ; not nil, must be a string
1150           (= 0 (length isearch-string))) ; shouldn't have to say this
1151       (if (equal completion isearch-string)  ;; no extension?
1152           (if completion-auto-help
1153               (with-output-to-temp-buffer "*Isearch completions*"
1154                 (display-completion-list 
1155                  (all-completions isearch-string alist))))
1156         (setq isearch-string completion))
1157       t)
1158      (t
1159       (temp-minibuffer-message "No completion")
1160       nil))))
1161
1162 (defun isearch-complete ()
1163   "Complete the search string from the strings on the search ring.
1164 The completed string is then editable in the minibuffer.
1165 If there is no completion possible, say so and continue searching."
1166   (interactive)
1167   (if (isearch-complete1)
1168       (isearch-edit-string)
1169     ;; else
1170     (sit-for 1)
1171     (isearch-update)))
1172
1173 (defun isearch-complete-edit ()
1174   "Same as `isearch-complete' except in the minibuffer."
1175   (interactive)
1176   (setq isearch-string (buffer-string))
1177   (if (isearch-complete1)
1178       (progn
1179         (erase-buffer)
1180         (insert isearch-string))))
1181
1182 \f
1183 ;;;==============================================================
1184 ;; The search status stack (and isearch window-local variables, not used).
1185
1186 (defun isearch-top-state ()
1187 ;;  (fetch-window-local-variables)
1188   (let ((cmd (car isearch-cmds)))
1189     (setq isearch-string (car cmd)
1190           isearch-message (car (cdr cmd))
1191           isearch-success (nth 3 cmd)
1192           isearch-forward (nth 4 cmd)
1193           isearch-other-end (nth 5 cmd)
1194           isearch-invalid-regexp (nth 6 cmd)
1195           isearch-wrapped (nth 7 cmd)
1196           isearch-barrier (nth 8 cmd))
1197     (goto-char (car (cdr (cdr cmd))))))
1198
1199 (defun isearch-pop-state ()
1200 ;;  (fetch-window-local-variables)
1201   (setq isearch-cmds (cdr isearch-cmds))
1202   (isearch-top-state)
1203   )
1204
1205 (defun isearch-push-state ()
1206   (setq isearch-cmds 
1207         (cons (list isearch-string isearch-message (point)
1208                     isearch-success isearch-forward isearch-other-end 
1209                     isearch-invalid-regexp isearch-wrapped isearch-barrier)
1210               isearch-cmds)))
1211
1212 \f
1213 ;;;==================================================================
1214 ;; Message string
1215
1216 (defun isearch-message (&optional c-q-hack ellipsis)
1217   ;; Generate and print the message string.
1218   (let ((cursor-in-echo-area ellipsis)
1219         (m (concat
1220             (isearch-message-prefix c-q-hack)
1221             isearch-message
1222             (isearch-message-suffix c-q-hack)
1223             )))
1224     (if c-q-hack m (display-message 'progress (format "%s" m)))))
1225
1226 (defun isearch-message-prefix (&optional c-q-hack nonincremental)
1227   ;; If about to search, and previous search regexp was invalid,
1228   ;; check that it still is.  If it is valid now,
1229   ;; let the message we display while searching say that it is valid.
1230   (and isearch-invalid-regexp
1231        (condition-case ()
1232            (progn (re-search-forward isearch-string (point) t)
1233                   (setq isearch-invalid-regexp nil))
1234          (error nil)))
1235   ;; #### - Yo!  Emacs assembles strings all over the place, they can't all
1236   ;; be internationalized in the manner proposed below...  Add an explicit
1237   ;; call to `gettext' and have the string snarfer pluck the english
1238   ;; strings out of the comment below.  XEmacs is on a purespace diet! -Stig
1239   (let ((m (concat (if isearch-success nil "failing ")
1240                    (if isearch-wrapped "wrapped ")
1241                    (if isearch-word "word ")
1242                    (if isearch-regexp "regexp ")
1243                    (if nonincremental "search" "I-search")
1244                    (if isearch-forward nil " backward")
1245                    ": "
1246                    )))
1247     (aset m 0 (upcase (aref m 0)))
1248     (gettext m)))
1249
1250 (defun isearch-message-suffix (&optional c-q-hack)
1251   (concat (if c-q-hack "^Q" "")
1252           (if isearch-invalid-regexp
1253               (concat " [" isearch-invalid-regexp "]")
1254             "")))
1255
1256 ;;;;; #### - yuck...this is soooo lame.  Is this really worth 4k of purespace???
1257 ;;;
1258 ;;;(let ((i (logior (if isearch-success 32 0)
1259 ;;;                (if isearch-wrapped 16 0)
1260 ;;;                (if isearch-word     8 0)
1261 ;;;                (if isearch-regexp   4 0)
1262 ;;;                (if nonincremental   2 0)
1263 ;;;                (if isearch-forward  1 0))))
1264 ;;;  (cond
1265 ;;;   ((= i 63) (gettext "Wrapped word regexp search: "))              ; 111111
1266 ;;;   ((= i 62) (gettext "Wrapped word regexp search backward: "))     ; 111110
1267 ;;;   ((= i 61) (gettext "Wrapped word regexp I-search: "))            ; 111101
1268 ;;;   ((= i 60) (gettext "Wrapped word regexp I-search backward: "))   ; 111100
1269 ;;;   ((= i 59) (gettext "Wrapped word search: "))                     ; 111011
1270 ;;;   ((= i 58) (gettext "Wrapped word search backward: "))            ; 111010
1271 ;;;   ((= i 57) (gettext "Wrapped word I-search: "))                   ; 111001
1272 ;;;   ((= i 56) (gettext "Wrapped word I-search backward: "))          ; 111000
1273 ;;;   ((= i 55) (gettext "Wrapped regexp search: "))                   ; 110111
1274 ;;;   ((= i 54) (gettext "Wrapped regexp search backward: "))          ; 110110
1275 ;;;   ((= i 53) (gettext "Wrapped regexp I-search: "))                 ; 110101
1276 ;;;   ((= i 52) (gettext "Wrapped regexp I-search backward: "))        ; 110100
1277 ;;;   ((= i 51) (gettext "Wrapped search: "))                          ; 110011
1278 ;;;   ((= i 50) (gettext "Wrapped search backward: "))                 ; 110010
1279 ;;;   ((= i 49) (gettext "Wrapped I-search: "))                        ; 110001
1280 ;;;   ((= i 48) (gettext "Wrapped I-search backward: "))               ; 110000
1281 ;;;   ((= i 47) (gettext "Word regexp search: "))                      ; 101111
1282 ;;;   ((= i 46) (gettext "Word regexp search backward: "))             ; 101110
1283 ;;;   ((= i 45) (gettext "Word regexp I-search: "))                    ; 101101
1284 ;;;   ((= i 44) (gettext "Word regexp I-search backward: "))           ; 101100
1285 ;;;   ((= i 43) (gettext "Word search: "))                             ; 101011
1286 ;;;   ((= i 42) (gettext "Word search backward: "))                    ; 101010
1287 ;;;   ((= i 41) (gettext "Word I-search: "))                           ; 101001
1288 ;;;   ((= i 40) (gettext "Word I-search backward: "))                  ; 101000
1289 ;;;   ((= i 39) (gettext "Regexp search: "))                           ; 100111
1290 ;;;   ((= i 38) (gettext "Regexp search backward: "))                  ; 100110
1291 ;;;   ((= i 37) (gettext "Regexp I-search: "))                         ; 100101
1292 ;;;   ((= i 36) (gettext "Regexp I-search backward: "))                ; 100100
1293 ;;;   ((= i 35) (gettext "Search: "))                                  ; 100011
1294 ;;;   ((= i 34) (gettext "Search backward: "))                         ; 100010
1295 ;;;   ((= i 33) (gettext "I-search: "))                                ; 100001
1296 ;;;   ((= i 32) (gettext "I-search backward: "))                       ; 100000
1297 ;;;   ((= i 31) (gettext "Failing wrapped word regexp search: "))      ; 011111
1298 ;;;   ((= i 30) (gettext "Failing wrapped word regexp search backward: ")) ; 011110
1299 ;;;   ((= i 29) (gettext "Failing wrapped word regexp I-search: "))    ; 011101
1300 ;;;   ((= i 28) (gettext "Failing wrapped word regexp I-search backward: ")) ; 011100
1301 ;;;   ((= i 27) (gettext "Failing wrapped word search: "))             ; 011011
1302 ;;;   ((= i 26) (gettext "Failing wrapped word search backward: "))    ; 011010
1303 ;;;   ((= i 25) (gettext "Failing wrapped word I-search: "))           ; 011001
1304 ;;;   ((= i 24) (gettext "Failing wrapped word I-search backward: "))  ; 011000
1305 ;;;   ((= i 23) (gettext "Failing wrapped regexp search: "))           ; 010111
1306 ;;;   ((= i 22) (gettext "Failing wrapped regexp search backward: "))  ; 010110
1307 ;;;   ((= i 21) (gettext "Failing wrapped regexp I-search: "))         ; 010101
1308 ;;;   ((= i 20) (gettext "Failing wrapped regexp I-search backward: ")) ; 010100
1309 ;;;   ((= i 19) (gettext "Failing wrapped search: "))                  ; 010011
1310 ;;;   ((= i 18) (gettext "Failing wrapped search backward: "))         ; 010010
1311 ;;;   ((= i 17) (gettext "Failing wrapped I-search: "))                ; 010001
1312 ;;;   ((= i 16) (gettext "Failing wrapped I-search backward: "))       ; 010000
1313 ;;;   ((= i 15) (gettext "Failing word regexp search: "))              ; 001111
1314 ;;;   ((= i 14) (gettext "Failing word regexp search backward: "))     ; 001110
1315 ;;;   ((= i 13) (gettext "Failing word regexp I-search: "))            ; 001101
1316 ;;;   ((= i 12) (gettext "Failing word regexp I-search backward: "))   ; 001100
1317 ;;;   ((= i 11) (gettext "Failing word search: "))                     ; 001011
1318 ;;;   ((= i 10) (gettext "Failing word search backward: "))            ; 001010
1319 ;;;   ((= i  9) (gettext "Failing word I-search: "))                   ; 001001
1320 ;;;   ((= i  8) (gettext "Failing word I-search backward: "))          ; 001000
1321 ;;;   ((= i  7) (gettext "Failing regexp search: "))                   ; 000111
1322 ;;;   ((= i  6) (gettext "Failing regexp search backward: "))          ; 000110
1323 ;;;   ((= i  5) (gettext "Failing regexp I-search: "))                 ; 000101
1324 ;;;   ((= i  4) (gettext "Failing regexp I-search backward: "))        ; 000100
1325 ;;;   ((= i  3) (gettext "Failing search: "))                          ; 000011
1326 ;;;   ((= i  2) (gettext "Failing search backward: "))                 ; 000010
1327 ;;;   ((= i  1) (gettext "Failing I-search: "))                        ; 000001
1328 ;;;   ((= i  0) (gettext "Failing I-search backward: "))               ; 000000
1329 ;;;   (t (error "Something's rotten")))))
1330
1331 \f
1332 ;;;========================================================
1333 ;;; Exiting
1334
1335 (put 'isearch-printing-char                     'isearch-command t)
1336 (put 'isearch-return-char                       'isearch-command t)
1337 (put 'isearch-repeat-forward                    'isearch-command t)
1338 (put 'isearch-repeat-backward                   'isearch-command t)
1339 (put 'isearch-delete-char                       'isearch-command t)
1340 (put 'isearch-help-or-delete-char               'isearch-command t)
1341 (put 'isearch-abort                             'isearch-command t)
1342 (put 'isearch-quote-char                        'isearch-command t)
1343 (put 'isearch-exit                              'isearch-command t)
1344 (put 'isearch-printing-char                     'isearch-command t)
1345 (put 'isearch-printing-char                     'isearch-command t)
1346 (put 'isearch-yank-word                         'isearch-command t)
1347 (put 'isearch-yank-line                         'isearch-command t)
1348 (put 'isearch-yank-kill                         'isearch-command t)
1349 (put 'isearch-yank-sexp                         'isearch-command t)
1350 (put 'isearch-*-char                            'isearch-command t)
1351 (put 'isearch-*-char                            'isearch-command t)
1352 (put 'isearch-|-char                            'isearch-command t)
1353 (put 'isearch-toggle-regexp                     'isearch-command t)
1354 (put 'isearch-toggle-case-fold                  'isearch-command t)
1355 (put 'isearch-edit-string                       'isearch-command t)
1356 (put 'isearch-mode-help                         'isearch-command t)
1357 (put 'isearch-ring-advance                      'isearch-command t)
1358 (put 'isearch-ring-retreat                      'isearch-command t)
1359 (put 'isearch-ring-advance-edit                 'isearch-command t)
1360 (put 'isearch-ring-retreat-edit                 'isearch-command t)
1361 (put 'isearch-whitespace-chars                  'isearch-command t)
1362 (put 'isearch-complete                          'isearch-command t)
1363 (put 'isearch-complete-edit                     'isearch-command t)
1364 (put 'isearch-edit-string                       'isearch-command t)
1365 (put 'isearch-toggle-regexp                     'isearch-command t)
1366 (put 'isearch-forward-exit-minibuffer           'isearch-command t)
1367 (put 'isearch-reverse-exit-minibuffer           'isearch-command t)
1368 (put 'isearch-nonincremental-exit-minibuffer    'isearch-command t)
1369 (put 'isearch-yank-x-selection                  'isearch-command t)
1370 (put 'isearch-yank-x-clipboard                  'isearch-command t)
1371
1372 ;; scrolling the scrollbar should not terminate isearch.
1373
1374 ;; vertical scrollbar:
1375 (put 'scrollbar-line-up                         'isearch-command t)
1376 (put 'scrollbar-line-down                       'isearch-command t)
1377 (put 'scrollbar-page-up                         'isearch-command t)
1378 (put 'scrollbar-page-down                       'isearch-command t)
1379 (put 'scrollbar-to-top                          'isearch-command t)
1380 (put 'scrollbar-to-bottom                       'isearch-command t)
1381 (put 'scrollbar-vertical-drag                   'isearch-command t)
1382
1383 ;; horizontal scrollbar:
1384 (put 'scrollbar-char-left                       'isearch-command t)
1385 (put 'scrollbar-char-right                      'isearch-command t)
1386 (put 'scrollbar-page-left                       'isearch-command t)
1387 (put 'scrollbar-page-right                      'isearch-command t)
1388 (put 'scrollbar-to-left                         'isearch-command t)
1389 (put 'scrollbar-to-right                        'isearch-command t)
1390 (put 'scrollbar-horizontal-drag                 'isearch-command t)
1391
1392 (defun isearch-pre-command-hook ()
1393   ;;
1394   ;; For use as the value of `pre-command-hook' when isearch-mode is active.
1395   ;; If the command about to be executed is not one of the isearch commands,
1396   ;; then isearch-mode is turned off before that command is executed.
1397   ;;
1398   ;; If the command about to be executed is self-insert-command, or is a
1399   ;; keyboard macro of a single key sequence which is bound to self-insert-
1400   ;; command, then we add those chars to the search ring instead of inserting
1401   ;; them in the buffer.  In this way, the set of self-searching characters
1402   ;; need not be exhaustively enumerated, but is derived from other maps.
1403   ;;
1404   (cond ((not (eq (current-buffer) isearch-buffer))
1405          ;; If the buffer (likely meaning "frame") has changed, bail.
1406          ;; This can also happen if a proc filter has popped up another
1407          ;; buffer, which is arguably a bad thing for it to have done,
1408          ;; but the way in which isearch would have hosed you in that
1409          ;; case is unarguably even worse. -jwz
1410          (isearch-done))
1411         (t
1412          (isearch-maybe-frob-keyboard-macros)
1413          (if (and this-command
1414                   (symbolp this-command)
1415                   (get this-command 'isearch-command))
1416              nil ; then continue.
1417            (isearch-done)))))
1418
1419 (defun isearch-maybe-frob-keyboard-macros ()
1420   ;;
1421   ;; If the command about to be executed is `self-insert-command' then change
1422   ;; the command to `isearch-printing-char' instead, meaning add the last-
1423   ;; typed character to the search string.
1424   ;;
1425   ;; If `this-command' is a string or a vector (that is, a keyboard macro)
1426   ;; and it contains only one command, which is bound to self-insert-command,
1427   ;; then do the same thing as for self-inserting commands: arrange for that
1428   ;; character to be added to the search string.  If we didn't do this, then
1429   ;; typing a compose sequence (a la x-compose.el) would terminate the search
1430   ;; and insert the character, instead of searching for that character.
1431   ;;
1432   ;; We should continue doing this, since it's pretty much the behavior one
1433   ;; would expect, but it will stop being so necessary once key-translation-
1434   ;; map exists and is used by x-compose.el and things like it, since the
1435   ;; translation will have been done before we see the keys.
1436   ;;
1437   (cond ((eq this-command 'self-insert-command)
1438          (setq this-command 'isearch-printing-char))
1439         ((and (or (stringp this-command) (vectorp this-command))
1440               (eq (key-binding this-command) 'self-insert-command))
1441          (setq last-command-event (character-to-event (aref this-command 0))
1442                last-command-char (and (stringp this-command)
1443                                       (aref this-command 0))
1444                this-command 'isearch-printing-char))
1445         ))
1446
1447 \f
1448 ;;;========================================================
1449 ;;; Highlighting
1450
1451 (defcustom isearch-highlight t
1452   "*Whether isearch and query-replace should highlight the text which 
1453 currently matches the search-string.")
1454
1455 (defvar isearch-extent nil)
1456
1457 ;; this face is initialized by x-faces.el since isearch is preloaded.
1458 ;; this face is now created in initialize-faces
1459 ;;(make-face 'isearch)
1460
1461 (defun isearch-make-extent (begin end)
1462   (let ((x (make-extent begin end (current-buffer))))
1463     ;; make the isearch extent always take prescedence over any mouse-
1464     ;; highlighted extents we may be passing through, since isearch, being
1465     ;; modal, is more interesting (there's nothing they could do with a
1466     ;; mouse-highlighted extent while in the midst of a search anyway).
1467     (set-extent-priority x (1+ mouse-highlight-priority))
1468     (set-extent-face x 'isearch)
1469     (setq isearch-extent x)))
1470
1471 (defun isearch-highlight (begin end)
1472   (if (null isearch-highlight)
1473       nil
1474     ;; make sure isearch-extent is in the current buffer
1475     (or (extentp isearch-extent)
1476         (isearch-make-extent begin end))
1477     (set-extent-endpoints isearch-extent begin end (current-buffer))))
1478
1479 (defun isearch-dehighlight (totally)
1480   (if (and isearch-highlight isearch-extent)
1481       (if totally
1482           (let ((inhibit-quit t))
1483             (if (extentp isearch-extent)
1484                 (delete-extent isearch-extent))
1485             (setq isearch-extent nil))
1486         (if (extentp isearch-extent)
1487             (detach-extent isearch-extent)
1488           (setq isearch-extent nil)))))
1489
1490 \f
1491 ;;;========================================================
1492 ;;; Searching
1493
1494 (defun isearch-search ()
1495   ;; Do the search with the current search string.
1496   (isearch-message nil t)
1497   (isearch-fix-case)
1498   (condition-case lossage
1499       (let ((inhibit-quit nil)
1500             (case-fold-search isearch-case-fold-search))
1501         (if isearch-regexp (setq isearch-invalid-regexp nil))
1502         (setq isearch-success
1503               (funcall
1504                (cond (isearch-word
1505                       (if isearch-forward
1506                           'word-search-forward 'word-search-backward))
1507                      (isearch-regexp
1508                       (if isearch-forward
1509                           're-search-forward 're-search-backward))
1510                      (t
1511                       (if isearch-forward 'search-forward 'search-backward)))
1512                isearch-string nil t))
1513         (setq isearch-just-started nil)
1514         (if isearch-success
1515             (setq isearch-other-end
1516                   (if isearch-forward (match-beginning 0) (match-end 0)))))
1517
1518     (quit (setq unread-command-event (character-to-event (quit-char)))
1519           (setq isearch-success nil))
1520
1521     (invalid-regexp 
1522      (setq isearch-invalid-regexp (car (cdr lossage)))
1523      (if (string-match
1524           "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
1525           isearch-invalid-regexp)
1526          (setq isearch-invalid-regexp (gettext "incomplete input")))))
1527
1528   (if isearch-success
1529       nil
1530
1531     ;; If we're being run inside a keyboard macro, then the call to
1532     ;; ding will signal an error (to terminate the macro).  We must
1533     ;; turn off isearch-mode first, so that we aren't still in isearch
1534     ;; mode after the macro exits.  Note that isearch-recursive-edit
1535     ;; must not be true if a keyboard macro is executing.
1536     (if (and executing-kbd-macro (not defining-kbd-macro))
1537         (progn
1538           (isearch-done)
1539           (ding nil 'isearch-failed)))
1540
1541     ;; Ding if failed this time after succeeding last time.
1542     (and (nth 3 (car isearch-cmds))
1543          (ding nil 'isearch-failed))
1544     (goto-char (nth 2 (car isearch-cmds)))))
1545
1546 ;;;=================================================
1547 ;; This is called from incremental-search
1548 ;; if the first input character is the exit character.
1549
1550 ;; We store the search string in `isearch-string'
1551 ;; which has been bound already by `isearch-search'
1552 ;; so that, when we exit, it is copied into `search-last-string'.
1553
1554 ;(defun nonincremental-search (forward regexp)
1555 ;  ;; This may be broken.  Anyway, it is replaced by the isearch-edit-string.
1556 ;  ;; Missing features: word search option, command history.
1557 ;  (setq isearch-forward forward
1558 ;       isearch-regexp regexp)
1559 ;  (let (char function
1560 ;       inhibit-quit
1561 ;       (cursor-in-echo-area t))
1562 ;    ;; Prompt assuming not word search,
1563 ;    (setq isearch-message 
1564 ;         (if isearch-regexp 
1565 ;             (if isearch-forward "Regexp search: "
1566 ;               "Regexp search backward: ")
1567 ;           (if isearch-forward "Search: " "Search backward: ")))
1568 ;    (message "%s" isearch-message)
1569 ;    ;; Read 1 char and switch to word search if it is ^W.
1570 ;    (setq char (read-char))
1571 ;    (if (eq char search-yank-word-char)
1572 ;       (setq isearch-message (if isearch-forward "Word search: " 
1573 ;                               "Word search backward: "))
1574 ;      ;; Otherwise let that 1 char be part of the search string.
1575 ;      (setq unread-command-event (character-to-event char))
1576 ;      )
1577 ;    (setq function
1578 ;         (if (eq char search-yank-word-char)
1579 ;             (if isearch-forward 'word-search-forward 'word-search-backward)
1580 ;           (if isearch-regexp
1581 ;               (if isearch-forward 're-search-forward 're-search-backward)
1582 ;             (if isearch-forward 'search-forward 'search-backward))))
1583 ;    ;; Read the search string with corrected prompt.
1584 ;    (setq isearch-string (read-string isearch-message isearch-string))
1585 ;    ;; Empty means use default.
1586 ;    (if (= 0 (length isearch-string))
1587 ;       (setq isearch-string search-last-string)
1588 ;      ;; Set last search string now so it is set even if we fail.
1589 ;      (setq search-last-string isearch-string))
1590 ;    ;; Since we used the minibuffer, we should be available for redo.
1591 ;    (setq command-history 
1592 ;         (cons (list function isearch-string) command-history))
1593 ;    ;; Go ahead and search.
1594 ;    (if search-caps-disable-folding
1595 ;       (setq isearch-case-fold-search 
1596 ;             (no-upper-case-p isearch-string isearch-regexp)))
1597 ;    (let ((case-fold-search isearch-case-fold-search))
1598 ;      (funcall function isearch-string))))
1599
1600 (defun isearch-no-upper-case-p (string)
1601   "Return t if there are no upper case chars in string.
1602 But upper case chars preceded by \\ do not count since they
1603 have special meaning in a regexp."
1604   ;; this incorrectly returns t for "\\\\A"
1605   (let ((case-fold-search nil))
1606     (not (string-match "\\(^\\|[^\\]\\)[A-Z]" string))))
1607 (make-obsolete 'isearch-no-upper-case-p 'no-upper-case-p)
1608
1609 ;; Used by etags.el and info.el
1610 (defmacro with-caps-disable-folding (string &rest body) "\
1611 Eval BODY with `case-fold-search' let to nil if STRING contains
1612 uppercase letters and `search-caps-disable-folding' is t."
1613   `(let ((case-fold-search
1614           (if (and case-fold-search search-caps-disable-folding)
1615               (isearch-no-upper-case-p ,string)
1616             case-fold-search)))
1617      ,@body))
1618 (make-obsolete 'with-caps-disable-folding 'with-search-caps-disable-folding)
1619 (put 'with-caps-disable-folding 'lisp-indent-function 1)
1620 (put 'with-caps-disable-folding 'edebug-form-spec '(form body))
1621
1622 ;;; isearch-mode.el ends here