diff options
author | Raphael <glephunter@gmail.com> | 2024-04-16 19:49:56 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-16 19:49:56 +0800 |
commit | 2fc2343728831d890a043def5d9d714947737cf6 (patch) | |
tree | f1b74509b333662e75dddc8eda730bbf53436115 | |
parent | 3065e2fa5da05de5b0d5bb3f3f3c64a053d20dc7 (diff) | |
download | rneovim-2fc2343728831d890a043def5d9d714947737cf6.tar.gz rneovim-2fc2343728831d890a043def5d9d714947737cf6.tar.bz2 rneovim-2fc2343728831d890a043def5d9d714947737cf6.zip |
fix(api): ignore 'autochdir' when setting buf in other win (#28371)
Problem: Wrong working directory when setting buffer in another window with
'autochdir' enabled.
Solution: Temporarily disable 'autochdir'.
-rw-r--r-- | src/nvim/window.c | 7 | ||||
-rw-r--r-- | test/functional/api/window_spec.lua | 25 |
2 files changed, 32 insertions, 0 deletions
diff --git a/src/nvim/window.c b/src/nvim/window.c index d86584e9ce..85ed73bd6d 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -752,8 +752,15 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err) goto cleanup; } + // temporarily disable 'autochdir' when using win_set_buf + // on non-current window + int save_acd = p_acd; + if (!switchwin.sw_same_win) { + p_acd = false; + } try_start(); int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0); + p_acd = save_acd; if (!try_end(err) && result == FAIL) { api_set_error(err, kErrorTypeException, diff --git a/test/functional/api/window_spec.lua b/test/functional/api/window_spec.lua index 4f0da7c0f4..2ad5f0e799 100644 --- a/test/functional/api/window_spec.lua +++ b/test/functional/api/window_spec.lua @@ -1748,6 +1748,31 @@ describe('API/win', function() pcall_err(api.nvim_open_win, 0, true, { split = 'below', win = 0 }) ) end) + + it('do not change dir when enter is false', function() + local expected = fn.getcwd() .. '/foo' + t.mkdir('foo') + exec_lua [[ + vim.opt.autochdir = true + local buf = vim.api.nvim_create_buf(false, true) + vim.api.nvim_buf_set_name(buf, 'Foo') + vim.api.nvim_create_autocmd('CmdlineEnter', { + callback = function() + local winid = vim.api.nvim_open_win(buf, false, { + relative = 'editor', + height = 1, + width = 1, + row = 1, + col = 1, + }) + vim.api.nvim_win_close(winid, true) + end, + }) + ]] + t.feed(':edit foo/bar.txt<CR>') + eq(t.is_os('win') and expected:gsub('/', '\\') or expected, fn.getcwd()) + t.rmdir('foo') + end) end) describe('set_config', function() |