2009-05-24 16:38:01 -04:00
|
|
|
;;; org-babel-script.el --- org-babel functions for scripting languages
|
2009-02-22 16:19:32 -05:00
|
|
|
|
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-05-24 16:38:01 -04:00
|
|
|
;; Org-Babel support for evaluating ruby, and python source code.
|
2009-02-22 16:19:32 -05:00
|
|
|
|
|
|
|
;;; Code:
|
2009-05-24 16:38:01 -04:00
|
|
|
(require 'org-babel)
|
2009-02-22 16:19:32 -05:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-script-add-interpreter (var cmds)
|
2009-02-22 16:19:32 -05:00
|
|
|
(set-default var cmds)
|
|
|
|
(mapc (lambda (cmd)
|
2009-06-02 19:26:57 -04:00
|
|
|
(org-babel-add-interpreter cmd)
|
2009-02-22 16:19:32 -05:00
|
|
|
(eval
|
2009-05-24 16:38:01 -04:00
|
|
|
`(defun ,(intern (concat "org-babel-execute:" cmd)) (body params)
|
|
|
|
,(concat "Evaluate a block of " cmd " script with org-babel. This function is
|
|
|
|
called by `org-babel-execute-src-block'. This function is an
|
|
|
|
automatically generated wrapper for `org-babel-script-execute'.")
|
|
|
|
(org-babel-script-execute ,cmd body params))))
|
2009-02-22 16:19:32 -05:00
|
|
|
cmds))
|
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defvar org-babel-script-ruby-wrapper-method
|
2009-04-02 23:02:56 -04:00
|
|
|
"
|
|
|
|
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
|
|
|
")
|
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defvar org-babel-script-python-wrapper-method
|
2009-04-02 23:02:56 -04:00
|
|
|
"
|
|
|
|
def main():
|
|
|
|
%s
|
|
|
|
|
|
|
|
print main()")
|
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defcustom org-babel-script-interpreters '("ruby" "python")
|
2009-02-22 16:19:32 -05:00
|
|
|
"List of interpreters of scripting languages which can be
|
2009-05-24 16:38:01 -04:00
|
|
|
executed through org-babel."
|
|
|
|
:group 'org-babel
|
|
|
|
:set 'org-babel-script-add-interpreter)
|
2009-02-22 16:19:32 -05:00
|
|
|
|
2009-06-02 19:26:57 -04:00
|
|
|
(mapc #'org-babel-add-interpreter org-babel-script-interpreters)
|
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-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-05-24 16:38:01 -04:00
|
|
|
(let ((vars (org-babel-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)
|
2009-05-24 16:38:01 -04:00
|
|
|
('ruby org-babel-script-ruby-wrapper-method)
|
|
|
|
('python org-babel-script-python-wrapper-method))
|
2009-04-02 23:02:56 -04:00
|
|
|
(concat
|
|
|
|
(mapconcat ;; define any variables
|
|
|
|
(lambda (pair)
|
|
|
|
(format "\t%s=%s"
|
|
|
|
(car pair)
|
2009-05-24 16:38:01 -04:00
|
|
|
(org-babel-script-var-to-ruby/python (cdr pair))))
|
2009-04-02 23:02:56 -04:00
|
|
|
vars "\n")
|
|
|
|
"\n"
|
2009-03-28 22:38:45 -04:00
|
|
|
(let ((body-lines (split-string body "[\n\r]+" t)))
|
2009-05-22 11:26:26 -04:00
|
|
|
(concat
|
|
|
|
(mapconcat (lambda (line) (format "\t%s" line)) (butlast body-lines) "\n")
|
|
|
|
(format "\n\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-05-14 09:53:52 -04:00
|
|
|
;; (message (format "shell output = %s" (buffer-string))) ;; debug results
|
2009-05-24 16:38:01 -04:00
|
|
|
(org-babel-script-table-or-results (buffer-string))))))
|
2009-03-28 17:49:08 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-script-var-to-ruby/python (var)
|
2009-04-02 23:02:56 -04:00
|
|
|
"Convert an elisp var into a string of ruby or python source
|
|
|
|
code specifying a var of the same value."
|
|
|
|
(if (listp var)
|
2009-05-24 16:38:01 -04:00
|
|
|
(concat "[" (mapconcat #'org-babel-script-var-to-ruby/python var ", ") "]")
|
2009-04-02 23:02:56 -04:00
|
|
|
(format "%S" var)))
|
2009-02-22 16:19:32 -05:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-script-table-or-results (results)
|
2009-03-28 22:38:45 -04:00
|
|
|
"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-05-24 16:38:01 -04:00
|
|
|
(setq results (org-babel-chomp results))
|
|
|
|
(org-babel-read
|
2009-04-02 23:02:56 -04:00
|
|
|
(if (string-match "^\\[.+\\]$" results)
|
|
|
|
;; somewhat hacky, but thanks to similarities between languages
|
|
|
|
;; it seems to work
|
2009-05-24 16:38:01 -04:00
|
|
|
(org-babel-read
|
2009-05-11 22:41:56 -04:00
|
|
|
(replace-regexp-in-string
|
2009-05-14 09:53:52 -04:00
|
|
|
"\\[" "(" (replace-regexp-in-string
|
|
|
|
"\\]" ")" (replace-regexp-in-string
|
|
|
|
", " " " (replace-regexp-in-string
|
|
|
|
"'" "\"" results)))))
|
2009-05-24 16:38:01 -04:00
|
|
|
(org-babel-chomp results))))
|
2009-03-28 22:38:45 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(provide 'org-babel-script)
|
|
|
|
;;; org-babel-script.el ends here
|