我已經定義與以下內容爲.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模式掛機名單,我有這個鉤子函數:
(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文件,該消息由記錄210表示cr/virtualenv-name
是nil
。然而,當我用C-h對這個名字時,我得到了「saas」。
顯然這裏有一個加載順序問題;有沒有辦法讓我的模式鉤子語句響應目錄本地變量?
也許你可以從你的鉤子調用(hack-local-variables)? – Lindydancer 2011-03-01 07:51:33
我希望我能兩次對此表示讚賞。 – 2011-03-01 18:09:29