2

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
Drew
  • 29,895
  • 7
  • 74
  • 104
moanrisy
  • 109
  • 1
  • 12
  • This may be a duplicate, but I don't have time to look for it. – Drew May 08 '20 at 23:40
  • I always try to find one before ask question. may be the duplicate have title that not explain the problem well. – moanrisy May 09 '20 at 02:26
  • 1
    Right. Sometimes it's tricky. And in the case of this error message there are multiple possible causes. Anyway, thanks for checking first - that helps a lot. – Drew May 09 '20 at 03:05

1 Answers1

2

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))))
Rorschach
  • 31,301
  • 5
  • 78
  • 129