Hi Bram,
On Sun, May 29, 2016 at 10:52 AM, Bram Moolenaar <[email protected]> wrote:
>
> Yegappan Lakshmanan wrote:
>
>> >> To use a script local function as a job callback function, it looks like
>> >> you need to use function('s:<function_name>') to generate a function
>> >> reference. Is it possible to simply use the 's:function_name' string?
>> >
>> > That doesn't work, because the string doesn't know what script the
>> > function is in. Using function() will figure that out.
>>
>> What about expanding the function name if it starts with "s:" or "<SID>"
>> to <SNR><number>_<function name> in the get_callback() function?
>
> That would be inconsistent with other places where a function reference
> can be used.
>
Just in case you decide to change this in the future, a patch is attached.
- Yegappan
>
>> Currently get_callback() doesn't allocate memory for the function name.
>> This needs to be changed to expand the function name. Maybe
>> arg->vval.v_string can be freed and reallocated?
>>
>> >
>> >> Using the function name (as a string) as a callback is supported
>> >> for global functions.
>> >
>> > Yes, then the function will be looked up when used, no context needed.
>>
>> This will be confusing to a user. Function name as a string can be used
>> for global functions but not for script local functions.
>
> It's been like that forever. We can't take away what already works, for
> future work it's better to always use function(). We can add more hints
> in the help for that.
>
--
--
You received this message from the "vim_dev" 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_dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.
diff --git a/src/eval.c b/src/eval.c
index 3578c99..09fe681 100644
--- a/src/eval.c
+++ b/src/eval.c
@@ -20771,8 +20771,33 @@ get_callback(typval_T *arg, partial_T **pp)
return (*pp)->pt_name;
}
*pp = NULL;
- if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
+ if (arg->v_type == VAR_FUNC)
return arg->vval.v_string;
+ else if (arg->v_type == VAR_STRING)
+ {
+ char_u *s = arg->vval.v_string;
+
+ if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
+ {
+ char sid_buf[25];
+ char_u *name;
+ int off = *s == 's' ? 2 : 5;
+
+ /* Expand s: and <SID> into <SNR>nr_ */
+ sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
+ name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
+ if (name != NULL)
+ {
+ STRCPY(name, sid_buf);
+ STRCAT(name, s + off);
+
+ vim_free(arg->vval.v_string);
+ arg->vval.v_string = name;
+ }
+ }
+
+ return arg->vval.v_string;
+ }
if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
return (char_u *)"";
EMSG(_("E921: Invalid callback argument"));