ob-calc.el 3.7 KB

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