Browse Source

org-babel: foreign-file references work, export is fully functional

1) source blocks can now reference source-blocks in other files using
   the filename:sourcename syntax

2) on export all references are now made to explicitly point to the
   original buffer using the filename:sourcename syntax.  This ensures
   that all references are correctly resolved on export, even when the
   referenced source block has already been processed.
Eric Schulte 15 years ago
parent
commit
62c8d0a835

+ 11 - 2
contrib/babel/lisp/org-babel-exp.el

@@ -94,8 +94,17 @@ options and are taken from `org-babel-defualt-inline-header-args'."
             (if (string-match "\n$" body) "" "\n"))))
 
 (defun org-babel-exp-results (body lang params &optional inline)
-  (let ((raw (org-babel-execute-src-block
-              nil (list lang body params) '(("results" . "silent")))))
+  (let* ((params
+          ;; lets ensure that we lookup references in the original file
+          (mapcar (lambda (pair)
+                    (if (and (eq (car pair) :var)
+                             (string-match org-babel-ref-split-regexp (cdr pair)))
+                        `(:var . ,(concat (match-string 1 (cdr pair))
+                                          "=" org-current-export-file
+                                          ":" (match-string 2 (cdr pair))))
+                      pair)) params))
+         (raw (org-babel-execute-src-block
+               nil (list lang body params) '(("results" . "silent")))))
     (if (and (stringp raw) (= 0 (length raw)))
         "=(no results)=" (format "=%S=" raw))))
 

+ 9 - 6
contrib/babel/lisp/org-babel-ref.el

@@ -62,6 +62,9 @@ names, and the emacs-lisp representation of the related value."
 	(other-params (assq-delete-all :var params)))
     (mapcar (lambda (assignment) (org-babel-ref-parse assignment other-params)) assignments)))
 
+(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]*")
+
 (defun org-babel-ref-parse (assignment params)
   "Parse a variable ASSIGNMENT in a header argument.  If the
 right hand side of the assignment has a literal value return that
@@ -70,8 +73,7 @@ and find it's value using `org-babel-ref-resolve-reference'.
 Return a list with two elements.  The first element of the list
 will be the name of the variable, and the second will be an
 emacs-lisp representation of the value of the variable."
-  (if (string-match
-       "[ \f\t\n\r\v]*\\(.+?\\)[ \f\t\n\r\v]*=[ \f\t\n\r\v]*\\(.+\\)[ \f\t\n\r\v]*" assignment)
+  (if (string-match org-babel-ref-split-regexp assignment)
       (let ((var (match-string 1 assignment))
             (ref (match-string 2 assignment)))
         (cons (intern var)
@@ -93,7 +95,7 @@ return nil."
   "Resolve the reference and return its value"
   (save-excursion
     (let ((case-fold-search t)
-          type args new-refere new-referent result lob-info)
+          type args new-refere new-referent result lob-info split-file split-ref)
       ;; assign any arguments to pass to source block
       (when (string-match "^\\(.+?\\)\(\\(.*\\)\)$" ref)
         (setq new-refere (match-string 1 ref))
@@ -105,9 +107,10 @@ return nil."
                                  (org-babel-ref-split-args new-referent))))
           ;; (message "args=%S" args) ;; debugging
           (setq ref new-refere)))
-      (when (string-match "\\(.+\\):\\(.+\\)" ref)
-        (find-file (match-string 1 ref))
-        (setf ref (match-string 2 ref)))
+      (when (string-match "^\\(.+\\):\\(.+\\)$" ref)
+        (setq split-file (match-string 1 ref))
+        (setq split-ref (match-string 2 ref))
+        (find-file split-file) (setq ref split-ref))
       (goto-char (point-min))
       (if (let ((result_regexp (concat "^#\\+\\(TBL\\|RES\\)NAME:[ \t]*"
                                        (regexp-quote ref) "[ \t]*$"))

+ 1 - 0
contrib/babel/lisp/org-babel.el

@@ -181,6 +181,7 @@ the header arguments specified at the source code block."
          (cmd (intern (concat "org-babel-execute:" lang)))
          result)
     ;; (message "params=%S" params) ;; debugging statement
+    ;; (message "vars=%S" (second processed-params)) ;; debugging statement
     (unless (member lang org-babel-interpreters)
       (error "Language is not in `org-babel-interpreters': %s" lang))
     (when arg (setq result-params (cons "silent" result-params)))