Function: count-screen-lines
count-screen-lines is a byte-compiled function defined in
window.el.gz.
Signature
(count-screen-lines &optional BEG END COUNT-FINAL-NEWLINE WINDOW)
Documentation
Return the number of screen lines in the region.
The number of screen lines may be different from the number of actual lines, due to line breaking, display table, etc.
Optional arguments BEG and END default to point-min and point-max
respectively.
If region ends with a newline, ignore it unless optional third argument COUNT-FINAL-NEWLINE is non-nil.
The optional fourth argument WINDOW specifies the window used for obtaining parameters such as width, horizontal scrolling, and so on. The default is to use the selected window's parameters.
Like vertical-motion, count-screen-lines always uses the current buffer,
regardless of which buffer is displayed in WINDOW. This makes possible to use
count-screen-lines in any buffer, whether or not it is currently displayed
in some window.
Probably introduced at or before Emacs version 21.1.
Source Code
;; Defined in /usr/src/emacs/lisp/window.el.gz
(defun count-screen-lines (&optional beg end count-final-newline window)
"Return the number of screen lines in the region.
The number of screen lines may be different from the number of actual lines,
due to line breaking, display table, etc.
Optional arguments BEG and END default to `point-min' and `point-max'
respectively.
If region ends with a newline, ignore it unless optional third argument
COUNT-FINAL-NEWLINE is non-nil.
The optional fourth argument WINDOW specifies the window used for obtaining
parameters such as width, horizontal scrolling, and so on. The default is
to use the selected window's parameters.
Like `vertical-motion', `count-screen-lines' always uses the current buffer,
regardless of which buffer is displayed in WINDOW. This makes possible to use
`count-screen-lines' in any buffer, whether or not it is currently displayed
in some window."
(unless beg
(setq beg (point-min)))
(unless end
(setq end (point-max)))
(if (= beg end)
0
(let ((start (min beg end))
(finish (max beg end))
count end-invisible-p)
;; When END is invisible because lines are truncated in WINDOW,
;; vertical-motion returns a number that is 1 larger than it
;; should. We need to fix that.
(setq end-invisible-p
(and (or truncate-lines (truncated-partial-width-window-p window))
(save-excursion
(goto-char finish)
(> (- (current-column) (window-hscroll window))
(window-body-width window)))))
(save-excursion
(save-restriction
(widen)
(narrow-to-region start
(if (and (not count-final-newline)
(= ?\n (char-before finish)))
(1- finish)
finish))
(goto-char start)
(setq count (vertical-motion (buffer-size) window))
(if end-invisible-p count (1+ count)))))))