org-babel-shell.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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)
  46. full-body result-type)))
  47. (if (member "scalar" result-params)
  48. results
  49. (setq results (let ((tmp-file (make-temp-file "org-babel-shell")))
  50. (with-temp-file tmp-file (insert results))
  51. (org-babel-import-elisp-from-file tmp-file)))
  52. (if (and (member "vector" results) (not (listp results)))
  53. (list (list results))
  54. results))))
  55. (defun org-babel-shell-var-to-shell (var)
  56. "Convert an elisp var into a string of shell commands
  57. specifying a var of the same value."
  58. (if (listp var)
  59. (concat "[" (mapconcat #'org-babel-shell-var-to-shell var ", ") "]")
  60. (format "%S" var)))
  61. (defun org-babel-shell-table-or-results (results)
  62. "If the results look like a table, then convert them into an
  63. Emacs-lisp table, otherwise return the results as a string."
  64. (org-babel-read
  65. (if (string-match "^\\[.+\\]$" results)
  66. (org-babel-read
  67. (replace-regexp-in-string
  68. "\\[" "(" (replace-regexp-in-string
  69. "\\]" ")" (replace-regexp-in-string
  70. ", " " " (replace-regexp-in-string
  71. "'" "\"" results)))))
  72. results)))
  73. ;; functions for comint evaluation
  74. (defvar org-babel-shell-buffers '(:default . nil))
  75. (defun org-babel-shell-session-buffer (session)
  76. (cdr (assoc session org-babel-shell-buffers)))
  77. (defun org-babel-shell-initiate-session (&optional session)
  78. "If there is not a current inferior-process-buffer in SESSION
  79. then create. Return the initialized session."
  80. (save-window-excursion
  81. (let* ((session (if session (intern session) :default))
  82. (shell-buffer (org-babel-shell-session-buffer session))
  83. (newp (not (org-babel-comint-buffer-livep shell-buffer))))
  84. (message "initiating shell buffer %S" shell-buffer)
  85. (shell shell-buffer)
  86. (when newp
  87. (setq shell-buffer (current-buffer))
  88. (org-babel-comint-wait-for-output shell-buffer))
  89. (setq org-babel-shell-buffers (cons (cons session shell-buffer) (assq-delete-all session org-babel-shell-buffers)))
  90. session)))
  91. (defvar org-babel-shell-eoe-indicator "echo 'org_babel_shell_eoe'"
  92. "Used to indicate that evaluation is has completed.")
  93. (defvar org-babel-shell-eoe-output "org_babel_shell_eoe"
  94. "Used to indicate that evaluation is has completed.")
  95. (defun org-babel-shell-evaluate (buffer body &optional result-type)
  96. "Pass BODY to the Shell process in BUFFER. If RESULT-TYPE equals
  97. 'output then return a list of the outputs of the statements in
  98. BODY, if RESULT-TYPE equals 'value then return the value of the
  99. last statement in BODY."
  100. (let* ((full-body (mapconcat #'org-babel-chomp
  101. (list body org-babel-shell-eoe-indicator) "\n"))
  102. (raw (org-babel-comint-with-output buffer org-babel-shell-eoe-output nil
  103. (insert full-body) (comint-send-input nil t)))
  104. (results (cdr (member org-babel-shell-eoe-output
  105. (reverse (mapcar #'org-babel-shell-strip-weird-long-prompt
  106. (mapcar #'org-babel-trim raw)))))))
  107. ;; (message (replace-regexp-in-string "%" "%%" (format "processed-results=%S" results))) ;; debugging
  108. (or (case result-type
  109. (output (org-babel-trim (mapconcat #'org-babel-trim (reverse results) "\n")))
  110. (value (car results))
  111. (t (reverse results))) "")))
  112. (defun org-babel-shell-strip-weird-long-prompt (string)
  113. (while (string-match "^% +[\r\n$]+ *" string)
  114. (setq string (substring string (match-end 0))))
  115. string)
  116. (provide 'org-babel-shell)
  117. ;;; org-babel-shell.el ends here