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