1 ;;; ob-mathematica.el --- org-babel functions for Mathematica evaluation
3 ;; Copyright (C) 2014 Yi Wang
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: https://github.com/tririver/wy-els/blob/master/ob-mathematica.el
8 ;; Distributed under the GNU GPL v2 or later
10 ;; Org-Babel support for evaluating Mathematica source code.
17 ;; Optionally require mma.el for font lock, etc
18 (require 'mma nil
'noerror
)
19 (add-to-list 'org-src-lang-modes
'("mathematica" .
"mma"))
21 (defvar org-babel-tangle-lang-exts
)
22 (add-to-list 'org-babel-tangle-lang-exts
'("mathematica" .
"m"))
24 (defvar org-babel-default-header-args
:mathematica
'())
26 (defvar org-babel-mathematica-command
"MathematicaScript -script"
27 "Name of the command for executing Mathematica code.")
29 (defvar org-babel-mathematica-command-alt
"math -noprompt"
30 "Name of the command for executing Mathematica code.")
32 (defun org-babel-expand-body:mathematica
(body params
)
33 "Expand BODY according to PARAMS, return the expanded body."
34 (let ((vars (org-babel--get-vars params
)))
36 (mapconcat ;; define any variables
40 (org-babel-mathematica-var-to-mathematica (cdr pair
))))
41 vars
"\n") "\nPrint[\n" body
"\n]\n")))
43 (defun org-babel-execute:mathematica
(body params
)
44 "Execute a block of Mathematica code with org-babel. This function is
45 called by `org-babel-execute-src-block'"
46 (let* ((result-params (cdr (assoc :result-params params
)))
47 (full-body (org-babel-expand-body:mathematica body params
))
48 (tmp-script-file (org-babel-temp-file "mathematica-"))
49 (cmd org-babel-mathematica-command
))
50 ;; actually execute the source-code block
51 (with-temp-file tmp-script-file
(insert full-body
))
52 ;; (with-temp-file "/tmp/dbg" (insert full-body))
54 (if (or (member "code" result-params
)
55 (member "pp" result-params
)
56 (and (member "output" result-params
)
57 (not (member "table" result-params
))))
59 (org-babel-script-escape (org-babel-trim raw
))))
60 (org-babel-eval (concat cmd
" " tmp-script-file
) ""))))
62 (defun org-babel-prep-session:mathematica
(session params
)
63 "This function does nothing so far"
64 (error "Currently no support for sessions"))
66 (defun org-babel-prep-session:mathematica
(session body params
)
67 "This function does nothing so far"
68 (error "Currently no support for sessions"))
70 (defun org-babel-mathematica-var-to-mathematica (var)
71 "Convert an elisp value to a Mathematica variable.
72 Convert an elisp value, VAR, into a string of Mathematica source code
73 specifying a variable of the same value."
75 (concat "{" (mapconcat #'org-babel-mathematica-var-to-mathematica var
", ") "}")
78 (provide 'ob-mathematica
)