ob-sh.el 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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: 7.01trans
  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 'ob-comint)
  23. (require 'ob-eval)
  24. (require 'shell)
  25. (eval-when-compile (require 'cl))
  26. (declare-function org-babel-ref-variables "ob-ref" (params))
  27. (declare-function org-babel-comint-in-buffer "ob-comint" (buffer &rest body))
  28. (declare-function org-babel-comint-wait-for-output "ob-comint" (buffer))
  29. (declare-function org-babel-comint-buffer-livep "ob-comint" (buffer))
  30. (declare-function org-babel-comint-with-output "ob-comint" (meta &rest body))
  31. (declare-function orgtbl-to-generic "org-table" (table params))
  32. (defvar org-babel-default-header-args:sh '())
  33. (defvar org-babel-sh-command "sh"
  34. "Command used to invoke a shell.
  35. This will be passed to `shell-command-on-region'")
  36. (defun org-babel-expand-body:sh (body params &optional processed-params)
  37. "Expand BODY according to PARAMS, return the expanded body."
  38. (mapconcat
  39. #'identity
  40. (append (org-babel-sh-variable-assignments params processed-params)
  41. (list body)) "\n"))
  42. (defun org-babel-execute:sh (body params)
  43. "Execute a block of Shell commands with Babel.
  44. This function is called by `org-babel-execute-src-block'."
  45. (let* ((processed-params (org-babel-process-params params))
  46. (session (org-babel-sh-initiate-session (nth 0 processed-params)))
  47. (result-params (nth 2 processed-params))
  48. (full-body (org-babel-expand-body:sh
  49. body params processed-params)))
  50. (org-babel-reassemble-table
  51. (org-babel-sh-evaluate session full-body result-params)
  52. (org-babel-pick-name
  53. (nth 4 processed-params) (cdr (assoc :colnames params)))
  54. (org-babel-pick-name
  55. (nth 5 processed-params) (cdr (assoc :rownames params))))))
  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. (var-lines (org-babel-sh-variable-assignments params)))
  60. (org-babel-comint-in-buffer session
  61. (mapc (lambda (var)
  62. (insert var) (comint-send-input nil t)
  63. (org-babel-comint-wait-for-output session)) var-lines))
  64. session))
  65. (defun org-babel-load-session:sh (session body params)
  66. "Load BODY into SESSION."
  67. (save-window-excursion
  68. (let ((buffer (org-babel-prep-session:sh session params)))
  69. (with-current-buffer buffer
  70. (goto-char (process-mark (get-buffer-process (current-buffer))))
  71. (insert (org-babel-chomp body)))
  72. buffer)))
  73. ;; helper functions
  74. (defun org-babel-sh-variable-assignments (params &optional processed-params)
  75. "Return list of shell statements assigning the block's variables"
  76. (let ((sep (cdr (assoc :separator params))))
  77. (mapcar
  78. (lambda (pair)
  79. (format "%s=%s"
  80. (car pair)
  81. (org-babel-sh-var-to-sh (cdr pair) sep)))
  82. (nth 1 (or processed-params (org-babel-process-params params))))))
  83. (defun org-babel-sh-var-to-sh (var &optional sep)
  84. "Convert an elisp value to a shell variable.
  85. Convert an elisp var into a string of shell commands specifying a
  86. 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
  94. (deep-string var) (list :sep (or sep "\t")))))
  95. (if (stringp var)
  96. (if (string-match "[\n\r]" var)
  97. (format "$(cat <<BABEL_STRING\n%s\nBABEL_STRING\n)" var)
  98. (format "%s" var))
  99. (format "%S" var))))
  100. (defun org-babel-sh-table-or-results (results)
  101. "Convert RESULTS to an appropriate elisp value.
  102. If the results look like a table, then convert them into an
  103. Emacs-lisp table, otherwise return the results as a string."
  104. (org-babel-read
  105. (if (string-match "^\\[.+\\]$" results)
  106. (org-babel-read
  107. (concat "'"
  108. (replace-regexp-in-string
  109. "\\[" "(" (replace-regexp-in-string
  110. "\\]" ")" (replace-regexp-in-string
  111. ", " " " (replace-regexp-in-string
  112. "'" "\"" results))))))
  113. results)))
  114. (defun org-babel-sh-initiate-session (&optional session params)
  115. "Initiate a session named SESSION according to PARAMS."
  116. (when (and session (not (string= session "none")))
  117. (save-window-excursion
  118. (or (org-babel-comint-buffer-livep session)
  119. (progn (shell session) (get-buffer (current-buffer)))))))
  120. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  121. "String to indicate that evaluation has completed.")
  122. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  123. "String to indicate that evaluation has completed.")
  124. (defun org-babel-sh-evaluate (session body &optional result-params)
  125. "Pass BODY to the Shell process in BUFFER.
  126. If RESULT-TYPE equals 'output then return a list of the outputs
  127. of the statements in BODY, if RESULT-TYPE equals 'value then
  128. return the value of the last statement in BODY."
  129. ((lambda (results)
  130. (when results
  131. (if (or (member "scalar" result-params)
  132. (member "output" result-params))
  133. results
  134. (let ((tmp-file (org-babel-temp-file "sh-")))
  135. (with-temp-file tmp-file (insert results))
  136. (org-babel-import-elisp-from-file tmp-file)))))
  137. (if (not session)
  138. (org-babel-eval org-babel-sh-command (org-babel-trim body))
  139. (mapconcat
  140. #'org-babel-sh-strip-weird-long-prompt
  141. (mapcar
  142. #'org-babel-trim
  143. (butlast
  144. (org-babel-comint-with-output
  145. (session org-babel-sh-eoe-output t body)
  146. (mapc
  147. (lambda (line)
  148. (insert line) (comint-send-input nil t) (sleep-for 0.25))
  149. (append
  150. (split-string (org-babel-trim body) "\n")
  151. (list org-babel-sh-eoe-indicator))))
  152. 2)) "\n"))))
  153. (defun org-babel-sh-strip-weird-long-prompt (string)
  154. "Remove prompt cruft from a string of shell output."
  155. (while (string-match "^% +[\r\n$]+ *" string)
  156. (setq string (substring string (match-end 0))))
  157. string)
  158. (provide 'ob-sh)
  159. ;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
  160. ;;; ob-sh.el ends here