litorgy-script.el 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. (defcustom litorgy-script-interpreters '("ruby" "python")
  38. "List of interpreters of scripting languages which can be
  39. executed through litorgy."
  40. :group 'litorgy
  41. :set 'litorgy-script-add-interpreter)
  42. (defun litorgy-script-execute (cmd body params)
  43. "Run CMD on BODY obeying any options set with PARAMS."
  44. (message (format "executing %s code block..." cmd))
  45. (let ((vars (litorgy-reference-variables params)))
  46. (save-window-excursion
  47. (with-temp-buffer
  48. (when (string= "ruby" cmd) (insert "def main\n"))
  49. ;; define any variables
  50. (mapcar
  51. (lambda (pair)
  52. (insert (format "%s=%s\n"
  53. (car pair)
  54. (litorgy-script-table-to-ruby/python (cdr pair)))))
  55. vars)
  56. (case (intern cmd)
  57. ('ruby
  58. (insert body)
  59. (insert "\nend\n\nputs main.inspect\n"))
  60. ('python
  61. (insert "def main():\n")
  62. (let ((body-lines (split-string body "[\n\r]+" t)))
  63. (mapc
  64. (lambda (line)
  65. (insert (format "\t%s\n" line)))
  66. (butlast body-lines))
  67. (insert (format "\treturn %s\n" (car (last body-lines)))))
  68. (insert "\nprint main()\n")))
  69. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  70. (litorgy-script-table-or-results (buffer-string))))))
  71. (defun litorgy-script-table-to-ruby/python (table)
  72. "Convert an elisp table (nested lists) into a string of ruby
  73. source code specifying a table (nested arrays)."
  74. (if (listp table)
  75. (concat "[" (mapconcat #'litorgy-script-table-to-ruby/python table ", ") "]")
  76. (format "%S" table)))
  77. (defun litorgy-script-table-or-results (results)
  78. "If the results look like a table, then convert them into an
  79. Emacs-lisp table, otherwise return the results as a string."
  80. (when (string-match "^\\[.+\\]$" results)
  81. (setq results
  82. ;; somewhat hacky, but thanks to similarities between
  83. ;; languages it seems to work
  84. (read (replace-regexp-in-string
  85. "\\[" "(" (replace-regexp-in-string
  86. "\\]" ")" (replace-regexp-in-string
  87. ", " " " (replace-regexp-in-string
  88. "'" "\"" results)))))))
  89. results)
  90. (provide 'litorgy-script)
  91. ;;; litorgy-script.el ends here