1 ;;; org-babel-sh.el --- org-babel functions for shell evaluation
3 ;; Copyright (C) 2009 Eric Schulte
5 ;; Author: Eric Schulte
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Org-Babel support for evaluating shell source code.
35 (org-babel-add-interpreter "sh")
37 (add-to-list 'org-babel-tangle-langs
'("sh" "sh" "#!/usr/bin/env sh"))
39 (defun org-babel-execute:sh
(body params
)
40 "Execute a block of Shell commands with org-babel. This
41 function is called by `org-babel-execute-src-block'."
42 (message "executing Shell source code block")
43 (let* ((processed-params (org-babel-process-params params
))
44 (session (org-babel-sh-initiate-session (first processed-params
)))
45 (vars (second processed-params
))
46 (result-type (fourth processed-params
))
48 (mapconcat ;; define any variables
52 (org-babel-sh-var-to-sh (cdr pair
))))
53 vars
"\n") "\n" body
"\n\n"))) ;; then the source block body
54 (org-babel-sh-evaluate session full-body result-type
)))
56 (defun org-babel-prep-session:sh
(session params
)
57 "Prepare SESSION according to the header arguments specified in PARAMS."
58 (let* ((session (org-babel-sh-initiate-session session
))
59 (vars (org-babel-ref-variables params
))
60 (var-lines (mapcar ;; define any variables
64 (org-babel-sh-var-to-sh (cdr pair
))))
66 (org-babel-comint-in-buffer session
68 (insert var
) (comint-send-input nil t
)
69 (org-babel-comint-wait-for-output session
)) var-lines
))))
73 (defun org-babel-sh-var-to-sh (var)
74 "Convert an elisp var into a string of shell commands
75 specifying a var of the same value."
77 (concat "[" (mapconcat #'org-babel-sh-var-to-sh var
", ") "]")
80 (defun org-babel-sh-table-or-results (results)
81 "If the results look like a table, then convert them into an
82 Emacs-lisp table, otherwise return the results as a string."
84 (if (string-match "^\\[.+\\]$" results
)
86 (replace-regexp-in-string
87 "\\[" "(" (replace-regexp-in-string
88 "\\]" ")" (replace-regexp-in-string
89 ", " " " (replace-regexp-in-string
93 (defun org-babel-sh-initiate-session (&optional session
)
94 (unless (string= session
"none")
95 (save-window-excursion
96 (or (org-babel-comint-buffer-livep session
)
97 (progn (shell session
) (get-buffer (current-buffer)))))))
99 (defvar org-babel-sh-eoe-indicator
"echo 'org_babel_sh_eoe'"
100 "Used to indicate that evaluation is has completed.")
101 (defvar org-babel-sh-eoe-output
"org_babel_sh_eoe"
102 "Used to indicate that evaluation is has completed.")
104 (defun org-babel-sh-evaluate (session body
&optional result-type
)
105 "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
106 'output then return a list of the outputs of the statements in
107 BODY, if RESULT-TYPE equals 'value then return the value of the
108 last statement in BODY."
110 ;; external process evaluation
111 (save-window-excursion
114 ;; (message "buffer=%s" (buffer-string)) ;; debugging
115 (shell-command-on-region (point-min) (point-max) "sh" 'replace
)
117 (output (buffer-string))
118 (value ;; TODO: figure out how to return non-output values from shell scripts
119 (let ((tmp-file (make-temp-file "org-babel-sh"))
120 (results (buffer-string)))
121 (with-temp-file tmp-file
(insert results
))
122 (org-babel-import-elisp-from-file tmp-file
))))))
123 ;; comint session evaluation
124 (flet ((strip-empty (lst)
125 (delq nil
(mapcar (lambda (el) (unless (= (length el
) 0) el
)) lst
))))
126 (let ((tmp-file (make-temp-file "org-babel-sh"))
129 org-babel-sh-eoe-output
132 (mapcar #'org-babel-sh-strip-weird-long-prompt
133 (mapcar #'org-babel-trim
134 (org-babel-comint-with-output
135 session org-babel-sh-eoe-output t
136 (mapc (lambda (line) (insert line
) (comint-send-input))
137 (strip-empty (split-string body
"\n")))
138 (insert org-babel-sh-eoe-indicator
)
139 (comint-send-input))))))))))
140 ;; (message (replace-regexp-in-string
141 ;; "%" "%%" (format "processed-results=%S" results))) ;; debugging
144 (output (org-babel-trim (mapconcat #'org-babel-trim
145 (reverse results
) "\n")))
146 (value (with-temp-file tmp-file
147 (insert (car results
)) (insert "\n"))
148 (org-babel-import-elisp-from-file tmp-file
))))
151 (defun org-babel-sh-strip-weird-long-prompt (string)
152 (while (string-match "^% +[\r\n$]+ *" string
)
153 (setq string
(substring string
(match-end 0))))
156 (provide 'org-babel-sh
)
157 ;;; org-babel-sh.el ends here