diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-12-02 21:00:27 +0800 |
---|---|---|
committer | zeertzjq <zeertzjq@outlook.com> | 2022-12-03 03:39:56 +0800 |
commit | 70ac0c9358251bf54f29a30a369880d94680ace7 (patch) | |
tree | 894b96de2f83a0394f30ef45591f185601c71ad0 | |
parent | 07e6296520fc83b1fdb287b5173494cdd0e9136f (diff) | |
download | rneovim-70ac0c9358251bf54f29a30a369880d94680ace7.tar.gz rneovim-70ac0c9358251bf54f29a30a369880d94680ace7.tar.bz2 rneovim-70ac0c9358251bf54f29a30a369880d94680ace7.zip |
vim-patch:8.2.3889: duplicate code for translating script-local function name
Problem: Duplicate code for translating script-local function name.
Solution: Move the code to get_scriptlocal_funcname(). (Yegappan Lakshmanan,
closes vim/vim#9393)
https://github.com/vim/vim/commit/e7f4abd38b6e05100c699900c8f87281e363beb2
Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
-rw-r--r-- | src/nvim/eval.c | 17 | ||||
-rw-r--r-- | src/nvim/eval/userfunc.c | 34 | ||||
-rw-r--r-- | src/nvim/option.c | 21 | ||||
-rw-r--r-- | src/nvim/testdir/test_expr.vim | 15 | ||||
-rw-r--r-- | src/nvim/testdir/test_normal.vim | 12 |
5 files changed, 71 insertions, 28 deletions
diff --git a/src/nvim/eval.c b/src/nvim/eval.c index c4afd6934c..d8dbcf6eb0 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -5018,18 +5018,11 @@ void common_function(typval_T *argvars, typval_T *rettv, bool is_funcref) int arg_idx = 0; list_T *list = NULL; if (strncmp(s, "s:", 2) == 0 || strncmp(s, "<SID>", 5) == 0) { - char sid_buf[25]; - int off = *s == 's' ? 2 : 5; - // Expand s: and <SID> into <SNR>nr_, so that the function can // also be called from another script. Using trans_function_name() // would also work, but some plugins depend on the name being // printable text. - snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRId64 "_", - (int64_t)current_sctx.sc_sid); - name = xmalloc(strlen(sid_buf) + strlen(s + off) + 1); - STRCPY(name, sid_buf); - STRCAT(name, s + off); + name = get_scriptlocal_funcname(s); } else { name = xstrdup(s); } @@ -5538,6 +5531,14 @@ bool callback_from_typval(Callback *const callback, typval_T *const arg) callback->type = kCallbackNone; callback->data.funcref = NULL; } else { + if (arg->v_type == VAR_STRING) { + char *newname = get_scriptlocal_funcname(arg->vval.v_string); + if (newname != NULL) { + xfree(arg->vval.v_string); + name = arg->vval.v_string = newname; + } + } + func_ref((char_u *)name); callback->data.funcref = xstrdup(name); callback->type = kCallbackFuncref; diff --git a/src/nvim/eval/userfunc.c b/src/nvim/eval/userfunc.c index 359ce08554..a37613dca9 100644 --- a/src/nvim/eval/userfunc.c +++ b/src/nvim/eval/userfunc.c @@ -1930,6 +1930,40 @@ theend: return (char_u *)name; } +/// If the "funcname" starts with "s:" or "<SID>", then expands it to the +/// current script ID and returns the expanded function name. The caller should +/// free the returned name. If not called from a script context or the function +/// name doesn't start with these prefixes, then returns NULL. +/// This doesn't check whether the script-local function exists or not. +char *get_scriptlocal_funcname(char *funcname) +{ + if (funcname == NULL) { + return NULL; + } + + if (strncmp(funcname, "s:", 2) != 0 + && strncmp(funcname, "<SID>", 5) != 0) { + // The function name is not a script-local function name + return NULL; + } + + if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) { + emsg(_(e_usingsid)); + return NULL; + } + + char sid_buf[25]; + // Expand s: and <SID> prefix into <SNR>nr_<name> + snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRId64 "_", + (int64_t)current_sctx.sc_sid); + const int off = *funcname == 's' ? 2 : 5; + char *newname = xmalloc(strlen(sid_buf) + strlen(funcname + off) + 1); + STRCPY(newname, sid_buf); + STRCAT(newname, funcname + off); + + return newname; +} + /// Call trans_function_name(), except that a lambda is returned as-is. /// Returns the name in allocated memory. char *save_function_name(char **name, bool skip, int flags, funcdict_T *fudi) diff --git a/src/nvim/option.c b/src/nvim/option.c index 6d461d9b9d..e67bacce61 100644 --- a/src/nvim/option.c +++ b/src/nvim/option.c @@ -5170,26 +5170,7 @@ int option_set_callback_func(char *optval, Callback *optcb) // treat everything else as a function name string tv = xcalloc(1, sizeof(*tv)); tv->v_type = VAR_STRING; - - // Function name starting with "s:" are supported only in a vimscript - // context. - if (strncmp(optval, "s:", 2) == 0) { - char sid_buf[25]; - - if (!SCRIPT_ID_VALID(current_sctx.sc_sid)) { - emsg(_(e_usingsid)); - return FAIL; - } - // Expand s: prefix into <SNR>nr_<name> - snprintf(sid_buf, sizeof(sid_buf), "<SNR>%" PRId64 "_", - (int64_t)current_sctx.sc_sid); - char *funcname = xmalloc(strlen(sid_buf) + strlen(optval + 2) + 1); - STRCPY(funcname, sid_buf); - STRCAT(funcname, optval + 2); - tv->vval.v_string = funcname; - } else { - tv->vval.v_string = xstrdup(optval); - } + tv->vval.v_string = xstrdup(optval); } Callback cb; diff --git a/src/nvim/testdir/test_expr.vim b/src/nvim/testdir/test_expr.vim index f33358c59a..47f7f5eb0e 100644 --- a/src/nvim/testdir/test_expr.vim +++ b/src/nvim/testdir/test_expr.vim @@ -525,6 +525,21 @@ func Test_funcref() call assert_fails('echo function("min") =~ function("min")', 'E694:') endfunc +" Test for calling function() and funcref() outside of a Vim script context. +func Test_function_outside_script() + let cleanup =<< trim END + call writefile([execute('messages')], 'Xtest.out') + qall + END + call writefile(cleanup, 'Xverify.vim') + call RunVim([], [], "-c \"echo function('s:abc')\" -S Xverify.vim") + call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0]) + call RunVim([], [], "-c \"echo funcref('s:abc')\" -S Xverify.vim") + call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0]) + call delete('Xtest.out') + call delete('Xverify.vim') +endfunc + func Test_setmatches() hi def link 1 Comment hi def link 2 PreProc diff --git a/src/nvim/testdir/test_normal.vim b/src/nvim/testdir/test_normal.vim index d9b392992f..9c5cc51f79 100644 --- a/src/nvim/testdir/test_normal.vim +++ b/src/nvim/testdir/test_normal.vim @@ -658,6 +658,18 @@ func Test_opfunc_callback() END " call CheckScriptSuccess(lines) + " setting 'opfunc' to a script local function outside of a script context + " should fail + let cleanup =<< trim END + call writefile([execute('messages')], 'Xtest.out') + qall + END + call writefile(cleanup, 'Xverify.vim') + call RunVim([], [], "-c \"set opfunc=s:abc\" -S Xverify.vim") + call assert_match('E81: Using <SID> not in a', readfile('Xtest.out')[0]) + call delete('Xtest.out') + call delete('Xverify.vim') + " cleanup set opfunc& delfunc OpFunc1 |