blob: 3f1a91d1542cb39ff94ab947a9c98fa9a7f539af [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 Moolenaareeac6772017-07-23 15:48:37 +020036 * - cursor flickers when moving the cursor
Bram Moolenaare4f25e42017-07-07 11:54:15 +020037 * - set buffer options to be scratch, hidden, nomodifiable, etc.
38 * - set buffer name to command, add (1) to avoid duplicates.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020039 * - Add a scrollback buffer (contains lines to scroll off the top).
40 * Can use the buf_T lines, store attributes somewhere else?
41 * - When the job ends:
42 * - Write "-- JOB ENDED --" in the terminal.
43 * - Put the terminal contents in the scrollback buffer.
44 * - Free the terminal emulator.
45 * - Display the scrollback buffer (but with attributes).
46 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020047 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020048 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020049 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020050 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020051 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020052 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020053 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020054 * - implement "term" for job_start(): more job options when starting a
55 * terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020056 * - implement ":buf {term-buf-name}"
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020057 * - implement term_list() list of buffers with a terminal
58 * - implement term_getsize(buf)
59 * - implement term_setsize(buf)
60 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
61 * - implement term_wait(buf) wait for screen to be updated
62 * - implement term_scrape(buf, row) inspect terminal screen
63 * - implement term_open(command, options) open terminal window
64 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020065 * - implement 'termkey'
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
67 * conversions.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068 */
69
70#include "vim.h"
71
72#ifdef FEAT_TERMINAL
73
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020074#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020075# define MIN(x,y) (x < y ? x : y)
76# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081/* typedef term_T in structs.h */
82struct terminal_S {
83 term_T *tl_next;
84
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020086 void *tl_winpty_config;
87 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020091 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020093 /* last known vterm size */
94 int tl_rows;
95 int tl_cols;
96 /* vterm size does not follow window size */
97 int tl_rows_fixed;
98 int tl_cols_fixed;
99
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200100 /* Range of screen rows to update. Zero based. */
101 int tl_dirty_row_start; /* -1 if nothing dirty */
102 int tl_dirty_row_end; /* row below last one to update */
103
104 pos_T tl_cursor;
105};
106
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200107/*
108 * List of all active terminals.
109 */
110static term_T *first_term = NULL;
111
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112
113#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
114#define KEY_BUF_LEN 200
115
116/*
117 * Functions with separate implementation for MS-Windows and Unix-like systems.
118 */
119static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200120static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200121static void term_free(term_T *term);
122
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123/**************************************
124 * 1. Generic code for all systems.
125 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126
127/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200128 * Determine the terminal size from 'termsize' and the current window.
129 * Assumes term->tl_rows and term->tl_cols are zero.
130 */
131 static void
132set_term_and_win_size(term_T *term)
133{
134 if (*curwin->w_p_tms != NUL)
135 {
136 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
137
138 term->tl_rows = atoi((char *)curwin->w_p_tms);
139 term->tl_cols = atoi((char *)p);
140 }
141 if (term->tl_rows == 0)
142 term->tl_rows = curwin->w_height;
143 else
144 {
145 win_setheight_win(term->tl_rows, curwin);
146 term->tl_rows_fixed = TRUE;
147 }
148 if (term->tl_cols == 0)
149 term->tl_cols = curwin->w_width;
150 else
151 {
152 win_setwidth_win(term->tl_cols, curwin);
153 term->tl_cols_fixed = TRUE;
154 }
155}
156
157/*
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200158 * ":terminal": open a terminal window and execute a job in it.
159 */
160 void
161ex_terminal(exarg_T *eap)
162{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200163 exarg_T split_ea;
164 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200165 term_T *term;
Bram Moolenaare173fd02017-07-22 19:03:32 +0200166 char_u *cmd = eap->arg;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200167
168 if (check_restricted() || check_secure())
169 return;
170
171 term = (term_T *)alloc_clear(sizeof(term_T));
172 if (term == NULL)
173 return;
174 term->tl_dirty_row_end = MAX_ROW;
175
176 /* Open a new window or tab. */
177 vim_memset(&split_ea, 0, sizeof(split_ea));
178 split_ea.cmdidx = CMD_new;
179 split_ea.cmd = (char_u *)"new";
180 split_ea.arg = (char_u *)"";
181 ex_splitview(&split_ea);
182 if (curwin == old_curwin)
183 {
184 /* split failed */
185 vim_free(term);
186 return;
187 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200188 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200189 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200190
191 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200192 term->tl_next = first_term;
193 first_term = term;
194
195 /* TODO: set buffer type, hidden, etc. */
196
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200197 set_term_and_win_size(term);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200198
Bram Moolenaare173fd02017-07-22 19:03:32 +0200199 if (cmd == NULL || *cmd == NUL)
200 cmd = p_sh;
201
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200202 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaare173fd02017-07-22 19:03:32 +0200203 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200204 {
205 /* store the size we ended up with */
206 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 }
208 else
209 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200210 free_terminal(term);
211 curbuf->b_term = NULL;
212
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200213 /* Wiping out the buffer will also close the window and call
214 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200215 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200216 }
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200217
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200218 /* TODO: Setup pty, see mch_call_shell(). */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200219}
220
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200221/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200222 * Free a terminal and everything it refers to.
223 * Kills the job if there is one.
224 * Called when wiping out a buffer.
225 */
226 void
227free_terminal(term_T *term)
228{
229 term_T *tp;
230
231 if (term == NULL)
232 return;
233 if (first_term == term)
234 first_term = term->tl_next;
235 else
236 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
237 if (tp->tl_next == term)
238 {
239 tp->tl_next = term->tl_next;
240 break;
241 }
242
243 if (term->tl_job != NULL)
244 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200245 if (term->tl_job->jv_status != JOB_ENDED
246 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200247 job_stop(term->tl_job, NULL, "kill");
248 job_unref(term->tl_job);
249 }
250
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200251 term_free(term);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200252 vim_free(term);
253}
254
255/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200256 * Write job output "msg[len]" to the vterm.
257 */
258 static void
259term_write_job_output(term_T *term, char_u *msg, size_t len)
260{
261 VTerm *vterm = term->tl_vterm;
262 char_u *p;
263 size_t done;
264 size_t len_now;
265
266 for (done = 0; done < len; done += len_now)
267 {
268 for (p = msg + done; p < msg + len; )
269 {
270 if (*p == NL)
271 break;
272 p += utf_ptr2len_len(p, len - (p - msg));
273 }
274 len_now = p - msg - done;
275 vterm_input_write(vterm, (char *)msg + done, len_now);
276 if (p < msg + len && *p == NL)
277 {
278 /* Convert NL to CR-NL, that appears to work best. */
279 vterm_input_write(vterm, "\r\n", 2);
280 ++len_now;
281 }
282 }
283
284 /* this invokes the damage callbacks */
285 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
286}
287
288/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200289 * Invoked when "msg" output from a job was received. Write it to the terminal
290 * of "buffer".
291 */
292 void
293write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
294{
295 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200296 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200297
298 ch_logn(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200299 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200300
301 /* TODO: only update once in a while. */
302 update_screen(0);
303 setcursor();
304 out_flush();
305}
306
307/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200308 * Convert typed key "c" into bytes to send to the job.
309 * Return the number of bytes in "buf".
310 */
311 static int
312term_convert_key(int c, char *buf)
313{
314 VTerm *vterm = curbuf->b_term->tl_vterm;
315 VTermKey key = VTERM_KEY_NONE;
316 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200317
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200318 switch (c)
319 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200320 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200321 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200322 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
323 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200324 case K_DEL: key = VTERM_KEY_DEL; break;
325 case K_DOWN: key = VTERM_KEY_DOWN; break;
326 case K_END: key = VTERM_KEY_END; break;
327 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
328 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
329 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
330 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
331 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
332 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
333 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
334 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
335 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
336 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
337 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
338 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
339 case K_HOME: key = VTERM_KEY_HOME; break;
340 case K_INS: key = VTERM_KEY_INS; break;
341 case K_K0: key = VTERM_KEY_KP_0; break;
342 case K_K1: key = VTERM_KEY_KP_1; break;
343 case K_K2: key = VTERM_KEY_KP_2; break;
344 case K_K3: key = VTERM_KEY_KP_3; break;
345 case K_K4: key = VTERM_KEY_KP_4; break;
346 case K_K5: key = VTERM_KEY_KP_5; break;
347 case K_K6: key = VTERM_KEY_KP_6; break;
348 case K_K7: key = VTERM_KEY_KP_7; break;
349 case K_K8: key = VTERM_KEY_KP_8; break;
350 case K_K9: key = VTERM_KEY_KP_9; break;
351 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
352 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
353 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
354 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
355 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
356 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
357 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
358 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
359 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
360 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
361 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
362 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
363 case K_LEFT: key = VTERM_KEY_LEFT; break;
364 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
365 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
366 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
367 case K_UP: key = VTERM_KEY_UP; break;
368 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200369
370 case K_MOUSEUP: /* TODO */ break;
371 case K_MOUSEDOWN: /* TODO */ break;
372 case K_MOUSELEFT: /* TODO */ break;
373 case K_MOUSERIGHT: /* TODO */ break;
374
375 case K_LEFTMOUSE: /* TODO */ break;
376 case K_LEFTMOUSE_NM: /* TODO */ break;
377 case K_LEFTDRAG: /* TODO */ break;
378 case K_LEFTRELEASE: /* TODO */ break;
379 case K_LEFTRELEASE_NM: /* TODO */ break;
380 case K_MIDDLEMOUSE: /* TODO */ break;
381 case K_MIDDLEDRAG: /* TODO */ break;
382 case K_MIDDLERELEASE: /* TODO */ break;
383 case K_RIGHTMOUSE: /* TODO */ break;
384 case K_RIGHTDRAG: /* TODO */ break;
385 case K_RIGHTRELEASE: /* TODO */ break;
386 case K_X1MOUSE: /* TODO */ break;
387 case K_X1DRAG: /* TODO */ break;
388 case K_X1RELEASE: /* TODO */ break;
389 case K_X2MOUSE: /* TODO */ break;
390 case K_X2DRAG: /* TODO */ break;
391 case K_X2RELEASE: /* TODO */ break;
392
393 /* TODO: handle all special keys and modifiers that terminal_loop()
394 * does not handle. */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200395 }
396
397 /*
398 * Convert special keys to vterm keys:
399 * - Write keys to vterm: vterm_keyboard_key()
400 * - Write output to channel.
401 */
402 if (key != VTERM_KEY_NONE)
403 /* Special key, let vterm convert it. */
404 vterm_keyboard_key(vterm, key, mod);
405 else
406 /* Normal character, let vterm convert it. */
407 vterm_keyboard_unichar(vterm, c, mod);
408
409 /* Read back the converted escape sequence. */
410 return vterm_output_read(vterm, buf, KEY_BUF_LEN);
411}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200412
Bram Moolenaar938783d2017-07-16 20:13:26 +0200413/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200414 * Wait for input and send it to the job.
415 * Return when the start of a CTRL-W command is typed or anything else that
416 * should be handled as a Normal mode command.
417 */
418 void
419terminal_loop(void)
420{
421 char buf[KEY_BUF_LEN];
422 int c;
423 size_t len;
424 static int mouse_was_outside = FALSE;
425 int dragging_outside = FALSE;
426
427 for (;;)
428 {
429 /* TODO: skip screen update when handling a sequence of keys. */
430 update_screen(0);
431 setcursor();
432 out_flush();
433 ++no_mapping;
434 ++allow_keys;
435 got_int = FALSE;
436 c = vgetc();
437 --no_mapping;
438 --allow_keys;
439
440 /* Catch keys that need to be handled as in Normal mode. */
441 switch (c)
442 {
443 case Ctrl_W:
444 case NUL:
445 case K_ZERO:
446 stuffcharReadbuff(c);
447 return;
448
449 case K_IGNORE: continue;
450
451 case K_LEFTDRAG:
452 case K_MIDDLEDRAG:
453 case K_RIGHTDRAG:
454 case K_X1DRAG:
455 case K_X2DRAG:
456 dragging_outside = mouse_was_outside;
457 /* FALLTHROUGH */
458 case K_LEFTMOUSE:
459 case K_LEFTMOUSE_NM:
460 case K_LEFTRELEASE:
461 case K_LEFTRELEASE_NM:
462 case K_MIDDLEMOUSE:
463 case K_MIDDLERELEASE:
464 case K_RIGHTMOUSE:
465 case K_RIGHTRELEASE:
466 case K_X1MOUSE:
467 case K_X1RELEASE:
468 case K_X2MOUSE:
469 case K_X2RELEASE:
470 if (mouse_row < W_WINROW(curwin)
471 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
472 || mouse_col < W_WINCOL(curwin)
473 || mouse_col >= W_ENDCOL(curwin)
474 || dragging_outside)
475 {
476 /* click outside the current window */
477 stuffcharReadbuff(c);
478 mouse_was_outside = TRUE;
479 return;
480 }
481 }
482 mouse_was_outside = FALSE;
483
484 /* Convert the typed key to a sequence of bytes for the job. */
485 len = term_convert_key(c, buf);
486 if (len > 0)
487 /* TODO: if FAIL is returned, stop? */
488 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
489 (char_u *)buf, len, NULL);
490 }
491}
492
493 static void
494position_cursor(win_T *wp, VTermPos *pos)
495{
496 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
497 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
498}
499
500static int handle_damage(VTermRect rect, void *user);
501static int handle_moverect(VTermRect dest, VTermRect src, void *user);
502static int handle_movecursor(VTermPos pos, VTermPos oldpos, int visible, void *user);
503static int handle_resize(int rows, int cols, void *user);
504
505static VTermScreenCallbacks screen_callbacks = {
506 handle_damage, /* damage */
507 handle_moverect, /* moverect */
508 handle_movecursor, /* movecursor */
509 NULL, /* settermprop */
510 NULL, /* bell */
511 handle_resize, /* resize */
512 NULL, /* sb_pushline */
513 NULL /* sb_popline */
514};
515
516 static int
517handle_damage(VTermRect rect, void *user)
518{
519 term_T *term = (term_T *)user;
520
521 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
522 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
523 redraw_buf_later(term->tl_buffer, NOT_VALID);
524 return 1;
525}
526
527 static int
528handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
529{
530 term_T *term = (term_T *)user;
531
532 /* TODO */
533 redraw_buf_later(term->tl_buffer, NOT_VALID);
534 return 1;
535}
536
537 static int
538handle_movecursor(
539 VTermPos pos,
540 VTermPos oldpos UNUSED,
541 int visible UNUSED,
542 void *user)
543{
544 term_T *term = (term_T *)user;
545 win_T *wp;
546 int is_current = FALSE;
547
548 FOR_ALL_WINDOWS(wp)
549 {
550 if (wp->w_buffer == term->tl_buffer)
551 {
552 position_cursor(wp, &pos);
553 if (wp == curwin)
554 is_current = TRUE;
555 }
556 }
557
558 if (is_current)
559 {
560 setcursor();
561 out_flush();
562 }
563
564 return 1;
565}
566
567/*
568 * The job running in the terminal resized the terminal.
569 */
570 static int
571handle_resize(int rows, int cols, void *user)
572{
573 term_T *term = (term_T *)user;
574 win_T *wp;
575
576 term->tl_rows = rows;
577 term->tl_cols = cols;
578 FOR_ALL_WINDOWS(wp)
579 {
580 if (wp->w_buffer == term->tl_buffer)
581 {
582 win_setheight_win(rows, wp);
583 win_setwidth_win(cols, wp);
584 }
585 }
586
587 redraw_buf_later(term->tl_buffer, NOT_VALID);
588 return 1;
589}
590
591/*
Bram Moolenaareeac6772017-07-23 15:48:37 +0200592 * Reverse engineer the RGB value into a cterm color index.
593 * First color is 1. Return 0 if no match found.
594 */
595 static int
596color2index(VTermColor *color)
597{
598 int red = color->red;
599 int blue = color->blue;
600 int green = color->green;
601
602 if (red == 0)
603 {
604 if (green == 0)
605 {
606 if (blue == 0)
607 return 1; /* black */
608 if (blue == 224)
609 return 5; /* blue */
610 }
611 else if (green == 224)
612 {
613 if (blue == 0)
614 return 3; /* green */
615 if (blue == 224)
616 return 7; /* cyan */
617 }
618 }
619 else if (red == 224)
620 {
621 if (green == 0)
622 {
623 if (blue == 0)
624 return 2; /* red */
625 if (blue == 224)
626 return 6; /* magenta */
627 }
628 else if (green == 224)
629 {
630 if (blue == 0)
631 return 4; /* yellow */
632 if (blue == 224)
633 return 8; /* white */
634 }
635 }
636 else if (red == 128)
637 {
638 if (green == 128 && blue == 128)
639 return 9; /* high intensity bladk */
640 }
641 else if (red == 255)
642 {
643 if (green == 64)
644 {
645 if (blue == 64)
646 return 10; /* high intensity red */
647 if (blue == 255)
648 return 14; /* high intensity magenta */
649 }
650 else if (green == 255)
651 {
652 if (blue == 64)
653 return 12; /* high intensity yellow */
654 if (blue == 255)
655 return 16; /* high intensity white */
656 }
657 }
658 else if (red == 64)
659 {
660 if (green == 64)
661 {
662 if (blue == 255)
663 return 13; /* high intensity blue */
664 }
665 else if (green == 255)
666 {
667 if (blue == 64)
668 return 11; /* high intensity green */
669 if (blue == 255)
670 return 15; /* high intensity cyan */
671 }
672 }
673 if (t_colors >= 256)
674 {
675 if (red == blue && red == green)
676 {
677 /* 24-color greyscale */
678 static int cutoff[23] = {
679 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
680 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
681 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
682 int i;
683
684 for (i = 0; i < 23; ++i)
685 if (red < cutoff[i])
686 return i + 233;
687 return 256;
688 }
689
690 /* 216-color cube */
691 return 17 + ((red + 25) / 0x33) * 36
692 + ((green + 25) / 0x33) * 6
693 + (blue + 25) / 0x33;
694 }
695 return 0;
696}
697
698/*
699 * Convert the attributes of a vterm cell into an attribute index.
700 */
701 static int
702cell2attr(VTermScreenCell *cell)
703{
704 int attr = 0;
705
706 if (cell->attrs.bold)
707 attr |= HL_BOLD;
708 if (cell->attrs.underline)
709 attr |= HL_UNDERLINE;
710 if (cell->attrs.italic)
711 attr |= HL_ITALIC;
712 if (cell->attrs.strike)
713 attr |= HL_STANDOUT;
714 if (cell->attrs.reverse)
715 attr |= HL_INVERSE;
716 if (cell->attrs.strike)
717 attr |= HL_UNDERLINE;
718
719#ifdef FEAT_GUI
720 if (gui.in_use)
721 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +0200722 guicolor_T fg, bg;
723
724 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
725 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
726 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200727 }
728 else
729#endif
730#ifdef FEAT_TERMGUICOLORS
731 if (p_tgc)
732 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200733 guicolor_T fg, bg;
734
735 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
736 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
737
738 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200739 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +0200740 else
Bram Moolenaareeac6772017-07-23 15:48:37 +0200741#endif
742 {
743 return get_cterm_attr_idx(attr, color2index(&cell->fg),
744 color2index(&cell->bg));
745 }
746 return 0;
747}
748
749/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200750 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200751 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200752 void
753term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200754{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200755 term_T *term = wp->w_buffer->b_term;
756 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200757 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200758 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200759 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200760
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200761 /*
762 * If the window was resized a redraw will be triggered and we get here.
763 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
764 */
765 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
766 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200767 {
768 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
769 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
770
771 vterm_set_size(vterm, rows, cols);
772 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
773 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200774 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200775 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200776
777 /* The cursor may have been moved when resizing. */
778 vterm_state_get_cursorpos(state, &pos);
779 position_cursor(wp, &pos);
780
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200781 /* TODO: Only redraw what changed. */
782 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200783 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200784 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200785 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200786
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200787 if (pos.row < term->tl_rows)
788 {
789 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200790 {
791 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200792 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200793
Bram Moolenaareeac6772017-07-23 15:48:37 +0200794 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
795 vim_memset(&cell, 0, sizeof(cell));
796
797 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200798 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200799 if (c == NUL)
800 {
801 ScreenLines[off] = ' ';
802 ScreenLinesUC[off] = NUL;
803 }
804 else
805 {
806#if defined(FEAT_MBYTE)
807 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200808 {
809 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200810 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200811 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200812 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200813 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200814 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200815 ScreenLinesUC[off] = NUL;
816 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200817#else
818 ScreenLines[off] = c;
819#endif
820 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200821 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200822
823 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200824 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200825 if (cell.width == 2)
826 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200827 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200828 ScreenLinesUC[off] = NUL;
829 ++pos.col;
830 ++off;
831 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200832 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200833 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200834 else
835 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200836
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200837 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
838 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200839 }
840}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200841
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200842/*
843 * Set job options common for Unix and MS-Windows.
844 */
845 static void
846setup_job_options(jobopt_T *opt, int rows, int cols)
847{
848 clear_job_options(opt);
849 opt->jo_mode = MODE_RAW;
850 opt->jo_out_mode = MODE_RAW;
851 opt->jo_err_mode = MODE_RAW;
852 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
853 opt->jo_io[PART_OUT] = JIO_BUFFER;
854 opt->jo_io[PART_ERR] = JIO_BUFFER;
855 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
856 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
857 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200858 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200859 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
860 opt->jo_term_rows = rows;
861 opt->jo_term_cols = cols;
862}
863
864/*
865 * Create a new vterm and initialize it.
866 */
867 static void
868create_vterm(term_T *term, int rows, int cols)
869{
870 VTerm *vterm;
871 VTermScreen *screen;
872
873 vterm = vterm_new(rows, cols);
874 term->tl_vterm = vterm;
875 screen = vterm_obtain_screen(vterm);
876 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
877 /* TODO: depends on 'encoding'. */
878 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200879
880 /* Vterm uses a default black background. Set it to white when
881 * 'background' is "light". */
882 if (*p_bg == 'l')
883 {
884 VTermColor fg, bg;
885
886 fg.red = fg.green = fg.blue = 0;
887 bg.red = bg.green = bg.blue = 255;
888 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
889 }
890
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200891 /* Required to initialize most things. */
892 vterm_screen_reset(screen, 1 /* hard */);
893}
894
895# ifdef WIN3264
896
897#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
898#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
899
900void* (*winpty_config_new)(int, void*);
901void* (*winpty_open)(void*, void*);
902void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
903BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
904void (*winpty_config_set_initial_size)(void*, int, int);
905LPCWSTR (*winpty_conin_name)(void*);
906LPCWSTR (*winpty_conout_name)(void*);
907LPCWSTR (*winpty_conerr_name)(void*);
908void (*winpty_free)(void*);
909void (*winpty_config_free)(void*);
910void (*winpty_spawn_config_free)(void*);
911void (*winpty_error_free)(void*);
912LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200913BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200914
915/**************************************
916 * 2. MS-Windows implementation.
917 */
918
919#define WINPTY_DLL "winpty.dll"
920
921static HINSTANCE hWinPtyDLL = NULL;
922
923 int
924dyn_winpty_init(void)
925{
926 int i;
927 static struct
928 {
929 char *name;
930 FARPROC *ptr;
931 } winpty_entry[] =
932 {
933 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
934 {"winpty_config_free", (FARPROC*)&winpty_config_free},
935 {"winpty_config_new", (FARPROC*)&winpty_config_new},
936 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
937 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
938 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
939 {"winpty_error_free", (FARPROC*)&winpty_error_free},
940 {"winpty_free", (FARPROC*)&winpty_free},
941 {"winpty_open", (FARPROC*)&winpty_open},
942 {"winpty_spawn", (FARPROC*)&winpty_spawn},
943 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
944 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
945 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200946 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200947 {NULL, NULL}
948 };
949
950 /* No need to initialize twice. */
951 if (hWinPtyDLL)
952 return 1;
953 /* Load winpty.dll */
954 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
955 if (!hWinPtyDLL)
956 {
957 EMSG2(_(e_loadlib), WINPTY_DLL);
958 return 0;
959 }
960 for (i = 0; winpty_entry[i].name != NULL
961 && winpty_entry[i].ptr != NULL; ++i)
962 {
963 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
964 winpty_entry[i].name)) == NULL)
965 {
966 EMSG2(_(e_loadfunc), winpty_entry[i].name);
967 return 0;
968 }
969 }
970
971 return 1;
972}
973
974/*
975 * Create a new terminal of "rows" by "cols" cells.
976 * Store a reference in "term".
977 * Return OK or FAIL.
978 */
979 static int
980term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
981{
982 WCHAR *p = enc_to_utf16(cmd, NULL);
983 channel_T *channel = NULL;
984 job_T *job = NULL;
985 jobopt_T opt;
986 DWORD error;
987 HANDLE jo = NULL, child_process_handle, child_thread_handle;
988 void *winpty_err;
989 void *spawn_config;
990
991 if (!dyn_winpty_init())
992 return FAIL;
993
994 if (p == NULL)
995 return FAIL;
996
997 job = job_alloc();
998 if (job == NULL)
999 goto failed;
1000
1001 channel = add_channel();
1002 if (channel == NULL)
1003 goto failed;
1004
1005 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1006 if (term->tl_winpty_config == NULL)
1007 goto failed;
1008
1009 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1010 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1011 if (term->tl_winpty == NULL)
1012 goto failed;
1013
1014 spawn_config = winpty_spawn_config_new(
1015 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1016 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1017 NULL,
1018 p,
1019 NULL,
1020 NULL,
1021 &winpty_err);
1022 if (spawn_config == NULL)
1023 goto failed;
1024
1025 channel = add_channel();
1026 if (channel == NULL)
1027 goto failed;
1028
1029 job = job_alloc();
1030 if (job == NULL)
1031 goto failed;
1032
1033 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1034 &child_thread_handle, &error, &winpty_err))
1035 goto failed;
1036
1037 channel_set_pipes(channel,
1038 (sock_T) CreateFileW(
1039 winpty_conin_name(term->tl_winpty),
1040 GENERIC_WRITE, 0, NULL,
1041 OPEN_EXISTING, 0, NULL),
1042 (sock_T) CreateFileW(
1043 winpty_conout_name(term->tl_winpty),
1044 GENERIC_READ, 0, NULL,
1045 OPEN_EXISTING, 0, NULL),
1046 (sock_T) CreateFileW(
1047 winpty_conerr_name(term->tl_winpty),
1048 GENERIC_READ, 0, NULL,
1049 OPEN_EXISTING, 0, NULL));
1050
1051 jo = CreateJobObject(NULL, NULL);
1052 if (jo == NULL)
1053 goto failed;
1054
1055 if (!AssignProcessToJobObject(jo, child_process_handle))
1056 goto failed;
1057
1058 winpty_spawn_config_free(spawn_config);
1059
1060 create_vterm(term, rows, cols);
1061
1062 setup_job_options(&opt, rows, cols);
1063 channel_set_job(channel, job, &opt);
1064
1065 job->jv_channel = channel;
1066 job->jv_proc_info.hProcess = child_process_handle;
1067 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1068 job->jv_job_object = jo;
1069 job->jv_status = JOB_STARTED;
1070 term->tl_job = job;
1071
1072 return OK;
1073
1074failed:
1075 if (channel != NULL)
1076 channel_clear(channel);
1077 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001078 {
1079 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001080 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001081 }
1082 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001083 if (jo != NULL)
1084 CloseHandle(jo);
1085 if (term->tl_winpty != NULL)
1086 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001087 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001088 if (term->tl_winpty_config != NULL)
1089 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001090 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091 if (winpty_err != NULL)
1092 {
1093 char_u *msg = utf16_to_enc(
1094 (short_u *)winpty_error_msg(winpty_err), NULL);
1095
1096 EMSG(msg);
1097 winpty_error_free(winpty_err);
1098 }
1099 return FAIL;
1100}
1101
1102/*
1103 * Free the terminal emulator part of "term".
1104 */
1105 static void
1106term_free(term_T *term)
1107{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001108 if (term->tl_winpty != NULL)
1109 winpty_free(term->tl_winpty);
1110 if (term->tl_winpty_config != NULL)
1111 winpty_config_free(term->tl_winpty_config);
1112 if (term->tl_vterm != NULL)
1113 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001114}
1115
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001116/*
1117 * Request size to terminal.
1118 */
1119 static void
1120term_report_winsize(term_T *term, int rows, int cols)
1121{
1122 winpty_set_size(term->tl_winpty, cols, rows, NULL);
1123}
1124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001125# else
1126
1127/**************************************
1128 * 3. Unix-like implementation.
1129 */
1130
1131/*
1132 * Create a new terminal of "rows" by "cols" cells.
1133 * Start job for "cmd".
1134 * Store the pointers in "term".
1135 * Return OK or FAIL.
1136 */
1137 static int
1138term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1139{
1140 typval_T argvars[2];
1141 jobopt_T opt;
1142
1143 create_vterm(term, rows, cols);
1144
1145 argvars[0].v_type = VAR_STRING;
1146 argvars[0].vval.v_string = cmd;
1147 setup_job_options(&opt, rows, cols);
1148 term->tl_job = job_start(argvars, &opt);
1149
Bram Moolenaar61a66052017-07-22 18:39:00 +02001150 return term->tl_job != NULL
1151 && term->tl_job->jv_channel != NULL
1152 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001153}
1154
1155/*
1156 * Free the terminal emulator part of "term".
1157 */
1158 static void
1159term_free(term_T *term)
1160{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02001161 if (term->tl_vterm != NULL)
1162 vterm_free(term->tl_vterm);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001163}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001164
1165/*
1166 * Request size to terminal.
1167 */
1168 static void
1169term_report_winsize(term_T *term, int rows, int cols)
1170{
1171 /* Use an ioctl() to report the new window size to the job. */
1172 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
1173 {
1174 int fd = -1;
1175 int part;
1176
1177 for (part = PART_OUT; part < PART_COUNT; ++part)
1178 {
1179 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
1180 if (isatty(fd))
1181 break;
1182 }
1183 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
1184 mch_stop_job(term->tl_job, (char_u *)"winch");
1185 }
1186}
1187
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001188# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001189
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001190#endif /* FEAT_TERMINAL */