1
0
forked from aniani/vim

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.
This commit is contained in:
Bram Moolenaar
2017-08-11 22:27:50 +02:00
parent 8ed5400739
commit da43b61ddd
7 changed files with 86 additions and 24 deletions

View File

@@ -8077,6 +8077,9 @@ term_start({cmd}, {options}) *term_start()*
"term_cols" horizontal size to use for the terminal, "term_cols" horizontal size to use for the terminal,
instead of using 'termsize' instead of using 'termsize'
"vertical" split the window vertically "vertical" split the window vertically
"curwin" use the current window, do not split the
window; fails if the current buffer
cannot be |abandon|ed
"term_finish" What to do when the job is finished: "term_finish" What to do when the job is finished:
"close": close any windows "close": close any windows
"open": open window if needed "open": open window if needed

View File

@@ -4455,6 +4455,13 @@ get_job_options(typval_T *tv, jobopt_T *opt, int supported, int supported2)
opt->jo_set |= JO2_VERTICAL; opt->jo_set |= JO2_VERTICAL;
opt->jo_vertical = get_tv_number(item); opt->jo_vertical = get_tv_number(item);
} }
else if (STRCMP(hi->hi_key, "curwin") == 0)
{
if (!(supported2 & JO2_CURWIN))
break;
opt->jo_set |= JO2_CURWIN;
opt->jo_curwin = get_tv_number(item);
}
#endif #endif
else if (STRCMP(hi->hi_key, "env") == 0) else if (STRCMP(hi->hi_key, "env") == 0)
{ {

View File

@@ -1484,7 +1484,7 @@ EX(CMD_tearoff, "tearoff", ex_tearoff,
NEEDARG|EXTRA|TRLBAR|NOTRLCOM|CMDWIN, NEEDARG|EXTRA|TRLBAR|NOTRLCOM|CMDWIN,
ADDR_LINES), ADDR_LINES),
EX(CMD_terminal, "terminal", ex_terminal, EX(CMD_terminal, "terminal", ex_terminal,
RANGE|NOTADR|FILES|TRLBAR|CMDWIN, RANGE|NOTADR|BANG|FILES|TRLBAR|CMDWIN,
ADDR_OTHER), ADDR_OTHER),
EX(CMD_tfirst, "tfirst", ex_tag, EX(CMD_tfirst, "tfirst", ex_tag,
RANGE|NOTADR|BANG|TRLBAR|ZEROR, RANGE|NOTADR|BANG|TRLBAR|ZEROR,

View File

@@ -1691,7 +1691,8 @@ struct channel_S {
#define JO2_TERM_ROWS 0x0040 /* "term_rows" */ #define JO2_TERM_ROWS 0x0040 /* "term_rows" */
#define JO2_TERM_COLS 0x0080 /* "term_cols" */ #define JO2_TERM_COLS 0x0080 /* "term_cols" */
#define JO2_VERTICAL 0x0100 /* "vertical" */ #define JO2_VERTICAL 0x0100 /* "vertical" */
#define JO2_ALL 0x01FF #define JO2_CURWIN 0x0200 /* "curwin" */
#define JO2_ALL 0x03FF
#define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE) #define JO_MODE_ALL (JO_MODE + JO_IN_MODE + JO_OUT_MODE + JO_ERR_MODE)
#define JO_CB_ALL \ #define JO_CB_ALL \
@@ -1752,6 +1753,7 @@ typedef struct
int jo_term_rows; int jo_term_rows;
int jo_term_cols; int jo_term_cols;
int jo_vertical; int jo_vertical;
int jo_curwin;
char_u *jo_term_name; char_u *jo_term_name;
int jo_term_finish; int jo_term_finish;
#endif #endif

View File

@@ -244,7 +244,7 @@ setup_job_options(jobopt_T *opt, int rows, int cols)
} }
static void static void
term_start(char_u *cmd, jobopt_T *opt) term_start(char_u *cmd, jobopt_T *opt, int forceit)
{ {
exarg_T split_ea; exarg_T split_ea;
win_T *old_curwin = curwin; win_T *old_curwin = curwin;
@@ -261,8 +261,22 @@ term_start(char_u *cmd, jobopt_T *opt)
term->tl_finish = opt->jo_term_finish; term->tl_finish = opt->jo_term_finish;
ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300); ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
/* Open a new window or tab. */
vim_memset(&split_ea, 0, sizeof(split_ea)); vim_memset(&split_ea, 0, sizeof(split_ea));
if (opt->jo_curwin)
{
/* Create a new buffer in the current window. */
if (!can_abandon(curbuf, forceit))
{
EMSG(_(e_nowrtmsg));
return;
}
if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
return;
}
else
{
/* Open a new window or tab. */
split_ea.cmdidx = CMD_new; split_ea.cmdidx = CMD_new;
split_ea.cmd = (char_u *)"new"; split_ea.cmd = (char_u *)"new";
split_ea.arg = (char_u *)""; split_ea.arg = (char_u *)"";
@@ -284,6 +298,7 @@ term_start(char_u *cmd, jobopt_T *opt)
vim_free(term); vim_free(term);
return; return;
} }
}
term->tl_buffer = curbuf; term->tl_buffer = curbuf;
curbuf->b_term = term; curbuf->b_term = term;
@@ -378,6 +393,8 @@ ex_terminal(exarg_T *eap)
opt.jo_term_finish = 'c'; opt.jo_term_finish = 'c';
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0) else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
opt.jo_term_finish = 'o'; opt.jo_term_finish = 'o';
else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
opt.jo_curwin = 1;
else else
{ {
if (*p) if (*p)
@@ -400,9 +417,8 @@ ex_terminal(exarg_T *eap)
else else
opt.jo_term_rows = eap->line2; opt.jo_term_rows = eap->line2;
} }
/* TODO: get more options from before the command */
term_start(cmd, &opt); term_start(cmd, &opt, eap->forceit);
} }
/* /*
@@ -2365,13 +2381,13 @@ f_term_start(typval_T *argvars, typval_T *rettv)
JO_TIMEOUT_ALL + JO_STOPONEXIT JO_TIMEOUT_ALL + JO_STOPONEXIT
+ JO_EXIT_CB + JO_CLOSE_CALLBACK, + JO_EXIT_CB + JO_CLOSE_CALLBACK,
JO2_TERM_NAME + JO2_TERM_FINISH JO2_TERM_NAME + JO2_TERM_FINISH
+ JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
+ JO2_CWD + JO2_ENV) == FAIL) + JO2_CWD + JO2_ENV) == FAIL)
return; return;
if (opt.jo_vertical) if (opt.jo_vertical)
cmdmod.split = WSP_VERT; cmdmod.split = WSP_VERT;
term_start(cmd, &opt); term_start(cmd, &opt, FALSE);
if (curbuf->b_term != NULL) if (curbuf->b_term != NULL)
rettv->vval.v_number = curbuf->b_fnum; rettv->vval.v_number = curbuf->b_fnum;

View File

@@ -283,6 +283,38 @@ func Test_terminal_size()
let size = term_getsize('') let size = term_getsize('')
bwipe! bwipe!
call assert_equal([7, 27], size) call assert_equal([7, 27], size)
endfunc
func Test_terminal_curwin()
let cmd = Get_cat_123_cmd()
call assert_equal(1, winnr('$'))
split dummy
exe 'terminal ++curwin ' . cmd
call assert_equal(2, winnr('$'))
bwipe!
split dummy
call term_start(cmd, {'curwin': 1})
call assert_equal(2, winnr('$'))
bwipe!
split dummy
call setline(1, 'change')
call assert_fails('terminal ++curwin ' . cmd, 'E37:')
call assert_equal(2, winnr('$'))
exe 'terminal! ++curwin ' . cmd
call assert_equal(2, winnr('$'))
bwipe!
split dummy
call setline(1, 'change')
call assert_fails("call term_start(cmd, {'curwin': 1})", 'E37:')
call assert_equal(2, winnr('$'))
bwipe!
split dummy
bwipe!
endfunc endfunc

View File

@@ -769,6 +769,8 @@ static char *(features[]) =
static int included_patches[] = static int included_patches[] =
{ /* Add new patch number below this line */ { /* Add new patch number below this line */
/**/
910,
/**/ /**/
909, 909,
/**/ /**/