D Source Code Blocks in Org Mode

Table of Contents

Org Mode support for D


D is a C++-like language made by Digital Mars. It features:

  • C++ syntax
  • Built-in garbage collector
  • Strong type system
  • Meta-programming
  • Seamless assembler support
  • Usable as a scripting language
  • C binary compatibility
  • and much more.

The scripting feature of D is leveraged in Org mode. Basically, we call rdmd instead of the dmd compiler. This transparently compiles, caches, and run the script.

D may be used like many other script languages (Perl, Awk, Emacs Lisp, etc.) to process data within Org files, without worrying about memory management, with the help of a strong type system, and enjoying the speed of a compiled language.

Requirements and Setup

  1. The D runtime must be installed on your platform. Look at http://dlang.org/ for downloading and instructions. Eventually, the dmd and rdmd compilers must be in the PATH. A separate d-mode.el package is available, for syntax higlighting. It is not required.
  2. Add ob-D.el in the lisp/org/ source tree. For now, this file is attached at the end of this page.
  3. In the org.el file, find a line which says:
    (const :tag "C" C)

    add a similar line for D:

    (const :tag "D" D)
  4. byte-compile the 2 modified *.el
  5. customize the variable org-babel-load-languages enabling D
    • either through M-x customize-variable

    (you may need a restart of Emacs prior to customization)

    • or in your .emacs

Org Mode Features for D Source Code Blocks

Header Arguments

:results output
(only output presently)
A global D variable named VARIABLE will be declared and initialized with VALUE Possible types for VARIABLE may be:
int, double, string, string[][]

The later type, string[][], is used for variables storing Org tables

:import package
An import statement will be declared in the D source before the source code


There is no support for sessions

Result Types

Presently, only output result is supported :results output


D support is quite similar to C and C++ support.

One difference though, is that in D compilation is cached. Only the first run incures compile time, up to the next change in the source.

Examples of Use

Hello World

Here is Hello World!

This source code block:

#+begin_src D
   import std.stdio;
   writefln ("Hello World!");

yields this result:

Hello World!

Process an Org Mode Table

We take an Org mode table as input, process it, and output a new Org mode table.

This table will be input in the D script, and iterated row by row:

nb sqr noise
0 0 0.23
1 1 1.31
2 4 4.61
3 9 9.05
4 16 16.55
#+begin_src D :results output :var somedata=somedata :var TT="321" :var QQ=3.14
  import std.stdio;
  import std.conv;

  void main()
    foreach (i, row; somedata) {
      writef ("|%2s ", i);
      foreach (cell; row)
        writef ("| %5s | %5s ", cell, 1000*to!real(cell));
      writefln ("| %7s |", to!real(TT)*to!real(QQ));

yields this result:

0 0 0 0 0 0.23 230 1007.94
1 1 1000 1 1000 1.31 1310 1007.94
2 2 2000 4 4000 4.61 4610 1007.94
3 3 3000 9 9000 9.05 9050 1007.94
4 4 4000 16 16000 16.55 16550 1007.94

Source file

C, C++, and D are quite similar languages. Thus, it make sense to merge all supports in a single, easy to maintain, lisp source file, named ob-C.el

Documentation from the http://orgmode.org/worg/ website (either in its HTML format or in its Org format) is licensed under the GNU Free Documentation License version 1.3 or later. The code examples and css stylesheets are licensed under the GNU General Public License v3 or later.