diff src/userfunc.c @ 26721:9c9b8d95b05f v8.2.3889

patch 8.2.3889: duplicate code for translating script-local function name Commit: https://github.com/vim/vim/commit/e7f4abd38b6e05100c699900c8f87281e363beb2 Author: Yegappan Lakshmanan <yegappan@yahoo.com> Date: Fri Dec 24 20:47:38 2021 +0000 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 #9393)
author Bram Moolenaar <Bram@vim.org>
date Fri, 24 Dec 2021 22:00:03 +0100
parents fac6673086df
children a8a4e1e7b111
line wrap: on
line diff
--- a/src/userfunc.c
+++ b/src/userfunc.c
@@ -3876,6 +3876,46 @@ untrans_function_name(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_u *
+get_scriptlocal_funcname(char_u *funcname)
+{
+    char	sid_buf[25];
+    int		off;
+    char_u	*newname;
+
+    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_using_sid_not_in_script_context));
+	return NULL;
+    }
+    // Expand s: prefix into <SNR>nr_<name>
+    vim_snprintf(sid_buf, sizeof(sid_buf), "<SNR>%ld_",
+	    (long)current_sctx.sc_sid);
+    off = *funcname == 's' ? 2 : 5;
+    newname = alloc(STRLEN(sid_buf) + STRLEN(funcname + off) + 1);
+    if (newname == NULL)
+	return NULL;
+    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.
  */