Merge pull request #24457 from seandewar/relax-cmdwin

feat(api): relax cmdwin restrictions for a few functions
This commit is contained in:
Sean Dewar 2023-07-26 21:27:35 +01:00 committed by GitHub
commit 8fe9f41f7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 174 additions and 43 deletions

View File

@ -2897,7 +2897,7 @@ nvim_win_hide({window}) *nvim_win_hide()*
or |nvim_win_close()|, which will close the buffer.
Attributes: ~
not allowed when |textlock| is active or in the |cmdwin|
not allowed when |textlock| is active
Parameters: ~
• {window} Window handle, or 0 for current window
@ -2915,7 +2915,7 @@ nvim_win_set_buf({window}, {buffer}) *nvim_win_set_buf()*
Sets the current buffer in a window, without side effects
Attributes: ~
not allowed when |textlock| is active or in the |cmdwin|
not allowed when |textlock| is active
Parameters: ~
• {window} Window handle, or 0 for current window
@ -3036,7 +3036,7 @@ nvim_open_win({buffer}, {enter}, {*config}) *nvim_open_win()*
<
Attributes: ~
not allowed when |textlock| is active or in the |cmdwin|
not allowed when |textlock| is active
Parameters: ~
• {buffer} Buffer to display, or 0 for current buffer

View File

@ -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;
}

View File

@ -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");

View File

@ -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;
}

View File

@ -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;

View File

@ -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;

View File

@ -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--"));

View File

@ -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)

View File

@ -43,6 +43,22 @@ describe('API/win', function()
eq('Invalid buffer id: 23', pcall_err(window, 'set_buf', nvim('get_current_win'), 23))
eq('Invalid window id: 23', pcall_err(window, 'set_buf', 23, nvim('get_current_buf')))
end)
it('disallowed in cmdwin if win=curwin or buf=curbuf', function()
local new_buf = meths.create_buf(true, true)
local new_win = meths.open_win(new_buf, false, {
relative='editor', row=10, col=10, width=50, height=10,
})
feed('q:')
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_set_buf, 0, new_buf))
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_set_buf, new_win, 0))
local next_buf = meths.create_buf(true, true)
meths.win_set_buf(new_win, next_buf)
eq(next_buf, meths.win_get_buf(new_win))
end)
end)
describe('{get,set}_cursor', function()
@ -524,13 +540,19 @@ describe('API/win', function()
command('split')
eq(2, #meths.list_wins())
local oldwin = meths.get_current_win()
local otherwin = meths.open_win(0, false, {
relative='editor', row=10, col=10, width=10, height=10,
})
-- Open cmdline-window.
feed('q:')
eq(3, #meths.list_wins())
eq(4, #meths.list_wins())
eq(':', funcs.getcmdwintype())
-- Vim: not allowed to close other windows from cmdline-window.
-- Not allowed to close previous window from cmdline-window.
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_close, oldwin, true))
-- Closing other windows is fine.
meths.win_close(otherwin, true)
eq(false, meths.win_is_valid(otherwin))
-- Close cmdline-window.
meths.win_close(0, true)
eq(2, #meths.list_wins())
@ -593,6 +615,24 @@ describe('API/win', function()
eq({oldwin}, meths.list_wins())
eq({oldbuf}, meths.list_bufs())
end)
it('in the cmdwin', function()
feed('q:')
-- Can close the cmdwin.
meths.win_hide(0)
eq('', funcs.getcmdwintype())
local old_win = meths.get_current_win()
local other_win = meths.open_win(0, false, {
relative='win', row=3, col=3, width=12, height=3
})
feed('q:')
-- Cannot close the previous window.
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_hide, old_win))
-- Can close other windows.
meths.win_hide(other_win)
eq(false, meths.win_is_valid(other_win))
end)
end)
describe('text_height', function()
@ -821,6 +861,31 @@ describe('API/win', function()
})
eq(1, funcs.exists('g:fired'))
end)
it('disallowed in cmdwin if enter=true or buf=curbuf', function()
local new_buf = meths.create_buf(true, true)
feed('q:')
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.open_win, new_buf, true, {
relative='editor', row=5, col=5, width=5, height=5,
}))
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.open_win, 0, false, {
relative='editor', row=5, col=5, width=5, height=5,
}))
eq(new_buf, meths.win_get_buf(meths.open_win(new_buf, false, {
relative='editor', row=5, col=5, width=5, height=5,
})))
end)
it('aborts if buffer is invalid', function()
local wins_before = meths.list_wins()
eq('Invalid buffer id: 1337', pcall_err(meths.open_win, 1337, false, {
relative='editor', row=5, col=5, width=5, height=5,
}))
eq(wins_before, meths.list_wins())
end)
end)
describe('get_config', function()

View File

@ -984,14 +984,46 @@ it('tabline is not redrawn in Ex mode #24122', function()
end)
describe("cmdline height", function()
before_each(clear)
it("does not crash resized screen #14263", function()
clear()
local screen = Screen.new(25, 10)
screen:attach()
command('set cmdheight=9999')
screen:try_resize(25, 5)
assert_alive()
end)
it('unchanged when trying to restore window sizes', function()
command('set showtabline=0 cmdheight=2 laststatus=0')
feed('q:') -- Closing cmdwin tries to restore sizes
command('set cmdheight=1 | quit')
eq(1, eval('&cmdheight'))
command('set showtabline=2 cmdheight=3')
feed('q:')
command('set showtabline=0 | quit')
eq(3, eval('&cmdheight'))
command('set cmdheight=1 laststatus=2')
feed('q:')
command('set laststatus=0 | quit')
eq(1, eval('&cmdheight'))
command('set laststatus=2')
feed('q:')
command('set laststatus=1 | quit')
eq(1, eval('&cmdheight'))
command('set laststatus=2 | belowright vsplit | wincmd _')
local restcmds = eval('winrestcmd()')
feed('q:')
command('set laststatus=1 | quit')
-- As we have 2 windows, &ls = 1 should still have a statusline on the last
-- window. As such, the number of available rows hasn't changed and the window
-- sizes should be restored.
eq(restcmds, eval('winrestcmd()'))
end)
end)
describe('cmdheight=0', function()

View File

@ -73,9 +73,10 @@ describe('eval-API', function()
-- Some functions checking textlock (usually those that may change the current window or buffer)
-- also ought to not be usable in the cmdwin.
local old_win = meths.get_current_win()
feed("q:")
eq('E11: Invalid in command-line window; <CR> executes, CTRL-C quits',
pcall_err(meths.win_hide, 0))
pcall_err(meths.set_current_win, old_win))
-- But others, like nvim_buf_set_lines(), which just changes text, is OK.
curbufmeths.set_lines(0, -1, 1, {"wow!"})