[vim/vim] `TextChangedI` is triggered upon entering Insert mode after triggered `TextChanged` (Issue #13265)

36 views
Skip to first unread message

Evgeni Chasnovski

unread,
Oct 4, 2023, 7:59:10 AM10/4/23
to vim/vim, Subscribed

Steps to reproduce

  1. Create an init file with the following contents:
put ='a'

let g:n = 0
au TextChangedI * let g:n = g:n + 1
  1. Open vim -u <path-to-init-file>
  2. Execute :echo g:n. It shows 0.
  3. Press i to enter Insert mode followed by <Esc> to exit it.
  4. Execute :echo g:n again. It shows 1 while there were no changes in Insert mode done.
  5. Press yyp three times. This would trigger TextChanged again.
  6. Repeat steps 3-5. Before entering Insert mode it would show 1, after enter-exit - 2. Although again no changes are made in Insert mode.

Expected behaviour

TextChangedI is triggered only when change is actually made in Insert mode, as per documentation: "After a change was made to the text in the current buffer in Insert mode."

This was originally discovered in neovim/neovim#25490 and seems to be the result of Neovim porting 8.2.3517 patch.

Version of Vim

9.0.1882

Environment

Operating system: EndeavourOS Linux x86_64 (6.5.5-arch1-1)
Terminal: st-0.9
Value of $TERM: st-256color
Shell: zsh 5.9

Logs and stack traces

No response


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13265@github.com>

Christian Brabandt

unread,
Oct 4, 2023, 3:40:00 PM10/4/23
to vim/vim, Subscribed

Hm, not sure this is a feature or a bug., as both TextChanged and TextChangedI compare against the b:changedtick variable and trigger when that changed. I think the basic idea is, b:changedtick is incremented for any changes made to the buffer. If it changes in insert mode, trigger TextChangedI or TexChangedP, if it changes in Normal mode, trigger TextChanged. I can see that his might be slightly unexpected however.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13265/1747522094@github.com>

Evgeni Chasnovski

unread,
Oct 5, 2023, 1:26:05 AM10/5/23
to vim/vim, Subscribed

If it changes in insert mode, trigger TextChangedI or TexChangedP, if it changes in Normal mode, trigger TextChanged. I can see that his might be slightly unexpected however.

Yes, this part sounds reasonably as expected. Maybe it still shouldn't trigger when variable increment is detected at the start of Insert mode "session"?

In the issue example changes are done only in Normal mode but it triggers TextChangedI. This both goes against documentation and how users can utilize this event to detect actual text changes in Insert mode. As of right now there doesn't seem to be a way to detect those.

If users want to instead detect when changedtick is incremented across all instances of Insert mode, they should be able to track InsertEnter, TextChangedI, and TextChangedP.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13265/1748052864@github.com>

Christian Brabandt

unread,
Oct 5, 2023, 4:46:05 PM10/5/23
to vim/vim, Subscribed

I have the following rough patch (no tests yet, only barely tested):

diff --git a/src/edit.c b/src/edit.c
index a51542525..19ed5b8c1 100644
--- a/src/edit.c
+++ b/src/edit.c
@@ -174,6 +174,9 @@ edit(
        return FALSE;
     }
     ins_compl_clear();     // clear stuff for CTRL-X mode
+    // Reset Changedtick_i, so that TextChangedI will only be triggered for stuff
+    // from insert mode
+    curbuf->b_last_changedtick_i = CHANGEDTICK(curbuf);

     /*
      * Trigger InsertEnter autocommands.  Do not do this for "r<CR>" or "grx".
@@ -840,6 +843,7 @@ doESCkey:
                if (cmdchar != 'r' && cmdchar != 'v' && c != Ctrl_C)
                    ins_apply_autocmds(EVENT_INSERTLEAVE);
                did_cursorhold = FALSE;
+               curbuf->b_last_changedtick = CHANGEDTICK(curbuf);
                return (c == Ctrl_O);
            }
            continue;


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issues/13265/1749616272@github.com>

Christian Brabandt

unread,
Oct 15, 2023, 4:02:21 AM10/15/23
to vim/vim, Subscribed

Closed #13265 as completed via d7ae263.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/issue/13265/issue_event/10657089866@github.com>

Reply all
Reply to author
Forward
0 new messages