blob: f2fae578951932414e0a6508f2e94dc7fa928b34 [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
22 * that 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
28 * terminal encoding and writting to the job over a channel.
29 *
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 *
35 * TODO:
Bram Moolenaar1f2903c2017-07-23 19:51:01 +020036 * - do not store terminal buffer in viminfo
37 * - put terminal title in the statusline
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020038 * - Add a scrollback buffer (contains lines to scroll off the top).
39 * Can use the buf_T lines, store attributes somewhere else?
40 * - When the job ends:
41 * - Write "-- JOB ENDED --" in the terminal.
42 * - Put the terminal contents in the scrollback buffer.
43 * - Free the terminal emulator.
44 * - Display the scrollback buffer (but with attributes).
45 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020046 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020047 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020049 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020050 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020051 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020053 * - implement "term" for job_start(): more job options when starting a
54 * terminal.
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020055 * - implement term_list() list of buffers with a terminal
56 * - implement term_getsize(buf)
57 * - implement term_setsize(buf)
58 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
59 * - implement term_wait(buf) wait for screen to be updated
60 * - implement term_scrape(buf, row) inspect terminal screen
61 * - implement term_open(command, options) open terminal window
62 * - implement term_getjob(buf)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
64 * conversions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020065 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 */
67
68#include "vim.h"
69
70#ifdef FEAT_TERMINAL
71
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020072#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020073# define MIN(x,y) (x < y ? x : y)
74# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020077#include "libvterm/include/vterm.h"
78
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079/* typedef term_T in structs.h */
80struct terminal_S {
81 term_T *tl_next;
82
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020083#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020084 void *tl_winpty_config;
85 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020089 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020091 /* last known vterm size */
92 int tl_rows;
93 int tl_cols;
94 /* vterm size does not follow window size */
95 int tl_rows_fixed;
96 int tl_cols_fixed;
97
Bram Moolenaare4f25e42017-07-07 11:54:15 +020098 /* Range of screen rows to update. Zero based. */
99 int tl_dirty_row_start; /* -1 if nothing dirty */
100 int tl_dirty_row_end; /* row below last one to update */
101
102 pos_T tl_cursor;
103};
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105/*
106 * List of all active terminals.
107 */
108static term_T *first_term = NULL;
109
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200110
111#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
112#define KEY_BUF_LEN 200
113
114/*
115 * Functions with separate implementation for MS-Windows and Unix-like systems.
116 */
117static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200118static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119static void term_free(term_T *term);
120
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121/**************************************
122 * 1. Generic code for all systems.
123 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124
125/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 * Determine the terminal size from 'termsize' and the current window.
127 * Assumes term->tl_rows and term->tl_cols are zero.
128 */
129 static void
130set_term_and_win_size(term_T *term)
131{
132 if (*curwin->w_p_tms != NUL)
133 {
134 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
135
136 term->tl_rows = atoi((char *)curwin->w_p_tms);
137 term->tl_cols = atoi((char *)p);
138 }
139 if (term->tl_rows == 0)
140 term->tl_rows = curwin->w_height;
141 else
142 {
143 win_setheight_win(term->tl_rows, curwin);
144 term->tl_rows_fixed = TRUE;
145 }
146 if (term->tl_cols == 0)
147 term->tl_cols = curwin->w_width;
148 else
149 {
150 win_setwidth_win(term->tl_cols, curwin);
151 term->tl_cols_fixed = TRUE;
152 }
153}
154
155/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200156 * ":terminal": open a terminal window and execute a job in it.
157 */
158 void
159ex_terminal(exarg_T *eap)
160{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200161 exarg_T split_ea;
162 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200164 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200165
166 if (check_restricted() || check_secure())
167 return;
168
169 term = (term_T *)alloc_clear(sizeof(term_T));
170 if (term == NULL)
171 return;
172 term->tl_dirty_row_end = MAX_ROW;
173
174 /* Open a new window or tab. */
175 vim_memset(&split_ea, 0, sizeof(split_ea));
176 split_ea.cmdidx = CMD_new;
177 split_ea.cmd = (char_u *)"new";
178 split_ea.arg = (char_u *)"";
179 ex_splitview(&split_ea);
180 if (curwin == old_curwin)
181 {
182 /* split failed */
183 vim_free(term);
184 return;
185 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200186 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200187 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200188
189 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200190 term->tl_next = first_term;
191 first_term = term;
192
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200193 if (buflist_findname(cmd) == NULL)
194 curbuf->b_ffname = vim_strsave(cmd);
195 else
196 {
197 int i;
198 size_t len = STRLEN(cmd) + 10;
199 char_u *p = alloc(len);
200
201 for (i = 1; p != NULL; ++i)
202 {
203 vim_snprintf((char *)p, len, "%s (%d)", cmd, i);
204 if (buflist_findname(p) == NULL)
205 {
206 curbuf->b_ffname = p;
207 break;
208 }
209 }
210 }
211 curbuf->b_fname = curbuf->b_ffname;
212
213 /* Mark the buffer as changed, so that it's not easy to abandon the job. */
214 curbuf->b_changed = TRUE;
215 curbuf->b_p_ma = FALSE;
216 set_string_option_direct((char_u *)"buftype", -1,
217 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200218
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200219 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200220
Bram Moolenaare173fd02017-07-22 19:03:32 +0200221 if (cmd == NULL || *cmd == NUL)
222 cmd = p_sh;
223
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200224 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200225 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200226 {
227 /* store the size we ended up with */
228 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200229 }
230 else
231 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200232 free_terminal(term);
233 curbuf->b_term = NULL;
234
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200235 /* Wiping out the buffer will also close the window and call
236 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200237 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200238 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200239
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200240 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241}
242
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200243/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200244 * Free a terminal and everything it refers to.
245 * Kills the job if there is one.
246 * Called when wiping out a buffer.
247 */
248 void
249free_terminal(term_T *term)
250{
251 term_T *tp;
252
253 if (term == NULL)
254 return;
255 if (first_term == term)
256 first_term = term->tl_next;
257 else
258 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
259 if (tp->tl_next == term)
260 {
261 tp->tl_next = term->tl_next;
262 break;
263 }
264
265 if (term->tl_job != NULL)
266 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200267 if (term->tl_job->jv_status != JOB_ENDED
268 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200269 job_stop(term->tl_job, NULL, "kill");
270 job_unref(term->tl_job);
271 }
272
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200273 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200274 vim_free(term);
275}
276
277/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200278 * Write job output "msg[len]" to the vterm.
279 */
280 static void
281term_write_job_output(term_T *term, char_u *msg, size_t len)
282{
283 VTerm *vterm = term->tl_vterm;
284 char_u *p;
285 size_t done;
286 size_t len_now;
287
288 for (done = 0; done < len; done += len_now)
289 {
290 for (p = msg + done; p < msg + len; )
291 {
292 if (*p == NL)
293 break;
294 p += utf_ptr2len_len(p, len - (p - msg));
295 }
296 len_now = p - msg - done;
297 vterm_input_write(vterm, (char *)msg + done, len_now);
298 if (p < msg + len && *p == NL)
299 {
300 /* Convert NL to CR-NL, that appears to work best. */
301 vterm_input_write(vterm, "\r\n", 2);
302 ++len_now;
303 }
304 }
305
306 /* this invokes the damage callbacks */
307 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
308}
309
310/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200311 * Invoked when "msg" output from a job was received. Write it to the terminal
312 * of "buffer".
313 */
314 void
315write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
316{
317 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200318 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200319
320 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200321 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200322
323 /* TODO: only update once in a while. */
324 update_screen(0);
325 setcursor();
326 out_flush();
327}
328
329/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200330 * Convert typed key "c" into bytes to send to the job.
331 * Return the number of bytes in "buf".
332 */
333 static int
334term_convert_key(int c, char *buf)
335{
336 VTerm *vterm = curbuf->b_term->tl_vterm;
337 VTermKey key = VTERM_KEY_NONE;
338 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200340 switch (c)
341 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200342 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200343 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200344 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
345 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200346 case K_DEL: key = VTERM_KEY_DEL; break;
347 case K_DOWN: key = VTERM_KEY_DOWN; break;
348 case K_END: key = VTERM_KEY_END; break;
349 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
350 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
351 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
352 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
353 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
354 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
355 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
356 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
357 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
358 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
359 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
360 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
361 case K_HOME: key = VTERM_KEY_HOME; break;
362 case K_INS: key = VTERM_KEY_INS; break;
363 case K_K0: key = VTERM_KEY_KP_0; break;
364 case K_K1: key = VTERM_KEY_KP_1; break;
365 case K_K2: key = VTERM_KEY_KP_2; break;
366 case K_K3: key = VTERM_KEY_KP_3; break;
367 case K_K4: key = VTERM_KEY_KP_4; break;
368 case K_K5: key = VTERM_KEY_KP_5; break;
369 case K_K6: key = VTERM_KEY_KP_6; break;
370 case K_K7: key = VTERM_KEY_KP_7; break;
371 case K_K8: key = VTERM_KEY_KP_8; break;
372 case K_K9: key = VTERM_KEY_KP_9; break;
373 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
374 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
375 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
376 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
377 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
378 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
379 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
380 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
381 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
382 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
383 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
384 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
385 case K_LEFT: key = VTERM_KEY_LEFT; break;
386 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
387 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
388 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
389 case K_UP: key = VTERM_KEY_UP; break;
390 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200391
392 case K_MOUSEUP: /* TODO */ break;
393 case K_MOUSEDOWN: /* TODO */ break;
394 case K_MOUSELEFT: /* TODO */ break;
395 case K_MOUSERIGHT: /* TODO */ break;
396
397 case K_LEFTMOUSE: /* TODO */ break;
398 case K_LEFTMOUSE_NM: /* TODO */ break;
399 case K_LEFTDRAG: /* TODO */ break;
400 case K_LEFTRELEASE: /* TODO */ break;
401 case K_LEFTRELEASE_NM: /* TODO */ break;
402 case K_MIDDLEMOUSE: /* TODO */ break;
403 case K_MIDDLEDRAG: /* TODO */ break;
404 case K_MIDDLERELEASE: /* TODO */ break;
405 case K_RIGHTMOUSE: /* TODO */ break;
406 case K_RIGHTDRAG: /* TODO */ break;
407 case K_RIGHTRELEASE: /* TODO */ break;
408 case K_X1MOUSE: /* TODO */ break;
409 case K_X1DRAG: /* TODO */ break;
410 case K_X1RELEASE: /* TODO */ break;
411 case K_X2MOUSE: /* TODO */ break;
412 case K_X2DRAG: /* TODO */ break;
413 case K_X2RELEASE: /* TODO */ break;
414
415 /* TODO: handle all special keys and modifiers that terminal_loop()
416 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200417 }
418
419 /*
420 * Convert special keys to vterm keys:
421 * - Write keys to vterm: vterm_keyboard_key()
422 * - Write output to channel.
423 */
424 if (key != VTERM_KEY_NONE)
425 /* Special key, let vterm convert it. */
426 vterm_keyboard_key(vterm, key, mod);
427 else
428 /* Normal character, let vterm convert it. */
429 vterm_keyboard_unichar(vterm, c, mod);
430
431 /* Read back the converted escape sequence. */
432 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
433}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200434
Bram Moolenaar938783d2017-07-16 20:13:26 +0200435/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200436 * Wait for input and send it to the job.
437 * Return when the start of a CTRL-W command is typed or anything else that
438 * should be handled as a Normal mode command.
439 */
440 void
441terminal_loop(void)
442{
443 char buf[KEY_BUF_LEN];
444 int c;
445 size_t len;
446 static int mouse_was_outside = FALSE;
447 int dragging_outside = FALSE;
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200448 int termkey = 0;
449
450 if (*curwin->w_p_tk != NUL)
451 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200452
453 for (;;)
454 {
455 /* TODO: skip screen update when handling a sequence of keys. */
456 update_screen(0);
457 setcursor();
458 out_flush();
459 ++no_mapping;
460 ++allow_keys;
461 got_int = FALSE;
462 c = vgetc();
463 --no_mapping;
464 --allow_keys;
465
Bram Moolenaardbe948d2017-07-23 22:50:51 +0200466 if (c == (termkey == 0 ? Ctrl_W : termkey))
467 {
468 stuffcharReadbuff(Ctrl_W);
469 return;
470 }
471
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200472 /* Catch keys that need to be handled as in Normal mode. */
473 switch (c)
474 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200475 case NUL:
476 case K_ZERO:
477 stuffcharReadbuff(c);
478 return;
479
480 case K_IGNORE: continue;
481
482 case K_LEFTDRAG:
483 case K_MIDDLEDRAG:
484 case K_RIGHTDRAG:
485 case K_X1DRAG:
486 case K_X2DRAG:
487 dragging_outside = mouse_was_outside;
488 /* FALLTHROUGH */
489 case K_LEFTMOUSE:
490 case K_LEFTMOUSE_NM:
491 case K_LEFTRELEASE:
492 case K_LEFTRELEASE_NM:
493 case K_MIDDLEMOUSE:
494 case K_MIDDLERELEASE:
495 case K_RIGHTMOUSE:
496 case K_RIGHTRELEASE:
497 case K_X1MOUSE:
498 case K_X1RELEASE:
499 case K_X2MOUSE:
500 case K_X2RELEASE:
501 if (mouse_row < W_WINROW(curwin)
502 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
503 || mouse_col < W_WINCOL(curwin)
504 || mouse_col >= W_ENDCOL(curwin)
505 || dragging_outside)
506 {
507 /* click outside the current window */
508 stuffcharReadbuff(c);
509 mouse_was_outside = TRUE;
510 return;
511 }
512 }
513 mouse_was_outside = FALSE;
514
515 /* Convert the typed key to a sequence of bytes for the job. */
516 len = term_convert_key(c, buf);
517 if (len > 0)
518 /* TODO: if FAIL is returned, stop? */
519 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
520 (char_u *)buf, len, NULL);
521 }
522}
523
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200524/*
525 * Called when a job has finished.
526 */
527 void
528term_job_ended(job_T *job)
529{
530 if (curbuf->b_term != NULL && curbuf->b_term->tl_job == job)
531 maketitle();
532}
533
534/*
535 * Return TRUE if the job for "buf" is still running.
536 */
537 int
538term_job_running(buf_T *buf)
539{
540 return buf->b_term != NULL && buf->b_term->tl_job != NULL
541 && buf->b_term->tl_job->jv_status == JOB_STARTED;
542}
543
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200544 static void
545position_cursor(win_T *wp, VTermPos *pos)
546{
547 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
548 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
549}
550
551static int handle_damage(VTermRect rect, void *user);
552static int handle_moverect(VTermRect dest, VTermRect src, void *user);
553static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
554static int handle_resize(int rows, int cols, void *user);
555
556static VTermScreenCallbacks screen_callbacks = {
557 handle_damage, /* damage */
558 handle_moverect, /* moverect */
559 handle_movecursor, /* movecursor */
560 NULL, /* settermprop */
561 NULL, /* bell */
562 handle_resize, /* resize */
563 NULL, /* sb_pushline */
564 NULL /* sb_popline */
565};
566
567 static int
568handle_damage(VTermRect rect, void *user)
569{
570 term_T *term = (term_T *)user;
571
572 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
573 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
574 redraw_buf_later(term->tl_buffer, NOT_VALID);
575 return 1;
576}
577
578 static int
579handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
580{
581 term_T *term = (term_T *)user;
582
583 /* TODO */
584 redraw_buf_later(term->tl_buffer, NOT_VALID);
585 return 1;
586}
587
588 static int
589handle_movecursor(
590 VTermPos pos,
591 VTermPos oldpos UNUSED,
592 int visible UNUSED,
593 void *user)
594{
595 term_T *term = (term_T *)user;
596 win_T *wp;
597 int is_current = FALSE;
598
599 FOR_ALL_WINDOWS(wp)
600 {
601 if (wp->w_buffer == term->tl_buffer)
602 {
603 position_cursor(wp, &pos);
604 if (wp == curwin)
605 is_current = TRUE;
606 }
607 }
608
609 if (is_current)
610 {
611 setcursor();
612 out_flush();
613 }
614
615 return 1;
616}
617
618/*
619 * The job running in the terminal resized the terminal.
620 */
621 static int
622handle_resize(int rows, int cols, void *user)
623{
624 term_T *term = (term_T *)user;
625 win_T *wp;
626
627 term->tl_rows = rows;
628 term->tl_cols = cols;
629 FOR_ALL_WINDOWS(wp)
630 {
631 if (wp->w_buffer == term->tl_buffer)
632 {
633 win_setheight_win(rows, wp);
634 win_setwidth_win(cols, wp);
635 }
636 }
637
638 redraw_buf_later(term->tl_buffer, NOT_VALID);
639 return 1;
640}
641
642/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200643 * Reverse engineer the RGB value into a cterm color index.
644 * First color is 1. Return 0 if no match found.
645 */
646 static int
647color2index(VTermColor *color)
648{
649 int red = color->red;
650 int blue = color->blue;
651 int green = color->green;
652
653 if (red == 0)
654 {
655 if (green == 0)
656 {
657 if (blue == 0)
658 return 1; /* black */
659 if (blue == 224)
660 return 5; /* blue */
661 }
662 else if (green == 224)
663 {
664 if (blue == 0)
665 return 3; /* green */
666 if (blue == 224)
667 return 7; /* cyan */
668 }
669 }
670 else if (red == 224)
671 {
672 if (green == 0)
673 {
674 if (blue == 0)
675 return 2; /* red */
676 if (blue == 224)
677 return 6; /* magenta */
678 }
679 else if (green == 224)
680 {
681 if (blue == 0)
682 return 4; /* yellow */
683 if (blue == 224)
684 return 8; /* white */
685 }
686 }
687 else if (red == 128)
688 {
689 if (green == 128 && blue == 128)
690 return 9; /* high intensity bladk */
691 }
692 else if (red == 255)
693 {
694 if (green == 64)
695 {
696 if (blue == 64)
697 return 10; /* high intensity red */
698 if (blue == 255)
699 return 14; /* high intensity magenta */
700 }
701 else if (green == 255)
702 {
703 if (blue == 64)
704 return 12; /* high intensity yellow */
705 if (blue == 255)
706 return 16; /* high intensity white */
707 }
708 }
709 else if (red == 64)
710 {
711 if (green == 64)
712 {
713 if (blue == 255)
714 return 13; /* high intensity blue */
715 }
716 else if (green == 255)
717 {
718 if (blue == 64)
719 return 11; /* high intensity green */
720 if (blue == 255)
721 return 15; /* high intensity cyan */
722 }
723 }
724 if (t_colors >= 256)
725 {
726 if (red == blue && red == green)
727 {
728 /* 24-color greyscale */
729 static int cutoff[23] = {
730 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
731 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
732 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
733 int i;
734
735 for (i = 0; i < 23; ++i)
736 if (red < cutoff[i])
737 return i + 233;
738 return 256;
739 }
740
741 /* 216-color cube */
742 return 17 + ((red + 25) / 0x33) * 36
743 + ((green + 25) / 0x33) * 6
744 + (blue + 25) / 0x33;
745 }
746 return 0;
747}
748
749/*
750 * Convert the attributes of a vterm cell into an attribute index.
751 */
752 static int
753cell2attr(VTermScreenCell *cell)
754{
755 int attr = 0;
756
757 if (cell->attrs.bold)
758 attr |= HL_BOLD;
759 if (cell->attrs.underline)
760 attr |= HL_UNDERLINE;
761 if (cell->attrs.italic)
762 attr |= HL_ITALIC;
763 if (cell->attrs.strike)
764 attr |= HL_STANDOUT;
765 if (cell->attrs.reverse)
766 attr |= HL_INVERSE;
767 if (cell->attrs.strike)
768 attr |= HL_UNDERLINE;
769
770#ifdef FEAT_GUI
771 if (gui.in_use)
772 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200773 guicolor_T fg, bg;
774
775 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
776 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
777 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200778 }
779 else
780#endif
781#ifdef FEAT_TERMGUICOLORS
782 if (p_tgc)
783 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200784 guicolor_T fg, bg;
785
786 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
787 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
788
789 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200790 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200791 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200792#endif
793 {
794 return get_cterm_attr_idx(attr, color2index(&cell->fg),
795 color2index(&cell->bg));
796 }
797 return 0;
798}
799
800/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200801 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200802 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200803 void
804term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200805{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200806 term_T *term = wp->w_buffer->b_term;
807 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200808 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200809 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200810 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200811
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200812 /*
813 * If the window was resized a redraw will be triggered and we get here.
814 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
815 */
816 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
817 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200818 {
819 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
820 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
821
822 vterm_set_size(vterm, rows, cols);
823 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
824 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200825 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200826 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200827
828 /* The cursor may have been moved when resizing. */
829 vterm_state_get_cursorpos(state, &pos);
830 position_cursor(wp, &pos);
831
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200832 /* TODO: Only redraw what changed. */
833 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200834 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200835 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200836 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200837
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200838 if (pos.row < term->tl_rows)
839 {
840 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200841 {
842 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200843 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200844
Bram Moolenaareeac6772017-07-23 15:48:37 +0200845 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
846 vim_memset(&cell, 0, sizeof(cell));
847
848 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200849 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200850 if (c == NUL)
851 {
852 ScreenLines[off] = ' ';
853 ScreenLinesUC[off] = NUL;
854 }
855 else
856 {
857#if defined(FEAT_MBYTE)
858 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200859 {
860 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200861 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200862 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200863 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200864 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200865 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200866 ScreenLinesUC[off] = NUL;
867 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200868#else
869 ScreenLines[off] = c;
870#endif
871 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200872 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200873
874 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200875 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200876 if (cell.width == 2)
877 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200878 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200879 ScreenLinesUC[off] = NUL;
880 ++pos.col;
881 ++off;
882 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200883 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200884 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200885 else
886 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200887
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200888 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
889 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200890 }
891}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200892
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200893/*
894 * Set job options common for Unix and MS-Windows.
895 */
896 static void
897setup_job_options(jobopt_T *opt, int rows, int cols)
898{
899 clear_job_options(opt);
900 opt->jo_mode = MODE_RAW;
901 opt->jo_out_mode = MODE_RAW;
902 opt->jo_err_mode = MODE_RAW;
903 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200904
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200905 opt->jo_io[PART_OUT] = JIO_BUFFER;
906 opt->jo_io[PART_ERR] = JIO_BUFFER;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200907 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
908
909 opt->jo_modifiable[PART_OUT] = 0;
910 opt->jo_modifiable[PART_ERR] = 0;
911 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
912
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200913 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
914 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200915 opt->jo_pty = TRUE;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200916 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
917
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200918 opt->jo_term_rows = rows;
919 opt->jo_term_cols = cols;
920}
921
922/*
923 * Create a new vterm and initialize it.
924 */
925 static void
926create_vterm(term_T *term, int rows, int cols)
927{
928 VTerm *vterm;
929 VTermScreen *screen;
930
931 vterm = vterm_new(rows, cols);
932 term->tl_vterm = vterm;
933 screen = vterm_obtain_screen(vterm);
934 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
935 /* TODO: depends on 'encoding'. */
936 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200937
938 /* Vterm uses a default black background. Set it to white when
939 * 'background' is "light". */
940 if (*p_bg == 'l')
941 {
942 VTermColor fg, bg;
943
944 fg.red = fg.green = fg.blue = 0;
945 bg.red = bg.green = bg.blue = 255;
946 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
947 }
948
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200949 /* Required to initialize most things. */
950 vterm_screen_reset(screen, 1 /* hard */);
951}
952
953# ifdef WIN3264
954
955#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
956#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
957
958void* (*winpty_config_new)(int, void*);
959void* (*winpty_open)(void*, void*);
960void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
961BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
962void (*winpty_config_set_initial_size)(void*, int, int);
963LPCWSTR (*winpty_conin_name)(void*);
964LPCWSTR (*winpty_conout_name)(void*);
965LPCWSTR (*winpty_conerr_name)(void*);
966void (*winpty_free)(void*);
967void (*winpty_config_free)(void*);
968void (*winpty_spawn_config_free)(void*);
969void (*winpty_error_free)(void*);
970LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200971BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200972
973/**************************************
974 * 2. MS-Windows implementation.
975 */
976
977#define WINPTY_DLL "winpty.dll"
978
979static HINSTANCE hWinPtyDLL = NULL;
980
981 int
982dyn_winpty_init(void)
983{
984 int i;
985 static struct
986 {
987 char *name;
988 FARPROC *ptr;
989 } winpty_entry[] =
990 {
991 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
992 {"winpty_config_free", (FARPROC*)&winpty_config_free},
993 {"winpty_config_new", (FARPROC*)&winpty_config_new},
994 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
995 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
996 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
997 {"winpty_error_free", (FARPROC*)&winpty_error_free},
998 {"winpty_free", (FARPROC*)&winpty_free},
999 {"winpty_open", (FARPROC*)&winpty_open},
1000 {"winpty_spawn", (FARPROC*)&winpty_spawn},
1001 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
1002 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
1003 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001004 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001005 {NULL, NULL}
1006 };
1007
1008 /* No need to initialize twice. */
1009 if (hWinPtyDLL)
1010 return 1;
1011 /* Load winpty.dll */
1012 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
1013 if (!hWinPtyDLL)
1014 {
1015 EMSG2(_(e_loadlib), WINPTY_DLL);
1016 return 0;
1017 }
1018 for (i = 0; winpty_entry[i].name != NULL
1019 && winpty_entry[i].ptr != NULL; ++i)
1020 {
1021 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
1022 winpty_entry[i].name)) == NULL)
1023 {
1024 EMSG2(_(e_loadfunc), winpty_entry[i].name);
1025 return 0;
1026 }
1027 }
1028
1029 return 1;
1030}
1031
1032/*
1033 * Create a new terminal of "rows" by "cols" cells.
1034 * Store a reference in "term".
1035 * Return OK or FAIL.
1036 */
1037 static int
1038term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1039{
1040 WCHAR *p = enc_to_utf16(cmd, NULL);
1041 channel_T *channel = NULL;
1042 job_T *job = NULL;
1043 jobopt_T opt;
1044 DWORD error;
1045 HANDLE jo = NULL, child_process_handle, child_thread_handle;
1046 void *winpty_err;
1047 void *spawn_config;
1048
1049 if (!dyn_winpty_init())
1050 return FAIL;
1051
1052 if (p == NULL)
1053 return FAIL;
1054
1055 job = job_alloc();
1056 if (job == NULL)
1057 goto failed;
1058
1059 channel = add_channel();
1060 if (channel == NULL)
1061 goto failed;
1062
1063 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1064 if (term->tl_winpty_config == NULL)
1065 goto failed;
1066
1067 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1068 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1069 if (term->tl_winpty == NULL)
1070 goto failed;
1071
1072 spawn_config = winpty_spawn_config_new(
1073 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1074 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1075 NULL,
1076 p,
1077 NULL,
1078 NULL,
1079 &winpty_err);
1080 if (spawn_config == NULL)
1081 goto failed;
1082
1083 channel = add_channel();
1084 if (channel == NULL)
1085 goto failed;
1086
1087 job = job_alloc();
1088 if (job == NULL)
1089 goto failed;
1090
1091 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1092 &child_thread_handle, &error, &winpty_err))
1093 goto failed;
1094
1095 channel_set_pipes(channel,
1096 (sock_T) CreateFileW(
1097 winpty_conin_name(term->tl_winpty),
1098 GENERIC_WRITE, 0, NULL,
1099 OPEN_EXISTING, 0, NULL),
1100 (sock_T) CreateFileW(
1101 winpty_conout_name(term->tl_winpty),
1102 GENERIC_READ, 0, NULL,
1103 OPEN_EXISTING, 0, NULL),
1104 (sock_T) CreateFileW(
1105 winpty_conerr_name(term->tl_winpty),
1106 GENERIC_READ, 0, NULL,
1107 OPEN_EXISTING, 0, NULL));
1108
1109 jo = CreateJobObject(NULL, NULL);
1110 if (jo == NULL)
1111 goto failed;
1112
1113 if (!AssignProcessToJobObject(jo, child_process_handle))
1114 goto failed;
1115
1116 winpty_spawn_config_free(spawn_config);
1117
1118 create_vterm(term, rows, cols);
1119
1120 setup_job_options(&opt, rows, cols);
1121 channel_set_job(channel, job, &opt);
1122
1123 job->jv_channel = channel;
1124 job->jv_proc_info.hProcess = child_process_handle;
1125 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1126 job->jv_job_object = jo;
1127 job->jv_status = JOB_STARTED;
1128 term->tl_job = job;
1129
1130 return OK;
1131
1132failed:
1133 if (channel != NULL)
1134 channel_clear(channel);
1135 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001136 {
1137 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001138 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001139 }
1140 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001141 if (jo != NULL)
1142 CloseHandle(jo);
1143 if (term->tl_winpty != NULL)
1144 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001145 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001146 if (term->tl_winpty_config != NULL)
1147 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001148 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001149 if (winpty_err != NULL)
1150 {
1151 char_u *msg = utf16_to_enc(
1152 (short_u *)winpty_error_msg(winpty_err), NULL);
1153
1154 EMSG(msg);
1155 winpty_error_free(winpty_err);
1156 }
1157 return FAIL;
1158}
1159
1160/*
1161 * Free the terminal emulator part of "term".
1162 */
1163 static void
1164term_free(term_T *term)
1165{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001166 if (term->tl_winpty != NULL)
1167 winpty_free(term->tl_winpty);
1168 if (term->tl_winpty_config != NULL)
1169 winpty_config_free(term->tl_winpty_config);
1170 if (term->tl_vterm != NULL)
1171 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001172}
1173
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001174/*
1175 * Request size to terminal.
1176 */
1177 static void
1178term_report_winsize(term_T *term, int rows, int cols)
1179{
1180 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1181}
1182
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001183# else
1184
1185/**************************************
1186 * 3. Unix-like implementation.
1187 */
1188
1189/*
1190 * Create a new terminal of "rows" by "cols" cells.
1191 * Start job for "cmd".
1192 * Store the pointers in "term".
1193 * Return OK or FAIL.
1194 */
1195 static int
1196term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1197{
1198 typval_T argvars[2];
1199 jobopt_T opt;
1200
1201 create_vterm(term, rows, cols);
1202
1203 argvars[0].v_type = VAR_STRING;
1204 argvars[0].vval.v_string = cmd;
1205 setup_job_options(&opt, rows, cols);
1206 term->tl_job = job_start(argvars, &opt);
1207
Bram Moolenaar61a66052017-07-22 18:39:00 +02001208 return term->tl_job != NULL
1209 && term->tl_job->jv_channel != NULL
1210 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001211}
1212
1213/*
1214 * Free the terminal emulator part of "term".
1215 */
1216 static void
1217term_free(term_T *term)
1218{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001219 if (term->tl_vterm != NULL)
1220 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001221}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001222
1223/*
1224 * Request size to terminal.
1225 */
1226 static void
1227term_report_winsize(term_T *term, int rows, int cols)
1228{
1229 /* Use an ioctl() to report the new window size to the job. */
1230 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1231 {
1232 int fd = -1;
1233 int part;
1234
1235 for (part = PART_OUT; part < PART_COUNT; ++part)
1236 {
1237 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1238 if (isatty(fd))
1239 break;
1240 }
1241 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1242 mch_stop_job(term->tl_job, (char_u *)"winch");
1243 }
1244}
1245
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001246# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001247
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001248#endif /* FEAT_TERMINAL */