diff options
author | zeertzjq <zeertzjq@outlook.com> | 2022-12-03 04:26:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-03 04:26:00 +0800 |
commit | 10c50d9f30138e7811789ba1c62f4c520cf04c8f (patch) | |
tree | 6be3628f7d152c1f703f59fa1050039c90244707 /src/nvim/eval/userfunc.c | |
parent | 07e6296520fc83b1fdb287b5173494cdd0e9136f (diff) | |
parent | afb3ff52ecafe2d5bd1239869124794bb2ac68b9 (diff) | |
download | rneovim-10c50d9f30138e7811789ba1c62f4c520cf04c8f.tar.gz rneovim-10c50d9f30138e7811789ba1c62f4c520cf04c8f.tar.bz2 rneovim-10c50d9f30138e7811789ba1c62f4c520cf04c8f.zip |
Merge pull request #21266 from zeertzjq/vim-8.2.3889
vim-patch:8.2.3889,9.0.{0805,0990}
Diffstat (limited to 'src/nvim/eval/userfunc.c')
-rw-r--r-- | src/nvim/eval/userfunc.c | 34 |
1 files changed, 34 insertions, 0 deletions
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) |