1 ;;; ob-comint.el --- org-babel functions for interaction with comint buffers
3 ;; Copyright (C) 2009-2016 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research, comint
7 ;; Homepage: http://orgmode.org
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;; These functions build on comint to ease the sending and receiving
27 ;; of commands and results from comint buffers.
29 ;; Note that the buffers in this file are analogous to sessions in
30 ;; org-babel at large.
36 (eval-when-compile (require 'cl
))
37 (declare-function with-parsed-tramp-file-name
"tramp"
38 (filename var
&rest body
) t
)
39 (declare-function tramp-flush-directory-property
"tramp-cache" (key directory
))
41 (defun org-babel-comint-buffer-livep (buffer)
42 "Check if BUFFER is a comint buffer with a live process."
43 (let ((buffer (if buffer
(get-buffer buffer
))))
44 (and buffer
(buffer-live-p buffer
) (get-buffer-process buffer
) buffer
)))
46 (defmacro org-babel-comint-in-buffer
(buffer &rest body
)
47 "Check BUFFER and execute BODY.
48 BUFFER is checked with `org-babel-comint-buffer-livep'. BODY is
49 executed inside the protection of `save-excursion' and
54 (unless (org-babel-comint-buffer-livep ,buffer
)
55 (error "Buffer %s does not exist or has no process" ,buffer
))
58 (def-edebug-spec org-babel-comint-in-buffer
(form body
))
60 (defmacro org-babel-comint-with-output
(meta &rest body
)
61 "Evaluate BODY in BUFFER and return process output.
62 Will wait until EOE-INDICATOR appears in the output, then return
63 all process output. If REMOVE-ECHO and FULL-BODY are present and
64 non-nil, then strip echo'd body from the returned output. META
65 should be a list containing the following where the last two
66 elements are optional.
68 (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
70 This macro ensures that the filter is removed in case of an error
71 or user `keyboard-quit' during execution of body."
73 (let ((buffer (car meta
))
74 (eoe-indicator (cadr meta
))
75 (remove-echo (cadr (cdr meta
)))
76 (full-body (cadr (cdr (cdr meta
)))))
77 `(org-babel-comint-in-buffer ,buffer
78 (let ((string-buffer "") dangling-text raw
)
80 (setq comint-output-filter-functions
81 (cons (lambda (text) (setq string-buffer
(concat string-buffer text
)))
82 comint-output-filter-functions
))
85 ;; got located, and save dangling text
86 (goto-char (process-mark (get-buffer-process (current-buffer))))
89 (setq dangling-text
(buffer-substring start end
))
90 (delete-region start end
))
91 ;; pass FULL-BODY to process
93 ;; wait for end-of-evaluation indicator
95 (goto-char comint-last-input-end
)
97 (and (re-search-forward
98 (regexp-quote ,eoe-indicator
) nil t
)
100 comint-prompt-regexp nil t
)))))
101 (accept-process-output (get-buffer-process (current-buffer)))
102 ;; thought the following this would allow async
103 ;; background running, but I was wrong...
104 ;; (run-with-timer .5 .5 'accept-process-output
105 ;; (get-buffer-process (current-buffer)))
107 ;; replace cut dangling text
108 (goto-char (process-mark (get-buffer-process (current-buffer))))
109 (insert dangling-text
))
111 (setq comint-output-filter-functions
112 (cdr comint-output-filter-functions
)))
113 ;; remove echo'd FULL-BODY from input
114 (if (and ,remove-echo
,full-body
116 (replace-regexp-in-string
117 "\n" "[\r\n]+" (regexp-quote (or ,full-body
"")))
119 (setq raw
(substring string-buffer
(match-end 0))))
120 (split-string string-buffer comint-prompt-regexp
)))))
121 (def-edebug-spec org-babel-comint-with-output
(sexp body
))
123 (defun org-babel-comint-input-command (buffer cmd
)
125 The input will not be echoed."
126 (org-babel-comint-in-buffer buffer
127 (goto-char (process-mark (get-buffer-process buffer
)))
130 (org-babel-comint-wait-for-output buffer
)))
132 (defun org-babel-comint-wait-for-output (buffer)
133 "Wait until output arrives from BUFFER.
134 Note: this is only safe when waiting for the result of a single
135 statement (not large blocks of code)."
136 (org-babel-comint-in-buffer buffer
138 (goto-char comint-last-input-end
)
139 (not (and (re-search-forward comint-prompt-regexp nil t
)
140 (goto-char (match-beginning 0))
141 (string= (face-name (face-at-point))
142 "comint-highlight-prompt"))))
143 (accept-process-output (get-buffer-process buffer
)))))
145 (defun org-babel-comint-eval-invisibly-and-wait-for-file
146 (buffer file string
&optional period
)
147 "Evaluate STRING in BUFFER invisibly.
148 Don't return until FILE exists. Code in STRING must ensure that
149 FILE exists at end of evaluation."
150 (unless (org-babel-comint-buffer-livep buffer
)
151 (error "Buffer %s does not exist or has no process" buffer
))
152 (if (file-exists-p file
) (delete-file file
))
154 (get-buffer-process buffer
)
155 (if (string-match "\n$" string
) string
(concat string
"\n")))
156 ;; From Tramp 2.1.19 the following cache flush is not necessary
157 (if (file-remote-p default-directory
)
159 (with-parsed-tramp-file-name default-directory nil
160 (tramp-flush-directory-property v
""))))
161 (while (not (file-exists-p file
)) (sit-for (or period
0.25))))
167 ;;; ob-comint.el ends here