lang/python: rewrite modeline version segment
+ Add $PYENV_ROOT/shims was added to exec-path, so pyenv python version is picked up on. + Fixes out-of-date python version in the modeline of other buffers after switching pyenv/pyvenv/conda envs. + The pipenv version and regular python display have been merged.
This commit is contained in:
parent
8afbb804d9
commit
b91a8f0d2f
2 changed files with 53 additions and 32 deletions
|
@ -1,11 +1,18 @@
|
||||||
;;; lang/python/autoload/python.el -*- lexical-binding: t; -*-
|
;;; lang/python/autoload/python.el -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
|
(defvar +python-version-cache (make-hash-table :test 'equal)
|
||||||
|
"TODO")
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +python/repl ()
|
(defun +python/repl ()
|
||||||
"Open the Python REPL."
|
"Open the Python REPL."
|
||||||
(interactive)
|
(interactive)
|
||||||
(process-buffer (run-python nil t t)))
|
(process-buffer (run-python nil t t)))
|
||||||
|
|
||||||
|
(defun +python--extract-version (prefix str)
|
||||||
|
(when str
|
||||||
|
(format "%s%s" prefix (cadr (split-string str " ")))))
|
||||||
|
|
||||||
;;;###autoload
|
;;;###autoload
|
||||||
(defun +python-version ()
|
(defun +python-version ()
|
||||||
"Return the currently installed version of python on your system or active in
|
"Return the currently installed version of python on your system or active in
|
||||||
|
@ -14,18 +21,37 @@ the current pipenv.
|
||||||
This is not necessarily aware of env management tools like virtualenv, pyenv or
|
This is not necessarily aware of env management tools like virtualenv, pyenv or
|
||||||
pipenv, unless those tools have modified the PATH that Emacs picked up when you
|
pipenv, unless those tools have modified the PATH that Emacs picked up when you
|
||||||
started it."
|
started it."
|
||||||
(let* ((pipenv-dir (pipenv-project-p))
|
(condition-case _
|
||||||
(default-directory (or pipenv-dir default-directory))
|
(if-let* ((proot (and (fboundp 'pipenv-project-p)
|
||||||
(command (if pipenv-dir
|
(pipenv-project-p))))
|
||||||
"pipenv run python --version"
|
(let* ((default-directory proot)
|
||||||
"python --version"))
|
(v (car (process-lines "pipenv" "run" "python" "--version"))))
|
||||||
(bin (car (split-string command " "))))
|
(puthash proot
|
||||||
(unless (executable-find bin)
|
(+python--extract-version "Pipenv " v)
|
||||||
(user-error "Couldn't find %s executable in PATH" bin))
|
+python-version-cache))
|
||||||
(with-temp-buffer
|
(puthash (doom-project-root)
|
||||||
(let ((p (apply #'call-process bin nil (current-buffer) nil
|
(+python--extract-version "Python " (car (process-lines "python" "--version")))
|
||||||
(cdr (split-string command " " t))))
|
+python-version-cache))
|
||||||
(output (string-trim (buffer-string))))
|
(error "Python")))
|
||||||
(unless (zerop p)
|
|
||||||
(user-error "'%s' failed: %s" command output))
|
|
||||||
(cadr (split-string output " " t))))))
|
;;
|
||||||
|
;; Hooks
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +python|update-version (&rest _)
|
||||||
|
"Update `+python--version' by consulting `+python-version' function."
|
||||||
|
(setq +python--version
|
||||||
|
(or (gethash (or (and (fboundp 'pipenv-project-p)
|
||||||
|
(pipenv-project-p))
|
||||||
|
(doom-project-root))
|
||||||
|
+python-version-cache)
|
||||||
|
(+python-version))))
|
||||||
|
|
||||||
|
;;;###autoload
|
||||||
|
(defun +python|update-version-in-all-buffers ()
|
||||||
|
"Update `+python-version' in all buffers in `python-mode'."
|
||||||
|
(dolist (buffer (doom-buffers-in-mode 'python-mode))
|
||||||
|
(setq +python-version-cache (clrhash +python-version-cache))
|
||||||
|
(with-current-buffer buffer
|
||||||
|
(+python|update-version))))
|
||||||
|
|
|
@ -1,10 +1,9 @@
|
||||||
;;; lang/python/config.el -*- lexical-binding: t; -*-
|
;;; lang/python/config.el -*- lexical-binding: t; -*-
|
||||||
|
|
||||||
(defvar +python-mode-line-indicator
|
(defconst +python-mode-line-indicator '("" +python--version)
|
||||||
'("Python" (+python-version (" " +python-version)))
|
|
||||||
"Format for the python version/env indicator in the mode-line.")
|
"Format for the python version/env indicator in the mode-line.")
|
||||||
|
|
||||||
(defvar-local +python-version nil
|
(defvar-local +python--version nil
|
||||||
"The python version in the current buffer.")
|
"The python version in the current buffer.")
|
||||||
|
|
||||||
|
|
||||||
|
@ -64,9 +63,6 @@
|
||||||
(setq mode-name +python-mode-line-indicator))
|
(setq mode-name +python-mode-line-indicator))
|
||||||
(add-hook 'python-mode-hook #'+python|adjust-mode-line)
|
(add-hook 'python-mode-hook #'+python|adjust-mode-line)
|
||||||
|
|
||||||
(defun +python|update-version (&rest _)
|
|
||||||
(setq +python-version (+python-version)))
|
|
||||||
(+python|update-version)
|
|
||||||
(add-hook 'python-mode-hook #'+python|update-version))
|
(add-hook 'python-mode-hook #'+python|update-version))
|
||||||
|
|
||||||
|
|
||||||
|
@ -135,8 +131,8 @@
|
||||||
:hook (python-mode . pipenv-mode)
|
:hook (python-mode . pipenv-mode)
|
||||||
:init (setq pipenv-with-projectile nil)
|
:init (setq pipenv-with-projectile nil)
|
||||||
:config
|
:config
|
||||||
(advice-add #'pipenv-activate :after-while #'+python|update-version)
|
(advice-add #'pipenv-activate :after-while #'+python|update-version-in-all-buffers)
|
||||||
(advice-add #'pipenv-deactivate :after-while #'+python|update-version))
|
(advice-add #'pipenv-deactivate :after-while #'+python|update-version-in-all-buffers))
|
||||||
|
|
||||||
|
|
||||||
(def-package! pyenv-mode
|
(def-package! pyenv-mode
|
||||||
|
@ -144,11 +140,10 @@
|
||||||
:after python
|
:after python
|
||||||
:config
|
:config
|
||||||
(pyenv-mode +1)
|
(pyenv-mode +1)
|
||||||
(advice-add #'pyenv-mode-set :after #'+python|update-version)
|
(when (executable-find "pyenv")
|
||||||
(advice-add #'pyenv-mode-unset :after #'+python|update-version)
|
(add-to-list 'exec-path (expand-file-name "shims" (or (getenv "PYENV_ROOT") "~/.pyenv"))))
|
||||||
(add-to-list '+python-mode-line-indicator
|
(advice-add #'pyenv-mode-set :after #'+python|update-version-in-all-buffers)
|
||||||
'(:eval (if (pyenv-mode-version) (concat " pyenv:" (pyenv-mode-version))))
|
(advice-add #'pyenv-mode-unset :after #'+python|update-version-in-all-buffers))
|
||||||
'append))
|
|
||||||
|
|
||||||
|
|
||||||
(def-package! pyvenv
|
(def-package! pyvenv
|
||||||
|
@ -156,8 +151,8 @@
|
||||||
:after python
|
:after python
|
||||||
:config
|
:config
|
||||||
(defun +python-current-pyvenv () pyvenv-virtual-env-name)
|
(defun +python-current-pyvenv () pyvenv-virtual-env-name)
|
||||||
(add-hook 'pyvenv-post-activate-hooks #'+python|update-version)
|
(add-hook 'pyvenv-post-activate-hooks #'+python|update-version-in-all-buffers)
|
||||||
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version)
|
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version-in-all-buffers)
|
||||||
(add-to-list '+python-mode-line-indicator
|
(add-to-list '+python-mode-line-indicator
|
||||||
'(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name))
|
'(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name))
|
||||||
'append))
|
'append))
|
||||||
|
@ -192,8 +187,8 @@
|
||||||
(conda-env-initialize-interactive-shells)
|
(conda-env-initialize-interactive-shells)
|
||||||
(after! eshell (conda-env-initialize-eshell))
|
(after! eshell (conda-env-initialize-eshell))
|
||||||
|
|
||||||
(add-hook 'conda-postactivate-hook #'+python|update-version)
|
(add-hook 'conda-postactivate-hook #'+python|update-version-in-all-buffers)
|
||||||
(add-hook 'conda-postdeactivate-hook #'+python|update-version)
|
(add-hook 'conda-postdeactivate-hook #'+python|update-version-in-all-buffers)
|
||||||
(add-to-list '+python-mode-line-indicator
|
(add-to-list '+python-mode-line-indicator
|
||||||
'(conda-env-current-name (" conda:" conda-env-current-name))
|
'(conda-env-current-name (" conda:" conda-env-current-name))
|
||||||
'append))
|
'append))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue