如何在主模式钩子中访问目录局部变量? [英] How can I access directory-local variables in my major mode hooks?
问题描述
我已经定义了一个.dir-locals.el文件,其中包含以下内容:
((python-mode。 (cr / virtualenv-name。saas))))
在我的.emacs中我有以下函数来检索该值并提供一个virtualenv路径:
(defun cr / virtualenv()
(cond cr / virtualenv-name(格式%s /%svirtualenv-base cr / virtualenv-name))
((getenvEMACS_VIRTUAL_ENV)(getenvEMACS_VIRTUAL_ENV))
(t /)$)
最后,在我的python-mode-hook列表中,我有这个钩子功能:
(add-hook'python-mode-hook'cr / python-mode-shell-设置)
(defun cr / python-mode-shell-setup()
(消息virtualenv-name is%scr / virtualenv-name)
(let (python-base(cr / virtualenv)))
(cond((和(fboundp'ipython-shell-hook)(file-executable-p(concat python-base/ bin / ipython)))
(setq pyth on-python-command(concat python-base/ bin / ipython))
(setq py-python-command(concat python-base/ bin / ipython))
(setq py- python-command-args'(-colorsNoColor))
(t
(setq python-python-command(concat python-base/ bin / python))
(setq py-python-command(concat python-base/ bin / python))
(setq py-python-command-args nil)))))
当我打开一个新的python文件,由 cr / python-mode-shell-setup
表示 cr / virtualenv-name
是 nil
。然而,当我C-h v的名字,我得到saas。
显然这里有一个加载订单问题;有没有办法让我的模式hook语句响应目录局部变量?
这是因为正常模式
调用(set-auto-mode)
和(hack-local-variables)
在该顺序。
然而 hack-local-variables-hook 是在本地变量有被处理,这使得一些解决方案:
-
第一个是使Emacs为每个主要运行一个新的局部变量钩模式:
(add-hook'hack-local-variables-hook'run-local- vars-mode-hook)
(defun run-local-vars-mode-hook()
在处理了本地变量之后,为主模式运行挂钩
run-hooks(intern(concat(symbol-name major-mode)-local-vars-hook))))
(add-hook'python-mode-local-vars-hook' cr / python-mode-shell-setup)
(您的ori第二个选项是使用可选的
LOCAL
参数add-hook
,使指定的函数缓冲区本地。使用这种方式,你可以按照如下方式编写你的钩子:(add-hook'python-mode -hook'cr / python-mode-shell-setup)
(defun cr / python-mode-shell-setup()
(add-hook'hack-local-variables-hook
(lambda()(messagevirtualenv-name is%scr / virtualenv-name)
(let((python-base(cr / virtualenv)))
(cond和(fboundp'ipython-shell-hook)(file-executable-p(concat python-base/ bin / ipython)))
(setq python-python-command(concat python-base/ bin / ipython)
(setq py-python-command(concat python-base/ bin / ipython))
(setq py-python-command-args'(-colorsNoColor ))
(t
(setq python-python-command(concat python-base/ bin / python))
(setq py-python-command(concat python-base /箱/ python))
(setq py-python-command-args nil)))))
nil t)); buffer-local hack-local-variables-hook
首先运行
python-mode-hook
,并为当前缓冲区注册匿名函数hack-local-variables-hook
只要;然后在处理局部变量后调用该函数。 -
Lindydancer的注释提示了第三种方法。它不像其他两个那么干净,但是证明是有趣的。我不喜欢引起
(hack-local-variables)
被调用两次的想法,但是我发现如果你设置了local-启用本地变量
缓冲区本地,它阻止(hack-local-variables)
做任何事情,所以你可以这样做:(defun cr / python-mode-shell-setup()
(报告错误文件局部变量错误:%s
(hack-local-variables)))
(set(make-local-variable'local-enable-local-variables )nil)
(let((python-base(cr / virtualenv)))
...))
显然,修改正常的执行顺序有点,所以副作用可能是可能的。我担心如果文件中的局部变量注释设置了相同的主要模式,这可能会导致无限递归,但实际上并不是一个问题。
局部变量头注释(例如
- * - mode:foo - * -
)由(set -auto-mode)
,所以这些都不错但是一个模式:foo
本地变量:
评论似乎是一个问题,因为它由(hack-local-variables)
,所以如果模式是这样设置的,我以为会导致递归。
练习我通过使用一个简单的函数作为模式来触发这个问题,这样做只是试图运行它的钩子;然而用正确模式进行测试没有出现问题,所以在现实中可能是安全的。我没有进一步研究(因为其他两个解决方案比这更清洁),但我猜想延迟模式钩子机制可能解释了吗?
I have defined a .dir-locals.el file with the following content:
((python-mode . ((cr/virtualenv-name . "saas"))))
In my .emacs I have the following function to retrieve this value and provide a virtualenv path:
(defun cr/virtualenv ()
(cond (cr/virtualenv-name (format "%s/%s" virtualenv-base cr/virtualenv-name))
((getenv "EMACS_VIRTUAL_ENV") (getenv "EMACS_VIRTUAL_ENV"))
(t "~/.emacs.d/python")))
Finally, in my python-mode-hook list, I have this hook function:
(add-hook 'python-mode-hook 'cr/python-mode-shell-setup)
(defun cr/python-mode-shell-setup ()
(message "virtualenv-name is %s" cr/virtualenv-name)
(let ((python-base (cr/virtualenv)))
(cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython")))
(setq python-python-command (concat python-base "/bin/ipython"))
(setq py-python-command (concat python-base "/bin/ipython"))
(setq py-python-command-args '( "-colors" "NoColor")))
(t
(setq python-python-command (concat python-base "/bin/python"))
(setq py-python-command (concat python-base "/bin/python"))
(setq py-python-command-args nil)))))
When I open a new python file, the message logged by cr/python-mode-shell-setup
indicates that cr/virtualenv-name
is nil
. However, when I C-h v the name, I get "saas" instead.
Obviously there's a load order issue here; is there a way to have my mode hook statements respond to directory-local variables?
This happens because normal-mode
calls (set-auto-mode)
and (hack-local-variables)
in that order.
However hack-local-variables-hook
is run after the local variables have been processed, which enables some solutions:
The first is to make Emacs run a new "local variables hook" for each major mode:
(add-hook 'hack-local-variables-hook 'run-local-vars-mode-hook) (defun run-local-vars-mode-hook () "Run a hook for the major-mode after the local variables have been processed." (run-hooks (intern (concat (symbol-name major-mode) "-local-vars-hook")))) (add-hook 'python-mode-local-vars-hook 'cr/python-mode-shell-setup)
(Your original function can be used unmodified, with that approach.)
A second option is to utilise the optional
LOCAL
argument toadd-hook
that makes the specified function buffer-local. With this approach you could write your hook as follows:(add-hook 'python-mode-hook 'cr/python-mode-shell-setup) (defun cr/python-mode-shell-setup () (add-hook 'hack-local-variables-hook (lambda () (message "virtualenv-name is %s" cr/virtualenv-name) (let ((python-base (cr/virtualenv))) (cond ((and (fboundp 'ipython-shell-hook) (file-executable-p (concat python-base "/bin/ipython"))) (setq python-python-command (concat python-base "/bin/ipython")) (setq py-python-command (concat python-base "/bin/ipython")) (setq py-python-command-args '( "-colors" "NoColor"))) (t (setq python-python-command (concat python-base "/bin/python")) (setq py-python-command (concat python-base "/bin/python")) (setq py-python-command-args nil))))) nil t)) ; buffer-local hack-local-variables-hook
i.e.
python-mode-hook
runs first and registers the anonymous function withhack-local-variables-hook
for the current buffer only; and that function is then called after the local variables have been processed.Lindydancer's comment prompts a third approach. It's not nearly as clean as the other two, but proved interesting regardless. I didn't like the idea of causing
(hack-local-variables)
to be called twice, but I see that if you set thelocal-enable-local-variables
buffer-locally, it prevents(hack-local-variables)
from doing anything, so you could do this:(defun cr/python-mode-shell-setup () (report-errors "File local-variables error: %s" (hack-local-variables))) (set (make-local-variable 'local-enable-local-variables) nil) (let ((python-base (cr/virtualenv))) ...))
Obviously that modifies the normal sequence of execution a little, so side effects may be possible. I was worried that if the same major mode is set by a local variable comment in the file, this might cause infinite recursion, but that doesn't actually appear to be a problem.
Local variable header comments (e.g.
-*- mode: foo -*-
) are handled by(set-auto-mode)
, so those are fine; but amode: foo
Local Variables:
comment seems like it would be an issue as it is handled by(hack-local-variables)
, and so if the mode is set that way I thought it would cause recursion.In practice I was able to trigger the problem by using a simple function as a 'mode' which did nothing more than try to run its hooks; however testing with a 'proper' mode did not exhibit the problem, so it's probably safe in reality. I didn't look into this further (as the other two solutions are much cleaner than this), but I would guess the delayed mode hooks mechanism probably explains it?
这篇关于如何在主模式钩子中访问目录局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!