1 ;;; org-babel-ref.el --- org-babel functions for referencing external data
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 3, or (at your option)
17 ;; This program is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Functions for referencing data from the header arguments of a
30 ;; org-babel block. The syntax of such a reference should be
32 ;; #+VAR: variable-name=file:resource-id
34 ;; - variable-name :: the name of the variable to which the value
37 ;; - file :: path to the file containing the resource, or omitted if
38 ;; resource is in the current file
40 ;; - resource-id :: the id or name of the resource
42 ;; So an example of a simple src block referencing table data in the
47 ;; | 4 | org-babel | 6 |
49 ;; #+begin_src emacs-lisp :var table=sandbox
57 (defun org-babel-ref-variables (params)
58 "Takes a parameter alist, and return an alist of variable
59 names, and the emacs-lisp representation of the related value."
61 (delq nil
(mapcar (lambda (pair) (if (eq (car pair
) :var
) (cdr pair
))) params
)))
63 (delq nil
(mapcar (lambda (pair) (unless (eq :var
(car pair
)) pair
)) params
))))
64 (mapcar (lambda (assignment) (org-babel-ref-parse assignment others
)) assignments
)))
66 (defvar org-babel-ref-split-regexp
67 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
69 (defun org-babel-ref-parse (assignment params
)
70 "Parse a variable ASSIGNMENT in a header argument. If the
71 right hand side of the assignment has a literal value return that
72 value, otherwise interpret as a reference to an external resource
73 and find it's value using `org-babel-ref-resolve-reference'.
74 Return a list with two elements. The first element of the list
75 will be the name of the variable, and the second will be an
76 emacs-lisp representation of the value of the variable."
77 (if (string-match org-babel-ref-split-regexp assignment
)
78 (let ((var (match-string 1 assignment
))
79 (ref (match-string 2 assignment
)))
81 (or (org-babel-ref-literal ref
)
82 (org-babel-ref-resolve-reference ref params
))))))
84 (defun org-babel-ref-literal (ref)
85 "Determine if the right side of a header argument variable
86 assignment is a literal value or is a reference to some external
87 resource. If REF is literal then return it's value, otherwise
89 (let ((out (org-babel-read ref
)))
91 (if (string-match "^\".+\"$" ref
)
95 (defun org-babel-ref-resolve-reference (ref params
)
96 "Resolve the reference and return its value"
98 (let ((case-fold-search t
)
99 type args new-refere new-referent result lob-info split-file split-ref
100 index index-row index-col
)
101 ;; if ref is indexed grab the indices
102 (when (string-match "\\[\\(.+\\)\\]" ref
)
103 (setq index
(match-string 1 ref
))
104 (setq ref
(substring ref
0 (match-beginning 0))))
105 ;; assign any arguments to pass to source block
106 (when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref
)
107 (setq new-refere
(match-string 1 ref
))
108 (setq new-referent
(match-string 2 ref
))
109 ;; (message "new-refere=%S, new-referent=%S" new-refere new-referent) ;; debugging
110 (when (> (length new-refere
) 0)
111 (if (> (length new-referent
) 0)
112 (setq args
(mapcar (lambda (ref) (cons :var ref
))
113 (org-babel-ref-split-args new-referent
))))
114 ;; (message "args=%S" args) ;; debugging
115 (setq ref new-refere
)))
116 (when (string-match "^\\(.+\\):\\(.+\\)$" ref
)
117 (setq split-file
(match-string 1 ref
))
118 (setq split-ref
(match-string 2 ref
))
119 (find-file split-file
) (setq ref split-ref
))
120 (goto-char (point-min))
121 (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
122 (regexp-quote ref
) "[ \t]*$"))
123 (regexp (concat "^#\\+SRCNAME:[ \t]*"
124 (regexp-quote ref
) "\\(\(.*\)\\)?" "[ \t]*$")))
125 ;; goto ref in the current buffer
127 (or (re-search-forward result_regexp nil t
)
128 (re-search-backward result_regexp nil t
)))
129 (re-search-forward regexp nil t
)
130 (re-search-backward regexp nil t
)
131 ;; check the Library of Babel
132 (setq lob-info
(cdr (assoc (intern ref
) org-babel-library-of-babel
)))))
133 (unless lob-info
(goto-char (match-beginning 0)))
134 ;; ;; TODO: allow searching for names in other buffers
135 ;; (setq id-loc (org-id-find ref 'marker)
136 ;; buffer (marker-buffer id-loc)
137 ;; loc (marker-position id-loc))
138 ;; (move-marker id-loc nil)
139 (progn (message (format "reference '%s' not found in this buffer" ref
))
140 (error (format "reference '%s' not found in this buffer" ref
))))
143 (while (not (setq type
(org-babel-ref-at-ref-p)))
146 (if (or (= (point) (point-min)) (= (point) (point-max)))
147 (error "reference not found"))))
148 (setq params
(org-babel-merge-params params args
'((:results .
"silent"))))
151 ('results-line
(org-babel-read-result))
152 ('table
(org-babel-read-table))
153 ('source-block
(org-babel-execute-src-block nil nil params
))
154 ('lob
(org-babel-execute-src-block nil lob-info params
))))
157 (if (and index
(listp result
))
158 (org-babel-ref-index-list index result
)
161 (defun org-babel-ref-index-list (index lis
)
162 "Return the subset of LIS indexed by INDEX. If INDEX is
163 separated by ,s then each PORTION is assumed to index into the
164 next deepest nesting or dimension. A valid PORTION can consist
165 of either an integer index, or two integers separated by a : in
166 which case the entire range is returned."
167 (if (string-match "^,?\\([^,]+\\)" index
)
168 (let ((length (length lis
))
169 (portion (match-string 1 index
))
170 (remainder (substring index
(match-end 0))))
171 (flet ((wrap (num) (if (< num
0) (+ length num
) num
)))
173 (lambda (sub-lis) (org-babel-ref-index-list remainder sub-lis
))
174 (if (string-match "\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)" portion
)
175 (mapcar (lambda (n) (nth n lis
))
177 (wrap (string-to-number (match-string 1 portion
)))
178 (wrap (string-to-number (match-string 2 portion
)))))
179 (list (nth (wrap (string-to-number portion
)) lis
))))))
182 (defun org-babel-ref-split-args (arg-string)
183 "Split ARG-STRING into top-level arguments of balanced parenthesis."
184 (let ((index 0) (depth 0) (buffer "") holder return
)
185 ;; crawl along string, splitting at any ","s which are on the top level
186 (while (< index
(length arg-string
))
187 (setq holder
(substring arg-string index
(+ 1 index
)))
188 (setq buffer
(concat buffer holder
))
189 (setq index
(+ 1 index
))
191 ((string= holder
",")
193 (setq return
(reverse (cons (substring buffer
0 -
1) return
)))
195 ((string= holder
"(") (setq depth
(+ depth
1)))
196 ((string= holder
")") (setq depth
(- depth
1)))))
197 (mapcar #'org-babel-trim
(reverse (cons buffer return
)))))
199 (defun org-babel-ref-at-ref-p ()
200 "Return the type of reference located at point or nil if none
201 of the supported reference types are found. Supported reference
202 types are tables and source blocks."
203 (cond ((org-at-table-p) 'table
)
204 ((looking-at "^#\\+BEGIN_SRC") 'source-block
)
205 ((looking-at org-babel-result-regexp
) 'results-line
)))
207 (provide 'org-babel-ref
)
208 ;;; org-babel-ref.el ends here