I'm having an issue where I would like to be able to create a template which will conditionally insert a string into the template based on the value of one of the fields.

This means I want some sort of embedded elisp (or elisp function call) to evaluate if one of my fields has the value 0. If it does not then I want to include a function definition in the snippet. If the field is 0 then there is no reason to include this function definition.

This is what I have so far but it seems to matter what the string is included and the concatenation fails.

$>registerFn(id, &$1FieldEnd, $1FieldOtherEnd,
$>                ${5:$1Handler}, 0);
`(if (string= (yas-field-value 5) "0")
        " ")
concat (concat "\nstatic void " (concat (yas-field-value 5) "(void *argPtr)"))
/* handlerImplementation */

  • You use backquote syntax, but you have no comma in the backquoted sexp. In that case, the backquote just acts like quote. Did you forget a comma, perhaps?
    – Drew
    Aug 8 '20 at 16:24
  • I'm sorry but I'm not familiar with that syntax are you suggesting that the syntax should be as follows ? ``` 'concat (concat "\nstatic void " (concat (yas-field-value 5) "(void *argPtr)")) ``` Aug 8 '20 at 18:05

I managed to solve this by simply putting the following in my doom.d/config.el

(with-eval-after-load 'yasnippet
        (defun maybe_notify_snip (snip_str)
            (if (string= snip_str "0")
                " "
                (concat (concat "\nstatic void " (concat snip_str "(void *argPtr)"))
                 /* handler implementation */

and then using it inside the template as such:

${5:$(maybe_notify_snip yas-text)}

This seems to ensure that the snippet is updated such that if I enter 0 for field 5 then we don't include a function implementation snippet but as soon as I enter something in filed 5 the implementation appears


On a more general note, here's an example that sets b to the value of a if a is not 0.

# -*- mode: snippet -*-
# name: test
# key: test
# --

a = ${1:0}
b = ${1:$(unless (equal yas-text "0") yas-text)}
  • This doesn't really address what I'm after. What I was trying to do was include an entire section in the template conditionally. Aug 11 '20 at 19:31
  • Not specifically. You'd have to wrap the last yas-text with the code you want to use.
    – jagrg
    Aug 12 '20 at 1:34

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.