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