Hi,
The attached patch adds support for two new babel header arguments:
=:noweb-prefix= and =:noweb-trans=.
=:noweb-prefix= can be set to =no= to disable the noweb prefix
behaviour, where prefix characters are repeated when expanding a
multiline noweb reference.
=:noweb-trans= can be set to =prin1-to-string= to insert a lisp string
representing the content of the referenced src block.
The goal is to allow one to use, say, a LaTeX src block to represent
some LaTeX snippet to be tangled into a string in some lisp (or other)
code. This isn't possible currently, and one has to manually string
escape the LaTeX code.
As an example, the following two blocks
#+BEGIN_SRC LaTeX :tangle no :noweb-ref nw
\usepackage{…}
\usepackage{…}
#+END_SRC
#+BEGIN_SRC emacs-lisp :noweb yes :tangle yes :noweb-prefix no
:noweb-trans prin1-to-string
(setq latex-header <<nw>>)
#+END_SRC
would tangle to
#+BEGIN_SRC emacs-lisp
(setq latex-header "\\usepackage{…}
\\usepackage{…}")
#+END_SRC
I've left undocumented the possibility of setting =:noweb-trans= to
another function. I wonder if anyone can think of some other use.
Regards,
--
Sébastien Miquel
From 66f271225767d07e12bcc73a1ddbadf038d245fa Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Miquel?= <sebastien.miq...@posteo.eu>
Date: Mon, 6 Sep 2021 18:45:42 +0200
Subject: [PATCH] ob-core.el: Add `:noweb-prefix`, `:noweb-trans` babel header
arguments
* lisp/ob-core.el (org-babel-expand-noweb-references): Add support for
`noweb-prefix' header argument, to not repeat the prefix characters
when expanding a noweb reference. Add support for `noweb-trans' header
argument, to apply a function to the noweb content upon
expansion.
(org-babel-common-header-args-w-values):
(org-babel-safe-header-args): Add `noweb-prefix' and `noweb-trans' values.
* doc/org-manual.org: Document `noweb-prefix' and `noweb-trans' babel header
arguments.
* etc/NEWS: Document `:noweb-prefix' and `:noweb-trans'.
---
doc/org-manual.org | 34 ++++++++++++++++++++++++++++++----
etc/ORG-NEWS | 9 +++++++++
lisp/ob-core.el | 26 ++++++++++++++++++++------
3 files changed, 59 insertions(+), 10 deletions(-)
diff --git a/doc/org-manual.org b/doc/org-manual.org
index b4c20f252..d7b1c4203 100644
--- a/doc/org-manual.org
+++ b/doc/org-manual.org
@@ -18554,10 +18554,11 @@ Note that the expansion now contains the results of the code block
: 100
Noweb insertions honor prefix characters that appear before the noweb
-syntax reference. This behavior is illustrated in the following
-example. Because the =<<example>>= noweb reference appears behind the
-SQL comment syntax, each line of the expanded noweb reference is
-commented. With:
+syntax reference. This behavior can be turned off by setting the
+=noweb-prefix= header argument to =no= and is illustrated in the
+following example. Because the =<<example>>= noweb reference appears
+behind the SQL comment syntax, each line of the expanded noweb
+reference is commented. With:
#+begin_example
,#+NAME: example
@@ -18626,6 +18627,31 @@ else:
print('do things when false')
#+end_example
+The header argument =noweb-trans= can be set to =prin1-to-string= to
+insert a lisp string representing the content of the referenced src
+block. With:
+
+#+begin_example
+,#+NAME: latex-header
+,#+BEGIN_SRC latex
+ \usepackage{amsmath}
+,#+END_SRC
+#+end_example
+
+#+texinfo: @noindent
+this code block:
+
+#+begin_example
+,#+BEGIN_SRC elisp :noweb yes
+ (setq header <<latex-header>>)
+,#+END_SRC
+#+end_example
+
+#+texinfo: @noindent
+expands to:
+
+: (setq header "\\usepackage{amsmath}")
+
When in doubt about the outcome of a source code block expansion, you
can preview the results with the following command:
diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index 335db4139..6fa808645 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -63,6 +63,15 @@ list of various table options (between brackets in LaTeX export),
since certain tabular environments, such as longtblr of the
tabularray LaTeX package, provides this structure.
+*** New =:noweb-prefix= and =:noweb-trans= babel header arguments
+
+=:noweb-prefix= can be set to =no= to prevent the prefix characters
+from being repeated when expanding a multiline noweb reference.
+
+=:noweb-trans= can be set to =prin1-to-string=. Noweb reference
+therein will be expanded to an elisp string representation of their
+content.
+
** New functions and changes in function arguments
*** New function ~org-element-cache-map~ for quick mapping across Org elements
diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 239a57f96..1d5d1bedc 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -411,6 +411,8 @@ then run `org-babel-switch-to-session'."
(noweb . ((yes no tangle no-export strip-export)))
(noweb-ref . :any)
(noweb-sep . :any)
+ (noweb-prefix . ((no yes)))
+ (noweb-trans . ((prin1-to-string)))
(output-dir . :any)
(padline . ((yes no)))
(post . :any)
@@ -436,9 +438,10 @@ specific header arguments as well.")
(defconst org-babel-safe-header-args
'(:cache :colnames :comments :exports :epilogue :hlines :noeval
- :noweb :noweb-ref :noweb-sep :padline :prologue :rownames
- :sep :session :tangle :wrap
+ :noweb :noweb-ref :noweb-sep :noweb-prefix :padline
+ :prologue :rownames :sep :session :tangle :wrap
(:eval . ("never" "query"))
+ (:noweb-trans . ("prin1-to-string"))
(:results . (lambda (str) (not (string-match "file" str)))))
"A list of safe header arguments for babel source blocks.
@@ -2825,6 +2828,12 @@ block but are passed literally to the \"example-block\"."
(lang (nth 0 info))
(body (nth 1 info))
(comment (string= "noweb" (cdr (assq :comments (nth 2 info)))))
+ (noweb-trans (when (cdr (assq :noweb-trans (nth 2 info)))
+ (intern (cdr (assq :noweb-trans (nth 2 info))))))
+ (noweb-prefix (let ((v (assq :noweb-prefix (nth 2 info))))
+ (or (not v)
+ (and (org-not-nil (cdr v))
+ (not (equal (cdr v) "no"))))))
(noweb-re (format "\\(.*?\\)\\(%s\\)"
(with-current-buffer parent-buffer
(org-babel-noweb-wrap))))
@@ -2919,11 +2928,16 @@ block but are passed literally to the \"example-block\"."
(let* ((info (org-babel-get-src-block-info t))
(ref (cdr (assq :noweb-ref (nth 2 info)))))
(push info (gethash ref cache))))))
- (funcall expand-references id cache)))))
+ (funcall expand-references id cache))))
+ (expansion (if (functionp noweb-trans)
+ (funcall noweb-trans expansion)
+ expansion)))
;; Interpose PREFIX between every line.
- (mapconcat #'identity
- (split-string expansion "[\n\r]")
- (concat "\n" prefix))))))
+ (if noweb-prefix
+ (mapconcat #'identity
+ (split-string expansion "[\n\r]")
+ (concat "\n" prefix))
+ expansion)))))
body t t 2)))
(defun org-babel--script-escape-inner (str)
--
2.34.1