org-babel-script.el 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. ;; this is totally not working well
  64. (org-babel-script-input-command interpreter (concat
  65. "org_babel_io_holder = $stdout;"
  66. (format "org_babel_tmp_file_holder = File.open('%s', 'w'); " tmp-file)
  67. "$stdout = org_babel_tmp_file_holder; "
  68. body
  69. "org_babel_tmp_file_holder.flush; "
  70. "$stdout = org_babel_io_holder;"))
  71. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string))))
  72. ((member "last" results-params) ;; the value of the last statement
  73. (org-babel-script-input-command interpreter full-body)
  74. (org-babel-script-table-or-results
  75. (org-babel-script-command-to-string interpreter "_"))))))
  76. (defun org-babel-script-var-to-ruby/python (var)
  77. "Convert an elisp var into a string of ruby or python source
  78. code specifying a var of the same value."
  79. (if (listp var)
  80. (concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
  81. (format "%S" var)))
  82. (defun org-babel-script-table-or-results (results)
  83. "If the results look like a table, then convert them into an
  84. Emacs-lisp table, otherwise return the results as a string."
  85. (setq results (org-babel-chomp results))
  86. (org-babel-read
  87. (if (string-match "^\\[.+\\]$" results)
  88. ;; somewhat hacky, but thanks to similarities between languages
  89. ;; it seems to work
  90. (org-babel-read
  91. (replace-regexp-in-string
  92. "\\[" "(" (replace-regexp-in-string
  93. "\\]" ")" (replace-regexp-in-string
  94. ", " " " (replace-regexp-in-string
  95. "'" "\"" results)))))
  96. (org-babel-chomp results))))
  97. ;; functions for interacting with comint sessions
  98. (defvar org-babel-script-ruby-session nil)
  99. (defvar org-babel-script-python-session nil)
  100. (defun org-babel-script-session (interpreter)
  101. (case (if (symbolp interpreter) interpreter (intern interpreter))
  102. ('ruby 'org-babel-script-ruby-session)
  103. ('python 'org-babel-script-python-session)))
  104. (defun org-babel-script-initiate-session (interpreter)
  105. "If there is not a current inferior-process-buffer in SESSION
  106. then create. Return the initialized session."
  107. (case (intern (format "%s" interpreter))
  108. ('ruby
  109. (setq org-babel-script-ruby-session (save-window-excursion
  110. (funcall #'run-ruby nil)
  111. (current-buffer))))
  112. ('python
  113. (setq org-babel-script-python-session (save-window-excursion
  114. (funcall #'run-python)
  115. (current-buffer))))))
  116. (defun org-babel-script-input-command (interpreter cmd)
  117. (org-babel-comint-input-command
  118. (eval (org-babel-script-session interpreter)) (org-babel-chomp cmd)))
  119. (defun org-babel-script-command-to-string (interpreter cmd)
  120. (org-babel-comint-command-to-string
  121. (eval (org-babel-script-session interpreter)) (org-babel-chomp cmd)))
  122. (provide 'org-babel-script)
  123. ;;; org-babel-script.el ends here