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; -*-
|
||||
|
||||
(defvar +python-version-cache (make-hash-table :test 'equal)
|
||||
"TODO")
|
||||
|
||||
;;;###autoload
|
||||
(defun +python/repl ()
|
||||
"Open the Python REPL."
|
||||
(interactive)
|
||||
(process-buffer (run-python nil t t)))
|
||||
|
||||
(defun +python--extract-version (prefix str)
|
||||
(when str
|
||||
(format "%s%s" prefix (cadr (split-string str " ")))))
|
||||
|
||||
;;;###autoload
|
||||
(defun +python-version ()
|
||||
"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
|
||||
pipenv, unless those tools have modified the PATH that Emacs picked up when you
|
||||
started it."
|
||||
(let* ((pipenv-dir (pipenv-project-p))
|
||||
(default-directory (or pipenv-dir default-directory))
|
||||
(command (if pipenv-dir
|
||||
"pipenv run python --version"
|
||||
"python --version"))
|
||||
(bin (car (split-string command " "))))
|
||||
(unless (executable-find bin)
|
||||
(user-error "Couldn't find %s executable in PATH" bin))
|
||||
(with-temp-buffer
|
||||
(let ((p (apply #'call-process bin nil (current-buffer) nil
|
||||
(cdr (split-string command " " t))))
|
||||
(output (string-trim (buffer-string))))
|
||||
(unless (zerop p)
|
||||
(user-error "'%s' failed: %s" command output))
|
||||
(cadr (split-string output " " t))))))
|
||||
(condition-case _
|
||||
(if-let* ((proot (and (fboundp 'pipenv-project-p)
|
||||
(pipenv-project-p))))
|
||||
(let* ((default-directory proot)
|
||||
(v (car (process-lines "pipenv" "run" "python" "--version"))))
|
||||
(puthash proot
|
||||
(+python--extract-version "Pipenv " v)
|
||||
+python-version-cache))
|
||||
(puthash (doom-project-root)
|
||||
(+python--extract-version "Python " (car (process-lines "python" "--version")))
|
||||
+python-version-cache))
|
||||
(error "Python")))
|
||||
|
||||
|
||||
;;
|
||||
;; 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))))
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue