Skip to content

Commit c024b46

Browse files
committed
patch 8.1.1498: ":write" increments b:changedtick even though nothing changed
Problem: ":write" increments b:changedtick even though nothing changed. (Daniel Hahler) Solution: Only increment b:changedtick if the modified flag is reset.
1 parent aef5c62 commit c024b46

9 files changed

Lines changed: 26 additions & 16 deletions

File tree

runtime/doc/eval.txt

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,8 +1504,10 @@ One local buffer variable is predefined:
15041504
*b:changedtick* *changetick*
15051505
b:changedtick The total number of changes to the current buffer. It is
15061506
incremented for each change. An undo command is also a change
1507-
in this case. This can be used to perform an action only when
1508-
the buffer has changed. Example: >
1507+
in this case. Resetting 'modified' when writing the buffer is
1508+
also counted.
1509+
This can be used to perform an action only when the buffer has
1510+
changed. Example: >
15091511
:if my_changedtick != b:changedtick
15101512
: let my_changedtick = b:changedtick
15111513
: call My_Update()

src/buffer.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ static char *e_auabort = N_("E855: Autocommands caused command to abort");
6060
/* Number of times free_buffer() was called. */
6161
static int buf_free_count = 0;
6262

63-
/* Read data from buffer for retrying. */
63+
/*
64+
* Read data from buffer for retrying.
65+
*/
6466
static int
6567
read_buffer(
6668
int read_stdin, /* read file from stdin, otherwise fifo */
@@ -104,7 +106,7 @@ read_buffer(
104106
if (!readonlymode && !BUFEMPTY())
105107
changed();
106108
else if (retval == OK)
107-
unchanged(curbuf, FALSE);
109+
unchanged(curbuf, FALSE, TRUE);
108110

109111
if (retval == OK)
110112
{
@@ -275,7 +277,7 @@ open_buffer(
275277
)
276278
changed();
277279
else if (retval == OK && !read_stdin && !read_fifo)
278-
unchanged(curbuf, FALSE);
280+
unchanged(curbuf, FALSE, TRUE);
279281
save_file_ff(curbuf); /* keep this fileformat */
280282

281283
/* Set last_changedtick to avoid triggering a TextChanged autocommand right
@@ -700,7 +702,7 @@ close_buffer(
700702
buf_clear_file(buf_T *buf)
701703
{
702704
buf->b_ml.ml_line_count = 1;
703-
unchanged(buf, TRUE);
705+
unchanged(buf, TRUE, TRUE);
704706
buf->b_shortname = FALSE;
705707
buf->b_p_eol = TRUE;
706708
buf->b_start_eol = TRUE;

src/change.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -842,9 +842,11 @@ changed_lines(
842842
/*
843843
* Called when the changed flag must be reset for buffer "buf".
844844
* When "ff" is TRUE also reset 'fileformat'.
845+
* When "always_inc_changedtick" is TRUE b:changedtick is incremented also when
846+
* the changed flag was off.
845847
*/
846848
void
847-
unchanged(buf_T *buf, int ff)
849+
unchanged(buf_T *buf, int ff, int always_inc_changedtick)
848850
{
849851
if (buf->b_changed || (ff && file_ff_differs(buf, FALSE)))
850852
{
@@ -857,8 +859,10 @@ unchanged(buf_T *buf, int ff)
857859
#ifdef FEAT_TITLE
858860
need_maketitle = TRUE; // set window title later
859861
#endif
862+
++CHANGEDTICK(buf);
860863
}
861-
++CHANGEDTICK(buf);
864+
else if (always_inc_changedtick)
865+
++CHANGEDTICK(buf);
862866
#ifdef FEAT_NETBEANS_INTG
863867
netbeans_unmodified(buf);
864868
#endif

src/ex_cmds2.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,7 +1197,7 @@ dialog_changed(
11971197
}
11981198
else if (ret == VIM_NO)
11991199
{
1200-
unchanged(buf, TRUE);
1200+
unchanged(buf, TRUE, FALSE);
12011201
}
12021202
else if (ret == VIM_ALL)
12031203
{
@@ -1240,7 +1240,7 @@ dialog_changed(
12401240
* mark all buffers as unchanged
12411241
*/
12421242
FOR_ALL_BUFFERS(buf2)
1243-
unchanged(buf2, TRUE);
1243+
unchanged(buf2, TRUE, FALSE);
12441244
}
12451245
}
12461246
#endif

src/fileio.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4908,8 +4908,8 @@ buf_write(
49084908
&& !write_info.bw_conv_error
49094909
&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL))
49104910
{
4911-
unchanged(buf, TRUE);
4912-
/* b:changedtick is always incremented in unchanged() but that
4911+
unchanged(buf, TRUE, FALSE);
4912+
/* b:changedtick is may be incremented in unchanged() but that
49134913
* should not trigger a TextChanged event. */
49144914
if (buf->b_last_changedtick + 1 == CHANGEDTICK(buf))
49154915
buf->b_last_changedtick = CHANGEDTICK(buf);
@@ -7081,7 +7081,7 @@ buf_reload(buf_T *buf, int orig_mode)
70817081
else if (buf == curbuf) /* "buf" still valid */
70827082
{
70837083
/* Mark the buffer as unmodified and free undo info. */
7084-
unchanged(buf, TRUE);
7084+
unchanged(buf, TRUE, TRUE);
70857085
if ((flags & READ_KEEP_UNDO) == 0)
70867086
{
70877087
u_blockfree(buf);

src/memline.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,7 @@ ml_recover(int checkext)
14351435
set_option_value((char_u *)"fenc", 0L, b0_fenc, OPT_LOCAL);
14361436
vim_free(b0_fenc);
14371437
}
1438-
unchanged(curbuf, TRUE);
1438+
unchanged(curbuf, TRUE, TRUE);
14391439

14401440
bnum = 1; /* start with block 1 */
14411441
page_count = 1; /* which is 1 page */

src/proto/change.pro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ void appended_lines_mark(linenr_T lnum, long count);
1414
void deleted_lines(linenr_T lnum, long count);
1515
void deleted_lines_mark(linenr_T lnum, long count);
1616
void changed_lines(linenr_T lnum, colnr_T col, linenr_T lnume, long xtra);
17-
void unchanged(buf_T *buf, int ff);
17+
void unchanged(buf_T *buf, int ff, int always_inc_changedtick);
1818
void ins_bytes(char_u *p);
1919
void ins_bytes_len(char_u *p, int len);
2020
void ins_char(int c);

src/undo.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2805,7 +2805,7 @@ u_undoredo(int undo)
28052805
/* per netbeans undo rules, keep it as modified */
28062806
if (!isNetbeansModified(curbuf))
28072807
#endif
2808-
unchanged(curbuf, FALSE);
2808+
unchanged(curbuf, FALSE, TRUE);
28092809

28102810
/*
28112811
* restore marks from before undo/redo

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,8 @@ static char *(features[]) =
767767

768768
static int included_patches[] =
769769
{ /* Add new patch number below this line */
770+
/**/
771+
1498,
770772
/**/
771773
1497,
772774
/**/

0 commit comments

Comments
 (0)