Merge branch 'maint'
[org-mode/org-kjn.git] / lisp / ob-shell.el
blob474a8f2a7f5f52786b3bdb589db1bc0cd2a9dd22
1 ;;; ob-shell.el --- org-babel functions for shell evaluation
3 ;; Copyright (C) 2009-2014 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
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 ;; Org-Babel support for evaluating shell source code.
28 ;;; Code:
29 (require 'ob)
30 (require 'shell)
31 (eval-when-compile (require 'cl))
33 (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
34 (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
35 (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
36 (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
37 (declare-function orgtbl-to-generic "org-table" (table params))
39 (defvar org-babel-default-header-args:sh '())
41 (defcustom org-babel-sh-command shell-file-name
42 "Command used to invoke a shell.
43 Set by default to the value of `shell-file-name'. This will be
44 passed to `shell-command-on-region'"
45 :group 'org-babel
46 :type 'string)
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)
54 (defcustom org-babel-shell-names
55 '("sh" "bash" "csh" "ash" "dash" "ksh" "mksh" "posh")
56 "List of names of shell supported by babel shell code blocks."
57 :group 'org-babel
58 :type 'string
59 :initialize
60 (lambda (symbol value)
61 (set-default symbol (second value))
62 (mapc
63 (lambda (name)
64 (eval `(defun ,(intern (concat "org-babel-execute:" name)) (body params)
65 ,(format "Execute a block of %s commands with Babel." name)
66 (let ((org-babel-sh-command ,name))
67 (org-babel-execute:shell body params)))))
68 (second value))))
70 (defun org-babel-execute:shell (body params)
71 "Execute a block of Shell commands with Babel.
72 This function is called by `org-babel-execute-src-block'."
73 (let* ((session (org-babel-sh-initiate-session
74 (cdr (assoc :session params))))
75 (stdin (let ((stdin (cdr (assoc :stdin params))))
76 (when stdin (org-babel-sh-var-to-string
77 (org-babel-ref-resolve stdin)))))
78 (cmdline (cdr (assoc :cmdline params)))
79 (full-body (org-babel-expand-body:generic
80 body params (org-babel-variable-assignments:sh params))))
81 (org-babel-reassemble-table
82 (org-babel-sh-evaluate session full-body params stdin cmdline)
83 (org-babel-pick-name
84 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
85 (org-babel-pick-name
86 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
88 (defun org-babel-prep-session:sh (session params)
89 "Prepare SESSION according to the header arguments specified in PARAMS."
90 (let* ((session (org-babel-sh-initiate-session session))
91 (var-lines (org-babel-variable-assignments:sh params)))
92 (org-babel-comint-in-buffer session
93 (mapc (lambda (var)
94 (insert var) (comint-send-input nil t)
95 (org-babel-comint-wait-for-output session)) var-lines))
96 session))
98 (defun org-babel-load-session:sh (session body params)
99 "Load BODY into SESSION."
100 (save-window-excursion
101 (let ((buffer (org-babel-prep-session:sh session params)))
102 (with-current-buffer buffer
103 (goto-char (process-mark (get-buffer-process (current-buffer))))
104 (insert (org-babel-chomp body)))
105 buffer)))
107 ;; helper functions
108 (defun org-babel-variable-assignments:sh-generic
109 (varname values &optional sep hline)
110 "Returns a list of statements declaring the values as a generic variable."
111 (format "%s=%s" varname (org-babel-sh-var-to-sh values sep hline)))
113 (defun org-babel-variable-assignments:bash_array
114 (varname values &optional sep hline)
115 "Returns a list of statements declaring the values as a bash array."
116 (format "unset %s\ndeclare -a %s=( \"%s\" )"
117 varname varname
118 (mapconcat 'identity
119 (mapcar
120 (lambda (value) (org-babel-sh-var-to-sh value sep hline))
121 values)
122 "\" \"")))
124 (defun org-babel-variable-assignments:bash_assoc
125 (varname values &optional sep hline)
126 "Returns a list of statements declaring the values as bash associative array."
127 (format "unset %s\ndeclare -A %s\n%s"
128 varname varname
129 (mapconcat 'identity
130 (mapcar
131 (lambda (items)
132 (format "%s[\"%s\"]=%s"
133 varname
134 (org-babel-sh-var-to-sh (car items) sep hline)
135 (org-babel-sh-var-to-sh (cdr items) sep hline)))
136 values)
137 "\n")))
139 (defun org-babel-variable-assignments:bash (varname values &optional sep hline)
140 "Represents the parameters as useful Bash shell variables."
141 (if (listp values)
142 (if (and (listp (car values)) (= 1 (length (car values))))
143 (org-babel-variable-assignments:bash_array varname values sep hline)
144 (org-babel-variable-assignments:bash_assoc varname values sep hline))
145 (org-babel-variable-assignments:sh-generic varname values sep hline)))
147 (defun org-babel-variable-assignments:sh (params)
148 "Return list of shell statements assigning the block's variables."
149 (let ((sep (cdr (assoc :separator params)))
150 (hline (when (string= "yes" (cdr (assoc :hlines params)))
151 (or (cdr (assoc :hline-string params))
152 "hline"))))
153 (mapcar
154 (lambda (pair)
155 (if (string= org-babel-sh-command "bash")
156 (org-babel-variable-assignments:bash
157 (car pair) (cdr pair) sep hline)
158 (org-babel-variable-assignments:sh-generic
159 (car pair) (cdr pair) sep hline)))
160 (mapcar #'cdr (org-babel-get-header params :var)))))
162 (defun org-babel-sh-var-to-sh (var &optional sep hline)
163 "Convert an elisp value to a shell variable.
164 Convert an elisp var into a string of shell commands specifying a
165 var of the same value."
166 (format org-babel-sh-var-quote-fmt
167 (org-babel-sh-var-to-string var sep hline)))
169 (defun org-babel-sh-var-to-string (var &optional sep hline)
170 "Convert an elisp value to a string."
171 (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
172 (cond
173 ((and (listp var) (or (listp (car var)) (equal (car var) 'hline)))
174 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var
175 :hline hline)))
176 ((listp var)
177 (mapconcat echo-var var "\n"))
178 (t (funcall echo-var var)))))
180 (defun org-babel-sh-table-or-results (results)
181 "Convert RESULTS to an appropriate elisp value.
182 If the results look like a table, then convert them into an
183 Emacs-lisp table, otherwise return the results as a string."
184 (org-babel-script-escape results))
186 (defun org-babel-sh-initiate-session (&optional session params)
187 "Initiate a session named SESSION according to PARAMS."
188 (when (and session (not (string= session "none")))
189 (save-window-excursion
190 (or (org-babel-comint-buffer-livep session)
191 (progn (shell session) (get-buffer (current-buffer)))))))
193 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
194 "String to indicate that evaluation has completed.")
195 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
196 "String to indicate that evaluation has completed.")
198 (defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
199 "Pass BODY to the Shell process in BUFFER.
200 If RESULT-TYPE equals 'output then return a list of the outputs
201 of the statements in BODY, if RESULT-TYPE equals 'value then
202 return the value of the last statement in BODY."
203 (let ((results
204 (cond
205 ((or stdin cmdline) ; external shell script w/STDIN
206 (let ((script-file (org-babel-temp-file "sh-script-"))
207 (stdin-file (org-babel-temp-file "sh-stdin-"))
208 (shebang (cdr (assoc :shebang params)))
209 (padline (not (string= "no" (cdr (assoc :padline params))))))
210 (with-temp-file script-file
211 (when shebang (insert (concat shebang "\n")))
212 (when padline (insert "\n"))
213 (insert body))
214 (set-file-modes script-file #o755)
215 (with-temp-file stdin-file (insert (or stdin "")))
216 (with-temp-buffer
217 (call-process-shell-command
218 (if shebang
219 script-file
220 (format "%s %s" org-babel-sh-command script-file))
221 stdin-file
222 (current-buffer) nil cmdline)
223 (buffer-string))))
224 (session ; session evaluation
225 (mapconcat
226 #'org-babel-sh-strip-weird-long-prompt
227 (mapcar
228 #'org-babel-trim
229 (butlast
230 (org-babel-comint-with-output
231 (session org-babel-sh-eoe-output t body)
232 (mapc
233 (lambda (line)
234 (insert line)
235 (comint-send-input nil t)
236 (while (save-excursion
237 (goto-char comint-last-input-end)
238 (not (re-search-forward
239 comint-prompt-regexp nil t)))
240 (accept-process-output
241 (get-buffer-process (current-buffer)))))
242 (append
243 (split-string (org-babel-trim body) "\n")
244 (list org-babel-sh-eoe-indicator))))
245 2)) "\n"))
246 ('otherwise ; external shell script
247 (if (and (cdr (assoc :shebang params))
248 (> (length (cdr (assoc :shebang params))) 0))
249 (let ((script-file (org-babel-temp-file "sh-script-"))
250 (shebang (cdr (assoc :shebang params)))
251 (padline (not (equal "no" (cdr (assoc :padline params))))))
252 (with-temp-file script-file
253 (when shebang (insert (concat shebang "\n")))
254 (when padline (insert "\n"))
255 (insert body))
256 (set-file-modes script-file #o755)
257 (org-babel-eval script-file ""))
258 (org-babel-eval org-babel-sh-command (org-babel-trim body)))))))
259 (when results
260 (let ((result-params (cdr (assoc :result-params params))))
261 (org-babel-result-cond result-params
262 results
263 (let ((tmp-file (org-babel-temp-file "sh-")))
264 (with-temp-file tmp-file (insert results))
265 (org-babel-import-elisp-from-file tmp-file)))))))
267 (defun org-babel-sh-strip-weird-long-prompt (string)
268 "Remove prompt cruft from a string of shell output."
269 (while (string-match "^% +[\r\n$]+ *" string)
270 (setq string (substring string (match-end 0))))
271 string)
273 (provide 'ob-shell)
277 ;;; ob-shell.el ends here