add :cmdline support to shell code blocks
[org-mode/org-tableheadings.git] / lisp / ob-shell.el
blob0560f4817654b7cc4251b636081f0af6887f6e30
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
109 (defun org-babel-variable-assignments:sh (params)
110 "Return list of shell statements assigning the block's variables."
111 (let ((sep (cdr (assoc :separator params))))
112 (mapcar
113 (lambda (pair)
114 (format "%s=%s"
115 (car pair)
116 (org-babel-sh-var-to-sh (cdr pair) sep)))
117 (mapcar #'cdr (org-babel-get-header params :var)))))
119 (defun org-babel-sh-var-to-sh (var &optional sep)
120 "Convert an elisp value to a shell variable.
121 Convert an elisp var into a string of shell commands specifying a
122 var of the same value."
123 (format org-babel-sh-var-quote-fmt (org-babel-sh-var-to-string var sep)))
125 (defun org-babel-sh-var-to-string (var &optional sep)
126 "Convert an elisp value to a string."
127 (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
128 (cond
129 ((and (listp var) (or (listp (car var)) (equal (car var) 'hline)))
130 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
131 ((listp var)
132 (mapconcat echo-var var "\n"))
133 (t (funcall echo-var var)))))
135 (defun org-babel-sh-table-or-results (results)
136 "Convert RESULTS to an appropriate elisp value.
137 If the results look like a table, then convert them into an
138 Emacs-lisp table, otherwise return the results as a string."
139 (org-babel-script-escape results))
141 (defun org-babel-sh-initiate-session (&optional session params)
142 "Initiate a session named SESSION according to PARAMS."
143 (when (and session (not (string= session "none")))
144 (save-window-excursion
145 (or (org-babel-comint-buffer-livep session)
146 (progn (shell session) (get-buffer (current-buffer)))))))
148 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
149 "String to indicate that evaluation has completed.")
150 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
151 "String to indicate that evaluation has completed.")
153 (defun org-babel-sh-evaluate (session body &optional params stdin cmdline)
154 "Pass BODY to the Shell process in BUFFER.
155 If RESULT-TYPE equals 'output then return a list of the outputs
156 of the statements in BODY, if RESULT-TYPE equals 'value then
157 return the value of the last statement in BODY."
158 (let ((results
159 (cond
160 ((or stdin cmdline) ; external shell script w/STDIN
161 (let ((script-file (org-babel-temp-file "sh-script-"))
162 (stdin-file (org-babel-temp-file "sh-stdin-"))
163 (shebang (cdr (assoc :shebang params)))
164 (padline (not (string= "no" (cdr (assoc :padline params))))))
165 (with-temp-file script-file
166 (when shebang (insert (concat shebang "\n")))
167 (when padline (insert "\n"))
168 (insert body))
169 (set-file-modes script-file #o755)
170 (with-temp-file stdin-file (insert (or stdin "")))
171 (with-temp-buffer
172 (call-process-shell-command
173 (if shebang
174 script-file
175 (format "%s %s" org-babel-sh-command script-file))
176 stdin-file
177 (current-buffer) nil cmdline)
178 (buffer-string))))
179 (session ; session evaluation
180 (mapconcat
181 #'org-babel-sh-strip-weird-long-prompt
182 (mapcar
183 #'org-babel-trim
184 (butlast
185 (org-babel-comint-with-output
186 (session org-babel-sh-eoe-output t body)
187 (mapc
188 (lambda (line)
189 (insert line)
190 (comint-send-input nil t)
191 (while (save-excursion
192 (goto-char comint-last-input-end)
193 (not (re-search-forward
194 comint-prompt-regexp nil t)))
195 (accept-process-output
196 (get-buffer-process (current-buffer)))))
197 (append
198 (split-string (org-babel-trim body) "\n")
199 (list org-babel-sh-eoe-indicator))))
200 2)) "\n"))
201 ('otherwise ; external shell script
202 (if (and (cdr (assoc :shebang params))
203 (> (length (cdr (assoc :shebang params))) 0))
204 (let ((script-file (org-babel-temp-file "sh-script-"))
205 (shebang (cdr (assoc :shebang params)))
206 (padline (not (equal "no" (cdr (assoc :padline params))))))
207 (with-temp-file script-file
208 (when shebang (insert (concat shebang "\n")))
209 (when padline (insert "\n"))
210 (insert body))
211 (set-file-modes script-file #o755)
212 (org-babel-eval script-file ""))
213 (org-babel-eval org-babel-sh-command (org-babel-trim body)))))))
214 (when results
215 (let ((result-params (cdr (assoc :result-params params))))
216 (org-babel-result-cond result-params
217 results
218 (let ((tmp-file (org-babel-temp-file "sh-")))
219 (with-temp-file tmp-file (insert results))
220 (org-babel-import-elisp-from-file tmp-file)))))))
222 (defun org-babel-sh-strip-weird-long-prompt (string)
223 "Remove prompt cruft from a string of shell output."
224 (while (string-match "^% +[\r\n$]+ *" string)
225 (setq string (substring string (match-end 0))))
226 string)
228 (provide 'ob-shell)
232 ;;; ob-shell.el ends here