Subject: bug#66678: 30.0.50; Tabs close erratically in text terminal

From: Petteri Hintsanen <[email-masked]>
Date: Sun, 22 Oct 2023 14:21:16 +0300

I'm running Emacs in xterm with xterm-mouse-mode and tab-bar-mode
enabled.  There, tabs have an "x" mark for closing them.  However, tabs
will also close if you click on the character position right after the
buffer name in the tab.  This makes it easy to unintentionally close a
tab instead of switching to it, which is very annoying.

Recipe:

- In xterm, start emacs by `emacs -Q -nw'.
- M-x xterm-mouse-mode
- M-x tab-bar-mode
- Create a new tab by clicking on "+" on the tab bar.
  Now you should have two tabs named `*scratch*'
- Click on the first (leftmost) tab right after the second asterisk.

Expected behavior: Leftmost tab is selected.
Actual behavior: Leftmost tab is closed.

I traced the issue to `tab-bar-auto-width' function which — I think — is
used to pad a tab text with spaces to make it have a uniform width
within the tab bar.  The function erroneously copies 'close-tab text
property from the close button text (tab-bar-close-button) and positions
it right after the buffer name, causing the said behavior.

The attached patch fixes the immediate issue, but I doubt if it is
sufficient, given that there is similar close tab-related logic elsewhere
in the code.  Another workaround is to set `tab-bar-close-button' to one
character string "x" instead of " x".

In GNU Emacs 30.0.50 (build 4, x86_64-pc-linux-gnu, GTK+ Version
 3.24.38, cairo version 1.16.0) of 2023-10-22 built on romeo
Repository revision: ae337884107c4d5f66aaceef0e8b548565317412
Repository branch: master
System Description: Debian GNU/Linux 12 (bookworm)

Configured features:
CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GSETTINGS HARFBUZZ JPEG JSON
LCMS2 LIBOTF LIBSELINUX LIBXML2 MODULES NOTIFY INOTIFY PDUMPER PNG RSVG
SECCOMP SOUND SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER WEBP
X11 XDBE XIM XINPUT2 XPM GTK3 ZLIB

Important settings:
  value of $LC_MESSAGES: en_US.UTF-8
  value of $LANG: fi_FI.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  xterm-mouse-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  tab-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  indent-tabs-mode: t
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message mailcap yank-media puny dired
dired-loaddefs rfc822 mml mml-sec password-cache epa derived epg rfc6068
epg-config gnus-util text-property-search mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils time-date subr-x
cl-extra help-mode icons cl-loaddefs cl-lib cus-start cus-load xt-mouse
term/xterm xterm byte-opt gv bytecomp byte-compile rmc iso-transl
tooltip cconv eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/x-win x-win term/common-win x-dnd
touch-screen tool-bar dnd fontset image regexp-opt fringe tabulated-list
replace newcomment text-mode lisp-mode prog-mode register page tab-bar
menu-bar rfn-eshadow isearch easymenu timer select scroll-bar mouse
jit-lock font-lock syntax font-core term/tty-colors frame minibuffer
nadvice seq simple cl-generic indonesian philippine cham georgian
utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao korean
japanese eucjp-ms cp51932 hebrew greek romanian slovak czech european
ethiopic indian cyrillic chinese composite emoji-zwj charscript charprop
case-table epa-hook jka-cmpr-hook help abbrev obarray oclosure
cl-preloaded button loaddefs theme-loaddefs faces cus-face macroexp
files window text-properties overlay sha1 md5 base64 format env
code-pages mule custom widget keymap hashtable-print-readable backquote
threads dbusbind inotify lcms2 dynamic-setting system-font-setting
font-render-setting cairo gtk x-toolkit xinput2 x multi-tty move-toolbar
make-network-process emacs)

Memory information:
((conses 16 58490 14195) (symbols 48 7111 0) (strings 32 17493 1913)
 (string-bytes 1 469587) (vectors 16 8894)
 (vector-slots 8 113240 10638) (floats 8 33 11100)
 (intervals 56 256 23) (buffers 992 11))

diff --git a/lisp/tab-bar.el b/lisp/tab-bar.el
index d2815c03ebf..e21367255a0 100644
--- a/lisp/tab-bar.el
+++ b/lisp/tab-bar.el
@@ -1227,7 +1227,9 @@ tab-bar-auto-width
                    ((< prev-width width)
                     (let* ((space (apply #'propertize " "
                                          (text-properties-at 0 name)))
-                           (ins-pos (- len (if close-p 1 0)))
+                           (ins-pos (- len (if close-p
+                                               (length tab-bar-close-button)
+                                             0)))
                            (prev-name name))
                       (while continue
                         (setq name (concat (substring name 0 ins-pos)