]> code.delx.au - gnu-emacs/blobdiff - lisp/isearch.el
(isearch-quote-char): Use it.
[gnu-emacs] / lisp / isearch.el
index 2c20d45bbd833c42fe654831e0dcef15ae752fa3..118d23ce38fab9306805695637930fe0a48be51f 100644 (file)
 ;; keep the behavior.  No point in forcing nonincremental search until
 ;; the last possible moment.
 
-;; TODO
-;; - Integrate the emacs 19 generalized command history.
-;; - Hooks and options for failed search.
-
-;;; Change Log:
-
-;; Changes before those recorded in ChangeLog:
-
-;; Revision 1.4  92/09/14  16:26:02  liberte
-;; Added prefix args to isearch-forward, etc. to switch between
-;;    string and regular expression searching.
-;; Added some support for lemacs.
-;; Added general isearch-highlight option - but only for lemacs so far.
-;; Added support for frame switching in emacs 19.
-;; Added word search option to isearch-edit-string.
-;; Renamed isearch-quit to isearch-abort.
-;; Numerous changes to comments and doc strings.
-;;
-;; Revision 1.3  92/06/29  13:10:08  liberte
-;; Moved modal isearch-mode handling into isearch-mode.
-;; Got rid of buffer-local isearch variables.
-;; isearch-edit-string used by ring adjustments, completion, and
-;; nonincremental searching.  C-s and C-r are additional exit commands.
-;; Renamed all regex to regexp.
-;; Got rid of found-start and found-point globals.
-;; Generalized handling of upper-case chars.
-
-;; Revision 1.2  92/05/27  11:33:57  liberte
-;; Emacs version 19 has a search ring, which is supported here.
-;; Other fixes found in the version 19 isearch are included here.
-;;
-;; Also see variables search-caps-disable-folding,
-;; search-nonincremental-instead, search-whitespace-regexp, and
-;; commands isearch-toggle-regexp, isearch-edit-string.
-;;
-;; semi-modal isearching is supported.
-
-;; Changes for 1.1
-;; 3/18/92 Fixed invalid-regexp.
-;; 3/18/92 Fixed yanking in regexps.
-
 ;;; Code:
 
 \f
@@ -150,12 +109,14 @@ string, and RET terminates editing and does a nonincremental search."
   :type 'boolean
   :group 'isearch)
 
-(defcustom search-whitespace-regexp "\\(?:\\s-+\\)"
+(defcustom search-whitespace-regexp "\\s-+"
   "*If non-nil, regular expression to match a sequence of whitespace chars.
 This applies to regular expression incremental search.
-You might want to use something like \"\\\\(?:[ \\t\\r\\n]+\\\\)\" instead.
-In the Customization buffer, that is `\\(?:[' followed by a space,
-a tab, a carriage return (control-M), a newline, and `]+\\)'."
+When you put a space or spaces in the incremental regexp, it stands for
+this, unless it is inside of a regexp construct such as [...] or *, + or ?.
+You might want to use something like \"[ \\t\\r\\n]+\" instead.
+In the Customization buffer, that is `[' followed by a space,
+a tab, a carriage return (control-M), a newline, and `]+'."
   :type 'regexp
   :group 'isearch)
 
@@ -187,8 +148,10 @@ Ordinarily the text becomes invisible again at the end of the search."
   :type 'boolean
   :group 'isearch)
 
