Q: how do I get
yasnippet to expand a text string correctly (recognizing snippet fields) from inside elisp code?
A very simple snippet is a just a string of characters with some control characters intermingled. We can also use elisp forms to create richer snippets, but it's not clear to me how to expand them appropriately.
Here's a simple example that captures the problem. I'm creating an
org-mode snippet to insert a link skeleton, which looks like
[[source][description]]. The following simple snippet produces the desired behavior of "start in the source field, end up in the description field":
We can intermingle elisp code in snippets (wrapped in `s). I would have thought that the following would produce exactly the same behavior as the simpler version, because the
if will simply evaluate to
[[$1][$0]] as a string:
`(if t "[[$1][$0]]")`
It does not do so, however. Instead, it acts as if the
$1 fields are not fields, ends the snippet, and places point after the final
]. So: how does one get the desired behavior when an elisp form evaluates to a text string with
yasnippet fields embedded in that string?
EDIT: as requested, here's the particular use-case that inspired the question. If the X clipboard contains a url link, I'd like it to populate the source field in the org link ([[source][description]]) and leave me in the destination field. It uses a helper function (
dan-xclipboard-link-p) which returns the url if it exists, or else
nil. It seems to me that I should be able to do something like this:
`(let ((link (dan-xclipboard-link-p))) (if link (concat "[[" link "][$0]]") "[[$1][$0]]"))`
In fact, with an earlier version of yasnippet, a version of this code worked as I intended it.
EDIT 2: Interestingly enough, this snippet works when there is a link on the X clipboard, but fails when not (ie, it doesn't recognize
$1 as a field:
[[`(or (dan-xclipboard-link-p) "$1")`][$0]]