ob-tests: adding tests of new un-named arguments
[org-mode/org-jambu.git] / lisp / ob-ref.el
blob67ff135a1d6adbaebdec88f96b0b7dc58b91a0b3
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.5
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" ())
61 (defvar org-babel-ref-split-regexp
62 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
64 (defun org-babel-ref-parse (assignment)
65 "Parse a variable ASSIGNMENT in a header argument.
66 If the right hand side of the assignment has a literal value
67 return that value, otherwise interpret as a reference to an
68 external resource and find it's value using
69 `org-babel-ref-resolve'. Return a list with two elements. The
70 first element of the list will be the name of the variable, and
71 the second will be an emacs-lisp representation of the value of
72 the variable."
73 (when (string-match org-babel-ref-split-regexp assignment)
74 (let ((var (match-string 1 assignment))
75 (ref (match-string 2 assignment)))
76 (cons (intern var)
77 (let ((out (org-babel-read ref)))
78 (if (equal out ref)
79 (if (string-match "^\".*\"$" ref)
80 (read ref)
81 (org-babel-ref-resolve ref))
82 out))))))
84 (defvar org-babel-library-of-babel)
85 (defun org-babel-ref-resolve (ref)
86 "Resolve the reference REF and return its value."
87 (save-window-excursion
88 (save-excursion
89 (let ((case-fold-search t)
90 type args new-refere new-header-args new-referent result
91 lob-info split-file split-ref index index-row index-col)
92 ;; if ref is indexed grab the indices -- beware nested indices
93 (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
94 (let ((str (substring ref 0 (match-beginning 0))))
95 (= (org-count ?( str) (org-count ?) str))))
96 (setq index (match-string 1 ref))
97 (setq ref (substring ref 0 (match-beginning 0))))
98 ;; assign any arguments to pass to source block
99 (when (string-match
100 "^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)\(\\(.*\\)\)$" ref)
101 (setq new-refere (match-string 1 ref))
102 (setq new-header-args (match-string 3 ref))
103 (setq new-referent (match-string 5 ref))
104 (when (> (length new-refere) 0)
105 (when (> (length new-referent) 0)
106 (setq args (mapcar (lambda (ref) (cons :var ref))
107 (org-babel-ref-split-args new-referent))))
108 (when (> (length new-header-args) 0)
109 (setq args (append (org-babel-parse-header-arguments new-header-args)
110 args)))
111 (setq ref new-refere)))
112 (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
113 (setq split-file (match-string 1 ref))
114 (setq split-ref (match-string 2 ref))
115 (find-file split-file) (setq ref split-ref))
116 (save-restriction
117 (widen)
118 (goto-char (point-min))
119 (if (let* ((rx (regexp-quote ref))
120 (res-rx (concat org-babel-result-regexp rx "[ \t]*$"))
121 (src-rx (concat org-babel-src-name-regexp
122 rx "\\(\(.*\)\\)?" "[ \t]*$")))
123 ;; goto ref in the current buffer
124 (or (and (not args)
125 (or (re-search-forward res-rx nil t)
126 (re-search-backward res-rx nil t)))
127 (re-search-forward src-rx nil t)
128 (re-search-backward src-rx nil t)
129 ;; check the Library of Babel
130 (setq lob-info (cdr (assoc (intern ref)
131 org-babel-library-of-babel)))))
132 (unless lob-info (goto-char (match-beginning 0)))
133 ;; ;; TODO: allow searching for names in other buffers
134 ;; (setq id-loc (org-id-find ref 'marker)
135 ;; buffer (marker-buffer id-loc)
136 ;; loc (marker-position id-loc))
137 ;; (move-marker id-loc nil)
138 (error "reference '%s' not found in this buffer" ref))
139 (if lob-info
140 (setq type 'lob)
141 (while (not (setq type (org-babel-ref-at-ref-p)))
142 (forward-line 1)
143 (beginning-of-line)
144 (if (or (= (point) (point-min)) (= (point) (point-max)))
145 (error "reference not found"))))
146 (let ((params (append args '((:results . "silent")))))
147 (setq result
148 (case type
149 ('results-line (org-babel-read-result))
150 ('table (org-babel-read-table))
151 ('list (org-babel-read-list))
152 ('file (org-babel-read-link))
153 ('source-block (org-babel-execute-src-block nil nil params))
154 ('lob (org-babel-execute-src-block nil lob-info params)))))
155 (if (symbolp result)
156 (format "%S" result)
157 (if (and index (listp result))
158 (org-babel-ref-index-list index result)
159 result)))))))
161 (defun org-babel-ref-index-list (index lis)
162 "Return the subset of LIS indexed by INDEX.
164 Indices are 0 based and negative indices count from the end of
165 LIS, so 0 references the first element of LIS and -1 references
166 the last. If INDEX is separated by \",\"s then each \"portion\"
167 is assumed to index into the next deepest nesting or dimension.
169 A valid \"portion\" can consist of either an integer index, two
170 integers separated by a \":\" in which case the entire range is
171 returned, or an empty string or \"*\" both of which are
172 interpreted to mean the entire range and as such are equivalent
173 to \"0:-1\"."
174 (if (and (> (length index) 0) (string-match "^\\([^,]*\\),?" index))
175 (let ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)")
176 (length (length lis))
177 (portion (match-string 1 index))
178 (remainder (substring index (match-end 0))))
179 (flet ((wrap (num) (if (< num 0) (+ length num) num))
180 (open (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls)))
181 (open
182 (mapcar
183 (lambda (sub-lis)
184 (if (listp sub-lis)
185 (org-babel-ref-index-list remainder sub-lis)
186 sub-lis))
187 (if (or (= 0 (length portion)) (string-match ind-re portion))
188 (mapcar
189 (lambda (n) (nth n lis))
190 (apply 'org-number-sequence
191 (if (and (> (length portion) 0) (match-string 2 portion))
192 (list
193 (wrap (string-to-number (match-string 2 portion)))
194 (wrap (string-to-number (match-string 3 portion))))
195 (list (wrap 0) (wrap -1)))))
196 (list (nth (wrap (string-to-number portion)) lis)))))))
197 lis))
199 (defun org-babel-ref-split-args (arg-string)
200 "Split ARG-STRING into top-level arguments of balanced parenthesis."
201 (let ((index 0) (depth 0) (buffer "") holder return)
202 ;; crawl along string, splitting at any ","s which are on the top level
203 (while (< index (length arg-string))
204 (setq holder (substring arg-string index (+ 1 index)))
205 (setq buffer (concat buffer holder))
206 (setq index (+ 1 index))
207 (cond
208 ((string= holder ",")
209 (when (= depth 0)
210 (setq return (reverse (cons (substring buffer 0 -1) return)))
211 (setq buffer "")))
212 ((or (string= holder "(") (string= holder "[")) (setq depth (+ depth 1)))
213 ((or (string= holder ")") (string= holder "]")) (setq depth (- depth 1)))))
214 (mapcar #'org-babel-trim (reverse (cons buffer return)))))
216 (defvar org-bracket-link-regexp)
217 (defun org-babel-ref-at-ref-p ()
218 "Return the type of reference located at point.
219 Return nil if none of the supported reference types are found.
220 Supported reference types are tables and source blocks."
221 (cond ((org-at-table-p) 'table)
222 ((org-at-item-p) 'list)
223 ((looking-at "^[ \t]*#\\+BEGIN_SRC") 'source-block)
224 ((looking-at org-bracket-link-regexp) 'file)
225 ((looking-at org-babel-result-regexp) 'results-line)))
227 (provide 'ob-ref)
229 ;; arch-tag: ace4a4f4-ea38-4dac-8fe6-6f52fcc43b6d
231 ;;; ob-ref.el ends here