2018-07-17 20:57:32 +02:00
|
|
|
;;; lang/python/autoload/python.el -*- lexical-binding: t; -*-
|
2017-03-04 18:46:38 -05:00
|
|
|
|
2018-09-25 22:17:41 -04:00
|
|
|
(defvar +python-version-cache (make-hash-table :test 'equal)
|
|
|
|
"TODO")
|
|
|
|
|
2017-03-04 18:46:38 -05:00
|
|
|
;;;###autoload
|
|
|
|
(defun +python/repl ()
|
|
|
|
"Open the Python REPL."
|
2017-03-19 22:50:52 -04:00
|
|
|
(interactive)
|
2018-10-02 23:52:29 -04:00
|
|
|
(unless python-shell-interpreter
|
|
|
|
(user-error "`python-shell-interpreter' isn't set"))
|
2018-09-26 20:37:35 -04:00
|
|
|
(pop-to-buffer
|
|
|
|
(process-buffer
|
2018-10-02 23:52:29 -04:00
|
|
|
(if-let* ((pipenv (executable-find "pipenv"))
|
|
|
|
(pipenv-project (pipenv-project-p)))
|
|
|
|
(let ((default-directory pipenv-project)
|
|
|
|
(python-shell-interpreter-args
|
|
|
|
(format "run %s %s"
|
|
|
|
python-shell-interpreter
|
|
|
|
python-shell-interpreter-args))
|
|
|
|
(python-shell-interpreter pipenv))
|
|
|
|
(run-python nil t t))
|
|
|
|
(run-python nil t t)))))
|
2018-07-31 14:02:34 +02:00
|
|
|
|
2018-09-25 22:17:41 -04:00
|
|
|
(defun +python--extract-version (prefix str)
|
|
|
|
(when str
|
|
|
|
(format "%s%s" prefix (cadr (split-string str " ")))))
|
|
|
|
|
2018-07-31 14:02:34 +02:00
|
|
|
;;;###autoload
|
|
|
|
(defun +python-version ()
|
|
|
|
"Return the currently installed version of python on your system or active in
|
|
|
|
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."
|
2018-09-25 22:17:41 -04:00
|
|
|
(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))
|
2018-09-28 13:54:20 -04:00
|
|
|
(puthash (or (doom-project-root) default-directory)
|
2018-09-25 22:17:41 -04:00
|
|
|
(+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))
|
2018-09-28 13:54:20 -04:00
|
|
|
(doom-project-root)
|
|
|
|
default-directory)
|
2018-09-25 22:17:41 -04:00
|
|
|
+python-version-cache)
|
|
|
|
(+python-version))))
|
|
|
|
|
|
|
|
;;;###autoload
|
2018-09-26 09:41:43 -04:00
|
|
|
(defun +python|update-version-in-all-buffers (&rest _)
|
2018-09-25 22:17:41 -04:00
|
|
|
"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))))
|