我需要给不同的major mode设置不同的company-backends
,自然想到用个函数wrap一下,省略掉
(add-hook 'web-mode-hook (lambda ()
(setq-local company-backends (append '(company-web-html company-css) company-backends))))
这里的lambda
setq-local
append
部分。
一开始写出来是这样的:
(defun jester/set-company-backends-for-mode (mode backends)
"append `BACKENDS' to the head of default `company-backends',
use the result for `MODE'\'s local `company-backends'."
(add-hook (intern (format "%s\-hook" mode))
(lambda () (setq-local company-backends (append backends company-backends)))))
结果一直显示backends
是个void variable。想了很久,明明这是个普通的function啊,backends作为参数不是会被eval吗,哪里出了问题。
终于灵光乍现:lambda
里面的内容不会被eval!怎么办呢,要eval它再构造lambda,只能用macro了,试着写了下,测试下来是对的:
(defmacro jester/set-company-backends-for-mode (mode backends)
"append `BACKENDS' to the head of default `company-backends',
use the result for `MODE'\'s local `company-backends'."
`(add-hook (intern (format "%s\-hook" ,mode))
(lambda () (setq-local company-backends (append ,backends company-backends)))))