This commit is contained in:
ry
2022-01-11 18:05:13 -08:00
parent 2046befee2
commit 8e7b654716
846 changed files with 71287 additions and 4 deletions

View File

@@ -0,0 +1,17 @@
;;; lang/python/autoload/conda.el -*- lexical-binding: t; -*-
;;;###if (featurep! +conda)
;;;###autoload
(defun +python/set-conda-home ()
"Set `conda-anaconda-home' (ANACONDA_HOME).
Usually it's `~/.anaconda3' on local machine, but it can be set to a remote
directory using TRAMP syntax, e.g. `/ssh:host:/usr/bin/anaconda3'. This way, you
can use a remote conda environment, including the corresponding remote python
executable and packages."
(interactive)
(require 'conda)
(when-let (home (read-directory-name "Set conda home: " "~" nil nil conda-anaconda-home))
(setq conda-anaconda-home home)
(message "Successfully changed conda home to: %s" (abbreviate-file-name home))))

View File

@@ -0,0 +1,30 @@
;;; lang/python/autoload/pyenv.el -*- lexical-binding: t; -*-
;;;###if (featurep! +pyenv)
;;;###autoload
(defvar +pyenv--version nil)
;;;###autoload
(defun +python-pyenv-mode-set-auto-h ()
"Set pyenv-mode version from buffer-local variable."
(when (eq major-mode 'python-mode)
(when (not (local-variable-p '+pyenv--version))
(make-local-variable '+pyenv--version)
(setq +pyenv--version (+python-pyenv-read-version-from-file)))
(if +pyenv--version
(pyenv-mode-set +pyenv--version)
(pyenv-mode-unset))))
;;;###autoload
(defun +python-pyenv-read-version-from-file ()
"Read pyenv version from .python-version file."
(when-let (root-path (projectile-locate-dominating-file default-directory ".python-version"))
(let* ((file-path (expand-file-name ".python-version" root-path))
(version
(with-temp-buffer
(insert-file-contents-literally file-path)
(string-trim (buffer-string)))))
(if (member version (pyenv-mode-versions))
version ;; return.
(message "pyenv: version `%s' is not installed (set by `%s')."
version file-path)))))

View File

@@ -0,0 +1,73 @@
;;; lang/python/autoload/python.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +python-executable-find (exe)
"Resolve the path to the EXE executable.
Tries to be aware of your active conda/pipenv/virtualenv environment, before
falling back on searching your PATH."
(if (file-name-absolute-p exe)
(and (file-executable-p exe)
exe)
(let ((exe-root (format "bin/%s" exe)))
(cond ((when python-shell-virtualenv-root
(let ((bin (expand-file-name exe-root python-shell-virtualenv-root)))
(if (file-exists-p bin) bin))))
((when (require 'conda nil t)
(let ((bin (expand-file-name (concat conda-env-current-name "/" exe-root)
(conda-env-default-location))))
(if (file-executable-p bin) bin))))
((when-let (bin (projectile-locate-dominating-file default-directory "bin/python"))
(setq-local doom-modeline-python-executable (expand-file-name "bin/python" bin))))
((executable-find exe))))))
;;;###autoload
(defun +python/open-repl ()
"Open the Python REPL."
(interactive)
(require 'python)
(unless python-shell-interpreter
(user-error "`python-shell-interpreter' isn't set"))
(pop-to-buffer
(process-buffer
(if-let* ((pipenv (+python-executable-find "pipenv"))
(pipenv-project (pipenv-project-p)))
(let ((default-directory pipenv-project)
(python-shell-interpreter-args
(format "run %s %s"
python-shell-interpreter
python-shell-interpreter-args))
(python-shell-interpreter pipenv))
(run-python nil nil t))
(run-python nil nil t)))))
;;;###autoload
(defun +python/open-ipython-repl ()
"Open an IPython REPL."
(interactive)
(require 'python)
(let ((python-shell-interpreter
(or (+python-executable-find (car +python-ipython-command))
"ipython"))
(python-shell-interpreter-args
(string-join (cdr +python-ipython-command) " ")))
(+python/open-repl)))
;;;###autoload
(defun +python/open-jupyter-repl ()
"Open a Jupyter console."
(interactive)
(require 'python)
(add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter")
(let ((python-shell-interpreter
(or (+python-executable-find (car +python-jupyter-command))
"jupyter"))
(python-shell-interpreter-args
(string-join (cdr +python-jupyter-command) " ")))
(+python/open-repl)))
;;;###autoload
(defun +python/optimize-imports ()
"organize imports"
(interactive)
(pyimport-remove-unused)
(py-isort-buffer))