Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * scriptfile.c: functions for dealing with the runtime directories/files |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 16 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 17 | // The names of packages that once were loaded are remembered. |
| 18 | static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL}; |
| 19 | #endif |
| 20 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 21 | // last used sequence number for sourcing scripts (current_sctx.sc_seq) |
| 22 | #ifdef FEAT_EVAL |
| 23 | static int last_current_SID_seq = 0; |
| 24 | #endif |
| 25 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 26 | /* |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 27 | * Initialize the execution stack. |
| 28 | */ |
| 29 | void |
| 30 | estack_init(void) |
| 31 | { |
| 32 | estack_T *entry; |
| 33 | |
| 34 | if (ga_grow(&exestack, 10) == FAIL) |
| 35 | mch_exit(0); |
| 36 | entry = ((estack_T *)exestack.ga_data) + exestack.ga_len; |
| 37 | entry->es_type = ETYPE_TOP; |
| 38 | entry->es_name = NULL; |
| 39 | entry->es_lnum = 0; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 40 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 41 | entry->es_info.ufunc = NULL; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 42 | #endif |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 43 | ++exestack.ga_len; |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * Add an item to the execution stack. |
| 48 | * Returns the new entry or NULL when out of memory. |
| 49 | */ |
| 50 | estack_T * |
| 51 | estack_push(etype_T type, char_u *name, long lnum) |
| 52 | { |
| 53 | estack_T *entry; |
| 54 | |
| 55 | // If memory allocation fails then we'll pop more than we push, eventually |
| 56 | // at the top level it will be OK again. |
| 57 | if (ga_grow(&exestack, 1) == OK) |
| 58 | { |
| 59 | entry = ((estack_T *)exestack.ga_data) + exestack.ga_len; |
| 60 | entry->es_type = type; |
| 61 | entry->es_name = name; |
| 62 | entry->es_lnum = lnum; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 63 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 64 | entry->es_info.ufunc = NULL; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 65 | #endif |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 66 | ++exestack.ga_len; |
| 67 | return entry; |
| 68 | } |
| 69 | return NULL; |
| 70 | } |
| 71 | |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 72 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 73 | /* |
| 74 | * Add a user function to the execution stack. |
| 75 | */ |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 76 | estack_T * |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 77 | estack_push_ufunc(ufunc_T *ufunc, long lnum) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 78 | { |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 79 | estack_T *entry = estack_push(ETYPE_UFUNC, |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 80 | ufunc->uf_name_exp != NULL |
| 81 | ? ufunc->uf_name_exp : ufunc->uf_name, lnum); |
| 82 | if (entry != NULL) |
| 83 | entry->es_info.ufunc = ufunc; |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 84 | return entry; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 85 | } |
Bram Moolenaar | 25e0f58 | 2020-05-25 22:36:50 +0200 | [diff] [blame] | 86 | |
| 87 | /* |
| 88 | * Return TRUE if "ufunc" with "lnum" is already at the top of the exe stack. |
| 89 | */ |
| 90 | int |
| 91 | estack_top_is_ufunc(ufunc_T *ufunc, long lnum) |
| 92 | { |
| 93 | estack_T *entry; |
| 94 | |
| 95 | if (exestack.ga_len == 0) |
| 96 | return FALSE; |
| 97 | entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1; |
| 98 | return entry->es_type == ETYPE_UFUNC |
| 99 | && STRCMP( entry->es_name, ufunc->uf_name_exp != NULL |
| 100 | ? ufunc->uf_name_exp : ufunc->uf_name) == 0 |
| 101 | && entry->es_lnum == lnum; |
| 102 | } |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 103 | #endif |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 104 | |
| 105 | /* |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 106 | * Take an item off of the execution stack and return it. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 107 | */ |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 108 | estack_T * |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 109 | estack_pop(void) |
| 110 | { |
Bram Moolenaar | c620c05 | 2020-07-08 15:16:19 +0200 | [diff] [blame] | 111 | if (exestack.ga_len == 0) |
| 112 | return NULL; |
| 113 | --exestack.ga_len; |
| 114 | return ((estack_T *)exestack.ga_data) + exestack.ga_len; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Get the current value for <sfile> in allocated memory. |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 119 | * "which" is ESTACK_SFILE for <sfile> and ESTACK_STACK for <stack>. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 120 | */ |
| 121 | char_u * |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 122 | estack_sfile(estack_arg_T which UNUSED) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 123 | { |
Bram Moolenaar | 85b0957 | 2019-12-30 10:57:00 +0100 | [diff] [blame] | 124 | estack_T *entry; |
| 125 | #ifdef FEAT_EVAL |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 126 | garray_T ga; |
Bram Moolenaar | 4d7a248 | 2020-01-06 19:53:43 +0100 | [diff] [blame] | 127 | size_t len; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 128 | int idx; |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 129 | etype_T last_type = ETYPE_SCRIPT; |
| 130 | char *type_name; |
Bram Moolenaar | 85b0957 | 2019-12-30 10:57:00 +0100 | [diff] [blame] | 131 | #endif |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 132 | |
| 133 | entry = ((estack_T *)exestack.ga_data) + exestack.ga_len - 1; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 134 | #ifdef FEAT_EVAL |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 135 | if (which == ESTACK_SFILE && entry->es_type != ETYPE_UFUNC) |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 136 | #endif |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 137 | { |
| 138 | if (entry->es_name == NULL) |
| 139 | return NULL; |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 140 | return vim_strsave(entry->es_name); |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 141 | } |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 142 | #ifdef FEAT_EVAL |
Bram Moolenaar | c449271 | 2021-11-22 15:37:15 +0000 | [diff] [blame] | 143 | // expand('<sfile>') works in a function for backwards compatibility, but |
| 144 | // may give an unexpected result. Disallow it in Vim 9 script. |
| 145 | if (which == ESTACK_SFILE && in_vim9script()) |
| 146 | { |
| 147 | int save_emsg_off = emsg_off; |
| 148 | |
| 149 | if (emsg_off == 1) |
| 150 | // f_expand() silences errors but we do want this one |
| 151 | emsg_off = 0; |
| 152 | emsg(_(e_cannot_expand_sfile_in_vim9_function)); |
| 153 | emsg_off = save_emsg_off; |
| 154 | return NULL; |
| 155 | } |
| 156 | |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 157 | // Give information about each stack entry up to the root. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 158 | // For a function we compose the call stack, as it was done in the past: |
| 159 | // "function One[123]..Two[456]..Three" |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 160 | ga_init2(&ga, sizeof(char), 100); |
| 161 | for (idx = 0; idx < exestack.ga_len; ++idx) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 162 | { |
| 163 | entry = ((estack_T *)exestack.ga_data) + idx; |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 164 | if (entry->es_name != NULL) |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 165 | { |
Bram Moolenaar | a810db3 | 2020-09-11 17:59:23 +0200 | [diff] [blame] | 166 | long lnum = 0; |
| 167 | char *dots; |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 168 | |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 169 | len = STRLEN(entry->es_name) + 15; |
| 170 | type_name = ""; |
| 171 | if (entry->es_type != last_type) |
| 172 | { |
| 173 | switch (entry->es_type) |
| 174 | { |
| 175 | case ETYPE_SCRIPT: type_name = "script "; break; |
| 176 | case ETYPE_UFUNC: type_name = "function "; break; |
| 177 | default: type_name = ""; break; |
| 178 | } |
| 179 | last_type = entry->es_type; |
| 180 | } |
| 181 | len += STRLEN(type_name); |
Bram Moolenaar | d3bb6a8 | 2020-07-26 15:55:25 +0200 | [diff] [blame] | 182 | if (ga_grow(&ga, (int)len) == FAIL) |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 183 | break; |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 184 | if (idx == exestack.ga_len - 1) |
| 185 | lnum = which == ESTACK_STACK ? SOURCING_LNUM : 0; |
| 186 | else |
| 187 | lnum = entry->es_lnum; |
Bram Moolenaar | a810db3 | 2020-09-11 17:59:23 +0200 | [diff] [blame] | 188 | dots = idx == exestack.ga_len - 1 ? "" : ".."; |
Bram Moolenaar | 4f25b1a | 2020-09-10 19:25:05 +0200 | [diff] [blame] | 189 | if (lnum == 0) |
| 190 | // For the bottom entry of <sfile>: do not add the line number, |
| 191 | // it is used in <slnum>. Also leave it out when the number is |
| 192 | // not set. |
Bram Moolenaar | d3bb6a8 | 2020-07-26 15:55:25 +0200 | [diff] [blame] | 193 | vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s%s", |
Bram Moolenaar | a810db3 | 2020-09-11 17:59:23 +0200 | [diff] [blame] | 194 | type_name, entry->es_name, dots); |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 195 | else |
Bram Moolenaar | a810db3 | 2020-09-11 17:59:23 +0200 | [diff] [blame] | 196 | vim_snprintf((char *)ga.ga_data + ga.ga_len, len, "%s%s[%ld]%s", |
| 197 | type_name, entry->es_name, lnum, dots); |
Bram Moolenaar | d3bb6a8 | 2020-07-26 15:55:25 +0200 | [diff] [blame] | 198 | ga.ga_len += (int)STRLEN((char *)ga.ga_data + ga.ga_len); |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 199 | } |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 200 | } |
| 201 | |
Bram Moolenaar | a5d0423 | 2020-07-26 15:37:02 +0200 | [diff] [blame] | 202 | return (char_u *)ga.ga_data; |
Bram Moolenaar | 09d4640 | 2019-12-29 23:53:01 +0100 | [diff] [blame] | 203 | #endif |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 207 | * ":runtime [what] {name}" |
| 208 | */ |
| 209 | void |
| 210 | ex_runtime(exarg_T *eap) |
| 211 | { |
| 212 | char_u *arg = eap->arg; |
| 213 | char_u *p = skiptowhite(arg); |
| 214 | int len = (int)(p - arg); |
| 215 | int flags = eap->forceit ? DIP_ALL : 0; |
| 216 | |
| 217 | if (STRNCMP(arg, "START", len) == 0) |
| 218 | { |
| 219 | flags += DIP_START + DIP_NORTP; |
| 220 | arg = skipwhite(arg + len); |
| 221 | } |
| 222 | else if (STRNCMP(arg, "OPT", len) == 0) |
| 223 | { |
| 224 | flags += DIP_OPT + DIP_NORTP; |
| 225 | arg = skipwhite(arg + len); |
| 226 | } |
| 227 | else if (STRNCMP(arg, "PACK", len) == 0) |
| 228 | { |
| 229 | flags += DIP_START + DIP_OPT + DIP_NORTP; |
| 230 | arg = skipwhite(arg + len); |
| 231 | } |
| 232 | else if (STRNCMP(arg, "ALL", len) == 0) |
| 233 | { |
| 234 | flags += DIP_START + DIP_OPT; |
| 235 | arg = skipwhite(arg + len); |
| 236 | } |
| 237 | |
| 238 | source_runtime(arg, flags); |
| 239 | } |
| 240 | |
| 241 | static void |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 242 | source_callback(char_u *fname, void *cookie) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 243 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 244 | (void)do_source(fname, FALSE, DOSO_NONE, cookie); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 245 | } |
| 246 | |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 247 | #ifdef FEAT_EVAL |
| 248 | /* |
| 249 | * Find an already loaded script "name". |
| 250 | * If found returns its script ID. If not found returns -1. |
| 251 | */ |
| 252 | static int |
| 253 | find_script_by_name(char_u *name) |
| 254 | { |
| 255 | int sid; |
| 256 | scriptitem_T *si; |
| 257 | |
| 258 | for (sid = script_items.ga_len; sid > 0; --sid) |
| 259 | { |
| 260 | // We used to check inode here, but that doesn't work: |
| 261 | // - If a script is edited and written, it may get a different |
| 262 | // inode number, even though to the user it is the same script. |
| 263 | // - If a script is deleted and another script is written, with a |
| 264 | // different name, the inode may be re-used. |
| 265 | si = SCRIPT_ITEM(sid); |
| 266 | if (si->sn_name != NULL && fnamecmp(si->sn_name, name) == 0) |
| 267 | return sid; |
| 268 | } |
| 269 | return -1; |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * Add a new scriptitem with all items initialized. |
| 274 | * When running out of memory "error" is set to FAIL. |
| 275 | * Returns the script ID. |
| 276 | */ |
| 277 | static int |
| 278 | get_new_scriptitem(int *error) |
| 279 | { |
| 280 | static scid_T last_current_SID = 0; |
| 281 | int sid = ++last_current_SID; |
Bram Moolenaar | b06cfcf | 2022-01-10 11:26:33 +0000 | [diff] [blame] | 282 | scriptitem_T *si = NULL; |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 283 | |
| 284 | if (ga_grow(&script_items, (int)(sid - script_items.ga_len)) == FAIL) |
| 285 | { |
| 286 | *error = FAIL; |
| 287 | return sid; |
| 288 | } |
| 289 | while (script_items.ga_len < sid) |
| 290 | { |
| 291 | si = ALLOC_CLEAR_ONE(scriptitem_T); |
| 292 | if (si == NULL) |
| 293 | { |
| 294 | *error = FAIL; |
| 295 | return sid; |
| 296 | } |
| 297 | ++script_items.ga_len; |
| 298 | SCRIPT_ITEM(script_items.ga_len) = si; |
| 299 | si->sn_name = NULL; |
| 300 | si->sn_version = 1; |
| 301 | |
| 302 | // Allocate the local script variables to use for this script. |
| 303 | new_script_vars(script_items.ga_len); |
| 304 | ga_init2(&si->sn_var_vals, sizeof(svar_T), 10); |
| 305 | hash_init(&si->sn_all_vars.dv_hashtab); |
| 306 | ga_init2(&si->sn_imports, sizeof(imported_T), 10); |
| 307 | ga_init2(&si->sn_type_list, sizeof(type_T), 10); |
| 308 | # ifdef FEAT_PROFILE |
| 309 | si->sn_prof_on = FALSE; |
| 310 | # endif |
| 311 | } |
| 312 | |
Bram Moolenaar | b06cfcf | 2022-01-10 11:26:33 +0000 | [diff] [blame] | 313 | // "si" can't be NULL, check only to avoid a compiler warning |
| 314 | if (si != NULL) |
| 315 | // Used to check script variable index is still valid. |
| 316 | si->sn_script_seq = current_sctx.sc_seq; |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 317 | |
| 318 | return sid; |
| 319 | } |
| 320 | |
| 321 | static void |
| 322 | find_script_callback(char_u *fname, void *cookie) |
| 323 | { |
| 324 | int sid; |
| 325 | int error = OK; |
| 326 | int *ret_sid = cookie; |
| 327 | |
| 328 | sid = find_script_by_name(fname); |
| 329 | if (sid < 0) |
| 330 | { |
| 331 | // script does not exist yet, create a new scriptitem |
| 332 | sid = get_new_scriptitem(&error); |
| 333 | if (error == OK) |
| 334 | { |
| 335 | scriptitem_T *si = SCRIPT_ITEM(sid); |
| 336 | |
| 337 | si->sn_name = vim_strsave(fname); |
| 338 | si->sn_state = SN_STATE_NOT_LOADED; |
| 339 | } |
| 340 | } |
| 341 | *ret_sid = sid; |
| 342 | } |
| 343 | #endif |
| 344 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 345 | /* |
| 346 | * Find the file "name" in all directories in "path" and invoke |
| 347 | * "callback(fname, cookie)". |
| 348 | * "name" can contain wildcards. |
| 349 | * When "flags" has DIP_ALL: source all files, otherwise only the first one. |
| 350 | * When "flags" has DIP_DIR: find directories instead of files. |
| 351 | * When "flags" has DIP_ERR: give an error message if there is no match. |
| 352 | * |
| 353 | * return FAIL when no file could be sourced, OK otherwise. |
| 354 | */ |
| 355 | int |
| 356 | do_in_path( |
| 357 | char_u *path, |
| 358 | char_u *name, |
| 359 | int flags, |
| 360 | void (*callback)(char_u *fname, void *ck), |
| 361 | void *cookie) |
| 362 | { |
| 363 | char_u *rtp; |
| 364 | char_u *np; |
| 365 | char_u *buf; |
| 366 | char_u *rtp_copy; |
| 367 | char_u *tail; |
| 368 | int num_files; |
| 369 | char_u **files; |
| 370 | int i; |
| 371 | int did_one = FALSE; |
| 372 | #ifdef AMIGA |
| 373 | struct Process *proc = (struct Process *)FindTask(0L); |
| 374 | APTR save_winptr = proc->pr_WindowPtr; |
| 375 | |
| 376 | // Avoid a requester here for a volume that doesn't exist. |
| 377 | proc->pr_WindowPtr = (APTR)-1L; |
| 378 | #endif |
| 379 | |
| 380 | // Make a copy of 'runtimepath'. Invoking the callback may change the |
| 381 | // value. |
| 382 | rtp_copy = vim_strsave(path); |
| 383 | buf = alloc(MAXPATHL); |
| 384 | if (buf != NULL && rtp_copy != NULL) |
| 385 | { |
Bram Moolenaar | 647a530 | 2020-05-03 17:01:24 +0200 | [diff] [blame] | 386 | if (p_verbose > 10 && name != NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 387 | { |
| 388 | verbose_enter(); |
| 389 | smsg(_("Searching for \"%s\" in \"%s\""), |
| 390 | (char *)name, (char *)path); |
| 391 | verbose_leave(); |
| 392 | } |
| 393 | |
| 394 | // Loop over all entries in 'runtimepath'. |
| 395 | rtp = rtp_copy; |
| 396 | while (*rtp != NUL && ((flags & DIP_ALL) || !did_one)) |
| 397 | { |
| 398 | size_t buflen; |
| 399 | |
| 400 | // Copy the path from 'runtimepath' to buf[]. |
| 401 | copy_option_part(&rtp, buf, MAXPATHL, ","); |
| 402 | buflen = STRLEN(buf); |
| 403 | |
| 404 | // Skip after or non-after directories. |
| 405 | if (flags & (DIP_NOAFTER | DIP_AFTER)) |
| 406 | { |
| 407 | int is_after = buflen >= 5 |
| 408 | && STRCMP(buf + buflen - 5, "after") == 0; |
| 409 | |
| 410 | if ((is_after && (flags & DIP_NOAFTER)) |
| 411 | || (!is_after && (flags & DIP_AFTER))) |
| 412 | continue; |
| 413 | } |
| 414 | |
| 415 | if (name == NULL) |
| 416 | { |
| 417 | (*callback)(buf, (void *) &cookie); |
| 418 | if (!did_one) |
| 419 | did_one = (cookie == NULL); |
| 420 | } |
| 421 | else if (buflen + STRLEN(name) + 2 < MAXPATHL) |
| 422 | { |
| 423 | add_pathsep(buf); |
| 424 | tail = buf + STRLEN(buf); |
| 425 | |
| 426 | // Loop over all patterns in "name" |
| 427 | np = name; |
| 428 | while (*np != NUL && ((flags & DIP_ALL) || !did_one)) |
| 429 | { |
| 430 | // Append the pattern from "name" to buf[]. |
| 431 | copy_option_part(&np, tail, (int)(MAXPATHL - (tail - buf)), |
| 432 | "\t "); |
| 433 | |
Bram Moolenaar | 647a530 | 2020-05-03 17:01:24 +0200 | [diff] [blame] | 434 | if (p_verbose > 10) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 435 | { |
| 436 | verbose_enter(); |
| 437 | smsg(_("Searching for \"%s\""), buf); |
| 438 | verbose_leave(); |
| 439 | } |
| 440 | |
| 441 | // Expand wildcards, invoke the callback for each match. |
| 442 | if (gen_expand_wildcards(1, &buf, &num_files, &files, |
| 443 | (flags & DIP_DIR) ? EW_DIR : EW_FILE) == OK) |
| 444 | { |
| 445 | for (i = 0; i < num_files; ++i) |
| 446 | { |
| 447 | (*callback)(files[i], cookie); |
| 448 | did_one = TRUE; |
| 449 | if (!(flags & DIP_ALL)) |
| 450 | break; |
| 451 | } |
| 452 | FreeWild(num_files, files); |
| 453 | } |
| 454 | } |
| 455 | } |
| 456 | } |
| 457 | } |
| 458 | vim_free(buf); |
| 459 | vim_free(rtp_copy); |
| 460 | if (!did_one && name != NULL) |
| 461 | { |
| 462 | char *basepath = path == p_rtp ? "runtimepath" : "packpath"; |
| 463 | |
| 464 | if (flags & DIP_ERR) |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 465 | semsg(_(e_directory_not_found_in_str_str), basepath, name); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 466 | else if (p_verbose > 0) |
| 467 | { |
| 468 | verbose_enter(); |
| 469 | smsg(_("not found in '%s': \"%s\""), basepath, name); |
| 470 | verbose_leave(); |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | #ifdef AMIGA |
| 475 | proc->pr_WindowPtr = save_winptr; |
| 476 | #endif |
| 477 | |
| 478 | return did_one ? OK : FAIL; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | * Find "name" in "path". When found, invoke the callback function for |
| 483 | * it: callback(fname, "cookie") |
| 484 | * When "flags" has DIP_ALL repeat for all matches, otherwise only the first |
| 485 | * one is used. |
| 486 | * Returns OK when at least one match found, FAIL otherwise. |
| 487 | * |
| 488 | * If "name" is NULL calls callback for each entry in "path". Cookie is |
| 489 | * passed by reference in this case, setting it to NULL indicates that callback |
| 490 | * has done its job. |
| 491 | */ |
| 492 | static int |
| 493 | do_in_path_and_pp( |
| 494 | char_u *path, |
| 495 | char_u *name, |
| 496 | int flags, |
| 497 | void (*callback)(char_u *fname, void *ck), |
| 498 | void *cookie) |
| 499 | { |
| 500 | int done = FAIL; |
| 501 | char_u *s; |
| 502 | int len; |
| 503 | char *start_dir = "pack/*/start/*/%s"; |
| 504 | char *opt_dir = "pack/*/opt/*/%s"; |
| 505 | |
| 506 | if ((flags & DIP_NORTP) == 0) |
| 507 | done = do_in_path(path, name, flags, callback, cookie); |
| 508 | |
| 509 | if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_START)) |
| 510 | { |
| 511 | len = (int)(STRLEN(start_dir) + STRLEN(name)); |
| 512 | s = alloc(len); |
| 513 | if (s == NULL) |
| 514 | return FAIL; |
| 515 | vim_snprintf((char *)s, len, start_dir, name); |
| 516 | done = do_in_path(p_pp, s, flags, callback, cookie); |
| 517 | vim_free(s); |
| 518 | } |
| 519 | |
| 520 | if ((done == FAIL || (flags & DIP_ALL)) && (flags & DIP_OPT)) |
| 521 | { |
| 522 | len = (int)(STRLEN(opt_dir) + STRLEN(name)); |
| 523 | s = alloc(len); |
| 524 | if (s == NULL) |
| 525 | return FAIL; |
| 526 | vim_snprintf((char *)s, len, opt_dir, name); |
| 527 | done = do_in_path(p_pp, s, flags, callback, cookie); |
| 528 | vim_free(s); |
| 529 | } |
| 530 | |
| 531 | return done; |
| 532 | } |
| 533 | |
| 534 | /* |
| 535 | * Just like do_in_path_and_pp(), using 'runtimepath' for "path". |
| 536 | */ |
| 537 | int |
| 538 | do_in_runtimepath( |
| 539 | char_u *name, |
| 540 | int flags, |
| 541 | void (*callback)(char_u *fname, void *ck), |
| 542 | void *cookie) |
| 543 | { |
| 544 | return do_in_path_and_pp(p_rtp, name, flags, callback, cookie); |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * Source the file "name" from all directories in 'runtimepath'. |
| 549 | * "name" can contain wildcards. |
| 550 | * When "flags" has DIP_ALL: source all files, otherwise only the first one. |
| 551 | * |
| 552 | * return FAIL when no file could be sourced, OK otherwise. |
| 553 | */ |
| 554 | int |
| 555 | source_runtime(char_u *name, int flags) |
| 556 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 557 | return source_in_path(p_rtp, name, flags, NULL); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | /* |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 561 | * Just like source_runtime(), but use "path" instead of 'runtimepath' |
| 562 | * and return the script ID in "ret_sid". |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 563 | */ |
| 564 | int |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 565 | source_in_path(char_u *path, char_u *name, int flags, int *ret_sid) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 566 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 567 | return do_in_path_and_pp(path, name, flags, source_callback, ret_sid); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 568 | } |
| 569 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 570 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 571 | |
| 572 | /* |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 573 | * Find "name" in 'runtimepath'. If found a new scriptitem is created for it |
| 574 | * and it's script ID is returned. |
| 575 | * If not found returns -1. |
| 576 | */ |
| 577 | int |
| 578 | find_script_in_rtp(char_u *name) |
| 579 | { |
| 580 | int sid = -1; |
| 581 | |
| 582 | (void)do_in_path_and_pp(p_rtp, name, DIP_NOAFTER, |
| 583 | find_script_callback, &sid); |
| 584 | return sid; |
| 585 | } |
| 586 | |
| 587 | /* |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 588 | * Expand wildcards in "pat" and invoke do_source() for each match. |
| 589 | */ |
| 590 | static void |
| 591 | source_all_matches(char_u *pat) |
| 592 | { |
| 593 | int num_files; |
| 594 | char_u **files; |
| 595 | int i; |
| 596 | |
| 597 | if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK) |
| 598 | { |
| 599 | for (i = 0; i < num_files; ++i) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 600 | (void)do_source(files[i], FALSE, DOSO_NONE, NULL); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 601 | FreeWild(num_files, files); |
| 602 | } |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | * Add the package directory to 'runtimepath'. |
| 607 | */ |
| 608 | static int |
| 609 | add_pack_dir_to_rtp(char_u *fname) |
| 610 | { |
| 611 | char_u *p4, *p3, *p2, *p1, *p; |
| 612 | char_u *entry; |
| 613 | char_u *insp = NULL; |
| 614 | int c; |
| 615 | char_u *new_rtp; |
| 616 | int keep; |
| 617 | size_t oldlen; |
| 618 | size_t addlen; |
| 619 | size_t new_rtp_len; |
| 620 | char_u *afterdir = NULL; |
| 621 | size_t afterlen = 0; |
| 622 | char_u *after_insp = NULL; |
| 623 | char_u *ffname = NULL; |
| 624 | size_t fname_len; |
| 625 | char_u *buf = NULL; |
| 626 | char_u *rtp_ffname; |
| 627 | int match; |
| 628 | int retval = FAIL; |
| 629 | |
| 630 | p4 = p3 = p2 = p1 = get_past_head(fname); |
| 631 | for (p = p1; *p; MB_PTR_ADV(p)) |
| 632 | if (vim_ispathsep_nocolon(*p)) |
| 633 | { |
| 634 | p4 = p3; p3 = p2; p2 = p1; p1 = p; |
| 635 | } |
| 636 | |
| 637 | // now we have: |
| 638 | // rtp/pack/name/start/name |
| 639 | // p4 p3 p2 p1 |
| 640 | // |
| 641 | // find the part up to "pack" in 'runtimepath' |
| 642 | c = *++p4; // append pathsep in order to expand symlink |
| 643 | *p4 = NUL; |
| 644 | ffname = fix_fname(fname); |
| 645 | *p4 = c; |
| 646 | if (ffname == NULL) |
| 647 | return FAIL; |
| 648 | |
| 649 | // Find "ffname" in "p_rtp", ignoring '/' vs '\' differences. |
| 650 | // Also stop at the first "after" directory. |
| 651 | fname_len = STRLEN(ffname); |
| 652 | buf = alloc(MAXPATHL); |
| 653 | if (buf == NULL) |
| 654 | goto theend; |
| 655 | for (entry = p_rtp; *entry != NUL; ) |
| 656 | { |
| 657 | char_u *cur_entry = entry; |
| 658 | |
| 659 | copy_option_part(&entry, buf, MAXPATHL, ","); |
| 660 | if (insp == NULL) |
| 661 | { |
| 662 | add_pathsep(buf); |
| 663 | rtp_ffname = fix_fname(buf); |
| 664 | if (rtp_ffname == NULL) |
| 665 | goto theend; |
| 666 | match = vim_fnamencmp(rtp_ffname, ffname, fname_len) == 0; |
| 667 | vim_free(rtp_ffname); |
| 668 | if (match) |
| 669 | // Insert "ffname" after this entry (and comma). |
| 670 | insp = entry; |
| 671 | } |
| 672 | |
| 673 | if ((p = (char_u *)strstr((char *)buf, "after")) != NULL |
| 674 | && p > buf |
| 675 | && vim_ispathsep(p[-1]) |
| 676 | && (vim_ispathsep(p[5]) || p[5] == NUL || p[5] == ',')) |
| 677 | { |
| 678 | if (insp == NULL) |
| 679 | // Did not find "ffname" before the first "after" directory, |
| 680 | // insert it before this entry. |
| 681 | insp = cur_entry; |
| 682 | after_insp = cur_entry; |
| 683 | break; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (insp == NULL) |
| 688 | // Both "fname" and "after" not found, append at the end. |
| 689 | insp = p_rtp + STRLEN(p_rtp); |
| 690 | |
| 691 | // check if rtp/pack/name/start/name/after exists |
| 692 | afterdir = concat_fnames(fname, (char_u *)"after", TRUE); |
| 693 | if (afterdir != NULL && mch_isdir(afterdir)) |
| 694 | afterlen = STRLEN(afterdir) + 1; // add one for comma |
| 695 | |
| 696 | oldlen = STRLEN(p_rtp); |
| 697 | addlen = STRLEN(fname) + 1; // add one for comma |
| 698 | new_rtp = alloc(oldlen + addlen + afterlen + 1); // add one for NUL |
| 699 | if (new_rtp == NULL) |
| 700 | goto theend; |
| 701 | |
| 702 | // We now have 'rtp' parts: {keep}{keep_after}{rest}. |
| 703 | // Create new_rtp, first: {keep},{fname} |
| 704 | keep = (int)(insp - p_rtp); |
| 705 | mch_memmove(new_rtp, p_rtp, keep); |
| 706 | new_rtp_len = keep; |
| 707 | if (*insp == NUL) |
| 708 | new_rtp[new_rtp_len++] = ','; // add comma before |
| 709 | mch_memmove(new_rtp + new_rtp_len, fname, addlen - 1); |
| 710 | new_rtp_len += addlen - 1; |
| 711 | if (*insp != NUL) |
| 712 | new_rtp[new_rtp_len++] = ','; // add comma after |
| 713 | |
| 714 | if (afterlen > 0 && after_insp != NULL) |
| 715 | { |
| 716 | int keep_after = (int)(after_insp - p_rtp); |
| 717 | |
| 718 | // Add to new_rtp: {keep},{fname}{keep_after},{afterdir} |
| 719 | mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, |
| 720 | keep_after - keep); |
| 721 | new_rtp_len += keep_after - keep; |
| 722 | mch_memmove(new_rtp + new_rtp_len, afterdir, afterlen - 1); |
| 723 | new_rtp_len += afterlen - 1; |
| 724 | new_rtp[new_rtp_len++] = ','; |
| 725 | keep = keep_after; |
| 726 | } |
| 727 | |
| 728 | if (p_rtp[keep] != NUL) |
| 729 | // Append rest: {keep},{fname}{keep_after},{afterdir}{rest} |
| 730 | mch_memmove(new_rtp + new_rtp_len, p_rtp + keep, oldlen - keep + 1); |
| 731 | else |
| 732 | new_rtp[new_rtp_len] = NUL; |
| 733 | |
| 734 | if (afterlen > 0 && after_insp == NULL) |
| 735 | { |
| 736 | // Append afterdir when "after" was not found: |
| 737 | // {keep},{fname}{rest},{afterdir} |
| 738 | STRCAT(new_rtp, ","); |
| 739 | STRCAT(new_rtp, afterdir); |
| 740 | } |
| 741 | |
| 742 | set_option_value((char_u *)"rtp", 0L, new_rtp, 0); |
| 743 | vim_free(new_rtp); |
| 744 | retval = OK; |
| 745 | |
| 746 | theend: |
| 747 | vim_free(buf); |
| 748 | vim_free(ffname); |
| 749 | vim_free(afterdir); |
| 750 | return retval; |
| 751 | } |
| 752 | |
| 753 | /* |
| 754 | * Load scripts in "plugin" and "ftdetect" directories of the package. |
| 755 | */ |
| 756 | static int |
| 757 | load_pack_plugin(char_u *fname) |
| 758 | { |
| 759 | static char *plugpat = "%s/plugin/**/*.vim"; |
| 760 | static char *ftpat = "%s/ftdetect/*.vim"; |
| 761 | int len; |
| 762 | char_u *ffname = fix_fname(fname); |
| 763 | char_u *pat = NULL; |
| 764 | int retval = FAIL; |
| 765 | |
| 766 | if (ffname == NULL) |
| 767 | return FAIL; |
| 768 | len = (int)STRLEN(ffname) + (int)STRLEN(ftpat); |
| 769 | pat = alloc(len); |
| 770 | if (pat == NULL) |
| 771 | goto theend; |
| 772 | vim_snprintf((char *)pat, len, plugpat, ffname); |
| 773 | source_all_matches(pat); |
| 774 | |
| 775 | { |
| 776 | char_u *cmd = vim_strsave((char_u *)"g:did_load_filetypes"); |
| 777 | |
| 778 | // If runtime/filetype.vim wasn't loaded yet, the scripts will be |
| 779 | // found when it loads. |
| 780 | if (cmd != NULL && eval_to_number(cmd) > 0) |
| 781 | { |
| 782 | do_cmdline_cmd((char_u *)"augroup filetypedetect"); |
| 783 | vim_snprintf((char *)pat, len, ftpat, ffname); |
| 784 | source_all_matches(pat); |
| 785 | do_cmdline_cmd((char_u *)"augroup END"); |
| 786 | } |
| 787 | vim_free(cmd); |
| 788 | } |
| 789 | vim_free(pat); |
| 790 | retval = OK; |
| 791 | |
| 792 | theend: |
| 793 | vim_free(ffname); |
| 794 | return retval; |
| 795 | } |
| 796 | |
| 797 | // used for "cookie" of add_pack_plugin() |
| 798 | static int APP_ADD_DIR; |
| 799 | static int APP_LOAD; |
| 800 | static int APP_BOTH; |
| 801 | |
| 802 | static void |
| 803 | add_pack_plugin(char_u *fname, void *cookie) |
| 804 | { |
| 805 | if (cookie != &APP_LOAD) |
| 806 | { |
| 807 | char_u *buf = alloc(MAXPATHL); |
| 808 | char_u *p; |
| 809 | int found = FALSE; |
| 810 | |
| 811 | if (buf == NULL) |
| 812 | return; |
| 813 | p = p_rtp; |
| 814 | while (*p != NUL) |
| 815 | { |
| 816 | copy_option_part(&p, buf, MAXPATHL, ","); |
| 817 | if (pathcmp((char *)buf, (char *)fname, -1) == 0) |
| 818 | { |
| 819 | found = TRUE; |
| 820 | break; |
| 821 | } |
| 822 | } |
| 823 | vim_free(buf); |
| 824 | if (!found) |
| 825 | // directory is not yet in 'runtimepath', add it |
| 826 | if (add_pack_dir_to_rtp(fname) == FAIL) |
| 827 | return; |
| 828 | } |
| 829 | |
| 830 | if (cookie != &APP_ADD_DIR) |
| 831 | load_pack_plugin(fname); |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * Add all packages in the "start" directory to 'runtimepath'. |
| 836 | */ |
| 837 | void |
| 838 | add_pack_start_dirs(void) |
| 839 | { |
| 840 | do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR, |
| 841 | add_pack_plugin, &APP_ADD_DIR); |
| 842 | } |
| 843 | |
| 844 | /* |
| 845 | * Load plugins from all packages in the "start" directory. |
| 846 | */ |
| 847 | void |
| 848 | load_start_packages(void) |
| 849 | { |
| 850 | did_source_packages = TRUE; |
| 851 | do_in_path(p_pp, (char_u *)"pack/*/start/*", DIP_ALL + DIP_DIR, |
| 852 | add_pack_plugin, &APP_LOAD); |
| 853 | } |
| 854 | |
| 855 | /* |
| 856 | * ":packloadall" |
| 857 | * Find plugins in the package directories and source them. |
| 858 | */ |
| 859 | void |
| 860 | ex_packloadall(exarg_T *eap) |
| 861 | { |
| 862 | if (!did_source_packages || eap->forceit) |
| 863 | { |
| 864 | // First do a round to add all directories to 'runtimepath', then load |
| 865 | // the plugins. This allows for plugins to use an autoload directory |
| 866 | // of another plugin. |
| 867 | add_pack_start_dirs(); |
| 868 | load_start_packages(); |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* |
| 873 | * ":packadd[!] {name}" |
| 874 | */ |
| 875 | void |
| 876 | ex_packadd(exarg_T *eap) |
| 877 | { |
| 878 | static char *plugpat = "pack/*/%s/%s"; |
| 879 | int len; |
| 880 | char *pat; |
| 881 | int round; |
| 882 | int res = OK; |
| 883 | |
| 884 | // Round 1: use "start", round 2: use "opt". |
| 885 | for (round = 1; round <= 2; ++round) |
| 886 | { |
| 887 | // Only look under "start" when loading packages wasn't done yet. |
| 888 | if (round == 1 && did_source_packages) |
| 889 | continue; |
| 890 | |
| 891 | len = (int)STRLEN(plugpat) + (int)STRLEN(eap->arg) + 5; |
| 892 | pat = alloc(len); |
| 893 | if (pat == NULL) |
| 894 | return; |
| 895 | vim_snprintf(pat, len, plugpat, round == 1 ? "start" : "opt", eap->arg); |
| 896 | // The first round don't give a "not found" error, in the second round |
| 897 | // only when nothing was found in the first round. |
| 898 | res = do_in_path(p_pp, (char_u *)pat, |
| 899 | DIP_ALL + DIP_DIR + (round == 2 && res == FAIL ? DIP_ERR : 0), |
| 900 | add_pack_plugin, eap->forceit ? &APP_ADD_DIR : &APP_BOTH); |
| 901 | vim_free(pat); |
| 902 | } |
| 903 | } |
| 904 | #endif |
| 905 | |
| 906 | /* |
Bram Moolenaar | 26262f8 | 2019-09-04 20:59:15 +0200 | [diff] [blame] | 907 | * Sort "gap" and remove duplicate entries. "gap" is expected to contain a |
| 908 | * list of file names in allocated memory. |
| 909 | */ |
| 910 | void |
| 911 | remove_duplicates(garray_T *gap) |
| 912 | { |
| 913 | int i; |
| 914 | int j; |
| 915 | char_u **fnames = (char_u **)gap->ga_data; |
| 916 | |
| 917 | sort_strings(fnames, gap->ga_len); |
| 918 | for (i = gap->ga_len - 1; i > 0; --i) |
| 919 | if (fnamecmp(fnames[i - 1], fnames[i]) == 0) |
| 920 | { |
| 921 | vim_free(fnames[i]); |
| 922 | for (j = i + 1; j < gap->ga_len; ++j) |
| 923 | fnames[j - 1] = fnames[j]; |
| 924 | --gap->ga_len; |
| 925 | } |
| 926 | } |
| 927 | |
| 928 | /* |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 929 | * Expand color scheme, compiler or filetype names. |
| 930 | * Search from 'runtimepath': |
| 931 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 932 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 933 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 934 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 935 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
| 936 | * "dirnames" is an array with one or more directory names. |
| 937 | */ |
| 938 | int |
| 939 | ExpandRTDir( |
| 940 | char_u *pat, |
| 941 | int flags, |
| 942 | int *num_file, |
| 943 | char_u ***file, |
| 944 | char *dirnames[]) |
| 945 | { |
| 946 | char_u *s; |
| 947 | char_u *e; |
| 948 | char_u *match; |
| 949 | garray_T ga; |
| 950 | int i; |
| 951 | int pat_len; |
| 952 | |
| 953 | *num_file = 0; |
| 954 | *file = NULL; |
| 955 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 956 | ga_init2(&ga, sizeof(char *), 10); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 957 | |
| 958 | for (i = 0; dirnames[i] != NULL; ++i) |
| 959 | { |
| 960 | s = alloc(STRLEN(dirnames[i]) + pat_len + 7); |
| 961 | if (s == NULL) |
| 962 | { |
| 963 | ga_clear_strings(&ga); |
| 964 | return FAIL; |
| 965 | } |
| 966 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
| 967 | globpath(p_rtp, s, &ga, 0); |
| 968 | vim_free(s); |
| 969 | } |
| 970 | |
| 971 | if (flags & DIP_START) { |
| 972 | for (i = 0; dirnames[i] != NULL; ++i) |
| 973 | { |
| 974 | s = alloc(STRLEN(dirnames[i]) + pat_len + 22); |
| 975 | if (s == NULL) |
| 976 | { |
| 977 | ga_clear_strings(&ga); |
| 978 | return FAIL; |
| 979 | } |
| 980 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 981 | globpath(p_pp, s, &ga, 0); |
| 982 | vim_free(s); |
| 983 | } |
| 984 | } |
| 985 | |
| 986 | if (flags & DIP_OPT) { |
| 987 | for (i = 0; dirnames[i] != NULL; ++i) |
| 988 | { |
| 989 | s = alloc(STRLEN(dirnames[i]) + pat_len + 20); |
| 990 | if (s == NULL) |
| 991 | { |
| 992 | ga_clear_strings(&ga); |
| 993 | return FAIL; |
| 994 | } |
| 995 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 996 | globpath(p_pp, s, &ga, 0); |
| 997 | vim_free(s); |
| 998 | } |
| 999 | } |
| 1000 | |
| 1001 | for (i = 0; i < ga.ga_len; ++i) |
| 1002 | { |
| 1003 | match = ((char_u **)ga.ga_data)[i]; |
| 1004 | s = match; |
| 1005 | e = s + STRLEN(s); |
| 1006 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 1007 | { |
| 1008 | e -= 4; |
| 1009 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
| 1010 | if (s < match || vim_ispathsep(*s)) |
| 1011 | break; |
| 1012 | ++s; |
| 1013 | *e = NUL; |
| 1014 | mch_memmove(match, s, e - s + 1); |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | if (ga.ga_len == 0) |
| 1019 | return FAIL; |
| 1020 | |
| 1021 | // Sort and remove duplicates which can happen when specifying multiple |
| 1022 | // directories in dirnames. |
| 1023 | remove_duplicates(&ga); |
| 1024 | |
| 1025 | *file = ga.ga_data; |
| 1026 | *num_file = ga.ga_len; |
| 1027 | return OK; |
| 1028 | } |
| 1029 | |
| 1030 | /* |
| 1031 | * Expand loadplugin names: |
| 1032 | * 'packpath'/pack/ * /opt/{pat} |
| 1033 | */ |
| 1034 | int |
| 1035 | ExpandPackAddDir( |
| 1036 | char_u *pat, |
| 1037 | int *num_file, |
| 1038 | char_u ***file) |
| 1039 | { |
| 1040 | char_u *s; |
| 1041 | char_u *e; |
| 1042 | char_u *match; |
| 1043 | garray_T ga; |
| 1044 | int i; |
| 1045 | int pat_len; |
| 1046 | |
| 1047 | *num_file = 0; |
| 1048 | *file = NULL; |
| 1049 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 1050 | ga_init2(&ga, sizeof(char *), 10); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1051 | |
| 1052 | s = alloc(pat_len + 26); |
| 1053 | if (s == NULL) |
| 1054 | { |
| 1055 | ga_clear_strings(&ga); |
| 1056 | return FAIL; |
| 1057 | } |
| 1058 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 1059 | globpath(p_pp, s, &ga, 0); |
| 1060 | vim_free(s); |
| 1061 | |
| 1062 | for (i = 0; i < ga.ga_len; ++i) |
| 1063 | { |
| 1064 | match = ((char_u **)ga.ga_data)[i]; |
| 1065 | s = gettail(match); |
| 1066 | e = s + STRLEN(s); |
| 1067 | mch_memmove(match, s, e - s + 1); |
| 1068 | } |
| 1069 | |
| 1070 | if (ga.ga_len == 0) |
| 1071 | return FAIL; |
| 1072 | |
| 1073 | // Sort and remove duplicates which can happen when specifying multiple |
| 1074 | // directories in dirnames. |
| 1075 | remove_duplicates(&ga); |
| 1076 | |
| 1077 | *file = ga.ga_data; |
| 1078 | *num_file = ga.ga_len; |
| 1079 | return OK; |
| 1080 | } |
| 1081 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 1082 | /* |
| 1083 | * Cookie used to source Ex commands from a buffer. |
| 1084 | */ |
| 1085 | typedef struct |
| 1086 | { |
| 1087 | garray_T lines_to_source; |
| 1088 | int lnum; |
| 1089 | linenr_T sourcing_lnum; |
| 1090 | } bufline_cookie_T; |
| 1091 | |
| 1092 | /* |
| 1093 | * Concatenate a Vim script line if it starts with a line continuation into a |
| 1094 | * growarray (excluding the continuation chars and leading whitespace). |
| 1095 | * Growsize of the growarray may be changed to speed up concatenations! |
| 1096 | * |
| 1097 | * Returns TRUE if this line did begin with a continuation (the next line |
| 1098 | * should also be considered, if it exists); FALSE otherwise. |
| 1099 | */ |
| 1100 | static int |
| 1101 | concat_continued_line( |
| 1102 | garray_T *ga, |
| 1103 | int init_growsize, |
| 1104 | char_u *nextline, |
| 1105 | int options) |
| 1106 | { |
| 1107 | int comment_char = in_vim9script() ? '#' : '"'; |
| 1108 | char_u *p = skipwhite(nextline); |
| 1109 | int contline; |
| 1110 | int do_vim9_all = in_vim9script() |
| 1111 | && options == GETLINE_CONCAT_ALL; |
| 1112 | int do_bar_cont = do_vim9_all |
| 1113 | || options == GETLINE_CONCAT_CONTBAR; |
| 1114 | |
| 1115 | if (*p == NUL) |
| 1116 | return FALSE; |
| 1117 | |
| 1118 | // Concatenate the next line when it starts with a backslash. |
| 1119 | /* Also check for a comment in between continuation lines: "\ */ |
| 1120 | // Also check for a Vim9 comment, empty line, line starting with '|', |
| 1121 | // but not "||". |
| 1122 | if ((p[0] == comment_char && p[1] == '\\' && p[2] == ' ') |
| 1123 | || (do_vim9_all && (*p == NUL |
| 1124 | || vim9_comment_start(p)))) |
| 1125 | return TRUE; |
| 1126 | |
| 1127 | contline = (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|')); |
| 1128 | if (!contline) |
| 1129 | return FALSE; |
| 1130 | |
| 1131 | // Adjust the growsize to the current length to speed up concatenating many |
| 1132 | // lines. |
| 1133 | if (ga->ga_len > init_growsize) |
| 1134 | ga->ga_growsize = ga->ga_len > 8000 ? 8000 : ga->ga_len; |
| 1135 | if (*p == '\\') |
| 1136 | ga_concat(ga, (char_u *)p + 1); |
| 1137 | else if (*p == '|') |
| 1138 | { |
| 1139 | ga_concat(ga, (char_u *)" "); |
| 1140 | ga_concat(ga, p); |
| 1141 | } |
| 1142 | |
| 1143 | return TRUE; |
| 1144 | } |
| 1145 | |
| 1146 | /* |
| 1147 | * Get one full line from a sourced string (in-memory, no file). |
| 1148 | * Called by do_cmdline() when it's called from source_using_linegetter(). |
| 1149 | * |
| 1150 | * Returns a pointer to allocated line, or NULL for end-of-file. |
| 1151 | */ |
| 1152 | static char_u * |
| 1153 | source_getbufline( |
| 1154 | int c UNUSED, |
| 1155 | void *cookie, |
| 1156 | int indent UNUSED, |
| 1157 | getline_opt_T opts) |
| 1158 | { |
| 1159 | bufline_cookie_T *p = cookie; |
| 1160 | char_u *line; |
| 1161 | garray_T ga; |
| 1162 | |
| 1163 | SOURCING_LNUM = p->sourcing_lnum + 1; |
| 1164 | |
| 1165 | if (p->lnum >= p->lines_to_source.ga_len) |
| 1166 | return NULL; |
| 1167 | line = ((char_u **)p->lines_to_source.ga_data)[p->lnum]; |
| 1168 | |
| 1169 | ga_init2(&ga, sizeof(char_u), 400); |
| 1170 | ga_concat(&ga, (char_u *)line); |
| 1171 | p->lnum++; |
| 1172 | |
| 1173 | if ((opts != GETLINE_NONE) && vim_strchr(p_cpo, CPO_CONCAT) == NULL) |
| 1174 | { |
| 1175 | while (p->lnum < p->lines_to_source.ga_len) |
| 1176 | { |
| 1177 | line = ((char_u **)p->lines_to_source.ga_data)[p->lnum]; |
| 1178 | if (!concat_continued_line(&ga, 400, line, opts)) |
| 1179 | break; |
| 1180 | p->sourcing_lnum++; |
| 1181 | p->lnum++; |
| 1182 | } |
| 1183 | } |
| 1184 | ga_append(&ga, NUL); |
| 1185 | p->sourcing_lnum++; |
| 1186 | |
| 1187 | return ga.ga_data; |
| 1188 | } |
| 1189 | |
| 1190 | /* |
| 1191 | * Source Ex commands from the lines in 'cookie'. |
| 1192 | */ |
| 1193 | static int |
| 1194 | do_sourcebuffer( |
| 1195 | void *cookie, |
| 1196 | char_u *scriptname) |
| 1197 | { |
| 1198 | char_u *save_sourcing_name = SOURCING_NAME; |
| 1199 | linenr_T save_sourcing_lnum = SOURCING_LNUM; |
| 1200 | char_u sourcing_name_buf[256]; |
| 1201 | sctx_T save_current_sctx; |
| 1202 | #ifdef FEAT_EVAL |
| 1203 | int sid; |
| 1204 | funccal_entry_T funccalp_entry; |
| 1205 | int save_estack_compiling = estack_compiling; |
| 1206 | scriptitem_T *si = NULL; |
| 1207 | #endif |
| 1208 | int save_sticky_cmdmod_flags = sticky_cmdmod_flags; |
| 1209 | int retval = FAIL; |
| 1210 | ESTACK_CHECK_DECLARATION |
| 1211 | |
| 1212 | if (save_sourcing_name == NULL) |
| 1213 | SOURCING_NAME = (char_u *)scriptname; |
| 1214 | else |
| 1215 | { |
| 1216 | vim_snprintf((char *)sourcing_name_buf, sizeof(sourcing_name_buf), |
| 1217 | "%s called at %s:%ld", scriptname, save_sourcing_name, |
| 1218 | save_sourcing_lnum); |
| 1219 | SOURCING_NAME = sourcing_name_buf; |
| 1220 | } |
| 1221 | SOURCING_LNUM = 0; |
| 1222 | |
| 1223 | // Keep the sourcing name/lnum, for recursive calls. |
| 1224 | estack_push(ETYPE_SCRIPT, scriptname, 0); |
| 1225 | ESTACK_CHECK_SETUP |
| 1226 | |
| 1227 | // "legacy" does not apply to commands in the script |
| 1228 | sticky_cmdmod_flags = 0; |
| 1229 | |
| 1230 | save_current_sctx = current_sctx; |
| 1231 | current_sctx.sc_version = 1; // default script version |
| 1232 | #ifdef FEAT_EVAL |
| 1233 | estack_compiling = FALSE; |
| 1234 | // Always use a new sequence number. |
| 1235 | current_sctx.sc_seq = ++last_current_SID_seq; |
| 1236 | current_sctx.sc_lnum = save_sourcing_lnum; |
| 1237 | save_funccal(&funccalp_entry); |
| 1238 | |
| 1239 | sid = find_script_by_name(scriptname); |
| 1240 | if (sid < 0) |
| 1241 | { |
| 1242 | int error = OK; |
| 1243 | |
| 1244 | // First time sourcing this buffer, create a new script item. |
| 1245 | |
| 1246 | sid = get_new_scriptitem(&error); |
| 1247 | if (error == FAIL) |
| 1248 | goto theend; |
| 1249 | current_sctx.sc_sid = sid; |
| 1250 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 1251 | si->sn_name = vim_strsave(scriptname); |
| 1252 | si->sn_state = SN_STATE_NEW; |
| 1253 | } |
| 1254 | else |
| 1255 | { |
| 1256 | // the buffer was sourced previously, reuse the script ID. |
| 1257 | current_sctx.sc_sid = sid; |
| 1258 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 1259 | si->sn_state = SN_STATE_RELOAD; |
| 1260 | } |
| 1261 | #endif |
| 1262 | |
| 1263 | retval = do_cmdline(NULL, source_getbufline, cookie, |
| 1264 | DOCMD_VERBOSE | DOCMD_NOWAIT | DOCMD_REPEAT); |
| 1265 | |
| 1266 | if (got_int) |
| 1267 | emsg(_(e_interrupted)); |
| 1268 | |
| 1269 | #ifdef FEAT_EVAL |
| 1270 | theend: |
| 1271 | #endif |
| 1272 | ESTACK_CHECK_NOW |
| 1273 | estack_pop(); |
| 1274 | current_sctx = save_current_sctx; |
| 1275 | SOURCING_LNUM = save_sourcing_lnum; |
| 1276 | SOURCING_NAME = save_sourcing_name; |
| 1277 | sticky_cmdmod_flags = save_sticky_cmdmod_flags; |
| 1278 | #ifdef FEAT_EVAL |
| 1279 | restore_funccal(); |
| 1280 | estack_compiling = save_estack_compiling; |
| 1281 | #endif |
| 1282 | |
| 1283 | return retval; |
| 1284 | } |
| 1285 | |
| 1286 | /* |
| 1287 | * :source Ex commands from the current buffer |
| 1288 | */ |
| 1289 | static void |
| 1290 | cmd_source_buffer(exarg_T *eap) |
| 1291 | { |
| 1292 | char_u *line = NULL; |
| 1293 | linenr_T curr_lnum; |
| 1294 | bufline_cookie_T cp; |
| 1295 | char_u sname[32]; |
| 1296 | |
| 1297 | if (curbuf == NULL) |
| 1298 | return; |
| 1299 | |
| 1300 | // Use ":source buffer=<num>" as the script name |
| 1301 | vim_snprintf((char *)sname, sizeof(sname), ":source buffer=%d", |
| 1302 | curbuf->b_fnum); |
| 1303 | |
| 1304 | ga_init2(&cp.lines_to_source, sizeof(char_u *), 100); |
| 1305 | |
| 1306 | // Copy the lines from the buffer into a grow array |
| 1307 | for (curr_lnum = eap->line1; curr_lnum <= eap->line2; curr_lnum++) |
| 1308 | { |
| 1309 | line = vim_strsave(ml_get(curr_lnum)); |
| 1310 | if (line == NULL) |
| 1311 | goto errret; |
| 1312 | if (ga_add_string(&cp.lines_to_source, line) == FAIL) |
| 1313 | goto errret; |
| 1314 | line = NULL; |
| 1315 | } |
| 1316 | cp.sourcing_lnum = 0; |
| 1317 | cp.lnum = 0; |
| 1318 | |
| 1319 | // Execute the Ex commands |
| 1320 | do_sourcebuffer((void *)&cp, (char_u *)sname); |
| 1321 | |
| 1322 | errret: |
| 1323 | vim_free(line); |
| 1324 | ga_clear_strings(&cp.lines_to_source); |
| 1325 | } |
| 1326 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1327 | static void |
| 1328 | cmd_source(char_u *fname, exarg_T *eap) |
| 1329 | { |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 1330 | if (*fname != NUL && eap != NULL && eap->addr_count > 0) |
| 1331 | { |
| 1332 | // if a filename is specified to :source, then a range is not allowed |
| 1333 | emsg(_(e_no_range_allowed)); |
| 1334 | return; |
| 1335 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1336 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 1337 | if (eap != NULL && *fname == NUL) |
| 1338 | { |
| 1339 | if (eap->forceit) |
| 1340 | // a file name is needed to source normal mode commands |
| 1341 | emsg(_(e_argument_required)); |
| 1342 | else |
| 1343 | // source ex commands from the current buffer |
| 1344 | cmd_source_buffer(eap); |
| 1345 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1346 | else if (eap != NULL && eap->forceit) |
| 1347 | // ":source!": read Normal mode commands |
| 1348 | // Need to execute the commands directly. This is required at least |
| 1349 | // for: |
| 1350 | // - ":g" command busy |
| 1351 | // - after ":argdo", ":windo" or ":bufdo" |
| 1352 | // - another command follows |
| 1353 | // - inside a loop |
| 1354 | openscript(fname, global_busy || listcmd_busy || eap->nextcmd != NULL |
| 1355 | #ifdef FEAT_EVAL |
| 1356 | || eap->cstack->cs_idx >= 0 |
| 1357 | #endif |
| 1358 | ); |
| 1359 | |
| 1360 | // ":source" read ex commands |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1361 | else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL) |
Bram Moolenaar | 460ae5d | 2022-01-01 14:19:49 +0000 | [diff] [blame] | 1362 | semsg(_(e_cant_open_file_str), fname); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | /* |
| 1366 | * ":source {fname}" |
| 1367 | */ |
| 1368 | void |
| 1369 | ex_source(exarg_T *eap) |
| 1370 | { |
| 1371 | #ifdef FEAT_BROWSE |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 1372 | if (cmdmod.cmod_flags & CMOD_BROWSE) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1373 | { |
| 1374 | char_u *fname = NULL; |
| 1375 | |
| 1376 | fname = do_browse(0, (char_u *)_("Source Vim script"), eap->arg, |
| 1377 | NULL, NULL, |
| 1378 | (char_u *)_(BROWSE_FILTER_MACROS), NULL); |
| 1379 | if (fname != NULL) |
| 1380 | { |
| 1381 | cmd_source(fname, eap); |
| 1382 | vim_free(fname); |
| 1383 | } |
| 1384 | } |
| 1385 | else |
| 1386 | #endif |
| 1387 | cmd_source(eap->arg, eap); |
| 1388 | } |
| 1389 | |
| 1390 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 1391 | /* |
| 1392 | * ":options" |
| 1393 | */ |
| 1394 | void |
| 1395 | ex_options( |
| 1396 | exarg_T *eap UNUSED) |
| 1397 | { |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1398 | char_u buf[500]; |
| 1399 | int multi_mods = 0; |
| 1400 | |
| 1401 | buf[0] = NUL; |
Bram Moolenaar | 02194d2 | 2020-10-24 23:08:38 +0200 | [diff] [blame] | 1402 | (void)add_win_cmd_modifers(buf, &cmdmod, &multi_mods); |
Bram Moolenaar | 7a1637f | 2020-04-13 21:16:21 +0200 | [diff] [blame] | 1403 | |
| 1404 | vim_setenv((char_u *)"OPTWIN_CMD", buf); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1405 | cmd_source((char_u *)SYS_OPTWIN_FILE, NULL); |
| 1406 | } |
| 1407 | #endif |
| 1408 | |
| 1409 | /* |
| 1410 | * ":source" and associated commands. |
| 1411 | */ |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1412 | |
| 1413 | #ifdef FEAT_EVAL |
| 1414 | /* |
| 1415 | * Return the address holding the next breakpoint line for a source cookie. |
| 1416 | */ |
| 1417 | linenr_T * |
| 1418 | source_breakpoint(void *cookie) |
| 1419 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1420 | return &((source_cookie_T *)cookie)->breakpoint; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1421 | } |
| 1422 | |
| 1423 | /* |
| 1424 | * Return the address holding the debug tick for a source cookie. |
| 1425 | */ |
| 1426 | int * |
| 1427 | source_dbg_tick(void *cookie) |
| 1428 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1429 | return &((source_cookie_T *)cookie)->dbg_tick; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1430 | } |
| 1431 | |
| 1432 | /* |
| 1433 | * Return the nesting level for a source cookie. |
| 1434 | */ |
| 1435 | int |
| 1436 | source_level(void *cookie) |
| 1437 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1438 | return ((source_cookie_T *)cookie)->level; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1439 | } |
Bram Moolenaar | 5409f5d | 2020-06-24 18:37:35 +0200 | [diff] [blame] | 1440 | |
| 1441 | /* |
Bram Moolenaar | aeb2bdd | 2020-08-18 22:32:03 +0200 | [diff] [blame] | 1442 | * Return the readahead line. Note that the pointer may become invalid when |
| 1443 | * getting the next line, if it's concatenated with the next one. |
Bram Moolenaar | 5409f5d | 2020-06-24 18:37:35 +0200 | [diff] [blame] | 1444 | */ |
| 1445 | char_u * |
| 1446 | source_nextline(void *cookie) |
| 1447 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1448 | return ((source_cookie_T *)cookie)->nextline; |
Bram Moolenaar | 5409f5d | 2020-06-24 18:37:35 +0200 | [diff] [blame] | 1449 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1450 | #endif |
| 1451 | |
| 1452 | #if (defined(MSWIN) && defined(FEAT_CSCOPE)) || defined(HAVE_FD_CLOEXEC) |
| 1453 | # define USE_FOPEN_NOINH |
| 1454 | /* |
| 1455 | * Special function to open a file without handle inheritance. |
| 1456 | * When possible the handle is closed on exec(). |
| 1457 | */ |
| 1458 | static FILE * |
| 1459 | fopen_noinh_readbin(char *filename) |
| 1460 | { |
| 1461 | # ifdef MSWIN |
| 1462 | int fd_tmp = mch_open(filename, O_RDONLY | O_BINARY | O_NOINHERIT, 0); |
| 1463 | # else |
| 1464 | int fd_tmp = mch_open(filename, O_RDONLY, 0); |
| 1465 | # endif |
| 1466 | |
| 1467 | if (fd_tmp == -1) |
| 1468 | return NULL; |
| 1469 | |
| 1470 | # ifdef HAVE_FD_CLOEXEC |
| 1471 | { |
| 1472 | int fdflags = fcntl(fd_tmp, F_GETFD); |
| 1473 | if (fdflags >= 0 && (fdflags & FD_CLOEXEC) == 0) |
| 1474 | (void)fcntl(fd_tmp, F_SETFD, fdflags | FD_CLOEXEC); |
| 1475 | } |
| 1476 | # endif |
| 1477 | |
| 1478 | return fdopen(fd_tmp, READBIN); |
| 1479 | } |
| 1480 | #endif |
| 1481 | |
| 1482 | /* |
| 1483 | * do_source: Read the file "fname" and execute its lines as EX commands. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1484 | * When "ret_sid" is not NULL and we loaded the script before, don't load it |
| 1485 | * again. |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1486 | * |
| 1487 | * This function may be called recursively! |
| 1488 | * |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1489 | * Return FAIL if file could not be opened, OK otherwise. |
| 1490 | * If a scriptitem_T was found or created "*ret_sid" is set to the SID. |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1491 | */ |
| 1492 | int |
| 1493 | do_source( |
| 1494 | char_u *fname, |
| 1495 | int check_other, // check for .vimrc and _vimrc |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1496 | int is_vimrc, // DOSO_ value |
| 1497 | int *ret_sid UNUSED) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1498 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 1499 | source_cookie_T cookie; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1500 | char_u *p; |
| 1501 | char_u *fname_exp; |
| 1502 | char_u *firstline = NULL; |
| 1503 | int retval = FAIL; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1504 | sctx_T save_current_sctx; |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 1505 | #ifdef FEAT_EVAL |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1506 | funccal_entry_T funccalp_entry; |
| 1507 | int save_debug_break_level = debug_break_level; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1508 | int sid; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1509 | scriptitem_T *si = NULL; |
Bram Moolenaar | 8de901e | 2021-06-11 22:21:24 +0200 | [diff] [blame] | 1510 | int save_estack_compiling = estack_compiling; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1511 | #endif |
| 1512 | #ifdef STARTUPTIME |
| 1513 | struct timeval tv_rel; |
| 1514 | struct timeval tv_start; |
| 1515 | #endif |
| 1516 | #ifdef FEAT_PROFILE |
| 1517 | proftime_T wait_start; |
| 1518 | #endif |
Bram Moolenaar | 2a9b62d | 2022-02-12 13:30:17 +0000 | [diff] [blame] | 1519 | int save_sticky_cmdmod_flags = sticky_cmdmod_flags; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1520 | int trigger_source_post = FALSE; |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 1521 | ESTACK_CHECK_DECLARATION |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1522 | |
| 1523 | p = expand_env_save(fname); |
| 1524 | if (p == NULL) |
| 1525 | return retval; |
| 1526 | fname_exp = fix_fname(p); |
| 1527 | vim_free(p); |
| 1528 | if (fname_exp == NULL) |
| 1529 | return retval; |
| 1530 | if (mch_isdir(fname_exp)) |
| 1531 | { |
| 1532 | smsg(_("Cannot source a directory: \"%s\""), fname); |
| 1533 | goto theend; |
| 1534 | } |
Bram Moolenaar | 8de901e | 2021-06-11 22:21:24 +0200 | [diff] [blame] | 1535 | #ifdef FEAT_EVAL |
Bram Moolenaar | f0a4069 | 2021-06-11 22:05:47 +0200 | [diff] [blame] | 1536 | estack_compiling = FALSE; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1537 | |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1538 | // See if we loaded this script before. |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1539 | sid = find_script_by_name(fname_exp); |
Bram Moolenaar | f479cac | 2022-01-12 12:54:55 +0000 | [diff] [blame] | 1540 | if (sid > 0 && ret_sid != NULL |
| 1541 | && SCRIPT_ITEM(sid)->sn_state != SN_STATE_NOT_LOADED) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1542 | { |
| 1543 | // Already loaded and no need to load again, return here. |
| 1544 | *ret_sid = sid; |
Bram Moolenaar | 292b90d | 2020-03-18 15:23:16 +0100 | [diff] [blame] | 1545 | retval = OK; |
| 1546 | goto theend; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1547 | } |
| 1548 | #endif |
| 1549 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1550 | // Apply SourceCmd autocommands, they should get the file and source it. |
| 1551 | if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL) |
| 1552 | && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp, |
| 1553 | FALSE, curbuf)) |
| 1554 | { |
| 1555 | #ifdef FEAT_EVAL |
| 1556 | retval = aborting() ? FAIL : OK; |
| 1557 | #else |
| 1558 | retval = OK; |
| 1559 | #endif |
| 1560 | if (retval == OK) |
| 1561 | // Apply SourcePost autocommands. |
| 1562 | apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, |
| 1563 | FALSE, curbuf); |
| 1564 | goto theend; |
| 1565 | } |
| 1566 | |
| 1567 | // Apply SourcePre autocommands, they may get the file. |
| 1568 | apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf); |
| 1569 | |
| 1570 | #ifdef USE_FOPEN_NOINH |
| 1571 | cookie.fp = fopen_noinh_readbin((char *)fname_exp); |
| 1572 | #else |
| 1573 | cookie.fp = mch_fopen((char *)fname_exp, READBIN); |
| 1574 | #endif |
| 1575 | if (cookie.fp == NULL && check_other) |
| 1576 | { |
| 1577 | // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa, |
| 1578 | // and ".exrc" by "_exrc" or vice versa. |
| 1579 | p = gettail(fname_exp); |
| 1580 | if ((*p == '.' || *p == '_') |
| 1581 | && (STRICMP(p + 1, "vimrc") == 0 |
| 1582 | || STRICMP(p + 1, "gvimrc") == 0 |
| 1583 | || STRICMP(p + 1, "exrc") == 0)) |
| 1584 | { |
| 1585 | if (*p == '_') |
| 1586 | *p = '.'; |
| 1587 | else |
| 1588 | *p = '_'; |
| 1589 | #ifdef USE_FOPEN_NOINH |
| 1590 | cookie.fp = fopen_noinh_readbin((char *)fname_exp); |
| 1591 | #else |
| 1592 | cookie.fp = mch_fopen((char *)fname_exp, READBIN); |
| 1593 | #endif |
| 1594 | } |
| 1595 | } |
| 1596 | |
| 1597 | if (cookie.fp == NULL) |
| 1598 | { |
| 1599 | if (p_verbose > 0) |
| 1600 | { |
| 1601 | verbose_enter(); |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1602 | if (SOURCING_NAME == NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1603 | smsg(_("could not source \"%s\""), fname); |
| 1604 | else |
| 1605 | smsg(_("line %ld: could not source \"%s\""), |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1606 | SOURCING_LNUM, fname); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1607 | verbose_leave(); |
| 1608 | } |
| 1609 | goto theend; |
| 1610 | } |
| 1611 | |
| 1612 | // The file exists. |
| 1613 | // - In verbose mode, give a message. |
| 1614 | // - For a vimrc file, may want to set 'compatible', call vimrc_found(). |
| 1615 | if (p_verbose > 1) |
| 1616 | { |
| 1617 | verbose_enter(); |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1618 | if (SOURCING_NAME == NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1619 | smsg(_("sourcing \"%s\""), fname); |
| 1620 | else |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1621 | smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1622 | verbose_leave(); |
| 1623 | } |
| 1624 | if (is_vimrc == DOSO_VIMRC) |
| 1625 | vimrc_found(fname_exp, (char_u *)"MYVIMRC"); |
| 1626 | else if (is_vimrc == DOSO_GVIMRC) |
| 1627 | vimrc_found(fname_exp, (char_u *)"MYGVIMRC"); |
| 1628 | |
| 1629 | #ifdef USE_CRNL |
| 1630 | // If no automatic file format: Set default to CR-NL. |
| 1631 | if (*p_ffs == NUL) |
| 1632 | cookie.fileformat = EOL_DOS; |
| 1633 | else |
| 1634 | cookie.fileformat = EOL_UNKNOWN; |
| 1635 | cookie.error = FALSE; |
| 1636 | #endif |
| 1637 | |
| 1638 | cookie.nextline = NULL; |
| 1639 | cookie.sourcing_lnum = 0; |
| 1640 | cookie.finished = FALSE; |
| 1641 | |
| 1642 | #ifdef FEAT_EVAL |
| 1643 | // Check if this script has a breakpoint. |
| 1644 | cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0); |
| 1645 | cookie.fname = fname_exp; |
| 1646 | cookie.dbg_tick = debug_tick; |
| 1647 | |
| 1648 | cookie.level = ex_nesting_level; |
| 1649 | #endif |
| 1650 | |
| 1651 | // Keep the sourcing name/lnum, for recursive calls. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1652 | estack_push(ETYPE_SCRIPT, fname_exp, 0); |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 1653 | ESTACK_CHECK_SETUP |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1654 | |
| 1655 | #ifdef STARTUPTIME |
| 1656 | if (time_fd != NULL) |
| 1657 | time_push(&tv_rel, &tv_start); |
| 1658 | #endif |
| 1659 | |
Bram Moolenaar | 2a9b62d | 2022-02-12 13:30:17 +0000 | [diff] [blame] | 1660 | // "legacy" does not apply to commands in the script |
| 1661 | sticky_cmdmod_flags = 0; |
| 1662 | |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 1663 | save_current_sctx = current_sctx; |
| 1664 | current_sctx.sc_version = 1; // default script version |
| 1665 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1666 | #ifdef FEAT_EVAL |
| 1667 | # ifdef FEAT_PROFILE |
| 1668 | if (do_profiling == PROF_YES) |
| 1669 | prof_child_enter(&wait_start); // entering a child now |
| 1670 | # endif |
| 1671 | |
| 1672 | // Don't use local function variables, if called from a function. |
| 1673 | // Also starts profiling timer for nested script. |
| 1674 | save_funccal(&funccalp_entry); |
| 1675 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1676 | current_sctx.sc_lnum = 0; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1677 | |
Dominique Pelle | af4a61a | 2021-12-27 17:21:41 +0000 | [diff] [blame] | 1678 | // Check if this script was sourced before to find its SID. |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1679 | // Always use a new sequence number. |
| 1680 | current_sctx.sc_seq = ++last_current_SID_seq; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1681 | if (sid > 0) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1682 | { |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1683 | hashtab_T *ht; |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 1684 | int todo; |
| 1685 | hashitem_T *hi; |
| 1686 | dictitem_T *di; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1687 | |
| 1688 | // loading the same script again |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1689 | current_sctx.sc_sid = sid; |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1690 | si = SCRIPT_ITEM(sid); |
| 1691 | if (si->sn_state == SN_STATE_NOT_LOADED) |
| 1692 | { |
| 1693 | // this script was found but not loaded yet |
| 1694 | si->sn_state = SN_STATE_NEW; |
| 1695 | } |
| 1696 | else |
| 1697 | { |
| 1698 | si->sn_state = SN_STATE_RELOAD; |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1699 | |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1700 | // Script-local variables remain but "const" can be set again. |
| 1701 | // In Vim9 script variables will be cleared when "vim9script" is |
| 1702 | // encountered without the "noclear" argument. |
| 1703 | ht = &SCRIPT_VARS(sid); |
| 1704 | todo = (int)ht->ht_used; |
| 1705 | for (hi = ht->ht_array; todo > 0; ++hi) |
| 1706 | if (!HASHITEM_EMPTY(hi)) |
| 1707 | { |
| 1708 | --todo; |
| 1709 | di = HI2DI(hi); |
| 1710 | di->di_flags |= DI_FLAGS_RELOAD; |
| 1711 | } |
| 1712 | // imports can be redefined once |
| 1713 | mark_imports_for_reload(sid); |
Bram Moolenaar | 0123cc1 | 2021-02-07 17:17:58 +0100 | [diff] [blame] | 1714 | |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1715 | // reset version, "vim9script" may have been added or removed. |
| 1716 | si->sn_version = 1; |
| 1717 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1718 | } |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1719 | else |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1720 | { |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1721 | int error = OK; |
Bram Moolenaar | 7ebcba6 | 2020-01-12 17:42:55 +0100 | [diff] [blame] | 1722 | |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 1723 | // It's new, generate a new SID and initialize the scriptitem. |
| 1724 | current_sctx.sc_sid = get_new_scriptitem(&error); |
| 1725 | if (error == FAIL) |
| 1726 | goto almosttheend; |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1727 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1728 | si->sn_name = fname_exp; |
| 1729 | fname_exp = vim_strsave(si->sn_name); // used for autocmd |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1730 | if (ret_sid != NULL) |
| 1731 | *ret_sid = current_sctx.sc_sid; |
Bram Moolenaar | 2b32700 | 2020-12-26 15:39:31 +0100 | [diff] [blame] | 1732 | |
Bram Moolenaar | 71eb3ad | 2021-12-26 12:07:30 +0000 | [diff] [blame] | 1733 | // Remember the "is_vimrc" flag for when the file is sourced again. |
| 1734 | si->sn_is_vimrc = is_vimrc; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1735 | } |
| 1736 | |
| 1737 | # ifdef FEAT_PROFILE |
| 1738 | if (do_profiling == PROF_YES) |
| 1739 | { |
| 1740 | int forceit; |
| 1741 | |
| 1742 | // Check if we do profiling for this script. |
| 1743 | if (!si->sn_prof_on && has_profiling(TRUE, si->sn_name, &forceit)) |
| 1744 | { |
| 1745 | script_do_profile(si); |
| 1746 | si->sn_pr_force = forceit; |
| 1747 | } |
| 1748 | if (si->sn_prof_on) |
| 1749 | { |
| 1750 | ++si->sn_pr_count; |
| 1751 | profile_start(&si->sn_pr_start); |
| 1752 | profile_zero(&si->sn_pr_children); |
| 1753 | } |
| 1754 | } |
| 1755 | # endif |
| 1756 | #endif |
| 1757 | |
| 1758 | cookie.conv.vc_type = CONV_NONE; // no conversion |
| 1759 | |
| 1760 | // Read the first line so we can check for a UTF-8 BOM. |
| 1761 | firstline = getsourceline(0, (void *)&cookie, 0, TRUE); |
| 1762 | if (firstline != NULL && STRLEN(firstline) >= 3 && firstline[0] == 0xef |
| 1763 | && firstline[1] == 0xbb && firstline[2] == 0xbf) |
| 1764 | { |
| 1765 | // Found BOM; setup conversion, skip over BOM and recode the line. |
| 1766 | convert_setup(&cookie.conv, (char_u *)"utf-8", p_enc); |
| 1767 | p = string_convert(&cookie.conv, firstline + 3, NULL); |
| 1768 | if (p == NULL) |
| 1769 | p = vim_strsave(firstline + 3); |
| 1770 | if (p != NULL) |
| 1771 | { |
| 1772 | vim_free(firstline); |
| 1773 | firstline = p; |
| 1774 | } |
| 1775 | } |
| 1776 | |
| 1777 | // Call do_cmdline, which will call getsourceline() to get the lines. |
| 1778 | do_cmdline(firstline, getsourceline, (void *)&cookie, |
| 1779 | DOCMD_VERBOSE|DOCMD_NOWAIT|DOCMD_REPEAT); |
| 1780 | retval = OK; |
| 1781 | |
| 1782 | #ifdef FEAT_PROFILE |
| 1783 | if (do_profiling == PROF_YES) |
| 1784 | { |
| 1785 | // Get "si" again, "script_items" may have been reallocated. |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1786 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1787 | if (si->sn_prof_on) |
| 1788 | { |
| 1789 | profile_end(&si->sn_pr_start); |
| 1790 | profile_sub_wait(&wait_start, &si->sn_pr_start); |
| 1791 | profile_add(&si->sn_pr_total, &si->sn_pr_start); |
| 1792 | profile_self(&si->sn_pr_self, &si->sn_pr_start, |
| 1793 | &si->sn_pr_children); |
| 1794 | } |
| 1795 | } |
| 1796 | #endif |
| 1797 | |
| 1798 | if (got_int) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1799 | emsg(_(e_interrupted)); |
Bram Moolenaar | e31ee86 | 2020-01-07 20:59:34 +0100 | [diff] [blame] | 1800 | ESTACK_CHECK_NOW |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1801 | estack_pop(); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1802 | if (p_verbose > 1) |
| 1803 | { |
| 1804 | verbose_enter(); |
| 1805 | smsg(_("finished sourcing %s"), fname); |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 1806 | if (SOURCING_NAME != NULL) |
| 1807 | smsg(_("continuing in %s"), SOURCING_NAME); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1808 | verbose_leave(); |
| 1809 | } |
| 1810 | #ifdef STARTUPTIME |
| 1811 | if (time_fd != NULL) |
| 1812 | { |
| 1813 | vim_snprintf((char *)IObuff, IOSIZE, "sourcing %s", fname); |
| 1814 | time_msg((char *)IObuff, &tv_start); |
| 1815 | time_pop(&tv_rel); |
| 1816 | } |
| 1817 | #endif |
| 1818 | |
| 1819 | if (!got_int) |
| 1820 | trigger_source_post = TRUE; |
| 1821 | |
| 1822 | #ifdef FEAT_EVAL |
| 1823 | // After a "finish" in debug mode, need to break at first command of next |
| 1824 | // sourced file. |
| 1825 | if (save_debug_break_level > ex_nesting_level |
| 1826 | && debug_break_level == ex_nesting_level) |
| 1827 | ++debug_break_level; |
| 1828 | #endif |
| 1829 | |
| 1830 | #ifdef FEAT_EVAL |
| 1831 | almosttheend: |
Bram Moolenaar | 71eb3ad | 2021-12-26 12:07:30 +0000 | [diff] [blame] | 1832 | // If "sn_save_cpo" is set that means we encountered "vim9script": restore |
| 1833 | // 'cpoptions', unless in the main .vimrc file. |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1834 | // Get "si" again, "script_items" may have been reallocated. |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1835 | si = SCRIPT_ITEM(current_sctx.sc_sid); |
Bram Moolenaar | 71eb3ad | 2021-12-26 12:07:30 +0000 | [diff] [blame] | 1836 | if (si->sn_save_cpo != NULL && si->sn_is_vimrc == DOSO_NONE) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1837 | { |
Bram Moolenaar | 3e19169 | 2021-03-17 17:46:00 +0100 | [diff] [blame] | 1838 | if (STRCMP(p_cpo, CPO_VIM) != 0) |
| 1839 | { |
| 1840 | char_u *f; |
| 1841 | char_u *t; |
| 1842 | |
| 1843 | // 'cpo' was changed in the script. Apply the same change to the |
| 1844 | // saved value, if possible. |
| 1845 | for (f = (char_u *)CPO_VIM; *f != NUL; ++f) |
| 1846 | if (vim_strchr(p_cpo, *f) == NULL |
| 1847 | && (t = vim_strchr(si->sn_save_cpo, *f)) != NULL) |
| 1848 | // flag was removed, also remove it from the saved 'cpo' |
| 1849 | mch_memmove(t, t + 1, STRLEN(t)); |
| 1850 | for (f = p_cpo; *f != NUL; ++f) |
| 1851 | if (vim_strchr((char_u *)CPO_VIM, *f) == NULL |
| 1852 | && vim_strchr(si->sn_save_cpo, *f) == NULL) |
| 1853 | { |
| 1854 | // flag was added, also add it to the saved 'cpo' |
| 1855 | t = alloc(STRLEN(si->sn_save_cpo) + 2); |
| 1856 | if (t != NULL) |
| 1857 | { |
| 1858 | *t = *f; |
| 1859 | STRCPY(t + 1, si->sn_save_cpo); |
| 1860 | vim_free(si->sn_save_cpo); |
| 1861 | si->sn_save_cpo = t; |
| 1862 | } |
| 1863 | } |
| 1864 | } |
Bram Moolenaar | 37294bd | 2021-03-10 13:40:08 +0100 | [diff] [blame] | 1865 | set_option_value((char_u *)"cpo", 0L, si->sn_save_cpo, OPT_NO_REDRAW); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1866 | } |
Bram Moolenaar | 71eb3ad | 2021-12-26 12:07:30 +0000 | [diff] [blame] | 1867 | VIM_CLEAR(si->sn_save_cpo); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1868 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1869 | restore_funccal(); |
| 1870 | # ifdef FEAT_PROFILE |
| 1871 | if (do_profiling == PROF_YES) |
| 1872 | prof_child_exit(&wait_start); // leaving a child now |
| 1873 | # endif |
| 1874 | #endif |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 1875 | current_sctx = save_current_sctx; |
| 1876 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1877 | fclose(cookie.fp); |
| 1878 | vim_free(cookie.nextline); |
| 1879 | vim_free(firstline); |
| 1880 | convert_setup(&cookie.conv, NULL, NULL); |
| 1881 | |
| 1882 | if (trigger_source_post) |
| 1883 | apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp, FALSE, curbuf); |
| 1884 | |
| 1885 | theend: |
| 1886 | vim_free(fname_exp); |
Bram Moolenaar | 2a9b62d | 2022-02-12 13:30:17 +0000 | [diff] [blame] | 1887 | sticky_cmdmod_flags = save_sticky_cmdmod_flags; |
Bram Moolenaar | 8de901e | 2021-06-11 22:21:24 +0200 | [diff] [blame] | 1888 | #ifdef FEAT_EVAL |
Bram Moolenaar | f0a4069 | 2021-06-11 22:05:47 +0200 | [diff] [blame] | 1889 | estack_compiling = save_estack_compiling; |
Bram Moolenaar | 8de901e | 2021-06-11 22:21:24 +0200 | [diff] [blame] | 1890 | #endif |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1891 | return retval; |
| 1892 | } |
| 1893 | |
| 1894 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 1895 | |
| 1896 | /* |
| 1897 | * ":scriptnames" |
| 1898 | */ |
| 1899 | void |
| 1900 | ex_scriptnames(exarg_T *eap) |
| 1901 | { |
| 1902 | int i; |
| 1903 | |
| 1904 | if (eap->addr_count > 0) |
| 1905 | { |
| 1906 | // :script {scriptId}: edit the script |
Bram Moolenaar | e3d4685 | 2020-08-29 13:39:17 +0200 | [diff] [blame] | 1907 | if (!SCRIPT_ID_VALID(eap->line2)) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 1908 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1909 | else |
| 1910 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1911 | eap->arg = SCRIPT_ITEM(eap->line2)->sn_name; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1912 | do_exedit(eap, NULL); |
| 1913 | } |
| 1914 | return; |
| 1915 | } |
| 1916 | |
| 1917 | for (i = 1; i <= script_items.ga_len && !got_int; ++i) |
Bram Moolenaar | 6079da7 | 2022-01-18 14:16:59 +0000 | [diff] [blame] | 1918 | { |
| 1919 | scriptitem_T *si = SCRIPT_ITEM(i); |
| 1920 | |
| 1921 | if (si->sn_name != NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1922 | { |
Bram Moolenaar | 6079da7 | 2022-01-18 14:16:59 +0000 | [diff] [blame] | 1923 | home_replace(NULL, si->sn_name, NameBuff, MAXPATHL, TRUE); |
| 1924 | vim_snprintf((char *)IObuff, IOSIZE, "%3d%s: %s", |
| 1925 | i, |
| 1926 | si->sn_state == SN_STATE_NOT_LOADED ? " A" : "", |
| 1927 | NameBuff); |
Bram Moolenaar | 769f589 | 2022-02-09 14:31:05 +0000 | [diff] [blame] | 1928 | if (!message_filtered(IObuff)) |
| 1929 | { |
| 1930 | msg_putchar('\n'); |
| 1931 | msg_outtrans(IObuff); |
| 1932 | out_flush(); // output one line at a time |
| 1933 | ui_breakcheck(); |
| 1934 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1935 | } |
Bram Moolenaar | 6079da7 | 2022-01-18 14:16:59 +0000 | [diff] [blame] | 1936 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1937 | } |
| 1938 | |
| 1939 | # if defined(BACKSLASH_IN_FILENAME) || defined(PROTO) |
| 1940 | /* |
| 1941 | * Fix slashes in the list of script names for 'shellslash'. |
| 1942 | */ |
| 1943 | void |
| 1944 | scriptnames_slash_adjust(void) |
| 1945 | { |
| 1946 | int i; |
| 1947 | |
| 1948 | for (i = 1; i <= script_items.ga_len; ++i) |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1949 | if (SCRIPT_ITEM(i)->sn_name != NULL) |
| 1950 | slash_adjust(SCRIPT_ITEM(i)->sn_name); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1951 | } |
| 1952 | # endif |
| 1953 | |
| 1954 | /* |
| 1955 | * Get a pointer to a script name. Used for ":verbose set". |
Bram Moolenaar | 7466706 | 2020-12-28 15:41:41 +0100 | [diff] [blame] | 1956 | * Message appended to "Last set from " |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1957 | */ |
| 1958 | char_u * |
| 1959 | get_scriptname(scid_T id) |
| 1960 | { |
| 1961 | if (id == SID_MODELINE) |
| 1962 | return (char_u *)_("modeline"); |
| 1963 | if (id == SID_CMDARG) |
| 1964 | return (char_u *)_("--cmd argument"); |
| 1965 | if (id == SID_CARG) |
| 1966 | return (char_u *)_("-c argument"); |
| 1967 | if (id == SID_ENV) |
| 1968 | return (char_u *)_("environment variable"); |
| 1969 | if (id == SID_ERROR) |
| 1970 | return (char_u *)_("error handler"); |
Bram Moolenaar | 7466706 | 2020-12-28 15:41:41 +0100 | [diff] [blame] | 1971 | if (id == SID_WINLAYOUT) |
| 1972 | return (char_u *)_("changed window size"); |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1973 | return SCRIPT_ITEM(id)->sn_name; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1974 | } |
| 1975 | |
| 1976 | # if defined(EXITFREE) || defined(PROTO) |
| 1977 | void |
| 1978 | free_scriptnames(void) |
| 1979 | { |
| 1980 | int i; |
| 1981 | |
| 1982 | for (i = script_items.ga_len; i > 0; --i) |
Bram Moolenaar | 8617348 | 2019-10-01 17:02:16 +0200 | [diff] [blame] | 1983 | { |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1984 | scriptitem_T *si = SCRIPT_ITEM(i); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 1985 | |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1986 | // the variables themselves are cleared in evalvars_clear() |
| 1987 | vim_free(si->sn_vars); |
| 1988 | |
| 1989 | vim_free(si->sn_name); |
Bram Moolenaar | 8d739de | 2020-10-14 19:39:19 +0200 | [diff] [blame] | 1990 | free_imports_and_script_vars(i); |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1991 | free_string_option(si->sn_save_cpo); |
Bram Moolenaar | a720be7 | 2019-10-22 21:45:19 +0200 | [diff] [blame] | 1992 | # ifdef FEAT_PROFILE |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1993 | ga_clear(&si->sn_prl_ga); |
Bram Moolenaar | a720be7 | 2019-10-22 21:45:19 +0200 | [diff] [blame] | 1994 | # endif |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 1995 | vim_free(si->sn_autoload_prefix); |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 1996 | vim_free(si); |
Bram Moolenaar | 8617348 | 2019-10-01 17:02:16 +0200 | [diff] [blame] | 1997 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 1998 | ga_clear(&script_items); |
| 1999 | } |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2000 | |
| 2001 | void |
| 2002 | free_autoload_scriptnames(void) |
| 2003 | { |
| 2004 | ga_clear_strings(&ga_loaded); |
| 2005 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2006 | # endif |
| 2007 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2008 | linenr_T |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 2009 | get_sourced_lnum( |
| 2010 | char_u *(*fgetline)(int, void *, int, getline_opt_T), |
| 2011 | void *cookie) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2012 | { |
| 2013 | return fgetline == getsourceline |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2014 | ? ((source_cookie_T *)cookie)->sourcing_lnum |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2015 | : SOURCING_LNUM; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2016 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 2017 | #endif |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2018 | |
| 2019 | static char_u * |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2020 | get_one_sourceline(source_cookie_T *sp) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2021 | { |
| 2022 | garray_T ga; |
| 2023 | int len; |
| 2024 | int c; |
| 2025 | char_u *buf; |
| 2026 | #ifdef USE_CRNL |
| 2027 | int has_cr; // CR-LF found |
| 2028 | #endif |
| 2029 | int have_read = FALSE; |
| 2030 | |
| 2031 | // use a growarray to store the sourced line |
| 2032 | ga_init2(&ga, 1, 250); |
| 2033 | |
| 2034 | // Loop until there is a finished line (or end-of-file). |
| 2035 | ++sp->sourcing_lnum; |
| 2036 | for (;;) |
| 2037 | { |
| 2038 | // make room to read at least 120 (more) characters |
| 2039 | if (ga_grow(&ga, 120) == FAIL) |
| 2040 | break; |
| 2041 | buf = (char_u *)ga.ga_data; |
| 2042 | |
| 2043 | if (fgets((char *)buf + ga.ga_len, ga.ga_maxlen - ga.ga_len, |
| 2044 | sp->fp) == NULL) |
| 2045 | break; |
| 2046 | len = ga.ga_len + (int)STRLEN(buf + ga.ga_len); |
| 2047 | #ifdef USE_CRNL |
| 2048 | // Ignore a trailing CTRL-Z, when in Dos mode. Only recognize the |
| 2049 | // CTRL-Z by its own, or after a NL. |
| 2050 | if ( (len == 1 || (len >= 2 && buf[len - 2] == '\n')) |
| 2051 | && sp->fileformat == EOL_DOS |
| 2052 | && buf[len - 1] == Ctrl_Z) |
| 2053 | { |
| 2054 | buf[len - 1] = NUL; |
| 2055 | break; |
| 2056 | } |
| 2057 | #endif |
| 2058 | |
| 2059 | have_read = TRUE; |
| 2060 | ga.ga_len = len; |
| 2061 | |
| 2062 | // If the line was longer than the buffer, read more. |
| 2063 | if (ga.ga_maxlen - ga.ga_len == 1 && buf[len - 1] != '\n') |
| 2064 | continue; |
| 2065 | |
| 2066 | if (len >= 1 && buf[len - 1] == '\n') // remove trailing NL |
| 2067 | { |
| 2068 | #ifdef USE_CRNL |
| 2069 | has_cr = (len >= 2 && buf[len - 2] == '\r'); |
| 2070 | if (sp->fileformat == EOL_UNKNOWN) |
| 2071 | { |
| 2072 | if (has_cr) |
| 2073 | sp->fileformat = EOL_DOS; |
| 2074 | else |
| 2075 | sp->fileformat = EOL_UNIX; |
| 2076 | } |
| 2077 | |
| 2078 | if (sp->fileformat == EOL_DOS) |
| 2079 | { |
| 2080 | if (has_cr) // replace trailing CR |
| 2081 | { |
| 2082 | buf[len - 2] = '\n'; |
| 2083 | --len; |
| 2084 | --ga.ga_len; |
| 2085 | } |
| 2086 | else // lines like ":map xx yy^M" will have failed |
| 2087 | { |
| 2088 | if (!sp->error) |
| 2089 | { |
| 2090 | msg_source(HL_ATTR(HLF_W)); |
| 2091 | emsg(_("W15: Warning: Wrong line separator, ^M may be missing")); |
| 2092 | } |
| 2093 | sp->error = TRUE; |
| 2094 | sp->fileformat = EOL_UNIX; |
| 2095 | } |
| 2096 | } |
| 2097 | #endif |
| 2098 | // The '\n' is escaped if there is an odd number of ^V's just |
| 2099 | // before it, first set "c" just before the 'V's and then check |
| 2100 | // len&c parities (is faster than ((len-c)%2 == 0)) -- Acevedo |
| 2101 | for (c = len - 2; c >= 0 && buf[c] == Ctrl_V; c--) |
| 2102 | ; |
| 2103 | if ((len & 1) != (c & 1)) // escaped NL, read more |
| 2104 | { |
| 2105 | ++sp->sourcing_lnum; |
| 2106 | continue; |
| 2107 | } |
| 2108 | |
| 2109 | buf[len - 1] = NUL; // remove the NL |
| 2110 | } |
| 2111 | |
| 2112 | // Check for ^C here now and then, so recursive :so can be broken. |
| 2113 | line_breakcheck(); |
| 2114 | break; |
| 2115 | } |
| 2116 | |
| 2117 | if (have_read) |
| 2118 | return (char_u *)ga.ga_data; |
| 2119 | |
| 2120 | vim_free(ga.ga_data); |
| 2121 | return NULL; |
| 2122 | } |
| 2123 | |
| 2124 | /* |
| 2125 | * Get one full line from a sourced file. |
| 2126 | * Called by do_cmdline() when it's called from do_source(). |
| 2127 | * |
| 2128 | * Return a pointer to the line in allocated memory. |
| 2129 | * Return NULL for end-of-file or some error. |
| 2130 | */ |
| 2131 | char_u * |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 2132 | getsourceline( |
| 2133 | int c UNUSED, |
| 2134 | void *cookie, |
| 2135 | int indent UNUSED, |
| 2136 | getline_opt_T options) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2137 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2138 | source_cookie_T *sp = (source_cookie_T *)cookie; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2139 | char_u *line; |
| 2140 | char_u *p; |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2141 | int do_vim9_all = in_vim9script() |
| 2142 | && options == GETLINE_CONCAT_ALL; |
Bram Moolenaar | 8242ebb | 2020-12-29 11:15:01 +0100 | [diff] [blame] | 2143 | int do_bar_cont = do_vim9_all |
| 2144 | || options == GETLINE_CONCAT_CONTBAR; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2145 | |
| 2146 | #ifdef FEAT_EVAL |
| 2147 | // If breakpoints have been added/deleted need to check for it. |
| 2148 | if (sp->dbg_tick < debug_tick) |
| 2149 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2150 | sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2151 | sp->dbg_tick = debug_tick; |
| 2152 | } |
| 2153 | # ifdef FEAT_PROFILE |
| 2154 | if (do_profiling == PROF_YES) |
| 2155 | script_line_end(); |
| 2156 | # endif |
| 2157 | #endif |
| 2158 | |
| 2159 | // Set the current sourcing line number. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2160 | SOURCING_LNUM = sp->sourcing_lnum + 1; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2161 | |
| 2162 | // Get current line. If there is a read-ahead line, use it, otherwise get |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2163 | // one now. "fp" is NULL if actually using a string. |
| 2164 | if (sp->finished || sp->fp == NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2165 | line = NULL; |
| 2166 | else if (sp->nextline == NULL) |
| 2167 | line = get_one_sourceline(sp); |
| 2168 | else |
| 2169 | { |
| 2170 | line = sp->nextline; |
| 2171 | sp->nextline = NULL; |
| 2172 | ++sp->sourcing_lnum; |
| 2173 | } |
| 2174 | #ifdef FEAT_PROFILE |
| 2175 | if (line != NULL && do_profiling == PROF_YES) |
| 2176 | script_line_start(); |
| 2177 | #endif |
| 2178 | |
| 2179 | // Only concatenate lines starting with a \ when 'cpoptions' doesn't |
| 2180 | // contain the 'C' flag. |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 2181 | if (line != NULL && options != GETLINE_NONE |
| 2182 | && vim_strchr(p_cpo, CPO_CONCAT) == NULL) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2183 | { |
Bram Moolenaar | 5072b47 | 2021-06-03 21:56:10 +0200 | [diff] [blame] | 2184 | int comment_char = in_vim9script() ? '#' : '"'; |
| 2185 | |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2186 | // compensate for the one line read-ahead |
| 2187 | --sp->sourcing_lnum; |
| 2188 | |
| 2189 | // Get the next line and concatenate it when it starts with a |
| 2190 | // backslash. We always need to read the next line, keep it in |
| 2191 | // sp->nextline. |
| 2192 | /* Also check for a comment in between continuation lines: "\ */ |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2193 | // Also check for a Vim9 comment, empty line, line starting with '|', |
| 2194 | // but not "||". |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2195 | sp->nextline = get_one_sourceline(sp); |
| 2196 | if (sp->nextline != NULL |
| 2197 | && (*(p = skipwhite(sp->nextline)) == '\\' |
Bram Moolenaar | 5072b47 | 2021-06-03 21:56:10 +0200 | [diff] [blame] | 2198 | || (p[0] == comment_char |
| 2199 | && p[1] == '\\' && p[2] == ' ') |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2200 | || (do_vim9_all && (*p == NUL |
| 2201 | || vim9_comment_start(p))) |
Bram Moolenaar | 8242ebb | 2020-12-29 11:15:01 +0100 | [diff] [blame] | 2202 | || (do_bar_cont && p[0] == '|' && p[1] != '|'))) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2203 | { |
| 2204 | garray_T ga; |
| 2205 | |
Bram Moolenaar | 04935fb | 2022-01-08 16:19:22 +0000 | [diff] [blame] | 2206 | ga_init2(&ga, sizeof(char_u), 400); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2207 | ga_concat(&ga, line); |
| 2208 | if (*p == '\\') |
| 2209 | ga_concat(&ga, p + 1); |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2210 | else if (*p == '|') |
| 2211 | { |
| 2212 | ga_concat(&ga, (char_u *)" "); |
| 2213 | ga_concat(&ga, p); |
| 2214 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2215 | for (;;) |
| 2216 | { |
| 2217 | vim_free(sp->nextline); |
| 2218 | sp->nextline = get_one_sourceline(sp); |
| 2219 | if (sp->nextline == NULL) |
| 2220 | break; |
| 2221 | p = skipwhite(sp->nextline); |
Bram Moolenaar | 8242ebb | 2020-12-29 11:15:01 +0100 | [diff] [blame] | 2222 | if (*p == '\\' || (do_bar_cont && p[0] == '|' && p[1] != '|')) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2223 | { |
| 2224 | // Adjust the growsize to the current length to speed up |
| 2225 | // concatenating many lines. |
| 2226 | if (ga.ga_len > 400) |
| 2227 | { |
| 2228 | if (ga.ga_len > 8000) |
| 2229 | ga.ga_growsize = 8000; |
| 2230 | else |
| 2231 | ga.ga_growsize = ga.ga_len; |
| 2232 | } |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2233 | if (*p == '\\') |
| 2234 | ga_concat(&ga, p + 1); |
| 2235 | else |
| 2236 | { |
| 2237 | ga_concat(&ga, (char_u *)" "); |
| 2238 | ga_concat(&ga, p); |
| 2239 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2240 | } |
Bram Moolenaar | 5072b47 | 2021-06-03 21:56:10 +0200 | [diff] [blame] | 2241 | else if (!(p[0] == (comment_char) |
Bram Moolenaar | 0f37e35 | 2021-06-02 15:28:15 +0200 | [diff] [blame] | 2242 | && p[1] == '\\' && p[2] == ' ') |
Bram Moolenaar | dcc58e0 | 2020-12-28 20:53:21 +0100 | [diff] [blame] | 2243 | && !(do_vim9_all && (*p == NUL || vim9_comment_start(p)))) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2244 | break; |
Bram Moolenaar | 75783bd | 2020-07-19 14:41:58 +0200 | [diff] [blame] | 2245 | /* drop a # comment or "\ comment line */ |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2246 | } |
| 2247 | ga_append(&ga, NUL); |
| 2248 | vim_free(line); |
| 2249 | line = ga.ga_data; |
| 2250 | } |
| 2251 | } |
| 2252 | |
| 2253 | if (line != NULL && sp->conv.vc_type != CONV_NONE) |
| 2254 | { |
| 2255 | char_u *s; |
| 2256 | |
| 2257 | // Convert the encoding of the script line. |
| 2258 | s = string_convert(&sp->conv, line, NULL); |
| 2259 | if (s != NULL) |
| 2260 | { |
| 2261 | vim_free(line); |
| 2262 | line = s; |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | #ifdef FEAT_EVAL |
| 2267 | // Did we encounter a breakpoint? |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2268 | if (sp->breakpoint != 0 && sp->breakpoint <= SOURCING_LNUM) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2269 | { |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2270 | dbg_breakpoint(sp->fname, SOURCING_LNUM); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2271 | // Find next breakpoint. |
Bram Moolenaar | 1a47ae3 | 2019-12-29 23:04:25 +0100 | [diff] [blame] | 2272 | sp->breakpoint = dbg_find_breakpoint(TRUE, sp->fname, SOURCING_LNUM); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2273 | sp->dbg_tick = debug_tick; |
| 2274 | } |
| 2275 | #endif |
| 2276 | |
| 2277 | return line; |
| 2278 | } |
| 2279 | |
| 2280 | /* |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 2281 | * Returns TRUE if sourcing a script either from a file or a buffer. |
| 2282 | * Otherwise returns FALSE. |
| 2283 | */ |
| 2284 | int |
| 2285 | sourcing_a_script(exarg_T *eap) |
| 2286 | { |
| 2287 | return (getline_equal(eap->getline, eap->cookie, getsourceline) |
| 2288 | || getline_equal(eap->getline, eap->cookie, source_getbufline)); |
| 2289 | } |
| 2290 | |
| 2291 | /* |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2292 | * ":scriptencoding": Set encoding conversion for a sourced script. |
| 2293 | */ |
| 2294 | void |
| 2295 | ex_scriptencoding(exarg_T *eap) |
| 2296 | { |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2297 | source_cookie_T *sp; |
| 2298 | char_u *name; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2299 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 2300 | if (!sourcing_a_script(eap)) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2301 | { |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 2302 | emsg(_(e_scriptencoding_used_outside_of_sourced_file)); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2303 | return; |
| 2304 | } |
| 2305 | |
| 2306 | if (*eap->arg != NUL) |
| 2307 | { |
| 2308 | name = enc_canonize(eap->arg); |
| 2309 | if (name == NULL) // out of memory |
| 2310 | return; |
| 2311 | } |
| 2312 | else |
| 2313 | name = eap->arg; |
| 2314 | |
| 2315 | // Setup for conversion from the specified encoding to 'encoding'. |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2316 | sp = (source_cookie_T *)getline_cookie(eap->getline, eap->cookie); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2317 | convert_setup(&sp->conv, name, p_enc); |
| 2318 | |
| 2319 | if (name != eap->arg) |
| 2320 | vim_free(name); |
| 2321 | } |
| 2322 | |
| 2323 | /* |
| 2324 | * ":scriptversion": Set Vim script version for a sourced script. |
| 2325 | */ |
| 2326 | void |
| 2327 | ex_scriptversion(exarg_T *eap UNUSED) |
| 2328 | { |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2329 | int nr; |
| 2330 | |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 2331 | if (!sourcing_a_script(eap)) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2332 | { |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 2333 | emsg(_(e_scriptversion_used_outside_of_sourced_file)); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2334 | return; |
| 2335 | } |
Bram Moolenaar | eb6880b | 2020-07-12 17:07:05 +0200 | [diff] [blame] | 2336 | if (in_vim9script()) |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2337 | { |
Bram Moolenaar | 451c2e3 | 2020-08-15 16:33:28 +0200 | [diff] [blame] | 2338 | emsg(_(e_cannot_use_scriptversion_after_vim9script)); |
Bram Moolenaar | 8a7d654 | 2020-01-26 15:56:19 +0100 | [diff] [blame] | 2339 | return; |
| 2340 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2341 | |
| 2342 | nr = getdigits(&eap->arg); |
| 2343 | if (nr == 0 || *eap->arg != NUL) |
Bram Moolenaar | 436b5ad | 2021-12-31 22:49:24 +0000 | [diff] [blame] | 2344 | emsg(_(e_invalid_argument)); |
Bram Moolenaar | 6797966 | 2020-06-20 22:50:47 +0200 | [diff] [blame] | 2345 | else if (nr > SCRIPT_VERSION_MAX) |
Bram Moolenaar | d82a47d | 2022-01-05 20:24:39 +0000 | [diff] [blame] | 2346 | semsg(_(e_scriptversion_not_supported_nr), nr); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2347 | else |
Bram Moolenaar | 7ebcba6 | 2020-01-12 17:42:55 +0100 | [diff] [blame] | 2348 | { |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2349 | current_sctx.sc_version = nr; |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 2350 | #ifdef FEAT_EVAL |
Bram Moolenaar | 21b9e97 | 2020-01-26 19:26:46 +0100 | [diff] [blame] | 2351 | SCRIPT_ITEM(current_sctx.sc_sid)->sn_version = nr; |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2352 | #endif |
Bram Moolenaar | 9b8d622 | 2020-12-28 18:26:00 +0100 | [diff] [blame] | 2353 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2354 | } |
| 2355 | |
| 2356 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 2357 | /* |
| 2358 | * ":finish": Mark a sourced file as finished. |
| 2359 | */ |
| 2360 | void |
| 2361 | ex_finish(exarg_T *eap) |
| 2362 | { |
Yegappan Lakshmanan | 36a5b68 | 2022-03-19 12:56:51 +0000 | [diff] [blame] | 2363 | if (sourcing_a_script(eap)) |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2364 | do_finish(eap, FALSE); |
| 2365 | else |
Bram Moolenaar | 1a99222 | 2021-12-31 17:25:48 +0000 | [diff] [blame] | 2366 | emsg(_(e_finish_used_outside_of_sourced_file)); |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | /* |
| 2370 | * Mark a sourced file as finished. Possibly makes the ":finish" pending. |
| 2371 | * Also called for a pending finish at the ":endtry" or after returning from |
| 2372 | * an extra do_cmdline(). "reanimate" is used in the latter case. |
| 2373 | */ |
| 2374 | void |
| 2375 | do_finish(exarg_T *eap, int reanimate) |
| 2376 | { |
| 2377 | int idx; |
| 2378 | |
| 2379 | if (reanimate) |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2380 | ((source_cookie_T *)getline_cookie(eap->getline, |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2381 | eap->cookie))->finished = FALSE; |
| 2382 | |
| 2383 | // Cleanup (and inactivate) conditionals, but stop when a try conditional |
| 2384 | // not in its finally clause (which then is to be executed next) is found. |
| 2385 | // In this case, make the ":finish" pending for execution at the ":endtry". |
| 2386 | // Otherwise, finish normally. |
| 2387 | idx = cleanup_conditionals(eap->cstack, 0, TRUE); |
| 2388 | if (idx >= 0) |
| 2389 | { |
| 2390 | eap->cstack->cs_pending[idx] = CSTP_FINISH; |
| 2391 | report_make_pending(CSTP_FINISH, NULL); |
| 2392 | } |
| 2393 | else |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2394 | ((source_cookie_T *)getline_cookie(eap->getline, |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2395 | eap->cookie))->finished = TRUE; |
| 2396 | } |
| 2397 | |
| 2398 | |
| 2399 | /* |
| 2400 | * Return TRUE when a sourced file had the ":finish" command: Don't give error |
| 2401 | * message for missing ":endif". |
| 2402 | * Return FALSE when not sourcing a file. |
| 2403 | */ |
| 2404 | int |
| 2405 | source_finished( |
Bram Moolenaar | 66250c9 | 2020-08-20 15:02:42 +0200 | [diff] [blame] | 2406 | char_u *(*fgetline)(int, void *, int, getline_opt_T), |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2407 | void *cookie) |
| 2408 | { |
| 2409 | return (getline_equal(fgetline, cookie, getsourceline) |
Bram Moolenaar | 9567efa | 2021-01-11 22:16:30 +0100 | [diff] [blame] | 2410 | && ((source_cookie_T *)getline_cookie( |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2411 | fgetline, cookie))->finished); |
| 2412 | } |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2413 | |
| 2414 | /* |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2415 | * Find the path of a script below the "autoload" directory. |
| 2416 | * Returns NULL if there is no "/autoload/" in the script name. |
| 2417 | */ |
| 2418 | char_u * |
| 2419 | script_name_after_autoload(scriptitem_T *si) |
| 2420 | { |
| 2421 | char_u *p = si->sn_name; |
| 2422 | char_u *res = NULL; |
| 2423 | |
| 2424 | for (;;) |
| 2425 | { |
| 2426 | char_u *n = (char_u *)strstr((char *)p, "autoload"); |
| 2427 | |
| 2428 | if (n == NULL) |
| 2429 | break; |
| 2430 | if (n > p && vim_ispathsep(n[-1]) && vim_ispathsep(n[8])) |
| 2431 | res = n + 9; |
| 2432 | p = n + 8; |
| 2433 | } |
| 2434 | return res; |
| 2435 | } |
| 2436 | |
| 2437 | /* |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2438 | * For an autoload script "autoload/dir/script.vim" return the prefix |
| 2439 | * "dir#script#" in allocated memory. |
| 2440 | * Returns NULL if anything is wrong. |
| 2441 | */ |
| 2442 | char_u * |
| 2443 | get_autoload_prefix(scriptitem_T *si) |
| 2444 | { |
| 2445 | char_u *p = script_name_after_autoload(si); |
| 2446 | char_u *prefix; |
| 2447 | |
| 2448 | if (p == NULL) |
| 2449 | return NULL; |
Bram Moolenaar | 3049fcf | 2022-01-13 19:25:50 +0000 | [diff] [blame] | 2450 | prefix = vim_strsave(p); |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2451 | if (prefix == NULL) |
| 2452 | return NULL; |
| 2453 | |
| 2454 | // replace all '/' with '#' and locate ".vim" at the end |
| 2455 | for (p = prefix; *p != NUL; p += mb_ptr2len(p)) |
| 2456 | { |
| 2457 | if (vim_ispathsep(*p)) |
| 2458 | *p = '#'; |
| 2459 | else if (STRCMP(p, ".vim") == 0) |
| 2460 | { |
| 2461 | p[0] = '#'; |
| 2462 | p[1] = NUL; |
| 2463 | return prefix; |
| 2464 | } |
| 2465 | } |
| 2466 | |
| 2467 | // did not find ".vim" at the end |
| 2468 | vim_free(prefix); |
| 2469 | return NULL; |
| 2470 | } |
| 2471 | |
| 2472 | /* |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2473 | * If in a Vim9 autoload script return "name" with the autoload prefix for the |
Bram Moolenaar | 9c7cae6 | 2022-01-20 19:10:25 +0000 | [diff] [blame] | 2474 | * script. If successful the returned name is allocated. |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2475 | * Otherwise it returns "name" unmodified. |
| 2476 | */ |
| 2477 | char_u * |
| 2478 | may_prefix_autoload(char_u *name) |
| 2479 | { |
| 2480 | if (SCRIPT_ID_VALID(current_sctx.sc_sid)) |
| 2481 | { |
| 2482 | scriptitem_T *si = SCRIPT_ITEM(current_sctx.sc_sid); |
| 2483 | |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2484 | if (si->sn_autoload_prefix != NULL) |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2485 | { |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2486 | char_u *basename = name; |
| 2487 | size_t len; |
| 2488 | char_u *res; |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2489 | |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2490 | if (*name == K_SPECIAL) |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2491 | { |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2492 | char_u *p = vim_strchr(name, '_'); |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2493 | |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2494 | // skip over "<SNR>99_" |
| 2495 | if (p != NULL) |
| 2496 | basename = p + 1; |
| 2497 | } |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2498 | |
Bram Moolenaar | fe2ef0b | 2022-01-10 18:08:00 +0000 | [diff] [blame] | 2499 | len = STRLEN(si->sn_autoload_prefix) + STRLEN(basename) + 2; |
| 2500 | res = alloc(len); |
| 2501 | if (res != NULL) |
| 2502 | { |
| 2503 | vim_snprintf((char *)res, len, "%s%s", |
| 2504 | si->sn_autoload_prefix, basename); |
| 2505 | return res; |
Bram Moolenaar | dc4451d | 2022-01-09 21:36:37 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
| 2508 | } |
| 2509 | return name; |
| 2510 | } |
| 2511 | |
| 2512 | /* |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2513 | * Return the autoload script name for a function or variable name. |
| 2514 | * Returns NULL when out of memory. |
| 2515 | * Caller must make sure that "name" contains AUTOLOAD_CHAR. |
| 2516 | */ |
| 2517 | char_u * |
| 2518 | autoload_name(char_u *name) |
| 2519 | { |
| 2520 | char_u *p, *q = NULL; |
| 2521 | char_u *scriptname; |
| 2522 | |
| 2523 | // Get the script file name: replace '#' with '/', append ".vim". |
| 2524 | scriptname = alloc(STRLEN(name) + 14); |
| 2525 | if (scriptname == NULL) |
| 2526 | return NULL; |
| 2527 | STRCPY(scriptname, "autoload/"); |
Bram Moolenaar | a177344 | 2020-08-12 15:21:22 +0200 | [diff] [blame] | 2528 | STRCAT(scriptname, name[0] == 'g' && name[1] == ':' ? name + 2: name); |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2529 | for (p = scriptname + 9; (p = vim_strchr(p, AUTOLOAD_CHAR)) != NULL; |
| 2530 | q = p, ++p) |
| 2531 | *p = '/'; |
| 2532 | STRCPY(q, ".vim"); |
| 2533 | return scriptname; |
| 2534 | } |
| 2535 | |
| 2536 | /* |
| 2537 | * If "name" has a package name try autoloading the script for it. |
| 2538 | * Return TRUE if a package was loaded. |
| 2539 | */ |
| 2540 | int |
| 2541 | script_autoload( |
| 2542 | char_u *name, |
| 2543 | int reload) // load script again when already loaded |
| 2544 | { |
| 2545 | char_u *p; |
| 2546 | char_u *scriptname, *tofree; |
| 2547 | int ret = FALSE; |
| 2548 | int i; |
Bram Moolenaar | daa2f36 | 2020-08-08 21:33:21 +0200 | [diff] [blame] | 2549 | int ret_sid; |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2550 | |
| 2551 | // If there is no '#' after name[0] there is no package name. |
| 2552 | p = vim_strchr(name, AUTOLOAD_CHAR); |
| 2553 | if (p == NULL || p == name) |
| 2554 | return FALSE; |
| 2555 | |
| 2556 | tofree = scriptname = autoload_name(name); |
| 2557 | if (scriptname == NULL) |
| 2558 | return FALSE; |
| 2559 | |
| 2560 | // Find the name in the list of previously loaded package names. Skip |
| 2561 | // "autoload/", it's always the same. |
| 2562 | for (i = 0; i < ga_loaded.ga_len; ++i) |
| 2563 | if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0) |
| 2564 | break; |
| 2565 | if (!reload && i < ga_loaded.ga_len) |
| 2566 | ret = FALSE; // was loaded already |
| 2567 | else |
| 2568 | { |
| 2569 | // Remember the name if it wasn't loaded already. |
| 2570 | if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK) |
| 2571 | { |
| 2572 | ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname; |
| 2573 | tofree = NULL; |
| 2574 | } |
| 2575 | |
| 2576 | // Try loading the package from $VIMRUNTIME/autoload/<name>.vim |
Bram Moolenaar | daa2f36 | 2020-08-08 21:33:21 +0200 | [diff] [blame] | 2577 | // Use "ret_sid" to avoid loading the same script again. |
=?UTF-8?q?Bj=C3=B6rn=20Linse?= | 223a950 | 2022-01-31 17:26:05 +0000 | [diff] [blame] | 2578 | if (source_in_path(p_rtp, scriptname, DIP_START, &ret_sid) == OK) |
Bram Moolenaar | da6c033 | 2019-09-01 16:01:30 +0200 | [diff] [blame] | 2579 | ret = TRUE; |
| 2580 | } |
| 2581 | |
| 2582 | vim_free(tofree); |
| 2583 | return ret; |
| 2584 | } |
Bram Moolenaar | 307c5a5 | 2019-08-25 15:41:00 +0200 | [diff] [blame] | 2585 | #endif |