org-babel-script.el 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. (full-body (concat
  52. (mapconcat ;; define any variables
  53. (lambda (pair)
  54. (format "\t%s=%s"
  55. (car pair)
  56. (org-babel-script-var-to-ruby/python (cdr pair))))
  57. vars "\n") body "\n"))) ;; then the source block body
  58. (org-babel-script-input-command interpreter full-body)
  59. (org-babel-script-table-or-results (org-babel-script-last-value interpreter))))
  60. (defun org-babel-script-var-to-ruby/python (var)
  61. "Convert an elisp var into a string of ruby or python source
  62. code specifying a var of the same value."
  63. (if (listp var)
  64. (concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
  65. (format "%S" var)))
  66. (defun org-babel-script-table-or-results (results)
  67. "If the results look like a table, then convert them into an
  68. Emacs-lisp table, otherwise return the results as a string."
  69. (setq results (org-babel-chomp results))
  70. (org-babel-read
  71. (if (string-match "^\\[.+\\]$" results)
  72. ;; somewhat hacky, but thanks to similarities between languages
  73. ;; it seems to work
  74. (org-babel-read
  75. (replace-regexp-in-string
  76. "\\[" "(" (replace-regexp-in-string
  77. "\\]" ")" (replace-regexp-in-string
  78. ", " " " (replace-regexp-in-string
  79. "'" "\"" results)))))
  80. (org-babel-chomp results))))
  81. ;; functions for interacting with comint
  82. (defvar org-babel-script-ruby-buffer nil
  83. "variable to hold the current ruby buffer")
  84. (defvar org-babel-script-python-buffer nil
  85. "variable to hold the current python buffer")
  86. (defun org-babel-script-interpreter-buffer (interpreter)
  87. (intern (format "org-babel-script-%s-buffer" interpreter)))
  88. (defun org-babel-script-initiate-session (interpreter)
  89. "If there is not a current inferior-process-buffer for
  90. INTERPRETER then create one. Return the buffer in which the
  91. session has been created."
  92. (save-window-excursion
  93. (let ((buffer (org-babel-script-interpreter-buffer interpreter)))
  94. (unless (and (buffer-live-p buffer) (get-buffer buffer))
  95. (case (intern interpreter)
  96. ('ruby (funcall #'run-ruby))
  97. ('python (funcall #'run-python)))))))
  98. (defun org-babel-script-wait-for-output (interpreter)
  99. "Wait until output arrives"
  100. (save-window-excursion
  101. (save-match-data
  102. (set-buffer (org-babel-script-initiate-session interpreter))
  103. (while (progn
  104. (goto-char comint-last-input-end)
  105. (not (re-search-forward comint-prompt-regexp nil t)))
  106. (accept-process-output (get-buffer-process (current-buffer)))))))
  107. (defun org-babel-script-input-command (interpreter cmd)
  108. "Pass CMD to INTERPRETER"
  109. (comint-send-string (get-buffer-process (org-babel-script-initiate-session interpreter)) (concat cmd "\n"))
  110. (org-babel-script-wait-for-output interpreter))
  111. (defun org-babel-script-command-to-string (interpreter cmd)
  112. (let ((buffer (org-babel-script-interpreter-buffer interpreter)))
  113. (org-babel-script-input-command interpreter cmd)
  114. (org-babel-script-last-value interpreter)))
  115. (defun org-babel-script-last-value (interpreter)
  116. "Return the last value passed to INTERPRETER"
  117. (save-excursion
  118. (save-match-data
  119. (set-buffer (org-babel-script-initiate-session interpreter))
  120. (goto-char (process-mark (get-buffer-process (current-buffer))))
  121. (forward-line 0)
  122. (org-babel-clean-text-properties (buffer-substring comint-last-input-end (- (point) 1))))))
  123. (provide 'org-babel-script)
  124. ;;; org-babel-script.el ends here