Friday, October 10, 2014

Re: TextChanged triggered on save

(redir to vim-dev)
On Fr, 10 Okt 2014, William Gardner wrote:

> I've got a plugin that registers an autocmd on TextChanged and uses the '[ and '] marks to act on the modified lines (trim trailing whitespace). There's a weird edge case that happens if you open a file and immediately save it. The save triggers TextChanged, and '[,'] defaults to 1,$. Here's a simple repro using a recently-built vim at 7.4.473:
>
> $ cat > foo
> foo
> bar
> baz
> ^D
> $ vim -N -u NONE foo
> :autocmd TextChanged * echomsg(line("'[") . "," . line("']"))
> :w
>
> And notice that "1,3" is printed in the status line.
>
> I can understand why '[,'] defaults to 1,$, but why does save trigger TextChanged?

Looks like on saving, the b:changedtick variable is incremented but the
last_changedtick variable is not, which triggers the autocommand.

This patch fixes it:

diff --git a/src/fileio.c b/src/fileio.c
--- a/src/fileio.c
+++ b/src/fileio.c
@@ -4877,6 +4877,11 @@ restore_backup:
)
{
unchanged(buf, TRUE);
+#ifdef FEAT_AUTOCMD
+ /* buf->b_changedtick is incremented in unchanged() */
+ if (last_changedtick + 1 == buf->b_changedtick)
+ last_changedtick = buf->b_changedtick;
+

No comments: