Skip to content

Commit 1dd9833

Browse files
committed
patch 8.0.1612: need to close terminal after shell stopped
Problem: Need to close terminal after shell stopped. Solution: Make :terminal without argument close the window by default.
1 parent af23bad commit 1dd9833

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

runtime/doc/terminal.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ Syntax ~
140140
if [command] is NONE no job is started, the pty of the
141141
terminal can be used by a command like gdb.
142142

143+
If [command] is missing the default behavior is to
144+
close the terminal when the shell exits. This can be
145+
changed with the ++noclose argument.
146+
If [command] is present the default behavior is to
147+
keep the terminal open in Terminal-Normal mode. This
148+
can be changed with the ++close argument.
149+
143150
A new buffer will be created, using [command] or
144151
'shell' as the name, prefixed with a "!". If a buffer
145152
by this name already exists a number is added in
@@ -155,9 +162,14 @@ Syntax ~
155162
Supported [options] are:
156163
++close The terminal window will close
157164
automatically when the job terminates.
165+
++noclose The terminal window will NOT close
166+
automatically when the job terminates.
158167
++open When the job terminates and no window
159168
shows it, a window will be opened.
160169
Note that this can be interruptive.
170+
The last of ++close, ++noclose and ++open
171+
matters and rules out earlier arguments.
172+
161173
++curwin Open the terminal in the current
162174
window, do not split the current
163175
window. Fails if the current buffer

src/terminal.c

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@
3838
* in tl_scrollback are no longer used.
3939
*
4040
* TODO:
41-
* - Make terminal close by default when started without a command. Add
42-
* ++noclose argument.
4341
* - Win32: In the GUI use a terminal emulator for :!cmd.
4442
* - Add a way to set the 16 ANSI colors, to be used for 'termguicolors' and in
4543
* the GUI.
@@ -123,7 +121,11 @@ struct terminal_S {
123121

124122
int tl_normal_mode; /* TRUE: Terminal-Normal mode */
125123
int tl_channel_closed;
126-
int tl_finish; /* 'c' for ++close, 'o' for ++open */
124+
int tl_finish;
125+
#define TL_FINISH_UNSET NUL
126+
#define TL_FINISH_CLOSE 'c' /* ++close or :terminal without argument */
127+
#define TL_FINISH_NOCLOSE 'n' /* ++noclose */
128+
#define TL_FINISH_OPEN 'o' /* ++open */
127129
char_u *tl_opencmd;
128130
char_u *tl_eof_chars;
129131

@@ -643,6 +645,8 @@ ex_terminal(exarg_T *eap)
643645

644646
if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
645647
opt.jo_term_finish = 'c';
648+
else if ((int)(p - cmd) == 7 && STRNICMP(cmd, "noclose", 7) == 0)
649+
opt.jo_term_finish = 'n';
646650
else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
647651
opt.jo_term_finish = 'o';
648652
else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
@@ -696,9 +700,15 @@ ex_terminal(exarg_T *eap)
696700
cmd = skipwhite(p);
697701
}
698702
if (*cmd == NUL)
703+
{
699704
/* Make a copy of 'shell', an autocommand may change the option. */
700705
tofree = cmd = vim_strsave(p_sh);
701706

707+
/* default to close when the shell exits */
708+
if (opt.jo_term_finish == NUL)
709+
opt.jo_term_finish = 'c';
710+
}
711+
702712
if (eap->addr_count > 0)
703713
{
704714
/* Write lines from current buffer to the job. */
@@ -1535,7 +1545,7 @@ set_terminal_mode(term_T *term, int normal_mode)
15351545
static void
15361546
cleanup_vterm(term_T *term)
15371547
{
1538-
if (term->tl_finish != 'c')
1548+
if (term->tl_finish != TL_FINISH_CLOSE)
15391549
move_terminal_to_buffer(term);
15401550
term_free_vterm(term);
15411551
set_terminal_mode(term, FALSE);
@@ -2603,7 +2613,7 @@ term_channel_closed(channel_T *ch)
26032613

26042614
cleanup_vterm(term);
26052615

2606-
if (term->tl_finish == 'c')
2616+
if (term->tl_finish == TL_FINISH_CLOSE)
26072617
{
26082618
aco_save_T aco;
26092619

@@ -2614,7 +2624,8 @@ term_channel_closed(channel_T *ch)
26142624
aucmd_restbuf(&aco);
26152625
break;
26162626
}
2617-
if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
2627+
if (term->tl_finish == TL_FINISH_OPEN
2628+
&& term->tl_buffer->b_nwindows == 0)
26182629
{
26192630
char buf[50];
26202631

src/testdir/test_terminal.vim

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,26 @@ func Test_terminal_finish_open_close()
367367

368368
let [cmd, waittime] = s:get_sleep_cmd()
369369

370+
" shell terminal closes automatically
371+
terminal
372+
let buf = bufnr('%')
373+
call assert_equal(2, winnr('$'))
374+
" Wait for the shell to display a prompt
375+
call WaitFor({-> term_getline(buf, 1) != ""})
376+
call Stop_shell_in_terminal(buf)
377+
call WaitFor("winnr('$') == 1", waittime)
378+
379+
" shell terminal that does not close automatically
380+
terminal ++noclose
381+
let buf = bufnr('%')
382+
call assert_equal(2, winnr('$'))
383+
" Wait for the shell to display a prompt
384+
call WaitFor({-> term_getline(buf, 1) != ""})
385+
call Stop_shell_in_terminal(buf)
386+
call assert_equal(2, winnr('$'))
387+
quit
388+
call assert_equal(1, winnr('$'))
389+
370390
exe 'terminal ++close ' . cmd
371391
call assert_equal(2, winnr('$'))
372392
wincmd p

src/version.c

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

767767
static int included_patches[] =
768768
{ /* Add new patch number below this line */
769+
/**/
770+
1612,
769771
/**/
770772
1611,
771773
/**/

0 commit comments

Comments
 (0)