* doc/org.texi: More changes to :var table (some examples were wrong)
[org-mode.git] / lisp / ob-maxima.el
blobf14cf550a71375493afc0ceec1ebed5241d7d85a
1 ;;; ob-maxima.el --- org-babel functions for maxima evaluation
3 ;; Copyright (C) 2009-2011 Free Software Foundation, Inc.
5 ;; Author: Eric S Fraga
6 ;; Eric Schulte
7 ;; Keywords: literate programming, reproducible research, maxima
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
27 ;; Org-Babel support for evaluating maxima entries.
29 ;; This differs from most standard languages in that
31 ;; 1) there is no such thing as a "session" in maxima
33 ;; 2) we are adding the "cmdline" header argument
35 ;;; Code:
36 (require 'ob)
38 (defvar org-babel-tangle-lang-exts)
39 (add-to-list 'org-babel-tangle-lang-exts '("maxima" . "max"))
41 (defvar org-babel-default-header-args:maxima '())
43 (defun org-babel-maxima-expand (body params)
44 "Expand a block of Maxima code according to its header arguments."
45 (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
46 (mapconcat 'identity
47 (list
48 ;; graphic output
49 (let ((graphic-file (org-babel-maxima-graphical-output-file params)))
50 (if graphic-file
51 (format
52 "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
53 graphic-file)
54 ""))
55 ;; variables
56 (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
57 ;; body
58 body
59 "gnuplot_close ()$")
60 "\n")))
62 (defun org-babel-execute:maxima (body params)
63 "Execute a block of Maxima entries with org-babel. This function is
64 called by `org-babel-execute-src-block'."
65 (message "executing Maxima source code block")
66 (let ((result
67 (let* ((result-params (split-string (or (cdr (assoc :results params)) "")))
68 (cmdline (cdr (assoc :cmdline params)))
69 (in-file (org-babel-temp-file "maxima-" ".max"))
70 (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s"
71 in-file cmdline)))
72 (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
73 (message cmd)
74 ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
75 (mapconcat
76 #'identity
77 (delq nil
78 (mapcar (lambda (line)
79 (unless (or (string-match "batch" line)
80 (string-match "^rat: replaced .*$" line)
81 (= 0 (length line)))
82 line))
83 (split-string raw "[\r\n]"))) "\n"))
84 (org-babel-eval cmd "")))))
85 (if (org-babel-maxima-graphical-output-file params)
86 nil
87 (if (or (member "scalar" result-params)
88 (member "verbatim" result-params)
89 (member "output" result-params))
90 result
91 (let ((tmp-file (org-babel-temp-file "maxima-res-")))
92 (with-temp-file tmp-file (insert result))
93 (org-babel-import-elisp-from-file tmp-file))))))
96 (defun org-babel-prep-session:maxima (session params)
97 (error "Maxima does not support sessions"))
99 (defun org-babel-maxima-var-to-maxima (pair)
100 "Convert an elisp val into a string of maxima code specifying a var
101 of the same value."
102 (let ((var (car pair))
103 (val (cdr pair)))
104 (when (symbolp val)
105 (setq val (symbol-name val))
106 (when (= (length val) 1)
107 (setq val (string-to-char val))))
108 (format "%S: %s$" var
109 (org-babel-maxima-elisp-to-maxima val))))
111 (defun org-babel-maxima-graphical-output-file (params)
112 "Name of file to which maxima should send graphical output."
113 (and (member "graphics" (cdr (assq :result-params params)))
114 (cdr (assq :file params))))
116 (defun org-babel-maxima-elisp-to-maxima (val)
117 "Return a string of maxima code which evaluates to VAL."
118 (if (listp val)
119 (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]")
120 (format "%s" val)))
123 (provide 'ob-maxima)
127 ;;; ob-maxima.el ends here