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