vim-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.
c024b46678
This commit is contained in:
Justin M. Keyes 2019-08-31 14:06:45 +02:00
parent d7aea13fee
commit 83c5701fe6
7 changed files with 26 additions and 22 deletions

View File

@ -1313,8 +1313,10 @@ One local buffer variable is predefined:
*b:changedtick* *changetick* *b:changedtick* *changetick*
b:changedtick The total number of changes to the current buffer. It is b:changedtick The total number of changes to the current buffer. It is
incremented for each change. An undo command is also a change incremented for each change. An undo command is also a change
in this case. This can be used to perform an action only when in this case. Resetting 'modified' when writing the buffer is
the buffer has changed. Example: > also counted.
This can be used to perform an action only when the buffer has
changed. Example: >
:if my_changedtick != b:changedtick :if my_changedtick != b:changedtick
: let my_changedtick = b:changedtick : let my_changedtick = b:changedtick
: call My_Update() : call My_Update()

View File

@ -144,7 +144,7 @@ read_buffer(
if (!readonlymode && !BUFEMPTY()) { if (!readonlymode && !BUFEMPTY()) {
changed(); changed();
} else if (retval != FAIL) { } else if (retval != FAIL) {
unchanged(curbuf, false); unchanged(curbuf, false, true);
} }
apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, false, apply_autocmds_retval(EVENT_STDINREADPOST, NULL, NULL, false,
@ -299,7 +299,7 @@ int open_buffer(
|| (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)) { || (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)) {
changed(); changed();
} else if (retval != FAIL && !read_stdin && !read_fifo) { } else if (retval != FAIL && !read_stdin && !read_fifo) {
unchanged(curbuf, false); unchanged(curbuf, false, true);
} }
save_file_ff(curbuf); // keep this fileformat save_file_ff(curbuf); // keep this fileformat
@ -641,13 +641,11 @@ void close_buffer(win_T *win, buf_T *buf, int action, int abort_if_last)
} }
} }
/* /// Make buffer not contain a file.
* Make buffer not contain a file.
*/
void buf_clear_file(buf_T *buf) void buf_clear_file(buf_T *buf)
{ {
buf->b_ml.ml_line_count = 1; buf->b_ml.ml_line_count = 1;
unchanged(buf, true); unchanged(buf, true, true);
buf->b_p_eol = true; buf->b_p_eol = true;
buf->b_start_eol = true; buf->b_start_eol = true;
buf->b_p_bomb = false; buf->b_p_bomb = false;

View File

@ -478,9 +478,11 @@ changed_lines(
} }
} }
/// Called when the changed flag must be reset for buffer "buf". /// Called when the changed flag must be reset for buffer `buf`.
/// When "ff" is true also reset 'fileformat'. /// When `ff` is true also reset 'fileformat'.
void unchanged(buf_T *buf, int ff) /// When `always_inc_changedtick` is true b:changedtick is incremented even
/// when the changed flag was off.
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;
@ -491,8 +493,10 @@ void unchanged(buf_T *buf, int ff)
check_status(buf); check_status(buf);
redraw_tabline = true; redraw_tabline = true;
need_maketitle = true; // set window title later need_maketitle = true; // set window title later
buf_inc_changedtick(buf);
} else if (always_inc_changedtick) {
buf_inc_changedtick(buf);
} }
buf_inc_changedtick(buf);
} }
/// Insert string "p" at the cursor position. Stops at a NUL byte. /// Insert string "p" at the cursor position. Stops at a NUL byte.

View File

@ -1323,7 +1323,7 @@ void dialog_changed(buf_T *buf, bool checkall)
(void)buf_write_all(buf, false); (void)buf_write_all(buf, false);
} }
} else if (ret == VIM_NO) { } else if (ret == VIM_NO) {
unchanged(buf, true); unchanged(buf, true, false);
} else if (ret == VIM_ALL) { } else if (ret == VIM_ALL) {
// Write all modified files that can be written. // Write all modified files that can be written.
// Skip readonly buffers, these need to be confirmed // Skip readonly buffers, these need to be confirmed
@ -1348,7 +1348,7 @@ void dialog_changed(buf_T *buf, bool checkall)
} else if (ret == VIM_DISCARDALL) { } else if (ret == VIM_DISCARDALL) {
// mark all buffers as unchanged // mark all buffers as unchanged
FOR_ALL_BUFFERS(buf2) { FOR_ALL_BUFFERS(buf2) {
unchanged(buf2, true); unchanged(buf2, true, false);
} }
} }
} }

View File

@ -3485,10 +3485,10 @@ restore_backup:
if (reset_changed && whole && !append if (reset_changed && whole && !append
&& !write_info.bw_conv_error && !write_info.bw_conv_error
&& (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) { && (overwriting || vim_strchr(p_cpo, CPO_PLUS) != NULL)) {
unchanged(buf, true); unchanged(buf, true, false);
const varnumber_T changedtick = buf_get_changedtick(buf); const varnumber_T changedtick = buf_get_changedtick(buf);
if (buf->b_last_changedtick + 1 == changedtick) { if (buf->b_last_changedtick + 1 == changedtick) {
// changedtick is always incremented in unchanged() but that // b:changedtick may be incremented in unchanged() but that
// should not trigger a TextChanged event. // should not trigger a TextChanged event.
buf->b_last_changedtick = changedtick; buf->b_last_changedtick = changedtick;
} }
@ -5107,14 +5107,14 @@ void buf_reload(buf_T *buf, int orig_mode)
} }
(void)move_lines(savebuf, buf); (void)move_lines(savebuf, buf);
} }
} else if (buf == curbuf) { /* "buf" still valid */ } else if (buf == curbuf) { // "buf" still valid.
/* Mark the buffer as unmodified and free undo info. */ // Mark the buffer as unmodified and free undo info.
unchanged(buf, TRUE); unchanged(buf, true, true);
if ((flags & READ_KEEP_UNDO) == 0) { if ((flags & READ_KEEP_UNDO) == 0) {
u_blockfree(buf); u_blockfree(buf);
u_clearall(buf); u_clearall(buf);
} else { } else {
/* Mark all undo states as changed. */ // Mark all undo states as changed.
u_unchanged(curbuf); u_unchanged(curbuf);
} }
} }

View File

@ -1003,7 +1003,7 @@ void ml_recover(void)
set_option_value("fenc", 0L, (char *)b0_fenc, OPT_LOCAL); set_option_value("fenc", 0L, (char *)b0_fenc, OPT_LOCAL);
xfree(b0_fenc); xfree(b0_fenc);
} }
unchanged(curbuf, TRUE); unchanged(curbuf, true, true);
bnum = 1; /* start with block 1 */ bnum = 1; /* start with block 1 */
page_count = 1; /* which is 1 page */ page_count = 1; /* which is 1 page */

View File

@ -2294,7 +2294,7 @@ static void u_undoredo(int undo, bool do_buf_event)
if (old_flags & UH_CHANGED) { if (old_flags & UH_CHANGED) {
changed(); changed();
} else { } else {
unchanged(curbuf, FALSE); unchanged(curbuf, false, true);
} }
// because the calls to changed()/unchanged() above will bump changedtick // because the calls to changed()/unchanged() above will bump changedtick