refactor(ctx): drop switch_win/restore_win #40693

This commit is contained in:
Justin M. Keyes
2026-07-11 13:29:59 -04:00
committed by GitHub
parent 0f1e0a7900
commit 4349c386db
8 changed files with 48 additions and 79 deletions

View File

@@ -254,13 +254,13 @@ Window nvim_open_win(Buffer buf, Boolean enter, Dict(win_config) *config, Error
if (parent == NULL || parent == curwin) {
wp = win_split_ins(size, flags, NULL, 0, NULL);
} else {
switchwin_T switchwin;
// `parent` is valid in `tp`, so switch_win should not fail.
const int result = switch_win(&switchwin, parent, tp, true);
assert(result == OK);
CtxSwitch switchwin;
// `parent` is valid in `tp`, so ctx_switch should not fail.
const bool result = ctx_switch(&switchwin, parent, tp, NULL, kCtxNoEvents | kCtxNoDisplay);
assert(result);
(void)result;
wp = win_split_ins(size, flags, NULL, 0, NULL);
restore_win(&switchwin, true);
ctx_restore(&switchwin);
}
});
if (wp) {
@@ -297,19 +297,19 @@ Window nvim_open_win(Buffer buf, Boolean enter, Dict(win_config) *config, Error
}
// Autocommands may close `wp` or move it to another tabpage, so update and check `tp` after each
// event. In each case, `wp` should already be valid in `tp`, so switch_win should not fail.
// event. In each case, `wp` should already be valid in `tp`, so ctx_switch should not fail.
// Also, autocommands may free the `buf` to switch to, so store a bufref to check.
bufref_T bufref;
set_bufref(&bufref, b);
if (!fconfig.noautocmd) {
switchwin_T switchwin;
const int result = switch_win_noblock(&switchwin, wp, tp, true);
assert(result == OK);
CtxSwitch switchwin;
const bool result = ctx_switch(&switchwin, wp, tp, NULL, kCtxNoDisplay);
assert(result);
(void)result;
if (apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf)) {
tp = win_find_tabpage(wp);
}
restore_win_noblock(&switchwin, true);
ctx_restore(&switchwin);
}
if (tp && enter) {
goto_tabpage_win(tp, wp);
@@ -577,20 +577,21 @@ static bool win_config_split(win_T *win, const Dict(win_config) *config, WinConf
TRY_WRAP(err, {
const bool need_switch = parent != NULL && parent != curwin;
switchwin_T switchwin;
CtxSwitch switchwin;
if (need_switch) {
// `parent` is valid in its tabpage, so switch_win should not fail.
const int result = switch_win(&switchwin, parent, parent_tp, true);
// `parent` is valid in its tabpage, so ctx_switch should not fail.
const bool result = ctx_switch(&switchwin, parent, parent_tp, NULL,
kCtxNoEvents | kCtxNoDisplay);
(void)result;
assert(result == OK);
assert(result);
}
to_split_ok = win_split_ins(0, flags, win, 0, unflat_altfr) != NULL;
if (!to_split_ok) {
// Restore `win` to the window list now, so it's valid for restore_win (if used).
// Restore `win` to the window list now, so it's valid for ctx_restore (if used).
win_append(win->w_prev, win, win_tp == curtab ? NULL : win_tp);
}
if (need_switch) {
restore_win(&switchwin, true);
ctx_restore(&switchwin);
}
});
if (!to_split_ok) {
@@ -714,7 +715,7 @@ restore_curwin:
// Remove grid if present. More reliable than checking curtab, as tabpage_check_windows may not
// run when temporarily switching tabpages, meaning grids may be stale from another tabpage!
// (e.g: switch_win_noblock with no_display=true)
// (e.g: ctx_switch with kCtxNoDisplay)
ui_comp_remove_grid(&win->w_grid_alloc);
// Redraw tabline, update window's hl attribs, etc. Set must_redraw here, as redraw_later might

View File

@@ -134,11 +134,11 @@ void nvim_win_set_cursor(Window win, ArrayOf(Integer, 2) pos, Error *err)
// make sure cursor is in visible range and
// cursorcolumn and cursorline are updated even if w != curwin
switchwin_T switchwin;
switch_win(&switchwin, w, NULL, true);
CtxSwitch switchwin;
ctx_switch(&switchwin, w, NULL, NULL, kCtxNoEvents | kCtxNoDisplay);
update_topline(curwin);
validate_cursor(curwin);
restore_win(&switchwin, true);
ctx_restore(&switchwin);
redraw_later(w, UPD_VALID);
w->w_redr_status = true;

View File

@@ -1490,15 +1490,15 @@ static int do_buffer_ext(int action, int start, int dir, int count, int flags)
// Switch to buf's holder window without entering it: caller keeps focus,
// BufEnter doesn't fire for the deleted buffer.
// curwin must be floating: buf != curbuf, yet firstwin (the last non-float) shows buf.
// Also firstwin is valid in curtab, so switch_win_noblock should not fail.
// Also firstwin is valid in curtab, so ctx_switch should not fail.
assert(curwin->w_floating);
switchwin_T switchwin;
const int rv = switch_win_noblock(&switchwin, firstwin, curtab, true);
assert(rv == OK);
CtxSwitch switchwin;
const bool rv = ctx_switch(&switchwin, firstwin, curtab, NULL, kCtxNoDisplay);
assert(rv);
(void)rv;
// retry (recurse)
do_buffer_ext(action, start, dir, count, flags);
restore_win_noblock(&switchwin, true);
ctx_restore(&switchwin);
}
if (buf != curbuf && bufref_valid(&bufref) && buf->b_nwindows <= 0) {

View File

@@ -89,6 +89,3 @@ typedef struct {
bool cs_apply_acd; ///< re-apply 'autochdir' on ctx_restore()
char *cs_save_sfname; ///< saved b_sfname (kCtxKeepCwd)
} CtxSwitch;
// TODO(justinmk): legacy alias, to avoid churn. Will be unalived soooon.
typedef CtxSwitch switchwin_T;

View File

@@ -3097,8 +3097,8 @@ static void get_var_from(const char *varname, typval_T *rettv, typval_T *deftv,
// If we have a buffer reference avoid the switching, we're saving and
// restoring curbuf directly.
const bool need_switch_win = !(tp == curtab && win == curwin) && !do_change_curbuf;
switchwin_T switchwin;
if (!need_switch_win || switch_win(&switchwin, win, tp, true) == OK) {
CtxSwitch switchwin;
if (!need_switch_win || ctx_switch(&switchwin, win, tp, NULL, kCtxNoEvents | kCtxNoDisplay)) {
if (*varname == '&' && htname != 't') {
buf_T *const save_curbuf = curbuf;
@@ -3155,7 +3155,7 @@ static void get_var_from(const char *varname, typval_T *rettv, typval_T *deftv,
if (need_switch_win) {
// restore previous notion of curwin
restore_win(&switchwin, true);
ctx_restore(&switchwin);
}
}
@@ -3327,8 +3327,8 @@ static void setwinvar(typval_T *argvars, int off)
}
bool need_switch_win = !(tp == curtab && win == curwin);
switchwin_T switchwin;
if (!need_switch_win || switch_win(&switchwin, win, tp, true) == OK) {
CtxSwitch switchwin;
if (!need_switch_win || ctx_switch(&switchwin, win, tp, NULL, kCtxNoEvents | kCtxNoDisplay)) {
if (*varname == '&') {
set_option_from_tv(varname + 1, varp);
} else {
@@ -3341,7 +3341,7 @@ static void setwinvar(typval_T *argvars, int off)
}
}
if (need_switch_win) {
restore_win(&switchwin, true);
ctx_restore(&switchwin);
}
}

View File

@@ -890,31 +890,3 @@ void f_winwidth(typval_T *argvars, typval_T *rettv, EvalFuncData fptr)
rettv->vval.v_number = wp->w_view_width;
}
}
/// TODO(justinmk): legacy shim, use ctx_switch() directly.
int switch_win(switchwin_T *switchwin, win_T *win, tabpage_T *tp, bool no_display)
{
return ctx_switch(switchwin, win, tp, NULL, kCtxNoEvents | (no_display ? kCtxNoDisplay : 0))
? OK : FAIL;
}
// TODO(justinmk): legacy shim, use ctx_switch() directly.
int switch_win_noblock(switchwin_T *switchwin, win_T *win, tabpage_T *tp, bool no_display)
{
return ctx_switch(switchwin, win, tp, NULL, no_display ? kCtxNoDisplay : 0)
? OK : FAIL;
}
/// TODO(justinmk): legacy shim, use ctx_restore() directly.
void restore_win(switchwin_T *switchwin, bool no_display)
{
(void)no_display; // recorded by ctx_switch()
ctx_restore(switchwin);
}
/// TODO(justinmk): legacy shim, use ctx_restore() directly.
void restore_win_noblock(switchwin_T *switchwin, bool no_display)
{
(void)no_display; // recorded by ctx_switch()
ctx_restore(switchwin);
}

View File

@@ -3,7 +3,6 @@
#include <stdbool.h>
#include "nvim/buffer_defs.h"
#include "nvim/context_defs.h" // IWYU pragma: keep (switchwin_T)
#include "nvim/eval/typval_defs.h" // IWYU pragma: keep
#include "nvim/os/os_defs.h"
#include "nvim/pos_defs.h"

View File

@@ -799,12 +799,12 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
// no redrawing and don't set the window title
RedrawingDisabled++;
switchwin_T switchwin;
int win_result;
CtxSwitch switchwin;
bool win_ok;
TRY_WRAP(err, {
win_result = switch_win_noblock(&switchwin, win, tab, true);
if (win_result != FAIL) {
win_ok = ctx_switch(&switchwin, win, tab, NULL, kCtxNoDisplay);
if (win_ok) {
const int save_acd = p_acd;
if (!switchwin.cs_same_win) {
// Temporarily disable 'autochdir' when setting buffer in another window.
@@ -818,7 +818,7 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
}
}
});
if (win_result == FAIL && !ERROR_SET(err)) {
if (!win_ok && !ERROR_SET(err)) {
api_set_error(err, kErrorTypeException, "Failed to switch to window %d", win_handle);
}
@@ -826,7 +826,7 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
// Still needed if do_buffer returns FAIL (e.g: autocmds abort script after buffer was set).
validate_cursor(curwin);
restore_win_noblock(&switchwin, true);
ctx_restore(&switchwin);
RedrawingDisabled--;
}
@@ -4305,7 +4305,7 @@ void unuse_tabpage(tabpage_T *tp)
tp->tp_curwin = curwin;
// Set this tab's stored cmdheight so use_tabpage() can restore it later.
// command_height() and win_new_screen_rows() also keep tp_ch_used in sync for the current tab
// between tab switches; this catches the no-display switch_win() path which bypasses them.
// between tab switches; this catches the no-display ctx_switch() path which bypasses them.
tp->tp_ch_used = p_ch;
}
@@ -4560,17 +4560,17 @@ tabpage_T *win_new_tabpage(int after, char *filename, bool enter, win_T **first)
win_new_screen_rows();
}
// Trigger autocommands in the context of the new window. Let switch_win_noblock handle stuff
// Trigger autocommands in the context of the new window. Let ctx_switch handle stuff
// like temporarily resetting VIsual_active.
switchwin_T switchwin;
const int sw_result = switch_win_noblock(&switchwin, newtp->tp_curwin, newtp, true);
assert(sw_result == OK); // tp_curwin is valid in newtp
(void)sw_result;
CtxSwitch switchwin;
const bool sw_ok = ctx_switch(&switchwin, newtp->tp_curwin, newtp, NULL, kCtxNoDisplay);
assert(sw_ok); // tp_curwin is valid in newtp
(void)sw_ok;
apply_autocmds(EVENT_WINNEW, NULL, NULL, false, curbuf);
apply_autocmds(EVENT_TABNEW, filename, filename, false, curbuf);
restore_win_noblock(&switchwin, true);
ctx_restore(&switchwin);
}
return newtp;
@@ -7367,12 +7367,12 @@ void set_winbar_all(bool make_room)
continue;
}
switchwin_T switchwin;
CtxSwitch switchwin;
// Use a no-display switch so the hidden tab's frame and window globals are active.
if (switch_win(&switchwin, tp->tp_curwin, tp, true) == OK) {
if (ctx_switch(&switchwin, tp->tp_curwin, tp, NULL, kCtxNoEvents | kCtxNoDisplay)) {
(void)set_winbar_curtab(make_room, false);
}
restore_win(&switchwin, true);
ctx_restore(&switchwin);
}
}