2009-05-24 20:38:01 +00:00
|
|
|
;;; org-babel-ref.el --- org-babel functions for referencing external data
|
2009-03-23 23:29:31 +00:00
|
|
|
|
2009-06-12 23:55:42 +00:00
|
|
|
;; Copyright (C) 2009 Eric Schulte, Dan Davison
|
2009-03-23 23:29:31 +00:00
|
|
|
|
2009-06-12 23:55:42 +00:00
|
|
|
;; Author: Eric Schulte, Dan Davison
|
2009-03-23 23:29:31 +00:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Functions for referencing data from the header arguments of a
|
2009-05-24 20:38:01 +00:00
|
|
|
;; org-babel block. The syntax of such a reference should be
|
2009-03-23 23:29:31 +00:00
|
|
|
;;
|
2009-03-24 00:01:11 +00:00
|
|
|
;; #+VAR: variable-name=file:resource-id
|
2009-03-23 23:29:31 +00:00
|
|
|
;;
|
|
|
|
;; - variable-name :: the name of the variable to which the value
|
|
|
|
;; will be assigned
|
|
|
|
;;
|
|
|
|
;; - file :: path to the file containing the resource, or omitted if
|
|
|
|
;; resource is in the current file
|
|
|
|
;;
|
2009-05-10 01:28:08 +00:00
|
|
|
;; - resource-id :: the id or name of the resource
|
2009-03-23 23:29:31 +00:00
|
|
|
;;
|
|
|
|
;; So an example of a simple src block referencing table data in the
|
|
|
|
;; same file would be
|
|
|
|
;;
|
2009-05-10 01:28:08 +00:00
|
|
|
;; #+TBLNAME: sandbox
|
|
|
|
;; | 1 | 2 | 3 |
|
2009-05-24 20:38:01 +00:00
|
|
|
;; | 4 | org-babel | 6 |
|
2009-05-10 01:28:08 +00:00
|
|
|
;;
|
|
|
|
;; #+begin_src emacs-lisp :var table=sandbox
|
|
|
|
;; (message table)
|
|
|
|
;; #+end_src
|
2009-03-23 23:29:31 +00:00
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
2009-05-24 20:38:01 +00:00
|
|
|
(require 'org-babel)
|
2009-03-23 23:29:31 +00:00
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
(defun org-babel-ref-variables (params)
|
2009-03-24 00:01:11 +00:00
|
|
|
"Takes a parameter alist, and return an alist of variable
|
2009-05-08 23:20:05 +00:00
|
|
|
names, and the emacs-lisp representation of the related value."
|
2009-10-20 02:53:44 +00:00
|
|
|
(let ((assignments
|
|
|
|
(delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params)))
|
2009-11-10 16:50:34 +00:00
|
|
|
(others
|
|
|
|
(delq nil (mapcar (lambda (pair) (unless (eq :var (car pair)) pair)) params))))
|
2010-03-27 21:09:10 +00:00
|
|
|
(mapcar (lambda (assignment) (org-babel-ref-parse assignment)) assignments)))
|
2009-03-24 00:01:11 +00:00
|
|
|
|
2009-10-23 04:56:05 +00:00
|
|
|
(defvar org-babel-ref-split-regexp
|
|
|
|
"[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
|
|
|
|
|
2010-03-27 21:09:10 +00:00
|
|
|
(defun org-babel-ref-parse (assignment &optional params)
|
2009-05-09 16:34:55 +00:00
|
|
|
"Parse a variable ASSIGNMENT in a header argument. If the
|
|
|
|
right hand side of the assignment has a literal value return that
|
|
|
|
value, otherwise interpret as a reference to an external resource
|
2009-05-24 20:38:01 +00:00
|
|
|
and find it's value using `org-babel-ref-resolve-reference'.
|
2009-05-09 16:34:55 +00:00
|
|
|
Return 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 representation of the value of the variable."
|
2009-10-23 04:56:05 +00:00
|
|
|
(if (string-match org-babel-ref-split-regexp assignment)
|
2009-05-09 16:34:55 +00:00
|
|
|
(let ((var (match-string 1 assignment))
|
|
|
|
(ref (match-string 2 assignment)))
|
|
|
|
(cons (intern var)
|
2009-05-24 20:38:01 +00:00
|
|
|
(or (org-babel-ref-literal ref)
|
2009-10-20 02:53:44 +00:00
|
|
|
(org-babel-ref-resolve-reference ref params))))))
|
2009-05-09 16:34:55 +00:00
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
(defun org-babel-ref-literal (ref)
|
2009-05-10 00:44:52 +00:00
|
|
|
"Determine if the right side of a header argument variable
|
|
|
|
assignment is a literal value or is a reference to some external
|
2010-06-01 22:55:59 +00:00
|
|
|
resource. REF should be a string of the right hand side of the
|
|
|
|
assignment. If REF is literal then return it's value, otherwise
|
2009-05-10 00:44:52 +00:00
|
|
|
return nil."
|
2009-05-24 20:38:01 +00:00
|
|
|
(let ((out (org-babel-read ref)))
|
2009-05-24 17:35:13 +00:00
|
|
|
(if (equal out ref)
|
2009-10-13 00:32:57 +00:00
|
|
|
(if (string-match "^\".+\"$" ref)
|
2009-05-24 17:35:13 +00:00
|
|
|
(read ref))
|
|
|
|
out)))
|
2009-05-10 00:44:52 +00:00
|
|
|
|
2010-03-27 21:09:10 +00:00
|
|
|
(defun org-babel-ref-resolve-reference (ref &optional params)
|
2009-07-10 05:20:56 +00:00
|
|
|
"Resolve the reference and return its value"
|
2009-03-24 00:01:11 +00:00
|
|
|
(save-excursion
|
2009-05-11 02:47:22 +00:00
|
|
|
(let ((case-fold-search t)
|
2009-11-14 03:26:44 +00:00
|
|
|
type args new-refere new-referent result lob-info split-file split-ref
|
|
|
|
index index-row index-col)
|
2010-04-24 19:26:34 +00:00
|
|
|
;; if ref is indexed grab the indices -- beware nested indicies
|
|
|
|
(when (and (string-match "\\[\\(.+\\)\\]" ref)
|
|
|
|
(let ((str (substring ref 0 (match-beginning 0))))
|
|
|
|
(= (count ?( str) (count ?) str))))
|
2009-11-14 03:26:44 +00:00
|
|
|
(setq index (match-string 1 ref))
|
|
|
|
(setq ref (substring ref 0 (match-beginning 0))))
|
2009-05-10 00:44:52 +00:00
|
|
|
;; assign any arguments to pass to source block
|
2009-05-24 17:35:13 +00:00
|
|
|
(when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref)
|
|
|
|
(setq new-refere (match-string 1 ref))
|
|
|
|
(setq new-referent (match-string 2 ref))
|
2009-07-22 22:05:13 +00:00
|
|
|
;; (message "new-refere=%S, new-referent=%S" new-refere new-referent) ;; debugging
|
2009-05-24 17:35:13 +00:00
|
|
|
(when (> (length new-refere) 0)
|
|
|
|
(if (> (length new-referent) 0)
|
2009-05-12 02:41:56 +00:00
|
|
|
(setq args (mapcar (lambda (ref) (cons :var ref))
|
2009-07-22 22:04:19 +00:00
|
|
|
(org-babel-ref-split-args new-referent))))
|
2009-07-22 22:05:13 +00:00
|
|
|
;; (message "args=%S" args) ;; debugging
|
2009-05-24 17:35:13 +00:00
|
|
|
(setq ref new-refere)))
|
2009-10-23 04:56:05 +00:00
|
|
|
(when (string-match "^\\(.+\\):\\(.+\\)$" ref)
|
|
|
|
(setq split-file (match-string 1 ref))
|
|
|
|
(setq split-ref (match-string 2 ref))
|
|
|
|
(find-file split-file) (setq ref split-ref))
|
2009-05-10 01:28:08 +00:00
|
|
|
(goto-char (point-min))
|
2009-11-29 17:00:19 +00:00
|
|
|
(if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*"
|
2009-06-14 00:35:39 +00:00
|
|
|
(regexp-quote ref) "[ \t]*$"))
|
2009-11-18 18:31:24 +00:00
|
|
|
(regexp (concat org-babel-source-name-regexp
|
2009-07-10 22:38:12 +00:00
|
|
|
(regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
|
2009-07-21 22:46:26 +00:00
|
|
|
;; goto ref in the current buffer
|
|
|
|
(or (and (not args)
|
|
|
|
(or (re-search-forward result_regexp nil t)
|
2009-10-20 02:57:50 +00:00
|
|
|
(re-search-backward result_regexp nil t)))
|
2009-06-14 00:35:39 +00:00
|
|
|
(re-search-forward regexp nil t)
|
2009-06-14 22:24:41 +00:00
|
|
|
(re-search-backward regexp nil t)
|
|
|
|
;; check the Library of Babel
|
|
|
|
(setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
|
2009-07-12 03:49:51 +00:00
|
|
|
(unless lob-info (goto-char (match-beginning 0)))
|
2009-05-10 01:28:08 +00:00
|
|
|
;; ;; 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)
|
2009-05-11 02:47:22 +00:00
|
|
|
(progn (message (format "reference '%s' not found in this buffer" ref))
|
|
|
|
(error (format "reference '%s' not found in this buffer" ref))))
|
2009-06-14 22:01:55 +00:00
|
|
|
(if lob-info
|
|
|
|
(setq type 'lob)
|
|
|
|
(while (not (setq type (org-babel-ref-at-ref-p)))
|
|
|
|
(forward-line 1)
|
|
|
|
(beginning-of-line)
|
|
|
|
(if (or (= (point) (point-min)) (= (point) (point-max)))
|
|
|
|
(error "reference not found"))))
|
2009-11-20 17:40:50 +00:00
|
|
|
(setq params (org-babel-merge-params params args '((:results . "silent"))))
|
2009-10-20 02:53:44 +00:00
|
|
|
(setq result
|
|
|
|
(case type
|
|
|
|
('results-line (org-babel-read-result))
|
|
|
|
('table (org-babel-read-table))
|
2010-02-10 02:38:50 +00:00
|
|
|
('file (org-babel-read-file))
|
2009-11-20 17:40:50 +00:00
|
|
|
('source-block (org-babel-execute-src-block nil nil params))
|
|
|
|
('lob (org-babel-execute-src-block nil lob-info params))))
|
2009-11-14 03:26:44 +00:00
|
|
|
(if (symbolp result)
|
|
|
|
(format "%S" result)
|
|
|
|
(if (and index (listp result))
|
|
|
|
(org-babel-ref-index-list index result)
|
|
|
|
result)))))
|
|
|
|
|
|
|
|
(defun org-babel-ref-index-list (index lis)
|
|
|
|
"Return the subset of LIS indexed by INDEX. If INDEX is
|
|
|
|
separated by ,s then each PORTION is assumed to index into the
|
|
|
|
next deepest nesting or dimension. A valid PORTION can consist
|
|
|
|
of either an integer index, or two integers separated by a : in
|
|
|
|
which case the entire range is returned."
|
|
|
|
(if (string-match "^,?\\([^,]+\\)" index)
|
|
|
|
(let ((length (length lis))
|
|
|
|
(portion (match-string 1 index))
|
|
|
|
(remainder (substring index (match-end 0))))
|
2010-04-21 15:35:13 +00:00
|
|
|
(flet ((wrap (num) (if (< num 0) (+ length num) num))
|
|
|
|
(open (lis) (if (and (listp lis) (= (length lis) 1)) (car lis) lis)))
|
|
|
|
(open
|
|
|
|
(mapcar
|
|
|
|
(lambda (sub-lis) (org-babel-ref-index-list remainder sub-lis))
|
|
|
|
(if (string-match "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)"
|
|
|
|
portion)
|
|
|
|
(mapcar (lambda (n) (nth n lis))
|
|
|
|
(apply 'number-sequence
|
|
|
|
(if (match-string 2 portion)
|
|
|
|
(list
|
|
|
|
(wrap (string-to-number (match-string 2 portion)))
|
|
|
|
(wrap (string-to-number (match-string 3 portion))))
|
|
|
|
(list (wrap 0) (wrap -1)))))
|
|
|
|
(list (nth (wrap (string-to-number portion)) lis)))))))
|
2009-11-14 03:26:44 +00:00
|
|
|
lis))
|
2009-03-24 00:01:11 +00:00
|
|
|
|
2009-07-22 22:04:19 +00:00
|
|
|
(defun org-babel-ref-split-args (arg-string)
|
|
|
|
"Split ARG-STRING into top-level arguments of balanced parenthesis."
|
|
|
|
(let ((index 0) (depth 0) (buffer "") holder return)
|
|
|
|
;; crawl along string, splitting at any ","s which are on the top level
|
|
|
|
(while (< index (length arg-string))
|
|
|
|
(setq holder (substring arg-string index (+ 1 index)))
|
|
|
|
(setq buffer (concat buffer holder))
|
|
|
|
(setq index (+ 1 index))
|
|
|
|
(cond
|
|
|
|
((string= holder ",")
|
|
|
|
(when (= depth 0)
|
|
|
|
(setq return (reverse (cons (substring buffer 0 -1) return)))
|
|
|
|
(setq buffer "")))
|
2010-02-14 18:48:28 +00:00
|
|
|
((or (string= holder "(") (string= holder "[")) (setq depth (+ depth 1)))
|
|
|
|
((or (string= holder ")") (string= holder "]")) (setq depth (- depth 1)))))
|
2009-07-24 02:55:29 +00:00
|
|
|
(mapcar #'org-babel-trim (reverse (cons buffer return)))))
|
2009-07-22 22:04:19 +00:00
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
(defun org-babel-ref-at-ref-p ()
|
2009-07-10 05:20:56 +00:00
|
|
|
"Return the type of reference located at point or nil if none
|
2009-04-03 00:58:04 +00:00
|
|
|
of the supported reference types are found. Supported reference
|
|
|
|
types are tables and source blocks."
|
|
|
|
(cond ((org-at-table-p) 'table)
|
2009-06-14 00:07:16 +00:00
|
|
|
((looking-at "^#\\+BEGIN_SRC") 'source-block)
|
2010-02-10 02:38:50 +00:00
|
|
|
((looking-at org-bracket-link-regexp) 'file)
|
2009-11-20 17:40:50 +00:00
|
|
|
((looking-at org-babel-result-regexp) 'results-line)))
|
2009-06-14 00:07:16 +00:00
|
|
|
|
2009-05-24 20:38:01 +00:00
|
|
|
(provide 'org-babel-ref)
|
|
|
|
;;; org-babel-ref.el ends here
|