summaryrefslogtreecommitdiff
path: root/src/scriptfile.c
diff options
context:
space:
mode:
authorBram Moolenaar <Bram@vim.org>2019-09-01 16:01:30 +0200
committerBram Moolenaar <Bram@vim.org>2019-09-01 16:01:30 +0200
commitda6c03342117fb7f4a8110bd9e8627b612a05a64 (patch)
tree978562abf59627127149d50d9f2650a69e78c078 /src/scriptfile.c
parent0fdddeeb66bbe326860ddfc573eba42f6487bbda (diff)
downloadvim-git-da6c03342117fb7f4a8110bd9e8627b612a05a64.tar.gz
patch 8.1.1957: more code can be moved to evalvars.cv8.1.1957
Problem: More code can be moved to evalvars.c. Solution: Move code to where it fits better. (Yegappan Lakshmanan, closes #4883)
Diffstat (limited to 'src/scriptfile.c')
-rw-r--r--src/scriptfile.c83
1 files changed, 83 insertions, 0 deletions
diff --git a/src/scriptfile.c b/src/scriptfile.c
index 15096e5cc..ca4abab1a 100644
--- a/src/scriptfile.c
+++ b/src/scriptfile.c
@@ -13,6 +13,11 @@
#include "vim.h"
+#if defined(FEAT_EVAL) || defined(PROTO)
+// The names of packages that once were loaded are remembered.
+static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
+#endif
+
/*
* ":runtime [what] {name}"
*/
@@ -1334,6 +1339,12 @@ free_scriptnames(void)
vim_free(SCRIPT_ITEM(i).sn_name);
ga_clear(&script_items);
}
+
+ void
+free_autoload_scriptnames(void)
+{
+ ga_clear_strings(&ga_loaded);
+}
# endif
#endif
@@ -1690,4 +1701,76 @@ source_finished(
&& ((struct source_cookie *)getline_cookie(
fgetline, cookie))->finished);
}
+
+/*
+ * Return the autoload script name for a function or variable name.
+ * Returns NULL when out of memory.
+ * Caller must make sure that "name" contains AUTOLOAD_CHAR.
+ */
+ char_u *
+autoload_name(char_u *name)
+{
+ char_u *p, *q = NULL;
+ char_u *scriptname;
+
+ // Get the script file name: replace '#' with '/', append ".vim".
+ scriptname = alloc(STRLEN(name) + 14);
+ if (scriptname == NULL)
+ return NULL;
+ STRCPY(scriptname, "autoload/");
+ STRCAT(scriptname, name);
+ for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL;
+ q = p, ++p)
+ *p = '/';
+ STRCPY(q, ".vim");
+ return scriptname;
+}
+
+/*
+ * If "name" has a package name try autoloading the script for it.
+ * Return TRUE if a package was loaded.
+ */
+ int
+script_autoload(
+ char_u *name,
+ int reload) // load script again when already loaded
+{
+ char_u *p;
+ char_u *scriptname, *tofree;
+ int ret = FALSE;
+ int i;
+
+ // If there is no '#' after name[0] there is no package name.
+ p = vim_strchr(name, AUTOLOAD_CHAR);
+ if (p == NULL || p == name)
+ return FALSE;
+
+ tofree = scriptname = autoload_name(name);
+ if (scriptname == NULL)
+ return FALSE;
+
+ // Find the name in the list of previously loaded package names. Skip
+ // "autoload/", it's always the same.
+ for (i = 0; i < ga_loaded.ga_len; ++i)
+ if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
+ break;
+ if (!reload && i < ga_loaded.ga_len)
+ ret = FALSE; // was loaded already
+ else
+ {
+ // Remember the name if it wasn't loaded already.
+ if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
+ {
+ ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
+ tofree = NULL;
+ }
+
+ // Try loading the package from $VIMRUNTIME/autoload/<name>.vim
+ if (source_runtime(scriptname, 0) == OK)
+ ret = TRUE;
+ }
+
+ vim_free(tofree);
+ return ret;
+}
#endif