From 8ab27ec0c0732648d9c684decf8eda0e5e0ef1b5 Mon Sep 17 00:00:00 2001 From: Visuwesh Date: Thu, 26 Dec 2024 18:36:24 +0530 Subject: [PATCH] ob-calc: Format vector and matrix results as Org tables * lisp/ob-calc.el: (org-babel-execute:calc): Format vector and matrix results as Org tables using the raw Calc object. * testing/lisp/test-ob-calc.el (ob-calc/matrix-inversion) (ob-calc/matrix-algebra, ob-calc/matrix-correct-conv-column) (ob-calc/matrix-correct-conv-row): Use ':results verbatim' for the old tests. (ob-calc/conv-scalar, ob-calc/conv-vector, ob-calc/conv-matrix): Add new tests to verify correct conversion of vector, matrix, and scalar results with ':results replace'. * etc/ORG-NEWS: Announce the new feature. Link: https://list.orgmode.org/878qsj1kjl.fsf@gmail.com --- etc/ORG-NEWS | 16 ++++++++++++++ lisp/ob-calc.el | 29 ++++++++++++++++++++----- testing/lisp/test-ob-calc.el | 42 ++++++++++++++++++++++++++++++++---- 3 files changed, 78 insertions(+), 9 deletions(-) diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS index f932c4ea2..f0112f33c 100644 --- a/etc/ORG-NEWS +++ b/etc/ORG-NEWS @@ -83,6 +83,22 @@ This should have minimal impact on non-iCalendar exporters, since users who manually set ~org-export-with-timestamps~ to ~active~ will now have diary timestamps included as well. +*** =ob-calc.el=: Vector and matrix are now inserted as Org tables by default + +~ob-calc~ now formats vector and matrix results as Org tables. This +conservation can be overriden using the ~:results verbatim~ keyword on +a per source block basis. + +To get back the old behaviour, add + +#+BEGIN_SRC emacs-lisp +(with-eval-after-load 'ob-calc + (setq org-babel-header-args:calc + (append '(:results . "verbatim") org-babel-header-args:calc))) +#+END_SRC + +to your configuration. + ** New features # We list the most important features, and the features that may diff --git a/lisp/ob-calc.el b/lisp/ob-calc.el index 171fd1b04..222c1f9be 100644 --- a/lisp/ob-calc.el +++ b/lisp/ob-calc.el @@ -114,11 +114,30 @@ (defun org-babel-execute:calc (body params) )))))) (mapcar #'org-trim (split-string (org-babel-expand-body:calc body params) "[\n\r]")))) - (save-excursion - (with-current-buffer "*Calculator*" - (prog1 - (calc-eval (calc-top 1)) - (calc-pop 1))))) + (let ((result (prog1 + ;; Cannot use 'top' as SEPARATOR reliably when the + ;; top of the stack has a vector. + (calc-eval (calc-top 1) 'raw) + (calc-eval 1 'pop))) + (calc-line-numbering) + lisp-table) + (org-babel-reassemble-table + (org-babel-result-cond (cdr (assq :result-params params)) + (calc-eval result) + (if (Math-vectorp result) + (progn + (dolist (r (if (math-matrixp result) + (cdr result) ; Ignore the 'vec item. + (list result))) + (setq r (cdr r)) ; Ignore the 'vec item. + (push (mapcar (lambda (x) (math-format-stack-value (list x 1 nil))) r) + lisp-table)) + (setq lisp-table (nreverse lisp-table))) + (calc-eval result))) + (org-babel-pick-name + (cdr (assq :colname-names params)) (cdr (assq :colnames params))) + (org-babel-pick-name + (cdr (assq :rowname-names params)) (cdr (assq :rownames params)))))) (defun org-babel-calc-maybe-resolve-var (el) "Resolve user variables in EL. diff --git a/testing/lisp/test-ob-calc.el b/testing/lisp/test-ob-calc.el index f6e8a5a2f..de6656a4e 100644 --- a/testing/lisp/test-ob-calc.el +++ b/testing/lisp/test-ob-calc.el @@ -63,7 +63,7 @@ (ert-deftest ob-calc/matrix-inversion () | 5 | 6 | 7 | | 9 | 14 | 11 | -#+BEGIN_SRC calc :results silent :var a=ob-calc-table-1 +#+BEGIN_SRC calc :results silent verbatim :var a=ob-calc-table-1 inv(a) #+END_SRC " (should (equal "[[-1, 0.625, -0.125], [0.25, -0.5, 0.25], [0.5, 0.125, -0.125]]" @@ -82,7 +82,7 @@ (ert-deftest ob-calc/matrix-algebra () #+NAME: ob-calc-table-2 | 1 | 2 | 3 | 4 | 5 | -#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2 +#+BEGIN_SRC calc :results silent verbatim :var a=ob-calc-table-2 a*2 - 2 #+END_SRC" (should (equal "[0, 2, 4, 6, 8]" @@ -108,7 +108,7 @@ (ert-deftest ob-calc/matrix-correct-conv-column () | 2 | | 3 | -#+BEGIN_SRC calc :results silent :var a=ob-calc-table-3 +#+BEGIN_SRC calc :results silent verbatim :var a=ob-calc-table-3 a #+END_SRC" (should (equal "[[1], [2], [3]]" @@ -120,11 +120,45 @@ (ert-deftest ob-calc/matrix-correct-conv-row () #+NAME: ob-calc-table-2 | 1 | 2 | 3 | 4 | 5 | -#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2 +#+BEGIN_SRC calc :results silent verbatim :var a=ob-calc-table-2 a #+END_SRC" (should (equal "[1, 2, 3, 4, 5]" (org-babel-execute-src-block))))) +(ert-deftest ob-calc/conv-scalar () + "Test of conversion of scalar for :result replace." + (org-test-with-temp-text "\ +#+BEGIN_SRC calc :results silent + 4 * 4 +#+END_SRC" + (should (equal "16" + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/conv-vector () + "Test of conversion of vector for :result replace." + (org-test-with-temp-text "\ +#+NAME: ob-calc-table +| 1 | 2 | 3 | 4.1 | 5 | 6 | + +#+BEGIN_SRC calc :results silent :var a=ob-calc-table + a * 2 +#+END_SRC" + (should (equal '(("2" "4" "6" "8.2" "10" "12")) + (org-babel-execute-src-block))))) + +(ert-deftest ob-calc/conv-matrix () + "Test of conversion of matrix for :result replace." + (org-test-with-temp-text "\ +#+NAME: ob-calc-table +| 1 | 2 | +| 2.2 | 4.2 | + +#+BEGIN_SRC calc :results silent table :var a=ob-calc-table + a * 2 +#+END_SRC" + (should (equal '(("2" "4") ("4.4" "8.4")) + (org-babel-execute-src-block))))) + (provide 'test-ob-calc) ;;; test-ob-calc.el ends here -- 2.45.2