浏览代码

added alternate single-line syntax for Library of Babel evaluation

Eric Schulte 16 年之前
父节点
当前提交
82e9587c72
共有 2 个文件被更改,包括 82 次插入2 次删除
  1. 36 2
      library-of-babel.org
  2. 46 0
      lisp/org-babel-lob.el

+ 36 - 2
library-of-babel.org

@@ -12,10 +12,44 @@
 
 
 
+* One Liner
+  Here's a different type of syntax that may work.
+
+  This uses one-liners of the form
+
+: #+lob:source-block-name variables
+
+  This takes advantage of the functionality already present in
+  [[file:lisp/org-babel-ref.el][org-babel-ref]] for resolving references to other source blocks which
+  may contain variable specifications.  See the bottom half of
+  [[file:lisp/org-babel-lob.el][org-babel-lob]] for the new implementation.  To test it out load
+  org-babel-lob and press =C-cC-c= on the =#+lob:= line further down.
+
+#+resname: R-plot-default-data
+| 0 | 0 |
+
+#+srcname: my-R-plot
+#+begin_src R :results silent :var data=R-plot-default-data
+plot(data)
+#+end_src
+
+#+tblname: example-R-plot-data
+| 1 |  2 |
+| 2 |  4 |
+| 3 |  9 |
+| 4 | 16 |
+| 5 | 25 |
+
+#+lob:my-R-plot data=example-R-plot-data
+
 * Plotting code
   Plot column 2 (y axis) against column 1 (x axis). Columns 3 and beyond, if present, are ignored.
-#+srcname: plot
-#+begin_src R
+
+#+resname: R-plot-default-data
+| 0 | 0 |
+
+#+srcname: R-plot
+#+begin_src R :results silent :var data=R-plot-default-data
 plot(data)
 #+end_src
 

+ 46 - 0
lisp/org-babel-lob.el

@@ -61,4 +61,50 @@
 		     (gethash srcname org-babel-library-of-babel))))
       (org-babel-execute-src-block nil info params))))
 
+;; alternate 1-liner syntax, this uses `seb' from org-babel-table.el
+(require 'org-babel-table)
+
+(defvar org-babel-lob-one-liner-regexp
+  "#\\+lob:\\([^ \t\n\r]+\\)\\([ \t]+\\([^\n]+\\)\\)?\n")
+
+(defun org-babel-lob-execute-maybe ()
+  "Detect if this is context for a org-babel Library Of Babel
+src-block and if so then run the appropriate source block from
+the Library."
+  (interactive)
+  (let ((info (org-babel-lob-get-info)))
+    (if info (progn (org-babel-lob-execute info) t) nil)))
+
+(add-hook 'org-ctrl-c-ctrl-c-hook 'org-babel-lob-execute-maybe)
+
+(defun org-babel-lob-get-info ()
+  "Return the information of the current Library of Babel line as
+a list of the following form.
+
+  (source-block-name header-arguments-alist)"
+  (let ((case-fold-search t))
+    (save-excursion
+      (move-beginning-of-line 1)
+      (if (looking-at org-babel-lob-one-liner-regexp)
+          (cons (org-babel-clean-text-properties (match-string 1))
+                (delq nil (mapcar (lambda (assignment)
+                                    (save-match-data
+                                      (if (string-match "\\(.+\\)=\\(.+\\)" assignment)
+                                          (list (org-babel-clean-text-properties (match-string 1 assignment))
+                                                (org-babel-clean-text-properties (match-string 2 assignment)))
+                                        nil)))
+                                  (split-string (match-string 3)))))))))
+
+(defun org-babel-lob-execute (info)
+  (let ((params (org-babel-parse-header-arguments
+                       (concat ":var results="
+                               (car info)
+                               "("
+                               (mapconcat (lambda (var-spec)
+                                            (format "%s=%s" (first var-spec) (second var-spec)))
+                                          (cdr info) ", ")
+                               ")"))))
+    (message "params=%S" params)
+    (org-babel-execute-src-block t (list "emacs-lisp" "results" params))))
+
 (provide 'org-babel-lob)