Subject: Re: bug#59311: 29.0.50; tab-bar global-mode-string affected by global-display-line-numbers

From: Juri Linkov <[email-masked]>
Date: Sun, 20 Nov 2022 10:20:40 +0200

>> >> @@ -322,6 +322,8 @@ string-pixel-width
>> >>      (with-current-buffer (get-buffer-create " *string-pixel-width*")
>> >> +      (when display-line-numbers-mode
>> >> +        (display-line-numbers-mode -1))
>> >>        (delete-region (point-min) (point-max))
>> >>        (insert string)
>> >>        (car (buffer-text-pixel-size nil nil t)))))
>> >
>> > Yes.  Or subtract what (line-number-display-width t) returns (it will
>> > return zero when line-numbers are turned OFF).
>> 
>> Shouldn't then buffer-text-pixel-size subtract line-number-display-width?
>> Isn't this the responsibility of buffer-text-pixel-size,
>> not responsibility of a caller like string-pixel-width?
>
> No, because line-numbers take space, and some use cases of
> buffer-text-pixel-size want to know that.  Only the caller knows whether the
> line-numbers should or shouldn't be included.  The principle is that we
> measure the space taken in the text-area, no matter how it is used.  (There
> are other display features that affect the result, for example,
> line-prefix.)

I still don't understand why string-pixel-width should handle line-numbers
that also degrades its performance.  I think it should be sufficient only
to disable line-numbers in internal buffers.  But ok, here are both:

diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
index 6e4d88b4df..fba817b010 100644
--- a/lisp/emacs-lisp/subr-x.el
+++ b/lisp/emacs-lisp/subr-x.el
@@ -324,7 +324,8 @@ string-pixel-width
     (with-current-buffer (get-buffer-create " *string-pixel-width*")
       (delete-region (point-min) (point-max))
       (insert string)
-      (car (buffer-text-pixel-size nil nil t)))))
+      (- (car (buffer-text-pixel-size nil nil t))
+         (line-number-display-width t)))))
 
 ;;;###autoload
 (defun string-glyph-split (string)
diff --git a/lisp/display-line-numbers.el b/lisp/display-line-numbers.el
index 897a88398f..cf5d353fba 100644
--- a/lisp/display-line-numbers.el
+++ b/lisp/display-line-numbers.el
@@ -101,7 +101,8 @@ display-line-numbers-mode
 
 (defun display-line-numbers--turn-on ()
   "Turn on `display-line-numbers-mode'."
-  (unless (minibufferp)
+  (unless (or (minibufferp)
+              (string-match-p "\\` " (buffer-name)))
     (display-line-numbers-mode)))
 
 ;;;###autoload