1 ;;; org-babel-ref.el --- org-babel functions for referencing external data
3 ;; Copyright (C) 2009 Eric Schulte, Dan Davison
5 ;; Author: Eric Schulte, Dan Davison
6 ;; Keywords: literate programming, reproducible research
7 ;; Homepage: http://orgmode.org
12 ;; This program 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, or (at your option)
17 ;; This program 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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Functions for referencing data from the header arguments of a
30 ;; org-babel block. The syntax of such a reference should be
32 ;; #+VAR: variable-name=file:resource-id
34 ;; - variable-name :: the name of the variable to which the value
37 ;; - file :: path to the file containing the resource, or omitted if
38 ;; resource is in the current file
40 ;; - resource-id :: the id or name of the resource
42 ;; So an example of a simple src block referencing table data in the
47 ;; | 4 | org-babel | 6 |
49 ;; #+begin_src emacs-lisp :var table=sandbox
57 (defun org-babel-ref-variables (params)
58 "Takes a parameter alist, and return an alist of variable
59 names, and the emacs-lisp representation of the related value."
60 (mapcar #'org-babel-ref-parse
61 (delq nil
(mapcar (lambda (pair) (if (eq (car pair
) :var
) (cdr pair
))) params
))))
63 (defun org-babel-ref-parse (assignment)
64 "Parse a variable ASSIGNMENT in a header argument. If the
65 right hand side of the assignment has a literal value return that
66 value, otherwise interpret as a reference to an external resource
67 and find it's value using `org-babel-ref-resolve-reference'.
68 Return a list with two elements. The first element of the list
69 will be the name of the variable, and the second will be an
70 emacs-lisp representation of the value of the variable."
72 "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment
)
73 (let ((var (match-string 1 assignment
))
74 (ref (match-string 2 assignment
)))
76 (or (org-babel-ref-literal ref
)
77 (org-babel-ref-resolve-reference ref
))))))
79 (defun org-babel-ref-literal (ref)
80 "Determine if the right side of a header argument variable
81 assignment is a literal value or is a reference to some external
82 resource. If REF is literal then return it's value, otherwise
84 (let ((out (org-babel-read ref
)))
86 (if (string-match "\"\\(.+\\)\"" ref
)
90 (defun org-babel-ref-resolve-reference (ref)
91 "Resolve the reference and return its value"
93 (let ((case-fold-search t
)
94 type args new-refere new-referent result lob-info
)
95 ;; assign any arguments to pass to source block
96 (when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref
)
97 (setq new-refere
(match-string 1 ref
))
98 (setq new-referent
(match-string 2 ref
))
99 ;; (message "new-refere=%S, new-referent=%S" new-refere new-referent) ;; debugging
100 (when (> (length new-refere
) 0)
101 (if (> (length new-referent
) 0)
102 (setq args
(mapcar (lambda (ref) (cons :var ref
))
103 (org-babel-ref-split-args new-referent
))))
104 ;; (message "args=%S" args) ;; debugging
105 (setq ref new-refere
)))
106 (when (string-match "\\(.+\\):\\(.+\\)" ref
)
107 (find-file (match-string 1 ref
))
108 (setf ref
(match-string 2 ref
)))
109 (goto-char (point-min))
110 (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
111 (regexp-quote ref
) "[ \t]*$"))
112 (regexp (concat "^#\\+SRCNAME:[ \t]*"
113 (regexp-quote ref
) "\\(\(.*\)\\)?" "[ \t]*$")))
114 ;; goto ref in the current buffer
116 (or (re-search-forward result_regexp nil t
)
117 (re-search-forward result_regexp nil t
)))
118 (re-search-forward regexp nil t
)
119 (re-search-backward regexp nil t
)
120 ;; check the Library of Babel
121 (setq lob-info
(cdr (assoc (intern ref
) org-babel-library-of-babel
)))))
122 (unless lob-info
(goto-char (match-beginning 0)))
123 ;; ;; TODO: allow searching for names in other buffers
124 ;; (setq id-loc (org-id-find ref 'marker)
125 ;; buffer (marker-buffer id-loc)
126 ;; loc (marker-position id-loc))
127 ;; (move-marker id-loc nil)
128 (progn (message (format "reference '%s' not found in this buffer" ref
))
129 (error (format "reference '%s' not found in this buffer" ref
))))
132 (while (not (setq type
(org-babel-ref-at-ref-p)))
135 (if (or (= (point) (point-min)) (= (point) (point-max)))
136 (error "reference not found"))))
137 ;; (message "type=%S" type) ;; debugging
139 ('results-line
(org-babel-read-result))
140 ('table
(org-babel-read-table))
142 (setq result
(org-babel-execute-src-block t nil args
))
143 (if (symbolp result
) (format "%S" result
) result
))
144 ('lob
(setq result
(org-babel-execute-src-block t lob-info args
)))))))
146 (defun org-babel-ref-split-args (arg-string)
147 "Split ARG-STRING into top-level arguments of balanced parenthesis."
148 (let ((index 0) (depth 0) (buffer "") holder return
)
149 ;; crawl along string, splitting at any ","s which are on the top level
150 (while (< index
(length arg-string
))
151 (setq holder
(substring arg-string index
(+ 1 index
)))
152 (setq buffer
(concat buffer holder
))
153 (setq index
(+ 1 index
))
155 ((string= holder
",")
157 (setq return
(reverse (cons (substring buffer
0 -
1) return
)))
159 ((string= holder
"(") (setq depth
(+ depth
1)))
160 ((string= holder
")") (setq depth
(- depth
1)))))
161 (mapcar #'org-babel-trim
(reverse (cons buffer return
)))))
163 (defun org-babel-ref-at-ref-p ()
164 "Return the type of reference located at point or nil if none
165 of the supported reference types are found. Supported reference
166 types are tables and source blocks."
167 (cond ((org-at-table-p) 'table
)
168 ((looking-at "^#\\+BEGIN_SRC") 'source-block
)
169 ((looking-at "^#\\+RESNAME:") 'results-line
)))
171 (provide 'org-babel-ref
)
172 ;;; org-babel-ref.el ends here