Skip to content

Commit da43b61

Browse files
committed
patch 8.0.0910: cannot create a terminal in the current window
Problem: Cannot create a terminal in the current window. Solution: Add option "curwin" and ++curwin.
1 parent 8ed5400 commit da43b61

7 files changed

Lines changed: 86 additions & 24 deletions

File tree

runtime/doc/eval.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8077,6 +8077,9 @@ term_start({cmd}, {options}) *term_start()*
80778077
"term_cols" horizontal size to use for the terminal,
80788078
instead of using 'termsize'
80798079
"vertical" split the window vertically
8080+
"curwin" use the current window, do not split the
8081+
window; fails if the current buffer
8082+
cannot be |abandon|ed
80808083
"term_finish" What to do when the job is finished:
80818084
"close": close any windows
80828085
"open": open window if needed

src/channel.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4455,6 +4455,13 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
44554455
opt->jo_set |= JO2_VERTICAL;
44564456
opt->jo_vertical = get_tv_number(item);
44574457
}
4458+
else if (STRCMP(hi->hi_key, "curwin") == 0)
4459+
{
4460+
if (!(supported2 & JO2_CURWIN))
4461+
break;
4462+
opt->jo_set |= JO2_CURWIN;
4463+
opt->jo_curwin = get_tv_number(item);
4464+
}
44584465
#endif
44594466
else if (STRCMP(hi->hi_key, "env") == 0)
44604467
{

src/ex_cmds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1484,7 +1484,7 @@ EX(CMD_tearoff, "tearoff", ex_tearoff,
14841484
NEEDARG|EXTRA|TRLBAR|NOTRLCOM|CMDWIN,
14851485
ADDR_LINES),
14861486
EX(CMD_terminal, "terminal", ex_terminal,
1487-
RANGE|NOTADR|FILES|TRLBAR|CMDWIN,
1487+
RANGE|NOTADR|BANG|FILES|TRLBAR|CMDWIN,
14881488
ADDR_OTHER),
14891489
EX(CMD_tfirst, "tfirst", ex_tag,
14901490
RANGE|NOTADR|BANG|TRLBAR|ZEROR,

src/structs.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1691,7 +1691,8 @@ struct channel_S {
16911691
#define JO2_TERM_ROWS 0x0040 /* "term_rows" */
16921692
#define JO2_TERM_COLS 0x0080 /* "term_cols" */
16931693
#define JO2_VERTICAL 0x0100 /* "vertical" */
1694-
#define JO2_ALL 0x01FF
1694+
#define JO2_CURWIN 0x0200 /* "curwin" */
1695+
#define JO2_ALL 0x03FF
16951696

16961697
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
16971698
#define JO_CB_ALL \
@@ -1752,6 +1753,7 @@ typedef struct
17521753
int jo_term_rows;
17531754
int jo_term_cols;
17541755
int jo_vertical;
1756+
int jo_curwin;
17551757
char_u *jo_term_name;
17561758
int jo_term_finish;
17571759
#endif

src/terminal.c

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ setup_job_options(jobopt_T *opt, int rows, int cols)
244244
}
245245

246246
static void
247-
term_start(char_u *cmd, jobopt_T *opt)
247+
term_start(char_u *cmd, jobopt_T *opt, int forceit)
248248
{
249249
exarg_T split_ea;
250250
win_T *old_curwin = curwin;
@@ -261,28 +261,43 @@ term_start(char_u *cmd, jobopt_T *opt)
261261
term->tl_finish = opt->jo_term_finish;
262262
ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
263263

264-
/* Open a new window or tab. */
265264
vim_memset(&split_ea, 0, sizeof(split_ea));
266-
split_ea.cmdidx = CMD_new;
267-
split_ea.cmd = (char_u *)"new";
268-
split_ea.arg = (char_u *)"";
269-
if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
265+
if (opt->jo_curwin)
270266
{
271-
split_ea.line2 = opt->jo_term_rows;
272-
split_ea.addr_count = 1;
267+
/* Create a new buffer in the current window. */
268+
if (!can_abandon(curbuf, forceit))
269+
{
270+
EMSG(_(e_nowrtmsg));
271+
return;
272+
}
273+
if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
274+
ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
275+
return;
273276
}
274-
if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
277+
else
275278
{
276-
split_ea.line2 = opt->jo_term_cols;
277-
split_ea.addr_count = 1;
278-
}
279+
/* Open a new window or tab. */
280+
split_ea.cmdidx = CMD_new;
281+
split_ea.cmd = (char_u *)"new";
282+
split_ea.arg = (char_u *)"";
283+
if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
284+
{
285+
split_ea.line2 = opt->jo_term_rows;
286+
split_ea.addr_count = 1;
287+
}
288+
if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
289+
{
290+
split_ea.line2 = opt->jo_term_cols;
291+
split_ea.addr_count = 1;
292+
}
279293

280-
ex_splitview(&split_ea);
281-
if (curwin == old_curwin)
282-
{
283-
/* split failed */
284-
vim_free(term);
285-
return;
294+
ex_splitview(&split_ea);
295+
if (curwin == old_curwin)
296+
{
297+
/* split failed */
298+
vim_free(term);
299+
return;
300+
}
286301
}
287302
term->tl_buffer = curbuf;
288303
curbuf->b_term = term;
@@ -378,6 +393,8 @@ ex_terminal(exarg_T *eap)
378393
opt.jo_term_finish = 'c';
379394
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
380395
opt.jo_term_finish = 'o';
396+
else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
397+
opt.jo_curwin = 1;
381398
else
382399
{
383400
if (*p)
@@ -400,9 +417,8 @@ ex_terminal(exarg_T *eap)
400417
else
401418
opt.jo_term_rows = eap->line2;
402419
}
403-
/* TODO: get more options from before the command */
404420

405-
term_start(cmd, &opt);
421+
term_start(cmd, &opt, eap->forceit);
406422
}
407423

408424
/*
@@ -2365,13 +2381,13 @@ f_term_start(typval_T *argvars, typval_T *rettv)
23652381
JO_TIMEOUT_ALL + JO_STOPONEXIT
23662382
+ JO_EXIT_CB + JO_CLOSE_CALLBACK,
23672383
JO2_TERM_NAME + JO2_TERM_FINISH
2368-
+ JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL
2384+
+ JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
23692385
+ JO2_CWD + JO2_ENV) == FAIL)
23702386
return;
23712387

23722388
if (opt.jo_vertical)
23732389
cmdmod.split = WSP_VERT;
2374-
term_start(cmd, &opt);
2390+
term_start(cmd, &opt, FALSE);
23752391

23762392
if (curbuf->b_term != NULL)
23772393
rettv->vval.v_number = curbuf->b_fnum;

src/testdir/test_terminal.vim

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,38 @@ func Test_terminal_size()
283283
let size = term_getsize('')
284284
bwipe!
285285
call assert_equal([7, 27], size)
286+
endfunc
287+
288+
func Test_terminal_curwin()
289+
let cmd = Get_cat_123_cmd()
290+
call assert_equal(1, winnr('$'))
291+
292+
split dummy
293+
exe 'terminal ++curwin ' . cmd
294+
call assert_equal(2, winnr('$'))
295+
bwipe!
296+
297+
split dummy
298+
call term_start(cmd, {'curwin': 1})
299+
call assert_equal(2, winnr('$'))
300+
bwipe!
301+
302+
split dummy
303+
call setline(1, 'change')
304+
call assert_fails('terminal ++curwin ' . cmd, 'E37:')
305+
call assert_equal(2, winnr('$'))
306+
exe 'terminal! ++curwin ' . cmd
307+
call assert_equal(2, winnr('$'))
308+
bwipe!
309+
310+
split dummy
311+
call setline(1, 'change')
312+
call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
313+
call assert_equal(2, winnr('$'))
314+
bwipe!
315+
316+
split dummy
317+
bwipe!
286318

287319
endfunc
288320

src/version.c

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

770770
static int included_patches[] =
771771
{ /* Add new patch number below this line */
772+
/**/
773+
910,
772774
/**/
773775
909,
774776
/**/

0 commit comments

Comments
 (0)