Merge branch 'maint'
[org-mode/org-tableheadings.git] / lisp / ob-calc.el
blob918f41d4e45ee6d9c2f5891d6d54c6e656693ee4
1 ;;; ob-calc.el --- Babel Functions for Calc -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2010-2016 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-trail)
32 (require 'calc-store)
34 (declare-function calc-store-into "calc-store" (&optional var))
35 (declare-function calc-recall "calc-store" (&optional var))
36 (declare-function math-evaluate-expr "calc-ext" (x))
38 (defvar org-babel-default-header-args:calc nil
39 "Default arguments for evaluating an calc source block.")
41 (defun org-babel-expand-body:calc (body _params)
42 "Expand BODY according to PARAMS, return the expanded body." body)
44 (defvar org--var-syms) ; Dynamically scoped from org-babel-execute:calc
46 (defun org-babel-execute:calc (body params)
47 "Execute a block of calc code with Babel."
48 (unless (get-buffer "*Calculator*")
49 (save-window-excursion (calc) (calc-quit)))
50 (let* ((vars (org-babel--get-vars params))
51 (org--var-syms (mapcar #'car vars))
52 (var-names (mapcar #'symbol-name org--var-syms)))
53 (mapc
54 (lambda (pair)
55 (calc-push-list (list (cdr pair)))
56 (calc-store-into (car pair)))
57 vars)
58 (mapc
59 (lambda (line)
60 (when (> (length line) 0)
61 (cond
62 ;; simple variable name
63 ((member line var-names) (calc-recall (intern line)))
64 ;; stack operation
65 ((string= "'" (substring line 0 1))
66 (funcall (lookup-key calc-mode-map (substring line 1)) nil))
67 ;; complex expression
69 (calc-push-list
70 (list (let ((res (calc-eval line)))
71 (cond
72 ((numberp res) res)
73 ((math-read-number res) (math-read-number res))
74 ((listp res) (error "Calc error \"%s\" on input \"%s\""
75 (cadr res) line))
76 (t (replace-regexp-in-string
77 "'" ""
78 (calc-eval
79 (math-evaluate-expr
80 ;; resolve user variables, calc built in
81 ;; variables are handled automatically
82 ;; upstream by calc
83 (mapcar #'org-babel-calc-maybe-resolve-var
84 ;; parse line into calc objects
85 (car (math-read-exprs line)))))))))
86 ))))))
87 (mapcar #'org-babel-trim
88 (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
89 (save-excursion
90 (with-current-buffer (get-buffer "*Calculator*")
91 (prog1
92 (calc-eval (calc-top 1))
93 (calc-pop 1)))))
95 (defun org-babel-calc-maybe-resolve-var (el)
96 (if (consp el)
97 (if (and (equal 'var (car el)) (member (cadr el) org--var-syms))
98 (progn
99 (calc-recall (cadr el))
100 (prog1 (calc-top 1)
101 (calc-pop 1)))
102 (mapcar #'org-babel-calc-maybe-resolve-var el))
103 el))
105 (provide 'ob-calc)
109 ;;; ob-calc.el ends here