ob-mathematica.el 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. ;;; ob-mathematica.el --- org-babel functions for Mathematica evaluation
  2. ;; Copyright (C) 2014 Yi Wang
  3. ;; Authors: Yi Wang
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: https://github.com/tririver/wy-els/blob/master/ob-mathematica.el
  6. ;; Distributed under the GNU GPL v2 or later
  7. ;; Org-Babel support for evaluating Mathematica source code.
  8. ;;; Code:
  9. (require 'ob)
  10. (require 'ob-ref)
  11. (require 'ob-comint)
  12. (require 'ob-eval)
  13. (declare-function org-trim "org" (s &optional keep-lead))
  14. ;; Optionally require mma.el for font lock, etc
  15. (require 'mma nil 'noerror)
  16. (add-to-list 'org-src-lang-modes '("mathematica" . "mma"))
  17. (defvar org-babel-tangle-lang-exts)
  18. (add-to-list 'org-babel-tangle-lang-exts '("mathematica" . "m"))
  19. (defvar org-babel-default-header-args:mathematica '())
  20. (defvar org-babel-mathematica-command "MathematicaScript -script"
  21. "Name of the command for executing Mathematica code.")
  22. (defvar org-babel-mathematica-command-alt "math -noprompt"
  23. "Name of the command for executing Mathematica code.")
  24. (defun org-babel-expand-body:mathematica (body params)
  25. "Expand BODY according to PARAMS, return the expanded body."
  26. (let ((vars (org-babel--get-vars params)))
  27. (concat
  28. (mapconcat ;; define any variables
  29. (lambda (pair)
  30. (format "%s=%s;"
  31. (car pair)
  32. (org-babel-mathematica-var-to-mathematica (cdr pair))))
  33. vars "\n") "\nPrint[\n" body "\n]\n")))
  34. (defun org-babel-execute:mathematica (body params)
  35. "Execute a block of Mathematica code with org-babel. This function is
  36. called by `org-babel-execute-src-block'"
  37. (let* ((result-params (cdr (assq :result-params params)))
  38. (full-body (org-babel-expand-body:mathematica body params))
  39. (tmp-script-file (org-babel-temp-file "mathematica-"))
  40. (cmd org-babel-mathematica-command))
  41. ;; actually execute the source-code block
  42. (with-temp-file tmp-script-file (insert full-body))
  43. ;; (with-temp-file "/tmp/dbg" (insert full-body))
  44. ((lambda (raw)
  45. (if (or (member "code" result-params)
  46. (member "pp" result-params)
  47. (and (member "output" result-params)
  48. (not (member "table" result-params))))
  49. raw
  50. (org-babel-script-escape (org-trim raw))))
  51. (org-babel-eval (concat cmd " " tmp-script-file) ""))))
  52. (defun org-babel-prep-session:mathematica (session params)
  53. "This function does nothing so far"
  54. (error "Currently no support for sessions"))
  55. (defun org-babel-prep-session:mathematica (session body params)
  56. "This function does nothing so far"
  57. (error "Currently no support for sessions"))
  58. (defun org-babel-mathematica-var-to-mathematica (var)
  59. "Convert an elisp value to a Mathematica variable.
  60. Convert an elisp value, VAR, into a string of Mathematica source code
  61. specifying a variable of the same value."
  62. (if (listp var)
  63. (concat "{" (mapconcat #'org-babel-mathematica-var-to-mathematica var ", ") "}")
  64. (format "%S" var)))
  65. (provide 'ob-mathematica)