ob-maxima.el 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. ;;; ob-maxima.el --- org-babel functions for maxima evaluation
  2. ;; Copyright (C) 2009-2012 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. (defun org-babel-maxima-expand (body params)
  32. "Expand a block of Maxima code according to its header arguments."
  33. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  34. (mapconcat 'identity
  35. (list
  36. ;; graphic output
  37. (let ((graphic-file (org-babel-maxima-graphical-output-file params)))
  38. (if graphic-file
  39. (format
  40. "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
  41. graphic-file)
  42. ""))
  43. ;; variables
  44. (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
  45. ;; body
  46. body
  47. "gnuplot_close ()$")
  48. "\n")))
  49. (defun org-babel-execute:maxima (body params)
  50. "Execute a block of Maxima entries with org-babel. This function is
  51. called by `org-babel-execute-src-block'."
  52. (message "executing Maxima source code block")
  53. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  54. (result
  55. (let* ((cmdline (cdr (assoc :cmdline params)))
  56. (in-file (org-babel-temp-file "maxima-" ".max"))
  57. (cmd (format "maxima --very-quiet -r 'batchload(%S)$' %s"
  58. in-file cmdline)))
  59. (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
  60. (message cmd)
  61. ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
  62. (mapconcat
  63. #'identity
  64. (delq nil
  65. (mapcar (lambda (line)
  66. (unless (or (string-match "batch" line)
  67. (string-match "^rat: replaced .*$" line)
  68. (= 0 (length line)))
  69. line))
  70. (split-string raw "[\r\n]"))) "\n"))
  71. (org-babel-eval cmd "")))))
  72. (if (org-babel-maxima-graphical-output-file params)
  73. nil
  74. (if (or (member "scalar" result-params)
  75. (member "verbatim" result-params)
  76. (member "output" result-params))
  77. result
  78. (let ((tmp-file (org-babel-temp-file "maxima-res-")))
  79. (with-temp-file tmp-file (insert result))
  80. (org-babel-import-elisp-from-file tmp-file))))))
  81. (defun org-babel-prep-session:maxima (session params)
  82. (error "Maxima does not support sessions"))
  83. (defun org-babel-maxima-var-to-maxima (pair)
  84. "Convert an elisp val into a string of maxima code specifying a var
  85. of the same value."
  86. (let ((var (car pair))
  87. (val (cdr pair)))
  88. (when (symbolp val)
  89. (setq val (symbol-name val))
  90. (when (= (length val) 1)
  91. (setq val (string-to-char val))))
  92. (format "%S: %s$" var
  93. (org-babel-maxima-elisp-to-maxima val))))
  94. (defun org-babel-maxima-graphical-output-file (params)
  95. "Name of file to which maxima should send graphical output."
  96. (and (member "graphics" (cdr (assq :result-params params)))
  97. (cdr (assq :file params))))
  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