Subject: Re: bug#78582: 30.1; which-key-mode overwrites custom key bindings

From: Gerd Möllmann <[email-masked]>
Date: Mon, 02 Jun 2025 05:49:14 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Alex Murray <[email-masked]>
>> Date: Tue, 27 May 2025 21:09:54 +0930
>> Cc: [email-masked]
>> 
>> Your hint about toolkit got me interested - for a bit more info on the
>> emacs snap, I build emacs both with pgtk
>> (https://github.com/alexmurray/emacs-snap/blob/master/snapcraft.yaml#L296)
>> and without it (https://github.com/alexmurray/emacs-snap/blob/master/snapcraft.yaml#L314)
>> and then bundle both inside the snap and use a script to try and
>> choose the most appropriate one to use at runtime.
>> 
>> If I force it to use the gtk variant (ie. non-pgtk) then I can no
>> longer reproduce this issue with the emacs snap. However I am able to
>> reproduce it with the pgtk variant.
>> 
>> Could it be an issue with pgtk specifically and not the emacs snap perse?
>
> Could be.  Would people who use PGTK please try reproducing the issue
> with the recipe and report?
>
> Po Lu, any ideas why the PGTK build could show this strange behavior?

I encountered this on macOS today. Reproducer with an installed Emacs 
(which is the Emacs.app on macOS):

  ~/Desktop/Emacs.app/Contents/MacOS/Emacs -q

In *scratch*, eval

 (global-set-key [f4] #'kill-current-buffer)
 (documentation 'rectangle-mark-mode)

Then C-h k <f4> and observe that it has been redefined.

A backtrace how this happens with which-key is at then end. In short,
which-key calls 'documentation' which loads lisp/loaddefs which
evaluates the global-set-keys loaddefs.el contains.

I have no idea how to fix this.

  global-set-key("\30(" kmacro-start-macro)
  byte-code("\300\301\302\303\304$\210\305\302\306\"\210\307\310\311\"\210\307\312\313\"\210\307\314\315\"\210\307\316\317\"\210\307\320\321\"\210\307\322\323\"\210\300\323\324\325\304\326%\210\327\330\331\332#\210\333\330\331\334#\210\300\311\324\335\304$\210\300\313\324\336\304$\210\300\337\324\340\304$\210\300\317\324\341\304$\210\300\321\324\342\304$\210\300\315\324\343\304$\210\300\344\324\345\304$\210\300\346\324\347#\210\300\350\324\351#\210\333\350\346\334#\210\300\352\324\353\304$\210\327\354\355\"\210\300\355\324\356\304$\210\305\324\357\"\207" [autoload kkc-region "kkc" ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 768162) t register-definition-prefixes ("kkc-") global-set-key "\30(" kmacro-start-macro "\30)" kmacro-end-macro "\30e" kmacro-end-and-call-macro [f3] kmacro-start-macro-or-insert-counter [f4] kmacro-end-or-call-macro "\30\13" kmacro-keymap "kmacro" ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 768555) keymap defalias kmacro-exec-ring-item funcall "Execute item ITEM from the macro ring.\nARG is the number of times to execute the item." make-obsolete "29.1" ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 768689) ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 769692) kmacro-call-macro ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 770110) ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 770706) ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 771651) ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 771871) kmacro-end-call-mouse ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 772193) kmacro ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 772352) kmacro-lambda-form ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 772507) kmacro-name-last-macro ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 772549) kmacro-menu list-keyboard-macros ("/Users/gerd/Desktop/Emacs.app/Contents/Resources/lisp/loaddefs.elc" . 772804) ("kmacro-")] 6)
  documentation(rectangle-mark-mode)
  which-key--propertize-description("rectangle-mark-mode" nil nil nil "rectangle-mark-mode")
  which-key--format-and-replace((("C-x DEL" . "backward-kill-sentence") ("C-x ESC ESC" . "repeat-complex-command") ("C-x RET" . "prefix") ("C-x SPC" . "rectangle-mark-mode") ("C-x TAB" . "indent-rigidly") ("C-x #" . "server-edit") ("C-x $" . "set-selective-display") ("C-x '" . "expand-abbrev") ("C-x (" . "kmacro-start-macro") ("C-x )" . "kmacro-end-macro") ("C-x *" . "calc-dispatch") ("C-x +" . "balance-windows") ("C-x -" . "shrink-window-if-larger-than-buffer") ("C-x ." . "set-fill-prefix") ("C-x 0" . "delete-window") ("C-x 1" . "delete-other-windows") ("C-x 2" . "split-window-below") ("C-x 3" . "split-window-right") ("C-x 4" . "ctl-x-4-prefix") ("C-x 5" . "ctl-x-5-prefix") ("C-x 8" . "prefix") ("C-x ;" . "comment-set-column") ("C-x <" . "scroll-left") ("C-x =" . "what-cursor-position") ("C-x >" . "scroll-right") ("C-x X" . "prefix") ("C-x [" . "backward-page") ("C-x \\" . "activate-transient-input-method") ("C-x ]" . "forward-page") ("C-x ^" . "enlarge-window") ("C-x `" . "next-error") ("C-x a" . "prefix") ("C-x b" . "consult-buffer") ("C-x d" . "dired") ("C-x e" . "kmacro-end-and-call-macro") ("C-x f" . "set-fill-column") ("C-x g" . "magit-status-quick") ("C-x h" . "mark-whole-buffer") ("C-x i" . "insert-file") ("C-x k" . "kill-buffer") ("C-x l" . "count-lines-page") ("C-x m" . "compose-mail") ("C-x n" . "prefix") ("C-x o" . "other-window") ("C-x p" . "prefix") ("C-x q" . "kbd-macro-query") ("C-x r" . "prefix") ("C-x s" . "save-some-buffers") ("C-x t" . "prefix") ("C-x u" . "vundo") ...) nil)
  which-key--get-bindings([24] nil nil)
  which-key--create-buffer-and-show([24])
  which-key--update()
  apply(which-key--update nil)
  timer-event-handler([t 0 1 0 t which-key--update nil idle 0 nil])