- ;; 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 (if (>= times 0) 1
+ (setq times (- times))
+ -1)))
+ ;; Note that we do not check the current line.
+ (while (and (> times 0)
+ (zerop (forward-line step)))
+ ;; (forward-line 1) returns 0 on moving within the last line.
+ (if (eobp)
+ (setq times -1)
+ (or (re-search-forward regexp (line-end-position) t)
+ (setq times (1- times))))))
+ (and (zerop times)
+ (looking-at ".*")))