all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [PATCH] ob-calc: Format vector and matrix results as Org table
@ 2024-12-13 18:53 Visuwesh
  2024-12-24 10:58 ` Ihor Radchenko
  0 siblings, 1 reply; 5+ messages in thread
From: Visuwesh @ 2024-12-13 18:53 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 355 bytes --]

Attached patch formats vector and matrix results from a Calc source
block as Org table.  To test, try

    #+NAME: test
    | 1 | 2 |

    #+BEGIN_SRC calc :var t=test
      2 * t
    #+END_SRC

It should return

    #+RESULTS:
    | 2 | 4 |

now.  The patch passes the old tests, and I added three new tests to
check the correctness of this conversion.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-calc-Format-vector-and-matrix-results-as-Org-tabl.patch --]
[-- Type: text/x-diff, Size: 5808 bytes --]

From f58dddf37728248e05cbdf2dcb56517f6723261e Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@gmail.com>
Date: Sat, 14 Dec 2024 00:23:14 +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.
---
 etc/ORG-NEWS                 |  6 ++++++
 lisp/ob-calc.el              | 29 ++++++++++++++++++++-----
 testing/lisp/test-ob-calc.el | 42 ++++++++++++++++++++++++++++++++----
 3 files changed, 68 insertions(+), 9 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index ca73f06e7..fa98c41e6 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -946,6 +946,12 @@ properties, links to headlines in the file can also be made more
 robust by using the file id instead of the file path.
 
 ** New features
+*** =ob-calc.el=: Vector and matrix results can be formatted as Org tables
+
+~ob-calc~ can now format vector and matrix results as Org tables.
+This conversion can be overriden using the usual ~:results verbatim~
+keyword.
+
 *** =ob-tangle.el=: New flag to remove tangle targets before writing
 
 When ~org-babel-tangle-remove-file-before-write~ is set to ~t~ the
diff --git a/lisp/ob-calc.el b/lisp/ob-calc.el
index 810ed1735..e3fdc0cbd 100644
--- a/lisp/ob-calc.el
+++ b/lisp/ob-calc.el
@@ -110,11 +110,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 (get-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 6d6ca104d..cfa1fb4f6 100644
--- a/testing/lisp/test-ob-calc.el
+++ b/testing/lisp/test-ob-calc.el
@@ -55,7 +55,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]]"
@@ -67,7 +67,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]"
@@ -93,7 +93,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]]"
@@ -105,11 +105,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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] ob-calc: Format vector and matrix results as Org table
  2024-12-13 18:53 [PATCH] ob-calc: Format vector and matrix results as Org table Visuwesh
@ 2024-12-24 10:58 ` Ihor Radchenko
  2024-12-24 13:02   ` Visuwesh
  2024-12-26 13:06   ` Visuwesh
  0 siblings, 2 replies; 5+ messages in thread
From: Ihor Radchenko @ 2024-12-24 10:58 UTC (permalink / raw)
  To: Visuwesh; +Cc: emacs-orgmode

Visuwesh <visuweshm@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.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ob-calc: Format vector and matrix results as Org table
  2024-12-24 10:58 ` Ihor Radchenko
@ 2024-12-24 13:02   ` Visuwesh
  2024-12-24 13:40     ` Ihor Radchenko
  2024-12-26 13:06   ` Visuwesh
  1 sibling, 1 reply; 5+ messages in thread
From: Visuwesh @ 2024-12-24 13:02 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

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

> Visuwesh <visuweshm@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.

I guess I mixed up main and master on my local clone (I probably have a
dummy master branch).

>> -<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.

Can you tell what to put in here to retain the old behaviour?  I am not
a regular user of babel and know next to nothing about these header
arguments to suggest the right setting of
org-babel-default-header-args:calc to gain back the old behaviour.

Thanks for the review.


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ob-calc: Format vector and matrix results as Org table
  2024-12-24 13:02   ` Visuwesh
@ 2024-12-24 13:40     ` Ihor Radchenko
  0 siblings, 0 replies; 5+ messages in thread
From: Ihor Radchenko @ 2024-12-24 13:40 UTC (permalink / raw)
  To: Visuwesh; +Cc: emacs-orgmode

Visuwesh <visuweshm@gmail.com> writes:

>> 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.
>
> Can you tell what to put in here to retain the old behaviour?  I am not
> a regular user of babel and know next to nothing about these header
> arguments to suggest the right setting of
> org-babel-default-header-args:calc to gain back the old behaviour.

:results verbatim I think.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] ob-calc: Format vector and matrix results as Org table
  2024-12-24 10:58 ` Ihor Radchenko
  2024-12-24 13:02   ` Visuwesh
@ 2024-12-26 13:06   ` Visuwesh
  1 sibling, 0 replies; 5+ messages in thread
From: Visuwesh @ 2024-12-26 13:06 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1162 bytes --]

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

> Visuwesh <visuweshm@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.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-ob-calc-Format-vector-and-matrix-results-as-Org-tabl.patch --]
[-- Type: text/x-diff, Size: 6114 bytes --]

From 8ab27ec0c0732648d9c684decf8eda0e5e0ef1b5 Mon Sep 17 00:00:00 2001
From: Visuwesh <visuweshm@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.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 |
 
-<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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-12-26 13:07 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-13 18:53 [PATCH] ob-calc: Format vector and matrix results as Org table Visuwesh
2024-12-24 10:58 ` Ihor Radchenko
2024-12-24 13:02   ` Visuwesh
2024-12-24 13:40     ` Ihor Radchenko
2024-12-26 13:06   ` Visuwesh

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.