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