2014-01-20 20 views
10

emacs-jediは異なるプロジェクトのファイルを編集しているときに検出し、対応するvirtualenvを利用できます。慣習で私のvirtualenvsは私のプロジェクトと同じ名前を持っています。それらは$HOME/.virtualenvs/emacs-jediでプロジェクト固有のvirtualenvを使用するにはどうすればいいですか

私が見つけたkenobi.elにありますが、virtualenvsはプロジェクトルートのbinディレクトリにあります。それはまた私が必要としない他のいくつかの機能を持っています。

kenobi.elからインスピレーションを得て、私は次のようにjediを初期化しました。それはかなりうまくいくが、完璧ではない。

私のプロジェクトからライブラリAをインポートした場合、ABをインポートします。私はAで定義された定義にジャンプすることができますが、そこには一度、Bから定義にジャンプすることはできません。

マイ初期化:

(defun project-directory (buffer-name) 
    (let ((git-dir (file-name-directory buffer-name))) 
    (while (and (not (file-exists-p (concat git-dir ".git"))) 
       git-dir) 
     (setq git-dir 
      (if (equal git-dir "/") 
       nil 
       (file-name-directory (directory-file-name git-dir))))) 
    git-dir)) 

(defun project-name (buffer-name) 
    (let ((git-dir (project-directory buffer-name))) 
    (if git-dir 
     (file-name-nondirectory 
     (directory-file-name git-dir)) 
     nil))) 

(defun virtualenv-directory (buffer-name) 
    (let ((venv-dir (expand-file-name 
        (concat "~/.virtualenvs/" (project-name buffer-name))))) 
    (if (and venv-dir (file-exists-p venv-dir)) 
     venv-dir 
     nil)))  

(defun jedi-setup-args() 
    (let ((venv-dir (virtualenv-directory buffer-file-name))) 
    (when venv-dir 
     (set (make-local-variable 'jedi:server-args) (list "--virtual-env" venv-dir))))) 

(setq jedi:setup-keys t) 
(setq jedi:complete-on-dot t) 
(add-hook 'python-mode-hook 'jedi-setup-args) 
(add-hook 'python-mode-hook 'jedi:setup) 

私はジェダイを初期化する方法が間違っていますか?

答えて

13

は、私は今のemacs-ジェダイはVIRTUAL_ENV環境変数からvirtualenvのパスをピックアップすることができ、virtualenvsを有効にするにはvirtualenvwrapper ELPAパッケージを使用するソリューションに落ち着いてきました。ここで

は完全な作業は、Emacs-ジェダイ初期化です:

(defun project-directory (buffer-name) 
    "Return the root directory of the project that contain the 
given BUFFER-NAME. Any directory with a .git or .jedi file/directory 
is considered to be a project root." 
    (interactive) 
    (let ((root-dir (file-name-directory buffer-name))) 
    (while (and root-dir 
       (not (file-exists-p (concat root-dir ".git"))) 
       (not (file-exists-p (concat root-dir ".jedi")))) 
     (setq root-dir 
      (if (equal root-dir "/") 
       nil 
       (file-name-directory (directory-file-name root-dir))))) 
    root-dir)) 

(defun project-name (buffer-name) 
    "Return the name of the project that contain the given BUFFER-NAME." 
    (let ((root-dir (project-directory buffer-name))) 
    (if root-dir 
     (file-name-nondirectory 
     (directory-file-name root-dir)) 
     nil))) 

(defun jedi-setup-venv() 
    "Activates the virtualenv of the current buffer." 
    (let ((project-name (project-name buffer-file-name))) 
    (when project-name (venv-workon project-name)))) 

(setq jedi:setup-keys t) 
(setq jedi:complete-on-dot t) 
(add-hook 'python-mode-hook 'jedi-setup-venv) 
(add-hook 'python-mode-hook 'jedi:setup) 

は、あなたが最初にvirtualenvwrapperをインストールする必要があることを忘れないでください。

プロジェクト仮想環境を自動的にアクティブ化する別の方法については、virtualenvwrapper documentationを参照してください。あなたのvirtualenvのの名前に

((python-mode . ((project-venv-name . "myproject-env")))) 

変更"myproject-env"python-modeフック使用virtualenvironmentをアクティブ:一言で言えば、あなたは、以下の内容で、プロジェクトのルートに.dir-locals.elファイルを作成することができ

(add-hook 'python-mode-hook (lambda() 
           (hack-local-variables) 
           (venv-workon project-venv-name))) 
(add-hook 'python-mode-hook 'jedi:setup) 
+0

を別の素敵なパッケージauto-virtualenvwrapperがあります:https://github.com/robert-zaremba/auto-virtualenvwrapper.el。私はあなたがそれを試みると思います。 – pingz

関連する問題