Conditional fields

Skip to first unread message

Ross Donaldson

Oct 28, 2014, 5:59:28 PM10/28/14
First: three cheers for yasnippet! This has already saved me a great deal of time. 

So, my question: I've made a snippet for a particular kind of template, written in YAML. This template can take one of two forms, depending on whether it's in folder Foo or folder Bar -- there's a Foo field that takes a single value, and a Bar field that takes a YAML list. Rather than having two nearly-identical templates, I'd like to use a little elisp to parse the folder and correctly add the field. 

The elisp itself is pretty straight-forward:

(let ((curpath (file-name-directory (buffer-file-name))))
  (if (string-match ".+\/Foo\/" curpath)
      "foo-field: "
      "bar-field:\n  -"

What I can't figure out is: how do you structure a tab field so that the *text* of the field will be interpreted by the elisp, but you still get the cursor positioned correctly? I've tried:

`(let ((curpath (file-name-directory (buffer-file-name))))
  (if (string-match ".+\/Foo\/" curpath)
      "foo-field: "
      "bar-field:\n  -"

This correctly splices in the field, but the cursor never moves to $1 -- it goes directly $0. Likewise, making the field strings "foo-field: $1" and "bar-field: $1" just writes out literal $1s. 

Is there a good way to do this with yasnippet?

Reply all
Reply to author
0 new messages