org.el: tiny docstring fix.
[org-mode.git] / lisp / ob-calc.el
blob45d94419f98eea8c401d73967948dad9cf7bc252
1 ;;; ob-calc.el --- org-babel functions for calc code evaluation
3 ;; Copyright (C) 2010-2011 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 (require 'calc-store)
32 (unless (featurep 'xemacs) (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 (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
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 (replace-regexp-in-string
72 "'\\[" "["
73 (calc-eval
74 (math-evaluate-expr
75 ;; resolve user variables, calc built in
76 ;; variables are handled automatically
77 ;; upstream by calc
78 (mapcar #'ob-calc-maybe-resolve-var
79 ;; parse line into calc objects
80 (car (math-read-exprs line)))))))))
81 (calc-eval line))))))))
82 (mapcar #'org-babel-trim
83 (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
84 (save-excursion
85 (with-current-buffer (get-buffer "*Calculator*")
86 (calc-eval (calc-top 1)))))
88 (defvar var-syms) ; Dynamically scoped from org-babel-execute:calc
89 (defun ob-calc-maybe-resolve-var (el)
90 (if (consp el)
91 (if (and (equal 'var (car el)) (member (cadr el) var-syms))
92 (progn
93 (calc-recall (cadr el))
94 (prog1 (calc-top 1)
95 (calc-pop 1)))
96 (mapcar #'ob-calc-maybe-resolve-var el))
97 el))
99 (provide 'ob-calc)
103 ;;; ob-calc.el ends here