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
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/>.
27 ;; Org-Babel support for evaluating calc code
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
)))
51 (calc-push-list (list (cdr pair
)))
52 (calc-store-into (car pair
)))
56 (when (> (length line
) 0)
58 ;; simple variable name
59 ((member line var-names
) (calc-recall (intern line
)))
61 ((string= "'" (substring line
0 1))
62 (funcall (lookup-key calc-mode-map
(substring line
1)) nil
))
69 ((math-read-number res
) (math-read-number res
))
70 ((listp res
) (error "calc error \"%s\" on input \"%s\""
72 (t (replace-regexp-in-string
76 ;; resolve user variables, calc built in
77 ;; variables are handled automatically
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]"))))
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)
92 (if (and (equal 'var
(car el
)) (member (cadr el
) var-syms
))
94 (calc-recall (cadr el
))
97 (mapcar #'ob-calc-maybe-resolve-var el
))
102 ;; arch-tag: 5c57a3b7-5818-4c6c-acda-7a94831a6449
104 ;;; ob-calc.el ends here