Subject: bug#75500: [PATCH] Fix keybinding alignment in `tmm-menubar'

From: Manuel Giraud <[email-masked]>
Date: Sat, 18 Jan 2025 17:30:29 +0100

Eli Zaretskii <[email-masked]> writes:

>> From: Manuel Giraud <[email-masked]>
>> Cc: [email-masked]
>> Date: Sat, 18 Jan 2025 15:11:10 +0100
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> >> Date: Sat, 11 Jan 2025 18:42:54 +0100
>> >> From:  Manuel Giraud via "Bug reports for GNU Emacs,
>> >>  the Swiss army knife of text editors" <[email-masked]>
>> >> 
>> >> 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.
>> >
>> > Thanks.  However, I couldn't see any misalignments in "List
>> > bookmarks".  Could you perhaps show a recipe for reproducing this,
>> > starting from "emacs -Q"?
>> 
>> You're right.  For me, the 'b' shortcut was "taken" for the "Back to
>> previous page" entry.  Anyway, you should see the misalignment for the
>> "List cookies" entry with the following recipe:
>> 
>>       - emacs -Q
>>       - M-: (setopt tmm-shortcut-inside-entry t) <RET>
>>       - M-x eww <RET>
>>       - gnu.org <RET>
>>       - M-x tmm-menubar <RET> E
>
> OK, I see it now, thanks.
>
> Coming back to the patch: please make sure the doc string of the
> function you are adding is according to our conventions (first line is
> a single complete sentence), and please explain in the comments _why_
> this shortening is needed and why exactly 2 spaces.

Do you meant like this?
>From 91d4914ff694a18e9b3f92a74c4e310cea4226c6 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
to avoid a misalignement.
(tmm-add-one-shortcut): Use it.
---
 lisp/tmm.el | 22 +++++++++++++++++++---
 1 file changed, 19 insertions(+), 3 deletions(-)

diff --git a/lisp/tmm.el b/lisp/tmm.el
index c46c477010f..9dcbf90b657 100644
--- a/lisp/tmm.el
+++ b/lisp/tmm.el
@@ -282,6 +282,17 @@ 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 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
@@ -327,9 +338,14 @@ tmm-add-one-shortcut
                      (add-text-properties pos (1+ pos) '(face highlight) res)
                      res)
                  ;; A fallback digit character: place it in front of the
-                 ;; menu entry.
-                 (concat (propertize (char-to-string char) 'face 'highlight)
-                         " " str))
+                 ;; menu entry.  We need to shorten the spaces between
+                 ;; the menu entry and the keybinding by two spaces
+                 ;; because we added two characters at the front (one
+                 ;; digit and one space) and this would cause a
+                 ;; misalignement otherwise.
+                 (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