1 ;;; ob-mathomatic.el --- Org-babel functions for mathomatic evaluation
3 ;; Copyright (C) 2009-2018 Free Software Foundation, Inc.
5 ;; Author: Eric S Fraga
7 ;; Luis Anaya (Mathomatic)
9 ;; Keywords: literate programming, reproducible research, mathomatic
10 ;; Homepage: https://orgmode.org
12 ;; This file is not part of GNU Emacs.
14 ;; This program is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; This program is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; Org-Babel support for evaluating mathomatic entries.
31 ;; This differs from most standard languages in that
33 ;; 1) there is no such thing as a "session" in mathomatic
35 ;; 2) we are adding the "cmdline" header argument
40 (defvar org-babel-tangle-lang-exts
)
41 (add-to-list 'org-babel-tangle-lang-exts
'("mathomatic" .
"math"))
43 (defvar org-babel-default-header-args
:mathomatic
'())
45 (defcustom org-babel-mathomatic-command
46 (if (boundp 'mathomatic-command
) mathomatic-command
"mathomatic")
47 "Command used to call mathomatic on the shell."
50 (defun org-babel-mathomatic-expand (body params
)
51 "Expand a block of Mathomatic code according to its header arguments."
52 (let ((vars (org-babel--get-vars params
)))
56 (let ((graphic-file (org-babel-mathomatic-graphical-output-file params
)))
59 ((string-match ".\.eps$" graphic-file
)
60 (format ;; Need to add command to send to file.
61 "set plot set terminal postscript eps\\;set output %S "
63 ((string-match ".\.ps$" graphic-file
)
64 (format ;; Need to add command to send to file.
65 "set plot set terminal postscript\\;set output %S "
68 ((string-match ".\.pic$" graphic-file
)
69 (format ;; Need to add command to send to file.
70 "set plot set terminal gpic\\;set output %S "
73 (format ;; Need to add command to send to file.
74 "set plot set terminal png\\;set output %S "
78 (mapconcat 'org-babel-mathomatic-var-to-mathomatic vars
"\n")
84 (defun org-babel-execute:mathomatic
(body params
)
85 "Execute a block of Mathomatic entries with org-babel. This function is
86 called by `org-babel-execute-src-block'."
87 (message "executing Mathomatic source code block")
88 (let ((result-params (split-string (or (cdr (assq :results params
)) "")))
90 (let* ((cmdline (or (cdr (assq :cmdline params
)) ""))
91 (in-file (org-babel-temp-file "mathomatic-" ".math"))
92 (cmd (format "%s -t -c -q %s %s"
93 org-babel-mathomatic-command in-file cmdline
)))
94 (with-temp-file in-file
(insert (org-babel-mathomatic-expand body params
)))
96 ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
100 (mapcar (lambda (line)
101 (unless (or (string-match "batch" line
)
102 (string-match "^rat: replaced .*$" line
)
105 (split-string raw
"[\r\n]"))) "\n"))
106 (org-babel-eval cmd
"")))))
107 (if (org-babel-mathomatic-graphical-output-file params
)
109 (if (or (member "scalar" result-params
)
110 (member "verbatim" result-params
)
111 (member "output" result-params
))
113 (let ((tmp-file (org-babel-temp-file "mathomatic-res-")))
114 (with-temp-file tmp-file
(insert result
))
115 (org-babel-import-elisp-from-file tmp-file
))))))
117 (defun org-babel-prep-session:mathomatic
(session params
)
118 (error "Mathomatic does not support sessions"))
120 (defun org-babel-mathomatic-var-to-mathomatic (pair)
121 "Convert an elisp val into a string of mathomatic code specifying a var
123 (let ((var (car pair
))
126 (setq val
(symbol-name val
))
127 (when (= (length val
) 1)
128 (setq val
(string-to-char val
))))
130 (org-babel-mathomatic-elisp-to-mathomatic val
))))
132 (defun org-babel-mathomatic-graphical-output-file (params)
133 "Name of file to which mathomatic should send graphical output."
134 (and (member "graphics" (cdr (assq :result-params params
)))
135 (cdr (assq :file params
))))
137 (defun org-babel-mathomatic-elisp-to-mathomatic (val)
138 "Return a string of mathomatic code which evaluates to VAL."
140 (mapconcat #'org-babel-mathomatic-elisp-to-mathomatic val
" ")
143 (provide 'ob-mathomatic
)
145 ;;; ob-mathomatic.el ends here