ob-mathomatic.el 5.0 KB

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