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