Tuesday, May 16, 2017

Re: Retrieve

2017-05-16 14:44 GMT+03:00 Ni Va <nivaemail@gmail.com>:
> Le mardi 16 mai 2017 12:27:34 UTC+2, ZyX a écrit :
>> 2017-05-16 11:38 GMT+03:00 Ni Va <nivaemail@gmail.com>:
>> > Hi,
>> >
>> > How to retrieve the result of this expression in a vimscript var
>> >
>> > let myvar = executable(prevline.',.s/PATTERN//gn')
>> >
>> > this seems to don't work.
>> >
>> > thank you!
>>
>> I do not understand what "expression" you mean, but `executable` is
>> for checking whether there is some executable e.g. for use in
>> `system()`. Given that you are trying to feed it a VimL command I
>> think you at least wanted `execute()`.
>>
>> >
>> > --
>> > --
>> > 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 vim_use+unsubscribe@googlegroups.com.
>> > For more options, visit https://groups.google.com/d/optout.
>
> Assuming this expre : prevline.',.s/PATTERN//gn' returns the number of occurences of pattern between prevline and currentline, I just want to store the result in a var.

The expression `prevline.',.s/PATTERN//gn'` returns a string which may
look like a VimL command (depends on what is in `prevline`). It is not
returning any numbers, it returns a string. If you execute this string
as a VimL *command* (commands are not expressions, what is an
expression is described at `:h expr`) it will print a number of
occurrences of PATTERN and number of lines where they occurred.
`execute` and `redir` are capable of capturing what command prints (it
is not a *result*, it is one of the side effects, commands do not
return anything).

You may do what you asked with `execute()`, but I would not suggest
actually doing this: most of what built-in commands print is affected
by the locale and the output you are seeking for definitely is. You
need to either create a wrapper which calls `language messages C` and
restores the locale afterwards, or use different technique to get the
numbers:

```VimL
let d = {'total_count': 0, 'prevline': 0, 'line_count': 0}
let pattern = 'PATTERN'
let expr = 'extend(d, {"prevline": line("."), "total_count":
d.total_count + 1, "line_count": d.line_count + (line(".") !=
d.prevline)})'
execute 'silent' printf('%s,s/%s/\=[%s, ""][-1]/en%s', prevline,
pattern, expr, repeat('g', !&gdefault))

let myvar = printf('%u matches on %u lines', d.total_count, d.line_count)
```

Note that you also have not cared about `&gdefault` option. And my
variant does not error out if there are zero matches (remove `e` from
flags if you need this).


>
> Thanks
>
> --
> --
> 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 vim_use+unsubscribe@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

--
--
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 vim_use+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment