ob-maxima.el 4.5 KB

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