(lazy-highlight-cleanup) <command>: Rename from
[bpt/emacs.git] / lisp / isearch.el
index 3a7ca9c..2a95227 100644 (file)
@@ -120,11 +120,6 @@ a tab, a carriage return (control-M), a newline, and `]+'."
   :type 'regexp
   :group 'isearch)
 
-(defcustom search-highlight t
-  "*Non-nil means incremental search highlights the current match."
-  :type 'boolean
-  :group 'isearch)
-
 (defcustom search-invisible 'open
   "If t incremental search can match hidden text.
 nil means don't match invisible text.
@@ -155,15 +150,6 @@ command history."
   :type 'boolean
   :group 'isearch)
 
-(defcustom isearch-lazy-highlight t
-  "*Controls the lazy-highlighting during incremental search.
-When non-nil, all text in the buffer matching the current search
-string is highlighted lazily (see `lazy-highlight-initial-delay'
-and `lazy-highlight-interval')."
-  :type 'boolean
-  :group 'lazy-highlight
-  :group 'isearch)
-
 (defvar isearch-mode-hook nil
   "Function(s) to call after starting up an incremental search.")
 
@@ -208,30 +194,69 @@ Default value, nil, means edit the string instead."
   :type 'boolean
   :group 'isearch)
 
+;;; isearch highlight customization.
+
+(defcustom search-highlight t
+  "*Non-nil means incremental search highlights the current match."
+  :type 'boolean
+  :group 'isearch)
+
+(defface isearch
+  '((((class color) (min-colors 88) (background light))
+     ;; The background must not be too dark, for that means
+     ;; the character is hard to see when the cursor is there.
+     (:background "magenta2" :foreground "lightskyblue1"))
+    (((class color) (min-colors 88) (background dark))
+     (:background "palevioletred2" :foreground "brown4"))
+    (((class color) (min-colors 16))
+     (:background "magenta4" :foreground "cyan1"))
+    (((class color) (min-colors 8))
+     (:background "magenta4" :foreground "cyan1"))
+    (t (:inverse-video t)))
+  "Face for highlighting Isearch matches."
+  :group 'isearch)
+(defvar isearch 'isearch)
+
+(defcustom isearch-lazy-highlight t
+  "*Controls the lazy-highlighting during incremental search.
+When non-nil, all text in the buffer matching the current search
+string is highlighted lazily (see `lazy-highlight-initial-delay'
+and `lazy-highlight-interval')."
+  :type 'boolean
+  :group 'lazy-highlight
+  :group 'isearch)
+
 ;;; Lazy highlight customization.
+
 (defgroup lazy-highlight nil
   "Lazy highlighting feature for matching strings."
   :prefix "lazy-highlight-"
   :version "21.1"
   :group 'isearch
-  :group 'replace)
+  :group 'matching)
 
 (defcustom lazy-highlight-cleanup t
   "*Controls whether to remove extra highlighting after a search.
 If this is nil, extra highlighting can be \"manually\" removed with
-\\[isearch-lazy-highlight-cleanup]."
+\\[lazy-highlight-cleanup]."
   :type 'boolean
   :group 'lazy-highlight)
+(defvaralias 'isearch-lazy-highlight-cleanup 'lazy-highlight-cleanup)
+(make-obsolete-variable 'isearch-lazy-highlight-cleanup 'lazy-highlight-cleanup "22.1")
 
 (defcustom lazy-highlight-initial-delay 0.25
   "*Seconds to wait before beginning to lazily highlight all matches."
   :type 'number
   :group 'lazy-highlight)
+(defvaralias 'isearch-lazy-highlight-initial-delay 'lazy-highlight-initial-delay)
+(make-obsolete-variable 'isearch-lazy-highlight-initial-delay 'lazy-highlight-initial-delay "22.1")
 
 (defcustom lazy-highlight-interval 0 ; 0.0625
   "*Seconds between lazily highlighting successive matches."
   :type 'number
   :group 'lazy-highlight)
+(defvaralias 'isearch-lazy-highlight-interval 'lazy-highlight-interval)
+(make-obsolete-variable 'isearch-lazy-highlight-interval 'lazy-highlight-interval "22.1")
 
 (defcustom lazy-highlight-max-at-a-time 20
   "*Maximum matches to highlight at a time (for `lazy-highlight').
