ob-sh.el 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 '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. This will be passed to
  35. `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") "\n" body "\n\n")))
  47. (defun org-babel-execute:sh (body params)
  48. "Execute a block of Shell commands with org-babel. This
  49. function is called by `org-babel-execute-src-block'."
  50. (message "executing Shell source code block")
  51. (let* ((processed-params (org-babel-process-params params))
  52. (session (org-babel-sh-initiate-session (nth 0 processed-params)))
  53. (result-params (nth 2 processed-params))
  54. (full-body (org-babel-expand-body:sh
  55. body params processed-params)))
  56. (org-babel-reassemble-table
  57. (org-babel-sh-evaluate session full-body result-params)
  58. (org-babel-pick-name
  59. (nth 4 processed-params) (cdr (assoc :colnames params)))
  60. (org-babel-pick-name
  61. (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
  97. (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. ((lambda (results)
  128. (if (or (member "scalar" result-params)
  129. (member "output" result-params))
  130. results
  131. (let ((tmp-file (make-temp-file "org-babel-sh")))
  132. (with-temp-file tmp-file (insert results))
  133. (org-babel-import-elisp-from-file tmp-file))))
  134. (if (not session)
  135. (org-babel-eval org-babel-sh-command (org-babel-trim body))
  136. (let ((tmp-file (make-temp-file "org-babel-sh")))
  137. (mapconcat
  138. #'org-babel-sh-strip-weird-long-prompt
  139. (mapcar
  140. #'org-babel-trim
  141. (butlast
  142. (org-babel-comint-with-output
  143. (session org-babel-sh-eoe-output t body)
  144. (mapc
  145. (lambda (line)
  146. (insert line) (comint-send-input nil t) (sleep-for 0.25))
  147. (append
  148. (split-string (org-babel-trim body) "\n")
  149. (list org-babel-sh-eoe-indicator))))
  150. 2)) "\n")))))
  151. (defun org-babel-sh-strip-weird-long-prompt (string)
  152. "Remove prompt cruft from a string of shell output."
  153. (while (string-match "^% +[\r\n$]+ *" string)
  154. (setq string (substring string (match-end 0))))
  155. string)
  156. (provide 'ob-sh)
  157. ;; arch-tag: 416dd531-c230-4b0a-a5bf-8d948f990f2d
  158. ;;; ob-sh.el ends here