The Lazy Programmer

May 14, 2008

D Development with Emacs

Filed under: D,Programming,Windows — ferruccio @ 8:44 pm
Tags: ,

A few months after discovering the D programming language, I finally got around to setting up a reasonable development environment just for writing D programs. After playing around a bit with Visual Studio and a few other Windows-based text editors, I settled on using Emacs to drive my work.

Not that I’ve used Emacs much before. I’ve often had to tweak configuration files on various *nix systems. As a result I’ve gotten to know Emacs, Vi, Joe, Jed, Nano, Pico and occasionally even Ed.

Now, when I say that I “know” these editors, I mean that I can fire them up, change some text, save and exit. Get in. Get out. Nobody gets hurt.

So I was doing some late night surfing a few days ago and I ran across two very serendipitous finds:

A post on Word Aligned titled Accidental Emacs. This is the best Emacs tutorial I’ve ever run across. I read it and immediately installed Emacs to try it out. What really got me hooked, though, was not how much Emacs can do out of the box or how customizable it is. No, what really got me is how discoverable it is. After learning a few basics, it took little effort to figure out how to set up syntax highlighting (font-locking in Emacs-speak) and other customization options.

This lead me to my second discovery: d-mode for Emacs.

Emacs has various modes built-in. Well, they’re not really built-in as much as they are implemented using Emacs’ scripting language: ELisp. There’s Fundamental mode for plain text editing. And for coding there’s c-mode, c++-mode, lisp-mode, ada-mode, perl-mode etc. These various modes control syntax highlighting and code formatting. They probably do a lot more, but that’s all I need for now 🙂

The Setup

The first thing I did was to install the D compiler and tools. After a bit of back-and-forth on whether to use the 1.0 compiler or the newer, shinier 2.0 compiler and whether to stick to the standard libraries or use Tango, I decided to use the 1.0 compiler with Tango. The 2.0 compiler is still in alpha and is not yet supported by Tango. Pick whichever combination suits you best. The instructions and files for downloading Tango (with the 1.0 compiler bundled along with it) are on the Tango website.

The 2.0 compiler is available from the Digital Mars website.

The next step is to install Emacs. If you’re using a Unix or Linux system, Emacs probably came with it or is available through a package manager. On Windows, it takes a little more effort to install, but it’s not too bad. Grab and unzip it to a directory of your choosing. Make sure the bin subdirectory is in your PATH and you’re good to go. You might also want to create a desktop shortcut to runemacs.exe in the bin directory, if you’re so inclined.

Once Emacs is up and running we can install d-mode. Copy the contents of the d-mode announcement and paste then in a file called d-mode.el in the emacs\lisp\progmodes directory. The file should start with the line:

;;; d-mode.el --- D Programming Language mode for (X)Emacs

and end with:

;;; d-mode.el ends here

You need to change one line to make it available as an Emacs mode. Line 350 (approximately) should be changed from:

;;;###autoload (add-to-list 'auto-mode-alist '("\\.d[i]?\\'" . d-mode))


(add-to-list 'auto-mode-alist '("\\.d[i]?\\'" . d-mode))

In other words, delete the ;;;###autoload prefix. Once that’s done we can compile the d-mode.el file (you don’t have to, but it will slow down Emacs start-up if you dont). Open a command prompt window, go to the emacs\lisp\progmodes directory and type:

emacs -batch -no-site-file -q -f batch-byte-compile d-mode.el

This will generate d-mode.elc.

The final step is to tell Emacs to load d-mode.elc on startup. We can do that in the .emacs file. On Windows the .emacs file will be located in Documents and Settings\username\Application Data. Add the following line to .emacs.

(load "d-mode.elc")

And you are done. When you load a .d file into Emacs, it should automatically enter d-mode. I’ll give it a shot right now with a sample program.

C:\tmp> emacs hello.d

and the result is:
hello.d in emacs

Note: You won’t get the same syntax highlighting colors or font by default. I changed the default font from courier to consolas. The default colors are a little over-the-top for my taste, so I changed them to a more minimalist set: blue for keywords, green for comments, red for strings and black for everything else.


1 Comment

  1. I had a very smooth experience with Code::Blocks and D, on Windows.

    Even the debugger works:)

    Comment by Vlad — May 20, 2008 @ 3:54 am

RSS feed for comments on this post.

Blog at

%d bloggers like this: