nested expr in a mapping

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

nested expr in a mapping

M Kelly
Hi,

Is it posible to nest conditionals in a mapping, something like, just for demonstration:

vnoremap <expr> vv (strlen(@y) == 1) ? ':let ...' : '("vcl" =~ getregtype("*")) ? 'some foo' : 'some bar' ' <bar> some more ...

Can we do this ?  Do I use ''' (3 single quotes) to embed quotes ?  (and then 5 for a 2nd level embed ...) ... ?

thx as always,
-m

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/da6436ec-0159-4364-8ab0-90e9b075764d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: nested expr in a mapping

M Kelly
Hi,

I suppose I can call a function to do this :-)

-m

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/fc4865e6-2357-4a8c-ae7c-c5fe7796b27a%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: nested expr in a mapping

Gary Johnson-4
In reply to this post by M Kelly
On 2020-02-13, M Kelly wrote:
> Hi,
>
> Is it posible to nest conditionals in a mapping, something like, just for
> demonstration:
>
> vnoremap <expr> vv (strlen(@y) == 1) ? ':let ...' : '("vcl" =~ getregtype("*"))
> ? 'some foo' : 'some bar' ' <bar> some more ...
>
> Can we do this ?

Yes.  See

    :help 41.3
    :help expr1
    :help :map-<expr>
    :help matchup.txt " lines 896-900 for some examples

> Do I use ''' (3 single quotes) to embed quotes ?  (and then 5
> for a 2nd level embed ...) ... ?

No.  Expressions are not nested using quotes.  You may use
parentheses if you wish, or just use the precedence rules.

It would be easier to help you if we knew what you had tried, the
actual results, and what you expected.

Regards,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200214042128.GC9366%40phoenix.
Reply | Threaded
Open this post in threaded view
|

Re: nested expr in a mapping

M Kelly
Hi,

ok, thank you.
I always thought there had to be quotes around each block, as in () ? 'foo' : 'bar'
So I was trying \' and \\' and \" and \\" and extra quotes for each additional nested block to no success.
I will remove all quotes and see if I can get it to work.

take care,
-m

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/16fe93d4-4447-457a-a9f0-1fd0a856745d%40googlegroups.com.
Reply | Threaded
Open this post in threaded view
|

Re: nested expr in a mapping

Gary Johnson-4
On 2020-02-14, M Kelly wrote:
> Hi,
>
> ok, thank you.
> I always thought there had to be quotes around each block, as in () ? 'foo' :
> 'bar'
> So I was trying \' and \\' and \" and \\" and extra quotes for each additional
> nested block to no success.
> I will remove all quotes and see if I can get it to work.

The quotes are necessary if the expression evaluates to a string and
each of the alternatives is a literal string.

Here are some simple examples.

    :let x = 1
    :echo x == 1 ? 73 : 88
    73

    :echo x == 0 ? "hello" : "goodbye"
    goodbye

In the first example, the expression evaluates to a number, while in
the second example the expression evaluates to a string.

Here's an example of nesting.

    :let x = 2
    :echo x == 1 ? "one" : x == 2 ? "two" : "something else"
    two

I deliberately didn't use any parentheses to show that they're often
not necessary, although they can make expressions more clear.

If you can't get it to work, post what you've tried here and we'll
figure it out.

Regards,
Gary

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/vim_use/20200215071733.GD9366%40phoenix.