如何在elisp中为宏提供参数列表? [英] How to give a list as arguments to a macro in elisp?

查看:77
本文介绍了如何在elisp中为宏提供参数列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现以下目标:

(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)

但是bind-keys *是这里的宏。

But bind-keys* is a macro here.

推荐答案

在绑定键的特定情况下,我认为更好的选择是要寻找一个函数来替换 bind-keys * 宏(我看不出为什么它应该是宏而不是函数)。

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)))

请注意,使用 setq 就像您所做的那样是有问题的:宏在编译期间需要扩展,但是 setq 不应由编译器执行(它应该由编译器进行编译,以便在以后执行代码时运行),因此宏没有合理的方法来访问var的值(至少要等到时间旅行才可以使用) )。

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).

这篇关于如何在elisp中为宏提供参数列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