New hook to add more properties to new agenda items, or to filter
[org-mode.git] / lisp / ob-sh.el
blobced35c0ab0e37220a2bff781f953eb236eea502c
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.01trans
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-ref-variables "ob-ref" (params))
37 (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
38 (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
39 (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
40 (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
41 (declare-function orgtbl-to-generic "org-table" (table params))
43 (defvar org-babel-default-header-args:sh '())
45 (defvar org-babel-sh-command "sh"
46 "Command used to invoke a shell.
47 This will be passed to `shell-command-on-region'")
49 (defun org-babel-expand-body:sh (body params &optional processed-params)
50 "Expand BODY according to PARAMS, return the expanded body."
51 (let ((vars (nth 1 (or processed-params (org-babel-process-params params))))
52 (sep (cdr (assoc :separator params))))
53 (concat
54 (mapconcat ;; define any variables
55 (lambda (pair)
56 (format "%s=%s"
57 (car pair)
58 (org-babel-sh-var-to-sh (cdr pair) sep)))
59 vars "\n") "\n" body "\n\n")))
61 (defun org-babel-execute:sh (body params)
62 "Execute a block of Shell commands with Babel.
63 This function is called by `org-babel-execute-src-block'."
64 (let* ((processed-params (org-babel-process-params params))
65 (session (org-babel-sh-initiate-session (nth 0 processed-params)))
66 (result-params (nth 2 processed-params))
67 (full-body (org-babel-expand-body:sh
68 body params processed-params)))
69 (org-babel-reassemble-table
70 (org-babel-sh-evaluate session full-body result-params)
71 (org-babel-pick-name
72 (nth 4 processed-params) (cdr (assoc :colnames params)))
73 (org-babel-pick-name
74 (nth 5 processed-params) (cdr (assoc :rownames params))))))
76 (defun org-babel-prep-session:sh (session params)
77 "Prepare SESSION according to the header arguments specified in PARAMS."
78 (let* ((session (org-babel-sh-initiate-session session))
79 (vars (org-babel-ref-variables params))
80 (sep (cdr (assoc :separator params)))
81 (var-lines (mapcar ;; define any variables
82 (lambda (pair)
83 (format "%s=%s"
84 (car pair)
85 (org-babel-sh-var-to-sh (cdr pair) sep)))
86 vars)))
87 (org-babel-comint-in-buffer session
88 (mapc (lambda (var)
89 (insert var) (comint-send-input nil t)
90 (org-babel-comint-wait-for-output session)) var-lines))
91 session))
93 (defun org-babel-load-session:sh (session body params)
94 "Load BODY into SESSION."
95 (save-window-excursion
96 (let ((buffer (org-babel-prep-session:sh session params)))
97 (with-current-buffer buffer
98 (goto-char (process-mark (get-buffer-process (current-buffer))))
99 (insert (org-babel-chomp body)))
100 buffer)))
102 ;; helper functions
104 (defun org-babel-sh-var-to-sh (var &optional sep)
105 "Convert an elisp value to a shell variable.
106 Convert an elisp var into a string of shell commands specifying a
107 var of the same value."
108 (if (listp var)
109 (flet ((deep-string (el)
110 (if (listp el)
111 (mapcar #'deep-string el)
112 (org-babel-sh-var-to-sh el sep))))
113 (format "$(cat <<BABEL_TABLE\n%s\nBABEL_TABLE\n)"
114 (orgtbl-to-generic
115 (deep-string var) (list :sep (or sep "\t")))))
116 (if (stringp var)
117 (if (string-match "[\n\r]" var)
118 (format "$(cat <<BABEL_STRING\n%s\nBABEL_STRING\n)" var)
119 (format "%s" var))
120 (format "%S" var))))
122 (defun org-babel-sh-table-or-results (results)
123 "Convert RESULTS to an appropriate elisp value.
124 If the results look like a table, then convert them into an
125 Emacs-lisp table, otherwise return the results as a string."
126 (org-babel-read
127 (if (string-match "^\\[.+\\]$" results)
128 (org-babel-read
129 (concat "'"
130 (replace-regexp-in-string
131 "\\[" "(" (replace-regexp-in-string
132 "\\]" ")" (replace-regexp-in-string
133 ", " " " (replace-regexp-in-string
134 "'" "\"" results))))))
135 results)))
137 (defun org-babel-sh-initiate-session (&optional session params)
138 "Initiate a session named SESSION according to PARAMS."
139 (when (and session (not (string= session "none")))
140 (save-window-excursion
141 (or (org-babel-comint-buffer-livep session)
142 (progn (shell session) (get-buffer (current-buffer)))))))
144 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
145 "String to indicate that evaluation has completed.")
146 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
147 "String to indicate that evaluation has completed.")
149 (defun org-babel-sh-evaluate (session body &optional result-params)
150 "Pass BODY to the Shell process in BUFFER.
151 If RESULT-TYPE equals 'output then return a list of the outputs
152 of the statements in BODY, if RESULT-TYPE equals 'value then
153 return the value of the last statement in BODY."
154 ((lambda (results)
155 (if (or (member "scalar" result-params)
156 (member "output" result-params))
157 results
158 (let ((tmp-file (make-temp-file "org-babel-sh")))
159 (with-temp-file tmp-file (insert results))
160 (org-babel-import-elisp-from-file tmp-file))))
161 (if (not session)
162 (org-babel-eval org-babel-sh-command (org-babel-trim body))
163 (let ((tmp-file (make-temp-file "org-babel-sh")))
164 (mapconcat
165 #'org-babel-sh-strip-weird-long-prompt
166 (mapcar
167 #'org-babel-trim
168 (butlast
169 (org-babel-comint-with-output
170 (session org-babel-sh-eoe-output t body)
171 (mapc
172 (lambda (line)
173 (insert line) (comint-send-input nil t) (sleep-for 0.25))
174 (append
175 (split-string (org-babel-trim body) "\n")
176 (list org-babel-sh-eoe-indicator))))
177 2)) "\n")))))
179 (defun org-babel-sh-strip-weird-long-prompt (string)
180 "Remove prompt cruft from a string of shell output."
181 (while (string-match "^% +[\r\n$]+ *" string)
182 (setq string (substring string (match-end 0))))
183 string)
185 (provide 'ob-sh)
187 ;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
189 ;;; ob-sh.el ends here