Commit | Line | Data |
---|---|---|
86fbb8ca CD |
1 | ;;; ob-sh.el --- org-babel functions for shell evaluation |
2 | ||
cbd20947 | 3 | ;; Copyright (C) 2009-2011 Free Software Foundation, Inc. |
86fbb8ca CD |
4 | |
5 | ;; Author: Eric Schulte | |
6 | ;; Keywords: literate programming, reproducible research | |
7 | ;; Homepage: http://orgmode.org | |
3ab2c837 | 8 | ;; Version: 7.7 |
86fbb8ca CD |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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. | |
16 | ||
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. | |
21 | ||
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/>. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Org-Babel support for evaluating shell source code. | |
28 | ||
29 | ;;; Code: | |
30 | (require 'ob) | |
3ab2c837 | 31 | (require 'ob-ref) |
86fbb8ca CD |
32 | (require 'ob-comint) |
33 | (require 'ob-eval) | |
34 | (require 'shell) | |
35 | (eval-when-compile (require 'cl)) | |
36 | ||
afe98dfa CD |
37 | (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body)) |
38 | (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer)) | |
39 | (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer)) | |
40 | (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body)) | |
86fbb8ca CD |
41 | (declare-function orgtbl-to-generic "org-table" (table params)) |
42 | ||
43 | (defvar org-babel-default-header-args:sh '()) | |
44 | ||
45 | (defvar org-babel-sh-command "sh" | |
46 | "Command used to invoke a shell. | |
47 | This will be passed to `shell-command-on-region'") | |
48 | ||
3ab2c837 BG |
49 | (defcustom org-babel-sh-var-quote-fmt |
50 | "$(cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n)" | |
51 | "Format string used to escape variables when passed to shell scripts." | |
52 | :group 'org-babel | |
53 | :type 'string) | |
54 | ||
86fbb8ca CD |
55 | (defun org-babel-execute:sh (body params) |
56 | "Execute a block of Shell commands with Babel. | |
57 | This function is called by `org-babel-execute-src-block'." | |
afe98dfa CD |
58 | (let* ((session (org-babel-sh-initiate-session |
59 | (cdr (assoc :session params)))) | |
3ab2c837 BG |
60 | (result-params (cdr (assoc :result-params params))) |
61 | (stdin ((lambda (stdin) (when stdin (org-babel-sh-var-to-string | |
62 | (org-babel-ref-resolve stdin)))) | |
63 | (cdr (assoc :stdin params)))) | |
afe98dfa CD |
64 | (full-body (org-babel-expand-body:generic |
65 | body params (org-babel-variable-assignments:sh params)))) | |
86fbb8ca | 66 | (org-babel-reassemble-table |
3ab2c837 | 67 | (org-babel-sh-evaluate session full-body result-params stdin) |
86fbb8ca | 68 | (org-babel-pick-name |
afe98dfa | 69 | (cdr (assoc :colname-names params)) (cdr (assoc :colnames params))) |
86fbb8ca | 70 | (org-babel-pick-name |
afe98dfa | 71 | (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))) |
86fbb8ca CD |
72 | |
73 | (defun org-babel-prep-session:sh (session params) | |
74 | "Prepare SESSION according to the header arguments specified in PARAMS." | |
75 | (let* ((session (org-babel-sh-initiate-session session)) | |
afe98dfa | 76 | (var-lines (org-babel-variable-assignments:sh params))) |
86fbb8ca CD |
77 | (org-babel-comint-in-buffer session |
78 | (mapc (lambda (var) | |
79 | (insert var) (comint-send-input nil t) | |
80 | (org-babel-comint-wait-for-output session)) var-lines)) | |
81 | session)) | |
82 | ||
83 | (defun org-babel-load-session:sh (session body params) | |
84 | "Load BODY into SESSION." | |
85 | (save-window-excursion | |
86 | (let ((buffer (org-babel-prep-session:sh session params))) | |
87 | (with-current-buffer buffer | |
88 | (goto-char (process-mark (get-buffer-process (current-buffer)))) | |
89 | (insert (org-babel-chomp body))) | |
90 | buffer))) | |
91 | ||
92 | ;; helper functions | |
93 | ||
afe98dfa CD |
94 | (defun org-babel-variable-assignments:sh (params) |
95 | "Return list of shell statements assigning the block's variables" | |
96 | (let ((sep (cdr (assoc :separator params)))) | |
97 | (mapcar | |
98 | (lambda (pair) | |
99 | (format "%s=%s" | |
100 | (car pair) | |
101 | (org-babel-sh-var-to-sh (cdr pair) sep))) | |
102 | (mapcar #'cdr (org-babel-get-header params :var))))) | |
103 | ||
86fbb8ca CD |
104 | (defun org-babel-sh-var-to-sh (var &optional sep) |
105 | "Convert an elisp value to a shell variable. | |
106 | Convert an elisp var into a string of shell commands specifying a | |
107 | var of the same value." | |
3ab2c837 BG |
108 | (format org-babel-sh-var-quote-fmt (org-babel-sh-var-to-string var sep))) |
109 | ||
110 | (defun org-babel-sh-var-to-string (var &optional sep) | |
111 | "Convert an elisp value to a string." | |
112 | (flet ((echo-var (v) (if (stringp v) v (format "%S" v)))) | |
113 | (cond | |
114 | ((and (listp var) (listp (car var))) | |
115 | (orgtbl-to-generic var (list :sep (or sep "\t") :fmt #'echo-var))) | |
116 | ((listp var) | |
117 | (mapconcat #'echo-var var "\n")) | |
118 | (t (echo-var var))))) | |
86fbb8ca CD |
119 | |
120 | (defun org-babel-sh-table-or-results (results) | |
121 | "Convert RESULTS to an appropriate elisp value. | |
122 | If the results look like a table, then convert them into an | |
123 | Emacs-lisp table, otherwise return the results as a string." | |
acedf35c | 124 | (org-babel-script-escape results)) |
86fbb8ca CD |
125 | |
126 | (defun org-babel-sh-initiate-session (&optional session params) | |
127 | "Initiate a session named SESSION according to PARAMS." | |
128 | (when (and session (not (string= session "none"))) | |
129 | (save-window-excursion | |
130 | (or (org-babel-comint-buffer-livep session) | |
131 | (progn (shell session) (get-buffer (current-buffer))))))) | |
132 | ||
133 | (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'" | |
134 | "String to indicate that evaluation has completed.") | |
135 | (defvar org-babel-sh-eoe-output "org_babel_sh_eoe" | |
136 | "String to indicate that evaluation has completed.") | |
137 | ||
3ab2c837 | 138 | (defun org-babel-sh-evaluate (session body &optional result-params stdin) |
86fbb8ca CD |
139 | "Pass BODY to the Shell process in BUFFER. |
140 | If RESULT-TYPE equals 'output then return a list of the outputs | |
141 | of the statements in BODY, if RESULT-TYPE equals 'value then | |
142 | return the value of the last statement in BODY." | |
143 | ((lambda (results) | |
afe98dfa CD |
144 | (when results |
145 | (if (or (member "scalar" result-params) | |
3ab2c837 | 146 | (member "verbatim" result-params) |
afe98dfa CD |
147 | (member "output" result-params)) |
148 | results | |
149 | (let ((tmp-file (org-babel-temp-file "sh-"))) | |
150 | (with-temp-file tmp-file (insert results)) | |
151 | (org-babel-import-elisp-from-file tmp-file))))) | |
3ab2c837 BG |
152 | (cond |
153 | (stdin ; external shell script w/STDIN | |
154 | (let ((script-file (org-babel-temp-file "sh-script-")) | |
155 | (stdin-file (org-babel-temp-file "sh-stdin-"))) | |
156 | (with-temp-file script-file (insert body)) | |
157 | (with-temp-file stdin-file (insert stdin)) | |
158 | (with-temp-buffer | |
159 | (call-process-shell-command | |
160 | (format "%s %s" org-babel-sh-command script-file) | |
161 | stdin-file | |
162 | (current-buffer)) | |
163 | (buffer-string)))) | |
164 | (session ; session evaluation | |
afe98dfa CD |
165 | (mapconcat |
166 | #'org-babel-sh-strip-weird-long-prompt | |
167 | (mapcar | |
168 | #'org-babel-trim | |
169 | (butlast | |
170 | (org-babel-comint-with-output | |
171 | (session org-babel-sh-eoe-output t body) | |
172 | (mapc | |
173 | (lambda (line) | |
3ab2c837 BG |
174 | (insert line) |
175 | (comint-send-input nil t) | |
176 | (while (save-excursion | |
177 | (goto-char comint-last-input-end) | |
178 | (not (re-search-forward | |
179 | comint-prompt-regexp nil t))) | |
180 | (accept-process-output (get-buffer-process (current-buffer))))) | |
afe98dfa CD |
181 | (append |
182 | (split-string (org-babel-trim body) "\n") | |
183 | (list org-babel-sh-eoe-indicator)))) | |
3ab2c837 BG |
184 | 2)) "\n")) |
185 | ('otherwise ; external shell script | |
186 | (org-babel-eval org-babel-sh-command (org-babel-trim body)))))) | |
86fbb8ca CD |
187 | |
188 | (defun org-babel-sh-strip-weird-long-prompt (string) | |
189 | "Remove prompt cruft from a string of shell output." | |
190 | (while (string-match "^% +[\r\n$]+ *" string) | |
191 | (setq string (substring string (match-end 0)))) | |
192 | string) | |
193 | ||
194 | (provide 'ob-sh) | |
195 | ||
5b409b39 | 196 | |
86fbb8ca CD |
197 | |
198 | ;;; ob-sh.el ends here |