Just now I wanted to count all the blank lines in a 909-line file.
I tried M-x count-matches with ^$ and it said there were 10 matches but I KNOW there are a lot more blank lines than that.
On 2017-06-12, Bruce Mardle <marblypup@yahoo.co.uk> wrote:
Just now I wanted to count all the blank lines in a 909-line file.
I tried M-x count-matches with ^$ and it said there were 10 matches but I KNOW there are a lot more blank lines than that.
Are you sure none of the blank lines have spacess in them?
Was point at the start of the file when you did it?
On Monday, 12 June 2017 08:25:03 UTC+1, Julian Bradfield wrote:
On 2017-06-12, Bruce Mardle <marblypup@yahoo.co.uk> wrote:
Just now I wanted to count all the blank lines in a 909-line file.
I tried M-x count-matches with ^$ and it said there were 10 matches
but I KNOW there are a lot more blank lines than that.
Are you sure none of the blank lines have spacess in them?
Was point at the start of the file when you did it?
Thanks for the reply. Yes (I wrote a C program to count them) and yes.
Thanks for the reply. Yes (I wrote a C program to count them) and yes.
Hi, everyone.
Just now I wanted to count all the blank lines in a 909-line file.
I tried M-x count-matches with ^$ and it said there were 10 matches
but I KNOW there are a lot more blank lines than that.
Eventually it occurred to me to count-matches with ^.+$
That returned the correct number of non-blank lines (551).
What was wrong with my ^$ ?
Bruce Mardle <marblypup@yahoo.co.uk> writes:
Hi, everyone.
Just now I wanted to count all the blank lines in a 909-line file.
I tried M-x count-matches with ^$ and it said there were 10 matches
but I KNOW there are a lot more blank lines than that.
Eventually it occurred to me to count-matches with ^.+$
That returned the correct number of non-blank lines (551).
What was wrong with my ^$ ?
It seems that consecutive empty lines are counted incorrectly. Only the first line of a group is count. Seems it's a flaw in the
implementation. I suggest to M-x report-emacs-bug.
Michael.
You may want to attach the patch, it's a simple paren-in-wrong-place
bug. 'count' needs to get increased every time, not only when the match spans more than zero chars.
--- 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")))
HTH
R'
* Bruce Mardle <marblypup@yahoo.co.uk>
| On Tuesday, 13 June 2017 17:52:02 UTC+1, Michael Heerdegen wrote:
| > Bruce Mardle <marblypup@yahoo.co.uk> writes:
| >
--<snip-snip>--
| > >
| > > What was wrong with my ^$ ?
| >
| > It seems that consecutive empty lines are counted incorrectly. Only
| > the first line of a group is count. Seems it's a flaw in the
| > implementation. I suggest to M-x report-emacs-bug.
| Thanks to everyone who replied. That's exactly right, Michael! I'll
| report it. isearch-forward-regexp treats blank lines separately with ^S
You may want to attach the patch, it's a simple paren-in-wrong-place
bug. 'count' needs to get increased every time, not only when the match spans more than zero chars.
--- 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")))
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).
- (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)))
Just now I wanted to count all the blank lines in a 909-line file.<snip>
What was wrong with my ^$ ?
Bruce Mardle <marblypup@yahoo.co.uk> writes:
<snip>
Just now I wanted to count all the blank lines in a 909-line file.<snip>
What was wrong with my ^$ ?
As an entirely pragmatic matter, you can count blank lines even with the current buggy commands using ^ C-Q C-J (that's control q to quote and
control j for newline). This seems to work even in buffers with DOS
line endings (presumably they are stripped and added on reading and
writing) and it won't spuriously count the last line (unless it really
is blank).
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 459 |
Nodes: | 16 (2 / 14) |
Uptime: | 03:22:45 |
Calls: | 9,345 |
Calls today: | 3 |
Files: | 13,539 |
Messages: | 6,082,418 |