litorgy-script.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. ;;; litorgy-script.el --- litorgy functions for script execution
  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. ;; Litorgy support for evaluating ruby, and python source code.
  24. ;;; Code:
  25. (require 'litorgy)
  26. (defun litorgy-script-add-interpreter (var cmds)
  27. (set-default var cmds)
  28. (mapc (lambda (cmd)
  29. (setq litorgy-interpreters (cons cmd litorgy-interpreters))
  30. (eval
  31. `(defun ,(intern (concat "litorgy-execute:" cmd)) (body params)
  32. ,(concat "Evaluate a block of " cmd " script with litorgy. This function is
  33. called by `litorgy-execute-src-block'. This function is an
  34. automatically generated wrapper for `litorgy-script-execute'.")
  35. (litorgy-script-execute ,cmd body params))))
  36. cmds))
  37. (defvar litorgy-script-ruby-wrapper-method
  38. "
  39. def main
  40. %s
  41. end
  42. puts main().inspect
  43. ")
  44. (defvar litorgy-script-python-wrapper-method
  45. "
  46. def main():
  47. %s
  48. print main()")
  49. (defcustom litorgy-script-interpreters '("ruby" "python")
  50. "List of interpreters of scripting languages which can be
  51. executed through litorgy."
  52. :group 'litorgy
  53. :set 'litorgy-script-add-interpreter)
  54. (defun litorgy-script-execute (cmd body params)
  55. "Run CMD on BODY obeying any options set with PARAMS."
  56. (message (format "executing %s code block..." cmd))
  57. (let ((vars (litorgy-ref-variables params)))
  58. (save-window-excursion
  59. (with-temp-buffer
  60. (insert
  61. (format
  62. (case (intern cmd)
  63. ('ruby litorgy-script-ruby-wrapper-method)
  64. ('python litorgy-script-python-wrapper-method))
  65. (concat
  66. (mapconcat ;; define any variables
  67. (lambda (pair)
  68. (format "\t%s=%s"
  69. (car pair)
  70. (litorgy-script-var-to-ruby/python (cdr pair))))
  71. vars "\n")
  72. "\n"
  73. (let ((body-lines (split-string body "[\n\r]+" t)))
  74. (mapconcat
  75. (lambda (line)
  76. (format "\t%s\n" line))
  77. (butlast body-lines) "\n")
  78. (format "\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 (buffer-string)) ;; debug results
  82. (litorgy-script-table-or-results (buffer-string))))))
  83. (defun litorgy-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 #'litorgy-script-var-to-ruby/python var ", ") "]")
  88. (format "%S" var)))
  89. (defun litorgy-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. (litorgy-read
  93. (if (string-match "^\\[.+\\]$" results)
  94. ;; somewhat hacky, but thanks to similarities between languages
  95. ;; it seems to work
  96. (replace-regexp-in-string
  97. "\\[" "(" (replace-regexp-in-string
  98. "\\]" ")" (replace-regexp-in-string
  99. ", " " " (replace-regexp-in-string
  100. "'" "\"" results))))
  101. results)))
  102. (provide 'litorgy-script)
  103. ;;; litorgy-script.el ends here