ob-calc.el 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. ;;; ob-calc.el --- Babel Functions for Calc -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2010-2022 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte
  4. ;; Maintainer: Tom Gillespie <tgbugs@gmail.com>
  5. ;; Keywords: literate programming, reproducible research
  6. ;; URL: https://orgmode.org
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Org-Babel support for evaluating calc code
  20. ;;; Code:
  21. (require 'org-macs)
  22. (org-assert-version)
  23. (require 'ob)
  24. (require 'org-macs)
  25. (require 'calc)
  26. (require 'calc-trail)
  27. (require 'calc-store)
  28. (declare-function calc-store-into "calc-store" (&optional var))
  29. (declare-function calc-recall "calc-store" (&optional var))
  30. (declare-function math-evaluate-expr "calc-ext" (x))
  31. (defvar org-babel-default-header-args:calc nil
  32. "Default arguments for evaluating a calc source block.")
  33. (defun org-babel-expand-body:calc (body _params)
  34. "Expand BODY according to PARAMS, return the expanded body." body)
  35. (defvar org--var-syms) ; Dynamically scoped from org-babel-execute:calc
  36. (defun org-babel-execute:calc (body params)
  37. "Execute a block of calc code with Babel."
  38. (unless (get-buffer "*Calculator*")
  39. (save-window-excursion (calc) (calc-quit)))
  40. (let* ((vars (org-babel--get-vars params))
  41. (org--var-syms (mapcar #'car vars))
  42. (var-names (mapcar #'symbol-name org--var-syms)))
  43. (mapc
  44. (lambda (pair)
  45. (calc-push-list (list (cdr pair)))
  46. (calc-store-into (car pair)))
  47. vars)
  48. (mapc
  49. (lambda (line)
  50. (when (> (length line) 0)
  51. (cond
  52. ;; simple variable name
  53. ((member line var-names) (calc-recall (intern line)))
  54. ;; stack operation
  55. ((string= "'" (substring line 0 1))
  56. (funcall (lookup-key calc-mode-map (substring line 1)) nil))
  57. ;; complex expression
  58. (t
  59. (calc-push-list
  60. (list (let ((res (calc-eval line)))
  61. (cond
  62. ((numberp res) res)
  63. ((math-read-number res) (math-read-number res))
  64. ((listp res) (error "Calc error \"%s\" on input \"%s\""
  65. (cadr res) line))
  66. (t (replace-regexp-in-string
  67. "'" ""
  68. (calc-eval
  69. (math-evaluate-expr
  70. ;; resolve user variables, calc built in
  71. ;; variables are handled automatically
  72. ;; upstream by calc
  73. (mapcar #'org-babel-calc-maybe-resolve-var
  74. ;; parse line into calc objects
  75. (car (math-read-exprs line)))))))))
  76. ))))))
  77. (mapcar #'org-trim
  78. (split-string (org-babel-expand-body:calc body params) "[\n\r]"))))
  79. (save-excursion
  80. (with-current-buffer (get-buffer "*Calculator*")
  81. (prog1
  82. (calc-eval (calc-top 1))
  83. (calc-pop 1)))))
  84. (defun org-babel-calc-maybe-resolve-var (el)
  85. (if (consp el)
  86. (if (and (eq 'var (car el)) (member (cadr el) org--var-syms))
  87. (progn
  88. (calc-recall (cadr el))
  89. (prog1 (calc-top 1)
  90. (calc-pop 1)))
  91. (mapcar #'org-babel-calc-maybe-resolve-var el))
  92. el))
  93. (provide 'ob-calc)
  94. ;;; ob-calc.el ends here