lang/python: refactor python mode-line indication

This commit is contained in:
Henrik Lissner 2019-03-07 00:11:45 -05:00
parent 9b180fda97
commit c0c4b897ea
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 24 additions and 30 deletions

View file

@ -1,8 +1,5 @@
;;; lang/python/config.el -*- lexical-binding: t; -*- ;;; lang/python/config.el -*- lexical-binding: t; -*-
(defconst +python-mode-line-indicator '("" +python--version)
"Format for the python version/env indicator in the mode-line.")
(defvar +python-ipython-repl-args '("-i" "--simple-prompt" "--no-color-info") (defvar +python-ipython-repl-args '("-i" "--simple-prompt" "--no-color-info")
"CLI arguments to initialize ipython with when `+python/open-ipython-repl' is "CLI arguments to initialize ipython with when `+python/open-ipython-repl' is
called.") called.")
@ -52,15 +49,9 @@ called.")
sp-point-after-word-p sp-point-after-word-p
sp-point-before-same-p)) sp-point-before-same-p))
(defun +python|detect-local-python-executable () ;; Affects pyenv and conda
(let ((path (+python-executable-find "python"))) (advice-add #'pythonic-activate :after-while #'+modeline|update-env-in-all-windows)
(when path (advice-add #'pythonic-deactivate :after #'+modeline|clear-env-in-all-windows)
(make-local-variable 'exec-path)
(add-to-list 'exec-path path))
(when (bound-and-true-p doom-modeline-mode)
(setq-local doom-modeline-python-executable
(or path python-shell-interpreter)))))
(add-hook 'python-mode-hook #'+python|detect-local-python-executable)
(setq-hook! 'python-mode-hook tab-width python-indent-offset)) (setq-hook! 'python-mode-hook tab-width python-indent-offset))
@ -151,20 +142,19 @@ called.")
(_ (pipenv-project-p))) (_ (pipenv-project-p)))
(format "PIPENV_MAX_DEPTH=9999 %s run %%c %%o %%s %%a" bin) (format "PIPENV_MAX_DEPTH=9999 %s run %%c %%o %%s %%a" bin)
"%c %o %s %a"))) "%c %o %s %a")))
(:description . "Run Python script"))) (:description . "Run Python script"))))
(when (featurep! :ui modeline)
(advice-add #'pipenv-activate :after-while #'+modeline|update-env-in-all-windows)
(advice-add #'pipenv-deactivate :after-while #'+modeline|update-env-in-all-windows)))
(def-package! pyvenv (def-package! pyvenv
:after python :after python
:config :init
(when (featurep! :ui modeline) (when (featurep! :ui modeline)
(add-hook 'pyvenv-post-activate-hooks #'+modeline|update-env-in-all-windows) (add-hook 'pyvenv-post-activate-hooks #'+modeline|update-env-in-all-windows)
(add-hook 'pyvenv-post-deactivate-hooks #'+modeline|update-env-in-all-windows)) (add-hook 'pyvenv-pre-deactivate-hooks #'+modeline|clear-env-in-all-windows))
:config
(add-hook 'hack-local-variables-hook #'pyvenv-track-virtualenv)
(add-to-list 'global-mode-string (add-to-list 'global-mode-string
'(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name)) '(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name " "))
'append)) 'append))
@ -174,10 +164,7 @@ 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")))))
(when (featurep! :ui modeline)
(advice-add #'pyenv-mode-set :after #'+modeline|update-env-in-all-windows)
(advice-add #'pyenv-mode-unset :after #'+modeline|update-env-in-all-windows)))
(def-package! conda (def-package! conda
@ -213,10 +200,7 @@ called.")
;; integration with term/eshell ;; integration with term/eshell
(conda-env-initialize-interactive-shells) (conda-env-initialize-interactive-shells)
(after! eshell (conda-env-initialize-eshell)) (after! eshell (conda-env-initialize-eshell))
(when (featurep! :ui modeline)
(add-hook 'conda-postactivate-hook #'+modeline|update-env-in-all-windows)
(add-hook 'conda-postdeactivate-hook #'+modeline|update-env-in-all-windows))
(add-to-list 'global-mode-string (add-to-list 'global-mode-string
'(conda-env-current-name (" conda:" conda-env-current-name)) '(conda-env-current-name (" conda:" conda-env-current-name " "))
'append)) 'append))

View file

@ -29,7 +29,17 @@ made to be added to `doom-big-font-mode-hook'."
;;;###autoload ;;;###autoload
(defun +modeline|update-env-in-all-windows (&rest _) (defun +modeline|update-env-in-all-windows (&rest _)
"TODO" "Update version strings in all buffers."
(dolist (window (window-list)) (dolist (window (window-list))
(with-selected-window window (with-selected-window window
(doom-modeline-update-env)))) (doom-modeline-update-env)
(force-mode-line-update))))
;;;###autoload
(defun +modeline|clear-env-in-all-windows (&rest _)
"Blank out version strings in all buffers."
(dolist (buffer (buffer-list))
(with-current-buffer buffer
(setq doom-modeline-env--version
(bound-and-true-p doom-modeline-load-string))))
(force-mode-line-update t))