Fix some Org errors revealed by `make check-declare'.
[bpt/emacs.git] / lisp / org / ob-sh.el
CommitLineData
86fbb8ca
CD
1;;; ob-sh.el --- org-babel functions for shell evaluation
2
3;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
8;; Version: 7.01
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25;;; Commentary:
26
27;; Org-Babel support for evaluating shell source code.
28
29;;; Code:
30(require 'ob)
31(require 'ob-comint)
32(require 'ob-eval)
33(require 'shell)
34(eval-when-compile (require 'cl))
35
36(declare-function org-babel-ref-variables "ob-ref" (params))
86fbb8ca
CD
37(declare-function orgtbl-to-generic "org-table" (table params))
38
39(defvar org-babel-default-header-args:sh '())
40
41(defvar org-babel-sh-command "sh"
42 "Command used to invoke a shell.
43This will be passed to `shell-command-on-region'")
44
45(defun org-babel-expand-body:sh (body params &optional processed-params)
46 "Expand BODY according to PARAMS, return the expanded body."
47 (let ((vars (nth 1 (or processed-params (org-babel-process-params params))))
48 (sep (cdr (assoc :separator params))))
49 (concat
50 (mapconcat ;; define any variables
51 (lambda (pair)
52 (format "%s=%s"
53 (car pair)
54 (org-babel-sh-var-to-sh (cdr pair) sep)))
55 vars "\n") "\n" body "\n\n")))
56
57(defun org-babel-execute:sh (body params)
58 "Execute a block of Shell commands with Babel.
59This function is called by `org-babel-execute-src-block'."
60 (let* ((processed-params (org-babel-process-params params))
61 (session (org-babel-sh-initiate-session (nth 0 processed-params)))
62 (result-params (nth 2 processed-params))
63 (full-body (org-babel-expand-body:sh
64 body params processed-params)))
65 (org-babel-reassemble-table
66 (org-babel-sh-evaluate session full-body result-params)
67 (org-babel-pick-name
68 (nth 4 processed-params) (cdr (assoc :colnames params)))
69 (org-babel-pick-name
70 (nth 5 processed-params) (cdr (assoc :rownames params))))))
71
72(defun org-babel-prep-session:sh (session params)
73 "Prepare SESSION according to the header arguments specified in PARAMS."
74 (let* ((session (org-babel-sh-initiate-session session))
75 (vars (org-babel-ref-variables params))
76 (sep (cdr (assoc :separator params)))
77 (var-lines (mapcar ;; define any variables
78 (lambda (pair)
79 (format "%s=%s"
80 (car pair)
81 (org-babel-sh-var-to-sh (cdr pair) sep)))
82 vars)))
83 (org-babel-comint-in-buffer session
84 (mapc (lambda (var)
85 (insert var) (comint-send-input nil t)
86 (org-babel-comint-wait-for-output session)) var-lines))
87 session))
88
89(defun org-babel-load-session:sh (session body params)
90 "Load BODY into SESSION."
91 (save-window-excursion
92 (let ((buffer (org-babel-prep-session:sh session params)))
93 (with-current-buffer buffer
94 (goto-char (process-mark (get-buffer-process (current-buffer))))
95 (insert (org-babel-chomp body)))
96 buffer)))
97
98;; helper functions
99
100(defun org-babel-sh-var-to-sh (var &optional sep)
101 "Convert an elisp value to a shell variable.
102Convert an elisp var into a string of shell commands specifying a
103var 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 "$(cat <<BABEL_TABLE\n%s\nBABEL_TABLE\n)"
110 (orgtbl-to-generic
111 (deep-string var) (list :sep (or sep "\t")))))
112 (if (stringp var)
113 (if (string-match "[\n\r]" var)
114 (format "$(cat <<BABEL_STRING\n%s\nBABEL_STRING\n)" var)
115 (format "%s" var))
116 (format "%S" var))))
117
118(defun org-babel-sh-table-or-results (results)
119 "Convert RESULTS to an appropriate elisp value.
120If the results look like a table, then convert them into an
121Emacs-lisp table, otherwise return the results as a string."
122 (org-babel-read
123 (if (string-match "^\\[.+\\]$" results)
124 (org-babel-read
125 (concat "'"
126 (replace-regexp-in-string
127 "\\[" "(" (replace-regexp-in-string
128 "\\]" ")" (replace-regexp-in-string
129 ", " " " (replace-regexp-in-string
130 "'" "\"" results))))))
131 results)))
132
133(defun org-babel-sh-initiate-session (&optional session params)
134 "Initiate a session named SESSION according to PARAMS."
135 (when (and session (not (string= session "none")))
136 (save-window-excursion
137 (or (org-babel-comint-buffer-livep session)
138 (progn (shell session) (get-buffer (current-buffer)))))))
139
140(defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
141 "String to indicate that evaluation has completed.")
142(defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
143 "String to indicate that evaluation has completed.")
144
145(defun org-babel-sh-evaluate (session body &optional result-params)
146 "Pass BODY to the Shell process in BUFFER.
147If RESULT-TYPE equals 'output then return a list of the outputs
148of the statements in BODY, if RESULT-TYPE equals 'value then
149return the value of the last statement in BODY."
150 ((lambda (results)
151 (if (or (member "scalar" result-params)
152 (member "output" result-params))
153 results
154 (let ((tmp-file (make-temp-file "org-babel-sh")))
155 (with-temp-file tmp-file (insert results))
156 (org-babel-import-elisp-from-file tmp-file))))
157 (if (not session)
158 (org-babel-eval org-babel-sh-command (org-babel-trim body))
159 (let ((tmp-file (make-temp-file "org-babel-sh")))
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) (comint-send-input nil t) (sleep-for 0.25))
170 (append
171 (split-string (org-babel-trim body) "\n")
172 (list org-babel-sh-eoe-indicator))))
173 2)) "\n")))))
174
175(defun org-babel-sh-strip-weird-long-prompt (string)
176 "Remove prompt cruft from a string of shell output."
177 (while (string-match "^% +[\r\n$]+ *" string)
178 (setq string (substring string (match-end 0))))
179 string)
180
181(provide 'ob-sh)
182
183;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
184
185;;; ob-sh.el ends here