summaryrefslogtreecommitdiff
path: root/src/vim9script.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2022-03-30 21:12:27 +0100
committerBram Moolenaar <Bram@vim.org>2022-03-30 21:12:27 +0100
commitc0ceeeb839b8c6bebd3a2abd1c07d40ec3c6edca (patch)
tree2f51e084f45dbe3437520d32f83e67fb9704aa6b /src/vim9script.c
parentb4ad3b0deac12674a7773311890b48fd39c6807c (diff)
downloadvim-git-c0ceeeb839b8c6bebd3a2abd1c07d40ec3c6edca.tar.gz
patch 8.2.4650: "import autoload" only works with using 'runtimepath'v8.2.4650
Problem: "import autoload" only works with using 'runtimepath'. Solution: Also support a relative and absolute file name.
Diffstat (limited to 'src/vim9script.c')
-rw-r--r--src/vim9script.c72
1 files changed, 50 insertions, 22 deletions
diff --git a/src/vim9script.c b/src/vim9script.c
index b793110b9..0dad36ff6 100644
--- a/src/vim9script.c
+++ b/src/vim9script.c
@@ -384,6 +384,38 @@ mark_imports_for_reload(int sid)
}
/*
+ * 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 @@ handle_import(
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 @@ handle_import(
)
{
// 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 @@ find_exported(
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)