litorgy-reference.el 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. ;;; litorgy-reference.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-reference-variables (params)
  49. "Takes a parameter alist, and return an alist of variable
  50. names, and the string representation of the related value."
  51. (mapcar #'litorgy-reference-parse
  52. (delq nil (mapcar (lambda (pair) (if (eq (car pair) :var) (cdr pair))) params))))
  53. (defun litorgy-reference-parse (reference)
  54. "Parse a reference to an external resource returning a list
  55. with two elements. The first element of the list will be the
  56. name of the variable, and the second will be an emacs-lisp
  57. representation of the value of the variable."
  58. (save-excursion
  59. (if (string-match "\\(.+\\)=\\(.+\\)" reference)
  60. (let ((var (match-string 1 reference))
  61. (ref (match-string 2 reference))
  62. direction)
  63. (when (string-match "\\(.+\\):\\(.+\\)" reference)
  64. (find-file (match-string 1 reference))
  65. (setf ref (match-string 2 reference)))
  66. (cons var (progn
  67. (cond ;; follow the reference in the current file
  68. ((string= ref "previous") (setq direction -1))
  69. ((string= ref "next") (setq direction 1))
  70. (t
  71. (goto-char (point-min))
  72. (setq direction 1)
  73. (unless (re-search-forward
  74. (concat "^#\\+TBLNAME:[ \t]*" (regexp-quote ref) "[ \t]*$") nil t)
  75. (setq id-loc (org-id-find name-or-id 'marker)
  76. buffer (marker-buffer id-loc)
  77. loc (marker-position id-loc))
  78. (move-marker id-loc nil))))
  79. (while (not (org-at-table-p))
  80. (forward-line direction)
  81. (if (or (= (point) (point-min)) (= (point) (point-max)))
  82. (error "no table found")))
  83. (org-table-to-lisp)))))))
  84. (provide 'litorgy-reference)
  85. ;;; litorgy-reference.el ends here