Problem writing snippet containing Emacs Lisp code

I am trying to use the cool YASnippet function: I am writing fragments containing Emacs Lisp inline code. There is a snippet for the first mode, which surrounds the entered text with "=", until the text, for example,

====

Text

====

Based on this snippet, I decided to modify it a bit (using Elisp) so that it would comment on these three lines depending on the main mode you are in (I thought such a snippet would be useful for organizing the source code), so I wrote following:

 ${1:`(insert comment-start)`} ${2:$(make-string (string-width text) ?\-)} $1 ${2:Text} $1 ${2:$(make-string (string-width text) ?\-)} $0 

This code works relatively well, except for one problem: the indents of these three lines are mixed, depending on the main mode I'm in (for example, in emacslisp-mode, the second and third lines move more to the right than the first line).

I think the source of the problem may have something to do with what happens after the line ${1: in the first line. If I add a character, I have no problem (i.e., all three lines are correctly aligned at the end of the fragment extension). If I add one space after this line, the alignment problem is still ongoing.

So my question is: do you know of any way to rewrite this fragment so that this misalignment does not occur? Do you know what is the source of this behavior?

Greetings

+4
source share
1 answer

From Fragment Writing :

Clear / line indent

The yas / indent-line variable controls indentation. It is tied to the "default auto", which leads to the indentation of your passage in accordance with the mode of the buffer in which it was inserted.

Another variable is yas / also-auto-indent-first-line, when non-nil does just that :-).

To use hard-coded padding in a fragment pattern, set this variable to.

To control the indentation based on each fragment, see also the # expand-env: directive in "Writing Fragments".

For backward compatibility with earlier versions of YASnippet, you can also put $> in your fragment, it will be executed there (indentation according to the mode) for indentation of the line. This only works when yas / indent-line is set to "auto".

 for (${int i = 0}; ${i < 10}; ${++i}) {$> $0$> }$> 
+2
source

Source: https://habr.com/ru/post/1333397/


All Articles