litorgy-ref.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. ;;; litorgy-ref.el --- litorgical functions for referencing external data
  2. ;; Copyright (C) 2009 Eric Schulte, Dan Davison, Austin F. Frank
  3. ;; Author: Eric Schulte, Dan Davison, Austin F. Frank
  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. ;; litorgical 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 | litorgy | 6 |
  42. ;;
  43. ;; #+begin_src emacs-lisp :var table=sandbox
  44. ;; (message table)
  45. ;; #+end_src
  46. ;;
  47. ;;; Code:
  48. (require 'litorgy)
  49. (defun litorgy-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 #'litorgy-ref-parse
  53. (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
  54. (defun litorgy-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 `litorgy-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 "\\(.+?\\)=\\(.+\\)" assignment)
  63. (let ((var (match-string 1 assignment))
  64. (ref (match-string 2 assignment)))
  65. (cons (intern var)
  66. (or (litorgy-ref-literal ref)
  67. (litorgy-ref-resolve-reference ref))))))
  68. (defun litorgy-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 (string-to-number ref)))
  74. (if (or (not (equal out 0)) (string= ref "0") (string= ref "0.0")) out ;; number
  75. (if (string-match "\"\\(.+\\)\"" ref) (read ref) ;; string
  76. nil)))) ;; reference
  77. (defun litorgy-ref-resolve-reference (ref)
  78. "Resolve the reference and return it's value"
  79. (save-excursion
  80. (let (type args new-ref)
  81. ;; assign any arguments to pass to source block
  82. (when (string-match "\\(.+\\)\(\\(.+\\)\)" ref)
  83. (save-match-data
  84. (setq args (mapcar (lambda (ref) (cons :var ref))
  85. (split-string (match-string 2 ref) ",[ \f\t\n\r\v]*"))))
  86. (setq ref (match-string 1 ref)))
  87. (when (string-match "\\(.+\\):\\(.+\\)" ref)
  88. (find-file (match-string 1 ref))
  89. (setf ref (match-string 2 ref)))
  90. (goto-char (point-min))
  91. (unless (let ((regexp (concat "^#\\+\\(TBL\\|SRC\\)NAME:[ \t]*"
  92. (regexp-quote ref) "[ \t]*$")))
  93. (or (re-search-forward regexp nil t)
  94. (re-search-backward regexp nil t)))
  95. ;; ;; TODO: allow searching for names in other buffers
  96. ;; (setq id-loc (org-id-find ref 'marker)
  97. ;; buffer (marker-buffer id-loc)
  98. ;; loc (marker-position id-loc))
  99. ;; (move-marker id-loc nil)
  100. (error (format "reference '%s' not found in this buffer" ref)))
  101. (while (not (setq type (litorgy-ref-at-ref-p)))
  102. (forward-line 1)
  103. (beginning-of-line)
  104. (if (or (= (point) (point-min)) (= (point) (point-max)))
  105. (error "reference not found")))
  106. (case type
  107. ('table
  108. (mapcar (lambda (row)
  109. (mapcar #'litorgy-read row))
  110. (org-table-to-lisp)))
  111. ('source-block
  112. (litorgy-execute-src-block t nil args))))))
  113. (defun litorgy-ref-at-ref-p ()
  114. "Return the type of reference located at point or nil of none
  115. of the supported reference types are found. Supported reference
  116. types are tables and source blocks."
  117. (cond ((org-at-table-p) 'table)
  118. ((looking-at "^#\\+BEGIN_SRC") 'source-block)))
  119. (provide 'litorgy-ref)
  120. ;;; litorgy-ref.el ends here