-(defcustom isearch-resume-enabled t
-  "*If non-nil, `isearch-resume' commands are added to the command history."
+(defcustom isearch-resume-in-command-history nil
+  "*If non-nil, `isearch-resume' commands are added to the command history.
+This allows you to resume earlier isearch sessions through the
+command history."
   :type 'boolean
   :group 'isearch)
 
@@ -198,6 +161,15 @@ Ordinarily the text becomes invisible again at the end of the search."
 (defvar isearch-mode-end-hook nil
   "Function(s) to call after terminating an incremental search.")
 
+(defvar isearch-wrap-function nil
+  "Function to call to wrap the search when search is failed.
+If nil, move point to the beginning of the buffer for a forward search,
+or to the end of the buffer for a backward search.")
+
+(defvar isearch-push-state-function nil
+  "Function to save a function restoring the mode-specific isearch state
+to the search status stack.")
+
 ;; Search ring.
 
 (defvar search-ring nil
@@ -290,8 +262,6 @@ Default value, nil, means edit the string instead."
     (define-key map "\r" 'isearch-exit)
     (define-key map "\C-j" 'isearch-printing-char)
     (define-key map "\t" 'isearch-printing-char)
-    (define-key map " " 'isearch-whitespace-chars)
-    (define-key map [?\S-\ ] 'isearch-whitespace-chars)
 
     (define-key map    "\C-w" 'isearch-yank-word-or-char)
     (define-key map "\M-\C-w" 'isearch-del-char)
@@ -515,7 +485,12 @@ the calling function until the search is done."
 Do incremental search forward for regular expression.
 With a prefix argument, do a regular string search instead.
 Like ordinary incremental search except that your input
-is treated as a regexp.  See \\[isearch-forward] for more info."
+is treated as a regexp.  See \\[isearch-forward] for more info.
+
+In regexp incremental searches, a space or spaces normally matches
+any whitespace (the variable `search-whitespace-regexp' controls
+precisely what that means).  If you want to search for a literal space
+and nothing else, enter `[ ]'."
   (interactive "P\np")
   (isearch-mode t (null not-regexp) nil (not no-recursive-edit)))
 
@@ -683,7 +658,7 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
   (setq disable-point-adjustment t))
 
 (defun isearch-done (&optional nopush edit)
-  (if isearch-resume-enabled
+  (if isearch-resume-in-command-history
       (let ((command `(isearch-resume ,isearch-string ,isearch-regexp
                                      ,isearch-word ,isearch-forward
                                      ,isearch-message
@@ -710,16 +685,7 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
     (if isearch-small-window
        (goto-char found-point)
       ;; Exiting the save-window-excursion clobbers window-start; restore it.
-      (set-window-start (selected-window) found-start t))
-
-    ;; If there was movement, mark the starting position.
-    ;; Maybe should test difference between and set mark iff > threshold.
-    (if (/= (point) isearch-opoint)
-       (or (and transient-mark-mode mark-active)
-           (progn
-             (push-mark isearch-opoint t)
-             (or executing-kbd-macro (> (minibuffer-depth) 0)
-                 (message "Mark saved where search started"))))))
+      (set-window-start (selected-window) found-start t)))
 
   (setq isearch-mode nil)
   (if isearch-input-method-local-p
@@ -744,6 +710,16 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
       (isearch-update-ring isearch-string isearch-regexp))
 
   (run-hooks 'isearch-mode-end-hook)
+
+  ;; If there was movement, mark the starting position.
+  ;; Maybe should test difference between and set mark iff > threshold.
+  (if (/= (point) isearch-opoint)
+      (or (and transient-mark-mode mark-active)
+         (progn
+           (push-mark isearch-opoint t)
+           (or executing-kbd-macro (> (minibuffer-depth) 0)
+               (message "Mark saved where search started")))))
+
   (and (not edit) isearch-recursive-edit (exit-recursive-edit)))
 
 (defun isearch-update-ring (string &optional regexp)
@@ -776,57 +752,62 @@ REGEXP says which ring to use."
 \f
 ;; The search status structure and stack.
 
-(defsubst isearch-string (frame)
+(defsubst isearch-string-state (frame)
   "Return the search string in FRAME."
   (aref frame 0))
-(defsubst isearch-message-string (frame)
+(defsubst isearch-message-state (frame)
   "Return the search string to display to the user in FRAME."
   (aref frame 1))
-(defsubst isearch-point (frame)
+(defsubst isearch-point-state (frame)
   "Return the point in FRAME."
   (aref frame 2))
-(defsubst isearch-success (frame)
+(defsubst isearch-success-state (frame)
   "Return the success flag in FRAME."
   (aref frame 3))
-(defsubst isearch-forward-flag (frame)
+(defsubst isearch-forward-state (frame)
   "Return the searching-forward flag in FRAME."
   (aref frame 4))
-(defsubst isearch-other-end (frame)
+(defsubst isearch-other-end-state (frame)
   "Return the other end of the match in FRAME."
   (aref frame 5))
-(defsubst isearch-word (frame)
+(defsubst isearch-word-state (frame)
   "Return the search-by-word flag in FRAME."
   (aref frame 6))
-(defsubst isearch-invalid-regexp (frame)
+(defsubst isearch-invalid-regexp-state (frame)
   "Return the regexp error message in FRAME, or nil if its regexp is valid."
   (aref frame 7))
-(defsubst isearch-wrapped (frame)
+(defsubst isearch-wrapped-state (frame)
   "Return the search-wrapped flag in FRAME."
   (aref frame 8))
-(defsubst isearch-barrier (frame)
+(defsubst isearch-barrier-state (frame)
   "Return the barrier value in FRAME."
   (aref frame 9))
-(defsubst isearch-within-brackets (frame)
+(defsubst isearch-within-brackets-state (frame)
   "Return the in-character-class flag in FRAME."
   (aref frame 10))
-(defsubst isearch-case-fold-search (frame)
+(defsubst isearch-case-fold-search-state (frame)
   "Return the case-folding flag in FRAME."
   (aref frame 11))
+(defsubst isearch-pop-fun-state (frame)
+  "Return the function restoring the mode-specific isearch state in FRAME."
+  (aref frame 12))
 
 (defun isearch-top-state ()
   (let ((cmd (car isearch-cmds)))
-    (setq isearch-string (isearch-string cmd)
-         isearch-message (isearch-message-string cmd)
-         isearch-success (isearch-success cmd)
-         isearch-forward (isearch-forward-flag cmd)
-         isearch-other-end (isearch-other-end cmd)
-         isearch-word (isearch-word cmd)
-         isearch-invalid-regexp (isearch-invalid-regexp cmd)
-         isearch-wrapped (isearch-wrapped cmd)
-         isearch-barrier (isearch-barrier cmd)
-         isearch-within-brackets (isearch-within-brackets cmd)
-         isearch-case-fold-search (isearch-case-fold-search cmd))
-    (goto-char (isearch-point cmd))))
+    (setq isearch-string (isearch-string-state cmd)
+         isearch-message (isearch-message-state cmd)
+         isearch-success (isearch-success-state cmd)
+         isearch-forward (isearch-forward-state cmd)
+         isearch-other-end (isearch-other-end-state cmd)
+         isearch-word (isearch-word-state cmd)
+         isearch-invalid-regexp (isearch-invalid-regexp-state cmd)
+         isearch-wrapped (isearch-wrapped-state cmd)
+         isearch-barrier (isearch-barrier-state cmd)
+         isearch-within-brackets (isearch-within-brackets-state cmd)
+         isearch-case-fold-search (isearch-case-fold-search-state cmd))
+    (if (functionp (isearch-pop-fun-state cmd))
+       (funcall (isearch-pop-fun-state cmd) cmd))
+    (goto-char (isearch-point-state cmd))))
 
 (defun isearch-pop-state ()
   (setq isearch-cmds (cdr isearch-cmds))
@@ -838,7 +819,9 @@ REGEXP says which ring to use."
                      isearch-success isearch-forward isearch-other-end
                      isearch-word
                      isearch-invalid-regexp isearch-wrapped isearch-barrier
-                     isearch-within-brackets isearch-case-fold-search)
+                     isearch-within-brackets isearch-case-fold-search
+                     (if isearch-push-state-function
+                         (funcall isearch-push-state-function)))
              isearch-cmds)))
 
 \f
@@ -1024,10 +1007,13 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
 (defun isearch-cancel ()
   "Terminate the search and go back to the starting point."
   (interactive)
+  (if (functionp (isearch-pop-fun-state (car (last isearch-cmds))))
+      (funcall (isearch-pop-fun-state (car (last isearch-cmds)))
+               (car (last isearch-cmds))))
   (goto-char isearch-opoint)
-  (isearch-done t)
+  (isearch-done t)                      ; exit isearch
   (isearch-clean-overlays)
-  (signal 'quit nil))  ; and pass on quit signal
+  (signal 'quit nil))                   ; and pass on quit signal
 
 (defun isearch-abort ()
   "Abort incremental search mode if searching is successful, signaling quit.
@@ -1039,11 +1025,9 @@ Use `isearch-exit' to quit without signaling."
   (if isearch-success
       ;; If search is successful, move back to starting point
       ;; and really do quit.
-      (progn (goto-char isearch-opoint)
-            (setq isearch-success nil)
-            (isearch-done t)   ; exit isearch
-            (isearch-clean-overlays)
-            (signal 'quit nil))  ; and pass on quit signal
+      (progn
+        (setq isearch-success nil)
+        (isearch-cancel))
     ;; If search is failing, or has an incomplete regexp,
     ;; rub out until it is once more successful.
     (while (or (not isearch-success) isearch-invalid-regexp)
@@ -1068,7 +1052,9 @@ Use `isearch-exit' to quit without signaling."
        ;; If already have what to search for, repeat it.
        (or isearch-success
            (progn
-             (goto-char (if isearch-forward (point-min) (point-max)))
+             (if isearch-wrap-function
+                 (funcall isearch-wrap-function)
+               (goto-char (if isearch-forward (point-min) (point-max))))
              (setq isearch-wrapped t))))
     ;; C-s in reverse or C-r in forward, change direction.
     (setq isearch-forward (not isearch-forward)))
@@ -1110,6 +1096,7 @@ Use `isearch-exit' to quit without signaling."
   (interactive)
   (setq isearch-regexp (not isearch-regexp))
   (if isearch-regexp (setq isearch-word nil))
+  (setq isearch-success t isearch-adjusted t)
   (isearch-update))
 
 (defun isearch-toggle-case-fold ()
@@ -1122,34 +1109,39 @@ Use `isearch-exit' to quit without signaling."
             (isearch-message-prefix nil nil isearch-nonincremental)
             isearch-message
             (if isearch-case-fold-search "in" "")))
-  (setq isearch-adjusted t)
+  (setq isearch-success t isearch-adjusted t)
   (sit-for 1)
   (isearch-update))
 
-(defun isearch-query-replace ()
+(defun isearch-query-replace (&optional regexp-flag)
   "Start query-replace with string to replace from last search string."
   (interactive)
   (barf-if-buffer-read-only)
+  (if regexp-flag (setq isearch-regexp t))
   (let ((case-fold-search isearch-case-fold-search))
     (isearch-done)
     (isearch-clean-overlays)
-    (and isearch-forward isearch-other-end (goto-char isearch-other-end))
+    (if (and (< isearch-other-end (point))
+             (not (and transient-mark-mode mark-active
+                       (< isearch-opoint (point)))))
+        (goto-char isearch-other-end))
+    (set query-replace-from-history-variable
+         (cons isearch-string
+               (symbol-value query-replace-from-history-variable)))
     (perform-replace
      isearch-string
-     (query-replace-read-to isearch-string "Query replace" isearch-regexp)
-     t isearch-regexp isearch-word)))
+     (query-replace-read-to
+      isearch-string
+      (if isearch-regexp "Query replace regexp" "Query replace")
+      isearch-regexp)
+     t isearch-regexp isearch-word nil nil
+     (if (and transient-mark-mode mark-active) (region-beginning))
+     (if (and transient-mark-mode mark-active) (region-end)))))
 
 (defun isearch-query-replace-regexp ()
   "Start query-replace-regexp with string to replace from last search string."
   (interactive)
-  (let ((query-replace-interactive t)
-        (case-fold-search isearch-case-fold-search))
-    ;; Put search string into the right ring
-    (setq isearch-regexp t)
-    (isearch-done)
-    (isearch-clean-overlays)
-    (and isearch-forward isearch-other-end (goto-char isearch-other-end))
-    (call-interactively 'query-replace-regexp)))
+  (isearch-query-replace t))
 
 \f
 (defun isearch-delete-char ()
@@ -1267,8 +1259,8 @@ might return the position of the end of the line."
 (defun isearch-yank-line ()
   "Pull rest of line from buffer into search string."
   (interactive)
-  (isearch-yank-internal 'line-end-position))
-
+  (isearch-yank-internal
+   (lambda () (line-end-position (if (eolp) 2 1)))))
 
 (defun isearch-search-and-update ()
   ;; Do the search and update the display.
@@ -1347,7 +1339,7 @@ barrier."
             ;; We have to check 2 stack frames because the last might be
             ;; invalid just because of a backslash.
             (or (not isearch-invalid-regexp)
-                (not (isearch-invalid-regexp (cadr isearch-cmds)))
+                (not (isearch-invalid-regexp-state (cadr isearch-cmds)))
                 allow-invalid))
     (if to-barrier
        (progn (goto-char isearch-barrier)
@@ -1362,8 +1354,8 @@ barrier."
        ;; Also skip over postfix operators -- though horrid,
        ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly legal.
        (while (and previous
-                   (or (isearch-invalid-regexp frame)
-                       (let* ((string (isearch-string frame))
+                   (or (isearch-invalid-regexp-state frame)
+                       (let* ((string (isearch-string-state frame))
                               (lchar (aref string (1- (length string)))))
                          ;; The operators aren't always operators; check
                          ;; backslashes.  This doesn't handle the case of
@@ -1371,7 +1363,7 @@ barrier."
                          ;; being special, but then we should fall back to
                          ;; the barrier anyway because it's all optional.
                          (if (isearch-backslash
-                              (isearch-string (car previous)))
+                              (isearch-string-state (car previous)))
                              (eq lchar ?\})
                            (memq lchar '(?* ?? ?+))))))
          (setq stack previous previous (cdr previous) frame (car stack)))
@@ -1379,7 +1371,7 @@ barrier."
          ;; `stack' now refers the most recent valid regexp that is not at
          ;; all optional in its last term.  Now dig one level deeper and find
          ;; what matched before that.
-         (let ((last-other-end (or (isearch-other-end (car previous))
+         (let ((last-other-end (or (isearch-other-end-state (car previous))
                                    isearch-barrier)))
            (goto-char (if isearch-forward
                           (max last-other-end isearch-barrier)
@@ -1642,8 +1634,7 @@ Isearch mode."
            (let ((ab-bel (isearch-string-out-of-window isearch-point)))
              (if ab-bel
                  (isearch-back-into-window (eq ab-bel 'above) isearch-point)
-               (or (eq (point) isearch-point)
-                   (goto-char isearch-point))))
+               (goto-char isearch-point)))
            (isearch-update))
          (search-exit-option
           (let (window)
@@ -1690,11 +1681,15 @@ Isearch mode."
     ;; Assume character codes 0200 - 0377 stand for characters in some
     ;; single-byte character set, and convert them to Emacs
     ;; characters.
-    (and enable-multibyte-characters
-        (>= char ?\200)
-        (<= char ?\377)
-        (setq char (unibyte-char-to-multibyte char)))
-    (isearch-process-search-char char)))
+    (if (and isearch-regexp (= char ?\ ))
+       (if (subregexp-context-p isearch-string pos)
+           (isearch-process-search-string "[ ]" " ")
+         (isearch-process-search-char char))
+      (and enable-multibyte-characters
+          (>= char ?\200)
+          (<= char ?\377)
+          (setq char (unibyte-char-to-multibyte char)))
+      (isearch-process-search-char char))))
 
 (defun isearch-return-char ()
   "Convert return into newline for incremental search.
@@ -1718,22 +1713,6 @@ Obsolete."
          (isearch-process-search-multibyte-characters char)
        (isearch-process-search-char char)))))
 
-(defun isearch-whitespace-chars ()
-  "Match all whitespace chars, if in regexp mode.
-If you want to search for just a space, type \\<isearch-mode-map>\\[isearch-quote-char] SPC."
-  (interactive)
-  (if isearch-regexp
-      (if (and search-whitespace-regexp (not isearch-within-brackets)
-              (not isearch-invalid-regexp))
-         (isearch-process-search-string search-whitespace-regexp " ")
-       (isearch-printing-char))
-    (progn
-      ;; This way of doing word search doesn't correctly extend current search.
-      ;;      (setq isearch-word t)
-      ;;      (setq isearch-adjusted t)
-      ;;      (goto-char isearch-barrier)
-      (isearch-printing-char))))
-
 (defun isearch-process-search-char (char)
   ;; Append the char to the search string, update the message and re-search.
   (isearch-process-search-string
@@ -1917,7 +1896,9 @@ If there is no completion possible, say so and continue searching."
   ;; If currently failing, display no ellipsis.
   (or isearch-success (setq ellipsis nil))
   (let ((m (concat (if isearch-success "" "failing ")
+                  (if isearch-adjusted "pending " "")
                   (if (and isearch-wrapped
+                           (not isearch-wrap-function)
                            (if isearch-forward
                                (> (point) isearch-opoint)
                              (< (point) isearch-opoint)))
@@ -1972,6 +1953,7 @@ Can be changed via `isearch-search-fun-function' for special needs."
       (let ((inhibit-point-motion-hooks search-invisible)
            (inhibit-quit nil)
            (case-fold-search isearch-case-fold-search)
+           (search-spaces-regexp search-whitespace-regexp)
            (retry t))
        (if isearch-regexp (setq isearch-invalid-regexp nil))
        (setq isearch-within-brackets nil)
@@ -2012,9 +1994,11 @@ Can be changed via `isearch-search-fun-function' for special needs."
   (if isearch-success
       nil
     ;; Ding if failed this time after succeeding last time.
-    (and (isearch-success (car isearch-cmds))
+    (and (isearch-success-state (car isearch-cmds))
         (ding))
-    (goto-char (isearch-point (car isearch-cmds)))))
+    (if (functionp (isearch-pop-fun-state (car isearch-cmds)))
+        (funcall (isearch-pop-fun-state (car isearch-cmds)) (car isearch-cmds)))
+    (goto-char (isearch-point-state (car isearch-cmds)))))
 
 
 ;; Called when opening an overlay, and we are still in isearch.
@@ -2375,14 +2359,16 @@ search string to change or the window to scroll)."
            isearch-lazy-highlight-case-fold-search isearch-case-fold-search
            isearch-lazy-highlight-regexp       isearch-regexp
             isearch-lazy-highlight-wrapped      nil)
-      (setq isearch-lazy-highlight-timer
-            (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
-                                 'isearch-lazy-highlight-update)))))
+      (unless (equal isearch-string "")
+       (setq isearch-lazy-highlight-timer
+             (run-with-idle-timer isearch-lazy-highlight-initial-delay nil
+                                  'isearch-lazy-highlight-update))))))
 
 (defun isearch-lazy-highlight-search ()
   "Search ahead for the next or previous match, for lazy highlighting.
 Attempt to do the search exactly the way the pending isearch would."
-  (let ((case-fold-search isearch-case-fold-search))
+  (let ((case-fold-search isearch-case-fold-search)
+       (search-spaces-regexp search-whitespace-regexp))
     (funcall (isearch-search-fun)
              isearch-string
              (if isearch-forward
@@ -2414,7 +2400,17 @@ Attempt to do the search exactly the way the pending isearch would."
                 (let ((mb (match-beginning 0))
                       (me (match-end 0)))
                   (if (= mb me)      ;zero-length match
-                      (forward-char 1)
+                     (if isearch-forward
+                         (if (= mb (if isearch-lazy-highlight-wrapped
+                                       isearch-lazy-highlight-start
+                                     (window-end)))
+                             (setq found nil)
+                           (forward-char 1))
+                       (if (= mb (if isearch-lazy-highlight-wrapped
+                                     isearch-lazy-highlight-end
+                                   (window-start)))
+                           (setq found nil)
+                         (forward-char -1)))
 
                     ;; non-zero-length match
                     (let ((ov (make-overlay mb me)))
@@ -2424,19 +2420,20 @@ Attempt to do the search exactly the way the pending isearch would."
                       (push ov isearch-lazy-highlight-overlays)))
                   (if isearch-forward
                       (setq isearch-lazy-highlight-end (point))
-                    (setq isearch-lazy-highlight-start (point))))
-
-              ;; not found
-              (if isearch-lazy-highlight-wrapped
-                  (setq looping nil
-                        nomore  t)
-                (setq isearch-lazy-highlight-wrapped t)
-                (if isearch-forward
-                    (progn
-                      (setq isearch-lazy-highlight-end (window-start))
-                      (goto-char (window-start)))
-                  (setq isearch-lazy-highlight-start (window-end))
-                  (goto-char (window-end)))))))
+                    (setq isearch-lazy-highlight-start (point)))))
+
+           ;; not found or zero-length match at the search bound
+           (if (not found)
+               (if isearch-lazy-highlight-wrapped
+                   (setq looping nil
+                         nomore  t)
+                 (setq isearch-lazy-highlight-wrapped t)
+                 (if isearch-forward
+                     (progn
+                       (setq isearch-lazy-highlight-end (window-start))
+                       (goto-char (window-start)))
+                   (setq isearch-lazy-highlight-start (window-end))
+                   (goto-char (window-end)))))))
         (unless nomore
           (setq isearch-lazy-highlight-timer
                 (run-at-time isearch-lazy-highlight-interval nil