How can I get create an autoloaded function from a macro function factory? For example, say I have a macro to create alignment functions as follows, but I want to be able to specify an option so the expanded macro has an autoload cookie.

(defmacro align-by (name doc regex)
  "Alignment function factory."
  (declare (indent 1))
  (let ((fn (intern name)))
    `(defun ,fn (start end)
       ,doc
       (interactive "r")
       (align-regexp start end ,regex))))

(align-by "align-backslash"
  "Align backslashes at end of line in region."
  "\\(\\s-*\\)\\\\$")

I know I can write this, but can I avoid needing to write this for every function?

;;;###autoload (autoload 'align-backslash "this-file")
share|improve this question
1  
What's the scenario where you would need to do this? I am having a hard time thinking of anything that wouldn't be solved by just autoloading the macro itself, but I am guessing this is just over my head, so I am genuinely curious. – Jack Aug 5 '16 at 1:37

It's not clear where the macro would pick up the name of the file to be autoloaded - you do not pass a file name to the macro, currently.

Assuming that the file name comes from a file that is being visited when the macro is expanded, this will do it:

(defmacro align-by (name doc regex)
  "Alignment function factory."
  (declare (indent 1))
  (let ((fn (intern name)))
    `(progn
       ,(and (buffer-file-name)
             `(autoload ',name ,(buffer-file-name)))
       (defun ,fn (start end)
         ,doc
         (interactive "r")
         (align-regexp start end ,regex)))))

Testing:

(macroexpand '(align-by "align-backslash"
           "Align backslashes at end of line in region."
           "\\(\\s-*\\)\\\\$"))

C-u C-x C-e shows that that gives this when the current buffer is not visiting a file:

(progn
  (autoload '"align-backslash" nil)
  (defun align-backslash
      (start end)
    "Align backslashes at end of line in region."
    (interactive "r")
    (align-regexp start end "\\(\\s-*\\)\\\\$")))

And it gives this when the buffer is visiting file foo.el, where ".../foo.el" is really the absolute file name for foo.el:

(progn
  (autoload '"align-backslash" ".../foo.el")
  (defun align-backslash
      (start end)
    "Align backslashes at end of line in region."
    (interactive "r")
    (align-regexp start end "\\(\\s-*\\)\\\\$")))
share|improve this answer
    
You don't need to expand the macros: autoload is called along with defun definition when you call (align-by "align-backslash" ...). – artscan Aug 6 '16 at 8:15

The code that picks up the ;;;###autoload cookies does expand the macros before looking at the code, so you should be able to just place a ;;;###autoload cookie right before a (align-by ...) expression and get the right autoload placed in the <foo>-autoloads.el file.

The problem, tho is that your macro is probably not going to be defined at the time the autoloads are created, so the macro expansion will not actually happen. Maybe a M-x report-emacs-bug is in order.

share|improve this answer

As emacs manual mentioned

If you write a function definition with an unusual macro that is not one of the known and recognized function definition methods, use of an ordinary magic autoload comment would copy the whole definition into loaddefs.el. That is not desirable. You can put the desired autoload call into loaddefs.el instead by writing this:

;;;###autoload (autoload 'foo "myfile")
(mydefunmacro foo
  ...)

Your align-by is like mydefunmacro in the manual example. It is not known function definition method and it is not supported by autoload mechanism.

So there are three alternatives:

  1. Extend the list of supported types (defun, defmacro, cl-defun, defclass,...) by your special macros. Then you can use simple ;;;###autoload "decorator".
  2. Invent your own mechanism of "myfile" parsing (without executing) and "loaddefs" populating by necessary autoload definitions.
  3. Use more complicated construction (with (autoload 'align-backslash "myfile")) as function defintion method.

If you rewrite align-by like this (without intern):

(defmacro align-by-defun (name doc regex)
  "Alignment function factory."
  (declare (indent 1))
  `(defun ,name (start end)
     ,doc
     (interactive "r")
     (align-regexp start end ,regex)))

;;;###autoload (autoload 'align-backslash "myfile")
(align-by-defun align-backslash
  "Align backslashes at end of line in region."
  "\\(\\s-*\\)\\\\$")

you can see that align-by is just a function definition method (like cl-defun).

share|improve this answer
    
No, you can use align-by-defun as a function factory too. It does not matter. But do you have the full list of your functions (align-backslash, ...) in advance? Or it will be created dynamically? – artscan Aug 7 '16 at 4:09
    
Do you want all of them have this-file link (to align-by definition) in help buffer or something else? – artscan Aug 7 '16 at 4:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.