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

From: Gerd Möllmann <[email-masked]>
Date: Mon, 02 Jun 2025 09:42:17 +0200

Eli Zaretskii <[email-masked]> writes:

>> From: Gerd Möllmann <[email-masked]>
>> Cc: Alex Murray <[email-masked]>,  Po Lu <[email-masked]>,
>>   [email-masked]
>> Date: Mon, 02 Jun 2025 05:49:14 +0200
>> 
>> Eli Zaretskii <[email-masked]> writes:
>> 
>> > 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.
>
> Not reproducible on my system with today's master branch.  When I
> press "C-h k <f4>" after evaluating the above line by line, I get the
> expected documentation of kill-current-buffer.

It seems to depend on trying this in an installed Emacs, and that that
Emacs is older than what is built in the repo that I built it in. For
example, the Emacs I'm running is from some weeks ago when I started
using the NS freeze workaround I posted to emacs-devel. In the repo, a
more current Emacs is built.

>> 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.
>
> "M-x debug-on-entry RET global-set-key RET" doesn't pop up the
> debugger when I evaluate "(documentation 'rectangle-mark-mode)".
>
> Anyway, why would Emacs want to load loaddefs, when loaddefs is
> preloaded?

Could be Fdocumentation:

doc.c:
  365   if (FIXNUMP (doc) || (CONSP (doc) && FIXNUMP (XCDR (doc))))
  366     {
  367       Lisp_Object tem = get_doc_string (doc, 0);
  368       if (NILP (tem) && try_reload)
  369         {
  370           /* The file is newer, we need to reset the pointers.  */
  371           reread_doc_file (Fcar_safe (doc));
  372           try_reload = false;

doc.c:
  310 static void
  311 reread_doc_file (Lisp_Object file)
  312 {
  313   if (NILP (file))
  314     Fsnarf_documentation (Vdoc_file_name);
  315   else
  316     save_match_data_load (file, Qt, Qt, Qt, Qnil);
  317 }

That's of course not the root cause, but somehow it's happening.