vim-patch:9.0.1546: some commands for opening a file don't use 'switchbuf' (#23600)

Problem:    Some commands for opening a file don't use 'switchbuf'.
Solution:   Use 'switchbuf' for more commands. (Yegappan Lakshmanan,
            closes vim/vim#12383, closes vim/vim#12381)

54be5fb382

Co-authored-by: Yegappan Lakshmanan <yegappan@yahoo.com>
This commit is contained in:
zeertzjq
2023-05-13 08:40:06 +08:00
committed by GitHub
parent a6d63591f1
commit f76e1ac92e
3 changed files with 103 additions and 14 deletions

View File

@@ -6201,16 +6201,18 @@ A jump table for the options with a short description can be found at |Q_op|.
'switchbuf' 'swb' string (default "uselast") 'switchbuf' 'swb' string (default "uselast")
global global
This option controls the behavior when switching between buffers. This option controls the behavior when switching between buffers.
Mostly for |quickfix| commands some values are also used for other This option is checked, when
commands, as mentioned below. - jumping to errors with the |quickfix| commands (|:cc|, |:cn|, |:cp|,
etc.)
- jumping to a tag using the |:stag| command.
- opening a file using the |CTRL-W_f| or |CTRL-W_F| command.
- jumping to a buffer using a buffer split command (e.g. |:sbuffer|,
|:sbnext|, or |:sbrewind|).
Possible values (comma-separated list): Possible values (comma-separated list):
useopen If included, jump to the first open window that useopen If included, jump to the first open window in the
contains the specified buffer (if there is one). current tab page that contains the specified buffer
Otherwise: Do not examine other windows. (if there is one). Otherwise: Do not examine other
This setting is checked with |quickfix| commands, when windows.
jumping to errors (":cc", ":cn", "cp", etc.). It is
also used in all buffer related split commands, for
example ":sbuffer", ":sbnext", or ":sbrewind".
usetab Like "useopen", but also consider windows in other tab usetab Like "useopen", but also consider windows in other tab
pages. pages.
split If included, split the current window before loading split If included, split the current window before loading

View File

@@ -514,18 +514,44 @@ wingotofile:
tabpage_T *oldtab = curtab; tabpage_T *oldtab = curtab;
win_T *oldwin = curwin; win_T *oldwin = curwin;
setpcmark(); setpcmark();
if (win_split(0, 0) == OK) {
// If 'switchbuf' is set to 'useopen' or 'usetab' and the
// file is already opened in a window, then jump to it.
win_T *wp = NULL;
if ((swb_flags & (SWB_USEOPEN | SWB_USETAB))
&& cmdmod.cmod_tab == 0) {
buf_T *existing_buf = buflist_findname_exp(ptr);
if (existing_buf != NULL) {
if (swb_flags & SWB_USEOPEN) {
wp = buf_jump_open_win(existing_buf);
}
// If 'switchbuf' contains "usetab": jump to first
// window in any tab page containing "existing_buf"
// if one exists.
if (wp == NULL && (swb_flags & SWB_USETAB)) {
wp = buf_jump_open_tab(existing_buf);
}
}
}
if (wp == NULL && win_split(0, 0) == OK) {
RESET_BINDING(curwin); RESET_BINDING(curwin);
if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) { if (do_ecmd(0, ptr, NULL, NULL, ECMD_LASTL, ECMD_HIDE, NULL) == FAIL) {
// Failed to open the file, close the window opened for it. // Failed to open the file, close the window opened for it.
win_close(curwin, false, false); win_close(curwin, false, false);
goto_tabpage_win(oldtab, oldwin); goto_tabpage_win(oldtab, oldwin);
} else if (nchar == 'F' && lnum >= 0) { } else {
curwin->w_cursor.lnum = lnum; wp = curwin;
check_cursor_lnum();
beginline(BL_SOL | BL_FIX);
} }
} }
if (wp != NULL && nchar == 'F' && lnum >= 0) {
curwin->w_cursor.lnum = lnum;
check_cursor_lnum();
beginline(BL_SOL | BL_FIX);
}
xfree(ptr); xfree(ptr);
} }
break; break;

View File

@@ -300,4 +300,65 @@ func Test_gf_subdirs_wildcard()
set path& set path&
endfunc endfunc
" Test for 'switchbuf' with gf and gF commands
func Test_gf_switchbuf()
call writefile(repeat(["aaa"], 10), "Xtest1", 'D')
edit Xtest1
new
call setline(1, ['Xtest1'])
" Test for 'useopen'
set switchbuf=useopen
call cursor(1, 1)
exe "normal \<C-W>f"
call assert_equal([2, 2], [winnr(), winnr('$')])
close
" If the file is opened in another tabpage, then it should not be considered
tabedit Xtest1
tabfirst
exe "normal \<C-W>f"
call assert_equal([1, 2], [winnr(), winnr('$')])
call assert_equal([1, 2], [tabpagenr(), tabpagenr('$')])
close
" Test for 'usetab'
set switchbuf=usetab
exe "normal \<C-W>f"
call assert_equal([1, 1], [winnr(), winnr('$')])
call assert_equal([2, 2], [tabpagenr(), tabpagenr('$')])
%bw!
" Test for CTRL-W_F with 'useopen'
set isfname-=:
call setline(1, ['Xtest1:5'])
set switchbuf=useopen
split +1 Xtest1
wincmd b
exe "normal \<C-W>F"
call assert_equal([1, 2], [winnr(), winnr('$')])
call assert_equal(5, line('.'))
close
" If the file is opened in another tabpage, then it should not be considered
tabedit +1 Xtest1
tabfirst
exe "normal \<C-W>F"
call assert_equal([1, 2], [winnr(), winnr('$')])
call assert_equal(5, line('.'))
call assert_equal([1, 2], [tabpagenr(), tabpagenr('$')])
close
" Test for CTRL_W_F with 'usetab'
set switchbuf=usetab
exe "normal \<C-W>F"
call assert_equal([2, 2], [tabpagenr(), tabpagenr('$')])
call assert_equal([1, 1], [winnr(), winnr('$')])
call assert_equal(5, line('.'))
set switchbuf=
set isfname&
%bw!
endfunc
" vim: shiftwidth=2 sts=2 expandtab " vim: shiftwidth=2 sts=2 expandtab