org-babel-shell.el 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 command 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 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. (defun org-babel-shell-initiate-session (&optional session)
  74. "If there is not a current inferior-process-buffer in SESSION
  75. then create. Return the initialized session."
  76. (save-window-excursion (shell session)
  77. (org-babel-comint-wait-for-output (current-buffer))
  78. (current-buffer)))
  79. (defvar org-babel-shell-eoe-indicator "echo 'org_babel_shell_eoe'"
  80. "Used to indicate that evaluation is has completed.")
  81. (defvar org-babel-shell-eoe-output "org_babel_shell_eoe"
  82. "Used to indicate that evaluation is has completed.")
  83. (defun org-babel-shell-evaluate (buffer body &optional result-type)
  84. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  85. 'output then return a list of the outputs of the statements in
  86. BODY, if RESULT-TYPE equals 'value then return the value of the
  87. last statement in BODY."
  88. (org-babel-comint-in-buffer buffer
  89. (let ((string-buffer "")
  90. (full-body (mapconcat #'org-babel-chomp
  91. (list body org-babel-shell-eoe-indicator) "\n"))
  92. results)
  93. (flet ((my-filt (text) (setq string-buffer (concat string-buffer text))))
  94. ;; setup filter
  95. (add-hook 'comint-output-filter-functions 'my-filt)
  96. ;; pass FULL-BODY to process
  97. (goto-char (process-mark (get-buffer-process buffer)))
  98. (insert full-body)
  99. (comint-send-input)
  100. ;; wait for end-of-evaluation indicator
  101. (while (progn
  102. (goto-char comint-last-input-end)
  103. (not (save-excursion (and (re-search-forward comint-prompt-regexp nil t)
  104. (re-search-forward (regexp-quote org-babel-shell-eoe-output) nil t)))))
  105. (accept-process-output (get-buffer-process buffer)))
  106. ;; remove filter
  107. (remove-hook 'comint-output-filter-functions 'my-filt))
  108. ;; (message (format "raw-results=%S" string-buffer)) ;; debugging
  109. ;; (message (format "split-results=%S" (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))) ;; debugging
  110. ;; split results with `comint-prompt-regexp'
  111. (setq results (cdr (member org-babel-shell-eoe-output
  112. (reverse (mapcar #'org-babel-shell-strip-weird-long-prompt
  113. (mapcar #'org-babel-trim (split-string string-buffer comint-prompt-regexp)))))))
  114. (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
  115. (or (case result-type
  116. (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
  117. (value (car results))
  118. (t (reverse results))) ""))))
  119. (defun org-babel-shell-strip-weird-long-prompt (string)
  120. (while (string-match "^% +[\r\n$]+ *" string)
  121. (setq string (substring string (match-end 0))))
  122. string)
  123. (provide 'org-babel-shell)
  124. ;;; org-babel-shell.el ends here