Merge pull request #1815 from bersace/pyenv-set-version-from-file
lang/python: recognize .python-version files
This commit is contained in:
commit
c023cef415
2 changed files with 33 additions and 1 deletions
30
modules/lang/python/autoload/pyenv.el
Normal file
30
modules/lang/python/autoload/pyenv.el
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
;;; lang/python/autoload/pyenv.el -*- lexical-binding: t; -*-
|
||||||
|
;;;###if (featurep! +pyenv)
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defvar +pyenv--version nil)
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +python-pyenv-mode-set-auto-h ()
|
||||||
|
"Set pyenv-mode version from buffer-local variable."
|
||||||
|
(when (eq major-mode 'python-mode)
|
||||||
|
(when (not (local-variable-p '+pyenv--version))
|
||||||
|
(make-local-variable '+pyenv--version)
|
||||||
|
(setq +pyenv--version (+python-pyenv-read-version-from-file)))
|
||||||
|
(if +pyenv--version
|
||||||
|
(pyenv-mode-set +pyenv--version)
|
||||||
|
(pyenv-mode-unset))))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +python-pyenv-read-version-from-file ()
|
||||||
|
"Read pyenv version from .python-version file."
|
||||||
|
(when-let (root-path (projectile-locate-dominating-file default-directory ".python-version"))
|
||||||
|
(let* ((file-path (expand-file-name ".python-version" root-path))
|
||||||
|
(version
|
||||||
|
(with-temp-buffer
|
||||||
|
(insert-file-contents-literally file-path)
|
||||||
|
(string-trim (buffer-string)))))
|
||||||
|
(if (member version (pyenv-mode-versions))
|
||||||
|
version ;; return.
|
||||||
|
(message "pyenv: version `%s' is not installed (set by `%s')."
|
||||||
|
version file-path)))))
|
|
@ -223,7 +223,9 @@ called.")
|
||||||
:config
|
:config
|
||||||
(pyenv-mode +1)
|
(pyenv-mode +1)
|
||||||
(when (executable-find "pyenv")
|
(when (executable-find "pyenv")
|
||||||
(add-to-list 'exec-path (expand-file-name "shims" (or (getenv "PYENV_ROOT") "~/.pyenv")))))
|
(add-to-list 'exec-path (expand-file-name "shims" (or (getenv "PYENV_ROOT") "~/.pyenv"))))
|
||||||
|
(add-hook 'python-mode-hook #'+python-pyenv-mode-set-auto-h)
|
||||||
|
(add-hook 'doom-switch-buffer-hook #'+python-pyenv-mode-set-auto-h))
|
||||||
|
|
||||||
|
|
||||||
(use-package! conda
|
(use-package! conda
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue