0
0
mirror of https://github.com/vim/vim.git synced 2025-07-26 11:04:33 -04:00

patch 8.1.0602: DirChanged is also triggered when directory didn't change

Problem:    DirChanged is also triggered when the directory didn't change.
            (Daniel Hahler)
Solution:   Compare the current with the new directory. (closes #3697)
This commit is contained in:
Bram Moolenaar 2018-12-16 15:38:02 +01:00
parent 4efe73b478
commit 2caad3fbbd
5 changed files with 61 additions and 27 deletions

View File

@ -9126,8 +9126,9 @@ post_chdir(int local)
void void
ex_cd(exarg_T *eap) ex_cd(exarg_T *eap)
{ {
char_u *new_dir; char_u *new_dir;
char_u *tofree; char_u *tofree;
int dir_differs;
new_dir = eap->arg; new_dir = eap->arg;
#if !defined(UNIX) && !defined(VMS) #if !defined(UNIX) && !defined(VMS)
@ -9183,7 +9184,9 @@ ex_cd(exarg_T *eap)
new_dir = NameBuff; new_dir = NameBuff;
} }
#endif #endif
if (new_dir == NULL || vim_chdir(new_dir)) dir_differs = new_dir == NULL || prev_dir == NULL
|| STRCMP(prev_dir, new_dir) != 0;
if (new_dir == NULL || (dir_differs && vim_chdir(new_dir)))
EMSG(_(e_failed)); EMSG(_(e_failed));
else else
{ {
@ -9195,9 +9198,11 @@ ex_cd(exarg_T *eap)
/* Echo the new current directory if the command was typed. */ /* Echo the new current directory if the command was typed. */
if (KeyTyped || p_verbose >= 5) if (KeyTyped || p_verbose >= 5)
ex_pwd(eap); ex_pwd(eap);
apply_autocmds(EVENT_DIRCHANGED,
is_local_chdir ? (char_u *)"window" : (char_u *)"global", if (dir_differs)
new_dir, FALSE, curbuf); apply_autocmds(EVENT_DIRCHANGED,
is_local_chdir ? (char_u *)"window" : (char_u *)"global",
new_dir, FALSE, curbuf);
} }
vim_free(tofree); vim_free(tofree);
} }

View File

@ -3390,17 +3390,29 @@ same_directory(char_u *f1, char_u *f2)
* Return OK or FAIL. * Return OK or FAIL.
*/ */
int int
vim_chdirfile(char_u *fname, char *trigger_autocmd UNUSED) vim_chdirfile(char_u *fname, char *trigger_autocmd)
{ {
char_u dir[MAXPATHL]; char_u old_dir[MAXPATHL];
char_u new_dir[MAXPATHL];
int res; int res;
vim_strncpy(dir, fname, MAXPATHL - 1); if (mch_dirname(old_dir, MAXPATHL) != OK)
*gettail_sep(dir) = NUL; *old_dir = NUL;
res = mch_chdir((char *)dir) == 0 ? OK : FAIL;
if (res == OK && trigger_autocmd != NULL) vim_strncpy(new_dir, fname, MAXPATHL - 1);
apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd, *gettail_sep(new_dir) = NUL;
dir, FALSE, curbuf);
if (STRCMP(old_dir, new_dir) == 0)
// nothing to do
res = OK;
else
{
res = mch_chdir((char *)new_dir) == 0 ? OK : FAIL;
if (res == OK && trigger_autocmd != NULL)
apply_autocmds(EVENT_DIRCHANGED, (char_u *)trigger_autocmd,
new_dir, FALSE, curbuf);
}
return res; return res;
} }
#endif #endif

View File

