From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp2.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms13.migadu.com with LMTPS id yFiCDCGdXGdACQEAe85BDQ:P1 (envelope-from ) for ; Fri, 13 Dec 2024 20:46:25 +0000 Received: from aspmx1.migadu.com ([2001:41d0:303:e16b::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp2.migadu.com with LMTPS id yFiCDCGdXGdACQEAe85BDQ (envelope-from ) for ; Fri, 13 Dec 2024 21:46:25 +0100 X-Envelope-To: larch@yhetil.org Authentication-Results: aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iASqtvXR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Seal: i=1; s=key1; d=yhetil.org; t=1734122785; a=rsa-sha256; cv=none; b=c0jGIZwdX27/TQQ5tNuA9eurUdbiMM546LSnpMf9SMYJWSIqfFAx45/hxFjwotmXQDvjj3 iU1xDfLHJUjIz8jIz3T1FFq7IDU5cKpO6vgN0fSthfwWQHW9r2IwAeUryRwOIZb7UUOnd8 3aBsjFRA9XQYEMst3ti9pjKCpmvrLfuo57/NrHvonK5jaVvePQ815ZKnpqc6QR7Txtc9Pz b2Xxv1sqhLkUAo+mE4u7z/O+rWar8l2kKJxvp03FbaX++AlTUXOTBCrEW3pia1QWwXAtev J6+5UneKJ4KE6DOxmt6cQ6fe4Wn9eptecu4jiGGvTuZbYAlz2E9MpaFoodwZhw== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=iASqtvXR; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (aspmx1.migadu.com: domain of "emacs-orgmode-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="emacs-orgmode-bounces+larch=yhetil.org@gnu.org" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1734122785; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=eIIr+/yjpSuzGBIndC6uvKDL9l7V70D0r38COH+Narg=; b=D0WsiWN8RlPz7j7aL/NmhdJ4lojvutOE8PUT/S4G0QwUtxchaGBpoD0GkWvok1a+UMUkWV 1JwOzz5TTgLBMMT1dGhZNPiJHqy8+MeLFewzksvzYfMWv4CTtD7bkjloz+Q9SzDE0Gi0ov k3wKVY8ItlAqgS2ueVQ6/fZIbI5I5yfAzqEUP05yq/WWo8hF+AL85sF3iErVxgoDH8wZL9 SEuXNSwglcBrDEiEbSZzUkzOFozmIzg+HvO/7QPU0l0o8YM47CAQlxKcK4HqIuV0LWK/fk DhLalonZxSf6WlYsTbhC0BvN75ThW5XbT0zNtV1fOdy16KukWHqtM+anoAN6LQ== Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id 0040C78EF5 for ; Fri, 13 Dec 2024 21:46:25 +0100 (CET) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tMAsW-0004Q3-Ra; Fri, 13 Dec 2024 13:59:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tMAsU-0004Lg-Vb for emacs-orgmode@gnu.org; Fri, 13 Dec 2024 13:59:06 -0500 Received: from mail-qv1-xf42.google.com ([2607:f8b0:4864:20::f42]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1tMAsS-0003r1-UC for emacs-orgmode@gnu.org; Fri, 13 Dec 2024 13:59:06 -0500 Received: by mail-qv1-xf42.google.com with SMTP id 6a1803df08f44-6dccccd429eso438366d6.3 for ; Fri, 13 Dec 2024 10:59:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1734116343; x=1734721143; darn=gnu.org; h=mime-version:user-agent:message-id:date:subject:to:from:from:to:cc :subject:date:message-id:reply-to; bh=eIIr+/yjpSuzGBIndC6uvKDL9l7V70D0r38COH+Narg=; b=iASqtvXRA2UI+p7/fB8GY6BSHjbNcwBb3PZ3jaKU99Rx5yHYI+gj+3W1XM9gWjgKgl p9dSNDa7lOxui4WK1D9xRsnj0vKWzi18Km9Mcb7OEWwsdc++DdBszvLd6cjPRx7df03K DTHhHMyjnrWvi3XPakKIX++ZphO4HcXqpfUCnrtYc5HXAip8hItGUdlmUimlBQpC18nk Gbzp3O7tQb7CxB5jYgWwvCnGNFYw7OadL+pOqEJsoXhlPuYBsmyiMklZSAS8IzcOoLTJ JS2IYg3cfSw0AbulrLO7NbsvSeH1gCFoLAeIYbSZ8WBDhwv/mIXq7S9ABF5h0B9dK2LW aLCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734116343; x=1734721143; h=mime-version:user-agent:message-id:date:subject:to:from :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eIIr+/yjpSuzGBIndC6uvKDL9l7V70D0r38COH+Narg=; b=BZRsX8r5kOMTyllDXdtF5rdSSqUS/asSeX0WMzf+ldeRs+Ej6k9lsVAYVv0/aqMBvq CAXFiYp12AvdCHo+qOpCpEEvVuOuRJOMP+2iu2X3Kir7Iobvy/D7Rbdu9C4TxmXojJEA tmfILNTV7nbXeV+EUZfkx1k9+D6qgG9D8p2F1g/kc8qkUgCHj0iP0sQs3/H3NPWL3Kr+ eMI+2ejeNUO8XXILAF/ZNfYwmRLnxwYXDZQnCXnPt3MR4IgFtoG13X/fj/kVU4DJCHIT szAiieYN7e7w6Oql2lkpkpejhqnlKdNfASYwi4Nvrqv5KRMIgv/NZ2UC4ShLP8J3PZfF QCfQ== X-Gm-Message-State: AOJu0YzF/hE8wZs8WAItm/pCs34gs9H/PmlITw95VYnJITo9iNSwPPsV Dw7/D8qy9S7B7x6zppeJdcvF5Q3rdz/mGWwI4Nrt5pJ0Vk7DPrIkBTtVvu9a X-Gm-Gg: ASbGncurKbZwzO5AYP2jSTN5WV2sSHu6A9IGXbV/KdhUpPVeEWJsmgegvV4yokl4BoM ULgWcbv1cJOCP7EdOEvwclEZrZ/Kqdf+kfsFKcTSxY41Z0uHXx9JGfGRLvt2/N+4mEdrMg3XNzQ wj4HSVHjmqX89EOfbdinEQ6qVyizVj1Rzu8GH+Wsj3tICD3TBZI0TL7oxrkyi3+bC2YKoRZL5t3 NSU21/Pa5+7TMIFJZRSyPGDSZyWrFLxyo/sFbOfcTRAmwfD6A9GwA== X-Google-Smtp-Source: AGHT+IFzOmUQDn60+dQeiH6uJSedqkMe66C4liUKQTH/UbAZ0gijLzSVF+i8zSHzTC3Bl64CeWjfwA== X-Received: by 2002:a05:6a21:3287:b0:1e1:94ee:c37a with SMTP id adf61e73a8af0-1e1dfd3527dmr6877143637.15.1734116035132; Fri, 13 Dec 2024 10:53:55 -0800 (PST) Received: from localhost ([115.240.90.130]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-801d5c0efefsm52985a12.52.2024.12.13.10.53.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 13 Dec 2024 10:53:54 -0800 (PST) From: Visuwesh To: emacs-orgmode@gnu.org Subject: [PATCH] ob-calc: Format vector and matrix results as Org table Date: Sat, 14 Dec 2024 00:23:50 +0530 Message-ID: <878qsj1kjl.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2607:f8b0:4864:20::f42; envelope-from=visuweshm@gmail.com; helo=mail-qv1-xf42.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-orgmode@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-orgmode-bounces+larch=yhetil.org@gnu.org Sender: emacs-orgmode-bounces+larch=yhetil.org@gnu.org X-Migadu-Flow: FLOW_IN X-Migadu-Country: US X-Migadu-Queue-Id: 0040C78EF5 X-Migadu-Scanner: mx13.migadu.com X-Migadu-Spam-Score: -9.41 X-Spam-Score: -9.41 X-TUID: 8MUhLz007gJM --=-=-= Content-Type: text/plain 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. --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-ob-calc-Format-vector-and-matrix-results-as-Org-tabl.patch >From f58dddf37728248e05cbdf2dcb56517f6723261e Mon Sep 17 00:00:00 2001 From: Visuwesh 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 | -#+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]]" @@ -67,7 +67,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]" @@ -93,7 +93,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]]" @@ -105,11 +105,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 --=-=-=--