org-babel-ref.el 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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. (mapcar #'org-babel-ref-parse
  53. (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
  54. (defun org-babel-ref-parse (assignment)
  55. "Parse a variable ASSIGNMENT in a header argument. If the
  56. right hand side of the assignment has a literal value return that
  57. value, otherwise interpret as a reference to an external resource
  58. and find it's value using `org-babel-ref-resolve-reference'.
  59. Return a list with two elements. The first element of the list
  60. will be the name of the variable, and the second will be an
  61. emacs-lisp representation of the value of the variable."
  62. (if (string-match "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment)
  63. (let ((var (match-string 1 assignment))
  64. (ref (match-string 2 assignment)))
  65. (cons (intern var)
  66. (or (org-babel-ref-literal ref)
  67. (org-babel-ref-resolve-reference ref))))))
  68. (defun org-babel-ref-literal (ref)
  69. "Determine if the right side of a header argument variable
  70. assignment is a literal value or is a reference to some external
  71. resource. If REF is literal then return it's value, otherwise
  72. return nil."
  73. (let ((out (org-babel-read ref)))
  74. (if (equal out ref)
  75. (if (string-match "\"\\(.+\\)\"" ref)
  76. (read ref))
  77. out)))
  78. (defun org-babel-ref-resolve-reference (ref)
  79. "Resolve the reference and return it's value"
  80. (save-excursion
  81. (let ((case-fold-search t)
  82. type args new-refere new-referent result lob-info)
  83. ;; assign any arguments to pass to source block
  84. (when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref)
  85. (setq new-refere (match-string 1 ref))
  86. (setq new-referent (match-string 2 ref))
  87. ;; (message (format "first second %S -- %S" new-refere new-referent)) ;; debugging
  88. (when (> (length new-refere) 0)
  89. (if (> (length new-referent) 0)
  90. (setq args (mapcar (lambda (ref) (cons :var ref))
  91. (split-string new-referent ",[ \f\t\n\r\v]*"))))
  92. (setq ref new-refere)))
  93. (when (string-match "\\(.+\\):\\(.+\\)" ref)
  94. (find-file (match-string 1 ref))
  95. (setf ref (match-string 2 ref)))
  96. (goto-char (point-min))
  97. (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
  98. (regexp-quote ref) "[ \t]*$"))
  99. (regexp (concat "^#\\+SRCNAME:[ \t]*"
  100. (regexp-quote ref) "[ \t]*$")))
  101. (or (re-search-forward result_regexp nil t)
  102. (re-search-forward result_regexp nil t)
  103. (re-search-forward regexp nil t)
  104. (re-search-backward regexp nil t)
  105. ;; check the Library of Babel
  106. (setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
  107. (goto-char (match-beginning 0))
  108. ;; ;; TODO: allow searching for names in other buffers
  109. ;; (setq id-loc (org-id-find ref 'marker)
  110. ;; buffer (marker-buffer id-loc)
  111. ;; loc (marker-position id-loc))
  112. ;; (move-marker id-loc nil)
  113. (progn (message (format "reference '%s' not found in this buffer" ref))
  114. (error (format "reference '%s' not found in this buffer" ref))))
  115. (if lob-info
  116. (setq type 'lob)
  117. (while (not (setq type (org-babel-ref-at-ref-p)))
  118. (forward-line 1)
  119. (beginning-of-line)
  120. (if (or (= (point) (point-min)) (= (point) (point-max)))
  121. (error "reference not found"))))
  122. (case type
  123. ('results-line (org-babel-ref-read-result))
  124. ('table (org-babel-ref-read-table))
  125. ('source-block
  126. (setq result (org-babel-execute-src-block
  127. t nil (org-combine-plists args nil)))
  128. (if (symbolp result) (format "%S" result) result))
  129. ('lob (setq result (org-babel-execute-src-block t lob-info args)))))))
  130. (defun org-babel-ref-at-ref-p ()
  131. "Return the type of reference located at point or nil of none
  132. of the supported reference types are found. Supported reference
  133. types are tables and source blocks."
  134. (cond ((org-at-table-p) 'table)
  135. ((looking-at "^#\\+BEGIN_SRC") 'source-block)
  136. ((looking-at "^#\\+RESNAME:") 'results-line)))
  137. (defun org-babel-ref-read-result ()
  138. "Read the result at `point' into emacs-lisp."
  139. (cond
  140. ((org-at-table-p) (org-babel-ref-read-table))
  141. ((looking-at ": ")
  142. (let ((result-string
  143. (org-babel-trim
  144. (mapconcat (lambda (line) (if (and (> (length line) 1)
  145. (string= ": " (substring line 0 2)))
  146. (substring line 2)
  147. line))
  148. (split-string
  149. (buffer-substring (point) (org-babel-result-end)) "[\r\n]+")
  150. "\n"))))
  151. (or (org-babel-number-p result-string) result-string)))
  152. ((looking-at "^#\\+RESNAME:")
  153. (save-excursion (forward-line 1) (org-babel-ref-read-result)))))
  154. (defun org-babel-ref-read-table ()
  155. "Read the table at `point' into emacs-lisp."
  156. (mapcar (lambda (row)
  157. (if (and (symbolp row) (equal row 'hline)) row
  158. (mapcar #'org-babel-read row)))
  159. (org-table-to-lisp)))
  160. (provide 'org-babel-ref)
  161. ;;; org-babel-ref.el ends here