blob: 63e1cec5f571b6deccba57315335cc17241c3de8 [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 Moolenaardd693ce2017-08-10 23:15:19 +020039 * - When using term_finish "open" have a way to specify how the window is to
40 * be opened. E.g. term_opencmd "10split buffer %d".
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020041 * - support different cursor shapes, colors and attributes
42 * - make term_getcursor() return type (none/block/bar/underline) and
43 * attributes (color, blink, etc.)
Bram Moolenaardd693ce2017-08-10 23:15:19 +020044 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020045 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020046 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020047 * For the GUI fill termios with default values, perhaps like pangoterm:
48 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
49 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020050 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020051 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020052 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020053 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020054 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020055 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
56 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020057 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020058 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020059 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020061 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020062 * terminal. Allow:
63 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
64 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
65 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
66 * Check that something is connected to the terminal.
67 * Test: "cat" reading from a file or buffer
68 * "ls" writing stdout to a file or buffer
69 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020070 * - support ":term NONE" to open a terminal with a pty but not running a job
71 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020072 * - if the job in the terminal does not support the mouse, we can use the
73 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020074 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
75 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020076 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020077 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020078 * - Copy text in the vterm to the Vim buffer once in a while, so that
79 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020080 */
81
82#include "vim.h"
83
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020084#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085
Bram Moolenaard5310982017-08-05 15:16:32 +020086#ifndef MIN
87# define MIN(x,y) ((x) < (y) ? (x) : (y))
88#endif
89#ifndef MAX
90# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020091#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093#include "libvterm/include/vterm.h"
94
Bram Moolenaar33a43be2017-08-06 21:36:22 +020095/* This is VTermScreenCell without the characters, thus much smaller. */
96typedef struct {
97 VTermScreenCellAttrs attrs;
98 char width;
99 VTermColor fg, bg;
100} cellattr_T;
101
Bram Moolenaard85f2712017-07-28 21:51:57 +0200102typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200103 int sb_cols; /* can differ per line */
104 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200105} sb_line_T;
106
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107/* typedef term_T in structs.h */
108struct terminal_S {
109 term_T *tl_next;
110
Bram Moolenaar423802d2017-07-30 16:52:24 +0200111 VTerm *tl_vterm;
112 job_T *tl_job;
113 buf_T *tl_buffer;
114
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200115 /* used when tl_job is NULL and only a pty was created */
116 int tl_tty_fd;
117 char_u *tl_tty_name;
118
Bram Moolenaar6d819742017-08-06 14:57:49 +0200119 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200120 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200121 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200122
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200124 void *tl_winpty_config;
125 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200126#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200127
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200128 /* last known vterm size */
129 int tl_rows;
130 int tl_cols;
131 /* vterm size does not follow window size */
132 int tl_rows_fixed;
133 int tl_cols_fixed;
134
Bram Moolenaar21554412017-07-24 21:44:43 +0200135 char_u *tl_title; /* NULL or allocated */
136 char_u *tl_status_text; /* NULL or allocated */
137
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200138 /* Range of screen rows to update. Zero based. */
139 int tl_dirty_row_start; /* -1 if nothing dirty */
140 int tl_dirty_row_end; /* row below last one to update */
141
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200143 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200144
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200145 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200146 int tl_cursor_visible;
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200147
148 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149};
150
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200151#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
152#define TMODE_LOOP 2 /* CTRL-W N used */
153
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154/*
155 * List of all active terminals.
156 */
157static term_T *first_term = NULL;
158
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200166static 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 +0200167static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200168static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200238 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
239 opt->jo_term_rows = rows;
240 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
241 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200242}
243
244 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200245term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200250 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200260 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200261 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200262
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200264 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200265 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 /* Create a new buffer in the current window. */
267 if (!can_abandon(curbuf, forceit))
268 {
269 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200270 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 return;
272 }
273 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
274 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200275 {
276 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200277 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 }
279 }
280 else if (opt->jo_hidden)
281 {
282 buf_T *buf;
283
284 /* Create a new buffer without a window. Make it the current buffer for
285 * a moment to be able to do the initialisations. */
286 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
287 BLN_NEW | BLN_LISTED);
288 if (buf == NULL || ml_open(buf) == FAIL)
289 {
290 vim_free(term);
291 return;
292 }
293 old_curbuf = curbuf;
294 --curbuf->b_nwindows;
295 curbuf = buf;
296 curwin->w_buffer = buf;
297 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200299 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 /* Open a new window or tab. */
302 split_ea.cmdidx = CMD_new;
303 split_ea.cmd = (char_u *)"new";
304 split_ea.arg = (char_u *)"";
305 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
306 {
307 split_ea.line2 = opt->jo_term_rows;
308 split_ea.addr_count = 1;
309 }
310 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
311 {
312 split_ea.line2 = opt->jo_term_cols;
313 split_ea.addr_count = 1;
314 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200315
Bram Moolenaarda43b612017-08-11 22:27:50 +0200316 ex_splitview(&split_ea);
317 if (curwin == old_curwin)
318 {
319 /* split failed */
320 vim_free(term);
321 return;
322 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200323 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200324 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200326
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200327 if (!opt->jo_hidden)
328 {
329 /* only one size was taken care of with :new, do the other one */
330 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
331 win_setheight(opt->jo_term_rows);
332 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
333 win_setwidth(opt->jo_term_cols);
334 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200337 term->tl_next = first_term;
338 first_term = term;
339
Bram Moolenaar293424c2017-07-26 23:11:01 +0200340 if (cmd == NULL || *cmd == NUL)
341 cmd = p_sh;
342
Bram Moolenaar78712a72017-08-05 14:50:12 +0200343 if (opt->jo_term_name != NULL)
344 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
345 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200346 {
347 int i;
348 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200349 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200350
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200351 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200352 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200353 /* Prepend a ! to the command name to avoid the buffer name equals
354 * the executable, otherwise ":w!" would overwrite it. */
355 if (i == 0)
356 vim_snprintf((char *)p, len, "!%s", cmd);
357 else
358 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200359 if (buflist_findname(p) == NULL)
360 {
361 curbuf->b_ffname = p;
362 break;
363 }
364 }
365 }
366 curbuf->b_fname = curbuf->b_ffname;
367
Bram Moolenaareb44a682017-08-03 22:44:55 +0200368 set_string_option_direct((char_u *)"buftype", -1,
369 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
370
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200371 /* Mark the buffer as not modifiable. It can only be made modifiable after
372 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200373 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200374
375 /* Set 'bufhidden' to "hide": allow closing the window. */
376 set_string_option_direct((char_u *)"bufhidden", -1,
377 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200378
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200379 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200380 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200381
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200382 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200383 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200384 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200385 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200386 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200387 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200388
389 if (old_curbuf != NULL)
390 {
391 --curbuf->b_nwindows;
392 curbuf = old_curbuf;
393 curwin->w_buffer = curbuf;
394 ++curbuf->b_nwindows;
395 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200396 }
397 else
398 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200399 buf_T *buf = curbuf;
400
Bram Moolenaard85f2712017-07-28 21:51:57 +0200401 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402 if (old_curbuf != NULL)
403 {
404 --curbuf->b_nwindows;
405 curbuf = old_curbuf;
406 curwin->w_buffer = curbuf;
407 ++curbuf->b_nwindows;
408 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200409
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200410 /* Wiping out the buffer will also close the window and call
411 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200412 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200413 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200414}
415
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200416/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200417 * ":terminal": open a terminal window and execute a job in it.
418 */
419 void
420ex_terminal(exarg_T *eap)
421{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200422 jobopt_T opt;
423 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200424
425 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200426
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200427 cmd = eap->arg;
428 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
429 {
430 char_u *p;
431
432 cmd += 2;
433 p = skiptowhite(cmd);
434 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
435 opt.jo_term_finish = 'c';
436 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
437 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200438 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
439 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200440 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
441 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 else
443 {
444 if (*p)
445 *p = NUL;
446 EMSG2(_("E181: Invalid attribute: %s"), cmd);
447 return;
448 }
449 cmd = skipwhite(p);
450 }
451
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200452 if (eap->addr_count == 2)
453 {
454 opt.jo_term_rows = eap->line1;
455 opt.jo_term_cols = eap->line2;
456 }
457 else if (eap->addr_count == 1)
458 {
459 if (cmdmod.split & WSP_VERT)
460 opt.jo_term_cols = eap->line2;
461 else
462 opt.jo_term_rows = eap->line2;
463 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200464
Bram Moolenaarda43b612017-08-11 22:27:50 +0200465 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200466}
467
468/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200469 * Free the scrollback buffer for "term".
470 */
471 static void
472free_scrollback(term_T *term)
473{
474 int i;
475
476 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
477 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
478 ga_clear(&term->tl_scrollback);
479}
480
481/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200482 * Free a terminal and everything it refers to.
483 * Kills the job if there is one.
484 * Called when wiping out a buffer.
485 */
486 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200487free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200488{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200489 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200490 term_T *tp;
491
492 if (term == NULL)
493 return;
494 if (first_term == term)
495 first_term = term->tl_next;
496 else
497 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
498 if (tp->tl_next == term)
499 {
500 tp->tl_next = term->tl_next;
501 break;
502 }
503
504 if (term->tl_job != NULL)
505 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200506 if (term->tl_job->jv_status != JOB_ENDED
507 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200508 job_stop(term->tl_job, NULL, "kill");
509 job_unref(term->tl_job);
510 }
511
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200512 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200513
514 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200515 vim_free(term->tl_title);
516 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200517 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200518 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200519}
520
521/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200522 * Write job output "msg[len]" to the vterm.
523 */
524 static void
525term_write_job_output(term_T *term, char_u *msg, size_t len)
526{
527 VTerm *vterm = term->tl_vterm;
528 char_u *p;
529 size_t done;
530 size_t len_now;
531
532 for (done = 0; done < len; done += len_now)
533 {
534 for (p = msg + done; p < msg + len; )
535 {
536 if (*p == NL)
537 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200538 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200539 }
540 len_now = p - msg - done;
541 vterm_input_write(vterm, (char *)msg + done, len_now);
542 if (p < msg + len && *p == NL)
543 {
544 /* Convert NL to CR-NL, that appears to work best. */
545 vterm_input_write(vterm, "\r\n", 2);
546 ++len_now;
547 }
548 }
549
550 /* this invokes the damage callbacks */
551 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
552}
553
Bram Moolenaar1c844932017-07-24 23:36:41 +0200554 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200555update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200556{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200557 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200558 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200559 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200560 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200561 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200562 if (term->tl_cursor_visible)
563 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200564 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200565#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200566 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200567 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200568#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200569 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200570}
571
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200572/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200573 * Invoked when "msg" output from a job was received. Write it to the terminal
574 * of "buffer".
575 */
576 void
577write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
578{
579 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200580 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200581
Bram Moolenaard85f2712017-07-28 21:51:57 +0200582 if (term->tl_vterm == NULL)
583 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200584 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200585 return;
586 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200587 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200588 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200589
Bram Moolenaar6d819742017-08-06 14:57:49 +0200590 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200591 {
592 /* TODO: only update once in a while. */
593 update_screen(0);
594 update_cursor(term, TRUE);
595 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200596}
597
598/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200599 * Send a mouse position and click to the vterm
600 */
601 static int
602term_send_mouse(VTerm *vterm, int button, int pressed)
603{
604 VTermModifier mod = VTERM_MOD_NONE;
605
606 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
607 mouse_col - W_WINCOL(curwin), mod);
608 vterm_mouse_button(vterm, button, pressed, mod);
609 return TRUE;
610}
611
612/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 * Convert typed key "c" into bytes to send to the job.
614 * Return the number of bytes in "buf".
615 */
616 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200617term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200618{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200619 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200620 VTermKey key = VTERM_KEY_NONE;
621 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200622 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200623
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200624 switch (c)
625 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200626 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200627 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200628 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
629 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200630 case K_DEL: key = VTERM_KEY_DEL; break;
631 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200632 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
633 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200634 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200635 case K_S_END: mod = VTERM_MOD_SHIFT;
636 key = VTERM_KEY_END; break;
637 case K_C_END: mod = VTERM_MOD_CTRL;
638 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200639 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
640 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
641 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
642 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
643 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
644 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
645 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
646 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
647 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
648 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
649 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
650 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
651 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200652 case K_S_HOME: mod = VTERM_MOD_SHIFT;
653 key = VTERM_KEY_HOME; break;
654 case K_C_HOME: mod = VTERM_MOD_CTRL;
655 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200656 case K_INS: key = VTERM_KEY_INS; break;
657 case K_K0: key = VTERM_KEY_KP_0; break;
658 case K_K1: key = VTERM_KEY_KP_1; break;
659 case K_K2: key = VTERM_KEY_KP_2; break;
660 case K_K3: key = VTERM_KEY_KP_3; break;
661 case K_K4: key = VTERM_KEY_KP_4; break;
662 case K_K5: key = VTERM_KEY_KP_5; break;
663 case K_K6: key = VTERM_KEY_KP_6; break;
664 case K_K7: key = VTERM_KEY_KP_7; break;
665 case K_K8: key = VTERM_KEY_KP_8; break;
666 case K_K9: key = VTERM_KEY_KP_9; break;
667 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
668 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
669 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
670 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
671 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
672 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
673 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
674 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
675 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
676 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
677 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
678 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
679 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200680 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
681 key = VTERM_KEY_LEFT; break;
682 case K_C_LEFT: mod = VTERM_MOD_CTRL;
683 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200684 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
685 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
686 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200687 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
688 key = VTERM_KEY_RIGHT; break;
689 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
690 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200691 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200692 case K_S_UP: mod = VTERM_MOD_SHIFT;
693 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200695
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
697 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
698 case K_MOUSELEFT: /* TODO */ return 0;
699 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200700
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200701 case K_LEFTMOUSE:
702 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
703 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
704 case K_LEFTRELEASE:
705 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
706 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
707 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
708 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
709 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
710 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
711 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
712 case K_X1MOUSE: /* TODO */ return 0;
713 case K_X1DRAG: /* TODO */ return 0;
714 case K_X1RELEASE: /* TODO */ return 0;
715 case K_X2MOUSE: /* TODO */ return 0;
716 case K_X2DRAG: /* TODO */ return 0;
717 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200718
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200719 case K_IGNORE: return 0;
720 case K_NOP: return 0;
721 case K_UNDO: return 0;
722 case K_HELP: return 0;
723 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
724 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
725 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
726 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
727 case K_SELECT: return 0;
728#ifdef FEAT_GUI
729 case K_VER_SCROLLBAR: return 0;
730 case K_HOR_SCROLLBAR: return 0;
731#endif
732#ifdef FEAT_GUI_TABLINE
733 case K_TABLINE: return 0;
734 case K_TABMENU: return 0;
735#endif
736#ifdef FEAT_NETBEANS_INTG
737 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
738#endif
739#ifdef FEAT_DND
740 case K_DROP: return 0;
741#endif
742#ifdef FEAT_AUTOCMD
743 case K_CURSORHOLD: return 0;
744#endif
745 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
746 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200747 }
748
749 /*
750 * Convert special keys to vterm keys:
751 * - Write keys to vterm: vterm_keyboard_key()
752 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200753 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200754 */
755 if (key != VTERM_KEY_NONE)
756 /* Special key, let vterm convert it. */
757 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200758 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200759 /* Normal character, let vterm convert it. */
760 vterm_keyboard_unichar(vterm, c, mod);
761
762 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200763 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200764}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200765
Bram Moolenaar938783d2017-07-16 20:13:26 +0200766/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200767 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200768 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200769 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200770term_job_running(term_T *term)
771{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200772 /* Also consider the job finished when the channel is closed, to avoid a
773 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200774 return term != NULL
775 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200776 && term->tl_job->jv_status == JOB_STARTED
777 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200778}
779
780/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200781 * Add the last line of the scrollback buffer to the buffer in the window.
782 */
783 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200784add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200785{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200786 buf_T *buf = term->tl_buffer;
787 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
788 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200789
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200790 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200791 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200792 {
793 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200794 curbuf = buf;
795 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200796 curbuf = curwin->w_buffer;
797 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200798}
799
800/*
801 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200802 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200803 */
804 static void
805move_terminal_to_buffer(term_T *term)
806{
807 win_T *wp;
808 int len;
809 int lines_skipped = 0;
810 VTermPos pos;
811 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200812 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200813 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200815 if (term->tl_vterm == NULL)
816 return;
817 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200818 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
819 {
820 len = 0;
821 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
822 if (vterm_screen_get_cell(screen, pos, &cell) != 0
823 && cell.chars[0] != NUL)
824 len = pos.col + 1;
825
826 if (len == 0)
827 ++lines_skipped;
828 else
829 {
830 while (lines_skipped > 0)
831 {
832 /* Line was skipped, add an empty line. */
833 --lines_skipped;
834 if (ga_grow(&term->tl_scrollback, 1) == OK)
835 {
836 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
837 + term->tl_scrollback.ga_len;
838
839 line->sb_cols = 0;
840 line->sb_cells = NULL;
841 ++term->tl_scrollback.ga_len;
842
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200843 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200844 }
845 }
846
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200847 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200848 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
849 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200850 garray_T ga;
851 int width;
852 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200853 + term->tl_scrollback.ga_len;
854
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200855 ga_init2(&ga, 1, 100);
856 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200857 {
858 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200859 {
860 width = 1;
861 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
862 if (ga_grow(&ga, 1) == OK)
863 ga.ga_len += mb_char2bytes(' ',
864 (char_u *)ga.ga_data + ga.ga_len);
865 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200866 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200867 {
868 width = cell.width;
869
870 p[pos.col].width = cell.width;
871 p[pos.col].attrs = cell.attrs;
872 p[pos.col].fg = cell.fg;
873 p[pos.col].bg = cell.bg;
874
875 if (ga_grow(&ga, MB_MAXBYTES) == OK)
876 {
877 int i;
878 int c;
879
880 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
881 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
882 (char_u *)ga.ga_data + ga.ga_len);
883 }
884 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200885 }
886 line->sb_cols = len;
887 line->sb_cells = p;
888 ++term->tl_scrollback.ga_len;
889
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200890 if (ga_grow(&ga, 1) == FAIL)
891 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
892 else
893 {
894 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
895 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
896 }
897 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200898 }
899 else
900 vim_free(p);
901 }
902 }
903
904 FOR_ALL_WINDOWS(wp)
905 {
906 if (wp->w_buffer == term->tl_buffer)
907 {
908 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
909 wp->w_cursor.col = 0;
910 wp->w_valid = 0;
911 redraw_win_later(wp, NOT_VALID);
912 }
913 }
914}
915
916 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200917set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200918{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200919 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200920 vim_free(term->tl_status_text);
921 term->tl_status_text = NULL;
922 if (term->tl_buffer == curbuf)
923 maketitle();
924}
925
926/*
927 * Called after the job if finished and Terminal mode is not active:
928 * Move the vterm contents into the scrollback buffer and free the vterm.
929 */
930 static void
931cleanup_vterm(term_T *term)
932{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200933 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200934 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200935 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200936 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937}
938
939/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200940 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941 * Suspends updating the terminal window.
942 */
943 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200944term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945{
946 term_T *term = curbuf->b_term;
947
948 /* Append the current terminal contents to the buffer. */
949 move_terminal_to_buffer(term);
950
Bram Moolenaar6d819742017-08-06 14:57:49 +0200951 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200952
Bram Moolenaar6d819742017-08-06 14:57:49 +0200953 /* Move the window cursor to the position of the cursor in the
954 * terminal. */
955 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
956 + term->tl_cursor_pos.row + 1;
957 check_cursor();
958 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200959
Bram Moolenaar6d819742017-08-06 14:57:49 +0200960 /* Display the same lines as in the terminal. */
961 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962}
963
964/*
965 * Returns TRUE if the current window contains a terminal and we are in
966 * Terminal-Normal mode.
967 */
968 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200969term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970{
971 term_T *term = curbuf->b_term;
972
Bram Moolenaar6d819742017-08-06 14:57:49 +0200973 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200974}
975
976/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200977 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978 * Restores updating the terminal window.
979 */
980 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200981term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982{
983 term_T *term = curbuf->b_term;
984 sb_line_T *line;
985 garray_T *gap;
986
987 /* Remove the terminal contents from the scrollback and the buffer. */
988 gap = &term->tl_scrollback;
989 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
990 {
991 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
992 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
993 vim_free(line->sb_cells);
994 --gap->ga_len;
995 if (gap->ga_len == 0)
996 break;
997 }
998 check_cursor();
999
Bram Moolenaar6d819742017-08-06 14:57:49 +02001000 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001001
1002 if (term->tl_channel_closed)
1003 cleanup_vterm(term);
1004 redraw_buf_and_status_later(curbuf, NOT_VALID);
1005}
1006
1007/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001008 * Get a key from the user without mapping.
1009 * TODO: use terminal mode mappings.
1010 */
1011 static int
1012term_vgetc()
1013{
1014 int c;
1015
1016 ++no_mapping;
1017 ++allow_keys;
1018 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001019#ifdef WIN3264
1020 ctrl_break_was_pressed = FALSE;
1021#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001022 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001023 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001024 --no_mapping;
1025 --allow_keys;
1026 return c;
1027}
1028
1029/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001030 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001031 * Return FAIL when the key needs to be handled in Normal mode.
1032 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001033 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001034 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001035send_keys_to_term(term_T *term, int c, int typed)
1036{
1037 char msg[KEY_BUF_LEN];
1038 size_t len;
1039 static int mouse_was_outside = FALSE;
1040 int dragging_outside = FALSE;
1041
1042 /* Catch keys that need to be handled as in Normal mode. */
1043 switch (c)
1044 {
1045 case NUL:
1046 case K_ZERO:
1047 if (typed)
1048 stuffcharReadbuff(c);
1049 return FAIL;
1050
1051 case K_IGNORE:
1052 return FAIL;
1053
1054 case K_LEFTDRAG:
1055 case K_MIDDLEDRAG:
1056 case K_RIGHTDRAG:
1057 case K_X1DRAG:
1058 case K_X2DRAG:
1059 dragging_outside = mouse_was_outside;
1060 /* FALLTHROUGH */
1061 case K_LEFTMOUSE:
1062 case K_LEFTMOUSE_NM:
1063 case K_LEFTRELEASE:
1064 case K_LEFTRELEASE_NM:
1065 case K_MIDDLEMOUSE:
1066 case K_MIDDLERELEASE:
1067 case K_RIGHTMOUSE:
1068 case K_RIGHTRELEASE:
1069 case K_X1MOUSE:
1070 case K_X1RELEASE:
1071 case K_X2MOUSE:
1072 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001073
1074 case K_MOUSEUP:
1075 case K_MOUSEDOWN:
1076 case K_MOUSELEFT:
1077 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001078 if (mouse_row < W_WINROW(curwin)
1079 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1080 || mouse_col < W_WINCOL(curwin)
1081 || mouse_col >= W_ENDCOL(curwin)
1082 || dragging_outside)
1083 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001084 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001085 if (typed)
1086 {
1087 stuffcharReadbuff(c);
1088 mouse_was_outside = TRUE;
1089 }
1090 return FAIL;
1091 }
1092 }
1093 if (typed)
1094 mouse_was_outside = FALSE;
1095
1096 /* Convert the typed key to a sequence of bytes for the job. */
1097 len = term_convert_key(term, c, msg);
1098 if (len > 0)
1099 /* TODO: if FAIL is returned, stop? */
1100 channel_send(term->tl_job->jv_channel, PART_IN,
1101 (char_u *)msg, (int)len, NULL);
1102
1103 return OK;
1104}
1105
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001106 static void
1107position_cursor(win_T *wp, VTermPos *pos)
1108{
1109 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1110 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1111 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1112}
1113
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001114/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001115 * Handle CTRL-W "": send register contents to the job.
1116 */
1117 static void
1118term_paste_register(int prev_c UNUSED)
1119{
1120 int c;
1121 list_T *l;
1122 listitem_T *item;
1123 long reglen = 0;
1124 int type;
1125
1126#ifdef FEAT_CMDL_INFO
1127 if (add_to_showcmd(prev_c))
1128 if (add_to_showcmd('"'))
1129 out_flush();
1130#endif
1131 c = term_vgetc();
1132#ifdef FEAT_CMDL_INFO
1133 clear_showcmd();
1134#endif
1135
1136 /* CTRL-W "= prompt for expression to evaluate. */
1137 if (c == '=' && get_expr_register() != '=')
1138 return;
1139
1140 l = (list_T *)get_reg_contents(c, GREG_LIST);
1141 if (l != NULL)
1142 {
1143 type = get_reg_type(c, &reglen);
1144 for (item = l->lv_first; item != NULL; item = item->li_next)
1145 {
1146 char_u *s = get_tv_string(&item->li_tv);
1147
1148 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1149 s, STRLEN(s), NULL);
1150 if (item->li_next != NULL || type == MLINE)
1151 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1152 (char_u *)"\r", 1, NULL);
1153 }
1154 list_free(l);
1155 }
1156}
1157
1158/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001159 * Returns TRUE if the current window contains a terminal and we are sending
1160 * keys to the job.
1161 */
1162 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001163term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001164{
1165 term_T *term = curbuf->b_term;
1166
1167 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001168 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001169 && term->tl_vterm != NULL
1170 && term_job_running(term);
1171}
1172
1173/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001174 * Wait for input and send it to the job.
1175 * Return when the start of a CTRL-W command is typed or anything else that
1176 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001177 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1178 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001179 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001180 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001181terminal_loop(void)
1182{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001183 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001184 int termkey = 0;
1185
1186 if (*curwin->w_p_tk != NUL)
1187 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001188 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001189
1190 for (;;)
1191 {
1192 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001193 /* Repeat redrawing in case a message is received while redrawing. */
1194 while (curwin->w_redr_type != 0)
1195 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001196 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001197
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001198 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001199 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001200 /* job finished while waiting for a character */
1201 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001202
Bram Moolenaarfae42832017-08-01 22:24:26 +02001203#ifdef UNIX
1204 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1205#endif
1206#ifdef WIN3264
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001207 /* On Windows we do not know whether the job can handle CTRL-C itself
1208 * or not. Therefore CTRL-C only sends a CTRL_C_EVENT to avoid killing
1209 * the shell instead of a command running in the shell.
1210 * Use CTRL-BREAK to kill the job. */
Bram Moolenaarfae42832017-08-01 22:24:26 +02001211 if (c == Ctrl_C)
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001212 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"int");
1213 if (ctrl_break_was_pressed)
1214 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001215#endif
1216
Bram Moolenaar69198192017-08-05 14:10:48 +02001217 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001218 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001219 int prev_c = c;
1220
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001221#ifdef FEAT_CMDL_INFO
1222 if (add_to_showcmd(c))
1223 out_flush();
1224#endif
1225 c = term_vgetc();
1226#ifdef FEAT_CMDL_INFO
1227 clear_showcmd();
1228#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001229 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001230 /* job finished while waiting for a character */
1231 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001232
Bram Moolenaar69198192017-08-05 14:10:48 +02001233 if (prev_c == Ctrl_BSL)
1234 {
1235 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001236 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001237 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1238 term_enter_normal_mode();
1239 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001240 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001241 /* Send both keys to the terminal. */
1242 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1243 }
1244 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001245 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001246 /* "CTRL-W .": send CTRL-W to the job */
1247 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001248 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001249 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001250 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001251 /* CTRL-W N : go to Terminal-Normal mode. */
1252 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001253 return FAIL;
1254 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001255 else if (c == '"')
1256 {
1257 term_paste_register(prev_c);
1258 continue;
1259 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001260 else if (termkey == 0 || c != termkey)
1261 {
1262 stuffcharReadbuff(Ctrl_W);
1263 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001264 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001265 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001266 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001267 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1268 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001269 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001270 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001271}
1272
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001273/*
1274 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001275 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001276 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001277 */
1278 void
1279term_job_ended(job_T *job)
1280{
1281 term_T *term;
1282 int did_one = FALSE;
1283
1284 for (term = first_term; term != NULL; term = term->tl_next)
1285 if (term->tl_job == job)
1286 {
1287 vim_free(term->tl_title);
1288 term->tl_title = NULL;
1289 vim_free(term->tl_status_text);
1290 term->tl_status_text = NULL;
1291 redraw_buf_and_status_later(term->tl_buffer, VALID);
1292 did_one = TRUE;
1293 }
1294 if (did_one)
1295 redraw_statuslines();
1296 if (curbuf->b_term != NULL)
1297 {
1298 if (curbuf->b_term->tl_job == job)
1299 maketitle();
1300 update_cursor(curbuf->b_term, TRUE);
1301 }
1302}
1303
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001304 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001305may_toggle_cursor(term_T *term)
1306{
1307 if (curbuf == term->tl_buffer)
1308 {
1309 if (term->tl_cursor_visible)
1310 cursor_on();
1311 else
1312 cursor_off();
1313 }
1314}
1315
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001316 static int
1317handle_damage(VTermRect rect, void *user)
1318{
1319 term_T *term = (term_T *)user;
1320
1321 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1322 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1323 redraw_buf_later(term->tl_buffer, NOT_VALID);
1324 return 1;
1325}
1326
1327 static int
1328handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1329{
1330 term_T *term = (term_T *)user;
1331
1332 /* TODO */
1333 redraw_buf_later(term->tl_buffer, NOT_VALID);
1334 return 1;
1335}
1336
1337 static int
1338handle_movecursor(
1339 VTermPos pos,
1340 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001341 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001342 void *user)
1343{
1344 term_T *term = (term_T *)user;
1345 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001346
1347 term->tl_cursor_pos = pos;
1348 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001349
1350 FOR_ALL_WINDOWS(wp)
1351 {
1352 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001353 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001355 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001356 {
1357 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001358 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001359 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001360
1361 return 1;
1362}
1363
Bram Moolenaar21554412017-07-24 21:44:43 +02001364 static int
1365handle_settermprop(
1366 VTermProp prop,
1367 VTermValue *value,
1368 void *user)
1369{
1370 term_T *term = (term_T *)user;
1371
1372 switch (prop)
1373 {
1374 case VTERM_PROP_TITLE:
1375 vim_free(term->tl_title);
1376 term->tl_title = vim_strsave((char_u *)value->string);
1377 vim_free(term->tl_status_text);
1378 term->tl_status_text = NULL;
1379 if (term == curbuf->b_term)
1380 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001381 break;
1382
1383 case VTERM_PROP_CURSORVISIBLE:
1384 term->tl_cursor_visible = value->boolean;
1385 may_toggle_cursor(term);
1386 out_flush();
1387 break;
1388
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001389 case VTERM_PROP_ALTSCREEN:
1390 /* TODO: do anything else? */
1391 term->tl_using_altscreen = value->boolean;
1392 break;
1393
Bram Moolenaar21554412017-07-24 21:44:43 +02001394 default:
1395 break;
1396 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001397 /* Always return 1, otherwise vterm doesn't store the value internally. */
1398 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001399}
1400
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001401/*
1402 * The job running in the terminal resized the terminal.
1403 */
1404 static int
1405handle_resize(int rows, int cols, void *user)
1406{
1407 term_T *term = (term_T *)user;
1408 win_T *wp;
1409
1410 term->tl_rows = rows;
1411 term->tl_cols = cols;
1412 FOR_ALL_WINDOWS(wp)
1413 {
1414 if (wp->w_buffer == term->tl_buffer)
1415 {
1416 win_setheight_win(rows, wp);
1417 win_setwidth_win(cols, wp);
1418 }
1419 }
1420
1421 redraw_buf_later(term->tl_buffer, NOT_VALID);
1422 return 1;
1423}
1424
Bram Moolenaard85f2712017-07-28 21:51:57 +02001425/*
1426 * Handle a line that is pushed off the top of the screen.
1427 */
1428 static int
1429handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1430{
1431 term_T *term = (term_T *)user;
1432
1433 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001434 if (ga_grow(&term->tl_scrollback, 1) == OK)
1435 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001436 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001437 int len = 0;
1438 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001439 int c;
1440 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001441 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001442 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001443
1444 /* do not store empty cells at the end */
1445 for (i = 0; i < cols; ++i)
1446 if (cells[i].chars[0] != 0)
1447 len = i + 1;
1448
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001449 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001450 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001451 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001452 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001453 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001454 for (col = 0; col < len; col += cells[col].width)
1455 {
1456 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1457 {
1458 ga.ga_len = 0;
1459 break;
1460 }
1461 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1462 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1463 (char_u *)ga.ga_data + ga.ga_len);
1464 p[col].width = cells[col].width;
1465 p[col].attrs = cells[col].attrs;
1466 p[col].fg = cells[col].fg;
1467 p[col].bg = cells[col].bg;
1468 }
1469 }
1470 if (ga_grow(&ga, 1) == FAIL)
1471 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1472 else
1473 {
1474 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1475 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1476 }
1477 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001478
1479 line = (sb_line_T *)term->tl_scrollback.ga_data
1480 + term->tl_scrollback.ga_len;
1481 line->sb_cols = len;
1482 line->sb_cells = p;
1483 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001484 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001485 }
1486 return 0; /* ignored */
1487}
1488
Bram Moolenaar21554412017-07-24 21:44:43 +02001489static VTermScreenCallbacks screen_callbacks = {
1490 handle_damage, /* damage */
1491 handle_moverect, /* moverect */
1492 handle_movecursor, /* movecursor */
1493 handle_settermprop, /* settermprop */
1494 NULL, /* bell */
1495 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001496 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001497 NULL /* sb_popline */
1498};
1499
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001500/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001501 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001502 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001503 */
1504 void
1505term_channel_closed(channel_T *ch)
1506{
1507 term_T *term;
1508 int did_one = FALSE;
1509
1510 for (term = first_term; term != NULL; term = term->tl_next)
1511 if (term->tl_job == ch->ch_job)
1512 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001513 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001514 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001515
Bram Moolenaard85f2712017-07-28 21:51:57 +02001516 vim_free(term->tl_title);
1517 term->tl_title = NULL;
1518 vim_free(term->tl_status_text);
1519 term->tl_status_text = NULL;
1520
Bram Moolenaar423802d2017-07-30 16:52:24 +02001521 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001522 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001523 {
1524 int fnum = term->tl_buffer->b_fnum;
1525
Bram Moolenaar423802d2017-07-30 16:52:24 +02001526 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001527
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001528 if (term->tl_finish == 'c')
1529 {
1530 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001531 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001532 curbuf = term->tl_buffer;
1533 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1534 break;
1535 }
1536 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1537 {
1538 char buf[50];
1539
1540 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001541 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001542 vim_snprintf(buf, sizeof(buf), "botright sbuf %d", fnum);
1543 do_cmdline_cmd((char_u *)buf);
1544 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001545 else
1546 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001547 }
1548
Bram Moolenaard85f2712017-07-28 21:51:57 +02001549 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001550 }
1551 if (did_one)
1552 {
1553 redraw_statuslines();
1554
1555 /* Need to break out of vgetc(). */
1556 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001557 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001558
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001559 term = curbuf->b_term;
1560 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001561 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001562 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001563 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001564 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001565 }
1566 }
1567}
1568
1569/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001570 * Reverse engineer the RGB value into a cterm color index.
1571 * First color is 1. Return 0 if no match found.
1572 */
1573 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001574color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001575{
1576 int red = color->red;
1577 int blue = color->blue;
1578 int green = color->green;
1579
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001580 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001581 if (red == 0)
1582 {
1583 if (green == 0)
1584 {
1585 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001586 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001587 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001588 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001589 }
1590 else if (green == 224)
1591 {
1592 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001593 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001594 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001595 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001596 }
1597 }
1598 else if (red == 224)
1599 {
1600 if (green == 0)
1601 {
1602 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001603 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001604 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001605 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001606 }
1607 else if (green == 224)
1608 {
1609 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001610 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001611 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001612 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001613 }
1614 }
1615 else if (red == 128)
1616 {
1617 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001618 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001619 }
1620 else if (red == 255)
1621 {
1622 if (green == 64)
1623 {
1624 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001625 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001626 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001627 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001628 }
1629 else if (green == 255)
1630 {
1631 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001632 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001633 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001634 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001635 }
1636 }
1637 else if (red == 64)
1638 {
1639 if (green == 64)
1640 {
1641 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001642 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001643 }
1644 else if (green == 255)
1645 {
1646 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001647 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001648 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001649 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001650 }
1651 }
1652 if (t_colors >= 256)
1653 {
1654 if (red == blue && red == green)
1655 {
1656 /* 24-color greyscale */
1657 static int cutoff[23] = {
1658 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1659 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1660 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1661 int i;
1662
1663 for (i = 0; i < 23; ++i)
1664 if (red < cutoff[i])
1665 return i + 233;
1666 return 256;
1667 }
1668
1669 /* 216-color cube */
1670 return 17 + ((red + 25) / 0x33) * 36
1671 + ((green + 25) / 0x33) * 6
1672 + (blue + 25) / 0x33;
1673 }
1674 return 0;
1675}
1676
1677/*
1678 * Convert the attributes of a vterm cell into an attribute index.
1679 */
1680 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001681cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001682{
1683 int attr = 0;
1684
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001685 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001686 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001687 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001688 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001689 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001690 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001691 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001692 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001693 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001694 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001695
1696#ifdef FEAT_GUI
1697 if (gui.in_use)
1698 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001699 guicolor_T fg, bg;
1700
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001701 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1702 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001703 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001704 }
1705 else
1706#endif
1707#ifdef FEAT_TERMGUICOLORS
1708 if (p_tgc)
1709 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001710 guicolor_T fg, bg;
1711
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001712 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1713 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001714
1715 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001716 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001717 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001718#endif
1719 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001720 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001721 int fg = color2index(&cellfg, TRUE, &bold);
1722 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001723
1724 /* with 8 colors set the bold attribute to get a bright foreground */
1725 if (bold == TRUE)
1726 attr |= HL_BOLD;
1727 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001728 }
1729 return 0;
1730}
1731
1732/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001733 * Called to update a window that contains an active terminal.
1734 * Returns FAIL when there is no terminal running in this window or in
1735 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001736 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001737 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001738term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001739{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001740 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001741 VTerm *vterm;
1742 VTermScreen *screen;
1743 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001744 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001745
Bram Moolenaar6d819742017-08-06 14:57:49 +02001746 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001747 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001748
Bram Moolenaard85f2712017-07-28 21:51:57 +02001749 vterm = term->tl_vterm;
1750 screen = vterm_obtain_screen(vterm);
1751 state = vterm_obtain_state(vterm);
1752
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001753 /*
1754 * If the window was resized a redraw will be triggered and we get here.
1755 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1756 */
1757 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1758 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001759 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001760 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1761 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1762 win_T *twp;
1763
1764 FOR_ALL_WINDOWS(twp)
1765 {
1766 /* When more than one window shows the same terminal, use the
1767 * smallest size. */
1768 if (twp->w_buffer == term->tl_buffer)
1769 {
1770 if (!term->tl_rows_fixed && rows > twp->w_height)
1771 rows = twp->w_height;
1772 if (!term->tl_cols_fixed && cols > twp->w_width)
1773 cols = twp->w_width;
1774 }
1775 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001776
1777 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001778 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001779 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001780 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001781 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001782
1783 /* The cursor may have been moved when resizing. */
1784 vterm_state_get_cursorpos(state, &pos);
1785 position_cursor(wp, &pos);
1786
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001787 /* TODO: Only redraw what changed. */
1788 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001789 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001790 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001791 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001792
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001793 if (pos.row < term->tl_rows)
1794 {
1795 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001796 {
1797 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001798 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001799
Bram Moolenaareeac6772017-07-23 15:48:37 +02001800 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1801 vim_memset(&cell, 0, sizeof(cell));
1802
1803 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001804 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001805 if (c == NUL)
1806 {
1807 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001808#if defined(FEAT_MBYTE)
1809 if (enc_utf8)
1810 ScreenLinesUC[off] = NUL;
1811#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001812 }
1813 else
1814 {
1815#if defined(FEAT_MBYTE)
1816 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001817 {
1818 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001819 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001820 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001821 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001822 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001823 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001824 if (enc_utf8)
1825 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001826 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001827#else
1828 ScreenLines[off] = c;
1829#endif
1830 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001831 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001832
1833 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001834 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001835 if (cell.width == 2)
1836 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001837 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001838#if defined(FEAT_MBYTE)
1839 if (enc_utf8)
1840 ScreenLinesUC[off] = NUL;
1841#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001842 ++pos.col;
1843 ++off;
1844 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001845 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001846 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001847 else
1848 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001849
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001850 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1851 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001852 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001853
1854 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001855}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001856
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001857/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001858 * Return TRUE if "wp" is a terminal window where the job has finished.
1859 */
1860 int
1861term_is_finished(buf_T *buf)
1862{
1863 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1864}
1865
1866/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001867 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001868 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001869 */
1870 int
1871term_show_buffer(buf_T *buf)
1872{
1873 term_T *term = buf->b_term;
1874
Bram Moolenaar6d819742017-08-06 14:57:49 +02001875 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001876}
1877
1878/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001879 * The current buffer is going to be changed. If there is terminal
1880 * highlighting remove it now.
1881 */
1882 void
1883term_change_in_curbuf(void)
1884{
1885 term_T *term = curbuf->b_term;
1886
1887 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1888 {
1889 free_scrollback(term);
1890 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001891
1892 /* The buffer is now like a normal buffer, it cannot be easily
1893 * abandoned when changed. */
1894 set_string_option_direct((char_u *)"buftype", -1,
1895 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001896 }
1897}
1898
1899/*
1900 * Get the screen attribute for a position in the buffer.
1901 */
1902 int
1903term_get_attr(buf_T *buf, linenr_T lnum, int col)
1904{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001905 term_T *term = buf->b_term;
1906 sb_line_T *line;
1907 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001908
Bram Moolenaar70229f92017-07-29 16:01:53 +02001909 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001910 return 0;
1911 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1912 if (col >= line->sb_cols)
1913 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001914 cellattr = line->sb_cells + col;
1915 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001916}
1917
1918/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001919 * Create a new vterm and initialize it.
1920 */
1921 static void
1922create_vterm(term_T *term, int rows, int cols)
1923{
1924 VTerm *vterm;
1925 VTermScreen *screen;
1926
1927 vterm = vterm_new(rows, cols);
1928 term->tl_vterm = vterm;
1929 screen = vterm_obtain_screen(vterm);
1930 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1931 /* TODO: depends on 'encoding'. */
1932 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001933
1934 /* Vterm uses a default black background. Set it to white when
1935 * 'background' is "light". */
1936 if (*p_bg == 'l')
1937 {
1938 VTermColor fg, bg;
1939
1940 fg.red = fg.green = fg.blue = 0;
1941 bg.red = bg.green = bg.blue = 255;
1942 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1943 }
1944
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001945 /* Required to initialize most things. */
1946 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001947
1948 /* Allow using alternate screen. */
1949 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950}
1951
Bram Moolenaar21554412017-07-24 21:44:43 +02001952/*
1953 * Return the text to show for the buffer name and status.
1954 */
1955 char_u *
1956term_get_status_text(term_T *term)
1957{
1958 if (term->tl_status_text == NULL)
1959 {
1960 char_u *txt;
1961 size_t len;
1962
Bram Moolenaar6d819742017-08-06 14:57:49 +02001963 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001964 {
1965 if (term_job_running(term))
1966 txt = (char_u *)_("Terminal");
1967 else
1968 txt = (char_u *)_("Terminal-finished");
1969 }
1970 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001971 txt = term->tl_title;
1972 else if (term_job_running(term))
1973 txt = (char_u *)_("running");
1974 else
1975 txt = (char_u *)_("finished");
1976 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001977 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001978 if (term->tl_status_text != NULL)
1979 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1980 term->tl_buffer->b_fname, txt);
1981 }
1982 return term->tl_status_text;
1983}
1984
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001985/*
1986 * Mark references in jobs of terminals.
1987 */
1988 int
1989set_ref_in_term(int copyID)
1990{
1991 int abort = FALSE;
1992 term_T *term;
1993 typval_T tv;
1994
1995 for (term = first_term; term != NULL; term = term->tl_next)
1996 if (term->tl_job != NULL)
1997 {
1998 tv.v_type = VAR_JOB;
1999 tv.vval.v_job = term->tl_job;
2000 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2001 }
2002 return abort;
2003}
2004
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002005/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002006 * Get the buffer from the first argument in "argvars".
2007 * Returns NULL when the buffer is not for a terminal window.
2008 */
2009 static buf_T *
2010term_get_buf(typval_T *argvars)
2011{
2012 buf_T *buf;
2013
2014 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2015 ++emsg_off;
2016 buf = get_buf_tv(&argvars[0], FALSE);
2017 --emsg_off;
2018 if (buf == NULL || buf->b_term == NULL)
2019 return NULL;
2020 return buf;
2021}
2022
2023/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002024 * "term_getaltscreen(buf)" function
2025 */
2026 void
2027f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2028{
2029 buf_T *buf = term_get_buf(argvars);
2030
2031 if (buf == NULL)
2032 return;
2033 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2034}
2035
2036/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002037 * "term_getattr(attr, name)" function
2038 */
2039 void
2040f_term_getattr(typval_T *argvars, typval_T *rettv)
2041{
2042 int attr;
2043 size_t i;
2044 char_u *name;
2045
2046 static struct {
2047 char *name;
2048 int attr;
2049 } attrs[] = {
2050 {"bold", HL_BOLD},
2051 {"italic", HL_ITALIC},
2052 {"underline", HL_UNDERLINE},
2053 {"strike", HL_STANDOUT},
2054 {"reverse", HL_INVERSE},
2055 };
2056
2057 attr = get_tv_number(&argvars[0]);
2058 name = get_tv_string_chk(&argvars[1]);
2059 if (name == NULL)
2060 return;
2061
2062 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2063 if (STRCMP(name, attrs[i].name) == 0)
2064 {
2065 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2066 break;
2067 }
2068}
2069
2070/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002071 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002072 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002073 void
2074f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002075{
Bram Moolenaar97870002017-07-30 18:28:38 +02002076 buf_T *buf = term_get_buf(argvars);
2077 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002078
Bram Moolenaar97870002017-07-30 18:28:38 +02002079 if (rettv_list_alloc(rettv) == FAIL)
2080 return;
2081 if (buf == NULL)
2082 return;
2083
2084 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002085 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
2086 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02002087 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002088}
2089
2090/*
2091 * "term_getjob(buf)" function
2092 */
2093 void
2094f_term_getjob(typval_T *argvars, typval_T *rettv)
2095{
2096 buf_T *buf = term_get_buf(argvars);
2097
2098 rettv->v_type = VAR_JOB;
2099 rettv->vval.v_job = NULL;
2100 if (buf == NULL)
2101 return;
2102
2103 rettv->vval.v_job = buf->b_term->tl_job;
2104 if (rettv->vval.v_job != NULL)
2105 ++rettv->vval.v_job->jv_refcount;
2106}
2107
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002108 static int
2109get_row_number(typval_T *tv, term_T *term)
2110{
2111 if (tv->v_type == VAR_STRING
2112 && tv->vval.v_string != NULL
2113 && STRCMP(tv->vval.v_string, ".") == 0)
2114 return term->tl_cursor_pos.row;
2115 return (int)get_tv_number(tv) - 1;
2116}
2117
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002118/*
2119 * "term_getline(buf, row)" function
2120 */
2121 void
2122f_term_getline(typval_T *argvars, typval_T *rettv)
2123{
2124 buf_T *buf = term_get_buf(argvars);
2125 term_T *term;
2126 int row;
2127
2128 rettv->v_type = VAR_STRING;
2129 if (buf == NULL)
2130 return;
2131 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002132 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002133
2134 if (term->tl_vterm == NULL)
2135 {
2136 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2137
2138 /* vterm is finished, get the text from the buffer */
2139 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2140 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2141 }
2142 else
2143 {
2144 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2145 VTermRect rect;
2146 int len;
2147 char_u *p;
2148
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002149 if (row < 0 || row >= term->tl_rows)
2150 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002151 len = term->tl_cols * MB_MAXBYTES + 1;
2152 p = alloc(len);
2153 if (p == NULL)
2154 return;
2155 rettv->vval.v_string = p;
2156
2157 rect.start_col = 0;
2158 rect.end_col = term->tl_cols;
2159 rect.start_row = row;
2160 rect.end_row = row + 1;
2161 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2162 }
2163}
2164
2165/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002166 * "term_getscrolled(buf)" function
2167 */
2168 void
2169f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2170{
2171 buf_T *buf = term_get_buf(argvars);
2172
2173 if (buf == NULL)
2174 return;
2175 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2176}
2177
2178/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002179 * "term_getsize(buf)" function
2180 */
2181 void
2182f_term_getsize(typval_T *argvars, typval_T *rettv)
2183{
2184 buf_T *buf = term_get_buf(argvars);
2185 list_T *l;
2186
2187 if (rettv_list_alloc(rettv) == FAIL)
2188 return;
2189 if (buf == NULL)
2190 return;
2191
2192 l = rettv->vval.v_list;
2193 list_append_number(l, buf->b_term->tl_rows);
2194 list_append_number(l, buf->b_term->tl_cols);
2195}
2196
2197/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002198 * "term_getstatus(buf)" function
2199 */
2200 void
2201f_term_getstatus(typval_T *argvars, typval_T *rettv)
2202{
2203 buf_T *buf = term_get_buf(argvars);
2204 term_T *term;
2205 char_u val[100];
2206
2207 rettv->v_type = VAR_STRING;
2208 if (buf == NULL)
2209 return;
2210 term = buf->b_term;
2211
2212 if (term_job_running(term))
2213 STRCPY(val, "running");
2214 else
2215 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002216 if (term->tl_normal_mode)
2217 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002218 rettv->vval.v_string = vim_strsave(val);
2219}
2220
2221/*
2222 * "term_gettitle(buf)" function
2223 */
2224 void
2225f_term_gettitle(typval_T *argvars, typval_T *rettv)
2226{
2227 buf_T *buf = term_get_buf(argvars);
2228
2229 rettv->v_type = VAR_STRING;
2230 if (buf == NULL)
2231 return;
2232
2233 if (buf->b_term->tl_title != NULL)
2234 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2235}
2236
2237/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002238 * "term_gettty(buf)" function
2239 */
2240 void
2241f_term_gettty(typval_T *argvars, typval_T *rettv)
2242{
2243 buf_T *buf = term_get_buf(argvars);
2244 char_u *p;
2245
2246 rettv->v_type = VAR_STRING;
2247 if (buf == NULL)
2248 return;
2249 if (buf->b_term->tl_job != NULL)
2250 p = buf->b_term->tl_job->jv_tty_name;
2251 else
2252 p = buf->b_term->tl_tty_name;
2253 if (p != NULL)
2254 rettv->vval.v_string = vim_strsave(p);
2255}
2256
2257/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002258 * "term_list()" function
2259 */
2260 void
2261f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2262{
2263 term_T *tp;
2264 list_T *l;
2265
2266 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2267 return;
2268
2269 l = rettv->vval.v_list;
2270 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2271 if (tp != NULL && tp->tl_buffer != NULL)
2272 if (list_append_number(l,
2273 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2274 return;
2275}
2276
2277/*
2278 * "term_scrape(buf, row)" function
2279 */
2280 void
2281f_term_scrape(typval_T *argvars, typval_T *rettv)
2282{
2283 buf_T *buf = term_get_buf(argvars);
2284 VTermScreen *screen = NULL;
2285 VTermPos pos;
2286 list_T *l;
2287 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002288 char_u *p;
2289 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002290
2291 if (rettv_list_alloc(rettv) == FAIL)
2292 return;
2293 if (buf == NULL)
2294 return;
2295 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002296
2297 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002298 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002299
2300 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002301 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002302 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002303 p = NULL;
2304 line = NULL;
2305 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002306 else
2307 {
2308 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2309
2310 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2311 return;
2312 p = ml_get_buf(buf, lnum + 1, FALSE);
2313 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2314 }
2315
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002316 for (pos.col = 0; pos.col < term->tl_cols; )
2317 {
2318 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002319 int width;
2320 VTermScreenCellAttrs attrs;
2321 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002322 char_u rgb[8];
2323 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2324 int off = 0;
2325 int i;
2326
2327 if (screen == NULL)
2328 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002329 cellattr_T *cellattr;
2330 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002331
2332 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333 if (pos.col >= line->sb_cols)
2334 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002335 cellattr = line->sb_cells + pos.col;
2336 width = cellattr->width;
2337 attrs = cellattr->attrs;
2338 fg = cellattr->fg;
2339 bg = cellattr->bg;
2340 len = MB_PTR2LEN(p);
2341 mch_memmove(mbs, p, len);
2342 mbs[len] = NUL;
2343 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002344 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002345 else
2346 {
2347 VTermScreenCell cell;
2348 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2349 break;
2350 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2351 {
2352 if (cell.chars[i] == 0)
2353 break;
2354 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2355 }
2356 mbs[off] = NUL;
2357 width = cell.width;
2358 attrs = cell.attrs;
2359 fg = cell.fg;
2360 bg = cell.bg;
2361 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002362 dcell = dict_alloc();
2363 list_append_dict(l, dcell);
2364
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002365 dict_add_nr_str(dcell, "chars", 0, mbs);
2366
2367 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002368 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002369 dict_add_nr_str(dcell, "fg", 0, rgb);
2370 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002371 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002372 dict_add_nr_str(dcell, "bg", 0, rgb);
2373
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002374 dict_add_nr_str(dcell, "attr",
2375 cell2attr(attrs, fg, bg), NULL);
2376 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002377
2378 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002379 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002380 ++pos.col;
2381 }
2382}
2383
2384/*
2385 * "term_sendkeys(buf, keys)" function
2386 */
2387 void
2388f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2389{
2390 buf_T *buf = term_get_buf(argvars);
2391 char_u *msg;
2392 term_T *term;
2393
2394 rettv->v_type = VAR_UNKNOWN;
2395 if (buf == NULL)
2396 return;
2397
2398 msg = get_tv_string_chk(&argvars[1]);
2399 if (msg == NULL)
2400 return;
2401 term = buf->b_term;
2402 if (term->tl_vterm == NULL)
2403 return;
2404
2405 while (*msg != NUL)
2406 {
2407 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2408 msg += MB_PTR2LEN(msg);
2409 }
2410
Bram Moolenaar6d819742017-08-06 14:57:49 +02002411 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002412 {
2413 /* TODO: only update once in a while. */
2414 update_screen(0);
2415 if (buf == curbuf)
2416 update_cursor(term, TRUE);
2417 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002418}
2419
2420/*
2421 * "term_start(command, options)" function
2422 */
2423 void
2424f_term_start(typval_T *argvars, typval_T *rettv)
2425{
2426 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002427 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002428
2429 if (cmd == NULL)
2430 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002431 init_job_options(&opt);
2432 /* TODO: allow more job options */
2433 if (argvars[1].v_type != VAR_UNKNOWN
2434 && get_job_options(&argvars[1], &opt,
2435 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002436 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002437 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN
Bram Moolenaarda43b612017-08-11 22:27:50 +02002438 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002439 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002440 return;
2441
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002442 if (opt.jo_vertical)
2443 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002444 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002445
2446 if (curbuf->b_term != NULL)
2447 rettv->vval.v_number = curbuf->b_fnum;
2448}
2449
2450/*
2451 * "term_wait" function
2452 */
2453 void
2454f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2455{
2456 buf_T *buf = term_get_buf(argvars);
2457
2458 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002459 {
2460 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002461 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002462 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002463 if (buf->b_term->tl_job == NULL)
2464 {
2465 ch_log(NULL, "term_wait(): no job to wait for");
2466 return;
2467 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002468
2469 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002470 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002471 {
2472 /* The job is dead, keep reading channel I/O until the channel is
2473 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002474 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002475 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2476 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002477 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002478 parse_queued_messages();
2479 ui_delay(10L, FALSE);
2480 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002481 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002482 parse_queued_messages();
2483 }
2484 else
2485 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002486 long wait = 10L;
2487
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002488 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002489 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002490
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002491 /* Wait for some time for any channel I/O. */
2492 if (argvars[1].v_type != VAR_UNKNOWN)
2493 wait = get_tv_number(&argvars[1]);
2494 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002495 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002496
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002497 /* Flushing messages on channels is hopefully sufficient.
2498 * TODO: is there a better way? */
2499 parse_queued_messages();
2500 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002501}
2502
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503# ifdef WIN3264
2504
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002505/**************************************
2506 * 2. MS-Windows implementation.
2507 */
2508
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002509#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2510#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2511
Bram Moolenaar8a773062017-07-24 22:29:21 +02002512void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002513void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002514void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002515BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2516void (*winpty_config_set_initial_size)(void*, int, int);
2517LPCWSTR (*winpty_conin_name)(void*);
2518LPCWSTR (*winpty_conout_name)(void*);
2519LPCWSTR (*winpty_conerr_name)(void*);
2520void (*winpty_free)(void*);
2521void (*winpty_config_free)(void*);
2522void (*winpty_spawn_config_free)(void*);
2523void (*winpty_error_free)(void*);
2524LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002525BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002526HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002527
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002528#define WINPTY_DLL "winpty.dll"
2529
2530static HINSTANCE hWinPtyDLL = NULL;
2531
2532 int
2533dyn_winpty_init(void)
2534{
2535 int i;
2536 static struct
2537 {
2538 char *name;
2539 FARPROC *ptr;
2540 } winpty_entry[] =
2541 {
2542 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2543 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2544 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2545 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2546 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2547 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2548 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2549 {"winpty_free", (FARPROC*)&winpty_free},
2550 {"winpty_open", (FARPROC*)&winpty_open},
2551 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2552 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2553 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2554 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002555 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002556 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002557 {NULL, NULL}
2558 };
2559
2560 /* No need to initialize twice. */
2561 if (hWinPtyDLL)
2562 return 1;
2563 /* Load winpty.dll */
2564 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2565 if (!hWinPtyDLL)
2566 {
2567 EMSG2(_(e_loadlib), WINPTY_DLL);
2568 return 0;
2569 }
2570 for (i = 0; winpty_entry[i].name != NULL
2571 && winpty_entry[i].ptr != NULL; ++i)
2572 {
2573 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2574 winpty_entry[i].name)) == NULL)
2575 {
2576 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2577 return 0;
2578 }
2579 }
2580
2581 return 1;
2582}
2583
2584/*
2585 * Create a new terminal of "rows" by "cols" cells.
2586 * Store a reference in "term".
2587 * Return OK or FAIL.
2588 */
2589 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002590term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002591{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002592 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002593 channel_T *channel = NULL;
2594 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002595 DWORD error;
2596 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2597 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002598 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002599 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002600
2601 if (!dyn_winpty_init())
2602 return FAIL;
2603
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002604 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002605 if (p == NULL)
2606 return FAIL;
2607
2608 job = job_alloc();
2609 if (job == NULL)
2610 goto failed;
2611
2612 channel = add_channel();
2613 if (channel == NULL)
2614 goto failed;
2615
2616 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2617 if (term->tl_winpty_config == NULL)
2618 goto failed;
2619
2620 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2621 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2622 if (term->tl_winpty == NULL)
2623 goto failed;
2624
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002625 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002626 spawn_config = winpty_spawn_config_new(
2627 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2628 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2629 NULL,
2630 p,
2631 NULL,
2632 NULL,
2633 &winpty_err);
2634 if (spawn_config == NULL)
2635 goto failed;
2636
2637 channel = add_channel();
2638 if (channel == NULL)
2639 goto failed;
2640
2641 job = job_alloc();
2642 if (job == NULL)
2643 goto failed;
2644
2645 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2646 &child_thread_handle, &error, &winpty_err))
2647 goto failed;
2648
2649 channel_set_pipes(channel,
2650 (sock_T) CreateFileW(
2651 winpty_conin_name(term->tl_winpty),
2652 GENERIC_WRITE, 0, NULL,
2653 OPEN_EXISTING, 0, NULL),
2654 (sock_T) CreateFileW(
2655 winpty_conout_name(term->tl_winpty),
2656 GENERIC_READ, 0, NULL,
2657 OPEN_EXISTING, 0, NULL),
2658 (sock_T) CreateFileW(
2659 winpty_conerr_name(term->tl_winpty),
2660 GENERIC_READ, 0, NULL,
2661 OPEN_EXISTING, 0, NULL));
2662
2663 jo = CreateJobObject(NULL, NULL);
2664 if (jo == NULL)
2665 goto failed;
2666
2667 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002668 {
2669 /* Failed, switch the way to terminate process with TerminateProcess. */
2670 CloseHandle(jo);
2671 jo = NULL;
2672 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002673
2674 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002675 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002676
2677 create_vterm(term, rows, cols);
2678
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002679 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002680 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002681
2682 job->jv_channel = channel;
2683 job->jv_proc_info.hProcess = child_process_handle;
2684 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2685 job->jv_job_object = jo;
2686 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002687 sprintf(buf, "winpty://%lu",
2688 GetProcessId(winpty_agent_process(term->tl_winpty)));
2689 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002690 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002691 term->tl_job = job;
2692
2693 return OK;
2694
2695failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002696 if (spawn_config != NULL)
2697 winpty_spawn_config_free(spawn_config);
2698 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002699 if (channel != NULL)
2700 channel_clear(channel);
2701 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002702 {
2703 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002704 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002705 }
2706 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002707 if (jo != NULL)
2708 CloseHandle(jo);
2709 if (term->tl_winpty != NULL)
2710 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002711 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002712 if (term->tl_winpty_config != NULL)
2713 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002714 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002715 if (winpty_err != NULL)
2716 {
2717 char_u *msg = utf16_to_enc(
2718 (short_u *)winpty_error_msg(winpty_err), NULL);
2719
2720 EMSG(msg);
2721 winpty_error_free(winpty_err);
2722 }
2723 return FAIL;
2724}
2725
2726/*
2727 * Free the terminal emulator part of "term".
2728 */
2729 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002730term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002731{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002732 if (term->tl_winpty != NULL)
2733 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002734 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002735 if (term->tl_winpty_config != NULL)
2736 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002737 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002738 if (term->tl_vterm != NULL)
2739 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002740 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002741}
2742
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002743/*
2744 * Request size to terminal.
2745 */
2746 static void
2747term_report_winsize(term_T *term, int rows, int cols)
2748{
2749 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2750}
2751
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002752# else
2753
2754/**************************************
2755 * 3. Unix-like implementation.
2756 */
2757
2758/*
2759 * Create a new terminal of "rows" by "cols" cells.
2760 * Start job for "cmd".
2761 * Store the pointers in "term".
2762 * Return OK or FAIL.
2763 */
2764 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002765term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002766{
2767 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002768
2769 create_vterm(term, rows, cols);
2770
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002771 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002772 argvars[0].v_type = VAR_STRING;
2773 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002774
2775 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002776 if (term->tl_job != NULL)
2777 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002778
Bram Moolenaar61a66052017-07-22 18:39:00 +02002779 return term->tl_job != NULL
2780 && term->tl_job->jv_channel != NULL
2781 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002782}
2783
2784/*
2785 * Free the terminal emulator part of "term".
2786 */
2787 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002788term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002789{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002790 if (term->tl_vterm != NULL)
2791 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002792 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002793}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002794
2795/*
2796 * Request size to terminal.
2797 */
2798 static void
2799term_report_winsize(term_T *term, int rows, int cols)
2800{
2801 /* Use an ioctl() to report the new window size to the job. */
2802 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2803 {
2804 int fd = -1;
2805 int part;
2806
2807 for (part = PART_OUT; part < PART_COUNT; ++part)
2808 {
2809 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2810 if (isatty(fd))
2811 break;
2812 }
2813 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002814 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002815 }
2816}
2817
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002818# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002819
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002820#endif /* FEAT_TERMINAL */