ob-ref.el 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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: 7.4
  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. (declare-function org-count "org" (CL-ITEM CL-SEQ))
  43. (declare-function org-in-item-p "org-list" ())
  44. (declare-function org-at-item-p "org-list" ())
  45. (defvar org-babel-ref-split-regexp
  46. "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
  47. (defun org-babel-ref-parse (assignment)
  48. "Parse a variable ASSIGNMENT in a header argument.
  49. If the right hand side of the assignment has a literal value
  50. return that value, otherwise interpret as a reference to an
  51. external resource and find it's value using
  52. `org-babel-ref-resolve'. Return a list with two elements. The
  53. first element of the list will be the name of the variable, and
  54. the second will be an emacs-lisp representation of the value of
  55. the variable."
  56. (when (string-match org-babel-ref-split-regexp assignment)
  57. (let ((var (match-string 1 assignment))
  58. (ref (match-string 2 assignment)))
  59. (cons (intern var)
  60. (let ((out (org-babel-read ref)))
  61. (if (equal out ref)
  62. (if (string-match "^\".*\"$" ref)
  63. (read ref)
  64. (org-babel-ref-resolve ref))
  65. out))))))
  66. (defvar org-babel-library-of-babel)
  67. (defun org-babel-ref-resolve (ref)
  68. "Resolve the reference REF and return its value."
  69. (save-window-excursion
  70. (save-excursion
  71. (let ((case-fold-search t)
  72. type args new-refere new-header-args new-referent result
  73. lob-info split-file split-ref index index-row index-col)
  74. ;; if ref is indexed grab the indices -- beware nested indices
  75. (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
  76. (let ((str (substring ref 0 (match-beginning 0))))
  77. (= (org-count ?( str) (org-count ?) str))))
  78. (setq index (match-string 1 ref))
  79. (setq ref (substring ref 0 (match-beginning 0))))
  80. ;; assign any arguments to pass to source block
  81. (when (string-match
  82. "^\\(.+?\\)\\(\\[\\(.*\\)\\]\\|\\(\\)\\)\(\\(.*\\)\)$" ref)
  83. (setq new-refere (match-string 1 ref))
  84. (setq new-header-args (match-string 3 ref))
  85. (setq new-referent (match-string 5 ref))
  86. (when (> (length new-refere) 0)
  87. (when (> (length new-referent) 0)
  88. (setq args (mapcar (lambda (ref) (cons :var ref))
  89. (org-babel-ref-split-args new-referent))))
  90. (when (> (length new-header-args) 0)
  91. (setq args (append (org-babel-parse-header-arguments new-header-args)
  92. args)))
  93. (setq ref new-refere)))
  94. (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
  95. (setq split-file (match-string 1 ref))
  96. (setq split-ref (match-string 2 ref))
  97. (find-file split-file) (setq ref split-ref))
  98. (save-restriction
  99. (widen)
  100. (goto-char (point-min))
  101. (if (let ((result_regexp (concat "^[ \t]*#\\+\\(TBLNAME\\|RESNAME"
  102. "\\|RESULTS\\):[ \t]*"
  103. (regexp-quote ref) "[ \t]*$"))
  104. (regexp (concat org-babel-src-name-regexp
  105. (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
  106. ;; goto ref in the current buffer
  107. (or (and (not args)
  108. (or (re-search-forward result_regexp nil t)
  109. (re-search-backward result_regexp nil t)))
  110. (re-search-forward regexp nil t)
  111. (re-search-backward regexp nil t)
  112. ;; check the Library of Babel
  113. (setq lob-info (cdr (assoc (intern ref)
  114. org-babel-library-of-babel)))))
  115. (unless lob-info (goto-char (match-beginning 0)))
  116. ;; ;; TODO: allow searching for names in other buffers
  117. ;; (setq id-loc (org-id-find ref 'marker)
  118. ;; buffer (marker-buffer id-loc)
  119. ;; loc (marker-position id-loc))
  120. ;; (move-marker id-loc nil)
  121. (error "reference '%s' not found in this buffer" ref))
  122. (if lob-info
  123. (setq type 'lob)
  124. (while (not (setq type (org-babel-ref-at-ref-p)))
  125. (forward-line 1)
  126. (beginning-of-line)
  127. (if (or (= (point) (point-min)) (= (point) (point-max)))
  128. (error "reference not found"))))
  129. (let ((params (append args '((:results . "silent")))))
  130. (setq result
  131. (case type
  132. ('results-line (org-babel-read-result))
  133. ('table (org-babel-read-table))
  134. ('list (org-babel-read-list))
  135. ('file (org-babel-read-link))
  136. ('source-block (org-babel-execute-src-block nil nil params))
  137. ('lob (org-babel-execute-src-block nil lob-info params)))))
  138. (if (symbolp result)
  139. (format "%S" result)
  140. (if (and index (listp result))
  141. (org-babel-ref-index-list index result)
  142. result)))))))
  143. (defun org-babel-ref-index-list (index lis)
  144. "Return the subset of LIS indexed by INDEX.
  145. Indices are 0 based and negative indices count from the end of
  146. LIS, so 0 references the first element of LIS and -1 references
  147. the last. If INDEX is separated by \",\"s then each \"portion\"
  148. is assumed to index into the next deepest nesting or dimension.
  149. A valid \"portion\" can consist of either an integer index, two
  150. integers separated by a \":\" in which case the entire range is
  151. returned, or an empty string or \"*\" both of which are
  152. interpreted to mean the entire range and as such are equivalent
  153. to \"0:-1\"."
  154. (if (and (> (length index) 0) (string-match "^\\([^,]*\\),?" index))
  155. (let ((ind-re "\\(\\([-[:digit:]]+\\):\\([-[:digit:]]+\\)\\|\*\\)")
  156. (length (length lis))
  157. (portion (match-string 1 index))
  158. (remainder (substring index (match-end 0))))
  159. (flet ((wrap (num) (if (< num 0) (+ length num) num))
  160. (open (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls)))
  161. (open
  162. (mapcar
  163. (lambda (sub-lis)
  164. (if (listp sub-lis)
  165. (org-babel-ref-index-list remainder sub-lis)
  166. sub-lis))
  167. (if (or (= 0 (length portion)) (string-match ind-re portion))
  168. (mapcar
  169. (lambda (n) (nth n lis))
  170. (apply 'org-number-sequence
  171. (if (and (> (length portion) 0) (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. (defvar org-bracket-link-regexp)
  195. (defun org-babel-ref-at-ref-p ()
  196. "Return the type of reference located at point.
  197. Return nil if none of the supported reference types are found.
  198. Supported reference types are tables and source blocks."
  199. (cond ((org-at-table-p) 'table)
  200. ((org-at-item-p) 'list)
  201. ((looking-at "^[ \t]*#\\+BEGIN_SRC") 'source-block)
  202. ((looking-at org-bracket-link-regexp) 'file)
  203. ((looking-at org-babel-result-regexp) 'results-line)))
  204. (provide 'ob-ref)
  205. ;; arch-tag: ace4a4f4-ea38-4dac-8fe6-6f52fcc43b6d
  206. ;;; ob-ref.el ends here