diff options
-rw-r--r-- | src/nvim/api/options.c | 31 | ||||
-rw-r--r-- | test/functional/api/vim_spec.lua | 11 |
2 files changed, 31 insertions, 11 deletions
diff --git a/src/nvim/api/options.c b/src/nvim/api/options.c index 8c174fc129..4ed676e613 100644 --- a/src/nvim/api/options.c +++ b/src/nvim/api/options.c @@ -504,6 +504,7 @@ static int access_option_value(char *key, long *numval, char **stringval, int op static int access_option_value_for(char *key, long *numval, char **stringval, int opt_flags, int opt_type, void *from, bool get, Error *err) { + bool need_switch = false; switchwin_T switchwin; aco_save_T aco; int result = 0; @@ -511,24 +512,32 @@ static int access_option_value_for(char *key, long *numval, char **stringval, in try_start(); switch (opt_type) { case SREQ_WIN: - if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true) - == FAIL) { - restore_win_noblock(&switchwin, true); - if (try_end(err)) { + need_switch = (win_T *)from != curwin; + if (need_switch) { + if (switch_win_noblock(&switchwin, (win_T *)from, win_find_tabpage((win_T *)from), true) + == FAIL) { + restore_win_noblock(&switchwin, true); + if (try_end(err)) { + return result; + } + api_set_error(err, kErrorTypeException, "Problem while switching windows"); return result; } - api_set_error(err, - kErrorTypeException, - "Problem while switching windows"); - return result; } result = access_option_value(key, numval, stringval, opt_flags, get, err); - restore_win_noblock(&switchwin, true); + if (need_switch) { + restore_win_noblock(&switchwin, true); + } break; case SREQ_BUF: - aucmd_prepbuf(&aco, (buf_T *)from); + need_switch = (buf_T *)from != curbuf; + if (need_switch) { + aucmd_prepbuf(&aco, (buf_T *)from); + } result = access_option_value(key, numval, stringval, opt_flags, get, err); - aucmd_restbuf(&aco); + if (need_switch) { + aucmd_restbuf(&aco); + } break; case SREQ_GLOBAL: result = access_option_value(key, numval, stringval, opt_flags, get, err); diff --git a/test/functional/api/vim_spec.lua b/test/functional/api/vim_spec.lua index 989ed27e16..63a4e9a39c 100644 --- a/test/functional/api/vim_spec.lua +++ b/test/functional/api/vim_spec.lua @@ -1469,6 +1469,17 @@ describe('API', function() nvim('win_set_option', win, 'number', true) eq(true, nvim('get_option_value', 'number', {win = win})) end) + + it('getting current buffer option does not adjust cursor #19381', function() + nvim('command', 'new') + local buf = nvim('get_current_buf').id + local win = nvim('get_current_win').id + insert('some text') + feed('0v$') + eq({1, 9}, nvim('win_get_cursor', win)) + nvim('get_option_value', 'filetype', {buf = buf}) + eq({1, 9}, nvim('win_get_cursor', win)) + end) end) describe('nvim_{get,set}_current_buf, nvim_list_bufs', function() |