From 008154954791001efcc46c28146e21403f3a698b Mon Sep 17 00:00:00 2001 From: bfredl Date: Mon, 21 Aug 2023 14:52:17 +0200 Subject: 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. --- src/nvim/optionstr.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'src/nvim/optionstr.c') diff --git a/src/nvim/optionstr.c b/src/nvim/optionstr.c index f07c05c113..07e2e5eed1 100644 --- a/src/nvim/optionstr.c +++ b/src/nvim/optionstr.c @@ -406,6 +406,19 @@ void set_string_option_direct_in_win(win_T *wp, const char *name, int opt_idx, c unblock_autocmds(); } +/// Like set_string_option_direct(), but for a buffer-local option in "buf". +/// Blocks autocommands to avoid the old curwin becoming invalid. +void set_string_option_direct_in_buf(buf_T *buf, const char *name, int opt_idx, const char *val, + int opt_flags, int set_sid) +{ + buf_T *save_curbuf = curbuf; + + block_autocmds(); + curbuf = buf; + set_string_option_direct(name, opt_idx, val, opt_flags, set_sid); + curbuf = save_curbuf; + unblock_autocmds(); +} /// Set a string option to a new value, handling the effects /// /// @param[in] opt_idx Option to set. -- cgit