Subject: bug#78995: [PATCH] ;;;autoload-expand for special macros

From: "J.D. Smith" <[email-masked]>
Date: Sun, 27 Jul 2025 13:59:05 -0400

Stefan Monnier <[email-masked]> writes:

>>> Ah right, my imagining above was not so misguided then.  Maybe the tramp
>>> maintainer will want to expand that simple macro away now.
>>
>> I give this explanation to Stefan, he added this macro to Tramp years
>> ago (commit 07d8827946f9) :-)
>>
>> --8<---------------cut here---------------start------------->8---
>>     Functionally split tramp-loaddefs.el into two parts: one part run
>>     while loading it at the very beginning of loading tramp.el (holding plain
>>     autoloads and declarations), and another run at the very end of loading
>>     tramp.el (holding setup code).
>>     This should reduce infinite-recursion problems while loading.
>> --8<---------------cut here---------------end--------------->8---
>>
>> See also bug#59194, which waits for me understanding what to do.
>
> [ It did look like something I'd seen before but I had no recollection
>   of actually writing it.  ]
>
> Damn!  So now I'm the one who needs to figure out how to keep it working
> in Emacs<31 while trying to make it behave with the new
> "autoload-expand".

Maybe it is as simple as requiring a (new?) file that defines the macro
directly, and whenever you require tramp-loaddefs, using NOERROR=t?

Then in Emacs 31, the first file with an ;;;###autoload followed by
usage of TRAMP's macro will be loaded, causing the macro to be defined.
If you want, you can declare `(autoload-macro expand)' when you define
the macro (I think old Emacs version will just ignore it?). This will
mean:

- In Emacs 31: the macro itself is never mentioned, but the add-hook and
  any other forms are instead copied into tramp-loaddefs.

- In earlier Emacs, the macro and then calls to it are both included in
  tramp-loaddefs, just as they are now.

I think either setup works (though on-demand expansion may be "safer",
since it doesn't depend on load order any more.)