From 30b4c1b40d3c5a69cc2d53727e8e3f53bdb6be70 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 4 Aug 2026 16:50:36 +0800 Subject: [PATCH] fix(ctx): missing options copy when loading hidden buffer #41149 It's necessary to copy the global 'fileencoding' to the buffer-local value before entering the buffer, otherwise 'fileencoding' is changed when reading the file, which will mark the file as modified. --- src/nvim/context.c | 2 ++ test/functional/vimscript/buf_functions_spec.lua | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/nvim/context.c b/src/nvim/context.c index 11e15d7466..19d3712847 100644 --- a/src/nvim/context.c +++ b/src/nvim/context.c @@ -491,6 +491,8 @@ bool ctx_switch(CtxSwitch *cs, win_T *wp, tabpage_T *tp, buf_T *buf, CtxSwitchFl wp = ctx_win_prep(cs, buf); // Leave the window we entered "from". leaving_window(curwin); + // We will soon enter "buf" and may have to copy buffer options. + buf_copy_options(buf, BCO_ENTER | BCO_NOHELP); prevwin = curwin; } assert(win_valid(wp)); diff --git a/test/functional/vimscript/buf_functions_spec.lua b/test/functional/vimscript/buf_functions_spec.lua index 23262acc81..d04dc5a9c2 100644 --- a/test/functional/vimscript/buf_functions_spec.lua +++ b/test/functional/vimscript/buf_functions_spec.lua @@ -324,3 +324,19 @@ describe('setbufvar() function', function() eq('Vim:E928: String required', pcall_err(fn.setbufvar, '', '&errorformat', true)) end) end) + +describe('bufload() function', function() + before_each(function() + t.write_file(fname, 'foo\n') + end) + after_each(function() + os.remove(fname) + end) + it('does not set hidden buffer as modified #41148', function() + local buf = fn.bufadd(fname) + fn.bufload(buf) + api.nvim_command('edit ' .. fname) + eq(true, api.nvim_buf_is_loaded(buf)) + eq(false, api.nvim_get_option_value('modified', { buf = buf })) + end) +end)