vim-patch:9.1.0040: issue with prompt buffer and hidden buffer (#27071)

Problem:  Modifying a hidden buffer still interferes with prompt buffer
          mode changes.
Solution: Save and restore b_prompt_insert.
          (zeertzjq)

closes: vim/vim#13875

Modifying hidden buffer still interferes with prompt buffer mode changes

f267847017
This commit is contained in:
zeertzjq 2024-01-18 07:23:48 +08:00
parent d5e1bc5706
commit 773877e024
4 changed files with 42 additions and 12 deletions

View File

@ -1432,9 +1432,11 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf)
} }
aco->save_curwin_handle = curwin->handle; aco->save_curwin_handle = curwin->handle;
aco->save_curbuf = curbuf;
aco->save_prevwin_handle = prevwin == NULL ? 0 : prevwin->handle; aco->save_prevwin_handle = prevwin == NULL ? 0 : prevwin->handle;
aco->save_State = State; aco->save_State = State;
if (bt_prompt(curbuf)) {
aco->save_prompt_insert = curbuf->b_prompt_insert;
}
if (win != NULL) { if (win != NULL) {
// There is a window for "buf" in the current tab page, make it the // There is a window for "buf" in the current tab page, make it the
@ -1547,6 +1549,9 @@ win_found:
curbuf = curwin->w_buffer; curbuf = curwin->w_buffer;
// May need to restore insert mode for a prompt buffer. // May need to restore insert mode for a prompt buffer.
entering_window(curwin); entering_window(curwin);
if (bt_prompt(curbuf)) {
curbuf->b_prompt_insert = aco->save_prompt_insert;
}
prevwin = win_find_by_handle(aco->save_prevwin_handle); prevwin = win_find_by_handle(aco->save_prevwin_handle);
vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables

View File

@ -24,7 +24,6 @@ struct AutoPat_S;
// Struct to save values in before executing autocommands for a buffer that is // Struct to save values in before executing autocommands for a buffer that is
// not the current buffer. // not the current buffer.
typedef struct { typedef struct {
buf_T *save_curbuf; ///< saved curbuf
int use_aucmd_win_idx; ///< index in aucmd_win[] if >= 0 int use_aucmd_win_idx; ///< index in aucmd_win[] if >= 0
handle_T save_curwin_handle; ///< ID of saved curwin handle_T save_curwin_handle; ///< ID of saved curwin
handle_T new_curwin_handle; ///< ID of new curwin handle_T new_curwin_handle; ///< ID of new curwin
@ -33,6 +32,7 @@ typedef struct {
char *globaldir; ///< saved value of globaldir char *globaldir; ///< saved value of globaldir
bool save_VIsual_active; ///< saved VIsual_active bool save_VIsual_active; ///< saved VIsual_active
int save_State; ///< saved State int save_State; ///< saved State
int save_prompt_insert; ///< saved b_prompt_insert
} aco_save_T; } aco_save_T;
typedef struct AutoCmd_S AutoCmd; typedef struct AutoCmd_S AutoCmd;

View File

@ -265,7 +265,7 @@ describe('prompt buffer', function()
eq({ mode = 'i', blocking = false }, meths.get_mode()) eq({ mode = 'i', blocking = false }, meths.get_mode())
end) end)
-- oldtest: Test_prompt_close_modify_hidden() -- oldtest: Test_prompt_leave_modify_hidden()
it('modifying hidden buffer does not prevent prompt buffer mode change', function() it('modifying hidden buffer does not prevent prompt buffer mode change', function()
source([[ source([[
file hidden file hidden
@ -274,14 +274,26 @@ describe('prompt buffer', function()
new prompt new prompt
set buftype=prompt set buftype=prompt
inoremap <buffer> w <Cmd>wincmd w<CR>
inoremap <buffer> q <Cmd>bwipe!<CR> inoremap <buffer> q <Cmd>bwipe!<CR>
autocmd BufWinLeave prompt call setbufline('hidden', 1, 'Test') autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
]]) ]])
feed('a') feed('a')
eq({ mode = 'i', blocking = false }, meths.get_mode()) eq({ mode = 'i', blocking = false }, meths.get_mode())
feed('w')
eq({ mode = 'n', blocking = false }, meths.get_mode())
feed('<C-W>w')
eq({ mode = 'i', blocking = false }, meths.get_mode())
feed('q') feed('q')
eq({ mode = 'n', blocking = false }, meths.get_mode()) eq({ mode = 'n', blocking = false }, meths.get_mode())
command('bwipe!') command('bwipe!')
expect('Test') expect([[
Leave
Enter
Leave
Close]])
end) end)
end) end)

View File

@ -302,9 +302,10 @@ func Test_prompt_appending_while_hidden()
call StopVimInTerminal(buf) call StopVimInTerminal(buf)
endfunc endfunc
" Modifying a hidden buffer while closing a prompt buffer should not prevent " Modifying a hidden buffer while leaving a prompt buffer should not prevent
" stopping of Insert mode. " stopping of Insert mode, and returning to the prompt buffer later should
func Test_prompt_close_modify_hidden() " restore Insert mode.
func Test_prompt_leave_modify_hidden()
call CanTestPromptBuffer() call CanTestPromptBuffer()
let script =<< trim END let script =<< trim END
@ -314,22 +315,34 @@ func Test_prompt_close_modify_hidden()
new prompt new prompt
set buftype=prompt set buftype=prompt
inoremap <buffer> w <Cmd>wincmd w<CR>
inoremap <buffer> q <Cmd>bwipe!<CR> inoremap <buffer> q <Cmd>bwipe!<CR>
autocmd BufWinLeave prompt call setbufline('hidden', 1, 'Test') autocmd BufLeave prompt call appendbufline('hidden', '$', 'Leave')
autocmd BufEnter prompt call appendbufline('hidden', '$', 'Enter')
autocmd BufWinLeave prompt call appendbufline('hidden', '$', 'Close')
END END
call writefile(script, 'XpromptCloseModifyHidden', 'D') call writefile(script, 'XpromptLeaveModifyHidden', 'D')
let buf = RunVimInTerminal('-S XpromptCloseModifyHidden', {'rows': 10}) let buf = RunVimInTerminal('-S XpromptLeaveModifyHidden', {'rows': 10})
call TermWait(buf) call TermWait(buf)
call term_sendkeys(buf, "a") call term_sendkeys(buf, "a")
call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))}) call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
call term_sendkeys(buf, "w")
call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
call term_sendkeys(buf, "\<C-W>w")
call WaitForAssert({-> assert_match('-- INSERT --', term_getline(buf, 10))})
call term_sendkeys(buf, "q") call term_sendkeys(buf, "q")
call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))}) call WaitForAssert({-> assert_notmatch('-- INSERT --', term_getline(buf, 10))})
call term_sendkeys(buf, ":bwipe!\<CR>") call term_sendkeys(buf, ":bwipe!\<CR>")
call WaitForAssert({-> assert_equal('Test', term_getline(buf, 1))}) call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 2))})
call WaitForAssert({-> assert_equal('Enter', term_getline(buf, 3))})
call WaitForAssert({-> assert_equal('Leave', term_getline(buf, 4))})
call WaitForAssert({-> assert_equal('Close', term_getline(buf, 5))})
call StopVimInTerminal(buf) call StopVimInTerminal(buf)
endfunc endfunc