ob-calc.el 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 (list ((lambda (res)
  37. (if (numberp res) res (math-read-number res)))
  38. (calc-eval line)))))))
  39. (split-string (org-babel-expand-body:calc body params) "[\n\r]"))
  40. (save-excursion
  41. (set-buffer (get-buffer "*Calculator*"))
  42. (calc-eval (calc-top 1))))
  43. (provide 'ob-calc)
  44. ;; arch-tag: 5c57a3b7-5818-4c6c-acda-7a94831a6449
  45. ;;; ob-calc.el ends here