]> code.delx.au - gnu-emacs/blobdiff - lisp/org/ob-ref.el
Spelling fixes.
[gnu-emacs] / lisp / org / ob-ref.el
index 7b06e90f924cf04615edc12ca395c2875bb5de8e..d0a2c14bee930f78376af9ec9c6358d1c1987b48 100644 (file)
@@ -2,10 +2,11 @@
 
 ;; Copyright (C) 2009-2011  Free Software Foundation, Inc.
 
-;; Author: Eric Schulte, Dan Davison
+;; Author: Eric Schulte
+;;     Dan Davison
 ;; Keywords: literate programming, reproducible research
 ;; Homepage: http://orgmode.org
-;; Version: 7.4
+;; Version: 7.7
 
 ;; This file is part of GNU Emacs.
 
 ;;; Code:
 (require 'ob)
 (eval-when-compile
-  (require 'org-list)
   (require 'cl))
 
 (declare-function org-remove-if-not "org" (predicate seq))
 (declare-function org-at-table-p "org" (&optional table-type))
 (declare-function org-count "org" (CL-ITEM CL-SEQ))
-(declare-function org-in-item-p "org-list" ())
+(declare-function org-at-item-p "org-list" ())
+(declare-function org-narrow-to-subtree "org" ())
+(declare-function org-id-find-id-in-file "org-id" (id file &optional markerp))
+(declare-function org-show-context "org" (&optional key))
 
 (defvar org-babel-ref-split-regexp
   "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*")
@@ -77,18 +80,46 @@ the variable."
       (cons (intern var)
            (let ((out (org-babel-read ref)))
              (if (equal out ref)
-                 (if (string-match "^\".+\"$" ref)
+                 (if (string-match "^\".*\"$" ref)
                      (read ref)
                    (org-babel-ref-resolve ref))
                out))))))
 
+(defun org-babel-ref-goto-headline-id (id)
+  (goto-char (point-min))
+  (let ((rx (regexp-quote id)))
+    (or (re-search-forward
+        (concat "^[ \t]*:CUSTOM_ID:[ \t]+" rx "[ \t]*$") nil t)
+       (let* ((file (org-id-find-id-file id))
+              (m (when file (org-id-find-id-in-file id file 'marker))))
+         (when (and file m)
+           (message "file:%S" file)
+           (switch-to-buffer (marker-buffer m))
+           (goto-char m)
+           (move-marker m nil)
+           (org-show-context)
+           t)))))
+
+(defun org-babel-ref-headline-body ()
+  (save-restriction
+    (org-narrow-to-subtree)
+    (buffer-substring
+     (save-excursion (goto-char (point-min))
+                    (forward-line 1)
+                    (when (looking-at "[ \t]*:PROPERTIES:")
+                      (re-search-forward ":END:" nil)
+                      (forward-char))
+                    (point))
+     (point-max))))
+
 (defvar org-babel-library-of-babel)
 (defun org-babel-ref-resolve (ref)
   "Resolve the reference REF and return its value."
+  (save-window-excursion
   (save-excursion
     (let ((case-fold-search t)
           type args new-refere new-header-args new-referent result
-         lob-info split-file split-ref index index-row index-col)
+         lob-info split-file split-ref index index-row index-col id)
       ;; if ref is indexed grab the indices -- beware nested indices
       (when (and (string-match "\\[\\([^\\[]+\\)\\]$" ref)
                 (let ((str (substring ref 0 (match-beginning 0))))
@@ -106,8 +137,8 @@ the variable."
            (setq args (mapcar (lambda (ref) (cons :var ref))
                               (org-babel-ref-split-args new-referent))))
          (when (> (length new-header-args) 0)
-           (setq args (append (org-babel-parse-header-arguments new-header-args)
-                              args)))
+           (setq args (append (org-babel-parse-header-arguments
+                               new-header-args) args)))
           (setq ref new-refere)))
       (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
         (setq split-file (match-string 1 ref))
@@ -116,48 +147,52 @@ the variable."
       (save-restriction
        (widen)
        (goto-char (point-min))
-       (if (let ((result_regexp (concat "^[ \t]*#\\+\\(TBLNAME\\|RESNAME"
-                                        "\\|RESULTS\\):[ \t]*"
-                                        (regexp-quote ref) "[ \t]*$"))
-                 (regexp (concat org-babel-src-name-regexp
-                                 (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
+       (if (let* ((rx (regexp-quote ref))
+                  (res-rx (concat org-babel-result-regexp rx "[ \t]*$"))
+                  (src-rx (concat org-babel-src-name-regexp
+                                  rx "\\(\(.*\)\\)?" "[ \t]*$")))
              ;; goto ref in the current buffer
              (or (and (not args)
-                      (or (re-search-forward result_regexp nil t)
-                          (re-search-backward result_regexp nil t)))
-                 (re-search-forward regexp nil t)
-                 (re-search-backward regexp nil t)
+                      (or (re-search-forward res-rx nil t)
+                          (re-search-backward res-rx nil t)))
+                 (re-search-forward src-rx nil t)
+                 (re-search-backward src-rx nil t)
+                 ;; check for local or global headlines by id
+                 (setq id (org-babel-ref-goto-headline-id ref))
                  ;; check the Library of Babel
                  (setq lob-info (cdr (assoc (intern ref)
                                             org-babel-library-of-babel)))))
-           (unless lob-info (goto-char (match-beginning 0)))
+           (unless (or lob-info id) (goto-char (match-beginning 0)))
          ;; ;; TODO: allow searching for names in other buffers
          ;; (setq id-loc (org-id-find ref 'marker)
          ;;       buffer (marker-buffer id-loc)
          ;;       loc (marker-position id-loc))
          ;; (move-marker id-loc nil)
          (error "reference '%s' not found in this buffer" ref))
-       (if lob-info
-           (setq type 'lob)
-         (while (not (setq type (org-babel-ref-at-ref-p)))
-           (forward-line 1)
-           (beginning-of-line)
-           (if (or (= (point) (point-min)) (= (point) (point-max)))
-               (error "reference not found"))))
+       (cond
+        (lob-info (setq type 'lob))
+        (id (setq type 'id))
+        (t (while (not (setq type (org-babel-ref-at-ref-p)))
+             (forward-line 1)
+             (beginning-of-line)
+             (if (or (= (point) (point-min)) (= (point) (point-max)))
+                 (error "reference not found")))))
        (let ((params (append args '((:results . "silent")))))
          (setq result
                (case type
-                 ('results-line (org-babel-read-result))
-                 ('table (org-babel-read-table))
-                 ('list (org-babel-read-list))
-                 ('file (org-babel-read-link))
-                 ('source-block (org-babel-execute-src-block nil nil params))
-                 ('lob (org-babel-execute-src-block nil lob-info params)))))
+                 (results-line (org-babel-read-result))
+                 (table        (org-babel-read-table))
+                 (list         (org-babel-read-list))
+                 (file         (org-babel-read-link))
+                 (source-block (org-babel-execute-src-block nil nil params))
+                 (lob          (org-babel-execute-src-block
+                                nil lob-info params))
+                 (id           (org-babel-ref-headline-body)))))
        (if (symbolp result)
            (format "%S" result)
          (if (and index (listp result))
              (org-babel-ref-index-list index result)
-           result))))))
+           result)))))))
 
 (defun org-babel-ref-index-list (index lis)
   "Return the subset of LIS indexed by INDEX.
@@ -181,7 +216,10 @@ to \"0:-1\"."
                (open (ls) (if (and (listp ls) (= (length ls) 1)) (car ls) ls)))
           (open
            (mapcar
-            (lambda (sub-lis) (org-babel-ref-index-list remainder sub-lis))
+            (lambda (sub-lis)
+             (if (listp sub-lis)
+                 (org-babel-ref-index-list remainder sub-lis)
+               sub-lis))
             (if (or (= 0 (length portion)) (string-match ind-re portion))
                 (mapcar
                 (lambda (n) (nth n lis))
@@ -205,7 +243,7 @@ to \"0:-1\"."
       (cond
        ((string= holder ",")
         (when (= depth 0)
-          (setq return (reverse (cons (substring buffer 0 -1) return)))
+          (setq return (cons (substring buffer 0 -1) return))
           (setq buffer "")))
        ((or (string= holder "(") (string= holder "[")) (setq depth (+ depth 1)))
        ((or (string= holder ")") (string= holder "]")) (setq depth (- depth 1)))))
@@ -217,7 +255,7 @@ to \"0:-1\"."
 Return nil if none of the supported reference types are found.
 Supported reference types are tables and source blocks."
   (cond ((org-at-table-p) 'table)
-       ((org-in-item-p) 'list)
+       ((org-at-item-p) 'list)
         ((looking-at "^[ \t]*#\\+BEGIN_SRC") 'source-block)
         ((looking-at org-bracket-link-regexp) 'file)
         ((looking-at org-babel-result-regexp) 'results-line)))
@@ -225,4 +263,5 @@ Supported reference types are tables and source blocks."
 (provide 'ob-ref)
 
 
+
 ;;; ob-ref.el ends here