ob-maxima.el 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. (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. (defun org-babel-maxima-expand (body params)
  36. "Expand a block of Maxima code according to its header arguments."
  37. (let ((vars (mapcar #'cdr (org-babel-get-header params :var))))
  38. (mapconcat 'identity
  39. (list
  40. ;; graphic output
  41. (let ((graphic-file (org-babel-maxima-graphical-output-file params)))
  42. (if graphic-file
  43. (format
  44. "set_plot_option ([gnuplot_term, png]); set_plot_option ([gnuplot_out_file, %S]);"
  45. graphic-file)
  46. ""))
  47. ;; variables
  48. (mapconcat 'org-babel-maxima-var-to-maxima vars "\n")
  49. ;; body
  50. body
  51. "gnuplot_close ()$")
  52. "\n")))
  53. (defun org-babel-execute:maxima (body params)
  54. "Execute a block of Maxima entries with org-babel. This function is
  55. called by `org-babel-execute-src-block'."
  56. (message "executing Maxima source code block")
  57. (let ((result-params (split-string (or (cdr (assoc :results params)) "")))
  58. (result
  59. (let* ((cmdline (cdr (assoc :cmdline params)))
  60. (in-file (org-babel-temp-file "maxima-" ".max"))
  61. (cmd (format "%s --very-quiet -r 'batchload(%S)$' %s"
  62. org-babel-maxima-command in-file cmdline)))
  63. (with-temp-file in-file (insert (org-babel-maxima-expand body params)))
  64. (message cmd)
  65. ((lambda (raw) ;; " | grep -v batch | grep -v 'replaced' | sed '/^$/d' "
  66. (mapconcat
  67. #'identity
  68. (delq nil
  69. (mapcar (lambda (line)
  70. (unless (or (string-match "batch" line)
  71. (string-match "^rat: replaced .*$" line)
  72. (= 0 (length line)))
  73. line))
  74. (split-string raw "[\r\n]"))) "\n"))
  75. (org-babel-eval cmd "")))))
  76. (if (org-babel-maxima-graphical-output-file params)
  77. nil
  78. (if (or (member "scalar" result-params)
  79. (member "verbatim" result-params)
  80. (member "output" 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-graphical-output-file (params)
  99. "Name of file to which maxima should send graphical output."
  100. (and (member "graphics" (cdr (assq :result-params params)))
  101. (cdr (assq :file params))))
  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