Convert consecutive FSF copyright years to ranges.
[bpt/emacs.git] / lisp / org / ob-calc.el
CommitLineData
afe98dfa
CD
1;;; ob-calc.el --- org-babel functions for calc code evaluation
2
73b0cd50 3;; Copyright (C) 2010-2011 Free Software Foundation, Inc
afe98dfa
CD
4
5;; Author: Eric Schulte
6;; Keywords: literate programming, reproducible research
7;; Homepage: http://orgmode.org
acedf35c 8;; Version: 7.4
afe98dfa
CD
9
10;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25;;; Commentary:
26
27;; Org-Babel support for evaluating calc code
28
29;;; Code:
30(require 'ob)
31(require 'calc)
32(require 'calc-trail)
33(eval-when-compile (require 'ob-comint))
34
35(defvar org-babel-default-header-args:calc nil
36 "Default arguments for evaluating an calc source block.")
37
38(defun org-babel-expand-body:calc (body params)
39 "Expand BODY according to PARAMS, return the expanded body." body)
40
41(defun org-babel-execute:calc (body params)
42 "Execute a block of calc code with Babel."
acedf35c
CD
43 (unless (get-buffer "*Calculator*")
44 (save-window-excursion (calc) (calc-quit)))
45 (let* ((vars (mapcar #'cdr (org-babel-get-header params :var)))
46 (var-syms (mapcar #'car vars))
47 (var-names (mapcar #'symbol-name var-syms)))
48 (mapc
49 (lambda (pair)
50 (calc-push-list (list (cdr pair)))
51 (calc-store-into (car pair)))
52 vars)
53 (mapc
54 (lambda (line)
55 (when (> (length line) 0)
56 (cond
57 ;; simple variable name
58 ((member line var-names) (calc-recall (intern line)))
59 ;; stack operation
60 ((string= "'" (substring line 0 1))
61 (funcall (lookup-key calc-mode-map (substring line 1)) nil))
62 ;; complex expression
63 (t
64 (calc-push-list
65 (list ((lambda (res)
66 (cond
67 ((numberp res) res)
68 ((math-read-number res) (math-read-number res))
69 ((listp res) (error "calc error \"%s\" on input \"%s\""
70 (cadr res) line))
71 (t (calc-eval
72 (math-evaluate-expr
73 ;; resolve user variables, calc built in
74 ;; variables are handled automatically
75 ;; upstream by calc
76 (mapcar (lambda (el)
77 (if (and (consp el) (equal 'var (car el))
78 (member (cadr el) var-syms))
79 (progn
80 (calc-recall (cadr el))
81 (prog1 (calc-top 1)
82 (calc-pop 1)))
83 el))
84 ;; parse line into calc objects
85 (car (math-read-exprs line))))))))
86 (calc-eval line))))))))
87 (mapcar #'org-babel-trim
88 (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
afe98dfa 89 (save-excursion
acedf35c
CD
90 (with-current-buffer (get-buffer "*Calculator*")
91 (calc-eval (calc-top 1)))))
afe98dfa
CD
92
93(provide 'ob-calc)
94
afe98dfa
CD
95
96;;; ob-calc.el ends here