ob-sh.el 8.3 KB

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