Skip to content
Browse files

Read venv from .venv instead of .dir-locals.el

  • Loading branch information...
1 parent 8c64123 commit 8b0f7c2f1cc4d8ec456d30c8de2a7e36889f25b4 @bartvandendriessche committed Mar 18, 2014
Showing with 37 additions and 17 deletions.
  1. +37 −17 settings/settings-python.el
View
54 settings/settings-python.el
@@ -16,27 +16,47 @@
(add-hook 'python-mode-hook 'jedi:setup)
(setq jedi:install-imenu)
(setq jedi:use-shortcuts t)
- (setq jedi:complete-on-dot t)))))))
- ;; load virtual environment if possible
- (add-hook 'python-mode-hook '(lambda ()
- (if project-venv-name
- ((hack-local-variables)
- (venv-workon project-venv-name)))))
+ (setq jedi:complete-on-dot t))))))))
- ;; use projectile-mode in python projects
- (add-hook 'python-mode-hook 'projectile-on)
+(defun settings-ac-python-mode-setup ()
+ (require 'auto-complete-config)
+ (yas-global-mode 1)
+ (add-to-list 'ac-sources 'ac-source-yasnippet))
- ;; be sure to `pip install flake8` for python flychecking
- (add-hook 'python-mode-hook 'flycheck-mode)
+(defun settings-venv-name-from-dot-venv-file (dot-venv-file)
+ "Reads the virtualenv from a .venv file"
+ (when (file-exists-p dot-venv-file)
+ (with-temp-buffer
+ (insert-file-contents dot-venv-file)
+ (if (string-match "[ \t]*$" (buffer-string))
+ (replace-match "" nil nil (buffer-string))
+ (buffer-string)))))
- (defun ac-python-mode-setup ()
- (require 'auto-complete-config)
- (yas-global-mode 1)
- (add-to-list 'ac-sources 'ac-source-yasnippet))
+(defun settings-guess-venv ()
+ "Guess the correct virtualenv for current file."
+ (require 'projectile)
+ (let* ((dot-venv-file (concat (file-name-as-directory (projectile-project-root)) ".venv"))
+ (venv-name (settings-venv-name-from-dot-venv-file dot-venv-file)))
+ (when venv-name venv-name)))
- (add-hook 'python-mode-hook 'ac-python-mode-setup)
+(defun settings-guess-venv-and-activate ()
+ "Guesses the correct virtualenv, and activates it."
+ (let ((venv-name (settings-guess-venv)))
+ (when venv-name
+ (message "Working on virtualenv %s" venv-name)
+ (venv-workon venv-name))))
- ;; python debugger command
- (setq gud-pdb-command-name "python -m pdb"))
+
+;; use projectile-mode in python projects
+(add-hook 'python-mode-hook 'projectile-on)
+;; be sure to `pip install flake8` for python flychecking
+(add-hook 'python-mode-hook 'flycheck-mode)
+;; setup python yasnippet autocomplete
+(add-hook 'python-mode-hook 'settings-ac-python-mode-setup)
+;; try to guess the correct virtualenv
+(add-hook 'python-mode-hook 'settings-guess-venv-and-activate)
+
+;; python debugger command
+(setq gud-pdb-command-name "python -m pdb")
(provide 'settings-python)

0 comments on commit 8b0f7c2

Please sign in to comment.
Something went wrong with that request. Please try again.