Subject: [PATCH] Fix keybinding alignment in `tmm-menubar'
From: Manuel Giraud <[email-masked]>
To: [email-masked]
Date: Sat, 11 Jan 2025 18:42:54 +0100
List-Id: <debbugs-submit.debbugs.gnu.org>

Tags: patch

Hi,

In tmm-menubar, for entries prepended by a numerical shortcut, the
associated keybinding ends up being misaligned.  For instance, you can
see that in the eww menu for the entry "List bookmarks".  This patch
fixes this misalignment.

In GNU Emacs 31.0.50 (build 21, x86_64-unknown-openbsd7.6, X toolkit) of
 2025-01-08 built on computer
Repository revision: 631f43d2bbe403e48d63d963281f1d948dee5263
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12101015
System Description: OpenBSD computer 7.6 GENERIC.MP#494 amd64

Configured using:
 'configure CC=egcc CPPFLAGS=-I/usr/local/include
 LDFLAGS=-L/usr/local/lib MAKEINFO=gmakeinfo --prefix=/home/manuel/emacs
 --bindir=/home/manuel/bin --with-x-toolkit=lucid
 --with-toolkit-scroll-bars=no --without-cairo
 --without-compress-install'

From eca3e6a46b71ad016b47e03ddf4c7c8fb6a6748d Mon Sep 17 00:00:00 2001
From: Manuel Giraud <[email-masked]>
Date: Sat, 11 Jan 2025 18:34:49 +0100
Subject: [PATCH] Fix keybinding alignment in `tmm-menubar'

* lisp/tmm.el (tmm--shorten-space-width): New function that
reduces the space between the text menu entry and its
keybinding.
(tmm-add-one-shortcut): Use it.
---
 lisp/tmm.el | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/lisp/tmm.el b/lisp/tmm.el
index c46c477010f..63053f7c13c 100644
--- a/lisp/tmm.el
+++ b/lisp/tmm.el
@@ -282,6 +282,18 @@ tmm-add-shortcuts
   (let ((tmm-next-shortcut-digit ?0))
     (mapcar #'tmm-add-one-shortcut (reverse list))))
 
+(defun tmm--shorten-space-width (str)
+  "Shorten the width between the text of the menu entry and the
+keybinding by 2 spaces."
+  (let* ((start (next-single-property-change 0 'display str))
+         (n (length str))
+         (end (previous-single-property-change n 'display str))
+         (curr-width (and start
+                          (plist-get (get-display-property start 'space str) :width))))
+    (when curr-width
+      (put-text-property start end 'display (cons 'space (list :width (- curr-width 2))) str))
+    str))
+
 (defsubst tmm-add-one-shortcut (elt)
   ;; uses the free vars tmm-next-shortcut-digit and tmm-short-cuts
   (cond
@@ -328,8 +340,9 @@ tmm-add-one-shortcut
                      res)
                  ;; A fallback digit character: place it in front of the
                  ;; menu entry.
-                 (concat (propertize (char-to-string char) 'face 'highlight)
-                         " " str))
+                 (tmm--shorten-space-width
+                  (concat (propertize (char-to-string char) 'face 'highlight)
+                          " " str)))
              (make-string 2 ?\s))
          (concat (if char (concat (char-to-string char) tmm-mid-prompt)
                    ;; Keep them lined up in columns.
-- 
2.47.1

-- 
Manuel Giraud