ob-calc.el 3.8 KB

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