blob: e8727ee6e5d7a189fc0848d837aa0490398279fb [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.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020039 * - support different cursor shapes, colors and attributes
40 * - make term_getcursor() return type (none/block/bar/underline) and
41 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020042 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020043 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020045 * For the GUI fill termios with default values, perhaps like pangoterm:
46 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
47 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020049 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020050 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020051 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020052 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020053 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
54 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020055 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020058 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020059 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020060 * terminal. Allow:
61 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
62 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
63 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
64 * Check that something is connected to the terminal.
65 * Test: "cat" reading from a file or buffer
66 * "ls" writing stdout to a file or buffer
67 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020068 * - support ":term NONE" to open a terminal with a pty but not running a job
69 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020070 * - if the job in the terminal does not support the mouse, we can use the
71 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020072 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
73 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020074 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020075 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020076 * - Copy text in the vterm to the Vim buffer once in a while, so that
77 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078 */
79
80#include "vim.h"
81
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020082#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020083
Bram Moolenaard5310982017-08-05 15:16:32 +020084#ifndef MIN
85# define MIN(x,y) ((x) < (y) ? (x) : (y))
86#endif
87#ifndef MAX
88# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020089#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091#include "libvterm/include/vterm.h"
92
Bram Moolenaar33a43be2017-08-06 21:36:22 +020093/* This is VTermScreenCell without the characters, thus much smaller. */
94typedef struct {
95 VTermScreenCellAttrs attrs;
96 char width;
97 VTermColor fg, bg;
98} cellattr_T;
99
Bram Moolenaard85f2712017-07-28 21:51:57 +0200100typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200101 int sb_cols; /* can differ per line */
102 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200103} sb_line_T;
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105/* typedef term_T in structs.h */
106struct terminal_S {
107 term_T *tl_next;
108
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109 VTerm *tl_vterm;
110 job_T *tl_job;
111 buf_T *tl_buffer;
112
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200113 /* used when tl_job is NULL and only a pty was created */
114 int tl_tty_fd;
115 char_u *tl_tty_name;
116
Bram Moolenaar6d819742017-08-06 14:57:49 +0200117 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200118 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200119 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200120 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200121
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200122#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 void *tl_winpty_config;
124 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200125#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200127 /* last known vterm size */
128 int tl_rows;
129 int tl_cols;
130 /* vterm size does not follow window size */
131 int tl_rows_fixed;
132 int tl_cols_fixed;
133
Bram Moolenaar21554412017-07-24 21:44:43 +0200134 char_u *tl_title; /* NULL or allocated */
135 char_u *tl_status_text; /* NULL or allocated */
136
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200137 /* Range of screen rows to update. Zero based. */
138 int tl_dirty_row_start; /* -1 if nothing dirty */
139 int tl_dirty_row_end; /* row below last one to update */
140
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200142 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200144 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200145 int tl_cursor_visible;
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 Moolenaar8f84c3a2017-07-22 16:14:44 +0200158
159#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
160#define KEY_BUF_LEN 200
161
162/*
163 * Functions with separate implementation for MS-Windows and Unix-like systems.
164 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200165static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200166static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200167static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200168
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200169/**************************************
170 * 1. Generic code for all systems.
171 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200172
173/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200174 * Determine the terminal size from 'termsize' and the current window.
175 * Assumes term->tl_rows and term->tl_cols are zero.
176 */
177 static void
178set_term_and_win_size(term_T *term)
179{
180 if (*curwin->w_p_tms != NUL)
181 {
182 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
183
184 term->tl_rows = atoi((char *)curwin->w_p_tms);
185 term->tl_cols = atoi((char *)p);
186 }
187 if (term->tl_rows == 0)
188 term->tl_rows = curwin->w_height;
189 else
190 {
191 win_setheight_win(term->tl_rows, curwin);
192 term->tl_rows_fixed = TRUE;
193 }
194 if (term->tl_cols == 0)
195 term->tl_cols = curwin->w_width;
196 else
197 {
198 win_setwidth_win(term->tl_cols, curwin);
199 term->tl_cols_fixed = TRUE;
200 }
201}
202
203/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 * Initialize job options for a terminal job.
205 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200206 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 static void
208init_job_options(jobopt_T *opt)
209{
210 clear_job_options(opt);
211
212 opt->jo_mode = MODE_RAW;
213 opt->jo_out_mode = MODE_RAW;
214 opt->jo_err_mode = MODE_RAW;
215 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
216
217 opt->jo_io[PART_OUT] = JIO_BUFFER;
218 opt->jo_io[PART_ERR] = JIO_BUFFER;
219 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
220
221 opt->jo_modifiable[PART_OUT] = 0;
222 opt->jo_modifiable[PART_ERR] = 0;
223 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
224
225 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
226}
227
228/*
229 * Set job options mandatory for a terminal job.
230 */
231 static void
232setup_job_options(jobopt_T *opt, int rows, int cols)
233{
234 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
235 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
236 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200237 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
238 opt->jo_term_rows = rows;
239 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
240 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200241}
242
243 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200244term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246 exarg_T split_ea;
247 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200249 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250
251 if (check_restricted() || check_secure())
252 return;
253
254 term = (term_T *)alloc_clear(sizeof(term_T));
255 if (term == NULL)
256 return;
257 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200258 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200259 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200260 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200261
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200262 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200263 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200264 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200265 /* Create a new buffer in the current window. */
266 if (!can_abandon(curbuf, forceit))
267 {
268 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200269 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200270 return;
271 }
272 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
273 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200274 {
275 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200276 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 }
278 }
279 else if (opt->jo_hidden)
280 {
281 buf_T *buf;
282
283 /* Create a new buffer without a window. Make it the current buffer for
284 * a moment to be able to do the initialisations. */
285 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
286 BLN_NEW | BLN_LISTED);
287 if (buf == NULL || ml_open(buf) == FAIL)
288 {
289 vim_free(term);
290 return;
291 }
292 old_curbuf = curbuf;
293 --curbuf->b_nwindows;
294 curbuf = buf;
295 curwin->w_buffer = buf;
296 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200297 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200298 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200300 /* Open a new window or tab. */
301 split_ea.cmdidx = CMD_new;
302 split_ea.cmd = (char_u *)"new";
303 split_ea.arg = (char_u *)"";
304 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
305 {
306 split_ea.line2 = opt->jo_term_rows;
307 split_ea.addr_count = 1;
308 }
309 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
310 {
311 split_ea.line2 = opt->jo_term_cols;
312 split_ea.addr_count = 1;
313 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200314
Bram Moolenaarda43b612017-08-11 22:27:50 +0200315 ex_splitview(&split_ea);
316 if (curwin == old_curwin)
317 {
318 /* split failed */
319 vim_free(term);
320 return;
321 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200322 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200323 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200324 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200325
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200326 if (!opt->jo_hidden)
327 {
328 /* only one size was taken care of with :new, do the other one */
329 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
330 win_setheight(opt->jo_term_rows);
331 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
332 win_setwidth(opt->jo_term_cols);
333 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200334
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200336 term->tl_next = first_term;
337 first_term = term;
338
Bram Moolenaar293424c2017-07-26 23:11:01 +0200339 if (cmd == NULL || *cmd == NUL)
340 cmd = p_sh;
341
Bram Moolenaar78712a72017-08-05 14:50:12 +0200342 if (opt->jo_term_name != NULL)
343 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
344 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200345 {
346 int i;
347 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200348 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200349
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200350 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200351 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200352 /* Prepend a ! to the command name to avoid the buffer name equals
353 * the executable, otherwise ":w!" would overwrite it. */
354 if (i == 0)
355 vim_snprintf((char *)p, len, "!%s", cmd);
356 else
357 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200358 if (buflist_findname(p) == NULL)
359 {
360 curbuf->b_ffname = p;
361 break;
362 }
363 }
364 }
365 curbuf->b_fname = curbuf->b_ffname;
366
Bram Moolenaar37c45832017-08-12 16:01:04 +0200367 if (opt->jo_term_opencmd != NULL)
368 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
369
Bram Moolenaareb44a682017-08-03 22:44:55 +0200370 set_string_option_direct((char_u *)"buftype", -1,
371 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
372
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200373 /* Mark the buffer as not modifiable. It can only be made modifiable after
374 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200375 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200376
377 /* Set 'bufhidden' to "hide": allow closing the window. */
378 set_string_option_direct((char_u *)"bufhidden", -1,
379 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200380
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200381 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200382 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200383
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200384 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200385 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200386 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200387 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200388 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200389 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200390
391 if (old_curbuf != NULL)
392 {
393 --curbuf->b_nwindows;
394 curbuf = old_curbuf;
395 curwin->w_buffer = curbuf;
396 ++curbuf->b_nwindows;
397 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200398 }
399 else
400 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200401 buf_T *buf = curbuf;
402
Bram Moolenaard85f2712017-07-28 21:51:57 +0200403 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200404 if (old_curbuf != NULL)
405 {
406 --curbuf->b_nwindows;
407 curbuf = old_curbuf;
408 curwin->w_buffer = curbuf;
409 ++curbuf->b_nwindows;
410 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200411
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200412 /* Wiping out the buffer will also close the window and call
413 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200414 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200415 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200416}
417
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200418/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200419 * ":terminal": open a terminal window and execute a job in it.
420 */
421 void
422ex_terminal(exarg_T *eap)
423{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200424 jobopt_T opt;
425 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200426
427 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200428
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200429 cmd = eap->arg;
430 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
431 {
432 char_u *p;
433
434 cmd += 2;
435 p = skiptowhite(cmd);
436 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
437 opt.jo_term_finish = 'c';
438 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
439 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200440 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
441 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200442 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
443 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200444 else
445 {
446 if (*p)
447 *p = NUL;
448 EMSG2(_("E181: Invalid attribute: %s"), cmd);
449 return;
450 }
451 cmd = skipwhite(p);
452 }
453
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200454 if (eap->addr_count == 2)
455 {
456 opt.jo_term_rows = eap->line1;
457 opt.jo_term_cols = eap->line2;
458 }
459 else if (eap->addr_count == 1)
460 {
461 if (cmdmod.split & WSP_VERT)
462 opt.jo_term_cols = eap->line2;
463 else
464 opt.jo_term_rows = eap->line2;
465 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200466
Bram Moolenaarda43b612017-08-11 22:27:50 +0200467 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200468}
469
470/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200471 * Free the scrollback buffer for "term".
472 */
473 static void
474free_scrollback(term_T *term)
475{
476 int i;
477
478 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
479 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
480 ga_clear(&term->tl_scrollback);
481}
482
483/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200484 * Free a terminal and everything it refers to.
485 * Kills the job if there is one.
486 * Called when wiping out a buffer.
487 */
488 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200489free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200490{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200491 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200492 term_T *tp;
493
494 if (term == NULL)
495 return;
496 if (first_term == term)
497 first_term = term->tl_next;
498 else
499 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
500 if (tp->tl_next == term)
501 {
502 tp->tl_next = term->tl_next;
503 break;
504 }
505
506 if (term->tl_job != NULL)
507 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200508 if (term->tl_job->jv_status != JOB_ENDED
509 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200510 job_stop(term->tl_job, NULL, "kill");
511 job_unref(term->tl_job);
512 }
513
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200514 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200515
516 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200517 vim_free(term->tl_title);
518 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200519 vim_free(term->tl_opencmd);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200520 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200521 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200522}
523
524/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200525 * Write job output "msg[len]" to the vterm.
526 */
527 static void
528term_write_job_output(term_T *term, char_u *msg, size_t len)
529{
530 VTerm *vterm = term->tl_vterm;
531 char_u *p;
532 size_t done;
533 size_t len_now;
534
535 for (done = 0; done < len; done += len_now)
536 {
537 for (p = msg + done; p < msg + len; )
538 {
539 if (*p == NL)
540 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200541 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200542 }
543 len_now = p - msg - done;
544 vterm_input_write(vterm, (char *)msg + done, len_now);
545 if (p < msg + len && *p == NL)
546 {
547 /* Convert NL to CR-NL, that appears to work best. */
548 vterm_input_write(vterm, "\r\n", 2);
549 ++len_now;
550 }
551 }
552
553 /* this invokes the damage callbacks */
554 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
555}
556
Bram Moolenaar1c844932017-07-24 23:36:41 +0200557 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200558update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200559{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200560 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200561 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200562 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200563 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200564 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200565 if (term->tl_cursor_visible)
566 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200567 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200568#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200569 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200570 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200571#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200572 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200573}
574
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200575/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200576 * Invoked when "msg" output from a job was received. Write it to the terminal
577 * of "buffer".
578 */
579 void
580write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
581{
582 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200583 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200584
Bram Moolenaard85f2712017-07-28 21:51:57 +0200585 if (term->tl_vterm == NULL)
586 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200587 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200588 return;
589 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200590 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200591 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200592
Bram Moolenaar6d819742017-08-06 14:57:49 +0200593 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200594 {
595 /* TODO: only update once in a while. */
596 update_screen(0);
597 update_cursor(term, TRUE);
598 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200599}
600
601/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200602 * Send a mouse position and click to the vterm
603 */
604 static int
605term_send_mouse(VTerm *vterm, int button, int pressed)
606{
607 VTermModifier mod = VTERM_MOD_NONE;
608
609 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
610 mouse_col - W_WINCOL(curwin), mod);
611 vterm_mouse_button(vterm, button, pressed, mod);
612 return TRUE;
613}
614
615/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200616 * Convert typed key "c" into bytes to send to the job.
617 * Return the number of bytes in "buf".
618 */
619 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200620term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200622 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200623 VTermKey key = VTERM_KEY_NONE;
624 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200625 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200626
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200627 switch (c)
628 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200629 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200631 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
632 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200633 case K_DEL: key = VTERM_KEY_DEL; break;
634 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200635 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
636 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200637 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200638 case K_S_END: mod = VTERM_MOD_SHIFT;
639 key = VTERM_KEY_END; break;
640 case K_C_END: mod = VTERM_MOD_CTRL;
641 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
643 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
644 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
645 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
646 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
647 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
648 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
649 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
650 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
651 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
652 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
653 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
654 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200655 case K_S_HOME: mod = VTERM_MOD_SHIFT;
656 key = VTERM_KEY_HOME; break;
657 case K_C_HOME: mod = VTERM_MOD_CTRL;
658 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200659 case K_INS: key = VTERM_KEY_INS; break;
660 case K_K0: key = VTERM_KEY_KP_0; break;
661 case K_K1: key = VTERM_KEY_KP_1; break;
662 case K_K2: key = VTERM_KEY_KP_2; break;
663 case K_K3: key = VTERM_KEY_KP_3; break;
664 case K_K4: key = VTERM_KEY_KP_4; break;
665 case K_K5: key = VTERM_KEY_KP_5; break;
666 case K_K6: key = VTERM_KEY_KP_6; break;
667 case K_K7: key = VTERM_KEY_KP_7; break;
668 case K_K8: key = VTERM_KEY_KP_8; break;
669 case K_K9: key = VTERM_KEY_KP_9; break;
670 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
671 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
672 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
673 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
674 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
675 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
676 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
677 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
678 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
679 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
680 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
681 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
682 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200683 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
684 key = VTERM_KEY_LEFT; break;
685 case K_C_LEFT: mod = VTERM_MOD_CTRL;
686 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200687 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
688 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
689 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200690 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
691 key = VTERM_KEY_RIGHT; break;
692 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
693 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200695 case K_S_UP: mod = VTERM_MOD_SHIFT;
696 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200697 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200698
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200699 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
700 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
701 case K_MOUSELEFT: /* TODO */ return 0;
702 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200703
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200704 case K_LEFTMOUSE:
705 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
706 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
707 case K_LEFTRELEASE:
708 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
709 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
710 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
711 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
712 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
713 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
714 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
715 case K_X1MOUSE: /* TODO */ return 0;
716 case K_X1DRAG: /* TODO */ return 0;
717 case K_X1RELEASE: /* TODO */ return 0;
718 case K_X2MOUSE: /* TODO */ return 0;
719 case K_X2DRAG: /* TODO */ return 0;
720 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200721
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200722 case K_IGNORE: return 0;
723 case K_NOP: return 0;
724 case K_UNDO: return 0;
725 case K_HELP: return 0;
726 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
727 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
728 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
729 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
730 case K_SELECT: return 0;
731#ifdef FEAT_GUI
732 case K_VER_SCROLLBAR: return 0;
733 case K_HOR_SCROLLBAR: return 0;
734#endif
735#ifdef FEAT_GUI_TABLINE
736 case K_TABLINE: return 0;
737 case K_TABMENU: return 0;
738#endif
739#ifdef FEAT_NETBEANS_INTG
740 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
741#endif
742#ifdef FEAT_DND
743 case K_DROP: return 0;
744#endif
745#ifdef FEAT_AUTOCMD
746 case K_CURSORHOLD: return 0;
747#endif
748 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
749 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200750 }
751
752 /*
753 * Convert special keys to vterm keys:
754 * - Write keys to vterm: vterm_keyboard_key()
755 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200756 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200757 */
758 if (key != VTERM_KEY_NONE)
759 /* Special key, let vterm convert it. */
760 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200761 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200762 /* Normal character, let vterm convert it. */
763 vterm_keyboard_unichar(vterm, c, mod);
764
765 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200766 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200767}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200768
Bram Moolenaar938783d2017-07-16 20:13:26 +0200769/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200770 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200771 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200772 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200773term_job_running(term_T *term)
774{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200775 /* Also consider the job finished when the channel is closed, to avoid a
776 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200777 return term != NULL
778 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200779 && term->tl_job->jv_status == JOB_STARTED
780 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200781}
782
783/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200784 * Add the last line of the scrollback buffer to the buffer in the window.
785 */
786 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200787add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200788{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200789 buf_T *buf = term->tl_buffer;
790 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
791 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200792
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200793 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200794 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200795 {
796 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200797 curbuf = buf;
798 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200799 curbuf = curwin->w_buffer;
800 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200801}
802
803/*
804 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200805 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200806 */
807 static void
808move_terminal_to_buffer(term_T *term)
809{
810 win_T *wp;
811 int len;
812 int lines_skipped = 0;
813 VTermPos pos;
814 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200815 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200816 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200817
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200818 if (term->tl_vterm == NULL)
819 return;
820 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200821 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
822 {
823 len = 0;
824 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
825 if (vterm_screen_get_cell(screen, pos, &cell) != 0
826 && cell.chars[0] != NUL)
827 len = pos.col + 1;
828
829 if (len == 0)
830 ++lines_skipped;
831 else
832 {
833 while (lines_skipped > 0)
834 {
835 /* Line was skipped, add an empty line. */
836 --lines_skipped;
837 if (ga_grow(&term->tl_scrollback, 1) == OK)
838 {
839 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
840 + term->tl_scrollback.ga_len;
841
842 line->sb_cols = 0;
843 line->sb_cells = NULL;
844 ++term->tl_scrollback.ga_len;
845
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847 }
848 }
849
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200850 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200851 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
852 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200853 garray_T ga;
854 int width;
855 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856 + term->tl_scrollback.ga_len;
857
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200858 ga_init2(&ga, 1, 100);
859 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200860 {
861 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200862 {
863 width = 1;
864 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
865 if (ga_grow(&ga, 1) == OK)
866 ga.ga_len += mb_char2bytes(' ',
867 (char_u *)ga.ga_data + ga.ga_len);
868 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200869 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200870 {
871 width = cell.width;
872
873 p[pos.col].width = cell.width;
874 p[pos.col].attrs = cell.attrs;
875 p[pos.col].fg = cell.fg;
876 p[pos.col].bg = cell.bg;
877
878 if (ga_grow(&ga, MB_MAXBYTES) == OK)
879 {
880 int i;
881 int c;
882
883 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
884 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
885 (char_u *)ga.ga_data + ga.ga_len);
886 }
887 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 }
889 line->sb_cols = len;
890 line->sb_cells = p;
891 ++term->tl_scrollback.ga_len;
892
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 if (ga_grow(&ga, 1) == FAIL)
894 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
895 else
896 {
897 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
898 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
899 }
900 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200901 }
902 else
903 vim_free(p);
904 }
905 }
906
907 FOR_ALL_WINDOWS(wp)
908 {
909 if (wp->w_buffer == term->tl_buffer)
910 {
911 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
912 wp->w_cursor.col = 0;
913 wp->w_valid = 0;
914 redraw_win_later(wp, NOT_VALID);
915 }
916 }
917}
918
919 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200920set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200921{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200922 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200923 vim_free(term->tl_status_text);
924 term->tl_status_text = NULL;
925 if (term->tl_buffer == curbuf)
926 maketitle();
927}
928
929/*
930 * Called after the job if finished and Terminal mode is not active:
931 * Move the vterm contents into the scrollback buffer and free the vterm.
932 */
933 static void
934cleanup_vterm(term_T *term)
935{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200936 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200937 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200938 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200939 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200940}
941
942/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200943 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200944 * Suspends updating the terminal window.
945 */
946 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200947term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200948{
949 term_T *term = curbuf->b_term;
950
951 /* Append the current terminal contents to the buffer. */
952 move_terminal_to_buffer(term);
953
Bram Moolenaar6d819742017-08-06 14:57:49 +0200954 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200955
Bram Moolenaar6d819742017-08-06 14:57:49 +0200956 /* Move the window cursor to the position of the cursor in the
957 * terminal. */
958 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
959 + term->tl_cursor_pos.row + 1;
960 check_cursor();
961 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200962
Bram Moolenaar6d819742017-08-06 14:57:49 +0200963 /* Display the same lines as in the terminal. */
964 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200965}
966
967/*
968 * Returns TRUE if the current window contains a terminal and we are in
969 * Terminal-Normal mode.
970 */
971 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200972term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200973{
974 term_T *term = curbuf->b_term;
975
Bram Moolenaar6d819742017-08-06 14:57:49 +0200976 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200977}
978
979/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200980 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200981 * Restores updating the terminal window.
982 */
983 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200984term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985{
986 term_T *term = curbuf->b_term;
987 sb_line_T *line;
988 garray_T *gap;
989
990 /* Remove the terminal contents from the scrollback and the buffer. */
991 gap = &term->tl_scrollback;
992 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
993 {
994 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
995 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
996 vim_free(line->sb_cells);
997 --gap->ga_len;
998 if (gap->ga_len == 0)
999 break;
1000 }
1001 check_cursor();
1002
Bram Moolenaar6d819742017-08-06 14:57:49 +02001003 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001004
1005 if (term->tl_channel_closed)
1006 cleanup_vterm(term);
1007 redraw_buf_and_status_later(curbuf, NOT_VALID);
1008}
1009
1010/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001011 * Get a key from the user without mapping.
1012 * TODO: use terminal mode mappings.
1013 */
1014 static int
1015term_vgetc()
1016{
1017 int c;
1018
1019 ++no_mapping;
1020 ++allow_keys;
1021 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001022#ifdef WIN3264
1023 ctrl_break_was_pressed = FALSE;
1024#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001025 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001026 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001027 --no_mapping;
1028 --allow_keys;
1029 return c;
1030}
1031
1032/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001033 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001034 * Return FAIL when the key needs to be handled in Normal mode.
1035 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001036 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001037 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001038send_keys_to_term(term_T *term, int c, int typed)
1039{
1040 char msg[KEY_BUF_LEN];
1041 size_t len;
1042 static int mouse_was_outside = FALSE;
1043 int dragging_outside = FALSE;
1044
1045 /* Catch keys that need to be handled as in Normal mode. */
1046 switch (c)
1047 {
1048 case NUL:
1049 case K_ZERO:
1050 if (typed)
1051 stuffcharReadbuff(c);
1052 return FAIL;
1053
1054 case K_IGNORE:
1055 return FAIL;
1056
1057 case K_LEFTDRAG:
1058 case K_MIDDLEDRAG:
1059 case K_RIGHTDRAG:
1060 case K_X1DRAG:
1061 case K_X2DRAG:
1062 dragging_outside = mouse_was_outside;
1063 /* FALLTHROUGH */
1064 case K_LEFTMOUSE:
1065 case K_LEFTMOUSE_NM:
1066 case K_LEFTRELEASE:
1067 case K_LEFTRELEASE_NM:
1068 case K_MIDDLEMOUSE:
1069 case K_MIDDLERELEASE:
1070 case K_RIGHTMOUSE:
1071 case K_RIGHTRELEASE:
1072 case K_X1MOUSE:
1073 case K_X1RELEASE:
1074 case K_X2MOUSE:
1075 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001076
1077 case K_MOUSEUP:
1078 case K_MOUSEDOWN:
1079 case K_MOUSELEFT:
1080 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001081 if (mouse_row < W_WINROW(curwin)
1082 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1083 || mouse_col < W_WINCOL(curwin)
1084 || mouse_col >= W_ENDCOL(curwin)
1085 || dragging_outside)
1086 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001087 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001088 if (typed)
1089 {
1090 stuffcharReadbuff(c);
1091 mouse_was_outside = TRUE;
1092 }
1093 return FAIL;
1094 }
1095 }
1096 if (typed)
1097 mouse_was_outside = FALSE;
1098
1099 /* Convert the typed key to a sequence of bytes for the job. */
1100 len = term_convert_key(term, c, msg);
1101 if (len > 0)
1102 /* TODO: if FAIL is returned, stop? */
1103 channel_send(term->tl_job->jv_channel, PART_IN,
1104 (char_u *)msg, (int)len, NULL);
1105
1106 return OK;
1107}
1108
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001109 static void
1110position_cursor(win_T *wp, VTermPos *pos)
1111{
1112 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1113 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1114 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1115}
1116
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001117/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001118 * Handle CTRL-W "": send register contents to the job.
1119 */
1120 static void
1121term_paste_register(int prev_c UNUSED)
1122{
1123 int c;
1124 list_T *l;
1125 listitem_T *item;
1126 long reglen = 0;
1127 int type;
1128
1129#ifdef FEAT_CMDL_INFO
1130 if (add_to_showcmd(prev_c))
1131 if (add_to_showcmd('"'))
1132 out_flush();
1133#endif
1134 c = term_vgetc();
1135#ifdef FEAT_CMDL_INFO
1136 clear_showcmd();
1137#endif
1138
1139 /* CTRL-W "= prompt for expression to evaluate. */
1140 if (c == '=' && get_expr_register() != '=')
1141 return;
1142
1143 l = (list_T *)get_reg_contents(c, GREG_LIST);
1144 if (l != NULL)
1145 {
1146 type = get_reg_type(c, &reglen);
1147 for (item = l->lv_first; item != NULL; item = item->li_next)
1148 {
1149 char_u *s = get_tv_string(&item->li_tv);
1150
1151 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1152 s, STRLEN(s), NULL);
1153 if (item->li_next != NULL || type == MLINE)
1154 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1155 (char_u *)"\r", 1, NULL);
1156 }
1157 list_free(l);
1158 }
1159}
1160
1161/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001162 * Returns TRUE if the current window contains a terminal and we are sending
1163 * keys to the job.
1164 */
1165 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001166term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001167{
1168 term_T *term = curbuf->b_term;
1169
1170 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001171 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001172 && term->tl_vterm != NULL
1173 && term_job_running(term);
1174}
1175
1176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001177 * Wait for input and send it to the job.
1178 * Return when the start of a CTRL-W command is typed or anything else that
1179 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001180 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1181 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001182 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001183 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001184terminal_loop(void)
1185{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001186 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001187 int termkey = 0;
1188
1189 if (*curwin->w_p_tk != NUL)
1190 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001191 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001192
1193 for (;;)
1194 {
1195 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001196 /* Repeat redrawing in case a message is received while redrawing. */
1197 while (curwin->w_redr_type != 0)
1198 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001199 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001200
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001201 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001202 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001203 /* job finished while waiting for a character */
1204 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001205
Bram Moolenaarfae42832017-08-01 22:24:26 +02001206#ifdef UNIX
1207 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1208#endif
1209#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001210 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001211 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001212 if (ctrl_break_was_pressed)
1213 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001214#endif
1215
Bram Moolenaar69198192017-08-05 14:10:48 +02001216 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001217 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001218 int prev_c = c;
1219
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001220#ifdef FEAT_CMDL_INFO
1221 if (add_to_showcmd(c))
1222 out_flush();
1223#endif
1224 c = term_vgetc();
1225#ifdef FEAT_CMDL_INFO
1226 clear_showcmd();
1227#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001228 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001229 /* job finished while waiting for a character */
1230 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001231
Bram Moolenaar69198192017-08-05 14:10:48 +02001232 if (prev_c == Ctrl_BSL)
1233 {
1234 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001235 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001236 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1237 term_enter_normal_mode();
1238 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001239 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001240 /* Send both keys to the terminal. */
1241 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1242 }
1243 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001244 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001245 /* "CTRL-W .": send CTRL-W to the job */
1246 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001247 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001248 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001249 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001250 /* CTRL-W N : go to Terminal-Normal mode. */
1251 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001252 return FAIL;
1253 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001254 else if (c == '"')
1255 {
1256 term_paste_register(prev_c);
1257 continue;
1258 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001259 else if (termkey == 0 || c != termkey)
1260 {
1261 stuffcharReadbuff(Ctrl_W);
1262 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001263 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001264 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001265 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001266 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1267 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001268 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001269 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001270}
1271
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001272/*
1273 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001274 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001275 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001276 */
1277 void
1278term_job_ended(job_T *job)
1279{
1280 term_T *term;
1281 int did_one = FALSE;
1282
1283 for (term = first_term; term != NULL; term = term->tl_next)
1284 if (term->tl_job == job)
1285 {
1286 vim_free(term->tl_title);
1287 term->tl_title = NULL;
1288 vim_free(term->tl_status_text);
1289 term->tl_status_text = NULL;
1290 redraw_buf_and_status_later(term->tl_buffer, VALID);
1291 did_one = TRUE;
1292 }
1293 if (did_one)
1294 redraw_statuslines();
1295 if (curbuf->b_term != NULL)
1296 {
1297 if (curbuf->b_term->tl_job == job)
1298 maketitle();
1299 update_cursor(curbuf->b_term, TRUE);
1300 }
1301}
1302
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001303 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001304may_toggle_cursor(term_T *term)
1305{
1306 if (curbuf == term->tl_buffer)
1307 {
1308 if (term->tl_cursor_visible)
1309 cursor_on();
1310 else
1311 cursor_off();
1312 }
1313}
1314
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001315 static int
1316handle_damage(VTermRect rect, void *user)
1317{
1318 term_T *term = (term_T *)user;
1319
1320 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1321 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1322 redraw_buf_later(term->tl_buffer, NOT_VALID);
1323 return 1;
1324}
1325
1326 static int
1327handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1328{
1329 term_T *term = (term_T *)user;
1330
1331 /* TODO */
1332 redraw_buf_later(term->tl_buffer, NOT_VALID);
1333 return 1;
1334}
1335
1336 static int
1337handle_movecursor(
1338 VTermPos pos,
1339 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001340 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001341 void *user)
1342{
1343 term_T *term = (term_T *)user;
1344 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001345
1346 term->tl_cursor_pos = pos;
1347 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001348
1349 FOR_ALL_WINDOWS(wp)
1350 {
1351 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001353 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001354 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001355 {
1356 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001357 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001358 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001359
1360 return 1;
1361}
1362
Bram Moolenaar21554412017-07-24 21:44:43 +02001363 static int
1364handle_settermprop(
1365 VTermProp prop,
1366 VTermValue *value,
1367 void *user)
1368{
1369 term_T *term = (term_T *)user;
1370
1371 switch (prop)
1372 {
1373 case VTERM_PROP_TITLE:
1374 vim_free(term->tl_title);
1375 term->tl_title = vim_strsave((char_u *)value->string);
1376 vim_free(term->tl_status_text);
1377 term->tl_status_text = NULL;
1378 if (term == curbuf->b_term)
1379 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001380 break;
1381
1382 case VTERM_PROP_CURSORVISIBLE:
1383 term->tl_cursor_visible = value->boolean;
1384 may_toggle_cursor(term);
1385 out_flush();
1386 break;
1387
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001388 case VTERM_PROP_ALTSCREEN:
1389 /* TODO: do anything else? */
1390 term->tl_using_altscreen = value->boolean;
1391 break;
1392
Bram Moolenaar21554412017-07-24 21:44:43 +02001393 default:
1394 break;
1395 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001396 /* Always return 1, otherwise vterm doesn't store the value internally. */
1397 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001398}
1399
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001400/*
1401 * The job running in the terminal resized the terminal.
1402 */
1403 static int
1404handle_resize(int rows, int cols, void *user)
1405{
1406 term_T *term = (term_T *)user;
1407 win_T *wp;
1408
1409 term->tl_rows = rows;
1410 term->tl_cols = cols;
1411 FOR_ALL_WINDOWS(wp)
1412 {
1413 if (wp->w_buffer == term->tl_buffer)
1414 {
1415 win_setheight_win(rows, wp);
1416 win_setwidth_win(cols, wp);
1417 }
1418 }
1419
1420 redraw_buf_later(term->tl_buffer, NOT_VALID);
1421 return 1;
1422}
1423
Bram Moolenaard85f2712017-07-28 21:51:57 +02001424/*
1425 * Handle a line that is pushed off the top of the screen.
1426 */
1427 static int
1428handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1429{
1430 term_T *term = (term_T *)user;
1431
1432 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001433 if (ga_grow(&term->tl_scrollback, 1) == OK)
1434 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001435 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001436 int len = 0;
1437 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001438 int c;
1439 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001440 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001441 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001442
1443 /* do not store empty cells at the end */
1444 for (i = 0; i < cols; ++i)
1445 if (cells[i].chars[0] != 0)
1446 len = i + 1;
1447
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001448 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001449 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001450 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001451 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001452 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001453 for (col = 0; col < len; col += cells[col].width)
1454 {
1455 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1456 {
1457 ga.ga_len = 0;
1458 break;
1459 }
1460 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1461 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1462 (char_u *)ga.ga_data + ga.ga_len);
1463 p[col].width = cells[col].width;
1464 p[col].attrs = cells[col].attrs;
1465 p[col].fg = cells[col].fg;
1466 p[col].bg = cells[col].bg;
1467 }
1468 }
1469 if (ga_grow(&ga, 1) == FAIL)
1470 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1471 else
1472 {
1473 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1474 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1475 }
1476 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001477
1478 line = (sb_line_T *)term->tl_scrollback.ga_data
1479 + term->tl_scrollback.ga_len;
1480 line->sb_cols = len;
1481 line->sb_cells = p;
1482 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001483 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001484 }
1485 return 0; /* ignored */
1486}
1487
Bram Moolenaar21554412017-07-24 21:44:43 +02001488static VTermScreenCallbacks screen_callbacks = {
1489 handle_damage, /* damage */
1490 handle_moverect, /* moverect */
1491 handle_movecursor, /* movecursor */
1492 handle_settermprop, /* settermprop */
1493 NULL, /* bell */
1494 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001495 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001496 NULL /* sb_popline */
1497};
1498
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001499/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001500 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001501 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001502 */
1503 void
1504term_channel_closed(channel_T *ch)
1505{
1506 term_T *term;
1507 int did_one = FALSE;
1508
1509 for (term = first_term; term != NULL; term = term->tl_next)
1510 if (term->tl_job == ch->ch_job)
1511 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001512 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001513 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001514
Bram Moolenaard85f2712017-07-28 21:51:57 +02001515 vim_free(term->tl_title);
1516 term->tl_title = NULL;
1517 vim_free(term->tl_status_text);
1518 term->tl_status_text = NULL;
1519
Bram Moolenaar423802d2017-07-30 16:52:24 +02001520 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001521 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001522 {
1523 int fnum = term->tl_buffer->b_fnum;
1524
Bram Moolenaar423802d2017-07-30 16:52:24 +02001525 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001526
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001527 if (term->tl_finish == 'c')
1528 {
1529 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001530 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001531 curbuf = term->tl_buffer;
1532 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1533 break;
1534 }
1535 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1536 {
1537 char buf[50];
1538
1539 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001540 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001541 vim_snprintf(buf, sizeof(buf),
1542 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001543 ? "botright sbuf %d"
1544 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001545 do_cmdline_cmd((char_u *)buf);
1546 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001547 else
1548 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001549 }
1550
Bram Moolenaard85f2712017-07-28 21:51:57 +02001551 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001552 }
1553 if (did_one)
1554 {
1555 redraw_statuslines();
1556
1557 /* Need to break out of vgetc(). */
1558 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001559 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001560
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001561 term = curbuf->b_term;
1562 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001563 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001564 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001565 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001566 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001567 }
1568 }
1569}
1570
1571/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001572 * Reverse engineer the RGB value into a cterm color index.
1573 * First color is 1. Return 0 if no match found.
1574 */
1575 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001576color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001577{
1578 int red = color->red;
1579 int blue = color->blue;
1580 int green = color->green;
1581
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001582 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001583 if (red == 0)
1584 {
1585 if (green == 0)
1586 {
1587 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001588 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001589 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001590 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001591 }
1592 else if (green == 224)
1593 {
1594 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001595 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001596 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001597 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001598 }
1599 }
1600 else if (red == 224)
1601 {
1602 if (green == 0)
1603 {
1604 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001605 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001606 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001607 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001608 }
1609 else if (green == 224)
1610 {
1611 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001612 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001613 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001614 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001615 }
1616 }
1617 else if (red == 128)
1618 {
1619 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001620 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001621 }
1622 else if (red == 255)
1623 {
1624 if (green == 64)
1625 {
1626 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001627 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001628 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001629 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001630 }
1631 else if (green == 255)
1632 {
1633 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001634 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001635 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001636 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001637 }
1638 }
1639 else if (red == 64)
1640 {
1641 if (green == 64)
1642 {
1643 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001644 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001645 }
1646 else if (green == 255)
1647 {
1648 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001649 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001650 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001651 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001652 }
1653 }
1654 if (t_colors >= 256)
1655 {
1656 if (red == blue && red == green)
1657 {
1658 /* 24-color greyscale */
1659 static int cutoff[23] = {
1660 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1661 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1662 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1663 int i;
1664
1665 for (i = 0; i < 23; ++i)
1666 if (red < cutoff[i])
1667 return i + 233;
1668 return 256;
1669 }
1670
1671 /* 216-color cube */
1672 return 17 + ((red + 25) / 0x33) * 36
1673 + ((green + 25) / 0x33) * 6
1674 + (blue + 25) / 0x33;
1675 }
1676 return 0;
1677}
1678
1679/*
1680 * Convert the attributes of a vterm cell into an attribute index.
1681 */
1682 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001683cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001684{
1685 int attr = 0;
1686
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001687 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001688 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001689 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001690 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001691 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001692 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001693 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001694 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001695 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001696 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001697
1698#ifdef FEAT_GUI
1699 if (gui.in_use)
1700 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001701 guicolor_T fg, bg;
1702
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001703 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1704 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001705 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001706 }
1707 else
1708#endif
1709#ifdef FEAT_TERMGUICOLORS
1710 if (p_tgc)
1711 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001712 guicolor_T fg, bg;
1713
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001714 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1715 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001716
1717 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001718 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001719 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001720#endif
1721 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001722 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001723 int fg = color2index(&cellfg, TRUE, &bold);
1724 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001725
1726 /* with 8 colors set the bold attribute to get a bright foreground */
1727 if (bold == TRUE)
1728 attr |= HL_BOLD;
1729 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001730 }
1731 return 0;
1732}
1733
1734/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001735 * Called to update a window that contains an active terminal.
1736 * Returns FAIL when there is no terminal running in this window or in
1737 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001738 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001739 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001740term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001741{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001742 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001743 VTerm *vterm;
1744 VTermScreen *screen;
1745 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001746 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001747
Bram Moolenaar6d819742017-08-06 14:57:49 +02001748 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001749 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001750
Bram Moolenaard85f2712017-07-28 21:51:57 +02001751 vterm = term->tl_vterm;
1752 screen = vterm_obtain_screen(vterm);
1753 state = vterm_obtain_state(vterm);
1754
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001755 /*
1756 * If the window was resized a redraw will be triggered and we get here.
1757 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1758 */
1759 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1760 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001761 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001762 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1763 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1764 win_T *twp;
1765
1766 FOR_ALL_WINDOWS(twp)
1767 {
1768 /* When more than one window shows the same terminal, use the
1769 * smallest size. */
1770 if (twp->w_buffer == term->tl_buffer)
1771 {
1772 if (!term->tl_rows_fixed && rows > twp->w_height)
1773 rows = twp->w_height;
1774 if (!term->tl_cols_fixed && cols > twp->w_width)
1775 cols = twp->w_width;
1776 }
1777 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001778
1779 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001780 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001781 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001782 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001783 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001784
1785 /* The cursor may have been moved when resizing. */
1786 vterm_state_get_cursorpos(state, &pos);
1787 position_cursor(wp, &pos);
1788
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001789 /* TODO: Only redraw what changed. */
1790 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001791 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001792 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001793 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001794
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001795 if (pos.row < term->tl_rows)
1796 {
1797 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001798 {
1799 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001800 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001801
Bram Moolenaareeac6772017-07-23 15:48:37 +02001802 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1803 vim_memset(&cell, 0, sizeof(cell));
1804
1805 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001806 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001807 if (c == NUL)
1808 {
1809 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001810#if defined(FEAT_MBYTE)
1811 if (enc_utf8)
1812 ScreenLinesUC[off] = NUL;
1813#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001814 }
1815 else
1816 {
1817#if defined(FEAT_MBYTE)
1818 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001819 {
1820 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001821 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001822 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001823 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001824 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001825 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001826 if (enc_utf8)
1827 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001828 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001829#else
1830 ScreenLines[off] = c;
1831#endif
1832 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001833 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001834
1835 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001836 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001837 if (cell.width == 2)
1838 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001839 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001840#if defined(FEAT_MBYTE)
1841 if (enc_utf8)
1842 ScreenLinesUC[off] = NUL;
1843#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001844 ++pos.col;
1845 ++off;
1846 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001847 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001848 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001849 else
1850 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001851
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001852 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1853 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001854 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001855
1856 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001857}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001858
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001859/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001860 * Return TRUE if "wp" is a terminal window where the job has finished.
1861 */
1862 int
1863term_is_finished(buf_T *buf)
1864{
1865 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1866}
1867
1868/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001869 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001870 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001871 */
1872 int
1873term_show_buffer(buf_T *buf)
1874{
1875 term_T *term = buf->b_term;
1876
Bram Moolenaar6d819742017-08-06 14:57:49 +02001877 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001878}
1879
1880/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001881 * The current buffer is going to be changed. If there is terminal
1882 * highlighting remove it now.
1883 */
1884 void
1885term_change_in_curbuf(void)
1886{
1887 term_T *term = curbuf->b_term;
1888
1889 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1890 {
1891 free_scrollback(term);
1892 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001893
1894 /* The buffer is now like a normal buffer, it cannot be easily
1895 * abandoned when changed. */
1896 set_string_option_direct((char_u *)"buftype", -1,
1897 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001898 }
1899}
1900
1901/*
1902 * Get the screen attribute for a position in the buffer.
1903 */
1904 int
1905term_get_attr(buf_T *buf, linenr_T lnum, int col)
1906{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001907 term_T *term = buf->b_term;
1908 sb_line_T *line;
1909 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001910
Bram Moolenaar70229f92017-07-29 16:01:53 +02001911 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001912 return 0;
1913 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1914 if (col >= line->sb_cols)
1915 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001916 cellattr = line->sb_cells + col;
1917 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001918}
1919
1920/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001921 * Create a new vterm and initialize it.
1922 */
1923 static void
1924create_vterm(term_T *term, int rows, int cols)
1925{
1926 VTerm *vterm;
1927 VTermScreen *screen;
1928
1929 vterm = vterm_new(rows, cols);
1930 term->tl_vterm = vterm;
1931 screen = vterm_obtain_screen(vterm);
1932 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1933 /* TODO: depends on 'encoding'. */
1934 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001935
1936 /* Vterm uses a default black background. Set it to white when
1937 * 'background' is "light". */
1938 if (*p_bg == 'l')
1939 {
1940 VTermColor fg, bg;
1941
1942 fg.red = fg.green = fg.blue = 0;
1943 bg.red = bg.green = bg.blue = 255;
1944 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1945 }
1946
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001947 /* Required to initialize most things. */
1948 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001949
1950 /* Allow using alternate screen. */
1951 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001952}
1953
Bram Moolenaar21554412017-07-24 21:44:43 +02001954/*
1955 * Return the text to show for the buffer name and status.
1956 */
1957 char_u *
1958term_get_status_text(term_T *term)
1959{
1960 if (term->tl_status_text == NULL)
1961 {
1962 char_u *txt;
1963 size_t len;
1964
Bram Moolenaar6d819742017-08-06 14:57:49 +02001965 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001966 {
1967 if (term_job_running(term))
1968 txt = (char_u *)_("Terminal");
1969 else
1970 txt = (char_u *)_("Terminal-finished");
1971 }
1972 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001973 txt = term->tl_title;
1974 else if (term_job_running(term))
1975 txt = (char_u *)_("running");
1976 else
1977 txt = (char_u *)_("finished");
1978 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001979 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001980 if (term->tl_status_text != NULL)
1981 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1982 term->tl_buffer->b_fname, txt);
1983 }
1984 return term->tl_status_text;
1985}
1986
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001987/*
1988 * Mark references in jobs of terminals.
1989 */
1990 int
1991set_ref_in_term(int copyID)
1992{
1993 int abort = FALSE;
1994 term_T *term;
1995 typval_T tv;
1996
1997 for (term = first_term; term != NULL; term = term->tl_next)
1998 if (term->tl_job != NULL)
1999 {
2000 tv.v_type = VAR_JOB;
2001 tv.vval.v_job = term->tl_job;
2002 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2003 }
2004 return abort;
2005}
2006
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002007/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002008 * Get the buffer from the first argument in "argvars".
2009 * Returns NULL when the buffer is not for a terminal window.
2010 */
2011 static buf_T *
2012term_get_buf(typval_T *argvars)
2013{
2014 buf_T *buf;
2015
2016 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2017 ++emsg_off;
2018 buf = get_buf_tv(&argvars[0], FALSE);
2019 --emsg_off;
2020 if (buf == NULL || buf->b_term == NULL)
2021 return NULL;
2022 return buf;
2023}
2024
2025/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002026 * "term_getaltscreen(buf)" function
2027 */
2028 void
2029f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2030{
2031 buf_T *buf = term_get_buf(argvars);
2032
2033 if (buf == NULL)
2034 return;
2035 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2036}
2037
2038/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002039 * "term_getattr(attr, name)" function
2040 */
2041 void
2042f_term_getattr(typval_T *argvars, typval_T *rettv)
2043{
2044 int attr;
2045 size_t i;
2046 char_u *name;
2047
2048 static struct {
2049 char *name;
2050 int attr;
2051 } attrs[] = {
2052 {"bold", HL_BOLD},
2053 {"italic", HL_ITALIC},
2054 {"underline", HL_UNDERLINE},
2055 {"strike", HL_STANDOUT},
2056 {"reverse", HL_INVERSE},
2057 };
2058
2059 attr = get_tv_number(&argvars[0]);
2060 name = get_tv_string_chk(&argvars[1]);
2061 if (name == NULL)
2062 return;
2063
2064 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2065 if (STRCMP(name, attrs[i].name) == 0)
2066 {
2067 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2068 break;
2069 }
2070}
2071
2072/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002073 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002074 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002075 void
2076f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002077{
Bram Moolenaar97870002017-07-30 18:28:38 +02002078 buf_T *buf = term_get_buf(argvars);
2079 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002080
Bram Moolenaar97870002017-07-30 18:28:38 +02002081 if (rettv_list_alloc(rettv) == FAIL)
2082 return;
2083 if (buf == NULL)
2084 return;
2085
2086 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002087 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2088 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002089 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002090}
2091
2092/*
2093 * "term_getjob(buf)" function
2094 */
2095 void
2096f_term_getjob(typval_T *argvars, typval_T *rettv)
2097{
2098 buf_T *buf = term_get_buf(argvars);
2099
2100 rettv->v_type = VAR_JOB;
2101 rettv->vval.v_job = NULL;
2102 if (buf == NULL)
2103 return;
2104
2105 rettv->vval.v_job = buf->b_term->tl_job;
2106 if (rettv->vval.v_job != NULL)
2107 ++rettv->vval.v_job->jv_refcount;
2108}
2109
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002110 static int
2111get_row_number(typval_T *tv, term_T *term)
2112{
2113 if (tv->v_type == VAR_STRING
2114 && tv->vval.v_string != NULL
2115 && STRCMP(tv->vval.v_string, ".") == 0)
2116 return term->tl_cursor_pos.row;
2117 return (int)get_tv_number(tv) - 1;
2118}
2119
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002120/*
2121 * "term_getline(buf, row)" function
2122 */
2123 void
2124f_term_getline(typval_T *argvars, typval_T *rettv)
2125{
2126 buf_T *buf = term_get_buf(argvars);
2127 term_T *term;
2128 int row;
2129
2130 rettv->v_type = VAR_STRING;
2131 if (buf == NULL)
2132 return;
2133 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002134 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002135
2136 if (term->tl_vterm == NULL)
2137 {
2138 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2139
2140 /* vterm is finished, get the text from the buffer */
2141 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2142 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2143 }
2144 else
2145 {
2146 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2147 VTermRect rect;
2148 int len;
2149 char_u *p;
2150
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002151 if (row < 0 || row >= term->tl_rows)
2152 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002153 len = term->tl_cols * MB_MAXBYTES + 1;
2154 p = alloc(len);
2155 if (p == NULL)
2156 return;
2157 rettv->vval.v_string = p;
2158
2159 rect.start_col = 0;
2160 rect.end_col = term->tl_cols;
2161 rect.start_row = row;
2162 rect.end_row = row + 1;
2163 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2164 }
2165}
2166
2167/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002168 * "term_getscrolled(buf)" function
2169 */
2170 void
2171f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2172{
2173 buf_T *buf = term_get_buf(argvars);
2174
2175 if (buf == NULL)
2176 return;
2177 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2178}
2179
2180/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002181 * "term_getsize(buf)" function
2182 */
2183 void
2184f_term_getsize(typval_T *argvars, typval_T *rettv)
2185{
2186 buf_T *buf = term_get_buf(argvars);
2187 list_T *l;
2188
2189 if (rettv_list_alloc(rettv) == FAIL)
2190 return;
2191 if (buf == NULL)
2192 return;
2193
2194 l = rettv->vval.v_list;
2195 list_append_number(l, buf->b_term->tl_rows);
2196 list_append_number(l, buf->b_term->tl_cols);
2197}
2198
2199/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002200 * "term_getstatus(buf)" function
2201 */
2202 void
2203f_term_getstatus(typval_T *argvars, typval_T *rettv)
2204{
2205 buf_T *buf = term_get_buf(argvars);
2206 term_T *term;
2207 char_u val[100];
2208
2209 rettv->v_type = VAR_STRING;
2210 if (buf == NULL)
2211 return;
2212 term = buf->b_term;
2213
2214 if (term_job_running(term))
2215 STRCPY(val, "running");
2216 else
2217 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002218 if (term->tl_normal_mode)
2219 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002220 rettv->vval.v_string = vim_strsave(val);
2221}
2222
2223/*
2224 * "term_gettitle(buf)" function
2225 */
2226 void
2227f_term_gettitle(typval_T *argvars, typval_T *rettv)
2228{
2229 buf_T *buf = term_get_buf(argvars);
2230
2231 rettv->v_type = VAR_STRING;
2232 if (buf == NULL)
2233 return;
2234
2235 if (buf->b_term->tl_title != NULL)
2236 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2237}
2238
2239/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002240 * "term_gettty(buf)" function
2241 */
2242 void
2243f_term_gettty(typval_T *argvars, typval_T *rettv)
2244{
2245 buf_T *buf = term_get_buf(argvars);
2246 char_u *p;
2247
2248 rettv->v_type = VAR_STRING;
2249 if (buf == NULL)
2250 return;
2251 if (buf->b_term->tl_job != NULL)
2252 p = buf->b_term->tl_job->jv_tty_name;
2253 else
2254 p = buf->b_term->tl_tty_name;
2255 if (p != NULL)
2256 rettv->vval.v_string = vim_strsave(p);
2257}
2258
2259/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002260 * "term_list()" function
2261 */
2262 void
2263f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2264{
2265 term_T *tp;
2266 list_T *l;
2267
2268 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2269 return;
2270
2271 l = rettv->vval.v_list;
2272 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2273 if (tp != NULL && tp->tl_buffer != NULL)
2274 if (list_append_number(l,
2275 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2276 return;
2277}
2278
2279/*
2280 * "term_scrape(buf, row)" function
2281 */
2282 void
2283f_term_scrape(typval_T *argvars, typval_T *rettv)
2284{
2285 buf_T *buf = term_get_buf(argvars);
2286 VTermScreen *screen = NULL;
2287 VTermPos pos;
2288 list_T *l;
2289 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002290 char_u *p;
2291 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002292
2293 if (rettv_list_alloc(rettv) == FAIL)
2294 return;
2295 if (buf == NULL)
2296 return;
2297 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002298
2299 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002300 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002301
2302 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002303 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002304 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002305 p = NULL;
2306 line = NULL;
2307 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002308 else
2309 {
2310 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2311
2312 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2313 return;
2314 p = ml_get_buf(buf, lnum + 1, FALSE);
2315 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2316 }
2317
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002318 for (pos.col = 0; pos.col < term->tl_cols; )
2319 {
2320 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002321 int width;
2322 VTermScreenCellAttrs attrs;
2323 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002324 char_u rgb[8];
2325 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2326 int off = 0;
2327 int i;
2328
2329 if (screen == NULL)
2330 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002331 cellattr_T *cellattr;
2332 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333
2334 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002335 if (pos.col >= line->sb_cols)
2336 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002337 cellattr = line->sb_cells + pos.col;
2338 width = cellattr->width;
2339 attrs = cellattr->attrs;
2340 fg = cellattr->fg;
2341 bg = cellattr->bg;
2342 len = MB_PTR2LEN(p);
2343 mch_memmove(mbs, p, len);
2344 mbs[len] = NUL;
2345 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002346 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002347 else
2348 {
2349 VTermScreenCell cell;
2350 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2351 break;
2352 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2353 {
2354 if (cell.chars[i] == 0)
2355 break;
2356 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2357 }
2358 mbs[off] = NUL;
2359 width = cell.width;
2360 attrs = cell.attrs;
2361 fg = cell.fg;
2362 bg = cell.bg;
2363 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002364 dcell = dict_alloc();
2365 list_append_dict(l, dcell);
2366
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002367 dict_add_nr_str(dcell, "chars", 0, mbs);
2368
2369 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002370 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002371 dict_add_nr_str(dcell, "fg", 0, rgb);
2372 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002373 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002374 dict_add_nr_str(dcell, "bg", 0, rgb);
2375
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002376 dict_add_nr_str(dcell, "attr",
2377 cell2attr(attrs, fg, bg), NULL);
2378 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002379
2380 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002381 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002382 ++pos.col;
2383 }
2384}
2385
2386/*
2387 * "term_sendkeys(buf, keys)" function
2388 */
2389 void
2390f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2391{
2392 buf_T *buf = term_get_buf(argvars);
2393 char_u *msg;
2394 term_T *term;
2395
2396 rettv->v_type = VAR_UNKNOWN;
2397 if (buf == NULL)
2398 return;
2399
2400 msg = get_tv_string_chk(&argvars[1]);
2401 if (msg == NULL)
2402 return;
2403 term = buf->b_term;
2404 if (term->tl_vterm == NULL)
2405 return;
2406
2407 while (*msg != NUL)
2408 {
2409 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2410 msg += MB_PTR2LEN(msg);
2411 }
2412
Bram Moolenaar6d819742017-08-06 14:57:49 +02002413 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002414 {
2415 /* TODO: only update once in a while. */
2416 update_screen(0);
2417 if (buf == curbuf)
2418 update_cursor(term, TRUE);
2419 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002420}
2421
2422/*
2423 * "term_start(command, options)" function
2424 */
2425 void
2426f_term_start(typval_T *argvars, typval_T *rettv)
2427{
2428 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002429 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002430
2431 if (cmd == NULL)
2432 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002433 init_job_options(&opt);
2434 /* TODO: allow more job options */
2435 if (argvars[1].v_type != VAR_UNKNOWN
2436 && get_job_options(&argvars[1], &opt,
2437 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002438 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002439 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002440 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002441 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002442 return;
2443
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002444 if (opt.jo_vertical)
2445 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002446 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002447
2448 if (curbuf->b_term != NULL)
2449 rettv->vval.v_number = curbuf->b_fnum;
2450}
2451
2452/*
2453 * "term_wait" function
2454 */
2455 void
2456f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2457{
2458 buf_T *buf = term_get_buf(argvars);
2459
2460 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002461 {
2462 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002463 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002464 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002465 if (buf->b_term->tl_job == NULL)
2466 {
2467 ch_log(NULL, "term_wait(): no job to wait for");
2468 return;
2469 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002470
2471 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002472 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002473 {
2474 /* The job is dead, keep reading channel I/O until the channel is
2475 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002476 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002477 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2478 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002479 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002480 parse_queued_messages();
2481 ui_delay(10L, FALSE);
2482 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002483 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002484 parse_queued_messages();
2485 }
2486 else
2487 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002488 long wait = 10L;
2489
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002490 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002491 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002492
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002493 /* Wait for some time for any channel I/O. */
2494 if (argvars[1].v_type != VAR_UNKNOWN)
2495 wait = get_tv_number(&argvars[1]);
2496 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002497 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002498
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002499 /* Flushing messages on channels is hopefully sufficient.
2500 * TODO: is there a better way? */
2501 parse_queued_messages();
2502 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002503}
2504
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002505# ifdef WIN3264
2506
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002507/**************************************
2508 * 2. MS-Windows implementation.
2509 */
2510
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002511#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2512#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2513
Bram Moolenaar8a773062017-07-24 22:29:21 +02002514void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002515void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002516void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002517BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2518void (*winpty_config_set_initial_size)(void*, int, int);
2519LPCWSTR (*winpty_conin_name)(void*);
2520LPCWSTR (*winpty_conout_name)(void*);
2521LPCWSTR (*winpty_conerr_name)(void*);
2522void (*winpty_free)(void*);
2523void (*winpty_config_free)(void*);
2524void (*winpty_spawn_config_free)(void*);
2525void (*winpty_error_free)(void*);
2526LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002527BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002528HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002529
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530#define WINPTY_DLL "winpty.dll"
2531
2532static HINSTANCE hWinPtyDLL = NULL;
2533
2534 int
2535dyn_winpty_init(void)
2536{
2537 int i;
2538 static struct
2539 {
2540 char *name;
2541 FARPROC *ptr;
2542 } winpty_entry[] =
2543 {
2544 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2545 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2546 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2547 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2548 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2549 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2550 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2551 {"winpty_free", (FARPROC*)&winpty_free},
2552 {"winpty_open", (FARPROC*)&winpty_open},
2553 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2554 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2555 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2556 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002557 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002558 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002559 {NULL, NULL}
2560 };
2561
2562 /* No need to initialize twice. */
2563 if (hWinPtyDLL)
2564 return 1;
2565 /* Load winpty.dll */
2566 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2567 if (!hWinPtyDLL)
2568 {
2569 EMSG2(_(e_loadlib), WINPTY_DLL);
2570 return 0;
2571 }
2572 for (i = 0; winpty_entry[i].name != NULL
2573 && winpty_entry[i].ptr != NULL; ++i)
2574 {
2575 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2576 winpty_entry[i].name)) == NULL)
2577 {
2578 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2579 return 0;
2580 }
2581 }
2582
2583 return 1;
2584}
2585
2586/*
2587 * Create a new terminal of "rows" by "cols" cells.
2588 * Store a reference in "term".
2589 * Return OK or FAIL.
2590 */
2591 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002592term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002593{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002594 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002595 channel_T *channel = NULL;
2596 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002597 DWORD error;
2598 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2599 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002600 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002601 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002602
2603 if (!dyn_winpty_init())
2604 return FAIL;
2605
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002606 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002607 if (p == NULL)
2608 return FAIL;
2609
2610 job = job_alloc();
2611 if (job == NULL)
2612 goto failed;
2613
2614 channel = add_channel();
2615 if (channel == NULL)
2616 goto failed;
2617
2618 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2619 if (term->tl_winpty_config == NULL)
2620 goto failed;
2621
2622 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2623 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2624 if (term->tl_winpty == NULL)
2625 goto failed;
2626
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002627 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002628 spawn_config = winpty_spawn_config_new(
2629 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2630 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2631 NULL,
2632 p,
2633 NULL,
2634 NULL,
2635 &winpty_err);
2636 if (spawn_config == NULL)
2637 goto failed;
2638
2639 channel = add_channel();
2640 if (channel == NULL)
2641 goto failed;
2642
2643 job = job_alloc();
2644 if (job == NULL)
2645 goto failed;
2646
2647 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2648 &child_thread_handle, &error, &winpty_err))
2649 goto failed;
2650
2651 channel_set_pipes(channel,
2652 (sock_T) CreateFileW(
2653 winpty_conin_name(term->tl_winpty),
2654 GENERIC_WRITE, 0, NULL,
2655 OPEN_EXISTING, 0, NULL),
2656 (sock_T) CreateFileW(
2657 winpty_conout_name(term->tl_winpty),
2658 GENERIC_READ, 0, NULL,
2659 OPEN_EXISTING, 0, NULL),
2660 (sock_T) CreateFileW(
2661 winpty_conerr_name(term->tl_winpty),
2662 GENERIC_READ, 0, NULL,
2663 OPEN_EXISTING, 0, NULL));
2664
2665 jo = CreateJobObject(NULL, NULL);
2666 if (jo == NULL)
2667 goto failed;
2668
2669 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002670 {
2671 /* Failed, switch the way to terminate process with TerminateProcess. */
2672 CloseHandle(jo);
2673 jo = NULL;
2674 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002675
2676 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002677 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002678
2679 create_vterm(term, rows, cols);
2680
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002681 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002682 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002683
2684 job->jv_channel = channel;
2685 job->jv_proc_info.hProcess = child_process_handle;
2686 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2687 job->jv_job_object = jo;
2688 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002689 sprintf(buf, "winpty://%lu",
2690 GetProcessId(winpty_agent_process(term->tl_winpty)));
2691 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002692 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002693 term->tl_job = job;
2694
2695 return OK;
2696
2697failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002698 if (spawn_config != NULL)
2699 winpty_spawn_config_free(spawn_config);
2700 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002701 if (channel != NULL)
2702 channel_clear(channel);
2703 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002704 {
2705 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002706 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002707 }
2708 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002709 if (jo != NULL)
2710 CloseHandle(jo);
2711 if (term->tl_winpty != NULL)
2712 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002713 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002714 if (term->tl_winpty_config != NULL)
2715 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002716 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002717 if (winpty_err != NULL)
2718 {
2719 char_u *msg = utf16_to_enc(
2720 (short_u *)winpty_error_msg(winpty_err), NULL);
2721
2722 EMSG(msg);
2723 winpty_error_free(winpty_err);
2724 }
2725 return FAIL;
2726}
2727
2728/*
2729 * Free the terminal emulator part of "term".
2730 */
2731 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002732term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002733{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002734 if (term->tl_winpty != NULL)
2735 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002736 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002737 if (term->tl_winpty_config != NULL)
2738 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002739 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002740 if (term->tl_vterm != NULL)
2741 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002742 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002743}
2744
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002745/*
2746 * Request size to terminal.
2747 */
2748 static void
2749term_report_winsize(term_T *term, int rows, int cols)
2750{
2751 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2752}
2753
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002754# else
2755
2756/**************************************
2757 * 3. Unix-like implementation.
2758 */
2759
2760/*
2761 * Create a new terminal of "rows" by "cols" cells.
2762 * Start job for "cmd".
2763 * Store the pointers in "term".
2764 * Return OK or FAIL.
2765 */
2766 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002767term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002768{
2769 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002770
2771 create_vterm(term, rows, cols);
2772
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002773 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002774 argvars[0].v_type = VAR_STRING;
2775 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002776
2777 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002778 if (term->tl_job != NULL)
2779 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002780
Bram Moolenaar61a66052017-07-22 18:39:00 +02002781 return term->tl_job != NULL
2782 && term->tl_job->jv_channel != NULL
2783 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002784}
2785
2786/*
2787 * Free the terminal emulator part of "term".
2788 */
2789 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002790term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002791{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002792 if (term->tl_vterm != NULL)
2793 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002794 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002795}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002796
2797/*
2798 * Request size to terminal.
2799 */
2800 static void
2801term_report_winsize(term_T *term, int rows, int cols)
2802{
2803 /* Use an ioctl() to report the new window size to the job. */
2804 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2805 {
2806 int fd = -1;
2807 int part;
2808
2809 for (part = PART_OUT; part < PART_COUNT; ++part)
2810 {
2811 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2812 if (isatty(fd))
2813 break;
2814 }
2815 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002816 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002817 }
2818}
2819
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002820# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002821
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002822#endif /* FEAT_TERMINAL */