litorgy-script.el 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. ;;; litorgy-script.el --- litorgy functions for script execution
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
  3. ;; Author: Eric Schulte, Dan Davison, Austin F. Frank
  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 shell, 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 '("sh" "bash" "zsh" "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. TODO: currently the params part is not implemented"
  45. (message (format "executing %s code block..." cmd))
  46. (let ((vars (litorgy-reference-variables params)))
  47. (save-window-excursion
  48. (with-temp-buffer
  49. ;; define any variables
  50. (mapcar
  51. (lambda (pair)
  52. (case (intern cmd)
  53. ((sh bash zsh) ;; TODO support table assignment in shell scripts
  54. (error (format "table assignment is not supported for %s" cmd)))
  55. ((ruby python)
  56. (insert (format "%s=%s\n"
  57. (car pair)
  58. (litorgy-table-to-ruby/python (cdr pair)))))
  59. ))
  60. vars)
  61. (insert body)
  62. (shell-command-on-region (point-min) (point-max) cmd nil 'replace)
  63. (message "finished executing source block")
  64. (buffer-string)))))
  65. (defun litorgy-table-to-ruby/python (table)
  66. "Convert an elisp table (nested lists) into a string of ruby
  67. source code specifying a table (nested arrays)."
  68. (if (listp table)
  69. (concat "[" (mapconcat #'litorgy-table-to-ruby/python table ", ") "]")
  70. (format "%S" table)))
  71. (provide 'litorgy-script)
  72. ;;; litorgy-script.el ends here