patch 8.2.4650: "import autoload" only works with using 'runtimepath'
Problem: "import autoload" only works with using 'runtimepath'.
Solution: Also support a relative and absolute file name.
diff --git a/src/vim9script.c b/src/vim9script.c
index b793110..0dad36f 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -384,6 +384,38 @@
}
/*
+ * Part of "import" that handles a relative or absolute file name/
+ * Returns OK or FAIL.
+ */
+ static int
+handle_import_fname(char_u *fname, int is_autoload, int *sid)
+{
+ if (is_autoload)
+ {
+ scriptitem_T *si;
+
+ *sid = find_script_by_name(fname);
+ if (*sid < 0)
+ {
+ int error = OK;
+
+ // script does not exist yet, create a new scriptitem
+ *sid = get_new_scriptitem_for_fname(&error, fname);
+ if (error == FAIL)
+ return FAIL;
+ }
+
+ si = SCRIPT_ITEM(*sid);
+ si->sn_import_autoload = TRUE;
+
+ // with testing override: load autoload script right away
+ if (!override_autoload || si->sn_state != SN_STATE_NOT_LOADED)
+ return OK;
+ }
+ return do_source(fname, FALSE, DOSO_NONE, sid);
+}
+
+/*
* Handle an ":import" command and add the resulting imported_T to "gap", when
* not NULL, or script "import_sid" sn_imports.
* "cctx" is NULL at the script level.
@@ -442,25 +474,18 @@
char_u *tail = gettail(si->sn_name);
char_u *from_name;
- if (is_autoload)
- res = FAIL;
- else
- {
+ // Relative to current script: "./name.vim", "../../name.vim".
+ len = STRLEN(si->sn_name) - STRLEN(tail) + STRLEN(tv.vval.v_string) + 2;
+ from_name = alloc((int)len);
+ if (from_name == NULL)
+ goto erret;
+ vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
+ add_pathsep(from_name);
+ STRCAT(from_name, tv.vval.v_string);
+ simplify_filename(from_name);
- // Relative to current script: "./name.vim", "../../name.vim".
- len = STRLEN(si->sn_name) - STRLEN(tail)
- + STRLEN(tv.vval.v_string) + 2;
- from_name = alloc((int)len);
- if (from_name == NULL)
- goto erret;
- vim_strncpy(from_name, si->sn_name, tail - si->sn_name);
- add_pathsep(from_name);
- STRCAT(from_name, tv.vval.v_string);
- simplify_filename(from_name);
-
- res = do_source(from_name, FALSE, DOSO_NONE, &sid);
- vim_free(from_name);
- }
+ res = handle_import_fname(from_name, is_autoload, &sid);
+ vim_free(from_name);
}
else if (mch_isFullName(tv.vval.v_string)
#ifdef BACKSLASH_IN_FILENAME
@@ -471,10 +496,7 @@
)
{
// Absolute path: "/tmp/name.vim"
- if (is_autoload)
- res = FAIL;
- else
- res = do_source(tv.vval.v_string, FALSE, DOSO_NONE, &sid);
+ res = handle_import_fname(tv.vval.v_string, is_autoload, &sid);
}
else if (is_autoload)
{
@@ -677,6 +699,12 @@
svar_T *sv;
scriptitem_T *script = SCRIPT_ITEM(sid);
+ if (script->sn_import_autoload && script->sn_state == SN_STATE_NOT_LOADED)
+ {
+ if (do_source(script->sn_name, FALSE, DOSO_NONE, NULL) == FAIL)
+ return -1;
+ }
+
// Find name in "script".
idx = get_script_item_idx(sid, name, 0, cctx, cstack);
if (idx >= 0)