3

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                              |
|                               |    
|                               |    
\-------------------------------/    

(Related: https://stackoverflow.com/questions/41352871/how-to-write-text-inside-a-rectangle-in-emacs-artist-mode)

Possible solutions might be

  1. To go into overwrite mode, just for the input of the $x's inside the boxes.
  2. To use a (non-existent - to the best of my knowledge) (ditaa-)rectangle align macro
  3. 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?

  • I think (not sure) this exact scenario is addressed in the Yasnippet manual here joaotavora.github.io/yasnippet/snippet-development.html#sec-3-5 – mankoff Mar 25 '18 at 0:22
  • A related scenario is discussed there, that is where I copied the ${1:$(make-string (string-width yas-text) ?\=)} from. I'd like to just insert one delete-forward-char, instead of one =. – Werner Mar 25 '18 at 9:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.