From ef931fcd3a52dc73de641859e17b35d27340891b Mon Sep 17 00:00:00 2001 From: Eric Schulte Date: Mon, 7 Jun 2010 17:38:35 -0700 Subject: [PATCH] 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 --- contrib/babel/lisp/org-babel-ref.el | 84 +++++++++++++++-------------- contrib/babel/lisp/org-babel.el | 33 ++++++------ 2 files changed, 61 insertions(+), 56 deletions(-) diff --git a/contrib/babel/lisp/org-babel-ref.el b/contrib/babel/lisp/org-babel-ref.el index 1d1c75839..3c7cf5cee 100644 --- a/contrib/babel/lisp/org-babel-ref.el +++ b/contrib/babel/lisp/org-babel-ref.el @@ -120,47 +120,49 @@ return nil." (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 "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*" - (regexp-quote ref) "[ \t]*$")) - (regexp (concat org-babel-source-name-regexp - (regexp-quote ref) "\\(\(.*\)\\)?" "[ \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) - ;; 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))) - ;; ;; 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) - (progn (message (format "reference '%s' not found in this buffer" ref)) - (error (format "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")))) - (setq params (org-babel-merge-params params args '((:results . "silent")))) - (setq result - (case type - ('results-line (org-babel-read-result)) - ('table (org-babel-read-table)) - ('file (org-babel-read-file)) - ('source-block (org-babel-execute-src-block nil nil params)) - ('lob (org-babel-execute-src-block nil lob-info params)))) - (if (symbolp result) - (format "%S" result) - (if (and index (listp result)) - (org-babel-ref-index-list index result) - result))))) + (save-restriction + (widen) + (goto-char (point-min)) + (if (let ((result_regexp (concat "^#\\+\\(TBLNAME\\|RESNAME\\|RESULTS\\):[ \t]*" + (regexp-quote ref) "[ \t]*$")) + (regexp (concat org-babel-source-name-regexp + (regexp-quote ref) "\\(\(.*\)\\)?" "[ \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) + ;; 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))) + ;; ;; 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) + (progn (message (format "reference '%s' not found in this buffer" ref)) + (error (format "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")))) + (setq params (org-babel-merge-params params args '((:results . "silent")))) + (setq result + (case type + ('results-line (org-babel-read-result)) + ('table (org-babel-read-table)) + ('file (org-babel-read-file)) + ('source-block (org-babel-execute-src-block nil nil params)) + ('lob (org-babel-execute-src-block nil lob-info params)))) + (if (symbolp result) + (format "%S" result) + (if (and index (listp result)) + (org-babel-ref-index-list index result) + result)))))) (defun org-babel-ref-index-list (index lis) "Return the subset of LIS indexed by INDEX. If INDEX is diff --git a/contrib/babel/lisp/org-babel.el b/contrib/babel/lisp/org-babel.el index 30005d5fe..530a77cf9 100644 --- a/contrib/babel/lisp/org-babel.el +++ b/contrib/babel/lisp/org-babel.el @@ -1232,21 +1232,24 @@ block but are passed literally to the \"example-block\"." (let ((raw (org-babel-ref-resolve-reference source-name nil))) (if (stringp raw) raw (format "%S" raw))) - (let ((point (org-babel-find-named-block source-name))) - (if point - (save-excursion - (goto-char point) - (org-babel-trim - (org-babel-expand-noweb-references - (org-babel-get-src-block-info)))) - ;; optionally raise an error if named - ;; source-block doesn't exist - (if (member lang org-babel-noweb-error-langs) - (error - (concat "<<%s>> could not be resolved " - "(see `org-babel-noweb-error-langs')") - source-name) - "")))) "[\n\r]") (concat "\n" prefix))))) + (save-restriction + (widen) + (let ((point (org-babel-find-named-block source-name))) + (if point + (save-excursion + (goto-char point) + (org-babel-trim + (org-babel-expand-noweb-references + (org-babel-get-src-block-info)))) + ;; optionally raise an error if named + ;; source-block doesn't exist + (if (member lang org-babel-noweb-error-langs) + (error + (concat "<<%s>> could not be resolved " + "(see `org-babel-noweb-error-langs')") + source-name) + ""))))) + "[\n\r]") (concat "\n" prefix))))) (nb-add (buffer-substring index (point-max))))) new-body))