ob-maxima.el 4.3 KB

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