Zwillingssterns Weltenwald
Published on Zwillingssterns Weltenwald (http://www.xn--drachentrnen-ocb.de)

Startseite > Minimal example for literate programming with noweb in emacs org-mode

Minimal example for literate programming with noweb in emacs org-mode

If you want to use the literate programming features in emacs org-mode, you can try this minimal example to get started: Activate org-babel-tangle [1], then put this into the file noweb-test.org:

Minimal example for noweb in org-mode

* Assign 

First we assign abc:

#+begin_src python :noweb-ref assign_abc
abc = "abc"
#+end_src

* Use

Then we use it in a function:

#+begin_src python :noweb tangle :tangle noweb-test.py
def x():
  <<assign_abc>>
  return abc

print(x())
#+end_src

noweb-test.org [2]

Hit C-c C-c to evaluate the source block. Hit C-c C-v C-t to put the expanded code into the file noweb-test.py.

The exported code looks like this:

def x():
  abc = "abc"
  return abc
print(x())

noweb-test.py [3]

(html generated with org-export-as-html-to-buffer and slightly reniced to escape the additional parsing I have on my site)

And with org-export-as-pdf we get this:

org-mode-noweb-example [4]

noweb-test.pdf [4]

Add :results output to the #+begin_src line of the second block to see the print results under that block when you hit C-c C-c in the block.

You can also use properties of headlines for giving the noweb-ref. Org-mode can then even concatenate several source blocks into one noweb reference. Just hit C-c C-x p to set a property (or use M-x org-set-property), then set noweb-ref to the name you want to use to embed all blocks under this heading together.

Note: org-babel prefixes each line of an included code-block with the prefix used for the reference (here <<assign_abc>>). This way you can easily include blocks inside python [5] functions.

Note: To keep noweb-references literally in the output or similar, have a look at the different options to :noweb [6].

Note: To do this with shell-code, it’s useful to change the noweb markers to {{{ and }}}, because << and >> are valid shell-syntax, so they disturb the highlighting in sh-mode. Also confirming the evaluation every time makes plain exporting problematic. To fix this, just add the following somewhere in the file (to keep this simple, just add it to the end):

# Local Variables:
# org-babel-noweb-wrap-start: "{{{"
# org-babel-noweb-wrap-end: "}}}"
# org-confirm-babel-evaluate: nil
# org-export-allow-bind-keywords: t
# End:

Have fun with Emacs [7] and org-mode [8]!

AnhangGröße
noweb-test.pdf [4]81.69 KB
noweb-test.org [2]290 Bytes
noweb-test.py.txt [3]49 Bytes
noweb-test-pdf.png [9]6.05 KB
Werke von Arne Babenhauserheide. Lizensiert, wo nichts anderes steht, unter der GPLv3 or later und weiteren freien Lizenzen.

Diese Seite nutzt Cookies. Und Bilder. Manchmal auch Text. Eins davon muss ich wohl erwähnen — sagen die meisten anderen, und ich habe grade keine Zeit, Rechtstexte dazu zu lesen…


Source URL: http://www.xn--drachentrnen-ocb.de/light/english/minimal-example-for-literate-programming-noweb-emacs-org-mode

Links:
[1] http://orgmode.org/worg/org-contrib/babel/intro.html#literate-programming
[2] http://www.xn--drachentrnen-ocb.de/files/noweb-test.org
[3] http://www.xn--drachentrnen-ocb.de/files/noweb-test.py.txt
[4] http://www.xn--drachentrnen-ocb.de/files/noweb-test.pdf
[5] http://python.org
[6] http://orgmode.org/manual/noweb.html
[7] http://gnu.org/software/emacs
[8] http://orgmode.org
[9] http://www.xn--drachentrnen-ocb.de/files/noweb-test-pdf.png