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