]> git.eshelyaron.com Git - emacs.git/commitdiff
Fix 'count-screen-lines' when lines are truncated
authorEli Zaretskii <eliz@gnu.org>
Sat, 2 May 2020 08:45:05 +0000 (11:45 +0300)
committerEli Zaretskii <eliz@gnu.org>
Sat, 2 May 2020 08:45:05 +0000 (11:45 +0300)
* 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

index 8512a0e2f976a4382a7a44f98a538a0f1148725e..d658cb81f65a3715b2f00068e296e837f4d4ed64 100644 (file)
@@ -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.