org-babel-ref.el 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. ;;; org-babel-ref.el --- org-babel functions for referencing external data
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison
  3. ;; Author: Eric Schulte, Dan Davison
  4. ;; Keywords: literate programming, reproducible research
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;;; License:
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;;
  13. ;; This program is distributed in the hope that it will be useful,
  14. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. ;; GNU General Public License for more details.
  17. ;;
  18. ;; You should have received a copy of the GNU General Public License
  19. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  20. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  21. ;; Boston, MA 02110-1301, USA.
  22. ;;; Commentary:
  23. ;; Functions for referencing data from the header arguments of a
  24. ;; org-babel block. The syntax of such a reference should be
  25. ;;
  26. ;; #+VAR: variable-name=file:resource-id
  27. ;;
  28. ;; - variable-name :: the name of the variable to which the value
  29. ;; will be assigned
  30. ;;
  31. ;; - file :: path to the file containing the resource, or omitted if
  32. ;; resource is in the current file
  33. ;;
  34. ;; - resource-id :: the id or name of the resource
  35. ;;
  36. ;; So an example of a simple src block referencing table data in the
  37. ;; same file would be
  38. ;;
  39. ;; #+TBLNAME: sandbox
  40. ;; | 1 | 2 | 3 |
  41. ;; | 4 | org-babel | 6 |
  42. ;;
  43. ;; #+begin_src emacs-lisp :var table=sandbox
  44. ;; (message table)
  45. ;; #+end_src
  46. ;;
  47. ;;; Code:
  48. (require 'org-babel)
  49. (defun org-babel-ref-variables (params)
  50. "Takes a parameter alist, and return an alist of variable
  51. names, and the emacs-lisp representation of the related value."
  52. (let ((assignments
  53. (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params)))
  54. (others
  55. (delq nil (mapcar (lambda (pair) (unless (eq :var (car pair)) pair)) params))))
  56. (mapcar (lambda (assignment) (org-babel-ref-parse assignment)) assignments)))
  57. (defvar org-babel-ref-split-regexp
  58. "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
  59. (defun org-babel-ref-parse (assignment &optional params)
  60. "Parse a variable ASSIGNMENT in a header argument. If the
  61. right hand side of the assignment has a literal value return that
  62. value, otherwise interpret as a reference to an external resource
  63. and find it's value using `org-babel-ref-resolve-reference'.
  64. Return a list with two elements. The first element of the list
  65. will be the name of the variable, and the second will be an
  66. emacs-lisp representation of the value of the variable."
  67. (if (string-match org-babel-ref-split-regexp assignment)
  68. (let ((var (match-string 1 assignment))
  69. (ref (match-string 2 assignment)))
  70. (cons (intern var)
  71. (or (org-babel-ref-literal ref)
  72. (org-babel-ref-resolve-reference ref params))))))
  73. (defun org-babel-ref-literal (ref)
  74. "Determine if the right side of a header argument variable
  75. assignment is a literal value or is a reference to some external
  76. resource. REF should be a string of the right hand side of the
  77. assignment. If REF is literal then return it's value, otherwise
  78. return nil."
  79. (let ((out (org-babel-read ref)))
  80. (if (equal out ref)
  81. (if (string-match "^\".+\"$" ref)
  82. (read ref))
  83. out)))
  84. (defun org-babel-ref-resolve-reference (ref &optional params)
  85. "Resolve the reference and return its value"
  86. (save-excursion
  87. (let ((case-fold-search t)
  88. type args new-refere new-referent result lob-info split-file split-ref
  89. index index-row index-col)
  90. ;; if ref is indexed grab the indices -- beware nested indicies
  91. (when (and (string-match "\\[\\(.+\\)\\]" ref)
  92. (let ((str (substring ref 0 (match-beginning 0))))
  93. (= (count ?( str) (count ?) str))))
  94. (setq index (match-string 1 ref))
  95. (setq ref (substring ref 0 (match-beginning 0))))
  96. ;; assign any arguments to pass to source block
  97. (when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref)
  98. (setq new-refere (match-string 1 ref))
  99. (setq new-referent (match-string 2 ref))
  100. ;; (message "new-refere=%S, new-referent=%S" new-refere new-referent) ;; debugging
  101. (when (> (length new-refere) 0)
  102. (if (> (length new-referent) 0)
  103. (setq args (mapcar (lambda (ref) (cons :var ref))
  104. (org-babel-ref-split-args new-referent))))
  105. ;; (message "args=%S" args) ;; debugging
  106. (setq ref new-refere)))
  107. (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
  108. (setq split-file (match-string 1 ref))
  109. (setq split-ref (match-string 2 ref))
  110. (find-file split-file) (setq ref split-ref))
  111. (goto-char (point-min))
  112. (if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*"
  113. (regexp-quote ref) "[ \t]*$"))
  114. (regexp (concat org-babel-source-name-regexp
  115. (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
  116. ;; goto ref in the current buffer
  117. (or (and (not args)
  118. (or (re-search-forward result_regexp nil t)
  119. (re-search-backward result_regexp nil t)))
  120. (re-search-forward regexp nil t)
  121. (re-search-backward regexp nil t)
  122. ;; check the Library of Babel
  123. (setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
  124. (unless lob-info (goto-char (match-beginning 0)))
  125. ;; ;; TODO: allow searching for names in other buffers
  126. ;; (setq id-loc (org-id-find ref 'marker)
  127. ;; buffer (marker-buffer id-loc)
  128. ;; loc (marker-position id-loc))
  129. ;; (move-marker id-loc nil)
  130. (progn (message (format "reference '%s' not found in this buffer" ref))
  131. (error (format "reference '%s' not found in this buffer" ref))))
  132. (if lob-info
  133. (setq type 'lob)
  134. (while (not (setq type (org-babel-ref-at-ref-p)))
  135. (forward-line 1)
  136. (beginning-of-line)
  137. (if (or (= (point) (point-min)) (= (point) (point-max)))
  138. (error "reference not found"))))
  139. (setq params (org-babel-merge-params params args '((:results . "silent"))))
  140. (setq result
  141. (case type
  142. ('results-line (org-babel-read-result))
  143. ('table (org-babel-read-table))
  144. ('file (org-babel-read-file))
  145. ('source-block (org-babel-execute-src-block nil nil params))
  146. ('lob (org-babel-execute-src-block nil lob-info params))))
  147. (if (symbolp result)
  148. (format "%S" result)
  149. (if (and index (listp result))
  150. (org-babel-ref-index-list index result)
  151. result)))))
  152. (defun org-babel-ref-index-list (index lis)
  153. "Return the subset of LIS indexed by INDEX. If INDEX is
  154. separated by ,s then each PORTION is assumed to index into the
  155. next deepest nesting or dimension. A valid PORTION can consist
  156. of either an integer index, or two integers separated by a : in
  157. which case the entire range is returned."
  158. (if (string-match "^,?\\([^,]+\\)" index)
  159. (let ((length (length lis))
  160. (portion (match-string 1 index))
  161. (remainder (substring index (match-end 0))))
  162. (flet ((wrap (num) (if (< num 0) (+ length num) num))
  163. (open (lis) (if (and (listp lis) (= (length lis) 1)) (car lis) lis)))
  164. (open
  165. (mapcar
  166. (lambda (sub-lis) (org-babel-ref-index-list remainder sub-lis))
  167. (if (string-match "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)"
  168. portion)
  169. (mapcar (lambda (n) (nth n lis))
  170. (apply 'number-sequence
  171. (if (match-string 2 portion)
  172. (list
  173. (wrap (string-to-number (match-string 2 portion)))
  174. (wrap (string-to-number (match-string 3 portion))))
  175. (list (wrap 0) (wrap -1)))))
  176. (list (nth (wrap (string-to-number portion)) lis)))))))
  177. lis))
  178. (defun org-babel-ref-split-args (arg-string)
  179. "Split ARG-STRING into top-level arguments of balanced parenthesis."
  180. (let ((index 0) (depth 0) (buffer "") holder return)
  181. ;; crawl along string, splitting at any ","s which are on the top level
  182. (while (< index (length arg-string))
  183. (setq holder (substring arg-string index (+ 1 index)))
  184. (setq buffer (concat buffer holder))
  185. (setq index (+ 1 index))
  186. (cond
  187. ((string= holder ",")
  188. (when (= depth 0)
  189. (setq return (reverse (cons (substring buffer 0 -1) return)))
  190. (setq buffer "")))
  191. ((or (string= holder "(") (string= holder "[")) (setq depth (+ depth 1)))
  192. ((or (string= holder ")") (string= holder "]")) (setq depth (- depth 1)))))
  193. (mapcar #'org-babel-trim (reverse (cons buffer return)))))
  194. (defun org-babel-ref-at-ref-p ()
  195. "Return the type of reference located at point or nil if none
  196. of the supported reference types are found. Supported reference
  197. types are tables and source blocks."
  198. (cond ((org-at-table-p) 'table)
  199. ((looking-at "^#\\+BEGIN_SRC") 'source-block)
  200. ((looking-at org-bracket-link-regexp) 'file)
  201. ((looking-at org-babel-result-regexp) 'results-line)))
  202. (provide 'org-babel-ref)
  203. ;;; org-babel-ref.el ends here