Add v:event flag on DirChanged signaling switching window (#13153)

Closes #9909
This commit is contained in:
Andrea Cedraro 2020-11-07 18:02:06 +01:00 committed by GitHub
parent 643f4a1787
commit 4c7ad9527d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 22 additions and 17 deletions

View File

@ -515,6 +515,8 @@ DirChanged After the |current-directory| was changed.
Sets these |v:event| keys:
cwd: current working directory
scope: "global", "tab", "window"
changed_window: v:true if we fired the event
switching window (or tab)
Non-recursive (event cannot trigger itself).
*FileAppendCmd*
FileAppendCmd Before appending to a file. Should do the

View File

@ -1614,6 +1614,8 @@ v:event Dictionary of event data for the current |autocommand|. Valid
|CompleteChanged|.
scrollbar Is |v:true| if popup menu have scrollbar, or
|v:false| if not.
changed_window Is |v:true| if the the event fired
while changing window (or tab) on |DirChanged|.
*v:exception* *exception-variable*
v:exception The value of the exception most recently caught and not

View File

@ -7506,7 +7506,7 @@ void post_chdir(CdScope scope, bool trigger_dirchanged)
shorten_fnames(true);
if (trigger_dirchanged) {
do_autocmd_dirchanged(cwd, scope);
do_autocmd_dirchanged(cwd, scope, false);
}
}

View File

@ -1565,7 +1565,7 @@ theend:
return file_name;
}
void do_autocmd_dirchanged(char *new_dir, CdScope scope)
void do_autocmd_dirchanged(char *new_dir, CdScope scope, bool changed_window)
{
static bool recursive = false;
@ -1601,6 +1601,7 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope)
tv_dict_add_str(dict, S_LEN("scope"), buf); // -V614
tv_dict_add_str(dict, S_LEN("cwd"), new_dir);
tv_dict_add_bool(dict, S_LEN("changed_window"), changed_window);
tv_dict_set_keys_readonly(dict);
apply_autocmds(EVENT_DIRCHANGED, (char_u *)buf, (char_u *)new_dir, false,
@ -1633,7 +1634,7 @@ int vim_chdirfile(char_u *fname)
slash_adjust((char_u *)dir);
#endif
if (!strequal(dir, (char *)NameBuff)) {
do_autocmd_dirchanged(dir, kCdScopeWindow);
do_autocmd_dirchanged(dir, kCdScopeWindow, false);
}
return OK;

View File

@ -4567,7 +4567,7 @@ static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid,
if (os_chdir(new_dir) == 0) {
if (!p_acd && !strequal(new_dir, cwd)) {
do_autocmd_dirchanged(new_dir, curwin->w_localdir
? kCdScopeWindow : kCdScopeTab);
? kCdScopeWindow : kCdScopeTab, true);
}
shorten_fnames(true);
}
@ -4576,7 +4576,7 @@ static void win_enter_ext(win_T *wp, bool undo_sync, int curwin_invalid,
// directory: Change to the global directory.
if (os_chdir((char *)globaldir) == 0) {
if (!p_acd && !strequal((char *)globaldir, cwd)) {
do_autocmd_dirchanged((char *)globaldir, kCdScopeGlobal);
do_autocmd_dirchanged((char *)globaldir, kCdScopeGlobal, true);
}
}
XFREE_CLEAR(globaldir);

View File

@ -29,15 +29,15 @@ describe('autocmd DirChanged', function()
it('sets v:event', function()
command('lcd '..dirs[1])
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
eq({cwd=dirs[1], scope='window', changed_window=false}, eval('g:ev'))
eq(1, eval('g:cdcount'))
command('tcd '..dirs[2])
eq({cwd=dirs[2], scope='tab'}, eval('g:ev'))
eq({cwd=dirs[2], scope='tab', changed_window=false}, eval('g:ev'))
eq(2, eval('g:cdcount'))
command('cd '..dirs[3])
eq({cwd=dirs[3], scope='global'}, eval('g:ev'))
eq({cwd=dirs[3], scope='global', changed_window=false}, eval('g:ev'))
eq(3, eval('g:cdcount'))
end)
@ -57,7 +57,7 @@ describe('autocmd DirChanged', function()
-- Set up a _nested_ handler.
command('autocmd DirChanged * nested lcd '..dirs[3])
command('lcd '..dirs[1])
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
eq({cwd=dirs[1], scope='window', changed_window=false}, eval('g:ev'))
eq(1, eval('g:cdcount'))
-- autocmd changed to dirs[3], but did NOT trigger another DirChanged.
eq(dirs[3], eval('getcwd()'))
@ -105,10 +105,10 @@ describe('autocmd DirChanged', function()
command('set autochdir')
command('split '..dirs[1]..'/foo')
eq({cwd=dirs[1], scope='window'}, eval('g:ev'))
eq({cwd=dirs[1], scope='window', changed_window=false}, eval('g:ev'))
command('split '..dirs[2]..'/bar')
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
eq({cwd=dirs[2], scope='window', changed_window=false}, eval('g:ev'))
eq(2, eval('g:cdcount'))
end)
@ -121,16 +121,16 @@ describe('autocmd DirChanged', function()
command('lcd '..dirs[1])
command('2wincmd w') -- window 2
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
eq({cwd=dirs[2], scope='window', changed_window=true}, eval('g:ev'))
eq(4, eval('g:cdcount'))
command('tabnew') -- tab 2 (tab-local CWD)
eq(4, eval('g:cdcount')) -- same CWD, no DirChanged event
command('tcd '..dirs[3])
command('tabnext') -- tab 1 (no tab-local CWD)
eq({cwd=dirs[2], scope='window'}, eval('g:ev'))
eq({cwd=dirs[2], scope='window', changed_window=true}, eval('g:ev'))
command('tabnext') -- tab 2
eq({cwd=dirs[3], scope='tab'}, eval('g:ev'))
eq({cwd=dirs[3], scope='tab', changed_window=true}, eval('g:ev'))
eq(7, eval('g:cdcount'))
command('tabnext') -- tab 1
@ -142,17 +142,17 @@ describe('autocmd DirChanged', function()
it('is triggered by nvim_set_current_dir()', function()
request('nvim_set_current_dir', dirs[1])
eq({cwd=dirs[1], scope='global'}, eval('g:ev'))
eq({cwd=dirs[1], scope='global', changed_window=false}, eval('g:ev'))
request('nvim_set_current_dir', dirs[2])
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
eq({cwd=dirs[2], scope='global', changed_window=false}, eval('g:ev'))
local status, err = pcall(function()
request('nvim_set_current_dir', '/doesnotexist')
end)
eq(false, status)
eq('Failed to change directory', string.match(err, ': (.*)'))
eq({cwd=dirs[2], scope='global'}, eval('g:ev'))
eq({cwd=dirs[2], scope='global', changed_window=false}, eval('g:ev'))
end)
it('works when local to buffer', function()