litorgy-ref.el 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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, or 'previous' to
  35. ;; grab the previous table, or 'next' to grab the
  36. ;; next table
  37. ;;
  38. ;; So an example of a simple src block referencing table data in the
  39. ;; same file would be
  40. ;;
  41. ;; #+var: table previous
  42. ;; #+begin_src emacs-lisp
  43. ;; (message table)
  44. ;; #+end_src
  45. ;;
  46. ;;; Code:
  47. (require 'litorgy)
  48. (defun litorgy-ref-variables (params)
  49. "Takes a parameter alist, and return an alist of variable
  50. names, and the emacs-lisp representation of the related value."
  51. (mapcar #'litorgy-ref-parse
  52. (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
  53. (defun litorgy-ref-literal (ref)
  54. "Determine if the right side of a header argument variable
  55. assignment is a literal value or is a reference to some external
  56. resource. If REF is literal then return it's value, otherwise
  57. return nil."
  58. (let ((out (string-to-number ref)))
  59. (if (or (not (equal out 0)) (string= ref "0") (string= ref "0.0")) out ;; number
  60. (if (string-match "\"\\(.+\\)\"" ref) (read ref) ;; string
  61. nil)))) ;; reference
  62. (defun litorgy-ref-parse (reference)
  63. "Parse the assignment REFERENCE of a variable specified in a
  64. header argument. If the assignment has a literal value return
  65. that value, otherwise interpret REFERENCE as a reference to an
  66. external resource returning a list with two elements. The first
  67. element of the list will be the name of the variable, and the
  68. second will be an emacs-lisp representation of the value of the
  69. variable."
  70. (save-excursion
  71. (if (string-match "\\(.+\\)=\\(.+\\)" reference)
  72. (let* ((var (match-string 1 reference))
  73. (ref (match-string 2 reference))
  74. (lit (litorgy-ref-literal ref))
  75. direction type)
  76. (when (string-match "\\(.+\\):\\(.+\\)" reference)
  77. (find-file (match-string 1 reference))
  78. (setf ref (match-string 2 reference)))
  79. (cons (intern var)
  80. (or lit
  81. (progn
  82. (cond ;; follow the reference in the current file
  83. ((string= ref "previous") (setq direction -1))
  84. ((string= ref "next") (setq direction 1))
  85. (t
  86. (goto-char (point-min))
  87. (setq direction 1)
  88. (unless (let ((regexp (concat "^#\\+\\(TBL\\|SRC\\)NAME:[ \t]*"
  89. (regexp-quote ref) "[ \t]*$")))
  90. (or (re-search-forward regexp nil t)
  91. (re-search-backward regexp nil t)))
  92. ;; ;; TODO: allow searching for names in other buffers
  93. ;; (setq id-loc (org-id-find ref 'marker)
  94. ;; buffer (marker-buffer id-loc)
  95. ;; loc (marker-position id-loc))
  96. ;; (move-marker id-loc nil)
  97. (error (format "reference '%s' not found in this buffer" ref)))))
  98. (while (not (setq type (litorgy-ref-at-ref-p)))
  99. (forward-line direction)
  100. (beginning-of-line)
  101. (if (or (= (point) (point-min)) (= (point) (point-max)))
  102. (error "reference not found")))
  103. (case type
  104. ('table
  105. (mapcar (lambda (row)
  106. (mapcar #'litorgy-read row))
  107. (org-table-to-lisp)))
  108. ('source-block
  109. (litorgy-execute-src-block t))))))))))
  110. (defun litorgy-ref-at-ref-p ()
  111. "Return the type of reference located at point or nil of none
  112. of the supported reference types are found. Supported reference
  113. types are tables and source blocks."
  114. (cond ((org-at-table-p) 'table)
  115. ((looking-at "^#\\+BEGIN_SRC") 'source-block)))
  116. (provide 'litorgy-ref)
  117. ;;; litorgy-ref.el ends here