1 ;;; ob-ref.el --- Babel Functions for Referencing External Data -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2009-2015 Free Software Foundation, Inc.
5 ;; Authors: Eric Schulte
7 ;; Keywords: literate programming, reproducible research
8 ;; Homepage: http://orgmode.org
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs 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 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs 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. If not, see <http://www.gnu.org/licenses/>.
27 ;; Functions for referencing data from the header arguments of a
28 ;; org-babel block. The syntax of such a reference should be
30 ;; #+VAR: variable-name=file:resource-id
32 ;; - variable-name :: the name of the variable to which the value
35 ;; - file :: path to the file containing the resource, or omitted if
36 ;; resource is in the current file
38 ;; - resource-id :: the id or name of the resource
40 ;; So an example of a simple src block referencing table data in the
45 ;; | 4 | org-babel | 6 |
47 ;; #+begin_src emacs-lisp :var table=sandbox
56 (declare-function org-at-item-p
"org-list" ())
57 (declare-function org-at-table-p
"org" (&optional table-type
))
58 (declare-function org-babel-lob-execute
"ob-lob" (info))
59 (declare-function org-babel-lob-get-info
"ob-lob" nil
)
60 (declare-function org-count
"org" (cl-item cl-seq
))
61 (declare-function org-element-at-point
"org-element" ())
62 (declare-function org-element-property
"org-element" (property element
))
63 (declare-function org-element-type
"org-element" (element))
64 (declare-function org-end-of-meta-data
"org" (&optional full
))
65 (declare-function org-find-property
"org" (property &optional value
))
66 (declare-function org-id-find-id-file
"org-id" (id))
67 (declare-function org-id-find-id-in-file
"org-id" (id file
&optional markerp
))
68 (declare-function org-in-commented-heading-p
"org" (&optional no-inheritance
))
69 (declare-function org-narrow-to-subtree
"org" ())
70 (declare-function org-pop-to-buffer-same-window
"org-compat"
71 (&optional buffer-or-name norecord label
))
72 (declare-function org-remove-if-not
"org" (predicate seq
))
73 (declare-function org-show-context
"org" (&optional key
))
76 (defvar org-babel-ref-split-regexp
77 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
79 (defvar org-babel-update-intermediate nil
80 "Update the in-buffer results of code blocks executed to resolve references.")
82 (defun org-babel-ref-parse (assignment)
83 "Parse a variable ASSIGNMENT in a header argument.
84 If the right hand side of the assignment has a literal value
85 return that value, otherwise interpret as a reference to an
86 external resource and find its value using
87 `org-babel-ref-resolve'. Return a list with two elements. The
88 first element of the list will be the name of the variable, and
89 the second will be an emacs-lisp representation of the value of
91 (when (string-match org-babel-ref-split-regexp assignment
)
92 (let ((var (match-string 1 assignment
))
93 (ref (match-string 2 assignment
)))
95 (let ((out (save-excursion
96 (when org-babel-current-src-block-location
97 (goto-char (if (markerp org-babel-current-src-block-location
)
98 (marker-position org-babel-current-src-block-location
)
99 org-babel-current-src-block-location
)))
100 (org-babel-read ref
))))
102 (if (string-match "^\".*\"$" ref
)
104 (org-babel-ref-resolve ref
))
107 (defun org-babel-ref-goto-headline-id (id)
108 (or (let ((h (org-find-property "CUSTOM_ID" id
)))
109 (when h
(goto-char h
)))
110 (let* ((file (org-id-find-id-file id
))
111 (m (when file
(org-id-find-id-in-file id file
'marker
))))
113 (message "file:%S" file
)
114 (org-pop-to-buffer-same-window (marker-buffer m
))
120 (defun org-babel-ref-headline-body ()
122 (org-narrow-to-subtree)
124 (save-excursion (goto-char (point-min))
125 (org-end-of-meta-data)
129 (defvar org-babel-lob-one-liner-regexp
)
130 (defvar org-babel-library-of-babel
)
131 (defun org-babel-ref-resolve (ref)
132 "Resolve the reference REF and return its value."
133 (save-window-excursion
134 (with-current-buffer (or org-babel-exp-reference-buffer
(current-buffer))
136 (let ((case-fold-search t
)
137 args new-refere new-header-args new-referent split-file split-ref
139 ;; if ref is indexed grab the indices -- beware nested indices
140 (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref
)
141 (let ((str (substring ref
0 (match-beginning 0))))
142 (= (org-count ?
( str
) (org-count ?
) str
))))
143 (setq index
(match-string 1 ref
))
144 (setq ref
(substring ref
0 (match-beginning 0))))
145 ;; assign any arguments to pass to source block
147 "^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)(\\(.*\\))$" ref
)
148 (setq new-refere
(match-string 1 ref
))
149 (setq new-header-args
(match-string 3 ref
))
150 (setq new-referent
(match-string 5 ref
))
151 (when (> (length new-refere
) 0)
152 (when (> (length new-referent
) 0)
153 (setq args
(mapcar (lambda (ref) (cons :var ref
))
154 (org-babel-ref-split-args new-referent
))))
155 (when (> (length new-header-args
) 0)
156 (setq args
(append (org-babel-parse-header-arguments
157 new-header-args
) args
)))
158 (setq ref new-refere
)))
159 (when (string-match "^\\(.+\\):\\(.+\\)$" ref
)
160 (setq split-file
(match-string 1 ref
))
161 (setq split-ref
(match-string 2 ref
))
162 (find-file split-file
)
163 (setq ref split-ref
))
164 (org-with-wide-buffer
165 (goto-char (point-min))
166 (let* ((params (append args
'((:results .
"silent"))))
167 (regexp (org-babel-named-data-regexp-for-name ref
))
171 ;; Check for code blocks or named data.
173 (while (re-search-forward regexp nil t
)
174 ;; Ignore COMMENTed headings and orphaned
175 ;; affiliated keywords.
176 (unless (org-in-commented-heading-p)
177 (let ((e (org-element-at-point)))
178 (when (equal (org-element-property :name e
) ref
)
180 (org-element-property :post-affiliated e
))
181 (pcase (org-element-type e
)
184 (org-babel-lob-execute
185 (org-babel-lob-get-info))))
188 (org-babel-execute-src-block
191 (not org-babel-update-intermediate
)
193 ((and (let v
(org-babel-read-element e
))
196 (_ (error "Reference not found")))))))
198 ;; Check for local or global headlines by ID.
199 ((org-babel-ref-goto-headline-id ref
)
200 (org-babel-ref-headline-body))
201 ;; Check the Library of Babel.
203 (cdr (assq (intern ref
) org-babel-library-of-babel
)))
204 (org-babel-execute-src-block nil lob-info params
))
205 (t (error "Reference `%s' not found in this buffer" ref
)))))
207 ((symbolp result
) (format "%S" result
))
208 ((and index
(listp result
))
209 (org-babel-ref-index-list index result
))
212 (defun org-babel-ref-index-list (index lis
)
213 "Return the subset of LIS indexed by INDEX.
215 Indices are 0 based and negative indices count from the end of
216 LIS, so 0 references the first element of LIS and -1 references
217 the last. If INDEX is separated by \",\"s then each \"portion\"
218 is assumed to index into the next deepest nesting or dimension.
220 A valid \"portion\" can consist of either an integer index, two
221 integers separated by a \":\" in which case the entire range is
222 returned, or an empty string or \"*\" both of which are
223 interpreted to mean the entire range and as such are equivalent
225 (if (and (> (length index
) 0) (string-match "^\\([^,]*\\),?" index
))
226 (let* ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\\*\\)")
228 (portion (match-string 1 index
))
229 (remainder (substring index
(match-end 0)))
230 (wrap (lambda (num) (if (< num
0) (+ lgth num
) num
)))
231 (open (lambda (ls) (if (and (listp ls
) (= (length ls
) 1)) (car ls
) ls
))))
237 (org-babel-ref-index-list remainder sub-lis
)
239 (if (or (= 0 (length portion
)) (string-match ind-re portion
))
241 (lambda (n) (nth n lis
))
242 (apply 'org-number-sequence
243 (if (and (> (length portion
) 0) (match-string 2 portion
))
245 (funcall wrap
(string-to-number (match-string 2 portion
)))
246 (funcall wrap
(string-to-number (match-string 3 portion
))))
247 (list (funcall wrap
0) (funcall wrap -
1)))))
248 (list (nth (funcall wrap
(string-to-number portion
)) lis
))))))
251 (defun org-babel-ref-split-args (arg-string)
252 "Split ARG-STRING into top-level arguments of balanced parenthesis."
253 (mapcar #'org-babel-trim
(org-babel-balanced-split arg-string
44)))
258 ;;; ob-ref.el ends here