lang/python: refactor conda integration

Removes the +python-conda-home variable and guesses conda-anaconda-home
at startup.
This commit is contained in:
Henrik Lissner 2018-07-17 22:06:16 +02:00
parent bb1171b64b
commit e8d4b74c29
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
2 changed files with 42 additions and 27 deletions

View file

@ -1,21 +1,33 @@
;;; lang/python/+conda.el -*- lexical-binding: t; -*- ;;; lang/python/+conda.el -*- lexical-binding: t; -*-
;;;###if (featurep! +conda) ;;;###if (featurep! +conda)
;; This file add conda support to doom-emacs. To get started, try `M-x' ;; Adds conda support to Doom Emacs. `conda-anaconda-home' should be the path to
;; `+python/set-conda-home' and then `M-x' `conda-env-activate'. ;; your anaconda installation, and will be guessed from the following:
;;
;; + ~/.anaconda3
;; + ~/.anaconda
;; + ~/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.
(def-package! conda (def-package! conda
:when (featurep! +conda) :when (featurep! +conda)
:after (python) :after python
:init
(defvar +python-conda-home '("~/.anaconda3" "/usr/bin/anaconda3" "~/.anaconda")
"A list of host pattern and corresponding anaconda home.")
:config :config
(advice-add 'anaconda-mode-bootstrap :override #'+python*anaconda-mode-bootstrap) (unless (cl-loop for dir in (list conda-anaconda-home "/usr/bin/anaconda3" "~/.anaconda")
(conda-env-autoactivate-mode -1) if (file-directory-p dir)
;; (add-hook 'python-mode-hook #'conda-env-activate-for-buffer) 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) (conda-env-initialize-interactive-shells)
(conda-env-initialize-eshell) (after! eshell (conda-env-initialize-eshell))
;; Version management with conda
(add-hook 'conda-postactivate-hook #'+python|add-conda-env-to-modeline) (add-hook! '(conda-postactivate-hook conda-postdeactivate-hook)
(add-hook 'conda-postdeactivate-hook #'+python|add-conda-env-to-modeline)) #'+python|add-conda-env-to-modeline)
(advice-add 'anaconda-mode-bootstrap :override #'+python*anaconda-mode-bootstrap-in-remote-environments))

View file

@ -3,25 +3,28 @@
;;;###autoload ;;;###autoload
(defun +python/set-conda-home () (defun +python/set-conda-home ()
"Set the CONDA HOME. "Set `conda-anaconda-home' (ANACONDA_HOME).
Usually it's `~/.anaconda3' on local machine, but you can also set it to a
remote directory using TRAMP syntax such as `/ssh:host:/usr/bin/anaconda3'. In Usually it's `~/.anaconda3' on local machine, but it can be set to a remote
that way you can use the remote conda environment as well as the corresponding directory using TRAMP syntax, e.g. `/ssh:host:/usr/bin/anaconda3'. This way, you
remote python executable and packages." can use a remote conda environment, including the corresponding remote python
executable and packages."
(interactive) (interactive)
(ivy-read "Set conda home:" +python-conda-home (require 'conda)
:history +python/set-conda-home--history (when-let* ((home (read-directory-name "Set conda home: " "~" nil nil conda-anaconda-home)))
:action (lambda (cand) (setq conda-anaconda-home cand)))) (setq conda-anaconda-home home)
(message "Successfully changed conda home to: %s" (abbreviate-file-name home))))
;;;###autoload ;;;###autoload
(defun +python|add-conda-env-to-modeline () (defun +python|add-conda-env-to-modeline ()
"Add conda environment string to the major mode in the modeline." "Add conda environment string to the major mode modeline segment."
(setq mode-name (setq mode-name
(if conda-env-current-name (if conda-env-current-name
(format "Py:conda:%s" conda-env-current-name) (format "Py:conda:%s" conda-env-current-name)
"Python"))) "Python")))
;;;###autoload ;;;###autoload
(defun +python*anaconda-mode-bootstrap (&optional callback) (defun +python*anaconda-mode-bootstrap-in-remote-environments (&optional callback)
"Advice to set up the anaconda-mode even in remote environment. "Advice to set up the anaconda-mode even in remote environment.
Original doc: Original doc:
Run `anaconda-mode' server. Run `anaconda-mode' server.