2009-02-22 16:19:32 -05:00
|
|
|
;;; litorgy-script.el --- litorgy functions for script execution
|
|
|
|
|
2009-03-29 18:08:53 -04:00
|
|
|
;; Copyright (C) 2009 Eric Schulte
|
2009-02-22 16:19:32 -05:00
|
|
|
|
2009-03-29 18:08:53 -04:00
|
|
|
;; Author: Eric Schulte
|
2009-02-22 16:19:32 -05:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
2009-03-28 22:38:45 -04:00
|
|
|
;; Litorgy support for evaluating ruby, and python source code.
|
2009-02-22 16:19:32 -05:00
|
|
|
|
|
|
|
;;; Code:
|
|
|
|
(require 'litorgy)
|
|
|
|
|
|
|
|
(defun litorgy-script-add-interpreter (var cmds)
|
|
|
|
(set-default var cmds)
|
|
|
|
(mapc (lambda (cmd)
|
|
|
|
(setq litorgy-interpreters (cons cmd litorgy-interpreters))
|
|
|
|
(eval
|
|
|
|
`(defun ,(intern (concat "litorgy-execute:" cmd)) (body params)
|
2009-03-28 17:49:08 -04:00
|
|
|
,(concat "Evaluate a block of " cmd " script with litorgy. This function is
|
|
|
|
called by `litorgy-execute-src-block'. This function is an
|
|
|
|
automatically generated wrapper for `litorgy-script-execute'.")
|
2009-02-22 16:19:32 -05:00
|
|
|
(litorgy-script-execute ,cmd body params))))
|
|
|
|
cmds))
|
|
|
|
|
2009-04-02 23:02:56 -04:00
|
|
|
(defvar litorgy-script-ruby-wrapper-method
|
|
|
|
"
|
|
|
|
def main
|
|
|
|
%s
|
|
|
|
end
|
2009-05-11 22:41:56 -04:00
|
|
|
results = main()
|
|
|
|
puts (results.class == String) ? results : results.inspect
|
2009-04-02 23:02:56 -04:00
|
|
|
")
|
|
|
|
|
|
|
|
(defvar litorgy-script-python-wrapper-method
|
|
|
|
"
|
|
|
|
def main():
|
|
|
|
%s
|
|
|
|
|
|
|
|
print main()")
|
|
|
|
|
2009-03-28 22:38:45 -04:00
|
|
|
(defcustom litorgy-script-interpreters '("ruby" "python")
|
2009-02-22 16:19:32 -05:00
|
|
|
"List of interpreters of scripting languages which can be
|
|
|
|
executed through litorgy."
|
|
|
|
:group 'litorgy
|
|
|
|
:set 'litorgy-script-add-interpreter)
|
|
|
|
|
|
|
|
(defun litorgy-script-execute (cmd body params)
|
2009-03-29 18:08:53 -04:00
|
|
|
"Run CMD on BODY obeying any options set with PARAMS."
|
2009-03-28 17:49:08 -04:00
|
|
|
(message (format "executing %s code block..." cmd))
|
2009-04-02 20:58:04 -04:00
|
|
|
(let ((vars (litorgy-ref-variables params)))
|
2009-03-28 17:49:08 -04:00
|
|
|
(save-window-excursion
|
|
|
|
(with-temp-buffer
|
2009-04-02 23:02:56 -04:00
|
|
|
(insert
|
|
|
|
(format
|
|
|
|
(case (intern cmd)
|
|
|
|
('ruby litorgy-script-ruby-wrapper-method)
|
|
|
|
('python litorgy-script-python-wrapper-method))
|
|
|
|
(concat
|
|
|
|
(mapconcat ;; define any variables
|
|
|
|
(lambda (pair)
|
|
|
|
(format "\t%s=%s"
|
|
|
|
(car pair)
|
|
|
|
(litorgy-script-var-to-ruby/python (cdr pair))))
|
|
|
|
vars "\n")
|
|
|
|
"\n"
|
2009-03-28 22:38:45 -04:00
|
|
|
(let ((body-lines (split-string body "[\n\r]+" t)))
|
2009-04-02 23:02:56 -04:00
|
|
|
(mapconcat
|
2009-03-28 22:38:45 -04:00
|
|
|
(lambda (line)
|
2009-04-02 23:02:56 -04:00
|
|
|
(format "\t%s\n" line))
|
|
|
|
(butlast body-lines) "\n")
|
|
|
|
(format "\treturn %s\n" (car (last body-lines)))))))
|
2009-04-23 18:57:16 -04:00
|
|
|
;; (message (buffer-substring (point-min) (point-max))) ;; debug script
|
2009-03-28 17:49:08 -04:00
|
|
|
(shell-command-on-region (point-min) (point-max) cmd nil 'replace)
|
2009-04-23 18:57:16 -04:00
|
|
|
;; (message (buffer-string)) ;; debug results
|
2009-03-28 22:38:45 -04:00
|
|
|
(litorgy-script-table-or-results (buffer-string))))))
|
2009-03-28 17:49:08 -04:00
|
|
|
|
2009-04-02 23:02:56 -04:00
|
|
|
(defun litorgy-script-var-to-ruby/python (var)
|
|
|
|
"Convert an elisp var into a string of ruby or python source
|
|
|
|
code specifying a var of the same value."
|
|
|
|
(if (listp var)
|
|
|
|
(concat "[" (mapconcat #'litorgy-script-var-to-ruby/python var ", ") "]")
|
|
|
|
(format "%S" var)))
|
2009-02-22 16:19:32 -05:00
|
|
|
|
2009-03-28 22:38:45 -04:00
|
|
|
(defun litorgy-script-table-or-results (results)
|
|
|
|
"If the results look like a table, then convert them into an
|
2009-03-29 18:08:53 -04:00
|
|
|
Emacs-lisp table, otherwise return the results as a string."
|
2009-04-02 23:02:56 -04:00
|
|
|
(litorgy-read
|
|
|
|
(if (string-match "^\\[.+\\]$" results)
|
|
|
|
;; somewhat hacky, but thanks to similarities between languages
|
|
|
|
;; it seems to work
|
2009-05-11 22:41:56 -04:00
|
|
|
(litorgy-read
|
|
|
|
(replace-regexp-in-string
|
2009-04-02 23:02:56 -04:00
|
|
|
"\\[" "(" (replace-regexp-in-string
|
|
|
|
"\\]" ")" (replace-regexp-in-string
|
|
|
|
", " " " (replace-regexp-in-string
|
2009-05-11 22:41:56 -04:00
|
|
|
"'" "\"" results)))))
|
|
|
|
;; strip trailing endline
|
|
|
|
(progn
|
|
|
|
(while (string= "\n" (substring results -1))
|
|
|
|
(setq results (substring results 0 -1)))
|
|
|
|
results))))
|
2009-03-28 22:38:45 -04:00
|
|
|
|
2009-02-22 16:19:32 -05:00
|
|
|
(provide 'litorgy-script)
|
|
|
|
;;; litorgy-script.el ends here
|