ob-calc.el 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ;;; ob-calc.el --- org-babel functions for calc code evaluation
  2. ;; Copyright (C) 2010 Free Software Foundation, Inc
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 7.01trans
  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 calc code
  20. ;;; Code:
  21. (require 'ob)
  22. (require 'calc)
  23. (require 'calc-trail)
  24. (eval-when-compile (require 'ob-comint))
  25. (defvar org-babel-default-header-args:calc nil
  26. "Default arguments for evaluating an calc source block.")
  27. (defun org-babel-expand-body:calc (body params)
  28. "Expand BODY according to PARAMS, return the expanded body." body)
  29. (defun org-babel-execute:calc (body params)
  30. "Execute a block of calc code with Babel."
  31. (mapcar
  32. (lambda (line)
  33. (when (> (length line) 0)
  34. (if (string= "'" (substring line 0 1))
  35. (funcall (lookup-key calc-mode-map (substring line 1)) nil)
  36. (calc-push-list
  37. (list ((lambda (res)
  38. (cond
  39. ((numberp res) res)
  40. ((listp res) (error "calc error \"%s\" on input \"%s\""
  41. (cadr res) line))
  42. (t res))
  43. (if (numberp res) res (math-read-number res)))
  44. (calc-eval line)))))))
  45. (mapcar #'org-babel-trim
  46. (split-string (org-babel-expand-body:calc body params) "[\n\r]")))
  47. (save-excursion
  48. (set-buffer (get-buffer "*Calculator*"))
  49. (calc-eval (calc-top 1))))
  50. (provide 'ob-calc)
  51. ;; arch-tag: 5c57a3b7-5818-4c6c-acda-7a94831a6449
  52. ;;; ob-calc.el ends here