@ -8,11 +8,19 @@ func Test_set_filename()
let cwd = getcwd() let cwd = getcwd()
call test_autochdir() call test_autochdir()
set acd set acd
let s:li = []
autocmd DirChanged auto call add(s:li, "autocd")
autocmd DirChanged auto call add(s:li, expand("<afile>"))
new new
w samples/Xtest w samples/Xtest
call assert_equal("Xtest", expand('%')) call assert_equal("Xtest", expand('%'))
call assert_equal("samples", substitute(getcwd(), '.*/\(\k*\)', '\1', '')) call assert_equal("samples", substitute(getcwd(), '.*/\(\k*\)', '\1', ''))
call assert_equal(["autocd", getcwd()], s:li)
bwipe! bwipe!
au! DirChanged
set noacd set noacd
exe 'cd ' . cwd exe 'cd ' . cwd
call delete('samples/Xtest') call delete('samples/Xtest')

View File

@ -1205,13 +1205,16 @@ function s:Before_test_dirchanged()
augroup END augroup END
let s:li = [] let s:li = []
let s:dir_this = getcwd() let s:dir_this = getcwd()
let s:dir_other = s:dir_this . '/foo' let s:dir_foo = s:dir_this . '/foo'
call mkdir(s:dir_other) call mkdir(s:dir_foo)
let s:dir_bar = s:dir_this . '/bar'
call mkdir(s:dir_bar)
endfunc endfunc
function s:After_test_dirchanged() function s:After_test_dirchanged()
exe 'cd' s:dir_this exe 'cd' s:dir_this
call delete(s:dir_other, 'd') call delete(s:dir_foo, 'd')
call delete(s:dir_bar, 'd')
augroup test_dirchanged augroup test_dirchanged
autocmd! autocmd!
augroup END augroup END
@ -1221,10 +1224,12 @@ function Test_dirchanged_global()
call s:Before_test_dirchanged() call s:Before_test_dirchanged()
autocmd test_dirchanged DirChanged global call add(s:li, "cd:") autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>")) autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
exe 'cd' s:dir_other exe 'cd' s:dir_foo
call assert_equal(["cd:", s:dir_other], s:li) call assert_equal(["cd:", s:dir_foo], s:li)
exe 'lcd' s:dir_other exe 'cd' s:dir_foo
call assert_equal(["cd:", s:dir_other], s:li) call assert_equal(["cd:", s:dir_foo], s:li)
exe 'lcd' s:dir_bar
call assert_equal(["cd:", s:dir_foo], s:li)
call s:After_test_dirchanged() call s:After_test_dirchanged()
endfunc endfunc
@ -1232,10 +1237,12 @@ function Test_dirchanged_local()
call s:Before_test_dirchanged() call s:Before_test_dirchanged()
autocmd test_dirchanged DirChanged window call add(s:li, "lcd:") autocmd test_dirchanged DirChanged window call add(s:li, "lcd:")
autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>")) autocmd test_dirchanged DirChanged window call add(s:li, expand("<afile>"))
exe 'cd' s:dir_other exe 'cd' s:dir_foo
call assert_equal([], s:li) call assert_equal([], s:li)
exe 'lcd' s:dir_other exe 'lcd' s:dir_bar
call assert_equal(["lcd:", s:dir_other], s:li) call assert_equal(["lcd:", s:dir_bar], s:li)
exe 'lcd' s:dir_bar
call assert_equal(["lcd:", s:dir_bar], s:li)
call s:After_test_dirchanged() call s:After_test_dirchanged()
endfunc endfunc
@ -1250,9 +1257,9 @@ function Test_dirchanged_auto()
set acd set acd
exe 'cd ..' exe 'cd ..'
call assert_equal([], s:li) call assert_equal([], s:li)
exe 'edit ' . s:dir_other . '/Xfile' exe 'edit ' . s:dir_foo . '/Xfile'
call assert_equal(s:dir_other, getcwd()) call assert_equal(s:dir_foo, getcwd())
call assert_equal(["auto:", s:dir_other], s:li) call assert_equal(["auto:", s:dir_foo], s:li)
set noacd set noacd
bwipe! bwipe!
call s:After_test_dirchanged() call s:After_test_dirchanged()

View File

@ -799,6 +799,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
602,
/**/ /**/
601, 601,
/**/ /**/