From 461b8c423b53dfd1b40e94377724e9b7b0911811 Mon Sep 17 00:00:00 2001 From: Valentin Boettcher Date: Wed, 22 Jan 2025 12:57:10 -0500 Subject: [PATCH] only send top level functions ... and don't get confused by functions within functions --- py-vterm-interaction.el | 64 ++++++++++++++++++++--------------------- 1 file changed, 31 insertions(+), 33 deletions(-) diff --git a/py-vterm-interaction.el b/py-vterm-interaction.el index 7542576..fb62e32 100644 --- a/py-vterm-interaction.el +++ b/py-vterm-interaction.el @@ -584,14 +584,6 @@ point, the cell is assumed to end with the buffer." If the function has no arguments, the function call is run immediately." (interactive) (save-mark-and-excursion - ;; (let ((last-mark (mark))) - ;; (python-mark-defun) - ;; (while (not (eq (mark) last-mark)) - ;; (setf last-mark (mark)) - ;; (python-mark-defun 1))) - ;; (mark (region-beginning)) - ;; (next-line) - ;; (cl-return) (let ((begin (save-mark-and-excursion (python-nav-beginning-of-defun 1) (point))) @@ -599,34 +591,40 @@ If the function has no arguments, the function call is run immediately." (python-nav-end-of-defun) (point)))) (python-nav-beginning-of-defun 1) - (next-line) - (let ((function-name-regex "def[ \t]+\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*(\\(.*\\))\\([ \t]*->[ \t]*.*?\\)?:")) - (save-match-data - (let ((name-found (re-search-backward function-name-regex nil nil))) - (if name-found - (progn - (let ((name (match-string 1)) - (args (match-string 2)) - (py-vterm-interaction-paste-with-return nil)) + (beginning-of-line) + (if (looking-at "[[:blank:]]+.*") + (progn + (previous-line) + (py-vterm-interaction-run-current-function)) + (progn + (next-line) + (let ((function-name-regex "def[ \t]+\\([a-zA-Z_][a-zA-Z0-9_]*\\)[ \t]*(\\(.*\\))\\([ \t]*->[ \t]*.*?\\)?:")) + (save-match-data + (let ((name-found (re-search-backward function-name-regex nil nil))) + (if name-found + (progn + (let ((name (match-string 1)) + (args (match-string 2)) + (py-vterm-interaction-paste-with-return nil)) - ;; capture decorators too - (save-excursion - (while - (progn (forward-line -1) - (beginning-of-line) - (if (looking-at "@") - (setq begin (point)) - nil)))) + ;; capture decorators too + (save-excursion + (while + (progn (forward-line -1) + (beginning-of-line) + (if (looking-at "@") + (setq begin (point)) + nil)))) - (let ((func (buffer-substring-no-properties begin end))) - (py-vterm-interaction--send-maybe-silent func name) - (py-vterm-interaction-send-return-key)) - (if (= 0 (length args)) - (progn - (py-vterm-interaction-paste-string (format "%s() " name)) + (let ((func (buffer-substring-no-properties begin end))) + (py-vterm-interaction--send-maybe-silent func name) (py-vterm-interaction-send-return-key)) - (py-vterm-interaction-paste-string (format "%s(" name))))) - (message "No function found")))))))) + (if (= 0 (length args)) + (progn + (py-vterm-interaction-paste-string (format "%s() " name)) + (py-vterm-interaction-send-return-key)) + (py-vterm-interaction-paste-string (format "%s(" name))))) + (message "No function found")))))))))) (defun py-vterm-interaction-send-buffer () "Send the whole content of the script buffer to the Python REPL line by line."