blob: 5fbfc2eea41242e72a29c00cdf8fb50056ba0fc6 [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 * - color for 'termguicolors'
37 * - cursor flickers when moving the cursor
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * - set buffer options to be scratch, hidden, nomodifiable, etc.
39 * - set buffer name to command, add (1) to avoid duplicates.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020040 * - Add a scrollback buffer (contains lines to scroll off the top).
41 * Can use the buf_T lines, store attributes somewhere else?
42 * - When the job ends:
43 * - Write "-- JOB ENDED --" in the terminal.
44 * - Put the terminal contents in the scrollback buffer.
45 * - Free the terminal emulator.
46 * - Display the scrollback buffer (but with attributes).
47 * Make the buffer not modifiable, drop attributes when making changes.
Bram Moolenaar938783d2017-07-16 20:13:26 +020048 * - when closing window and job has not ended, make terminal hidden?
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +020049 * - don't allow exiting Vim when a terminal is still running a job
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar938783d2017-07-16 20:13:26 +020051 * - command line completion for :terminal
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020053 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020055 * - implement "term" for job_start(): more job options when starting a
56 * terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020057 * - implement ":buf {term-buf-name}"
Bram Moolenaar96ca27a2017-07-17 23:20:24 +020058 * - implement term_list() list of buffers with a terminal
59 * - implement term_getsize(buf)
60 * - implement term_setsize(buf)
61 * - implement term_sendkeys(buf, keys) send keystrokes to a terminal
62 * - implement term_wait(buf) wait for screen to be updated
63 * - implement term_scrape(buf, row) inspect terminal screen
64 * - implement term_open(command, options) open terminal window
65 * - implement term_getjob(buf)
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020066 * - implement 'termkey'
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
68 * conversions.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020069 */
70
71#include "vim.h"
72
73#ifdef FEAT_TERMINAL
74
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020075#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020076# define MIN(x,y) (x < y ? x : y)
77# define MAX(x,y) (x > y ? x : y)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082/* typedef term_T in structs.h */
83struct terminal_S {
84 term_T *tl_next;
85
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020086#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087 void *tl_winpty_config;
88 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020089#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090 VTerm *tl_vterm;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091 job_T *tl_job;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +020092 buf_T *tl_buffer;
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020094 /* last known vterm size */
95 int tl_rows;
96 int tl_cols;
97 /* vterm size does not follow window size */
98 int tl_rows_fixed;
99 int tl_cols_fixed;
100
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101 /* Range of screen rows to update. Zero based. */
102 int tl_dirty_row_start; /* -1 if nothing dirty */
103 int tl_dirty_row_end; /* row below last one to update */
104
105 pos_T tl_cursor;
106};
107
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200108/*
109 * List of all active terminals.
110 */
111static term_T *first_term = NULL;
112
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200113
114#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
115#define KEY_BUF_LEN 200
116
117/*
118 * Functions with separate implementation for MS-Windows and Unix-like systems.
119 */
120static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd);
121static 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 {
733 /* TODO */
734 }
735#endif
736 {
737 return get_cterm_attr_idx(attr, color2index(&cell->fg),
738 color2index(&cell->bg));
739 }
740 return 0;
741}
742
743/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200744 * Called to update the window that contains the terminal.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200745 */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200746 void
747term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200748{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200749 term_T *term = wp->w_buffer->b_term;
750 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200751 VTermScreen *screen = vterm_obtain_screen(vterm);
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200752 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200753 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200754
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200755 /*
756 * If the window was resized a redraw will be triggered and we get here.
757 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
758 */
759 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
760 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +0200761 {
762 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
763 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
764
765 vterm_set_size(vterm, rows, cols);
766 ch_logn(term->tl_job->jv_channel, "Resizing terminal to %d lines",
767 rows);
768
769#if defined(UNIX)
770 /* Use an ioctl() to report the new window size to the job. */
771 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
772 {
773 int fd = -1;
774 int part;
775
776 for (part = PART_OUT; part < PART_COUNT; ++part)
777 {
778 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
779 if (isatty(fd))
780 break;
781 }
782 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
783 mch_stop_job(term->tl_job, (char_u *)"winch");
784 }
785#endif
786 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +0200787
788 /* The cursor may have been moved when resizing. */
789 vterm_state_get_cursorpos(state, &pos);
790 position_cursor(wp, &pos);
791
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200792 /* TODO: Only redraw what changed. */
793 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +0200794 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200795 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200796 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200797
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200798 if (pos.row < term->tl_rows)
799 {
800 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200801 {
802 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200803 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200804
Bram Moolenaareeac6772017-07-23 15:48:37 +0200805 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
806 vim_memset(&cell, 0, sizeof(cell));
807
808 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200809 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200810 if (c == NUL)
811 {
812 ScreenLines[off] = ' ';
813 ScreenLinesUC[off] = NUL;
814 }
815 else
816 {
817#if defined(FEAT_MBYTE)
818 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200819 {
820 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200821 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200822 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200823 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200824 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200825 ScreenLines[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200826 ScreenLinesUC[off] = NUL;
827 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200828#else
829 ScreenLines[off] = c;
830#endif
831 }
Bram Moolenaareeac6772017-07-23 15:48:37 +0200832 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200833
834 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200835 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200836 if (cell.width == 2)
837 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +0200838 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200839 ScreenLinesUC[off] = NUL;
840 ++pos.col;
841 ++off;
842 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200843 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200844 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200845 else
846 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +0200847
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200848 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
849 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +0200850 }
851}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200852
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200853/*
854 * Set job options common for Unix and MS-Windows.
855 */
856 static void
857setup_job_options(jobopt_T *opt, int rows, int cols)
858{
859 clear_job_options(opt);
860 opt->jo_mode = MODE_RAW;
861 opt->jo_out_mode = MODE_RAW;
862 opt->jo_err_mode = MODE_RAW;
863 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
864 opt->jo_io[PART_OUT] = JIO_BUFFER;
865 opt->jo_io[PART_ERR] = JIO_BUFFER;
866 opt->jo_set |= JO_OUT_IO + (JO_OUT_IO << (PART_ERR - PART_OUT));
867 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
868 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
Bram Moolenaar5a1feb82017-07-22 18:04:08 +0200869 opt->jo_pty = TRUE;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200870 opt->jo_set |= JO_OUT_BUF + (JO_OUT_BUF << (PART_ERR - PART_OUT));
871 opt->jo_term_rows = rows;
872 opt->jo_term_cols = cols;
873}
874
875/*
876 * Create a new vterm and initialize it.
877 */
878 static void
879create_vterm(term_T *term, int rows, int cols)
880{
881 VTerm *vterm;
882 VTermScreen *screen;
883
884 vterm = vterm_new(rows, cols);
885 term->tl_vterm = vterm;
886 screen = vterm_obtain_screen(vterm);
887 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
888 /* TODO: depends on 'encoding'. */
889 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +0200890
891 /* Vterm uses a default black background. Set it to white when
892 * 'background' is "light". */
893 if (*p_bg == 'l')
894 {
895 VTermColor fg, bg;
896
897 fg.red = fg.green = fg.blue = 0;
898 bg.red = bg.green = bg.blue = 255;
899 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
900 }
901
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200902 /* Required to initialize most things. */
903 vterm_screen_reset(screen, 1 /* hard */);
904}
905
906# ifdef WIN3264
907
908#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
909#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
910
911void* (*winpty_config_new)(int, void*);
912void* (*winpty_open)(void*, void*);
913void* (*winpty_spawn_config_new)(int, void*, LPCWSTR, void*, void*, void*);
914BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
915void (*winpty_config_set_initial_size)(void*, int, int);
916LPCWSTR (*winpty_conin_name)(void*);
917LPCWSTR (*winpty_conout_name)(void*);
918LPCWSTR (*winpty_conerr_name)(void*);
919void (*winpty_free)(void*);
920void (*winpty_config_free)(void*);
921void (*winpty_spawn_config_free)(void*);
922void (*winpty_error_free)(void*);
923LPCWSTR (*winpty_error_msg)(void*);
924
925/**************************************
926 * 2. MS-Windows implementation.
927 */
928
929#define WINPTY_DLL "winpty.dll"
930
931static HINSTANCE hWinPtyDLL = NULL;
932
933 int
934dyn_winpty_init(void)
935{
936 int i;
937 static struct
938 {
939 char *name;
940 FARPROC *ptr;
941 } winpty_entry[] =
942 {
943 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
944 {"winpty_config_free", (FARPROC*)&winpty_config_free},
945 {"winpty_config_new", (FARPROC*)&winpty_config_new},
946 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
947 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
948 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
949 {"winpty_error_free", (FARPROC*)&winpty_error_free},
950 {"winpty_free", (FARPROC*)&winpty_free},
951 {"winpty_open", (FARPROC*)&winpty_open},
952 {"winpty_spawn", (FARPROC*)&winpty_spawn},
953 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
954 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
955 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
956 {NULL, NULL}
957 };
958
959 /* No need to initialize twice. */
960 if (hWinPtyDLL)
961 return 1;
962 /* Load winpty.dll */
963 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
964 if (!hWinPtyDLL)
965 {
966 EMSG2(_(e_loadlib), WINPTY_DLL);
967 return 0;
968 }
969 for (i = 0; winpty_entry[i].name != NULL
970 && winpty_entry[i].ptr != NULL; ++i)
971 {
972 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
973 winpty_entry[i].name)) == NULL)
974 {
975 EMSG2(_(e_loadfunc), winpty_entry[i].name);
976 return 0;
977 }
978 }
979
980 return 1;
981}
982
983/*
984 * Create a new terminal of "rows" by "cols" cells.
985 * Store a reference in "term".
986 * Return OK or FAIL.
987 */
988 static int
989term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
990{
991 WCHAR *p = enc_to_utf16(cmd, NULL);
992 channel_T *channel = NULL;
993 job_T *job = NULL;
994 jobopt_T opt;
995 DWORD error;
996 HANDLE jo = NULL, child_process_handle, child_thread_handle;
997 void *winpty_err;
998 void *spawn_config;
999
1000 if (!dyn_winpty_init())
1001 return FAIL;
1002
1003 if (p == NULL)
1004 return FAIL;
1005
1006 job = job_alloc();
1007 if (job == NULL)
1008 goto failed;
1009
1010 channel = add_channel();
1011 if (channel == NULL)
1012 goto failed;
1013
1014 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
1015 if (term->tl_winpty_config == NULL)
1016 goto failed;
1017
1018 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
1019 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
1020 if (term->tl_winpty == NULL)
1021 goto failed;
1022
1023 spawn_config = winpty_spawn_config_new(
1024 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
1025 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
1026 NULL,
1027 p,
1028 NULL,
1029 NULL,
1030 &winpty_err);
1031 if (spawn_config == NULL)
1032 goto failed;
1033
1034 channel = add_channel();
1035 if (channel == NULL)
1036 goto failed;
1037
1038 job = job_alloc();
1039 if (job == NULL)
1040 goto failed;
1041
1042 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
1043 &child_thread_handle, &error, &winpty_err))
1044 goto failed;
1045
1046 channel_set_pipes(channel,
1047 (sock_T) CreateFileW(
1048 winpty_conin_name(term->tl_winpty),
1049 GENERIC_WRITE, 0, NULL,
1050 OPEN_EXISTING, 0, NULL),
1051 (sock_T) CreateFileW(
1052 winpty_conout_name(term->tl_winpty),
1053 GENERIC_READ, 0, NULL,
1054 OPEN_EXISTING, 0, NULL),
1055 (sock_T) CreateFileW(
1056 winpty_conerr_name(term->tl_winpty),
1057 GENERIC_READ, 0, NULL,
1058 OPEN_EXISTING, 0, NULL));
1059
1060 jo = CreateJobObject(NULL, NULL);
1061 if (jo == NULL)
1062 goto failed;
1063
1064 if (!AssignProcessToJobObject(jo, child_process_handle))
1065 goto failed;
1066
1067 winpty_spawn_config_free(spawn_config);
1068
1069 create_vterm(term, rows, cols);
1070
1071 setup_job_options(&opt, rows, cols);
1072 channel_set_job(channel, job, &opt);
1073
1074 job->jv_channel = channel;
1075 job->jv_proc_info.hProcess = child_process_handle;
1076 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
1077 job->jv_job_object = jo;
1078 job->jv_status = JOB_STARTED;
1079 term->tl_job = job;
1080
1081 return OK;
1082
1083failed:
1084 if (channel != NULL)
1085 channel_clear(channel);
1086 if (job != NULL)
1087 job_cleanup(job);
1088 if (jo != NULL)
1089 CloseHandle(jo);
1090 if (term->tl_winpty != NULL)
1091 winpty_free(term->tl_winpty);
1092 if (term->tl_winpty_config != NULL)
1093 winpty_config_free(term->tl_winpty_config);
1094 if (winpty_err != NULL)
1095 {
1096 char_u *msg = utf16_to_enc(
1097 (short_u *)winpty_error_msg(winpty_err), NULL);
1098
1099 EMSG(msg);
1100 winpty_error_free(winpty_err);
1101 }
1102 return FAIL;
1103}
1104
1105/*
1106 * Free the terminal emulator part of "term".
1107 */
1108 static void
1109term_free(term_T *term)
1110{
1111 winpty_free(term->tl_winpty);
1112 winpty_config_free(term->tl_winpty_config);
1113 vterm_free(term->tl_vterm);
1114}
1115
1116# else
1117
1118/**************************************
1119 * 3. Unix-like implementation.
1120 */
1121
1122/*
1123 * Create a new terminal of "rows" by "cols" cells.
1124 * Start job for "cmd".
1125 * Store the pointers in "term".
1126 * Return OK or FAIL.
1127 */
1128 static int
1129term_and_job_init(term_T *term, int rows, int cols, char_u *cmd)
1130{
1131 typval_T argvars[2];
1132 jobopt_T opt;
1133
1134 create_vterm(term, rows, cols);
1135
1136 argvars[0].v_type = VAR_STRING;
1137 argvars[0].vval.v_string = cmd;
1138 setup_job_options(&opt, rows, cols);
1139 term->tl_job = job_start(argvars, &opt);
1140
Bram Moolenaar61a66052017-07-22 18:39:00 +02001141 return term->tl_job != NULL
1142 && term->tl_job->jv_channel != NULL
1143 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001144}
1145
1146/*
1147 * Free the terminal emulator part of "term".
1148 */
1149 static void
1150term_free(term_T *term)
1151{
1152 vterm_free(term->tl_vterm);
1153}
1154# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001155
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001156#endif /* FEAT_TERMINAL */