ob-calc.el 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. ;;; ob-calc.el --- org-babel functions for calc code evaluation
  2. ;; Copyright (C) 2010-2012 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; Org-Babel support for evaluating calc code
  19. ;;; Code:
  20. (require 'ob)
  21. (require 'calc)
  22. (unless (featurep 'xemacs)
  23. (require 'calc-trail)
  24. (require 'calc-store))
  25. (eval-when-compile (require 'ob-comint))
  26. (declare-function calc-store-into "calc-store" (&optional var))
  27. (declare-function calc-recall "calc-store" (&optional var))
  28. (declare-function math-evaluate-expr "calc-ext" (x))
  29. (defvar org-babel-default-header-args:calc nil
  30. "Default arguments for evaluating an calc source block.")
  31. (defun org-babel-expand-body:calc (body params)
  32. "Expand BODY according to PARAMS, return the expanded body." body)
  33. (defun org-babel-execute:calc (body params)
  34. "Execute a block of calc code with Babel."
  35. (unless (get-buffer "*Calculator*")
  36. (save-window-excursion (calc) (calc-quit)))
  37. (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
  38. (var-syms (mapcar #'car vars))
  39. (var-names (mapcar #'symbol-name var-syms)))
  40. (mapc
  41. (lambda (pair)
  42. (calc-push-list (list (cdr pair)))
  43. (calc-store-into (car pair)))
  44. vars)
  45. (mapc
  46. (lambda (line)
  47. (when (> (length line) 0)
  48. (cond
  49. ;; simple variable name
  50. ((member line var-names) (calc-recall (intern line)))
  51. ;; stack operation
  52. ((string= "'" (substring line 0 1))
  53. (funcall (lookup-key calc-mode-map (substring line 1)) nil))
  54. ;; complex expression
  55. (t
  56. (calc-push-list
  57. (list ((lambda (res)
  58. (cond
  59. ((numberp res) res)
  60. ((math-read-number res) (math-read-number res))
  61. ((listp res) (error "calc error \"%s\" on input \"%s\""
  62. (cadr res) line))
  63. (t (replace-regexp-in-string
  64. "'\\[" "["
  65. (calc-eval
  66. (math-evaluate-expr
  67. ;; resolve user variables, calc built in
  68. ;; variables are handled automatically
  69. ;; upstream by calc
  70. (mapcar #'ob-calc-maybe-resolve-var
  71. ;; parse line into calc objects
  72. (car (math-read-exprs line)))))))))
  73. (calc-eval line))))))))
  74. (mapcar #'org-babel-trim
  75. (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
  76. (save-excursion
  77. (with-current-buffer (get-buffer "*Calculator*")
  78. (calc-eval (calc-top 1)))))
  79. (defvar var-syms) ; Dynamically scoped from org-babel-execute:calc
  80. (defun ob-calc-maybe-resolve-var (el)
  81. (if (consp el)
  82. (if (and (equal 'var (car el)) (member (cadr el) var-syms))
  83. (progn
  84. (calc-recall (cadr el))
  85. (prog1 (calc-top 1)
  86. (calc-pop 1)))
  87. (mapcar #'ob-calc-maybe-resolve-var el))
  88. el))
  89. (provide 'ob-calc)
  90. ;;; ob-calc.el ends here