diff src/term.c @ 28453:862068e9e2a7 v8.2.4751

patch 8.2.4751: mapping <SID>name.Func does not work for autoload script Commit: https://github.com/vim/vim/commit/648dd88af67c7abac31915cbf0025f97031c96c1 Author: Bram Moolenaar <Bram@vim.org> Date: Thu Apr 14 21:36:15 2022 +0100 patch 8.2.4751: mapping <SID>name.Func does not work for autoload script Problem: Mapping <SID>name.Func does not work for script in autoload directory. Solution: Use the # form for a script in the autoload directory. (closes #10186)
author Bram Moolenaar <Bram@vim.org>
date Thu, 14 Apr 2022 22:45:03 +0200
parents 6f753a8125f0
children 4dcccb2673fe
line wrap: on
line diff
--- a/src/term.c
+++ b/src/term.c
@@ -5963,24 +5963,26 @@ replace_termcodes(
     int		do_special;	// recognize <> key codes
     int		do_key_code;	// recognize raw key codes
     char_u	*result;	// buffer for resulting string
+    garray_T	ga;
 
     do_backslash = (vim_strchr(p_cpo, CPO_BSLASH) == NULL);
     do_special = (vim_strchr(p_cpo, CPO_SPECI) == NULL)
 						  || (flags & REPTERM_SPECIAL);
     do_key_code = (vim_strchr(p_cpo, CPO_KEYCODE) == NULL);
+    src = from;
 
     /*
      * Allocate space for the translation.  Worst case a single character is
      * replaced by 6 bytes (shifted special key), plus a NUL at the end.
+     * In the rare case more might be needed ga_grow() must be called again.
      */
-    result = alloc(STRLEN(from) * 6 + 1);
-    if (result == NULL)		// out of memory
+    ga_init2(&ga, 1L, 100);
+    if (ga_grow(&ga, STRLEN(src) * 6 + 1) == FAIL) // out of memory
     {
 	*bufp = NULL;
 	return from;
     }
-
-    src = from;
+    result = ga.ga_data;
 
     /*
      * Check for #n at start only: function key n
@@ -6033,8 +6035,28 @@ replace_termcodes(
 
 			if (imp != NULL)
 			{
+			    scriptitem_T    *si = SCRIPT_ITEM(imp->imp_sid);
+			    size_t	    len;
+
+			    src = dot + 1;
+			    if (si->sn_autoload_prefix != NULL)
+			    {
+				// Turn "<SID>name.Func"
+				// into "scriptname#Func".
+				len = STRLEN(si->sn_autoload_prefix);
+				if (ga_grow(&ga, STRLEN(src) * 6 + len + 1)
+								       == FAIL)
+				{
+				    ga_clear(&ga);
+				    *bufp = NULL;
+				    return from;
+				}
+				result = ga.ga_data;
+				STRCPY(result + dlen, si->sn_autoload_prefix);
+				dlen += len;
+				continue;
+			    }
 			    sid = imp->imp_sid;
-			    src = dot + 1;
 			}
 		    }
 
@@ -6048,7 +6070,6 @@ replace_termcodes(
 		}
 	    }
 #endif
-
 	    slen = trans_special(&src, result + dlen, FSK_KEYCODE
 			  | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
 								 did_simplify);