* Ben Bacarisse <***@bsb.me.uk>
| Ralf Fassel <***@gmx.de> writes:
| > --- emacs/24.3/lisp/replace.el 2017/06/14 12:40:49 1.1
| > +++ emacs/24.3/lisp/replace.el 2017/06/14 12:41:02
| > @@ -804,8 +804,8 @@
| > (progn (setq opoint (point))
| > (re-search-forward regexp rend t)))
| > (if (= opoint (point))
| > - (forward-char 1)
| > - (setq count (1+ count))))
| > + (forward-char 1))
| > + (setq count (1+ count)))
| > (when interactive (message "%d occurrence%s"
| > count
| > (if (= count 1) "" "s")))
| I'm not sure it's as simple as that. I tried the same fix but did not
| suggest it because it does not match my idea if blank lines and I had no
| time to investigate further.
| With that patch, a file containing the five hex bytes 78 0A 0A 78 0A is
| reported as having three blank lines. One blank line is always counted
| at the end of the buffer (unless it has zero bytes) but I'm not sure
| where the others come come. Do you get the same results?
Yes, you're right. The logic in 'how-many' seems flawed, since it
advances point only if the match itself did not advance point.
Therefore the first match leaves point at the blank line, but since we
advanced point for the match, point stays where it is, and the blank
line is matched again and is counted twice.
The third match is the final newline at the end of the buffer which also
matches "^$". This is true also for isearch-regexp-forward, which finds
two matches (which I consider wrong, too, since the trailing newline
somehow belongs to the line before that, but this seems to be a
different story).
The following version also looks at match-beginning/match-end to
determine whether to advance point after the match, and I'll leave it to
the experts to determine whether the whole 'opoint' business in there
might go away completely.
--- replace.el 2017/06/14 17:13:35 1.1
+++ replace.el 2017/06/14 17:13:57
@@ -803,9 +803,11 @@
(while (and (< (point) rend)
(progn (setq opoint (point))
(re-search-forward regexp rend t)))
- (if (= opoint (point))
- (forward-char 1)
- (setq count (1+ count))))
+ (if (or (= opoint (point))
+ (and (= (match-beginning 0) (match-end 0))
+ (not (= (point) rend))))
+ (forward-char 1))
+ (setq count (1+ count)))
(when interactive (message "%d occurrence%s"
count
(if (= count 1) "" "s")))
HTH
R'