5

I have simple code blocks that I would like to show the noweb generated source for inside the same org-mode file. eg:

#+NAME: example
#+BEGIN_SRC java :classname Test :results code  :exports both :tangle yes :noweb yes

<<preface>>
String result = "hello world";
<<end>>

#+END_SRC

where preface and end are something like this:

#+NAME: teststart
#+BEGIN_SRC java  +n :results none :exports code :tangle yes :noweb yes
public class Test {
  public static void main(String[] args)
  {
#+END_SRC

#+NAME: testend
#+BEGIN_SRC java +n :classname Test :results none :exports code :tangle yes :noweb yes
      System.out.print(result);
    }
  }
#+END_SRC

I can do a file include, eg:

#+INCLUDE: "./Test.java" src java

and see it in exports, but not the org-mode file itself.

E Bro
  • 133
  • 4
  • I don't know of a way to do what you want, but I wanted to note that you can export to org, producing another org file with the expansions in place. Would that do? You would probably want to tangle and export only the first block (the other two are "internal"). – NickD Apr 03 '17 at 01:26
  • When I do as you suggest, it exports the _result_, eg `Hello World`, not the noweb generated code. eg: `#+BEGIN_SRC java :classname Test :results org :exports org :tangle yes :noweb yes` – E Bro Apr 03 '17 at 03:17
  • Have you configured the org exporter? If you do `C-c C-e`, do you get the Org export option? There is not enough space in the comment for the complete file, so I'll add it in an answer. – NickD Apr 03 '17 at 15:47

2 Answers2

3

The function org-babel-expand-noweb-referenes will expand the <<noweb>> entries. You can call this function from another source block (use a :wrap header argument to make source the output is wrapped in an appropriate source block):

#+NAME: expand-noweb
#+BEGIN_SRC elisp :exports none
  (save-excursion
    (org-babel-goto-named-src-block name)
    (org-babel-expand-noweb-references))

#+END_SRC

#+CALL: expand-noweb(name="example") :wrap source javascript
erikstokes
  • 12,686
  • 2
  • 34
  • 56
0

Assuming you have configured the org exporter (C-C C-e O o in the menu), the following file works for me (in the limited manner as explained in my first comment - but as I said, it is not quite what you wanted, but maybe it's good enough):

* Tangling into another org file

#+NAME: example
#+BEGIN_SRC java :classname Test :exports code :tangle yes :noweb yes

<<preface>>
String result = "hello world";
<<postface>>

#+END_SRC

where preface and end are something like this (but will disappear in the export):

#+NAME: preface
#+BEGIN_SRC java  +n :results none :exports none
public class Test {
  public static void main(String[] args)
  {
#+END_SRC

#+NAME: postface
#+BEGIN_SRC java +n :classname Test :results none :exports none
      System.out.print(result);
    }
  }
#+END_SRC

Note the names and the options.

Doing C-c C-e O o exports into test.org.org (where the original file was test.org) and contains just the expanded code:

 # Created 2017-04-03 Mon 11:51
 #+TITLE: 
 #+AUTHOR: Me
 * Tangling into another org file

 #+NAME: example
 #+BEGIN_SRC java

   public class Test {
     public static void main(String[] args)
     {
   String result = "hello world";
       System.out.print(result);
     }
   }
 #+END_SRC

 where preface and end are something like this (but will disappear in the export):
NickD
  • 27,023
  • 3
  • 23
  • 42
  • Cool, thanks. Unfortunately, produces _nothing_ in my org.org export (which basically looks ident to original org file). Must be the configuration of my exporter. Using Spacemacs setup. Also, not quite what I was looking for, but could be hacked to be. – E Bro Apr 03 '17 at 22:54