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.
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.
On Sun, Jul 22, 2012 at 5:55 PM, OMF <obsessivemathsfr...@gmail.com> wrote:
> 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.
> On Sunday, July 22, 2012 10:42:24 AM UTC+1, Paul Pichaureau wrote:
>> 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
> To post to this group, send email to smart-snippet@googlegroups.com.
> To unsubscribe from this group, send email to
> smart-snippet+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/smart-snippet?hl=en.
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.
# -*- 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 !
Le dimanche 22 juillet 2012 20:33:27 UTC+2, Joao a écrit :
> 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.
> On Sun, Jul 22, 2012 at 5:55 PM, OMF <obsessivemathsfr...@gmail.com> > wrote: > > 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.
> > On Sunday, July 22, 2012 10:42:24 AM UTC+1, Paul Pichaureau wrote:
> >> 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
> > To post to this group, send email to smart-snippet@googlegroups.com. > > To unsubscribe from this group, send email to > > smart-snippet+unsubscribe@googlegroups.com. > > For more options, visit this group at > > http://groups.google.com/group/smart-snippet?hl=en.
> Le dimanche 22 juillet 2012 20:33:27 UTC+2, Joao a écrit :
>> 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.
>> On Sun, Jul 22, 2012 at 5:55 PM, OMF <obsessivemathsfr...@gmail.com> >> wrote: >> > 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.
>> > On Sunday, July 22, 2012 10:42:24 AM UTC+1, Paul Pichaureau wrote:
>> >> 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
>> > -- >> > 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-snippet@googlegroups.com. >> > To unsubscribe from this group, send email to >> > smart-snippet+unsubscribe@googlegroups.com. >> > For more options, visit this group at >> > http://groups.google.com/group/smart-snippet?hl=en.
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?
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.
> 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?
> To post to this group, send email to smart-snippet@googlegroups.com.
> To unsubscribe from this group, send email to
> smart-snippet+unsubscribe@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/smart-snippet?hl=en.