org-babel-script.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. (org-babel-add-interpreter cmd)
  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. (mapc #'org-babel-add-interpreter org-babel-script-interpreters)
  56. (defun org-babel-script-execute (cmd body params)
  57. "Run CMD on BODY obeying any options set with PARAMS."
  58. (message (format "executing %s code block..." cmd))
  59. (let ((vars (org-babel-ref-variables params)))
  60. (save-window-excursion
  61. (with-temp-buffer
  62. (insert
  63. (format
  64. (case (intern cmd)
  65. ('ruby org-babel-script-ruby-wrapper-method)
  66. ('python org-babel-script-python-wrapper-method))
  67. (concat
  68. (mapconcat ;; define any variables
  69. (lambda (pair)
  70. (format "\t%s=%s"
  71. (car pair)
  72. (org-babel-script-var-to-ruby/python (cdr pair))))
  73. vars "\n")
  74. "\n"
  75. (let ((body-lines (split-string body "[\n\r]+" t)))
  76. (concat
  77. (mapconcat (lambda (line) (format "\t%s" line)) (butlast body-lines) "\n")
  78. (format "\n\treturn %s\n" (car (last body-lines))))))))
  79. ;; (message (buffer-substring (point-min) (point-max))) ;; debug script
  80. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  81. ;; (message (format "shell output = %s" (buffer-string))) ;; debug results
  82. (org-babel-script-table-or-results (buffer-string))))))
  83. (defun org-babel-script-var-to-ruby/python (var)
  84. "Convert an elisp var into a string of ruby or python source
  85. code specifying a var of the same value."
  86. (if (listp var)
  87. (concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
  88. (format "%S" var)))
  89. (defun org-babel-script-table-or-results (results)
  90. "If the results look like a table, then convert them into an
  91. Emacs-lisp table, otherwise return the results as a string."
  92. (setq results (org-babel-chomp results))
  93. (org-babel-read
  94. (if (string-match "^\\[.+\\]$" results)
  95. ;; somewhat hacky, but thanks to similarities between languages
  96. ;; it seems to work
  97. (org-babel-read
  98. (replace-regexp-in-string
  99. "\\[" "(" (replace-regexp-in-string
  100. "\\]" ")" (replace-regexp-in-string
  101. ", " " " (replace-regexp-in-string
  102. "'" "\"" results)))))
  103. (org-babel-chomp results))))
  104. (provide 'org-babel-script)
  105. ;;; org-babel-script.el ends here