Search code examples
variablesdebuggingemacsquotebackquote

'Symbol's value as variable is void:' when using parameter on defun inside lambda


I would like to make this function to set keybind more shorter.

(defun defkey-arg2 ()
  (exwm-input-set-key (kbd "s-g")
                      (lambda ()
                        (interactive)
                        (start-process-shell-command gkamus nil gkamus))))

then I write the shorter function with 2 parameters (the keybind and the app name)

(defun defkey-arg2 (key command) (...)

When I try the key as parameter, it will work

(defun defkey-arg2 (key)
  (exwm-input-set-key (kbd key)
                      (lambda ()
                        (interactive)
                        (start-process-shell-command gkamus nil gkamus))))

(defkey-arg2 "s-g")

But, when I try write function like this

(defun defkey-arg2 (key command)

or

(defun defkey-arg2 (command)
  (exwm-input-set-key (kbd "s-g")
                      (lambda ()
                        (interactive)
                        (start-process-shell-command command nil command)))

(defkey-arg2 "gkamus")

it raises error:

Symbol's value as variable is void:' when using parameter on defun

Solution

  • The body of lambda isn't evaluated. Using a backquote, the value of command can be substituted into the resulting expression.

    (defun defkey-arg2 (command)
      (define-key (current-local-map)
        (kbd "s-g")
        `(lambda ()
           (interactive)
           (start-process-shell-command ,command nil ,command))))