org-babel-script.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. (defun org-babel-script-add-interpreter (var cmds)
  27. (set-default var cmds)
  28. (mapc (lambda (cmd)
  29. (setq org-babel-interpreters (cons cmd org-babel-interpreters))
  30. (eval
  31. `(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
  32. ,(concat "Evaluate a block of " cmd " script with org-babel. This function is
  33. called by `org-babel-execute-src-block'. This function is an
  34. automatically generated wrapper for `org-babel-script-execute'.")
  35. (org-babel-script-execute ,cmd body params))))
  36. cmds))
  37. (defvar org-babel-script-ruby-wrapper-method
  38. "
  39. def main
  40. %s
  41. end
  42. results = main()
  43. puts (results.class == String) ? results : results.inspect
  44. ")
  45. (defvar org-babel-script-python-wrapper-method
  46. "
  47. def main():
  48. %s
  49. print main()")
  50. (defcustom org-babel-script-interpreters '("ruby" "python")
  51. "List of interpreters of scripting languages which can be
  52. executed through org-babel."
  53. :group 'org-babel
  54. :set 'org-babel-script-add-interpreter)
  55. (defun org-babel-script-execute (cmd body params)
  56. "Run CMD on BODY obeying any options set with PARAMS."
  57. (message (format "executing %s code block..." cmd))
  58. (let ((vars (org-babel-ref-variables params)))
  59. (save-window-excursion
  60. (with-temp-buffer
  61. (insert
  62. (format
  63. (case (intern cmd)
  64. ('ruby org-babel-script-ruby-wrapper-method)
  65. ('python org-babel-script-python-wrapper-method))
  66. (concat
  67. (mapconcat ;; define any variables
  68. (lambda (pair)
  69. (format "\t%s=%s"
  70. (car pair)
  71. (org-babel-script-var-to-ruby/python (cdr pair))))
  72. vars "\n")
  73. "\n"
  74. (let ((body-lines (split-string body "[\n\r]+" t)))
  75. (concat
  76. (mapconcat (lambda (line) (format "\t%s" line)) (butlast body-lines) "\n")
  77. (format "\n\treturn %s\n" (car (last body-lines))))))))
  78. ;; (message (buffer-substring (point-min) (point-max))) ;; debug script
  79. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  80. ;; (message (format "shell output = %s" (buffer-string))) ;; debug results
  81. (org-babel-script-table-or-results (buffer-string))))))
  82. (defun org-babel-script-var-to-ruby/python (var)
  83. "Convert an elisp var into a string of ruby or python source
  84. code specifying a var of the same value."
  85. (if (listp var)
  86. (concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
  87. (format "%S" var)))
  88. (defun org-babel-script-table-or-results (results)
  89. "If the results look like a table, then convert them into an
  90. Emacs-lisp table, otherwise return the results as a string."
  91. (setq results (org-babel-chomp results))
  92. (org-babel-read
  93. (if (string-match "^\\[.+\\]$" results)
  94. ;; somewhat hacky, but thanks to similarities between languages
  95. ;; it seems to work
  96. (org-babel-read
  97. (replace-regexp-in-string
  98. "\\[" "(" (replace-regexp-in-string
  99. "\\]" ")" (replace-regexp-in-string
  100. ", " " " (replace-regexp-in-string
  101. "'" "\"" results)))))
  102. (org-babel-chomp results))))
  103. (provide 'org-babel-script)
  104. ;;; org-babel-script.el ends here