ob-sh.el 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  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-sh-command "sh"
  36. "Command used to invoke a shell. This will be passed to
  37. `shell-command-on-region'")
  38. (defun org-babel-expand-body:sh (body params &optional processed-params)
  39. "Expand BODY according to PARAMS, return the expanded body."
  40. (let ((vars (nth 1 (or processed-params (org-babel-process-params params))))
  41. (sep (cdr (assoc :separator params))))
  42. (concat
  43. (mapconcat ;; define any variables
  44. (lambda (pair)
  45. (format "%s=%s"
  46. (car pair)
  47. (org-babel-sh-var-to-sh (cdr pair) sep)))
  48. vars "\n") "\n" body "\n\n")))
  49. (defun org-babel-execute:sh (body params)
  50. "Execute a block of Shell commands with org-babel. This
  51. function is called by `org-babel-execute-src-block'."
  52. (message "executing Shell source code block")
  53. (let* ((processed-params (org-babel-process-params params))
  54. (session (org-babel-sh-initiate-session (nth 0 processed-params)))
  55. (result-params (nth 2 processed-params))
  56. (full-body (org-babel-expand-body:sh
  57. body params processed-params))) ;; then the source block body
  58. (org-babel-reassemble-table
  59. (org-babel-sh-evaluate session full-body result-params)
  60. (org-babel-pick-name (nth 4 processed-params) (cdr (assoc :colnames params)))
  61. (org-babel-pick-name (nth 5 processed-params) (cdr (assoc :rownames params))))))
  62. (defun org-babel-prep-session:sh (session params)
  63. "Prepare SESSION according to the header arguments specified in PARAMS."
  64. (let* ((session (org-babel-sh-initiate-session session))
  65. (vars (org-babel-ref-variables params))
  66. (sep (cdr (assoc :separator params)))
  67. (var-lines (mapcar ;; define any variables
  68. (lambda (pair)
  69. (format "%s=%s"
  70. (car pair)
  71. (org-babel-sh-var-to-sh (cdr pair) sep)))
  72. vars)))
  73. (org-babel-comint-in-buffer session
  74. (mapc (lambda (var)
  75. (insert var) (comint-send-input nil t)
  76. (org-babel-comint-wait-for-output session)) var-lines))
  77. session))
  78. (defun org-babel-load-session:sh (session body params)
  79. "Load BODY into SESSION."
  80. (save-window-excursion
  81. (let ((buffer (org-babel-prep-session:sh session params)))
  82. (with-current-buffer buffer
  83. (goto-char (process-mark (get-buffer-process (current-buffer))))
  84. (insert (org-babel-chomp body)))
  85. buffer)))
  86. ;; helper functions
  87. (defun org-babel-sh-var-to-sh (var &optional sep)
  88. "Convert an elisp var into a string of shell commands
  89. specifying a var of the same value."
  90. (if (listp var)
  91. (flet ((deep-string (el)
  92. (if (listp el)
  93. (mapcar #'deep-string el)
  94. (org-babel-sh-var-to-sh el sep))))
  95. (format "$(cat <<BABEL_TABLE\n%s\nBABEL_TABLE\n)"
  96. (orgtbl-to-generic (deep-string var) (list :sep (or sep "\t")))))
  97. (if (stringp var) (format "%s" var) (format "%S" var))))
  98. (defun org-babel-sh-table-or-results (results)
  99. "If the results look like a table, then convert them into an
  100. Emacs-lisp table, otherwise return the results as a string."
  101. (org-babel-read
  102. (if (string-match "^\\[.+\\]$" results)
  103. (org-babel-read
  104. (concat "'"
  105. (replace-regexp-in-string
  106. "\\[" "(" (replace-regexp-in-string
  107. "\\]" ")" (replace-regexp-in-string
  108. ", " " " (replace-regexp-in-string
  109. "'" "\"" results))))))
  110. results)))
  111. (defun org-babel-sh-initiate-session (&optional session params)
  112. "Initiate a session named SESSION according to PARAMS."
  113. (when (and session (not (string= session "none")))
  114. (save-window-excursion
  115. (or (org-babel-comint-buffer-livep session)
  116. (progn (shell session) (get-buffer (current-buffer)))))))
  117. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  118. "Used to indicate that evaluation is has completed.")
  119. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  120. "Used to indicate that evaluation is has completed.")
  121. (defun org-babel-sh-evaluate (session body &optional result-params)
  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. (org-babel-shell-command-on-region (point-min) (point-max) org-babel-sh-command 'current-buffer 'replace)
  133. (cond
  134. ((member "output" result-params) (buffer-string))
  135. ;; TODO: figure out how to return non-output values from shell scripts
  136. (t ;; if not "output" then treat as "value"
  137. (if (member "scalar" result-params)
  138. (buffer-string)
  139. (let ((tmp-file (make-temp-file "org-babel-sh"))
  140. (results (buffer-string)))
  141. (with-temp-file tmp-file (insert results))
  142. (org-babel-import-elisp-from-file tmp-file)))))))
  143. ;; comint session evaluation
  144. (flet ((strip-empty (lst)
  145. (delq nil (mapcar (lambda (el) (unless (= (length el) 0) el)) lst))))
  146. (let ((tmp-file (make-temp-file "org-babel-sh"))
  147. (results
  148. (cdr (member
  149. org-babel-sh-eoe-output
  150. (strip-empty
  151. (reverse
  152. (mapcar #'org-babel-sh-strip-weird-long-prompt
  153. (mapcar #'org-babel-trim
  154. (org-babel-comint-with-output
  155. (session org-babel-sh-eoe-output t body)
  156. (mapc (lambda (line) (insert line) (comint-send-input))
  157. (strip-empty (split-string body "\n")))
  158. (insert org-babel-sh-eoe-indicator)
  159. (comint-send-input))))))))))
  160. ;; (message (replace-regexp-in-string
  161. ;; "%" "%%" (format "processed-results=%S" results))) ;; debugging
  162. (or (and results
  163. (cond
  164. ((member "output" result-params)
  165. (org-babel-trim (mapconcat #'org-babel-trim
  166. (reverse results) "\n")))
  167. (t ;; if not "output" then treat as "value"
  168. (with-temp-file tmp-file
  169. (insert (car results)) (insert "\n"))
  170. (org-babel-import-elisp-from-file tmp-file))))
  171. "")))))
  172. (defun org-babel-sh-strip-weird-long-prompt (string)
  173. "Remove prompt cruft from a string of shell output."
  174. (while (string-match "^% +[\r\n$]+ *" string)
  175. (setq string (substring string (match-end 0))))
  176. string)
  177. (provide 'ob-sh)
  178. ;;; ob-sh.el ends here