vim-patch:9.1.0272: autocmd may change cwd after :tcd and :lcd (#28223)

Problem:  Autocommand may change currect directory after :tcd and :lcd.
Solution: Also clear tp_localdir and w_localdir when using aucmd_win.
          (zeertzjq)

closes: vim/vim#14435

9d956ee8ea
This commit is contained in:
zeertzjq 2024-04-08 06:25:22 +08:00 committed by GitHub
parent d32cbef595
commit d188b929d4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 57 additions and 4 deletions

View File

@ -1325,9 +1325,11 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf)
buf->b_nwindows++;
win_init_empty(auc_win); // set cursor and topline to safe values
// Make sure w_localdir and globaldir are NULL to avoid a chdir() in
// win_enter_ext().
// Make sure w_localdir, tp_localdir and globaldir are NULL to avoid a
// chdir() in win_enter_ext().
XFREE_CLEAR(auc_win->w_localdir);
aco->tp_localdir = curtab->tp_localdir;
curtab->tp_localdir = NULL;
aco->globaldir = globaldir;
globaldir = NULL;
@ -1427,6 +1429,13 @@ win_found:
vars_clear(&awp->w_vars->dv_hashtab); // free all w: variables
hash_init(&awp->w_vars->dv_hashtab); // re-use the hashtab
// If :lcd has been used in the autocommand window, correct current
// directory before restoring tp_localdir and globaldir.
if (awp->w_localdir != NULL) {
win_fix_current_dir();
}
xfree(curtab->tp_localdir);
curtab->tp_localdir = aco->tp_localdir;
xfree(globaldir);
globaldir = aco->globaldir;

View File

@ -19,6 +19,7 @@ typedef struct {
handle_T new_curwin_handle; ///< ID of new curwin
handle_T save_prevwin_handle; ///< ID of saved prevwin
bufref_T new_curbuf; ///< new curbuf
char *tp_localdir; ///< saved value of tp_localdir
char *globaldir; ///< saved value of globaldir
bool save_VIsual_active; ///< saved VIsual_active
int save_State; ///< saved State

View File

@ -4936,7 +4936,7 @@ static void win_enter_ext(win_T *const wp, const int flags)
win_fix_cursor(get_real_state() & (MODE_NORMAL|MODE_CMDLINE|MODE_TERMINAL));
}
fix_current_dir();
win_fix_current_dir();
entering_window(curwin);
// Careful: autocommands may close the window and make "wp" invalid
@ -4989,7 +4989,7 @@ static void win_enter_ext(win_T *const wp, const int flags)
}
/// Used after making another window the current one: change directory if needed.
void fix_current_dir(void)
void win_fix_current_dir(void)
{
// New directory is either the local directory of the window, tab or NULL.
char *new_dir = curwin->w_localdir ? curwin->w_localdir : curtab->tp_localdir;

View File

@ -3522,6 +3522,49 @@ func Test_switch_window_in_autocmd_window()
call assert_false(bufexists('Xb.txt'))
endfunc
" Test that using the autocommand window doesn't change current directory.
func Test_autocmd_window_cwd()
let saveddir = getcwd()
call mkdir('Xcwd/a/b/c/d', 'pR')
new Xa.txt
tabnew
new Xb.txt
tabprev
cd Xcwd
call assert_match('/Xcwd$', getcwd())
call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
autocmd BufEnter Xb.txt lcd ./a/b/c/d
doautoall BufEnter
au! BufEnter
call assert_match('/Xcwd$', getcwd())
call assert_match('\[global\] .*/Xcwd$', trim(execute('verbose pwd')))
tabnext
cd ./a
tcd ./b
lcd ./c
call assert_match('/Xcwd/a/b/c$', getcwd())
call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
autocmd BufEnter Xa.txt call assert_match('Xcwd/a/b/c$', getcwd())
doautoall BufEnter
au! BufEnter
call assert_match('/Xcwd/a/b/c$', getcwd())
call assert_match('\[window\] .*/Xcwd/a/b/c$', trim(execute('verbose pwd')))
bwipe!
call assert_match('/Xcwd/a/b$', getcwd())
call assert_match('\[tabpage\] .*/Xcwd/a/b$', trim(execute('verbose pwd')))
bwipe!
call assert_match('/Xcwd/a$', getcwd())
call assert_match('\[global\] .*/Xcwd/a$', trim(execute('verbose pwd')))
bwipe!
call chdir(saveddir)
endfunc
func Test_bufwipeout_changes_window()
" This should not crash, but we don't have any expectations about what
" happens, changing window in BufWipeout has unpredictable results.