Bump to version 7.6
[org-mode/org-mode-NeilSmithlineMods.git] / lisp / ob-ref.el
blob641518a46db9aecade05606ea35be97a478b0dad
1 ;;; ob-ref.el --- org-babel functions for referencing external data
3 ;; Copyright (C) 2009, 2010 Free Software Foundation, Inc.
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
8 ;; Version: 7.6
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/>.
25 ;;; Commentary:
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
33 ;; will be assigned
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
41 ;; same file would be
43 ;; #+TBLNAME: sandbox
44 ;; | 1 | 2 | 3 |
45 ;; | 4 | org-babel | 6 |
47 ;; #+begin_src emacs-lisp :var table=sandbox
48 ;; (message table)
49 ;; #+end_src
51 ;;; Code:
52 (require 'ob)
53 (eval-when-compile
54 (require 'cl))
56 (declare-function org-remove-if-not "org" (predicate seq))
57 (declare-function org-at-table-p "org" (&optional table-type))
58 (declare-function org-count "org" (CL-ITEM CL-SEQ))
59 (declare-function org-at-item-p "org-list" ())
60 (declare-function org-narrow-to-subtree "org" ())
61 (declare-function org-id-find-id-in-file "org-id" (id file &optional markerp))
62 (declare-function org-show-context "org" (&optional key))
64 (defvar org-babel-ref-split-regexp
65 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
67 (defun org-babel-ref-parse (assignment)
68 "Parse a variable ASSIGNMENT in a header argument.
69 If the right hand side of the assignment has a literal value
70 return that value, otherwise interpret as a reference to an
71 external resource and find it's value using
72 `org-babel-ref-resolve'. Return a list with two elements. The
73 first element of the list will be the name of the variable, and
74 the second will be an emacs-lisp representation of the value of
75 the variable."
76 (when (string-match org-babel-ref-split-regexp assignment)
77 (let ((var (match-string 1 assignment))
78 (ref (match-string 2 assignment)))
79 (cons (intern var)
80 (let ((out (org-babel-read ref)))
81 (if (equal out ref)
82 (if (string-match "^\".*\"$" ref)
83 (read ref)
84 (org-babel-ref-resolve ref))
85 out))))))
87 (defun org-babel-ref-goto-headline-id (id)
88 (goto-char (point-min))
89 (let ((rx (regexp-quote id)))
90 (or (re-search-forward
91 (concat "^[ \t]*:CUSTOM_ID:[ \t]+" rx "[ \t]*$") nil t)
92 (let* ((file (org-id-find-id-file id))
93 (m (when file (org-id-find-id-in-file id file 'marker))))
94 (when (and file m)
95 (message "file:%S" file)
96 (switch-to-buffer (marker-buffer m))
97 (goto-char m)
98 (move-marker m nil)
99 (org-show-context)
100 t)))))
102 (defun org-babel-ref-headline-body ()
103 (save-restriction
104 (org-narrow-to-subtree)
105 (buffer-substring
106 (save-excursion (goto-char (point-min))
107 (forward-line 1)
108 (when (looking-at "[ \t]*:PROPERTIES:")
109 (re-search-forward ":END:" nil)
110 (forward-char))
111 (point))
112 (point-max))))
114 (defvar org-babel-library-of-babel)
115 (defun org-babel-ref-resolve (ref)
116 "Resolve the reference REF and return its value."
117 (save-window-excursion
118 (save-excursion
119 (let ((case-fold-search t)
120 type args new-refere new-header-args new-referent result
121 lob-info split-file split-ref index index-row index-col id)
122 ;; if ref is indexed grab the indices -- beware nested indices
123 (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
124 (let ((str (substring ref 0 (match-beginning 0))))
125 (= (org-count ?( str) (org-count ?) str))))
126 (setq index (match-string 1 ref))
127 (setq ref (substring ref 0 (match-beginning 0))))
128 ;; assign any arguments to pass to source block
129 (when (string-match
130 "^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)\(\\(.*\\)\)$" ref)
131 (setq new-refere (match-string 1 ref))
132 (setq new-header-args (match-string 3 ref))
133 (setq new-referent (match-string 5 ref))
134 (when (> (length new-refere) 0)
135 (when (> (length new-referent) 0)
136 (setq args (mapcar (lambda (ref) (cons :var ref))
137 (org-babel-ref-split-args new-referent))))
138 (when (> (length new-header-args) 0)
139 (setq args (append (org-babel-parse-header-arguments
140 new-header-args) args)))
141 (setq ref new-refere)))
142 (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
143 (setq split-file (match-string 1 ref))
144 (setq split-ref (match-string 2 ref))
145 (find-file split-file) (setq ref split-ref))
146 (save-restriction
147 (widen)
148 (goto-char (point-min))
149 (if (let* ((rx (regexp-quote ref))
150 (res-rx (concat org-babel-result-regexp rx "[ \t]*$"))
151 (src-rx (concat org-babel-src-name-regexp
152 rx "\\(\(.*\)\\)?" "[ \t]*$")))
153 ;; goto ref in the current buffer
154 (or (and (not args)
155 (or (re-search-forward res-rx nil t)
156 (re-search-backward res-rx nil t)))
157 (re-search-forward src-rx nil t)
158 (re-search-backward src-rx nil t)
159 ;; check for local or global headlines by id
160 (setq id (org-babel-ref-goto-headline-id ref))
161 ;; check the Library of Babel
162 (setq lob-info (cdr (assoc (intern ref)
163 org-babel-library-of-babel)))))
164 (unless (or lob-info id) (goto-char (match-beginning 0)))
165 ;; ;; TODO: allow searching for names in other buffers
166 ;; (setq id-loc (org-id-find ref 'marker)
167 ;; buffer (marker-buffer id-loc)
168 ;; loc (marker-position id-loc))
169 ;; (move-marker id-loc nil)
170 (error "reference '%s' not found in this buffer" ref))
171 (cond
172 (lob-info (setq type 'lob))
173 (id (setq type 'id))
174 (t (while (not (setq type (org-babel-ref-at-ref-p)))
175 (forward-line 1)
176 (beginning-of-line)
177 (if (or (= (point) (point-min)) (= (point) (point-max)))
178 (error "reference not found")))))
179 (let ((params (append args '((:results . "silent")))))
180 (setq result
181 (case type
182 (results-line (org-babel-read-result))
183 (table (org-babel-read-table))
184 (list (org-babel-read-list))
185 (file (org-babel-read-link))
186 (source-block (org-babel-execute-src-block nil nil params))
187 (lob (org-babel-execute-src-block
188 nil lob-info params))
189 (id (org-babel-ref-headline-body)))))
190 (if (symbolp result)
191 (format "%S" result)
192 (if (and index (listp result))
193 (org-babel-ref-index-list index result)
194 result)))))))
196 (defun org-babel-ref-index-list (index lis)
197 "Return the subset of LIS indexed by INDEX.
199 Indices are 0 based and negative indices count from the end of
200 LIS, so 0 references the first element of LIS and -1 references
201 the last. If INDEX is separated by \",\"s then each \"portion\"
202 is assumed to index into the next deepest nesting or dimension.
204 A valid \"portion\" can consist of either an integer index, two
205 integers separated by a \":\" in which case the entire range is
206 returned, or an empty string or \"*\" both of which are
207 interpreted to mean the entire range and as such are equivalent
208 to \"0:-1\"."
209 (if (and (> (length index) 0) (string-match "^\\([^,]*\\),?" index))
210 (let ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)")
211 (length (length lis))
212 (portion (match-string 1 index))
213 (remainder (substring index (match-end 0))))
214 (flet ((wrap (num) (if (< num 0) (+ length num) num))
215 (open (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls)))
216 (open
217 (mapcar
218 (lambda (sub-lis)
219 (if (listp sub-lis)
220 (org-babel-ref-index-list remainder sub-lis)
221 sub-lis))
222 (if (or (= 0 (length portion)) (string-match ind-re portion))
223 (mapcar
224 (lambda (n) (nth n lis))
225 (apply 'org-number-sequence
226 (if (and (> (length portion) 0) (match-string 2 portion))
227 (list
228 (wrap (string-to-number (match-string 2 portion)))
229 (wrap (string-to-number (match-string 3 portion))))
230 (list (wrap 0) (wrap -1)))))
231 (list (nth (wrap (string-to-number portion)) lis)))))))
232 lis))
234 (defun org-babel-ref-split-args (arg-string)
235 "Split ARG-STRING into top-level arguments of balanced parenthesis."
236 (let ((index 0) (depth 0) (buffer "") holder return)
237 ;; crawl along string, splitting at any ","s which are on the top level
238 (while (< index (length arg-string))
239 (setq holder (substring arg-string index (+ 1 index)))
240 (setq buffer (concat buffer holder))
241 (setq index (+ 1 index))
242 (cond
243 ((string= holder ",")
244 (when (= depth 0)
245 (setq return (cons (substring buffer 0 -1) return))
246 (setq buffer "")))
247 ((or (string= holder "(") (string= holder "[")) (setq depth (+ depth 1)))
248 ((or (string= holder ")") (string= holder "]")) (setq depth (- depth 1)))))
249 (mapcar #'org-babel-trim (reverse (cons buffer return)))))
251 (defvar org-bracket-link-regexp)
252 (defun org-babel-ref-at-ref-p ()
253 "Return the type of reference located at point.
254 Return nil if none of the supported reference types are found.
255 Supported reference types are tables and source blocks."
256 (cond ((org-at-table-p) 'table)
257 ((org-at-item-p) 'list)
258 ((looking-at "^[ \t]*#\\+BEGIN_SRC") 'source-block)
259 ((looking-at org-bracket-link-regexp) 'file)
260 ((looking-at org-babel-result-regexp) 'results-line)))
262 (provide 'ob-ref)
264 ;; arch-tag: ace4a4f4-ea38-4dac-8fe6-6f52fcc43b6d
266 ;;; ob-ref.el ends here