DONE Both supplied
[rgr-org-mode.git] / lisp / org-babel-ref.el
blob8bf0814acf68e2bc42de0ab6db0b74420365f592
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 (split-string new-referent ",[ \f\t\n\r\v]*"))))
104 (message "args=%S" args)
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-ref-read-result))
140 ('table (org-babel-ref-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-at-ref-p ()
147 "Return the type of reference located at point or nil if none
148 of the supported reference types are found. Supported reference
149 types are tables and source blocks."
150 (cond ((org-at-table-p) 'table)
151 ((looking-at "^#\\+BEGIN_SRC") 'source-block)
152 ((looking-at "^#\\+RESNAME:") 'results-line)))
154 (defun org-babel-ref-read-result ()
155 "Read the result at `point' into emacs-lisp."
156 (cond
157 ((org-at-table-p) (org-babel-ref-read-table))
158 ((looking-at ": ")
159 (let ((result-string
160 (org-babel-trim
161 (mapconcat (lambda (line) (if (and (> (length line) 1)
162 (string= ": " (substring line 0 2)))
163 (substring line 2)
164 line))
165 (split-string
166 (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
167 "\n"))))
168 (or (org-babel-number-p result-string) result-string)))
169 ((looking-at "^#\\+RESNAME:")
170 (save-excursion (forward-line 1) (org-babel-ref-read-result)))))
172 (defun org-babel-ref-read-table ()
173 "Read the table at `point' into emacs-lisp."
174 (mapcar (lambda (row)
175 (if (and (symbolp row) (equal row 'hline)) row
176 (mapcar #'org-babel-read row)))
177 (org-table-to-lisp)))
179 (provide 'org-babel-ref)
180 ;;; org-babel-ref.el ends here