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