4

I have a yasnippet that is conflicting with multiple-cursors because it is not exiting immediately. When multiple-cursors is not active, I would like the snippet to pause and permit me to enter the data. Can I have an example, please, of a yasnippet that uses some elisp conditions:

For example,

(if mc-mode
  \hspace*{$0}
  \hspace*{$1})

3 Answers 3

4

Yasnippet will accept elisp (including, but limited to conditions) provided that it is surrounded by backticks. In the following example, $0 will cause the snippet to automatically exit when mc-mode is t; whereas, $1 will cause a pause at that location for user input when mc-mode is nil.

`(if mc-mode
    "\\\\hspace*{$0}"
    "\\\\hspace*{$1}")`
4

You can use the #condition directive to provide an elisp predicate. The snippet will only be expanded when the condition code you provide evaluates to non-nil. So, you could simply add this to your snippet:

# condition: (mc-mode)
2
  • I interpreted, perhaps incorrectly (?), the term expand to mean that yasnippet does what it is supposed to do. And, that the #condition is designed to prevent yasnippet from functioning if the condition is satisfied. Does the #condition affect the whole kitten-kaboodle (i.e., nothing at all should happen), or just whether to pause at position $1 to wait for user input?
    – lawlist
    Dec 18, 2014 at 5:55
  • If the condition returns nil, the snippet will not be eligible for expansion. So, when #condition is nil, yas-expand behaves as if the snippet didn't exist. What it does instead of snippet expansion(assuming no other snippet is a match either) depends on the fallback behaviour.
    – Pradhan
    Dec 18, 2014 at 6:02
1

Another condition example that is simple enough but might be pretty handy in many projects with clear and stable structure.

# -*- mode: snippet -*-
# name: controller snippet foo
# condition: (cl-search "app/controllers/" buffer-file-name)
A snippet that activates only when:
(1) you're in corresponding major-mode
(2) your buffer file name is within "app/controllers/" path.

This should be pretty handy in case one has lots of snippets and want to exclude some of the based on the file directories they're called from.

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.