comparison src/ex_cmds.c @ 13380:69517d67421f v8.0.1564

patch 8.0.1564: too many #ifdefs commit https://github.com/vim/vim/commit/f2bd8ef2b4507d02c6043affff8f7e85e3414d5f Author: Bram Moolenaar <Bram@vim.org> Date: Sun Mar 4 18:08:14 2018 +0100 patch 8.0.1564: too many #ifdefs Problem: Too many #ifdefs. Solution: Graduate the +autocmd feature. Takes away 450 #ifdefs and increases code size of tiny Vim by only 40 Kbyte.
author Christian Brabandt <cb@256bit.org>
date Sun, 04 Mar 2018 18:15:08 +0100
parents 81c348d40312
children 6740c499de13
comparison
equal deleted inserted replaced
13379:0f9dd1b43244 13380:69517d67421f
26 static int viminfo_encoding(vir_T *virp); 26 static int viminfo_encoding(vir_T *virp);
27 static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing); 27 static int read_viminfo_up_to_marks(vir_T *virp, int forceit, int writing);
28 #endif 28 #endif
29 29
30 static int check_readonly(int *forceit, buf_T *buf); 30 static int check_readonly(int *forceit, buf_T *buf);
31 #ifdef FEAT_AUTOCMD
32 static void delbuf_msg(char_u *name); 31 static void delbuf_msg(char_u *name);
33 #endif
34 static int 32 static int
35 #ifdef __BORLANDC__ 33 #ifdef __BORLANDC__
36 _RTLENTRYF 34 _RTLENTRYF
37 #endif 35 #endif
38 help_compare(const void *s1, const void *s2); 36 help_compare(const void *s1, const void *s2);
1146 else /* :range! */ 1144 else /* :range! */
1147 { 1145 {
1148 /* Careful: This may recursively call do_bang() again! (because of 1146 /* Careful: This may recursively call do_bang() again! (because of
1149 * autocommands) */ 1147 * autocommands) */
1150 do_filter(line1, line2, eap, newcmd, do_in, do_out); 1148 do_filter(line1, line2, eap, newcmd, do_in, do_out);
1151 #ifdef FEAT_AUTOCMD
1152 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf); 1149 apply_autocmds(EVENT_SHELLFILTERPOST, NULL, NULL, FALSE, curbuf);
1153 #endif
1154 } 1150 }
1155 if (free_newcmd) 1151 if (free_newcmd)
1156 vim_free(newcmd); 1152 vim_free(newcmd);
1157 } 1153 }
1158 1154
1184 char_u *otmp = NULL; 1180 char_u *otmp = NULL;
1185 linenr_T linecount; 1181 linenr_T linecount;
1186 linenr_T read_linecount; 1182 linenr_T read_linecount;
1187 pos_T cursor_save; 1183 pos_T cursor_save;
1188 char_u *cmd_buf; 1184 char_u *cmd_buf;
1189 #ifdef FEAT_AUTOCMD
1190 buf_T *old_curbuf = curbuf; 1185 buf_T *old_curbuf = curbuf;
1191 #endif
1192 int shell_flags = 0; 1186 int shell_flags = 0;
1193 1187
1194 if (*cmd == NUL) /* no filter command */ 1188 if (*cmd == NUL) /* no filter command */
1195 return; 1189 return;
1196 1190
1257 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap, 1251 if (itmp != NULL && buf_write(curbuf, itmp, NULL, line1, line2, eap,
1258 FALSE, FALSE, FALSE, TRUE) == FAIL) 1252 FALSE, FALSE, FALSE, TRUE) == FAIL)
1259 { 1253 {
1260 msg_putchar('\n'); /* keep message from buf_write() */ 1254 msg_putchar('\n'); /* keep message from buf_write() */
1261 --no_wait_return; 1255 --no_wait_return;
1262 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1256 #if defined(FEAT_EVAL)
1263 if (!aborting()) 1257 if (!aborting())
1264 #endif 1258 #endif
1265 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */ 1259 (void)EMSG2(_(e_notcreate), itmp); /* will call wait_return */
1266 goto filterend; 1260 goto filterend;
1267 } 1261 }
1268 #ifdef FEAT_AUTOCMD
1269 if (curbuf != old_curbuf) 1262 if (curbuf != old_curbuf)
1270 goto filterend; 1263 goto filterend;
1271 #endif
1272 1264
1273 if (!do_out) 1265 if (!do_out)
1274 msg_putchar('\n'); 1266 msg_putchar('\n');
1275 1267
1276 /* Create the shell command in allocated memory. */ 1268 /* Create the shell command in allocated memory. */
1332 if (otmp != NULL) 1324 if (otmp != NULL)
1333 { 1325 {
1334 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM, 1326 if (readfile(otmp, NULL, line2, (linenr_T)0, (linenr_T)MAXLNUM,
1335 eap, READ_FILTER) != OK) 1327 eap, READ_FILTER) != OK)
1336 { 1328 {
1337 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 1329 #if defined(FEAT_EVAL)
1338 if (!aborting()) 1330 if (!aborting())
1339 #endif 1331 #endif
1340 { 1332 {
1341 msg_putchar('\n'); 1333 msg_putchar('\n');
1342 EMSG2(_(e_notread), otmp); 1334 EMSG2(_(e_notread), otmp);
1343 } 1335 }
1344 goto error; 1336 goto error;
1345 } 1337 }
1346 #ifdef FEAT_AUTOCMD
1347 if (curbuf != old_curbuf) 1338 if (curbuf != old_curbuf)
1348 goto filterend; 1339 goto filterend;
1349 #endif
1350 } 1340 }
1351 1341
1352 read_linecount = curbuf->b_ml.ml_line_count - read_linecount; 1342 read_linecount = curbuf->b_ml.ml_line_count - read_linecount;
1353 1343
1354 if (shell_flags & SHELL_READ) 1344 if (shell_flags & SHELL_READ)
1424 wait_return(FALSE); 1414 wait_return(FALSE);
1425 } 1415 }
1426 1416
1427 filterend: 1417 filterend:
1428 1418
1429 #ifdef FEAT_AUTOCMD
1430 if (curbuf != old_curbuf) 1419 if (curbuf != old_curbuf)
1431 { 1420 {
1432 --no_wait_return; 1421 --no_wait_return;
1433 EMSG(_("E135: *Filter* Autocommands must not change current buffer")); 1422 EMSG(_("E135: *Filter* Autocommands must not change current buffer"));
1434 } 1423 }
1435 #endif
1436 if (itmp != NULL) 1424 if (itmp != NULL)
1437 mch_remove(itmp); 1425 mch_remove(itmp);
1438 if (otmp != NULL) 1426 if (otmp != NULL)
1439 mch_remove(otmp); 1427 mch_remove(otmp);
1440 vim_free(itmp); 1428 vim_free(itmp);
1480 /* 1468 /*
1481 * For autocommands we want to get the output on the current screen, to 1469 * For autocommands we want to get the output on the current screen, to
1482 * avoid having to type return below. 1470 * avoid having to type return below.
1483 */ 1471 */
1484 msg_putchar('\r'); /* put cursor at start of line */ 1472 msg_putchar('\r'); /* put cursor at start of line */
1485 #ifdef FEAT_AUTOCMD
1486 if (!autocmd_busy) 1473 if (!autocmd_busy)
1487 #endif
1488 { 1474 {
1489 #ifdef MSWIN 1475 #ifdef MSWIN
1490 if (!winstart) 1476 if (!winstart)
1491 #endif 1477 #endif
1492 stoptermcap(); 1478 stoptermcap();
1495 if (!winstart) 1481 if (!winstart)
1496 #endif 1482 #endif
1497 msg_putchar('\n'); /* may shift screen one line up */ 1483 msg_putchar('\n'); /* may shift screen one line up */
1498 1484
1499 /* warning message before calling the shell */ 1485 /* warning message before calling the shell */
1500 if (p_warn 1486 if (p_warn && !autocmd_busy && msg_silent == 0)
1501 #ifdef FEAT_AUTOCMD
1502 && !autocmd_busy
1503 #endif
1504 && msg_silent == 0)
1505 FOR_ALL_BUFFERS(buf) 1487 FOR_ALL_BUFFERS(buf)
1506 if (bufIsChangedNotTerm(buf)) 1488 if (bufIsChangedNotTerm(buf))
1507 { 1489 {
1508 #ifdef FEAT_GUI_MSWIN 1490 #ifdef FEAT_GUI_MSWIN
1509 if (!winstart) 1491 if (!winstart)
1534 { 1516 {
1535 msg_row = Rows - 1; 1517 msg_row = Rows - 1;
1536 msg_col = 0; 1518 msg_col = 0;
1537 } 1519 }
1538 1520
1539 #ifdef FEAT_AUTOCMD
1540 if (autocmd_busy) 1521 if (autocmd_busy)
1541 { 1522 {
1542 if (msg_silent == 0) 1523 if (msg_silent == 0)
1543 redraw_later_clear(); 1524 redraw_later_clear();
1544 } 1525 }
1545 else 1526 else
1546 #endif
1547 { 1527 {
1548 /* 1528 /*
1549 * For ":sh" there is no need to call wait_return(), just redraw. 1529 * For ":sh" there is no need to call wait_return(), just redraw.
1550 * Also for the Win32 GUI (the output is in a console window). 1530 * Also for the Win32 GUI (the output is in a console window).
1551 * Otherwise there is probably text on the screen that the user wants 1531 * Otherwise there is probably text on the screen that the user wants
1610 } 1590 }
1611 1591
1612 /* display any error messages now */ 1592 /* display any error messages now */
1613 display_errors(); 1593 display_errors();
1614 1594
1615 #ifdef FEAT_AUTOCMD
1616 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf); 1595 apply_autocmds(EVENT_SHELLCMDPOST, NULL, NULL, FALSE, curbuf);
1617 #endif
1618 } 1596 }
1619 1597
1620 /* 1598 /*
1621 * Create a shell command from a command string, input redirection file and 1599 * Create a shell command from a command string, input redirection file and
1622 * output redirection file. 1600 * output redirection file.
2977 rename_buffer(char_u *new_fname) 2955 rename_buffer(char_u *new_fname)
2978 { 2956 {
2979 char_u *fname, *sfname, *xfname; 2957 char_u *fname, *sfname, *xfname;
2980 buf_T *buf; 2958 buf_T *buf;
2981 2959
2982 #ifdef FEAT_AUTOCMD
2983 buf = curbuf; 2960 buf = curbuf;
2984 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 2961 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
2985 /* buffer changed, don't change name now */ 2962 /* buffer changed, don't change name now */
2986 if (buf != curbuf) 2963 if (buf != curbuf)
2987 return FAIL; 2964 return FAIL;
2988 # ifdef FEAT_EVAL 2965 #ifdef FEAT_EVAL
2989 if (aborting()) /* autocmds may abort script processing */ 2966 if (aborting()) /* autocmds may abort script processing */
2990 return FAIL; 2967 return FAIL;
2991 # endif
2992 #endif 2968 #endif
2993 /* 2969 /*
2994 * The name of the current buffer will be changed. 2970 * The name of the current buffer will be changed.
2995 * A new (unlisted) buffer entry needs to be made to hold the old file 2971 * A new (unlisted) buffer entry needs to be made to hold the old file
2996 * name, which will become the alternate file name. 2972 * name, which will become the alternate file name.
3015 if (buf != NULL && !cmdmod.keepalt) 2991 if (buf != NULL && !cmdmod.keepalt)
3016 curwin->w_alt_fnum = buf->b_fnum; 2992 curwin->w_alt_fnum = buf->b_fnum;
3017 } 2993 }
3018 vim_free(fname); 2994 vim_free(fname);
3019 vim_free(sfname); 2995 vim_free(sfname);
3020 #ifdef FEAT_AUTOCMD
3021 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 2996 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
3022 #endif 2997
3023 /* Change directories when the 'acd' option is set. */ 2998 /* Change directories when the 'acd' option is set. */
3024 DO_AUTOCHDIR 2999 DO_AUTOCHDIR
3025 return OK; 3000 return OK;
3026 } 3001 }
3027 3002
3196 3171
3197 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK) 3172 if (check_overwrite(eap, curbuf, fname, ffname, other) == OK)
3198 { 3173 {
3199 if (eap->cmdidx == CMD_saveas && alt_buf != NULL) 3174 if (eap->cmdidx == CMD_saveas && alt_buf != NULL)
3200 { 3175 {
3201 #ifdef FEAT_AUTOCMD
3202 buf_T *was_curbuf = curbuf; 3176 buf_T *was_curbuf = curbuf;
3203 3177
3204 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf); 3178 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, curbuf);
3205 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf); 3179 apply_autocmds(EVENT_BUFFILEPRE, NULL, NULL, FALSE, alt_buf);
3206 # ifdef FEAT_EVAL 3180 #ifdef FEAT_EVAL
3207 if (curbuf != was_curbuf || aborting()) 3181 if (curbuf != was_curbuf || aborting())
3208 # else 3182 #else
3209 if (curbuf != was_curbuf) 3183 if (curbuf != was_curbuf)
3210 # endif 3184 #endif
3211 { 3185 {
3212 /* buffer changed, don't change name now */ 3186 /* buffer changed, don't change name now */
3213 retval = FAIL; 3187 retval = FAIL;
3214 goto theend; 3188 goto theend;
3215 } 3189 }
3216 #endif
3217 /* Exchange the file names for the current and the alternate 3190 /* Exchange the file names for the current and the alternate
3218 * buffer. This makes it look like we are now editing the buffer 3191 * buffer. This makes it look like we are now editing the buffer
3219 * under the new name. Must be done before buf_write(), because 3192 * under the new name. Must be done before buf_write(), because
3220 * if there is no file name and 'cpo' contains 'F', it will set 3193 * if there is no file name and 'cpo' contains 'F', it will set
3221 * the file name. */ 3194 * the file name. */
3227 curbuf->b_ffname = fname; 3200 curbuf->b_ffname = fname;
3228 fname = alt_buf->b_sfname; 3201 fname = alt_buf->b_sfname;
3229 alt_buf->b_sfname = curbuf->b_sfname; 3202 alt_buf->b_sfname = curbuf->b_sfname;
3230 curbuf->b_sfname = fname; 3203 curbuf->b_sfname = fname;
3231 buf_name_changed(curbuf); 3204 buf_name_changed(curbuf);
3232 #ifdef FEAT_AUTOCMD 3205
3233 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf); 3206 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, curbuf);
3234 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf); 3207 apply_autocmds(EVENT_BUFFILEPOST, NULL, NULL, FALSE, alt_buf);
3235 if (!alt_buf->b_p_bl) 3208 if (!alt_buf->b_p_bl)
3236 { 3209 {
3237 alt_buf->b_p_bl = TRUE; 3210 alt_buf->b_p_bl = TRUE;
3238 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf); 3211 apply_autocmds(EVENT_BUFADD, NULL, NULL, FALSE, alt_buf);
3239 } 3212 }
3240 # ifdef FEAT_EVAL 3213 #ifdef FEAT_EVAL
3241 if (curbuf != was_curbuf || aborting()) 3214 if (curbuf != was_curbuf || aborting())
3242 # else 3215 #else
3243 if (curbuf != was_curbuf) 3216 if (curbuf != was_curbuf)
3244 # endif 3217 #endif
3245 { 3218 {
3246 /* buffer changed, don't write the file */ 3219 /* buffer changed, don't write the file */
3247 retval = FAIL; 3220 retval = FAIL;
3248 goto theend; 3221 goto theend;
3249 } 3222 }
3258 } 3231 }
3259 3232
3260 /* Autocommands may have changed buffer names, esp. when 3233 /* Autocommands may have changed buffer names, esp. when
3261 * 'autochdir' is set. */ 3234 * 'autochdir' is set. */
3262 fname = curbuf->b_sfname; 3235 fname = curbuf->b_sfname;
3263 #endif
3264 } 3236 }
3265 3237
3266 name_was_missing = curbuf->b_ffname == NULL; 3238 name_was_missing = curbuf->b_ffname == NULL;
3267 3239
3268 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2, 3240 retval = buf_write(curbuf, ffname, fname, eap->line1, eap->line2,
3487 { 3459 {
3488 ++error; 3460 ++error;
3489 } 3461 }
3490 else 3462 else
3491 { 3463 {
3492 #ifdef FEAT_AUTOCMD
3493 bufref_T bufref; 3464 bufref_T bufref;
3494 3465
3495 set_bufref(&bufref, buf); 3466 set_bufref(&bufref, buf);
3496 #endif
3497 if (buf_write_all(buf, eap->forceit) == FAIL) 3467 if (buf_write_all(buf, eap->forceit) == FAIL)
3498 ++error; 3468 ++error;
3499 #ifdef FEAT_AUTOCMD
3500 /* an autocommand may have deleted the buffer */ 3469 /* an autocommand may have deleted the buffer */
3501 if (!bufref_valid(&bufref)) 3470 if (!bufref_valid(&bufref))
3502 buf = firstbuf; 3471 buf = firstbuf;
3503 #endif
3504 } 3472 }
3505 eap->forceit = save_forceit; /* check_overwrite() may set it */ 3473 eap->forceit = save_forceit; /* check_overwrite() may set it */
3506 } 3474 }
3507 } 3475 }
3508 if (exiting) 3476 if (exiting)
3602 int retval; 3570 int retval;
3603 char_u *free_me = NULL; 3571 char_u *free_me = NULL;
3604 3572
3605 if (text_locked()) 3573 if (text_locked())
3606 return GETFILE_ERROR; 3574 return GETFILE_ERROR;
3607 #ifdef FEAT_AUTOCMD
3608 if (curbuf_locked()) 3575 if (curbuf_locked())
3609 return GETFILE_ERROR; 3576 return GETFILE_ERROR;
3610 #endif
3611 3577
3612 if (fnum == 0) 3578 if (fnum == 0)
3613 { 3579 {
3614 /* make ffname full path, set sfname */ 3580 /* make ffname full path, set sfname */
3615 fname_expand(curbuf, &ffname, &sfname); 3581 fname_expand(curbuf, &ffname, &sfname);
3700 int flags, 3666 int flags,
3701 win_T *oldwin) 3667 win_T *oldwin)
3702 { 3668 {
3703 int other_file; /* TRUE if editing another file */ 3669 int other_file; /* TRUE if editing another file */
3704 int oldbuf; /* TRUE if using existing buffer */ 3670 int oldbuf; /* TRUE if using existing buffer */
3705 #ifdef FEAT_AUTOCMD
3706 int auto_buf = FALSE; /* TRUE if autocommands brought us 3671 int auto_buf = FALSE; /* TRUE if autocommands brought us
3707 into the buffer unexpectedly */ 3672 into the buffer unexpectedly */
3708 char_u *new_name = NULL; 3673 char_u *new_name = NULL;
3674 #if defined(FEAT_EVAL)
3709 int did_set_swapcommand = FALSE; 3675 int did_set_swapcommand = FALSE;
3710 #endif 3676 #endif
3711 buf_T *buf; 3677 buf_T *buf;
3712 bufref_T bufref; 3678 bufref_T bufref;
3713 #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3714 bufref_T old_curbuf; 3679 bufref_T old_curbuf;
3715 #endif
3716 char_u *free_fname = NULL; 3680 char_u *free_fname = NULL;
3717 #ifdef FEAT_BROWSE 3681 #ifdef FEAT_BROWSE
3718 char_u *browse_file = NULL; 3682 char_u *browse_file = NULL;
3719 #endif 3683 #endif
3720 int retval = FAIL; 3684 int retval = FAIL;
3734 int readfile_flags = 0; 3698 int readfile_flags = 0;
3735 int did_inc_redrawing_disabled = FALSE; 3699 int did_inc_redrawing_disabled = FALSE;
3736 3700
3737 if (eap != NULL) 3701 if (eap != NULL)
3738 command = eap->do_ecmd_cmd; 3702 command = eap->do_ecmd_cmd;
3739 #if defined(FEAT_AUTOCMD) || defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
3740 set_bufref(&old_curbuf, curbuf); 3703 set_bufref(&old_curbuf, curbuf);
3741 #endif
3742 3704
3743 if (fnum != 0) 3705 if (fnum != 0)
3744 { 3706 {
3745 if (fnum == curbuf->b_fnum) /* file is already being edited */ 3707 if (fnum == curbuf->b_fnum) /* file is already being edited */
3746 return OK; /* nothing to do */ 3708 return OK; /* nothing to do */
3749 else 3711 else
3750 { 3712 {
3751 #ifdef FEAT_BROWSE 3713 #ifdef FEAT_BROWSE
3752 if (cmdmod.browse) 3714 if (cmdmod.browse)
3753 { 3715 {
3754 # ifdef FEAT_AUTOCMD
3755 if ( 3716 if (
3756 # ifdef FEAT_GUI 3717 # ifdef FEAT_GUI
3757 !gui.in_use && 3718 !gui.in_use &&
3758 # endif 3719 # endif
3759 au_has_group((char_u *)"FileExplorer")) 3720 au_has_group((char_u *)"FileExplorer"))
3760 { 3721 {
3761 /* No browsing supported but we do have the file explorer: 3722 /* No browsing supported but we do have the file explorer:
3762 * Edit the directory. */ 3723 * Edit the directory. */
3763 if (ffname == NULL || !mch_isdir(ffname)) 3724 if (ffname == NULL || !mch_isdir(ffname))
3764 ffname = (char_u *)"."; 3725 ffname = (char_u *)".";
3765 } 3726 }
3766 else 3727 else
3767 # endif
3768 { 3728 {
3769 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname, 3729 browse_file = do_browse(0, (char_u *)_("Edit File"), ffname,
3770 NULL, NULL, NULL, curbuf); 3730 NULL, NULL, NULL, curbuf);
3771 if (browse_file == NULL) 3731 if (browse_file == NULL)
3772 goto theend; 3732 goto theend;
3836 * End Visual mode before switching to another buffer, so the text can be 3796 * End Visual mode before switching to another buffer, so the text can be
3837 * copied into the GUI selection buffer. 3797 * copied into the GUI selection buffer.
3838 */ 3798 */
3839 reset_VIsual(); 3799 reset_VIsual();
3840 3800
3841 #ifdef FEAT_AUTOCMD 3801 #if defined(FEAT_EVAL)
3842 if ((command != NULL || newlnum > (linenr_T)0) 3802 if ((command != NULL || newlnum > (linenr_T)0)
3843 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL) 3803 && *get_vim_var_str(VV_SWAPCOMMAND) == NUL)
3844 { 3804 {
3845 int len; 3805 int len;
3846 char_u *p; 3806 char_u *p;
3899 goto theend; 3859 goto theend;
3900 } 3860 }
3901 #endif 3861 #endif
3902 buf = buflist_new(ffname, sfname, 0L, 3862 buf = buflist_new(ffname, sfname, 0L,
3903 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED)); 3863 BLN_CURBUF | ((flags & ECMD_SET_HELP) ? 0 : BLN_LISTED));
3904 #ifdef FEAT_AUTOCMD 3864
3905 /* autocommands may change curwin and curbuf */ 3865 /* autocommands may change curwin and curbuf */
3906 if (oldwin != NULL) 3866 if (oldwin != NULL)
3907 oldwin = curwin; 3867 oldwin = curwin;
3908 set_bufref(&old_curbuf, curbuf); 3868 set_bufref(&old_curbuf, curbuf);
3909 #endif
3910 } 3869 }
3911 if (buf == NULL) 3870 if (buf == NULL)
3912 goto theend; 3871 goto theend;
3913 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */ 3872 if (buf->b_ml.ml_mfp == NULL) /* no memfile yet */
3914 { 3873 {
3919 oldbuf = TRUE; 3878 oldbuf = TRUE;
3920 set_bufref(&bufref, buf); 3879 set_bufref(&bufref, buf);
3921 (void)buf_check_timestamp(buf, FALSE); 3880 (void)buf_check_timestamp(buf, FALSE);
3922 /* Check if autocommands made the buffer invalid or changed the 3881 /* Check if autocommands made the buffer invalid or changed the
3923 * current buffer. */ 3882 * current buffer. */
3924 if (!bufref_valid(&bufref) 3883 if (!bufref_valid(&bufref) || curbuf != old_curbuf.br_buf)
3925 #ifdef FEAT_AUTOCMD
3926 || curbuf != old_curbuf.br_buf
3927 #endif
3928 )
3929 goto theend; 3884 goto theend;
3930 #ifdef FEAT_EVAL 3885 #ifdef FEAT_EVAL
3931 if (aborting()) /* autocmds may abort script processing */ 3886 if (aborting()) /* autocmds may abort script processing */
3932 goto theend; 3887 goto theend;
3933 #endif 3888 #endif
3948 * If the current buffer was empty and has no file name, curbuf 3903 * If the current buffer was empty and has no file name, curbuf
3949 * is returned by buflist_new(), nothing to do here. 3904 * is returned by buflist_new(), nothing to do here.
3950 */ 3905 */
3951 if (buf != curbuf) 3906 if (buf != curbuf)
3952 { 3907 {
3953 #ifdef FEAT_AUTOCMD
3954 /* 3908 /*
3955 * Be careful: The autocommands may delete any buffer and change 3909 * Be careful: The autocommands may delete any buffer and change
3956 * the current buffer. 3910 * the current buffer.
3957 * - If the buffer we are going to edit is deleted, give up. 3911 * - If the buffer we are going to edit is deleted, give up.
3958 * - If the current buffer is deleted, prefer to load the new 3912 * - If the current buffer is deleted, prefer to load the new
3969 { 3923 {
3970 /* new buffer has been deleted */ 3924 /* new buffer has been deleted */
3971 delbuf_msg(new_name); /* frees new_name */ 3925 delbuf_msg(new_name); /* frees new_name */
3972 goto theend; 3926 goto theend;
3973 } 3927 }
3974 # ifdef FEAT_EVAL 3928 #ifdef FEAT_EVAL
3975 if (aborting()) /* autocmds may abort script processing */ 3929 if (aborting()) /* autocmds may abort script processing */
3976 { 3930 {
3977 vim_free(new_name); 3931 vim_free(new_name);
3978 goto theend; 3932 goto theend;
3979 } 3933 }
3980 # endif 3934 #endif
3981 if (buf == curbuf) /* already in new buffer */ 3935 if (buf == curbuf) /* already in new buffer */
3982 auto_buf = TRUE; 3936 auto_buf = TRUE;
3983 else 3937 else
3984 { 3938 {
3985 win_T *the_curwin = curwin; 3939 win_T *the_curwin = curwin;
3988 * window. And set b_locked for the same reason. */ 3942 * window. And set b_locked for the same reason. */
3989 the_curwin->w_closing = TRUE; 3943 the_curwin->w_closing = TRUE;
3990 ++buf->b_locked; 3944 ++buf->b_locked;
3991 3945
3992 if (curbuf == old_curbuf.br_buf) 3946 if (curbuf == old_curbuf.br_buf)
3993 #endif
3994 buf_copy_options(buf, BCO_ENTER); 3947 buf_copy_options(buf, BCO_ENTER);
3995 3948
3996 /* Close the link to the current buffer. This will set 3949 /* Close the link to the current buffer. This will set
3997 * oldwin->w_buffer to NULL. */ 3950 * oldwin->w_buffer to NULL. */
3998 u_sync(FALSE); 3951 u_sync(FALSE);
3999 close_buffer(oldwin, curbuf, 3952 close_buffer(oldwin, curbuf,
4000 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE); 3953 (flags & ECMD_HIDE) ? 0 : DOBUF_UNLOAD, FALSE);
4001 3954
4002 #ifdef FEAT_AUTOCMD
4003 the_curwin->w_closing = FALSE; 3955 the_curwin->w_closing = FALSE;
4004 --buf->b_locked; 3956 --buf->b_locked;
4005 3957
4006 # ifdef FEAT_EVAL 3958 #ifdef FEAT_EVAL
4007 /* autocmds may abort script processing */ 3959 /* autocmds may abort script processing */
4008 if (aborting() && curwin->w_buffer != NULL) 3960 if (aborting() && curwin->w_buffer != NULL)
4009 { 3961 {
4010 vim_free(new_name); 3962 vim_free(new_name);
4011 goto theend; 3963 goto theend;
4012 } 3964 }
4013 # endif 3965 #endif
4014 /* Be careful again, like above. */ 3966 /* Be careful again, like above. */
4015 if (!bufref_valid(&au_new_curbuf)) 3967 if (!bufref_valid(&au_new_curbuf))
4016 { 3968 {
4017 /* new buffer has been deleted */ 3969 /* new buffer has been deleted */
4018 delbuf_msg(new_name); /* frees new_name */ 3970 delbuf_msg(new_name); /* frees new_name */
4019 goto theend; 3971 goto theend;
4020 } 3972 }
4021 if (buf == curbuf) /* already in new buffer */ 3973 if (buf == curbuf) /* already in new buffer */
4022 auto_buf = TRUE; 3974 auto_buf = TRUE;
4023 else 3975 else
4024 #endif
4025 { 3976 {
4026 #ifdef FEAT_SYN_HL 3977 #ifdef FEAT_SYN_HL
4027 /* 3978 /*
4028 * <VN> We could instead free the synblock 3979 * <VN> We could instead free the synblock
4029 * and re-attach to buffer, perhaps. 3980 * and re-attach to buffer, perhaps.
4052 * values. Also restores old folding stuff. */ 4003 * values. Also restores old folding stuff. */
4053 get_winopts(curbuf); 4004 get_winopts(curbuf);
4054 #ifdef FEAT_SPELL 4005 #ifdef FEAT_SPELL
4055 did_get_winopts = TRUE; 4006 did_get_winopts = TRUE;
4056 #endif 4007 #endif
4057
4058 #ifdef FEAT_AUTOCMD
4059 } 4008 }
4060 vim_free(new_name); 4009 vim_free(new_name);
4061 au_new_curbuf.br_buf = NULL; 4010 au_new_curbuf.br_buf = NULL;
4062 au_new_curbuf.br_buf_free_count = 0; 4011 au_new_curbuf.br_buf_free_count = 0;
4063 #endif
4064 } 4012 }
4065 4013
4066 curwin->w_pcmark.lnum = 1; 4014 curwin->w_pcmark.lnum = 1;
4067 curwin->w_pcmark.col = 0; 4015 curwin->w_pcmark.col = 0;
4068 } 4016 }
4081 /* Don't redraw until the cursor is in the right line, otherwise 4029 /* Don't redraw until the cursor is in the right line, otherwise
4082 * autocommands may cause ml_get errors. */ 4030 * autocommands may cause ml_get errors. */
4083 ++RedrawingDisabled; 4031 ++RedrawingDisabled;
4084 did_inc_redrawing_disabled = TRUE; 4032 did_inc_redrawing_disabled = TRUE;
4085 4033
4086 #ifdef FEAT_AUTOCMD
4087 buf = curbuf; 4034 buf = curbuf;
4088 #endif
4089 if ((flags & ECMD_SET_HELP) || keep_help_flag) 4035 if ((flags & ECMD_SET_HELP) || keep_help_flag)
4090 { 4036 {
4091 prepare_help_buffer(); 4037 prepare_help_buffer();
4092 } 4038 }
4093 else 4039 else
4096 * using CTRL-O to go back to a help file. */ 4042 * using CTRL-O to go back to a help file. */
4097 if (!curbuf->b_help) 4043 if (!curbuf->b_help)
4098 set_buflisted(TRUE); 4044 set_buflisted(TRUE);
4099 } 4045 }
4100 4046
4101 #ifdef FEAT_AUTOCMD
4102 /* If autocommands change buffers under our fingers, forget about 4047 /* If autocommands change buffers under our fingers, forget about
4103 * editing the file. */ 4048 * editing the file. */
4104 if (buf != curbuf) 4049 if (buf != curbuf)
4105 goto theend; 4050 goto theend;
4106 # ifdef FEAT_EVAL 4051 #ifdef FEAT_EVAL
4107 if (aborting()) /* autocmds may abort script processing */ 4052 if (aborting()) /* autocmds may abort script processing */
4108 goto theend; 4053 goto theend;
4109 # endif 4054 #endif
4110 4055
4111 /* Since we are starting to edit a file, consider the filetype to be 4056 /* Since we are starting to edit a file, consider the filetype to be
4112 * unset. Helps for when an autocommand changes files and expects syntax 4057 * unset. Helps for when an autocommand changes files and expects syntax
4113 * highlighting to work in the other file. */ 4058 * highlighting to work in the other file. */
4114 did_filetype = FALSE; 4059 did_filetype = FALSE;
4115 #endif
4116 4060
4117 /* 4061 /*
4118 * other_file oldbuf 4062 * other_file oldbuf
4119 * FALSE FALSE re-edit same file, buffer is re-used 4063 * FALSE FALSE re-edit same file, buffer is re-used
4120 * FALSE TRUE re-edit same file, nothing changes 4064 * FALSE TRUE re-edit same file, nothing changes
4127 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL) 4071 if (newlnum == ECMD_LAST || newlnum == ECMD_LASTL)
4128 { 4072 {
4129 newlnum = curwin->w_cursor.lnum; 4073 newlnum = curwin->w_cursor.lnum;
4130 solcol = curwin->w_cursor.col; 4074 solcol = curwin->w_cursor.col;
4131 } 4075 }
4132 #ifdef FEAT_AUTOCMD
4133 buf = curbuf; 4076 buf = curbuf;
4134 if (buf->b_fname != NULL) 4077 if (buf->b_fname != NULL)
4135 new_name = vim_strsave(buf->b_fname); 4078 new_name = vim_strsave(buf->b_fname);
4136 else 4079 else
4137 new_name = NULL; 4080 new_name = NULL;
4138 set_bufref(&bufref, buf); 4081 set_bufref(&bufref, buf);
4139 #endif 4082
4140 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur) 4083 if (p_ur < 0 || curbuf->b_ml.ml_line_count <= p_ur)
4141 { 4084 {
4142 /* Save all the text, so that the reload can be undone. 4085 /* Save all the text, so that the reload can be undone.
4143 * Sync first so that this is a separate undo-able action. */ 4086 * Sync first so that this is a separate undo-able action. */
4144 u_sync(FALSE); 4087 u_sync(FALSE);
4145 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE) 4088 if (u_savecommon(0, curbuf->b_ml.ml_line_count + 1, 0, TRUE)
4146 == FAIL) 4089 == FAIL)
4147 { 4090 {
4148 #ifdef FEAT_AUTOCMD
4149 vim_free(new_name); 4091 vim_free(new_name);
4150 #endif
4151 goto theend; 4092 goto theend;
4152 } 4093 }
4153 u_unchanged(curbuf); 4094 u_unchanged(curbuf);
4154 buf_freeall(curbuf, BFA_KEEP_UNDO); 4095 buf_freeall(curbuf, BFA_KEEP_UNDO);
4155 4096
4156 /* tell readfile() not to clear or reload undo info */ 4097 /* tell readfile() not to clear or reload undo info */
4157 readfile_flags = READ_KEEP_UNDO; 4098 readfile_flags = READ_KEEP_UNDO;
4158 } 4099 }
4159 else 4100 else
4160 buf_freeall(curbuf, 0); /* free all things for buffer */ 4101 buf_freeall(curbuf, 0); /* free all things for buffer */
4161 #ifdef FEAT_AUTOCMD 4102
4162 /* If autocommands deleted the buffer we were going to re-edit, give 4103 /* If autocommands deleted the buffer we were going to re-edit, give
4163 * up and jump to the end. */ 4104 * up and jump to the end. */
4164 if (!bufref_valid(&bufref)) 4105 if (!bufref_valid(&bufref))
4165 { 4106 {
4166 delbuf_msg(new_name); /* frees new_name */ 4107 delbuf_msg(new_name); /* frees new_name */
4171 /* If autocommands change buffers under our fingers, forget about 4112 /* If autocommands change buffers under our fingers, forget about
4172 * re-editing the file. Should do the buf_clear_file(), but perhaps 4113 * re-editing the file. Should do the buf_clear_file(), but perhaps
4173 * the autocommands changed the buffer... */ 4114 * the autocommands changed the buffer... */
4174 if (buf != curbuf) 4115 if (buf != curbuf)
4175 goto theend; 4116 goto theend;
4176 # ifdef FEAT_EVAL 4117 #ifdef FEAT_EVAL
4177 if (aborting()) /* autocmds may abort script processing */ 4118 if (aborting()) /* autocmds may abort script processing */
4178 goto theend; 4119 goto theend;
4179 # endif
4180 #endif 4120 #endif
4181 buf_clear_file(curbuf); 4121 buf_clear_file(curbuf);
4182 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */ 4122 curbuf->b_op_start.lnum = 0; /* clear '[ and '] marks */
4183 curbuf->b_op_end.lnum = 0; 4123 curbuf->b_op_end.lnum = 0;
4184 } 4124 }
4192 /* 4132 /*
4193 * Check if we are editing the w_arg_idx file in the argument list. 4133 * Check if we are editing the w_arg_idx file in the argument list.
4194 */ 4134 */
4195 check_arg_idx(curwin); 4135 check_arg_idx(curwin);
4196 4136
4197 #ifdef FEAT_AUTOCMD
4198 if (!auto_buf) 4137 if (!auto_buf)
4199 #endif
4200 { 4138 {
4201 /* 4139 /*
4202 * Set cursor and init window before reading the file and executing 4140 * Set cursor and init window before reading the file and executing
4203 * autocommands. This allows for the autocommands to position the 4141 * autocommands. This allows for the autocommands to position the
4204 * cursor. 4142 * cursor.
4235 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */ 4173 curbuf->b_flags |= BF_CHECK_RO; /* set/reset 'ro' flag */
4236 4174
4237 /* 4175 /*
4238 * Open the buffer and read the file. 4176 * Open the buffer and read the file.
4239 */ 4177 */
4240 #if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) 4178 #if defined(FEAT_EVAL)
4241 if (should_abort(open_buffer(FALSE, eap, readfile_flags))) 4179 if (should_abort(open_buffer(FALSE, eap, readfile_flags)))
4242 retval = FAIL; 4180 retval = FAIL;
4243 #else 4181 #else
4244 (void)open_buffer(FALSE, eap, readfile_flags); 4182 (void)open_buffer(FALSE, eap, readfile_flags);
4245 #endif 4183 #endif
4248 if (swap_exists_action == SEA_QUIT) 4186 if (swap_exists_action == SEA_QUIT)
4249 retval = FAIL; 4187 retval = FAIL;
4250 handle_swap_exists(&old_curbuf); 4188 handle_swap_exists(&old_curbuf);
4251 #endif 4189 #endif
4252 } 4190 }
4253 #ifdef FEAT_AUTOCMD
4254 else 4191 else
4255 { 4192 {
4256 /* Read the modelines, but only to set window-local options. Any 4193 /* Read the modelines, but only to set window-local options. Any
4257 * buffer-local options have already been set and may have been 4194 * buffer-local options have already been set and may have been
4258 * changed by the user. */ 4195 * changed by the user. */
4262 &retval); 4199 &retval);
4263 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf, 4200 apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, FALSE, curbuf,
4264 &retval); 4201 &retval);
4265 } 4202 }
4266 check_arg_idx(curwin); 4203 check_arg_idx(curwin);
4267 #endif
4268 4204
4269 /* If autocommands change the cursor position or topline, we should 4205 /* If autocommands change the cursor position or topline, we should
4270 * keep it. Also when it moves within a line. */ 4206 * keep it. Also when it moves within a line. */
4271 if (!EQUAL_POS(curwin->w_cursor, orig_pos)) 4207 if (!EQUAL_POS(curwin->w_cursor, orig_pos))
4272 { 4208 {
4340 4276
4341 /* 4277 /*
4342 * Did not read the file, need to show some info about the file. 4278 * Did not read the file, need to show some info about the file.
4343 * Do this after setting the cursor. 4279 * Do this after setting the cursor.
4344 */ 4280 */
4345 if (oldbuf 4281 if (oldbuf && !auto_buf)
4346 #ifdef FEAT_AUTOCMD
4347 && !auto_buf
4348 #endif
4349 )
4350 { 4282 {
4351 int msg_scroll_save = msg_scroll; 4283 int msg_scroll_save = msg_scroll;
4352 4284
4353 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file 4285 /* Obey the 'O' flag in 'cpoptions': overwrite any previous file
4354 * message. */ 4286 * message. */
4428 #endif 4360 #endif
4429 4361
4430 theend: 4362 theend:
4431 if (did_inc_redrawing_disabled) 4363 if (did_inc_redrawing_disabled)
4432 --RedrawingDisabled; 4364 --RedrawingDisabled;
4433 #ifdef FEAT_AUTOCMD 4365 #if defined(FEAT_EVAL)
4434 if (did_set_swapcommand) 4366 if (did_set_swapcommand)
4435 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1); 4367 set_vim_var_string(VV_SWAPCOMMAND, NULL, -1);
4436 #endif 4368 #endif
4437 #ifdef FEAT_BROWSE 4369 #ifdef FEAT_BROWSE
4438 vim_free(browse_file); 4370 vim_free(browse_file);
4439 #endif 4371 #endif
4440 vim_free(free_fname); 4372 vim_free(free_fname);
4441 return retval; 4373 return retval;
4442 } 4374 }
4443 4375
4444 #ifdef FEAT_AUTOCMD
4445 static void 4376 static void
4446 delbuf_msg(char_u *name) 4377 delbuf_msg(char_u *name)
4447 { 4378 {
4448 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"), 4379 EMSG2(_("E143: Autocommands unexpectedly deleted new buffer %s"),
4449 name == NULL ? (char_u *)"" : name); 4380 name == NULL ? (char_u *)"" : name);
4450 vim_free(name); 4381 vim_free(name);
4451 au_new_curbuf.br_buf = NULL; 4382 au_new_curbuf.br_buf = NULL;
4452 au_new_curbuf.br_buf_free_count = 0; 4383 au_new_curbuf.br_buf_free_count = 0;
4453 } 4384 }
4454 #endif
4455 4385
4456 static int append_indent = 0; /* autoindent for first line */ 4386 static int append_indent = 0; /* autoindent for first line */
4457 4387
4458 /* 4388 /*
4459 * ":insert" and ":append", also used by ":change" 4389 * ":insert" and ":append", also used by ":change"
5154 /* 5084 /*
5155 * Check for a match on each line. 5085 * Check for a match on each line.
5156 */ 5086 */
5157 line2 = eap->line2; 5087 line2 = eap->line2;
5158 for (lnum = eap->line1; lnum <= line2 && !(got_quit 5088 for (lnum = eap->line1; lnum <= line2 && !(got_quit
5159 #if defined(FEAT_EVAL) && defined(FEAT_AUTOCMD) 5089 #if defined(FEAT_EVAL)
5160 || aborting() 5090 || aborting()
5161 #endif 5091 #endif
5162 ); ++lnum) 5092 ); ++lnum)
5163 { 5093 {
5164 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum, 5094 nmatch = vim_regexec_multi(&regmatch, curwin, curbuf, lnum,
6868 char_u *fname; 6798 char_u *fname;
6869 char_u *p; 6799 char_u *p;
6870 char_u *rt; 6800 char_u *rt;
6871 int mustfree; 6801 int mustfree;
6872 6802
6873 #ifdef FEAT_AUTOCMD
6874 /* Set filetype to "help" if still needed. */ 6803 /* Set filetype to "help" if still needed. */
6875 if (STRCMP(curbuf->b_p_ft, "help") != 0) 6804 if (STRCMP(curbuf->b_p_ft, "help") != 0)
6876 { 6805 {
6877 ++curbuf_lock; 6806 ++curbuf_lock;
6878 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL); 6807 set_option_value((char_u *)"ft", 0L, (char_u *)"help", OPT_LOCAL);
6879 --curbuf_lock; 6808 --curbuf_lock;
6880 } 6809 }
6881 #endif
6882 6810
6883 #ifdef FEAT_SYN_HL 6811 #ifdef FEAT_SYN_HL
6884 if (!syntax_present(curwin)) 6812 if (!syntax_present(curwin))
6885 #endif 6813 #endif
6886 { 6814 {