From 4349c386db9a50aea717c6625babcbe15917741c Mon Sep 17 00:00:00 2001 From: "Justin M. Keyes" Date: Sat, 11 Jul 2026 13:29:59 -0400 Subject: [PATCH] refactor(ctx): drop switch_win/restore_win #40693 --- src/nvim/api/win_config.c | 35 ++++++++++++++++++----------------- src/nvim/api/window.c | 6 +++--- src/nvim/buffer.c | 10 +++++----- src/nvim/context_defs.h | 3 --- src/nvim/eval/vars.c | 12 ++++++------ src/nvim/eval/window.c | 28 ---------------------------- src/nvim/eval/window.h | 1 - src/nvim/window.c | 32 ++++++++++++++++---------------- 8 files changed, 48 insertions(+), 79 deletions(-) diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index b159acf7e6..56d757849b 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -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 diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 7e7bcd03e0..bb564400fc 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -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; diff --git a/src/nvim/buffer.c b/src/nvim/buffer.c index 85ae97ef01..1e6930b492 100644 --- a/src/nvim/buffer.c +++ b/src/nvim/buffer.c @@ -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) { diff --git a/src/nvim/context_defs.h b/src/nvim/context_defs.h index 3458b8851a..74cd8d9275 100644 --- a/src/nvim/context_defs.h +++ b/src/nvim/context_defs.h @@ -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; diff --git a/src/nvim/eval/vars.c b/src/nvim/eval/vars.c index 3f135e0e77..28deb7708b 100644 --- a/src/nvim/eval/vars.c +++ b/src/nvim/eval/vars.c @@ -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); } } diff --git a/src/nvim/eval/window.c b/src/nvim/eval/window.c index 0b779427e9..7206187417 100644 --- a/src/nvim/eval/window.c +++ b/src/nvim/eval/window.c @@ -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); -} diff --git a/src/nvim/eval/window.h b/src/nvim/eval/window.h index e5e58f25f7..e5534a41cc 100644 --- a/src/nvim/eval/window.h +++ b/src/nvim/eval/window.h @@ -3,7 +3,6 @@ #include #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" diff --git a/src/nvim/window.c b/src/nvim/window.c index 6369353380..94f6231e56 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -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); } }