Skip to content

Commit 8ad80de

Browse files
committed
patch 8.0.0621: :stag does not respect 'switchbuf'
Problem: The ":stag" command does not respect 'switchbuf'. Solution: Check 'switchbuf' for tag commands that may open a new window. (Ingo Karkat, closes #1681) Define macros for the return values of getfile().
1 parent b463e8d commit 8ad80de

7 files changed

Lines changed: 103 additions & 23 deletions

File tree

src/buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2352,8 +2352,8 @@ buflist_getfile(
23522352
#endif
23532353

23542354
++RedrawingDisabled;
2355-
if (getfile(buf->b_fnum, NULL, NULL, (options & GETF_SETMARK),
2356-
lnum, forceit) <= 0)
2355+
if (GETFILE_SUCCESS(getfile(buf->b_fnum, NULL, NULL,
2356+
(options & GETF_SETMARK), lnum, forceit)))
23572357
{
23582358
--RedrawingDisabled;
23592359

src/ex_cmds.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3520,11 +3520,14 @@ check_readonly(int *forceit, buf_T *buf)
35203520

35213521
/*
35223522
* Try to abandon current file and edit a new or existing file.
3523-
* 'fnum' is the number of the file, if zero use ffname/sfname.
3523+
* "fnum" is the number of the file, if zero use ffname/sfname.
3524+
* "lnum" is the line number for the cursor in the new file (if non-zero).
35243525
*
3525-
* Return 1 for "normal" error, 2 for "not written" error, 0 for success
3526-
* -1 for successfully opening another file.
3527-
* 'lnum' is the line number for the cursor in the new file (if non-zero).
3526+
* Return:
3527+
* GETFILE_ERROR for "normal" error,
3528+
* GETFILE_NOT_WRITTEN for "not written" error,
3529+
* GETFILE_SAME_FILE for success
3530+
* GETFILE_OPEN_OTHER for successfully opening another file.
35283531
*/
35293532
int
35303533
getfile(
@@ -3540,10 +3543,10 @@ getfile(
35403543
char_u *free_me = NULL;
35413544

35423545
if (text_locked())
3543-
return 1;
3546+
return GETFILE_ERROR;
35443547
#ifdef FEAT_AUTOCMD
35453548
if (curbuf_locked())
3546-
return 1;
3549+
return GETFILE_ERROR;
35473550
#endif
35483551

35493552
if (fnum == 0)
@@ -3570,7 +3573,7 @@ getfile(
35703573
if (other)
35713574
--no_wait_return;
35723575
EMSG(_(e_nowrtmsg));
3573-
retval = 2; /* file has been changed */
3576+
retval = GETFILE_NOT_WRITTEN; /* file has been changed */
35743577
goto theend;
35753578
}
35763579
}
@@ -3584,14 +3587,14 @@ getfile(
35843587
curwin->w_cursor.lnum = lnum;
35853588
check_cursor_lnum();
35863589
beginline(BL_SOL | BL_FIX);
3587-
retval = 0; /* it's in the same file */
3590+
retval = GETFILE_SAME_FILE; /* it's in the same file */
35883591
}
35893592
else if (do_ecmd(fnum, ffname, sfname, NULL, lnum,
35903593
(P_HID(curbuf) ? ECMD_HIDE : 0) + (forceit ? ECMD_FORCEIT : 0),
35913594
curwin) == OK)
3592-
retval = -1; /* opened another file */
3595+
retval = GETFILE_OPEN_OTHER; /* opened another file */
35933596
else
3594-
retval = 1; /* error encountered */
3597+
retval = GETFILE_ERROR; /* error encountered */
35953598

35963599
theend:
35973600
vim_free(free_me);

src/search.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,9 +1524,9 @@ do_search(
15241524
* search_for_exact_line(buf, pos, dir, pat)
15251525
*
15261526
* Search for a line starting with the given pattern (ignoring leading
1527-
* white-space), starting from pos and going in direction dir. pos will
1527+
* white-space), starting from pos and going in direction "dir". "pos" will
15281528
* contain the position of the match found. Blank lines match only if
1529-
* ADDING is set. if p_ic is set then the pattern must be in lowercase.
1529+
* ADDING is set. If p_ic is set then the pattern must be in lowercase.
15301530
* Return OK for success, or FAIL if no line found.
15311531
*/
15321532
int
@@ -5397,8 +5397,9 @@ find_pattern_in_path(
53975397
#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
53985398
if (g_do_tagpreview != 0)
53995399
{
5400-
if (getfile(0, curwin_save->w_buffer->b_fname,
5401-
NULL, TRUE, lnum, FALSE) > 0)
5400+
if (!GETFILE_SUCCESS(getfile(
5401+
0, curwin_save->w_buffer->b_fname,
5402+
NULL, TRUE, lnum, FALSE)))
54025403
break; /* failed to jump to file */
54035404
}
54045405
else
@@ -5408,8 +5409,9 @@ find_pattern_in_path(
54085409
}
54095410
else
54105411
{
5411-
if (getfile(0, files[depth].name, NULL, TRUE,
5412-
files[depth].lnum, FALSE) > 0)
5412+
if (!GETFILE_SUCCESS(getfile(
5413+
0, files[depth].name, NULL, TRUE,
5414+
files[depth].lnum, FALSE)))
54135415
break; /* failed to jump to file */
54145416
/* autocommands may have changed the lnum, we don't
54155417
* want that here */

src/tag.c

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3088,7 +3088,7 @@ jumpto_tag(
30883088
char_u *fname;
30893089
tagptrs_T tagp;
30903090
int retval = FAIL;
3091-
int getfile_result;
3091+
int getfile_result = GETFILE_UNUSED;
30923092
int search_options;
30933093
#ifdef FEAT_SEARCH_EXTRA
30943094
int save_no_hlsearch;
@@ -3202,7 +3202,29 @@ jumpto_tag(
32023202

32033203
/* If it was a CTRL-W CTRL-] command split window now. For ":tab tag"
32043204
* open a new tab page. */
3205-
if (postponed_split || cmdmod.tab != 0)
3205+
if (postponed_split && (swb_flags & (SWB_USEOPEN | SWB_USETAB)))
3206+
{
3207+
buf_T *existing_buf = buflist_findname_exp(fname);
3208+
3209+
if (existing_buf != NULL)
3210+
{
3211+
win_T *wp = NULL;
3212+
3213+
if (swb_flags & SWB_USEOPEN)
3214+
wp = buf_jump_open_win(existing_buf);
3215+
3216+
/* If 'switchbuf' contains "usetab": jump to first window in any tab
3217+
* page containing "existing_buf" if one exists */
3218+
if (wp == NULL && (swb_flags & SWB_USETAB))
3219+
wp = buf_jump_open_tab(existing_buf);
3220+
/* We've switched to the buffer, the usual loading of the file must
3221+
* be skipped. */
3222+
if (wp != NULL)
3223+
getfile_result = GETFILE_SAME_FILE;
3224+
}
3225+
}
3226+
if (getfile_result == GETFILE_UNUSED
3227+
&& (postponed_split || cmdmod.tab != 0))
32063228
{
32073229
if (win_split(postponed_split > 0 ? postponed_split : 0,
32083230
postponed_split_flags) == FAIL)
@@ -3225,10 +3247,11 @@ jumpto_tag(
32253247
#endif
32263248
keep_help_flag = curbuf->b_help;
32273249
}
3228-
getfile_result = getfile(0, fname, NULL, TRUE, (linenr_T)0, forceit);
3250+
if (getfile_result == GETFILE_UNUSED)
3251+
getfile_result = getfile(0, fname, NULL, TRUE, (linenr_T)0, forceit);
32293252
keep_help_flag = FALSE;
32303253

3231-
if (getfile_result <= 0) /* got to the right file */
3254+
if (GETFILE_SUCCESS(getfile_result)) /* got to the right file */
32323255
{
32333256
curwin->w_set_curswant = TRUE;
32343257
#ifdef FEAT_WINDOWS
@@ -3377,7 +3400,7 @@ jumpto_tag(
33773400
#endif
33783401

33793402
/* Return OK if jumped to another file (at least we found the file!). */
3380-
if (getfile_result == -1)
3403+
if (getfile_result == GETFILE_OPEN_OTHER)
33813404
retval = OK;
33823405

33833406
if (retval == OK)

src/testdir/test_tagjump.vim

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,48 @@ func Test_duplicate_tagjump()
6565
call delete('Xfile1')
6666
endfunc
6767

68+
func Test_tagjump_switchbuf()
69+
set tags=Xtags
70+
call writefile(["!_TAG_FILE_ENCODING\tutf-8\t//",
71+
\ "second\tXfile1\t2",
72+
\ "third\tXfile1\t3",],
73+
\ 'Xtags')
74+
call writefile(['first', 'second', 'third'], 'Xfile1')
75+
76+
enew | only
77+
set switchbuf=
78+
stag second
79+
call assert_equal(2, winnr('$'))
80+
call assert_equal(2, line('.'))
81+
stag third
82+
call assert_equal(3, winnr('$'))
83+
call assert_equal(3, line('.'))
84+
85+
enew | only
86+
set switchbuf=useopen
87+
stag second
88+
call assert_equal(2, winnr('$'))
89+
call assert_equal(2, line('.'))
90+
stag third
91+
call assert_equal(2, winnr('$'))
92+
call assert_equal(3, line('.'))
93+
94+
enew | only
95+
set switchbuf=usetab
96+
tab stag second
97+
call assert_equal(2, tabpagenr('$'))
98+
call assert_equal(2, line('.'))
99+
1tabnext | stag third
100+
call assert_equal(2, tabpagenr('$'))
101+
call assert_equal(3, line('.'))
102+
103+
tabclose!
104+
enew | only
105+
call delete('Xfile1')
106+
call delete('Xtags')
107+
set switchbuf&vim
108+
endfunc
109+
68110
" Tests for [ CTRL-I and CTRL-W CTRL-I commands
69111
function Test_keyword_jump()
70112
call writefile(["#include Xinclude", "",

src/version.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -764,6 +764,8 @@ static char *(features[]) =
764764

765765
static int included_patches[] =
766766
{ /* Add new patch number below this line */
767+
/**/
768+
621,
767769
/**/
768770
620,
769771
/**/

src/vim.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,14 @@ extern int (*dyn_libintl_putenv)(const char *envstring);
958958
#define GETF_ALT 0x02 /* jumping to alternate file (not buf num) */
959959
#define GETF_SWITCH 0x04 /* respect 'switchbuf' settings when jumping */
960960

961+
/* Return values of getfile() */
962+
#define GETFILE_ERROR 1 /* normal error */
963+
#define GETFILE_NOT_WRITTEN 2 /* "not written" error */
964+
#define GETFILE_SAME_FILE 0 /* success, same file */
965+
#define GETFILE_OPEN_OTHER -1 /* success, opened another file */
966+
#define GETFILE_UNUSED 8
967+
#define GETFILE_SUCCESS(x) ((x) <= 0)
968+
961969
/* Values for buflist_new() flags */
962970
#define BLN_CURBUF 1 /* may re-use curbuf for new buffer */
963971
#define BLN_LISTED 2 /* put new buffer in buffer list */

0 commit comments

Comments
 (0)