home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Source Code 1993 July / THE_SOURCE_CODE_CD_ROM.iso / gnu / lucid / lemacs-19.6 / lisp / prim / isearch-mode.el < prev    next >
Encoding:
Text File  |  1993-02-17  |  50.3 KB  |  1,414 lines

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