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

From: Eli Zaretskii <[email-masked]>
Date: Fri, 18 Nov 2022 10:42:49 +0200

> Cc: [email-masked]
> From: Juri Linkov <[email-masked]>
> Date: Fri, 18 Nov 2022 09:15:02 +0200
> 
> > 2) Eval the following to enable global-display-line-numbers-mode:
> >
> > (progn
> >   (global-display-line-numbers-mode 1))
> > ...
> > Result: the global-mode-line is displayed in the tab-bar, right-aligned,
> > with an incorrect padding on the right. See attached video.
> 
> Thanks for the bug report.  Here is a possible fix
> to get the correct pixel width without taking into account
> line numbers inserted to the text area of the same buffer:
> 
> diff --git a/lisp/emacs-lisp/subr-x.el b/lisp/emacs-lisp/subr-x.el
> index 6e4d88b4df..92bbdfaa9c 100644
> --- a/lisp/emacs-lisp/subr-x.el
> +++ b/lisp/emacs-lisp/subr-x.el
> @@ -322,6 +322,8 @@ string-pixel-width
>      ;; Keeping a work buffer around is more efficient than creating a
>      ;; new temporary buffer.
>      (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).

> But really I think the bug is in global-display-line-numbers-mode.
> Maybe it should not enable display-line-numbers-mode
> in internal buffers whose names start with a space?

Regardless, functions that must make sure line-numbers are off should
do that explicitly, or account for the line-number width in their
geometry calculations.  That's because whatever
global-display-line-numbers-mode does, it is always possible that a
particular buffer will have line-numbers enabled for some reason.

> For example, here is what tab-line-mode--turn-on does:
> 
>   (defun tab-line-mode--turn-on ()
>     (unless (or (minibufferp)
>                 (string-match-p "\\` " (buffer-name))
>                 (memq major-mode tab-line-exclude-modes)
>                 (get major-mode 'tab-line-exclude)
>                 (buffer-local-value 'tab-line-exclude (current-buffer)))
>       (tab-line-mode 1)))
> 
> But display-line-numbers--turn-on is only:
> 
>   (defun display-line-numbers--turn-on ()
>     (unless (minibufferp)
>       (display-line-numbers-mode)))
> 
> I think we should add at least (string-match-p "\\` " (buffer-name))
> to display-line-numbers--turn-on.

That's a possibility, but you may find that some Lisp code somewhere
assumes line-numbers are ON in a temporary buffer, e.g. because it
wants to measure something that is directly related to line-numbers.

So this kind of reasoning is a slippery slope, IME, and tends to
introduce subtle bugs elsewhere it takes us years to find.