babel: resolving references outside of the visible portion of narrowed buffers

Thanks to Tom Dye for pointing out the need for this fix

* contrib/babel/lisp/org-babel.el (org-babel-expand-noweb-references):
  now able to find noweb references even outside of the narrowed
  portion of the buffer when the buffer is narrowed

* contrib/babel/lisp/org-babel-ref.el (org-babel-ref-resolve-reference):
  now able to resolve references which are located outside of the
  narrowed portion of the buffer when the buffer is narrowed
This commit is contained in:
Eric Schulte 2010-06-07 17:38:35 -07:00
parent 93ab492464
commit ef931fcd3a
2 changed files with 61 additions and 56 deletions

View File

@ -120,47 +120,49 @@ return nil."
(setq split-file (match-string 1 ref)) (setq split-file (match-string 1 ref))
(setq split-ref (match-string 2 ref)) (setq split-ref (match-string 2 ref))
(find-file split-file) (setq ref split-ref)) (find-file split-file) (setq ref split-ref))
(goto-char (point-min)) (save-restriction
(if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*" (widen)
(regexp-quote ref) "[ \t]*$")) (goto-char (point-min))
(regexp (concat org-babel-source-name-regexp (if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*"
(regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$"))) (regexp-quote ref) "[ \t]*$"))
;; goto ref in the current buffer (regexp (concat org-babel-source-name-regexp
(or (and (not args) (regexp-quote ref) "\\(\(.*\)\\)?" "[ \t]*$")))
(or (re-search-forward result_regexp nil t) ;; goto ref in the current buffer
(re-search-backward result_regexp nil t))) (or (and (not args)
(re-search-forward regexp nil t) (or (re-search-forward result_regexp nil t)
(re-search-backward regexp nil t) (re-search-backward result_regexp nil t)))
;; check the Library of Babel (re-search-forward regexp nil t)
(setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel))))) (re-search-backward regexp nil t)
(unless lob-info (goto-char (match-beginning 0))) ;; check the Library of Babel
;; ;; TODO: allow searching for names in other buffers (setq lob-info (cdr (assoc (intern ref) org-babel-library-of-babel)))))
;; (setq id-loc (org-id-find ref 'marker) (unless lob-info (goto-char (match-beginning 0)))
;; buffer (marker-buffer id-loc) ;; ;; TODO: allow searching for names in other buffers
;; loc (marker-position id-loc)) ;; (setq id-loc (org-id-find ref 'marker)
;; (move-marker id-loc nil) ;; buffer (marker-buffer id-loc)
(progn (message (format "reference '%s' not found in this buffer" ref)) ;; loc (marker-position id-loc))
(error (format "reference '%s' not found in this buffer" ref)))) ;; (move-marker id-loc nil)
(if lob-info (progn (message (format "reference '%s' not found in this buffer" ref))
(setq type 'lob) (error (format "reference '%s' not found in this buffer" ref))))
(while (not (setq type (org-babel-ref-at-ref-p))) (if lob-info
(forward-line 1) (setq type 'lob)
(beginning-of-line) (while (not (setq type (org-babel-ref-at-ref-p)))
(if (or (= (point) (point-min)) (= (point) (point-max))) (forward-line 1)
(error "reference not found")))) (beginning-of-line)
(setq params (org-babel-merge-params params args '((:results . "silent")))) (if (or (= (point) (point-min)) (= (point) (point-max)))
(setq result (error "reference not found"))))
(case type (setq params (org-babel-merge-params params args '((:results . "silent"))))
('results-line (org-babel-read-result)) (setq result
('table (org-babel-read-table)) (case type
('file (org-babel-read-file)) ('results-line (org-babel-read-result))
('source-block (org-babel-execute-src-block nil nil params)) ('table (org-babel-read-table))
('lob (org-babel-execute-src-block nil lob-info params)))) ('file (org-babel-read-file))
(if (symbolp result) ('source-block (org-babel-execute-src-block nil nil params))
(format "%S" result) ('lob (org-babel-execute-src-block nil lob-info params))))
(if (and index (listp result)) (if (symbolp result)
(org-babel-ref-index-list index result) (format "%S" result)
result))))) (if (and index (listp result))
(org-babel-ref-index-list index result)
result))))))
(defun org-babel-ref-index-list (index lis) (defun org-babel-ref-index-list (index lis)
"Return the subset of LIS indexed by INDEX. If INDEX is "Return the subset of LIS indexed by INDEX. If INDEX is

View File

@ -1232,21 +1232,24 @@ block but are passed literally to the \"example-block\"."
(let ((raw (org-babel-ref-resolve-reference (let ((raw (org-babel-ref-resolve-reference
source-name nil))) source-name nil)))
(if (stringp raw) raw (format "%S" raw))) (if (stringp raw) raw (format "%S" raw)))
(let ((point (org-babel-find-named-block source-name))) (save-restriction
(if point (widen)
(save-excursion (let ((point (org-babel-find-named-block source-name)))
(goto-char point) (if point
(org-babel-trim (save-excursion
(org-babel-expand-noweb-references (goto-char point)
(org-babel-get-src-block-info)))) (org-babel-trim
;; optionally raise an error if named (org-babel-expand-noweb-references
;; source-block doesn't exist (org-babel-get-src-block-info))))
(if (member lang org-babel-noweb-error-langs) ;; optionally raise an error if named
(error ;; source-block doesn't exist
(concat "<<%s>> could not be resolved " (if (member lang org-babel-noweb-error-langs)
"(see `org-babel-noweb-error-langs')") (error
source-name) (concat "<<%s>> could not be resolved "
"")))) "[\n\r]") (concat "\n" prefix))))) "(see `org-babel-noweb-error-langs')")
source-name)
"")))))
"[\n\r]") (concat "\n" prefix)))))
(nb-add (buffer-substring index (point-max))))) (nb-add (buffer-substring index (point-max)))))
new-body)) new-body))