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)
diff --git a/src/term.c b/src/term.c
index e51134f..1c6aadc 100644
--- a/src/term.c
+++ b/src/term.c
@@ -5963,24 +5963,26 @@
     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 @@
 
 			if (imp != NULL)
 			{
-			    sid = imp->imp_sid;
+			    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;
 			}
 		    }
 
@@ -6048,7 +6070,6 @@
 		}
 	    }
 #endif
-
 	    slen = trans_special(&src, result + dlen, FSK_KEYCODE
 			  | ((flags & REPTERM_NO_SIMPLIFY) ? 0 : FSK_SIMPLIFY),
 								 did_simplify);
diff --git a/src/testdir/test_vim9_import.vim b/src/testdir/test_vim9_import.vim
index 94cb760..dd9f566 100644
--- a/src/testdir/test_vim9_import.vim
+++ b/src/testdir/test_vim9_import.vim
@@ -669,32 +669,63 @@
   nunmap <F4>
 enddef
 
-def Test_use_autoload_import_in_mapping()
+def Test_use_relative_autoload_import_in_mapping()
   var lines =<< trim END
       vim9script
       export def Func()
         g:result = 42
       enddef
   END
-  writefile(lines, 'XautoloadExport.vim')
+  writefile(lines, 'XrelautoloadExport.vim')
   lines =<< trim END
       vim9script
-      import autoload './XautoloadExport.vim' as some
+      import autoload './XrelautoloadExport.vim' as some
       nnoremap <F3> :call <SID>some.Func()<CR>
   END
   writefile(lines, 'Xmapscript.vim')
 
   source Xmapscript.vim
-  assert_match('\d\+ A: .*XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
+  assert_match('\d\+ A: .*XrelautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
   feedkeys("\<F3>", "xt")
   assert_equal(42, g:result)
 
   unlet g:result
-  delete('XautoloadExport.vim')
+  delete('XrelautoloadExport.vim')
   delete('Xmapscript.vim')
   nunmap <F3>
 enddef
 
+def Test_use_autoload_import_in_mapping()
+  var lines =<< trim END
+      vim9script
+      export def Func()
+        g:result = 49
+      enddef
+  END
+  mkdir('Xdir/autoload', 'p')
+  writefile(lines, 'Xdir/autoload/XautoloadExport.vim')
+  var save_rtp = &rtp
+  exe 'set rtp^=' .. getcwd() .. '/Xdir'
+
+  lines =<< trim END
+      vim9script
+      import autoload 'XautoloadExport.vim' as some
+      nnoremap <F3> :call <SID>some.Func()<CR>
+  END
+  writefile(lines, 'Xmapscript.vim')
+
+  source Xmapscript.vim
+  assert_match('\d\+ A: .*autoload/XautoloadExport.vim', execute('scriptnames')->split("\n")[-1])
+  feedkeys("\<F3>", "xt")
+  assert_equal(49, g:result)
+
+  unlet g:result
+  delete('Xmapscript.vim')
+  nunmap <F3>
+  delete('Xdir', 'rf')
+  &rtp = save_rtp
+enddef
+
 def Test_use_import_in_command_completion()
   var lines =<< trim END
       vim9script
diff --git a/src/version.c b/src/version.c
index 68b61f4..db82785 100644
--- a/src/version.c
+++ b/src/version.c
@@ -747,6 +747,8 @@
 static int included_patches[] =
 {   /* Add new patch number below this line */
 /**/
+    4751,
+/**/
     4750,
 /**/
     4749,