lang/python: rewrite mode-line indicator (again)

This commit is contained in:
Henrik Lissner 2018-08-01 01:13:49 +02:00
parent 9cf5907c48
commit 90d0f334f8
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,9 +1,11 @@
;;; lang/python/config.el -*- lexical-binding: t; -*- ;;; lang/python/config.el -*- lexical-binding: t; -*-
(defvar +python-version-functions '(+python-version) (defvar +python-mode-line-indicator
"A list of functions to retrieve a version or environment string from. The '("Python" (+python-version (" " +python-version)))
first to return non-nil will have its result appended to the python-mode "Format for the python version/env indicator in the mode-line.")
`mode-name' and displayed in the mode-line.")
(defvar-local +python-version nil
"The python version in the current buffer.")
;; ;;
@ -54,21 +56,22 @@ first to return non-nil will have its result appended to the python-mode
python-shell-completion-string-code python-shell-completion-string-code
"';'.join(get_ipython().Completer.all_completions('''%s'''))\n")) "';'.join(get_ipython().Completer.all_completions('''%s'''))\n"))
;; Python version in modeline ;; TODO Remove condition once :ui modeline replaces :ui doom-modeline
(defun +python|update-version (&rest _) (when (featurep! :ui modeline)
(setq +python-version (run-hook-with-args-until-success '+python-version-functions)) (def-modeline-segment! +python-major-mode
(dolist (buffer (doom-buffers-in-mode 'python-mode (buffer-list))) (propertize (format-mode-line +python-mode-line-indicator)
(with-current-buffer buffer 'face (if (active) 'doom-modeline-buffer-major-mode)))
(+python|add-version-to-modeline +python-version))))
(defalias '+python*update-version #'+python|update-version)
(defun +python|add-version-to-modeline (&optional version) (defun +python|add-version-to-modeline ()
"Add version string to the major mode in the modeline." "Add python/pipenv version string to the major mode in the modeline."
(setq mode-name (when-let* ((target (memq '+modeline-major-mode mode-line-format-right)))
(if-let* ((result (or version (+python|update-version)))) (setcar target '+python-major-mode)))
(format "Python %s" result) (add-hook 'python-mode-hook #'+python|add-version-to-modeline)
"Python")))
(add-hook 'python-mode-hook #'+python|add-version-to-modeline)) (defun +python|update-version (&rest _)
(setq +python-version (+python-version)))
(+python|update-version)
(add-hook 'python-mode-hook #'+python|update-version)))
(def-package! anaconda-mode (def-package! anaconda-mode
@ -142,8 +145,9 @@ first to return non-nil will have its result appended to the python-mode
:after python :after python
:config :config
(pyenv-mode +1) (pyenv-mode +1)
(advice-add #'pyenv-mode-set :after #'+python*update-version) (advice-add #'pyenv-mode-set :after #'+python|update-version)
(advice-add #'pyenv-mode-unset :after #'+python*update-version) (advice-add #'pyenv-mode-unset :after #'+python|update-version)
(add-to-list '+python-mode-line-indicator '(:eval (if (pyenv-mode-version) (concat " pyenv:%s" (pyenv-mode-version)))) 'append)
(add-to-list '+python-version-functions #'pyenv-mode-version nil #'eq)) (add-to-list '+python-version-functions #'pyenv-mode-version nil #'eq))
@ -154,6 +158,7 @@ first to return non-nil will have its result appended to the python-mode
(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)
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version) (add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version)
(add-to-list '+python-mode-line-indicator '(pyvenv-virtual-env-name (" venv:" pyvenv-virtual-env-name)) 'append)
(add-to-list '+python-version-functions #'+python-current-pyvenv nil #'eq)) (add-to-list '+python-version-functions #'+python-current-pyvenv nil #'eq))
@ -188,5 +193,6 @@ first to return non-nil will have its result appended to the python-mode
(add-hook 'conda-postactivate-hook #'+python|update-version) (add-hook 'conda-postactivate-hook #'+python|update-version)
(add-hook 'conda-postdeactivate-hook #'+python|update-version) (add-hook 'conda-postdeactivate-hook #'+python|update-version)
(add-to-list '+python-version-functions #'+python-conda-env nil #'eq) (add-to-list '+python-version-functions #'+python-conda-env nil #'eq)
(add-to-list '+python-mode-line-indicator '(conda-env-current-name (" conda:" conda-env-current-name)) 'append)
(advice-add 'anaconda-mode-bootstrap :override #'+python*anaconda-mode-bootstrap-in-remote-environments)) (advice-add 'anaconda-mode-bootstrap :override #'+python*anaconda-mode-bootstrap-in-remote-environments))