From 6b4970f6e0ac36021b2a8bd0533f5078040d31f7 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 23 Jul 2023 19:50:20 +0100 Subject: feat(api): allow open_win/win_set_buf in the cmdwin in some cases Problem: As discussed on Matrix, there was some interest in having `nvim_open_win` again be able to open floats in the cmdwin (e.g: displaying a hover doc related to what's in the cmdwin). After #23228, this was disallowed. Solution: Allow `nvim_open_win` in the cmdwin as long as `!enter` and `buffer != curbuf` (the former can cause all sorts of issues, and the latter can crash Nvim after closing cmdwin). Also allow `nvim_win_set_buf` in a similar fashion. Note that we're not *entirely* sure if this is 100% safe (cmdwin is a global-state-using-main-loop-calling beast), but this seems to work OK..? Also: - Check the buffer argument of `nvim_open_win` earlier, and abort if it's invalid (it used to still open a window in this case). - Untranslate `e_cmdwin` errors in the API (other errors in the API are not translated: although not detailed in the API contract yet, errors are supposed to be stable). --- src/nvim/api/vim.c | 2 +- src/nvim/api/win_config.c | 13 +++++++++++-- src/nvim/api/window.c | 15 ++++++++++++--- src/nvim/window.c | 14 ++++---------- 4 files changed, 28 insertions(+), 16 deletions(-) (limited to 'src') diff --git a/src/nvim/api/vim.c b/src/nvim/api/vim.c index 8738b3e38e..b4a6fa718b 100644 --- a/src/nvim/api/vim.c +++ b/src/nvim/api/vim.c @@ -967,7 +967,7 @@ Integer nvim_open_term(Buffer buffer, DictionaryOf(LuaRef) opts, Error *err) } if (cmdwin_type != 0 && buf == curbuf) { - api_set_error(err, kErrorTypeException, "%s", _(e_cmdwin)); + api_set_error(err, kErrorTypeException, "%s", e_cmdwin); return 0; } diff --git a/src/nvim/api/win_config.c b/src/nvim/api/win_config.c index 6ca36a0daf..81a239d913 100644 --- a/src/nvim/api/win_config.c +++ b/src/nvim/api/win_config.c @@ -158,8 +158,17 @@ /// @return Window handle, or 0 on error Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, Error *err) FUNC_API_SINCE(6) - FUNC_API_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { + buf_T *buf = find_buffer_by_handle(buffer, err); + if (!buf) { + return 0; + } + if (cmdwin_type != 0 && (enter || buf == curbuf)) { + api_set_error(err, kErrorTypeException, "%s", e_cmdwin); + return 0; + } + FloatConfig fconfig = FLOAT_CONFIG_INIT; if (!parse_float_config(config, &fconfig, false, true, err)) { return 0; @@ -173,7 +182,7 @@ Window nvim_open_win(Buffer buffer, Boolean enter, Dict(float_config) *config, E } // autocmds in win_enter or win_set_buf below may close the window if (win_valid(wp) && buffer > 0) { - win_set_buf(wp->handle, buffer, fconfig.noautocmd, err); + win_set_buf(wp, buf, fconfig.noautocmd, err); } if (!win_valid(wp)) { api_set_error(err, kErrorTypeException, "Window was closed immediately"); diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 5480584aa5..666f09e890 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -50,9 +50,18 @@ Buffer nvim_win_get_buf(Window window, Error *err) /// @param[out] err Error details, if any void nvim_win_set_buf(Window window, Buffer buffer, Error *err) FUNC_API_SINCE(5) - FUNC_API_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { - win_set_buf(window, buffer, false, err); + win_T *win = find_window_by_handle(window, err); + buf_T *buf = find_buffer_by_handle(buffer, err); + if (!win || !buf) { + return; + } + if (cmdwin_type != 0 && (win == curwin || buf == curbuf)) { + api_set_error(err, kErrorTypeException, "%s", e_cmdwin); + return; + } + win_set_buf(win, buf, false, err); } /// Gets the (1,0)-indexed, buffer-relative cursor position for a given window @@ -396,7 +405,7 @@ void nvim_win_close(Window window, Boolean force, Error *err) if (win == curwin) { cmdwin_result = Ctrl_C; } else { - api_set_error(err, kErrorTypeException, "%s", _(e_cmdwin)); + api_set_error(err, kErrorTypeException, "%s", e_cmdwin); } return; } diff --git a/src/nvim/window.c b/src/nvim/window.c index e230bde95c..fa7ca53b08 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -698,15 +698,9 @@ static void cmd_with_count(char *cmd, char *bufp, size_t bufsize, int64_t Prenum } } -void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err) +void win_set_buf(win_T *win, buf_T *buf, bool noautocmd, Error *err) + FUNC_ATTR_NONNULL_ALL { - win_T *win = find_window_by_handle(window, err); - buf_T *buf = find_buffer_by_handle(buffer, err); - - if (!win || !buf) { - return; - } - tabpage_T *tab = win_find_tabpage(win); // no redrawing and don't set the window title @@ -720,7 +714,7 @@ void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err) api_set_error(err, kErrorTypeException, "Failed to switch to window %d", - window); + win->handle); } try_start(); @@ -729,7 +723,7 @@ void win_set_buf(Window window, Buffer buffer, bool noautocmd, Error *err) api_set_error(err, kErrorTypeException, "Failed to set buffer %d", - buffer); + buf->handle); } // If window is not current, state logic will not validate its cursor. -- cgit From 5d921e28c1cc33eced22bbfa823460ca241e3dc1 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Sun, 23 Jul 2023 23:10:28 +0100 Subject: feat(api): allow win_close in cmdwin to close wins except previous Disallow closing the previous window from `nvim_win_close`, as this will cause issues. Again, no telling how safe this is. It also requires exposing old_curwin. :/ Also note that it's possible for the `&cmdheight` to change if, for example, there are 2 tabpages and `nvim_win_close` is used to close the last window in the other tabpage while `&stal` is 1. This is addressed in a later commit. --- src/nvim/api/window.c | 5 +++-- src/nvim/ex_getln.c | 3 +++ src/nvim/globals.h | 1 + 3 files changed, 7 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 666f09e890..350d934825 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -404,10 +404,11 @@ void nvim_win_close(Window window, Boolean force, Error *err) if (cmdwin_type != 0) { if (win == curwin) { cmdwin_result = Ctrl_C; - } else { + return; + } else if (win == cmdwin_old_curwin) { api_set_error(err, kErrorTypeException, "%s", e_cmdwin); + return; } - return; } tabpage_T *tabpage = win_find_tabpage(win); diff --git a/src/nvim/ex_getln.c b/src/nvim/ex_getln.c index 9dcfa99a37..5f1f5d5adc 100644 --- a/src/nvim/ex_getln.c +++ b/src/nvim/ex_getln.c @@ -4398,6 +4398,7 @@ static int open_cmdwin(void) // Set "cmdwin_type" before any autocommands may mess things up. cmdwin_type = get_cmdline_type(); cmdwin_level = ccline.level; + cmdwin_old_curwin = old_curwin; // Create empty command-line buffer. if (buf_open_scratch(0, _("[Command Line]")) == FAIL) { @@ -4405,6 +4406,7 @@ static int open_cmdwin(void) win_close(curwin, true, false); ga_clear(&winsizes); cmdwin_type = 0; + cmdwin_old_curwin = NULL; return Ctrl_C; } // Command-line buffer has bufhidden=wipe, unlike a true "scratch" buffer. @@ -4501,6 +4503,7 @@ static int open_cmdwin(void) cmdwin_type = 0; cmdwin_level = 0; + cmdwin_old_curwin = NULL; exmode_active = save_exmode; diff --git a/src/nvim/globals.h b/src/nvim/globals.h index dc7753f222..a1156a0196 100644 --- a/src/nvim/globals.h +++ b/src/nvim/globals.h @@ -831,6 +831,7 @@ EXTERN bool km_startsel INIT(= false); EXTERN int cmdwin_type INIT(= 0); ///< type of cmdline window or 0 EXTERN int cmdwin_result INIT(= 0); ///< result of cmdline window or 0 EXTERN int cmdwin_level INIT(= 0); ///< cmdline recursion level +EXTERN win_T *cmdwin_old_curwin INIT(= NULL); ///< curwin before opening cmdline window or NULL EXTERN char no_lines_msg[] INIT(= N_("--No lines in buffer--")); -- cgit From 472271199e483d3f23d62c272b20c5290eec5474 Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 24 Jul 2023 14:19:01 +0100 Subject: feat(api): allow win_hide to close cmdwin or non-previous windows This aligns its behaviour better with `nvim_win_close`. Note that `:hide` is actually incapable of closing the cmdwin, unlike `:close` and `:quit`, so this is a bit of a difference in behaviour. --- src/nvim/api/window.c | 16 +++------------- src/nvim/window.c | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/nvim/api/window.c b/src/nvim/api/window.c index 350d934825..f32a7e671d 100644 --- a/src/nvim/api/window.c +++ b/src/nvim/api/window.c @@ -362,10 +362,10 @@ Boolean nvim_win_is_valid(Window window) /// @param[out] err Error details, if any void nvim_win_hide(Window window, Error *err) FUNC_API_SINCE(7) - FUNC_API_TEXTLOCK + FUNC_API_TEXTLOCK_ALLOW_CMDWIN { win_T *win = find_window_by_handle(window, err); - if (!win) { + if (!win || !can_close_in_cmdwin(win, err)) { return; } @@ -397,20 +397,10 @@ void nvim_win_close(Window window, Boolean force, Error *err) FUNC_API_TEXTLOCK_ALLOW_CMDWIN { win_T *win = find_window_by_handle(window, err); - if (!win) { + if (!win || !can_close_in_cmdwin(win, err)) { return; } - if (cmdwin_type != 0) { - if (win == curwin) { - cmdwin_result = Ctrl_C; - return; - } else if (win == cmdwin_old_curwin) { - api_set_error(err, kErrorTypeException, "%s", e_cmdwin); - return; - } - } - tabpage_T *tabpage = win_find_tabpage(win); TryState tstate; try_enter(&tstate); diff --git a/src/nvim/window.c b/src/nvim/window.c index fa7ca53b08..d55bda18c8 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -2670,6 +2670,23 @@ static bool can_close_floating_windows(void) return true; } +/// @return true if, considering the cmdwin, `win` is safe to close. +/// If false and `win` is the cmdwin, it is closed; otherwise, `err` is set. +bool can_close_in_cmdwin(win_T *win, Error *err) + FUNC_ATTR_NONNULL_ALL +{ + if (cmdwin_type != 0) { + if (win == curwin) { + cmdwin_result = Ctrl_C; + return false; + } else if (win == cmdwin_old_curwin) { + api_set_error(err, kErrorTypeException, "%s", e_cmdwin); + return false; + } + } + return true; +} + /// Close the possibly last window in a tab page. /// /// @param win window to close -- cgit From a47be0b2d90b26905866faf5b7cc82d9c17be9bb Mon Sep 17 00:00:00 2001 From: Sean Dewar Date: Mon, 24 Jul 2023 11:56:26 +0100 Subject: fix(window): prevent win_size_restore from changing cmdheight Currently it only skips if `Rows` changed, but it's possible for the height of the usable area for windows to change (e.g: via `&ch`, `&stal` or `&ls`), which can cause the value of `&cmdheight` to change when the sizes are restored. This is a Vim bug, so I've submitted a PR there too. No telling when it'll be merged though, given the current lack of activity there. `ROWS_AVAIL` is convenient here, but also subtracts the `global_stl_height()`. Not ideal, as we also care about the height of the last statusline for other values of `&ls`. Meh. Introduce `last_stl_height` for getting the height of the last statusline and use it in `win_size_save/restore` and `last_status` (means `last_status_rec`'s `statusline` argument will now be true if `&ls` is 3, but that does not change the behaviour). Also corrects the logic in `comp_col` to not assume there's a last statusline if `&ls` is 1 and the last window is floating. --- src/nvim/drawscreen.c | 2 +- src/nvim/window.c | 21 +++++++++++++++------ 2 files changed, 16 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/nvim/drawscreen.c b/src/nvim/drawscreen.c index 1554a9304d..73dd584fb1 100644 --- a/src/nvim/drawscreen.c +++ b/src/nvim/drawscreen.c @@ -1137,7 +1137,7 @@ static void recording_mode(int attr) /// of 'ru_col'. void comp_col(void) { - int last_has_status = (p_ls > 1 || (p_ls == 1 && !ONE_WINDOW)); + bool last_has_status = last_stl_height(false) > 0; sc_col = 0; ru_col = 0; diff --git a/src/nvim/window.c b/src/nvim/window.c index d55bda18c8..d6d677de3f 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -5712,8 +5712,9 @@ void win_size_save(garray_T *gap) { ga_init(gap, (int)sizeof(int), 1); ga_grow(gap, win_count() * 2 + 1); - // first entry is value of 'lines' - ((int *)gap->ga_data)[gap->ga_len++] = Rows; + // first entry is the total lines available for windows + ((int *)gap->ga_data)[gap->ga_len++] = + (int)ROWS_AVAIL + global_stl_height() - last_stl_height(false); FOR_ALL_WINDOWS_IN_TAB(wp, curtab) { ((int *)gap->ga_data)[gap->ga_len++] = @@ -5723,13 +5724,14 @@ void win_size_save(garray_T *gap) } // Restore window sizes, but only if the number of windows is still the same -// and 'lines' didn't change. +// and total lines available for windows didn't change. // Does not free the growarray. void win_size_restore(garray_T *gap) FUNC_ATTR_NONNULL_ALL { if (win_count() * 2 + 1 == gap->ga_len - && ((int *)gap->ga_data)[0] == Rows) { + && ((int *)gap->ga_data)[0] == + (int)ROWS_AVAIL + global_stl_height() - last_stl_height(false)) { // The order matters, because frames contain other frames, but it's // difficult to get right. The easy way out is to do it twice. for (int j = 0; j < 2; j++) { @@ -6993,8 +6995,7 @@ char *file_name_in_line(char *line, int col, int options, int count, char *rel_f void last_status(bool morewin) { // Don't make a difference between horizontal or vertical split. - last_status_rec(topframe, (p_ls == 2 || (p_ls == 1 && (morewin || !one_nonfloat()))), - global_stl_height() > 0); + last_status_rec(topframe, last_stl_height(morewin) > 0, global_stl_height() > 0); } // Remove status line from window, replacing it with a horizontal separator if needed. @@ -7193,6 +7194,14 @@ int global_stl_height(void) return (p_ls == 3) ? STATUS_HEIGHT : 0; } +/// Return the height of the last window's statusline, or the global statusline if set. +/// +/// @param morewin pretend there are two or more windows if true. +int last_stl_height(bool morewin) +{ + return (p_ls > 1 || (p_ls == 1 && (!one_nonfloat() || morewin))) ? STATUS_HEIGHT : 0; +} + /// Return the minimal number of rows that is needed on the screen to display /// the current number of windows. int min_rows(void) -- cgit