From 5cfdaaaeac0f53a621696d8eb6b5a3ba90438c98 Mon Sep 17 00:00:00 2001 From: zeertzjq Date: Tue, 16 Apr 2024 20:57:01 +0800 Subject: 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. --- src/nvim/api/buffer.c | 12 ++++++++++++ src/nvim/autocmd.c | 2 +- src/nvim/window.c | 15 ++++++++++----- 3 files changed, 23 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/nvim/api/buffer.c b/src/nvim/api/buffer.c index b32159dc96..452ba49e04 100644 --- a/src/nvim/api/buffer.c +++ b/src/nvim/api/buffer.c @@ -39,6 +39,7 @@ #include "nvim/memory_defs.h" #include "nvim/move.h" #include "nvim/ops.h" +#include "nvim/option_vars.h" #include "nvim/pos_defs.h" #include "nvim/state_defs.h" #include "nvim/types_defs.h" @@ -984,12 +985,23 @@ void nvim_buf_set_name(Buffer buffer, String name, Error *err) 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 aco_save_T aco; aucmd_prepbuf(&aco, buf); int ren_ret = rename_buffer(name.data); aucmd_restbuf(&aco); + if (!is_curbuf) { + p_acd = save_acd; + } + if (try_end(err)) { return; } diff --git a/src/nvim/autocmd.c b/src/nvim/autocmd.c index e9db4abf9b..c5d81d4cd2 100644 --- a/src/nvim/autocmd.c +++ b/src/nvim/autocmd.c @@ -1340,7 +1340,7 @@ void aucmd_prepbuf(aco_save_T *aco, buf_T *buf) win_config_float(auc_win, auc_win->w_config); } // Prevent chdir() call in win_enter_ext(), through do_autochdir() - int save_acd = p_acd; + const int save_acd = p_acd; p_acd = false; // no redrawing and don't set the window title RedrawingDisabled++; diff --git a/src/nvim/window.c b/src/nvim/window.c index 85ed73bd6d..ea879d450b 100644 --- a/src/nvim/window.c +++ b/src/nvim/window.c @@ -752,15 +752,20 @@ 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; + try_start(); + + const int save_acd = p_acd; if (!switchwin.sw_same_win) { + // Temporarily disable 'autochdir' when setting buffer in another window. p_acd = false; } - try_start(); + 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) { api_set_error(err, kErrorTypeException, -- cgit