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