diff options
author | Andrea Cedraro <a.cedraro@gmail.com> | 2020-11-07 18:02:06 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-07 12:02:06 -0500 |
commit | 4c7ad9527db7c4476fd07c1fa95d89983dfba775 (patch) | |
tree | 7e4ac373771376d9e2331c7ba8edccde36553b58 | |
parent | 643f4a178751df34b27189f7aebea313b17bad3b (diff) | |
download | rneovim-4c7ad9527db7c4476fd07c1fa95d89983dfba775.tar.gz rneovim-4c7ad9527db7c4476fd07c1fa95d89983dfba775.tar.bz2 rneovim-4c7ad9527db7c4476fd07c1fa95d89983dfba775.zip |
Add v:event flag on DirChanged signaling switching window (#13153)
Closes #9909
-rw-r--r-- | runtime/doc/autocmd.txt | 2 | ||||
-rw-r--r-- | runtime/doc/eval.txt | 2 | ||||
-rw-r--r-- | src/nvim/ex_docmd.c | 2 | ||||
-rw-r--r-- | src/nvim/file_search.c | 5 | ||||
-rw-r--r-- | src/nvim/window.c | 4 | ||||
-rw-r--r-- | test/functional/autocmd/dirchanged_spec.lua | 24 |
6 files changed, 22 insertions, 17 deletions
diff --git a/runtime/doc/autocmd.txt b/runtime/doc/autocmd.txt index dbaba1b3ad..ee90d67cbc 100644 --- a/runtime/doc/autocmd.txt +++ b/runtime/doc/autocmd.txt @@ -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 diff --git a/runtime/doc/eval.txt b/runtime/doc/eval.txt index 800de63a55..6da89f5491 100644 --- a/runtime/doc/eval.txt +++ b/runtime/doc/eval.txt @@ -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 diff --git a/src/nvim/ex_docmd.c b/src/nvim/ex_docmd.c index d7ed2fe97d..60963f5411 100644 --- a/src/nvim/ex_docmd.c +++ b/src/nvim/ex_docmd.c @@ -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); } } diff --git a/src/nvim/file_search.c b/src/nvim/file_search.c index 47272df2f0..b1fa0b6779 100644 --- a/src/nvim/file_search.c +++ b/src/nvim/file_search.c @@ -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; diff --git a/src/nvim/window.c b/src/nvim/window.c index 9d918ebeb0..3429e3df70 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -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); diff --git a/test/functional/autocmd/dirchanged_spec.lua b/test/functional/autocmd/dirchanged_spec.lua index 7979e91a4c..6f2da24cf2 100644 --- a/test/functional/autocmd/dirchanged_spec.lua +++ b/test/functional/autocmd/dirchanged_spec.lua @@ -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() |