ob-ref.el 9.6 KB

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