ob-sh.el 6.4 KB

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