@@ -241,6 +266,8 @@ A value of nil means highlight all matches."
   :type '(choice (const :tag "All" nil)
                 (integer :tag "Some"))
   :group 'lazy-highlight)
+(defvaralias 'isearch-lazy-highlight-max-at-a-time 'lazy-highlight-max-at-a-time)
+(make-obsolete-variable 'isearch-lazy-highlight-max-at-a-time 'lazy-highlight-max-at-a-time "22.1")
 
 (defface lazy-highlight
   '((((class color) (min-colors 88) (background light))
@@ -253,8 +280,11 @@ A value of nil means highlight all matches."
      (:background "turquoise3"))
     (t (:underline t)))
   "Face for lazy highlighting of matches other than the current one."
-  :group 'isearch-faces
   :group 'lazy-highlight)
+(put 'isearch-lazy-highlight-face 'face-alias 'lazy-highlight)
+(defvar lazy-highlight-face 'lazy-highlight)
+(defvaralias 'isearch-lazy-highlight-face 'lazy-highlight-face)
+(make-obsolete-variable 'isearch-lazy-highlight-face 'lazy-highlight-face "22.1")
 \f
 ;; Define isearch-mode keymap.
 
@@ -407,9 +437,8 @@ Each set is a vector of the form:
 (defvar isearch-string "")  ; The current search string.
 (defvar isearch-message "") ; text-char-description version of isearch-string
 
-(defvar isearch-success t)             ; Searching is currently successful.
-(defvar isearch-invalid-regexp nil)    ; Regexp not well formed.
-(defvar isearch-within-brackets nil)   ; Regexp has unclosed [.
+(defvar isearch-success t)     ; Searching is currently successful.
+(defvar isearch-error nil)     ; Error message for failed search.
 (defvar isearch-other-end nil) ; Start (end) of match if forward (backward).
 (defvar isearch-wrapped nil)   ; Searching restarted from the top (bottom).
 (defvar isearch-barrier 0)
@@ -507,6 +536,11 @@ Type \\[isearch-quote-char] to quote control character to search for it.
 \\[isearch-abort] when search is successful aborts and moves point to\
  starting point.
 
+Type \\[isearch-query-replace] to start `query-replace' with string to\
+replace from  last search string.
+Type \\[isearch-query-replace-regexp] to start `query-replace-regexp'\
+with string to replace from last search string..
+
 Type \\[isearch-toggle-case-fold] to toggle search case-sensitivity.
 Type \\[isearch-toggle-regexp] to toggle regular-expression mode.
 Type \\[isearch-edit-string] to edit the search string in the minibuffer.
@@ -605,8 +639,7 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
        isearch-barrier (point)
        isearch-adjusted nil
        isearch-yank-flag nil
-       isearch-invalid-regexp nil
-       isearch-within-brackets nil
+       isearch-error nil
        isearch-slow-terminal-mode (and (<= baud-rate search-slow-speed)
                                        (> (window-height)
                                           (* 4
@@ -710,7 +743,8 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
   (setq ;; quit-flag nil  not for isearch-mode
    isearch-adjusted nil
    isearch-yank-flag nil)
-  (if isearch-lazy-highlight (isearch-lazy-highlight-new-loop))
+  (when isearch-lazy-highlight
+    (isearch-lazy-highlight-new-loop nil nil))
   ;; We must prevent the point moving to the end of composition when a
   ;; part of the composition has just been searched.
   (setq disable-point-adjustment t))
@@ -734,7 +768,7 @@ is treated as a regexp.  See \\[isearch-forward] for more info."
   ;; (setq pre-command-hook isearch-old-pre-command-hook) ; for lemacs
   (setq minibuffer-message-timeout isearch-original-minibuffer-message-timeout)
   (isearch-dehighlight)
-  (isearch-lazy-highlight-cleanup lazy-highlight-cleanup)
+  (lazy-highlight-cleanup lazy-highlight-cleanup)
   (let ((found-start (window-start (selected-window)))
        (found-point (point)))
     (if isearch-window-configuration
@@ -831,7 +865,7 @@ REGEXP says which ring to use."
 (defsubst isearch-word-state (frame)
   "Return the search-by-word flag in FRAME."
   (aref frame 6))
-(defsubst isearch-invalid-regexp-state (frame)
+(defsubst isearch-error-state (frame)
   "Return the regexp error message in FRAME, or nil if its regexp is valid."
   (aref frame 7))
 (defsubst isearch-wrapped-state (frame)
@@ -840,15 +874,12 @@ REGEXP says which ring to use."
 (defsubst isearch-barrier-state (frame)
   "Return the barrier value in FRAME."
   (aref frame 9))
-(defsubst isearch-within-brackets-state (frame)
-  "Return the in-character-class flag in FRAME."
-  (aref frame 10))
 (defsubst isearch-case-fold-search-state (frame)
   "Return the case-folding flag in FRAME."
-  (aref frame 11))
+  (aref frame 10))
 (defsubst isearch-pop-fun-state (frame)
   "Return the function restoring the mode-specific isearch state in FRAME."
-  (aref frame 12))
+  (aref frame 11))
 
 (defun isearch-top-state ()
   (let ((cmd (car isearch-cmds)))
@@ -858,10 +889,9 @@ REGEXP says which ring to use."
          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-error (isearch-error-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))
@@ -876,8 +906,8 @@ REGEXP says which ring to use."
        (cons (vector isearch-string isearch-message (point)
                      isearch-success isearch-forward isearch-other-end
                      isearch-word
-                     isearch-invalid-regexp isearch-wrapped isearch-barrier
-                     isearch-within-brackets isearch-case-fold-search
+                     isearch-error isearch-wrapped isearch-barrier
+                     isearch-case-fold-search
                      (if isearch-push-state-function
                          (funcall isearch-push-state-function)))
              isearch-cmds)))
@@ -940,8 +970,7 @@ If first char entered is \\[isearch-yank-word-or-char], then do word search inst
              (isearch-barrier isearch-barrier)
              (isearch-adjusted isearch-adjusted)
              (isearch-yank-flag isearch-yank-flag)
-             (isearch-invalid-regexp isearch-invalid-regexp)
-             (isearch-within-brackets isearch-within-brackets)
+             (isearch-error isearch-error)
   ;;; Don't bind this.  We want isearch-search, below, to set it.
   ;;; And the old value won't matter after that.
   ;;;      (isearch-other-end isearch-other-end)
@@ -1088,7 +1117,7 @@ Use `isearch-exit' to quit without signaling."
         (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)
+    (while (or (not isearch-success) isearch-error)
       (isearch-pop-state))
     (isearch-update)))
 
@@ -1110,10 +1139,11 @@ Use `isearch-exit' to quit without signaling."
        ;; If already have what to search for, repeat it.
        (or isearch-success
            (progn
+             ;; Set isearch-wrapped before calling isearch-wrap-function
+             (setq isearch-wrapped t)
              (if isearch-wrap-function
                  (funcall isearch-wrap-function)
-               (goto-char (if isearch-forward (point-min) (point-max))))
-             (setq isearch-wrapped t))))
+               (goto-char (if isearch-forward (point-min) (point-max)))))))
     ;; C-s in reverse or C-r in forward, change direction.
     (setq isearch-forward (not isearch-forward)))
 
