Merge branch 'master' of orgmode.org:org-mode
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-sh.el
blob84ec52d5721dc59496750febcb7c8b7ffc4fb806
1 ;;; ob-sh.el --- org-babel functions for shell evaluation
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.5
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating shell source code.
29 ;;; Code:
30 (require 'ob)
31 (require 'ob-comint)
32 (require 'ob-eval)
33 (require 'shell)
34 (eval-when-compile (require 'cl))
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))
40 (declare-function orgtbl-to-generic "org-table" (table params))
42 (defvar org-babel-default-header-args:sh '())
44 (defvar org-babel-sh-command "sh"
45 "Command used to invoke a shell.
46 This will be passed to `shell-command-on-region'")
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 (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'."
57 (let* ((session (org-babel-sh-initiate-session
58 (cdr (assoc :session params))))
59 (result-params (cdr (assoc :result-params params)))
60 (full-body (org-babel-expand-body:generic
61 body params (org-babel-variable-assignments:sh params))))
62 (org-babel-reassemble-table
63 (org-babel-sh-evaluate session full-body result-params)
64 (org-babel-pick-name
65 (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
66 (org-babel-pick-name
67 (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params))))))
69 (defun org-babel-prep-session:sh (session params)
70 "Prepare SESSION according to the header arguments specified in PARAMS."
71 (let* ((session (org-babel-sh-initiate-session session))
72 (var-lines (org-babel-variable-assignments:sh params)))
73 (org-babel-comint-in-buffer session
74 (mapc (lambda (var)
75 (insert var) (comint-send-input nil t)
76 (org-babel-comint-wait-for-output session)) var-lines))
77 session))
79 (defun org-babel-load-session:sh (session body params)
80 "Load BODY into SESSION."
81 (save-window-excursion
82 (let ((buffer (org-babel-prep-session:sh session params)))
83 (with-current-buffer buffer
84 (goto-char (process-mark (get-buffer-process (current-buffer))))
85 (insert (org-babel-chomp body)))
86 buffer)))
88 ;; helper functions
90 (defun org-babel-variable-assignments:sh (params)
91 "Return list of shell statements assigning the block's variables"
92 (let ((sep (cdr (assoc :separator params))))
93 (mapcar
94 (lambda (pair)
95 (format "%s=%s"
96 (car pair)
97 (org-babel-sh-var-to-sh (cdr pair) sep)))
98 (mapcar #'cdr (org-babel-get-header params :var)))))
100 (defun org-babel-sh-var-to-sh (var &optional sep)
101 "Convert an elisp value to a shell variable.
102 Convert an elisp var into a string of shell commands specifying a
103 var of the same value."
104 (if (listp var)
105 (flet ((deep-string (el)
106 (if (listp el)
107 (mapcar #'deep-string el)
108 (org-babel-sh-var-to-sh el sep))))
109 (format org-babel-sh-var-quote-fmt
110 (orgtbl-to-generic
111 (deep-string (if (listp (car var)) var (list var)))
112 (list :sep (or sep "\t")))))
113 (if (stringp var)
114 (if (string-match "[ \t\n\r]" var)
115 (format org-babel-sh-var-quote-fmt var)
116 (format "%s" var))
117 (format "%S" var))))
119 (defun org-babel-sh-table-or-results (results)
120 "Convert RESULTS to an appropriate elisp value.
121 If the results look like a table, then convert them into an
122 Emacs-lisp table, otherwise return the results as a string."
123 (org-babel-script-escape results))
125 (defun org-babel-sh-initiate-session (&optional session params)
126 "Initiate a session named SESSION according to PARAMS."
127 (when (and session (not (string= session "none")))
128 (save-window-excursion
129 (or (org-babel-comint-buffer-livep session)
130 (progn (shell session) (get-buffer (current-buffer)))))))
132 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
133 "String to indicate that evaluation has completed.")
134 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
135 "String to indicate that evaluation has completed.")
137 (defun org-babel-sh-evaluate (session body &optional result-params)
138 "Pass BODY to the Shell process in BUFFER.
139 If RESULT-TYPE equals 'output then return a list of the outputs
140 of the statements in BODY, if RESULT-TYPE equals 'value then
141 return the value of the last statement in BODY."
142 ((lambda (results)
143 (when results
144 (if (or (member "scalar" result-params)
145 (member "output" result-params))
146 results
147 (let ((tmp-file (org-babel-temp-file "sh-")))
148 (with-temp-file tmp-file (insert results))
149 (org-babel-import-elisp-from-file tmp-file)))))
150 (if (not session)
151 (org-babel-eval org-babel-sh-command (org-babel-trim body))
152 (mapconcat
153 #'org-babel-sh-strip-weird-long-prompt
154 (mapcar
155 #'org-babel-trim
156 (butlast
157 (org-babel-comint-with-output
158 (session org-babel-sh-eoe-output t body)
159 (mapc
160 (lambda (line)
161 (insert line) (comint-send-input nil t) (sleep-for 0.25))
162 (append
163 (split-string (org-babel-trim body) "\n")
164 (list org-babel-sh-eoe-indicator))))
165 2)) "\n"))))
167 (defun org-babel-sh-strip-weird-long-prompt (string)
168 "Remove prompt cruft from a string of shell output."
169 (while (string-match "^% +[\r\n$]+ *" string)
170 (setq string (substring string (match-end 0))))
171 string)
173 (provide 'ob-sh)
175 ;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
177 ;;; ob-sh.el ends here