[செவ்வாய் டிசம்பர் 24, 2024] Ihor Radchenko wrote:

> Visuwesh <visuwe...@gmail.com> writes:
>
>> Attached patch formats vector and matrix results from a Calc source
>> block as Org table.  To test, try
>> ...
>> now.  The patch passes the old tests, and I added three new tests to
>> check the correctness of this conversion.
>
> Thanks!
>
> etc/ORG-NEWS entry seems to be displaced. It should be under Org 9.8
> heading.
>
> Also, the patch does not apply onto the latest main.
>
>> -<point>#+BEGIN_SRC calc :results silent :var a=ob-calc-table-1
>> +<point>#+BEGIN_SRC calc :results silent verbatim :var a=ob-calc-table-1
>
> You had to change the tests, so we have a breaking change.
>
> The change can be viewed as a bug fix since ob-calc previously did not
> conform to babel conventions, so I do not object changing the defaults.
> However, we need to inform the affected users and put the announcement
> into "breaking changes" section. We should also recommend changing
> org-babel-default-header-args:calc to restore the old behavior.

Thanks, these should be fixed in the attached patch.

>From 8ab27ec0c0732648d9c684decf8eda0e5e0ef1b5 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuwe...@gmail.com>
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....@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 |
 
-<point>#+BEGIN_SRC calc :results silent :var a=ob-calc-table-1
+<point>#+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 |
 
-<point>#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2
+<point>#+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 |
 
-<point>#+BEGIN_SRC calc :results silent :var a=ob-calc-table-3
+<point>#+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 |
 
-<point>#+BEGIN_SRC calc :results silent :var a=ob-calc-table-2
+<point>#+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 "\
+<point>#+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 |
+
+<point>#+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 |
+
+<point>#+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

Reply via email to