Subject: bug#66022: 30.0.50; kmacro overwriting global keybindings

From: Gerd Möllmann <[email-masked]>
Date: Sun, 17 Sep 2023 08:31:47 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Date: Sat, 16 Sep 2023 08:38:22 +0200
>>
>> current master 1442f4043a761e9bdeeb4e1fbe9822c2987c1502, emacs -Q
>
> The same problem exists on emacs-29 (and I presume in older versions
> of Emacs).
>
>>   (keymap-global-set "S-<mouse-3>" 'ignore)
>>
>> then C-x ( C-g.
>
> (The C-g part is not relevant; the same happens if you finish defining
> the macro.)

True.

>>   (keymap-lookup global-map "S-<mouse-3>")
>>   => kmacro-end-call-mouse
>>
>> Notice that the binding has been overwritten.
>
> This is because of this snippet at top level of kmacro.el:
>
>   (if kmacro-call-mouse-event
>     (global-set-key (vector kmacro-call-mouse-event) #'kmacro-end-call-mouse))
>
> As the doc string of kmacro-call-mouse-event says:
>
>   (defcustom kmacro-call-mouse-event 'S-mouse-3
>     "The mouse event used by kmacro to call a macro.
>   Set to nil if no mouse binding is desired."
>     :type 'symbol)
>
> customize this to nil to avoid overwriting the S-<mouse-3> binding you
> have.

Ah, finally someone I can understand ;-).

>> This is actually not the binding I care about, but it's the only one I
>> can easily reproduce with emacs -Q.  With my init file, and with a
>> (trace-function 'global-set-key) I see
>>
>> ======================================================================
>> 1 -> (global-set-key "(" kmacro-start-macro)
>> 1 <- global-set-key: kmacro-start-macro
>> ======================================================================
>> 1 -> (global-set-key ")" kmacro-end-macro)
>> 1 <- global-set-key: kmacro-end-macro
>> ======================================================================
>> 1 -> (global-set-key "e" kmacro-end-and-call-macro)
>> 1 <- global-set-key: kmacro-end-and-call-macro
>> ======================================================================
>> 1 -> (global-set-key [f3] kmacro-start-macro-or-insert-counter)
>> 1 <- global-set-key: kmacro-start-macro-or-insert-counter
>> ======================================================================
>> 1 -> (global-set-key [f4] kmacro-end-or-call-macro)
>> 1 <- global-set-key: kmacro-end-or-call-macro
>> ======================================================================
>> 1 -> (global-set-key "" kmacro-keymap)
>> 1 <- global-set-key: kmacro-keymap
>> ======================================================================
>> 1 -> (global-set-key "6" 2C-command)
>> 1 <- global-set-key: 2C-command
>> ======================================================================
>> 1 -> (global-set-key [f2] 2C-command)
>> 1 <- global-set-key: 2C-command
>>
>> which overwrites part of my key bindings, also with ones from two-column.
>
> Those bindings come from loaddefs.el, which should be preloaded, so
> the bindings should be executed when dumping Emacs, as part of loading
> loadup.el, not when processing your init files.

So I thought, too.

> Can you tell more about how you captured those traces, exactly?

I added a (trace-function 'global-set-key) to my init file, in an
after-init-hook, so that it only traced after my key bindings were done.
At some point, I still don't know how I triggered this, this trace
appeared.

> what are the details of your build, and in particular did you build
> "--with-native-compilation=aot", per chance?

No native compilation, the only config option different fromt he default
is --without-dbus.  Please find at the end what report-emacs-bug
produces.  It's a clean build starting with git clean -xdf.

I have now added this to my init.el:

(defun my-watch-f2 (&rest args)
  (when (eq (car args) global-map)
    (let ((key (cl-second args)))
      (when (and (vectorp key) (eq (aref key 0) 'f2))
	(debug)))))

(add-hook 'after-init-hook
	  (lambda ()
	    (add-function :after (symbol-function 'define-key) #'my-watch-f2)))

which is supposed to drop me into the debugger when my F2 binding gets
redefined.  Alas, as I mentioned, I don't know what exactly I do when
this happens.

I'll report back, when I have something.

In GNU Emacs 30.0.50 (build 1, x86_64-apple-darwin20.6.0, NS
 appkit-2022.70 Version 11.7.10 (Build 20G1427)) of 2023-09-16 built on
 Pro.fritz.box
Repository revision: 302bc23f7cb9fbde7f225650d833e1c09da97338
Repository branch: master
Windowing system distributor 'Apple', version 10.3.2022
System Description:  macOS 11.7.10

Configured using:
 'configure --without-dbus --cache-file
 /var/folders/1d/k_6t25f94sl83szqbf8gpkrh0000gn/T//config.cache.master
 --disable-silent-rules'

Configured features:
ACL GIF GLIB GMP GNUTLS JPEG LCMS2 LIBXML2 MODULES NOTIFY KQUEUE NS
PDUMPER PNG RSVG SQLITE3 THREADS TIFF TOOLKIT_SCROLL_BARS TREE_SITTER
WEBP XIM ZLIB

Important settings:
  locale-coding-system: utf-8-unix

Major mode: Message

Minor modes in effect:
  global-git-commit-mode: t
  magit-auto-revert-mode: t
  gnus-message-citation-mode: t
  mml-mode: t
  global-company-mode: t
  company-mode: t
  desktop-save-mode: t
  global-auto-revert-mode: t
  recentf-mode: t
  server-mode: t
  pixel-scroll-precision-mode: t
  yas-global-mode: t
  yas-minor-mode: t
  which-key-mode: t
  persistent-scratch-autosave-mode: t
  delete-selection-mode: t
  global-hl-line-mode: t
  global-display-line-numbers-mode: t
  display-line-numbers-mode: t
  ws-butler-global-mode: t
  ws-butler-mode: t
  vertico-posframe-mode: t
  vertico-multiform-mode: t
  vertico-mouse-mode: t
  vertico-grid-mode: t
  vertico-mode: t
  override-global-mode: t
  straight-use-package-mode: t
  straight-package-neutering-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  minibuffer-regexp-mode: t
  line-number-mode: t
  auto-fill-function: yas--auto-fill
  transient-mark-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  abbrev-mode: t

Load-path shadows:
/Users/gerd/.config/emacs.d.default/straight/build/external-completion/external-completion hides /Applications/Emacs.app/Contents/Resources/lisp/external-completion
/Users/gerd/.config/emacs.d.default/straight/build/jsonrpc/jsonrpc hides /Applications/Emacs.app/Contents/Resources/lisp/jsonrpc
/Users/gerd/.config/emacs.d.default/straight/build/transient/transient hides /Applications/Emacs.app/Contents/Resources/lisp/transient
/Users/gerd/.config/emacs.d.default/straight/build/xref/xref hides /Applications/Emacs.app/Contents/Resources/lisp/progmodes/xref
/Users/gerd/.config/emacs.d.default/straight/build/project/project hides /Applications/Emacs.app/Contents/Resources/lisp/progmodes/project
/Users/gerd/.config/emacs.d.default/straight/build/eglot/eglot hides /Applications/Emacs.app/Contents/Resources/lisp/progmodes/eglot
/Users/gerd/.config/emacs.d.default/straight/build/flymake/flymake hides /Applications/Emacs.app/Contents/Resources/lisp/progmodes/flymake
/Users/gerd/.config/emacs.d.default/straight/build/soap-client/soap-client hides /Applications/Emacs.app/Contents/Resources/lisp/net/soap-client
/Users/gerd/.config/emacs.d.default/straight/build/soap-client/soap-inspect hides /Applications/Emacs.app/Contents/Resources/lisp/net/soap-inspect
/Users/gerd/.config/emacs.d.default/straight/build/eldoc/eldoc hides /Applications/Emacs.app/Contents/Resources/lisp/emacs-lisp/eldoc

Features:
(shadow emacsbug nndoc gnus-dup mm-archive url-cache debbugs-gnu
debbugs-compat debbugs soap-client url-http url-auth url-gw rng-xsd
rng-dt rng-util xsd-regexp consult-register magit-extras magit-bookmark
magit-submodule magit-blame magit-stash magit-reflog magit-bisect
magit-push magit-pull magit-fetch magit-clone magit-remote magit-commit
magit-sequence magit-notes magit-worktree magit-tag magit-merge
magit-branch magit-reset magit-files magit-refs magit-status magit
package url-handlers magit-repos magit-apply magit-wip magit-log
which-func magit-diff smerge-mode git-commit log-edit add-log magit-core
magit-autorevert magit-margin magit-transient magit-process with-editor
magit-mode magit-git magit-base crm qp sort smiley gnus-cite mail-extr
textsec uni-scripts idna-mapping uni-confusable textsec-check gnus-async
gnus-bcklg gnus-ml disp-table nndraft nnmh utf-7 nnfolder epa-file
network-stream nsm gnus-agent gnus-srvr gnus-score score-mode nnvirtual
gnus-msg gnus-art mm-uu mml2015 mm-view mml-smime smime gnutls dig nntp
gnus-cache gnus-sum shr pixel-fill kinsoku url-file svg dom browse-url
url url-proxy url-privacy url-expand url-methods url-history url-cookie
generate-lisp-file url-domsuf gnus-group gnus-undo gnus-start gnus-dbus
dbus xml gnus-cloud nnimap nnmail mail-source utf7 nnoo parse-time
iso8601 gnus-spec gnus-int gnus-range message mailcap yank-media puny
rfc822 mml mml-sec epa derived epg rfc6068 epg-config mm-decode
mm-bodies mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader
gnus-win shortdoc comp comp-cstr rx help-fns radix-tree
exec-path-from-shell consult-eglot cc-mode cc-fonts cc-guess cc-menus
cc-cmds cc-styles cc-align cc-engine cc-vars cc-defs eglot
external-completion jsonrpc flymake-proc flymake thingatpt diff ert ewoc
debug backtrace find-func warnings icons url-util url-parse url-vars
imenu time dired-aux jka-compr company-oddmuse company-keywords
company-etags etags fileloop generator xref company-gtags
company-dabbrev-code company-dabbrev company-files company-clang
company-capf company-cmake company-semantic company-template
company-bbdb company pcase org-bullets-autoloads ediff ediff-merg
ediff-mult ediff-wind ediff-diff ediff-help ediff-init ediff-util
inf-lisp shell pcomplete desktop frameset autorevert filenotify recentf
tree-widget dired dired-loaddefs modus-vivendi-theme modus-themes server
transient pixel-scroll cua-base vc-hg vc-git diff-mode vc-bzr vc-src
vc-sccs vc-svn vc-cvs vc-rcs log-view pcvs-util bug-reference
debbugs-autoloads soap-client-autoloads smtpmail auth-source
password-cache json map sendmail rfc2047 rfc2045 ietf-drums yasnippet
yasnippet-autoloads which-key which-key-autoloads persistent-scratch
persistent-scratch-autoloads delsel hl-line display-line-numbers gnus
nnheader gnus-util time-date mail-utils range mm-util mail-prsvr
wid-edit compile comint ansi-osc ansi-color ring ws-butler
ws-butler-autoloads consult-eglot-autoloads eglot-autoloads
external-completion-autoloads flymake-autoloads project-autoloads
xref-autoloads eldoc-autoloads jsonrpc-autoloads company-autoloads
embark-consult-autoloads embark-autoloads consult-ls-git vc
vc-dispatcher project consult-ls-git-autoloads consult bookmark
text-property-search pp consult-autoloads marginalia-autoloads orderless
orderless-autoloads vertico-posframe posframe cus-load
vertico-posframe-autoloads posframe-autoloads vertico-multiform
vertico-mouse vertico-grid vertico vertico-autoloads magit-section
format-spec eieio eieio-core dash cursor-sensor compat edmacro kmacro
byte-opt magit-autoloads magit-section-autoloads git-commit-autoloads
with-editor-autoloads transient-autoloads dash-autoloads
compat-autoloads info use-package-bind-key bind-key easy-mmode
exec-path-from-shell-autoloads finder-inf use-package-core
straight-autoloads cl-seq cl-extra help-mode straight subr-x cl-macs
cl-loaddefs cl-lib bytecomp byte-compile chemacs gv rmc iso-transl
tooltip cconv eldoc paren electric uniquify ediff-hook vc-hooks
lisp-float-type elisp-mode mwheel term/ns-win ns-win ucs-normalize
mule-util term/common-win touch-screen tool-bar dnd fontset image
regexp-opt fringe tabulated-list replace newcomment text-mode lisp-mode
prog-mode register page tab-bar menu-bar rfn-eshadow isearch easymenu
timer select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer nadvice seq simple cl-generic
indonesian philippine cham georgian utf-8-lang misc-lang vietnamese
tibetan thai tai-viet lao korean japanese eucjp-ms cp51932 hebrew greek
romanian slovak czech european ethiopic indian cyrillic chinese
composite emoji-zwj charscript charprop case-table epa-hook
jka-cmpr-hook help abbrev obarray oclosure cl-preloaded button loaddefs
theme-loaddefs faces cus-face macroexp files window text-properties
overlay sha1 md5 base64 format env code-pages mule custom widget keymap
hashtable-print-readable backquote threads kqueue cocoa ns lcms2
multi-tty make-network-process emacs)

Memory information:
((conses 16 446510 266970) (symbols 48 37802 10)
 (strings 32 136051 12377) (string-bytes 1 4585001) (vectors 16 79623)
 (vector-slots 8 1731485 105152) (floats 8 514 627)
 (intervals 56 2530 840) (buffers 992 36))

> Stefan, any comments?