Mercurial > vim
comparison src/scriptfile.c @ 27049:140102677c12 v8.2.4053
patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Commit: https://github.com/vim/vim/commit/fe2ef0b2cda0b25c45bd9e320f8b77931ee8ef2e
Author: Bram Moolenaar <Bram@vim.org>
Date: Mon Jan 10 18:08:00 2022 +0000
patch 8.2.4053: Vim9: autoload mechanism doesn't fully work yet
Problem: Vim9: autoload mechanism doesn't fully work yet.
Solution: Define functions and variables with their autoload name, add the
prefix when calling a function, find the variable in the table of
script variables.
author | Bram Moolenaar <Bram@vim.org> |
---|---|
date | Mon, 10 Jan 2022 19:15:04 +0100 |
parents | d31bd8607975 |
children | d4e7e3d82e78 |
comparison
equal
deleted
inserted
replaced
27048:80bbad68ec27 | 27049:140102677c12 |
---|---|
1709 free_imports_and_script_vars(i); | 1709 free_imports_and_script_vars(i); |
1710 free_string_option(si->sn_save_cpo); | 1710 free_string_option(si->sn_save_cpo); |
1711 # ifdef FEAT_PROFILE | 1711 # ifdef FEAT_PROFILE |
1712 ga_clear(&si->sn_prl_ga); | 1712 ga_clear(&si->sn_prl_ga); |
1713 # endif | 1713 # endif |
1714 vim_free(si->sn_autoload_prefix); | |
1714 vim_free(si); | 1715 vim_free(si); |
1715 } | 1716 } |
1716 ga_clear(&script_items); | 1717 ga_clear(&script_items); |
1717 } | 1718 } |
1718 | 1719 |
2140 } | 2141 } |
2141 return res; | 2142 return res; |
2142 } | 2143 } |
2143 | 2144 |
2144 /* | 2145 /* |
2146 * For an autoload script "autoload/dir/script.vim" return the prefix | |
2147 * "dir#script#" in allocated memory. | |
2148 * Returns NULL if anything is wrong. | |
2149 */ | |
2150 char_u * | |
2151 get_autoload_prefix(scriptitem_T *si) | |
2152 { | |
2153 char_u *p = script_name_after_autoload(si); | |
2154 char_u *prefix; | |
2155 | |
2156 if (p == NULL) | |
2157 return NULL; | |
2158 prefix = vim_strsave(p); | |
2159 if (prefix == NULL) | |
2160 return NULL; | |
2161 | |
2162 // replace all '/' with '#' and locate ".vim" at the end | |
2163 for (p = prefix; *p != NUL; p += mb_ptr2len(p)) | |
2164 { | |
2165 if (vim_ispathsep(*p)) | |
2166 *p = '#'; | |
2167 else if (STRCMP(p, ".vim") == 0) | |
2168 { | |
2169 p[0] = '#'; | |
2170 p[1] = NUL; | |
2171 return prefix; | |
2172 } | |
2173 } | |
2174 | |
2175 // did not find ".vim" at the end | |
2176 vim_free(prefix); | |
2177 return NULL; | |
2178 } | |
2179 | |
2180 /* | |
2145 * If in a Vim9 autoload script return "name" with the autoload prefix for the | 2181 * If in a Vim9 autoload script return "name" with the autoload prefix for the |
2146 * script. If successful "name" is freed, the returned name is allocated. | 2182 * script. If successful "name" is freed, the returned name is allocated. |
2147 * Otherwise it returns "name" unmodified. | 2183 * Otherwise it returns "name" unmodified. |
2148 */ | 2184 */ |
2149 char_u * | 2185 char_u * |
2151 { | 2187 { |
2152 if (SCRIPT_ID_VALID(current_sctx.sc_sid)) | 2188 if (SCRIPT_ID_VALID(current_sctx.sc_sid)) |
2153 { | 2189 { |
2154 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); | 2190 scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
2155 | 2191 |
2156 if (si->sn_is_autoload) | 2192 if (si->sn_autoload_prefix != NULL) |
2157 { | 2193 { |
2158 char_u *p = script_name_after_autoload(si); | 2194 char_u *basename = name; |
2159 | 2195 size_t len; |
2160 if (p != NULL) | 2196 char_u *res; |
2197 | |
2198 if (*name == K_SPECIAL) | |
2161 { | 2199 { |
2162 char_u *tail = vim_strsave(p); | 2200 char_u *p = vim_strchr(name, '_'); |
2163 | 2201 |
2164 if (tail != NULL) | 2202 // skip over "<SNR>99_" |
2165 { | 2203 if (p != NULL) |
2166 for (p = tail; *p != NUL; p += mb_ptr2len(p)) | 2204 basename = p + 1; |
2167 { | 2205 } |
2168 if (vim_ispathsep(*p)) | 2206 |
2169 *p = '#'; | 2207 len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2; |
2170 else if (STRCMP(p, ".vim")) | 2208 res = alloc(len); |
2171 { | 2209 if (res != NULL) |
2172 size_t len = (p - tail) + STRLEN(name) + 2; | 2210 { |
2173 char_u *res = alloc(len); | 2211 vim_snprintf((char *)res, len, "%s%s", |
2174 | 2212 si->sn_autoload_prefix, basename); |
2175 if (res == NULL) | 2213 return res; |
2176 break; | |
2177 *p = NUL; | |
2178 vim_snprintf((char *)res, len, "%s#%s", tail, name); | |
2179 vim_free(name); | |
2180 vim_free(tail); | |
2181 return res; | |
2182 } | |
2183 } | |
2184 } | |
2185 // did not find ".vim" at the end | |
2186 vim_free(tail); | |
2187 } | 2214 } |
2188 } | 2215 } |
2189 } | 2216 } |
2190 return name; | 2217 return name; |
2191 } | 2218 } |