From b59e1d9e464c55e61f9eb5a4ae29e2bef90424ae Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Fri, 17 May 2024 16:18:10 -0400 Subject: [PATCH] feature: importing the buffer --- README.org | 3 +++ python-vterm.el | 13 +++++++++++-- scripts/star_import_script.py | 21 +++++++++++++++++++++ 3 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 scripts/star_import_script.py diff --git a/README.org b/README.org index 05b34c1..9df9807 100644 --- a/README.org +++ b/README.org @@ -129,6 +129,9 @@ C-c C-f python-vterm-run-current-function C-c C-r python-vterm-send-run-buffer-file Send a line to evaluate the buffer's file using ipython %run magic. +C-c C-i Import the current buffer file like `from import *' in the python repl. + This is especially useful with `%autoload 3' in ipython. + C-c C-d python-vterm-send-cd-to-buffer-directory Send %cd function call to the Python REPL to change the current working directory of REPL to the buffer's directory. diff --git a/python-vterm.el b/python-vterm.el index 7c2c138..9e87929 100644 --- a/python-vterm.el +++ b/python-vterm.el @@ -141,7 +141,7 @@ python interpreter is ipython. This times out after (setq default-directory (plist-get context :cwd)) (setq python-vterm-repl-script-buffer (plist-get context :script-buffer))) (python-vterm-repl-mode) - (run-with-timer .5 nil + (run-with-timer 1 nil (lambda (buffer) (with-current-buffer buffer (with-timeout (python-vterm-repl-launch-timeout @@ -149,7 +149,7 @@ python interpreter is ipython. This times out after (kill-buffer buffer))) (while (not (python-vterm-repl-prompt-status)) (message "waiting for prompt...") - (sit-for 0.2) + (sit-for 0.5) t) (setq python-vterm-repl-interpreter (if (eq (python-vterm--execute-script "is_ipython") :false) @@ -572,6 +572,14 @@ If the function has no arguments, the function call is run immediately." (with-current-buffer (python-vterm-fellow-repl-buffer) (python-vterm-paste-string (python-vterm--load-file file "load script buffer"))))) +(defun python-vterm-send-import-buffer-file () + "Import the current buffer file like `from import *' in the python repl." + (interactive) + (let ((file buffer-file-name) + (python-vterm-paste-with-return t)) + (with-current-buffer (python-vterm-fellow-repl-buffer) + (python-vterm--execute-script "star_import_script" file)))) + (defun python-vterm-send-cd-to-buffer-directory () "Change the REPL's working directory to the directory of the buffer file." (interactive) @@ -601,6 +609,7 @@ If the function has no arguments, the function call is run immediately." (,(kbd "C-c C-c") . python-vterm-send-region-or-current-line) (,(kbd "C-c C-j") . python-vterm-send-current-cell) (,(kbd "C-c C-f") . python-vterm-run-current-function) + (,(kbd "C-c C-i") . python-vterm-send-import-buffer-file) (,(kbd "C-c C-b") . python-vterm-send-buffer) (,(kbd "C-c C-r") . python-vterm-send-run-buffer-file) (,(kbd "C-c C-d") . python-vterm-send-cd-to-buffer-directory))) diff --git a/scripts/star_import_script.py b/scripts/star_import_script.py new file mode 100644 index 0000000..0ae2aae --- /dev/null +++ b/scripts/star_import_script.py @@ -0,0 +1,21 @@ +"""Import all public names from a module into the current namespace.""" + + +def star_import_script(tmpfile, path): + import importlib, pathlib, sys + + name = pathlib.Path(path).stem + spec = importlib.util.spec_from_file_location(name, path) + mdl = importlib.util.module_from_spec(spec) + sys.modules[name] = mdl + spec.loader.exec_module(mdl) + + if "__all__" in mdl.__dict__: + names = mdl.__dict__["__all__"] + else: + names = [x for x in mdl.__dict__ if not x.startswith("_")] + + # now drag them in + globals().update({k: getattr(mdl, k) for k in names}) + + dump_json(tmpfile, True)