在emacs中,我们可以定义可自定义的用户选项变量.
defcustom - 在Emacs中编程Lisp http://www.gnu.org/software/emacs/manual/html_node/eintr/defcustom.html
我们可以使变量具有缓冲区本地绑定.
创建Buffer-Local - GNU Emacs Lisp参考手册http://www.gnu.org/software/emacs/manual/html_node/elisp/Creating-Buffer_002dLocal.html#Creating-Buffer_002dLocal
如果我想制作不可自定义的变量,我可以使用make-local-variable或setq-local.
但我找不到任何方法如何使自定义变量具有缓冲区本地绑定.
即使我调用make-local-variable了变量defcustom,也custom-set-variables设置为global-value.
如果我调用setq-local,则将value设置为local-variable.这个比较好.但我认为这不是最佳做法.
有没有任何有效的方法如何为defcustom定义的变量设置缓冲区本地值?
答案是:你不能,至少不能使用自定义UI.
你可以做的是添加一个sexp,它将变量的缓冲区本地值设置为你的init文件.
做一个如下:
无论缓冲区是什么,都要使它始终是缓冲区本地的:
(make-variable-buffer-local 'the-variable)
你可以把它放在你的init文件中.
只为当前缓冲区设置缓冲区本地,即在选择它之后为某些缓冲区:
(make-local-variable 'the-variable)
为此,您需要将sexp放入选择所需缓冲区的sexp中.那可能是,例如:
(with-current-buffer (get-buffer-create "the-buffer-name")
(make-local-variable 'the-variable))
这假定缓冲区可以合理地创建或已经存在.如果你这样做,这样做后,你custom-file已经被加载.也就是说,在你的init文件中,加载后custom-file(我推荐),或者,如果你不使用custom-file,在Customize自动生成的任何代码之后(例如custom-set-variables).
您也可以将make-local-variablesexp放在模式挂钩上,因此每当您处于具有特定模式的缓冲区时,它就会被执行.
所有这些,我在2012年向Emacs Dev 提交了一个增强请求,要求用户能够使用Customize UI设置(并可能保存)用户选项的缓冲区本地值.到目前为止,它睡在"愿望清单"类别中.