Function: org-babel-comint-with-output

org-babel-comint-with-output is a macro defined in ob-comint.el.gz.

Signature

(org-babel-comint-with-output META &rest BODY)

Documentation

Evaluate BODY in BUFFER and return process output.

Will wait until EOE-INDICATOR appears in the output, then return all process output. If REMOVE-ECHO and FULL-BODY are present and non-nil, then strip echo'd body from the returned output. PROMPT-HANDLING may be either of the symbols filter-prompts, in which case the output is split by comint-prompt-regexp and returned as a list; or, disable-prompt-filtering, which suppresses this behavior and returns the full output as a string. META should be a list containing the following where the last three elements are optional.

 (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY PROMPT-HANDLING)

This macro ensures that the filter is removed in case of an error or user keyboard-quit during execution of body.

Source Code

;; Defined in /usr/src/emacs/lisp/org/ob-comint.el.gz
(defmacro org-babel-comint-with-output (meta &rest body)
  "Evaluate BODY in BUFFER and return process output.
Will wait until EOE-INDICATOR appears in the output, then return
all process output.  If REMOVE-ECHO and FULL-BODY are present and
non-nil, then strip echo'd body from the returned output.
PROMPT-HANDLING may be either of the symbols `filter-prompts', in
which case the output is split by `comint-prompt-regexp' and
returned as a list; or, `disable-prompt-filtering', which
suppresses this behavior and returns the full output as a string.
META should be a list containing the following where the last
three elements are optional.

 (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY PROMPT-HANDLING)

This macro ensures that the filter is removed in case of an error
or user `keyboard-quit' during execution of body."
  (declare (indent 1) (debug (sexp body)))
  (let ((buffer (nth 0 meta))
	(eoe-indicator (nth 1 meta))
	(remove-echo (nth 2 meta))
	(full-body (nth 3 meta))
        (prompt-handling (nth 4 meta)))
    `(org-babel-comint-in-buffer ,buffer
       (let* ((string-buffer "")
	      (comint-output-filter-functions
	       (cons (lambda (text)
                       (setq string-buffer (concat string-buffer text)))
		     comint-output-filter-functions))
	      dangling-text)
         ;; got located, and save dangling text
	 (goto-char (process-mark (get-buffer-process (current-buffer))))
	 (let ((start (point))
	       (end (point-max)))
	   (setq dangling-text (buffer-substring start end))
	   (delete-region start end))
	 ;; pass FULL-BODY to process
	 ,@body
	 ;; wait for end-of-evaluation indicator
         (let ((start-time (current-time)))
	   (while (not (save-excursion
                         (and (string-match
			       (regexp-quote ,eoe-indicator) string-buffer)
			      (string-match
			       comint-prompt-regexp string-buffer))))
	     (accept-process-output
              (get-buffer-process (current-buffer))
              org-babel-comint-fallback-regexp-threshold)
             (when (and org-babel-comint-prompt-regexp-fallback
                        (> (float-time (time-since start-time))
                           org-babel-comint-fallback-regexp-threshold)
                        (progn
                          (goto-char comint-last-input-end)
                          (save-excursion
                            (and
                             (string-match
			      (regexp-quote ,eoe-indicator) string-buffer)
			     (string-match
                              org-babel-comint-prompt-regexp-fallback string-buffer)))))
               (org-babel-comint--set-fallback-prompt))))
	 ;; replace cut dangling text
         (goto-char (process-mark (get-buffer-process (current-buffer))))
	 (insert dangling-text)

         ;; remove echo'd FULL-BODY from input
         (and ,remove-echo ,full-body
              (setq string-buffer (org-babel-comint--echo-filter string-buffer ,full-body)))

         (if (org-babel-comint--remove-prompts-p ,prompt-handling)
             (org-babel-comint--prompt-filter string-buffer)
           string-buffer)))))