Haskell needs more work on the differences between interactive and non-interactive...
[org-mode.git] / lisp / langs / org-babel-sh.el
blob8bc54f7b52f7cdb381051e0904a5f93431b7ba48
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
29 ;; Org-Babel support for evaluating shell source code.
31 ;;; Code:
32 (require 'org-babel)
33 (require 'shell)
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' via multiple-value-bind."
42 (message "executing Shell source code block")
43 (let* ((full-body (concat
44 (mapconcat ;; define any variables
45 (lambda (pair)
46 (format "%s=%s"
47 (car pair)
48 (org-babel-sh-var-to-sh (cdr pair))))
49 vars "\n") "\n" body "\n\n")) ;; then the source block body
50 (session (org-babel-sh-initiate-session session)))
51 (org-babel-sh-evaluate session full-body result-type)))
53 (defun org-babel-prep-session:sh (session params)
54 "Prepare SESSION according to the header arguments specified in PARAMS."
55 (let* ((session (org-babel-sh-initiate-session session))
56 (vars (org-babel-ref-variables params))
57 (var-lines (mapcar ;; define any variables
58 (lambda (pair)
59 (format "%s=%s"
60 (car pair)
61 (org-babel-sh-var-to-sh (cdr pair))))
62 vars)))
63 (org-babel-comint-in-buffer session
64 (mapc (lambda (var)
65 (insert var) (comint-send-input nil t)
66 (org-babel-comint-wait-for-output session)) var-lines))))
68 ;; helper functions
70 (defun org-babel-sh-var-to-sh (var)
71 "Convert an elisp var into a string of shell commands
72 specifying a var of the same value."
73 (if (listp var)
74 (concat "[" (mapconcat #'org-babel-sh-var-to-sh var ", ") "]")
75 (format "%S" var)))
77 (defun org-babel-sh-table-or-results (results)
78 "If the results look like a table, then convert them into an
79 Emacs-lisp table, otherwise return the results as a string."
80 (org-babel-read
81 (if (string-match "^\\[.+\\]$" results)
82 (org-babel-read
83 (replace-regexp-in-string
84 "\\[" "(" (replace-regexp-in-string
85 "\\]" ")" (replace-regexp-in-string
86 ", " " " (replace-regexp-in-string
87 "'" "\"" results)))))
88 results)))
90 (defvar org-babel-sh-buffers '(:default . nil))
92 (defun org-babel-sh-session-buffer (session)
93 (cdr (assoc session org-babel-sh-buffers)))
95 (defun org-babel-sh-initiate-session-by-key (&optional session)
96 "If there is not a current inferior-process-buffer in SESSION
97 then create. Return the initialized session."
98 (save-window-excursion
99 (let* ((session (if session (intern session) :default))
100 (sh-buffer (org-babel-sh-session-buffer session))
101 (newp (not (org-babel-comint-buffer-livep sh-buffer))))
102 (if (and sh-buffer (get-buffer sh-buffer) (not (buffer-live-p sh-buffer)))
103 (setq sh-buffer nil))
104 (shell sh-buffer)
105 (when newp
106 (setq sh-buffer (current-buffer))
107 (org-babel-comint-wait-for-output sh-buffer))
108 (setq org-babel-sh-buffers (cons (cons session sh-buffer)
109 (assq-delete-all session org-babel-sh-buffers)))
110 session)))
112 (defun org-babel-sh-initiate-session (&optional session)
113 (unless (string= session "none")
114 (org-babel-sh-session-buffer (org-babel-sh-initiate-session-by-key session))))
116 (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
117 "Used to indicate that evaluation is has completed.")
118 (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
119 "Used to indicate that evaluation is has completed.")
121 (defun org-babel-sh-evaluate (buffer body &optional result-type)
122 "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
123 'output then return a list of the outputs of the statements in
124 BODY, if RESULT-TYPE equals 'value then return the value of the
125 last statement in BODY."
126 (if (not session)
127 ;; external process evaluation
128 (save-window-excursion
129 (with-temp-buffer
130 (insert body)
131 ;; (message "buffer=%s" (buffer-string)) ;; debugging
132 (shell-command-on-region (point-min) (point-max) "sh" 'replace)
133 (case result-type
134 (output (buffer-string))
135 (value ;; TODO: figure out how to return non-output values from shell scripts
136 (let ((tmp-file (make-temp-file "org-babel-sh"))
137 (results (buffer-string)))
138 (with-temp-file tmp-file (insert results))
139 (org-babel-import-elisp-from-file tmp-file))))))
140 ;; comint session evaluation
141 (let* ((tmp-file (make-temp-file "org-babel-sh"))
142 (full-body (mapconcat #'org-babel-chomp
143 (list body org-babel-sh-eoe-indicator) "\n"))
144 (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
145 (insert full-body) (comint-send-input nil t)))
146 (results (cdr (member org-babel-sh-eoe-output
147 (reverse (mapcar #'org-babel-sh-strip-weird-long-prompt
148 (mapcar #'org-babel-trim raw)))))))
149 ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
150 (or (case result-type
151 (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
152 (value (with-temp-file tmp-file (insert (car results)))
153 (org-babel-import-elisp-from-file tmp-file)))) "")))
155 (defun org-babel-sh-strip-weird-long-prompt (string)
156 (while (string-match "^% +[\r\n$]+ *" string)
157 (setq string (substring string (match-end 0))))
158 string)
160 (provide 'org-babel-sh)
161 ;;; org-babel-sh.el ends here