ob-clojure-litterate: Fix trigger functions.
[org-mode/org-tableheadings.git] / contrib / lisp / ob-mathematica.el
blobcb35dec09c2f1bcbef4e20f691932ca4de001938
1 ;;; ob-mathematica.el --- org-babel functions for Mathematica evaluation
3 ;; Copyright (C) 2014 Yi Wang
5 ;; Authors: 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.
12 ;;; Code:
13 (require 'ob)
14 (require 'ob-ref)
15 (require 'ob-comint)
16 (require 'ob-eval)
18 (declare-function org-trim "org" (s &optional keep-lead))
20 ;; Optionally require mma.el for font lock, etc
21 (require 'mma nil 'noerror)
22 (add-to-list 'org-src-lang-modes '("mathematica" . "mma"))
24 (defvar org-babel-tangle-lang-exts)
25 (add-to-list 'org-babel-tangle-lang-exts '("mathematica" . "m"))
27 (defvar org-babel-default-header-args:mathematica '())
29 (defvar org-babel-mathematica-command "MathematicaScript -script"
30 "Name of the command for executing Mathematica code.")
32 (defvar org-babel-mathematica-command-alt "math -noprompt"
33 "Name of the command for executing Mathematica code.")
35 (defun org-babel-expand-body:mathematica (body params)
36 "Expand BODY according to PARAMS, return the expanded body."
37 (let ((vars (org-babel--get-vars params)))
38 (concat
39 (mapconcat ;; define any variables
40 (lambda (pair)
41 (format "%s=%s;"
42 (car pair)
43 (org-babel-mathematica-var-to-mathematica (cdr pair))))
44 vars "\n") "\nPrint[\n" body "\n]\n")))
46 (defun org-babel-execute:mathematica (body params)
47 "Execute a block of Mathematica code with org-babel. This function is
48 called by `org-babel-execute-src-block'"
49 (let* ((result-params (cdr (assq :result-params params)))
50 (full-body (org-babel-expand-body:mathematica body params))
51 (tmp-script-file (org-babel-temp-file "mathematica-"))
52 (cmd org-babel-mathematica-command))
53 ;; actually execute the source-code block
54 (with-temp-file tmp-script-file (insert full-body))
55 ;; (with-temp-file "/tmp/dbg" (insert full-body))
56 ((lambda (raw)
57 (if (or (member "code" result-params)
58 (member "pp" result-params)
59 (and (member "output" result-params)
60 (not (member "table" result-params))))
61 raw
62 (org-babel-script-escape (org-trim raw))))
63 (org-babel-eval (concat cmd " " tmp-script-file) ""))))
65 (defun org-babel-prep-session:mathematica (session params)
66 "This function does nothing so far"
67 (error "Currently no support for sessions"))
69 (defun org-babel-prep-session:mathematica (session body params)
70 "This function does nothing so far"
71 (error "Currently no support for sessions"))
73 (defun org-babel-mathematica-var-to-mathematica (var)
74 "Convert an elisp value to a Mathematica variable.
75 Convert an elisp value, VAR, into a string of Mathematica source code
76 specifying a variable of the same value."
77 (if (listp var)
78 (concat "{" (mapconcat #'org-babel-mathematica-var-to-mathematica var ", ") "}")
79 (format "%S" var)))
81 (provide 'ob-mathematica)