blob: 0267e5f545429b82e81f2763d9f549a7c2b85271 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
Bram Moolenaar679653e2017-08-13 14:13:19 +020037 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020039 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040 * TODO:
Bram Moolenaardd693ce2017-08-10 23:15:19 +020041 * - Make argument list work on MS-Windows. #1954
Bram Moolenaard85f2712017-07-28 21:51:57 +020042 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020043 * For the GUI fill termios with default values, perhaps like pangoterm:
44 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
45 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020046 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020048 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020049 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020050 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
51 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020052 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020054 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020055 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020056 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020057 * terminal. Allow:
58 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
59 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
60 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
61 * Check that something is connected to the terminal.
62 * Test: "cat" reading from a file or buffer
63 * "ls" writing stdout to a file or buffer
64 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020065 * - support ":term NONE" to open a terminal with a pty but not running a job
66 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020067 * - if the job in the terminal does not support the mouse, we can use the
68 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
70 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020071 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020072 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020073 * - Copy text in the vterm to the Vim buffer once in a while, so that
74 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075 */
76
77#include "vim.h"
78
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020079#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080
Bram Moolenaard5310982017-08-05 15:16:32 +020081#ifndef MIN
82# define MIN(x,y) ((x) < (y) ? (x) : (y))
83#endif
84#ifndef MAX
85# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020087
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020088#include "libvterm/include/vterm.h"
89
Bram Moolenaar33a43be2017-08-06 21:36:22 +020090/* This is VTermScreenCell without the characters, thus much smaller. */
91typedef struct {
92 VTermScreenCellAttrs attrs;
93 char width;
94 VTermColor fg, bg;
95} cellattr_T;
96
Bram Moolenaard85f2712017-07-28 21:51:57 +020097typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020098 int sb_cols; /* can differ per line */
99 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200100} sb_line_T;
101
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200102/* typedef term_T in structs.h */
103struct terminal_S {
104 term_T *tl_next;
105
Bram Moolenaar423802d2017-07-30 16:52:24 +0200106 VTerm *tl_vterm;
107 job_T *tl_job;
108 buf_T *tl_buffer;
109
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200110 /* used when tl_job is NULL and only a pty was created */
111 int tl_tty_fd;
112 char_u *tl_tty_name;
113
Bram Moolenaar6d819742017-08-06 14:57:49 +0200114 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200115 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200116 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200117 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200118
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200119#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200120 void *tl_winpty_config;
121 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200122#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200123
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200124 /* last known vterm size */
125 int tl_rows;
126 int tl_cols;
127 /* vterm size does not follow window size */
128 int tl_rows_fixed;
129 int tl_cols_fixed;
130
Bram Moolenaar21554412017-07-24 21:44:43 +0200131 char_u *tl_title; /* NULL or allocated */
132 char_u *tl_status_text; /* NULL or allocated */
133
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200134 /* Range of screen rows to update. Zero based. */
135 int tl_dirty_row_start; /* -1 if nothing dirty */
136 int tl_dirty_row_end; /* row below last one to update */
137
Bram Moolenaard85f2712017-07-28 21:51:57 +0200138 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200139 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200141 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200142 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200143 int tl_cursor_blink;
144 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
145 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200146
147 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148};
149
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200150#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
151#define TMODE_LOOP 2 /* CTRL-W N used */
152
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200153/*
154 * List of all active terminals.
155 */
156static term_T *first_term = NULL;
157
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200158/* Terminal active in terminal_loop(). */
159static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200160
161#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
162#define KEY_BUF_LEN 200
163
164/*
165 * Functions with separate implementation for MS-Windows and Unix-like systems.
166 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200167static int term_and_job_init(term_T *term, int rows, int cols,
168 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200240 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
241 opt->jo_term_rows = rows;
242 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
243 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200244}
245
246 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200247term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 exarg_T split_ea;
250 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200252 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253
254 if (check_restricted() || check_secure())
255 return;
256
257 term = (term_T *)alloc_clear(sizeof(term_T));
258 if (term == NULL)
259 return;
260 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200261 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200262 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200263 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200264 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200269 /* Create a new buffer in the current window. */
270 if (!can_abandon(curbuf, forceit))
271 {
272 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200273 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200274 return;
275 }
276 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
277 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 {
279 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200280 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200281 }
282 }
283 else if (opt->jo_hidden)
284 {
285 buf_T *buf;
286
287 /* Create a new buffer without a window. Make it the current buffer for
288 * a moment to be able to do the initialisations. */
289 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
290 BLN_NEW | BLN_LISTED);
291 if (buf == NULL || ml_open(buf) == FAIL)
292 {
293 vim_free(term);
294 return;
295 }
296 old_curbuf = curbuf;
297 --curbuf->b_nwindows;
298 curbuf = buf;
299 curwin->w_buffer = buf;
300 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200304 /* Open a new window or tab. */
305 split_ea.cmdidx = CMD_new;
306 split_ea.cmd = (char_u *)"new";
307 split_ea.arg = (char_u *)"";
308 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
309 {
310 split_ea.line2 = opt->jo_term_rows;
311 split_ea.addr_count = 1;
312 }
313 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
314 {
315 split_ea.line2 = opt->jo_term_cols;
316 split_ea.addr_count = 1;
317 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200318
Bram Moolenaarda43b612017-08-11 22:27:50 +0200319 ex_splitview(&split_ea);
320 if (curwin == old_curwin)
321 {
322 /* split failed */
323 vim_free(term);
324 return;
325 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200327 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200330 if (!opt->jo_hidden)
331 {
332 /* only one size was taken care of with :new, do the other one */
333 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
334 win_setheight(opt->jo_term_rows);
335 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
336 win_setwidth(opt->jo_term_cols);
337 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200338
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340 term->tl_next = first_term;
341 first_term = term;
342
Bram Moolenaar78712a72017-08-05 14:50:12 +0200343 if (opt->jo_term_name != NULL)
344 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
345 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200346 {
347 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200348 size_t len;
349 char_u *cmd, *p;
350
351 if (argvar->v_type == VAR_STRING)
352 cmd = argvar->vval.v_string;
353 else if (argvar->v_type != VAR_LIST
354 || argvar->vval.v_list == NULL
355 || argvar->vval.v_list->lv_len < 1)
356 cmd = (char_u*)"";
357 else
358 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
359
360 len = STRLEN(cmd) + 10;
361 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200363 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200364 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200365 /* Prepend a ! to the command name to avoid the buffer name equals
366 * the executable, otherwise ":w!" would overwrite it. */
367 if (i == 0)
368 vim_snprintf((char *)p, len, "!%s", cmd);
369 else
370 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200371 if (buflist_findname(p) == NULL)
372 {
373 curbuf->b_ffname = p;
374 break;
375 }
376 }
377 }
378 curbuf->b_fname = curbuf->b_ffname;
379
Bram Moolenaar37c45832017-08-12 16:01:04 +0200380 if (opt->jo_term_opencmd != NULL)
381 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
382
Bram Moolenaareb44a682017-08-03 22:44:55 +0200383 set_string_option_direct((char_u *)"buftype", -1,
384 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
385
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200386 /* Mark the buffer as not modifiable. It can only be made modifiable after
387 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200388 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200389
390 /* Set 'bufhidden' to "hide": allow closing the window. */
391 set_string_option_direct((char_u *)"bufhidden", -1,
392 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200393
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200394 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200395 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200396
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200397 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200398 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200400 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200402 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200403
404 if (old_curbuf != NULL)
405 {
406 --curbuf->b_nwindows;
407 curbuf = old_curbuf;
408 curwin->w_buffer = curbuf;
409 ++curbuf->b_nwindows;
410 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200411 }
412 else
413 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200414 buf_T *buf = curbuf;
415
Bram Moolenaard85f2712017-07-28 21:51:57 +0200416 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200417 if (old_curbuf != NULL)
418 {
419 --curbuf->b_nwindows;
420 curbuf = old_curbuf;
421 curwin->w_buffer = curbuf;
422 ++curbuf->b_nwindows;
423 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200424
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200425 /* Wiping out the buffer will also close the window and call
426 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200427 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200428 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200429}
430
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200431/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200432 * ":terminal": open a terminal window and execute a job in it.
433 */
434 void
435ex_terminal(exarg_T *eap)
436{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200437 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200438 jobopt_T opt;
439 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200440
441 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200442
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200443 cmd = eap->arg;
444 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
445 {
446 char_u *p;
447
448 cmd += 2;
449 p = skiptowhite(cmd);
450 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
451 opt.jo_term_finish = 'c';
452 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
453 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200454 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
455 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200456 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
457 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200458 else
459 {
460 if (*p)
461 *p = NUL;
462 EMSG2(_("E181: Invalid attribute: %s"), cmd);
463 return;
464 }
465 cmd = skipwhite(p);
466 }
467
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200468 if (eap->addr_count == 2)
469 {
470 opt.jo_term_rows = eap->line1;
471 opt.jo_term_cols = eap->line2;
472 }
473 else if (eap->addr_count == 1)
474 {
475 if (cmdmod.split & WSP_VERT)
476 opt.jo_term_cols = eap->line2;
477 else
478 opt.jo_term_rows = eap->line2;
479 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200480
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200481 argvar.v_type = VAR_STRING;
482 argvar.vval.v_string = cmd;
483 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200484}
485
486/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200487 * Free the scrollback buffer for "term".
488 */
489 static void
490free_scrollback(term_T *term)
491{
492 int i;
493
494 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
495 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
496 ga_clear(&term->tl_scrollback);
497}
498
499/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200500 * Free a terminal and everything it refers to.
501 * Kills the job if there is one.
502 * Called when wiping out a buffer.
503 */
504 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200505free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200506{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200507 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200508 term_T *tp;
509
510 if (term == NULL)
511 return;
512 if (first_term == term)
513 first_term = term->tl_next;
514 else
515 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
516 if (tp->tl_next == term)
517 {
518 tp->tl_next = term->tl_next;
519 break;
520 }
521
522 if (term->tl_job != NULL)
523 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200524 if (term->tl_job->jv_status != JOB_ENDED
525 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200526 job_stop(term->tl_job, NULL, "kill");
527 job_unref(term->tl_job);
528 }
529
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200530 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200531
532 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200533 vim_free(term->tl_title);
534 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200535 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200536 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200537 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200538 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200539 if (in_terminal_loop == term)
540 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200541}
542
543/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200544 * Write job output "msg[len]" to the vterm.
545 */
546 static void
547term_write_job_output(term_T *term, char_u *msg, size_t len)
548{
549 VTerm *vterm = term->tl_vterm;
550 char_u *p;
551 size_t done;
552 size_t len_now;
553
554 for (done = 0; done < len; done += len_now)
555 {
556 for (p = msg + done; p < msg + len; )
557 {
558 if (*p == NL)
559 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200560 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200561 }
562 len_now = p - msg - done;
563 vterm_input_write(vterm, (char *)msg + done, len_now);
564 if (p < msg + len && *p == NL)
565 {
566 /* Convert NL to CR-NL, that appears to work best. */
567 vterm_input_write(vterm, "\r\n", 2);
568 ++len_now;
569 }
570 }
571
572 /* this invokes the damage callbacks */
573 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
574}
575
Bram Moolenaar1c844932017-07-24 23:36:41 +0200576 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200577update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200578{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200579 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200580 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200581 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200582 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200583 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200584 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200585 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200586 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200587#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200588 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200589 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200590#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200591 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200592}
593
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200594/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200595 * Invoked when "msg" output from a job was received. Write it to the terminal
596 * of "buffer".
597 */
598 void
599write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
600{
601 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200602 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200603
Bram Moolenaard85f2712017-07-28 21:51:57 +0200604 if (term->tl_vterm == NULL)
605 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200606 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200607 return;
608 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200609 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200610 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200611
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200612 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
613 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200614 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200615 {
616 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200617 ch_log(term->tl_job->jv_channel, "updating screen");
618 if (buffer == curbuf)
619 {
620 update_screen(0);
621 update_cursor(term, TRUE);
622 }
623 else
624 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200625 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200626}
627
628/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200629 * Send a mouse position and click to the vterm
630 */
631 static int
632term_send_mouse(VTerm *vterm, int button, int pressed)
633{
634 VTermModifier mod = VTERM_MOD_NONE;
635
636 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
637 mouse_col - W_WINCOL(curwin), mod);
638 vterm_mouse_button(vterm, button, pressed, mod);
639 return TRUE;
640}
641
642/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200643 * Convert typed key "c" into bytes to send to the job.
644 * Return the number of bytes in "buf".
645 */
646 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200647term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200648{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200649 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 VTermKey key = VTERM_KEY_NONE;
651 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200652 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200653
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654 switch (c)
655 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200656 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200657 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200658 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
659 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660 case K_DEL: key = VTERM_KEY_DEL; break;
661 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200662 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
663 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200664 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200665 case K_S_END: mod = VTERM_MOD_SHIFT;
666 key = VTERM_KEY_END; break;
667 case K_C_END: mod = VTERM_MOD_CTRL;
668 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
670 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
671 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
672 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
673 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
674 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
675 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
676 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
677 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
678 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
679 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
680 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
681 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200682 case K_S_HOME: mod = VTERM_MOD_SHIFT;
683 key = VTERM_KEY_HOME; break;
684 case K_C_HOME: mod = VTERM_MOD_CTRL;
685 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200686 case K_INS: key = VTERM_KEY_INS; break;
687 case K_K0: key = VTERM_KEY_KP_0; break;
688 case K_K1: key = VTERM_KEY_KP_1; break;
689 case K_K2: key = VTERM_KEY_KP_2; break;
690 case K_K3: key = VTERM_KEY_KP_3; break;
691 case K_K4: key = VTERM_KEY_KP_4; break;
692 case K_K5: key = VTERM_KEY_KP_5; break;
693 case K_K6: key = VTERM_KEY_KP_6; break;
694 case K_K7: key = VTERM_KEY_KP_7; break;
695 case K_K8: key = VTERM_KEY_KP_8; break;
696 case K_K9: key = VTERM_KEY_KP_9; break;
697 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
698 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
699 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
700 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
701 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
702 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
703 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
704 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
705 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
706 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
707 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
708 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
709 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200710 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
711 key = VTERM_KEY_LEFT; break;
712 case K_C_LEFT: mod = VTERM_MOD_CTRL;
713 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200714 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
715 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
716 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200717 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
718 key = VTERM_KEY_RIGHT; break;
719 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
720 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200721 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200722 case K_S_UP: mod = VTERM_MOD_SHIFT;
723 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200724 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200725
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200726 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
727 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
728 case K_MOUSELEFT: /* TODO */ return 0;
729 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200730
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_LEFTMOUSE:
732 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
733 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
734 case K_LEFTRELEASE:
735 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
736 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
737 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
738 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
739 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
740 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
741 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
742 case K_X1MOUSE: /* TODO */ return 0;
743 case K_X1DRAG: /* TODO */ return 0;
744 case K_X1RELEASE: /* TODO */ return 0;
745 case K_X2MOUSE: /* TODO */ return 0;
746 case K_X2DRAG: /* TODO */ return 0;
747 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200748
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200749 case K_IGNORE: return 0;
750 case K_NOP: return 0;
751 case K_UNDO: return 0;
752 case K_HELP: return 0;
753 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
754 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
755 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
756 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
757 case K_SELECT: return 0;
758#ifdef FEAT_GUI
759 case K_VER_SCROLLBAR: return 0;
760 case K_HOR_SCROLLBAR: return 0;
761#endif
762#ifdef FEAT_GUI_TABLINE
763 case K_TABLINE: return 0;
764 case K_TABMENU: return 0;
765#endif
766#ifdef FEAT_NETBEANS_INTG
767 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
768#endif
769#ifdef FEAT_DND
770 case K_DROP: return 0;
771#endif
772#ifdef FEAT_AUTOCMD
773 case K_CURSORHOLD: return 0;
774#endif
775 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
776 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200777 }
778
779 /*
780 * Convert special keys to vterm keys:
781 * - Write keys to vterm: vterm_keyboard_key()
782 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200783 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200784 */
785 if (key != VTERM_KEY_NONE)
786 /* Special key, let vterm convert it. */
787 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200788 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200789 /* Normal character, let vterm convert it. */
790 vterm_keyboard_unichar(vterm, c, mod);
791
792 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200793 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200794}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200795
Bram Moolenaar938783d2017-07-16 20:13:26 +0200796/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200797 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200798 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200799 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200800term_job_running(term_T *term)
801{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200802 /* Also consider the job finished when the channel is closed, to avoid a
803 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200804 return term != NULL
805 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200806 && term->tl_job->jv_status == JOB_STARTED
807 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200808}
809
810/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200811 * Add the last line of the scrollback buffer to the buffer in the window.
812 */
813 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200814add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200815{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200816 buf_T *buf = term->tl_buffer;
817 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
818 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200819
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200820 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200821 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200822 {
823 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200824 curbuf = buf;
825 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200826 curbuf = curwin->w_buffer;
827 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200828}
829
830/*
831 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200832 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200833 */
834 static void
835move_terminal_to_buffer(term_T *term)
836{
837 win_T *wp;
838 int len;
839 int lines_skipped = 0;
840 VTermPos pos;
841 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200842 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200843 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200844
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200845 if (term->tl_vterm == NULL)
846 return;
847 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200848 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
849 {
850 len = 0;
851 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
852 if (vterm_screen_get_cell(screen, pos, &cell) != 0
853 && cell.chars[0] != NUL)
854 len = pos.col + 1;
855
856 if (len == 0)
857 ++lines_skipped;
858 else
859 {
860 while (lines_skipped > 0)
861 {
862 /* Line was skipped, add an empty line. */
863 --lines_skipped;
864 if (ga_grow(&term->tl_scrollback, 1) == OK)
865 {
866 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
867 + term->tl_scrollback.ga_len;
868
869 line->sb_cols = 0;
870 line->sb_cells = NULL;
871 ++term->tl_scrollback.ga_len;
872
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200873 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200874 }
875 }
876
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200877 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200878 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
879 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200880 garray_T ga;
881 int width;
882 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883 + term->tl_scrollback.ga_len;
884
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200885 ga_init2(&ga, 1, 100);
886 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 {
888 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200889 {
890 width = 1;
891 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
892 if (ga_grow(&ga, 1) == OK)
893 ga.ga_len += mb_char2bytes(' ',
894 (char_u *)ga.ga_data + ga.ga_len);
895 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200897 {
898 width = cell.width;
899
900 p[pos.col].width = cell.width;
901 p[pos.col].attrs = cell.attrs;
902 p[pos.col].fg = cell.fg;
903 p[pos.col].bg = cell.bg;
904
905 if (ga_grow(&ga, MB_MAXBYTES) == OK)
906 {
907 int i;
908 int c;
909
910 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
911 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
912 (char_u *)ga.ga_data + ga.ga_len);
913 }
914 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200915 }
916 line->sb_cols = len;
917 line->sb_cells = p;
918 ++term->tl_scrollback.ga_len;
919
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200920 if (ga_grow(&ga, 1) == FAIL)
921 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
922 else
923 {
924 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
925 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
926 }
927 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 }
929 else
930 vim_free(p);
931 }
932 }
933
934 FOR_ALL_WINDOWS(wp)
935 {
936 if (wp->w_buffer == term->tl_buffer)
937 {
938 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
939 wp->w_cursor.col = 0;
940 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200941 if (wp->w_cursor.lnum >= wp->w_height)
942 {
943 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
944
945 if (wp->w_topline < min_topline)
946 wp->w_topline = min_topline;
947 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200948 redraw_win_later(wp, NOT_VALID);
949 }
950 }
951}
952
953 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200954set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200955{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200956 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200957 vim_free(term->tl_status_text);
958 term->tl_status_text = NULL;
959 if (term->tl_buffer == curbuf)
960 maketitle();
961}
962
963/*
964 * Called after the job if finished and Terminal mode is not active:
965 * Move the vterm contents into the scrollback buffer and free the vterm.
966 */
967 static void
968cleanup_vterm(term_T *term)
969{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200970 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200971 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200972 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200973 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200974}
975
976/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200977 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 * Suspends updating the terminal window.
979 */
980 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200981term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982{
983 term_T *term = curbuf->b_term;
984
985 /* Append the current terminal contents to the buffer. */
986 move_terminal_to_buffer(term);
987
Bram Moolenaar6d819742017-08-06 14:57:49 +0200988 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200989
Bram Moolenaar6d819742017-08-06 14:57:49 +0200990 /* Move the window cursor to the position of the cursor in the
991 * terminal. */
992 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
993 + term->tl_cursor_pos.row + 1;
994 check_cursor();
995 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200996
Bram Moolenaar6d819742017-08-06 14:57:49 +0200997 /* Display the same lines as in the terminal. */
998 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200999}
1000
1001/*
1002 * Returns TRUE if the current window contains a terminal and we are in
1003 * Terminal-Normal mode.
1004 */
1005 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001006term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001007{
1008 term_T *term = curbuf->b_term;
1009
Bram Moolenaar6d819742017-08-06 14:57:49 +02001010 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001011}
1012
1013/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001014 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001015 * Restores updating the terminal window.
1016 */
1017 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001018term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019{
1020 term_T *term = curbuf->b_term;
1021 sb_line_T *line;
1022 garray_T *gap;
1023
1024 /* Remove the terminal contents from the scrollback and the buffer. */
1025 gap = &term->tl_scrollback;
1026 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1027 {
1028 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1029 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1030 vim_free(line->sb_cells);
1031 --gap->ga_len;
1032 if (gap->ga_len == 0)
1033 break;
1034 }
1035 check_cursor();
1036
Bram Moolenaar6d819742017-08-06 14:57:49 +02001037 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001038
1039 if (term->tl_channel_closed)
1040 cleanup_vterm(term);
1041 redraw_buf_and_status_later(curbuf, NOT_VALID);
1042}
1043
1044/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001045 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001046 * Note: while waiting a terminal may be closed and freed if the channel is
1047 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001048 * TODO: use terminal mode mappings.
1049 */
1050 static int
1051term_vgetc()
1052{
1053 int c;
1054
1055 ++no_mapping;
1056 ++allow_keys;
1057 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001058#ifdef WIN3264
1059 ctrl_break_was_pressed = FALSE;
1060#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001061 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001062 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001063 --no_mapping;
1064 --allow_keys;
1065 return c;
1066}
1067
1068/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001069 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001070 * Return FAIL when the key needs to be handled in Normal mode.
1071 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001072 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001073 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001074send_keys_to_term(term_T *term, int c, int typed)
1075{
1076 char msg[KEY_BUF_LEN];
1077 size_t len;
1078 static int mouse_was_outside = FALSE;
1079 int dragging_outside = FALSE;
1080
1081 /* Catch keys that need to be handled as in Normal mode. */
1082 switch (c)
1083 {
1084 case NUL:
1085 case K_ZERO:
1086 if (typed)
1087 stuffcharReadbuff(c);
1088 return FAIL;
1089
1090 case K_IGNORE:
1091 return FAIL;
1092
1093 case K_LEFTDRAG:
1094 case K_MIDDLEDRAG:
1095 case K_RIGHTDRAG:
1096 case K_X1DRAG:
1097 case K_X2DRAG:
1098 dragging_outside = mouse_was_outside;
1099 /* FALLTHROUGH */
1100 case K_LEFTMOUSE:
1101 case K_LEFTMOUSE_NM:
1102 case K_LEFTRELEASE:
1103 case K_LEFTRELEASE_NM:
1104 case K_MIDDLEMOUSE:
1105 case K_MIDDLERELEASE:
1106 case K_RIGHTMOUSE:
1107 case K_RIGHTRELEASE:
1108 case K_X1MOUSE:
1109 case K_X1RELEASE:
1110 case K_X2MOUSE:
1111 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001112
1113 case K_MOUSEUP:
1114 case K_MOUSEDOWN:
1115 case K_MOUSELEFT:
1116 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001117 if (mouse_row < W_WINROW(curwin)
1118 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1119 || mouse_col < W_WINCOL(curwin)
1120 || mouse_col >= W_ENDCOL(curwin)
1121 || dragging_outside)
1122 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001123 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001124 if (typed)
1125 {
1126 stuffcharReadbuff(c);
1127 mouse_was_outside = TRUE;
1128 }
1129 return FAIL;
1130 }
1131 }
1132 if (typed)
1133 mouse_was_outside = FALSE;
1134
1135 /* Convert the typed key to a sequence of bytes for the job. */
1136 len = term_convert_key(term, c, msg);
1137 if (len > 0)
1138 /* TODO: if FAIL is returned, stop? */
1139 channel_send(term->tl_job->jv_channel, PART_IN,
1140 (char_u *)msg, (int)len, NULL);
1141
1142 return OK;
1143}
1144
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001145 static void
1146position_cursor(win_T *wp, VTermPos *pos)
1147{
1148 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1149 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1150 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1151}
1152
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001153/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001154 * Handle CTRL-W "": send register contents to the job.
1155 */
1156 static void
1157term_paste_register(int prev_c UNUSED)
1158{
1159 int c;
1160 list_T *l;
1161 listitem_T *item;
1162 long reglen = 0;
1163 int type;
1164
1165#ifdef FEAT_CMDL_INFO
1166 if (add_to_showcmd(prev_c))
1167 if (add_to_showcmd('"'))
1168 out_flush();
1169#endif
1170 c = term_vgetc();
1171#ifdef FEAT_CMDL_INFO
1172 clear_showcmd();
1173#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001174 if (!term_use_loop())
1175 /* job finished while waiting for a character */
1176 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001177
1178 /* CTRL-W "= prompt for expression to evaluate. */
1179 if (c == '=' && get_expr_register() != '=')
1180 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001181 if (!term_use_loop())
1182 /* job finished while waiting for a character */
1183 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001184
1185 l = (list_T *)get_reg_contents(c, GREG_LIST);
1186 if (l != NULL)
1187 {
1188 type = get_reg_type(c, &reglen);
1189 for (item = l->lv_first; item != NULL; item = item->li_next)
1190 {
1191 char_u *s = get_tv_string(&item->li_tv);
1192
1193 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1194 s, STRLEN(s), NULL);
1195 if (item->li_next != NULL || type == MLINE)
1196 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1197 (char_u *)"\r", 1, NULL);
1198 }
1199 list_free(l);
1200 }
1201}
1202
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001203#if defined(FEAT_GUI) || defined(PROTO)
1204/*
1205 * Return TRUE when the cursor of the terminal should be displayed.
1206 */
1207 int
1208use_terminal_cursor()
1209{
1210 return in_terminal_loop != NULL;
1211}
1212
1213 cursorentry_T *
1214term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1215{
1216 term_T *term = in_terminal_loop;
1217 static cursorentry_T entry;
1218
1219 vim_memset(&entry, 0, sizeof(entry));
1220 entry.shape = entry.mshape =
1221 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1222 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1223 SHAPE_BLOCK;
1224 entry.percentage = 20;
1225 if (term->tl_cursor_blink)
1226 {
1227 entry.blinkwait = 700;
1228 entry.blinkon = 400;
1229 entry.blinkon = 250;
1230 }
1231 *fg = gui.back_pixel;
1232 if (term->tl_cursor_color == NULL)
1233 *bg = gui.norm_pixel;
1234 else
1235 *bg = color_name2handle(term->tl_cursor_color);
1236 entry.name = "n";
1237 entry.used_for = SHAPE_CURSOR;
1238
1239 return &entry;
1240}
1241#endif
1242
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001243static int did_change_cursor = FALSE;
1244
1245 static void
1246may_set_cursor_props(term_T *term)
1247{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001248#ifdef FEAT_GUI
1249 /* For the GUI the cursor properties are obtained with
1250 * term_get_cursor_shape(). */
1251 if (gui.in_use)
1252 return;
1253#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001254 if (in_terminal_loop == term)
1255 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001256 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001257 if (term->tl_cursor_color != NULL)
1258 term_cursor_color(term->tl_cursor_color);
1259 else
1260 term_cursor_color((char_u *)"");
1261 /* do both blink and shape+blink, in case setting shape does not work */
1262 term_cursor_blink(term->tl_cursor_blink);
1263 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1264 }
1265}
1266
1267 static void
1268may_restore_cursor_props(void)
1269{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001270#ifdef FEAT_GUI
1271 if (gui.in_use)
1272 return;
1273#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001274 if (did_change_cursor)
1275 {
1276 did_change_cursor = FALSE;
1277 ui_cursor_shape_forced(TRUE);
1278 term_cursor_color((char_u *)"");
1279 term_cursor_blink(FALSE);
1280 }
1281}
1282
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001283/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001284 * Returns TRUE if the current window contains a terminal and we are sending
1285 * keys to the job.
1286 */
1287 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001288term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001289{
1290 term_T *term = curbuf->b_term;
1291
1292 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001293 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001294 && term->tl_vterm != NULL
1295 && term_job_running(term);
1296}
1297
1298/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001299 * Wait for input and send it to the job.
1300 * Return when the start of a CTRL-W command is typed or anything else that
1301 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001302 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1303 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001304 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001305 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001306terminal_loop(void)
1307{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001308 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001309 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001310 int ret;
1311
Bram Moolenaar679653e2017-08-13 14:13:19 +02001312 /* Remember the terminal we are sending keys to. However, the terminal
1313 * might be closed while waiting for a character, e.g. typing "exit" in a
1314 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1315 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001316 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001317
1318 if (*curwin->w_p_tk != NUL)
1319 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001320 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001321 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001322
1323 for (;;)
1324 {
1325 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001326 /* Repeat redrawing in case a message is received while redrawing. */
1327 while (curwin->w_redr_type != 0)
1328 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001329 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001330
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001331 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001332 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001333 /* job finished while waiting for a character */
1334 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001335 if (c == K_IGNORE)
1336 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001337
Bram Moolenaarfae42832017-08-01 22:24:26 +02001338#ifdef UNIX
1339 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1340#endif
1341#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001342 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001343 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001344 if (ctrl_break_was_pressed)
1345 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001346#endif
1347
Bram Moolenaar69198192017-08-05 14:10:48 +02001348 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001349 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001350 int prev_c = c;
1351
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001352#ifdef FEAT_CMDL_INFO
1353 if (add_to_showcmd(c))
1354 out_flush();
1355#endif
1356 c = term_vgetc();
1357#ifdef FEAT_CMDL_INFO
1358 clear_showcmd();
1359#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001360 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361 /* job finished while waiting for a character */
1362 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001363
Bram Moolenaar69198192017-08-05 14:10:48 +02001364 if (prev_c == Ctrl_BSL)
1365 {
1366 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001367 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001368 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1369 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001370 ret = FAIL;
1371 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001372 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001373 /* Send both keys to the terminal. */
1374 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1375 }
1376 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001377 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001378 /* "CTRL-W .": send CTRL-W to the job */
1379 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001380 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001381 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001382 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001383 /* CTRL-W N : go to Terminal-Normal mode. */
1384 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001385 ret = FAIL;
1386 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001387 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001388 else if (c == '"')
1389 {
1390 term_paste_register(prev_c);
1391 continue;
1392 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001393 else if (termkey == 0 || c != termkey)
1394 {
1395 stuffcharReadbuff(Ctrl_W);
1396 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001397 ret = OK;
1398 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001399 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001400 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001401 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001402 {
1403 ret = OK;
1404 goto theend;
1405 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001406 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001407 ret = FAIL;
1408
1409theend:
1410 in_terminal_loop = NULL;
1411 may_restore_cursor_props();
1412 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001413}
1414
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001415/*
1416 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001417 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001418 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001419 */
1420 void
1421term_job_ended(job_T *job)
1422{
1423 term_T *term;
1424 int did_one = FALSE;
1425
1426 for (term = first_term; term != NULL; term = term->tl_next)
1427 if (term->tl_job == job)
1428 {
1429 vim_free(term->tl_title);
1430 term->tl_title = NULL;
1431 vim_free(term->tl_status_text);
1432 term->tl_status_text = NULL;
1433 redraw_buf_and_status_later(term->tl_buffer, VALID);
1434 did_one = TRUE;
1435 }
1436 if (did_one)
1437 redraw_statuslines();
1438 if (curbuf->b_term != NULL)
1439 {
1440 if (curbuf->b_term->tl_job == job)
1441 maketitle();
1442 update_cursor(curbuf->b_term, TRUE);
1443 }
1444}
1445
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001446 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001447may_toggle_cursor(term_T *term)
1448{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001449 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001450 {
1451 if (term->tl_cursor_visible)
1452 cursor_on();
1453 else
1454 cursor_off();
1455 }
1456}
1457
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001458 static int
1459handle_damage(VTermRect rect, void *user)
1460{
1461 term_T *term = (term_T *)user;
1462
1463 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1464 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1465 redraw_buf_later(term->tl_buffer, NOT_VALID);
1466 return 1;
1467}
1468
1469 static int
1470handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1471{
1472 term_T *term = (term_T *)user;
1473
1474 /* TODO */
1475 redraw_buf_later(term->tl_buffer, NOT_VALID);
1476 return 1;
1477}
1478
1479 static int
1480handle_movecursor(
1481 VTermPos pos,
1482 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001483 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001484 void *user)
1485{
1486 term_T *term = (term_T *)user;
1487 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001488
1489 term->tl_cursor_pos = pos;
1490 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001491
1492 FOR_ALL_WINDOWS(wp)
1493 {
1494 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001495 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001496 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001497 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001498 {
1499 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001500 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001501 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001502
1503 return 1;
1504}
1505
Bram Moolenaar21554412017-07-24 21:44:43 +02001506 static int
1507handle_settermprop(
1508 VTermProp prop,
1509 VTermValue *value,
1510 void *user)
1511{
1512 term_T *term = (term_T *)user;
1513
1514 switch (prop)
1515 {
1516 case VTERM_PROP_TITLE:
1517 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001518 /* a blank title isn't useful, make it empty, so that "running" is
1519 * displayed */
1520 if (*skipwhite((char_u *)value->string) == NUL)
1521 term->tl_title = NULL;
1522 else
1523 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001524 vim_free(term->tl_status_text);
1525 term->tl_status_text = NULL;
1526 if (term == curbuf->b_term)
1527 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001528 break;
1529
1530 case VTERM_PROP_CURSORVISIBLE:
1531 term->tl_cursor_visible = value->boolean;
1532 may_toggle_cursor(term);
1533 out_flush();
1534 break;
1535
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001536 case VTERM_PROP_CURSORBLINK:
1537 term->tl_cursor_blink = value->boolean;
1538 may_set_cursor_props(term);
1539 break;
1540
1541 case VTERM_PROP_CURSORSHAPE:
1542 term->tl_cursor_shape = value->number;
1543 may_set_cursor_props(term);
1544 break;
1545
1546 case VTERM_PROP_CURSORCOLOR:
1547 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001548 if (*value->string == NUL)
1549 term->tl_cursor_color = NULL;
1550 else
1551 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001552 may_set_cursor_props(term);
1553 break;
1554
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001555 case VTERM_PROP_ALTSCREEN:
1556 /* TODO: do anything else? */
1557 term->tl_using_altscreen = value->boolean;
1558 break;
1559
Bram Moolenaar21554412017-07-24 21:44:43 +02001560 default:
1561 break;
1562 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001563 /* Always return 1, otherwise vterm doesn't store the value internally. */
1564 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001565}
1566
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001567/*
1568 * The job running in the terminal resized the terminal.
1569 */
1570 static int
1571handle_resize(int rows, int cols, void *user)
1572{
1573 term_T *term = (term_T *)user;
1574 win_T *wp;
1575
1576 term->tl_rows = rows;
1577 term->tl_cols = cols;
1578 FOR_ALL_WINDOWS(wp)
1579 {
1580 if (wp->w_buffer == term->tl_buffer)
1581 {
1582 win_setheight_win(rows, wp);
1583 win_setwidth_win(cols, wp);
1584 }
1585 }
1586
1587 redraw_buf_later(term->tl_buffer, NOT_VALID);
1588 return 1;
1589}
1590
Bram Moolenaard85f2712017-07-28 21:51:57 +02001591/*
1592 * Handle a line that is pushed off the top of the screen.
1593 */
1594 static int
1595handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1596{
1597 term_T *term = (term_T *)user;
1598
1599 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001600 if (ga_grow(&term->tl_scrollback, 1) == OK)
1601 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001602 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001603 int len = 0;
1604 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001605 int c;
1606 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001607 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001608 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001609
1610 /* do not store empty cells at the end */
1611 for (i = 0; i < cols; ++i)
1612 if (cells[i].chars[0] != 0)
1613 len = i + 1;
1614
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001615 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001616 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001617 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001618 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001619 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001620 for (col = 0; col < len; col += cells[col].width)
1621 {
1622 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1623 {
1624 ga.ga_len = 0;
1625 break;
1626 }
1627 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1628 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1629 (char_u *)ga.ga_data + ga.ga_len);
1630 p[col].width = cells[col].width;
1631 p[col].attrs = cells[col].attrs;
1632 p[col].fg = cells[col].fg;
1633 p[col].bg = cells[col].bg;
1634 }
1635 }
1636 if (ga_grow(&ga, 1) == FAIL)
1637 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1638 else
1639 {
1640 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1641 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1642 }
1643 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001644
1645 line = (sb_line_T *)term->tl_scrollback.ga_data
1646 + term->tl_scrollback.ga_len;
1647 line->sb_cols = len;
1648 line->sb_cells = p;
1649 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001650 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001651 }
1652 return 0; /* ignored */
1653}
1654
Bram Moolenaar21554412017-07-24 21:44:43 +02001655static VTermScreenCallbacks screen_callbacks = {
1656 handle_damage, /* damage */
1657 handle_moverect, /* moverect */
1658 handle_movecursor, /* movecursor */
1659 handle_settermprop, /* settermprop */
1660 NULL, /* bell */
1661 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001662 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001663 NULL /* sb_popline */
1664};
1665
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001666/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001667 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001668 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001669 */
1670 void
1671term_channel_closed(channel_T *ch)
1672{
1673 term_T *term;
1674 int did_one = FALSE;
1675
1676 for (term = first_term; term != NULL; term = term->tl_next)
1677 if (term->tl_job == ch->ch_job)
1678 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001679 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001680 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001681
Bram Moolenaard85f2712017-07-28 21:51:57 +02001682 vim_free(term->tl_title);
1683 term->tl_title = NULL;
1684 vim_free(term->tl_status_text);
1685 term->tl_status_text = NULL;
1686
Bram Moolenaar423802d2017-07-30 16:52:24 +02001687 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001688 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001689 {
1690 int fnum = term->tl_buffer->b_fnum;
1691
Bram Moolenaar423802d2017-07-30 16:52:24 +02001692 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001693
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001694 if (term->tl_finish == 'c')
1695 {
1696 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001697 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001698 curbuf = term->tl_buffer;
1699 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1700 break;
1701 }
1702 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1703 {
1704 char buf[50];
1705
1706 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001707 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001708 vim_snprintf(buf, sizeof(buf),
1709 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001710 ? "botright sbuf %d"
1711 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001712 do_cmdline_cmd((char_u *)buf);
1713 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001714 else
1715 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001716 }
1717
Bram Moolenaard85f2712017-07-28 21:51:57 +02001718 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001719 }
1720 if (did_one)
1721 {
1722 redraw_statuslines();
1723
1724 /* Need to break out of vgetc(). */
1725 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001726 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001727
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001728 term = curbuf->b_term;
1729 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001730 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001731 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001732 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001733 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001734 }
1735 }
1736}
1737
1738/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001739 * Reverse engineer the RGB value into a cterm color index.
1740 * First color is 1. Return 0 if no match found.
1741 */
1742 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001743color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001744{
1745 int red = color->red;
1746 int blue = color->blue;
1747 int green = color->green;
1748
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001749 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001750 if (red == 0)
1751 {
1752 if (green == 0)
1753 {
1754 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001755 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001756 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001757 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001758 }
1759 else if (green == 224)
1760 {
1761 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001762 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001763 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001764 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001765 }
1766 }
1767 else if (red == 224)
1768 {
1769 if (green == 0)
1770 {
1771 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001772 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001773 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001774 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001775 }
1776 else if (green == 224)
1777 {
1778 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001779 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001780 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001781 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001782 }
1783 }
1784 else if (red == 128)
1785 {
1786 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001787 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001788 }
1789 else if (red == 255)
1790 {
1791 if (green == 64)
1792 {
1793 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001794 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001795 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001796 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001797 }
1798 else if (green == 255)
1799 {
1800 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001801 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001802 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001803 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001804 }
1805 }
1806 else if (red == 64)
1807 {
1808 if (green == 64)
1809 {
1810 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001811 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001812 }
1813 else if (green == 255)
1814 {
1815 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001816 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001817 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001818 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001819 }
1820 }
1821 if (t_colors >= 256)
1822 {
1823 if (red == blue && red == green)
1824 {
1825 /* 24-color greyscale */
1826 static int cutoff[23] = {
1827 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1828 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1829 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1830 int i;
1831
1832 for (i = 0; i < 23; ++i)
1833 if (red < cutoff[i])
1834 return i + 233;
1835 return 256;
1836 }
1837
1838 /* 216-color cube */
1839 return 17 + ((red + 25) / 0x33) * 36
1840 + ((green + 25) / 0x33) * 6
1841 + (blue + 25) / 0x33;
1842 }
1843 return 0;
1844}
1845
1846/*
1847 * Convert the attributes of a vterm cell into an attribute index.
1848 */
1849 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001850cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001851{
1852 int attr = 0;
1853
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001854 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001855 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001856 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001857 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001858 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001859 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001860 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001861 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001862 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001863 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001864
1865#ifdef FEAT_GUI
1866 if (gui.in_use)
1867 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001868 guicolor_T fg, bg;
1869
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001870 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1871 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001872 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001873 }
1874 else
1875#endif
1876#ifdef FEAT_TERMGUICOLORS
1877 if (p_tgc)
1878 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001879 guicolor_T fg, bg;
1880
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001881 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1882 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001883
1884 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001885 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001886 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001887#endif
1888 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001889 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001890 int fg = color2index(&cellfg, TRUE, &bold);
1891 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001892
1893 /* with 8 colors set the bold attribute to get a bright foreground */
1894 if (bold == TRUE)
1895 attr |= HL_BOLD;
1896 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001897 }
1898 return 0;
1899}
1900
1901/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001902 * Called to update a window that contains an active terminal.
1903 * Returns FAIL when there is no terminal running in this window or in
1904 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001905 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001906 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001907term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001908{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001909 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001910 VTerm *vterm;
1911 VTermScreen *screen;
1912 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001913 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001914
Bram Moolenaar6d819742017-08-06 14:57:49 +02001915 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001916 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001917
Bram Moolenaard85f2712017-07-28 21:51:57 +02001918 vterm = term->tl_vterm;
1919 screen = vterm_obtain_screen(vterm);
1920 state = vterm_obtain_state(vterm);
1921
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001922 /*
1923 * If the window was resized a redraw will be triggered and we get here.
1924 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1925 */
1926 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1927 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001928 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001929 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1930 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1931 win_T *twp;
1932
1933 FOR_ALL_WINDOWS(twp)
1934 {
1935 /* When more than one window shows the same terminal, use the
1936 * smallest size. */
1937 if (twp->w_buffer == term->tl_buffer)
1938 {
1939 if (!term->tl_rows_fixed && rows > twp->w_height)
1940 rows = twp->w_height;
1941 if (!term->tl_cols_fixed && cols > twp->w_width)
1942 cols = twp->w_width;
1943 }
1944 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001945
1946 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001947 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001948 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001949 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001950 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001951
1952 /* The cursor may have been moved when resizing. */
1953 vterm_state_get_cursorpos(state, &pos);
1954 position_cursor(wp, &pos);
1955
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001956 /* TODO: Only redraw what changed. */
1957 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001958 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001959 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001960 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001961
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001962 if (pos.row < term->tl_rows)
1963 {
1964 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001965 {
1966 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001967 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001968
Bram Moolenaareeac6772017-07-23 15:48:37 +02001969 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1970 vim_memset(&cell, 0, sizeof(cell));
1971
1972 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001973 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001974 if (c == NUL)
1975 {
1976 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001977#if defined(FEAT_MBYTE)
1978 if (enc_utf8)
1979 ScreenLinesUC[off] = NUL;
1980#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001981 }
1982 else
1983 {
1984#if defined(FEAT_MBYTE)
1985 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001986 {
1987 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001988 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001989 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001990 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001991 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001992 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001993 if (enc_utf8)
1994 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001995 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001996#else
1997 ScreenLines[off] = c;
1998#endif
1999 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002000 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002001
2002 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002003 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002004 if (cell.width == 2)
2005 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002006 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002007#if defined(FEAT_MBYTE)
2008 if (enc_utf8)
2009 ScreenLinesUC[off] = NUL;
2010#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002011 ++pos.col;
2012 ++off;
2013 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002014 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002015 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002016 else
2017 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002018
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002019 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2020 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002021 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002022
2023 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002024}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002025
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002026/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002027 * Return TRUE if "wp" is a terminal window where the job has finished.
2028 */
2029 int
2030term_is_finished(buf_T *buf)
2031{
2032 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2033}
2034
2035/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002036 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002037 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002038 */
2039 int
2040term_show_buffer(buf_T *buf)
2041{
2042 term_T *term = buf->b_term;
2043
Bram Moolenaar6d819742017-08-06 14:57:49 +02002044 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002045}
2046
2047/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002048 * The current buffer is going to be changed. If there is terminal
2049 * highlighting remove it now.
2050 */
2051 void
2052term_change_in_curbuf(void)
2053{
2054 term_T *term = curbuf->b_term;
2055
2056 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2057 {
2058 free_scrollback(term);
2059 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002060
2061 /* The buffer is now like a normal buffer, it cannot be easily
2062 * abandoned when changed. */
2063 set_string_option_direct((char_u *)"buftype", -1,
2064 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002065 }
2066}
2067
2068/*
2069 * Get the screen attribute for a position in the buffer.
2070 */
2071 int
2072term_get_attr(buf_T *buf, linenr_T lnum, int col)
2073{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002074 term_T *term = buf->b_term;
2075 sb_line_T *line;
2076 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002077
Bram Moolenaar70229f92017-07-29 16:01:53 +02002078 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002079 return 0;
2080 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2081 if (col >= line->sb_cols)
2082 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002083 cellattr = line->sb_cells + col;
2084 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002085}
2086
2087/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002088 * Create a new vterm and initialize it.
2089 */
2090 static void
2091create_vterm(term_T *term, int rows, int cols)
2092{
2093 VTerm *vterm;
2094 VTermScreen *screen;
2095
2096 vterm = vterm_new(rows, cols);
2097 term->tl_vterm = vterm;
2098 screen = vterm_obtain_screen(vterm);
2099 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2100 /* TODO: depends on 'encoding'. */
2101 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002102
2103 /* Vterm uses a default black background. Set it to white when
2104 * 'background' is "light". */
2105 if (*p_bg == 'l')
2106 {
2107 VTermColor fg, bg;
2108
2109 fg.red = fg.green = fg.blue = 0;
2110 bg.red = bg.green = bg.blue = 255;
2111 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2112 }
2113
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002114 /* Required to initialize most things. */
2115 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002116
2117 /* Allow using alternate screen. */
2118 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002119}
2120
Bram Moolenaar21554412017-07-24 21:44:43 +02002121/*
2122 * Return the text to show for the buffer name and status.
2123 */
2124 char_u *
2125term_get_status_text(term_T *term)
2126{
2127 if (term->tl_status_text == NULL)
2128 {
2129 char_u *txt;
2130 size_t len;
2131
Bram Moolenaar6d819742017-08-06 14:57:49 +02002132 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002133 {
2134 if (term_job_running(term))
2135 txt = (char_u *)_("Terminal");
2136 else
2137 txt = (char_u *)_("Terminal-finished");
2138 }
2139 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002140 txt = term->tl_title;
2141 else if (term_job_running(term))
2142 txt = (char_u *)_("running");
2143 else
2144 txt = (char_u *)_("finished");
2145 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002146 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002147 if (term->tl_status_text != NULL)
2148 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2149 term->tl_buffer->b_fname, txt);
2150 }
2151 return term->tl_status_text;
2152}
2153
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002154/*
2155 * Mark references in jobs of terminals.
2156 */
2157 int
2158set_ref_in_term(int copyID)
2159{
2160 int abort = FALSE;
2161 term_T *term;
2162 typval_T tv;
2163
2164 for (term = first_term; term != NULL; term = term->tl_next)
2165 if (term->tl_job != NULL)
2166 {
2167 tv.v_type = VAR_JOB;
2168 tv.vval.v_job = term->tl_job;
2169 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2170 }
2171 return abort;
2172}
2173
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002174/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002175 * Get the buffer from the first argument in "argvars".
2176 * Returns NULL when the buffer is not for a terminal window.
2177 */
2178 static buf_T *
2179term_get_buf(typval_T *argvars)
2180{
2181 buf_T *buf;
2182
2183 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2184 ++emsg_off;
2185 buf = get_buf_tv(&argvars[0], FALSE);
2186 --emsg_off;
2187 if (buf == NULL || buf->b_term == NULL)
2188 return NULL;
2189 return buf;
2190}
2191
2192/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002193 * "term_getaltscreen(buf)" function
2194 */
2195 void
2196f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2197{
2198 buf_T *buf = term_get_buf(argvars);
2199
2200 if (buf == NULL)
2201 return;
2202 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2203}
2204
2205/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002206 * "term_getattr(attr, name)" function
2207 */
2208 void
2209f_term_getattr(typval_T *argvars, typval_T *rettv)
2210{
2211 int attr;
2212 size_t i;
2213 char_u *name;
2214
2215 static struct {
2216 char *name;
2217 int attr;
2218 } attrs[] = {
2219 {"bold", HL_BOLD},
2220 {"italic", HL_ITALIC},
2221 {"underline", HL_UNDERLINE},
2222 {"strike", HL_STANDOUT},
2223 {"reverse", HL_INVERSE},
2224 };
2225
2226 attr = get_tv_number(&argvars[0]);
2227 name = get_tv_string_chk(&argvars[1]);
2228 if (name == NULL)
2229 return;
2230
2231 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2232 if (STRCMP(name, attrs[i].name) == 0)
2233 {
2234 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2235 break;
2236 }
2237}
2238
2239/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002240 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002241 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002242 void
2243f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002244{
Bram Moolenaar97870002017-07-30 18:28:38 +02002245 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002246 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002247 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002248 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002249
Bram Moolenaar97870002017-07-30 18:28:38 +02002250 if (rettv_list_alloc(rettv) == FAIL)
2251 return;
2252 if (buf == NULL)
2253 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002254 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002255
2256 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002257 list_append_number(l, term->tl_cursor_pos.row + 1);
2258 list_append_number(l, term->tl_cursor_pos.col + 1);
2259
2260 d = dict_alloc();
2261 if (d != NULL)
2262 {
2263 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2264 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2265 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2266 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2267 ? (char_u *)"" : term->tl_cursor_color);
2268 list_append_dict(l, d);
2269 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002270}
2271
2272/*
2273 * "term_getjob(buf)" function
2274 */
2275 void
2276f_term_getjob(typval_T *argvars, typval_T *rettv)
2277{
2278 buf_T *buf = term_get_buf(argvars);
2279
2280 rettv->v_type = VAR_JOB;
2281 rettv->vval.v_job = NULL;
2282 if (buf == NULL)
2283 return;
2284
2285 rettv->vval.v_job = buf->b_term->tl_job;
2286 if (rettv->vval.v_job != NULL)
2287 ++rettv->vval.v_job->jv_refcount;
2288}
2289
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002290 static int
2291get_row_number(typval_T *tv, term_T *term)
2292{
2293 if (tv->v_type == VAR_STRING
2294 && tv->vval.v_string != NULL
2295 && STRCMP(tv->vval.v_string, ".") == 0)
2296 return term->tl_cursor_pos.row;
2297 return (int)get_tv_number(tv) - 1;
2298}
2299
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300/*
2301 * "term_getline(buf, row)" function
2302 */
2303 void
2304f_term_getline(typval_T *argvars, typval_T *rettv)
2305{
2306 buf_T *buf = term_get_buf(argvars);
2307 term_T *term;
2308 int row;
2309
2310 rettv->v_type = VAR_STRING;
2311 if (buf == NULL)
2312 return;
2313 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002314 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002315
2316 if (term->tl_vterm == NULL)
2317 {
2318 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2319
2320 /* vterm is finished, get the text from the buffer */
2321 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2322 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2323 }
2324 else
2325 {
2326 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2327 VTermRect rect;
2328 int len;
2329 char_u *p;
2330
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002331 if (row < 0 || row >= term->tl_rows)
2332 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333 len = term->tl_cols * MB_MAXBYTES + 1;
2334 p = alloc(len);
2335 if (p == NULL)
2336 return;
2337 rettv->vval.v_string = p;
2338
2339 rect.start_col = 0;
2340 rect.end_col = term->tl_cols;
2341 rect.start_row = row;
2342 rect.end_row = row + 1;
2343 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2344 }
2345}
2346
2347/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002348 * "term_getscrolled(buf)" function
2349 */
2350 void
2351f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2352{
2353 buf_T *buf = term_get_buf(argvars);
2354
2355 if (buf == NULL)
2356 return;
2357 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2358}
2359
2360/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002361 * "term_getsize(buf)" function
2362 */
2363 void
2364f_term_getsize(typval_T *argvars, typval_T *rettv)
2365{
2366 buf_T *buf = term_get_buf(argvars);
2367 list_T *l;
2368
2369 if (rettv_list_alloc(rettv) == FAIL)
2370 return;
2371 if (buf == NULL)
2372 return;
2373
2374 l = rettv->vval.v_list;
2375 list_append_number(l, buf->b_term->tl_rows);
2376 list_append_number(l, buf->b_term->tl_cols);
2377}
2378
2379/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002380 * "term_getstatus(buf)" function
2381 */
2382 void
2383f_term_getstatus(typval_T *argvars, typval_T *rettv)
2384{
2385 buf_T *buf = term_get_buf(argvars);
2386 term_T *term;
2387 char_u val[100];
2388
2389 rettv->v_type = VAR_STRING;
2390 if (buf == NULL)
2391 return;
2392 term = buf->b_term;
2393
2394 if (term_job_running(term))
2395 STRCPY(val, "running");
2396 else
2397 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002398 if (term->tl_normal_mode)
2399 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002400 rettv->vval.v_string = vim_strsave(val);
2401}
2402
2403/*
2404 * "term_gettitle(buf)" function
2405 */
2406 void
2407f_term_gettitle(typval_T *argvars, typval_T *rettv)
2408{
2409 buf_T *buf = term_get_buf(argvars);
2410
2411 rettv->v_type = VAR_STRING;
2412 if (buf == NULL)
2413 return;
2414
2415 if (buf->b_term->tl_title != NULL)
2416 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2417}
2418
2419/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002420 * "term_gettty(buf)" function
2421 */
2422 void
2423f_term_gettty(typval_T *argvars, typval_T *rettv)
2424{
2425 buf_T *buf = term_get_buf(argvars);
2426 char_u *p;
2427
2428 rettv->v_type = VAR_STRING;
2429 if (buf == NULL)
2430 return;
2431 if (buf->b_term->tl_job != NULL)
2432 p = buf->b_term->tl_job->jv_tty_name;
2433 else
2434 p = buf->b_term->tl_tty_name;
2435 if (p != NULL)
2436 rettv->vval.v_string = vim_strsave(p);
2437}
2438
2439/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002440 * "term_list()" function
2441 */
2442 void
2443f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2444{
2445 term_T *tp;
2446 list_T *l;
2447
2448 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2449 return;
2450
2451 l = rettv->vval.v_list;
2452 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2453 if (tp != NULL && tp->tl_buffer != NULL)
2454 if (list_append_number(l,
2455 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2456 return;
2457}
2458
2459/*
2460 * "term_scrape(buf, row)" function
2461 */
2462 void
2463f_term_scrape(typval_T *argvars, typval_T *rettv)
2464{
2465 buf_T *buf = term_get_buf(argvars);
2466 VTermScreen *screen = NULL;
2467 VTermPos pos;
2468 list_T *l;
2469 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002470 char_u *p;
2471 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002472
2473 if (rettv_list_alloc(rettv) == FAIL)
2474 return;
2475 if (buf == NULL)
2476 return;
2477 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002478
2479 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002480 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002481
2482 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002483 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002484 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002485 p = NULL;
2486 line = NULL;
2487 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002488 else
2489 {
2490 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2491
2492 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2493 return;
2494 p = ml_get_buf(buf, lnum + 1, FALSE);
2495 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2496 }
2497
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002498 for (pos.col = 0; pos.col < term->tl_cols; )
2499 {
2500 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002501 int width;
2502 VTermScreenCellAttrs attrs;
2503 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002504 char_u rgb[8];
2505 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2506 int off = 0;
2507 int i;
2508
2509 if (screen == NULL)
2510 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002511 cellattr_T *cellattr;
2512 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002513
2514 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002515 if (pos.col >= line->sb_cols)
2516 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002517 cellattr = line->sb_cells + pos.col;
2518 width = cellattr->width;
2519 attrs = cellattr->attrs;
2520 fg = cellattr->fg;
2521 bg = cellattr->bg;
2522 len = MB_PTR2LEN(p);
2523 mch_memmove(mbs, p, len);
2524 mbs[len] = NUL;
2525 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002526 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002527 else
2528 {
2529 VTermScreenCell cell;
2530 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2531 break;
2532 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2533 {
2534 if (cell.chars[i] == 0)
2535 break;
2536 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2537 }
2538 mbs[off] = NUL;
2539 width = cell.width;
2540 attrs = cell.attrs;
2541 fg = cell.fg;
2542 bg = cell.bg;
2543 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002544 dcell = dict_alloc();
2545 list_append_dict(l, dcell);
2546
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002547 dict_add_nr_str(dcell, "chars", 0, mbs);
2548
2549 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002550 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002551 dict_add_nr_str(dcell, "fg", 0, rgb);
2552 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002553 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002554 dict_add_nr_str(dcell, "bg", 0, rgb);
2555
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002556 dict_add_nr_str(dcell, "attr",
2557 cell2attr(attrs, fg, bg), NULL);
2558 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002559
2560 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002561 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002562 ++pos.col;
2563 }
2564}
2565
2566/*
2567 * "term_sendkeys(buf, keys)" function
2568 */
2569 void
2570f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2571{
2572 buf_T *buf = term_get_buf(argvars);
2573 char_u *msg;
2574 term_T *term;
2575
2576 rettv->v_type = VAR_UNKNOWN;
2577 if (buf == NULL)
2578 return;
2579
2580 msg = get_tv_string_chk(&argvars[1]);
2581 if (msg == NULL)
2582 return;
2583 term = buf->b_term;
2584 if (term->tl_vterm == NULL)
2585 return;
2586
2587 while (*msg != NUL)
2588 {
2589 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2590 msg += MB_PTR2LEN(msg);
2591 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002592}
2593
2594/*
2595 * "term_start(command, options)" function
2596 */
2597 void
2598f_term_start(typval_T *argvars, typval_T *rettv)
2599{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002600 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002601
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002602 init_job_options(&opt);
2603 /* TODO: allow more job options */
2604 if (argvars[1].v_type != VAR_UNKNOWN
2605 && get_job_options(&argvars[1], &opt,
2606 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002607 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002608 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002609 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002610 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002611 return;
2612
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002613 if (opt.jo_vertical)
2614 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002615 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002616
2617 if (curbuf->b_term != NULL)
2618 rettv->vval.v_number = curbuf->b_fnum;
2619}
2620
2621/*
2622 * "term_wait" function
2623 */
2624 void
2625f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2626{
2627 buf_T *buf = term_get_buf(argvars);
2628
2629 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002630 {
2631 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002632 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002633 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002634 if (buf->b_term->tl_job == NULL)
2635 {
2636 ch_log(NULL, "term_wait(): no job to wait for");
2637 return;
2638 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002639
2640 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002641 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002642 {
2643 /* The job is dead, keep reading channel I/O until the channel is
2644 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002645 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002646 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2647 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002648 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002649 parse_queued_messages();
2650 ui_delay(10L, FALSE);
2651 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002652 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002653 parse_queued_messages();
2654 }
2655 else
2656 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002657 long wait = 10L;
2658
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002659 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002660 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002661
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002662 /* Wait for some time for any channel I/O. */
2663 if (argvars[1].v_type != VAR_UNKNOWN)
2664 wait = get_tv_number(&argvars[1]);
2665 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002666 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002667
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002668 /* Flushing messages on channels is hopefully sufficient.
2669 * TODO: is there a better way? */
2670 parse_queued_messages();
2671 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002672}
2673
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002674# ifdef WIN3264
2675
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002676/**************************************
2677 * 2. MS-Windows implementation.
2678 */
2679
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002680#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2681#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2682
Bram Moolenaar8a773062017-07-24 22:29:21 +02002683void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002684void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002685void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002686BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2687void (*winpty_config_set_initial_size)(void*, int, int);
2688LPCWSTR (*winpty_conin_name)(void*);
2689LPCWSTR (*winpty_conout_name)(void*);
2690LPCWSTR (*winpty_conerr_name)(void*);
2691void (*winpty_free)(void*);
2692void (*winpty_config_free)(void*);
2693void (*winpty_spawn_config_free)(void*);
2694void (*winpty_error_free)(void*);
2695LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002696BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002697HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002698
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002699#define WINPTY_DLL "winpty.dll"
2700
2701static HINSTANCE hWinPtyDLL = NULL;
2702
2703 int
2704dyn_winpty_init(void)
2705{
2706 int i;
2707 static struct
2708 {
2709 char *name;
2710 FARPROC *ptr;
2711 } winpty_entry[] =
2712 {
2713 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2714 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2715 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2716 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2717 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2718 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2719 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2720 {"winpty_free", (FARPROC*)&winpty_free},
2721 {"winpty_open", (FARPROC*)&winpty_open},
2722 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2723 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2724 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2725 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002726 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002727 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002728 {NULL, NULL}
2729 };
2730
2731 /* No need to initialize twice. */
2732 if (hWinPtyDLL)
2733 return 1;
2734 /* Load winpty.dll */
2735 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2736 if (!hWinPtyDLL)
2737 {
2738 EMSG2(_(e_loadlib), WINPTY_DLL);
2739 return 0;
2740 }
2741 for (i = 0; winpty_entry[i].name != NULL
2742 && winpty_entry[i].ptr != NULL; ++i)
2743 {
2744 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2745 winpty_entry[i].name)) == NULL)
2746 {
2747 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2748 return 0;
2749 }
2750 }
2751
2752 return 1;
2753}
2754
2755/*
2756 * Create a new terminal of "rows" by "cols" cells.
2757 * Store a reference in "term".
2758 * Return OK or FAIL.
2759 */
2760 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002761term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002762{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002763 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002764 channel_T *channel = NULL;
2765 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002766 DWORD error;
2767 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2768 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002769 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002770 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002771 garray_T ga;
2772 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002773
2774 if (!dyn_winpty_init())
2775 return FAIL;
2776
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002777 if (argvar->v_type == VAR_STRING)
2778 cmd = argvar->vval.v_string;
2779 else
2780 {
2781 ga_init2(&ga, (int)sizeof(char*), 20);
2782 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2783 goto failed;
2784 cmd = ga.ga_data;
2785 }
2786
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002787 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002788 if (p == NULL)
2789 return FAIL;
2790
2791 job = job_alloc();
2792 if (job == NULL)
2793 goto failed;
2794
2795 channel = add_channel();
2796 if (channel == NULL)
2797 goto failed;
2798
2799 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2800 if (term->tl_winpty_config == NULL)
2801 goto failed;
2802
2803 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2804 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2805 if (term->tl_winpty == NULL)
2806 goto failed;
2807
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002808 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002809 spawn_config = winpty_spawn_config_new(
2810 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2811 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2812 NULL,
2813 p,
2814 NULL,
2815 NULL,
2816 &winpty_err);
2817 if (spawn_config == NULL)
2818 goto failed;
2819
2820 channel = add_channel();
2821 if (channel == NULL)
2822 goto failed;
2823
2824 job = job_alloc();
2825 if (job == NULL)
2826 goto failed;
2827
2828 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2829 &child_thread_handle, &error, &winpty_err))
2830 goto failed;
2831
2832 channel_set_pipes(channel,
2833 (sock_T) CreateFileW(
2834 winpty_conin_name(term->tl_winpty),
2835 GENERIC_WRITE, 0, NULL,
2836 OPEN_EXISTING, 0, NULL),
2837 (sock_T) CreateFileW(
2838 winpty_conout_name(term->tl_winpty),
2839 GENERIC_READ, 0, NULL,
2840 OPEN_EXISTING, 0, NULL),
2841 (sock_T) CreateFileW(
2842 winpty_conerr_name(term->tl_winpty),
2843 GENERIC_READ, 0, NULL,
2844 OPEN_EXISTING, 0, NULL));
2845
2846 jo = CreateJobObject(NULL, NULL);
2847 if (jo == NULL)
2848 goto failed;
2849
2850 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002851 {
2852 /* Failed, switch the way to terminate process with TerminateProcess. */
2853 CloseHandle(jo);
2854 jo = NULL;
2855 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002856
2857 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002858 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002859
2860 create_vterm(term, rows, cols);
2861
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002862 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002863 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002864
2865 job->jv_channel = channel;
2866 job->jv_proc_info.hProcess = child_process_handle;
2867 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2868 job->jv_job_object = jo;
2869 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002870 sprintf(buf, "winpty://%lu",
2871 GetProcessId(winpty_agent_process(term->tl_winpty)));
2872 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002873 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002874 term->tl_job = job;
2875
2876 return OK;
2877
2878failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002879 if (argvar->v_type == VAR_LIST)
2880 vim_free(ga.ga_data);
2881 if (p != NULL)
2882 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002883 if (spawn_config != NULL)
2884 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002885 if (channel != NULL)
2886 channel_clear(channel);
2887 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002888 {
2889 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002890 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002891 }
2892 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002893 if (jo != NULL)
2894 CloseHandle(jo);
2895 if (term->tl_winpty != NULL)
2896 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002897 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002898 if (term->tl_winpty_config != NULL)
2899 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002900 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002901 if (winpty_err != NULL)
2902 {
2903 char_u *msg = utf16_to_enc(
2904 (short_u *)winpty_error_msg(winpty_err), NULL);
2905
2906 EMSG(msg);
2907 winpty_error_free(winpty_err);
2908 }
2909 return FAIL;
2910}
2911
2912/*
2913 * Free the terminal emulator part of "term".
2914 */
2915 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002916term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002917{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002918 if (term->tl_winpty != NULL)
2919 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002920 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002921 if (term->tl_winpty_config != NULL)
2922 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002923 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002924 if (term->tl_vterm != NULL)
2925 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002926 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002927}
2928
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002929/*
2930 * Request size to terminal.
2931 */
2932 static void
2933term_report_winsize(term_T *term, int rows, int cols)
2934{
2935 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2936}
2937
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002938# else
2939
2940/**************************************
2941 * 3. Unix-like implementation.
2942 */
2943
2944/*
2945 * Create a new terminal of "rows" by "cols" cells.
2946 * Start job for "cmd".
2947 * Store the pointers in "term".
2948 * Return OK or FAIL.
2949 */
2950 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002951term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002952{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002953 create_vterm(term, rows, cols);
2954
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002955 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002956 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002957 if (term->tl_job != NULL)
2958 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002959
Bram Moolenaar61a66052017-07-22 18:39:00 +02002960 return term->tl_job != NULL
2961 && term->tl_job->jv_channel != NULL
2962 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002963}
2964
2965/*
2966 * Free the terminal emulator part of "term".
2967 */
2968 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002969term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002970{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002971 if (term->tl_vterm != NULL)
2972 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002973 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002974}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002975
2976/*
2977 * Request size to terminal.
2978 */
2979 static void
2980term_report_winsize(term_T *term, int rows, int cols)
2981{
2982 /* Use an ioctl() to report the new window size to the job. */
2983 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2984 {
2985 int fd = -1;
2986 int part;
2987
2988 for (part = PART_OUT; part < PART_COUNT; ++part)
2989 {
2990 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2991 if (isatty(fd))
2992 break;
2993 }
2994 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002995 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002996 }
2997}
2998
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002999# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003000
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003001#endif /* FEAT_TERMINAL */