blob: 1ce4db7e973e64d75cf55812252d9d496dc1bb69 [file] [log] [blame]
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001/* vi:set ts=8 sts=4 sw=4 noet:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * Terminal window support, see ":help :terminal".
12 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020013 * There are three parts:
14 * 1. Generic code for all systems.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020015 * Uses libvterm for the terminal emulator.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020016 * 2. The MS-Windows implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020017 * Uses winpty.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020018 * 3. The Unix-like implementation.
Bram Moolenaarb13501f2017-07-22 22:32:56 +020019 * Uses pseudo-tty's (pty's).
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020020 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020022 * this library is in the libvterm directory.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020023 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020024 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020026 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
Bram Moolenaar8a773062017-07-24 22:29:21 +020028 * terminal encoding and writing to the job over a channel.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020029 *
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020030 * If the job produces output, it is written to the terminal emulator. The
31 * terminal emulator invokes callbacks when its screen content changes. The
32 * line range is stored in tl_dirty_row_start and tl_dirty_row_end. Once in a
33 * while, if the terminal window is visible, the screen contents is drawn.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020034 *
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020035 * When the job ends the text is put in a buffer. Redrawing then happens from
36 * that buffer, attributes come from the scrollback buffer tl_scrollback.
37 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020038 * TODO:
Bram Moolenaar78712a72017-08-05 14:50:12 +020039 * - job_start('ls') sometimes does not work.
Bram Moolenaard8dc1792017-08-03 11:55:21 +020040 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar423802d2017-07-30 16:52:24 +020041 * - in bash mouse clicks are inserting characters.
42 * - mouse scroll: when over other window, scroll that window.
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +020043 * - add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020044 * - For the scrollback buffer store lines in the buffer, only attributes in
45 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020046 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020048 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
49 * job finishes).
50 * - add option values to the command:
51 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020052 * - support different cursor shapes, colors and attributes
53 * - make term_getcursor() return type (none/block/bar/underline) and
54 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020055 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020056 * For the GUI fill termios with default values, perhaps like pangoterm:
57 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
58 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020059 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020060 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020061 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020062 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020063 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020064 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
65 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020066 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020067 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020068 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020069 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020070 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020071 * terminal. Allow:
72 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
73 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
74 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
75 * Check that something is connected to the terminal.
76 * Test: "cat" reading from a file or buffer
77 * "ls" writing stdout to a file or buffer
78 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020079 * - support ":term NONE" to open a terminal with a pty but not running a job
80 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020081 * - if the job in the terminal does not support the mouse, we can use the
82 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
84 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020085 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020086 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020087 * - Copy text in the vterm to the Vim buffer once in a while, so that
88 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089 */
90
91#include "vim.h"
92
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020093#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094
Bram Moolenaard5310982017-08-05 15:16:32 +020095#ifndef MIN
96# define MIN(x,y) ((x) < (y) ? (x) : (y))
97#endif
98#ifndef MAX
99# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200100#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200102#include "libvterm/include/vterm.h"
103
Bram Moolenaard85f2712017-07-28 21:51:57 +0200104typedef struct sb_line_S {
105 int sb_cols; /* can differ per line */
106 VTermScreenCell *sb_cells; /* allocated */
107} sb_line_T;
108
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200109/* typedef term_T in structs.h */
110struct terminal_S {
111 term_T *tl_next;
112
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113 VTerm *tl_vterm;
114 job_T *tl_job;
115 buf_T *tl_buffer;
116
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200117 /* used when tl_job is NULL and only a pty was created */
118 int tl_tty_fd;
119 char_u *tl_tty_name;
120
Bram Moolenaar423802d2017-07-30 16:52:24 +0200121 int tl_terminal_mode;
122 int tl_channel_closed;
123
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200124#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125 void *tl_winpty_config;
126 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200127#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200128
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200129 /* last known vterm size */
130 int tl_rows;
131 int tl_cols;
132 /* vterm size does not follow window size */
133 int tl_rows_fixed;
134 int tl_cols_fixed;
135
Bram Moolenaar21554412017-07-24 21:44:43 +0200136 char_u *tl_title; /* NULL or allocated */
137 char_u *tl_status_text; /* NULL or allocated */
138
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139 /* Range of screen rows to update. Zero based. */
140 int tl_dirty_row_start; /* -1 if nothing dirty */
141 int tl_dirty_row_end; /* row below last one to update */
142
Bram Moolenaard85f2712017-07-28 21:51:57 +0200143 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200144 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200145
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200146 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200147 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148};
149
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150/*
151 * List of all active terminals.
152 */
153static term_T *first_term = NULL;
154
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200163static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200164static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200165
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200166/**************************************
167 * 1. Generic code for all systems.
168 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169
170/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171 * Determine the terminal size from 'termsize' and the current window.
172 * Assumes term->tl_rows and term->tl_cols are zero.
173 */
174 static void
175set_term_and_win_size(term_T *term)
176{
177 if (*curwin->w_p_tms != NUL)
178 {
179 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
180
181 term->tl_rows = atoi((char *)curwin->w_p_tms);
182 term->tl_cols = atoi((char *)p);
183 }
184 if (term->tl_rows == 0)
185 term->tl_rows = curwin->w_height;
186 else
187 {
188 win_setheight_win(term->tl_rows, curwin);
189 term->tl_rows_fixed = TRUE;
190 }
191 if (term->tl_cols == 0)
192 term->tl_cols = curwin->w_width;
193 else
194 {
195 win_setwidth_win(term->tl_cols, curwin);
196 term->tl_cols_fixed = TRUE;
197 }
198}
199
200/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200201 * Initialize job options for a terminal job.
202 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200203 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 static void
205init_job_options(jobopt_T *opt)
206{
207 clear_job_options(opt);
208
209 opt->jo_mode = MODE_RAW;
210 opt->jo_out_mode = MODE_RAW;
211 opt->jo_err_mode = MODE_RAW;
212 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
213
214 opt->jo_io[PART_OUT] = JIO_BUFFER;
215 opt->jo_io[PART_ERR] = JIO_BUFFER;
216 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
217
218 opt->jo_modifiable[PART_OUT] = 0;
219 opt->jo_modifiable[PART_ERR] = 0;
220 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
221
222 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
223}
224
225/*
226 * Set job options mandatory for a terminal job.
227 */
228 static void
229setup_job_options(jobopt_T *opt, int rows, int cols)
230{
231 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
232 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
233 opt->jo_pty = TRUE;
234 opt->jo_term_rows = rows;
235 opt->jo_term_cols = cols;
236}
237
238 static void
239term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200240{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 exarg_T split_ea;
242 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200243 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
245 if (check_restricted() || check_secure())
246 return;
247
248 term = (term_T *)alloc_clear(sizeof(term_T));
249 if (term == NULL)
250 return;
251 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200252 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200253 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 /* Open a new window or tab. */
256 vim_memset(&split_ea, 0, sizeof(split_ea));
257 split_ea.cmdidx = CMD_new;
258 split_ea.cmd = (char_u *)"new";
259 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200260 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
261 {
262 split_ea.line2 = opt->jo_term_rows;
263 split_ea.addr_count = 1;
264 }
265 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
266 {
267 split_ea.line2 = opt->jo_term_cols;
268 split_ea.addr_count = 1;
269 }
270
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271 ex_splitview(&split_ea);
272 if (curwin == old_curwin)
273 {
274 /* split failed */
275 vim_free(term);
276 return;
277 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200278 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200279 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200280
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200281 /* only one size was taken care of with :new, do the other one */
282 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
283 win_setheight(opt->jo_term_rows);
284 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
285 win_setwidth(opt->jo_term_cols);
286
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200287 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200288 term->tl_next = first_term;
289 first_term = term;
290
Bram Moolenaar293424c2017-07-26 23:11:01 +0200291 if (cmd == NULL || *cmd == NUL)
292 cmd = p_sh;
293
Bram Moolenaar78712a72017-08-05 14:50:12 +0200294 if (opt->jo_term_name != NULL)
295 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
296 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200297 {
298 int i;
299 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200300 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200301
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200302 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200303 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200304 /* Prepend a ! to the command name to avoid the buffer name equals
305 * the executable, otherwise ":w!" would overwrite it. */
306 if (i == 0)
307 vim_snprintf((char *)p, len, "!%s", cmd);
308 else
309 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310 if (buflist_findname(p) == NULL)
311 {
312 curbuf->b_ffname = p;
313 break;
314 }
315 }
316 }
317 curbuf->b_fname = curbuf->b_ffname;
318
Bram Moolenaareb44a682017-08-03 22:44:55 +0200319 set_string_option_direct((char_u *)"buftype", -1,
320 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
321
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200322 /* Mark the buffer as not modifiable. It can only be made modifiable after
323 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200324 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200325
326 /* Set 'bufhidden' to "hide": allow closing the window. */
327 set_string_option_direct((char_u *)"bufhidden", -1,
328 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200331 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200334 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335 {
336 /* store the size we ended up with */
337 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 }
339 else
340 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200341 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200342
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 /* Wiping out the buffer will also close the window and call
344 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200345 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200347}
348
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200349/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200350 * ":terminal": open a terminal window and execute a job in it.
351 */
352 void
353ex_terminal(exarg_T *eap)
354{
355 jobopt_T opt;
356
357 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200358
359 if (eap->addr_count == 2)
360 {
361 opt.jo_term_rows = eap->line1;
362 opt.jo_term_cols = eap->line2;
363 }
364 else if (eap->addr_count == 1)
365 {
366 if (cmdmod.split & WSP_VERT)
367 opt.jo_term_cols = eap->line2;
368 else
369 opt.jo_term_rows = eap->line2;
370 }
371 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200372
373 term_start(eap->arg, &opt);
374}
375
376/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200377 * Free the scrollback buffer for "term".
378 */
379 static void
380free_scrollback(term_T *term)
381{
382 int i;
383
384 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
385 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
386 ga_clear(&term->tl_scrollback);
387}
388
389/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200390 * Free a terminal and everything it refers to.
391 * Kills the job if there is one.
392 * Called when wiping out a buffer.
393 */
394 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200395free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200396{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200397 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200398 term_T *tp;
399
400 if (term == NULL)
401 return;
402 if (first_term == term)
403 first_term = term->tl_next;
404 else
405 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
406 if (tp->tl_next == term)
407 {
408 tp->tl_next = term->tl_next;
409 break;
410 }
411
412 if (term->tl_job != NULL)
413 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200414 if (term->tl_job->jv_status != JOB_ENDED
415 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200416 job_stop(term->tl_job, NULL, "kill");
417 job_unref(term->tl_job);
418 }
419
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200420 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200421
422 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200423 vim_free(term->tl_title);
424 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200425 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200426 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200427}
428
429/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200430 * Write job output "msg[len]" to the vterm.
431 */
432 static void
433term_write_job_output(term_T *term, char_u *msg, size_t len)
434{
435 VTerm *vterm = term->tl_vterm;
436 char_u *p;
437 size_t done;
438 size_t len_now;
439
440 for (done = 0; done < len; done += len_now)
441 {
442 for (p = msg + done; p < msg + len; )
443 {
444 if (*p == NL)
445 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200446 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200447 }
448 len_now = p - msg - done;
449 vterm_input_write(vterm, (char *)msg + done, len_now);
450 if (p < msg + len && *p == NL)
451 {
452 /* Convert NL to CR-NL, that appears to work best. */
453 vterm_input_write(vterm, "\r\n", 2);
454 ++len_now;
455 }
456 }
457
458 /* this invokes the damage callbacks */
459 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
460}
461
Bram Moolenaar1c844932017-07-24 23:36:41 +0200462 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200463update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200464{
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200465 if (term->tl_terminal_mode)
466 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200467 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200468 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200469 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200470 if (term->tl_cursor_visible)
471 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200472 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200473#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200474 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200475 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200476#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200477 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200478}
479
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200480/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200481 * Invoked when "msg" output from a job was received. Write it to the terminal
482 * of "buffer".
483 */
484 void
485write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
486{
487 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200489
Bram Moolenaard85f2712017-07-28 21:51:57 +0200490 if (term->tl_vterm == NULL)
491 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200492 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200493 return;
494 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200495 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200497
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200498 if (!term->tl_terminal_mode)
499 {
500 /* TODO: only update once in a while. */
501 update_screen(0);
502 update_cursor(term, TRUE);
503 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200504}
505
506/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200507 * Send a mouse position and click to the vterm
508 */
509 static int
510term_send_mouse(VTerm *vterm, int button, int pressed)
511{
512 VTermModifier mod = VTERM_MOD_NONE;
513
514 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
515 mouse_col - W_WINCOL(curwin), mod);
516 vterm_mouse_button(vterm, button, pressed, mod);
517 return TRUE;
518}
519
520/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200521 * Convert typed key "c" into bytes to send to the job.
522 * Return the number of bytes in "buf".
523 */
524 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200525term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200526{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200527 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200528 VTermKey key = VTERM_KEY_NONE;
529 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200530 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200531
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 switch (c)
533 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200535 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200536 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
537 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200538 case K_DEL: key = VTERM_KEY_DEL; break;
539 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200540 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
541 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200543 case K_S_END: mod = VTERM_MOD_SHIFT;
544 key = VTERM_KEY_END; break;
545 case K_C_END: mod = VTERM_MOD_CTRL;
546 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
548 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
549 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
550 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
551 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
552 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
553 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
554 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
555 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
556 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
557 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
558 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
559 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200560 case K_S_HOME: mod = VTERM_MOD_SHIFT;
561 key = VTERM_KEY_HOME; break;
562 case K_C_HOME: mod = VTERM_MOD_CTRL;
563 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564 case K_INS: key = VTERM_KEY_INS; break;
565 case K_K0: key = VTERM_KEY_KP_0; break;
566 case K_K1: key = VTERM_KEY_KP_1; break;
567 case K_K2: key = VTERM_KEY_KP_2; break;
568 case K_K3: key = VTERM_KEY_KP_3; break;
569 case K_K4: key = VTERM_KEY_KP_4; break;
570 case K_K5: key = VTERM_KEY_KP_5; break;
571 case K_K6: key = VTERM_KEY_KP_6; break;
572 case K_K7: key = VTERM_KEY_KP_7; break;
573 case K_K8: key = VTERM_KEY_KP_8; break;
574 case K_K9: key = VTERM_KEY_KP_9; break;
575 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
576 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
577 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
578 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
579 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
580 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
581 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
582 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
583 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
584 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
585 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
586 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
587 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200588 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
589 key = VTERM_KEY_LEFT; break;
590 case K_C_LEFT: mod = VTERM_MOD_CTRL;
591 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200592 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
593 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
594 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200595 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
596 key = VTERM_KEY_RIGHT; break;
597 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
598 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200600 case K_S_UP: mod = VTERM_MOD_SHIFT;
601 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200602 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200603
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200604 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
605 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
606 case K_MOUSELEFT: /* TODO */ return 0;
607 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200608
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200609 case K_LEFTMOUSE:
610 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
611 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
612 case K_LEFTRELEASE:
613 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
614 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
615 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
616 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
617 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
618 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
619 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
620 case K_X1MOUSE: /* TODO */ return 0;
621 case K_X1DRAG: /* TODO */ return 0;
622 case K_X1RELEASE: /* TODO */ return 0;
623 case K_X2MOUSE: /* TODO */ return 0;
624 case K_X2DRAG: /* TODO */ return 0;
625 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200626
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200627 case K_IGNORE: return 0;
628 case K_NOP: return 0;
629 case K_UNDO: return 0;
630 case K_HELP: return 0;
631 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
632 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
633 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
634 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
635 case K_SELECT: return 0;
636#ifdef FEAT_GUI
637 case K_VER_SCROLLBAR: return 0;
638 case K_HOR_SCROLLBAR: return 0;
639#endif
640#ifdef FEAT_GUI_TABLINE
641 case K_TABLINE: return 0;
642 case K_TABMENU: return 0;
643#endif
644#ifdef FEAT_NETBEANS_INTG
645 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
646#endif
647#ifdef FEAT_DND
648 case K_DROP: return 0;
649#endif
650#ifdef FEAT_AUTOCMD
651 case K_CURSORHOLD: return 0;
652#endif
653 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
654 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 }
656
657 /*
658 * Convert special keys to vterm keys:
659 * - Write keys to vterm: vterm_keyboard_key()
660 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200661 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 */
663 if (key != VTERM_KEY_NONE)
664 /* Special key, let vterm convert it. */
665 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200666 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 /* Normal character, let vterm convert it. */
668 vterm_keyboard_unichar(vterm, c, mod);
669
670 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200671 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200673
Bram Moolenaar938783d2017-07-16 20:13:26 +0200674/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200675 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200676 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200677 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200678term_job_running(term_T *term)
679{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200680 /* Also consider the job finished when the channel is closed, to avoid a
681 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200682 return term != NULL
683 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200684 && term->tl_job->jv_status == JOB_STARTED
685 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200686}
687
688/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200689 * Add the last line of the scrollback buffer to the buffer in the window.
690 */
691 static void
692add_scrollback_line_to_buffer(term_T *term)
693{
694 linenr_T lnum = term->tl_scrollback.ga_len - 1;
695 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
696 garray_T ga;
697 int c;
698 int col;
699 int i;
700
701 ga_init2(&ga, 1, 100);
702 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
703 {
704 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
705 goto failed;
706 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
707 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
708 (char_u *)ga.ga_data + ga.ga_len);
709 }
710 if (ga_grow(&ga, 1) == FAIL)
711 goto failed;
712 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
713 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
714
715 if (lnum == 0)
716 {
717 /* Delete the empty line that was in the empty buffer. */
718 curbuf = term->tl_buffer;
719 ml_delete(2, FALSE);
720 curbuf = curwin->w_buffer;
721 }
722
723failed:
724 ga_clear(&ga);
725}
726
727/*
728 * Add the current lines of the terminal to scrollback and to the buffer.
729 * Called after the job has ended and when switching to Terminal mode.
730 */
731 static void
732move_terminal_to_buffer(term_T *term)
733{
734 win_T *wp;
735 int len;
736 int lines_skipped = 0;
737 VTermPos pos;
738 VTermScreenCell cell;
739 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200740 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200741
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200742 if (term->tl_vterm == NULL)
743 return;
744 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200745 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
746 {
747 len = 0;
748 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
749 if (vterm_screen_get_cell(screen, pos, &cell) != 0
750 && cell.chars[0] != NUL)
751 len = pos.col + 1;
752
753 if (len == 0)
754 ++lines_skipped;
755 else
756 {
757 while (lines_skipped > 0)
758 {
759 /* Line was skipped, add an empty line. */
760 --lines_skipped;
761 if (ga_grow(&term->tl_scrollback, 1) == OK)
762 {
763 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
764 + term->tl_scrollback.ga_len;
765
766 line->sb_cols = 0;
767 line->sb_cells = NULL;
768 ++term->tl_scrollback.ga_len;
769
770 add_scrollback_line_to_buffer(term);
771 }
772 }
773
774 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
775 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
776 {
777 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
778 + term->tl_scrollback.ga_len;
779
780 for (pos.col = 0; pos.col < len; ++pos.col)
781 {
782 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
783 vim_memset(p + pos.col, 0, sizeof(cell));
784 else
785 p[pos.col] = cell;
786 }
787 line->sb_cols = len;
788 line->sb_cells = p;
789 ++term->tl_scrollback.ga_len;
790
791 add_scrollback_line_to_buffer(term);
792 }
793 else
794 vim_free(p);
795 }
796 }
797
798 FOR_ALL_WINDOWS(wp)
799 {
800 if (wp->w_buffer == term->tl_buffer)
801 {
802 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
803 wp->w_cursor.col = 0;
804 wp->w_valid = 0;
805 redraw_win_later(wp, NOT_VALID);
806 }
807 }
808}
809
810 static void
811set_terminal_mode(term_T *term, int on)
812{
813 term->tl_terminal_mode = on;
814 vim_free(term->tl_status_text);
815 term->tl_status_text = NULL;
816 if (term->tl_buffer == curbuf)
817 maketitle();
818}
819
820/*
821 * Called after the job if finished and Terminal mode is not active:
822 * Move the vterm contents into the scrollback buffer and free the vterm.
823 */
824 static void
825cleanup_vterm(term_T *term)
826{
827 move_terminal_to_buffer(term);
828 term_free_vterm(term);
829 set_terminal_mode(term, FALSE);
830}
831
832/*
833 * Switch from sending keys to the job to Terminal-Normal mode.
834 * Suspends updating the terminal window.
835 */
836 static void
837term_enter_terminal_mode()
838{
839 term_T *term = curbuf->b_term;
840
841 /* Append the current terminal contents to the buffer. */
842 move_terminal_to_buffer(term);
843
844 set_terminal_mode(term, TRUE);
845}
846
847/*
848 * Returns TRUE if the current window contains a terminal and we are in
849 * Terminal-Normal mode.
850 */
851 int
852term_in_terminal_mode()
853{
854 term_T *term = curbuf->b_term;
855
856 return term != NULL && term->tl_terminal_mode;
857}
858
859/*
860 * Switch from Terminal-Normal mode to sending keys to the job.
861 * Restores updating the terminal window.
862 */
863 void
864term_leave_terminal_mode()
865{
866 term_T *term = curbuf->b_term;
867 sb_line_T *line;
868 garray_T *gap;
869
870 /* Remove the terminal contents from the scrollback and the buffer. */
871 gap = &term->tl_scrollback;
872 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
873 {
874 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
875 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
876 vim_free(line->sb_cells);
877 --gap->ga_len;
878 if (gap->ga_len == 0)
879 break;
880 }
881 check_cursor();
882
883 set_terminal_mode(term, FALSE);
884
885 if (term->tl_channel_closed)
886 cleanup_vterm(term);
887 redraw_buf_and_status_later(curbuf, NOT_VALID);
888}
889
890/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200891 * Get a key from the user without mapping.
892 * TODO: use terminal mode mappings.
893 */
894 static int
895term_vgetc()
896{
897 int c;
898
899 ++no_mapping;
900 ++allow_keys;
901 got_int = FALSE;
902 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200903 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200904 --no_mapping;
905 --allow_keys;
906 return c;
907}
908
909/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200910 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200911 * Return FAIL when the key needs to be handled in Normal mode.
912 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200913 */
914 static int
915send_keys_to_term(term_T *term, int c, int typed)
916{
917 char msg[KEY_BUF_LEN];
918 size_t len;
919 static int mouse_was_outside = FALSE;
920 int dragging_outside = FALSE;
921
922 /* Catch keys that need to be handled as in Normal mode. */
923 switch (c)
924 {
925 case NUL:
926 case K_ZERO:
927 if (typed)
928 stuffcharReadbuff(c);
929 return FAIL;
930
931 case K_IGNORE:
932 return FAIL;
933
934 case K_LEFTDRAG:
935 case K_MIDDLEDRAG:
936 case K_RIGHTDRAG:
937 case K_X1DRAG:
938 case K_X2DRAG:
939 dragging_outside = mouse_was_outside;
940 /* FALLTHROUGH */
941 case K_LEFTMOUSE:
942 case K_LEFTMOUSE_NM:
943 case K_LEFTRELEASE:
944 case K_LEFTRELEASE_NM:
945 case K_MIDDLEMOUSE:
946 case K_MIDDLERELEASE:
947 case K_RIGHTMOUSE:
948 case K_RIGHTRELEASE:
949 case K_X1MOUSE:
950 case K_X1RELEASE:
951 case K_X2MOUSE:
952 case K_X2RELEASE:
953 if (mouse_row < W_WINROW(curwin)
954 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
955 || mouse_col < W_WINCOL(curwin)
956 || mouse_col >= W_ENDCOL(curwin)
957 || dragging_outside)
958 {
959 /* click outside the current window */
960 if (typed)
961 {
962 stuffcharReadbuff(c);
963 mouse_was_outside = TRUE;
964 }
965 return FAIL;
966 }
967 }
968 if (typed)
969 mouse_was_outside = FALSE;
970
971 /* Convert the typed key to a sequence of bytes for the job. */
972 len = term_convert_key(term, c, msg);
973 if (len > 0)
974 /* TODO: if FAIL is returned, stop? */
975 channel_send(term->tl_job->jv_channel, PART_IN,
976 (char_u *)msg, (int)len, NULL);
977
978 return OK;
979}
980
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200981 static void
982position_cursor(win_T *wp, VTermPos *pos)
983{
984 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
985 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
986 wp->w_valid |= (VALID_WCOL|VALID_WROW);
987}
988
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200989/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +0200990 * Handle CTRL-W "": send register contents to the job.
991 */
992 static void
993term_paste_register(int prev_c UNUSED)
994{
995 int c;
996 list_T *l;
997 listitem_T *item;
998 long reglen = 0;
999 int type;
1000
1001#ifdef FEAT_CMDL_INFO
1002 if (add_to_showcmd(prev_c))
1003 if (add_to_showcmd('"'))
1004 out_flush();
1005#endif
1006 c = term_vgetc();
1007#ifdef FEAT_CMDL_INFO
1008 clear_showcmd();
1009#endif
1010
1011 /* CTRL-W "= prompt for expression to evaluate. */
1012 if (c == '=' && get_expr_register() != '=')
1013 return;
1014
1015 l = (list_T *)get_reg_contents(c, GREG_LIST);
1016 if (l != NULL)
1017 {
1018 type = get_reg_type(c, &reglen);
1019 for (item = l->lv_first; item != NULL; item = item->li_next)
1020 {
1021 char_u *s = get_tv_string(&item->li_tv);
1022
1023 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1024 s, STRLEN(s), NULL);
1025 if (item->li_next != NULL || type == MLINE)
1026 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1027 (char_u *)"\r", 1, NULL);
1028 }
1029 list_free(l);
1030 }
1031}
1032
1033/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001034 * Returns TRUE if the current window contains a terminal and we are sending
1035 * keys to the job.
1036 */
1037 int
1038term_use_loop()
1039{
1040 term_T *term = curbuf->b_term;
1041
1042 return term != NULL
1043 && !term->tl_terminal_mode
1044 && term->tl_vterm != NULL
1045 && term_job_running(term);
1046}
1047
1048/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001049 * Wait for input and send it to the job.
1050 * Return when the start of a CTRL-W command is typed or anything else that
1051 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001052 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1053 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001054 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001055 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001056terminal_loop(void)
1057{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001058 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001059 int termkey = 0;
1060
1061 if (*curwin->w_p_tk != NUL)
1062 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001063 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001064
1065 for (;;)
1066 {
1067 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001068 /* Repeat redrawing in case a message is received while redrawing. */
1069 while (curwin->w_redr_type != 0)
1070 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001071 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001072
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001073 c = term_vgetc();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001074 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001075 /* job finished while waiting for a character */
1076 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001077
Bram Moolenaarfae42832017-08-01 22:24:26 +02001078#ifdef UNIX
1079 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1080#endif
1081#ifdef WIN3264
1082 if (c == Ctrl_C)
1083 /* We don't know if the job can handle CTRL-C itself or not, this
1084 * may kill the shell instead of killing the command running in the
1085 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001086 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001087#endif
1088
Bram Moolenaar69198192017-08-05 14:10:48 +02001089 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001090 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001091 int prev_c = c;
1092
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001093#ifdef FEAT_CMDL_INFO
1094 if (add_to_showcmd(c))
1095 out_flush();
1096#endif
1097 c = term_vgetc();
1098#ifdef FEAT_CMDL_INFO
1099 clear_showcmd();
1100#endif
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02001101 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001102 /* job finished while waiting for a character */
1103 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001104
Bram Moolenaar69198192017-08-05 14:10:48 +02001105 if (prev_c == Ctrl_BSL)
1106 {
1107 if (c == Ctrl_N)
1108 /* CTRL-\ CTRL-N : execute one Normal mode command. */
1109 return OK;
1110 /* Send both keys to the terminal. */
1111 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1112 }
1113 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001114 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001115 /* "CTRL-W .": send CTRL-W to the job */
1116 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001117 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001118 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001119 {
1120 term_enter_terminal_mode();
1121 return FAIL;
1122 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001123 else if (c == '"')
1124 {
1125 term_paste_register(prev_c);
1126 continue;
1127 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001128 else if (termkey == 0 || c != termkey)
1129 {
1130 stuffcharReadbuff(Ctrl_W);
1131 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001132 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001133 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001134 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001135 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1136 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001137 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001138 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001139}
1140
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001141/*
1142 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001143 * This updates the title and status, but does not close the vter, because
1144 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001145 */
1146 void
1147term_job_ended(job_T *job)
1148{
1149 term_T *term;
1150 int did_one = FALSE;
1151
1152 for (term = first_term; term != NULL; term = term->tl_next)
1153 if (term->tl_job == job)
1154 {
1155 vim_free(term->tl_title);
1156 term->tl_title = NULL;
1157 vim_free(term->tl_status_text);
1158 term->tl_status_text = NULL;
1159 redraw_buf_and_status_later(term->tl_buffer, VALID);
1160 did_one = TRUE;
1161 }
1162 if (did_one)
1163 redraw_statuslines();
1164 if (curbuf->b_term != NULL)
1165 {
1166 if (curbuf->b_term->tl_job == job)
1167 maketitle();
1168 update_cursor(curbuf->b_term, TRUE);
1169 }
1170}
1171
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001172 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001173may_toggle_cursor(term_T *term)
1174{
1175 if (curbuf == term->tl_buffer)
1176 {
1177 if (term->tl_cursor_visible)
1178 cursor_on();
1179 else
1180 cursor_off();
1181 }
1182}
1183
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001184 static int
1185handle_damage(VTermRect rect, void *user)
1186{
1187 term_T *term = (term_T *)user;
1188
1189 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1190 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1191 redraw_buf_later(term->tl_buffer, NOT_VALID);
1192 return 1;
1193}
1194
1195 static int
1196handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1197{
1198 term_T *term = (term_T *)user;
1199
1200 /* TODO */
1201 redraw_buf_later(term->tl_buffer, NOT_VALID);
1202 return 1;
1203}
1204
1205 static int
1206handle_movecursor(
1207 VTermPos pos,
1208 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001209 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001210 void *user)
1211{
1212 term_T *term = (term_T *)user;
1213 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001214
1215 term->tl_cursor_pos = pos;
1216 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001217
1218 FOR_ALL_WINDOWS(wp)
1219 {
1220 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001221 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001222 }
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02001223 if (term->tl_buffer == curbuf && !term->tl_terminal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001224 {
1225 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001226 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001227 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001228
1229 return 1;
1230}
1231
Bram Moolenaar21554412017-07-24 21:44:43 +02001232 static int
1233handle_settermprop(
1234 VTermProp prop,
1235 VTermValue *value,
1236 void *user)
1237{
1238 term_T *term = (term_T *)user;
1239
1240 switch (prop)
1241 {
1242 case VTERM_PROP_TITLE:
1243 vim_free(term->tl_title);
1244 term->tl_title = vim_strsave((char_u *)value->string);
1245 vim_free(term->tl_status_text);
1246 term->tl_status_text = NULL;
1247 if (term == curbuf->b_term)
1248 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001249 break;
1250
1251 case VTERM_PROP_CURSORVISIBLE:
1252 term->tl_cursor_visible = value->boolean;
1253 may_toggle_cursor(term);
1254 out_flush();
1255 break;
1256
Bram Moolenaar21554412017-07-24 21:44:43 +02001257 default:
1258 break;
1259 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001260 /* Always return 1, otherwise vterm doesn't store the value internally. */
1261 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001262}
1263
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001264/*
1265 * The job running in the terminal resized the terminal.
1266 */
1267 static int
1268handle_resize(int rows, int cols, void *user)
1269{
1270 term_T *term = (term_T *)user;
1271 win_T *wp;
1272
1273 term->tl_rows = rows;
1274 term->tl_cols = cols;
1275 FOR_ALL_WINDOWS(wp)
1276 {
1277 if (wp->w_buffer == term->tl_buffer)
1278 {
1279 win_setheight_win(rows, wp);
1280 win_setwidth_win(cols, wp);
1281 }
1282 }
1283
1284 redraw_buf_later(term->tl_buffer, NOT_VALID);
1285 return 1;
1286}
1287
Bram Moolenaard85f2712017-07-28 21:51:57 +02001288/*
1289 * Handle a line that is pushed off the top of the screen.
1290 */
1291 static int
1292handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1293{
1294 term_T *term = (term_T *)user;
1295
1296 /* TODO: Limit the number of lines that are stored. */
1297 /* TODO: put the text in the buffer. */
1298 if (ga_grow(&term->tl_scrollback, 1) == OK)
1299 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001300 VTermScreenCell *p = NULL;
1301 int len = 0;
1302 int i;
1303 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001304
1305 /* do not store empty cells at the end */
1306 for (i = 0; i < cols; ++i)
1307 if (cells[i].chars[0] != 0)
1308 len = i + 1;
1309
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001310 if (len > 0)
1311 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001312 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001313 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001314
1315 line = (sb_line_T *)term->tl_scrollback.ga_data
1316 + term->tl_scrollback.ga_len;
1317 line->sb_cols = len;
1318 line->sb_cells = p;
1319 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001320 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001321
1322 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001323 }
1324 return 0; /* ignored */
1325}
1326
Bram Moolenaar21554412017-07-24 21:44:43 +02001327static VTermScreenCallbacks screen_callbacks = {
1328 handle_damage, /* damage */
1329 handle_moverect, /* moverect */
1330 handle_movecursor, /* movecursor */
1331 handle_settermprop, /* settermprop */
1332 NULL, /* bell */
1333 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001334 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001335 NULL /* sb_popline */
1336};
1337
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001338/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001339 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001340 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001341 */
1342 void
1343term_channel_closed(channel_T *ch)
1344{
1345 term_T *term;
1346 int did_one = FALSE;
1347
1348 for (term = first_term; term != NULL; term = term->tl_next)
1349 if (term->tl_job == ch->ch_job)
1350 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001351 term->tl_channel_closed = TRUE;
1352
Bram Moolenaard85f2712017-07-28 21:51:57 +02001353 vim_free(term->tl_title);
1354 term->tl_title = NULL;
1355 vim_free(term->tl_status_text);
1356 term->tl_status_text = NULL;
1357
Bram Moolenaar423802d2017-07-30 16:52:24 +02001358 /* Unless in Terminal-Normal mode: clear the vterm. */
1359 if (!term->tl_terminal_mode)
1360 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361
1362 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1363 did_one = TRUE;
1364 }
1365 if (did_one)
1366 {
1367 redraw_statuslines();
1368
1369 /* Need to break out of vgetc(). */
1370 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001371 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001372
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001373 term = curbuf->b_term;
1374 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001375 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001376 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001377 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001378 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001379 }
1380 }
1381}
1382
1383/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001384 * Reverse engineer the RGB value into a cterm color index.
1385 * First color is 1. Return 0 if no match found.
1386 */
1387 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001388color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001389{
1390 int red = color->red;
1391 int blue = color->blue;
1392 int green = color->green;
1393
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001394 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001395 if (red == 0)
1396 {
1397 if (green == 0)
1398 {
1399 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001400 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001401 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001402 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001403 }
1404 else if (green == 224)
1405 {
1406 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001407 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001408 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001409 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001410 }
1411 }
1412 else if (red == 224)
1413 {
1414 if (green == 0)
1415 {
1416 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001417 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001418 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001419 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001420 }
1421 else if (green == 224)
1422 {
1423 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001424 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001425 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001426 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001427 }
1428 }
1429 else if (red == 128)
1430 {
1431 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001432 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001433 }
1434 else if (red == 255)
1435 {
1436 if (green == 64)
1437 {
1438 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001439 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001440 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001441 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001442 }
1443 else if (green == 255)
1444 {
1445 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001446 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001447 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001448 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001449 }
1450 }
1451 else if (red == 64)
1452 {
1453 if (green == 64)
1454 {
1455 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001456 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001457 }
1458 else if (green == 255)
1459 {
1460 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001461 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001462 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001463 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001464 }
1465 }
1466 if (t_colors >= 256)
1467 {
1468 if (red == blue && red == green)
1469 {
1470 /* 24-color greyscale */
1471 static int cutoff[23] = {
1472 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1473 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1474 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1475 int i;
1476
1477 for (i = 0; i < 23; ++i)
1478 if (red < cutoff[i])
1479 return i + 233;
1480 return 256;
1481 }
1482
1483 /* 216-color cube */
1484 return 17 + ((red + 25) / 0x33) * 36
1485 + ((green + 25) / 0x33) * 6
1486 + (blue + 25) / 0x33;
1487 }
1488 return 0;
1489}
1490
1491/*
1492 * Convert the attributes of a vterm cell into an attribute index.
1493 */
1494 static int
1495cell2attr(VTermScreenCell *cell)
1496{
1497 int attr = 0;
1498
1499 if (cell->attrs.bold)
1500 attr |= HL_BOLD;
1501 if (cell->attrs.underline)
1502 attr |= HL_UNDERLINE;
1503 if (cell->attrs.italic)
1504 attr |= HL_ITALIC;
1505 if (cell->attrs.strike)
1506 attr |= HL_STANDOUT;
1507 if (cell->attrs.reverse)
1508 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001509
1510#ifdef FEAT_GUI
1511 if (gui.in_use)
1512 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001513 guicolor_T fg, bg;
1514
1515 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1516 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1517 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001518 }
1519 else
1520#endif
1521#ifdef FEAT_TERMGUICOLORS
1522 if (p_tgc)
1523 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001524 guicolor_T fg, bg;
1525
1526 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1527 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1528
1529 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001530 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001531 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001532#endif
1533 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001534 int bold = MAYBE;
1535 int fg = color2index(&cell->fg, TRUE, &bold);
1536 int bg = color2index(&cell->bg, FALSE, &bold);
1537
1538 /* with 8 colors set the bold attribute to get a bright foreground */
1539 if (bold == TRUE)
1540 attr |= HL_BOLD;
1541 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001542 }
1543 return 0;
1544}
1545
1546/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001547 * Called to update the window that contains a terminal.
1548 * Returns FAIL when there is no terminal running in this window.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001549 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001550 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001551term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001552{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001553 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001554 VTerm *vterm;
1555 VTermScreen *screen;
1556 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001557 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001558
Bram Moolenaar423802d2017-07-30 16:52:24 +02001559 if (term == NULL || term->tl_vterm == NULL || term->tl_terminal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001560 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001561
Bram Moolenaard85f2712017-07-28 21:51:57 +02001562 vterm = term->tl_vterm;
1563 screen = vterm_obtain_screen(vterm);
1564 state = vterm_obtain_state(vterm);
1565
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001566 /*
1567 * If the window was resized a redraw will be triggered and we get here.
1568 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1569 */
1570 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1571 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001572 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001573 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1574 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1575 win_T *twp;
1576
1577 FOR_ALL_WINDOWS(twp)
1578 {
1579 /* When more than one window shows the same terminal, use the
1580 * smallest size. */
1581 if (twp->w_buffer == term->tl_buffer)
1582 {
1583 if (!term->tl_rows_fixed && rows > twp->w_height)
1584 rows = twp->w_height;
1585 if (!term->tl_cols_fixed && cols > twp->w_width)
1586 cols = twp->w_width;
1587 }
1588 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001589
1590 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001591 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001592 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001593 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001594 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001595
1596 /* The cursor may have been moved when resizing. */
1597 vterm_state_get_cursorpos(state, &pos);
1598 position_cursor(wp, &pos);
1599
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001600 /* TODO: Only redraw what changed. */
1601 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001602 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001603 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001606 if (pos.row < term->tl_rows)
1607 {
1608 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001609 {
1610 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001611 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001612
Bram Moolenaareeac6772017-07-23 15:48:37 +02001613 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1614 vim_memset(&cell, 0, sizeof(cell));
1615
1616 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001617 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001618 if (c == NUL)
1619 {
1620 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001621#if defined(FEAT_MBYTE)
1622 if (enc_utf8)
1623 ScreenLinesUC[off] = NUL;
1624#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001625 }
1626 else
1627 {
1628#if defined(FEAT_MBYTE)
1629 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001630 {
1631 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001632 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001633 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001634 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001635 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001636 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001637 if (enc_utf8)
1638 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001639 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001640#else
1641 ScreenLines[off] = c;
1642#endif
1643 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001644 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001645
1646 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001647 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001648 if (cell.width == 2)
1649 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001650 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001651#if defined(FEAT_MBYTE)
1652 if (enc_utf8)
1653 ScreenLinesUC[off] = NUL;
1654#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001655 ++pos.col;
1656 ++off;
1657 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001658 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001659 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001660 else
1661 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001662
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001663 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1664 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001665 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001666
1667 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001668}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001669
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001670/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001671 * Return TRUE if "wp" is a terminal window where the job has finished.
1672 */
1673 int
1674term_is_finished(buf_T *buf)
1675{
1676 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1677}
1678
1679/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001680 * Return TRUE if "wp" is a terminal window where the job has finished or we
1681 * are in Terminal-Normal mode.
1682 */
1683 int
1684term_show_buffer(buf_T *buf)
1685{
1686 term_T *term = buf->b_term;
1687
1688 return term != NULL && (term->tl_vterm == NULL || term->tl_terminal_mode);
1689}
1690
1691/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001692 * The current buffer is going to be changed. If there is terminal
1693 * highlighting remove it now.
1694 */
1695 void
1696term_change_in_curbuf(void)
1697{
1698 term_T *term = curbuf->b_term;
1699
1700 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1701 {
1702 free_scrollback(term);
1703 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001704
1705 /* The buffer is now like a normal buffer, it cannot be easily
1706 * abandoned when changed. */
1707 set_string_option_direct((char_u *)"buftype", -1,
1708 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001709 }
1710}
1711
1712/*
1713 * Get the screen attribute for a position in the buffer.
1714 */
1715 int
1716term_get_attr(buf_T *buf, linenr_T lnum, int col)
1717{
1718 term_T *term = buf->b_term;
1719 sb_line_T *line;
1720
Bram Moolenaar70229f92017-07-29 16:01:53 +02001721 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001722 return 0;
1723 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1724 if (col >= line->sb_cols)
1725 return 0;
1726 return cell2attr(line->sb_cells + col);
1727}
1728
1729/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001730 * Create a new vterm and initialize it.
1731 */
1732 static void
1733create_vterm(term_T *term, int rows, int cols)
1734{
1735 VTerm *vterm;
1736 VTermScreen *screen;
1737
1738 vterm = vterm_new(rows, cols);
1739 term->tl_vterm = vterm;
1740 screen = vterm_obtain_screen(vterm);
1741 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1742 /* TODO: depends on 'encoding'. */
1743 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001744
1745 /* Vterm uses a default black background. Set it to white when
1746 * 'background' is "light". */
1747 if (*p_bg == 'l')
1748 {
1749 VTermColor fg, bg;
1750
1751 fg.red = fg.green = fg.blue = 0;
1752 bg.red = bg.green = bg.blue = 255;
1753 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1754 }
1755
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001756 /* Required to initialize most things. */
1757 vterm_screen_reset(screen, 1 /* hard */);
1758}
1759
Bram Moolenaar21554412017-07-24 21:44:43 +02001760/*
1761 * Return the text to show for the buffer name and status.
1762 */
1763 char_u *
1764term_get_status_text(term_T *term)
1765{
1766 if (term->tl_status_text == NULL)
1767 {
1768 char_u *txt;
1769 size_t len;
1770
Bram Moolenaar423802d2017-07-30 16:52:24 +02001771 if (term->tl_terminal_mode)
1772 {
1773 if (term_job_running(term))
1774 txt = (char_u *)_("Terminal");
1775 else
1776 txt = (char_u *)_("Terminal-finished");
1777 }
1778 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001779 txt = term->tl_title;
1780 else if (term_job_running(term))
1781 txt = (char_u *)_("running");
1782 else
1783 txt = (char_u *)_("finished");
1784 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001785 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001786 if (term->tl_status_text != NULL)
1787 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1788 term->tl_buffer->b_fname, txt);
1789 }
1790 return term->tl_status_text;
1791}
1792
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001793/*
1794 * Mark references in jobs of terminals.
1795 */
1796 int
1797set_ref_in_term(int copyID)
1798{
1799 int abort = FALSE;
1800 term_T *term;
1801 typval_T tv;
1802
1803 for (term = first_term; term != NULL; term = term->tl_next)
1804 if (term->tl_job != NULL)
1805 {
1806 tv.v_type = VAR_JOB;
1807 tv.vval.v_job = term->tl_job;
1808 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1809 }
1810 return abort;
1811}
1812
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001813/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001814 * Get the buffer from the first argument in "argvars".
1815 * Returns NULL when the buffer is not for a terminal window.
1816 */
1817 static buf_T *
1818term_get_buf(typval_T *argvars)
1819{
1820 buf_T *buf;
1821
1822 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1823 ++emsg_off;
1824 buf = get_buf_tv(&argvars[0], FALSE);
1825 --emsg_off;
1826 if (buf == NULL || buf->b_term == NULL)
1827 return NULL;
1828 return buf;
1829}
1830
1831/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001832 * "term_getattr(attr, name)" function
1833 */
1834 void
1835f_term_getattr(typval_T *argvars, typval_T *rettv)
1836{
1837 int attr;
1838 size_t i;
1839 char_u *name;
1840
1841 static struct {
1842 char *name;
1843 int attr;
1844 } attrs[] = {
1845 {"bold", HL_BOLD},
1846 {"italic", HL_ITALIC},
1847 {"underline", HL_UNDERLINE},
1848 {"strike", HL_STANDOUT},
1849 {"reverse", HL_INVERSE},
1850 };
1851
1852 attr = get_tv_number(&argvars[0]);
1853 name = get_tv_string_chk(&argvars[1]);
1854 if (name == NULL)
1855 return;
1856
1857 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1858 if (STRCMP(name, attrs[i].name) == 0)
1859 {
1860 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1861 break;
1862 }
1863}
1864
1865/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001866 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001867 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001868 void
1869f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001870{
Bram Moolenaar97870002017-07-30 18:28:38 +02001871 buf_T *buf = term_get_buf(argvars);
1872 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001873
Bram Moolenaar97870002017-07-30 18:28:38 +02001874 if (rettv_list_alloc(rettv) == FAIL)
1875 return;
1876 if (buf == NULL)
1877 return;
1878
1879 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001880 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1881 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001882 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001883}
1884
1885/*
1886 * "term_getjob(buf)" function
1887 */
1888 void
1889f_term_getjob(typval_T *argvars, typval_T *rettv)
1890{
1891 buf_T *buf = term_get_buf(argvars);
1892
1893 rettv->v_type = VAR_JOB;
1894 rettv->vval.v_job = NULL;
1895 if (buf == NULL)
1896 return;
1897
1898 rettv->vval.v_job = buf->b_term->tl_job;
1899 if (rettv->vval.v_job != NULL)
1900 ++rettv->vval.v_job->jv_refcount;
1901}
1902
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001903 static int
1904get_row_number(typval_T *tv, term_T *term)
1905{
1906 if (tv->v_type == VAR_STRING
1907 && tv->vval.v_string != NULL
1908 && STRCMP(tv->vval.v_string, ".") == 0)
1909 return term->tl_cursor_pos.row;
1910 return (int)get_tv_number(tv) - 1;
1911}
1912
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001913/*
1914 * "term_getline(buf, row)" function
1915 */
1916 void
1917f_term_getline(typval_T *argvars, typval_T *rettv)
1918{
1919 buf_T *buf = term_get_buf(argvars);
1920 term_T *term;
1921 int row;
1922
1923 rettv->v_type = VAR_STRING;
1924 if (buf == NULL)
1925 return;
1926 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001927 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001928
1929 if (term->tl_vterm == NULL)
1930 {
1931 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1932
1933 /* vterm is finished, get the text from the buffer */
1934 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1935 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1936 }
1937 else
1938 {
1939 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1940 VTermRect rect;
1941 int len;
1942 char_u *p;
1943
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001944 if (row < 0 || row >= term->tl_rows)
1945 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001946 len = term->tl_cols * MB_MAXBYTES + 1;
1947 p = alloc(len);
1948 if (p == NULL)
1949 return;
1950 rettv->vval.v_string = p;
1951
1952 rect.start_col = 0;
1953 rect.end_col = term->tl_cols;
1954 rect.start_row = row;
1955 rect.end_row = row + 1;
1956 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1957 }
1958}
1959
1960/*
1961 * "term_getsize(buf)" function
1962 */
1963 void
1964f_term_getsize(typval_T *argvars, typval_T *rettv)
1965{
1966 buf_T *buf = term_get_buf(argvars);
1967 list_T *l;
1968
1969 if (rettv_list_alloc(rettv) == FAIL)
1970 return;
1971 if (buf == NULL)
1972 return;
1973
1974 l = rettv->vval.v_list;
1975 list_append_number(l, buf->b_term->tl_rows);
1976 list_append_number(l, buf->b_term->tl_cols);
1977}
1978
1979/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001980 * "term_getstatus(buf)" function
1981 */
1982 void
1983f_term_getstatus(typval_T *argvars, typval_T *rettv)
1984{
1985 buf_T *buf = term_get_buf(argvars);
1986 term_T *term;
1987 char_u val[100];
1988
1989 rettv->v_type = VAR_STRING;
1990 if (buf == NULL)
1991 return;
1992 term = buf->b_term;
1993
1994 if (term_job_running(term))
1995 STRCPY(val, "running");
1996 else
1997 STRCPY(val, "finished");
1998 if (term->tl_terminal_mode)
1999 STRCAT(val, ",terminal");
2000 rettv->vval.v_string = vim_strsave(val);
2001}
2002
2003/*
2004 * "term_gettitle(buf)" function
2005 */
2006 void
2007f_term_gettitle(typval_T *argvars, typval_T *rettv)
2008{
2009 buf_T *buf = term_get_buf(argvars);
2010
2011 rettv->v_type = VAR_STRING;
2012 if (buf == NULL)
2013 return;
2014
2015 if (buf->b_term->tl_title != NULL)
2016 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2017}
2018
2019/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002020 * "term_gettty(buf)" function
2021 */
2022 void
2023f_term_gettty(typval_T *argvars, typval_T *rettv)
2024{
2025 buf_T *buf = term_get_buf(argvars);
2026 char_u *p;
2027
2028 rettv->v_type = VAR_STRING;
2029 if (buf == NULL)
2030 return;
2031 if (buf->b_term->tl_job != NULL)
2032 p = buf->b_term->tl_job->jv_tty_name;
2033 else
2034 p = buf->b_term->tl_tty_name;
2035 if (p != NULL)
2036 rettv->vval.v_string = vim_strsave(p);
2037}
2038
2039/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002040 * "term_list()" function
2041 */
2042 void
2043f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2044{
2045 term_T *tp;
2046 list_T *l;
2047
2048 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2049 return;
2050
2051 l = rettv->vval.v_list;
2052 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2053 if (tp != NULL && tp->tl_buffer != NULL)
2054 if (list_append_number(l,
2055 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2056 return;
2057}
2058
2059/*
2060 * "term_scrape(buf, row)" function
2061 */
2062 void
2063f_term_scrape(typval_T *argvars, typval_T *rettv)
2064{
2065 buf_T *buf = term_get_buf(argvars);
2066 VTermScreen *screen = NULL;
2067 VTermPos pos;
2068 list_T *l;
2069 term_T *term;
2070
2071 if (rettv_list_alloc(rettv) == FAIL)
2072 return;
2073 if (buf == NULL)
2074 return;
2075 term = buf->b_term;
2076 if (term->tl_vterm != NULL)
2077 screen = vterm_obtain_screen(term->tl_vterm);
2078
2079 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002080 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002081 for (pos.col = 0; pos.col < term->tl_cols; )
2082 {
2083 dict_T *dcell;
2084 VTermScreenCell cell;
2085 char_u rgb[8];
2086 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2087 int off = 0;
2088 int i;
2089
2090 if (screen == NULL)
2091 {
2092 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2093 sb_line_T *line;
2094
2095 /* vterm has finished, get the cell from scrollback */
2096 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2097 break;
2098 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2099 if (pos.col >= line->sb_cols)
2100 break;
2101 cell = line->sb_cells[pos.col];
2102 }
2103 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2104 break;
2105 dcell = dict_alloc();
2106 list_append_dict(l, dcell);
2107
2108 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2109 {
2110 if (cell.chars[i] == 0)
2111 break;
2112 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2113 }
2114 mbs[off] = NUL;
2115 dict_add_nr_str(dcell, "chars", 0, mbs);
2116
2117 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2118 cell.fg.red, cell.fg.green, cell.fg.blue);
2119 dict_add_nr_str(dcell, "fg", 0, rgb);
2120 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2121 cell.bg.red, cell.bg.green, cell.bg.blue);
2122 dict_add_nr_str(dcell, "bg", 0, rgb);
2123
2124 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2125 dict_add_nr_str(dcell, "width", cell.width, NULL);
2126
2127 ++pos.col;
2128 if (cell.width == 2)
2129 ++pos.col;
2130 }
2131}
2132
2133/*
2134 * "term_sendkeys(buf, keys)" function
2135 */
2136 void
2137f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2138{
2139 buf_T *buf = term_get_buf(argvars);
2140 char_u *msg;
2141 term_T *term;
2142
2143 rettv->v_type = VAR_UNKNOWN;
2144 if (buf == NULL)
2145 return;
2146
2147 msg = get_tv_string_chk(&argvars[1]);
2148 if (msg == NULL)
2149 return;
2150 term = buf->b_term;
2151 if (term->tl_vterm == NULL)
2152 return;
2153
2154 while (*msg != NUL)
2155 {
2156 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2157 msg += MB_PTR2LEN(msg);
2158 }
2159
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002160 if (!term->tl_terminal_mode)
2161 {
2162 /* TODO: only update once in a while. */
2163 update_screen(0);
2164 if (buf == curbuf)
2165 update_cursor(term, TRUE);
2166 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002167}
2168
2169/*
2170 * "term_start(command, options)" function
2171 */
2172 void
2173f_term_start(typval_T *argvars, typval_T *rettv)
2174{
2175 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002176 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002177
2178 if (cmd == NULL)
2179 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002180 init_job_options(&opt);
2181 /* TODO: allow more job options */
2182 if (argvars[1].v_type != VAR_UNKNOWN
2183 && get_job_options(&argvars[1], &opt,
2184 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002185 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2186 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002187 return;
2188
2189 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002190
2191 if (curbuf->b_term != NULL)
2192 rettv->vval.v_number = curbuf->b_fnum;
2193}
2194
2195/*
2196 * "term_wait" function
2197 */
2198 void
2199f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2200{
2201 buf_T *buf = term_get_buf(argvars);
2202
2203 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002204 {
2205 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002206 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002207 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002208 if (buf->b_term->tl_job == NULL)
2209 {
2210 ch_log(NULL, "term_wait(): no job to wait for");
2211 return;
2212 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002213
2214 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002215 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002216 {
2217 /* The job is dead, keep reading channel I/O until the channel is
2218 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002219 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002220 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2221 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002222 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002223 parse_queued_messages();
2224 ui_delay(10L, FALSE);
2225 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002226 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002227 parse_queued_messages();
2228 }
2229 else
2230 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002231 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002232 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002233
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002234 /* Wait for 10 msec for any channel I/O. */
2235 /* TODO: use delay from optional argument */
2236 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002237 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002238
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002239 /* Flushing messages on channels is hopefully sufficient.
2240 * TODO: is there a better way? */
2241 parse_queued_messages();
2242 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002243}
2244
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002245# ifdef WIN3264
2246
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002247/**************************************
2248 * 2. MS-Windows implementation.
2249 */
2250
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002251#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2252#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2253
Bram Moolenaar8a773062017-07-24 22:29:21 +02002254void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002255void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002256void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002257BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2258void (*winpty_config_set_initial_size)(void*, int, int);
2259LPCWSTR (*winpty_conin_name)(void*);
2260LPCWSTR (*winpty_conout_name)(void*);
2261LPCWSTR (*winpty_conerr_name)(void*);
2262void (*winpty_free)(void*);
2263void (*winpty_config_free)(void*);
2264void (*winpty_spawn_config_free)(void*);
2265void (*winpty_error_free)(void*);
2266LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002267BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002268HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002269
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002270#define WINPTY_DLL "winpty.dll"
2271
2272static HINSTANCE hWinPtyDLL = NULL;
2273
2274 int
2275dyn_winpty_init(void)
2276{
2277 int i;
2278 static struct
2279 {
2280 char *name;
2281 FARPROC *ptr;
2282 } winpty_entry[] =
2283 {
2284 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2285 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2286 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2287 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2288 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2289 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2290 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2291 {"winpty_free", (FARPROC*)&winpty_free},
2292 {"winpty_open", (FARPROC*)&winpty_open},
2293 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2294 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2295 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2296 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002297 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002298 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002299 {NULL, NULL}
2300 };
2301
2302 /* No need to initialize twice. */
2303 if (hWinPtyDLL)
2304 return 1;
2305 /* Load winpty.dll */
2306 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2307 if (!hWinPtyDLL)
2308 {
2309 EMSG2(_(e_loadlib), WINPTY_DLL);
2310 return 0;
2311 }
2312 for (i = 0; winpty_entry[i].name != NULL
2313 && winpty_entry[i].ptr != NULL; ++i)
2314 {
2315 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2316 winpty_entry[i].name)) == NULL)
2317 {
2318 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2319 return 0;
2320 }
2321 }
2322
2323 return 1;
2324}
2325
2326/*
2327 * Create a new terminal of "rows" by "cols" cells.
2328 * Store a reference in "term".
2329 * Return OK or FAIL.
2330 */
2331 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002332term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002333{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002334 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002335 channel_T *channel = NULL;
2336 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002337 DWORD error;
2338 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2339 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002340 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002341 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002342
2343 if (!dyn_winpty_init())
2344 return FAIL;
2345
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002346 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002347 if (p == NULL)
2348 return FAIL;
2349
2350 job = job_alloc();
2351 if (job == NULL)
2352 goto failed;
2353
2354 channel = add_channel();
2355 if (channel == NULL)
2356 goto failed;
2357
2358 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2359 if (term->tl_winpty_config == NULL)
2360 goto failed;
2361
2362 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2363 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2364 if (term->tl_winpty == NULL)
2365 goto failed;
2366
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002367 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002368 spawn_config = winpty_spawn_config_new(
2369 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2370 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2371 NULL,
2372 p,
2373 NULL,
2374 NULL,
2375 &winpty_err);
2376 if (spawn_config == NULL)
2377 goto failed;
2378
2379 channel = add_channel();
2380 if (channel == NULL)
2381 goto failed;
2382
2383 job = job_alloc();
2384 if (job == NULL)
2385 goto failed;
2386
2387 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2388 &child_thread_handle, &error, &winpty_err))
2389 goto failed;
2390
2391 channel_set_pipes(channel,
2392 (sock_T) CreateFileW(
2393 winpty_conin_name(term->tl_winpty),
2394 GENERIC_WRITE, 0, NULL,
2395 OPEN_EXISTING, 0, NULL),
2396 (sock_T) CreateFileW(
2397 winpty_conout_name(term->tl_winpty),
2398 GENERIC_READ, 0, NULL,
2399 OPEN_EXISTING, 0, NULL),
2400 (sock_T) CreateFileW(
2401 winpty_conerr_name(term->tl_winpty),
2402 GENERIC_READ, 0, NULL,
2403 OPEN_EXISTING, 0, NULL));
2404
2405 jo = CreateJobObject(NULL, NULL);
2406 if (jo == NULL)
2407 goto failed;
2408
2409 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002410 {
2411 /* Failed, switch the way to terminate process with TerminateProcess. */
2412 CloseHandle(jo);
2413 jo = NULL;
2414 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002415
2416 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002417 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002418
2419 create_vterm(term, rows, cols);
2420
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002421 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002422 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002423
2424 job->jv_channel = channel;
2425 job->jv_proc_info.hProcess = child_process_handle;
2426 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2427 job->jv_job_object = jo;
2428 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002429 sprintf(buf, "winpty://%lu",
2430 GetProcessId(winpty_agent_process(term->tl_winpty)));
2431 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002432 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002433 term->tl_job = job;
2434
2435 return OK;
2436
2437failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002438 if (spawn_config != NULL)
2439 winpty_spawn_config_free(spawn_config);
2440 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002441 if (channel != NULL)
2442 channel_clear(channel);
2443 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002444 {
2445 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002446 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002447 }
2448 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002449 if (jo != NULL)
2450 CloseHandle(jo);
2451 if (term->tl_winpty != NULL)
2452 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002453 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002454 if (term->tl_winpty_config != NULL)
2455 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002456 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002457 if (winpty_err != NULL)
2458 {
2459 char_u *msg = utf16_to_enc(
2460 (short_u *)winpty_error_msg(winpty_err), NULL);
2461
2462 EMSG(msg);
2463 winpty_error_free(winpty_err);
2464 }
2465 return FAIL;
2466}
2467
2468/*
2469 * Free the terminal emulator part of "term".
2470 */
2471 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002472term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002473{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002474 if (term->tl_winpty != NULL)
2475 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002476 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002477 if (term->tl_winpty_config != NULL)
2478 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002479 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002480 if (term->tl_vterm != NULL)
2481 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002482 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002483}
2484
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002485/*
2486 * Request size to terminal.
2487 */
2488 static void
2489term_report_winsize(term_T *term, int rows, int cols)
2490{
2491 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2492}
2493
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002494# else
2495
2496/**************************************
2497 * 3. Unix-like implementation.
2498 */
2499
2500/*
2501 * Create a new terminal of "rows" by "cols" cells.
2502 * Start job for "cmd".
2503 * Store the pointers in "term".
2504 * Return OK or FAIL.
2505 */
2506 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002507term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002508{
2509 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002510
2511 create_vterm(term, rows, cols);
2512
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002513 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002514 argvars[0].v_type = VAR_STRING;
2515 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002516
2517 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002518 if (term->tl_job != NULL)
2519 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002520
Bram Moolenaar61a66052017-07-22 18:39:00 +02002521 return term->tl_job != NULL
2522 && term->tl_job->jv_channel != NULL
2523 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002524}
2525
2526/*
2527 * Free the terminal emulator part of "term".
2528 */
2529 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002530term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002531{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002532 if (term->tl_vterm != NULL)
2533 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002534 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002535}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002536
2537/*
2538 * Request size to terminal.
2539 */
2540 static void
2541term_report_winsize(term_T *term, int rows, int cols)
2542{
2543 /* Use an ioctl() to report the new window size to the job. */
2544 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2545 {
2546 int fd = -1;
2547 int part;
2548
2549 for (part = PART_OUT; part < PART_COUNT; ++part)
2550 {
2551 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2552 if (isatty(fd))
2553 break;
2554 }
2555 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2556 mch_stop_job(term->tl_job, (char_u *)"winch");
2557 }
2558}
2559
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002560# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002561
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002562#endif /* FEAT_TERMINAL */