【问题标题】:emacs not accepting lambda in after-change-functionsemacs 在更改后函数中不接受 lambda
【发布时间】:2014-11-27 09:19:31
【问题描述】:

我正在尝试通过类方法重载给定缓冲区中的更改后函数。 notify-others-of-change 是一个任意函数。

(defmethod set-after-change-functions ((server server-class)
                                              name-of-buffer)
  "Adds appropriate after-change-functions to the given name-of-buffer."
  (with-current-buffer name-of-buffer
    (setq-local after-change-functions
                (cons 
                   (lambda (beg end prev-length)
                        (notify-others-of-change
                           server beg end prev-length))
                   after-change-functions))))

当试图在给定的缓冲区上运行它时(传入一个有效的服务器对象,我检查了),Emacs 对我大喊“符号作为变量的值是无效的:服务器”并且 after-change-functions 变为 nil,即使之前有元素。但是,当改为

(defmethod set-after-change-functions ((server server-class)
                                              name-of-buffer)
  "Adds appropriate after-change-functions to the given name-of-buffer."
  (with-current-buffer name-of-buffer
    (setq-local after-change-functions
                (cons
                   #'notify-others-of-change-SIMPLE
                   after-change-functions))))

其中 notify-others-of-change-SIMPLE 是基本的更改后函数,它只接受上面 lambda 中的三个参数,一切似乎都有效。我更喜欢在这里使用 lambda,但似乎不可能。为什么会出现这个问题,是否可以更改它以允许使用 lambda?

【问题讨论】:

  • 您设置了lexical-binding 吗?你的 lambda 函数引用了server,所以它必须是一个闭包才能工作。
  • 另见this question
  • 不要将setqsetq-local 用于挂钩。使用add-hookremove-hook
  • 你说“我更喜欢在这里使用 lambda”,但是将 lambdas 不必要地放入钩子变量中是不好的做法——它会使更新或删除它们有问题(如果你不这样做会导致错误小心),它使检查钩子变得困难(特别是如果 lambda 是字节编译的),并且无法从钩子跳转到函数定义的代码。由于所有这些原因,您应该更喜欢在挂钩中使用命名函数。

标签: variables debugging emacs


【解决方案1】:

将词法绑定设置为 t 作为文件局部变量,如 legoscia 链接的问题中所述,并确保对我所有的 lambdas 进行尖锐引用而不是仅仅引用,设法解决了问题。谢谢!

【讨论】:

  • 请不要引用你的 lambda,那个宏已经自引用了。
  • 好吧,在我这样做之前它没有用(维护未绑定的变量消息)。
  • 您提到您使用了尖锐的引号“而不仅仅是引用”。我推荐的根本不是引用。
  • 是的。文档here 表明两者是等价的;是风格问题吗?
  • 主要是。此代码 sn-p 是公开的,因此不应显示低于标准的示例。
猜你喜欢
  • 2020-04-27
  • 2021-11-21
  • 2016-04-02
  • 1970-01-01
  • 1970-01-01
  • 2010-11-29
  • 1970-01-01
  • 2016-04-21
  • 1970-01-01
相关资源
最近更新 更多