all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Justin Burkett <justin@burkett.cc>
To: Stefan Kangas <stefankangas@gmail.com>
Cc: 73568@debbugs.gnu.org, Morgan Willcock <morgan@ice9.digital>
Subject: bug#73568: 30.0.91; which-key error in speedbar (wrong-type-argument wholenump -13)
Date: Tue, 1 Oct 2024 22:01:12 -0400	[thread overview]
Message-ID: <CAF8XuLgn+Syd4tZ55ECw9sKadNvEfq3NAUp94vdGa65dwbfggw@mail.gmail.com> (raw)
In-Reply-To: <CADwFkmknNxH=nEgxUq=3SZ6Jp-S16TCfVFA3PvE_vmNsGW-j4Q@mail.gmail.com>

[-- Attachment #1: Type: text/plain, Size: 1548 bytes --]

Thanks. Can you try the attached patch for the first problem?

I'm not sure what the best approach is with speedbar. I'm inclined to
disable which-key for that case, but maybe showing which-key in the
original frame is better?

On Mon, Sep 30, 2024 at 5:53 PM Stefan Kangas <stefankangas@gmail.com> wrote:
>
> Morgan Willcock <morgan@ice9.digital> writes:
>
> > An error is signalled when which-key is activated, point is within the
> > speedbar frame, and a portion of a key-sequence has been typed
> > (e.g. C-x).
> >
> > To generate an example backtrace:
> >
> >   emacs -Q \
> >       --eval "(which-key-mode)" \
> >       --eval "(speedbar-get-focus)" \
> >       --eval "(toggle-debug-on-error)" \
> >       --eval "(setq unread-command-events (listify-key-sequence \"\C-x\"))"
>
> Here's the error I get:
>
> Debugger entered--Lisp error: (wrong-type-argument wholenump -14)
>   make-string(-14 32)
>
> The problem occurs is in `which-key--pad-column': the speedbar frame is
> only 14 characters wide, and so can't fit longer strings than that.
>
> I actually see two bugs here:
>
> 1. Create a regular Emacs frame of width 14, enable `which-key-mode' and
>    press some key, e.g. C-h.  You now get a similar backtrace.  Probably
>    `which-key' should simply be smarter in this case, for example by
>    truncating instead of padding.
>
> 2. With a dframe, as in speedbar, I guess `which-key' should try to
>    display it's help window in the original frame?
>
> I'm copying in Justin Burkett.

[-- Attachment #2: 0001-Fix-which-key-layout-error-when-available-width-is-s.patch --]
[-- Type: application/octet-stream, Size: 4332 bytes --]

From a8015d0c3f5cc5c1d0b1b45f4b7c096dbfc92664 Mon Sep 17 00:00:00 2001
From: Justin Burkett <justin@burkett.cc>
Date: Tue, 1 Oct 2024 21:52:07 -0400
Subject: [PATCH] Fix which-key layout error when available width is small

* lisp/which-key.el (which-key--col-widths): Add more efficient function
for calculating column widths
(which-key--pad-column): Handle case where available width for which-key
buffer is smaller than the width of a single column.
---
 lisp/which-key.el | 71 ++++++++++++++++++++++++++++++++---------------
 1 file changed, 48 insertions(+), 23 deletions(-)

diff --git a/lisp/which-key.el b/lisp/which-key.el
index 232145f7fb5..6fd61462abe 100644
--- a/lisp/which-key.el
+++ b/lisp/which-key.el
@@ -39,6 +39,7 @@
 (require 'cl-lib)
 (require 'button)
 (require 'regexp-opt)
+(require 'seq)
 
 ;; For compiler
 (defvar evil-operator-shortcut-map)
@@ -2012,33 +2013,57 @@ which-key--join-columns
          (rows (apply #'cl-mapcar #'list padded)))
     (mapconcat (lambda (row) (mapconcat #'identity row " ")) rows "\n")))
 
-(defsubst which-key--max-len (keys index &optional initial-value)
-  "Find the max length of the INDEX element in each of KEYS."
-  (cl-reduce
-   (lambda (x y) (max x (which-key--string-width (nth index y))))
-   keys :initial-value (if initial-value initial-value 0)))
+(defun which-key--col-widths (keys)
+  "Find the max length of each column in KEYS.
+KEYS is assumed to be a list of lists of strings, where the sublists all
+have the same number of strings."
+  (let* ((n (length (car keys))))
+    (seq-reduce
+     (lambda (x y)
+       (seq-map-indexed
+        (lambda (e i)
+          (max (nth i x) (which-key--string-width e)))
+        y))
+     keys
+     (make-list n 0))))
 
 (defun which-key--pad-column (col-keys avl-width)
-  "Pad cells of COL-KEYS to AVL-WIDTH.
+  "Pad COL-KEYS cells to same width respecting AVL-WIDTH.
 Take a column of (key separator description) COL-KEYS,
 calculate the max width in the column and pad all cells out to
-that width."
-  (let* ((col-key-width  (+ which-key-add-column-padding
-                            (which-key--max-len col-keys 0)))
-         (col-sep-width  (which-key--max-len col-keys 1))
-         (avl-width      (- avl-width col-key-width col-sep-width))
-         (col-desc-width (min avl-width
-                              (which-key--max-len
-                               col-keys 2
-                               which-key-min-column-description-width)))
-         (col-width      (+ col-key-width col-sep-width col-desc-width))
-         (col-format     (concat "%" (int-to-string col-key-width) "s%s%s")))
-    (cons col-width
-          (mapcar (pcase-lambda (`(,key ,sep ,desc ,_doc))
-                    (concat
-                     (format col-format key sep desc)
-                     (make-string (- col-desc-width (string-width desc)) ?\s)))
-                  col-keys))))
+that width.
+
+If AVL-WIDTH is too small to contain the column, truncate the last
+element of COL-KEYS instead."
+  (let* ((col-widths (which-key--col-widths col-keys))
+         (key-col-width (+ which-key-add-column-padding
+                           (nth 0 col-widths)))
+         (sep-col-width (nth 1 col-widths))
+         (rem-avl-width (- avl-width key-col-width sep-col-width))
+         (desc-col-width (nth 2 col-widths))
+         (total-width (+ key-col-width sep-col-width desc-col-width)))
+    (cond
+     ((> which-key-min-column-description-width desc-col-width rem-avl-width)
+      ;; we can't fit the column
+      (cons 0 (make-list (length col-keys) "")))
+     ((> desc-col-width rem-avl-width)
+      ;; truncate the last column
+      (cons avl-width
+            (mapcar
+             (pcase-lambda (`(,key ,sep ,desc ,_doc))
+               (string-limit
+                (format (format "%%%ds%%s%%s" key-col-width) key sep desc)
+                avl-width))
+             col-keys)))
+     (t
+      ;; pad the last column
+      (cons total-width
+            (mapcar
+             (pcase-lambda (`(,key ,sep ,desc ,_doc))
+               (string-pad
+                (format (format "%%%ds%%s%%s" key-col-width) key sep desc)
+                total-width))
+             col-keys))))))
 
 (defun which-key--partition-list (n list)
   "Partition LIST into N-sized sublists."
-- 
2.46.2


  reply	other threads:[~2024-10-02  2:01 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-30 18:08 bug#73568: 30.0.91; which-key error in speedbar (wrong-type-argument wholenump -13) Morgan Willcock
2024-09-30 21:53 ` Stefan Kangas
2024-10-02  2:01   ` Justin Burkett [this message]
2024-10-02  7:18     ` Eli Zaretskii

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAF8XuLgn+Syd4tZ55ECw9sKadNvEfq3NAUp94vdGa65dwbfggw@mail.gmail.com \
    --to=justin@burkett.cc \
    --cc=73568@debbugs.gnu.org \
    --cc=morgan@ice9.digital \
    --cc=stefankangas@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.