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