variables - 运行计时器时出错:Emacs init.el 中的(无效变量消息)
问题描述
为什么我会Error running timer: (void-variable message)
在我的 `init.el - Emacs 中进入下面的函数?
(defun cypher/cowsayx-sclock (in-minutes message)
(interactive "nSet the time from now - min.: \nsWhat: ")
(run-at-time (* in-minutes 60)
nil
(lambda ()
(message "%S" message)
(shell-command (format "xcowsay %s" (shell-quote-argument
message))))))
解决方案
您需要打开lexical-binding
,因为message
lambda 中的该事件不会被视为自由变量。它是 function 的局部词法变量cypher/cowsayx-sclock
,但在 lambda 中它是免费的。
否则,您需要替换lambda 表达式中的变量值message
,并将其用作列表。这是一个反引号表达式,它为您提供了message
替换值的列表。
`(lambda ()
(message "%S" ',message)
(shell-command (format "xcowsay %s" (shell-quote-argument ',message)))
但这比 using 的性能要差lexical-binding
,后者会为 lambda 生成一个闭包,封装 的值message
。
请参阅 Elisp 手册,节点Using Lexical Binding。
例如,您可以将其放在注释行的末尾作为文件的第一行:
-*- lexical-binding:t -*-
例如,如果您的代码在文件中,foo.el
那么这可能是它的第一行:
;;; foo.el --- Code that does foo things. -*- lexical-binding:t -*-
推荐阅读
- javascript - TypeScript 错误:类型“字符串”不可分配给排版的类型
- c# - ivi.visa 读取二进制块数据的问题
- mybatis - 创建一个 mybatis dao 模板来动态执行任何查询
- python - 显示两个数据框列中两个字符串之间的差异的位置,pandas
- javascript - Office.js onReady/initialize 在 Internet Explorer 上不起作用
- import - 无法打开文件“load_departments.dump”;错误:2
- javascript - 在Angular 2+中处理子组件中通过引用传递的对象
- hibernate - 如何在不触发验证的情况下将模型传递给表单(编辑前)
- javascript - 如何在 Google Sheets API (Cocos Creator) 中使用“查找”
- url - 亚马逊如何缩短会员网址