blob: 0f70cc6a994e4b9cc3d6e7898a367f38f8b03896 [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 Moolenaard85f2712017-07-28 21:51:57 +020039 * - For the scrollback buffer store lines in the buffer, only attributes in
40 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020041 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020042 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020043 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
44 * job finishes).
45 * - add option values to the command:
46 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020047 * - support different cursor shapes, colors and attributes
Bram Moolenaar6d819742017-08-06 14:57:49 +020048 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020049 * - make term_getcursor() return type (none/block/bar/underline) and
50 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020051 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020052 * For the GUI fill termios with default values, perhaps like pangoterm:
53 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
54 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020055 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020056 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020057 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020058 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020059 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
61 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020062 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020063 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020064 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020065 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020066 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020067 * terminal. Allow:
68 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
69 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
70 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
71 * Check that something is connected to the terminal.
72 * Test: "cat" reading from a file or buffer
73 * "ls" writing stdout to a file or buffer
74 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020075 * - support ":term NONE" to open a terminal with a pty but not running a job
76 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020077 * - if the job in the terminal does not support the mouse, we can use the
78 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
80 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020081 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020082 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020083 * - Copy text in the vterm to the Vim buffer once in a while, so that
84 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020085 */
86
87#include "vim.h"
88
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020089#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020090
Bram Moolenaard5310982017-08-05 15:16:32 +020091#ifndef MIN
92# define MIN(x,y) ((x) < (y) ? (x) : (y))
93#endif
94#ifndef MAX
95# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020096#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020098#include "libvterm/include/vterm.h"
99
Bram Moolenaard85f2712017-07-28 21:51:57 +0200100typedef struct sb_line_S {
101 int sb_cols; /* can differ per line */
102 VTermScreenCell *sb_cells; /* allocated */
103} sb_line_T;
104
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200105/* typedef term_T in structs.h */
106struct terminal_S {
107 term_T *tl_next;
108
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109 VTerm *tl_vterm;
110 job_T *tl_job;
111 buf_T *tl_buffer;
112
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200113 /* used when tl_job is NULL and only a pty was created */
114 int tl_tty_fd;
115 char_u *tl_tty_name;
116
Bram Moolenaar6d819742017-08-06 14:57:49 +0200117 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200118 int tl_channel_closed;
119
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200120#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200121 void *tl_winpty_config;
122 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125 /* last known vterm size */
126 int tl_rows;
127 int tl_cols;
128 /* vterm size does not follow window size */
129 int tl_rows_fixed;
130 int tl_cols_fixed;
131
Bram Moolenaar21554412017-07-24 21:44:43 +0200132 char_u *tl_title; /* NULL or allocated */
133 char_u *tl_status_text; /* NULL or allocated */
134
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200135 /* Range of screen rows to update. Zero based. */
136 int tl_dirty_row_start; /* -1 if nothing dirty */
137 int tl_dirty_row_end; /* row below last one to update */
138
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200140 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200142 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200143 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144};
145
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200146#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
147#define TMODE_LOOP 2 /* CTRL-W N used */
148
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149/*
150 * List of all active terminals.
151 */
152static term_T *first_term = NULL;
153
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200154
155#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
156#define KEY_BUF_LEN 200
157
158/*
159 * Functions with separate implementation for MS-Windows and Unix-like systems.
160 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200161static 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 +0200162static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200163static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200164
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200165/**************************************
166 * 1. Generic code for all systems.
167 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200168
169/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200170 * Determine the terminal size from 'termsize' and the current window.
171 * Assumes term->tl_rows and term->tl_cols are zero.
172 */
173 static void
174set_term_and_win_size(term_T *term)
175{
176 if (*curwin->w_p_tms != NUL)
177 {
178 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
179
180 term->tl_rows = atoi((char *)curwin->w_p_tms);
181 term->tl_cols = atoi((char *)p);
182 }
183 if (term->tl_rows == 0)
184 term->tl_rows = curwin->w_height;
185 else
186 {
187 win_setheight_win(term->tl_rows, curwin);
188 term->tl_rows_fixed = TRUE;
189 }
190 if (term->tl_cols == 0)
191 term->tl_cols = curwin->w_width;
192 else
193 {
194 win_setwidth_win(term->tl_cols, curwin);
195 term->tl_cols_fixed = TRUE;
196 }
197}
198
199/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200200 * Initialize job options for a terminal job.
201 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200202 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200203 static void
204init_job_options(jobopt_T *opt)
205{
206 clear_job_options(opt);
207
208 opt->jo_mode = MODE_RAW;
209 opt->jo_out_mode = MODE_RAW;
210 opt->jo_err_mode = MODE_RAW;
211 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
212
213 opt->jo_io[PART_OUT] = JIO_BUFFER;
214 opt->jo_io[PART_ERR] = JIO_BUFFER;
215 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
216
217 opt->jo_modifiable[PART_OUT] = 0;
218 opt->jo_modifiable[PART_ERR] = 0;
219 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
220
221 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
222}
223
224/*
225 * Set job options mandatory for a terminal job.
226 */
227 static void
228setup_job_options(jobopt_T *opt, int rows, int cols)
229{
230 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
231 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
232 opt->jo_pty = TRUE;
233 opt->jo_term_rows = rows;
234 opt->jo_term_cols = cols;
235}
236
237 static void
238term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200239{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200240 exarg_T split_ea;
241 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200242 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200243
244 if (check_restricted() || check_secure())
245 return;
246
247 term = (term_T *)alloc_clear(sizeof(term_T));
248 if (term == NULL)
249 return;
250 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200251 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200252 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253
254 /* Open a new window or tab. */
255 vim_memset(&split_ea, 0, sizeof(split_ea));
256 split_ea.cmdidx = CMD_new;
257 split_ea.cmd = (char_u *)"new";
258 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200259 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
260 {
261 split_ea.line2 = opt->jo_term_rows;
262 split_ea.addr_count = 1;
263 }
264 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
265 {
266 split_ea.line2 = opt->jo_term_cols;
267 split_ea.addr_count = 1;
268 }
269
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200270 ex_splitview(&split_ea);
271 if (curwin == old_curwin)
272 {
273 /* split failed */
274 vim_free(term);
275 return;
276 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200277 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200278 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200279
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200280 /* only one size was taken care of with :new, do the other one */
281 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
282 win_setheight(opt->jo_term_rows);
283 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
284 win_setwidth(opt->jo_term_cols);
285
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200286 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200287 term->tl_next = first_term;
288 first_term = term;
289
Bram Moolenaar293424c2017-07-26 23:11:01 +0200290 if (cmd == NULL || *cmd == NUL)
291 cmd = p_sh;
292
Bram Moolenaar78712a72017-08-05 14:50:12 +0200293 if (opt->jo_term_name != NULL)
294 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
295 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200296 {
297 int i;
298 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200299 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200300
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200301 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200302 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200303 /* Prepend a ! to the command name to avoid the buffer name equals
304 * the executable, otherwise ":w!" would overwrite it. */
305 if (i == 0)
306 vim_snprintf((char *)p, len, "!%s", cmd);
307 else
308 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200309 if (buflist_findname(p) == NULL)
310 {
311 curbuf->b_ffname = p;
312 break;
313 }
314 }
315 }
316 curbuf->b_fname = curbuf->b_ffname;
317
Bram Moolenaareb44a682017-08-03 22:44:55 +0200318 set_string_option_direct((char_u *)"buftype", -1,
319 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
320
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200321 /* Mark the buffer as not modifiable. It can only be made modifiable after
322 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200323 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200324
325 /* Set 'bufhidden' to "hide": allow closing the window. */
326 set_string_option_direct((char_u *)"bufhidden", -1,
327 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200330 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200331
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200332 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200333 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200334 {
335 /* store the size we ended up with */
336 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200337 }
338 else
339 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200340 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200341
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200342 /* Wiping out the buffer will also close the window and call
343 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200344 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200345 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200346}
347
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200348/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200349 * ":terminal": open a terminal window and execute a job in it.
350 */
351 void
352ex_terminal(exarg_T *eap)
353{
354 jobopt_T opt;
355
356 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200357
358 if (eap->addr_count == 2)
359 {
360 opt.jo_term_rows = eap->line1;
361 opt.jo_term_cols = eap->line2;
362 }
363 else if (eap->addr_count == 1)
364 {
365 if (cmdmod.split & WSP_VERT)
366 opt.jo_term_cols = eap->line2;
367 else
368 opt.jo_term_rows = eap->line2;
369 }
370 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200371
372 term_start(eap->arg, &opt);
373}
374
375/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200376 * Free the scrollback buffer for "term".
377 */
378 static void
379free_scrollback(term_T *term)
380{
381 int i;
382
383 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
384 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
385 ga_clear(&term->tl_scrollback);
386}
387
388/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200389 * Free a terminal and everything it refers to.
390 * Kills the job if there is one.
391 * Called when wiping out a buffer.
392 */
393 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200394free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200395{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200396 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200397 term_T *tp;
398
399 if (term == NULL)
400 return;
401 if (first_term == term)
402 first_term = term->tl_next;
403 else
404 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
405 if (tp->tl_next == term)
406 {
407 tp->tl_next = term->tl_next;
408 break;
409 }
410
411 if (term->tl_job != NULL)
412 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200413 if (term->tl_job->jv_status != JOB_ENDED
414 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200415 job_stop(term->tl_job, NULL, "kill");
416 job_unref(term->tl_job);
417 }
418
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200419 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200420
421 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200422 vim_free(term->tl_title);
423 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200424 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200425 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200426}
427
428/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200429 * Write job output "msg[len]" to the vterm.
430 */
431 static void
432term_write_job_output(term_T *term, char_u *msg, size_t len)
433{
434 VTerm *vterm = term->tl_vterm;
435 char_u *p;
436 size_t done;
437 size_t len_now;
438
439 for (done = 0; done < len; done += len_now)
440 {
441 for (p = msg + done; p < msg + len; )
442 {
443 if (*p == NL)
444 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200445 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200446 }
447 len_now = p - msg - done;
448 vterm_input_write(vterm, (char *)msg + done, len_now);
449 if (p < msg + len && *p == NL)
450 {
451 /* Convert NL to CR-NL, that appears to work best. */
452 vterm_input_write(vterm, "\r\n", 2);
453 ++len_now;
454 }
455 }
456
457 /* this invokes the damage callbacks */
458 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
459}
460
Bram Moolenaar1c844932017-07-24 23:36:41 +0200461 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200462update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200463{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200464 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200465 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200466 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200467 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200468 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200469 if (term->tl_cursor_visible)
470 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200471 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200472#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200473 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200474 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200475#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200476 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200477}
478
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200479/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200480 * Invoked when "msg" output from a job was received. Write it to the terminal
481 * of "buffer".
482 */
483 void
484write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
485{
486 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200487 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200488
Bram Moolenaard85f2712017-07-28 21:51:57 +0200489 if (term->tl_vterm == NULL)
490 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200491 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200492 return;
493 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200494 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200495 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200496
Bram Moolenaar6d819742017-08-06 14:57:49 +0200497 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200498 {
499 /* TODO: only update once in a while. */
500 update_screen(0);
501 update_cursor(term, TRUE);
502 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200503}
504
505/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200506 * Send a mouse position and click to the vterm
507 */
508 static int
509term_send_mouse(VTerm *vterm, int button, int pressed)
510{
511 VTermModifier mod = VTERM_MOD_NONE;
512
513 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
514 mouse_col - W_WINCOL(curwin), mod);
515 vterm_mouse_button(vterm, button, pressed, mod);
516 return TRUE;
517}
518
519/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200520 * Convert typed key "c" into bytes to send to the job.
521 * Return the number of bytes in "buf".
522 */
523 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200524term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200525{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200526 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200527 VTermKey key = VTERM_KEY_NONE;
528 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200529 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200530
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200531 switch (c)
532 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200533 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200535 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
536 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200537 case K_DEL: key = VTERM_KEY_DEL; break;
538 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200539 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
540 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200541 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200542 case K_S_END: mod = VTERM_MOD_SHIFT;
543 key = VTERM_KEY_END; break;
544 case K_C_END: mod = VTERM_MOD_CTRL;
545 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200546 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
547 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
548 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
549 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
550 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
551 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
552 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
553 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
554 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
555 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
556 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
557 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
558 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200559 case K_S_HOME: mod = VTERM_MOD_SHIFT;
560 key = VTERM_KEY_HOME; break;
561 case K_C_HOME: mod = VTERM_MOD_CTRL;
562 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200563 case K_INS: key = VTERM_KEY_INS; break;
564 case K_K0: key = VTERM_KEY_KP_0; break;
565 case K_K1: key = VTERM_KEY_KP_1; break;
566 case K_K2: key = VTERM_KEY_KP_2; break;
567 case K_K3: key = VTERM_KEY_KP_3; break;
568 case K_K4: key = VTERM_KEY_KP_4; break;
569 case K_K5: key = VTERM_KEY_KP_5; break;
570 case K_K6: key = VTERM_KEY_KP_6; break;
571 case K_K7: key = VTERM_KEY_KP_7; break;
572 case K_K8: key = VTERM_KEY_KP_8; break;
573 case K_K9: key = VTERM_KEY_KP_9; break;
574 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
575 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
576 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
577 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
578 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
579 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
580 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
581 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
582 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
583 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
584 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
585 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
586 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200587 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
588 key = VTERM_KEY_LEFT; break;
589 case K_C_LEFT: mod = VTERM_MOD_CTRL;
590 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200591 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
592 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
593 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200594 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
595 key = VTERM_KEY_RIGHT; break;
596 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
597 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200598 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200599 case K_S_UP: mod = VTERM_MOD_SHIFT;
600 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200601 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200602
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200603 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
604 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
605 case K_MOUSELEFT: /* TODO */ return 0;
606 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200607
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200608 case K_LEFTMOUSE:
609 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
610 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
611 case K_LEFTRELEASE:
612 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
613 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
614 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
615 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
616 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
617 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
618 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
619 case K_X1MOUSE: /* TODO */ return 0;
620 case K_X1DRAG: /* TODO */ return 0;
621 case K_X1RELEASE: /* TODO */ return 0;
622 case K_X2MOUSE: /* TODO */ return 0;
623 case K_X2DRAG: /* TODO */ return 0;
624 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200625
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200626 case K_IGNORE: return 0;
627 case K_NOP: return 0;
628 case K_UNDO: return 0;
629 case K_HELP: return 0;
630 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
631 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
632 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
633 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
634 case K_SELECT: return 0;
635#ifdef FEAT_GUI
636 case K_VER_SCROLLBAR: return 0;
637 case K_HOR_SCROLLBAR: return 0;
638#endif
639#ifdef FEAT_GUI_TABLINE
640 case K_TABLINE: return 0;
641 case K_TABMENU: return 0;
642#endif
643#ifdef FEAT_NETBEANS_INTG
644 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
645#endif
646#ifdef FEAT_DND
647 case K_DROP: return 0;
648#endif
649#ifdef FEAT_AUTOCMD
650 case K_CURSORHOLD: return 0;
651#endif
652 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
653 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654 }
655
656 /*
657 * Convert special keys to vterm keys:
658 * - Write keys to vterm: vterm_keyboard_key()
659 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200660 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 */
662 if (key != VTERM_KEY_NONE)
663 /* Special key, let vterm convert it. */
664 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200665 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200666 /* Normal character, let vterm convert it. */
667 vterm_keyboard_unichar(vterm, c, mod);
668
669 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200670 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200671}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200672
Bram Moolenaar938783d2017-07-16 20:13:26 +0200673/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200674 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200675 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200676 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200677term_job_running(term_T *term)
678{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200679 /* Also consider the job finished when the channel is closed, to avoid a
680 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200681 return term != NULL
682 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200683 && term->tl_job->jv_status == JOB_STARTED
684 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200685}
686
687/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200688 * Add the last line of the scrollback buffer to the buffer in the window.
689 */
690 static void
691add_scrollback_line_to_buffer(term_T *term)
692{
693 linenr_T lnum = term->tl_scrollback.ga_len - 1;
694 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
695 garray_T ga;
696 int c;
697 int col;
698 int i;
699
700 ga_init2(&ga, 1, 100);
701 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
702 {
703 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
704 goto failed;
705 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
706 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
707 (char_u *)ga.ga_data + ga.ga_len);
708 }
709 if (ga_grow(&ga, 1) == FAIL)
710 goto failed;
711 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
712 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
713
714 if (lnum == 0)
715 {
716 /* Delete the empty line that was in the empty buffer. */
717 curbuf = term->tl_buffer;
718 ml_delete(2, FALSE);
719 curbuf = curwin->w_buffer;
720 }
721
722failed:
723 ga_clear(&ga);
724}
725
726/*
727 * Add the current lines of the terminal to scrollback and to the buffer.
728 * Called after the job has ended and when switching to Terminal mode.
729 */
730 static void
731move_terminal_to_buffer(term_T *term)
732{
733 win_T *wp;
734 int len;
735 int lines_skipped = 0;
736 VTermPos pos;
737 VTermScreenCell cell;
738 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200739 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200740
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200741 if (term->tl_vterm == NULL)
742 return;
743 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200744 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
745 {
746 len = 0;
747 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
748 if (vterm_screen_get_cell(screen, pos, &cell) != 0
749 && cell.chars[0] != NUL)
750 len = pos.col + 1;
751
752 if (len == 0)
753 ++lines_skipped;
754 else
755 {
756 while (lines_skipped > 0)
757 {
758 /* Line was skipped, add an empty line. */
759 --lines_skipped;
760 if (ga_grow(&term->tl_scrollback, 1) == OK)
761 {
762 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
763 + term->tl_scrollback.ga_len;
764
765 line->sb_cols = 0;
766 line->sb_cells = NULL;
767 ++term->tl_scrollback.ga_len;
768
769 add_scrollback_line_to_buffer(term);
770 }
771 }
772
773 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
774 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
775 {
776 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
777 + term->tl_scrollback.ga_len;
778
779 for (pos.col = 0; pos.col < len; ++pos.col)
780 {
781 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
782 vim_memset(p + pos.col, 0, sizeof(cell));
783 else
784 p[pos.col] = cell;
785 }
786 line->sb_cols = len;
787 line->sb_cells = p;
788 ++term->tl_scrollback.ga_len;
789
790 add_scrollback_line_to_buffer(term);
791 }
792 else
793 vim_free(p);
794 }
795 }
796
797 FOR_ALL_WINDOWS(wp)
798 {
799 if (wp->w_buffer == term->tl_buffer)
800 {
801 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
802 wp->w_cursor.col = 0;
803 wp->w_valid = 0;
804 redraw_win_later(wp, NOT_VALID);
805 }
806 }
807}
808
809 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200810set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200811{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200812 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200813 vim_free(term->tl_status_text);
814 term->tl_status_text = NULL;
815 if (term->tl_buffer == curbuf)
816 maketitle();
817}
818
819/*
820 * Called after the job if finished and Terminal mode is not active:
821 * Move the vterm contents into the scrollback buffer and free the vterm.
822 */
823 static void
824cleanup_vterm(term_T *term)
825{
826 move_terminal_to_buffer(term);
827 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200828 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200829}
830
831/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200832 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200833 * Suspends updating the terminal window.
834 */
835 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200836term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200837{
838 term_T *term = curbuf->b_term;
839
840 /* Append the current terminal contents to the buffer. */
841 move_terminal_to_buffer(term);
842
Bram Moolenaar6d819742017-08-06 14:57:49 +0200843 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200844
Bram Moolenaar6d819742017-08-06 14:57:49 +0200845 /* Move the window cursor to the position of the cursor in the
846 * terminal. */
847 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
848 + term->tl_cursor_pos.row + 1;
849 check_cursor();
850 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200851
Bram Moolenaar6d819742017-08-06 14:57:49 +0200852 /* Display the same lines as in the terminal. */
853 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854}
855
856/*
857 * Returns TRUE if the current window contains a terminal and we are in
858 * Terminal-Normal mode.
859 */
860 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200861term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862{
863 term_T *term = curbuf->b_term;
864
Bram Moolenaar6d819742017-08-06 14:57:49 +0200865 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200866}
867
868/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200869 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200870 * Restores updating the terminal window.
871 */
872 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200873term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200874{
875 term_T *term = curbuf->b_term;
876 sb_line_T *line;
877 garray_T *gap;
878
879 /* Remove the terminal contents from the scrollback and the buffer. */
880 gap = &term->tl_scrollback;
881 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
882 {
883 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
884 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
885 vim_free(line->sb_cells);
886 --gap->ga_len;
887 if (gap->ga_len == 0)
888 break;
889 }
890 check_cursor();
891
Bram Moolenaar6d819742017-08-06 14:57:49 +0200892 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893
894 if (term->tl_channel_closed)
895 cleanup_vterm(term);
896 redraw_buf_and_status_later(curbuf, NOT_VALID);
897}
898
899/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200900 * Get a key from the user without mapping.
901 * TODO: use terminal mode mappings.
902 */
903 static int
904term_vgetc()
905{
906 int c;
907
908 ++no_mapping;
909 ++allow_keys;
910 got_int = FALSE;
911 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200912 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200913 --no_mapping;
914 --allow_keys;
915 return c;
916}
917
918/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200919 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200920 * Return FAIL when the key needs to be handled in Normal mode.
921 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200922 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200923 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200924send_keys_to_term(term_T *term, int c, int typed)
925{
926 char msg[KEY_BUF_LEN];
927 size_t len;
928 static int mouse_was_outside = FALSE;
929 int dragging_outside = FALSE;
930
931 /* Catch keys that need to be handled as in Normal mode. */
932 switch (c)
933 {
934 case NUL:
935 case K_ZERO:
936 if (typed)
937 stuffcharReadbuff(c);
938 return FAIL;
939
940 case K_IGNORE:
941 return FAIL;
942
943 case K_LEFTDRAG:
944 case K_MIDDLEDRAG:
945 case K_RIGHTDRAG:
946 case K_X1DRAG:
947 case K_X2DRAG:
948 dragging_outside = mouse_was_outside;
949 /* FALLTHROUGH */
950 case K_LEFTMOUSE:
951 case K_LEFTMOUSE_NM:
952 case K_LEFTRELEASE:
953 case K_LEFTRELEASE_NM:
954 case K_MIDDLEMOUSE:
955 case K_MIDDLERELEASE:
956 case K_RIGHTMOUSE:
957 case K_RIGHTRELEASE:
958 case K_X1MOUSE:
959 case K_X1RELEASE:
960 case K_X2MOUSE:
961 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200962
963 case K_MOUSEUP:
964 case K_MOUSEDOWN:
965 case K_MOUSELEFT:
966 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200967 if (mouse_row < W_WINROW(curwin)
968 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
969 || mouse_col < W_WINCOL(curwin)
970 || mouse_col >= W_ENDCOL(curwin)
971 || dragging_outside)
972 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200973 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200974 if (typed)
975 {
976 stuffcharReadbuff(c);
977 mouse_was_outside = TRUE;
978 }
979 return FAIL;
980 }
981 }
982 if (typed)
983 mouse_was_outside = FALSE;
984
985 /* Convert the typed key to a sequence of bytes for the job. */
986 len = term_convert_key(term, c, msg);
987 if (len > 0)
988 /* TODO: if FAIL is returned, stop? */
989 channel_send(term->tl_job->jv_channel, PART_IN,
990 (char_u *)msg, (int)len, NULL);
991
992 return OK;
993}
994
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200995 static void
996position_cursor(win_T *wp, VTermPos *pos)
997{
998 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
999 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1000 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1001}
1002
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001003/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001004 * Handle CTRL-W "": send register contents to the job.
1005 */
1006 static void
1007term_paste_register(int prev_c UNUSED)
1008{
1009 int c;
1010 list_T *l;
1011 listitem_T *item;
1012 long reglen = 0;
1013 int type;
1014
1015#ifdef FEAT_CMDL_INFO
1016 if (add_to_showcmd(prev_c))
1017 if (add_to_showcmd('"'))
1018 out_flush();
1019#endif
1020 c = term_vgetc();
1021#ifdef FEAT_CMDL_INFO
1022 clear_showcmd();
1023#endif
1024
1025 /* CTRL-W "= prompt for expression to evaluate. */
1026 if (c == '=' && get_expr_register() != '=')
1027 return;
1028
1029 l = (list_T *)get_reg_contents(c, GREG_LIST);
1030 if (l != NULL)
1031 {
1032 type = get_reg_type(c, &reglen);
1033 for (item = l->lv_first; item != NULL; item = item->li_next)
1034 {
1035 char_u *s = get_tv_string(&item->li_tv);
1036
1037 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1038 s, STRLEN(s), NULL);
1039 if (item->li_next != NULL || type == MLINE)
1040 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1041 (char_u *)"\r", 1, NULL);
1042 }
1043 list_free(l);
1044 }
1045}
1046
1047/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001048 * Returns TRUE if the current window contains a terminal and we are sending
1049 * keys to the job.
1050 */
1051 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001052term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001053{
1054 term_T *term = curbuf->b_term;
1055
1056 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001057 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001058 && term->tl_vterm != NULL
1059 && term_job_running(term);
1060}
1061
1062/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001063 * Wait for input and send it to the job.
1064 * Return when the start of a CTRL-W command is typed or anything else that
1065 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001066 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1067 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001068 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001069 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001070terminal_loop(void)
1071{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001072 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001073 int termkey = 0;
1074
1075 if (*curwin->w_p_tk != NUL)
1076 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001077 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001078
1079 for (;;)
1080 {
1081 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001082 /* Repeat redrawing in case a message is received while redrawing. */
1083 while (curwin->w_redr_type != 0)
1084 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001085 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001086
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001087 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001088 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001089 /* job finished while waiting for a character */
1090 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001091
Bram Moolenaarfae42832017-08-01 22:24:26 +02001092#ifdef UNIX
1093 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1094#endif
1095#ifdef WIN3264
1096 if (c == Ctrl_C)
1097 /* We don't know if the job can handle CTRL-C itself or not, this
1098 * may kill the shell instead of killing the command running in the
1099 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001100 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001101#endif
1102
Bram Moolenaar69198192017-08-05 14:10:48 +02001103 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001104 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001105 int prev_c = c;
1106
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001107#ifdef FEAT_CMDL_INFO
1108 if (add_to_showcmd(c))
1109 out_flush();
1110#endif
1111 c = term_vgetc();
1112#ifdef FEAT_CMDL_INFO
1113 clear_showcmd();
1114#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001115 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001116 /* job finished while waiting for a character */
1117 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001118
Bram Moolenaar69198192017-08-05 14:10:48 +02001119 if (prev_c == Ctrl_BSL)
1120 {
1121 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001122 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001123 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1124 term_enter_normal_mode();
1125 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001126 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001127 /* Send both keys to the terminal. */
1128 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1129 }
1130 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001131 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001132 /* "CTRL-W .": send CTRL-W to the job */
1133 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001134 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001135 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001136 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001137 /* CTRL-W N : go to Terminal-Normal mode. */
1138 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001139 return FAIL;
1140 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001141 else if (c == '"')
1142 {
1143 term_paste_register(prev_c);
1144 continue;
1145 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001146 else if (termkey == 0 || c != termkey)
1147 {
1148 stuffcharReadbuff(Ctrl_W);
1149 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001150 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001151 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001152 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001153 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1154 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001155 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001156 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001157}
1158
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001159/*
1160 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001161 * This updates the title and status, but does not close the vter, because
1162 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001163 */
1164 void
1165term_job_ended(job_T *job)
1166{
1167 term_T *term;
1168 int did_one = FALSE;
1169
1170 for (term = first_term; term != NULL; term = term->tl_next)
1171 if (term->tl_job == job)
1172 {
1173 vim_free(term->tl_title);
1174 term->tl_title = NULL;
1175 vim_free(term->tl_status_text);
1176 term->tl_status_text = NULL;
1177 redraw_buf_and_status_later(term->tl_buffer, VALID);
1178 did_one = TRUE;
1179 }
1180 if (did_one)
1181 redraw_statuslines();
1182 if (curbuf->b_term != NULL)
1183 {
1184 if (curbuf->b_term->tl_job == job)
1185 maketitle();
1186 update_cursor(curbuf->b_term, TRUE);
1187 }
1188}
1189
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001190 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001191may_toggle_cursor(term_T *term)
1192{
1193 if (curbuf == term->tl_buffer)
1194 {
1195 if (term->tl_cursor_visible)
1196 cursor_on();
1197 else
1198 cursor_off();
1199 }
1200}
1201
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001202 static int
1203handle_damage(VTermRect rect, void *user)
1204{
1205 term_T *term = (term_T *)user;
1206
1207 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1208 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1209 redraw_buf_later(term->tl_buffer, NOT_VALID);
1210 return 1;
1211}
1212
1213 static int
1214handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1215{
1216 term_T *term = (term_T *)user;
1217
1218 /* TODO */
1219 redraw_buf_later(term->tl_buffer, NOT_VALID);
1220 return 1;
1221}
1222
1223 static int
1224handle_movecursor(
1225 VTermPos pos,
1226 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001227 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001228 void *user)
1229{
1230 term_T *term = (term_T *)user;
1231 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001232
1233 term->tl_cursor_pos = pos;
1234 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001235
1236 FOR_ALL_WINDOWS(wp)
1237 {
1238 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001239 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001240 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001241 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001242 {
1243 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001244 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001245 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001246
1247 return 1;
1248}
1249
Bram Moolenaar21554412017-07-24 21:44:43 +02001250 static int
1251handle_settermprop(
1252 VTermProp prop,
1253 VTermValue *value,
1254 void *user)
1255{
1256 term_T *term = (term_T *)user;
1257
1258 switch (prop)
1259 {
1260 case VTERM_PROP_TITLE:
1261 vim_free(term->tl_title);
1262 term->tl_title = vim_strsave((char_u *)value->string);
1263 vim_free(term->tl_status_text);
1264 term->tl_status_text = NULL;
1265 if (term == curbuf->b_term)
1266 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001267 break;
1268
1269 case VTERM_PROP_CURSORVISIBLE:
1270 term->tl_cursor_visible = value->boolean;
1271 may_toggle_cursor(term);
1272 out_flush();
1273 break;
1274
Bram Moolenaar21554412017-07-24 21:44:43 +02001275 default:
1276 break;
1277 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001278 /* Always return 1, otherwise vterm doesn't store the value internally. */
1279 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001280}
1281
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001282/*
1283 * The job running in the terminal resized the terminal.
1284 */
1285 static int
1286handle_resize(int rows, int cols, void *user)
1287{
1288 term_T *term = (term_T *)user;
1289 win_T *wp;
1290
1291 term->tl_rows = rows;
1292 term->tl_cols = cols;
1293 FOR_ALL_WINDOWS(wp)
1294 {
1295 if (wp->w_buffer == term->tl_buffer)
1296 {
1297 win_setheight_win(rows, wp);
1298 win_setwidth_win(cols, wp);
1299 }
1300 }
1301
1302 redraw_buf_later(term->tl_buffer, NOT_VALID);
1303 return 1;
1304}
1305
Bram Moolenaard85f2712017-07-28 21:51:57 +02001306/*
1307 * Handle a line that is pushed off the top of the screen.
1308 */
1309 static int
1310handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1311{
1312 term_T *term = (term_T *)user;
1313
1314 /* TODO: Limit the number of lines that are stored. */
1315 /* TODO: put the text in the buffer. */
1316 if (ga_grow(&term->tl_scrollback, 1) == OK)
1317 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001318 VTermScreenCell *p = NULL;
1319 int len = 0;
1320 int i;
1321 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001322
1323 /* do not store empty cells at the end */
1324 for (i = 0; i < cols; ++i)
1325 if (cells[i].chars[0] != 0)
1326 len = i + 1;
1327
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001328 if (len > 0)
1329 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001330 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001331 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001332
1333 line = (sb_line_T *)term->tl_scrollback.ga_data
1334 + term->tl_scrollback.ga_len;
1335 line->sb_cols = len;
1336 line->sb_cells = p;
1337 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001338 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001339
1340 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001341 }
1342 return 0; /* ignored */
1343}
1344
Bram Moolenaar21554412017-07-24 21:44:43 +02001345static VTermScreenCallbacks screen_callbacks = {
1346 handle_damage, /* damage */
1347 handle_moverect, /* moverect */
1348 handle_movecursor, /* movecursor */
1349 handle_settermprop, /* settermprop */
1350 NULL, /* bell */
1351 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001352 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001353 NULL /* sb_popline */
1354};
1355
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001357 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001358 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001359 */
1360 void
1361term_channel_closed(channel_T *ch)
1362{
1363 term_T *term;
1364 int did_one = FALSE;
1365
1366 for (term = first_term; term != NULL; term = term->tl_next)
1367 if (term->tl_job == ch->ch_job)
1368 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001369 term->tl_channel_closed = TRUE;
1370
Bram Moolenaard85f2712017-07-28 21:51:57 +02001371 vim_free(term->tl_title);
1372 term->tl_title = NULL;
1373 vim_free(term->tl_status_text);
1374 term->tl_status_text = NULL;
1375
Bram Moolenaar423802d2017-07-30 16:52:24 +02001376 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001377 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001378 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001379
1380 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1381 did_one = TRUE;
1382 }
1383 if (did_one)
1384 {
1385 redraw_statuslines();
1386
1387 /* Need to break out of vgetc(). */
1388 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001389 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001390
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001391 term = curbuf->b_term;
1392 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001393 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001394 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001395 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001396 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001397 }
1398 }
1399}
1400
1401/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001402 * Reverse engineer the RGB value into a cterm color index.
1403 * First color is 1. Return 0 if no match found.
1404 */
1405 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001406color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001407{
1408 int red = color->red;
1409 int blue = color->blue;
1410 int green = color->green;
1411
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001412 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001413 if (red == 0)
1414 {
1415 if (green == 0)
1416 {
1417 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001418 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001419 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001420 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001421 }
1422 else if (green == 224)
1423 {
1424 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001425 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001426 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001427 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001428 }
1429 }
1430 else if (red == 224)
1431 {
1432 if (green == 0)
1433 {
1434 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001435 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001436 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001437 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001438 }
1439 else if (green == 224)
1440 {
1441 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001442 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001443 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001444 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001445 }
1446 }
1447 else if (red == 128)
1448 {
1449 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001450 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001451 }
1452 else if (red == 255)
1453 {
1454 if (green == 64)
1455 {
1456 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001457 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001458 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001459 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001460 }
1461 else if (green == 255)
1462 {
1463 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001464 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001465 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001466 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001467 }
1468 }
1469 else if (red == 64)
1470 {
1471 if (green == 64)
1472 {
1473 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001474 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001475 }
1476 else if (green == 255)
1477 {
1478 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001479 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001480 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001481 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001482 }
1483 }
1484 if (t_colors >= 256)
1485 {
1486 if (red == blue && red == green)
1487 {
1488 /* 24-color greyscale */
1489 static int cutoff[23] = {
1490 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1491 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1492 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1493 int i;
1494
1495 for (i = 0; i < 23; ++i)
1496 if (red < cutoff[i])
1497 return i + 233;
1498 return 256;
1499 }
1500
1501 /* 216-color cube */
1502 return 17 + ((red + 25) / 0x33) * 36
1503 + ((green + 25) / 0x33) * 6
1504 + (blue + 25) / 0x33;
1505 }
1506 return 0;
1507}
1508
1509/*
1510 * Convert the attributes of a vterm cell into an attribute index.
1511 */
1512 static int
1513cell2attr(VTermScreenCell *cell)
1514{
1515 int attr = 0;
1516
1517 if (cell->attrs.bold)
1518 attr |= HL_BOLD;
1519 if (cell->attrs.underline)
1520 attr |= HL_UNDERLINE;
1521 if (cell->attrs.italic)
1522 attr |= HL_ITALIC;
1523 if (cell->attrs.strike)
1524 attr |= HL_STANDOUT;
1525 if (cell->attrs.reverse)
1526 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001527
1528#ifdef FEAT_GUI
1529 if (gui.in_use)
1530 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001531 guicolor_T fg, bg;
1532
1533 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1534 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1535 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001536 }
1537 else
1538#endif
1539#ifdef FEAT_TERMGUICOLORS
1540 if (p_tgc)
1541 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001542 guicolor_T fg, bg;
1543
1544 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1545 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1546
1547 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001548 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001549 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001550#endif
1551 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001552 int bold = MAYBE;
1553 int fg = color2index(&cell->fg, TRUE, &bold);
1554 int bg = color2index(&cell->bg, FALSE, &bold);
1555
1556 /* with 8 colors set the bold attribute to get a bright foreground */
1557 if (bold == TRUE)
1558 attr |= HL_BOLD;
1559 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001560 }
1561 return 0;
1562}
1563
1564/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001565 * Called to update a window that contains an active terminal.
1566 * Returns FAIL when there is no terminal running in this window or in
1567 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001568 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001569 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001570term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001571{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001572 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001573 VTerm *vterm;
1574 VTermScreen *screen;
1575 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001576 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001577
Bram Moolenaar6d819742017-08-06 14:57:49 +02001578 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001579 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001580
Bram Moolenaard85f2712017-07-28 21:51:57 +02001581 vterm = term->tl_vterm;
1582 screen = vterm_obtain_screen(vterm);
1583 state = vterm_obtain_state(vterm);
1584
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001585 /*
1586 * If the window was resized a redraw will be triggered and we get here.
1587 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1588 */
1589 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1590 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001591 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001592 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1593 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1594 win_T *twp;
1595
1596 FOR_ALL_WINDOWS(twp)
1597 {
1598 /* When more than one window shows the same terminal, use the
1599 * smallest size. */
1600 if (twp->w_buffer == term->tl_buffer)
1601 {
1602 if (!term->tl_rows_fixed && rows > twp->w_height)
1603 rows = twp->w_height;
1604 if (!term->tl_cols_fixed && cols > twp->w_width)
1605 cols = twp->w_width;
1606 }
1607 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001608
1609 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001610 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001611 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001612 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001613 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001614
1615 /* The cursor may have been moved when resizing. */
1616 vterm_state_get_cursorpos(state, &pos);
1617 position_cursor(wp, &pos);
1618
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001619 /* TODO: Only redraw what changed. */
1620 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001621 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001622 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001623 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001624
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001625 if (pos.row < term->tl_rows)
1626 {
1627 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001628 {
1629 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001630 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001631
Bram Moolenaareeac6772017-07-23 15:48:37 +02001632 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1633 vim_memset(&cell, 0, sizeof(cell));
1634
1635 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001636 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001637 if (c == NUL)
1638 {
1639 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001640#if defined(FEAT_MBYTE)
1641 if (enc_utf8)
1642 ScreenLinesUC[off] = NUL;
1643#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001644 }
1645 else
1646 {
1647#if defined(FEAT_MBYTE)
1648 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001649 {
1650 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001651 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001652 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001653 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001654 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001655 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001656 if (enc_utf8)
1657 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001658 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001659#else
1660 ScreenLines[off] = c;
1661#endif
1662 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001663 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001664
1665 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001666 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667 if (cell.width == 2)
1668 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001669 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001670#if defined(FEAT_MBYTE)
1671 if (enc_utf8)
1672 ScreenLinesUC[off] = NUL;
1673#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001674 ++pos.col;
1675 ++off;
1676 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001677 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001678 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001679 else
1680 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001681
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001682 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1683 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001684 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001685
1686 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001687}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001688
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001689/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001690 * Return TRUE if "wp" is a terminal window where the job has finished.
1691 */
1692 int
1693term_is_finished(buf_T *buf)
1694{
1695 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1696}
1697
1698/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001699 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001700 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001701 */
1702 int
1703term_show_buffer(buf_T *buf)
1704{
1705 term_T *term = buf->b_term;
1706
Bram Moolenaar6d819742017-08-06 14:57:49 +02001707 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001708}
1709
1710/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001711 * The current buffer is going to be changed. If there is terminal
1712 * highlighting remove it now.
1713 */
1714 void
1715term_change_in_curbuf(void)
1716{
1717 term_T *term = curbuf->b_term;
1718
1719 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1720 {
1721 free_scrollback(term);
1722 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001723
1724 /* The buffer is now like a normal buffer, it cannot be easily
1725 * abandoned when changed. */
1726 set_string_option_direct((char_u *)"buftype", -1,
1727 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001728 }
1729}
1730
1731/*
1732 * Get the screen attribute for a position in the buffer.
1733 */
1734 int
1735term_get_attr(buf_T *buf, linenr_T lnum, int col)
1736{
1737 term_T *term = buf->b_term;
1738 sb_line_T *line;
1739
Bram Moolenaar70229f92017-07-29 16:01:53 +02001740 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001741 return 0;
1742 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1743 if (col >= line->sb_cols)
1744 return 0;
1745 return cell2attr(line->sb_cells + col);
1746}
1747
1748/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001749 * Create a new vterm and initialize it.
1750 */
1751 static void
1752create_vterm(term_T *term, int rows, int cols)
1753{
1754 VTerm *vterm;
1755 VTermScreen *screen;
1756
1757 vterm = vterm_new(rows, cols);
1758 term->tl_vterm = vterm;
1759 screen = vterm_obtain_screen(vterm);
1760 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1761 /* TODO: depends on 'encoding'. */
1762 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001763
1764 /* Vterm uses a default black background. Set it to white when
1765 * 'background' is "light". */
1766 if (*p_bg == 'l')
1767 {
1768 VTermColor fg, bg;
1769
1770 fg.red = fg.green = fg.blue = 0;
1771 bg.red = bg.green = bg.blue = 255;
1772 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1773 }
1774
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001775 /* Required to initialize most things. */
1776 vterm_screen_reset(screen, 1 /* hard */);
1777}
1778
Bram Moolenaar21554412017-07-24 21:44:43 +02001779/*
1780 * Return the text to show for the buffer name and status.
1781 */
1782 char_u *
1783term_get_status_text(term_T *term)
1784{
1785 if (term->tl_status_text == NULL)
1786 {
1787 char_u *txt;
1788 size_t len;
1789
Bram Moolenaar6d819742017-08-06 14:57:49 +02001790 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001791 {
1792 if (term_job_running(term))
1793 txt = (char_u *)_("Terminal");
1794 else
1795 txt = (char_u *)_("Terminal-finished");
1796 }
1797 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001798 txt = term->tl_title;
1799 else if (term_job_running(term))
1800 txt = (char_u *)_("running");
1801 else
1802 txt = (char_u *)_("finished");
1803 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001804 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001805 if (term->tl_status_text != NULL)
1806 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1807 term->tl_buffer->b_fname, txt);
1808 }
1809 return term->tl_status_text;
1810}
1811
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001812/*
1813 * Mark references in jobs of terminals.
1814 */
1815 int
1816set_ref_in_term(int copyID)
1817{
1818 int abort = FALSE;
1819 term_T *term;
1820 typval_T tv;
1821
1822 for (term = first_term; term != NULL; term = term->tl_next)
1823 if (term->tl_job != NULL)
1824 {
1825 tv.v_type = VAR_JOB;
1826 tv.vval.v_job = term->tl_job;
1827 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1828 }
1829 return abort;
1830}
1831
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001832/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001833 * Get the buffer from the first argument in "argvars".
1834 * Returns NULL when the buffer is not for a terminal window.
1835 */
1836 static buf_T *
1837term_get_buf(typval_T *argvars)
1838{
1839 buf_T *buf;
1840
1841 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1842 ++emsg_off;
1843 buf = get_buf_tv(&argvars[0], FALSE);
1844 --emsg_off;
1845 if (buf == NULL || buf->b_term == NULL)
1846 return NULL;
1847 return buf;
1848}
1849
1850/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001851 * "term_getattr(attr, name)" function
1852 */
1853 void
1854f_term_getattr(typval_T *argvars, typval_T *rettv)
1855{
1856 int attr;
1857 size_t i;
1858 char_u *name;
1859
1860 static struct {
1861 char *name;
1862 int attr;
1863 } attrs[] = {
1864 {"bold", HL_BOLD},
1865 {"italic", HL_ITALIC},
1866 {"underline", HL_UNDERLINE},
1867 {"strike", HL_STANDOUT},
1868 {"reverse", HL_INVERSE},
1869 };
1870
1871 attr = get_tv_number(&argvars[0]);
1872 name = get_tv_string_chk(&argvars[1]);
1873 if (name == NULL)
1874 return;
1875
1876 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1877 if (STRCMP(name, attrs[i].name) == 0)
1878 {
1879 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1880 break;
1881 }
1882}
1883
1884/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001885 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001886 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001887 void
1888f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001889{
Bram Moolenaar97870002017-07-30 18:28:38 +02001890 buf_T *buf = term_get_buf(argvars);
1891 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001892
Bram Moolenaar97870002017-07-30 18:28:38 +02001893 if (rettv_list_alloc(rettv) == FAIL)
1894 return;
1895 if (buf == NULL)
1896 return;
1897
1898 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001899 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1900 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001901 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001902}
1903
1904/*
1905 * "term_getjob(buf)" function
1906 */
1907 void
1908f_term_getjob(typval_T *argvars, typval_T *rettv)
1909{
1910 buf_T *buf = term_get_buf(argvars);
1911
1912 rettv->v_type = VAR_JOB;
1913 rettv->vval.v_job = NULL;
1914 if (buf == NULL)
1915 return;
1916
1917 rettv->vval.v_job = buf->b_term->tl_job;
1918 if (rettv->vval.v_job != NULL)
1919 ++rettv->vval.v_job->jv_refcount;
1920}
1921
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001922 static int
1923get_row_number(typval_T *tv, term_T *term)
1924{
1925 if (tv->v_type == VAR_STRING
1926 && tv->vval.v_string != NULL
1927 && STRCMP(tv->vval.v_string, ".") == 0)
1928 return term->tl_cursor_pos.row;
1929 return (int)get_tv_number(tv) - 1;
1930}
1931
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001932/*
1933 * "term_getline(buf, row)" function
1934 */
1935 void
1936f_term_getline(typval_T *argvars, typval_T *rettv)
1937{
1938 buf_T *buf = term_get_buf(argvars);
1939 term_T *term;
1940 int row;
1941
1942 rettv->v_type = VAR_STRING;
1943 if (buf == NULL)
1944 return;
1945 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001946 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001947
1948 if (term->tl_vterm == NULL)
1949 {
1950 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1951
1952 /* vterm is finished, get the text from the buffer */
1953 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1954 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1955 }
1956 else
1957 {
1958 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1959 VTermRect rect;
1960 int len;
1961 char_u *p;
1962
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001963 if (row < 0 || row >= term->tl_rows)
1964 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001965 len = term->tl_cols * MB_MAXBYTES + 1;
1966 p = alloc(len);
1967 if (p == NULL)
1968 return;
1969 rettv->vval.v_string = p;
1970
1971 rect.start_col = 0;
1972 rect.end_col = term->tl_cols;
1973 rect.start_row = row;
1974 rect.end_row = row + 1;
1975 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1976 }
1977}
1978
1979/*
1980 * "term_getsize(buf)" function
1981 */
1982 void
1983f_term_getsize(typval_T *argvars, typval_T *rettv)
1984{
1985 buf_T *buf = term_get_buf(argvars);
1986 list_T *l;
1987
1988 if (rettv_list_alloc(rettv) == FAIL)
1989 return;
1990 if (buf == NULL)
1991 return;
1992
1993 l = rettv->vval.v_list;
1994 list_append_number(l, buf->b_term->tl_rows);
1995 list_append_number(l, buf->b_term->tl_cols);
1996}
1997
1998/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02001999 * "term_getstatus(buf)" function
2000 */
2001 void
2002f_term_getstatus(typval_T *argvars, typval_T *rettv)
2003{
2004 buf_T *buf = term_get_buf(argvars);
2005 term_T *term;
2006 char_u val[100];
2007
2008 rettv->v_type = VAR_STRING;
2009 if (buf == NULL)
2010 return;
2011 term = buf->b_term;
2012
2013 if (term_job_running(term))
2014 STRCPY(val, "running");
2015 else
2016 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002017 if (term->tl_normal_mode)
2018 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002019 rettv->vval.v_string = vim_strsave(val);
2020}
2021
2022/*
2023 * "term_gettitle(buf)" function
2024 */
2025 void
2026f_term_gettitle(typval_T *argvars, typval_T *rettv)
2027{
2028 buf_T *buf = term_get_buf(argvars);
2029
2030 rettv->v_type = VAR_STRING;
2031 if (buf == NULL)
2032 return;
2033
2034 if (buf->b_term->tl_title != NULL)
2035 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2036}
2037
2038/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002039 * "term_gettty(buf)" function
2040 */
2041 void
2042f_term_gettty(typval_T *argvars, typval_T *rettv)
2043{
2044 buf_T *buf = term_get_buf(argvars);
2045 char_u *p;
2046
2047 rettv->v_type = VAR_STRING;
2048 if (buf == NULL)
2049 return;
2050 if (buf->b_term->tl_job != NULL)
2051 p = buf->b_term->tl_job->jv_tty_name;
2052 else
2053 p = buf->b_term->tl_tty_name;
2054 if (p != NULL)
2055 rettv->vval.v_string = vim_strsave(p);
2056}
2057
2058/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002059 * "term_list()" function
2060 */
2061 void
2062f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2063{
2064 term_T *tp;
2065 list_T *l;
2066
2067 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2068 return;
2069
2070 l = rettv->vval.v_list;
2071 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2072 if (tp != NULL && tp->tl_buffer != NULL)
2073 if (list_append_number(l,
2074 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2075 return;
2076}
2077
2078/*
2079 * "term_scrape(buf, row)" function
2080 */
2081 void
2082f_term_scrape(typval_T *argvars, typval_T *rettv)
2083{
2084 buf_T *buf = term_get_buf(argvars);
2085 VTermScreen *screen = NULL;
2086 VTermPos pos;
2087 list_T *l;
2088 term_T *term;
2089
2090 if (rettv_list_alloc(rettv) == FAIL)
2091 return;
2092 if (buf == NULL)
2093 return;
2094 term = buf->b_term;
2095 if (term->tl_vterm != NULL)
2096 screen = vterm_obtain_screen(term->tl_vterm);
2097
2098 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002099 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002100 for (pos.col = 0; pos.col < term->tl_cols; )
2101 {
2102 dict_T *dcell;
2103 VTermScreenCell cell;
2104 char_u rgb[8];
2105 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2106 int off = 0;
2107 int i;
2108
2109 if (screen == NULL)
2110 {
2111 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2112 sb_line_T *line;
2113
2114 /* vterm has finished, get the cell from scrollback */
2115 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2116 break;
2117 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2118 if (pos.col >= line->sb_cols)
2119 break;
2120 cell = line->sb_cells[pos.col];
2121 }
2122 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2123 break;
2124 dcell = dict_alloc();
2125 list_append_dict(l, dcell);
2126
2127 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2128 {
2129 if (cell.chars[i] == 0)
2130 break;
2131 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2132 }
2133 mbs[off] = NUL;
2134 dict_add_nr_str(dcell, "chars", 0, mbs);
2135
2136 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2137 cell.fg.red, cell.fg.green, cell.fg.blue);
2138 dict_add_nr_str(dcell, "fg", 0, rgb);
2139 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2140 cell.bg.red, cell.bg.green, cell.bg.blue);
2141 dict_add_nr_str(dcell, "bg", 0, rgb);
2142
2143 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2144 dict_add_nr_str(dcell, "width", cell.width, NULL);
2145
2146 ++pos.col;
2147 if (cell.width == 2)
2148 ++pos.col;
2149 }
2150}
2151
2152/*
2153 * "term_sendkeys(buf, keys)" function
2154 */
2155 void
2156f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2157{
2158 buf_T *buf = term_get_buf(argvars);
2159 char_u *msg;
2160 term_T *term;
2161
2162 rettv->v_type = VAR_UNKNOWN;
2163 if (buf == NULL)
2164 return;
2165
2166 msg = get_tv_string_chk(&argvars[1]);
2167 if (msg == NULL)
2168 return;
2169 term = buf->b_term;
2170 if (term->tl_vterm == NULL)
2171 return;
2172
2173 while (*msg != NUL)
2174 {
2175 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2176 msg += MB_PTR2LEN(msg);
2177 }
2178
Bram Moolenaar6d819742017-08-06 14:57:49 +02002179 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002180 {
2181 /* TODO: only update once in a while. */
2182 update_screen(0);
2183 if (buf == curbuf)
2184 update_cursor(term, TRUE);
2185 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002186}
2187
2188/*
2189 * "term_start(command, options)" function
2190 */
2191 void
2192f_term_start(typval_T *argvars, typval_T *rettv)
2193{
2194 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002195 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002196
2197 if (cmd == NULL)
2198 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002199 init_job_options(&opt);
2200 /* TODO: allow more job options */
2201 if (argvars[1].v_type != VAR_UNKNOWN
2202 && get_job_options(&argvars[1], &opt,
2203 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002204 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2205 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002206 return;
2207
2208 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002209
2210 if (curbuf->b_term != NULL)
2211 rettv->vval.v_number = curbuf->b_fnum;
2212}
2213
2214/*
2215 * "term_wait" function
2216 */
2217 void
2218f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2219{
2220 buf_T *buf = term_get_buf(argvars);
2221
2222 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002223 {
2224 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002225 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002226 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002227 if (buf->b_term->tl_job == NULL)
2228 {
2229 ch_log(NULL, "term_wait(): no job to wait for");
2230 return;
2231 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002232
2233 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002234 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002235 {
2236 /* The job is dead, keep reading channel I/O until the channel is
2237 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002238 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002239 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2240 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002241 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002242 parse_queued_messages();
2243 ui_delay(10L, FALSE);
2244 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002245 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002246 parse_queued_messages();
2247 }
2248 else
2249 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002250 long wait = 10L;
2251
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002252 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002253 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002254
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002255 /* Wait for some time for any channel I/O. */
2256 if (argvars[1].v_type != VAR_UNKNOWN)
2257 wait = get_tv_number(&argvars[1]);
2258 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002259 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002260
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002261 /* Flushing messages on channels is hopefully sufficient.
2262 * TODO: is there a better way? */
2263 parse_queued_messages();
2264 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002265}
2266
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002267# ifdef WIN3264
2268
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002269/**************************************
2270 * 2. MS-Windows implementation.
2271 */
2272
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002273#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2274#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2275
Bram Moolenaar8a773062017-07-24 22:29:21 +02002276void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002277void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002278void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002279BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2280void (*winpty_config_set_initial_size)(void*, int, int);
2281LPCWSTR (*winpty_conin_name)(void*);
2282LPCWSTR (*winpty_conout_name)(void*);
2283LPCWSTR (*winpty_conerr_name)(void*);
2284void (*winpty_free)(void*);
2285void (*winpty_config_free)(void*);
2286void (*winpty_spawn_config_free)(void*);
2287void (*winpty_error_free)(void*);
2288LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002289BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002290HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002291
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002292#define WINPTY_DLL "winpty.dll"
2293
2294static HINSTANCE hWinPtyDLL = NULL;
2295
2296 int
2297dyn_winpty_init(void)
2298{
2299 int i;
2300 static struct
2301 {
2302 char *name;
2303 FARPROC *ptr;
2304 } winpty_entry[] =
2305 {
2306 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2307 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2308 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2309 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2310 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2311 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2312 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2313 {"winpty_free", (FARPROC*)&winpty_free},
2314 {"winpty_open", (FARPROC*)&winpty_open},
2315 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2316 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2317 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2318 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002319 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002320 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002321 {NULL, NULL}
2322 };
2323
2324 /* No need to initialize twice. */
2325 if (hWinPtyDLL)
2326 return 1;
2327 /* Load winpty.dll */
2328 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2329 if (!hWinPtyDLL)
2330 {
2331 EMSG2(_(e_loadlib), WINPTY_DLL);
2332 return 0;
2333 }
2334 for (i = 0; winpty_entry[i].name != NULL
2335 && winpty_entry[i].ptr != NULL; ++i)
2336 {
2337 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2338 winpty_entry[i].name)) == NULL)
2339 {
2340 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2341 return 0;
2342 }
2343 }
2344
2345 return 1;
2346}
2347
2348/*
2349 * Create a new terminal of "rows" by "cols" cells.
2350 * Store a reference in "term".
2351 * Return OK or FAIL.
2352 */
2353 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002354term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002356 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002357 channel_T *channel = NULL;
2358 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002359 DWORD error;
2360 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2361 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002362 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002363 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002364
2365 if (!dyn_winpty_init())
2366 return FAIL;
2367
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002368 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002369 if (p == NULL)
2370 return FAIL;
2371
2372 job = job_alloc();
2373 if (job == NULL)
2374 goto failed;
2375
2376 channel = add_channel();
2377 if (channel == NULL)
2378 goto failed;
2379
2380 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2381 if (term->tl_winpty_config == NULL)
2382 goto failed;
2383
2384 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2385 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2386 if (term->tl_winpty == NULL)
2387 goto failed;
2388
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002389 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002390 spawn_config = winpty_spawn_config_new(
2391 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2392 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2393 NULL,
2394 p,
2395 NULL,
2396 NULL,
2397 &winpty_err);
2398 if (spawn_config == NULL)
2399 goto failed;
2400
2401 channel = add_channel();
2402 if (channel == NULL)
2403 goto failed;
2404
2405 job = job_alloc();
2406 if (job == NULL)
2407 goto failed;
2408
2409 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2410 &child_thread_handle, &error, &winpty_err))
2411 goto failed;
2412
2413 channel_set_pipes(channel,
2414 (sock_T) CreateFileW(
2415 winpty_conin_name(term->tl_winpty),
2416 GENERIC_WRITE, 0, NULL,
2417 OPEN_EXISTING, 0, NULL),
2418 (sock_T) CreateFileW(
2419 winpty_conout_name(term->tl_winpty),
2420 GENERIC_READ, 0, NULL,
2421 OPEN_EXISTING, 0, NULL),
2422 (sock_T) CreateFileW(
2423 winpty_conerr_name(term->tl_winpty),
2424 GENERIC_READ, 0, NULL,
2425 OPEN_EXISTING, 0, NULL));
2426
2427 jo = CreateJobObject(NULL, NULL);
2428 if (jo == NULL)
2429 goto failed;
2430
2431 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002432 {
2433 /* Failed, switch the way to terminate process with TerminateProcess. */
2434 CloseHandle(jo);
2435 jo = NULL;
2436 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002437
2438 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002439 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002440
2441 create_vterm(term, rows, cols);
2442
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002443 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002444 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002445
2446 job->jv_channel = channel;
2447 job->jv_proc_info.hProcess = child_process_handle;
2448 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2449 job->jv_job_object = jo;
2450 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002451 sprintf(buf, "winpty://%lu",
2452 GetProcessId(winpty_agent_process(term->tl_winpty)));
2453 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002454 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002455 term->tl_job = job;
2456
2457 return OK;
2458
2459failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002460 if (spawn_config != NULL)
2461 winpty_spawn_config_free(spawn_config);
2462 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002463 if (channel != NULL)
2464 channel_clear(channel);
2465 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002466 {
2467 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002468 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002469 }
2470 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002471 if (jo != NULL)
2472 CloseHandle(jo);
2473 if (term->tl_winpty != NULL)
2474 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002475 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002476 if (term->tl_winpty_config != NULL)
2477 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002478 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002479 if (winpty_err != NULL)
2480 {
2481 char_u *msg = utf16_to_enc(
2482 (short_u *)winpty_error_msg(winpty_err), NULL);
2483
2484 EMSG(msg);
2485 winpty_error_free(winpty_err);
2486 }
2487 return FAIL;
2488}
2489
2490/*
2491 * Free the terminal emulator part of "term".
2492 */
2493 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002494term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002495{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002496 if (term->tl_winpty != NULL)
2497 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002498 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002499 if (term->tl_winpty_config != NULL)
2500 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002501 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002502 if (term->tl_vterm != NULL)
2503 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002504 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002505}
2506
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002507/*
2508 * Request size to terminal.
2509 */
2510 static void
2511term_report_winsize(term_T *term, int rows, int cols)
2512{
2513 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2514}
2515
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002516# else
2517
2518/**************************************
2519 * 3. Unix-like implementation.
2520 */
2521
2522/*
2523 * Create a new terminal of "rows" by "cols" cells.
2524 * Start job for "cmd".
2525 * Store the pointers in "term".
2526 * Return OK or FAIL.
2527 */
2528 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002529term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530{
2531 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002532
2533 create_vterm(term, rows, cols);
2534
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002535 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002536 argvars[0].v_type = VAR_STRING;
2537 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002538
2539 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002540 if (term->tl_job != NULL)
2541 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002542
Bram Moolenaar61a66052017-07-22 18:39:00 +02002543 return term->tl_job != NULL
2544 && term->tl_job->jv_channel != NULL
2545 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002546}
2547
2548/*
2549 * Free the terminal emulator part of "term".
2550 */
2551 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002552term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002553{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002554 if (term->tl_vterm != NULL)
2555 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002556 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002557}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002558
2559/*
2560 * Request size to terminal.
2561 */
2562 static void
2563term_report_winsize(term_T *term, int rows, int cols)
2564{
2565 /* Use an ioctl() to report the new window size to the job. */
2566 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2567 {
2568 int fd = -1;
2569 int part;
2570
2571 for (part = PART_OUT; part < PART_COUNT; ++part)
2572 {
2573 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2574 if (isatty(fd))
2575 break;
2576 }
2577 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2578 mch_stop_job(term->tl_job, (char_u *)"winch");
2579 }
2580}
2581
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002582# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002583
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002584#endif /* FEAT_TERMINAL */