ob-calc.el 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.3
  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. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  32. (var-syms (mapcar #'car vars))
  33. (var-names (mapcar #'symbol-name var-syms)))
  34. (mapc
  35. (lambda (pair)
  36. (calc-push-list (list (cdr pair)))
  37. (calc-store-into (car pair)))
  38. vars)
  39. (mapcar
  40. (lambda (line)
  41. (when (> (length line) 0)
  42. (cond
  43. ;; simple variable name
  44. ((member line var-names) (calc-recall (intern line)))
  45. ;; stack operation
  46. ((string= "'" (substring line 0 1))
  47. (funcall (lookup-key calc-mode-map (substring line 1)) nil))
  48. ;; complex expression
  49. (t
  50. (calc-push-list
  51. (list ((lambda (res)
  52. (cond
  53. ((numberp res) res)
  54. ((math-read-number res) (math-read-number res))
  55. ((listp res) (error "calc error \"%s\" on input \"%s\""
  56. (cadr res) line))
  57. (t (calc-eval
  58. (math-evaluate-expr
  59. ;; resolve user variables, calc built in
  60. ;; variables are handled automatically
  61. ;; upstream by calc
  62. (mapcar (lambda (el)
  63. (if (and (consp el) (equal 'var (car el))
  64. (member (cadr el) var-syms))
  65. (progn
  66. (calc-recall (cadr el))
  67. (prog1 (calc-top 1)
  68. (calc-pop 1)))
  69. el))
  70. ;; parse line into calc objects
  71. (first (math-read-exprs line))))))))
  72. (calc-eval line))))))))
  73. (mapcar #'org-babel-trim
  74. (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
  75. (save-excursion
  76. (set-buffer (get-buffer "*Calculator*"))
  77. (calc-eval (calc-top 1))))
  78. (provide 'ob-calc)
  79. ;; arch-tag: 5c57a3b7-5818-4c6c-acda-7a94831a6449
  80. ;;; ob-calc.el ends here