]> code.delx.au - gnu-emacs/commitdiff
(view-search-no-match-lines): Add a doc string. Rewrite to simplify
authorGlenn Morris <rgm@gnu.org>
Tue, 25 Sep 2007 02:28:49 +0000 (02:28 +0000)
committerGlenn Morris <rgm@gnu.org>
Tue, 25 Sep 2007 02:28:49 +0000 (02:28 +0000)
and work better.

lisp/ChangeLog
lisp/view.el

index c9875cfdcca7c84a1254f2aaa5b906837dc28aef..5a0025f95fe873706f8b2047119ac2cf956c218c 100644 (file)
@@ -1,3 +1,8 @@
+2007-09-25  Glenn Morris  <rgm@gnu.org>
+
+       * view.el (view-search-no-match-lines): Add a doc string.
+       Rewrite to simplify and work better.
+
 2007-09-24  Dan Nicolaescu  <dann@ics.uci.edu>
 
        * progmodes/cc-mode.el (c-mode-base-map): Use
index f6722f537569b676d641f4ab5e4efd7a10eb76e2..04f288a75d2f4d2fc407872b9eff68dc2e253529 100644 (file)
@@ -990,27 +990,30 @@ for highlighting the match that is found."
               times (if no "no " "") regexp)
       (sit-for 4))))
 
+;; This is the dumb approach, looking at each line.  The original
+;; version of this function looked like it might have been trying to
+;; do something clever, but not succeeding:
+;; http://lists.gnu.org/archive/html/bug-gnu-emacs/2007-09/msg00073.html
 (defun view-search-no-match-lines (times regexp)
-  ;; Search for the TIMESt occurrence of line with no match for REGEXP.
-  (let ((back (and (< times 0) (setq times (- times)) -1))
-       n)
-    (while (> times 0)
-      (save-excursion (beginning-of-line (if back (- times) (1+ times)))
-                     (setq n (point)))
-      (setq times
-           (cond
-            ((< (count-lines (point) n) times) -1) ; Not enough lines.
-            ((or (null (re-search-forward regexp nil t back))
-                 (if back (and (< (match-end 0) n)
-                               (> (count-lines (match-end 0) n) 1))
-                   (and (< n (match-beginning 0))
-                        (> (count-lines n (match-beginning 0)) 1))))
-             0)                        ; No match within lines.
-            (back (count-lines (max n (match-beginning 0)) (match-end 0)))
-            (t (count-lines (match-beginning 0) (min n (match-end 0))))))
-      (goto-char n))
-    (and (zerop times) (looking-at "^.*$"))))
-
+  "Search for the TIMESth occurrence of a line with no match for REGEXP.
+If such a line is found, return non-nil and set the match-data to that line.
+If TIMES is negative, search backwards."
+  (let ((step 1)
+        (noerror 'move))
+    (when (< times 0)
+      (setq times (- times)
+            step -1
+            noerror t))
+    ;; Note that we do not check the current line.
+    (while (and (> times 0)
+                (zerop (forward-line step)))
+      ;; Move only to handle eob in the forward case: on last line,
+      ;; (forward-line 1) returns 0 before the end of line.
+      (or (re-search-forward regexp (line-end-position) noerror)
+          (setq times (1- times)))))
+  (when (zerop times)
+    (forward-line 0)
+    (looking-at ".*")))
 
 (provide 'view)