Merge commit 'origin/master'
[rgr-org-mode.git] / lisp / org-babel-ref.el
blob0e8695f6fd4f3ec240598f0f2341f49077125552
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
8 ;; Version: 0.01
10 ;;; License:
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)
15 ;; any later version.
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.
27 ;;; Commentary:
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
35 ;; will be assigned
36 ;;
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
43 ;; same file would be
45 ;; #+TBLNAME: sandbox
46 ;; | 1 | 2 | 3 |
47 ;; | 4 | org-babel | 6 |
49 ;; #+begin_src emacs-lisp :var table=sandbox
50 ;; (message table)
51 ;; #+end_src
54 ;;; Code:
55 (require 'org-babel)
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."
71 (if (string-match
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)))
75 (cons (intern var)
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
83 return nil."
84 (let ((out (org-babel-read ref)))
85 (if (equal out ref)
86 (if (string-match "\"\\(.+\\)\"" ref)
87 (read ref))
88 out)))
90 (defun org-babel-ref-resolve-reference (ref)
91 "Resolve the reference and return its value"
92 (save-excursion
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
115 (or (and (not args)
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))))
130 (if lob-info
131 (setq type 'lob)
132 (while (not (setq type (org-babel-ref-at-ref-p)))
133 (forward-line 1)
134 (beginning-of-line)
135 (if (or (= (point) (point-min)) (= (point) (point-max)))
136 (error "reference not found"))))
137 ;; (message "type=%S" type) ;; debugging
138 (case type
139 ('results-line (org-babel-read-result))
140 ('table (org-babel-read-table))
141 ('source-block
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))
154 (cond
155 ((string= holder ",")
156 (when (= depth 0)
157 (setq return (reverse (cons (substring buffer 0 -1) return)))
158 (setq buffer "")))
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