now able to assign variables from emacs-lisp source blocks

This commit is contained in:
Eric Schulte 2009-04-02 17:58:04 -07:00
parent 91dea38923
commit 77c33de593
8 changed files with 96 additions and 24 deletions

View File

@ -31,7 +31,7 @@
;;; Code:
(require 'org)
(require 'litorgy)
(require 'litorgy-reference)
(require 'litorgy-ref)
;; language specific files
(require 'litorgy-script)

View File

@ -38,7 +38,7 @@
called by `litorgy-execute-src-block'."
(message "executing R source code block...")
(save-window-excursion
(let ((vars (litorgy-reference-variables params))
(let ((vars (litorgy-ref-variables params))
results)
(litorgy-R-initiate-R-buffer)
(litorgy-R-command-to-string body))))

View File

@ -38,12 +38,14 @@
function is called by `litorgy-execute-src-block'."
(message "executing emacs-lisp code block...")
(save-window-excursion
(let ((vars (litorgy-reference-variables params))
(let ((vars (litorgy-ref-variables params))
(print-level nil) (print-length nil) results)
(setq results
(eval `(let ,(mapcar (lambda (var) `(,(car var) ',(cdr var))) vars)
,(read body))))
(if (listp results) results (format "%S" results)))))
(if (assoc :raw params)
results
(if (listp results) results (format "%S" results))))))
(provide 'litorgy-lisp)
;;; litorgy-lisp.el ends here

View File

@ -1,4 +1,4 @@
;;; litorgy-reference.el --- litorgical functions for referencing external data
;;; litorgy-ref.el --- litorgical functions for referencing external data
;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
@ -73,13 +73,13 @@ This is taken almost directly from `org-read-prop'."
out))
cell))
(defun litorgy-reference-variables (params)
(defun litorgy-ref-variables (params)
"Takes a parameter alist, and return an alist of variable
names, and the string representation of the related value."
(mapcar #'litorgy-reference-parse
(mapcar #'litorgy-ref-parse
(delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
(defun litorgy-reference-parse (reference)
(defun litorgy-ref-parse (reference)
"Parse a reference to an external resource returning a list
with two elements. The first element of the list will be the
name of the variable, and the second will be an emacs-lisp
@ -88,7 +88,7 @@ representation of the value of the variable."
(if (string-match "\\(.+\\)=\\(.+\\)" reference)
(let ((var (match-string 1 reference))
(ref (match-string 2 reference))
direction)
direction type)
(when (string-match "\\(.+\\):\\(.+\\)" reference)
(find-file (match-string 1 reference))
(setf ref (match-string 2 reference)))
@ -100,23 +100,35 @@ representation of the value of the variable."
(t
(goto-char (point-min))
(setq direction 1)
(unless (let ((regexp (concat "^#\\+TBLNAME:[ \t]*"
(unless (let ((regexp (concat "^#\\+\\(TBL\\|SRC\\)NAME:[ \t]*"
(regexp-quote ref) "[ \t]*$")))
(or (re-search-forward regexp nil t)
(re-search-backward regexp nil t)))
;; ;; TODO: allow searching for table in other buffers
;; ;; TODO: allow searching for names in other buffers
;; (setq id-loc (org-id-find ref 'marker)
;; buffer (marker-buffer id-loc)
;; loc (marker-position id-loc))
;; (move-marker id-loc nil)
(error (format "table '%s' not found in this buffer" ref)))))
(while (not (org-at-table-p))
(error (format "reference '%s' not found in this buffer" ref)))))
(while (not (setq type (litorgy-ref-at-ref-p)))
(forward-line direction)
(beginning-of-line)
(if (or (= (point) (point-min)) (= (point) (point-max)))
(error "no table found")))
(mapcar (lambda (row)
(mapcar #'litorgy-read-cell row))
(org-table-to-lisp))))))))
(error "reference not found")))
(case type
('table
(mapcar (lambda (row)
(mapcar #'litorgy-read-cell row))
(org-table-to-lisp)))
('source-block
(litorgy-execute-src-block t)))))))))
(provide 'litorgy-reference)
;;; litorgy-reference.el ends here
(defun litorgy-ref-at-ref-p ()
"Return the type of reference located at point or nil of none
of the supported reference types are found. Supported reference
types are tables and source blocks."
(cond ((org-at-table-p) 'table)
((looking-at "^#\\+BEGIN_SRC") 'source-block)))
(provide 'litorgy-ref)
;;; litorgy-ref.el ends here

View File

@ -52,7 +52,7 @@ executed through litorgy."
(defun litorgy-script-execute (cmd body params)
"Run CMD on BODY obeying any options set with PARAMS."
(message (format "executing %s code block..." cmd))
(let ((vars (litorgy-reference-variables params)))
(let ((vars (litorgy-ref-variables params)))
(save-window-excursion
(with-temp-buffer
(when (string= "ruby" cmd) (insert "def main\n"))

View File

@ -52,7 +52,7 @@ executed through litorgy."
(defun litorgy-shell-execute (cmd body params)
"Run CMD on BODY obeying any options set with PARAMS."
(message (format "executing %s code block..." cmd))
(let ((vars (litorgy-reference-variables params)))
(let ((vars (litorgy-ref-variables params)))
(save-window-excursion
(with-temp-buffer
(if (> (length vars) 0)

View File

@ -94,7 +94,9 @@ lisp code use the `litorgy-add-interpreter' function."
"Execute the current source code block, and dump the results
into the buffer immediately following the block. Results are
commented by `org-toggle-fixed-width-section'. With optional
prefix don't dump results into buffer."
prefix don't dump results into buffer but rather return the
results in raw elisp (this is useful for automated execution of a
source block)."
(interactive "P")
(let* ((info (litorgy-get-src-block-info))
(lang (first info))
@ -104,9 +106,11 @@ prefix don't dump results into buffer."
result)
(unless (member lang litorgy-interpreters)
(error "Language is not in `litorgy-interpreters': %s" lang))
(when arg
(setq params (cons '(:raw . t) params)))
(setq result (funcall cmd body params))
(if arg
(message (format "%S" result))
result
(litorgy-insert-result result (cdr (assoc :results params))))))
(defun litorgy-eval-buffer (&optional arg)
@ -132,7 +136,7 @@ form. (language body header-arguments-alist)"
(unless (save-excursion
(beginning-of-line 1)
(looking-at litorgy-src-block-regexp))
(error "not looking at src-block"))
(error (format "not looking at src-block (%s)" (point))))
(let ((lang (litorgy-clean-text-properties (match-string 1)))
(args (litorgy-clean-text-properties (or (match-string 3) "")))
(body (litorgy-clean-text-properties (match-string 4))))

View File

@ -54,6 +54,13 @@ to specify whether the target is a source code block or a table
(alternately we could just match the first one with the given name
whether it's a table or a source code block).
At least initially I'll try to implement this so that there is no need
to specify whether the reference is to a table or a source-code block.
That seems to be simpler both in terms of use and implementation.
This is currently working only with emacs lisp as in the following
example in the [[* emacs lisp source reference][emacs lisp source reference]].
* Bugs
** Args out of range error
@ -258,6 +265,53 @@ x
#+end_src
** referencing other source blocks
Doing this in emacs-lisp first because it's trivial to convert
emacs-lisp results to and from emacs-lisp.
*** emacs lisp source reference
This first example performs a calculation in the first source block
named =top=, the results of this calculation are then saved into the
variable =first= by the header argument =:var first=top=, and it is
used in the calculations of the second source block.
#+SRCNAME: top
#+begin_src emacs-lisp
(+ 4 2)
#+end_src
#+begin_src emacs-lisp :var first=top :results replace
(* first 3)
#+end_src
: 18
This example is the same as the previous only the variable being
passed through is a table rather than a number.
#+begin_src emacs-lisp :results silent
(defun transpose (table)
(apply #'mapcar* #'list table))
#+end_src
#+TBLNAME: top_table
| 1 | 2 | 3 |
| 4 | schulte | 6 |
#+SRCNAME: second_src_example
#+begin_src emacs-lisp :var table=top_table
(transpose table)
#+end_src
#+begin_src emacs-lisp :var table=second_src_example
(transpose table)
#+end_src
| 1 | 2 | 3 |
| 4 | "schulte" | 6 |
* COMMENT Commentary
I'm seeing this as like commit notes, and a place for less formal
communication of the goals of our changes.