org-agenda.el: Fix buglets
[org-mode.git] / lisp / ob-sh.el
blob96f275bc87acf8db741e6e25b1da9119c91d32db
1 ;;; ob-sh.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 (defvar org-babel-sh-command "sh"
42 "Command used to invoke a shell.
43 This will be passed to `shell-command-on-region'")
45 (defcustom org-babel-sh-var-quote-fmt
46 "$(cat <<'BABEL_TABLE'\n%s\nBABEL_TABLE\n)"
47 "Format string used to escape variables when passed to shell scripts."
48 :group 'org-babel
49 :type 'string)
51 (defun org-babel-execute:sh (body params)
52 "Execute a block of Shell commands with Babel.
53 This function is called by `org-babel-execute-src-block'."
54 (let* ((session (org-babel-sh-initiate-session
55 (cdr (assoc :session params))))
56 (stdin (let ((stdin (cdr (assoc :stdin params))))
57 (when stdin (org-babel-sh-var-to-string
58 (org-babel-ref-resolve stdin)))))
59 (full-body (org-babel-expand-body:generic
60 body params (org-babel-variable-assignments:sh params))))
61 (org-babel-reassemble-table
62 (org-babel-sh-evaluate session full-body params stdin)
63 (org-babel-pick-name
64 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
65 (org-babel-pick-name
66 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
68 (defun org-babel-prep-session:sh (session params)
69 "Prepare SESSION according to the header arguments specified in PARAMS."
70 (let* ((session (org-babel-sh-initiate-session session))
71 (var-lines (org-babel-variable-assignments:sh params)))
72 (org-babel-comint-in-buffer session
73 (mapc (lambda (var)
74 (insert var) (comint-send-input nil t)
75 (org-babel-comint-wait-for-output session)) var-lines))
76 session))
78 (defun org-babel-load-session:sh (session body params)
79 "Load BODY into SESSION."
80 (save-window-excursion
81 (let ((buffer (org-babel-prep-session:sh session params)))
82 (with-current-buffer buffer
83 (goto-char (process-mark (get-buffer-process (current-buffer))))
84 (insert (org-babel-chomp body)))
85 buffer)))
87 ;; helper functions
89 (defun org-babel-variable-assignments:sh (params)
90 "Return list of shell statements assigning the block's variables."
91 (let ((sep (cdr (assoc :separator params))))
92 (mapcar
93 (lambda (pair)
94 (format "%s=%s"
95 (car pair)
96 (org-babel-sh-var-to-sh (cdr pair) sep)))
97 (mapcar #'cdr (org-babel-get-header params :var)))))
99 (defun org-babel-sh-var-to-sh (var &optional sep)
100 "Convert an elisp value to a shell variable.
101 Convert an elisp var into a string of shell commands specifying a
102 var of the same value."
103 (format org-babel-sh-var-quote-fmt (org-babel-sh-var-to-string var sep)))
105 (defun org-babel-sh-var-to-string (var &optional sep)
106 "Convert an elisp value to a string."
107 (let ((echo-var (lambda (v) (if (stringp v) v (format "%S" v)))))
108 (cond
109 ((and (listp var) (or (listp (car var)) (equal (car var) 'hline)))
110 (orgtbl-to-generic var (list :sep (or sep "\t") :fmt echo-var)))
111 ((listp var)
112 (mapconcat echo-var var "\n"))
113 (t (funcall echo-var var)))))
115 (defun org-babel-sh-table-or-results (results)
116 "Convert RESULTS to an appropriate elisp value.
117 If the results look like a table, then convert them into an
118 Emacs-lisp table, otherwise return the results as a string."
119 (org-babel-script-escape results))
121 (defun org-babel-sh-initiate-session (&optional session params)
122 "Initiate a session named SESSION according to PARAMS."
123 (when (and session (not (string= session "none")))
124 (save-window-excursion
125 (or (org-babel-comint-buffer-livep session)
126 (progn (shell session) (get-buffer (current-buffer)))))))
128 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
129 "String to indicate that evaluation has completed.")
130 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
131 "String to indicate that evaluation has completed.")
133 (defun org-babel-sh-evaluate (session body &optional params stdin)
134 "Pass BODY to the Shell process in BUFFER.
135 If RESULT-TYPE equals 'output then return a list of the outputs
136 of the statements in BODY, if RESULT-TYPE equals 'value then
137 return the value of the last statement in BODY."
138 (let ((results
139 (cond
140 (stdin ; external shell script w/STDIN
141 (let ((script-file (org-babel-temp-file "sh-script-"))
142 (stdin-file (org-babel-temp-file "sh-stdin-"))
143 (shebang (cdr (assoc :shebang params)))
144 (padline (not (string= "no" (cdr (assoc :padline params))))))
145 (with-temp-file script-file
146 (when shebang (insert (concat shebang "\n")))
147 (when padline (insert "\n"))
148 (insert body))
149 (set-file-modes script-file #o755)
150 (with-temp-file stdin-file (insert stdin))
151 (with-temp-buffer
152 (call-process-shell-command
153 (if shebang
154 script-file
155 (format "%s %s" org-babel-sh-command script-file))
156 stdin-file
157 (current-buffer))
158 (buffer-string))))
159 (session ; session evaluation
160 (mapconcat
161 #'org-babel-sh-strip-weird-long-prompt
162 (mapcar
163 #'org-babel-trim
164 (butlast
165 (org-babel-comint-with-output
166 (session org-babel-sh-eoe-output t body)
167 (mapc
168 (lambda (line)
169 (insert line)
170 (comint-send-input nil t)
171 (while (save-excursion
172 (goto-char comint-last-input-end)
173 (not (re-search-forward
174 comint-prompt-regexp nil t)))
175 (accept-process-output
176 (get-buffer-process (current-buffer)))))
177 (append
178 (split-string (org-babel-trim body) "\n")
179 (list org-babel-sh-eoe-indicator))))
180 2)) "\n"))
181 ('otherwise ; external shell script
182 (if (and (cdr (assoc :shebang params))
183 (> (length (cdr (assoc :shebang params))) 0))
184 (let ((script-file (org-babel-temp-file "sh-script-"))
185 (shebang (cdr (assoc :shebang params)))
186 (padline (not (equal "no" (cdr (assoc :padline params))))))
187 (with-temp-file script-file
188 (when shebang (insert (concat shebang "\n")))
189 (when padline (insert "\n"))
190 (insert body))
191 (set-file-modes script-file #o755)
192 (org-babel-eval script-file ""))
193 (org-babel-eval org-babel-sh-command (org-babel-trim body)))))))
194 (when results
195 (let ((result-params (cdr (assoc :result-params params))))
196 (org-babel-result-cond result-params
197 results
198 (let ((tmp-file (org-babel-temp-file "sh-")))
199 (with-temp-file tmp-file (insert results))
200 (org-babel-import-elisp-from-file tmp-file)))))))
202 (defun org-babel-sh-strip-weird-long-prompt (string)
203 "Remove prompt cruft from a string of shell output."
204 (while (string-match "^% +[\r\n$]+ *" string)
205 (setq string (substring string (match-end 0))))
206 string)
208 (provide 'ob-sh)
212 ;;; ob-sh.el ends here