From c071d5a531a6f17e229b0f4a9239f0f054321bc9 Mon Sep 17 00:00:00 2001 From: Jack Kamm Date: Sat, 30 Sep 2023 21:04:12 -0700 Subject: [PATCH] python.el: Function to return output without necessarily inhibiting * lisp/progmodes/python.el (python-shell-output-filter-inhibit-output): Variable to determine whether output currently inhibited. (python-shell-send-string): Check `python-shell-output-filter-inhibit-output' instead of `python-shell-output-filter-in-progress' to determine whether output is inhibited. (python-shell-output-filter): Set `python-shell-output-filter-inhibit-output' to nil after reaching end of output. (python-shell-send-string-no-output): Turned into a thin wrapper around `python-shell-send-string-maybe-output'. (python-shell-send-string-maybe-output): Adapted from `python-shell-send-string-no-output'. Add function argument on whether output should be inhibited, and set `python-shell-output-filter-inhibit-output' accordingly. If not inhibited, then the original filter function is run in addition to `python-shell-output-filter', and `comint-last-prompt' is not reset after evaluation (since the original output filter will set it approriately). --- lisp/progmodes/python.el | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/lisp/progmodes/python.el b/lisp/progmodes/python.el index d3cb5a77e22..30d0416d035 100644 --- a/lisp/progmodes/python.el +++ b/lisp/progmodes/python.el @@ -2736,6 +2736,7 @@ python-shell-dedicated (defvar python-shell-output-filter-in-progress nil) (defvar python-shell-output-filter-buffer nil) +(defvar python-shell-output-filter-inhibit-output nil) (defmacro python-shell--add-to-path-with-priority (pathvar paths) "Modify PATHVAR and ensure PATHS are added only once at beginning." @@ -3750,7 +3751,7 @@ python-shell-send-string (python-shell--encode-string string) (python-shell--encode-string (or (buffer-file-name) ""))))) - (unless python-shell-output-filter-in-progress + (unless python-shell-output-filter-inhibit-output (with-current-buffer (process-buffer process) (save-excursion (goto-char (process-mark process)) @@ -3780,6 +3781,7 @@ python-shell-output-filter ;; Output ends when `python-shell-output-filter-buffer' contains ;; the prompt attached at the end of it. (setq python-shell-output-filter-in-progress nil + python-shell-output-filter-inhibit-output nil python-shell-output-filter-buffer (substring python-shell-output-filter-buffer 0 (match-beginning 0))) @@ -3795,12 +3797,21 @@ python-shell-output-filter (defun python-shell-send-string-no-output (string &optional process) "Send STRING to PROCESS and inhibit output. Return the output." + (python-shell-send-string-maybe-output string process)) + +(defun python-shell-send-string-maybe-output (string &optional process show-output) + "Send STRING to PROCESS and return the output. +Inhibit printing the output unless SHOW-OUTPUT is non-nil." (or process (setq process (python-shell-get-process-or-error))) - (cl-letf* (((process-filter process) + (cl-letf* ((original-filter-fn (process-filter process)) + ((process-filter process) (lambda (_proc str) (with-current-buffer (process-buffer process) - (python-shell-output-filter str)))) + (python-shell-output-filter str)) + (when show-output + (funcall original-filter-fn process str)))) (python-shell-output-filter-in-progress t) + (python-shell-output-filter-inhibit-output (not show-output)) (inhibit-quit t) (buffer (process-buffer process)) (last-prompt (cond ((boundp 'comint-last-prompt-overlay) @@ -3812,13 +3823,15 @@ python-shell-send-string-no-output (with-local-quit (unwind-protect (python-shell-send-string string process) - (when (not (null last-prompt)) - (with-current-buffer buffer - (set last-prompt last-prompt-value)))) + (unless show-output + (when (not (null last-prompt)) + (with-current-buffer buffer + (set last-prompt last-prompt-value))))) (while python-shell-output-filter-in-progress ;; `python-shell-output-filter' takes care of setting - ;; `python-shell-output-filter-in-progress' to NIL after it - ;; detects end of output. + ;; `python-shell-output-filter-in-progress' and + ;; `python-shell-output-filter-inhibit-output' to NIL after + ;; it detects end of output. (accept-process-output process)) (prog1 python-shell-output-filter-buffer -- 2.42.0