[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

count-lines and off-by-one error

From: Thibaut Verron
Subject: count-lines and off-by-one error
Date: Wed, 26 Aug 2020 14:29:58 +0200


The documentation for `count-lines' is the following:

> Return number of lines between START and END.
> This is usually the number of newlines between them,
> but can be one more if START is not equal to END
> and the greater of them is not at the start of a line.

I can vaguely understand this behaviour: it counts the number of non-empty lines between START and END... except that it also counts empty lines if they are in the middle. So, the number of lines, including the last one if non-empty. Is that so?

Is there no built-in function computing the number of newlines between two positions? There are multiple ways to do it, for example a subtraction between `line-number-at-pos' (which does call `count-lines' but returns the correct result), a wrapper around `count-lines' working around the edge cases, or a call to `count-matches'. But it seems to me that it's a common enough task to warrant a built-in method.

At the very least, `count-screen-lines' takes an optional argument specifying whether to _always_ count this extra line (so, even at the beginning of a line), maybe `count-lines' could get this behaviour too?

Thanks, best regards,

reply via email to

[Prev in Thread] Current Thread [Next in Thread]