Browse Source

Update

master
10sr 6 years ago
parent
commit
82b78af375
Signed by: 10sr GPG Key ID: 7BEC428194130EB2
1 changed files with 41 additions and 17 deletions
  1. +41
    -17
      emacs.el

+ 41
- 17
emacs.el View File

@@ -2021,18 +2021,22 @@ This variable is intended to be explicitly set by user.
When nil, `with-venv' tries to find suitable venv dir.")

(defmacro with-venv-dir (dir &rest body)
"Set python environment to DIR and execute BODY."
"Set python environment to DIR and execute BODY.

This macro does not check if DIR is a valid python environemnt.
If dir is nil, execute BODY as usual."
`(let ((--with-venv-process-environment-orig (cl-copy-list process-environment))
(--with-venv-exec-path-orig (cl-copy-list exec-path)))
(unwind-protect
(progn
;; Do the same thing that bin/activate does
(setq exec-path
(cons (concat ,dir "/bin")
exec-path))
(setenv "VIRTUAL_ENV" ,dir)
(setenv "PATH" (concat ,dir "/bin:" (or (getenv "PATH") "")))
(setenv "PYTHONHOME")
(when dir
;; Do the same thing that bin/activate does
(setq exec-path
(cons (concat ,dir "/bin")
exec-path))
(setenv "VIRTUAL_ENV" ,dir)
(setenv "PATH" (concat ,dir "/bin:" (or (getenv "PATH") "")))
(setenv "PYTHONHOME"))
,@body)
(setq process-environment
--with-venv-process-environment-orig)
@@ -2042,25 +2046,37 @@ When nil, `with-venv' tries to find suitable venv dir.")
(defmacro with-venv (&rest body)
"Execute BODY with venv enabled.

This function tries to find suitable venv dir, or raise error when none found."
This function tries to find suitable venv dir, or run BODY as usual when no
suitable environment was found."
`(with-venv-dir
;; If set explicitly use it
,(or with-venv-venv-dir
(with-venv-find-venv-dir))
,@body))

(defvar-local with-venv-previously-used nil
"Previously used venv dir path.")
(defun with-venv-find-venv-dir (&optional dir)
"Try to find venv dir for DIR or raise error when none found."
"Try to find venv dir for DIR.
If none found return nil."
(with-temp-buffer
(when dir
(cd dir))
(or
;; Check pipenv
(with-venv--find-venv-dir-pipenv)
;; Check poetry
(with-venv--find-venv-dir-poetry)
;; Search for .venv dir
(with-venv--find-venv-dir-by-name))))
(let ((result (or
;; Check used previously
(with-venv-check-exists
with-venv-previously-used)
;; Check pipenv
(with-venv-check-exists
(with-venv--find-venv-dir-pipenv))
;; Check poetry
(with-venv-check-exists
(with-venv--find-venv-dir-poetry))
;; Search for .venv dir
(with-venv-check-exists
(with-venv--find-venv-dir-by-name)))))
(setq with-venv-previously-used
result))))

(defun with-venv--find-venv-dir-pipenv ()
"Try to find venv dir via pipenv."
@@ -2087,9 +2103,17 @@ This function tries to find suitable venv dir, or raise error when none found."
(let ((dir (locate-dominating-file default-directory
".venv")))
(when dir
;; TODO: check with -check-exists
(expand-file-name ".venv"
dir))))

(defun with-venv-check-exists (dir)
"Return DIR as is if \"bin\" directory was found under DIR."
(and dir
(file-directory-p (expand-file-name "bin"
dir))
dir))


;; (with-venv (:dir default-directory) (message "a"))
;; (with-venv () (message "a"))


Loading…
Cancel
Save