Best way to programmatically define a function

18 views
Skip to first unread message

lith

unread,
Apr 26, 2015, 1:42:45 PM4/26/15
to vim...@googlegroups.com
Hi!

What's the best way to create a function that creates functions?

I currently construct a string with the vim code, put that into a register, and then evaluate the register. See https://github.com/tomtom/tcomment_vim/blob/master/plugin/tcomment.vim#L165 for an example.

This works well but seems to cause serious problems in certain situations with certain installations. See here for an example report: https://github.com/tomtom/tcomment_vim/issues/140

Is this a known issue with certain versions of gvim?

Is there an alternative way to achieve this that doesn't cause the problem reported above?

Regards,
Tom

Nikolay Pavlov

unread,
Apr 26, 2015, 2:33:18 PM4/26/15
to vim...@googlegroups.com
2015-04-26 20:42 GMT+03:00 lith <mini...@gmail.com>:
> Hi!
>
> What's the best way to create a function that creates functions?
>
> I currently construct a string with the vim code, put that into a register, and then evaluate the register. See https://github.com/tomtom/tcomment_vim/blob/master/plugin/tcomment.vim#L165 for an example.

It is very strange to do this via a register. Just use `execute join(fn, "\n")`.

Also note that `let @t=t` does *not* restore register correctly. E.g. try

call setreg('a', ['a', 'b', 'c'], 'b1')
echo getregtype('a') " Echoes ^V1
let @a=@a
echo getregtype('a') " Echoes v: register type is determined
automatically at `let @a` and is never block

. Also

let reg = ["a\n", "b"]
call setreg('a', reg)
let @a=@a
echo reg ==# getreg('a', 1, 1) " Echoes 0: NUL cannot be restored
via `let @a`

.

>
> This works well but seems to cause serious problems in certain situations with certain installations. See here for an example report: https://github.com/tomtom/tcomment_vim/issues/140
>
> Is this a known issue with certain versions of gvim?
>
> Is there an alternative way to achieve this that doesn't cause the problem reported above?
>
> Regards,
> Tom
>
> --
> --
> 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+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

lith

unread,
Apr 26, 2015, 2:51:42 PM4/26/15
to vim...@googlegroups.com
> It is very strange to do this via a register. Just use `execute join(fn, "\n")`.

Hm ... I'm pretty sure the last time I tried the more obvious solution it didn't work for defining functions. Thanks.

Thanks for pointing out the problem with the register.
Reply all
Reply to author
Forward
0 new messages