blob: 0b4c2ef68ab98e99f7d840faba2842fe31a7855b [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 Moolenaar9698ad72017-08-12 14:52:15 +02001210 /* On Windows we do not know whether the job can handle CTRL-C itself
1211 * or not. Therefore CTRL-C only sends a CTRL_C_EVENT to avoid killing
1212 * the shell instead of a command running in the shell.
1213 * Use CTRL-BREAK to kill the job. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02001214 if (c == Ctrl_C)
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001215 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"int");
1216 if (ctrl_break_was_pressed)
1217 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001218#endif
1219
Bram Moolenaar69198192017-08-05 14:10:48 +02001220 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001221 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001222 int prev_c = c;
1223
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001224#ifdef FEAT_CMDL_INFO
1225 if (add_to_showcmd(c))
1226 out_flush();
1227#endif
1228 c = term_vgetc();
1229#ifdef FEAT_CMDL_INFO
1230 clear_showcmd();
1231#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001232 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001233 /* job finished while waiting for a character */
1234 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001235
Bram Moolenaar69198192017-08-05 14:10:48 +02001236 if (prev_c == Ctrl_BSL)
1237 {
1238 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001239 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001240 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1241 term_enter_normal_mode();
1242 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001243 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001244 /* Send both keys to the terminal. */
1245 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1246 }
1247 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001248 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001249 /* "CTRL-W .": send CTRL-W to the job */
1250 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001251 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001252 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001253 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001254 /* CTRL-W N : go to Terminal-Normal mode. */
1255 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001256 return FAIL;
1257 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001258 else if (c == '"')
1259 {
1260 term_paste_register(prev_c);
1261 continue;
1262 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001263 else if (termkey == 0 || c != termkey)
1264 {
1265 stuffcharReadbuff(Ctrl_W);
1266 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001267 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001268 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001269 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001270 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1271 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001272 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001273 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001274}
1275
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001276/*
1277 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001278 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001279 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001280 */
1281 void
1282term_job_ended(job_T *job)
1283{
1284 term_T *term;
1285 int did_one = FALSE;
1286
1287 for (term = first_term; term != NULL; term = term->tl_next)
1288 if (term->tl_job == job)
1289 {
1290 vim_free(term->tl_title);
1291 term->tl_title = NULL;
1292 vim_free(term->tl_status_text);
1293 term->tl_status_text = NULL;
1294 redraw_buf_and_status_later(term->tl_buffer, VALID);
1295 did_one = TRUE;
1296 }
1297 if (did_one)
1298 redraw_statuslines();
1299 if (curbuf->b_term != NULL)
1300 {
1301 if (curbuf->b_term->tl_job == job)
1302 maketitle();
1303 update_cursor(curbuf->b_term, TRUE);
1304 }
1305}
1306
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001307 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001308may_toggle_cursor(term_T *term)
1309{
1310 if (curbuf == term->tl_buffer)
1311 {
1312 if (term->tl_cursor_visible)
1313 cursor_on();
1314 else
1315 cursor_off();
1316 }
1317}
1318
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001319 static int
1320handle_damage(VTermRect rect, void *user)
1321{
1322 term_T *term = (term_T *)user;
1323
1324 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1325 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1326 redraw_buf_later(term->tl_buffer, NOT_VALID);
1327 return 1;
1328}
1329
1330 static int
1331handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1332{
1333 term_T *term = (term_T *)user;
1334
1335 /* TODO */
1336 redraw_buf_later(term->tl_buffer, NOT_VALID);
1337 return 1;
1338}
1339
1340 static int
1341handle_movecursor(
1342 VTermPos pos,
1343 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001344 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001345 void *user)
1346{
1347 term_T *term = (term_T *)user;
1348 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001349
1350 term->tl_cursor_pos = pos;
1351 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352
1353 FOR_ALL_WINDOWS(wp)
1354 {
1355 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001357 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001358 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001359 {
1360 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001361 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001362 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001363
1364 return 1;
1365}
1366
Bram Moolenaar21554412017-07-24 21:44:43 +02001367 static int
1368handle_settermprop(
1369 VTermProp prop,
1370 VTermValue *value,
1371 void *user)
1372{
1373 term_T *term = (term_T *)user;
1374
1375 switch (prop)
1376 {
1377 case VTERM_PROP_TITLE:
1378 vim_free(term->tl_title);
1379 term->tl_title = vim_strsave((char_u *)value->string);
1380 vim_free(term->tl_status_text);
1381 term->tl_status_text = NULL;
1382 if (term == curbuf->b_term)
1383 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001384 break;
1385
1386 case VTERM_PROP_CURSORVISIBLE:
1387 term->tl_cursor_visible = value->boolean;
1388 may_toggle_cursor(term);
1389 out_flush();
1390 break;
1391
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001392 case VTERM_PROP_ALTSCREEN:
1393 /* TODO: do anything else? */
1394 term->tl_using_altscreen = value->boolean;
1395 break;
1396
Bram Moolenaar21554412017-07-24 21:44:43 +02001397 default:
1398 break;
1399 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001400 /* Always return 1, otherwise vterm doesn't store the value internally. */
1401 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001402}
1403
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001404/*
1405 * The job running in the terminal resized the terminal.
1406 */
1407 static int
1408handle_resize(int rows, int cols, void *user)
1409{
1410 term_T *term = (term_T *)user;
1411 win_T *wp;
1412
1413 term->tl_rows = rows;
1414 term->tl_cols = cols;
1415 FOR_ALL_WINDOWS(wp)
1416 {
1417 if (wp->w_buffer == term->tl_buffer)
1418 {
1419 win_setheight_win(rows, wp);
1420 win_setwidth_win(cols, wp);
1421 }
1422 }
1423
1424 redraw_buf_later(term->tl_buffer, NOT_VALID);
1425 return 1;
1426}
1427
Bram Moolenaard85f2712017-07-28 21:51:57 +02001428/*
1429 * Handle a line that is pushed off the top of the screen.
1430 */
1431 static int
1432handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1433{
1434 term_T *term = (term_T *)user;
1435
1436 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001437 if (ga_grow(&term->tl_scrollback, 1) == OK)
1438 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001439 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001440 int len = 0;
1441 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001442 int c;
1443 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001444 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001445 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001446
1447 /* do not store empty cells at the end */
1448 for (i = 0; i < cols; ++i)
1449 if (cells[i].chars[0] != 0)
1450 len = i + 1;
1451
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001452 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001453 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001454 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001455 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001456 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001457 for (col = 0; col < len; col += cells[col].width)
1458 {
1459 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1460 {
1461 ga.ga_len = 0;
1462 break;
1463 }
1464 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1465 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1466 (char_u *)ga.ga_data + ga.ga_len);
1467 p[col].width = cells[col].width;
1468 p[col].attrs = cells[col].attrs;
1469 p[col].fg = cells[col].fg;
1470 p[col].bg = cells[col].bg;
1471 }
1472 }
1473 if (ga_grow(&ga, 1) == FAIL)
1474 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1475 else
1476 {
1477 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1478 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1479 }
1480 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001481
1482 line = (sb_line_T *)term->tl_scrollback.ga_data
1483 + term->tl_scrollback.ga_len;
1484 line->sb_cols = len;
1485 line->sb_cells = p;
1486 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001487 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001488 }
1489 return 0; /* ignored */
1490}
1491
Bram Moolenaar21554412017-07-24 21:44:43 +02001492static VTermScreenCallbacks screen_callbacks = {
1493 handle_damage, /* damage */
1494 handle_moverect, /* moverect */
1495 handle_movecursor, /* movecursor */
1496 handle_settermprop, /* settermprop */
1497 NULL, /* bell */
1498 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001499 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001500 NULL /* sb_popline */
1501};
1502
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001503/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001504 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001505 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001506 */
1507 void
1508term_channel_closed(channel_T *ch)
1509{
1510 term_T *term;
1511 int did_one = FALSE;
1512
1513 for (term = first_term; term != NULL; term = term->tl_next)
1514 if (term->tl_job == ch->ch_job)
1515 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001516 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001517 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001518
Bram Moolenaard85f2712017-07-28 21:51:57 +02001519 vim_free(term->tl_title);
1520 term->tl_title = NULL;
1521 vim_free(term->tl_status_text);
1522 term->tl_status_text = NULL;
1523
Bram Moolenaar423802d2017-07-30 16:52:24 +02001524 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001525 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001526 {
1527 int fnum = term->tl_buffer->b_fnum;
1528
Bram Moolenaar423802d2017-07-30 16:52:24 +02001529 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001530
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001531 if (term->tl_finish == 'c')
1532 {
1533 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001534 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001535 curbuf = term->tl_buffer;
1536 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1537 break;
1538 }
1539 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1540 {
1541 char buf[50];
1542
1543 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001544 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001545 vim_snprintf(buf, sizeof(buf),
1546 term->tl_opencmd == NULL
1547 ? "botright sbuf %d" : term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001548 do_cmdline_cmd((char_u *)buf);
1549 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001550 else
1551 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001552 }
1553
Bram Moolenaard85f2712017-07-28 21:51:57 +02001554 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001555 }
1556 if (did_one)
1557 {
1558 redraw_statuslines();
1559
1560 /* Need to break out of vgetc(). */
1561 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001562 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001563
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001564 term = curbuf->b_term;
1565 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001566 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001567 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001568 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001569 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001570 }
1571 }
1572}
1573
1574/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001575 * Reverse engineer the RGB value into a cterm color index.
1576 * First color is 1. Return 0 if no match found.
1577 */
1578 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001579color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001580{
1581 int red = color->red;
1582 int blue = color->blue;
1583 int green = color->green;
1584
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001585 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001586 if (red == 0)
1587 {
1588 if (green == 0)
1589 {
1590 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001591 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001592 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001593 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001594 }
1595 else if (green == 224)
1596 {
1597 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001598 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001599 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001600 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001601 }
1602 }
1603 else if (red == 224)
1604 {
1605 if (green == 0)
1606 {
1607 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001608 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001609 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001610 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001611 }
1612 else if (green == 224)
1613 {
1614 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001615 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001616 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001617 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001618 }
1619 }
1620 else if (red == 128)
1621 {
1622 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001623 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001624 }
1625 else if (red == 255)
1626 {
1627 if (green == 64)
1628 {
1629 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001630 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001631 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001632 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001633 }
1634 else if (green == 255)
1635 {
1636 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001637 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001638 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001639 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001640 }
1641 }
1642 else if (red == 64)
1643 {
1644 if (green == 64)
1645 {
1646 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001647 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001648 }
1649 else if (green == 255)
1650 {
1651 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001652 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001653 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001654 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001655 }
1656 }
1657 if (t_colors >= 256)
1658 {
1659 if (red == blue && red == green)
1660 {
1661 /* 24-color greyscale */
1662 static int cutoff[23] = {
1663 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1664 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1665 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1666 int i;
1667
1668 for (i = 0; i < 23; ++i)
1669 if (red < cutoff[i])
1670 return i + 233;
1671 return 256;
1672 }
1673
1674 /* 216-color cube */
1675 return 17 + ((red + 25) / 0x33) * 36
1676 + ((green + 25) / 0x33) * 6
1677 + (blue + 25) / 0x33;
1678 }
1679 return 0;
1680}
1681
1682/*
1683 * Convert the attributes of a vterm cell into an attribute index.
1684 */
1685 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001686cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001687{
1688 int attr = 0;
1689
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001690 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001691 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001692 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001693 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001694 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001695 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001696 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001697 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001698 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001699 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001700
1701#ifdef FEAT_GUI
1702 if (gui.in_use)
1703 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001704 guicolor_T fg, bg;
1705
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001706 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1707 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001708 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001709 }
1710 else
1711#endif
1712#ifdef FEAT_TERMGUICOLORS
1713 if (p_tgc)
1714 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001715 guicolor_T fg, bg;
1716
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001717 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1718 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001719
1720 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001721 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001722 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001723#endif
1724 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001725 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001726 int fg = color2index(&cellfg, TRUE, &bold);
1727 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001728
1729 /* with 8 colors set the bold attribute to get a bright foreground */
1730 if (bold == TRUE)
1731 attr |= HL_BOLD;
1732 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001733 }
1734 return 0;
1735}
1736
1737/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001738 * Called to update a window that contains an active terminal.
1739 * Returns FAIL when there is no terminal running in this window or in
1740 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001741 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001742 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001743term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001744{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001745 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001746 VTerm *vterm;
1747 VTermScreen *screen;
1748 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001749 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001750
Bram Moolenaar6d819742017-08-06 14:57:49 +02001751 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001752 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001753
Bram Moolenaard85f2712017-07-28 21:51:57 +02001754 vterm = term->tl_vterm;
1755 screen = vterm_obtain_screen(vterm);
1756 state = vterm_obtain_state(vterm);
1757
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001758 /*
1759 * If the window was resized a redraw will be triggered and we get here.
1760 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1761 */
1762 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1763 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001764 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001765 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1766 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1767 win_T *twp;
1768
1769 FOR_ALL_WINDOWS(twp)
1770 {
1771 /* When more than one window shows the same terminal, use the
1772 * smallest size. */
1773 if (twp->w_buffer == term->tl_buffer)
1774 {
1775 if (!term->tl_rows_fixed && rows > twp->w_height)
1776 rows = twp->w_height;
1777 if (!term->tl_cols_fixed && cols > twp->w_width)
1778 cols = twp->w_width;
1779 }
1780 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001781
1782 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001783 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001784 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001785 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001786 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001787
1788 /* The cursor may have been moved when resizing. */
1789 vterm_state_get_cursorpos(state, &pos);
1790 position_cursor(wp, &pos);
1791
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001792 /* TODO: Only redraw what changed. */
1793 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001794 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001795 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001796 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001797
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001798 if (pos.row < term->tl_rows)
1799 {
1800 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001801 {
1802 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001803 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001804
Bram Moolenaareeac6772017-07-23 15:48:37 +02001805 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1806 vim_memset(&cell, 0, sizeof(cell));
1807
1808 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001809 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001810 if (c == NUL)
1811 {
1812 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001813#if defined(FEAT_MBYTE)
1814 if (enc_utf8)
1815 ScreenLinesUC[off] = NUL;
1816#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001817 }
1818 else
1819 {
1820#if defined(FEAT_MBYTE)
1821 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001822 {
1823 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001824 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001825 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001826 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001827 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001828 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001829 if (enc_utf8)
1830 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001831 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001832#else
1833 ScreenLines[off] = c;
1834#endif
1835 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001836 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001837
1838 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001839 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001840 if (cell.width == 2)
1841 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001842 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001843#if defined(FEAT_MBYTE)
1844 if (enc_utf8)
1845 ScreenLinesUC[off] = NUL;
1846#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001847 ++pos.col;
1848 ++off;
1849 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001850 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001851 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001852 else
1853 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001854
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001855 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1856 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001857 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001858
1859 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001860}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001861
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001862/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001863 * Return TRUE if "wp" is a terminal window where the job has finished.
1864 */
1865 int
1866term_is_finished(buf_T *buf)
1867{
1868 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1869}
1870
1871/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001872 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001873 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001874 */
1875 int
1876term_show_buffer(buf_T *buf)
1877{
1878 term_T *term = buf->b_term;
1879
Bram Moolenaar6d819742017-08-06 14:57:49 +02001880 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001881}
1882
1883/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001884 * The current buffer is going to be changed. If there is terminal
1885 * highlighting remove it now.
1886 */
1887 void
1888term_change_in_curbuf(void)
1889{
1890 term_T *term = curbuf->b_term;
1891
1892 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1893 {
1894 free_scrollback(term);
1895 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001896
1897 /* The buffer is now like a normal buffer, it cannot be easily
1898 * abandoned when changed. */
1899 set_string_option_direct((char_u *)"buftype", -1,
1900 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001901 }
1902}
1903
1904/*
1905 * Get the screen attribute for a position in the buffer.
1906 */
1907 int
1908term_get_attr(buf_T *buf, linenr_T lnum, int col)
1909{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001910 term_T *term = buf->b_term;
1911 sb_line_T *line;
1912 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001913
Bram Moolenaar70229f92017-07-29 16:01:53 +02001914 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001915 return 0;
1916 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1917 if (col >= line->sb_cols)
1918 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001919 cellattr = line->sb_cells + col;
1920 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001921}
1922
1923/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001924 * Create a new vterm and initialize it.
1925 */
1926 static void
1927create_vterm(term_T *term, int rows, int cols)
1928{
1929 VTerm *vterm;
1930 VTermScreen *screen;
1931
1932 vterm = vterm_new(rows, cols);
1933 term->tl_vterm = vterm;
1934 screen = vterm_obtain_screen(vterm);
1935 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1936 /* TODO: depends on 'encoding'. */
1937 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001938
1939 /* Vterm uses a default black background. Set it to white when
1940 * 'background' is "light". */
1941 if (*p_bg == 'l')
1942 {
1943 VTermColor fg, bg;
1944
1945 fg.red = fg.green = fg.blue = 0;
1946 bg.red = bg.green = bg.blue = 255;
1947 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1948 }
1949
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950 /* Required to initialize most things. */
1951 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001952
1953 /* Allow using alternate screen. */
1954 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001955}
1956
Bram Moolenaar21554412017-07-24 21:44:43 +02001957/*
1958 * Return the text to show for the buffer name and status.
1959 */
1960 char_u *
1961term_get_status_text(term_T *term)
1962{
1963 if (term->tl_status_text == NULL)
1964 {
1965 char_u *txt;
1966 size_t len;
1967
Bram Moolenaar6d819742017-08-06 14:57:49 +02001968 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001969 {
1970 if (term_job_running(term))
1971 txt = (char_u *)_("Terminal");
1972 else
1973 txt = (char_u *)_("Terminal-finished");
1974 }
1975 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001976 txt = term->tl_title;
1977 else if (term_job_running(term))
1978 txt = (char_u *)_("running");
1979 else
1980 txt = (char_u *)_("finished");
1981 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001982 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001983 if (term->tl_status_text != NULL)
1984 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1985 term->tl_buffer->b_fname, txt);
1986 }
1987 return term->tl_status_text;
1988}
1989
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001990/*
1991 * Mark references in jobs of terminals.
1992 */
1993 int
1994set_ref_in_term(int copyID)
1995{
1996 int abort = FALSE;
1997 term_T *term;
1998 typval_T tv;
1999
2000 for (term = first_term; term != NULL; term = term->tl_next)
2001 if (term->tl_job != NULL)
2002 {
2003 tv.v_type = VAR_JOB;
2004 tv.vval.v_job = term->tl_job;
2005 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2006 }
2007 return abort;
2008}
2009
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002010/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002011 * Get the buffer from the first argument in "argvars".
2012 * Returns NULL when the buffer is not for a terminal window.
2013 */
2014 static buf_T *
2015term_get_buf(typval_T *argvars)
2016{
2017 buf_T *buf;
2018
2019 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2020 ++emsg_off;
2021 buf = get_buf_tv(&argvars[0], FALSE);
2022 --emsg_off;
2023 if (buf == NULL || buf->b_term == NULL)
2024 return NULL;
2025 return buf;
2026}
2027
2028/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002029 * "term_getaltscreen(buf)" function
2030 */
2031 void
2032f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2033{
2034 buf_T *buf = term_get_buf(argvars);
2035
2036 if (buf == NULL)
2037 return;
2038 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2039}
2040
2041/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002042 * "term_getattr(attr, name)" function
2043 */
2044 void
2045f_term_getattr(typval_T *argvars, typval_T *rettv)
2046{
2047 int attr;
2048 size_t i;
2049 char_u *name;
2050
2051 static struct {
2052 char *name;
2053 int attr;
2054 } attrs[] = {
2055 {"bold", HL_BOLD},
2056 {"italic", HL_ITALIC},
2057 {"underline", HL_UNDERLINE},
2058 {"strike", HL_STANDOUT},
2059 {"reverse", HL_INVERSE},
2060 };
2061
2062 attr = get_tv_number(&argvars[0]);
2063 name = get_tv_string_chk(&argvars[1]);
2064 if (name == NULL)
2065 return;
2066
2067 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2068 if (STRCMP(name, attrs[i].name) == 0)
2069 {
2070 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2071 break;
2072 }
2073}
2074
2075/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002076 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002077 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002078 void
2079f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002080{
Bram Moolenaar97870002017-07-30 18:28:38 +02002081 buf_T *buf = term_get_buf(argvars);
2082 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002083
Bram Moolenaar97870002017-07-30 18:28:38 +02002084 if (rettv_list_alloc(rettv) == FAIL)
2085 return;
2086 if (buf == NULL)
2087 return;
2088
2089 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002090 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2091 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002092 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002093}
2094
2095/*
2096 * "term_getjob(buf)" function
2097 */
2098 void
2099f_term_getjob(typval_T *argvars, typval_T *rettv)
2100{
2101 buf_T *buf = term_get_buf(argvars);
2102
2103 rettv->v_type = VAR_JOB;
2104 rettv->vval.v_job = NULL;
2105 if (buf == NULL)
2106 return;
2107
2108 rettv->vval.v_job = buf->b_term->tl_job;
2109 if (rettv->vval.v_job != NULL)
2110 ++rettv->vval.v_job->jv_refcount;
2111}
2112
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002113 static int
2114get_row_number(typval_T *tv, term_T *term)
2115{
2116 if (tv->v_type == VAR_STRING
2117 && tv->vval.v_string != NULL
2118 && STRCMP(tv->vval.v_string, ".") == 0)
2119 return term->tl_cursor_pos.row;
2120 return (int)get_tv_number(tv) - 1;
2121}
2122
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002123/*
2124 * "term_getline(buf, row)" function
2125 */
2126 void
2127f_term_getline(typval_T *argvars, typval_T *rettv)
2128{
2129 buf_T *buf = term_get_buf(argvars);
2130 term_T *term;
2131 int row;
2132
2133 rettv->v_type = VAR_STRING;
2134 if (buf == NULL)
2135 return;
2136 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002137 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002138
2139 if (term->tl_vterm == NULL)
2140 {
2141 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2142
2143 /* vterm is finished, get the text from the buffer */
2144 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2145 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2146 }
2147 else
2148 {
2149 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2150 VTermRect rect;
2151 int len;
2152 char_u *p;
2153
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002154 if (row < 0 || row >= term->tl_rows)
2155 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002156 len = term->tl_cols * MB_MAXBYTES + 1;
2157 p = alloc(len);
2158 if (p == NULL)
2159 return;
2160 rettv->vval.v_string = p;
2161
2162 rect.start_col = 0;
2163 rect.end_col = term->tl_cols;
2164 rect.start_row = row;
2165 rect.end_row = row + 1;
2166 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2167 }
2168}
2169
2170/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002171 * "term_getscrolled(buf)" function
2172 */
2173 void
2174f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2175{
2176 buf_T *buf = term_get_buf(argvars);
2177
2178 if (buf == NULL)
2179 return;
2180 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2181}
2182
2183/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002184 * "term_getsize(buf)" function
2185 */
2186 void
2187f_term_getsize(typval_T *argvars, typval_T *rettv)
2188{
2189 buf_T *buf = term_get_buf(argvars);
2190 list_T *l;
2191
2192 if (rettv_list_alloc(rettv) == FAIL)
2193 return;
2194 if (buf == NULL)
2195 return;
2196
2197 l = rettv->vval.v_list;
2198 list_append_number(l, buf->b_term->tl_rows);
2199 list_append_number(l, buf->b_term->tl_cols);
2200}
2201
2202/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002203 * "term_getstatus(buf)" function
2204 */
2205 void
2206f_term_getstatus(typval_T *argvars, typval_T *rettv)
2207{
2208 buf_T *buf = term_get_buf(argvars);
2209 term_T *term;
2210 char_u val[100];
2211
2212 rettv->v_type = VAR_STRING;
2213 if (buf == NULL)
2214 return;
2215 term = buf->b_term;
2216
2217 if (term_job_running(term))
2218 STRCPY(val, "running");
2219 else
2220 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002221 if (term->tl_normal_mode)
2222 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002223 rettv->vval.v_string = vim_strsave(val);
2224}
2225
2226/*
2227 * "term_gettitle(buf)" function
2228 */
2229 void
2230f_term_gettitle(typval_T *argvars, typval_T *rettv)
2231{
2232 buf_T *buf = term_get_buf(argvars);
2233
2234 rettv->v_type = VAR_STRING;
2235 if (buf == NULL)
2236 return;
2237
2238 if (buf->b_term->tl_title != NULL)
2239 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2240}
2241
2242/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002243 * "term_gettty(buf)" function
2244 */
2245 void
2246f_term_gettty(typval_T *argvars, typval_T *rettv)
2247{
2248 buf_T *buf = term_get_buf(argvars);
2249 char_u *p;
2250
2251 rettv->v_type = VAR_STRING;
2252 if (buf == NULL)
2253 return;
2254 if (buf->b_term->tl_job != NULL)
2255 p = buf->b_term->tl_job->jv_tty_name;
2256 else
2257 p = buf->b_term->tl_tty_name;
2258 if (p != NULL)
2259 rettv->vval.v_string = vim_strsave(p);
2260}
2261
2262/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002263 * "term_list()" function
2264 */
2265 void
2266f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2267{
2268 term_T *tp;
2269 list_T *l;
2270
2271 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2272 return;
2273
2274 l = rettv->vval.v_list;
2275 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2276 if (tp != NULL && tp->tl_buffer != NULL)
2277 if (list_append_number(l,
2278 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2279 return;
2280}
2281
2282/*
2283 * "term_scrape(buf, row)" function
2284 */
2285 void
2286f_term_scrape(typval_T *argvars, typval_T *rettv)
2287{
2288 buf_T *buf = term_get_buf(argvars);
2289 VTermScreen *screen = NULL;
2290 VTermPos pos;
2291 list_T *l;
2292 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002293 char_u *p;
2294 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002295
2296 if (rettv_list_alloc(rettv) == FAIL)
2297 return;
2298 if (buf == NULL)
2299 return;
2300 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002301
2302 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002303 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002304
2305 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002306 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002307 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002308 p = NULL;
2309 line = NULL;
2310 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002311 else
2312 {
2313 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2314
2315 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2316 return;
2317 p = ml_get_buf(buf, lnum + 1, FALSE);
2318 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2319 }
2320
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002321 for (pos.col = 0; pos.col < term->tl_cols; )
2322 {
2323 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002324 int width;
2325 VTermScreenCellAttrs attrs;
2326 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002327 char_u rgb[8];
2328 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2329 int off = 0;
2330 int i;
2331
2332 if (screen == NULL)
2333 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002334 cellattr_T *cellattr;
2335 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002336
2337 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002338 if (pos.col >= line->sb_cols)
2339 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002340 cellattr = line->sb_cells + pos.col;
2341 width = cellattr->width;
2342 attrs = cellattr->attrs;
2343 fg = cellattr->fg;
2344 bg = cellattr->bg;
2345 len = MB_PTR2LEN(p);
2346 mch_memmove(mbs, p, len);
2347 mbs[len] = NUL;
2348 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002349 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002350 else
2351 {
2352 VTermScreenCell cell;
2353 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2354 break;
2355 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2356 {
2357 if (cell.chars[i] == 0)
2358 break;
2359 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2360 }
2361 mbs[off] = NUL;
2362 width = cell.width;
2363 attrs = cell.attrs;
2364 fg = cell.fg;
2365 bg = cell.bg;
2366 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002367 dcell = dict_alloc();
2368 list_append_dict(l, dcell);
2369
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002370 dict_add_nr_str(dcell, "chars", 0, mbs);
2371
2372 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002373 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002374 dict_add_nr_str(dcell, "fg", 0, rgb);
2375 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002376 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002377 dict_add_nr_str(dcell, "bg", 0, rgb);
2378
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002379 dict_add_nr_str(dcell, "attr",
2380 cell2attr(attrs, fg, bg), NULL);
2381 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002382
2383 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002384 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002385 ++pos.col;
2386 }
2387}
2388
2389/*
2390 * "term_sendkeys(buf, keys)" function
2391 */
2392 void
2393f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2394{
2395 buf_T *buf = term_get_buf(argvars);
2396 char_u *msg;
2397 term_T *term;
2398
2399 rettv->v_type = VAR_UNKNOWN;
2400 if (buf == NULL)
2401 return;
2402
2403 msg = get_tv_string_chk(&argvars[1]);
2404 if (msg == NULL)
2405 return;
2406 term = buf->b_term;
2407 if (term->tl_vterm == NULL)
2408 return;
2409
2410 while (*msg != NUL)
2411 {
2412 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2413 msg += MB_PTR2LEN(msg);
2414 }
2415
Bram Moolenaar6d819742017-08-06 14:57:49 +02002416 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002417 {
2418 /* TODO: only update once in a while. */
2419 update_screen(0);
2420 if (buf == curbuf)
2421 update_cursor(term, TRUE);
2422 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002423}
2424
2425/*
2426 * "term_start(command, options)" function
2427 */
2428 void
2429f_term_start(typval_T *argvars, typval_T *rettv)
2430{
2431 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002432 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002433
2434 if (cmd == NULL)
2435 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002436 init_job_options(&opt);
2437 /* TODO: allow more job options */
2438 if (argvars[1].v_type != VAR_UNKNOWN
2439 && get_job_options(&argvars[1], &opt,
2440 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002441 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002442 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002443 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002444 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002445 return;
2446
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002447 if (opt.jo_vertical)
2448 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002449 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002450
2451 if (curbuf->b_term != NULL)
2452 rettv->vval.v_number = curbuf->b_fnum;
2453}
2454
2455/*
2456 * "term_wait" function
2457 */
2458 void
2459f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2460{
2461 buf_T *buf = term_get_buf(argvars);
2462
2463 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002464 {
2465 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002466 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002467 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002468 if (buf->b_term->tl_job == NULL)
2469 {
2470 ch_log(NULL, "term_wait(): no job to wait for");
2471 return;
2472 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002473
2474 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002475 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002476 {
2477 /* The job is dead, keep reading channel I/O until the channel is
2478 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002479 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002480 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2481 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002482 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002483 parse_queued_messages();
2484 ui_delay(10L, FALSE);
2485 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002486 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002487 parse_queued_messages();
2488 }
2489 else
2490 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002491 long wait = 10L;
2492
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002493 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002494 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002495
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002496 /* Wait for some time for any channel I/O. */
2497 if (argvars[1].v_type != VAR_UNKNOWN)
2498 wait = get_tv_number(&argvars[1]);
2499 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002500 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002501
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002502 /* Flushing messages on channels is hopefully sufficient.
2503 * TODO: is there a better way? */
2504 parse_queued_messages();
2505 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002506}
2507
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002508# ifdef WIN3264
2509
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002510/**************************************
2511 * 2. MS-Windows implementation.
2512 */
2513
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002514#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2515#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2516
Bram Moolenaar8a773062017-07-24 22:29:21 +02002517void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002518void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002519void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002520BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2521void (*winpty_config_set_initial_size)(void*, int, int);
2522LPCWSTR (*winpty_conin_name)(void*);
2523LPCWSTR (*winpty_conout_name)(void*);
2524LPCWSTR (*winpty_conerr_name)(void*);
2525void (*winpty_free)(void*);
2526void (*winpty_config_free)(void*);
2527void (*winpty_spawn_config_free)(void*);
2528void (*winpty_error_free)(void*);
2529LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002530BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002531HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002532
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002533#define WINPTY_DLL "winpty.dll"
2534
2535static HINSTANCE hWinPtyDLL = NULL;
2536
2537 int
2538dyn_winpty_init(void)
2539{
2540 int i;
2541 static struct
2542 {
2543 char *name;
2544 FARPROC *ptr;
2545 } winpty_entry[] =
2546 {
2547 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2548 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2549 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2550 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2551 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2552 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2553 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2554 {"winpty_free", (FARPROC*)&winpty_free},
2555 {"winpty_open", (FARPROC*)&winpty_open},
2556 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2557 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2558 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2559 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002560 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002561 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002562 {NULL, NULL}
2563 };
2564
2565 /* No need to initialize twice. */
2566 if (hWinPtyDLL)
2567 return 1;
2568 /* Load winpty.dll */
2569 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2570 if (!hWinPtyDLL)
2571 {
2572 EMSG2(_(e_loadlib), WINPTY_DLL);
2573 return 0;
2574 }
2575 for (i = 0; winpty_entry[i].name != NULL
2576 && winpty_entry[i].ptr != NULL; ++i)
2577 {
2578 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2579 winpty_entry[i].name)) == NULL)
2580 {
2581 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2582 return 0;
2583 }
2584 }
2585
2586 return 1;
2587}
2588
2589/*
2590 * Create a new terminal of "rows" by "cols" cells.
2591 * Store a reference in "term".
2592 * Return OK or FAIL.
2593 */
2594 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002595term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002596{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002597 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002598 channel_T *channel = NULL;
2599 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002600 DWORD error;
2601 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2602 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002603 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002604 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605
2606 if (!dyn_winpty_init())
2607 return FAIL;
2608
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002609 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002610 if (p == NULL)
2611 return FAIL;
2612
2613 job = job_alloc();
2614 if (job == NULL)
2615 goto failed;
2616
2617 channel = add_channel();
2618 if (channel == NULL)
2619 goto failed;
2620
2621 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2622 if (term->tl_winpty_config == NULL)
2623 goto failed;
2624
2625 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2626 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2627 if (term->tl_winpty == NULL)
2628 goto failed;
2629
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002630 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002631 spawn_config = winpty_spawn_config_new(
2632 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2633 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2634 NULL,
2635 p,
2636 NULL,
2637 NULL,
2638 &winpty_err);
2639 if (spawn_config == NULL)
2640 goto failed;
2641
2642 channel = add_channel();
2643 if (channel == NULL)
2644 goto failed;
2645
2646 job = job_alloc();
2647 if (job == NULL)
2648 goto failed;
2649
2650 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2651 &child_thread_handle, &error, &winpty_err))
2652 goto failed;
2653
2654 channel_set_pipes(channel,
2655 (sock_T) CreateFileW(
2656 winpty_conin_name(term->tl_winpty),
2657 GENERIC_WRITE, 0, NULL,
2658 OPEN_EXISTING, 0, NULL),
2659 (sock_T) CreateFileW(
2660 winpty_conout_name(term->tl_winpty),
2661 GENERIC_READ, 0, NULL,
2662 OPEN_EXISTING, 0, NULL),
2663 (sock_T) CreateFileW(
2664 winpty_conerr_name(term->tl_winpty),
2665 GENERIC_READ, 0, NULL,
2666 OPEN_EXISTING, 0, NULL));
2667
2668 jo = CreateJobObject(NULL, NULL);
2669 if (jo == NULL)
2670 goto failed;
2671
2672 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002673 {
2674 /* Failed, switch the way to terminate process with TerminateProcess. */
2675 CloseHandle(jo);
2676 jo = NULL;
2677 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002678
2679 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002680 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002681
2682 create_vterm(term, rows, cols);
2683
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002684 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002685 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002686
2687 job->jv_channel = channel;
2688 job->jv_proc_info.hProcess = child_process_handle;
2689 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2690 job->jv_job_object = jo;
2691 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002692 sprintf(buf, "winpty://%lu",
2693 GetProcessId(winpty_agent_process(term->tl_winpty)));
2694 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002695 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002696 term->tl_job = job;
2697
2698 return OK;
2699
2700failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002701 if (spawn_config != NULL)
2702 winpty_spawn_config_free(spawn_config);
2703 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002704 if (channel != NULL)
2705 channel_clear(channel);
2706 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002707 {
2708 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002709 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002710 }
2711 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002712 if (jo != NULL)
2713 CloseHandle(jo);
2714 if (term->tl_winpty != NULL)
2715 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002716 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002717 if (term->tl_winpty_config != NULL)
2718 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002719 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002720 if (winpty_err != NULL)
2721 {
2722 char_u *msg = utf16_to_enc(
2723 (short_u *)winpty_error_msg(winpty_err), NULL);
2724
2725 EMSG(msg);
2726 winpty_error_free(winpty_err);
2727 }
2728 return FAIL;
2729}
2730
2731/*
2732 * Free the terminal emulator part of "term".
2733 */
2734 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002735term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002736{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002737 if (term->tl_winpty != NULL)
2738 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002739 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002740 if (term->tl_winpty_config != NULL)
2741 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002742 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002743 if (term->tl_vterm != NULL)
2744 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002745 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002746}
2747
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002748/*
2749 * Request size to terminal.
2750 */
2751 static void
2752term_report_winsize(term_T *term, int rows, int cols)
2753{
2754 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2755}
2756
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002757# else
2758
2759/**************************************
2760 * 3. Unix-like implementation.
2761 */
2762
2763/*
2764 * Create a new terminal of "rows" by "cols" cells.
2765 * Start job for "cmd".
2766 * Store the pointers in "term".
2767 * Return OK or FAIL.
2768 */
2769 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002770term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002771{
2772 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002773
2774 create_vterm(term, rows, cols);
2775
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002776 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002777 argvars[0].v_type = VAR_STRING;
2778 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002779
2780 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002781 if (term->tl_job != NULL)
2782 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002783
Bram Moolenaar61a66052017-07-22 18:39:00 +02002784 return term->tl_job != NULL
2785 && term->tl_job->jv_channel != NULL
2786 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002787}
2788
2789/*
2790 * Free the terminal emulator part of "term".
2791 */
2792 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002793term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002794{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002795 if (term->tl_vterm != NULL)
2796 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002797 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002798}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002799
2800/*
2801 * Request size to terminal.
2802 */
2803 static void
2804term_report_winsize(term_T *term, int rows, int cols)
2805{
2806 /* Use an ioctl() to report the new window size to the job. */
2807 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2808 {
2809 int fd = -1;
2810 int part;
2811
2812 for (part = PART_OUT; part < PART_COUNT; ++part)
2813 {
2814 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2815 if (isatty(fd))
2816 break;
2817 }
2818 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002819 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002820 }
2821}
2822
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002823# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002824
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002825#endif /* FEAT_TERMINAL */