@@ -1348,8 +1378,7 @@ might return the position of the end of the line."
                     (min isearch-opoint isearch-barrier))))
        (progn
          (setq isearch-success t
-               isearch-invalid-regexp nil
-               isearch-within-brackets nil
+               isearch-error nil
                isearch-other-end (match-end 0))
          (if (and (eq isearch-case-fold-search t) search-upper-case)
              (setq isearch-case-fold-search
@@ -1396,8 +1425,8 @@ barrier."
                                (not want-backslash))
             ;; 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-state (cadr isearch-cmds)))
+            (or (not isearch-error)
+                (not (isearch-error-state (cadr isearch-cmds)))
                 allow-invalid))
     (if to-barrier
        (progn (goto-char isearch-barrier)
@@ -1412,7 +1441,7 @@ barrier."
        ;; Also skip over postfix operators -- though horrid,
        ;; 'ab?\{5,6\}+\{1,2\}*' is perfectly legal.
        (while (and previous
-                   (or (isearch-invalid-regexp-state frame)
+                   (or (isearch-error-state frame)
                        (let* ((string (isearch-string-state frame))
                               (lchar (aref string (1- (length string)))))
                          ;; The operators aren't always operators; check
@@ -1429,8 +1458,10 @@ 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-state (car previous))
-                                   isearch-barrier)))
+         (let ((last-other-end
+                (or (and (car previous)
+                         (isearch-other-end-state (car previous)))
+                    isearch-barrier)))
            (goto-char (if isearch-forward
                           (max last-other-end isearch-barrier)
                         (min last-other-end isearch-barrier)))
@@ -1524,6 +1555,7 @@ Scroll-bar or mode-line events are processed appropriately."
 (put 'delete-other-windows 'isearch-scroll t)
 (put 'balance-windows 'isearch-scroll t)
 (put 'split-window-vertically 'isearch-scroll t)
+(put 'split-window-horizontally 'isearch-scroll t)
 (put 'enlarge-window 'isearch-scroll t)
 
 ;; Universal argument commands
@@ -1945,11 +1977,10 @@ If there is no completion possible, say so and continue searching."
   ;; If about to search, and previous search regexp was invalid,
   ;; check that it still is.  If it is valid now,
   ;; let the message we display while searching say that it is valid.
-  (and isearch-invalid-regexp ellipsis
+  (and isearch-error ellipsis
        (condition-case ()
           (progn (re-search-forward isearch-string (point) t)
-                 (setq isearch-invalid-regexp nil
-                       isearch-within-brackets nil))
+                 (setq isearch-error nil))
         (error nil)))
   ;; If currently failing, display no ellipsis.
   (or isearch-success (setq ellipsis nil))
@@ -1975,8 +2006,8 @@ If there is no completion possible, say so and continue searching."
 
 (defun isearch-message-suffix (&optional c-q-hack ellipsis)
   (concat (if c-q-hack "^Q" "")
-         (if isearch-invalid-regexp
-             (concat " [" isearch-invalid-regexp "]")
+         (if isearch-error
+             (concat " [" isearch-error "]")
            "")))
 
 \f
@@ -2013,8 +2044,7 @@ Can be changed via `isearch-search-fun-function' for special needs."
            (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)
+       (setq isearch-error nil)
        (while retry
          (setq isearch-success
                (funcall
@@ -2038,16 +2068,19 @@ Can be changed via `isearch-search-fun-function' for special needs."
          (setq isearch-success nil))
 
     (invalid-regexp
-     (setq isearch-invalid-regexp (car (cdr lossage)))
-     (setq isearch-within-brackets (string-match "\\`Unmatched \\["
-                                                isearch-invalid-regexp))
+     (setq isearch-error (car (cdr lossage)))
      (if (string-match
          "\\`Premature \\|\\`Unmatched \\|\\`Invalid "
-         isearch-invalid-regexp)
-        (setq isearch-invalid-regexp "incomplete input")))
+         isearch-error)
+        (setq isearch-error "incomplete input")))
+
+    (search-failed
+     (setq isearch-success nil)
+     (setq isearch-error (nth 2 lossage)))
+
     (error
      ;; stack overflow in regexp search.
-     (setq isearch-invalid-regexp (format "%s" lossage))))
+     (setq isearch-error (format "%s" lossage))))
 
   (if isearch-success
       nil
@@ -2203,31 +2236,8 @@ Can be changed via `isearch-search-fun-function' for special needs."
              (setq isearch-hidden t)))))))
 
 \f
-;; Highlighting
-
-(defvar isearch-overlay nil)
-
-(defun isearch-highlight (beg end)
-  (unless (null search-highlight)
-    (cond (isearch-overlay
-          ;; Overlay already exists, just move it.
-          (move-overlay isearch-overlay beg end (current-buffer)))
-
-         (t
-          ;; Overlay doesn't exist, create it.
-          (setq isearch-overlay (make-overlay beg end))
-          (overlay-put isearch-overlay 'face isearch)
-           (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
-           ))))
-
-(defun isearch-dehighlight ()
-  (when isearch-overlay
-    (delete-overlay isearch-overlay)))
-
-
 ;; General utilities
 
-
 (defun isearch-no-upper-case-p (string regexp-flag)
   "Return t if there are no upper case chars in STRING.
 If REGEXP-FLAG is non-nil, disregard letters preceded by `\\' (but not `\\\\')
