ob-sh.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. ;;; ob-sh.el --- org-babel functions for shell evaluation
  2. ;; Copyright (C) 2009 Eric Schulte
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Org-Babel support for evaluating shell source code.
  24. ;;; Code:
  25. (require 'ob)
  26. (require 'shell)
  27. (eval-when-compile
  28. (require 'cl))
  29. (declare-function org-babel-ref-variables "ob-ref" (params))
  30. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  31. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  32. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  33. (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
  34. (declare-function orgtbl-to-generic "org-table" (table params))
  35. (defvar org-babel-default-header-args:sh '())
  36. (defvar org-babel-sh-command "sh"
  37. "Command used to invoke a shell. This will be passed to
  38. `shell-command-on-region'")
  39. (defun org-babel-expand-body:sh (body params &optional processed-params)
  40. "Expand BODY according to PARAMS, return the expanded body."
  41. (let ((vars (nth 1 (or processed-params (org-babel-process-params params))))
  42. (sep (cdr (assoc :separator params))))
  43. (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) sep)))
  49. vars "\n") "\n" body "\n\n")))
  50. (defun org-babel-execute:sh (body params)
  51. "Execute a block of Shell commands with org-babel. This
  52. function is called by `org-babel-execute-src-block'."
  53. (message "executing Shell source code block")
  54. (let* ((processed-params (org-babel-process-params params))
  55. (session (org-babel-sh-initiate-session (nth 0 processed-params)))
  56. (result-params (nth 2 processed-params))
  57. (full-body (org-babel-expand-body:sh
  58. body params processed-params))) ;; then the source block body
  59. (org-babel-reassemble-table
  60. (org-babel-sh-evaluate session full-body result-params)
  61. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  62. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  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))
  66. (vars (org-babel-ref-variables params))
  67. (sep (cdr (assoc :separator params)))
  68. (var-lines (mapcar ;; define any variables
  69. (lambda (pair)
  70. (format "%s=%s"
  71. (car pair)
  72. (org-babel-sh-var-to-sh (cdr pair) sep)))
  73. vars)))
  74. (org-babel-comint-in-buffer session
  75. (mapc (lambda (var)
  76. (insert var) (comint-send-input nil t)
  77. (org-babel-comint-wait-for-output session)) var-lines))
  78. session))
  79. (defun org-babel-load-session:sh (session body params)
  80. "Load BODY into SESSION."
  81. (save-window-excursion
  82. (let ((buffer (org-babel-prep-session:sh session params)))
  83. (with-current-buffer buffer
  84. (goto-char (process-mark (get-buffer-process (current-buffer))))
  85. (insert (org-babel-chomp body)))
  86. buffer)))
  87. ;; helper functions
  88. (defun org-babel-sh-var-to-sh (var &optional sep)
  89. "Convert an elisp var into a string of shell commands
  90. specifying a var of the same value."
  91. (if (listp var)
  92. (flet ((deep-string (el)
  93. (if (listp el)
  94. (mapcar #'deep-string el)
  95. (org-babel-sh-var-to-sh el sep))))
  96. (format "$(cat <<BABEL_TABLE\n%s\nBABEL_TABLE\n)"
  97. (orgtbl-to-generic (deep-string var) (list :sep (or sep "\t")))))
  98. (if (stringp var) (format "%s" var) (format "%S" var))))
  99. (defun org-babel-sh-table-or-results (results)
  100. "If the results look like a table, then convert them into an
  101. Emacs-lisp table, otherwise return the results as a string."
  102. (org-babel-read
  103. (if (string-match "^\\[.+\\]$" results)
  104. (org-babel-read
  105. (concat "'"
  106. (replace-regexp-in-string
  107. "\\[" "(" (replace-regexp-in-string
  108. "\\]" ")" (replace-regexp-in-string
  109. ", " " " (replace-regexp-in-string
  110. "'" "\"" results))))))
  111. results)))
  112. (defun org-babel-sh-initiate-session (&optional session params)
  113. "Initiate a session named SESSION according to PARAMS."
  114. (when (and session (not (string= session "none")))
  115. (save-window-excursion
  116. (or (org-babel-comint-buffer-livep session)
  117. (progn (shell session) (get-buffer (current-buffer)))))))
  118. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  119. "Used to indicate that evaluation is has completed.")
  120. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  121. "Used to indicate that evaluation is has completed.")
  122. (defun org-babel-sh-evaluate (session body &optional result-params)
  123. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  124. 'output then return a list of the outputs of the statements in
  125. BODY, if RESULT-TYPE equals 'value then return the value of the
  126. last statement in BODY."
  127. (if (not session)
  128. ;; external process evaluation
  129. (save-window-excursion
  130. (with-temp-buffer
  131. (insert body)
  132. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  133. (org-babel-shell-command-on-region (point-min) (point-max) org-babel-sh-command 'current-buffer 'replace)
  134. (cond
  135. ((member "output" result-params) (buffer-string))
  136. ;; TODO: figure out how to return non-output values from shell scripts
  137. (t ;; if not "output" then treat as "value"
  138. (if (member "scalar" result-params)
  139. (buffer-string)
  140. (let ((tmp-file (make-temp-file "org-babel-sh"))
  141. (results (buffer-string)))
  142. (with-temp-file tmp-file (insert results))
  143. (org-babel-import-elisp-from-file tmp-file)))))))
  144. ;; comint session evaluation
  145. (flet ((strip-empty (lst)
  146. (delq nil (mapcar (lambda (el) (unless (= (length el) 0) el)) lst))))
  147. (let ((tmp-file (make-temp-file "org-babel-sh"))
  148. (results
  149. (cdr (member
  150. org-babel-sh-eoe-output
  151. (strip-empty
  152. (reverse
  153. (mapcar #'org-babel-sh-strip-weird-long-prompt
  154. (mapcar #'org-babel-trim
  155. (org-babel-comint-with-output
  156. (session org-babel-sh-eoe-output t body)
  157. (mapc (lambda (line) (insert line) (comint-send-input))
  158. (strip-empty (split-string body "\n")))
  159. (insert org-babel-sh-eoe-indicator)
  160. (comint-send-input))))))))))
  161. ;; (message (replace-regexp-in-string
  162. ;; "%" "%%" (format "processed-results=%S" results))) ;; debugging
  163. (or (and results
  164. (cond
  165. ((member "output" result-params)
  166. (org-babel-trim (mapconcat #'org-babel-trim
  167. (reverse results) "\n")))
  168. (t ;; if not "output" then treat as "value"
  169. (with-temp-file tmp-file
  170. (insert (car results)) (insert "\n"))
  171. (org-babel-import-elisp-from-file tmp-file))))
  172. "")))))
  173. (defun org-babel-sh-strip-weird-long-prompt (string)
  174. "Remove prompt cruft from a string of shell output."
  175. (while (string-match "^% +[\r\n$]+ *" string)
  176. (setq string (substring string (match-end 0))))
  177. string)
  178. (provide 'ob-sh)
  179. ;;; ob-sh.el ends here