2

我想让这个函数设置键绑定更短。

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

然后我用 2 个参数(键绑定和应用程序名称)编写更短的函数

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

当我尝试将密钥作为参数时,它将起作用

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

(defkey-arg2 "s-g")

但是,当我尝试编写这样的函数时

(defun defkey-arg2 (key command)

或者

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

(defkey-arg2 "gkamus")

它引发错误:

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

1 回答 1

2

的主体lambda不被评估。使用反引号,command可以将 的值代入结果表达式。

(defun defkey-arg2 (command)
  (define-key (current-local-map)
    (kbd "s-g")
    `(lambda ()
       (interactive)
       (start-process-shell-command ,command nil ,command))))
于 2020-05-08T21:28:25.167 回答