patch 9.1.0272: autocmd may change cwd after :tcd and :lcd

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

closes: #14435

Signed-off-by: zeertzjq <zeertzjq@outlook.com>
Signed-off-by: Christian Brabandt <cb@256bit.org>
diff --git a/src/autocmd.c b/src/autocmd.c
index b1c74cb..666cf45 100644
--- a/src/autocmd.c
+++ b/src/autocmd.c
@@ -1660,6 +1660,11 @@
 
 	win_init_popup_win(auc_win, buf);
 
+	// Make sure tp_localdir and globaldir are NULL to avoid a
+	// chdir() in win_enter_ext().
+	// win_init_popup_win() has already set w_localdir to NULL.
+	aco->tp_localdir = curtab->tp_localdir;
+	curtab->tp_localdir = NULL;
 	aco->globaldir = globaldir;
 	globaldir = NULL;
 
@@ -1773,6 +1778,12 @@
 	vars_clear(&awp->w_vars->dv_hashtab);  // free all w: variables
 	hash_init(&awp->w_vars->dv_hashtab);   // re-use the hashtab
 #endif
+	// 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();
+	vim_free(curtab->tp_localdir);
+	curtab->tp_localdir = aco->tp_localdir;
 	vim_free(globaldir);
 	globaldir = aco->globaldir;