1 ;;; ob-comint.el --- org-babel functions for interaction with comint buffers
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, comint
7 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; These functions build on comint to ease the sending and receiving
28 ;; of commands and results from comint buffers.
30 ;; Note that the buffers in this file are analogous to sessions in
31 ;; org-babel at large.
36 (eval-when-compile (require 'cl
))
38 (defun org-babel-comint-buffer-livep (buffer)
39 "Check if BUFFER is a comint buffer with a live process."
40 (let ((buffer (if buffer
(get-buffer buffer
))))
41 (and buffer
(buffer-live-p buffer
) (get-buffer-process buffer
) buffer
)))
43 (defmacro org-babel-comint-in-buffer
(buffer &rest body
)
44 "Check BUFFER with `org-babel-comint-buffer-livep' then execute
45 body inside the protection of `save-window-excursion' and
50 (unless (org-babel-comint-buffer-livep ,buffer
)
51 (error "buffer %s doesn't exist or has no process" ,buffer
))
55 (defmacro org-babel-comint-with-output
(meta &rest body
)
56 "Evaluate BODY in BUFFER, wait until EOE-INDICATOR appears in
57 output, then return all process output. If REMOVE-ECHO and
58 FULL-BODY are present and non-nil, then strip echo'd body from
59 the returned output. META should be a list containing the
60 following where the last two elements are optional.
62 (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
64 This macro ensures that the filter is removed in case of an error
65 or user `keyboard-quit' during execution of body."
67 (let ((buffer (car meta
))
68 (eoe-indicator (cadr meta
))
69 (remove-echo (cadr (cdr meta
)))
70 (full-body (cadr (cdr (cdr meta
)))))
71 `(org-babel-comint-in-buffer ,buffer
72 (let ((string-buffer "") dangling-text raw
)
73 (flet ((my-filt (text)
74 (setq string-buffer
(concat string-buffer text
))))
76 (add-hook 'comint-output-filter-functions
'my-filt
)
79 ;; got located, and save dangling text
80 (goto-char (process-mark (get-buffer-process (current-buffer))))
83 (setq dangling-text
(buffer-substring start end
))
84 (delete-region start end
))
85 ;; pass FULL-BODY to process
87 ;; wait for end-of-evaluation indicator
89 (goto-char comint-last-input-end
)
91 (and (re-search-forward
92 comint-prompt-regexp nil t
)
94 (regexp-quote ,eoe-indicator
) nil t
)))))
95 (accept-process-output (get-buffer-process (current-buffer)))
96 ;; thought the following this would allow async
97 ;; background running, but I was wrong...
98 ;; (run-with-timer .5 .5 'accept-process-output
99 ;; (get-buffer-process (current-buffer)))
101 ;; replace cut dangling text
102 (goto-char (process-mark (get-buffer-process (current-buffer))))
103 (insert dangling-text
))
105 (remove-hook 'comint-output-filter-functions
'my-filt
)))
106 ;; remove echo'd FULL-BODY from input
107 (if (and ,remove-echo
,full-body
109 (replace-regexp-in-string
110 "\n" "[\r\n]+" (regexp-quote (or ,full-body
"")))
112 (setq raw
(substring string-buffer
(match-end 0))))
113 (split-string string-buffer comint-prompt-regexp
)))))
115 (defun org-babel-comint-input-command (buffer cmd
)
116 "Pass CMD to BUFFER The input will not be echoed."
117 (org-babel-comint-in-buffer buffer
118 (goto-char (process-mark (get-buffer-process buffer
)))
121 (org-babel-comint-wait-for-output buffer
)))
123 (defun org-babel-comint-wait-for-output (buffer)
124 "Wait until output arrives from BUFFER. Note: this is only
125 safe when waiting for the result of a single statement (not large
127 (org-babel-comint-in-buffer buffer
129 (goto-char comint-last-input-end
)
130 (not (and (re-search-forward comint-prompt-regexp nil t
)
131 (goto-char (match-beginning 0))
132 (string= (face-name (face-at-point))
133 "comint-highlight-prompt"))))
134 (accept-process-output (get-buffer-process buffer
)))))
138 ;; arch-tag: 9adddce6-0864-4be3-b0b5-6c5157dc7889
140 ;;; ob-comint.el ends here