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