UP | HOME

Support via Liberapay

Vala Source Code Blocks in Org Mode

Org Mode support for Vala

Introduction

Vala is a programming language for the GObject system (think Glib and GTK) that is converted to C source code and then compiled.

You can write non-interactive programs in Vala and capture their output (stdout) in the orgmode document.

You could even write interactive programs that start a GUI when you evaluate them, but this would be somewhat awkward because your emacs session will freeze while the program is running.

Requirements and Setup

Installation and configuration of Vala software

You need to install a Vala compiler and accompanying libraries as well as a C compiler on your system (see Installing Vala).

Emacs configuration

Apart from at least version 9.1 of Org, no other Emacs packages are required.

Optional packages of interest might be vala-mode.el (a major editing mode for Vala code; alternative link) or flycheck-vala.el (Flycheck integration).

Org-mode configuration (org-babel-do-load-languages)

The ob-vala.el file is part of Emacs. To activate Vala as a Babel language, simply add (vala . t) to the org-babel-do-load-languages function in your Emacs configuration file, as shown below:

(org-babel-do-load-languages
 'org-babel-load-languages (quote ((emacs-lisp . t)
                                   (vala . t)
                                   (perl . t))))

Babel expects a Vala compiler named valac in your path, but you can change this by editing the defcustom org-babel-vala-compiler.

Org Mode Features for Vala Source Code Blocks

Header Arguments

Vala-specific header arguments

Vala language blocks support two special header arguments:

  • :flags passes arguments to the compiler
  • :cmdline passes commandline arguments to the generated executable

Sessions

As Vala is a compiled language, there is no support for sessions.

Result Types

Which result types are supported?

There is no special result type handling. The standard output of the evaluated code block is parsed by the default Babel rules.

A single number should become a number, a single string a string. Multiple values are parsed as a list. Multiple lines of multiple values become a table (see Commandline parameters example).

Other

Differences from other supported languages

There currently is no support for the :var header. If you need to pass parameters to your Vala code, you can only use :flags or :cmdline (see Vala-specific header arguments).

Examples of Use

Hello World!

class Demo.HelloWorld : GLib.Object {
    public static int main(string[] args) {
        stdout.printf("Hello World!\n");
        return 0;
    }
}

Multiple classes

Multiple classes can be defined in a single code block:

class HelloWorld : GLib.Object {
    public static void greet() {
        stdout.printf("Hello World!\n");
    }
}

class Demo : GLib.Object {
    public static int main(string[] args) {
        HelloWorld.greet();
        return 0;
    }
}

Conditional compilation

The header argument :flags -D FOO passes the flags -D FOO to the Vala compiler so the #if FOO compilation option is used:

  class Demo.HelloWorld : GLib.Object {
      public static int main(string[] args) {
#if FOO
          stdout.printf("Foo\n");
#else
          stdout.printf("No foo\n");
#endif
          return 0;
      }
  }

GTK example

using Gtk;

public class HelloWorldWindow : Window {

    public HelloWorldWindow () {
        var label = new Label ("Hello World");
        add (label);
        set_default_size (100, 100);
    }
}

void main (string[] args) {
    Gtk.init (ref args);

    var win = new HelloWorldWindow ();
    win.destroy.connect (Gtk.main_quit);
    win.show_all ();

    Gtk.main ();
}

The header argument :flags passes --pkg gtk+-3.0 to the Vala compiler to include the GTK3 library. It needs to be installed (eg. package libgtk-3-dev on Ubuntu >= 11.04).

This example must be executed under a graphical environment.

The output is ignored (:results silent).

Commandline parameters

class Demo.HelloWorld : GLib.Object {
    public static int main(string[] args) {
        // skip args[0] as it is the binary name
        for (int i=1; i < args.length; i++) {
            stdout.printf("argv[%d] = %s\n" , i, args[i]);
        }
        return 0;
    }
}

The header argument :cmdline passes commandline arguments to the Vala program upon execution.

Not that the automatic conversion of the result data recognizes this output as a table. You can change this by using the header argument :results verbatim.

Links to tutorials and other resources

See Vala Documentation for the API documentation, tutorials, sample code and other resources.

Documentation from the 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.