ob-sh.el 7.1 KB

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