How to insert conditionnal text

144 views
Skip to first unread message

Paul Pichaureau

unread,
Jul 22, 2012, 5:42:24 AM7/22/12
to smart-...@googlegroups.com
Hi,

I'm a new user of yasnippet, and I really enjoy it! It helps me *a lot* !

I use a lot of command on LaTeX with key syntax. I would like to make some part of snippet disappear if the I don't enter a string. For example

test<Tab>

will produce

\test[title={_}]

(the underscore is the cursor)

Now if I type <Tab>, the resulting text in the buffer should be

\test 

But if i type 'doo', I would like to see

\test[title={doo}]

Do you know if it's possible ?

Thank you very much !

--
  Paul Pichaureau

OMF

unread,
Jul 22, 2012, 12:55:25 PM7/22/12
to smart-...@googlegroups.com
I managed to solve this problem by using embedded elisp. For example, here is a latex mode sqrt snippet that will include left and right square brackets if the field $1 is non empty

# -*- mode: snippet -*-
# key: rt
# group: math
# name : \sqrt[n]{x} or \sqrt{x}  if n is the null string
# --
\sqrt${1:$(if (string-equal "" yas/text) "" "[")}${1:}${1:$(if (string-equal "" yas/text) "" "]")}{$2}$0

However, there is a problem with calling this recursively, but that is for another thread.

João Távora

unread,
Jul 22, 2012, 2:33:27 PM7/22/12
to smart-...@googlegroups.com
This is the answer I was going to recommend, but I believe you can
replace the (string-equal "" yas/text) call with just a call to the
(yas/text) function, which returns nil if yas/text is the empty
string.

I'll test the recursive case when I find the time. Meanwhile maybe you
could open an issue at
https://github.com/capitaomorte/yasnippet/issues.

João
> --
> You received this message because you are subscribed to the Google Groups
> "smart-snippet and YASnippet" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/smart-snippet/-/rL_MZIEVR_sJ.
>
> To post to this group, send email to smart-...@googlegroups.com.
> To unsubscribe from this group, send email to
> smart-snippe...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/smart-snippet?hl=en.



--
João Távora

OMF

unread,
Jul 22, 2012, 4:05:26 PM7/22/12
to smart-...@googlegroups.com
Sorry, but I am not signed up to github at the moment.

After some checking, I believe it is the case that yas does not in fact execute any lisp code at all while in a nested snippet. But I cannot confirm this with certainty.

Paul Pichaureau

unread,
Jul 23, 2012, 3:57:22 AM7/23/12
to smart-...@googlegroups.com
It works fine! Thank you!

This is the shortest version:

# -*- mode: snippet -*-
# key: rt
# group: math
# name : \sqrt[n]{x} or \sqrt{x}  if n is the null string
# --
\sqrt${1:$(if (yas/text) "[" "")}${1:}${1:$(if (yas/text) "]" "")}{$2}$0


 I've polished a little the snippet, since I prefer to have some description. This is my result:

# -*- mode: snippet -*-
# key: rt
# group: math
# name : \sqrt[n]{x} or \sqrt{x}  if n is the null string
# --
\sqrt${1:$(if (string-equal "<power>" yas/text) "" "[")}${1:<power>}${1:$(if (string-equal "<power>" yas/text) "" "]")}{$2}$0

Thank you very much, it will help me a lot !

Paul Pichaureau

unread,
Jul 23, 2012, 4:57:27 AM7/23/12
to smart-...@googlegroups.com

 I've polished a little the snippet, since I prefer to have some description. This is my result:

# -*- mode: snippet -*-
# key: rt
# group: math
# name : \sqrt[n]{x} or \sqrt{x}  if n is the null string
# --
\sqrt${1:$(if (string-equal "<power>" yas/text) "" "[")}${1:<power>}${1:$(if (string-equal "<power>" yas/text) "" "]")}{$2}$0

Unfortunately, this doesn't work, as I can't have an empty argument here (if i press <del>, the yas/text is empty but the [] are typeset..)

Not very important, but if you have an advice, it will be welcomed !

Dror Atariah

unread,
Jul 25, 2012, 1:25:26 AM7/25/12
to smart-...@googlegroups.com
Very nice idea!

I'm trying out this code:
# -*- mode: snippet -*-
# key: sqrt
# group: Math
# name: Sq. Root
# --
\sqrt${1:$(if (yas/text) "[" "")}${1:}${1:$(if (yas/text) "]" "")}{$2}$0

But it doesn't work. If I enter the text into the optional "[_]" field then everything is fine. Otherwise, if I hit: "sqrt [TAB] [TAB]" in order to leave the optional field empty, the result is: \sqrt{} and the courser is located on the "s". What am I doing wrong?
 

Anders Eurenius

unread,
Jul 25, 2012, 4:15:18 AM7/25/12
to smart-...@googlegroups.com
I tried this by making a new snippet for text mode, reload-all:ed and
tried it, but it just ate the sqrt word, without any error message or
anything. (Is this a known bug?) Then I tried changing it around some,
and it started working.. Lastly, I noticed I was back to the exact
same original snippet, so no, it wasn't anything in the snippet.

This feels like an annoyingly common problem. Is there any way to make
yasnippet more talkative?

aes

---
I'm on the debian stable GNU Emacs 23.2.1 (x86_64-pc-linux-gnu, GTK+
Version 2.20.1) and 0.7.0 (d3fc5cd) of yasnippet.
> --
> You received this message because you are subscribed to the Google Groups
> "smart-snippet and YASnippet" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/smart-snippet/-/hvgSf1VIUzgJ.
>
> To post to this group, send email to smart-...@googlegroups.com.
> To unsubscribe from this group, send email to
> smart-snippe...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages