mirror of
https://github.com/neovim/neovim.git
synced 2025-09-06 03:18:16 +00:00
fix(api): ignore 'autochdir' when renaming other buf (#28376)
Problem: Renaming non-current buffer changes working directory when 'autochdir' is set. Solution: Temporarily disable 'autochdir'. Add more tests for the win_set_buf change.
This commit is contained in:
@@ -39,6 +39,7 @@
|
|||||||
#include "nvim/memory_defs.h"
|
#include "nvim/memory_defs.h"
|
||||||
#include "nvim/move.h"
|
#include "nvim/move.h"
|
||||||
#include "nvim/ops.h"
|
#include "nvim/ops.h"
|
||||||
|
#include "nvim/option_vars.h"
|
||||||
#include "nvim/pos_defs.h"
|
#include "nvim/pos_defs.h"
|
||||||
#include "nvim/state_defs.h"
|
#include "nvim/state_defs.h"
|
||||||
#include "nvim/types_defs.h"
|
#include "nvim/types_defs.h"
|
||||||
@@ -984,12 +985,23 @@ void nvim_buf_set_name(Buffer buffer, String name, Error *err)
|
|||||||
|
|
||||||
try_start();
|
try_start();
|
||||||
|
|
||||||
|
const bool is_curbuf = buf == curbuf;
|
||||||
|
const int save_acd = p_acd;
|
||||||
|
if (!is_curbuf) {
|
||||||
|
// Temporarily disable 'autochdir' when setting file name for another buffer.
|
||||||
|
p_acd = false;
|
||||||
|
}
|
||||||
|
|
||||||
// Using aucmd_*: autocommands will be executed by rename_buffer
|
// Using aucmd_*: autocommands will be executed by rename_buffer
|
||||||
aco_save_T aco;
|
aco_save_T aco;
|
||||||
aucmd_prepbuf(&aco, buf);
|
aucmd_prepbuf(&aco, buf);
|
||||||
int ren_ret = rename_buffer(name.data);
|
int ren_ret = rename_buffer(name.data);
|
||||||
aucmd_restbuf(&aco);
|
aucmd_restbuf(&aco);
|
||||||
|
|
||||||
|
if (!is_curbuf) {
|
||||||
|
p_acd = save_acd;
|
||||||
|
}
|
||||||
|
|
||||||
if (try_end(err)) {
|
if (try_end(err)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@@ -1340,7 +1340,7 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf)
|
|||||||
win_config_float(auc_win, auc_win->w_config);
|
win_config_float(auc_win, auc_win->w_config);
|
||||||
}
|
}
|
||||||
// Prevent chdir() call in win_enter_ext(), through do_autochdir()
|
// Prevent chdir() call in win_enter_ext(), through do_autochdir()
|
||||||
int save_acd = p_acd;
|
const int save_acd = p_acd;
|
||||||
p_acd = false;
|
p_acd = false;
|
||||||
// no redrawing and don't set the window title
|
// no redrawing and don't set the window title
|
||||||
RedrawingDisabled++;
|
RedrawingDisabled++;
|
||||||
|
@@ -752,15 +752,20 @@ void win_set_buf(win_T *win, buf_T *buf, Error *err)
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
// temporarily disable 'autochdir' when using win_set_buf
|
try_start();
|
||||||
// on non-current window
|
|
||||||
int save_acd = p_acd;
|
const int save_acd = p_acd;
|
||||||
if (!switchwin.sw_same_win) {
|
if (!switchwin.sw_same_win) {
|
||||||
|
// Temporarily disable 'autochdir' when setting buffer in another window.
|
||||||
p_acd = false;
|
p_acd = false;
|
||||||
}
|
}
|
||||||
try_start();
|
|
||||||
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
|
int result = do_buffer(DOBUF_GOTO, DOBUF_FIRST, FORWARD, buf->b_fnum, 0);
|
||||||
p_acd = save_acd;
|
|
||||||
|
if (!switchwin.sw_same_win) {
|
||||||
|
p_acd = save_acd;
|
||||||
|
}
|
||||||
|
|
||||||
if (!try_end(err) && result == FAIL) {
|
if (!try_end(err) && result == FAIL) {
|
||||||
api_set_error(err,
|
api_set_error(err,
|
||||||
kErrorTypeException,
|
kErrorTypeException,
|
||||||
|
@@ -2048,6 +2048,37 @@ describe('api/buf', function()
|
|||||||
eq(1, fn.filereadable(new_name))
|
eq(1, fn.filereadable(new_name))
|
||||||
os.remove(new_name)
|
os.remove(new_name)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("with 'autochdir'", function()
|
||||||
|
local topdir
|
||||||
|
local oldbuf
|
||||||
|
local newbuf
|
||||||
|
|
||||||
|
before_each(function()
|
||||||
|
command('set shellslash')
|
||||||
|
topdir = fn.getcwd()
|
||||||
|
t.mkdir(topdir .. '/Xacd')
|
||||||
|
|
||||||
|
oldbuf = api.nvim_get_current_buf()
|
||||||
|
command('vnew')
|
||||||
|
newbuf = api.nvim_get_current_buf()
|
||||||
|
command('set autochdir')
|
||||||
|
end)
|
||||||
|
|
||||||
|
after_each(function()
|
||||||
|
t.rmdir(topdir .. '/Xacd')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('does not change cwd with non-current buffer', function()
|
||||||
|
api.nvim_buf_set_name(oldbuf, topdir .. '/Xacd/foo.txt')
|
||||||
|
eq(topdir, fn.getcwd())
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('changes cwd with current buffer', function()
|
||||||
|
api.nvim_buf_set_name(newbuf, topdir .. '/Xacd/foo.txt')
|
||||||
|
eq(topdir .. '/Xacd', fn.getcwd())
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('nvim_buf_is_loaded', function()
|
describe('nvim_buf_is_loaded', function()
|
||||||
|
@@ -111,6 +111,44 @@ describe('API/win', function()
|
|||||||
api.nvim_win_set_buf(new_win, next_buf)
|
api.nvim_win_set_buf(new_win, next_buf)
|
||||||
eq(next_buf, api.nvim_win_get_buf(new_win))
|
eq(next_buf, api.nvim_win_get_buf(new_win))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
describe("with 'autochdir'", function()
|
||||||
|
local topdir
|
||||||
|
local otherbuf
|
||||||
|
local oldwin
|
||||||
|
local newwin
|
||||||
|
|
||||||
|
before_each(function()
|
||||||
|
command('set shellslash')
|
||||||
|
topdir = fn.getcwd()
|
||||||
|
t.mkdir(topdir .. '/Xacd')
|
||||||
|
t.mkdir(topdir .. '/Xacd/foo')
|
||||||
|
otherbuf = api.nvim_create_buf(false, true)
|
||||||
|
api.nvim_buf_set_name(otherbuf, topdir .. '/Xacd/baz.txt')
|
||||||
|
|
||||||
|
command('set autochdir')
|
||||||
|
command('edit Xacd/foo/bar.txt')
|
||||||
|
eq(topdir .. '/Xacd/foo', fn.getcwd())
|
||||||
|
|
||||||
|
oldwin = api.nvim_get_current_win()
|
||||||
|
command('vsplit')
|
||||||
|
newwin = api.nvim_get_current_win()
|
||||||
|
end)
|
||||||
|
|
||||||
|
after_each(function()
|
||||||
|
t.rmdir(topdir .. '/Xacd')
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('does not change cwd with non-current window', function()
|
||||||
|
api.nvim_win_set_buf(oldwin, otherbuf)
|
||||||
|
eq(topdir .. '/Xacd/foo', fn.getcwd())
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('changes cwd with current window', function()
|
||||||
|
api.nvim_win_set_buf(newwin, otherbuf)
|
||||||
|
eq(topdir .. '/Xacd', fn.getcwd())
|
||||||
|
end)
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe('{get,set}_cursor', function()
|
describe('{get,set}_cursor', function()
|
||||||
@@ -1749,29 +1787,44 @@ describe('API/win', function()
|
|||||||
)
|
)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('do not change dir when enter is false', function()
|
describe("with 'autochdir'", function()
|
||||||
local expected = fn.getcwd() .. '/foo'
|
local topdir
|
||||||
t.mkdir('foo')
|
local otherbuf
|
||||||
exec_lua [[
|
|
||||||
vim.opt.autochdir = true
|
before_each(function()
|
||||||
local buf = vim.api.nvim_create_buf(false, true)
|
command('set shellslash')
|
||||||
vim.api.nvim_buf_set_name(buf, 'Foo')
|
topdir = fn.getcwd()
|
||||||
vim.api.nvim_create_autocmd('CmdlineEnter', {
|
t.mkdir(topdir .. '/Xacd')
|
||||||
callback = function()
|
t.mkdir(topdir .. '/Xacd/foo')
|
||||||
local winid = vim.api.nvim_open_win(buf, false, {
|
otherbuf = api.nvim_create_buf(false, true)
|
||||||
relative = 'editor',
|
api.nvim_buf_set_name(otherbuf, topdir .. '/Xacd/baz.txt')
|
||||||
height = 1,
|
|
||||||
width = 1,
|
command('set autochdir')
|
||||||
row = 1,
|
command('edit Xacd/foo/bar.txt')
|
||||||
col = 1,
|
eq(topdir .. '/Xacd/foo', fn.getcwd())
|
||||||
})
|
end)
|
||||||
vim.api.nvim_win_close(winid, true)
|
|
||||||
end,
|
after_each(function()
|
||||||
})
|
t.rmdir(topdir .. '/Xacd')
|
||||||
]]
|
end)
|
||||||
t.feed(':edit foo/bar.txt<CR>')
|
|
||||||
eq(t.is_os('win') and expected:gsub('/', '\\') or expected, fn.getcwd())
|
it('does not change cwd with enter=false #15280', function()
|
||||||
t.rmdir('foo')
|
api.nvim_open_win(
|
||||||
|
otherbuf,
|
||||||
|
false,
|
||||||
|
{ relative = 'editor', height = 5, width = 5, row = 5, col = 5 }
|
||||||
|
)
|
||||||
|
eq(topdir .. '/Xacd/foo', fn.getcwd())
|
||||||
|
end)
|
||||||
|
|
||||||
|
it('changes cwd with enter=true', function()
|
||||||
|
api.nvim_open_win(
|
||||||
|
otherbuf,
|
||||||
|
true,
|
||||||
|
{ relative = 'editor', height = 5, width = 5, row = 5, col = 5 }
|
||||||
|
)
|
||||||
|
eq(topdir .. '/Xacd', fn.getcwd())
|
||||||
|
end)
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user