refactor(change): do API changes to buffer without curbuf switch

Most of the messy things when changing a non-current buffer is
not about the buffer, it is about windows. In particular, it is about
`curwin`.

When editing a non-current buffer which is displayed in some other
window in the current tabpage, one such window will be "borrowed" as the
curwin. But this means if two or more non-current windows displayed the buffers,
one of them will be treated differenty. this is not desirable.

In particular, with nvim_buf_set_text, cursor _column_ position was only
corrected for one single window. Two new tests are added: the test
with just one non-current window passes, but the one with two didn't.

Two corresponding such tests were also added for nvim_buf_set_lines.
This already worked correctly on master, but make sure this is
well-tested for future refactors.

Also, nvim_create_buf no longer invokes autocmds just because you happened
to use `scratch=true`. No option value was changed, therefore OptionSet
must not be fired.
This commit is contained in:
bfredl
2023-08-21 14:52:17 +02:00
parent 1635c9e75e
commit 0081549547
33 changed files with 737 additions and 548 deletions

View File

@@ -164,7 +164,7 @@ static int read_buffer(int read_stdin, exarg_T *eap, int flags)
// Set or reset 'modified' before executing autocommands, so that
// it can be changed there.
if (!readonlymode && !buf_is_empty(curbuf)) {
changed();
changed(curbuf);
} else if (retval != FAIL) {
unchanged(curbuf, false, true);
}
@@ -175,20 +175,22 @@ static int read_buffer(int read_stdin, exarg_T *eap, int flags)
return retval;
}
/// Ensure buffer "buf" is loaded. Does not trigger the swap-exists action.
void buffer_ensure_loaded(buf_T *buf)
/// Ensure buffer "buf" is loaded.
bool buf_ensure_loaded(buf_T *buf)
{
if (buf->b_ml.ml_mfp != NULL) {
return;
// already open (common case)
return true;
}
aco_save_T aco;
// Make sure the buffer is in a window.
aucmd_prepbuf(&aco, buf);
swap_exists_action = SEA_NONE;
open_buffer(false, NULL, 0);
// status can be OK or NOTDONE (which also means ok/done)
int status = open_buffer(false, NULL, 0);
aucmd_restbuf(&aco);
return (status != FAIL);
}
/// Open current buffer, that is: open the memfile and read the file into
@@ -343,7 +345,7 @@ int open_buffer(int read_stdin, exarg_T *eap, int flags_arg)
if ((got_int && vim_strchr(p_cpo, CPO_INTMOD) != NULL)
|| modified_was_set // ":set modified" used in autocmd
|| (aborting() && vim_strchr(p_cpo, CPO_INTMOD) != NULL)) {
changed();
changed(curbuf);
} else if (retval != FAIL && !read_stdin && !read_fifo) {
unchanged(curbuf, false, true);
}
@@ -749,8 +751,6 @@ void buf_clear(void)
ml_delete((linenr_T)1, false);
}
deleted_lines_mark(1, line_count); // prepare for display
ml_close(curbuf, true); // free memline_T
buf_clear_file(curbuf);
}
/// buf_freeall() - free all things allocated for a buffer that are related to
@@ -2124,7 +2124,7 @@ void buflist_getfpos(void)
fpos = &buflist_findfmark(curbuf)->mark;
curwin->w_cursor.lnum = fpos->lnum;
check_cursor_lnum();
check_cursor_lnum(curwin);
if (p_sol) {
curwin->w_cursor.col = 0;