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