1 ;;; ob-sh.el --- org-babel functions for shell evaluation
3 ;; Copyright (C) 2009-2016 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/>.
26 ;; Org-Babel support for evaluating shell source code.
31 (eval-when-compile (require 'cl
))
33 (declare-function org-babel-comint-wait-for-output
"ob-comint" (buffer))
34 (declare-function org-babel-comint-buffer-livep
"ob-comint" (buffer))
35 (declare-function orgtbl-to-generic
"org-table"
36 (table params
&optional backend
))
38 (defvar org-babel-default-header-args
:sh
'())
40 (defvar org-babel-sh-command
"sh"
41 "Command used to invoke a shell.
42 This will be passed to `shell-command-on-region'")
44 (defcustom org-babel-sh-var-quote-fmt
45 "$(cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n)"
46 "Format string used to escape variables when passed to shell scripts."
50 (defun org-babel-execute:sh
(body params
)
51 "Execute a block of Shell commands with Babel.
52 This function is called by `org-babel-execute-src-block'."
53 (let* ((session (org-babel-sh-initiate-session
54 (cdr (assoc :session params
))))
55 (stdin (let ((stdin (cdr (assoc :stdin params
))))
56 (when stdin
(org-babel-sh-var-to-string
57 (org-babel-ref-resolve stdin
)))))
58 (full-body (org-babel-expand-body:generic
59 body params
(org-babel-variable-assignments:sh params
))))
60 (org-babel-reassemble-table
61 (org-babel-sh-evaluate session full-body params stdin
)
63 (cdr (assoc :colname-names params
)) (cdr (assoc :colnames params
)))
65 (cdr (assoc :rowname-names params
)) (cdr (assoc :rownames params
))))))
67 (defun org-babel-prep-session:sh
(session params
)
68 "Prepare SESSION according to the header arguments specified in PARAMS."
69 (let* ((session (org-babel-sh-initiate-session session
))
70 (var-lines (org-babel-variable-assignments:sh params
)))
71 (org-babel-comint-in-buffer session
73 (insert var
) (comint-send-input nil t
)
74 (org-babel-comint-wait-for-output session
)) var-lines
))
77 (defun org-babel-load-session:sh
(session body params
)
78 "Load BODY into SESSION."
79 (save-window-excursion
80 (let ((buffer (org-babel-prep-session:sh session params
)))
81 (with-current-buffer buffer
82 (goto-char (process-mark (get-buffer-process (current-buffer))))
83 (insert (org-babel-chomp body
)))
88 (defun org-babel-variable-assignments:sh
(params)
89 "Return list of shell statements assigning the block's variables."
90 (let ((sep (cdr (assoc :separator params
))))
95 (org-babel-sh-var-to-sh (cdr pair
) sep
)))
96 (mapcar #'cdr
(org-babel-get-header params
:var
)))))
98 (defun org-babel-sh-var-to-sh (var &optional sep
)
99 "Convert an elisp value to a shell variable.
100 Convert an elisp var into a string of shell commands specifying a
101 var of the same value."
102 (format org-babel-sh-var-quote-fmt
(org-babel-sh-var-to-string var sep
)))
104 (defun org-babel-sh-var-to-string (var &optional sep
)
105 "Convert an elisp value to a string."
106 (let ((echo-var (lambda (v) (if (stringp v
) v
(format "%S" v
)))))
108 ((and (listp var
) (or (listp (car var
)) (equal (car var
) 'hline
)))
109 (orgtbl-to-generic var
(list :sep
(or sep
"\t") :fmt echo-var
)))
111 (mapconcat echo-var var
"\n"))
112 (t (funcall echo-var var
)))))
114 (defun org-babel-sh-table-or-results (results)
115 "Convert RESULTS to an appropriate elisp value.
116 If the results look like a table, then convert them into an
117 Emacs-lisp table, otherwise return the results as a string."
118 (org-babel-script-escape results
))
120 (defun org-babel-sh-initiate-session (&optional session params
)
121 "Initiate a session named SESSION according to PARAMS."
122 (when (and session
(not (string= session
"none")))
123 (save-window-excursion
124 (or (org-babel-comint-buffer-livep session
)
127 ;; Needed for Emacs 23 since the marker is initially
128 ;; undefined and the filter functions try to use it without
130 (set-marker comint-last-output-start
(point))
131 (get-buffer (current-buffer)))))))
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.")
138 (defun org-babel-sh-evaluate (session body
&optional params stdin
)
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."
145 (stdin ; external shell script w/STDIN
146 (let ((script-file (org-babel-temp-file "sh-script-"))
147 (stdin-file (org-babel-temp-file "sh-stdin-"))
148 (shebang (cdr (assoc :shebang params
)))
149 (padline (not (string= "no" (cdr (assoc :padline params
))))))
150 (with-temp-file script-file
151 (when shebang
(insert (concat shebang
"\n")))
152 (when padline
(insert "\n"))
154 (set-file-modes script-file
#o755
)
155 (with-temp-file stdin-file
(insert stdin
))
157 (call-process-shell-command
160 (format "%s %s" org-babel-sh-command script-file
))
164 (session ; session evaluation
166 #'org-babel-sh-strip-weird-long-prompt
170 (org-babel-comint-with-output
171 (session org-babel-sh-eoe-output t body
)
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
181 (get-buffer-process (current-buffer)))))
183 (split-string (org-babel-trim body
) "\n")
184 (list org-babel-sh-eoe-indicator
))))
186 ('otherwise
; external shell script
187 (if (and (cdr (assoc :shebang params
))
188 (> (length (cdr (assoc :shebang params
))) 0))
189 (let ((script-file (org-babel-temp-file "sh-script-"))
190 (shebang (cdr (assoc :shebang params
)))
191 (padline (not (equal "no" (cdr (assoc :padline params
))))))
192 (with-temp-file script-file
193 (when shebang
(insert (concat shebang
"\n")))
194 (when padline
(insert "\n"))
196 (set-file-modes script-file
#o755
)
197 (org-babel-eval script-file
""))
198 (org-babel-eval org-babel-sh-command
(org-babel-trim body
)))))))
200 (let ((result-params (cdr (assoc :result-params params
))))
201 (org-babel-result-cond result-params
203 (let ((tmp-file (org-babel-temp-file "sh-")))
204 (with-temp-file tmp-file
(insert results
))
205 (org-babel-import-elisp-from-file tmp-file
)))))))
207 (defun org-babel-sh-strip-weird-long-prompt (string)
208 "Remove prompt cruft from a string of shell output."
209 (while (string-match "^% +[\r\n$]+ *" string
)
210 (setq string
(substring string
(match-end 0))))
217 ;;; ob-sh.el ends here