org-babel-ruby.el 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. ;;; org-babel-ruby.el --- org-babel functions for ruby 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 ruby source code.
  24. ;;; Requirements:
  25. ;; - ruby and irb executables :: http://www.ruby-lang.org/
  26. ;;
  27. ;; - ruby-mode :: Can be installed through ELPA, or from
  28. ;; http://github.com/eschulte/rinari/raw/master/util/ruby-mode.el
  29. ;;
  30. ;; - inf-ruby mode :: Can be installed through ELPA, or from
  31. ;; http://github.com/eschulte/rinari/raw/master/util/inf-ruby.el
  32. ;;; Code:
  33. (require 'org-babel)
  34. (require 'inf-ruby)
  35. (org-babel-add-interpreter "ruby")
  36. (add-to-list 'org-babel-tangle-langs '("ruby" "rb" "#!/usr/bin/env ruby"))
  37. (defun org-babel-execute:ruby (body params)
  38. "Execute a block of Ruby code with org-babel. This function is
  39. called by `org-babel-execute-src-block' via multiple-value-bind."
  40. (message "executing Ruby source code block")
  41. (let ((full-body (concat
  42. (mapconcat ;; define any variables
  43. (lambda (pair)
  44. (format "%s=%s"
  45. (car pair)
  46. (org-babel-ruby-var-to-ruby (cdr pair))))
  47. vars "\n") "\n" body "\n")) ;; then the source block body
  48. (session (org-babel-ruby-initiate-session session)))
  49. (org-babel-ruby-evaluate session full-body result-type)))
  50. (defun org-babel-prep-session:ruby (session params)
  51. "Prepare SESSION according to the header arguments specified in PARAMS."
  52. ;; (message "params=%S" params) ;; debugging
  53. (let* ((session (org-babel-ruby-initiate-session session))
  54. (vars (org-babel-ref-variables params))
  55. (var-lines (mapcar ;; define any variables
  56. (lambda (pair)
  57. (format "%s=%s"
  58. (car pair)
  59. (org-babel-ruby-var-to-ruby (cdr pair))))
  60. vars)))
  61. ;; (message "vars=%S" vars) ;; debugging
  62. (org-babel-comint-in-buffer session
  63. (sit-for .5) (goto-char (point-max))
  64. (mapc (lambda (var)
  65. (insert var) (comint-send-input nil t)
  66. (org-babel-comint-wait-for-output session)
  67. (sit-for .1) (goto-char (point-max))) var-lines))))
  68. ;; helper functions
  69. (defun org-babel-ruby-var-to-ruby (var)
  70. "Convert an elisp var into a string of ruby source code
  71. specifying a var of the same value."
  72. (if (listp var)
  73. (concat "[" (mapconcat #'org-babel-ruby-var-to-ruby var ", ") "]")
  74. (format "%S" var)))
  75. (defun org-babel-ruby-table-or-string (results)
  76. "If the results look like a table, then convert them into an
  77. Emacs-lisp table, otherwise return the results as a string."
  78. (org-babel-read
  79. (if (and (stringp results) (string-match "^\\[.+\\]$" results))
  80. (org-babel-read
  81. (replace-regexp-in-string
  82. "\\[" "(" (replace-regexp-in-string
  83. "\\]" ")" (replace-regexp-in-string
  84. ", " " " (replace-regexp-in-string
  85. "'" "\"" results)))))
  86. results)))
  87. (defun org-babel-ruby-initiate-session (&optional session)
  88. "If there is not a current inferior-process-buffer in SESSION
  89. then create. Return the initialized session."
  90. (unless (string= session "none")
  91. (let ((session-buffer (save-window-excursion (run-ruby nil session) (current-buffer))))
  92. (if (org-babel-comint-buffer-livep session-buffer)
  93. session-buffer
  94. (sit-for .5)
  95. (org-babel-ruby-initiate-session session)))))
  96. (defvar org-babel-ruby-last-value-eval "_"
  97. "When evaluated by Ruby this returns the return value of the last statement.")
  98. (defvar org-babel-ruby-eoe-indicator ":org_babel_ruby_eoe"
  99. "Used to indicate that evaluation is has completed.")
  100. (defvar org-babel-ruby-wrapper-method
  101. "
  102. def main()
  103. %s
  104. end
  105. results = main()
  106. File.open('%s', 'w'){ |f| f.write((results.class == String) ? results : results.inspect) }
  107. ")
  108. (defun org-babel-ruby-evaluate (buffer body &optional result-type)
  109. "Pass BODY to the Ruby process in BUFFER. If RESULT-TYPE equals
  110. 'output then return a list of the outputs of the statements in
  111. BODY, if RESULT-TYPE equals 'value then return the value of the
  112. last statement in BODY, as elisp."
  113. (if (not session)
  114. ;; external process evaluation
  115. (save-window-excursion
  116. (case result-type
  117. (output
  118. (with-temp-buffer
  119. (insert body)
  120. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  121. (shell-command-on-region (point-min) (point-max) "ruby" 'replace)
  122. (buffer-string)))
  123. (value
  124. (let ((tmp-file (make-temp-file "ruby-functional-results")))
  125. (with-temp-buffer
  126. (insert (format org-babel-ruby-wrapper-method body tmp-file))
  127. ;; (message "buffer=%s" (buffer-string)) ;; debugging
  128. (shell-command-on-region (point-min) (point-max) "ruby"))
  129. (org-babel-ruby-table-or-string
  130. (with-temp-buffer (insert-file-contents tmp-file) (buffer-string)))))))
  131. ;; comint session evaluation
  132. (let* ((full-body
  133. (mapconcat
  134. #'org-babel-chomp
  135. (list body org-babel-ruby-last-value-eval org-babel-ruby-eoe-indicator) "\n"))
  136. (raw (org-babel-comint-with-output buffer org-babel-ruby-eoe-indicator t
  137. (insert full-body) (comint-send-input nil t)))
  138. (results (cdr (member org-babel-ruby-eoe-indicator
  139. (reverse (mapcar #'org-babel-ruby-read-string
  140. (mapcar #'org-babel-trim raw)))))))
  141. (case result-type
  142. (output (mapconcat #'identity (reverse (cdr results)) "\n"))
  143. (value (org-babel-ruby-table-or-string (car results)))))))
  144. (defun org-babel-ruby-read-string (string)
  145. "Strip \\\"s from around ruby string"
  146. (if (string-match "\"\\([^\000]+\\)\"" string)
  147. (match-string 1 string)
  148. string))
  149. (provide 'org-babel-ruby)
  150. ;;; org-babel-ruby.el ends here