org-babel-sh.el 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. ;;; org-babel-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 'org-babel)
  26. (require 'shell)
  27. (org-babel-add-interpreter "sh")
  28. (add-to-list 'org-babel-tangle-langs '("sh" "sh" "#!/usr/bin/env sh"))
  29. (defun org-babel-execute:sh (body params)
  30. "Execute a block of Shell commands with org-babel. This
  31. function is called by `org-babel-execute-src-block'."
  32. (message "executing Shell source code block")
  33. (let* ((processed-params (org-babel-process-params params))
  34. (session (org-babel-sh-initiate-session (first processed-params)))
  35. (vars (second processed-params))
  36. (result-type (fourth processed-params))
  37. (full-body (concat
  38. (mapconcat ;; define any variables
  39. (lambda (pair)
  40. (format "%s=%s"
  41. (car pair)
  42. (org-babel-sh-var-to-sh (cdr pair))))
  43. vars "\n") "\n" body "\n\n"))) ;; then the source block body
  44. (org-babel-sh-evaluate session full-body result-type)))
  45. (defun org-babel-prep-session:sh (session params)
  46. "Prepare SESSION according to the header arguments specified in PARAMS."
  47. (let* ((session (org-babel-sh-initiate-session session))
  48. (vars (org-babel-ref-variables params))
  49. (var-lines (mapcar ;; define any variables
  50. (lambda (pair)
  51. (format "%s=%s"
  52. (car pair)
  53. (org-babel-sh-var-to-sh (cdr pair))))
  54. vars)))
  55. (org-babel-comint-in-buffer session
  56. (mapc (lambda (var)
  57. (insert var) (comint-send-input nil t)
  58. (org-babel-comint-wait-for-output session)) var-lines))
  59. session))
  60. (defun org-babel-load-session:sh (session body params)
  61. "Load BODY into SESSION."
  62. (save-window-excursion
  63. (let ((buffer (org-babel-prep-session:sh session params)))
  64. (with-current-buffer buffer
  65. (goto-char (process-mark (get-buffer-process (current-buffer))))
  66. (insert (org-babel-chomp body)))
  67. buffer)))
  68. ;; helper functions
  69. (defun org-babel-sh-var-to-sh (var)
  70. "Convert an elisp var into a string of shell commands
  71. specifying a var of the same value."
  72. (if (listp var)
  73. (concat "[" (mapconcat #'org-babel-sh-var-to-sh var ", ") "]")
  74. (format "%S" var)))
  75. (defun org-babel-sh-table-or-results (results)
  76. "If the results look like a table, then convert them into an
  77. Emacs-lisp table, otherwise return the results as a string."
  78. (org-babel-read
  79. (if (string-match "^\\[.+\\]$" results)
  80. (org-babel-read
  81. (replace-regexp-in-string
  82. "\\[" "(" (replace-regexp-in-string
  83. "\\]" ")" (replace-regexp-in-string
  84. ", " " " (replace-regexp-in-string
  85. "'" "\"" results)))))
  86. results)))
  87. (defun org-babel-sh-initiate-session (&optional session)
  88. (unless (string= session "none")
  89. (save-window-excursion
  90. (or (org-babel-comint-buffer-livep session)
  91. (progn (shell session) (get-buffer (current-buffer)))))))
  92. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  93. "Used to indicate that evaluation is has completed.")
  94. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  95. "Used to indicate that evaluation is has completed.")
  96. (defun org-babel-sh-evaluate (session body &optional result-type)
  97. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  98. 'output then return a list of the outputs of the statements in
  99. BODY, if RESULT-TYPE equals 'value then return the value of the
  100. last statement in BODY."
  101. (if (not session)
  102. ;; external process evaluation
  103. (save-window-excursion
  104. (with-temp-buffer
  105. (insert body)
  106. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  107. (shell-command-on-region (point-min) (point-max) "sh" 'replace)
  108. (case result-type
  109. (output (buffer-string))
  110. (value ;; TODO: figure out how to return non-output values from shell scripts
  111. (let ((tmp-file (make-temp-file "org-babel-sh"))
  112. (results (buffer-string)))
  113. (with-temp-file tmp-file (insert results))
  114. (org-babel-import-elisp-from-file tmp-file))))))
  115. ;; comint session evaluation
  116. (flet ((strip-empty (lst)
  117. (delq nil (mapcar (lambda (el) (unless (= (length el) 0) el)) lst))))
  118. (let ((tmp-file (make-temp-file "org-babel-sh"))
  119. (results
  120. (cdr (member
  121. org-babel-sh-eoe-output
  122. (strip-empty
  123. (reverse
  124. (mapcar #'org-babel-sh-strip-weird-long-prompt
  125. (mapcar #'org-babel-trim
  126. (org-babel-comint-with-output
  127. session org-babel-sh-eoe-output t
  128. (mapc (lambda (line) (insert line) (comint-send-input))
  129. (strip-empty (split-string body "\n")))
  130. (insert org-babel-sh-eoe-indicator)
  131. (comint-send-input))))))))))
  132. ;; (message (replace-regexp-in-string
  133. ;; "%" "%%" (format "processed-results=%S" results))) ;; debugging
  134. (or (and results
  135. (case result-type
  136. (output (org-babel-trim (mapconcat #'org-babel-trim
  137. (reverse results) "\n")))
  138. (value (with-temp-file tmp-file
  139. (insert (car results)) (insert "\n"))
  140. (org-babel-import-elisp-from-file tmp-file))))
  141. "")))))
  142. (defun org-babel-sh-strip-weird-long-prompt (string)
  143. (while (string-match "^% +[\r\n$]+ *" string)
  144. (setq string (substring string (match-end 0))))
  145. string)
  146. (provide 'org-babel-sh)
  147. ;;; org-babel-sh.el ends here