blob: 7d26deb142b2eba61aa4dba1f2fefe4dca48a24f [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 Moolenaard8dc1792017-08-03 11:55:21 +020039 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020040 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020041 * - For the scrollback buffer store lines in the buffer, only attributes in
42 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020043 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020044 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020045 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
46 * job finishes).
47 * - add option values to the command:
48 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020049 * - support different cursor shapes, colors and attributes
50 * - make term_getcursor() return type (none/block/bar/underline) and
51 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020052 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020053 * For the GUI fill termios with default values, perhaps like pangoterm:
54 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
55 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020059 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020060 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020061 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
62 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020067 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020068 * terminal. Allow:
69 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
70 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
71 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
72 * Check that something is connected to the terminal.
73 * Test: "cat" reading from a file or buffer
74 * "ls" writing stdout to a file or buffer
75 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020076 * - support ":term NONE" to open a terminal with a pty but not running a job
77 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020078 * - if the job in the terminal does not support the mouse, we can use the
79 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
81 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020082 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020083 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020084 * - Copy text in the vterm to the Vim buffer once in a while, so that
85 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086 */
87
88#include "vim.h"
89
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020090#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaard5310982017-08-05 15:16:32 +020092#ifndef MIN
93# define MIN(x,y) ((x) < (y) ? (x) : (y))
94#endif
95#ifndef MAX
96# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020097#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020098
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020099#include "libvterm/include/vterm.h"
100
Bram Moolenaard85f2712017-07-28 21:51:57 +0200101typedef struct sb_line_S {
102 int sb_cols; /* can differ per line */
103 VTermScreenCell *sb_cells; /* allocated */
104} sb_line_T;
105
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200106/* typedef term_T in structs.h */
107struct terminal_S {
108 term_T *tl_next;
109
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 VTerm *tl_vterm;
111 job_T *tl_job;
112 buf_T *tl_buffer;
113
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200114 /* used when tl_job is NULL and only a pty was created */
115 int tl_tty_fd;
116 char_u *tl_tty_name;
117
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200118 int tl_terminal_mode; /* 0, TMODE_ONCE or TMODE_LOOP */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200119 int tl_channel_closed;
120
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200122 void *tl_winpty_config;
123 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200124#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 /* last known vterm size */
127 int tl_rows;
128 int tl_cols;
129 /* vterm size does not follow window size */
130 int tl_rows_fixed;
131 int tl_cols_fixed;
132
Bram Moolenaar21554412017-07-24 21:44:43 +0200133 char_u *tl_title; /* NULL or allocated */
134 char_u *tl_status_text; /* NULL or allocated */
135
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200136 /* Range of screen rows to update. Zero based. */
137 int tl_dirty_row_start; /* -1 if nothing dirty */
138 int tl_dirty_row_end; /* row below last one to update */
139
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200143 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200144 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145};
146
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200147#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
148#define TMODE_LOOP 2 /* CTRL-W N used */
149
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150/*
151 * List of all active terminals.
152 */
153static term_T *first_term = NULL;
154
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162static 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 +0200163static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200164static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200165
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200166/**************************************
167 * 1. Generic code for all systems.
168 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169
170/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171 * Determine the terminal size from 'termsize' and the current window.
172 * Assumes term->tl_rows and term->tl_cols are zero.
173 */
174 static void
175set_term_and_win_size(term_T *term)
176{
177 if (*curwin->w_p_tms != NUL)
178 {
179 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
180
181 term->tl_rows = atoi((char *)curwin->w_p_tms);
182 term->tl_cols = atoi((char *)p);
183 }
184 if (term->tl_rows == 0)
185 term->tl_rows = curwin->w_height;
186 else
187 {
188 win_setheight_win(term->tl_rows, curwin);
189 term->tl_rows_fixed = TRUE;
190 }
191 if (term->tl_cols == 0)
192 term->tl_cols = curwin->w_width;
193 else
194 {
195 win_setwidth_win(term->tl_cols, curwin);
196 term->tl_cols_fixed = TRUE;
197 }
198}
199
200/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200201 * Initialize job options for a terminal job.
202 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200203 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 static void
205init_job_options(jobopt_T *opt)
206{
207 clear_job_options(opt);
208
209 opt->jo_mode = MODE_RAW;
210 opt->jo_out_mode = MODE_RAW;
211 opt->jo_err_mode = MODE_RAW;
212 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
213
214 opt->jo_io[PART_OUT] = JIO_BUFFER;
215 opt->jo_io[PART_ERR] = JIO_BUFFER;
216 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
217
218 opt->jo_modifiable[PART_OUT] = 0;
219 opt->jo_modifiable[PART_ERR] = 0;
220 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
221
222 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
223}
224
225/*
226 * Set job options mandatory for a terminal job.
227 */
228 static void
229setup_job_options(jobopt_T *opt, int rows, int cols)
230{
231 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
232 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
233 opt->jo_pty = TRUE;
234 opt->jo_term_rows = rows;
235 opt->jo_term_cols = cols;
236}
237
238 static void
239term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200240{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 exarg_T split_ea;
242 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200243 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
245 if (check_restricted() || check_secure())
246 return;
247
248 term = (term_T *)alloc_clear(sizeof(term_T));
249 if (term == NULL)
250 return;
251 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200252 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200253 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 /* Open a new window or tab. */
256 vim_memset(&split_ea, 0, sizeof(split_ea));
257 split_ea.cmdidx = CMD_new;
258 split_ea.cmd = (char_u *)"new";
259 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200260 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
261 {
262 split_ea.line2 = opt->jo_term_rows;
263 split_ea.addr_count = 1;
264 }
265 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
266 {
267 split_ea.line2 = opt->jo_term_cols;
268 split_ea.addr_count = 1;
269 }
270
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271 ex_splitview(&split_ea);
272 if (curwin == old_curwin)
273 {
274 /* split failed */
275 vim_free(term);
276 return;
277 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200278 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200279 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200280
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200281 /* only one size was taken care of with :new, do the other one */
282 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
283 win_setheight(opt->jo_term_rows);
284 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
285 win_setwidth(opt->jo_term_cols);
286
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200287 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200288 term->tl_next = first_term;
289 first_term = term;
290
Bram Moolenaar293424c2017-07-26 23:11:01 +0200291 if (cmd == NULL || *cmd == NUL)
292 cmd = p_sh;
293
Bram Moolenaar78712a72017-08-05 14:50:12 +0200294 if (opt->jo_term_name != NULL)
295 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
296 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200297 {
298 int i;
299 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200300 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200301
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200302 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200303 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200304 /* Prepend a ! to the command name to avoid the buffer name equals
305 * the executable, otherwise ":w!" would overwrite it. */
306 if (i == 0)
307 vim_snprintf((char *)p, len, "!%s", cmd);
308 else
309 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310 if (buflist_findname(p) == NULL)
311 {
312 curbuf->b_ffname = p;
313 break;
314 }
315 }
316 }
317 curbuf->b_fname = curbuf->b_ffname;
318
Bram Moolenaareb44a682017-08-03 22:44:55 +0200319 set_string_option_direct((char_u *)"buftype", -1,
320 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
321
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200322 /* Mark the buffer as not modifiable. It can only be made modifiable after
323 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200324 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200325
326 /* Set 'bufhidden' to "hide": allow closing the window. */
327 set_string_option_direct((char_u *)"bufhidden", -1,
328 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200331 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200334 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335 {
336 /* store the size we ended up with */
337 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 }
339 else
340 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200341 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200342
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 /* Wiping out the buffer will also close the window and call
344 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200345 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200347}
348
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200349/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200350 * ":terminal": open a terminal window and execute a job in it.
351 */
352 void
353ex_terminal(exarg_T *eap)
354{
355 jobopt_T opt;
356
357 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200358
359 if (eap->addr_count == 2)
360 {
361 opt.jo_term_rows = eap->line1;
362 opt.jo_term_cols = eap->line2;
363 }
364 else if (eap->addr_count == 1)
365 {
366 if (cmdmod.split & WSP_VERT)
367 opt.jo_term_cols = eap->line2;
368 else
369 opt.jo_term_rows = eap->line2;
370 }
371 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200372
373 term_start(eap->arg, &opt);
374}
375
376/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200377 * Free the scrollback buffer for "term".
378 */
379 static void
380free_scrollback(term_T *term)
381{
382 int i;
383
384 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
385 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
386 ga_clear(&term->tl_scrollback);
387}
388
389/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200390 * Free a terminal and everything it refers to.
391 * Kills the job if there is one.
392 * Called when wiping out a buffer.
393 */
394 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200395free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200396{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200397 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200398 term_T *tp;
399
400 if (term == NULL)
401 return;
402 if (first_term == term)
403 first_term = term->tl_next;
404 else
405 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
406 if (tp->tl_next == term)
407 {
408 tp->tl_next = term->tl_next;
409 break;
410 }
411
412 if (term->tl_job != NULL)
413 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200414 if (term->tl_job->jv_status != JOB_ENDED
415 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200416 job_stop(term->tl_job, NULL, "kill");
417 job_unref(term->tl_job);
418 }
419
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200420 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200421
422 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200423 vim_free(term->tl_title);
424 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200425 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200426 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200427}
428
429/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200430 * Write job output "msg[len]" to the vterm.
431 */
432 static void
433term_write_job_output(term_T *term, char_u *msg, size_t len)
434{
435 VTerm *vterm = term->tl_vterm;
436 char_u *p;
437 size_t done;
438 size_t len_now;
439
440 for (done = 0; done < len; done += len_now)
441 {
442 for (p = msg + done; p < msg + len; )
443 {
444 if (*p == NL)
445 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200446 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200447 }
448 len_now = p - msg - done;
449 vterm_input_write(vterm, (char *)msg + done, len_now);
450 if (p < msg + len && *p == NL)
451 {
452 /* Convert NL to CR-NL, that appears to work best. */
453 vterm_input_write(vterm, "\r\n", 2);
454 ++len_now;
455 }
456 }
457
458 /* this invokes the damage callbacks */
459 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
460}
461
Bram Moolenaar1c844932017-07-24 23:36:41 +0200462 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200463update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200464{
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200465 if (term->tl_terminal_mode != 0)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200466 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200467 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200468 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200469 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200470 if (term->tl_cursor_visible)
471 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200472 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200473#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200474 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200475 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200476#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200477 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200478}
479
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200480/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200481 * Invoked when "msg" output from a job was received. Write it to the terminal
482 * of "buffer".
483 */
484 void
485write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
486{
487 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200489
Bram Moolenaard85f2712017-07-28 21:51:57 +0200490 if (term->tl_vterm == NULL)
491 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200492 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200493 return;
494 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200495 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200497
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200498 if (term->tl_terminal_mode == 0)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200499 {
500 /* TODO: only update once in a while. */
501 update_screen(0);
502 update_cursor(term, TRUE);
503 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200504}
505
506/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200507 * Send a mouse position and click to the vterm
508 */
509 static int
510term_send_mouse(VTerm *vterm, int button, int pressed)
511{
512 VTermModifier mod = VTERM_MOD_NONE;
513
514 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
515 mouse_col - W_WINCOL(curwin), mod);
516 vterm_mouse_button(vterm, button, pressed, mod);
517 return TRUE;
518}
519
520/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200521 * Convert typed key "c" into bytes to send to the job.
522 * Return the number of bytes in "buf".
523 */
524 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200525term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200526{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200527 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200528 VTermKey key = VTERM_KEY_NONE;
529 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200530 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200531
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 switch (c)
533 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200535 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200536 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
537 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200538 case K_DEL: key = VTERM_KEY_DEL; break;
539 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200540 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
541 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200543 case K_S_END: mod = VTERM_MOD_SHIFT;
544 key = VTERM_KEY_END; break;
545 case K_C_END: mod = VTERM_MOD_CTRL;
546 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
548 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
549 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
550 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
551 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
552 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
553 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
554 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
555 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
556 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
557 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
558 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
559 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200560 case K_S_HOME: mod = VTERM_MOD_SHIFT;
561 key = VTERM_KEY_HOME; break;
562 case K_C_HOME: mod = VTERM_MOD_CTRL;
563 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564 case K_INS: key = VTERM_KEY_INS; break;
565 case K_K0: key = VTERM_KEY_KP_0; break;
566 case K_K1: key = VTERM_KEY_KP_1; break;
567 case K_K2: key = VTERM_KEY_KP_2; break;
568 case K_K3: key = VTERM_KEY_KP_3; break;
569 case K_K4: key = VTERM_KEY_KP_4; break;
570 case K_K5: key = VTERM_KEY_KP_5; break;
571 case K_K6: key = VTERM_KEY_KP_6; break;
572 case K_K7: key = VTERM_KEY_KP_7; break;
573 case K_K8: key = VTERM_KEY_KP_8; break;
574 case K_K9: key = VTERM_KEY_KP_9; break;
575 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
576 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
577 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
578 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
579 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
580 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
581 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
582 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
583 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
584 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
585 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
586 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
587 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200588 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
589 key = VTERM_KEY_LEFT; break;
590 case K_C_LEFT: mod = VTERM_MOD_CTRL;
591 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200592 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
593 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
594 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200595 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
596 key = VTERM_KEY_RIGHT; break;
597 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
598 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200600 case K_S_UP: mod = VTERM_MOD_SHIFT;
601 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200602 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200603
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200604 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
605 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
606 case K_MOUSELEFT: /* TODO */ return 0;
607 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200608
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200609 case K_LEFTMOUSE:
610 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
611 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
612 case K_LEFTRELEASE:
613 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
614 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
615 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
616 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
617 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
618 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
619 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
620 case K_X1MOUSE: /* TODO */ return 0;
621 case K_X1DRAG: /* TODO */ return 0;
622 case K_X1RELEASE: /* TODO */ return 0;
623 case K_X2MOUSE: /* TODO */ return 0;
624 case K_X2DRAG: /* TODO */ return 0;
625 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200626
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200627 case K_IGNORE: return 0;
628 case K_NOP: return 0;
629 case K_UNDO: return 0;
630 case K_HELP: return 0;
631 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
632 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
633 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
634 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
635 case K_SELECT: return 0;
636#ifdef FEAT_GUI
637 case K_VER_SCROLLBAR: return 0;
638 case K_HOR_SCROLLBAR: return 0;
639#endif
640#ifdef FEAT_GUI_TABLINE
641 case K_TABLINE: return 0;
642 case K_TABMENU: return 0;
643#endif
644#ifdef FEAT_NETBEANS_INTG
645 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
646#endif
647#ifdef FEAT_DND
648 case K_DROP: return 0;
649#endif
650#ifdef FEAT_AUTOCMD
651 case K_CURSORHOLD: return 0;
652#endif
653 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
654 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 }
656
657 /*
658 * Convert special keys to vterm keys:
659 * - Write keys to vterm: vterm_keyboard_key()
660 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200661 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 */
663 if (key != VTERM_KEY_NONE)
664 /* Special key, let vterm convert it. */
665 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200666 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 /* Normal character, let vterm convert it. */
668 vterm_keyboard_unichar(vterm, c, mod);
669
670 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200671 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200673
Bram Moolenaar938783d2017-07-16 20:13:26 +0200674/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200675 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200676 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200677 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200678term_job_running(term_T *term)
679{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200680 /* Also consider the job finished when the channel is closed, to avoid a
681 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200682 return term != NULL
683 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200684 && term->tl_job->jv_status == JOB_STARTED
685 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200686}
687
688/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200689 * Add the last line of the scrollback buffer to the buffer in the window.
690 */
691 static void
692add_scrollback_line_to_buffer(term_T *term)
693{
694 linenr_T lnum = term->tl_scrollback.ga_len - 1;
695 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
696 garray_T ga;
697 int c;
698 int col;
699 int i;
700
701 ga_init2(&ga, 1, 100);
702 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
703 {
704 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
705 goto failed;
706 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
707 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
708 (char_u *)ga.ga_data + ga.ga_len);
709 }
710 if (ga_grow(&ga, 1) == FAIL)
711 goto failed;
712 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
713 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
714
715 if (lnum == 0)
716 {
717 /* Delete the empty line that was in the empty buffer. */
718 curbuf = term->tl_buffer;
719 ml_delete(2, FALSE);
720 curbuf = curwin->w_buffer;
721 }
722
723failed:
724 ga_clear(&ga);
725}
726
727/*
728 * Add the current lines of the terminal to scrollback and to the buffer.
729 * Called after the job has ended and when switching to Terminal mode.
730 */
731 static void
732move_terminal_to_buffer(term_T *term)
733{
734 win_T *wp;
735 int len;
736 int lines_skipped = 0;
737 VTermPos pos;
738 VTermScreenCell cell;
739 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200740 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200741
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200742 if (term->tl_vterm == NULL)
743 return;
744 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200745 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
746 {
747 len = 0;
748 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
749 if (vterm_screen_get_cell(screen, pos, &cell) != 0
750 && cell.chars[0] != NUL)
751 len = pos.col + 1;
752
753 if (len == 0)
754 ++lines_skipped;
755 else
756 {
757 while (lines_skipped > 0)
758 {
759 /* Line was skipped, add an empty line. */
760 --lines_skipped;
761 if (ga_grow(&term->tl_scrollback, 1) == OK)
762 {
763 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
764 + term->tl_scrollback.ga_len;
765
766 line->sb_cols = 0;
767 line->sb_cells = NULL;
768 ++term->tl_scrollback.ga_len;
769
770 add_scrollback_line_to_buffer(term);
771 }
772 }
773
774 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
775 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
776 {
777 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
778 + term->tl_scrollback.ga_len;
779
780 for (pos.col = 0; pos.col < len; ++pos.col)
781 {
782 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
783 vim_memset(p + pos.col, 0, sizeof(cell));
784 else
785 p[pos.col] = cell;
786 }
787 line->sb_cols = len;
788 line->sb_cells = p;
789 ++term->tl_scrollback.ga_len;
790
791 add_scrollback_line_to_buffer(term);
792 }
793 else
794 vim_free(p);
795 }
796 }
797
798 FOR_ALL_WINDOWS(wp)
799 {
800 if (wp->w_buffer == term->tl_buffer)
801 {
802 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
803 wp->w_cursor.col = 0;
804 wp->w_valid = 0;
805 redraw_win_later(wp, NOT_VALID);
806 }
807 }
808}
809
810 static void
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200811set_terminal_mode(term_T *term, int mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200812{
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200813 term->tl_terminal_mode = mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814 vim_free(term->tl_status_text);
815 term->tl_status_text = NULL;
816 if (term->tl_buffer == curbuf)
817 maketitle();
818}
819
820/*
821 * Called after the job if finished and Terminal mode is not active:
822 * Move the vterm contents into the scrollback buffer and free the vterm.
823 */
824 static void
825cleanup_vterm(term_T *term)
826{
827 move_terminal_to_buffer(term);
828 term_free_vterm(term);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200829 set_terminal_mode(term, 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830}
831
832/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200833 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834 * Suspends updating the terminal window.
835 */
836 static void
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200837term_enter_terminal_mode(int mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200838{
839 term_T *term = curbuf->b_term;
840
841 /* Append the current terminal contents to the buffer. */
842 move_terminal_to_buffer(term);
843
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200844 set_terminal_mode(term, mode);
845
846 if (mode == TMODE_ONCE)
847 {
848 /* Move the window cursor to the position of the cursor in the
849 * terminal. */
850 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
851 + term->tl_cursor_pos.row + 1;
852 check_cursor();
853 coladvance(term->tl_cursor_pos.col);
854
855 /* Display the same lines as in the terminal. */
856 curwin->w_topline = term->tl_scrollback_scrolled + 1;
857 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858}
859
860/*
861 * Returns TRUE if the current window contains a terminal and we are in
862 * Terminal-Normal mode.
863 */
864 int
865term_in_terminal_mode()
866{
867 term_T *term = curbuf->b_term;
868
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200869 return term != NULL && term->tl_terminal_mode != 0;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200870}
871
872/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200873 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200874 * Restores updating the terminal window.
875 */
876 void
877term_leave_terminal_mode()
878{
879 term_T *term = curbuf->b_term;
880 sb_line_T *line;
881 garray_T *gap;
882
883 /* Remove the terminal contents from the scrollback and the buffer. */
884 gap = &term->tl_scrollback;
885 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
886 {
887 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
888 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
889 vim_free(line->sb_cells);
890 --gap->ga_len;
891 if (gap->ga_len == 0)
892 break;
893 }
894 check_cursor();
895
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200896 set_terminal_mode(term, 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897
898 if (term->tl_channel_closed)
899 cleanup_vterm(term);
900 redraw_buf_and_status_later(curbuf, NOT_VALID);
901}
902
903/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200904 * Get a key from the user without mapping.
905 * TODO: use terminal mode mappings.
906 */
907 static int
908term_vgetc()
909{
910 int c;
911
912 ++no_mapping;
913 ++allow_keys;
914 got_int = FALSE;
915 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200916 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200917 --no_mapping;
918 --allow_keys;
919 return c;
920}
921
922/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200924 * Return FAIL when the key needs to be handled in Normal mode.
925 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200926 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200927 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200928send_keys_to_term(term_T *term, int c, int typed)
929{
930 char msg[KEY_BUF_LEN];
931 size_t len;
932 static int mouse_was_outside = FALSE;
933 int dragging_outside = FALSE;
934
935 /* Catch keys that need to be handled as in Normal mode. */
936 switch (c)
937 {
938 case NUL:
939 case K_ZERO:
940 if (typed)
941 stuffcharReadbuff(c);
942 return FAIL;
943
944 case K_IGNORE:
945 return FAIL;
946
947 case K_LEFTDRAG:
948 case K_MIDDLEDRAG:
949 case K_RIGHTDRAG:
950 case K_X1DRAG:
951 case K_X2DRAG:
952 dragging_outside = mouse_was_outside;
953 /* FALLTHROUGH */
954 case K_LEFTMOUSE:
955 case K_LEFTMOUSE_NM:
956 case K_LEFTRELEASE:
957 case K_LEFTRELEASE_NM:
958 case K_MIDDLEMOUSE:
959 case K_MIDDLERELEASE:
960 case K_RIGHTMOUSE:
961 case K_RIGHTRELEASE:
962 case K_X1MOUSE:
963 case K_X1RELEASE:
964 case K_X2MOUSE:
965 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200966
967 case K_MOUSEUP:
968 case K_MOUSEDOWN:
969 case K_MOUSELEFT:
970 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200971 if (mouse_row < W_WINROW(curwin)
972 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
973 || mouse_col < W_WINCOL(curwin)
974 || mouse_col >= W_ENDCOL(curwin)
975 || dragging_outside)
976 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200977 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200978 if (typed)
979 {
980 stuffcharReadbuff(c);
981 mouse_was_outside = TRUE;
982 }
983 return FAIL;
984 }
985 }
986 if (typed)
987 mouse_was_outside = FALSE;
988
989 /* Convert the typed key to a sequence of bytes for the job. */
990 len = term_convert_key(term, c, msg);
991 if (len > 0)
992 /* TODO: if FAIL is returned, stop? */
993 channel_send(term->tl_job->jv_channel, PART_IN,
994 (char_u *)msg, (int)len, NULL);
995
996 return OK;
997}
998
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200999 static void
1000position_cursor(win_T *wp, VTermPos *pos)
1001{
1002 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1003 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1004 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1005}
1006
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001007/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001008 * Handle CTRL-W "": send register contents to the job.
1009 */
1010 static void
1011term_paste_register(int prev_c UNUSED)
1012{
1013 int c;
1014 list_T *l;
1015 listitem_T *item;
1016 long reglen = 0;
1017 int type;
1018
1019#ifdef FEAT_CMDL_INFO
1020 if (add_to_showcmd(prev_c))
1021 if (add_to_showcmd('"'))
1022 out_flush();
1023#endif
1024 c = term_vgetc();
1025#ifdef FEAT_CMDL_INFO
1026 clear_showcmd();
1027#endif
1028
1029 /* CTRL-W "= prompt for expression to evaluate. */
1030 if (c == '=' && get_expr_register() != '=')
1031 return;
1032
1033 l = (list_T *)get_reg_contents(c, GREG_LIST);
1034 if (l != NULL)
1035 {
1036 type = get_reg_type(c, &reglen);
1037 for (item = l->lv_first; item != NULL; item = item->li_next)
1038 {
1039 char_u *s = get_tv_string(&item->li_tv);
1040
1041 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1042 s, STRLEN(s), NULL);
1043 if (item->li_next != NULL || type == MLINE)
1044 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1045 (char_u *)"\r", 1, NULL);
1046 }
1047 list_free(l);
1048 }
1049}
1050
1051/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001052 * Returns TRUE if the current window contains a terminal and we are sending
1053 * keys to the job.
1054 */
1055 int
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001056term_use_loop(int once)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001057{
1058 term_T *term = curbuf->b_term;
1059
1060 return term != NULL
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001061 && (once ? term->tl_terminal_mode != TMODE_LOOP
1062 : term->tl_terminal_mode == 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001063 && term->tl_vterm != NULL
1064 && term_job_running(term);
1065}
1066
1067/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001068 * Wait for input and send it to the job.
1069 * Return when the start of a CTRL-W command is typed or anything else that
1070 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001071 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1072 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001074 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001075terminal_loop(void)
1076{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001077 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001078 int termkey = 0;
1079
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001080 if (curbuf->b_term->tl_terminal_mode != 0)
1081 {
1082 /* Got back from TMODE_ONCE, enter Terminal-Job mode. */
1083 term_leave_terminal_mode();
1084 update_cursor(curbuf->b_term, TRUE);
1085 }
1086
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001087 if (*curwin->w_p_tk != NUL)
1088 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001089 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001090
1091 for (;;)
1092 {
1093 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001094 /* Repeat redrawing in case a message is received while redrawing. */
1095 while (curwin->w_redr_type != 0)
1096 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001097 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001098
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001099 c = term_vgetc();
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001100 if (!term_use_loop(FALSE))
Bram Moolenaard85f2712017-07-28 21:51:57 +02001101 /* job finished while waiting for a character */
1102 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001103
Bram Moolenaarfae42832017-08-01 22:24:26 +02001104#ifdef UNIX
1105 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1106#endif
1107#ifdef WIN3264
1108 if (c == Ctrl_C)
1109 /* We don't know if the job can handle CTRL-C itself or not, this
1110 * may kill the shell instead of killing the command running in the
1111 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001112 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001113#endif
1114
Bram Moolenaar69198192017-08-05 14:10:48 +02001115 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001116 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001117 int prev_c = c;
1118
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001119#ifdef FEAT_CMDL_INFO
1120 if (add_to_showcmd(c))
1121 out_flush();
1122#endif
1123 c = term_vgetc();
1124#ifdef FEAT_CMDL_INFO
1125 clear_showcmd();
1126#endif
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001127 if (!term_use_loop(FALSE))
Bram Moolenaard85f2712017-07-28 21:51:57 +02001128 /* job finished while waiting for a character */
1129 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001130
Bram Moolenaar69198192017-08-05 14:10:48 +02001131 if (prev_c == Ctrl_BSL)
1132 {
1133 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001134 {
Bram Moolenaar69198192017-08-05 14:10:48 +02001135 /* CTRL-\ CTRL-N : execute one Normal mode command. */
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001136 term_enter_terminal_mode(TMODE_ONCE);
Bram Moolenaar69198192017-08-05 14:10:48 +02001137 return OK;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001138 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001139 /* Send both keys to the terminal. */
1140 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1141 }
1142 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001143 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001144 /* "CTRL-W .": send CTRL-W to the job */
1145 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001146 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001147 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001148 {
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001149 term_enter_terminal_mode(TMODE_LOOP);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001150 return FAIL;
1151 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001152 else if (c == '"')
1153 {
1154 term_paste_register(prev_c);
1155 continue;
1156 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001157 else if (termkey == 0 || c != termkey)
1158 {
1159 stuffcharReadbuff(Ctrl_W);
1160 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001161 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001162 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001163 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001164 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1165 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001166 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001167 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001168}
1169
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001170/*
1171 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001172 * This updates the title and status, but does not close the vter, because
1173 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001174 */
1175 void
1176term_job_ended(job_T *job)
1177{
1178 term_T *term;
1179 int did_one = FALSE;
1180
1181 for (term = first_term; term != NULL; term = term->tl_next)
1182 if (term->tl_job == job)
1183 {
1184 vim_free(term->tl_title);
1185 term->tl_title = NULL;
1186 vim_free(term->tl_status_text);
1187 term->tl_status_text = NULL;
1188 redraw_buf_and_status_later(term->tl_buffer, VALID);
1189 did_one = TRUE;
1190 }
1191 if (did_one)
1192 redraw_statuslines();
1193 if (curbuf->b_term != NULL)
1194 {
1195 if (curbuf->b_term->tl_job == job)
1196 maketitle();
1197 update_cursor(curbuf->b_term, TRUE);
1198 }
1199}
1200
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001201 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001202may_toggle_cursor(term_T *term)
1203{
1204 if (curbuf == term->tl_buffer)
1205 {
1206 if (term->tl_cursor_visible)
1207 cursor_on();
1208 else
1209 cursor_off();
1210 }
1211}
1212
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001213 static int
1214handle_damage(VTermRect rect, void *user)
1215{
1216 term_T *term = (term_T *)user;
1217
1218 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1219 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1220 redraw_buf_later(term->tl_buffer, NOT_VALID);
1221 return 1;
1222}
1223
1224 static int
1225handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1226{
1227 term_T *term = (term_T *)user;
1228
1229 /* TODO */
1230 redraw_buf_later(term->tl_buffer, NOT_VALID);
1231 return 1;
1232}
1233
1234 static int
1235handle_movecursor(
1236 VTermPos pos,
1237 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001238 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001239 void *user)
1240{
1241 term_T *term = (term_T *)user;
1242 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001243
1244 term->tl_cursor_pos = pos;
1245 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001246
1247 FOR_ALL_WINDOWS(wp)
1248 {
1249 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001250 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001251 }
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001252 if (term->tl_buffer == curbuf && term->tl_terminal_mode == 0)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001253 {
1254 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001255 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001256 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001257
1258 return 1;
1259}
1260
Bram Moolenaar21554412017-07-24 21:44:43 +02001261 static int
1262handle_settermprop(
1263 VTermProp prop,
1264 VTermValue *value,
1265 void *user)
1266{
1267 term_T *term = (term_T *)user;
1268
1269 switch (prop)
1270 {
1271 case VTERM_PROP_TITLE:
1272 vim_free(term->tl_title);
1273 term->tl_title = vim_strsave((char_u *)value->string);
1274 vim_free(term->tl_status_text);
1275 term->tl_status_text = NULL;
1276 if (term == curbuf->b_term)
1277 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001278 break;
1279
1280 case VTERM_PROP_CURSORVISIBLE:
1281 term->tl_cursor_visible = value->boolean;
1282 may_toggle_cursor(term);
1283 out_flush();
1284 break;
1285
Bram Moolenaar21554412017-07-24 21:44:43 +02001286 default:
1287 break;
1288 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001289 /* Always return 1, otherwise vterm doesn't store the value internally. */
1290 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001291}
1292
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001293/*
1294 * The job running in the terminal resized the terminal.
1295 */
1296 static int
1297handle_resize(int rows, int cols, void *user)
1298{
1299 term_T *term = (term_T *)user;
1300 win_T *wp;
1301
1302 term->tl_rows = rows;
1303 term->tl_cols = cols;
1304 FOR_ALL_WINDOWS(wp)
1305 {
1306 if (wp->w_buffer == term->tl_buffer)
1307 {
1308 win_setheight_win(rows, wp);
1309 win_setwidth_win(cols, wp);
1310 }
1311 }
1312
1313 redraw_buf_later(term->tl_buffer, NOT_VALID);
1314 return 1;
1315}
1316
Bram Moolenaard85f2712017-07-28 21:51:57 +02001317/*
1318 * Handle a line that is pushed off the top of the screen.
1319 */
1320 static int
1321handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1322{
1323 term_T *term = (term_T *)user;
1324
1325 /* TODO: Limit the number of lines that are stored. */
1326 /* TODO: put the text in the buffer. */
1327 if (ga_grow(&term->tl_scrollback, 1) == OK)
1328 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001329 VTermScreenCell *p = NULL;
1330 int len = 0;
1331 int i;
1332 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001333
1334 /* do not store empty cells at the end */
1335 for (i = 0; i < cols; ++i)
1336 if (cells[i].chars[0] != 0)
1337 len = i + 1;
1338
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001339 if (len > 0)
1340 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001341 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001342 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001343
1344 line = (sb_line_T *)term->tl_scrollback.ga_data
1345 + term->tl_scrollback.ga_len;
1346 line->sb_cols = len;
1347 line->sb_cells = p;
1348 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001349 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001350
1351 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001352 }
1353 return 0; /* ignored */
1354}
1355
Bram Moolenaar21554412017-07-24 21:44:43 +02001356static VTermScreenCallbacks screen_callbacks = {
1357 handle_damage, /* damage */
1358 handle_moverect, /* moverect */
1359 handle_movecursor, /* movecursor */
1360 handle_settermprop, /* settermprop */
1361 NULL, /* bell */
1362 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001363 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001364 NULL /* sb_popline */
1365};
1366
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001367/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001368 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001369 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001370 */
1371 void
1372term_channel_closed(channel_T *ch)
1373{
1374 term_T *term;
1375 int did_one = FALSE;
1376
1377 for (term = first_term; term != NULL; term = term->tl_next)
1378 if (term->tl_job == ch->ch_job)
1379 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001380 term->tl_channel_closed = TRUE;
1381
Bram Moolenaard85f2712017-07-28 21:51:57 +02001382 vim_free(term->tl_title);
1383 term->tl_title = NULL;
1384 vim_free(term->tl_status_text);
1385 term->tl_status_text = NULL;
1386
Bram Moolenaar423802d2017-07-30 16:52:24 +02001387 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001388 if (term->tl_terminal_mode == 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001389 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001390
1391 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1392 did_one = TRUE;
1393 }
1394 if (did_one)
1395 {
1396 redraw_statuslines();
1397
1398 /* Need to break out of vgetc(). */
1399 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001400 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001401
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001402 term = curbuf->b_term;
1403 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001404 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001405 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001406 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001407 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001408 }
1409 }
1410}
1411
1412/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001413 * Reverse engineer the RGB value into a cterm color index.
1414 * First color is 1. Return 0 if no match found.
1415 */
1416 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001417color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001418{
1419 int red = color->red;
1420 int blue = color->blue;
1421 int green = color->green;
1422
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001423 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001424 if (red == 0)
1425 {
1426 if (green == 0)
1427 {
1428 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001429 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001430 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001431 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001432 }
1433 else if (green == 224)
1434 {
1435 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001436 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001437 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001438 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001439 }
1440 }
1441 else if (red == 224)
1442 {
1443 if (green == 0)
1444 {
1445 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001446 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001447 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001448 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001449 }
1450 else if (green == 224)
1451 {
1452 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001453 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001454 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001455 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001456 }
1457 }
1458 else if (red == 128)
1459 {
1460 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001461 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001462 }
1463 else if (red == 255)
1464 {
1465 if (green == 64)
1466 {
1467 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001468 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001469 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001470 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001471 }
1472 else if (green == 255)
1473 {
1474 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001475 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001476 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001477 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001478 }
1479 }
1480 else if (red == 64)
1481 {
1482 if (green == 64)
1483 {
1484 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001485 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001486 }
1487 else if (green == 255)
1488 {
1489 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001490 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001491 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001492 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001493 }
1494 }
1495 if (t_colors >= 256)
1496 {
1497 if (red == blue && red == green)
1498 {
1499 /* 24-color greyscale */
1500 static int cutoff[23] = {
1501 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1502 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1503 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1504 int i;
1505
1506 for (i = 0; i < 23; ++i)
1507 if (red < cutoff[i])
1508 return i + 233;
1509 return 256;
1510 }
1511
1512 /* 216-color cube */
1513 return 17 + ((red + 25) / 0x33) * 36
1514 + ((green + 25) / 0x33) * 6
1515 + (blue + 25) / 0x33;
1516 }
1517 return 0;
1518}
1519
1520/*
1521 * Convert the attributes of a vterm cell into an attribute index.
1522 */
1523 static int
1524cell2attr(VTermScreenCell *cell)
1525{
1526 int attr = 0;
1527
1528 if (cell->attrs.bold)
1529 attr |= HL_BOLD;
1530 if (cell->attrs.underline)
1531 attr |= HL_UNDERLINE;
1532 if (cell->attrs.italic)
1533 attr |= HL_ITALIC;
1534 if (cell->attrs.strike)
1535 attr |= HL_STANDOUT;
1536 if (cell->attrs.reverse)
1537 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001538
1539#ifdef FEAT_GUI
1540 if (gui.in_use)
1541 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001542 guicolor_T fg, bg;
1543
1544 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1545 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1546 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001547 }
1548 else
1549#endif
1550#ifdef FEAT_TERMGUICOLORS
1551 if (p_tgc)
1552 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001553 guicolor_T fg, bg;
1554
1555 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1556 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1557
1558 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001559 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001560 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001561#endif
1562 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001563 int bold = MAYBE;
1564 int fg = color2index(&cell->fg, TRUE, &bold);
1565 int bg = color2index(&cell->bg, FALSE, &bold);
1566
1567 /* with 8 colors set the bold attribute to get a bright foreground */
1568 if (bold == TRUE)
1569 attr |= HL_BOLD;
1570 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001571 }
1572 return 0;
1573}
1574
1575/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001576 * Called to update the window that contains a terminal.
1577 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001578 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001579 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001580term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001581{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001582 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001583 VTerm *vterm;
1584 VTermScreen *screen;
1585 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001586 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001587
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001588 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode != 0)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001589 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001590
Bram Moolenaard85f2712017-07-28 21:51:57 +02001591 vterm = term->tl_vterm;
1592 screen = vterm_obtain_screen(vterm);
1593 state = vterm_obtain_state(vterm);
1594
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001595 /*
1596 * If the window was resized a redraw will be triggered and we get here.
1597 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1598 */
1599 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1600 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001601 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001602 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1603 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1604 win_T *twp;
1605
1606 FOR_ALL_WINDOWS(twp)
1607 {
1608 /* When more than one window shows the same terminal, use the
1609 * smallest size. */
1610 if (twp->w_buffer == term->tl_buffer)
1611 {
1612 if (!term->tl_rows_fixed && rows > twp->w_height)
1613 rows = twp->w_height;
1614 if (!term->tl_cols_fixed && cols > twp->w_width)
1615 cols = twp->w_width;
1616 }
1617 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001618
1619 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001620 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001621 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001622 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001623 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001624
1625 /* The cursor may have been moved when resizing. */
1626 vterm_state_get_cursorpos(state, &pos);
1627 position_cursor(wp, &pos);
1628
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001629 /* TODO: Only redraw what changed. */
1630 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001631 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001632 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001633 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001634
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001635 if (pos.row < term->tl_rows)
1636 {
1637 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001638 {
1639 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001640 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001641
Bram Moolenaareeac6772017-07-23 15:48:37 +02001642 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1643 vim_memset(&cell, 0, sizeof(cell));
1644
1645 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001646 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001647 if (c == NUL)
1648 {
1649 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001650#if defined(FEAT_MBYTE)
1651 if (enc_utf8)
1652 ScreenLinesUC[off] = NUL;
1653#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001654 }
1655 else
1656 {
1657#if defined(FEAT_MBYTE)
1658 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001659 {
1660 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001661 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001662 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001663 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001664 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001665 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001666 if (enc_utf8)
1667 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001668 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001669#else
1670 ScreenLines[off] = c;
1671#endif
1672 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001673 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001674
1675 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001676 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001677 if (cell.width == 2)
1678 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001679 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001680#if defined(FEAT_MBYTE)
1681 if (enc_utf8)
1682 ScreenLinesUC[off] = NUL;
1683#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001684 ++pos.col;
1685 ++off;
1686 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001687 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001688 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001689 else
1690 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001691
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001692 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1693 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001694 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001695
1696 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001697}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001698
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001699/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001700 * Return TRUE if "wp" is a terminal window where the job has finished.
1701 */
1702 int
1703term_is_finished(buf_T *buf)
1704{
1705 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1706}
1707
1708/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001709 * Return TRUE if "wp" is a terminal window where the job has finished or we
1710 * are in Terminal-Normal mode.
1711 */
1712 int
1713term_show_buffer(buf_T *buf)
1714{
1715 term_T *term = buf->b_term;
1716
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001717 return term != NULL
1718 && (term->tl_vterm == NULL || term->tl_terminal_mode != 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001719}
1720
1721/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001722 * The current buffer is going to be changed. If there is terminal
1723 * highlighting remove it now.
1724 */
1725 void
1726term_change_in_curbuf(void)
1727{
1728 term_T *term = curbuf->b_term;
1729
1730 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1731 {
1732 free_scrollback(term);
1733 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001734
1735 /* The buffer is now like a normal buffer, it cannot be easily
1736 * abandoned when changed. */
1737 set_string_option_direct((char_u *)"buftype", -1,
1738 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001739 }
1740}
1741
1742/*
1743 * Get the screen attribute for a position in the buffer.
1744 */
1745 int
1746term_get_attr(buf_T *buf, linenr_T lnum, int col)
1747{
1748 term_T *term = buf->b_term;
1749 sb_line_T *line;
1750
Bram Moolenaar70229f92017-07-29 16:01:53 +02001751 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001752 return 0;
1753 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1754 if (col >= line->sb_cols)
1755 return 0;
1756 return cell2attr(line->sb_cells + col);
1757}
1758
1759/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001760 * Create a new vterm and initialize it.
1761 */
1762 static void
1763create_vterm(term_T *term, int rows, int cols)
1764{
1765 VTerm *vterm;
1766 VTermScreen *screen;
1767
1768 vterm = vterm_new(rows, cols);
1769 term->tl_vterm = vterm;
1770 screen = vterm_obtain_screen(vterm);
1771 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1772 /* TODO: depends on 'encoding'. */
1773 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001774
1775 /* Vterm uses a default black background. Set it to white when
1776 * 'background' is "light". */
1777 if (*p_bg == 'l')
1778 {
1779 VTermColor fg, bg;
1780
1781 fg.red = fg.green = fg.blue = 0;
1782 bg.red = bg.green = bg.blue = 255;
1783 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1784 }
1785
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001786 /* Required to initialize most things. */
1787 vterm_screen_reset(screen, 1 /* hard */);
1788}
1789
Bram Moolenaar21554412017-07-24 21:44:43 +02001790/*
1791 * Return the text to show for the buffer name and status.
1792 */
1793 char_u *
1794term_get_status_text(term_T *term)
1795{
1796 if (term->tl_status_text == NULL)
1797 {
1798 char_u *txt;
1799 size_t len;
1800
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001801 if (term->tl_terminal_mode != 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001802 {
1803 if (term_job_running(term))
1804 txt = (char_u *)_("Terminal");
1805 else
1806 txt = (char_u *)_("Terminal-finished");
1807 }
1808 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001809 txt = term->tl_title;
1810 else if (term_job_running(term))
1811 txt = (char_u *)_("running");
1812 else
1813 txt = (char_u *)_("finished");
1814 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001815 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001816 if (term->tl_status_text != NULL)
1817 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1818 term->tl_buffer->b_fname, txt);
1819 }
1820 return term->tl_status_text;
1821}
1822
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001823/*
1824 * Mark references in jobs of terminals.
1825 */
1826 int
1827set_ref_in_term(int copyID)
1828{
1829 int abort = FALSE;
1830 term_T *term;
1831 typval_T tv;
1832
1833 for (term = first_term; term != NULL; term = term->tl_next)
1834 if (term->tl_job != NULL)
1835 {
1836 tv.v_type = VAR_JOB;
1837 tv.vval.v_job = term->tl_job;
1838 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1839 }
1840 return abort;
1841}
1842
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001843/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001844 * Get the buffer from the first argument in "argvars".
1845 * Returns NULL when the buffer is not for a terminal window.
1846 */
1847 static buf_T *
1848term_get_buf(typval_T *argvars)
1849{
1850 buf_T *buf;
1851
1852 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1853 ++emsg_off;
1854 buf = get_buf_tv(&argvars[0], FALSE);
1855 --emsg_off;
1856 if (buf == NULL || buf->b_term == NULL)
1857 return NULL;
1858 return buf;
1859}
1860
1861/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001862 * "term_getattr(attr, name)" function
1863 */
1864 void
1865f_term_getattr(typval_T *argvars, typval_T *rettv)
1866{
1867 int attr;
1868 size_t i;
1869 char_u *name;
1870
1871 static struct {
1872 char *name;
1873 int attr;
1874 } attrs[] = {
1875 {"bold", HL_BOLD},
1876 {"italic", HL_ITALIC},
1877 {"underline", HL_UNDERLINE},
1878 {"strike", HL_STANDOUT},
1879 {"reverse", HL_INVERSE},
1880 };
1881
1882 attr = get_tv_number(&argvars[0]);
1883 name = get_tv_string_chk(&argvars[1]);
1884 if (name == NULL)
1885 return;
1886
1887 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1888 if (STRCMP(name, attrs[i].name) == 0)
1889 {
1890 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1891 break;
1892 }
1893}
1894
1895/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001896 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001897 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001898 void
1899f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001900{
Bram Moolenaar97870002017-07-30 18:28:38 +02001901 buf_T *buf = term_get_buf(argvars);
1902 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001903
Bram Moolenaar97870002017-07-30 18:28:38 +02001904 if (rettv_list_alloc(rettv) == FAIL)
1905 return;
1906 if (buf == NULL)
1907 return;
1908
1909 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001910 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1911 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001912 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001913}
1914
1915/*
1916 * "term_getjob(buf)" function
1917 */
1918 void
1919f_term_getjob(typval_T *argvars, typval_T *rettv)
1920{
1921 buf_T *buf = term_get_buf(argvars);
1922
1923 rettv->v_type = VAR_JOB;
1924 rettv->vval.v_job = NULL;
1925 if (buf == NULL)
1926 return;
1927
1928 rettv->vval.v_job = buf->b_term->tl_job;
1929 if (rettv->vval.v_job != NULL)
1930 ++rettv->vval.v_job->jv_refcount;
1931}
1932
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001933 static int
1934get_row_number(typval_T *tv, term_T *term)
1935{
1936 if (tv->v_type == VAR_STRING
1937 && tv->vval.v_string != NULL
1938 && STRCMP(tv->vval.v_string, ".") == 0)
1939 return term->tl_cursor_pos.row;
1940 return (int)get_tv_number(tv) - 1;
1941}
1942
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001943/*
1944 * "term_getline(buf, row)" function
1945 */
1946 void
1947f_term_getline(typval_T *argvars, typval_T *rettv)
1948{
1949 buf_T *buf = term_get_buf(argvars);
1950 term_T *term;
1951 int row;
1952
1953 rettv->v_type = VAR_STRING;
1954 if (buf == NULL)
1955 return;
1956 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001957 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001958
1959 if (term->tl_vterm == NULL)
1960 {
1961 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1962
1963 /* vterm is finished, get the text from the buffer */
1964 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1965 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1966 }
1967 else
1968 {
1969 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1970 VTermRect rect;
1971 int len;
1972 char_u *p;
1973
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001974 if (row < 0 || row >= term->tl_rows)
1975 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001976 len = term->tl_cols * MB_MAXBYTES + 1;
1977 p = alloc(len);
1978 if (p == NULL)
1979 return;
1980 rettv->vval.v_string = p;
1981
1982 rect.start_col = 0;
1983 rect.end_col = term->tl_cols;
1984 rect.start_row = row;
1985 rect.end_row = row + 1;
1986 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1987 }
1988}
1989
1990/*
1991 * "term_getsize(buf)" function
1992 */
1993 void
1994f_term_getsize(typval_T *argvars, typval_T *rettv)
1995{
1996 buf_T *buf = term_get_buf(argvars);
1997 list_T *l;
1998
1999 if (rettv_list_alloc(rettv) == FAIL)
2000 return;
2001 if (buf == NULL)
2002 return;
2003
2004 l = rettv->vval.v_list;
2005 list_append_number(l, buf->b_term->tl_rows);
2006 list_append_number(l, buf->b_term->tl_cols);
2007}
2008
2009/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002010 * "term_getstatus(buf)" function
2011 */
2012 void
2013f_term_getstatus(typval_T *argvars, typval_T *rettv)
2014{
2015 buf_T *buf = term_get_buf(argvars);
2016 term_T *term;
2017 char_u val[100];
2018
2019 rettv->v_type = VAR_STRING;
2020 if (buf == NULL)
2021 return;
2022 term = buf->b_term;
2023
2024 if (term_job_running(term))
2025 STRCPY(val, "running");
2026 else
2027 STRCPY(val, "finished");
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02002028 if (term->tl_terminal_mode != 0)
Bram Moolenaarb000e322017-07-30 19:38:21 +02002029 STRCAT(val, ",terminal");
2030 rettv->vval.v_string = vim_strsave(val);
2031}
2032
2033/*
2034 * "term_gettitle(buf)" function
2035 */
2036 void
2037f_term_gettitle(typval_T *argvars, typval_T *rettv)
2038{
2039 buf_T *buf = term_get_buf(argvars);
2040
2041 rettv->v_type = VAR_STRING;
2042 if (buf == NULL)
2043 return;
2044
2045 if (buf->b_term->tl_title != NULL)
2046 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2047}
2048
2049/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002050 * "term_gettty(buf)" function
2051 */
2052 void
2053f_term_gettty(typval_T *argvars, typval_T *rettv)
2054{
2055 buf_T *buf = term_get_buf(argvars);
2056 char_u *p;
2057
2058 rettv->v_type = VAR_STRING;
2059 if (buf == NULL)
2060 return;
2061 if (buf->b_term->tl_job != NULL)
2062 p = buf->b_term->tl_job->jv_tty_name;
2063 else
2064 p = buf->b_term->tl_tty_name;
2065 if (p != NULL)
2066 rettv->vval.v_string = vim_strsave(p);
2067}
2068
2069/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002070 * "term_list()" function
2071 */
2072 void
2073f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2074{
2075 term_T *tp;
2076 list_T *l;
2077
2078 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2079 return;
2080
2081 l = rettv->vval.v_list;
2082 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2083 if (tp != NULL && tp->tl_buffer != NULL)
2084 if (list_append_number(l,
2085 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2086 return;
2087}
2088
2089/*
2090 * "term_scrape(buf, row)" function
2091 */
2092 void
2093f_term_scrape(typval_T *argvars, typval_T *rettv)
2094{
2095 buf_T *buf = term_get_buf(argvars);
2096 VTermScreen *screen = NULL;
2097 VTermPos pos;
2098 list_T *l;
2099 term_T *term;
2100
2101 if (rettv_list_alloc(rettv) == FAIL)
2102 return;
2103 if (buf == NULL)
2104 return;
2105 term = buf->b_term;
2106 if (term->tl_vterm != NULL)
2107 screen = vterm_obtain_screen(term->tl_vterm);
2108
2109 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002110 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002111 for (pos.col = 0; pos.col < term->tl_cols; )
2112 {
2113 dict_T *dcell;
2114 VTermScreenCell cell;
2115 char_u rgb[8];
2116 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2117 int off = 0;
2118 int i;
2119
2120 if (screen == NULL)
2121 {
2122 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2123 sb_line_T *line;
2124
2125 /* vterm has finished, get the cell from scrollback */
2126 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2127 break;
2128 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2129 if (pos.col >= line->sb_cols)
2130 break;
2131 cell = line->sb_cells[pos.col];
2132 }
2133 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2134 break;
2135 dcell = dict_alloc();
2136 list_append_dict(l, dcell);
2137
2138 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2139 {
2140 if (cell.chars[i] == 0)
2141 break;
2142 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2143 }
2144 mbs[off] = NUL;
2145 dict_add_nr_str(dcell, "chars", 0, mbs);
2146
2147 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2148 cell.fg.red, cell.fg.green, cell.fg.blue);
2149 dict_add_nr_str(dcell, "fg", 0, rgb);
2150 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2151 cell.bg.red, cell.bg.green, cell.bg.blue);
2152 dict_add_nr_str(dcell, "bg", 0, rgb);
2153
2154 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2155 dict_add_nr_str(dcell, "width", cell.width, NULL);
2156
2157 ++pos.col;
2158 if (cell.width == 2)
2159 ++pos.col;
2160 }
2161}
2162
2163/*
2164 * "term_sendkeys(buf, keys)" function
2165 */
2166 void
2167f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2168{
2169 buf_T *buf = term_get_buf(argvars);
2170 char_u *msg;
2171 term_T *term;
2172
2173 rettv->v_type = VAR_UNKNOWN;
2174 if (buf == NULL)
2175 return;
2176
2177 msg = get_tv_string_chk(&argvars[1]);
2178 if (msg == NULL)
2179 return;
2180 term = buf->b_term;
2181 if (term->tl_vterm == NULL)
2182 return;
2183
2184 while (*msg != NUL)
2185 {
2186 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2187 msg += MB_PTR2LEN(msg);
2188 }
2189
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02002190 if (term->tl_terminal_mode == 0)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002191 {
2192 /* TODO: only update once in a while. */
2193 update_screen(0);
2194 if (buf == curbuf)
2195 update_cursor(term, TRUE);
2196 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002197}
2198
2199/*
2200 * "term_start(command, options)" function
2201 */
2202 void
2203f_term_start(typval_T *argvars, typval_T *rettv)
2204{
2205 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002206 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002207
2208 if (cmd == NULL)
2209 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002210 init_job_options(&opt);
2211 /* TODO: allow more job options */
2212 if (argvars[1].v_type != VAR_UNKNOWN
2213 && get_job_options(&argvars[1], &opt,
2214 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002215 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2216 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002217 return;
2218
2219 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002220
2221 if (curbuf->b_term != NULL)
2222 rettv->vval.v_number = curbuf->b_fnum;
2223}
2224
2225/*
2226 * "term_wait" function
2227 */
2228 void
2229f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2230{
2231 buf_T *buf = term_get_buf(argvars);
2232
2233 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002234 {
2235 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002236 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002237 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002238 if (buf->b_term->tl_job == NULL)
2239 {
2240 ch_log(NULL, "term_wait(): no job to wait for");
2241 return;
2242 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002243
2244 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002245 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002246 {
2247 /* The job is dead, keep reading channel I/O until the channel is
2248 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002249 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002250 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2251 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002252 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002253 parse_queued_messages();
2254 ui_delay(10L, FALSE);
2255 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002256 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002257 parse_queued_messages();
2258 }
2259 else
2260 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002261 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002262 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002263
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002264 /* Wait for 10 msec for any channel I/O. */
2265 /* TODO: use delay from optional argument */
2266 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002267 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002268
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002269 /* Flushing messages on channels is hopefully sufficient.
2270 * TODO: is there a better way? */
2271 parse_queued_messages();
2272 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002273}
2274
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002275# ifdef WIN3264
2276
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002277/**************************************
2278 * 2. MS-Windows implementation.
2279 */
2280
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002281#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2282#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2283
Bram Moolenaar8a773062017-07-24 22:29:21 +02002284void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002285void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002286void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002287BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2288void (*winpty_config_set_initial_size)(void*, int, int);
2289LPCWSTR (*winpty_conin_name)(void*);
2290LPCWSTR (*winpty_conout_name)(void*);
2291LPCWSTR (*winpty_conerr_name)(void*);
2292void (*winpty_free)(void*);
2293void (*winpty_config_free)(void*);
2294void (*winpty_spawn_config_free)(void*);
2295void (*winpty_error_free)(void*);
2296LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002297BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002298HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002299
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002300#define WINPTY_DLL "winpty.dll"
2301
2302static HINSTANCE hWinPtyDLL = NULL;
2303
2304 int
2305dyn_winpty_init(void)
2306{
2307 int i;
2308 static struct
2309 {
2310 char *name;
2311 FARPROC *ptr;
2312 } winpty_entry[] =
2313 {
2314 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2315 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2316 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2317 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2318 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2319 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2320 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2321 {"winpty_free", (FARPROC*)&winpty_free},
2322 {"winpty_open", (FARPROC*)&winpty_open},
2323 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2324 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2325 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2326 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002327 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002328 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002329 {NULL, NULL}
2330 };
2331
2332 /* No need to initialize twice. */
2333 if (hWinPtyDLL)
2334 return 1;
2335 /* Load winpty.dll */
2336 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2337 if (!hWinPtyDLL)
2338 {
2339 EMSG2(_(e_loadlib), WINPTY_DLL);
2340 return 0;
2341 }
2342 for (i = 0; winpty_entry[i].name != NULL
2343 && winpty_entry[i].ptr != NULL; ++i)
2344 {
2345 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2346 winpty_entry[i].name)) == NULL)
2347 {
2348 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2349 return 0;
2350 }
2351 }
2352
2353 return 1;
2354}
2355
2356/*
2357 * Create a new terminal of "rows" by "cols" cells.
2358 * Store a reference in "term".
2359 * Return OK or FAIL.
2360 */
2361 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002362term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002363{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002364 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002365 channel_T *channel = NULL;
2366 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002367 DWORD error;
2368 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2369 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002370 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002371 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002372
2373 if (!dyn_winpty_init())
2374 return FAIL;
2375
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002376 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002377 if (p == NULL)
2378 return FAIL;
2379
2380 job = job_alloc();
2381 if (job == NULL)
2382 goto failed;
2383
2384 channel = add_channel();
2385 if (channel == NULL)
2386 goto failed;
2387
2388 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2389 if (term->tl_winpty_config == NULL)
2390 goto failed;
2391
2392 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2393 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2394 if (term->tl_winpty == NULL)
2395 goto failed;
2396
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002397 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002398 spawn_config = winpty_spawn_config_new(
2399 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2400 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2401 NULL,
2402 p,
2403 NULL,
2404 NULL,
2405 &winpty_err);
2406 if (spawn_config == NULL)
2407 goto failed;
2408
2409 channel = add_channel();
2410 if (channel == NULL)
2411 goto failed;
2412
2413 job = job_alloc();
2414 if (job == NULL)
2415 goto failed;
2416
2417 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2418 &child_thread_handle, &error, &winpty_err))
2419 goto failed;
2420
2421 channel_set_pipes(channel,
2422 (sock_T) CreateFileW(
2423 winpty_conin_name(term->tl_winpty),
2424 GENERIC_WRITE, 0, NULL,
2425 OPEN_EXISTING, 0, NULL),
2426 (sock_T) CreateFileW(
2427 winpty_conout_name(term->tl_winpty),
2428 GENERIC_READ, 0, NULL,
2429 OPEN_EXISTING, 0, NULL),
2430 (sock_T) CreateFileW(
2431 winpty_conerr_name(term->tl_winpty),
2432 GENERIC_READ, 0, NULL,
2433 OPEN_EXISTING, 0, NULL));
2434
2435 jo = CreateJobObject(NULL, NULL);
2436 if (jo == NULL)
2437 goto failed;
2438
2439 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002440 {
2441 /* Failed, switch the way to terminate process with TerminateProcess. */
2442 CloseHandle(jo);
2443 jo = NULL;
2444 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002445
2446 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002447 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002448
2449 create_vterm(term, rows, cols);
2450
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002451 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002452 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453
2454 job->jv_channel = channel;
2455 job->jv_proc_info.hProcess = child_process_handle;
2456 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2457 job->jv_job_object = jo;
2458 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002459 sprintf(buf, "winpty://%lu",
2460 GetProcessId(winpty_agent_process(term->tl_winpty)));
2461 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002462 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002463 term->tl_job = job;
2464
2465 return OK;
2466
2467failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002468 if (spawn_config != NULL)
2469 winpty_spawn_config_free(spawn_config);
2470 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002471 if (channel != NULL)
2472 channel_clear(channel);
2473 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002474 {
2475 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002476 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002477 }
2478 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002479 if (jo != NULL)
2480 CloseHandle(jo);
2481 if (term->tl_winpty != NULL)
2482 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002483 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002484 if (term->tl_winpty_config != NULL)
2485 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002486 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002487 if (winpty_err != NULL)
2488 {
2489 char_u *msg = utf16_to_enc(
2490 (short_u *)winpty_error_msg(winpty_err), NULL);
2491
2492 EMSG(msg);
2493 winpty_error_free(winpty_err);
2494 }
2495 return FAIL;
2496}
2497
2498/*
2499 * Free the terminal emulator part of "term".
2500 */
2501 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002502term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002504 if (term->tl_winpty != NULL)
2505 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002506 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002507 if (term->tl_winpty_config != NULL)
2508 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002509 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002510 if (term->tl_vterm != NULL)
2511 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002512 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002513}
2514
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002515/*
2516 * Request size to terminal.
2517 */
2518 static void
2519term_report_winsize(term_T *term, int rows, int cols)
2520{
2521 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2522}
2523
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002524# else
2525
2526/**************************************
2527 * 3. Unix-like implementation.
2528 */
2529
2530/*
2531 * Create a new terminal of "rows" by "cols" cells.
2532 * Start job for "cmd".
2533 * Store the pointers in "term".
2534 * Return OK or FAIL.
2535 */
2536 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002537term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002538{
2539 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002540
2541 create_vterm(term, rows, cols);
2542
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002543 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002544 argvars[0].v_type = VAR_STRING;
2545 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002546
2547 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002548 if (term->tl_job != NULL)
2549 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002550
Bram Moolenaar61a66052017-07-22 18:39:00 +02002551 return term->tl_job != NULL
2552 && term->tl_job->jv_channel != NULL
2553 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002554}
2555
2556/*
2557 * Free the terminal emulator part of "term".
2558 */
2559 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002560term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002561{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002562 if (term->tl_vterm != NULL)
2563 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002564 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002565}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002566
2567/*
2568 * Request size to terminal.
2569 */
2570 static void
2571term_report_winsize(term_T *term, int rows, int cols)
2572{
2573 /* Use an ioctl() to report the new window size to the job. */
2574 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2575 {
2576 int fd = -1;
2577 int part;
2578
2579 for (part = PART_OUT; part < PART_COUNT; ++part)
2580 {
2581 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2582 if (isatty(fd))
2583 break;
2584 }
2585 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2586 mch_stop_job(term->tl_job, (char_u *)"winch");
2587 }
2588}
2589
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002590# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002591
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002592#endif /* FEAT_TERMINAL */