Why do I get Error running timer: (void-variable message)
in the function below in my `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))))))
You need to turn on
lexical-binding
, for thatmessage
occurrence in the lambda not to be treated as a free variable. It's a lexical variable local to functioncypher/cowsayx-sclock
, but within the lambda it's free.Otherwise, you need to instead substitute the value of variable
message
in the lambda expression, and use that as a list. Here's a backquoted expression that gives you that list with themessage
value substituted.But this is less performant than using
lexical-binding
, which produces a closure for the lambda, encapsulating the value ofmessage
.See the Elisp manual, node Using Lexical Binding.
You can, for example, just put this at the end of a comment line as the first line of your file:
For example, if your code is in file
foo.el
then this could be its first line: