org-html.el: Use `org-line' instead of `line' as the free variable name.
[org-mode.git] / lisp / ob-comint.el
bloba0712b90f35558ce94a34f789ad46a0471a158ef
1 ;;; ob-comint.el --- org-babel functions for interaction with comint buffers
3 ;; Copyright (C) 2009-2012 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/>.
24 ;;; Commentary:
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.
32 ;;; Code:
33 (require 'ob)
34 (require 'comint)
35 (eval-when-compile (require 'cl))
36 (declare-function with-parsed-tramp-file-name "tramp" (filename var &rest body))
37 (declare-function tramp-flush-directory-property "tramp" (vec directory))
39 (defun org-babel-comint-buffer-livep (buffer)
40 "Check if BUFFER is a comint buffer with a live process."
41 (let ((buffer (if buffer (get-buffer buffer))))
42 (and buffer (buffer-live-p buffer) (get-buffer-process buffer) buffer)))
44 (defmacro org-babel-comint-in-buffer (buffer &rest body)
45 "Check BUFFER and execute BODY.
46 BUFFER is checked with `org-babel-comint-buffer-livep'. BODY is
47 executed inside the protection of `save-excursion' and
48 `save-match-data'."
49 (declare (indent 1))
50 `(save-excursion
51 (save-match-data
52 (unless (org-babel-comint-buffer-livep ,buffer)
53 (error "buffer %s doesn't exist or has no process" ,buffer))
54 (set-buffer ,buffer)
55 ,@body)))
56 (def-edebug-spec org-babel-comint-in-buffer (form body))
58 (defmacro org-babel-comint-with-output (meta &rest body)
59 "Evaluate BODY in BUFFER and return process output.
60 Will wait until EOE-INDICATOR appears in the output, then return
61 all process output. If REMOVE-ECHO and FULL-BODY are present and
62 non-nil, then strip echo'd body from the returned output. META
63 should be a list containing the following where the last two
64 elements are optional.
66 (BUFFER EOE-INDICATOR REMOVE-ECHO FULL-BODY)
68 This macro ensures that the filter is removed in case of an error
69 or user `keyboard-quit' during execution of body."
70 (declare (indent 1))
71 (let ((buffer (car meta))
72 (eoe-indicator (cadr meta))
73 (remove-echo (cadr (cdr meta)))
74 (full-body (cadr (cdr (cdr meta)))))
75 `(org-babel-comint-in-buffer ,buffer
76 (let ((string-buffer "") dangling-text raw)
77 (flet ((my-filt (text)
78 (setq string-buffer (concat string-buffer text))))
79 ;; setup filter
80 (add-hook 'comint-output-filter-functions 'my-filt)
81 (unwind-protect
82 (progn
83 ;; got located, and save dangling text
84 (goto-char (process-mark (get-buffer-process (current-buffer))))
85 (let ((start (point))
86 (end (point-max)))
87 (setq dangling-text (buffer-substring start end))
88 (delete-region start end))
89 ;; pass FULL-BODY to process
90 ,@body
91 ;; wait for end-of-evaluation indicator
92 (while (progn
93 (goto-char comint-last-input-end)
94 (not (save-excursion
95 (and (re-search-forward
96 (regexp-quote ,eoe-indicator) nil t)
97 (re-search-forward
98 comint-prompt-regexp nil t)))))
99 (accept-process-output (get-buffer-process (current-buffer)))
100 ;; thought the following this would allow async
101 ;; background running, but I was wrong...
102 ;; (run-with-timer .5 .5 'accept-process-output
103 ;; (get-buffer-process (current-buffer)))
105 ;; replace cut dangling text
106 (goto-char (process-mark (get-buffer-process (current-buffer))))
107 (insert dangling-text))
108 ;; remove filter
109 (remove-hook 'comint-output-filter-functions 'my-filt)))
110 ;; remove echo'd FULL-BODY from input
111 (if (and ,remove-echo ,full-body
112 (string-match
113 (replace-regexp-in-string
114 "\n" "[\r\n]+" (regexp-quote (or ,full-body "")))
115 string-buffer))
116 (setq raw (substring string-buffer (match-end 0))))
117 (split-string string-buffer comint-prompt-regexp)))))
118 (def-edebug-spec org-babel-comint-with-output (form body))
120 (defun org-babel-comint-input-command (buffer cmd)
121 "Pass CMD to BUFFER.
122 The input will not be echoed."
123 (org-babel-comint-in-buffer buffer
124 (goto-char (process-mark (get-buffer-process buffer)))
125 (insert cmd)
126 (comint-send-input)
127 (org-babel-comint-wait-for-output buffer)))
129 (defun org-babel-comint-wait-for-output (buffer)
130 "Wait until output arrives from BUFFER.
131 Note: this is only safe when waiting for the result of a single
132 statement (not large blocks of code)."
133 (org-babel-comint-in-buffer buffer
134 (while (progn
135 (goto-char comint-last-input-end)
136 (not (and (re-search-forward comint-prompt-regexp nil t)
137 (goto-char (match-beginning 0))
138 (string= (face-name (face-at-point))
139 "comint-highlight-prompt"))))
140 (accept-process-output (get-buffer-process buffer)))))
142 (defun org-babel-comint-eval-invisibly-and-wait-for-file
143 (buffer file string &optional period)
144 "Evaluate STRING in BUFFER invisibly.
145 Don't return until FILE exists. Code in STRING must ensure that
146 FILE exists at end of evaluation."
147 (unless (org-babel-comint-buffer-livep buffer)
148 (error "buffer %s doesn't exist or has no process" buffer))
149 (if (file-exists-p file) (delete-file file))
150 (process-send-string
151 (get-buffer-process buffer)
152 (if (string-match "\n$" string) string (concat string "\n")))
153 ;; From Tramp 2.1.19 the following cache flush is not necessary
154 (if (file-remote-p default-directory)
155 (let (v)
156 (with-parsed-tramp-file-name default-directory nil
157 (tramp-flush-directory-property v ""))))
158 (while (not (file-exists-p file)) (sit-for (or period 0.25))))
160 (provide 'ob-comint)
164 ;;; ob-comint.el ends here