comparison src/scriptfile.c @ 19181:94eda51ba9ba v8.2.0149

patch 8.2.0149: maintaining a Vim9 branch separately is more work Commit: https://github.com/vim/vim/commit/8a7d6542b33e5d2b352262305c3bfdb2d14e1cf8 Author: Bram Moolenaar <Bram@vim.org> Date: Sun Jan 26 15:56:19 2020 +0100 patch 8.2.0149: maintaining a Vim9 branch separately is more work Problem: Maintaining a Vim9 branch separately is more work. Solution: Merge the Vim9 script changes.
author Bram Moolenaar <Bram@vim.org>
date Sun, 26 Jan 2020 16:00:05 +0100
parents ea3ac1de7704
children 17d878a2ddaa
comparison
equal deleted inserted replaced
19180:8edf0aeb71b9 19181:94eda51ba9ba
180 180
181 source_runtime(arg, flags); 181 source_runtime(arg, flags);
182 } 182 }
183 183
184 static void 184 static void
185 source_callback(char_u *fname, void *cookie UNUSED) 185 source_callback(char_u *fname, void *cookie)
186 { 186 {
187 (void)do_source(fname, FALSE, DOSO_NONE); 187 (void)do_source(fname, FALSE, DOSO_NONE, cookie);
188 } 188 }
189 189
190 /* 190 /*
191 * Find the file "name" in all directories in "path" and invoke 191 * Find the file "name" in all directories in "path" and invoke
192 * "callback(fname, cookie)". 192 * "callback(fname, cookie)".
397 * return FAIL when no file could be sourced, OK otherwise. 397 * return FAIL when no file could be sourced, OK otherwise.
398 */ 398 */
399 int 399 int
400 source_runtime(char_u *name, int flags) 400 source_runtime(char_u *name, int flags)
401 { 401 {
402 return source_in_path(p_rtp, name, flags); 402 return source_in_path(p_rtp, name, flags, NULL);
403 } 403 }
404 404
405 /* 405 /*
406 * Just like source_runtime(), but use "path" instead of 'runtimepath'. 406 * Just like source_runtime(), but use "path" instead of 'runtimepath'.
407 */ 407 */
408 int 408 int
409 source_in_path(char_u *path, char_u *name, int flags) 409 source_in_path(char_u *path, char_u *name, int flags, int *ret_sid)
410 { 410 {
411 return do_in_path_and_pp(path, name, flags, source_callback, NULL); 411 return do_in_path_and_pp(path, name, flags, source_callback, ret_sid);
412 } 412 }
413 413
414 414
415 #if defined(FEAT_EVAL) || defined(PROTO) 415 #if defined(FEAT_EVAL) || defined(PROTO)
416 416
425 int i; 425 int i;
426 426
427 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK) 427 if (gen_expand_wildcards(1, &pat, &num_files, &files, EW_FILE) == OK)
428 { 428 {
429 for (i = 0; i < num_files; ++i) 429 for (i = 0; i < num_files; ++i)
430 (void)do_source(files[i], FALSE, DOSO_NONE); 430 (void)do_source(files[i], FALSE, DOSO_NONE, NULL);
431 FreeWild(num_files, files); 431 FreeWild(num_files, files);
432 } 432 }
433 } 433 }
434 434
435 /* 435 /*
928 || eap->cstack->cs_idx >= 0 928 || eap->cstack->cs_idx >= 0
929 #endif 929 #endif
930 ); 930 );
931 931
932 // ":source" read ex commands 932 // ":source" read ex commands
933 else if (do_source(fname, FALSE, DOSO_NONE) == FAIL) 933 else if (do_source(fname, FALSE, DOSO_NONE, NULL) == FAIL)
934 semsg(_(e_notopen), fname); 934 semsg(_(e_notopen), fname);
935 } 935 }
936 936
937 /* 937 /*
938 * ":source {fname}" 938 * ":source {fname}"
1061 } 1061 }
1062 #endif 1062 #endif
1063 1063
1064 /* 1064 /*
1065 * do_source: Read the file "fname" and execute its lines as EX commands. 1065 * do_source: Read the file "fname" and execute its lines as EX commands.
1066 * When "ret_sid" is not NULL and we loaded the script before, don't load it
1067 * again.
1066 * 1068 *
1067 * This function may be called recursively! 1069 * This function may be called recursively!
1068 * 1070 *
1069 * return FAIL if file could not be opened, OK otherwise 1071 * Return FAIL if file could not be opened, OK otherwise.
1072 * If a scriptitem_T was found or created "*ret_sid" is set to the SID.
1070 */ 1073 */
1071 int 1074 int
1072 do_source( 1075 do_source(
1073 char_u *fname, 1076 char_u *fname,
1074 int check_other, // check for .vimrc and _vimrc 1077 int check_other, // check for .vimrc and _vimrc
1075 int is_vimrc) // DOSO_ value 1078 int is_vimrc, // DOSO_ value
1079 int *ret_sid UNUSED)
1076 { 1080 {
1077 struct source_cookie cookie; 1081 struct source_cookie cookie;
1078 char_u *p; 1082 char_u *p;
1079 char_u *fname_exp; 1083 char_u *fname_exp;
1080 char_u *firstline = NULL; 1084 char_u *firstline = NULL;
1083 sctx_T save_current_sctx; 1087 sctx_T save_current_sctx;
1084 static scid_T last_current_SID = 0; 1088 static scid_T last_current_SID = 0;
1085 static int last_current_SID_seq = 0; 1089 static int last_current_SID_seq = 0;
1086 funccal_entry_T funccalp_entry; 1090 funccal_entry_T funccalp_entry;
1087 int save_debug_break_level = debug_break_level; 1091 int save_debug_break_level = debug_break_level;
1092 int sid;
1088 scriptitem_T *si = NULL; 1093 scriptitem_T *si = NULL;
1089 # ifdef UNIX 1094 # ifdef UNIX
1090 stat_T st; 1095 stat_T st;
1091 int stat_ok; 1096 int stat_ok;
1092 # endif 1097 # endif
1112 { 1117 {
1113 smsg(_("Cannot source a directory: \"%s\""), fname); 1118 smsg(_("Cannot source a directory: \"%s\""), fname);
1114 goto theend; 1119 goto theend;
1115 } 1120 }
1116 1121
1117 // Apply SourceCmd autocommands, they should get the file and source it.
1118 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1119 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1120 FALSE, curbuf))
1121 {
1122 #ifdef FEAT_EVAL 1122 #ifdef FEAT_EVAL
1123 retval = aborting() ? FAIL : OK; 1123 // See if we loaded this script before.
1124 #else
1125 retval = OK;
1126 #endif
1127 if (retval == OK)
1128 // Apply SourcePost autocommands.
1129 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1130 FALSE, curbuf);
1131 goto theend;
1132 }
1133
1134 // Apply SourcePre autocommands, they may get the file.
1135 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1136
1137 #ifdef USE_FOPEN_NOINH
1138 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1139 #else
1140 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1141 #endif
1142 if (cookie.fp == NULL && check_other)
1143 {
1144 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1145 // and ".exrc" by "_exrc" or vice versa.
1146 p = gettail(fname_exp);
1147 if ((*p == '.' || *p == '_')
1148 && (STRICMP(p + 1, "vimrc") == 0
1149 || STRICMP(p + 1, "gvimrc") == 0
1150 || STRICMP(p + 1, "exrc") == 0))
1151 {
1152 if (*p == '_')
1153 *p = '.';
1154 else
1155 *p = '_';
1156 #ifdef USE_FOPEN_NOINH
1157 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1158 #else
1159 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1160 #endif
1161 }
1162 }
1163
1164 if (cookie.fp == NULL)
1165 {
1166 if (p_verbose > 0)
1167 {
1168 verbose_enter();
1169 if (SOURCING_NAME == NULL)
1170 smsg(_("could not source \"%s\""), fname);
1171 else
1172 smsg(_("line %ld: could not source \"%s\""),
1173 SOURCING_LNUM, fname);
1174 verbose_leave();
1175 }
1176 goto theend;
1177 }
1178
1179 // The file exists.
1180 // - In verbose mode, give a message.
1181 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1182 if (p_verbose > 1)
1183 {
1184 verbose_enter();
1185 if (SOURCING_NAME == NULL)
1186 smsg(_("sourcing \"%s\""), fname);
1187 else
1188 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
1189 verbose_leave();
1190 }
1191 if (is_vimrc == DOSO_VIMRC)
1192 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1193 else if (is_vimrc == DOSO_GVIMRC)
1194 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1195
1196 #ifdef USE_CRNL
1197 // If no automatic file format: Set default to CR-NL.
1198 if (*p_ffs == NUL)
1199 cookie.fileformat = EOL_DOS;
1200 else
1201 cookie.fileformat = EOL_UNKNOWN;
1202 cookie.error = FALSE;
1203 #endif
1204
1205 cookie.nextline = NULL;
1206 cookie.sourcing_lnum = 0;
1207 cookie.finished = FALSE;
1208
1209 #ifdef FEAT_EVAL
1210 // Check if this script has a breakpoint.
1211 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1212 cookie.fname = fname_exp;
1213 cookie.dbg_tick = debug_tick;
1214
1215 cookie.level = ex_nesting_level;
1216 #endif
1217
1218 // Keep the sourcing name/lnum, for recursive calls.
1219 estack_push(ETYPE_SCRIPT, fname_exp, 0);
1220 ESTACK_CHECK_SETUP
1221
1222 #ifdef STARTUPTIME
1223 if (time_fd != NULL)
1224 time_push(&tv_rel, &tv_start);
1225 #endif
1226
1227 #ifdef FEAT_EVAL
1228 # ifdef FEAT_PROFILE
1229 if (do_profiling == PROF_YES)
1230 prof_child_enter(&wait_start); // entering a child now
1231 # endif
1232
1233 // Don't use local function variables, if called from a function.
1234 // Also starts profiling timer for nested script.
1235 save_funccal(&funccalp_entry);
1236
1237 save_current_sctx = current_sctx;
1238 current_sctx.sc_lnum = 0;
1239 current_sctx.sc_version = 1; // default script version
1240
1241 // Check if this script was sourced before to finds its SID.
1242 // If it's new, generate a new SID.
1243 // Always use a new sequence number.
1244 current_sctx.sc_seq = ++last_current_SID_seq;
1245 # ifdef UNIX 1124 # ifdef UNIX
1246 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0); 1125 stat_ok = (mch_stat((char *)fname_exp, &st) >= 0);
1247 # endif 1126 # endif
1248 for (current_sctx.sc_sid = script_items.ga_len; current_sctx.sc_sid > 0; 1127 for (sid = script_items.ga_len; sid > 0; --sid)
1249 --current_sctx.sc_sid) 1128 {
1250 { 1129 si = &SCRIPT_ITEM(sid);
1251 si = &SCRIPT_ITEM(current_sctx.sc_sid);
1252 if (si->sn_name != NULL) 1130 if (si->sn_name != NULL)
1253 { 1131 {
1254 # ifdef UNIX 1132 # ifdef UNIX
1255 // Compare dev/ino when possible, it catches symbolic links. Also 1133 // Compare dev/ino when possible, it catches symbolic links. Also
1256 // compare file names, the inode may change when the file was 1134 // compare file names, the inode may change when the file was
1262 if (fnamecmp(si->sn_name, fname_exp) == 0) 1140 if (fnamecmp(si->sn_name, fname_exp) == 0)
1263 // Found it! 1141 // Found it!
1264 break; 1142 break;
1265 } 1143 }
1266 } 1144 }
1267 if (current_sctx.sc_sid == 0) 1145 if (sid > 0 && ret_sid != NULL)
1268 { 1146 {
1147 // Already loaded and no need to load again, return here.
1148 *ret_sid = sid;
1149 return OK;
1150 }
1151 #endif
1152
1153 // Apply SourceCmd autocommands, they should get the file and source it.
1154 if (has_autocmd(EVENT_SOURCECMD, fname_exp, NULL)
1155 && apply_autocmds(EVENT_SOURCECMD, fname_exp, fname_exp,
1156 FALSE, curbuf))
1157 {
1158 #ifdef FEAT_EVAL
1159 retval = aborting() ? FAIL : OK;
1160 #else
1161 retval = OK;
1162 #endif
1163 if (retval == OK)
1164 // Apply SourcePost autocommands.
1165 apply_autocmds(EVENT_SOURCEPOST, fname_exp, fname_exp,
1166 FALSE, curbuf);
1167 goto theend;
1168 }
1169
1170 // Apply SourcePre autocommands, they may get the file.
1171 apply_autocmds(EVENT_SOURCEPRE, fname_exp, fname_exp, FALSE, curbuf);
1172
1173 #ifdef USE_FOPEN_NOINH
1174 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1175 #else
1176 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1177 #endif
1178 if (cookie.fp == NULL && check_other)
1179 {
1180 // Try again, replacing file name ".vimrc" by "_vimrc" or vice versa,
1181 // and ".exrc" by "_exrc" or vice versa.
1182 p = gettail(fname_exp);
1183 if ((*p == '.' || *p == '_')
1184 && (STRICMP(p + 1, "vimrc") == 0
1185 || STRICMP(p + 1, "gvimrc") == 0
1186 || STRICMP(p + 1, "exrc") == 0))
1187 {
1188 if (*p == '_')
1189 *p = '.';
1190 else
1191 *p = '_';
1192 #ifdef USE_FOPEN_NOINH
1193 cookie.fp = fopen_noinh_readbin((char *)fname_exp);
1194 #else
1195 cookie.fp = mch_fopen((char *)fname_exp, READBIN);
1196 #endif
1197 }
1198 }
1199
1200 if (cookie.fp == NULL)
1201 {
1202 if (p_verbose > 0)
1203 {
1204 verbose_enter();
1205 if (SOURCING_NAME == NULL)
1206 smsg(_("could not source \"%s\""), fname);
1207 else
1208 smsg(_("line %ld: could not source \"%s\""),
1209 SOURCING_LNUM, fname);
1210 verbose_leave();
1211 }
1212 goto theend;
1213 }
1214
1215 // The file exists.
1216 // - In verbose mode, give a message.
1217 // - For a vimrc file, may want to set 'compatible', call vimrc_found().
1218 if (p_verbose > 1)
1219 {
1220 verbose_enter();
1221 if (SOURCING_NAME == NULL)
1222 smsg(_("sourcing \"%s\""), fname);
1223 else
1224 smsg(_("line %ld: sourcing \"%s\""), SOURCING_LNUM, fname);
1225 verbose_leave();
1226 }
1227 if (is_vimrc == DOSO_VIMRC)
1228 vimrc_found(fname_exp, (char_u *)"MYVIMRC");
1229 else if (is_vimrc == DOSO_GVIMRC)
1230 vimrc_found(fname_exp, (char_u *)"MYGVIMRC");
1231
1232 #ifdef USE_CRNL
1233 // If no automatic file format: Set default to CR-NL.
1234 if (*p_ffs == NUL)
1235 cookie.fileformat = EOL_DOS;
1236 else
1237 cookie.fileformat = EOL_UNKNOWN;
1238 cookie.error = FALSE;
1239 #endif
1240
1241 cookie.nextline = NULL;
1242 cookie.sourcing_lnum = 0;
1243 cookie.finished = FALSE;
1244
1245 #ifdef FEAT_EVAL
1246 // Check if this script has a breakpoint.
1247 cookie.breakpoint = dbg_find_breakpoint(TRUE, fname_exp, (linenr_T)0);
1248 cookie.fname = fname_exp;
1249 cookie.dbg_tick = debug_tick;
1250
1251 cookie.level = ex_nesting_level;
1252 #endif
1253
1254 // Keep the sourcing name/lnum, for recursive calls.
1255 estack_push(ETYPE_SCRIPT, fname_exp, 0);
1256 ESTACK_CHECK_SETUP
1257
1258 #ifdef STARTUPTIME
1259 if (time_fd != NULL)
1260 time_push(&tv_rel, &tv_start);
1261 #endif
1262
1263 #ifdef FEAT_EVAL
1264 # ifdef FEAT_PROFILE
1265 if (do_profiling == PROF_YES)
1266 prof_child_enter(&wait_start); // entering a child now
1267 # endif
1268
1269 // Don't use local function variables, if called from a function.
1270 // Also starts profiling timer for nested script.
1271 save_funccal(&funccalp_entry);
1272
1273 save_current_sctx = current_sctx;
1274 current_sctx.sc_lnum = 0;
1275 current_sctx.sc_version = 1; // default script version
1276
1277 // Check if this script was sourced before to finds its SID.
1278 // Always use a new sequence number.
1279 current_sctx.sc_seq = ++last_current_SID_seq;
1280 if (sid > 0)
1281 {
1282 hashtab_T *ht;
1283 hashitem_T *hi;
1284 dictitem_T *di;
1285 int todo;
1286
1287 // loading the same script again
1288 si->sn_had_command = FALSE;
1289 current_sctx.sc_sid = sid;
1290
1291 ht = &SCRIPT_VARS(sid);
1292 todo = (int)ht->ht_used;
1293 for (hi = ht->ht_array; todo > 0; ++hi)
1294 if (!HASHITEM_EMPTY(hi))
1295 {
1296 --todo;
1297 di = HI2DI(hi);
1298 di->di_flags |= DI_FLAGS_RELOAD;
1299 }
1300 }
1301 else
1302 {
1303 // It's new, generate a new SID.
1269 current_sctx.sc_sid = ++last_current_SID; 1304 current_sctx.sc_sid = ++last_current_SID;
1270 if (ga_grow(&script_items, 1305 if (ga_grow(&script_items,
1271 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL) 1306 (int)(current_sctx.sc_sid - script_items.ga_len)) == FAIL)
1272 goto almosttheend; 1307 goto almosttheend;
1273 while (script_items.ga_len < current_sctx.sc_sid) 1308 while (script_items.ga_len < current_sctx.sc_sid)
1274 { 1309 {
1275 ++script_items.ga_len; 1310 ++script_items.ga_len;
1276 SCRIPT_ITEM(script_items.ga_len).sn_name = NULL; 1311 si = &SCRIPT_ITEM(script_items.ga_len);
1277 SCRIPT_ITEM(script_items.ga_len).sn_version = 1; 1312 si->sn_name = NULL;
1313 si->sn_version = 1;
1278 1314
1279 // Allocate the local script variables to use for this script. 1315 // Allocate the local script variables to use for this script.
1280 new_script_vars(script_items.ga_len); 1316 new_script_vars(script_items.ga_len);
1317 ga_init2(&si->sn_var_vals, sizeof(typval_T), 10);
1318 ga_init2(&si->sn_imports, sizeof(imported_T), 10);
1319 ga_init2(&si->sn_type_list, sizeof(type_T), 10);
1281 # ifdef FEAT_PROFILE 1320 # ifdef FEAT_PROFILE
1282 SCRIPT_ITEM(script_items.ga_len).sn_prof_on = FALSE; 1321 si->sn_prof_on = FALSE;
1283 # endif 1322 # endif
1284 } 1323 }
1285 si = &SCRIPT_ITEM(current_sctx.sc_sid); 1324 si = &SCRIPT_ITEM(current_sctx.sc_sid);
1286 si->sn_name = fname_exp; 1325 si->sn_name = fname_exp;
1287 fname_exp = vim_strsave(si->sn_name); // used for autocmd 1326 fname_exp = vim_strsave(si->sn_name); // used for autocmd
1293 si->sn_ino = st.st_ino; 1332 si->sn_ino = st.st_ino;
1294 } 1333 }
1295 else 1334 else
1296 si->sn_dev_valid = FALSE; 1335 si->sn_dev_valid = FALSE;
1297 # endif 1336 # endif
1337 if (ret_sid != NULL)
1338 *ret_sid = current_sctx.sc_sid;
1298 } 1339 }
1299 1340
1300 # ifdef FEAT_PROFILE 1341 # ifdef FEAT_PROFILE
1301 if (do_profiling == PROF_YES) 1342 if (do_profiling == PROF_YES)
1302 { 1343 {
1390 ++debug_break_level; 1431 ++debug_break_level;
1391 #endif 1432 #endif
1392 1433
1393 #ifdef FEAT_EVAL 1434 #ifdef FEAT_EVAL
1394 almosttheend: 1435 almosttheend:
1436 // Get "si" again, "script_items" may have been reallocated.
1437 si = &SCRIPT_ITEM(current_sctx.sc_sid);
1438 if (si->sn_save_cpo != NULL)
1439 {
1440 free_string_option(p_cpo);
1441 p_cpo = si->sn_save_cpo;
1442 si->sn_save_cpo = NULL;
1443 }
1444
1395 current_sctx = save_current_sctx; 1445 current_sctx = save_current_sctx;
1396 restore_funccal(); 1446 restore_funccal();
1397 # ifdef FEAT_PROFILE 1447 # ifdef FEAT_PROFILE
1398 if (do_profiling == PROF_YES) 1448 if (do_profiling == PROF_YES)
1399 prof_child_exit(&wait_start); // leaving a child now 1449 prof_child_exit(&wait_start); // leaving a child now
1486 1536
1487 for (i = script_items.ga_len; i > 0; --i) 1537 for (i = script_items.ga_len; i > 0; --i)
1488 { 1538 {
1489 // the variables themselves are cleared in evalvars_clear() 1539 // the variables themselves are cleared in evalvars_clear()
1490 vim_free(SCRIPT_ITEM(i).sn_vars); 1540 vim_free(SCRIPT_ITEM(i).sn_vars);
1541
1491 vim_free(SCRIPT_ITEM(i).sn_name); 1542 vim_free(SCRIPT_ITEM(i).sn_name);
1543 free_string_option(SCRIPT_ITEM(i).sn_save_cpo);
1492 # ifdef FEAT_PROFILE 1544 # ifdef FEAT_PROFILE
1493 ga_clear(&SCRIPT_ITEM(i).sn_prl_ga); 1545 ga_clear(&SCRIPT_ITEM(i).sn_prl_ga);
1494 # endif 1546 # endif
1495 } 1547 }
1496 ga_clear(&script_items); 1548 ga_clear(&script_items);
1787 if (!getline_equal(eap->getline, eap->cookie, getsourceline)) 1839 if (!getline_equal(eap->getline, eap->cookie, getsourceline))
1788 { 1840 {
1789 emsg(_("E984: :scriptversion used outside of a sourced file")); 1841 emsg(_("E984: :scriptversion used outside of a sourced file"));
1790 return; 1842 return;
1791 } 1843 }
1844 if (current_sctx.sc_version == SCRIPT_VERSION_VIM9)
1845 {
1846 emsg(_("E1040: Cannot use :scriptversion after :vim9script"));
1847 return;
1848 }
1792 1849
1793 nr = getdigits(&eap->arg); 1850 nr = getdigits(&eap->arg);
1794 if (nr == 0 || *eap->arg != NUL) 1851 if (nr == 0 || *eap->arg != NUL)
1795 emsg(_(e_invarg)); 1852 emsg(_(e_invarg));
1796 else if (nr > 4) 1853 else if (nr > 4)