From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Eli Zaretskii Newsgroups: gmane.emacs.bugs Subject: bug#48095: 28.0.50; display-line-numbers-mode / display-line-numbers-width-start incorrect Date: Thu, 29 Apr 2021 12:03:36 +0300 Message-ID: <83pmydea3b.fsf@gnu.org> References: Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="6273"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 48095@debbugs.gnu.org To: Len Trigg Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Apr 29 11:05:37 2021 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lc2c5-0001VV-Aa for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 29 Apr 2021 11:05:37 +0200 Original-Received: from localhost ([::1]:60898 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lc2c4-0005EF-DJ for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 29 Apr 2021 05:05:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:56434) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lc2aY-0004Z3-NX for bug-gnu-emacs@gnu.org; Thu, 29 Apr 2021 05:04:03 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:43275) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lc2aY-0003F7-DJ for bug-gnu-emacs@gnu.org; Thu, 29 Apr 2021 05:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1lc2aY-0005ur-8o for bug-gnu-emacs@gnu.org; Thu, 29 Apr 2021 05:04:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Eli Zaretskii Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 29 Apr 2021 09:04:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 48095 X-GNU-PR-Package: emacs Original-Received: via spool by 48095-submit@debbugs.gnu.org id=B48095.161968703722730 (code B ref 48095); Thu, 29 Apr 2021 09:04:02 +0000 Original-Received: (at 48095) by debbugs.gnu.org; 29 Apr 2021 09:03:57 +0000 Original-Received: from localhost ([127.0.0.1]:54821 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lc2aS-0005uY-O6 for submit@debbugs.gnu.org; Thu, 29 Apr 2021 05:03:57 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:48058) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1lc2aP-0005uJ-2I for 48095@debbugs.gnu.org; Thu, 29 Apr 2021 05:03:54 -0400 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:38767) by eggs.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lc2aJ-0003AE-Tk; Thu, 29 Apr 2021 05:03:47 -0400 Original-Received: from 84.94.185.95.cable.012.net.il ([84.94.185.95]:3884 helo=home-c4e4a596f7) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1lc2aI-0005cQ-Rp; Thu, 29 Apr 2021 05:03:47 -0400 In-Reply-To: (message from Len Trigg on Thu, 29 Apr 2021 14:43:30 +1200) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:205160 Archived-At: > From: Len Trigg > Date: Thu, 29 Apr 2021 14:43:30 +1200 > > I am using display-line-numbers mode, and would like to avoid it > changing the width assigned to the line numbers as the buffer is > scrolled. I have (setq display-line-numbers-width-start t), which > purports to check in advance how much width is required. It seems > somehow incorrect though. To reproduce: > > 1 Create a buffer with 90 lines. > > 2 Ensure the window is approx 40 lines tall, displaying the first 40 lines > of the buffer. > > 3 Ensure display-line-numbers-width-start is t > > 4 Turn on display-line-numbers-mode. > > 5 Scroll down to near the bottom of the buffer. At some point, emacs > adds an extra character of width to the line numbers, even though no > line number higher than 90 is ever shown. If emacs needed an extra > character for whatever reason, surely display-line-numbers-width-start > should have incorporated that up front (otherwise what's the point)? This is not exactly as simple as it may sound. When the line numbers are produced at display time, Emacs accounts for the maximum possible number of lines that could fit in the window, and enlarges the width when the window could display more lines, so that the width doesn't change in the middle of the window. So if the window is, say, 35 lines high, and the last portion of the buffer is about to be displayed, Emacs computes the width necessary to display up to 35 more lines, even if those lines are not yet in the buffer. There's also the possibility that the text will have different faces, and that could affect the actual number of lines (via the font height). But this cannot be done up front, because when the mode is turned on in a buffer, the dimensions of the window where it will be displayed are not necessarily known, and neither are the fonts that will be used to display the buffer. And windows nowadays can be as small as a few lines or as toll as the full screen. What I can offer to make the situation slightly better is to allow display-line-numbers-width-start to have a numeric value, which will then be interpreted as the number of extra lines to account for when computing the required width. A user who wants this is supposed to know how tall are the windows in his/her sessions, and should set the value to the maximum height of windows he/she wants to support. The patch for that is below; could you please try it and see if this provides a satisfactory solution for your problem? diff --git a/lisp/display-line-numbers.el b/lisp/display-line-numbers.el index a6fa813..0a834d4 100644 --- a/lisp/display-line-numbers.el +++ b/lisp/display-line-numbers.el @@ -56,12 +56,15 @@ display-line-numbers-grow-only (defcustom display-line-numbers-width-start nil "If non-nil, count number of lines to use for line number width. -When `display-line-numbers-mode' is turned on, -`display-line-numbers-width' is set to the minimum width necessary -to display all line numbers in the buffer." +When `display-line-numbers-mode' is turned on, if this option is +non-nil, `display-line-numbers-width' is set up front to a width +necessary to display all line numbers in the buffer. If the value +is a positive number, it is interpreted as extra lines to account +for when computing the required width." :group 'display-line-numbers - :type 'boolean - :version "26.1") + :type '(choice (boolean :tag "Minimum width for buffer's line count") + (integer :tag "Number of extra lines to account for")) + :version "28.1") (defun display-line-numbers-update-width () "Prevent the line number width from shrinking." @@ -83,7 +86,11 @@ display-line-numbers-mode (when display-line-numbers-width-start (setq display-line-numbers-width (length (number-to-string - (count-lines (point-min) (point-max)))))) + (+ (count-lines (point-min) (point-max)) + (if (and (numberp display-line-numbers-width-start) + (> display-line-numbers-width-start 0)) + display-line-numbers-width-start + 0)))))) (when display-line-numbers-grow-only (add-hook 'pre-command-hook #'display-line-numbers-update-width nil t)) (setq display-line-numbers display-line-numbers-type))