emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Multiple noweb-ref
@ 2023-02-09 10:01 Théo Maxime Tyburn
  2023-02-20 14:23 ` Ihor Radchenko
  0 siblings, 1 reply; 4+ messages in thread
From: Théo Maxime Tyburn @ 2023-02-09 10:01 UTC (permalink / raw)
  To: emacs-orgmode

Hi,

I have several code blocks that I label using `:noweb-ref label`. I set
them for all blocks under a subtree by setting the org property
`header-args+` of the root of the subtree to `:noweb-ref label`.

Now with this approach, I can't accumulate `noweb-ref` like I could do for
tags. I would like all blocks under a subtree A to have the
reference "foo" and all the blocks under a subtree B of subtree A to
have the reference "bar", while also having the reference "foo".

What I would like to have, when I expand `<<bar>>` is to get all the blocks
under A, and when I expand `<<foo>>` I get all the blocks under B. For
now when I expand `<<foo>>` I only get the blocks under A that are not under B.

Is there a way to do this with the current features of org-babel?

Anyway I tried to hack my way trough it. It seems there are two things
to do :
1) Enable noweb-ref to contain multiple references.
2) Accumulate references when using header-args+ or use tags to set the
value of noweb-ref

I came up with a quick patch for 1):
modified   lisp/ob-core.el
@@ -2910,8 +2910,11 @@ block but are passed literally to the \"example-block\"."
 			(if (org-in-commented-heading-p)
 			    (org-forward-heading-same-level nil t)
 			  (let* ((info (org-babel-get-src-block-info t))
-				 (ref (cdr (assq :noweb-ref (nth 2 info)))))
-			    (push info (gethash ref cache))))))
+				 (refs (cdr (assq :noweb-ref (nth 2 info)))))
+                            (if refs
+                                (dolist (ref (s-split "+" refs))
+			          (push info (gethash ref cache)))
+                              (push info (gethash refs cache)))))))
 		     (funcall expand-references id cache)))))
 	     ;; Interpose PREFIX between every line.
 	     (mapconcat #'identity

With this I already get interesting results. A noweb-ref "foo+bar" gets
expanded by <<foo>> by and <<bar>>. For example if I define references like
this

#+BEGIN_SRC emacs-lisp :noweb-ref one
aaa
#+END_SRC
#+BEGIN_SRC emacs-lisp :noweb-ref two
bbb
#+END_SRC
#+BEGIN_SRC emacs-lisp :noweb-ref one+two
ababab
#+END_SRC

and tangle them like this

#+BEGIN_SRC emacs-lisp :tangle one.el :noweb yes
<<one>>
#+END_SRC
#+BEGIN_SRC emacs-lisp :tangle two.el :noweb yes
<<two>>
#+END_SRC

I get two files
one.el:
aaa
ababab

and two.el:
bbb
ababab

This is already nice!
Feedback on the code is of course very welcome. Not sure if using a plus
sign as a delimiter is clever. Also not sure if using `s-split` is a
good idea, what would be the builtin alternative?

For 2) I didn't check in detail how one could achieve this. I have the
impression it would be easier to use tags. One could define a
new variable `org-babel-set-noweb-refs-from-tags` that would be used in
`org-babel-get-src-block-info` to generate the value of noweb-ref we
would like to have depending on the tags of the headline of the
block. I'll try this soonish.

Best,

Théo


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2023-03-03 16:49 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-09 10:01 Multiple noweb-ref Théo Maxime Tyburn
2023-02-20 14:23 ` Ihor Radchenko
2023-02-26 18:51   ` Théo Maxime Tyburn
2023-03-03 16:49     ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).