lang/python: add support for more env managers

+ Rewritten +conda support
+ Adds +pyenv and +pyvenv flags with support.
+ New +ipython flag to enable ipython REPL support
+ Added pipenv support. This is the new default, instead of pyenv, and
  isn't hidden behind a module flag because it is officially endorsed by
  python.

Addresses #736
This commit is contained in:
Henrik Lissner 2018-07-31 14:02:34 +02:00
parent 85af18a04d
commit 560d16d651
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
5 changed files with 112 additions and 88 deletions

View file

@ -1,14 +1,9 @@
;;; 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.")
(defvar +python-mode-name-functions '(+python-version)
"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
`mode-name' and displayed in the mode-line.")
;;
@ -22,7 +17,7 @@ is loaded.")
python-indent-guess-indent-offset-verbose nil
python-shell-interpreter "python")
:config
(set-env! "PYTHONPATH" "PYENV_ROOT")
(set-env! "PYTHONPATH" "PYENV")
(set-electric! 'python-mode :chars '(?:))
(set-repl-handler! 'python-mode #'+python/repl)
@ -44,7 +39,11 @@ is loaded.")
:for "for"
:return "return" :yield "yield")
(when (executable-find "ipython")
(define-key python-mode-map (kbd "DEL") nil) ; interferes with smartparens
(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)))
(when (featurep! +ipython)
(setq python-shell-interpreter "ipython"
python-shell-interpreter-args "-i --simple-prompt --no-color-info"
python-shell-prompt-regexp "In \\[[0-9]+\\]: "
@ -55,45 +54,21 @@ is loaded.")
python-shell-completion-string-code
"';'.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)
(if-let* ((result (run-hook-with-args-until-success '+python-mode-name-functions)))
(format "Python %s" result)
"Python")))
(add-hook 'python-mode-hook #'+python|add-version-to-modeline)
(if (not (executable-find "pyenv"))
(setq-default +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 "PYENV_VERSION= 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
(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))))
(add-hook 'python-mode-hook #'+python|add-version-to-modeline))
(def-package! anaconda-mode
:after python
:hook python-mode
:init
(setq anaconda-mode-installation-directory (concat doom-etc-dir "anaconda/")
anaconda-mode-eldoc-as-single-line t)
:config
(add-hook 'python-mode-hook #'anaconda-mode)
(add-hook 'anaconda-mode-hook #'anaconda-eldoc-mode)
(set-company-backend! 'python-mode '(company-anaconda))
(set-popup-rule! "^\\*anaconda-mode" :select nil)
@ -111,6 +86,8 @@ environment variables."
(add-hook! 'python-mode-hook
(add-hook 'kill-buffer-hook #'+python|auto-kill-anaconda-processes nil t))
(when (featurep 'evil)
(add-hook 'anaconda-mode-hook #'evil-normalize-keymaps))
(map! :map anaconda-mode-map
:localleader
:prefix "f"
@ -123,13 +100,14 @@ environment variables."
(def-package! nose
:commands nose-mode
:preface
(defvar nose-mode-map (make-sparse-keymap))
:init
(associate! nose-mode :match "/test_.+\\.py$" :modes (python-mode))
:preface (defvar nose-mode-map (make-sparse-keymap))
:init (associate! nose-mode :match "/test_.+\\.py$" :modes (python-mode))
:config
(set-popup-rule! "^\\*nosetests" :size 0.4 :select nil)
(set-yas-minor-mode! 'nose-mode)
(when (featurep 'evil)
(add-hook 'nose-mode-hook #'evil-normalize-keymaps))
(map! :map nose-mode-map
:localleader
:prefix "t"
@ -143,9 +121,59 @@ environment variables."
;;
;; Evil integration
;; Environment management
;;
(when (featurep! :feature evil +everywhere)
(add-hook! '(anaconda-mode-hook nose-mode-hook)
#'evil-normalize-keymaps))
(def-package! pipenv
:commands pipenv-project-p
:hook (python-mode . pipenv-mode))
(def-package! pyenv-mode
:when (featurep! +pyenv)
:after python
:config
(pyenv-mode +1)
(add-to-list '+python-mode-name-functions #'pyenv-mode-version nil #'eq))
(def-package! pyvenv
:when (featurep! +pyvenv)
:after python
:config
(defun +python-current-pyvenv () pyvenv-virtual-env-name)
(add-to-list '+python-mode-name-functions #'+python-current-pyvenv nil #'eq))
(def-package! conda
:when (featurep! +conda)
:after python
:config
;; Adds conda support to Doom Emacs. `conda-anaconda-home' should be the path
;; to your anaconda installation, and will be guessed from the following:
;;
;; + ~/.anaconda3
;; + ~/.anaconda
;; + ~/.miniconda
;; + ~/usr/bin/anaconda3
;;
;; If none of these work, you'll need to set `conda-anaconda-home' yourself.
;;
;; Once set, run M-x `conda-env-activate' to switch between environments OR
;; turn on `conda-env-autoactivate-mode' if you want it done automatically.
(unless (cl-loop for dir in (list conda-anaconda-home
"~/.anaconda"
"~/.miniconda"
"/usr/bin/anaconda3")
if (file-directory-p dir)
return (setq conda-anaconda-home dir
conda-env-home-directory dir))
(message "Cannot find Anaconda installation"))
;; integration with term/eshell
(conda-env-initialize-interactive-shells)
(after! eshell (conda-env-initialize-eshell))
(add-to-list '+python-mode-name-functions #'+python-conda-env nil #'eq)
(advice-add 'anaconda-mode-bootstrap :override #'+python*anaconda-mode-bootstrap-in-remote-environments))