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

From: "J.D. Smith" <[email-masked]>
Date: Sun, 10 Aug 2025 17:41:37 -0400

"J.D. Smith" <[email-masked]> writes:

> Michael Albinus <[email-masked]> writes:
>
>> "J.D. Smith" <[email-masked]> writes:
>>
>> Hi,
>>
>>> Hmm, I guess `loaddefs-gen' was introduced in emacs 29.  But I think you
>>> don't actually need that `require' at all.  Maybe try a simple:
>>>
>>>     (unless (alist-get 'autoload-macro macro-declarations-alist)
>>>       (push '(autoload-macro ignore) macro-declarations-alist))
>>
>> That works with Emacs 28, if I put these lines before the
>> tramp--with-startup macro declaration.
>>
>> I cannot test with the master branch, because the autoload-macro patch
>> doesn't seem to be there yet.
>
> Great.  I will push it soon and then you can test, or feel free to try
> this patch in the meantime.

Here's an updated patch to try with TRAMP, to see if it works as
expected in both old and new Emacs versions.  Note that there are many
places where TRAMP does something more advanced, like:

  ;;;###tramp-autoload
  (when (featurep 'dbusbind)
    (tramp--with-startup
     (dolist (method tramp-gvfs-methods)
     ...

so I think you'll need to keep autoloading the macro definition too.

diff --git a/lisp/net/tramp-compat.el b/lisp/net/tramp-compat.el
index c1fb8ceaa80..9549bb8bcea 100644
--- a/lisp/net/tramp-compat.el
+++ b/lisp/net/tramp-compat.el
@@ -29,7 +29,7 @@
 
 ;;; Code:
 
-(require 'tramp-loaddefs)
+(require 'tramp-loaddefs nil t)  ; guard against load during autoload gen
 (require 'ansi-color)
 (require 'auth-source)
 (require 'format-spec)
diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
index e0e080021c7..f9d15216d5d 100644
--- a/lisp/net/tramp.el
+++ b/lisp/net/tramp.el
@@ -103,8 +103,15 @@ tramp-completion-file-name-regexp
 
   (put 'tramp--startup-hook 'tramp-suppress-trace t)
 
+  ;; TODO: once (autoload-macro expand) is available in all supported
+  ;; Emacs versions, this can be eliminated:
+  ;;  backward compatibility for autoload-macro declare form
+  (unless (assq 'autoload-macro macro-declarations-alist)
+    (push '(autoload-macro ignore) macro-declarations-alist))
+
   (defmacro tramp--with-startup (&rest body)
     "Schedule BODY to be executed at the end of tramp.el."
+    (declare (autoload-macro expand))
     `(add-hook 'tramp--startup-hook (lambda () ,@body)))
 
   (eval-and-compile