Question

I'm trying to write a snippet whose inputs (tab-stop fields) are two numbers, and which returns their sum. But I don't know how to reference the values of both fields at the same time, and it seems like I can't reference the values of the tab-stop within embedded elisp code.

Here is what I tried:

First number: $1
Second number: $2
Sum of two numbers: `(+ (string-to-number $1) (string-to-number $2))`

But when I expand the snippet, the text [yas] elisp error! appears where the sum should go. What am I doing wrong?

Was it helpful?

Solution

joaotavora recently pointed out that this can be done using yas-field-value:

First number: ${1:0}
Second number: ${2:0}
Sum of two numbers: ${2:$(+ (string-to-number (or (yas-field-value 1) "0")) (string-to-number (or yas-text "0")))}

OTHER TIPS

and sorry for the confusion. According to the official snippet writing guide, what you are looking for is called a mirror. Unfortunately, a mirror can only mirror a single variable, so you seem to be out of luck.

This is probably because yasnippet needs to know which mirrors to update when you type a field. (It doesn't want to update them all, because that could be costly), so it needs a way of determining which mirrors are affected by which fields. If it allowed arbitrary substitutions, that would be impossible to determine. (A simple keyword search isn't enough because the variable could be hidden behind metaprogramming).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top