lang/python: rewrite version mode-line segment

This commit is contained in:
Henrik Lissner 2018-07-31 23:07:22 +02:00
parent 43dcaa1853
commit b4c8584f64
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395

View file

@ -1,6 +1,6 @@
;;; lang/python/config.el -*- lexical-binding: t; -*- ;;; lang/python/config.el -*- lexical-binding: t; -*-
(defvar +python-mode-name-functions '(+python-version) (defvar +python-version-functions '(+python-version)
"A list of functions to retrieve a version or environment string from. The "A list of functions to retrieve a version or environment string from. The
first to return non-nil will have its result appended to the python-mode first to return non-nil will have its result appended to the python-mode
`mode-name' and displayed in the mode-line.") `mode-name' and displayed in the mode-line.")
@ -54,10 +54,15 @@ 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
(defun +python|update-version (&rest _)
(setq +python-version (run-hook-with-args-until-success '+python-version-functions)))
(defalias '+python*update-version #'+python|update-version)
(defun +python|add-version-to-modeline () (defun +python|add-version-to-modeline ()
"Add version string to the major mode in the modeline." "Add version string to the major mode in the modeline."
(setq mode-name (setq mode-name
(if-let* ((result (run-hook-with-args-until-success '+python-mode-name-functions))) (if-let* ((result (+python|update-version)))
(format "Python %s" result) (format "Python %s" result)
"Python"))) "Python")))
(add-hook 'python-mode-hook #'+python|add-version-to-modeline)) (add-hook 'python-mode-hook #'+python|add-version-to-modeline))
@ -134,7 +139,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)
(add-to-list '+python-mode-name-functions #'pyenv-mode-version nil #'eq)) (advice-add #'pyenv-mode-set :after #'+python*update-version)
(advice-add #'pyenv-mode-unset :after #'+python*update-version)
(add-to-list '+python-version-functions #'pyenv-mode-version nil #'eq))
(def-package! pyvenv (def-package! pyvenv
@ -142,25 +149,26 @@ first to return non-nil will have its result appended to the python-mode
:after python :after python
:config :config
(defun +python-current-pyvenv () pyvenv-virtual-env-name) (defun +python-current-pyvenv () pyvenv-virtual-env-name)
(add-to-list '+python-mode-name-functions #'+python-current-pyvenv nil #'eq)) (add-hook 'pyvenv-post-activate-hooks #'+python|update-version)
(add-hook 'pyvenv-post-deactivate-hooks #'+python|update-version)
(add-to-list '+python-version-functions #'+python-current-pyvenv nil #'eq))
(def-package! conda (def-package! conda
:when (featurep! +conda) :when (featurep! +conda)
:after python :after python
:config :config
;; Adds conda support to Doom Emacs. `conda-anaconda-home' should be the path ;; The location of your anaconda home will be guessed from the following:
;; to your anaconda installation, and will be guessed from the following:
;; ;;
;; + ANACONDA_HOME
;; + ~/.anaconda3 ;; + ~/.anaconda3
;; + ~/.anaconda ;; + ~/.anaconda
;; + ~/.miniconda ;; + ~/.miniconda
;; + ~/usr/bin/anaconda3 ;; + ~/usr/bin/anaconda3
;; ;;
;; If none of these work, you'll need to set `conda-anaconda-home' yourself. ;; If none of these work for you, you must set `conda-anaconda-home'
;; ;; explicitly. Once set, run M-x `conda-env-activate' to switch between
;; Once set, run M-x `conda-env-activate' to switch between environments OR ;; environments
;; turn on `conda-env-autoactivate-mode' if you want it done automatically.
(unless (cl-loop for dir in (list conda-anaconda-home (unless (cl-loop for dir in (list conda-anaconda-home
"~/.anaconda" "~/.anaconda"
"~/.miniconda" "~/.miniconda"
@ -174,6 +182,8 @@ first to return non-nil will have its result appended to the python-mode
(conda-env-initialize-interactive-shells) (conda-env-initialize-interactive-shells)
(after! eshell (conda-env-initialize-eshell)) (after! eshell (conda-env-initialize-eshell))
(add-to-list '+python-mode-name-functions #'+python-conda-env nil #'eq) (add-hook 'conda-postactivate-hook #'+python|update-version)
(add-hook 'conda-postdeactivate-hook #'+python|update-version)
(add-to-list '+python-version-functions #'+python-conda-env nil #'eq)
(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))