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