Attached is a patch to add this feature based on 25.0.94. Please consider applying it to Emacs. I have also included the full new version of the function below.
(defun count-lines (start end &optional ignore-invisible-lines-flag)
"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.
With optional IGNORE-INVISIBLE-LINES-FLAG non-nil,
lines collapsed with selective-display are excluded
from the line count."
(narrow-to-region start end)
(cond ((and (not ignore-invisible-lines-flag) (eq selective-display t))
(let ((done 0))
(while (re-search-forward "\n\\|\r[^\n]" nil t 40)
(setq done (+ 40 done)))
(while (re-search-forward "\n\\|\r[^\n]" nil t 1)
(setq done (+ 1 done)))
(if (and (/= start end)
(- (buffer-size) (forward-line (buffer-size))
(let ((invisible-count 0)
(while (re-search-forward "\n\\|\r[^\n]" nil t)
(setq prop (get-char-property (1- (point)) 'invisible))
(if (if (eq buffer-invisibility-spec t)
(or (memq prop buffer-invisibility-spec)
(assq prop buffer-invisibility-spec)))
(setq invisible-count (1+ invisible-count))))
(t (- (buffer-size) (forward-line (buffer-size))))))))
On Wed, Jun 1, 2016 at 10:23 AM, Robert Weiner <address@hidden
> Often when particular lines are hidden, one wants to count only visible lines. count-lines presently cannot do this. XEmacs has for a long-time had count-lines take an optional 3rd parameter flag which if non-nil, ignores invisible lines in the line count. This would be very helpful whenever selective-display is in use.