我定义了一个 .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”。
显然这里有一个加载顺序问题;有没有办法让我的模式挂钩语句响应目录局部变量?