如何访问主模式挂钩中的目录局部变量?
我定义了一个包含以下内容的 .dir-locals.el 文件:
((python-mode . ((cr/virtualenv-name . "saas"))))
在我的 .emacs 中,我有以下函数来检索该值并提供 virtualenv 路径:
(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")))
最后,在我的 python-mode-hook 列表中,我有这个钩子功能:
(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)))))
当我打开一个新的 python 文件时,cr/python-mode-shell-setup
记录的消息表明 cr/virtualenv-name
为 nil< /代码>。然而,当我查看这个名字时,我得到的是“saas”。
显然这里存在加载顺序问题;有没有办法让我的模式挂钩语句响应目录局部变量?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
发生这种情况是因为
正常模式
按顺序调用(set-auto-mode)
和(hack-local-variables)
。然而 hack-local-variables-hook 是在处理局部变量之后运行的,这可以实现一些解决方案:
(使用这种方法,您的原始函数可以不加修改地使用。)
add-hook
的可选LOCAL
参数,使指定的函数成为缓冲区本地的。使用这种方法,您可以按如下方式编写钩子:ie
python-mode-hook
首先运行,并仅针对当前缓冲区向hack-local-variables-hook
注册匿名函数;然后在处理局部变量后调用该函数。Lindydancer 的评论提出了第三种方法。它不像其他两个那么干净,但无论如何都证明很有趣。我不喜欢导致
(hack-local-variables)
被调用两次的想法,但我发现如果您设置local-enable-local-variables
本地缓冲区,它会阻止(hack-local-variables)
做任何事情,所以你可以这样做:显然,这会稍微修改正常的执行顺序,因此可能会产生副作用。我担心如果文件中的局部变量 comment 设置相同的主要模式,这可能会导致无限递归,但这实际上似乎不是问题。
局部变量头注释(例如
-*- mode: foo -*-
)由(set-auto-mode)
处理,所以这些都很好;但是mode: foo
Local Variables:
注释似乎是一个问题,因为它是由(hack-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:
(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: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: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?