From b42e0c40c8b8e906eccb14db0d0648a39e49c363 Mon Sep 17 00:00:00 2001 From: Michael Lingelbach Date: Sat, 18 Dec 2021 19:18:47 -0800 Subject: [PATCH] fix: update last cursor on first CursorMoved (#16698) Closes https://github.com/neovim/neovim/issues/16625 https://github.com/neovim/neovim/issues/12923 The first defined CursorMoved autocommand will immediately fire if the cursor has previously moved upon definition of the autocommand. Plugins add dummy autocommands such as: ```lua autocmd CursorMoved * execute '' ``` to avoid this behavior. Instead, when defining a new CursorHold autocommand, force update the last cursor position. See https://github.com/vim/vim/issues/2053 --- src/nvim/autocmd.c | 6 ++++++ test/functional/autocmd/cursormoved_spec.lua | 8 ++++++++ 2 files changed, 14 insertions(+) diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index 3780cad1d6..463bd5e0e6 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -932,6 +932,12 @@ static int do_autocmd_event(event_T event, char_u *pat, bool once, int nested, c last_mode = get_mode(); } + // If the event is CursorMoved, update the last cursor position + // position to avoid immediately triggering the autocommand + if (event == EVENT_CURSORMOVED && !has_event(EVENT_CURSORMOVED)) { + curwin->w_last_cursormoved = curwin->w_cursor; + } + ap->cmds = NULL; *prev_ap = ap; last_autopat[(int)event] = ap; diff --git a/test/functional/autocmd/cursormoved_spec.lua b/test/functional/autocmd/cursormoved_spec.lua index d0f46e689b..9641d4b096 100644 --- a/test/functional/autocmd/cursormoved_spec.lua +++ b/test/functional/autocmd/cursormoved_spec.lua @@ -31,4 +31,12 @@ describe('CursorMoved', function() eq({'aaa'}, funcs.nvim_buf_get_lines(eval('g:buf'), 0, -1, true)) eq(0, eval('g:cursormoved')) end) + + it("is not triggered by cursor movement prior to first CursorMoved instantiation", function() + source([[ + let g:cursormoved = 0 + autocmd CursorMoved * let g:cursormoved += 1 + ]]) + eq(0, eval('g:cursormoved')) + end) end)