1

I'm in the process of "modularizing" my config file and ran into a problem involving elisp macros.

Because of the way in which I'm defining my configuration modules, I ended up with a bunch of code blocks as the following:

(with-eval-after-load (expand-file-name
                       "latex.el"
                       j/config-path)
  (leader
    :infix "o"
    "e" '(ebib :which-key "ebib"))
  (some-function x y z))

Since I was repeting that a lot, I tried to define a macro to have more readable config files. It is my first time writing macros in elisp, so this was what I ended up with:

(defmacro j/con-load (module &rest funcs)
      "TODO doc goes here"
      `(with-eval-after-load
           (expand-file-name (concat ,module ".el" j/config-path))
         funcs))

I'm trying to call it with

(j/con-load "latex"
  (leader
    :infix "o"
    "e" '(ebib :which-key "ebib"))
  (some-function x y z))

but it just doesn't work.

What am I doing wrong here?

2 Answers 2

2

M-x pp-macroexpand-last-sexp is your friend. It shows you the code that your macro is expanding into, which is:

(with-eval-after-load
    (expand-file-name
     (concat "latex" ".el" j/config-path))
  funcs)

Which tells Emacs to evaluate the variable funcs (which probably won't exist) should it ever load a library </path/to/current/default-directory>/latex.el</value/of/j/config-path> (which seems rather unlikely to ever occur).

@q.undertow has covered the concat usage.

For the rest, you probably wanted to splice your &rest argument into the expanded macro with ,@funcs ?

See C-hig (elisp)Backquote for more about splicing.

3

For one thing (and I am not at all sure there isn't another problem), the call to expand-file-name in the macro body is scrambled. It should be

(expand-file-name (concat ,module ".el") j/config-path)

Also you may want to evaluate the concatentation at macro expansion time:

(expand-file-name ,(concat module ".el") j/config-path)

hope this helps

1
  • Thanks for pointing this out. As you said, there are more errors in my code, with this is one less thing to worry about. Aug 30, 2020 at 17:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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