2009-05-24 16:38:01 -04:00
|
|
|
;;; org-babel-ref.el --- org-babel functions for referencing external data
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2009-06-12 19:55:42 -04:00
|
|
|
;; Copyright (C) 2009 Eric Schulte, Dan Davison
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2009-06-12 19:55:42 -04:00
|
|
|
;; Author: Eric Schulte, Dan Davison
|
2009-03-23 19:29:31 -04:00
|
|
|
;; Keywords: literate programming, reproducible research
|
|
|
|
;; Homepage: http://orgmode.org
|
|
|
|
;; Version: 0.01
|
|
|
|
|
|
|
|
;;; License:
|
|
|
|
|
|
|
|
;; This program is free software; you can redistribute it and/or modify
|
|
|
|
;; it under the terms of the GNU General Public License as published by
|
|
|
|
;; the Free Software Foundation; either version 3, or (at your option)
|
|
|
|
;; any later version.
|
|
|
|
;;
|
|
|
|
;; This program is distributed in the hope that it will be useful,
|
|
|
|
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
;; GNU General Public License for more details.
|
|
|
|
;;
|
|
|
|
;; You should have received a copy of the GNU General Public License
|
|
|
|
;; along with GNU Emacs; see the file COPYING. If not, write to the
|
|
|
|
;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
;; Boston, MA 02110-1301, USA.
|
|
|
|
|
|
|
|
;;; Commentary:
|
|
|
|
|
|
|
|
;; Functions for referencing data from the header arguments of a
|
2009-05-24 16:38:01 -04:00
|
|
|
;; org-babel block. The syntax of such a reference should be
|
2009-03-23 19:29:31 -04:00
|
|
|
;;
|
2009-03-23 20:01:11 -04:00
|
|
|
;; #+VAR: variable-name=file:resource-id
|
2009-03-23 19:29:31 -04:00
|
|
|
;;
|
|
|
|
;; - variable-name :: the name of the variable to which the value
|
|
|
|
;; will be assigned
|
|
|
|
;;
|
|
|
|
;; - file :: path to the file containing the resource, or omitted if
|
|
|
|
;; resource is in the current file
|
|
|
|
;;
|
2009-05-09 21:28:08 -04:00
|
|
|
;; - resource-id :: the id or name of the resource
|
2009-03-23 19:29:31 -04:00
|
|
|
;;
|
|
|
|
;; So an example of a simple src block referencing table data in the
|
|
|
|
;; same file would be
|
|
|
|
;;
|
2009-05-09 21:28:08 -04:00
|
|
|
;; #+TBLNAME: sandbox
|
|
|
|
;; | 1 | 2 | 3 |
|
2009-05-24 16:38:01 -04:00
|
|
|
;; | 4 | org-babel | 6 |
|
2009-05-09 21:28:08 -04:00
|
|
|
;;
|
|
|
|
;; #+begin_src emacs-lisp :var table=sandbox
|
|
|
|
;; (message table)
|
|
|
|
;; #+end_src
|
2009-03-23 19:29:31 -04:00
|
|
|
;;
|
|
|
|
|
|
|
|
;;; Code:
|
2009-05-24 16:38:01 -04:00
|
|
|
(require 'org-babel)
|
2009-03-23 19:29:31 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-variables (params)
|
2009-03-23 20:01:11 -04:00
|
|
|
"Takes a parameter alist, and return an alist of variable
|
2009-05-08 19:20:05 -04:00
|
|
|
names, and the emacs-lisp representation of the related value."
|
2009-05-24 16:38:01 -04:00
|
|
|
(mapcar #'org-babel-ref-parse
|
2009-05-09 21:28:08 -04:00
|
|
|
(delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
|
2009-03-23 20:01:11 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-parse (assignment)
|
2009-05-09 12:34:55 -04:00
|
|
|
"Parse a variable ASSIGNMENT in a header argument. If the
|
|
|
|
right hand side of the assignment has a literal value return that
|
|
|
|
value, otherwise interpret as a reference to an external resource
|
2009-05-24 16:38:01 -04:00
|
|
|
and find it's value using `org-babel-ref-resolve-reference'.
|
2009-05-09 12:34:55 -04:00
|
|
|
Return a list with two elements. The first element of the list
|
|
|
|
will be the name of the variable, and the second will be an
|
|
|
|
emacs-lisp representation of the value of the variable."
|
2009-05-10 11:27:06 -04:00
|
|
|
(if (string-match "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment)
|
2009-05-09 12:34:55 -04:00
|
|
|
(let ((var (match-string 1 assignment))
|
|
|
|
(ref (match-string 2 assignment)))
|
|
|
|
(cons (intern var)
|
2009-05-24 16:38:01 -04:00
|
|
|
(or (org-babel-ref-literal ref)
|
|
|
|
(org-babel-ref-resolve-reference ref))))))
|
2009-05-09 12:34:55 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-literal (ref)
|
2009-05-09 20:44:52 -04:00
|
|
|
"Determine if the right side of a header argument variable
|
|
|
|
assignment is a literal value or is a reference to some external
|
|
|
|
resource. If REF is literal then return it's value, otherwise
|
|
|
|
return nil."
|
2009-05-24 16:38:01 -04:00
|
|
|
(let ((out (org-babel-read ref)))
|
2009-05-24 13:35:13 -04:00
|
|
|
(if (equal out ref)
|
|
|
|
(if (string-match "\"\\(.+\\)\"" ref)
|
|
|
|
(read ref))
|
|
|
|
out)))
|
2009-05-09 20:44:52 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-resolve-reference (ref)
|
2009-05-09 12:34:55 -04:00
|
|
|
"Resolve the reference and return it's value"
|
2009-03-23 20:01:11 -04:00
|
|
|
(save-excursion
|
2009-05-10 22:47:22 -04:00
|
|
|
(let ((case-fold-search t)
|
2009-06-14 18:01:55 -04:00
|
|
|
type args new-refere new-referent result lob-info)
|
2009-05-09 20:44:52 -04:00
|
|
|
;; assign any arguments to pass to source block
|
2009-05-24 13:35:13 -04:00
|
|
|
(when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref)
|
|
|
|
(setq new-refere (match-string 1 ref))
|
|
|
|
(setq new-referent (match-string 2 ref))
|
|
|
|
;; (message (format "first second %S -- %S" new-refere new-referent)) ;; debugging
|
|
|
|
(when (> (length new-refere) 0)
|
|
|
|
(if (> (length new-referent) 0)
|
2009-05-11 22:41:56 -04:00
|
|
|
(setq args (mapcar (lambda (ref) (cons :var ref))
|
2009-05-24 13:35:13 -04:00
|
|
|
(split-string new-referent ",[ \f\t\n\r\v]*"))))
|
|
|
|
(setq ref new-refere)))
|
2009-05-09 12:34:55 -04:00
|
|
|
(when (string-match "\\(.+\\):\\(.+\\)" ref)
|
|
|
|
(find-file (match-string 1 ref))
|
|
|
|
(setf ref (match-string 2 ref)))
|
2009-05-09 21:28:08 -04:00
|
|
|
(goto-char (point-min))
|
2009-06-13 20:07:16 -04:00
|
|
|
(if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
|
2009-06-13 20:35:39 -04:00
|
|
|
(regexp-quote ref) "[ \t]*$"))
|
|
|
|
(regexp (concat "^#\\+SRCNAME:[ \t]*"
|
|
|
|
(regexp-quote ref) "[ \t]*$")))
|
|
|
|
(or (re-search-forward result_regexp nil t)
|
|
|
|
(re-search-forward result_regexp nil t)
|
|
|
|
(re-search-forward regexp nil t)
|
2009-06-14 18:24:41 -04:00
|
|
|
(re-search-backward regexp nil t)
|
|
|
|
;; check the Library of Babel
|
|
|
|
(setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
|
2009-06-13 20:07:16 -04:00
|
|
|
(goto-char (match-beginning 0))
|
2009-05-09 21:28:08 -04:00
|
|
|
;; ;; TODO: allow searching for names in other buffers
|
|
|
|
;; (setq id-loc (org-id-find ref 'marker)
|
|
|
|
;; buffer (marker-buffer id-loc)
|
|
|
|
;; loc (marker-position id-loc))
|
|
|
|
;; (move-marker id-loc nil)
|
2009-05-10 22:47:22 -04:00
|
|
|
(progn (message (format "reference '%s' not found in this buffer" ref))
|
|
|
|
(error (format "reference '%s' not found in this buffer" ref))))
|
2009-06-14 18:01:55 -04:00
|
|
|
(if lob-info
|
|
|
|
(setq type 'lob)
|
|
|
|
(while (not (setq type (org-babel-ref-at-ref-p)))
|
|
|
|
(forward-line 1)
|
|
|
|
(beginning-of-line)
|
|
|
|
(if (or (= (point) (point-min)) (= (point) (point-max)))
|
|
|
|
(error "reference not found"))))
|
2009-05-09 12:34:55 -04:00
|
|
|
(case type
|
2009-06-13 20:07:16 -04:00
|
|
|
('results-line (org-babel-ref-read-result))
|
|
|
|
('table (org-babel-ref-read-table))
|
2009-05-09 12:34:55 -04:00
|
|
|
('source-block
|
2009-06-09 18:28:59 -04:00
|
|
|
(setq result (org-babel-execute-src-block
|
2009-06-12 19:50:53 -04:00
|
|
|
t nil (org-combine-plists args nil)))
|
2009-06-14 18:01:55 -04:00
|
|
|
(if (symbolp result) (format "%S" result) result))
|
|
|
|
('lob (setq result (org-babel-execute-src-block t lob-info args)))))))
|
2009-03-23 20:01:11 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(defun org-babel-ref-at-ref-p ()
|
2009-04-02 20:58:04 -04:00
|
|
|
"Return the type of reference located at point or nil of none
|
|
|
|
of the supported reference types are found. Supported reference
|
|
|
|
types are tables and source blocks."
|
|
|
|
(cond ((org-at-table-p) 'table)
|
2009-06-13 20:07:16 -04:00
|
|
|
((looking-at "^#\\+BEGIN_SRC") 'source-block)
|
|
|
|
((looking-at "^#\\+RESNAME:") 'results-line)))
|
|
|
|
|
|
|
|
(defun org-babel-ref-read-result ()
|
|
|
|
"Read the result at `point' into emacs-lisp."
|
|
|
|
(cond
|
|
|
|
((org-at-table-p) (org-babel-ref-read-table))
|
|
|
|
((looking-at ": ")
|
|
|
|
(let ((result-string
|
|
|
|
(org-babel-trim
|
|
|
|
(mapconcat (lambda (line) (if (and (> (length line) 1)
|
|
|
|
(string= ": " (substring line 0 2)))
|
|
|
|
(substring line 2)
|
|
|
|
line))
|
|
|
|
(split-string
|
|
|
|
(buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
|
|
|
|
"\n"))))
|
|
|
|
(or (org-babel-number-p result-string) result-string)))
|
|
|
|
((looking-at "^#\\+RESNAME:")
|
|
|
|
(save-excursion (forward-line 1) (org-babel-ref-read-result)))))
|
|
|
|
|
|
|
|
(defun org-babel-ref-read-table ()
|
|
|
|
"Read the table at `point' into emacs-lisp."
|
|
|
|
(mapcar (lambda (row)
|
|
|
|
(if (and (symbolp row) (equal row 'hline)) row
|
|
|
|
(mapcar #'org-babel-read row)))
|
|
|
|
(org-table-to-lisp)))
|
2009-04-02 20:58:04 -04:00
|
|
|
|
2009-05-24 16:38:01 -04:00
|
|
|
(provide 'org-babel-ref)
|
|
|
|
;;; org-babel-ref.el ends here
|