org-babel-script.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. ;;; org-babel-script.el --- org-babel functions for scripting languages
  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 ruby, and python source code.
  24. ;;; Code:
  25. (require 'org-babel)
  26. (require 'inf-ruby)
  27. (require 'python)
  28. ;; org-babel introduction and formalities
  29. (defun org-babel-script-add-interpreter (var cmds)
  30. (set-default var cmds)
  31. (mapc (lambda (cmd)
  32. (org-babel-add-interpreter cmd)
  33. (eval
  34. `(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
  35. ,(concat "Evaluate a block of " cmd " script with org-babel. This function is
  36. called by `org-babel-execute-src-block'. This function is an
  37. automatically generated wrapper for `org-babel-script-execute'.")
  38. (org-babel-script-execute ,cmd body params))))
  39. cmds))
  40. (defcustom org-babel-script-interpreters '("ruby" "python")
  41. "List of interpreters of scripting languages which can be
  42. executed through org-babel."
  43. :group 'org-babel
  44. :set 'org-babel-script-add-interpreter)
  45. (mapc #'org-babel-add-interpreter org-babel-script-interpreters)
  46. ;; main execute function used by org-babel
  47. (defun org-babel-script-execute (interpreter body params)
  48. "Pass BODY to INTERPRETER obeying any options set with PARAMS."
  49. (message (format "executing %s code block..." cmd))
  50. (let* ((vars (org-babel-ref-variables params))
  51. (results-params (split-string (or (cdr (assoc :results params)) "")))
  52. (full-body (concat
  53. (mapconcat ;; define any variables
  54. (lambda (pair)
  55. (format "\t%s=%s"
  56. (car pair)
  57. (org-babel-script-var-to-ruby/python (cdr pair))))
  58. vars "\n") body "\n"))) ;; then the source block body
  59. (org-babel-script-initiate-session interpreter)
  60. (cond
  61. ((member "script" results-params) ;; collect all output
  62. (let ((tmp-file (make-temp-file "org-babel-R-script-output")))
  63. (org-babel-comint-input-command org-babel-R-buffer (format "sink(%S)" tmp-file))
  64. (org-babel-comint-input-command org-babel-R-buffer body)
  65. (org-babel-comint-input-command org-babel-R-buffer "sink()")
  66. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  67. ((member "last" results-params) ;; the value of the last statement
  68. (org-babel-script-input-command interpreter full-body)
  69. (org-babel-script-table-or-results
  70. (org-babel-script-command-to-string interpreter "_"))))))
  71. (defun org-babel-script-var-to-ruby/python (var)
  72. "Convert an elisp var into a string of ruby or python source
  73. code specifying a var of the same value."
  74. (if (listp var)
  75. (concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
  76. (format "%S" var)))
  77. (defun org-babel-script-table-or-results (results)
  78. "If the results look like a table, then convert them into an
  79. Emacs-lisp table, otherwise return the results as a string."
  80. (setq results (org-babel-chomp results))
  81. (org-babel-read
  82. (if (string-match "^\\[.+\\]$" results)
  83. ;; somewhat hacky, but thanks to similarities between languages
  84. ;; it seems to work
  85. (org-babel-read
  86. (replace-regexp-in-string
  87. "\\[" "(" (replace-regexp-in-string
  88. "\\]" ")" (replace-regexp-in-string
  89. ", " " " (replace-regexp-in-string
  90. "'" "\"" results)))))
  91. (org-babel-chomp results))))
  92. ;; functions for interacting with comint sessions
  93. (defvar org-babel-script-ruby-session nil)
  94. (defvar org-babel-script-python-session nil)
  95. (defun org-babel-script-session (interpreter)
  96. (case (if (symbolp interpreter) interpreter (intern interpreter))
  97. ('ruby 'org-babel-script-ruby-session)
  98. ('python 'org-babel-script-python-session)))
  99. (defun org-babel-script-initiate-session (interpreter)
  100. "If there is not a current inferior-process-buffer in SESSION
  101. then create. Return the initialized session."
  102. (case (intern (format "%s" interpreter))
  103. ('ruby
  104. (setq org-babel-script-ruby-session (save-window-excursion
  105. (funcall #'run-ruby nil)
  106. (current-buffer))))
  107. ('python
  108. (setq org-babel-script-python-session (save-window-excursion
  109. (funcall #'run-python)
  110. (current-buffer))))))
  111. (defun org-babel-script-input-command (interpreter cmd)
  112. (setq cmd (org-babel-chomp cmd))
  113. (message (format "input = %S" cmd))
  114. (org-babel-comint-input-command (eval (org-babel-script-session interpreter)) cmd))
  115. (defun org-babel-script-command-to-string (interpreter cmd)
  116. (setq cmd (org-babel-chomp cmd))
  117. (message (format "string = %S" cmd))
  118. (org-babel-comint-command-to-string (eval (org-babel-script-session interpreter)) cmd))
  119. (provide 'org-babel-script)
  120. ;;; org-babel-script.el ends here