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
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 via email to