Let python/ruby modules handle version strings

Also, set RBENV_VERSION and PYENV_VERSION envvars.
This commit is contained in:
Henrik Lissner 2017-09-26 01:02:42 +02:00
parent 47b6465bc3
commit 738d85e84f
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 76 additions and 23 deletions

View file

@ -1,5 +1,20 @@
;;; lang/python/config.el -*- lexical-binding: t; -*- ;;; lang/python/config.el -*- lexical-binding: t; -*-
(defvar +python-pyenv-root nil
"The path to pyenv's root directory. This is automatically set when `python'
is loaded.")
(defvar +python-pyenv-versions nil
"Available versions of python in pyenv.")
(defvar-local +python-current-version nil
"The currently active pyenv version.")
;;
;; Plugins
;;
(def-package! python (def-package! python
:commands python-mode :commands python-mode
:init :init
@ -24,6 +39,33 @@
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"))
;; Version management with pyenv
(defun +python|add-version-to-modeline ()
"Add version string to the major mode in the modeline."
(setq mode-name
(if +python-current-version
(format "Python %s" +python-current-version)
"Python")))
(add-hook 'python-mode-hook #'+python|add-version-to-modeline)
(if (not (executable-find "pyenv"))
(setq +python-current-version (string-trim (shell-command-to-string "python --version 2>&1 | cut -d' ' -f2")))
(setq +python-pyenv-root (string-trim (shell-command-to-string "pyenv root"))
+python-pyenv-versions (split-string (shell-command-to-string "pyenv versions --bare") "\n" t))
(defun +python|detect-pyenv-version ()
"Detect the pyenv version for the current project and set the relevant
environment variables."
(when-let (version-str (shell-command-to-string "python --version 2>&1 | cut -d' ' -f2"))
(setq version-str (string-trim version-str)
+python-current-version version-str)
(let ((pyenv-current-path (concat +python-pyenv-root "/versions/" version-str)))
(when (file-directory-p pyenv-current-path)
(setq pythonic-environment pyenv-current-path)))
(when (member version-str +python-pyenv-versions)
(setenv "PYENV_VERSION" version-str))))
(add-hook 'python-mode-hook #'+python|detect-pyenv-version))
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens (define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
(sp-with-modes 'python-mode (sp-with-modes 'python-mode
(sp-local-pair "'" nil :unless '(sp-point-before-word-p sp-point-after-word-p sp-point-before-same-p)))) (sp-local-pair "'" nil :unless '(sp-point-before-word-p sp-point-after-word-p sp-point-before-same-p))))

View file

@ -1,5 +1,16 @@
;;; lang/ruby/config.el -*- lexical-binding: t; -*- ;;; lang/ruby/config.el -*- lexical-binding: t; -*-
(defvar +ruby-rbenv-versions nil
"Available versions of ruby in rbenv.")
(defvar-local +ruby-current-version nil
"The currently active ruby version.")
;;
;; Plugins
;;
(def-package! ruby-mode (def-package! ruby-mode
:mode ("\\.rb$" "\\.rake$" "\\.gemspec$" "\\.?pryrc$" :mode ("\\.rb$" "\\.rake$" "\\.gemspec$" "\\.?pryrc$"
"/\\(Gem\\|Cap\\|Vagrant\\|Rake\\|Pod\\|Puppet\\|Berks\\)file$") "/\\(Gem\\|Cap\\|Vagrant\\|Rake\\|Pod\\|Puppet\\|Berks\\)file$")
@ -13,6 +24,29 @@
;; Don't interfere with my custom RET behavior ;; Don't interfere with my custom RET behavior
(define-key ruby-mode-map [?\n] nil) (define-key ruby-mode-map [?\n] nil)
;; Version management with rbenv
(defun +ruby|add-version-to-modeline ()
"Add version string to the major mode in the modeline."
(setq mode-name
(if +python-current-version
(format "Ruby %s" +ruby-current-version)
"Ruby")))
(add-hook 'ruby-mode-hook #'+ruby|add-version-to-modeline)
(if (not (executable-find "rbenv"))
(setq +ruby-current-version (string-trim (shell-command-to-string "ruby --version 2>&1 | cut -d' ' -f2")))
(setq +ruby-rbenv-versions (split-string (shell-command-to-string "rbenv versions --bare") "\n" t))
(defun +ruby|detect-rbenv-version ()
"Detect the rbenv version for the current project and set the relevant
environment variables."
(when-let (version-str (shell-command-to-string "ruby --version 2>&1 | cut -d' ' -f2"))
(setq version-str (string-trim version-str)
+ruby-current-version version-str)
(when (member version-str +ruby-rbenv-versions)
(setenv "RBENV_VERSION" version-str))))
(add-hook 'ruby-mode-hook #'+ruby|detect-rbenv-version))
(map! :map ruby-mode-map (map! :map ruby-mode-map
:localleader :localleader
:prefix "r" :prefix "r"

View file

@ -169,27 +169,6 @@ active."
:group '+doom-modeline) :group '+doom-modeline)
;;
;; Bootstrap
;;
;; Show version string for multi-version managers like rvm, rbenv, pyenv, etc.
(defvar-local +doom-modeline-env-version nil)
(defvar-local +doom-modeline-env-command nil)
(add-hook! '(focus-in-hook find-file-hook) #'+doom-modeline|update-env)
(defun +doom-modeline|update-env ()
(when +doom-modeline-env-command
(let* ((default-directory (doom-project-root))
(s (shell-command-to-string +doom-modeline-env-command)))
(setq +doom-modeline-env-version (if (string-match "[ \t\n\r]+\\'" s)
(replace-match "" t t s)
s)))))
;; Only support python and ruby for now
(add-hook! 'python-mode-hook (setq +doom-modeline-env-command "python --version 2>&1 | cut -d' ' -f2"))
(add-hook! 'ruby-mode-hook (setq +doom-modeline-env-command "ruby --version 2>&1 | cut -d' ' -f2"))
;; ;;
;; Modeline helpers ;; Modeline helpers
;; ;;
@ -388,8 +367,6 @@ directory, the file name, and its state (modified, read-only or non-existent)."
(concat (format-mode-line mode-name) (concat (format-mode-line mode-name)
(when (stringp mode-line-process) (when (stringp mode-line-process)
mode-line-process) mode-line-process)
(when +doom-modeline-env-version
(concat " " +doom-modeline-env-version))
(and (featurep 'face-remap) (and (featurep 'face-remap)
(/= text-scale-mode-amount 0) (/= text-scale-mode-amount 0)
(format " (%+d)" text-scale-mode-amount))) (format " (%+d)" text-scale-mode-amount)))