Org-mode mailing list
 help / color / mirror / Atom feed
From: Tom Gillespie <tgbugs@gmail.com>
To: pietru@caramail.com
Cc: Org Mode List <emacs-orgmode@gnu.org>
Subject: Re: Multiple calc commands with orgbabel
Date: Wed, 5 May 2021 10:03:56 -0700
Message-ID: <CA+G3_PMDX8nO6xGCMaySg2gD=kJrkzE3e-uoOGPCe5xj96QTWg@mail.gmail.com> (raw)
In-Reply-To: <87eeelrv6s.fsf@ucl.ac.uk>

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

Here is a quick and dirty implementation that more or less does what
you want (I think). The if t would probably need to be replaced by a
value that corresponded to an option that indicated that ob-calc
should resolve all expressions on the stack. This isn't really an
issue of return value, it is due to the fact that ob-calc makes
stateful modifications to calc. If you want a stateless (idempotent?)
ob-calc block you would need to do something like this as well, and
then you would need an option to discard the additional values instead
of retruning them as I do here. Best!
Tom

[-- Attachment #2: ob-calc-multi-return.patch --]
[-- Type: text/x-patch, Size: 1606 bytes --]

diff --git a/lisp/ob-calc.el b/lisp/ob-calc.el
index 39ebce100..e2102feca 100644
--- a/lisp/ob-calc.el
+++ b/lisp/ob-calc.el
@@ -48,6 +48,7 @@
   "Execute a block of calc code with Babel."
   (unless (get-buffer "*Calculator*")
     (save-window-excursion (calc) (calc-quit)))
+  (let ((unpopped 0))
   (let* ((vars (org-babel--get-vars params))
 	 (org--var-syms (mapcar #'car vars))
 	 (var-names (mapcar #'symbol-name org--var-syms)))
@@ -58,12 +59,14 @@
      vars)
     (mapc
      (lambda (line)
+       (setq unpopped (1+ unpopped)) ; ICK
        (when (> (length line) 0)
 	 (cond
 	  ;; simple variable name
 	  ((member line var-names) (calc-recall (intern line)))
 	  ;; stack operation
 	  ((string= "'" (substring line 0 1))
+       (setq unpopped (- unpopped 2))
 	   (funcall (lookup-key calc-mode-map (substring line 1)) nil))
 	  ;; complex expression
 	  (t
@@ -89,9 +92,17 @@
 	     (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)))))
+      (if t
+          (let ((out
+                 (cl-loop for i from 1 to unpopped
+                          do (message "%S %S" unpopped calc-stack)
+                          collect (calc-eval (calc-top 1))
+                          do (calc-pop 1))))
+            (message "%S" out)
+            (mapcar #'list (reverse out)))
+        (prog1
+            (calc-eval (calc-top 1))
+            (calc-pop 1)))))))
 
 (defun org-babel-calc-maybe-resolve-var (el)
   (if (consp el)

  reply	other threads:[~2021-05-05 17:15 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-05  5:46 pietru
2021-05-05  6:33 ` Eric S Fraga
2021-05-05  6:40   ` pietru
2021-05-05  6:41     ` Eric S Fraga
2021-05-05  6:45       ` pietru
2021-05-05 15:11   ` Matt Price
2021-05-05 15:29     ` pietru
2021-05-05 16:19       ` Tom Gillespie
2021-05-05 16:39     ` Eric S Fraga
2021-05-05 17:03       ` Tom Gillespie [this message]
2021-05-06 11:16         ` Bastien
2021-05-06 15:21           ` Tom Gillespie
2021-05-07  4:04             ` Bastien
2021-05-07  7:01               ` Tom Gillespie
2021-05-07  7:31                 ` Bastien
2021-05-05 20:24       ` pietru
2021-05-05 23:09       ` ob-fortran behaviour Christopher Dimech
2021-05-05 23:29         ` Christopher Dimech
2021-05-06  2:24       ` ob-fortran fixes to column position 6 and subroutine keywords pietru
2021-05-05  7:12 ` Multiple calc commands with orgbabel Dr. Arne Babenhauserheide
2021-05-05  7:20   ` pietru
2021-05-05  7:23   ` Eric S Fraga
2021-05-05  7:26     ` pietru
2021-05-05  7:59       ` Eric S Fraga
2021-05-05  8:00         ` pietru

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

  List information: https://orgmode.org

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

  git send-email \
    --in-reply-to='CA+G3_PMDX8nO6xGCMaySg2gD=kJrkzE3e-uoOGPCe5xj96QTWg@mail.gmail.com' \
    --to=tgbugs@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=pietru@caramail.com \
    /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

Org-mode mailing list

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://orgmode.org/list/0 list/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 list list/ https://orgmode.org/list \
		emacs-orgmode@gnu.org
	public-inbox-index list

Example config snippet for mirrors.
Newsgroups are available over NNTP:
	nntp://news.yhetil.org/yhetil.emacs.orgmode
	nntp://news.gmane.io/gmane.emacs.orgmode


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git