diff options
author | Sean Dewar <seandewar@users.noreply.github.com> | 2023-07-26 21:27:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-26 21:27:35 +0100 |
commit | 8fe9f41f7f9da2009d11855ec0548b9dbe548a69 (patch) | |
tree | 8759d99d2b3f034449a0ac73dd0d77736d1c2aec /src | |
parent | ccf328172bac2b02f9bd19fa58e105958514a28a (diff) | |
parent | a47be0b2d90b26905866faf5b7cc82d9c17be9bb (diff) | |
download | rneovim-8fe9f41f7f9da2009d11855ec0548b9dbe548a69.tar.gz rneovim-8fe9f41f7f9da2009d11855ec0548b9dbe548a69.tar.bz2 rneovim-8fe9f41f7f9da2009d11855ec0548b9dbe548a69.zip |
Merge pull request #24457 from seandewar/relax-cmdwin
feat(api): relax cmdwin restrictions for a few functions
Diffstat (limited to 'src')
-rw-r--r-- | src/nvim/api/vim.c | 2 | ||||
-rw-r--r-- | src/nvim/api/win_config.c | 13 | ||||
-rw-r--r-- | src/nvim/api/window.c | 28 | ||||
-rw-r--r-- | src/nvim/drawscreen.c | 2 | ||||
-rw-r--r-- | src/nvim/ex_getln.c | 3 | ||||
-rw-r--r-- | src/nvim/globals.h | 1 | ||||
-rw-r--r-- | src/nvim/window.c | 52 |
7 files changed, 67 insertions, 34 deletions
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..f32a7e671d 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 @@ -353,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; } @@ -388,16 +397,7 @@ 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) { - return; - } - - if (cmdwin_type != 0) { - if (win == curwin) { - cmdwin_result = Ctrl_C; - } else { - api_set_error(err, kErrorTypeException, "%s", _(e_cmdwin)); - } + if (!win || !can_close_in_cmdwin(win, err)) { return; } 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/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--")); diff --git a/src/nvim/window.c b/src/nvim/window.c index e230bde95c..d6d677de3f 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. @@ -2676,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 @@ -5701,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++] = @@ -5712,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++) { @@ -6982,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. @@ -7182,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) |