Merge branch 'maint'
[org-mode.git] / lisp / ob-maxima.el
blob224b3605035a07cc199d0ba8c31bf8afcd2bd39b
1 ;;; ob-maxima.el --- Babel Functions for Maxima -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2017 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 <https://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 (defcustom org-babel-maxima-command
44 (if (boundp 'maxima-command) maxima-command "maxima")
45 "Command used to call maxima on the shell."
46 :group 'org-babel
47 :type 'string)
49 (defun org-babel-maxima-expand (body params)
50 "Expand a block of Maxima code according to its header arguments."
51 (let ((vars (org-babel--get-vars params))
52 (epilogue (cdr (assq :epilogue params)))
53 (prologue (cdr (assq :prologue params))))
54 (mapconcat 'identity
55 (list
56 ;; Any code from the specified prologue at the start.
57 prologue
58 ;; graphic output
59 (let ((graphic-file (ignore-errors (org-babel-graphical-output-file params))))
60 (if graphic-file
61 (format
62 "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
63 graphic-file)
64 ""))
65 ;; variables
66 (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
67 ;; body
68 body
69 ;; Any code from the specified epilogue at the end.
70 epilogue
71 "gnuplot_close ()$")
72 "\n")))
74 (defun org-babel-execute:maxima (body params)
75 "Execute a block of Maxima entries with org-babel.
76 This function is called by `org-babel-execute-src-block'."
77 (message "executing Maxima source code block")
78 (let ((result-params (split-string (or (cdr (assq :results params)) "")))
79 (result
80 (let* ((cmdline (or (cdr (assq :cmdline params)) ""))
81 (in-file (org-babel-temp-file "maxima-" ".max"))
82 (cmd (format "%s --very-quiet -r 'batchload(%S)$' %s"
83 org-babel-maxima-command in-file cmdline)))
84 (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
85 (message cmd)
86 ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
87 (let ((raw (org-babel-eval cmd "")))
88 (mapconcat
89 #'identity
90 (delq nil
91 (mapcar (lambda (line)
92 (unless (or (string-match "batch" line)
93 (string-match "^rat: replaced .*$" line)
94 (string-match "^;;; Loading #P" line)
95 (= 0 (length line)))
96 line))
97 (split-string raw "[\r\n]"))) "\n")))))
98 (if (ignore-errors (org-babel-graphical-output-file params))
99 nil
100 (org-babel-result-cond result-params
101 result
102 (let ((tmp-file (org-babel-temp-file "maxima-res-")))
103 (with-temp-file tmp-file (insert result))
104 (org-babel-import-elisp-from-file tmp-file))))))
107 (defun org-babel-prep-session:maxima (_session _params)
108 (error "Maxima does not support sessions"))
110 (defun org-babel-maxima-var-to-maxima (pair)
111 "Convert an elisp val into a string of maxima code specifying a var
112 of the same value."
113 (let ((var (car pair))
114 (val (cdr pair)))
115 (when (symbolp val)
116 (setq val (symbol-name val))
117 (when (= (length val) 1)
118 (setq val (string-to-char val))))
119 (format "%S: %s$" var
120 (org-babel-maxima-elisp-to-maxima val))))
122 (defun org-babel-maxima-elisp-to-maxima (val)
123 "Return a string of maxima code which evaluates to VAL."
124 (if (listp val)
125 (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]")
126 (format "%s" val)))
129 (provide 'ob-maxima)
133 ;;; ob-maxima.el ends here