Subject: Re: bug#73734: [PATCH] Fix tmm menu layout

From: Eli Zaretskii <[email-masked]>
Date: Fri, 11 Oct 2024 19:44:33 +0300

> From: Manuel Giraud <[email-masked]>
> Cc: [email-masked],  [email-masked]
> Date: Fri, 11 Oct 2024 12:45:21 +0200
> 
> diff --git a/lisp/tmm.el b/lisp/tmm.el
> index 1e3942761a1..4ddf61cc9b3 100644
> --- a/lisp/tmm.el
> +++ b/lisp/tmm.el
> @@ -484,13 +484,16 @@ tmm-get-keymap
>              (when binding
>                (setq binding (key-description binding))
>                ;; Try to align the keybindings.
> -              (let ((colwidth (- (/ (frame-width) 3) 15)))
> +              (let* ((colwidth (min 30 (- (/ (window-width) 2) 10)))
> +                     (spaces (make-string (max 2 (- colwidth
> +                                                    (string-width str)
> +                                                    (string-width binding)))
> +                                          ?\s)))
> +                (dotimes (i (length spaces))
> +                  (put-text-property i (1+ i) 'display '(space . (:width 1)) spaces))
>                  (setq str
>                        (concat str
> -                              (make-string (max 2 (- colwidth
> -                                                     (string-width str)
> -                                                     (string-width binding)))
> -                                           ?\s)
> +                              spaces
>                                binding)))))))
>        (and km (stringp km) (setq str km))
>        ;; Verify that the command is enabled;
> 
> But then, the result is that, now, the keybinding is placed just *one*
> space after its menu entry's name.  So yes, the confusion I was talking
> about is gone but we completly loose the spacing between entry name and
> keybinding as well as the vertical alignment of keybindings.

I meant this:

diff --git a/lisp/tmm.el b/lisp/tmm.el
index ed74c30..258f1a5 100644
--- a/lisp/tmm.el
+++ b/lisp/tmm.el
@@ -487,13 +487,15 @@ tmm-get-keymap
             (when binding
               (setq binding (key-description binding))
               ;; Try to align the keybindings.
-              (let ((colwidth (min 30 (- (/ (window-width) 2) 10))))
+              (let* ((colwidth (min 30 (- (/ (window-width) 2) 10)))
+                     (nspaces (max 2 (- colwidth
+                                        (string-width str)
+                                        (string-width binding)))))
                 (setq str
                       (concat str
-                              (make-string (max 2 (- colwidth
-                                                     (string-width str)
-                                                     (string-width binding)))
-                                           ?\s)
+                              (propertize (make-string nspaces ?\s)
+                                          'display
+                                          (cons 'space (list :width nspaces)))
                               binding)))))))
       (and km (stringp km) (setq str km))
       ;; Verify that the command is enabled;

This doesn't align bindings if a variable-pitch font is used, but
it still produces a better display than the current code does when
variable-pitch font is the default font.