org-babel-shell.el 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. ;;; org-babel-shell.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. (defun org-babel-execute:sh (body params)
  29. "Execute a block of Shell commands with org-babel. This
  30. function is called by `org-babel-execute-src-block'."
  31. (message "executing Shell source code block")
  32. (let* ((vars (org-babel-ref-variables params))
  33. (result-params (split-string (or (cdr (assoc :results params)) "")))
  34. (result-type (cond ((member "output" result-params) 'output)
  35. ((member "value" result-params) 'value)
  36. (t 'value)))
  37. (full-body (concat
  38. (mapconcat ;; define any variables
  39. (lambda (pair)
  40. (format "%s=%s"
  41. (car pair)
  42. (org-babel-shell-var-to-shell (cdr pair))))
  43. vars "\n") "\n" body "\n\n")) ;; then the source block body
  44. (session (org-babel-shell-initiate-session (cdr (assoc :session params))))
  45. (results (org-babel-shell-evaluate (org-babel-shell-session-buffer session) full-body result-type)))
  46. (if (member "scalar" result-params)
  47. results
  48. (setq results (let ((tmp-file (make-temp-file "org-babel-ruby")))
  49. (with-temp-file tmp-file (insert results))
  50. (org-babel-import-elisp-from-file tmp-file)))
  51. (if (and (member "vector" results) (not (listp results)))
  52. (list (list results))
  53. results))))
  54. (defun org-babel-shell-var-to-shell (var)
  55. "Convert an elisp var into a string of shell commands
  56. specifying a var of the same value."
  57. (if (listp var)
  58. (concat "[" (mapconcat #'org-babel-shell-var-to-shell var ", ") "]")
  59. (format "%S" var)))
  60. (defun org-babel-shell-table-or-results (results)
  61. "If the results look like a table, then convert them into an
  62. Emacs-lisp table, otherwise return the results as a string."
  63. (org-babel-read
  64. (if (string-match "^\\[.+\\]$" results)
  65. (org-babel-read
  66. (replace-regexp-in-string
  67. "\\[" "(" (replace-regexp-in-string
  68. "\\]" ")" (replace-regexp-in-string
  69. ", " " " (replace-regexp-in-string
  70. "'" "\"" results)))))
  71. results)))
  72. ;; functions for comint evaluation
  73. (defvar org-babel-shell-buffers '(:default . nil))
  74. (defun org-babel-shell-session-buffer (session)
  75. (cdr (assoc session org-babel-shell-buffers)))
  76. (defun org-babel-shell-initiate-session (&optional session)
  77. "If there is not a current inferior-process-buffer in SESSION
  78. then create. Return the initialized session."
  79. (save-window-excursion
  80. (let* ((session (if session (intern session) :default))
  81. (shell-buffer (org-babel-shell-session-buffer session))
  82. (newp (not (org-babel-comint-buffer-livep shell-buffer))))
  83. (shell shell-buffer)
  84. (when newp
  85. (setq shell-buffer (current-buffer))
  86. (org-babel-comint-wait-for-output shell-buffer))
  87. (setq org-babel-shell-buffers (cons (cons session shell-buffer) (assq-delete-all session org-babel-shell-buffers)))
  88. session)))
  89. (defvar org-babel-shell-eoe-indicator "echo 'org_babel_shell_eoe'"
  90. "Used to indicate that evaluation is has completed.")
  91. (defvar org-babel-shell-eoe-output "org_babel_shell_eoe"
  92. "Used to indicate that evaluation is has completed.")
  93. (defun org-babel-shell-evaluate (buffer body &optional result-type)
  94. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  95. 'output then return a list of the outputs of the statements in
  96. BODY, if RESULT-TYPE equals 'value then return the value of the
  97. last statement in BODY."
  98. (org-babel-comint-in-buffer buffer
  99. (let ((string-buffer "")
  100. (full-body (mapconcat #'org-babel-chomp
  101. (list body org-babel-shell-eoe-indicator) "\n"))
  102. results)
  103. (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
  104. ;; setup filter
  105. (add-hook 'comint-output-filter-functions 'my-filt)
  106. ;; pass FULL-BODY to process
  107. (goto-char (process-mark (get-buffer-process buffer)))
  108. (insert full-body)
  109. (comint-send-input)
  110. ;; wait for end-of-evaluation indicator
  111. (while (progn
  112. (goto-char comint-last-input-end)
  113. (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
  114. (re-search-forward (regexp-quote org-babel-shell-eoe-output) nil t)))))
  115. (accept-process-output (get-buffer-process buffer)))
  116. ;; remove filter
  117. (remove-hook 'comint-output-filter-functions 'my-filt))
  118. ;; (message (format "raw-results=%S" string-buffer)) ;; debugging
  119. ;; (message (format "split-results=%S" (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))) ;; debugging
  120. ;; split results with `comint-prompt-regexp'
  121. (setq results (cdr (member org-babel-shell-eoe-output
  122. (reverse (mapcar #'org-babel-shell-strip-weird-long-prompt
  123. (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))))))
  124. ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
  125. (or (case result-type
  126. (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
  127. (value (car results))
  128. (t (reverse results))) ""))))
  129. (defun org-babel-shell-strip-weird-long-prompt (string)
  130. (while (string-match "^% +[\r\n$]+ *" string)
  131. (setq string (substring string (match-end 0))))
  132. string)
  133. (provide 'org-babel-shell)
  134. ;;; org-babel-shell.el ends here