Merge pull request #13205 from romgrk/add-bufmodified-autocmd

Implement BufModifiedSet autocmd
This commit is contained in:
Björn Linse
2020-11-09 12:23:14 +01:00
committed by GitHub
7 changed files with 51 additions and 0 deletions

View File

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

View File

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

View File

@@ -129,6 +129,7 @@ void changed(void)
void changed_internal(void)
{
curbuf->b_changed = true;
curbuf->b_changed_invalid = true;
ml_setflags(curbuf);
check_status(curbuf);
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))) {
buf->b_changed = false;
buf->b_changed_invalid = true;
ml_setflags(buf);
if (ff) {
save_file_ff(buf);

View File

@@ -1488,6 +1488,14 @@ static void ins_redraw(
do_autocmd_winscrolled(curwin);
}
// Trigger BufModified if b_changed_invalid is set.
if (ready && has_event(EVENT_BUFMODIFIEDSET)
&& curbuf->b_changed_invalid == true
&& !pum_visible()) {
apply_autocmds(EVENT_BUFMODIFIEDSET, NULL, NULL, false, curbuf);
curbuf->b_changed_invalid = false;
}
if (curwin->w_p_cole > 0 && conceal_cursor_line(curwin)
&& conceal_cursor_moved) {
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_BUFMODIFIEDSET)
&& curbuf->b_changed_invalid == true) {
apply_autocmds(EVENT_BUFMODIFIEDSET, NULL, NULL, false, curbuf);
curbuf->b_changed_invalid = false;
}
}
static void normal_check_folds(NormalState *s)
{
// 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_text_changed(s);
normal_check_window_scrolled(s);
normal_check_buffer_modified(s);
// Updating diffs from changed() does not always work properly,
// esp. updating folds. Do an update just before redrawing if