all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Visuwesh <visuweshm@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH] ob-calc: Format vector and matrix results as Org table
Date: Thu, 26 Dec 2024 18:36:58 +0530	[thread overview]
Message-ID: <8734iaegp9.fsf@gmail.com> (raw)
In-Reply-To: <877c7pibzv.fsf@localhost> (Ihor Radchenko's message of "Tue, 24 Dec 2024 10:58:12 +0000")

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


  parent reply	other threads:[~2024-12-26 13:07 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-12-27 17:54     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8734iaegp9.fsf@gmail.com \
    --to=visuweshm@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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.