godblessfq
godblessfq

Reputation: 218

How to give a list as arguments to a macro in elisp?

I want to achieve something like this:

(setq my-global-keybindings
      '(([?\C-x ?\C-d] . dired)
        ([?\C-x ?\C-b] . ibuffer)
        ([?\C-x b] . ivy-switch-buffer)))

(apply #'bind-keys* my-global-keybindings)

But bind-keys* is a macro here.

Upvotes: 2

Views: 604

Answers (1)

Stefan
Stefan

Reputation: 28521

In the specific case of binding keys, I think the better option is to look for a function that replaces that bind-keys* macro (I don't see any justification why it should be a macro rather than a function).

But as for the more general question, here's how I'd do it:

(defmacro my-multi-bind-keys (bindings)
  (macroexp-progn
    (mapcar (lambda (binding)
             `(bind-keys* ,@binding))
            bindings)))
(my-multi-bind-keys (([?\C-x ?\C-d] dired)
                     ([?\C-x ?\C-b] ibuffer)
                     ([?\C-x b] ivy-switch-buffer)))

Note that using a setq like you did is problematic: macros needs to be expanded during compilation but the setq should not be executed by the compiler (it should be compiled by the compiler to be run when the code is later executed) so there's no sound way for the macro to get access to the var's value (at least until time-travel is made to work).

Upvotes: 1

Related Questions