0
0
mirror of https://github.com/vim/vim.git synced 2025-09-23 03:43:49 -04:00

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.
This commit is contained in:
Bram Moolenaar
2022-03-30 21:12:27 +01:00
parent b4ad3b0dea
commit c0ceeeb839
15 changed files with 442 additions and 82 deletions

View File

@@ -383,6 +383,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.
@@ -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)