org-babel-sh.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. ;; helper functions
  60. (defun org-babel-sh-var-to-sh (var)
  61. "Convert an elisp var into a string of shell commands
  62. specifying a var of the same value."
  63. (if (listp var)
  64. (concat "[" (mapconcat #'org-babel-sh-var-to-sh var ", ") "]")
  65. (format "%S" var)))
  66. (defun org-babel-sh-table-or-results (results)
  67. "If the results look like a table, then convert them into an
  68. Emacs-lisp table, otherwise return the results as a string."
  69. (org-babel-read
  70. (if (string-match "^\\[.+\\]$" results)
  71. (org-babel-read
  72. (replace-regexp-in-string
  73. "\\[" "(" (replace-regexp-in-string
  74. "\\]" ")" (replace-regexp-in-string
  75. ", " " " (replace-regexp-in-string
  76. "'" "\"" results)))))
  77. results)))
  78. (defvar org-babel-sh-buffers '(:default . nil))
  79. (defun org-babel-sh-session-buffer (session)
  80. (cdr (assoc session org-babel-sh-buffers)))
  81. (defun org-babel-sh-initiate-session-by-key (&optional session)
  82. "If there is not a current inferior-process-buffer in SESSION
  83. then create. Return the initialized session."
  84. (save-window-excursion
  85. (let* ((session (if session (intern session) :default))
  86. (sh-buffer (org-babel-sh-session-buffer session))
  87. (newp (not (org-babel-comint-buffer-livep sh-buffer))))
  88. (if (and sh-buffer (get-buffer sh-buffer) (not (buffer-live-p sh-buffer)))
  89. (setq sh-buffer nil))
  90. (shell sh-buffer)
  91. (when newp
  92. (setq sh-buffer (current-buffer))
  93. (org-babel-comint-wait-for-output sh-buffer))
  94. (setq org-babel-sh-buffers (cons (cons session sh-buffer)
  95. (assq-delete-all session org-babel-sh-buffers)))
  96. session)))
  97. (defun org-babel-sh-initiate-session (&optional session)
  98. (unless (string= session "none")
  99. (org-babel-sh-session-buffer (org-babel-sh-initiate-session-by-key session))))
  100. (defvar org-babel-sh-eoe-indicator "echo 'org_babel_sh_eoe'"
  101. "Used to indicate that evaluation is has completed.")
  102. (defvar org-babel-sh-eoe-output "org_babel_sh_eoe"
  103. "Used to indicate that evaluation is has completed.")
  104. (defun org-babel-sh-evaluate (buffer body &optional result-type)
  105. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  106. 'output then return a list of the outputs of the statements in
  107. BODY, if RESULT-TYPE equals 'value then return the value of the
  108. last statement in BODY."
  109. (if (not session)
  110. ;; external process evaluation
  111. (save-window-excursion
  112. (with-temp-buffer
  113. (insert body)
  114. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  115. (shell-command-on-region (point-min) (point-max) "sh" 'replace)
  116. (case result-type
  117. (output (buffer-string))
  118. (value ;; TODO: figure out how to return non-output values from shell scripts
  119. (let ((tmp-file (make-temp-file "org-babel-sh"))
  120. (results (buffer-string)))
  121. (with-temp-file tmp-file (insert results))
  122. (org-babel-import-elisp-from-file tmp-file))))))
  123. ;; comint session evaluation
  124. (let* ((tmp-file (make-temp-file "org-babel-sh"))
  125. (full-body (mapconcat #'org-babel-chomp
  126. (list body org-babel-sh-eoe-indicator) "\n"))
  127. (raw (org-babel-comint-with-output buffer org-babel-sh-eoe-output nil
  128. (insert full-body) (comint-send-input nil t)))
  129. (results (cdr (member org-babel-sh-eoe-output
  130. (reverse (mapcar #'org-babel-sh-strip-weird-long-prompt
  131. (mapcar #'org-babel-trim raw)))))))
  132. ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
  133. (or (case result-type
  134. (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
  135. (value (with-temp-file tmp-file (insert (car results)))
  136. (org-babel-import-elisp-from-file tmp-file)))) "")))
  137. (defun org-babel-sh-strip-weird-long-prompt (string)
  138. (while (string-match "^% +[\r\n$]+ *" string)
  139. (setq string (substring string (match-end 0))))
  140. string)
  141. (provide 'org-babel-sh)
  142. ;;; org-babel-sh.el ends here