ob-maxima.el 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. ;;; ob-maxima.el --- Babel Functions for Maxima -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2009-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric S Fraga
  4. ;; Eric Schulte
  5. ;; Keywords: literate programming, reproducible research, maxima
  6. ;; Homepage: https://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 <https://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. ;; 1) there is no such thing as a "session" in maxima
  23. ;; 2) we are adding the "cmdline" header argument
  24. ;;; Code:
  25. (require 'ob)
  26. (defvar org-babel-tangle-lang-exts)
  27. (add-to-list 'org-babel-tangle-lang-exts '("maxima" . "max"))
  28. (defvar org-babel-default-header-args:maxima '())
  29. (defcustom org-babel-maxima-command
  30. (if (boundp 'maxima-command) maxima-command "maxima")
  31. "Command used to call maxima on the shell."
  32. :group 'org-babel
  33. :type 'string)
  34. (defun org-babel-maxima-expand (body params)
  35. "Expand a block of Maxima code according to its header arguments."
  36. (let ((vars (org-babel--get-vars params))
  37. (epilogue (cdr (assq :epilogue params)))
  38. (prologue (cdr (assq :prologue params))))
  39. (mapconcat 'identity
  40. (list
  41. ;; Any code from the specified prologue at the start.
  42. prologue
  43. ;; graphic output
  44. (let ((graphic-file (ignore-errors (org-babel-graphical-output-file params))))
  45. (if graphic-file
  46. (format
  47. "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
  48. graphic-file)
  49. ""))
  50. ;; variables
  51. (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
  52. ;; body
  53. body
  54. ;; Any code from the specified epilogue at the end.
  55. epilogue
  56. "gnuplot_close ()$")
  57. "\n")))
  58. (defun org-babel-execute:maxima (body params)
  59. "Execute a block of Maxima entries with org-babel.
  60. This function is called by `org-babel-execute-src-block'."
  61. (message "executing Maxima source code block")
  62. (let ((result-params (split-string (or (cdr (assq :results params)) "")))
  63. (result
  64. (let* ((cmdline (or (cdr (assq :cmdline params)) ""))
  65. (in-file (org-babel-temp-file "maxima-" ".max"))
  66. (cmd (format "%s --very-quiet -r 'batchload(%S)$' %s"
  67. org-babel-maxima-command in-file cmdline)))
  68. (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
  69. (message cmd)
  70. ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
  71. (let ((raw (org-babel-eval cmd "")))
  72. (mapconcat
  73. #'identity
  74. (delq nil
  75. (mapcar (lambda (line)
  76. (unless (or (string-match "batch" line)
  77. (string-match "^rat: replaced .*$" line)
  78. (string-match "^;;; Loading #P" line)
  79. (= 0 (length line)))
  80. line))
  81. (split-string raw "[\r\n]"))) "\n")))))
  82. (if (ignore-errors (org-babel-graphical-output-file params))
  83. nil
  84. (org-babel-result-cond result-params
  85. result
  86. (let ((tmp-file (org-babel-temp-file "maxima-res-")))
  87. (with-temp-file tmp-file (insert result))
  88. (org-babel-import-elisp-from-file tmp-file))))))
  89. (defun org-babel-prep-session:maxima (_session _params)
  90. (error "Maxima does not support sessions"))
  91. (defun org-babel-maxima-var-to-maxima (pair)
  92. "Convert an elisp val into a string of maxima code specifying a var
  93. of the same value."
  94. (let ((var (car pair))
  95. (val (cdr pair)))
  96. (when (symbolp val)
  97. (setq val (symbol-name val))
  98. (when (= (length val) 1)
  99. (setq val (string-to-char val))))
  100. (format "%S: %s$" var
  101. (org-babel-maxima-elisp-to-maxima val))))
  102. (defun org-babel-maxima-elisp-to-maxima (val)
  103. "Return a string of maxima code which evaluates to VAL."
  104. (if (listp val)
  105. (concat "[" (mapconcat #'org-babel-maxima-elisp-to-maxima val ", ") "]")
  106. (format "%s" val)))
  107. (provide 'ob-maxima)
  108. ;;; ob-maxima.el ends here