Hello,

"Berry, Charles" <ccbe...@ucsd.edu> writes:

> I guess I was unclear. There are two ways to fix this.
>
> 1) let bind org-babel-current-src-block-location in
> org-babel-expand-noweb-references in the loop that scans for
> noweb-ref'ed src blocks. This fixes the bug, but contradicts the
> docstring for o-b-c-s-b-l, which says it is the location of the
> currently executing src block. Maybe not a big deal, since
> `org-babel-exp-src-block' can export blocks that are not actually
> executed which is another contradiction of the docstring. Maybe change
> the docstring.
>
> 2) rewrite org-babel-params-from-properties to add an optional arg
> `src-block-location' and use it when provided to govern where to look
> up properties. Modify `org-babel-get-src-block-info' accordingly to
> add that arg when calling o-b-p-f-p. This honors the use of
> o-b-c-s-b-l as the location of the executing src block, but inflates
> the code to accommodate just the `noweb-ref' case.
>
> I think `2' is better as it makes clearer where o-b-p-f-p is looking
> for properties when reading the code of org-babel-get-src-block-info.

Since :noweb-ref is the only property that absolutely needs to be
retrieved from definition, another option would be to write a specific
function for that.

It implies some duplicated efforts with `org-babel-get-src-block-info',
but it is faster when the source name doesn't match, which is the most
common case, and avoids all side-effects from
`org-babel-get-src-block-info'.

WDYT?

Regards,

-- 
Nicolas Goaziou
>From ea24f751fd4ec91857d59e2c287754d3d6dc33f1 Mon Sep 17 00:00:00 2001
From: Nicolas Goaziou <m...@nicolasgoaziou.fr>
Date: Tue, 19 Dec 2017 19:55:51 +0100
Subject: [PATCH] ob-core: Correctly find  Noweb reference

* lisp/ob-core.el (org-babel--noweb-reference): New function.
(org-babel-expand-noweb-references): Use new function.
---
 lisp/ob-core.el | 41 +++++++++++++++++++++++++++++++++++++----
 1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index ade39ec67..bc3c255f5 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -2662,6 +2662,36 @@ CONTEXT may be one of :tangle, :export or :eval."
     (cl-some (lambda (v) (member v allowed-values))
 	     (split-string (or (cdr (assq :noweb params)) "")))))
 
+(defun org-babel--noweb-reference (element)
+  "Return Noweb reference for ELEMENT.
+ELEMENT is a source block.  Return value from `:noweb-ref',
+possibly inherited from properties, or nil."
+  (let ((language (org-element-property :language element)))
+    (cdr
+     (or (assq :noweb-ref		;from block itself
+	       (org-babel-parse-header-arguments
+		(mapconcat #'identity
+			   (cons (org-element-property :parameters element)
+				 (org-element-property :header element))
+			   " ")))
+	 (assq :noweb-ref		;from properties
+	       (org-babel-parse-header-arguments
+		(org-trim
+		 (concat
+		  (and language
+		       (org-entry-get (point)
+				      (concat "header-args:" language)
+				      'inherit))
+		  " "
+		  (org-entry-get (point) "header-args" 'inherit)))))
+	 (and language
+	      (let ((lang-headers
+		     (intern (concat "org-babel-default-header-args:"
+				     language))))
+		(and (boundp lang-headers)
+		     (assq :noweb-ref (symbol-value lang-headers)))))
+	 (assq :noweb-ref org-babel-default-header-args)))))
+
 (defun org-babel-expand-noweb-references (&optional info parent-buffer)
   "Expand Noweb references in the body of the current source code block.
 
@@ -2777,11 +2807,14 @@ block but are passed literally to the \"example-block\"."
 			;; those with a matching Noweb reference.
 			(let ((expansion nil))
 			  (org-babel-map-src-blocks nil
-			    (let ((i (org-babel-get-src-block-info 'light)))
+			    (let ((element (org-element-at-point)))
 			      (when (equal source-name
-					   (cdr (assq :noweb-ref (nth 2 i))))
-				(let ((sep (or (cdr (assq :noweb-sep (nth 2 i)))
-					       "\n")))
+					   (org-babel--noweb-reference element))
+				(let* ((i (org-babel-get-src-block-info
+					   'light element))
+				       (sep
+					(or (cdr (assq :noweb-sep (nth 2 i)))
+					    "\n")))
 				  (setq expansion
 					(cons sep
 					      (cons (funcall expand-body i)
-- 
2.15.1

Reply via email to