+182
-54
lines changedFilter options
+182
-54
lines changed Original file line number Diff line number Diff line change
@@ -525,8 +525,19 @@ DirChanged After the |current-directory| was changed.
525
525
"global" to trigger on `:cd`
526
526
"auto" to trigger on 'autochdir'.
527
527
Sets these |v:event| keys:
528
-
cwd: current working directory
529
-
scope: "global", "tabpage", "window"
528
+
cwd: current working directory
529
+
scope: "global", "tabpage", "window"
530
+
changed_window: v:true if we fired the event
531
+
switching window (or tab)
532
+
<afile> is set to the new directory name.
533
+
Non-recursive (event cannot trigger itself).
534
+
*DirChangedPre*
535
+
DirChangedPre When the |current-directory| is going to be
536
+
changed, as with |DirChanged|.
537
+
The pattern is like with |DirChanged|.
538
+
Sets these |v:event| keys:
539
+
directory: new working directory
540
+
scope: "global", "tabpage", "window"
530
541
changed_window: v:true if we fired the event
531
542
switching window (or tab)
532
543
<afile> is set to the new directory name.
Original file line number Diff line number Diff line change
@@ -433,7 +433,8 @@ Vimscript compatibility:
433
433
`this_session` does not alias to |v:this_session|
434
434
435
435
Working directory (Vim implemented some of these later than Nvim):
436
-
- |DirChanged| can be triggered when switching to another window.
436
+
- |DirChanged| and |DirChangedPre| can be triggered when switching to another
437
+
window or tab.
437
438
- |getcwd()| and |haslocaldir()| may throw errors if the tab page or window
438
439
cannot be found. *E5000* *E5001* *E5002*
439
440
- |haslocaldir()| checks for tab-local directory if and only if -1 is passed as
Original file line number Diff line number Diff line change
@@ -40,6 +40,7 @@ return {
40
40
'DiagnosticChanged', -- diagnostics in a buffer were modified
41
41
'DiffUpdated', -- diffs have been updated
42
42
'DirChanged', -- directory changed
43
+
'DirChangedPre', -- directory is going to change
43
44
'EncodingChanged', -- after changing the 'encoding' option
44
45
'ExitPre', -- before exiting
45
46
'FileAppendCmd', -- append to a file using command
@@ -132,18 +133,14 @@ return {
132
133
nvim_specific = {
133
134
BufModifiedSet=true,
134
135
DiagnosticChanged=true,
135
-
DirChanged=true,
136
136
RecordingEnter=true,
137
137
RecordingLeave=true,
138
138
Signal=true,
139
-
TabClosed=true,
140
-
TabNew=true,
141
139
TabNewEntered=true,
142
140
TermClose=true,
143
141
TermOpen=true,
144
142
UIEnter=true,
145
143
UILeave=true,
146
-
WinClosed=true,
147
144
WinScrolled=true,
148
145
},
149
146
}
Original file line number Diff line number Diff line change
@@ -1517,13 +1517,13 @@ static bool apply_autocmds_group(event_T event, char_u *fname, char_u *fname_io,
1517
1517
|| event == EVENT_CMDLINELEAVE || event == EVENT_CMDWINENTER
1518
1518
|| event == EVENT_CMDWINLEAVE || event == EVENT_CMDUNDEFINED
1519
1519
|| event == EVENT_COLORSCHEME || event == EVENT_COLORSCHEMEPRE
1520
-
|| event == EVENT_DIRCHANGED || event == EVENT_FILETYPE
1521
-
|| event == EVENT_FUNCUNDEFINED || event == EVENT_MODECHANGED
1522
-
|| event == EVENT_OPTIONSET || event == EVENT_QUICKFIXCMDPOST
1523
-
|| event == EVENT_QUICKFIXCMDPRE || event == EVENT_REMOTEREPLY
1524
-
|| event == EVENT_SPELLFILEMISSING || event == EVENT_SYNTAX
1525
-
|| event == EVENT_SIGNAL || event == EVENT_TABCLOSED
1526
-
|| event == EVENT_WINCLOSED) {
1520
+
|| event == EVENT_DIRCHANGED || event == EVENT_DIRCHANGEDPRE
1521
+
|| event == EVENT_FILETYPE || event == EVENT_FUNCUNDEFINED
1522
+
|| event == EVENT_MODECHANGED || event == EVENT_OPTIONSET
1523
+
|| event == EVENT_QUICKFIXCMDPOST || event == EVENT_QUICKFIXCMDPRE
1524
+
|| event == EVENT_REMOTEREPLY || event == EVENT_SPELLFILEMISSING
1525
+
|| event == EVENT_SYNTAX || event == EVENT_SIGNAL
1526
+
|| event == EVENT_TABCLOSED || event == EVENT_WINCLOSED) {
1527
1527
fname = vim_strsave(fname);
1528
1528
} else {
1529
1529
fname = (char_u *)FullName_save((char *)fname, false);
Original file line number Diff line number Diff line change
@@ -7784,7 +7784,7 @@ static char_u *get_prevdir(CdScope scope)
7784
7784
/// Deal with the side effects of changing the current directory.
7785
7785
///
7786
7786
/// @param scope Scope of the function call (global, tab or window).
7787
-
void post_chdir(CdScope scope, bool trigger_dirchanged)
7787
+
static void post_chdir(CdScope scope, bool trigger_dirchanged)
7788
7788
{
7789
7789
// Always overwrite the window-local CWD.
7790
7790
XFREE_CLEAR(curwin->w_localdir);
@@ -7825,7 +7825,7 @@ void post_chdir(CdScope scope, bool trigger_dirchanged)
7825
7825
shorten_fnames(true);
7826
7826
7827
7827
if (trigger_dirchanged) {
7828
-
do_autocmd_dirchanged(cwd, scope, kCdCauseManual);
7828
+
do_autocmd_dirchanged(cwd, scope, kCdCauseManual, false);
7829
7829
}
7830
7830
}
7831
7831
@@ -7869,10 +7869,13 @@ bool changedir_func(char_u *new_dir, CdScope scope)
7869
7869
}
7870
7870
7871
7871
bool dir_differs = pdir == NULL || pathcmp((char *)pdir, (char *)new_dir, -1) != 0;
7872
-
if (dir_differs && vim_chdir(new_dir) != 0) {
7873
-
emsg(_(e_failed));
7874
-
xfree(pdir);
7875
-
return false;
7872
+
if (dir_differs) {
7873
+
do_autocmd_dirchanged((char *)new_dir, scope, kCdCauseManual, true);
7874
+
if (vim_chdir(new_dir) != 0) {
7875
+
emsg(_(e_failed));
7876
+
xfree(pdir);
7877
+
return false;
7878
+
}
7876
7879
}
7877
7880
7878
7881
char_u **pp;
Original file line number Diff line number Diff line change
@@ -1600,11 +1600,13 @@ char_u *find_file_in_path_option(char_u *ptr, size_t len, int options, int first
1600
1600
return file_name;
1601
1601
}
1602
1602
1603
-
void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
1603
+
void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause, bool pre)
1604
1604
{
1605
1605
static bool recursive = false;
1606
1606
1607
-
if (recursive || !has_event(EVENT_DIRCHANGED)) {
1607
+
event_T event = pre ? EVENT_DIRCHANGEDPRE : EVENT_DIRCHANGED;
1608
+
1609
+
if (recursive || !has_event(event)) {
1608
1610
// No autocommand was defined or we changed
1609
1611
// the directory from this autocommand.
1610
1612
return;
@@ -1638,8 +1640,12 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
1638
1640
new_dir = new_dir_buf;
1639
1641
#endif
1640
1642
1643
+
if (pre) {
1644
+
tv_dict_add_str(dict, S_LEN("directory"), new_dir);
1645
+
} else {
1646
+
tv_dict_add_str(dict, S_LEN("cwd"), new_dir);
1647
+
}
1641
1648
tv_dict_add_str(dict, S_LEN("scope"), buf); // -V614
1642
-
tv_dict_add_str(dict, S_LEN("cwd"), new_dir);
1643
1649
tv_dict_add_bool(dict, S_LEN("changed_window"), cause == kCdCauseWindow);
1644
1650
tv_dict_set_keys_readonly(dict);
1645
1651
@@ -1655,8 +1661,7 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
1655
1661
abort();
1656
1662
}
1657
1663
1658
-
apply_autocmds(EVENT_DIRCHANGED, (char_u *)buf, (char_u *)new_dir, false,
1659
-
curbuf);
1664
+
apply_autocmds(event, (char_u *)buf, (char_u *)new_dir, false, curbuf);
1660
1665
1661
1666
restore_v_event(dict, &save_v_event);
1662
1667
@@ -1682,12 +1687,16 @@ int vim_chdirfile(char_u *fname, CdCause cause)
1682
1687
return OK;
1683
1688
}
1684
1689
1690
+
if (cause != kCdCauseOther) {
1691
+
do_autocmd_dirchanged(dir, kCdScopeWindow, cause, true);
1692
+
}
1693
+
1685
1694
if (os_chdir(dir) != 0) {
1686
1695
return FAIL;
1687
1696
}
1688
1697
1689
1698
if (cause != kCdCauseOther) {
1690
-
do_autocmd_dirchanged(dir, kCdScopeWindow, cause);
1699
+
do_autocmd_dirchanged(dir, kCdScopeWindow, cause, false);
1691
1700
}
1692
1701
1693
1702
return OK;
Original file line number Diff line number Diff line change
@@ -1850,14 +1850,16 @@ endfunc
1850
1850
1851
1851
function Test_dirchanged_global()
1852
1852
call s:Before_test_dirchanged()
1853
+
autocmd test_dirchanged DirChangedPre global call add(s:li, "pre cd " .. v:event.directory)
1853
1854
autocmd test_dirchanged DirChanged global call add(s:li, "cd:")
1854
1855
autocmd test_dirchanged DirChanged global call add(s:li, expand("<afile>"))
1855
1856
call chdir(s:dir_foo)
1856
-
call assert_equal(["cd:", s:dir_foo], s:li)
1857
+
let expected = ["pre cd " .. s:dir_foo, "cd:", s:dir_foo]
1858
+
call assert_equal(expected, s:li)
1857
1859
call chdir(s:dir_foo)
1858
-
call assert_equal(["cd:", s:dir_foo], s:li)
1860
+
call assert_equal(expected, s:li)
1859
1861
exe 'lcd ' .. fnameescape(s:dir_bar)
1860
-
call assert_equal(["cd:", s:dir_foo], s:li)
1862
+
call assert_equal(expected, s:li)
1861
1863
call s:After_test_dirchanged()
1862
1864
endfunc
1863
1865
@@ -1879,14 +1881,16 @@ function Test_dirchanged_auto()
1879
1881
CheckOption autochdir
1880
1882
call s:Before_test_dirchanged()
1881
1883
call test_autochdir()
1884
+
autocmd test_dirchanged DirChangedPre auto call add(s:li, "pre cd " .. v:event.directory)
1882
1885
autocmd test_dirchanged DirChanged auto call add(s:li, "auto:")
1883
1886
autocmd test_dirchanged DirChanged auto call add(s:li, expand("<afile>"))
1884
1887
set acd
1885
1888
cd ..
1886
1889
call assert_equal([], s:li)
1887
1890
exe 'edit ' . s:dir_foo . '/Xfile'
1888
1891
call assert_equal(s:dir_foo, getcwd())
1889
-
call assert_equal(["auto:", s:dir_foo], s:li)
1892
+
let expected = ["pre cd " .. s:dir_foo, "auto:", s:dir_foo]
1893
+
call assert_equal(expected, s:li)
1890
1894
set noacd
1891
1895
bwipe!
1892
1896
call s:After_test_dirchanged()
Original file line number Diff line number Diff line change
@@ -4645,20 +4645,29 @@ void fix_current_dir(void)
4645
4645
globaldir = (char_u *)xstrdup(cwd);
4646
4646
}
4647
4647
}
4648
+
bool dir_differs = pathcmp(new_dir, cwd, -1) != 0;
4649
+
if (!p_acd && dir_differs) {
4650
+
do_autocmd_dirchanged(new_dir, curwin->w_localdir ? kCdScopeWindow : kCdScopeTabpage,
4651
+
kCdCauseWindow, true);
4652
+
}
4648
4653
if (os_chdir(new_dir) == 0) {
4649
-
if (!p_acd && pathcmp(new_dir, cwd, -1) != 0) {
4650
-
do_autocmd_dirchanged(new_dir, curwin->w_localdir
4651
-
? kCdScopeWindow : kCdScopeTabpage, kCdCauseWindow);
4654
+
if (!p_acd && dir_differs) {
4655
+
do_autocmd_dirchanged(new_dir, curwin->w_localdir ? kCdScopeWindow : kCdScopeTabpage,
4656
+
kCdCauseWindow, false);
4652
4657
}
4653
-
last_chdir_reason = NULL;
4654
-
shorten_fnames(true);
4655
4658
}
4659
+
last_chdir_reason = NULL;
4660
+
shorten_fnames(true);
4656
4661
} else if (globaldir != NULL) {
4657
4662
// Window doesn't have a local directory and we are not in the global
4658
4663
// directory: Change to the global directory.
4664
+
bool dir_differs = pathcmp((char *)globaldir, cwd, -1) != 0;
4665
+
if (!p_acd && dir_differs) {
4666
+
do_autocmd_dirchanged((char *)globaldir, kCdScopeGlobal, kCdCauseWindow, true);
4667
+
}
4659
4668
if (os_chdir((char *)globaldir) == 0) {
4660
-
if (!p_acd && pathcmp((char *)globaldir, cwd, -1) != 0) {
4661
-
do_autocmd_dirchanged((char *)globaldir, kCdScopeGlobal, kCdCauseWindow);
4669
+
if (!p_acd && dir_differs) {
4670
+
do_autocmd_dirchanged((char *)globaldir, kCdScopeGlobal, kCdCauseWindow, false);
4662
4671
}
4663
4672
}
4664
4673
XFREE_CLEAR(globaldir);
You can’t perform that action at this time.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4