From mboxrd@z Thu Jan 1 00:00:00 1970 From: "Eric Schulte" Subject: Re: Possible Calc support for Org-Babel? Date: Fri, 29 Oct 2010 00:42:53 -0600 Message-ID: <87zktxv6s2.fsf@gmail.com> References: <7CDB7386-9B39-46CB-94E8-C6B5CBD59C4F@me.com> <87mxq0ajgw.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain Return-path: Received: from [140.186.70.92] (port=38732 helo=eggs.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PBig0-0004aq-Nq for emacs-orgmode@gnu.org; Fri, 29 Oct 2010 02:43:04 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1PBifz-00013M-Jn for emacs-orgmode@gnu.org; Fri, 29 Oct 2010 02:43:00 -0400 Received: from mail-gw0-f41.google.com ([74.125.83.41]:63823) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1PBifz-00013I-GH for emacs-orgmode@gnu.org; Fri, 29 Oct 2010 02:42:59 -0400 Received: by gwb11 with SMTP id 11so2172167gwb.0 for ; Thu, 28 Oct 2010 23:42:58 -0700 (PDT) In-Reply-To: <87mxq0ajgw.fsf@gmail.com> (Eric Schulte's message of "Tue, 26 Oct 2010 12:37:35 -0600") List-Id: "General discussions about Org-mode." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org Errors-To: emacs-orgmode-bounces+geo-emacs-orgmode=m.gmane.org@gnu.org To: Matthew Oesting Cc: emacs-orgmode@gnu.org "Eric Schulte" writes: > > Although I'm not familiar with using Calc as anything more than a 1-off > calculator in the bottom of the frame (i.e. M-x calc) this sounds like a > good approach to using calc to execute code blocks. > Did I mention I'm not familiar with Calc. I've thrown together a very naive first pass at a function for evaluating calc code blocks. This inverts the normal calc (as I understand it) use of ' prefixes and assumes that every line is an algebraic expression unless that line is prefixed with a ' in which case it is taken as a stack operation. This *does* change the value of the stack, allowing multiple code blocks to collaborate, in effect treating the stack as a session. I'd be interested to hear what real calc users think of this approach. Best -- Eric to use this evaluate the following function, and then try the subsequent code blocks evaluate this code block to add support for calc code blocks #+begin_src emacs-lisp (defun org-babel-execute:calc (body params) "Execute a block of calc code with Babel." (mapcar (lambda (line) (when (> (length line) 0) (if (string= "'" (substring line 0 1)) (funcall (lookup-key calc-mode-map (substring line 1)) nil) (calc-push-list (list (math-read-number (calc-eval line))))))) (split-string body "[\n\r]")) (calc-eval (calc-top 1))) #+end_src This block pushes 1 and 2 on the stack, then adds them #+begin_src calc 1 2 '+ #+end_src This block evaluates 3^3 with calc pushing the result on the stack and returning it into the Org-mode buffer #+begin_src calc 3^3 #+end_src This block evaluates (2+2)*4 pushing the result on the stack, it then calls calc-plus adding the top two elements on the stack (one of which is left over from the previous code block). #+begin_src calc (2+2)*4 '+ #+end_src