feat: implement BufModified autocmd

This commit is contained in:
Rom Grk 2020-11-02 02:11:22 -05:00
parent 643f4a1787
commit 24db59ca8f
5 changed files with 25 additions and 0 deletions

View File

@ -7,6 +7,7 @@ return {
'BufFilePre', -- before renaming a buffer 'BufFilePre', -- before renaming a buffer
'BufHidden', -- just after buffer becomes hidden 'BufHidden', -- just after buffer becomes hidden
'BufLeave', -- before leaving a buffer 'BufLeave', -- before leaving a buffer
'BufModified', -- after the 'modified' state of a buffer changes
'BufNew', -- after creating any buffer 'BufNew', -- after creating any buffer
'BufNewFile', -- when creating a buffer for a new file 'BufNewFile', -- when creating a buffer for a new file
'BufReadCmd', -- read buffer using command 'BufReadCmd', -- read buffer using command
@ -124,6 +125,7 @@ return {
-- List of nvim-specific events or aliases for the purpose of generating -- List of nvim-specific events or aliases for the purpose of generating
-- syntax file -- syntax file
nvim_specific = { nvim_specific = {
BufModified=true,
DirChanged=true, DirChanged=true,
Signal=true, Signal=true,
TabClosed=true, TabClosed=true,

View File

@ -541,6 +541,8 @@ struct file_buffer {
int b_changed; // 'modified': Set to true if something in the int b_changed; // 'modified': Set to true if something in the
// file has been changed and not written out. // file has been changed and not written out.
bool b_changed_notified; // if the BufModified autocmd has been triggered
// since the last time b_changed was modified
/// Change-identifier incremented for each change, including undo. /// Change-identifier incremented for each change, including undo.
/// ///

View File

@ -129,6 +129,7 @@ void changed(void)
void changed_internal(void) void changed_internal(void)
{ {
curbuf->b_changed = true; curbuf->b_changed = true;
curbuf->b_changed_notified = false;
ml_setflags(curbuf); ml_setflags(curbuf);
check_status(curbuf); check_status(curbuf);
redraw_tabline = true; redraw_tabline = true;
@ -502,6 +503,7 @@ void unchanged(buf_T *buf, int ff, bool always_inc_changedtick)
{ {
if (buf->b_changed || (ff && file_ff_differs(buf, false))) { if (buf->b_changed || (ff && file_ff_differs(buf, false))) {
buf->b_changed = false; buf->b_changed = false;
buf->b_changed_notified = false;
ml_setflags(buf); ml_setflags(buf);
if (ff) { if (ff) {
save_file_ff(buf); save_file_ff(buf);

View File

@ -1488,6 +1488,14 @@ static void ins_redraw(
do_autocmd_winscrolled(curwin); do_autocmd_winscrolled(curwin);
} }
// Trigger BufModified if b_changed_notified is false.
if (ready && has_event(EVENT_BUFMODIFIED)
&& curbuf->b_changed_notified == false
&& !pum_visible()) {
apply_autocmds(EVENT_BUFMODIFIED, NULL, NULL, false, curbuf);
curbuf->b_changed_notified = true;
}
if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin) if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin)
&& conceal_cursor_moved) { && conceal_cursor_moved) {
redrawWinline(curwin, curwin->w_cursor.lnum); redrawWinline(curwin, curwin->w_cursor.lnum);

View File

@ -1229,6 +1229,16 @@ static void normal_check_text_changed(NormalState *s)
} }
} }
static void normal_check_buffer_modified(NormalState *s)
{
// Trigger BufModified if b_modified changed
if (!finish_op && has_event(EVENT_BUFMODIFIED)
&& curbuf->b_changed_notified == false) {
apply_autocmds(EVENT_BUFMODIFIED, NULL, NULL, false, curbuf);
curbuf->b_changed_notified = true;
}
}
static void normal_check_folds(NormalState *s) static void normal_check_folds(NormalState *s)
{ {
// Include a closed fold completely in the Visual area. // Include a closed fold completely in the Visual area.
@ -1336,6 +1346,7 @@ static int normal_check(VimState *state)
normal_check_cursor_moved(s); normal_check_cursor_moved(s);
normal_check_text_changed(s); normal_check_text_changed(s);
normal_check_window_scrolled(s); normal_check_window_scrolled(s);
normal_check_buffer_modified(s);
// Updating diffs from changed() does not always work properly, // Updating diffs from changed() does not always work properly,
// esp. updating folds. Do an update just before redrawing if // esp. updating folds. Do an update just before redrawing if