diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/autocmd.c | 106 | ||||
-rw-r--r-- | src/nvim/autocmd.h | 2 | ||||
-rw-r--r-- | src/nvim/buffer.c | 46 | ||||
-rw-r--r-- | src/nvim/diff.c | 7 | ||||
-rw-r--r-- | src/nvim/eval.c | 7 | ||||
-rw-r--r-- | src/nvim/eval/buffer.c | 15 | ||||
-rw-r--r-- | src/nvim/eval/vars.c | 12 | ||||
-rw-r--r-- | src/nvim/eval/window.c | 4 | ||||
-rw-r--r-- | src/nvim/ex_cmds2.c | 6 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 2 | ||||
-rw-r--r-- | src/nvim/fileio.c | 45 | ||||
-rw-r--r-- | src/nvim/globals.h | 15 | ||||
-rw-r--r-- | src/nvim/main.c | 2 | ||||
-rw-r--r-- | src/nvim/quickfix.c | 96 | ||||
-rw-r--r-- | src/nvim/terminal.c | 2 | ||||
-rw-r--r-- | src/nvim/testdir/test_autocmd.vim | 23 | ||||
-rw-r--r-- | src/nvim/window.c | 54 |
17 files changed, 292 insertions, 152 deletions
diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 57b5adca53..6efb05ab45 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -633,6 +633,11 @@ void do_augroup(char *arg, int del_group) } } +void autocmd_init(void) +{ + CLEAR_FIELD(aucmd_win); +} + #if defined(EXITFREE) void free_all_autocmds(void) { @@ -659,9 +664,22 @@ void free_all_autocmds(void) api_free_string(name); }) map_destroy(int, String)(&map_augroup_id_to_name); + + // aucmd_win[] is freed in win_free_all() } #endif +/// Return true if "win" is an active entry in aucmd_win[]. +bool is_aucmd_win(win_T *win) +{ + for (int i = 0; i < AUCMD_WIN_COUNT; i++) { + if (aucmd_win[i].auc_win_used && aucmd_win[i].auc_win == win) { + return true; + } + } + return false; +} + // Return the event number for event name "start". // Return NUM_EVENTS if the event name was not found. // Return a pointer to the next event name in "end". @@ -1310,6 +1328,13 @@ void ex_doautoall(exarg_T *eap) // Find a window for this buffer and save some values. aucmd_prepbuf(&aco, buf); + if (curbuf != buf) { + // Failed to find a window for this buffer. Better not execute + // autocommands then. + retval = FAIL; + break; + } + set_bufref(&bufref, buf); // execute the autocommands for this buffer @@ -1319,7 +1344,7 @@ void ex_doautoall(exarg_T *eap) // Execute the modeline settings, but don't set window-local // options if we are using the current window for another // buffer. - do_modelines(curwin == aucmd_win ? OPT_NOWIN : 0); + do_modelines(is_aucmd_win(curwin) ? OPT_NOWIN : 0); } // restore the current window @@ -1358,8 +1383,9 @@ bool check_nomodeline(char **argp) /// Prepare for executing autocommands for (hidden) buffer `buf`. /// If the current buffer is not in any visible window, put it in a temporary -/// floating window `aucmd_win`. +/// floating window using an entry in `aucmd_win[]`. /// Set `curbuf` and `curwin` to match `buf`. +/// When this fails `curbuf` is not equal `buf`. /// /// @param aco structure to save values in /// @param buf new curbuf @@ -1381,15 +1407,28 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) } } - // Allocate the `aucmd_win` dummy floating window. - if (win == NULL && aucmd_win == NULL) { - win_alloc_aucmd_win(); - need_append = false; - } - if (win == NULL && aucmd_win_used) { - // Strange recursive autocommand, fall back to using the current - // window. Expect a few side effects... - win = curwin; + // Allocate a window when needed. + win_T *auc_win = NULL; + int auc_idx = AUCMD_WIN_COUNT; + if (win == NULL) { + for (auc_idx = 0; auc_idx < AUCMD_WIN_COUNT; auc_idx++) { + if (!aucmd_win[auc_idx].auc_win_used) { + if (aucmd_win[auc_idx].auc_win == NULL) { + win_alloc_aucmd_win(auc_idx); + need_append = false; + } + auc_win = aucmd_win[auc_idx].auc_win; + aucmd_win[auc_idx].auc_win_used = true; + break; + } + } + + // If this fails (using all AUCMD_WIN_COUNT entries) + // then we can't reliably execute the autocmd, + // return with "curbuf" unequal "buf". + if (auc_win == NULL) { + return; + } } aco->save_curwin_handle = curwin->handle; @@ -1399,42 +1438,41 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) // There is a window for "buf" in the current tab page, make it the // curwin. This is preferred, it has the least side effects (esp. if // "buf" is curbuf). - aco->use_aucmd_win = false; + aco->use_aucmd_win_idx = -1; curwin = win; } else { - // There is no window for "buf", use "aucmd_win". To minimize the side + // There is no window for "buf", use "auc_win". To minimize the side // effects, insert it in the current tab page. // Anything related to a window (e.g., setting folds) may have // unexpected results. - aco->use_aucmd_win = true; - aucmd_win_used = true; - aucmd_win->w_buffer = buf; - aucmd_win->w_s = &buf->b_s; + aco->use_aucmd_win_idx = auc_idx; + auc_win->w_buffer = buf; + auc_win->w_s = &buf->b_s; buf->b_nwindows++; - win_init_empty(aucmd_win); // set cursor and topline to safe values + win_init_empty(auc_win); // set cursor and topline to safe values // Make sure w_localdir and globaldir are NULL to avoid a chdir() in // win_enter_ext(). - XFREE_CLEAR(aucmd_win->w_localdir); + XFREE_CLEAR(auc_win->w_localdir); aco->globaldir = globaldir; globaldir = NULL; block_autocmds(); // We don't want BufEnter/WinEnter autocommands. if (need_append) { - win_append(lastwin, aucmd_win); - pmap_put(handle_T)(&window_handles, aucmd_win->handle, aucmd_win); - win_config_float(aucmd_win, aucmd_win->w_float_config); + win_append(lastwin, auc_win); + pmap_put(handle_T)(&window_handles, auc_win->handle, auc_win); + win_config_float(auc_win, auc_win->w_float_config); } // Prevent chdir() call in win_enter_ext(), through do_autochdir() int save_acd = p_acd; p_acd = false; // no redrawing and don't set the window title RedrawingDisabled++; - win_enter(aucmd_win, false); + win_enter(auc_win, false); RedrawingDisabled--; p_acd = save_acd; unblock_autocmds(); - curwin = aucmd_win; + curwin = auc_win; } curbuf = buf; aco->new_curwin_handle = curwin->handle; @@ -1451,18 +1489,20 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) /// @param aco structure holding saved values void aucmd_restbuf(aco_save_T *aco) { - if (aco->use_aucmd_win) { + if (aco->use_aucmd_win_idx >= 0) { + win_T *awp = aucmd_win[aco->use_aucmd_win_idx].auc_win; + curbuf->b_nwindows--; - // Find "aucmd_win", it can't be closed, but it may be in another tab page. + // Find "awp", it can't be closed, but it may be in another tab page. // Do not trigger autocommands here. block_autocmds(); - if (curwin != aucmd_win) { + if (curwin != awp) { FOR_ALL_TAB_WINDOWS(tp, wp) { - if (wp == aucmd_win) { + if (wp == awp) { if (tp != curtab) { goto_tabpage_tp(tp, true, true); } - win_goto(aucmd_win); + win_goto(awp); goto win_found; } } @@ -1477,7 +1517,9 @@ win_found: grid_free(&curwin->w_grid_alloc); } - aucmd_win_used = false; + // The window is marked as not used, but it is not freed, it can be + // used again. + aucmd_win[aco->use_aucmd_win_idx].auc_win_used = false; if (!valid_tabpage_win(curtab)) { // no valid window in current tabpage @@ -1498,8 +1540,8 @@ win_found: entering_window(curwin); prevwin = win_find_by_handle(aco->save_prevwin_handle); - vars_clear(&aucmd_win->w_vars->dv_hashtab); // free all w: variables - hash_init(&aucmd_win->w_vars->dv_hashtab); // re-use the hashtab + vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables + hash_init(&awp->w_vars->dv_hashtab); // re-use the hashtab xfree(globaldir); globaldir = aco->globaldir; diff --git a/src/nvim/autocmd.h b/src/nvim/autocmd.h index c159254c29..791b589167 100644 --- a/src/nvim/autocmd.h +++ b/src/nvim/autocmd.h @@ -25,7 +25,7 @@ struct AutoPat_S; // not the current buffer. typedef struct { buf_T *save_curbuf; ///< saved curbuf - bool use_aucmd_win; ///< using aucmd_win + int use_aucmd_win_idx; ///< index in aucmd_win[] if >= 0 handle_T save_curwin_handle; ///< ID of saved curwin handle_T new_curwin_handle; ///< ID of new curwin handle_T save_prevwin_handle; ///< ID of saved prevwin diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 6cb171978b..2060f799bd 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -175,6 +175,22 @@ static int read_buffer(int read_stdin, exarg_T *eap, int flags) return retval; } +/// Ensure buffer "buf" is loaded. Does not trigger the swap-exists action. +void buffer_ensure_loaded(buf_T *buf) +{ + if (buf->b_ml.ml_mfp == NULL) { + aco_save_T aco; + + // Make sure the buffer is in a window. If not then skip it. + aucmd_prepbuf(&aco, buf); + if (curbuf == buf) { + swap_exists_action = SEA_NONE; + open_buffer(false, NULL, 0); + aucmd_restbuf(&aco); + } + } +} + /// Open current buffer, that is: open the memfile and read the file into /// memory. /// @@ -352,18 +368,21 @@ int open_buffer(int read_stdin, exarg_T *eap, int flags_arg) if (bufref_valid(&old_curbuf) && old_curbuf.br_buf->b_ml.ml_mfp != NULL) { aco_save_T aco; - // Go to the buffer that was opened. + // Go to the buffer that was opened, make sure it is in a window. + // If not then skip it. aucmd_prepbuf(&aco, old_curbuf.br_buf); - do_modelines(0); - curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); + if (curbuf == old_curbuf.br_buf) { + do_modelines(0); + curbuf->b_flags &= ~(BF_CHECK_RO | BF_NEVERLOADED); - if ((flags & READ_NOWINENTER) == 0) { - apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, false, curbuf, - &retval); - } + if ((flags & READ_NOWINENTER) == 0) { + apply_autocmds_retval(EVENT_BUFWINENTER, NULL, NULL, false, curbuf, + &retval); + } - // restore curwin/curbuf and a few other things - aucmd_restbuf(&aco); + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); + } } return retval; @@ -1316,7 +1335,7 @@ int do_buffer(int action, int start, int dir, int count, int forceit) // Repeat this so long as we end up in a window with this buffer. while (buf == curbuf && !(curwin->w_closing || curwin->w_buffer->b_locked > 0) - && (lastwin == aucmd_win || !last_window(curwin))) { + && (is_aucmd_win(lastwin) || !last_window(curwin))) { if (win_close(curwin, false, false) == FAIL) { break; } @@ -4155,9 +4174,14 @@ bool buf_contents_changed(buf_T *buf) exarg_T ea; prep_exarg(&ea, buf); - // set curwin/curbuf to buf and save a few things + // Set curwin/curbuf to buf and save a few things. aco_save_T aco; aucmd_prepbuf(&aco, newbuf); + if (curbuf != newbuf) { + // Failed to find a window for "newbuf". + wipe_buffer(newbuf, false); + return true; + } if (ml_open(curbuf) == OK && readfile(buf->b_ffname, buf->b_fname, diff --git a/src/nvim/diff.c b/src/nvim/diff.c index 1ae0e91cda..64a142fa0e 100644 --- a/src/nvim/diff.c +++ b/src/nvim/diff.c @@ -2943,8 +2943,13 @@ void ex_diffgetput(exarg_T *eap) // Need to make the other buffer the current buffer to be able to make // changes in it. - // set curwin/curbuf to buf and save a few things + // Set curwin/curbuf to buf and save a few things. aucmd_prepbuf(&aco, curtab->tp_diffbuf[idx_other]); + if (curbuf != curtab->tp_diffbuf[idx_other]) { + // Could not find a window for this buffer, the rest is likely to + // fail. + goto theend; + } } const int idx_from = eap->cmdidx == CMD_diffget ? idx_other : idx_cur; diff --git a/src/nvim/eval.c b/src/nvim/eval.c index dfa9238327..c4afd6934c 100644 --- a/src/nvim/eval.c +++ b/src/nvim/eval.c @@ -4174,8 +4174,11 @@ bool garbage_collect(bool testing) ABORTING(set_ref_in_fmark)(wp->w_jumplist[i].fmark, copyID); } } - if (aucmd_win != NULL) { - ABORTING(set_ref_in_item)(&aucmd_win->w_winvar.di_tv, copyID, NULL, NULL); + // window-local variables in autocmd windows + for (int i = 0; i < AUCMD_WIN_COUNT; i++) { + if (aucmd_win[i].auc_win != NULL) { + ABORTING(set_ref_in_item)(&aucmd_win[i].auc_win->w_winvar.di_tv, copyID, NULL, NULL); + } } // registers (ShaDa additional data) diff --git a/src/nvim/eval/buffer.c b/src/nvim/eval/buffer.c index 4816a955a8..6ebf54ef0f 100644 --- a/src/nvim/eval/buffer.c +++ b/src/nvim/eval/buffer.c @@ -86,6 +86,8 @@ static void find_win_for_curbuf(void) /// /// Information is saved in "cob" and MUST be restored by calling /// change_other_buffer_restore(). +/// +/// If this fails then "curbuf" will not be equal to "buf". static void change_other_buffer_prepare(cob_T *cob, buf_T *buf) { CLEAR_POINTER(cob); @@ -103,7 +105,9 @@ static void change_other_buffer_prepare(cob_T *cob, buf_T *buf) // curwin->w_buffer differ from "curbuf", use the autocmd window. curbuf = curwin->w_buffer; aucmd_prepbuf(&cob->cob_aco, buf); - cob->cob_using_aco = true; + if (curbuf == buf) { + cob->cob_using_aco = true; + } } } @@ -302,13 +306,8 @@ void f_bufload(typval_T *argvars, typval_T *unused, EvalFuncData fptr) { buf_T *buf = get_buf_arg(&argvars[0]); - if (buf != NULL && buf->b_ml.ml_mfp == NULL) { - aco_save_T aco; - - aucmd_prepbuf(&aco, buf); - swap_exists_action = SEA_NONE; - open_buffer(false, NULL, 0); - aucmd_restbuf(&aco); + if (buf != NULL) { + buffer_ensure_loaded(buf); } } diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 5459eac5bf..f002071ad2 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -1800,13 +1800,15 @@ void f_setbufvar(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) if (*varname == '&') { aco_save_T aco; - // set curbuf to be our buf, temporarily + // Set curbuf to be our buf, temporarily. aucmd_prepbuf(&aco, buf); + if (curbuf == buf) { + // Only when it worked to set "curbuf". + set_option_from_tv(varname + 1, varp); - set_option_from_tv(varname + 1, varp); - - // reset notion of buffer - aucmd_restbuf(&aco); + // reset notion of buffer + aucmd_restbuf(&aco); + } } else { const size_t varname_len = strlen(varname); char *const bufvarname = xmalloc(varname_len + 3); diff --git a/src/nvim/eval/window.c b/src/nvim/eval/window.c index d6c761bc66..7cb346216d 100644 --- a/src/nvim/eval/window.c +++ b/src/nvim/eval/window.c @@ -590,7 +590,7 @@ static void win_move_into_split(win_T *wp, win_T *targetwin, int size, int flags int height = wp->w_height; win_T *oldwin = curwin; - if (wp == targetwin || wp == aucmd_win) { + if (wp == targetwin || is_aucmd_win(wp)) { return; } @@ -674,7 +674,7 @@ void f_win_gettype(typval_T *argvars, typval_T *rettv, EvalFuncData fptr) return; } } - if (wp == aucmd_win) { + if (is_aucmd_win(wp)) { rettv->vval.v_string = xstrdup("autocmd"); } else if (wp->w_p_pvw) { rettv->vval.v_string = xstrdup("preview"); diff --git a/src/nvim/ex_cmds2.c b/src/nvim/ex_cmds2.c index b6489e97b2..d3c0c417e1 100644 --- a/src/nvim/ex_cmds2.c +++ b/src/nvim/ex_cmds2.c @@ -683,8 +683,10 @@ void ex_listdo(exarg_T *eap) curbuf); } else { aucmd_prepbuf(&aco, buf); - apply_autocmds(EVENT_SYNTAX, buf->b_p_syn, buf->b_fname, true, buf); - aucmd_restbuf(&aco); + if (curbuf == buf) { + apply_autocmds(EVENT_SYNTAX, buf->b_p_syn, buf->b_fname, true, buf); + aucmd_restbuf(&aco); + } } // start over, in case autocommands messed things up. diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index c2d73360e3..8e7dfcbd43 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -4621,7 +4621,7 @@ static void ex_pclose(exarg_T *eap) void ex_win_close(int forceit, win_T *win, tabpage_T *tp) { // Never close the autocommand window. - if (win == aucmd_win) { + if (is_aucmd_win(win)) { emsg(_(e_autocmd_close)); return; } diff --git a/src/nvim/fileio.c b/src/nvim/fileio.c index 2bff0fa2f9..dabec5e95e 100644 --- a/src/nvim/fileio.c +++ b/src/nvim/fileio.c @@ -2310,6 +2310,12 @@ int buf_write(buf_T *buf, char *fname, char *sfname, linenr_T start, linenr_T en // Set curwin/curbuf to buf and save a few things. aucmd_prepbuf(&aco, buf); + if (curbuf != buf) { + // Could not find a window for "buf". Doing more might cause + // problems, better bail out. + return FAIL; + } + set_bufref(&bufref, buf); if (append) { @@ -3612,25 +3618,27 @@ nofail: // Apply POST autocommands. // Careful: The autocommands may call buf_write() recursively! + // Only do this when a window was found for "buf". aucmd_prepbuf(&aco, buf); + if (curbuf == buf) { + if (append) { + apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, + false, curbuf, eap); + } else if (filtering) { + apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, + false, curbuf, eap); + } else if (reset_changed && whole) { + apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, + false, curbuf, eap); + } else { + apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, + false, curbuf, eap); + } - if (append) { - apply_autocmds_exarg(EVENT_FILEAPPENDPOST, fname, fname, - false, curbuf, eap); - } else if (filtering) { - apply_autocmds_exarg(EVENT_FILTERWRITEPOST, NULL, fname, - false, curbuf, eap); - } else if (reset_changed && whole) { - apply_autocmds_exarg(EVENT_BUFWRITEPOST, fname, fname, - false, curbuf, eap); - } else { - apply_autocmds_exarg(EVENT_FILEWRITEPOST, fname, fname, - false, curbuf, eap); + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); } - // restore curwin/curbuf and a few other things - aucmd_restbuf(&aco); - if (aborting()) { // autocmds may abort script processing retval = false; } @@ -5003,8 +5011,13 @@ void buf_reload(buf_T *buf, int orig_mode, bool reload_options) aco_save_T aco; int flags = READ_NEW; - // set curwin/curbuf for "buf" and save some things + // Set curwin/curbuf for "buf" and save some things. aucmd_prepbuf(&aco, buf); + if (curbuf != buf) { + // Failed to find a window for "buf", it is dangerous to continue, + // better bail out. + return; + } // Unless reload_options is set, we only want to read the text from the // file, not reset the syntax highlighting, clear marks, diff status, etc. diff --git a/src/nvim/globals.h b/src/nvim/globals.h index 737c92bc8c..9b4ef66b14 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -422,8 +422,19 @@ EXTERN win_T *prevwin INIT(= NULL); // previous window EXTERN win_T *curwin; // currently active window -EXTERN win_T *aucmd_win; // window used in aucmd_prepbuf() -EXTERN int aucmd_win_used INIT(= false); // aucmd_win is being used +/// When executing autocommands for a buffer that is not in any window, a +/// special window is created to handle the side effects. When autocommands +/// nest we may need more than one. Allow for up to five, if more are needed +/// something crazy is happening. +enum { AUCMD_WIN_COUNT = 5, }; + +typedef struct { + win_T *auc_win; ///< Window used in aucmd_prepbuf(). When not NULL the + ///< window has been allocated. + bool auc_win_used; ///< This auc_win is being used. +} aucmdwin_T; + +EXTERN aucmdwin_T aucmd_win[AUCMD_WIN_COUNT]; // The window layout is kept in a tree of frames. topframe points to the top // of the tree. diff --git a/src/nvim/main.c b/src/nvim/main.c index 78b59887e7..88ce8fb2e8 100644 --- a/src/nvim/main.c +++ b/src/nvim/main.c @@ -251,6 +251,8 @@ int main(int argc, char **argv) // `argc` and `argv` are also copied, so that they can be changed. init_params(¶ms, argc, argv); + autocmd_init(); + // Since os_open is called during the init_startuptime, we need to call // fs_init before it. fs_init(); diff --git a/src/nvim/quickfix.c b/src/nvim/quickfix.c index f083b6792b..be6ddede2e 100644 --- a/src/nvim/quickfix.c +++ b/src/nvim/quickfix.c @@ -3902,21 +3902,27 @@ static void qf_update_buffer(qf_info_T *qi, qfline_T *old_last) aco_save_T aco; + bool do_fill = true; if (old_last == NULL) { // set curwin/curbuf to buf and save a few things aucmd_prepbuf(&aco, buf); + if (curbuf != buf) { + do_fill = false; // failed to find a window for "buf" + } } - qf_update_win_titlevar(qi); + if (do_fill) { + qf_update_win_titlevar(qi); - qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid); - buf_inc_changedtick(buf); + qf_fill_buffer(qf_get_curlist(qi), buf, old_last, qf_winid); + buf_inc_changedtick(buf); - if (old_last == NULL) { - (void)qf_win_pos_update(qi, 0); + if (old_last == NULL) { + (void)qf_win_pos_update(qi, 0); - // restore curwin/curbuf and a few other things - aucmd_restbuf(&aco); + // restore curwin/curbuf and a few other things + aucmd_restbuf(&aco); + } } // Only redraw when added lines are visible. This avoids flickering when @@ -5463,9 +5469,11 @@ static int vgr_process_files(win_T *wp, qf_info_T *qi, vgr_args_T *cmd_args, boo // options! aco_save_T aco; aucmd_prepbuf(&aco, buf); - apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, buf->b_fname, true, buf); - do_modelines(OPT_NOWIN); - aucmd_restbuf(&aco); + if (curbuf == buf) { + apply_autocmds(EVENT_FILETYPE, buf->b_p_ft, buf->b_fname, true, buf); + do_modelines(OPT_NOWIN); + aucmd_restbuf(&aco); + } } } } @@ -5623,41 +5631,43 @@ static buf_T *load_dummy_buffer(char *fname, char *dirname_start, char *resultin // set curwin/curbuf to buf and save a few things aco_save_T aco; aucmd_prepbuf(&aco, newbuf); - - // Need to set the filename for autocommands. - (void)setfname(curbuf, fname, NULL, false); - - // Create swap file now to avoid the ATTENTION message. - check_need_swap(true); - - // Remove the "dummy" flag, otherwise autocommands may not - // work. - curbuf->b_flags &= ~BF_DUMMY; - - bufref_T newbuf_to_wipe; - newbuf_to_wipe.br_buf = NULL; - int readfile_result = readfile(fname, NULL, (linenr_T)0, (linenr_T)0, - (linenr_T)MAXLNUM, NULL, - READ_NEW | READ_DUMMY, false); - newbuf->b_locked--; - if (readfile_result == OK - && !got_int - && !(curbuf->b_flags & BF_NEW)) { - failed = false; - if (curbuf != newbuf) { - // Bloody autocommands changed the buffer! Can happen when - // using netrw and editing a remote file. Use the current - // buffer instead, delete the dummy one after restoring the - // window stuff. - set_bufref(&newbuf_to_wipe, newbuf); - newbuf = curbuf; + if (curbuf == newbuf) { + // Need to set the filename for autocommands. + (void)setfname(curbuf, fname, NULL, false); + + // Create swap file now to avoid the ATTENTION message. + check_need_swap(true); + + // Remove the "dummy" flag, otherwise autocommands may not + // work. + curbuf->b_flags &= ~BF_DUMMY; + + bufref_T newbuf_to_wipe; + newbuf_to_wipe.br_buf = NULL; + int readfile_result = readfile(fname, NULL, (linenr_T)0, (linenr_T)0, + (linenr_T)MAXLNUM, NULL, + READ_NEW | READ_DUMMY, false); + newbuf->b_locked--; + if (readfile_result == OK + && !got_int + && !(curbuf->b_flags & BF_NEW)) { + failed = false; + if (curbuf != newbuf) { + // Bloody autocommands changed the buffer! Can happen when + // using netrw and editing a remote file. Use the current + // buffer instead, delete the dummy one after restoring the + // window stuff. + set_bufref(&newbuf_to_wipe, newbuf); + newbuf = curbuf; + } } - } - // Restore curwin/curbuf and a few other things. - aucmd_restbuf(&aco); - if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) { - wipe_buffer(newbuf_to_wipe.br_buf, false); + // Restore curwin/curbuf and a few other things. + aucmd_restbuf(&aco); + + if (newbuf_to_wipe.br_buf != NULL && bufref_valid(&newbuf_to_wipe)) { + wipe_buffer(newbuf_to_wipe.br_buf, false); + } } // Add back the "dummy" flag, otherwise buflist_findname_file_id() diff --git a/src/nvim/terminal.c b/src/nvim/terminal.c index 206d9ac836..10a0c9c18c 100644 --- a/src/nvim/terminal.c +++ b/src/nvim/terminal.c @@ -387,7 +387,7 @@ void terminal_check_size(Terminal *term) // Check if there is a window that displays the terminal and find the maximum width and height. // Skip the autocommand window which isn't actually displayed. FOR_ALL_TAB_WINDOWS(tp, wp) { - if (wp == aucmd_win) { + if (is_aucmd_win(wp)) { continue; } if (wp->w_buffer && wp->w_buffer->terminal == term) { diff --git a/src/nvim/testdir/test_autocmd.vim b/src/nvim/testdir/test_autocmd.vim index 6fa191d928..199721b15e 100644 --- a/src/nvim/testdir/test_autocmd.vim +++ b/src/nvim/testdir/test_autocmd.vim @@ -3483,4 +3483,27 @@ func Test_autocmd_split_dummy() call delete('Xerr') endfunc +" This was crashing because there was only one window to execute autocommands +" in. +func Test_autocmd_nested_setbufvar() + CheckFeature python3 + + set hidden + edit Xaaa + edit Xbbb + call setline(1, 'bar') + enew + au BufWriteCmd Xbbb ++nested call setbufvar('Xaaa', '&ft', 'foo') | bw! Xaaa + au FileType foo call py3eval('vim.current.buffer.options["cindent"]') + wall + + au! BufWriteCmd + au! FileType foo + set nohidden + call delete('Xaaa') + call delete('Xbbb') + %bwipe! +endfunc + + " vim: shiftwidth=2 sts=2 expandtab diff --git a/src/nvim/window.c b/src/nvim/window.c index 5dd35537fa..2927855073 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -1046,8 +1046,8 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) { win_T *wp = new_wp; - // aucmd_win should always remain floating - if (new_wp != NULL && new_wp == aucmd_win) { + // aucmd_win[] should always remain floating + if (new_wp != NULL && is_aucmd_win(new_wp)) { return FAIL; } @@ -1505,7 +1505,7 @@ int win_split_ins(int size, int flags, win_T *new_wp, int dir) // equalize the window sizes. if (do_equal || dir != 0) { win_equal(wp, true, (flags & WSP_VERT) ? (dir == 'v' ? 'b' : 'h') : (dir == 'h' ? 'b' : 'v')); - } else if (*p_spk != 'c' && wp != aucmd_win) { + } else if (*p_spk != 'c' && !is_aucmd_win(wp)) { win_fix_scroll(false); } @@ -1940,7 +1940,7 @@ static void win_totop(int size, int flags) beep_flush(); return; } - if (curwin == aucmd_win) { + if (is_aucmd_win(curwin)) { return; } if (check_split_disallowed() == FAIL) { @@ -2086,7 +2086,7 @@ void win_equal(win_T *next_curwin, bool current, int dir) win_equal_rec(next_curwin == NULL ? curwin : next_curwin, current, topframe, dir, 0, tabline_height(), Columns, topframe->fr_height); - if (*p_spk != 'c' && next_curwin != aucmd_win) { + if (*p_spk != 'c' && !is_aucmd_win(next_curwin)) { win_fix_scroll(true); } } @@ -2459,7 +2459,7 @@ void close_windows(buf_T *buf, bool keep_curwin) // Start from lastwin to close floating windows with the same buffer first. // When the autocommand window is involved win_close() may need to print an error message. - for (win_T *wp = lastwin; wp != NULL && (lastwin == aucmd_win || !one_window(wp));) { + for (win_T *wp = lastwin; wp != NULL && (is_aucmd_win(lastwin) || !one_window(wp));) { if (wp->w_buffer == buf && (!keep_curwin || wp != curwin) && !(wp->w_closing || wp->w_buffer->b_locked > 0)) { if (win_close(wp, false, false) == FAIL) { @@ -2508,14 +2508,14 @@ bool last_window(win_T *win) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT return one_window(win) && first_tabpage->tp_next == NULL; } -/// Check that current tab page contains no more then one window other than `aucmd_win`. -/// @param counted_float counted even if floating, but not if it is `aucmd_win` +/// Check if current tab page contains no more than one window other than `aucmd_win[]`. +/// @param counted_float counted even if floating, but not if it is `aucmd_win[]` bool one_window(win_T *counted_float) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT { bool seen_one = false; FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { - if (wp != aucmd_win && (!wp->w_floating || wp == counted_float)) { + if (!is_aucmd_win(wp) && (!wp->w_floating || wp == counted_float)) { if (seen_one) { return false; } @@ -2545,7 +2545,7 @@ bool last_nonfloat(win_T *wp) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT /// @return true if all floating windows can be closed static bool can_close_floating_windows(void) { - assert(lastwin != aucmd_win); + assert(!is_aucmd_win(lastwin)); for (win_T *wp = lastwin; wp->w_floating; wp = wp->w_prev) { buf_T *buf = wp->w_buffer; int need_hide = (bufIsChanged(buf) && buf->b_nwindows <= 1); @@ -2663,12 +2663,12 @@ int win_close(win_T *win, bool free_buf, bool force) || (win->w_buffer != NULL && win->w_buffer->b_locked > 0)) { return FAIL; // window is already being closed } - if (win == aucmd_win) { + if (is_aucmd_win(win)) { emsg(_(e_autocmd_close)); return FAIL; } if (lastwin->w_floating && one_window(win)) { - if (lastwin == aucmd_win) { + if (is_aucmd_win(lastwin)) { emsg(_("E814: Cannot close window, only autocmd window would remain")); return FAIL; } @@ -3092,15 +3092,19 @@ void win_free_all(void) win_remove(lastwin, NULL); int dummy; (void)win_free_mem(wp, &dummy, NULL); - if (wp == aucmd_win) { - aucmd_win = NULL; + for (int i = 0; i < AUCMD_WIN_COUNT; i++) { + if (aucmd_win[i].auc_win == wp) { + aucmd_win[i].auc_win = NULL; + } } } - if (aucmd_win != NULL) { - int dummy; - (void)win_free_mem(aucmd_win, &dummy, NULL); - aucmd_win = NULL; + for (int i = 0; i < AUCMD_WIN_COUNT; i++) { + if (aucmd_win[i].auc_win != NULL) { + int dummy; + (void)win_free_mem(aucmd_win[i].auc_win, &dummy, NULL); + aucmd_win[i].auc_win = NULL; + } } while (firstwin != NULL) { @@ -3904,18 +3908,18 @@ void win_alloc_first(void) unuse_tabpage(first_tabpage); } -// Init `aucmd_win`. This can only be done after the first window +// Init `aucmd_win[idx]`. This can only be done after the first window // is fully initialized, thus it can't be in win_alloc_first(). -void win_alloc_aucmd_win(void) +void win_alloc_aucmd_win(int idx) { Error err = ERROR_INIT; FloatConfig fconfig = FLOAT_CONFIG_INIT; fconfig.width = Columns; fconfig.height = 5; fconfig.focusable = false; - aucmd_win = win_new_float(NULL, true, fconfig, &err); - aucmd_win->w_buffer->b_nwindows--; - RESET_BINDING(aucmd_win); + aucmd_win[idx].auc_win = win_new_float(NULL, true, fconfig, &err); + aucmd_win[idx].auc_win->w_buffer->b_nwindows--; + RESET_BINDING(aucmd_win[idx].auc_win); } // Allocate the first window or the first window in a new tab page. @@ -5108,7 +5112,7 @@ static void win_free(win_T *wp, tabpage_T *tp) win_free_grid(wp, false); - if (wp != aucmd_win) { + if (win_valid_any_tab(wp)) { win_remove(wp, tp); } if (autocmd_busy) { @@ -7059,7 +7063,7 @@ bool only_one_window(void) FUNC_ATTR_PURE FUNC_ATTR_WARN_UNUSED_RESULT FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { if (wp->w_buffer != NULL && (!((bt_help(wp->w_buffer) && !bt_help(curbuf)) || wp->w_floating - || wp->w_p_pvw) || wp == curwin) && wp != aucmd_win) { + || wp->w_p_pvw) || wp == curwin) && !is_aucmd_win(wp)) { count++; } } |