ob-calc: added to org-src-lang-modes, safer evaluation, better error messages
[org-mode/org-jambu.git] / lisp / ob-calc.el
blob5571286be1822271260eb14d023064776ab52a29
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.01trans
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-trail)
33 (eval-when-compile (require 'ob-comint))
35 (defvar org-babel-default-header-args:calc nil
36 "Default arguments for evaluating an calc source block.")
38 (defun org-babel-expand-body:calc (body params)
39 "Expand BODY according to PARAMS, return the expanded body." body)
41 (defun org-babel-execute:calc (body params)
42 "Execute a block of calc code with Babel."
43 (mapcar
44 (lambda (line)
45 (when (> (length line) 0)
46 (if (string= "'" (substring line 0 1))
47 (funcall (lookup-key calc-mode-map (substring line 1)) nil)
48 (calc-push-list
49 (list ((lambda (res)
50 (cond
51 ((numberp res) res)
52 ((listp res) (error "calc error \"%s\" on input \"%s\""
53 (cadr res) line))
54 (t res))
55 (if (numberp res) res (math-read-number res)))
56 (calc-eval line)))))))
57 (mapcar #'org-babel-trim
58 (split-string (org-babel-expand-body:calc body params) "[\n\r]")))
59 (save-excursion
60 (set-buffer (get-buffer "*Calculator*"))
61 (calc-eval (calc-top 1))))
63 (provide 'ob-calc)
65 ;; arch-tag: 5c57a3b7-5818-4c6c-acda-7a94831a6449
67 ;;; ob-calc.el ends here