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