From 1c99aed0c0b74870dadee21ba8f36c1518f62e9c Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Tue, 25 Aug 2020 21:22:56 -0400 Subject: [PATCH] Add +python-{ipython,jupyter}-command vars for REPLs --- modules/lang/python/autoload/python.el | 52 +++++++++++++++----------- modules/lang/python/config.el | 10 ++--- 2 files changed, 34 insertions(+), 28 deletions(-) diff --git a/modules/lang/python/autoload/python.el b/modules/lang/python/autoload/python.el index 052db2a29..01016ba03 100644 --- a/modules/lang/python/autoload/python.el +++ b/modules/lang/python/autoload/python.el @@ -1,5 +1,25 @@ ;;; 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." @@ -25,8 +45,11 @@ "Open an IPython REPL." (interactive) (require 'python) - (let ((python-shell-interpreter (or (+python-executable-find "ipython") "ipython")) - (python-shell-interpreter-args (string-join +python-ipython-repl-args " "))) + (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 @@ -35,28 +58,13 @@ (interactive) (require 'python) (add-to-list 'python-shell-completion-native-disabled-interpreters "jupyter") - (let ((python-shell-interpreter (or (+python-executable-find "jupyter") "jupyter")) - (python-shell-interpreter-args (format "console %s" (string-join +python-jupyter-repl-args " ")))) + (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-executable-find (exe) - "TODO" - (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/optimize-imports () "organize imports" diff --git a/modules/lang/python/config.el b/modules/lang/python/config.el index eb2b4fe13..634b3c362 100644 --- a/modules/lang/python/config.el +++ b/modules/lang/python/config.el @@ -1,12 +1,10 @@ ;;; lang/python/config.el -*- lexical-binding: t; -*- -(defvar +python-ipython-repl-args '("-i" "--simple-prompt" "--no-color-info") - "CLI arguments to initialize ipython with when `+python/open-ipython-repl' is -called.") +(defvar +python-ipython-command '("ipython" "-i" "--simple-prompt" "--no-color-info") + "Command to initialize the ipython REPL for `+python/open-ipython-repl'.") -(defvar +python-jupyter-repl-args '("--simple-prompt") - "CLI arguments to initialize 'jupiter console %s' with when -`+python/open-ipython-repl' is called.") +(defvar +python-jupyter-command '("jupyter" "console" "--simple-prompt") + "Command to initialize the jupyter REPL for `+python/open-jupyter-repl'.") (after! projectile (pushnew! projectile-project-root-files "setup.py" "requirements.txt"))