feat(events): add DirChangedPre

In Nvim, like DirChanged, this also triggers when switching windows.

This marks Vim patch 8.2.4335 as ported.

vim-patch:8.2.4335: no autocommand event triggered before changing directory

Problem:    No autocommand event triggered before changing directory. (Ronnie
            Magatti)
Solution:   Add DirChangedPre. (closes vim/vim#9721)
28e8f73ae2
This commit is contained in:
zeertzjq
2022-02-11 12:44:47 +08:00
parent 851252f79d
commit 059d36e326
9 changed files with 184 additions and 56 deletions

View File

@@ -1600,11 +1600,13 @@ theend:
return file_name;
}
void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause, bool pre)
{
static bool recursive = false;
if (recursive || !has_event(EVENT_DIRCHANGED)) {
event_T event = pre ? EVENT_DIRCHANGEDPRE : EVENT_DIRCHANGED;
if (recursive || !has_event(event)) {
// No autocommand was defined or we changed
// the directory from this autocommand.
return;
@@ -1638,8 +1640,12 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
new_dir = new_dir_buf;
#endif
if (pre) {
tv_dict_add_str(dict, S_LEN("directory"), new_dir);
} else {
tv_dict_add_str(dict, S_LEN("cwd"), new_dir);
}
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"), cause == kCdCauseWindow);
tv_dict_set_keys_readonly(dict);
@@ -1655,8 +1661,7 @@ void do_autocmd_dirchanged(char *new_dir, CdScope scope, CdCause cause)
abort();
}
apply_autocmds(EVENT_DIRCHANGED, (char_u *)buf, (char_u *)new_dir, false,
curbuf);
apply_autocmds(event, (char_u *)buf, (char_u *)new_dir, false, curbuf);
restore_v_event(dict, &save_v_event);
@@ -1682,12 +1687,16 @@ int vim_chdirfile(char_u *fname, CdCause cause)
return OK;
}
if (cause != kCdCauseOther) {
do_autocmd_dirchanged(dir, kCdScopeWindow, cause, true);
}
if (os_chdir(dir) != 0) {
return FAIL;
}
if (cause != kCdCauseOther) {
do_autocmd_dirchanged(dir, kCdScopeWindow, cause);
do_autocmd_dirchanged(dir, kCdScopeWindow, cause, false);
}
return OK;