From 0ea1463e6796a5c0f8e10c4be0a891f417c0ae66 Mon Sep 17 00:00:00 2001 From: Eli Zaretskii Date: Sat, 2 May 2020 11:45:05 +0300 Subject: [PATCH] Fix 'count-screen-lines' when lines are truncated * lisp/window.el (count-screen-lines): Fix the return value when lines are truncated in the window, and the end of the region is invisible due to this truncation. (Bug#40849) --- lisp/window.el | 36 ++++++++++++++++++++++++++---------- 1 file changed, 26 insertions(+), 10 deletions(-) diff --git a/lisp/window.el b/lisp/window.el index 8512a0e2f97..d658cb81f65 100644 --- a/lisp/window.el +++ b/lisp/window.el @@ -8638,16 +8638,32 @@ in some window." (setq end (point-max))) (if (= beg end) 0 - (save-excursion - (save-restriction - (widen) - (narrow-to-region (min beg end) - (if (and (not count-final-newline) - (= ?\n (char-before (max beg end)))) - (1- (max beg end)) - (max beg end))) - (goto-char (point-min)) - (1+ (vertical-motion (buffer-size) window)))))) + (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 + (and (natnump truncate-partial-width-windows) + (< (window-total-width window) + truncate-partial-width-windows))) + (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))))))) (defun window-buffer-height (window) "Return the height (in screen lines) of the buffer that WINDOW is displaying. -- 2.39.5