mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 11:28:22 +00:00
Merge pull request #13205 from romgrk/add-bufmodified-autocmd
Implement BufModifiedSet autocmd
This commit is contained in:
@@ -253,6 +253,9 @@ BufLeave Before leaving to another buffer. Also when
|
|||||||
new current window is not for the same buffer.
|
new current window is not for the same buffer.
|
||||||
|
|
||||||
Not used for ":qa" or ":q" when exiting Vim.
|
Not used for ":qa" or ":q" when exiting Vim.
|
||||||
|
*BufModifiedSet*
|
||||||
|
BufModifiedSet After the `'modified'` value of a buffer has
|
||||||
|
been changed.
|
||||||
*BufNew*
|
*BufNew*
|
||||||
BufNew Just after creating a new buffer. Also used
|
BufNew Just after creating a new buffer. Also used
|
||||||
just after a buffer has been renamed. When
|
just after a buffer has been renamed. When
|
||||||
|
@@ -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
|
||||||
|
'BufModifiedSet', -- 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 = {
|
||||||
|
BufModifiedSet=true,
|
||||||
DirChanged=true,
|
DirChanged=true,
|
||||||
Signal=true,
|
Signal=true,
|
||||||
TabClosed=true,
|
TabClosed=true,
|
||||||
|
@@ -541,6 +541,9 @@ 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_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.
|
/// Change-identifier incremented for each change, including undo.
|
||||||
///
|
///
|
||||||
|
@@ -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_invalid = true;
|
||||||
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_invalid = true;
|
||||||
ml_setflags(buf);
|
ml_setflags(buf);
|
||||||
if (ff) {
|
if (ff) {
|
||||||
save_file_ff(buf);
|
save_file_ff(buf);
|
||||||
|
@@ -1488,6 +1488,14 @@ static void ins_redraw(
|
|||||||
do_autocmd_winscrolled(curwin);
|
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)
|
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);
|
||||||
|
@@ -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)
|
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
|
||||||
|
22
test/functional/autocmd/bufmodifiedset_spec.lua
Normal file
22
test/functional/autocmd/bufmodifiedset_spec.lua
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
local helpers = require('test.functional.helpers')(after_each)
|
||||||
|
|
||||||
|
local clear = helpers.clear
|
||||||
|
local eq = helpers.eq
|
||||||
|
local eval = helpers.eval
|
||||||
|
local source = helpers.source
|
||||||
|
local request = helpers.request
|
||||||
|
|
||||||
|
describe('BufModified', function()
|
||||||
|
before_each(clear)
|
||||||
|
|
||||||
|
it('is triggered when modified and un-modified', function()
|
||||||
|
source([[
|
||||||
|
let g:modified = 0
|
||||||
|
autocmd BufModifiedSet * let g:modified += 1
|
||||||
|
]])
|
||||||
|
request("nvim_command", [[normal! aa\<Esc>]])
|
||||||
|
eq(1, eval('g:modified'))
|
||||||
|
request("nvim_command", [[normal! u]])
|
||||||
|
eq(2, eval('g:modified'))
|
||||||
|
end)
|
||||||
|
end)
|
Reference in New Issue
Block a user