2009-01-28

drawing pictures

Even though we sometimes use words and sentences etc., many things are better expressed using pictures. Especially in technical discussions it's often useful to convey thoughts through various kinds of boxes and lines between them. For this, emacs has picture-mode. It's a bit hard to textually describe how to draw pictures in it, but a lot of it can be explained with the following little matrix (think of the directions on a compass).
NW
C-c `
N
C-c <UP>
NE
C-c '
W
C-c <LEFT>
  E
C-c <RIGHT>
SW
C-c /
S
C-c <DOWN>
SE
C-c \
The key pressed determines your typing direction; ie. type C-c ' and the some '/'-characters will get you a line to the upper-right (north-east).

This will be only available after you activate picture-mode, (M-x picture-mode). Now, because picture-mode is often used in combination with another (say, you want to draw a nice little ascii-art diagram in an e-mail), you can activate picture mode, do your drawing action, and then go back to the previous mode with C-c C-c. There are some more interesting commands; please see the emacs reference manual for details.

ditaa

Now, after you've written all these fine ascii-art diagrams, you might want to make them a bit more flashy for inclusing in another document. For that, there is Ditaa, which I learned from my dear colleague Stefan. Ditaa is a GPL Java program, that takes an ascii-art diagram, and turns it into something flashy. See below for an example; we take some simple ascii art, and ditaa turns it into the nice picture on the right:
                        
                              +----------+
          Drawing with        |cYEL   {s}|
            ditaa             +          +
                              | emacs fu |
                              +----------+
                                  ^  
                                  |  
    +--------+   +-------+    +-------+    
    |cGRE    |-->+ ditaa +--> |       |    
    |  Text  |   +-------+    |diagram|  
    |Document|   | magic!|    |       |    
    |     {d}|   |cBLU   |    | cRED  |    
    +---+----+   +-------+    +-------+     
        ^     
        |
    +--------+
    |cPNK{io}| 
    |  user  |
    +--------+
Ditaa is a bit slow, but the results are quite nice. You could run ditaa from within emacs with something like:
(setq ditaa-cmd "java -jar <path-to-ditaa>ditaa0_6b.jar")
(defun djcb-ditaa-generate ()
  (interactive)
  (shell-command
    (concat ditaa-cmd " " buffer-file-name)))
after which you can generate those pictures with M-x djcb-ditaa-generate. This is rather primitive of course. For example, it should be possible to integrate this with DocView (screencast), which is to be part of emacs 23. But this is left as an exercise to the reader...

artist-mode

There's another mode for emacs, called artist-mode, which is better suited when you want to express your emotions through art. It must be said, however, that it's often quicker to create some figures in artist-mode than in picture-mode; that's not really a problem, as you can switch between the two. There's a nice screen cast of someone using artist-mode to draw a linked-list in a code comment.

For artist-mode, the most important thing to remember is Shift + middle-click on your mouse to change the tool you are using. And see the picture, which is my interpretation of naïve art. My lack of talent, however, cannot be blaimed on artist-mode, I'm afraid...

5 comments:

Anonymous said...

My emacs gives the artist-mode menu with just plain middle-click - I think the shift might be redundant.

vinhdizzo said...

don't know if this is the right post to comment on, but how when using this with org-mode, how do we get around the "C-c '"? These bindings de/activate the separate buffer for the chunk, and it is used for the NW direction. did u run into this?

djcb said...

@vinhdizzo: you can of course change the keybinding, or edit in another buffer.

but yeah, the C-c prefix is used in many places (esp. org-mode), conflicts are to be expected.

Anonymous said...

Using artist-mode in org-mode with sections collapsed causes a problem. It looks like artist-mode is matching the line you click on to a relative line (presumably to the top on the screen) in the buffer. Which counts the hidden lines too.

Arne Babenhauserheide said...

for using artist-mode in org-mode, just select some part, then use narrow-to-region, enable artist-mode, draw, then switch back to org-mode and use widen.

(imagine M-x in front of most commands :) )