@@ -2259,6 +2269,27 @@ since they have special meaning in a regexp."
        (append char-or-events unread-command-events)))
 
 \f
+;; Highlighting
+
+(defvar isearch-overlay nil)
+
+(defun isearch-highlight (beg end)
+  (unless (null search-highlight)
+    (cond (isearch-overlay
+          ;; Overlay already exists, just move it.
+          (move-overlay isearch-overlay beg end (current-buffer)))
+
+         (t
+          ;; Overlay doesn't exist, create it.
+          (setq isearch-overlay (make-overlay beg end))
+          (overlay-put isearch-overlay 'face isearch)
+           (overlay-put isearch-overlay 'priority 1) ;higher than lazy overlays
+           ))))
+
+(defun isearch-dehighlight ()
+  (when isearch-overlay
+    (delete-overlay isearch-overlay)))
+\f
 ;; isearch-lazy-highlight feature
 ;; by Bob Glickstein <http://www.zanshin.com/~bobg/>
 
@@ -2281,34 +2312,13 @@ since they have special meaning in a regexp."
 ;;    `isearch-word' and `isearch-regexp';
 ;;  - the direction of the current search is expected to be given by
 ;;    `isearch-forward';
-;;  - the variable `isearch-invalid-regexp' is expected to be true
+;;  - the variable `isearch-error' is expected to be true
 ;;    iff `isearch-string' is an invalid regexp.
 
-(defgroup isearch-faces nil
-  "Lazy highlighting feature for incremental search."
-  :version "21.1"
-  :group 'isearch)
-
-(defface isearch
-  '((((class color) (min-colors 88) (background light))
-     ;; The background must not be too dark, for that means
-     ;; the character is hard to see when the cursor is there.
-     (:background "magenta2" :foreground "lightskyblue1"))
-    (((class color) (min-colors 88) (background dark))
-     (:background "palevioletred2" :foreground "brown4"))
-    (((class color) (min-colors 16))
-     (:background "magenta4" :foreground "cyan1"))
-    (((class color) (min-colors 8))
-     (:background "magenta4" :foreground "cyan1"))
-    (t (:inverse-video t)))
-  "Face for highlighting Isearch matches."
-  :group 'isearch-faces)
-(defvar isearch 'isearch)
-
-(defvar isearch-lazy-highlight-face 'lazy-highlight)
-
 (defvar isearch-lazy-highlight-overlays nil)
 (defvar isearch-lazy-highlight-wrapped nil)
+(defvar isearch-lazy-highlight-start-limit nil)
+(defvar isearch-lazy-highlight-end-limit nil)
 (defvar isearch-lazy-highlight-start nil)
 (defvar isearch-lazy-highlight-end nil)
 (defvar isearch-lazy-highlight-timer nil)
@@ -2319,7 +2329,7 @@ since they have special meaning in a regexp."
 (defvar isearch-lazy-highlight-case-fold-search nil)
 (defvar isearch-lazy-highlight-regexp nil)
 
-(defun isearch-lazy-highlight-cleanup (&optional force)
+(defun lazy-highlight-cleanup (&optional force)
   "Stop lazy highlighting and remove extra highlighting from current buffer.
 FORCE non-nil means do it whether or not `lazy-highlight-cleanup'
 is nil.  This function is called when exiting an incremental search if
@@ -2334,10 +2344,15 @@ is nil.  This function is called when exiting an incremental search if
     (cancel-timer isearch-lazy-highlight-timer)
     (setq isearch-lazy-highlight-timer nil)))
 
-(defun isearch-lazy-highlight-new-loop ()
+(defalias 'isearch-lazy-highlight-cleanup 'lazy-highlight-cleanup)
+(make-obsolete 'isearch-lazy-highlight-cleanup 'lazy-highlight-cleanup "22.1")
+
+(defun isearch-lazy-highlight-new-loop (beg end)
   "Cleanup any previous `lazy-highlight' loop and begin a new one.
-This happens when `isearch-update' is invoked (which can cause the
-search string to change or the window to scroll)."
+BEG and END specify the bounds within which highlighting should occur.
+This is called when `isearch-update' is invoked (which can cause the
+search string to change or the window to scroll).  It is also used
+by other Emacs features."
   (when (and (null executing-kbd-macro)
              (sit-for 0)         ;make sure (window-start) is credible
              (or (not (equal isearch-string
@@ -2353,8 +2368,10 @@ search string to change or the window to scroll)."
                  (not (= (window-end)   ; Window may have been split/joined.
                          isearch-lazy-highlight-window-end))))
     ;; something important did indeed change
-    (isearch-lazy-highlight-cleanup t) ;kill old loop & remove overlays
-    (when (not isearch-invalid-regexp)
+    (lazy-highlight-cleanup t) ;kill old loop & remove overlays
+    (when (not isearch-error)
+      (setq isearch-lazy-highlight-start-limit beg
+           isearch-lazy-highlight-end-limit end)
       (setq isearch-lazy-highlight-window       (selected-window)
             isearch-lazy-highlight-window-start (window-start)
             isearch-lazy-highlight-window-end   (window-end)
@@ -2374,16 +2391,20 @@ search string to change or the window to scroll)."
 Attempt to do the search exactly the way the pending isearch would."
   (let ((case-fold-search isearch-case-fold-search)
        (search-spaces-regexp search-whitespace-regexp))
-    (funcall (isearch-search-fun)
-             isearch-string
-             (if isearch-forward
-                 (if isearch-lazy-highlight-wrapped
-                     isearch-lazy-highlight-start
-                   (window-end))
-               (if isearch-lazy-highlight-wrapped
-                   isearch-lazy-highlight-end
-                 (window-start)))
-             t)))
+    (condition-case nil
+       (funcall (isearch-search-fun)
+                isearch-string
+                (if isearch-forward
+                    (min (or isearch-lazy-highlight-end-limit (point-max))
+                         (if isearch-lazy-highlight-wrapped
+                             isearch-lazy-highlight-start
+                           (window-end)))
+                  (max (or isearch-lazy-highlight-start-limit (point-min))
+                       (if isearch-lazy-highlight-wrapped
+                           isearch-lazy-highlight-end
+                         (window-start))))
+                t)
+      (error nil))))
 
 (defun isearch-lazy-highlight-update ()
   "Update highlighting of other matches for current search."
@@ -2425,7 +2446,7 @@ Attempt to do the search exactly the way the pending isearch would."
                        ;; non-zero-length match
                        (let ((ov (make-overlay mb me)))
                          (push ov isearch-lazy-highlight-overlays)
-                         (overlay-put ov 'face isearch-lazy-highlight-face)
+                         (overlay-put ov 'face lazy-highlight-face)
                          (overlay-put ov 'priority 0) ;lower than main overlay
                          (overlay-put ov 'window (selected-window))))
                      (if isearch-forward
@@ -2441,12 +2462,14 @@ Attempt to do the search exactly the way the pending isearch would."
                      (if isearch-forward
                          (progn
                            (setq isearch-lazy-highlight-end (window-start))
-                           (goto-char (window-start)))
+                           (goto-char (max (or isearch-lazy-highlight-start-limit (point-min))
+                                           (window-start))))
                        (setq isearch-lazy-highlight-start (window-end))
-                       (goto-char (window-end)))))))
+                       (goto-char (min (or isearch-lazy-highlight-end-limit (point-max))
+                                       (window-end))))))))
            (unless nomore
              (setq isearch-lazy-highlight-timer
-                   (run-at-time isearch-lazy-highlight-interval nil
+                   (run-at-time lazy-highlight-interval nil
                                 'isearch-lazy-highlight-update)))))))))
 
 (defun isearch-resume (search regexp word forward message case-fold)