To quickly add simple diagrams to notes, I've created yasnippets for ditaa boxes. The text inside each box is a $x - and therefore has a length I do not know at snippet design time. As a consequence, the ditaa boxes are not aligned the way they should be to allow ditaa to render a correct diagram.
Like this:
/-------------------------------\
| Text |
| |
| |
\-------------------------------/
Possible solutions might be
- To go into overwrite mode, just for the input of the $x's inside the boxes.
- To use a (non-existent - to the best of my knowledge) (ditaa-)rectangle align macro
- To use an elisp function in the snippet that kills one character for each character that is typed.
Below an example of an implementation of option 3:
# -*- mode: snippet overwrite -*-
# name: ditaa2
# key: ditaa2
# --
#+name ${1:unique-name}
#+begin_src ditaa :file diagram.png
/-------------------------------\ /-------------------------------\
| c147 | | c603 |
+-------------------------------+ +-------------------------------+
| $2 ${2:$(delete-forward-char (string-width yas-text))} +------->+ $3 |
| | | |
| | | |
\-------------------------------/ \-------------------------------/
#+end_src
${1:$(make-string (string-width yas-text) ?\=)}
This kills one space for character 1, but two spaces for character two, and three for the third character (instead of 1, 1 and 1).
(Note in the example my failed attempt to set an overwrite
minor mode and a simple example with ='s at the bottom of the snippet - straight from the yasnippet manual.)
I thought that perhaps everytime yas-text
was updated, the mirror receives a new string-width and therefore the subsequent delete was increasingly large. But using (delete-forward-char 1)
just kills 1 character and then stops - even after continued editing of $2.
How to remove exactly as much spaces as the input is long?
Or, more broadly, how to align the ditaa rectangle after the string length is known?