Skip to content

Commit 6d81974

Browse files
committed
patch 8.0.0877: using CTRL-\ CTRL-N in terminal is inconsistent
Problem: Using CTRL-\ CTRL-N in terminal is inconsistent. Solution: Stay in Normal mode.
1 parent 39d21e3 commit 6d81974

File tree

6 files changed

+55
-63
lines changed

6 files changed

+55
-63
lines changed

src/main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,13 +1356,13 @@ main_loop(
13561356
else
13571357
{
13581358
#ifdef FEAT_TERMINAL
1359-
if (term_use_loop(TRUE)
1359+
if (term_use_loop()
13601360
&& oa.op_type == OP_NOP && oa.regname == NUL
13611361
&& !VIsual_active)
13621362
{
13631363
/* If terminal_loop() returns OK we got a key that is handled
1364-
* in Normal model. With FAIL the terminal was closed and the
1365-
* screen needs to be redrawn. */
1364+
* in Normal model. With FAIL we first need to position the
1365+
* cursor and the screen needs to be redrawn. */
13661366
if (terminal_loop() == OK)
13671367
normal_cmd(&oa, TRUE);
13681368
}

src/normal.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4639,7 +4639,7 @@ nv_mousescroll(cmdarg_T *cap)
46394639
if (cap->arg == MSCR_UP || cap->arg == MSCR_DOWN)
46404640
{
46414641
# ifdef FEAT_TERMINAL
4642-
if (term_use_loop(FALSE))
4642+
if (term_use_loop())
46434643
send_keys_to_term(curbuf->b_term, cap->cmdchar, TRUE);
46444644
else
46454645
# endif
@@ -9061,10 +9061,10 @@ nv_edit(cmdarg_T *cap)
90619061
#endif
90629062
}
90639063
#ifdef FEAT_TERMINAL
9064-
else if (term_in_terminal_mode())
9064+
else if (term_in_normal_mode())
90659065
{
90669066
clearop(cap->oap);
9067-
term_leave_terminal_mode();
9067+
term_enter_job_mode();
90689068
return;
90699069
}
90709070
#endif

src/option.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8228,7 +8228,7 @@ set_bool_option(
82288228
{
82298229
# ifdef FEAT_TERMINAL
82308230
/* Cannot set 'modifiable' when in Terminal mode. */
8231-
if (term_in_terminal_mode()
8231+
if (term_in_normal_mode()
82328232
|| (bt_terminal(curbuf) && !term_is_finished(curbuf)))
82338233
{
82348234
curbuf->b_p_ma = FALSE;

src/proto/terminal.pro

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ void ex_terminal(exarg_T *eap);
33
void free_terminal(buf_T *buf);
44
void write_to_term(buf_T *buffer, char_u *msg, channel_T *channel);
55
int term_job_running(term_T *term);
6-
int term_in_terminal_mode(void);
7-
void term_leave_terminal_mode(void);
6+
int term_in_normal_mode(void);
7+
void term_enter_job_mode(void);
88
int send_keys_to_term(term_T *term, int c, int typed);
9-
int term_use_loop(int once);
9+
int term_use_loop(void);
1010
int terminal_loop(void);
1111
void term_job_ended(job_T *job);
1212
void term_channel_closed(channel_T *ch);

src/terminal.c

Lines changed: 43 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,7 @@
3636
* that buffer, attributes come from the scrollback buffer tl_scrollback.
3737
*
3838
* TODO:
39-
* - MS-Windows: no redraw for 'updatetime' #1915
40-
* - add argument to term_wait() for waiting time.
39+
* - Add argument to term_wait() for waiting time.
4140
* - For the scrollback buffer store lines in the buffer, only attributes in
4241
* tl_scrollback.
4342
* - When the job ends:
@@ -47,6 +46,7 @@
4746
* - add option values to the command:
4847
* :term <24x80> <close> vim notes.txt
4948
* - support different cursor shapes, colors and attributes
49+
* - MS-Windows: no redraw for 'updatetime' #1915
5050
* - make term_getcursor() return type (none/block/bar/underline) and
5151
* attributes (color, blink, etc.)
5252
* - To set BS correctly, check get_stty(); Pass the fd of the pty.
@@ -115,7 +115,7 @@ struct terminal_S {
115115
int tl_tty_fd;
116116
char_u *tl_tty_name;
117117

118-
int tl_terminal_mode; /* 0, TMODE_ONCE or TMODE_LOOP */
118+
int tl_normal_mode; /* TRUE: Terminal-Normal mode */
119119
int tl_channel_closed;
120120

121121
#ifdef WIN3264
@@ -462,7 +462,7 @@ term_write_job_output(term_T *term, char_u *msg, size_t len)
462462
static void
463463
update_cursor(term_T *term, int redraw)
464464
{
465-
if (term->tl_terminal_mode != 0)
465+
if (term->tl_normal_mode)
466466
return;
467467
setcursor();
468468
if (redraw && term->tl_buffer == curbuf)
@@ -495,7 +495,7 @@ write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
495495
ch_log(channel, "writing %d bytes to terminal", (int)len);
496496
term_write_job_output(term, msg, len);
497497

498-
if (term->tl_terminal_mode == 0)
498+
if (!term->tl_normal_mode)
499499
{
500500
/* TODO: only update once in a while. */
501501
update_screen(0);
@@ -808,9 +808,9 @@ move_terminal_to_buffer(term_T *term)
808808
}
809809

810810
static void
811-
set_terminal_mode(term_T *term, int mode)
811+
set_terminal_mode(term_T *term, int normal_mode)
812812
{
813-
term->tl_terminal_mode = mode;
813+
term->tl_normal_mode = normal_mode;
814814
vim_free(term->tl_status_text);
815815
term->tl_status_text = NULL;
816816
if (term->tl_buffer == curbuf)
@@ -826,55 +826,52 @@ cleanup_vterm(term_T *term)
826826
{
827827
move_terminal_to_buffer(term);
828828
term_free_vterm(term);
829-
set_terminal_mode(term, 0);
829+
set_terminal_mode(term, FALSE);
830830
}
831831

832832
/*
833833
* Switch from Terminal-Job mode to Terminal-Normal mode.
834834
* Suspends updating the terminal window.
835835
*/
836836
static void
837-
term_enter_terminal_mode(int mode)
837+
term_enter_normal_mode(void)
838838
{
839839
term_T *term = curbuf->b_term;
840840

841841
/* Append the current terminal contents to the buffer. */
842842
move_terminal_to_buffer(term);
843843

844-
set_terminal_mode(term, mode);
844+
set_terminal_mode(term, TRUE);
845845

846-
if (mode == TMODE_ONCE)
847-
{
848-
/* Move the window cursor to the position of the cursor in the
849-
* terminal. */
850-
curwin->w_cursor.lnum = term->tl_scrollback_scrolled
851-
+ term->tl_cursor_pos.row + 1;
852-
check_cursor();
853-
coladvance(term->tl_cursor_pos.col);
846+
/* Move the window cursor to the position of the cursor in the
847+
* terminal. */
848+
curwin->w_cursor.lnum = term->tl_scrollback_scrolled
849+
+ term->tl_cursor_pos.row + 1;
850+
check_cursor();
851+
coladvance(term->tl_cursor_pos.col);
854852

855-
/* Display the same lines as in the terminal. */
856-
curwin->w_topline = term->tl_scrollback_scrolled + 1;
857-
}
853+
/* Display the same lines as in the terminal. */
854+
curwin->w_topline = term->tl_scrollback_scrolled + 1;
858855
}
859856

860857
/*
861858
* Returns TRUE if the current window contains a terminal and we are in
862859
* Terminal-Normal mode.
863860
*/
864861
int
865-
term_in_terminal_mode()
862+
term_in_normal_mode(void)
866863
{
867864
term_T *term = curbuf->b_term;
868865

869-
return term != NULL && term->tl_terminal_mode != 0;
866+
return term != NULL && term->tl_normal_mode;
870867
}
871868

872869
/*
873870
* Switch from Terminal-Normal mode to Terminal-Job mode.
874871
* Restores updating the terminal window.
875872
*/
876873
void
877-
term_leave_terminal_mode()
874+
term_enter_job_mode()
878875
{
879876
term_T *term = curbuf->b_term;
880877
sb_line_T *line;
@@ -893,7 +890,7 @@ term_leave_terminal_mode()
893890
}
894891
check_cursor();
895892

896-
set_terminal_mode(term, 0);
893+
set_terminal_mode(term, FALSE);
897894

898895
if (term->tl_channel_closed)
899896
cleanup_vterm(term);
@@ -1053,13 +1050,12 @@ term_paste_register(int prev_c UNUSED)
10531050
* keys to the job.
10541051
*/
10551052
int
1056-
term_use_loop(int once)
1053+
term_use_loop(void)
10571054
{
10581055
term_T *term = curbuf->b_term;
10591056

10601057
return term != NULL
1061-
&& (once ? term->tl_terminal_mode != TMODE_LOOP
1062-
: term->tl_terminal_mode == 0)
1058+
&& !term->tl_normal_mode
10631059
&& term->tl_vterm != NULL
10641060
&& term_job_running(term);
10651061
}
@@ -1077,13 +1073,6 @@ terminal_loop(void)
10771073
int c;
10781074
int termkey = 0;
10791075

1080-
if (curbuf->b_term->tl_terminal_mode != 0)
1081-
{
1082-
/* Got back from TMODE_ONCE, enter Terminal-Job mode. */
1083-
term_leave_terminal_mode();
1084-
update_cursor(curbuf->b_term, TRUE);
1085-
}
1086-
10871076
if (*curwin->w_p_tk != NUL)
10881077
termkey = string_to_key(curwin->w_p_tk, TRUE);
10891078
position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
@@ -1097,7 +1086,7 @@ terminal_loop(void)
10971086
update_cursor(curbuf->b_term, FALSE);
10981087

10991088
c = term_vgetc();
1100-
if (!term_use_loop(FALSE))
1089+
if (!term_use_loop())
11011090
/* job finished while waiting for a character */
11021091
break;
11031092

@@ -1124,17 +1113,17 @@ terminal_loop(void)
11241113
#ifdef FEAT_CMDL_INFO
11251114
clear_showcmd();
11261115
#endif
1127-
if (!term_use_loop(FALSE))
1116+
if (!term_use_loop())
11281117
/* job finished while waiting for a character */
11291118
break;
11301119

11311120
if (prev_c == Ctrl_BSL)
11321121
{
11331122
if (c == Ctrl_N)
11341123
{
1135-
/* CTRL-\ CTRL-N : execute one Normal mode command. */
1136-
term_enter_terminal_mode(TMODE_ONCE);
1137-
return OK;
1124+
/* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1125+
term_enter_normal_mode();
1126+
return FAIL;
11381127
}
11391128
/* Send both keys to the terminal. */
11401129
send_keys_to_term(curbuf->b_term, prev_c, TRUE);
@@ -1146,7 +1135,8 @@ terminal_loop(void)
11461135
}
11471136
else if (c == 'N')
11481137
{
1149-
term_enter_terminal_mode(TMODE_LOOP);
1138+
/* CTRL-W N : go to Terminal-Normal mode. */
1139+
term_enter_normal_mode();
11501140
return FAIL;
11511141
}
11521142
else if (c == '"')
@@ -1249,7 +1239,7 @@ handle_movecursor(
12491239
if (wp->w_buffer == term->tl_buffer)
12501240
position_cursor(wp, &pos);
12511241
}
1252-
if (term->tl_buffer == curbuf && term->tl_terminal_mode == 0)
1242+
if (term->tl_buffer == curbuf && !term->tl_normal_mode)
12531243
{
12541244
may_toggle_cursor(term);
12551245
update_cursor(term, term->tl_cursor_visible);
@@ -1385,7 +1375,7 @@ term_channel_closed(channel_T *ch)
13851375
term->tl_status_text = NULL;
13861376

13871377
/* Unless in Terminal-Normal mode: clear the vterm. */
1388-
if (term->tl_terminal_mode == 0)
1378+
if (!term->tl_normal_mode)
13891379
cleanup_vterm(term);
13901380

13911381
redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
@@ -1573,8 +1563,9 @@ cell2attr(VTermScreenCell *cell)
15731563
}
15741564

15751565
/*
1576-
* Called to update the window that contains a terminal.
1577-
* Returns FAIL when there is no terminal running in this window.
1566+
* Called to update a window that contains an active terminal.
1567+
* Returns FAIL when there is no terminal running in this window or in
1568+
* Terminal-Normal mode.
15781569
*/
15791570
int
15801571
term_update_window(win_T *wp)
@@ -1585,7 +1576,7 @@ term_update_window(win_T *wp)
15851576
VTermState *state;
15861577
VTermPos pos;
15871578

1588-
if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode != 0)
1579+
if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
15891580
return FAIL;
15901581

15911582
vterm = term->tl_vterm;
@@ -1707,15 +1698,14 @@ term_is_finished(buf_T *buf)
17071698

17081699
/*
17091700
* Return TRUE if "wp" is a terminal window where the job has finished or we
1710-
* are in Terminal-Normal mode.
1701+
* are in Terminal-Normal mode, thus we show the buffer contents.
17111702
*/
17121703
int
17131704
term_show_buffer(buf_T *buf)
17141705
{
17151706
term_T *term = buf->b_term;
17161707

1717-
return term != NULL
1718-
&& (term->tl_vterm == NULL || term->tl_terminal_mode != 0);
1708+
return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
17191709
}
17201710

17211711
/*
@@ -1798,7 +1788,7 @@ term_get_status_text(term_T *term)
17981788
char_u *txt;
17991789
size_t len;
18001790

1801-
if (term->tl_terminal_mode != 0)
1791+
if (term->tl_normal_mode)
18021792
{
18031793
if (term_job_running(term))
18041794
txt = (char_u *)_("Terminal");
@@ -2025,8 +2015,8 @@ f_term_getstatus(typval_T *argvars, typval_T *rettv)
20252015
STRCPY(val, "running");
20262016
else
20272017
STRCPY(val, "finished");
2028-
if (term->tl_terminal_mode != 0)
2029-
STRCAT(val, ",terminal");
2018+
if (term->tl_normal_mode)
2019+
STRCAT(val, ",normal");
20302020
rettv->vval.v_string = vim_strsave(val);
20312021
}
20322022

@@ -2187,7 +2177,7 @@ f_term_sendkeys(typval_T *argvars, typval_T *rettv)
21872177
msg += MB_PTR2LEN(msg);
21882178
}
21892179

2190-
if (term->tl_terminal_mode == 0)
2180+
if (!term->tl_normal_mode)
21912181
{
21922182
/* TODO: only update once in a while. */
21932183
update_screen(0);

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+
877,
772774
/**/
773775
876,
774776
/**/

0 commit comments

Comments
 (0)