blob: d300b1f648562e44eb6fec6116f18a952174ae22 [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 Moolenaar6d819742017-08-06 14:57:49 +020039 * - Add argument to term_wait() for waiting time.
Bram Moolenaard85f2712017-07-28 21:51:57 +020040 * - For the scrollback buffer store lines in the buffer, only attributes in
41 * tl_scrollback.
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020042 * - When the job ends:
Bram Moolenaar21554412017-07-24 21:44:43 +020043 * - Need an option or argument to drop the window+buffer right away, to be
Bram Moolenaar423802d2017-07-30 16:52:24 +020044 * used for a shell or Vim. 'termfinish'; "close", "open" (open window when
45 * job finishes).
46 * - add option values to the command:
47 * :term <24x80> <close> vim notes.txt
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020048 * - support different cursor shapes, colors and attributes
Bram Moolenaar6d819742017-08-06 14:57:49 +020049 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaar12d93ee2017-07-30 19:02:02 +020050 * - make term_getcursor() return type (none/block/bar/underline) and
51 * attributes (color, blink, etc.)
Bram Moolenaard85f2712017-07-28 21:51:57 +020052 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020053 * For the GUI fill termios with default values, perhaps like pangoterm:
54 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
55 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020056 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020057 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020058 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020059 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020060 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020061 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
62 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020063 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020066 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020067 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020068 * terminal. Allow:
69 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
70 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
71 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
72 * Check that something is connected to the terminal.
73 * Test: "cat" reading from a file or buffer
74 * "ls" writing stdout to a file or buffer
75 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020076 * - support ":term NONE" to open a terminal with a pty but not running a job
77 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020078 * - if the job in the terminal does not support the mouse, we can use the
79 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
81 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020082 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020083 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020084 * - Copy text in the vterm to the Vim buffer once in a while, so that
85 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086 */
87
88#include "vim.h"
89
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020090#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020091
Bram Moolenaard5310982017-08-05 15:16:32 +020092#ifndef MIN
93# define MIN(x,y) ((x) < (y) ? (x) : (y))
94#endif
95#ifndef MAX
96# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020097#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020098
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020099#include "libvterm/include/vterm.h"
100
Bram Moolenaard85f2712017-07-28 21:51:57 +0200101typedef struct sb_line_S {
102 int sb_cols; /* can differ per line */
103 VTermScreenCell *sb_cells; /* allocated */
104} sb_line_T;
105
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200106/* typedef term_T in structs.h */
107struct terminal_S {
108 term_T *tl_next;
109
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 VTerm *tl_vterm;
111 job_T *tl_job;
112 buf_T *tl_buffer;
113
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200114 /* used when tl_job is NULL and only a pty was created */
115 int tl_tty_fd;
116 char_u *tl_tty_name;
117
Bram Moolenaar6d819742017-08-06 14:57:49 +0200118 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200119 int tl_channel_closed;
120
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200122 void *tl_winpty_config;
123 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200124#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 /* last known vterm size */
127 int tl_rows;
128 int tl_cols;
129 /* vterm size does not follow window size */
130 int tl_rows_fixed;
131 int tl_cols_fixed;
132
Bram Moolenaar21554412017-07-24 21:44:43 +0200133 char_u *tl_title; /* NULL or allocated */
134 char_u *tl_status_text; /* NULL or allocated */
135
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200136 /* Range of screen rows to update. Zero based. */
137 int tl_dirty_row_start; /* -1 if nothing dirty */
138 int tl_dirty_row_end; /* row below last one to update */
139
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200143 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200144 int tl_cursor_visible;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145};
146
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200147#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
148#define TMODE_LOOP 2 /* CTRL-W N used */
149
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150/*
151 * List of all active terminals.
152 */
153static term_T *first_term = NULL;
154
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200162static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200163static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200164static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200165
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200166/**************************************
167 * 1. Generic code for all systems.
168 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200169
170/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171 * Determine the terminal size from 'termsize' and the current window.
172 * Assumes term->tl_rows and term->tl_cols are zero.
173 */
174 static void
175set_term_and_win_size(term_T *term)
176{
177 if (*curwin->w_p_tms != NUL)
178 {
179 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
180
181 term->tl_rows = atoi((char *)curwin->w_p_tms);
182 term->tl_cols = atoi((char *)p);
183 }
184 if (term->tl_rows == 0)
185 term->tl_rows = curwin->w_height;
186 else
187 {
188 win_setheight_win(term->tl_rows, curwin);
189 term->tl_rows_fixed = TRUE;
190 }
191 if (term->tl_cols == 0)
192 term->tl_cols = curwin->w_width;
193 else
194 {
195 win_setwidth_win(term->tl_cols, curwin);
196 term->tl_cols_fixed = TRUE;
197 }
198}
199
200/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200201 * Initialize job options for a terminal job.
202 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200203 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 static void
205init_job_options(jobopt_T *opt)
206{
207 clear_job_options(opt);
208
209 opt->jo_mode = MODE_RAW;
210 opt->jo_out_mode = MODE_RAW;
211 opt->jo_err_mode = MODE_RAW;
212 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
213
214 opt->jo_io[PART_OUT] = JIO_BUFFER;
215 opt->jo_io[PART_ERR] = JIO_BUFFER;
216 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
217
218 opt->jo_modifiable[PART_OUT] = 0;
219 opt->jo_modifiable[PART_ERR] = 0;
220 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
221
222 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
223}
224
225/*
226 * Set job options mandatory for a terminal job.
227 */
228 static void
229setup_job_options(jobopt_T *opt, int rows, int cols)
230{
231 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
232 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
233 opt->jo_pty = TRUE;
234 opt->jo_term_rows = rows;
235 opt->jo_term_cols = cols;
236}
237
238 static void
239term_start(char_u *cmd, jobopt_T *opt)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200240{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200241 exarg_T split_ea;
242 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200243 term_T *term;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200244
245 if (check_restricted() || check_secure())
246 return;
247
248 term = (term_T *)alloc_clear(sizeof(term_T));
249 if (term == NULL)
250 return;
251 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200252 term->tl_cursor_visible = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200253 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 /* Open a new window or tab. */
256 vim_memset(&split_ea, 0, sizeof(split_ea));
257 split_ea.cmdidx = CMD_new;
258 split_ea.cmd = (char_u *)"new";
259 split_ea.arg = (char_u *)"";
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200260 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
261 {
262 split_ea.line2 = opt->jo_term_rows;
263 split_ea.addr_count = 1;
264 }
265 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
266 {
267 split_ea.line2 = opt->jo_term_cols;
268 split_ea.addr_count = 1;
269 }
270
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200271 ex_splitview(&split_ea);
272 if (curwin == old_curwin)
273 {
274 /* split failed */
275 vim_free(term);
276 return;
277 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200278 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200279 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200280
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200281 /* only one size was taken care of with :new, do the other one */
282 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
283 win_setheight(opt->jo_term_rows);
284 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
285 win_setwidth(opt->jo_term_cols);
286
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200287 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200288 term->tl_next = first_term;
289 first_term = term;
290
Bram Moolenaar293424c2017-07-26 23:11:01 +0200291 if (cmd == NULL || *cmd == NUL)
292 cmd = p_sh;
293
Bram Moolenaar78712a72017-08-05 14:50:12 +0200294 if (opt->jo_term_name != NULL)
295 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
296 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200297 {
298 int i;
299 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200300 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200301
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200302 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200303 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200304 /* Prepend a ! to the command name to avoid the buffer name equals
305 * the executable, otherwise ":w!" would overwrite it. */
306 if (i == 0)
307 vim_snprintf((char *)p, len, "!%s", cmd);
308 else
309 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200310 if (buflist_findname(p) == NULL)
311 {
312 curbuf->b_ffname = p;
313 break;
314 }
315 }
316 }
317 curbuf->b_fname = curbuf->b_ffname;
318
Bram Moolenaareb44a682017-08-03 22:44:55 +0200319 set_string_option_direct((char_u *)"buftype", -1,
320 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
321
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200322 /* Mark the buffer as not modifiable. It can only be made modifiable after
323 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200324 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200325
326 /* Set 'bufhidden' to "hide": allow closing the window. */
327 set_string_option_direct((char_u *)"bufhidden", -1,
328 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200331 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200332
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200333 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200334 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200335 {
336 /* store the size we ended up with */
337 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 }
339 else
340 {
Bram Moolenaard85f2712017-07-28 21:51:57 +0200341 free_terminal(curbuf);
Bram Moolenaar61a66052017-07-22 18:39:00 +0200342
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200343 /* Wiping out the buffer will also close the window and call
344 * free_terminal(). */
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200345 do_buffer(DOBUF_WIPE, DOBUF_CURRENT, FORWARD, 0, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200346 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200347}
348
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200349/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200350 * ":terminal": open a terminal window and execute a job in it.
351 */
352 void
353ex_terminal(exarg_T *eap)
354{
355 jobopt_T opt;
356
357 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200358
359 if (eap->addr_count == 2)
360 {
361 opt.jo_term_rows = eap->line1;
362 opt.jo_term_cols = eap->line2;
363 }
364 else if (eap->addr_count == 1)
365 {
366 if (cmdmod.split & WSP_VERT)
367 opt.jo_term_cols = eap->line2;
368 else
369 opt.jo_term_rows = eap->line2;
370 }
371 /* TODO: get more options from before the command */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200372
373 term_start(eap->arg, &opt);
374}
375
376/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200377 * Free the scrollback buffer for "term".
378 */
379 static void
380free_scrollback(term_T *term)
381{
382 int i;
383
384 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
385 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
386 ga_clear(&term->tl_scrollback);
387}
388
389/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200390 * Free a terminal and everything it refers to.
391 * Kills the job if there is one.
392 * Called when wiping out a buffer.
393 */
394 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200395free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200396{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200397 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200398 term_T *tp;
399
400 if (term == NULL)
401 return;
402 if (first_term == term)
403 first_term = term->tl_next;
404 else
405 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
406 if (tp->tl_next == term)
407 {
408 tp->tl_next = term->tl_next;
409 break;
410 }
411
412 if (term->tl_job != NULL)
413 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200414 if (term->tl_job->jv_status != JOB_ENDED
415 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200416 job_stop(term->tl_job, NULL, "kill");
417 job_unref(term->tl_job);
418 }
419
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200420 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200421
422 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200423 vim_free(term->tl_title);
424 vim_free(term->tl_status_text);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200425 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200426 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200427}
428
429/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200430 * Write job output "msg[len]" to the vterm.
431 */
432 static void
433term_write_job_output(term_T *term, char_u *msg, size_t len)
434{
435 VTerm *vterm = term->tl_vterm;
436 char_u *p;
437 size_t done;
438 size_t len_now;
439
440 for (done = 0; done < len; done += len_now)
441 {
442 for (p = msg + done; p < msg + len; )
443 {
444 if (*p == NL)
445 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200446 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200447 }
448 len_now = p - msg - done;
449 vterm_input_write(vterm, (char *)msg + done, len_now);
450 if (p < msg + len && *p == NL)
451 {
452 /* Convert NL to CR-NL, that appears to work best. */
453 vterm_input_write(vterm, "\r\n", 2);
454 ++len_now;
455 }
456 }
457
458 /* this invokes the damage callbacks */
459 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
460}
461
Bram Moolenaar1c844932017-07-24 23:36:41 +0200462 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200463update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200464{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200465 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200466 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200467 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200468 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200469 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200470 if (term->tl_cursor_visible)
471 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200472 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200473#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200474 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200475 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200476#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200477 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200478}
479
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200480/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200481 * Invoked when "msg" output from a job was received. Write it to the terminal
482 * of "buffer".
483 */
484 void
485write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
486{
487 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200488 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200489
Bram Moolenaard85f2712017-07-28 21:51:57 +0200490 if (term->tl_vterm == NULL)
491 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200492 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200493 return;
494 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200495 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200496 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200497
Bram Moolenaar6d819742017-08-06 14:57:49 +0200498 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200499 {
500 /* TODO: only update once in a while. */
501 update_screen(0);
502 update_cursor(term, TRUE);
503 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200504}
505
506/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200507 * Send a mouse position and click to the vterm
508 */
509 static int
510term_send_mouse(VTerm *vterm, int button, int pressed)
511{
512 VTermModifier mod = VTERM_MOD_NONE;
513
514 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
515 mouse_col - W_WINCOL(curwin), mod);
516 vterm_mouse_button(vterm, button, pressed, mod);
517 return TRUE;
518}
519
520/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200521 * Convert typed key "c" into bytes to send to the job.
522 * Return the number of bytes in "buf".
523 */
524 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200525term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200526{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200527 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200528 VTermKey key = VTERM_KEY_NONE;
529 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200530 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200531
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200532 switch (c)
533 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200534 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200535 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200536 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
537 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200538 case K_DEL: key = VTERM_KEY_DEL; break;
539 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200540 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
541 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200542 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200543 case K_S_END: mod = VTERM_MOD_SHIFT;
544 key = VTERM_KEY_END; break;
545 case K_C_END: mod = VTERM_MOD_CTRL;
546 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200547 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
548 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
549 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
550 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
551 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
552 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
553 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
554 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
555 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
556 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
557 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
558 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
559 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200560 case K_S_HOME: mod = VTERM_MOD_SHIFT;
561 key = VTERM_KEY_HOME; break;
562 case K_C_HOME: mod = VTERM_MOD_CTRL;
563 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200564 case K_INS: key = VTERM_KEY_INS; break;
565 case K_K0: key = VTERM_KEY_KP_0; break;
566 case K_K1: key = VTERM_KEY_KP_1; break;
567 case K_K2: key = VTERM_KEY_KP_2; break;
568 case K_K3: key = VTERM_KEY_KP_3; break;
569 case K_K4: key = VTERM_KEY_KP_4; break;
570 case K_K5: key = VTERM_KEY_KP_5; break;
571 case K_K6: key = VTERM_KEY_KP_6; break;
572 case K_K7: key = VTERM_KEY_KP_7; break;
573 case K_K8: key = VTERM_KEY_KP_8; break;
574 case K_K9: key = VTERM_KEY_KP_9; break;
575 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
576 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
577 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
578 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
579 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
580 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
581 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
582 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
583 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
584 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
585 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
586 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
587 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200588 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
589 key = VTERM_KEY_LEFT; break;
590 case K_C_LEFT: mod = VTERM_MOD_CTRL;
591 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200592 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
593 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
594 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200595 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
596 key = VTERM_KEY_RIGHT; break;
597 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
598 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200600 case K_S_UP: mod = VTERM_MOD_SHIFT;
601 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200602 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200603
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200604 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
605 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
606 case K_MOUSELEFT: /* TODO */ return 0;
607 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200608
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200609 case K_LEFTMOUSE:
610 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
611 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
612 case K_LEFTRELEASE:
613 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
614 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
615 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
616 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
617 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
618 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
619 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
620 case K_X1MOUSE: /* TODO */ return 0;
621 case K_X1DRAG: /* TODO */ return 0;
622 case K_X1RELEASE: /* TODO */ return 0;
623 case K_X2MOUSE: /* TODO */ return 0;
624 case K_X2DRAG: /* TODO */ return 0;
625 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200626
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200627 case K_IGNORE: return 0;
628 case K_NOP: return 0;
629 case K_UNDO: return 0;
630 case K_HELP: return 0;
631 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
632 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
633 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
634 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
635 case K_SELECT: return 0;
636#ifdef FEAT_GUI
637 case K_VER_SCROLLBAR: return 0;
638 case K_HOR_SCROLLBAR: return 0;
639#endif
640#ifdef FEAT_GUI_TABLINE
641 case K_TABLINE: return 0;
642 case K_TABMENU: return 0;
643#endif
644#ifdef FEAT_NETBEANS_INTG
645 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
646#endif
647#ifdef FEAT_DND
648 case K_DROP: return 0;
649#endif
650#ifdef FEAT_AUTOCMD
651 case K_CURSORHOLD: return 0;
652#endif
653 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
654 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 }
656
657 /*
658 * Convert special keys to vterm keys:
659 * - Write keys to vterm: vterm_keyboard_key()
660 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200661 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 */
663 if (key != VTERM_KEY_NONE)
664 /* Special key, let vterm convert it. */
665 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200666 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 /* Normal character, let vterm convert it. */
668 vterm_keyboard_unichar(vterm, c, mod);
669
670 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200671 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200673
Bram Moolenaar938783d2017-07-16 20:13:26 +0200674/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200675 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200676 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200677 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200678term_job_running(term_T *term)
679{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200680 /* Also consider the job finished when the channel is closed, to avoid a
681 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200682 return term != NULL
683 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200684 && term->tl_job->jv_status == JOB_STARTED
685 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200686}
687
688/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200689 * Add the last line of the scrollback buffer to the buffer in the window.
690 */
691 static void
692add_scrollback_line_to_buffer(term_T *term)
693{
694 linenr_T lnum = term->tl_scrollback.ga_len - 1;
695 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
696 garray_T ga;
697 int c;
698 int col;
699 int i;
700
701 ga_init2(&ga, 1, 100);
702 for (col = 0; col < line->sb_cols; col += line->sb_cells[col].width)
703 {
704 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
705 goto failed;
706 for (i = 0; (c = line->sb_cells[col].chars[i]) > 0 || i == 0; ++i)
707 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
708 (char_u *)ga.ga_data + ga.ga_len);
709 }
710 if (ga_grow(&ga, 1) == FAIL)
711 goto failed;
712 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
713 ml_append_buf(term->tl_buffer, lnum, ga.ga_data, ga.ga_len + 1, FALSE);
714
715 if (lnum == 0)
716 {
717 /* Delete the empty line that was in the empty buffer. */
718 curbuf = term->tl_buffer;
719 ml_delete(2, FALSE);
720 curbuf = curwin->w_buffer;
721 }
722
723failed:
724 ga_clear(&ga);
725}
726
727/*
728 * Add the current lines of the terminal to scrollback and to the buffer.
729 * Called after the job has ended and when switching to Terminal mode.
730 */
731 static void
732move_terminal_to_buffer(term_T *term)
733{
734 win_T *wp;
735 int len;
736 int lines_skipped = 0;
737 VTermPos pos;
738 VTermScreenCell cell;
739 VTermScreenCell *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200740 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200741
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200742 if (term->tl_vterm == NULL)
743 return;
744 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200745 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
746 {
747 len = 0;
748 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
749 if (vterm_screen_get_cell(screen, pos, &cell) != 0
750 && cell.chars[0] != NUL)
751 len = pos.col + 1;
752
753 if (len == 0)
754 ++lines_skipped;
755 else
756 {
757 while (lines_skipped > 0)
758 {
759 /* Line was skipped, add an empty line. */
760 --lines_skipped;
761 if (ga_grow(&term->tl_scrollback, 1) == OK)
762 {
763 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
764 + term->tl_scrollback.ga_len;
765
766 line->sb_cols = 0;
767 line->sb_cells = NULL;
768 ++term->tl_scrollback.ga_len;
769
770 add_scrollback_line_to_buffer(term);
771 }
772 }
773
774 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
775 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
776 {
777 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
778 + term->tl_scrollback.ga_len;
779
780 for (pos.col = 0; pos.col < len; ++pos.col)
781 {
782 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
783 vim_memset(p + pos.col, 0, sizeof(cell));
784 else
785 p[pos.col] = cell;
786 }
787 line->sb_cols = len;
788 line->sb_cells = p;
789 ++term->tl_scrollback.ga_len;
790
791 add_scrollback_line_to_buffer(term);
792 }
793 else
794 vim_free(p);
795 }
796 }
797
798 FOR_ALL_WINDOWS(wp)
799 {
800 if (wp->w_buffer == term->tl_buffer)
801 {
802 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
803 wp->w_cursor.col = 0;
804 wp->w_valid = 0;
805 redraw_win_later(wp, NOT_VALID);
806 }
807 }
808}
809
810 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200811set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200812{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200813 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814 vim_free(term->tl_status_text);
815 term->tl_status_text = NULL;
816 if (term->tl_buffer == curbuf)
817 maketitle();
818}
819
820/*
821 * Called after the job if finished and Terminal mode is not active:
822 * Move the vterm contents into the scrollback buffer and free the vterm.
823 */
824 static void
825cleanup_vterm(term_T *term)
826{
827 move_terminal_to_buffer(term);
828 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200829 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830}
831
832/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200833 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834 * Suspends updating the terminal window.
835 */
836 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200837term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200838{
839 term_T *term = curbuf->b_term;
840
841 /* Append the current terminal contents to the buffer. */
842 move_terminal_to_buffer(term);
843
Bram Moolenaar6d819742017-08-06 14:57:49 +0200844 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200845
Bram Moolenaar6d819742017-08-06 14:57:49 +0200846 /* Move the window cursor to the position of the cursor in the
847 * terminal. */
848 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
849 + term->tl_cursor_pos.row + 1;
850 check_cursor();
851 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200852
Bram Moolenaar6d819742017-08-06 14:57:49 +0200853 /* Display the same lines as in the terminal. */
854 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200855}
856
857/*
858 * Returns TRUE if the current window contains a terminal and we are in
859 * Terminal-Normal mode.
860 */
861 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200862term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200863{
864 term_T *term = curbuf->b_term;
865
Bram Moolenaar6d819742017-08-06 14:57:49 +0200866 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200867}
868
869/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200870 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200871 * Restores updating the terminal window.
872 */
873 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200874term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875{
876 term_T *term = curbuf->b_term;
877 sb_line_T *line;
878 garray_T *gap;
879
880 /* Remove the terminal contents from the scrollback and the buffer. */
881 gap = &term->tl_scrollback;
882 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
883 {
884 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
885 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
886 vim_free(line->sb_cells);
887 --gap->ga_len;
888 if (gap->ga_len == 0)
889 break;
890 }
891 check_cursor();
892
Bram Moolenaar6d819742017-08-06 14:57:49 +0200893 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200894
895 if (term->tl_channel_closed)
896 cleanup_vterm(term);
897 redraw_buf_and_status_later(curbuf, NOT_VALID);
898}
899
900/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200901 * Get a key from the user without mapping.
902 * TODO: use terminal mode mappings.
903 */
904 static int
905term_vgetc()
906{
907 int c;
908
909 ++no_mapping;
910 ++allow_keys;
911 got_int = FALSE;
912 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +0200913 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +0200914 --no_mapping;
915 --allow_keys;
916 return c;
917}
918
919/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200920 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +0200921 * Return FAIL when the key needs to be handled in Normal mode.
922 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200923 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200924 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200925send_keys_to_term(term_T *term, int c, int typed)
926{
927 char msg[KEY_BUF_LEN];
928 size_t len;
929 static int mouse_was_outside = FALSE;
930 int dragging_outside = FALSE;
931
932 /* Catch keys that need to be handled as in Normal mode. */
933 switch (c)
934 {
935 case NUL:
936 case K_ZERO:
937 if (typed)
938 stuffcharReadbuff(c);
939 return FAIL;
940
941 case K_IGNORE:
942 return FAIL;
943
944 case K_LEFTDRAG:
945 case K_MIDDLEDRAG:
946 case K_RIGHTDRAG:
947 case K_X1DRAG:
948 case K_X2DRAG:
949 dragging_outside = mouse_was_outside;
950 /* FALLTHROUGH */
951 case K_LEFTMOUSE:
952 case K_LEFTMOUSE_NM:
953 case K_LEFTRELEASE:
954 case K_LEFTRELEASE_NM:
955 case K_MIDDLEMOUSE:
956 case K_MIDDLERELEASE:
957 case K_RIGHTMOUSE:
958 case K_RIGHTRELEASE:
959 case K_X1MOUSE:
960 case K_X1RELEASE:
961 case K_X2MOUSE:
962 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200963
964 case K_MOUSEUP:
965 case K_MOUSEDOWN:
966 case K_MOUSELEFT:
967 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200968 if (mouse_row < W_WINROW(curwin)
969 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
970 || mouse_col < W_WINCOL(curwin)
971 || mouse_col >= W_ENDCOL(curwin)
972 || dragging_outside)
973 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +0200974 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200975 if (typed)
976 {
977 stuffcharReadbuff(c);
978 mouse_was_outside = TRUE;
979 }
980 return FAIL;
981 }
982 }
983 if (typed)
984 mouse_was_outside = FALSE;
985
986 /* Convert the typed key to a sequence of bytes for the job. */
987 len = term_convert_key(term, c, msg);
988 if (len > 0)
989 /* TODO: if FAIL is returned, stop? */
990 channel_send(term->tl_job->jv_channel, PART_IN,
991 (char_u *)msg, (int)len, NULL);
992
993 return OK;
994}
995
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +0200996 static void
997position_cursor(win_T *wp, VTermPos *pos)
998{
999 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1000 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1001 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1002}
1003
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001004/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001005 * Handle CTRL-W "": send register contents to the job.
1006 */
1007 static void
1008term_paste_register(int prev_c UNUSED)
1009{
1010 int c;
1011 list_T *l;
1012 listitem_T *item;
1013 long reglen = 0;
1014 int type;
1015
1016#ifdef FEAT_CMDL_INFO
1017 if (add_to_showcmd(prev_c))
1018 if (add_to_showcmd('"'))
1019 out_flush();
1020#endif
1021 c = term_vgetc();
1022#ifdef FEAT_CMDL_INFO
1023 clear_showcmd();
1024#endif
1025
1026 /* CTRL-W "= prompt for expression to evaluate. */
1027 if (c == '=' && get_expr_register() != '=')
1028 return;
1029
1030 l = (list_T *)get_reg_contents(c, GREG_LIST);
1031 if (l != NULL)
1032 {
1033 type = get_reg_type(c, &reglen);
1034 for (item = l->lv_first; item != NULL; item = item->li_next)
1035 {
1036 char_u *s = get_tv_string(&item->li_tv);
1037
1038 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1039 s, STRLEN(s), NULL);
1040 if (item->li_next != NULL || type == MLINE)
1041 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1042 (char_u *)"\r", 1, NULL);
1043 }
1044 list_free(l);
1045 }
1046}
1047
1048/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001049 * Returns TRUE if the current window contains a terminal and we are sending
1050 * keys to the job.
1051 */
1052 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001053term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001054{
1055 term_T *term = curbuf->b_term;
1056
1057 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001058 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001059 && term->tl_vterm != NULL
1060 && term_job_running(term);
1061}
1062
1063/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001064 * Wait for input and send it to the job.
1065 * Return when the start of a CTRL-W command is typed or anything else that
1066 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001067 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1068 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001069 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001070 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001071terminal_loop(void)
1072{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001073 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001074 int termkey = 0;
1075
1076 if (*curwin->w_p_tk != NUL)
1077 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001078 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001079
1080 for (;;)
1081 {
1082 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001083 /* Repeat redrawing in case a message is received while redrawing. */
1084 while (curwin->w_redr_type != 0)
1085 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001086 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001087
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001088 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001089 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001090 /* job finished while waiting for a character */
1091 break;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001092
Bram Moolenaarfae42832017-08-01 22:24:26 +02001093#ifdef UNIX
1094 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1095#endif
1096#ifdef WIN3264
1097 if (c == Ctrl_C)
1098 /* We don't know if the job can handle CTRL-C itself or not, this
1099 * may kill the shell instead of killing the command running in the
1100 * shell. */
Bram Moolenaard8dc1792017-08-03 11:55:21 +02001101 mch_stop_job(curbuf->b_term->tl_job, (char_u *)"quit");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001102#endif
1103
Bram Moolenaar69198192017-08-05 14:10:48 +02001104 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001105 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001106 int prev_c = c;
1107
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001108#ifdef FEAT_CMDL_INFO
1109 if (add_to_showcmd(c))
1110 out_flush();
1111#endif
1112 c = term_vgetc();
1113#ifdef FEAT_CMDL_INFO
1114 clear_showcmd();
1115#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001116 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001117 /* job finished while waiting for a character */
1118 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001119
Bram Moolenaar69198192017-08-05 14:10:48 +02001120 if (prev_c == Ctrl_BSL)
1121 {
1122 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001123 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001124 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1125 term_enter_normal_mode();
1126 return FAIL;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001127 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001128 /* Send both keys to the terminal. */
1129 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1130 }
1131 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001132 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001133 /* "CTRL-W .": send CTRL-W to the job */
1134 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001135 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001136 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001137 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001138 /* CTRL-W N : go to Terminal-Normal mode. */
1139 term_enter_normal_mode();
Bram Moolenaar423802d2017-07-30 16:52:24 +02001140 return FAIL;
1141 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001142 else if (c == '"')
1143 {
1144 term_paste_register(prev_c);
1145 continue;
1146 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001147 else if (termkey == 0 || c != termkey)
1148 {
1149 stuffcharReadbuff(Ctrl_W);
1150 stuffcharReadbuff(c);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001151 return OK;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001152 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001153 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001154 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
1155 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001156 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001157 return FAIL;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001158}
1159
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001160/*
1161 * Called when a job has finished.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001162 * This updates the title and status, but does not close the vter, because
1163 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001164 */
1165 void
1166term_job_ended(job_T *job)
1167{
1168 term_T *term;
1169 int did_one = FALSE;
1170
1171 for (term = first_term; term != NULL; term = term->tl_next)
1172 if (term->tl_job == job)
1173 {
1174 vim_free(term->tl_title);
1175 term->tl_title = NULL;
1176 vim_free(term->tl_status_text);
1177 term->tl_status_text = NULL;
1178 redraw_buf_and_status_later(term->tl_buffer, VALID);
1179 did_one = TRUE;
1180 }
1181 if (did_one)
1182 redraw_statuslines();
1183 if (curbuf->b_term != NULL)
1184 {
1185 if (curbuf->b_term->tl_job == job)
1186 maketitle();
1187 update_cursor(curbuf->b_term, TRUE);
1188 }
1189}
1190
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001191 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001192may_toggle_cursor(term_T *term)
1193{
1194 if (curbuf == term->tl_buffer)
1195 {
1196 if (term->tl_cursor_visible)
1197 cursor_on();
1198 else
1199 cursor_off();
1200 }
1201}
1202
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001203 static int
1204handle_damage(VTermRect rect, void *user)
1205{
1206 term_T *term = (term_T *)user;
1207
1208 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1209 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1210 redraw_buf_later(term->tl_buffer, NOT_VALID);
1211 return 1;
1212}
1213
1214 static int
1215handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1216{
1217 term_T *term = (term_T *)user;
1218
1219 /* TODO */
1220 redraw_buf_later(term->tl_buffer, NOT_VALID);
1221 return 1;
1222}
1223
1224 static int
1225handle_movecursor(
1226 VTermPos pos,
1227 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001228 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001229 void *user)
1230{
1231 term_T *term = (term_T *)user;
1232 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001233
1234 term->tl_cursor_pos = pos;
1235 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001236
1237 FOR_ALL_WINDOWS(wp)
1238 {
1239 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001240 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001241 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001242 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001243 {
1244 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001245 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001246 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001247
1248 return 1;
1249}
1250
Bram Moolenaar21554412017-07-24 21:44:43 +02001251 static int
1252handle_settermprop(
1253 VTermProp prop,
1254 VTermValue *value,
1255 void *user)
1256{
1257 term_T *term = (term_T *)user;
1258
1259 switch (prop)
1260 {
1261 case VTERM_PROP_TITLE:
1262 vim_free(term->tl_title);
1263 term->tl_title = vim_strsave((char_u *)value->string);
1264 vim_free(term->tl_status_text);
1265 term->tl_status_text = NULL;
1266 if (term == curbuf->b_term)
1267 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001268 break;
1269
1270 case VTERM_PROP_CURSORVISIBLE:
1271 term->tl_cursor_visible = value->boolean;
1272 may_toggle_cursor(term);
1273 out_flush();
1274 break;
1275
Bram Moolenaar21554412017-07-24 21:44:43 +02001276 default:
1277 break;
1278 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001279 /* Always return 1, otherwise vterm doesn't store the value internally. */
1280 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001281}
1282
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001283/*
1284 * The job running in the terminal resized the terminal.
1285 */
1286 static int
1287handle_resize(int rows, int cols, void *user)
1288{
1289 term_T *term = (term_T *)user;
1290 win_T *wp;
1291
1292 term->tl_rows = rows;
1293 term->tl_cols = cols;
1294 FOR_ALL_WINDOWS(wp)
1295 {
1296 if (wp->w_buffer == term->tl_buffer)
1297 {
1298 win_setheight_win(rows, wp);
1299 win_setwidth_win(cols, wp);
1300 }
1301 }
1302
1303 redraw_buf_later(term->tl_buffer, NOT_VALID);
1304 return 1;
1305}
1306
Bram Moolenaard85f2712017-07-28 21:51:57 +02001307/*
1308 * Handle a line that is pushed off the top of the screen.
1309 */
1310 static int
1311handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1312{
1313 term_T *term = (term_T *)user;
1314
1315 /* TODO: Limit the number of lines that are stored. */
1316 /* TODO: put the text in the buffer. */
1317 if (ga_grow(&term->tl_scrollback, 1) == OK)
1318 {
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001319 VTermScreenCell *p = NULL;
1320 int len = 0;
1321 int i;
1322 sb_line_T *line;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001323
1324 /* do not store empty cells at the end */
1325 for (i = 0; i < cols; ++i)
1326 if (cells[i].chars[0] != 0)
1327 len = i + 1;
1328
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001329 if (len > 0)
1330 p = (VTermScreenCell *)alloc((int)sizeof(VTermScreenCell) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001331 if (p != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001332 mch_memmove(p, cells, sizeof(VTermScreenCell) * len);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001333
1334 line = (sb_line_T *)term->tl_scrollback.ga_data
1335 + term->tl_scrollback.ga_len;
1336 line->sb_cols = len;
1337 line->sb_cells = p;
1338 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001339 ++term->tl_scrollback_scrolled;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001340
1341 add_scrollback_line_to_buffer(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001342 }
1343 return 0; /* ignored */
1344}
1345
Bram Moolenaar21554412017-07-24 21:44:43 +02001346static VTermScreenCallbacks screen_callbacks = {
1347 handle_damage, /* damage */
1348 handle_moverect, /* moverect */
1349 handle_movecursor, /* movecursor */
1350 handle_settermprop, /* settermprop */
1351 NULL, /* bell */
1352 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001353 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001354 NULL /* sb_popline */
1355};
1356
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001357/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001358 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001359 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001360 */
1361 void
1362term_channel_closed(channel_T *ch)
1363{
1364 term_T *term;
1365 int did_one = FALSE;
1366
1367 for (term = first_term; term != NULL; term = term->tl_next)
1368 if (term->tl_job == ch->ch_job)
1369 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001370 term->tl_channel_closed = TRUE;
1371
Bram Moolenaard85f2712017-07-28 21:51:57 +02001372 vim_free(term->tl_title);
1373 term->tl_title = NULL;
1374 vim_free(term->tl_status_text);
1375 term->tl_status_text = NULL;
1376
Bram Moolenaar423802d2017-07-30 16:52:24 +02001377 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001378 if (!term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001379 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001380
1381 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
1382 did_one = TRUE;
1383 }
1384 if (did_one)
1385 {
1386 redraw_statuslines();
1387
1388 /* Need to break out of vgetc(). */
1389 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001390 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001391
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001392 term = curbuf->b_term;
1393 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001394 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001395 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001396 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001397 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001398 }
1399 }
1400}
1401
1402/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001403 * Reverse engineer the RGB value into a cterm color index.
1404 * First color is 1. Return 0 if no match found.
1405 */
1406 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001407color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001408{
1409 int red = color->red;
1410 int blue = color->blue;
1411 int green = color->green;
1412
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001413 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001414 if (red == 0)
1415 {
1416 if (green == 0)
1417 {
1418 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001419 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001420 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001421 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001422 }
1423 else if (green == 224)
1424 {
1425 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001426 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001427 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001428 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001429 }
1430 }
1431 else if (red == 224)
1432 {
1433 if (green == 0)
1434 {
1435 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001436 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001437 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001438 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001439 }
1440 else if (green == 224)
1441 {
1442 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001443 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001444 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001445 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001446 }
1447 }
1448 else if (red == 128)
1449 {
1450 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001451 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001452 }
1453 else if (red == 255)
1454 {
1455 if (green == 64)
1456 {
1457 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001458 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001459 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001460 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001461 }
1462 else if (green == 255)
1463 {
1464 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001465 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001466 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001467 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001468 }
1469 }
1470 else if (red == 64)
1471 {
1472 if (green == 64)
1473 {
1474 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001475 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001476 }
1477 else if (green == 255)
1478 {
1479 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001480 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001481 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001482 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001483 }
1484 }
1485 if (t_colors >= 256)
1486 {
1487 if (red == blue && red == green)
1488 {
1489 /* 24-color greyscale */
1490 static int cutoff[23] = {
1491 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1492 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1493 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1494 int i;
1495
1496 for (i = 0; i < 23; ++i)
1497 if (red < cutoff[i])
1498 return i + 233;
1499 return 256;
1500 }
1501
1502 /* 216-color cube */
1503 return 17 + ((red + 25) / 0x33) * 36
1504 + ((green + 25) / 0x33) * 6
1505 + (blue + 25) / 0x33;
1506 }
1507 return 0;
1508}
1509
1510/*
1511 * Convert the attributes of a vterm cell into an attribute index.
1512 */
1513 static int
1514cell2attr(VTermScreenCell *cell)
1515{
1516 int attr = 0;
1517
1518 if (cell->attrs.bold)
1519 attr |= HL_BOLD;
1520 if (cell->attrs.underline)
1521 attr |= HL_UNDERLINE;
1522 if (cell->attrs.italic)
1523 attr |= HL_ITALIC;
1524 if (cell->attrs.strike)
1525 attr |= HL_STANDOUT;
1526 if (cell->attrs.reverse)
1527 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001528
1529#ifdef FEAT_GUI
1530 if (gui.in_use)
1531 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001532 guicolor_T fg, bg;
1533
1534 fg = gui_mch_get_rgb_color(cell->fg.red, cell->fg.green, cell->fg.blue);
1535 bg = gui_mch_get_rgb_color(cell->bg.red, cell->bg.green, cell->bg.blue);
1536 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001537 }
1538 else
1539#endif
1540#ifdef FEAT_TERMGUICOLORS
1541 if (p_tgc)
1542 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001543 guicolor_T fg, bg;
1544
1545 fg = gui_get_rgb_color_cmn(cell->fg.red, cell->fg.green, cell->fg.blue);
1546 bg = gui_get_rgb_color_cmn(cell->bg.red, cell->bg.green, cell->bg.blue);
1547
1548 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001549 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001550 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001551#endif
1552 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001553 int bold = MAYBE;
1554 int fg = color2index(&cell->fg, TRUE, &bold);
1555 int bg = color2index(&cell->bg, FALSE, &bold);
1556
1557 /* with 8 colors set the bold attribute to get a bright foreground */
1558 if (bold == TRUE)
1559 attr |= HL_BOLD;
1560 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001561 }
1562 return 0;
1563}
1564
1565/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001566 * Called to update a window that contains an active terminal.
1567 * Returns FAIL when there is no terminal running in this window or in
1568 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001569 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001570 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001571term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001572{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001573 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001574 VTerm *vterm;
1575 VTermScreen *screen;
1576 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001577 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001578
Bram Moolenaar6d819742017-08-06 14:57:49 +02001579 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001580 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001581
Bram Moolenaard85f2712017-07-28 21:51:57 +02001582 vterm = term->tl_vterm;
1583 screen = vterm_obtain_screen(vterm);
1584 state = vterm_obtain_state(vterm);
1585
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001586 /*
1587 * If the window was resized a redraw will be triggered and we get here.
1588 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1589 */
1590 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1591 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001592 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001593 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1594 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1595 win_T *twp;
1596
1597 FOR_ALL_WINDOWS(twp)
1598 {
1599 /* When more than one window shows the same terminal, use the
1600 * smallest size. */
1601 if (twp->w_buffer == term->tl_buffer)
1602 {
1603 if (!term->tl_rows_fixed && rows > twp->w_height)
1604 rows = twp->w_height;
1605 if (!term->tl_cols_fixed && cols > twp->w_width)
1606 cols = twp->w_width;
1607 }
1608 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001609
1610 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001611 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001612 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001613 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001614 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001615
1616 /* The cursor may have been moved when resizing. */
1617 vterm_state_get_cursorpos(state, &pos);
1618 position_cursor(wp, &pos);
1619
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001620 /* TODO: Only redraw what changed. */
1621 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001622 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001623 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001624 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001625
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001626 if (pos.row < term->tl_rows)
1627 {
1628 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001629 {
1630 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001631 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001632
Bram Moolenaareeac6772017-07-23 15:48:37 +02001633 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1634 vim_memset(&cell, 0, sizeof(cell));
1635
1636 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001637 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001638 if (c == NUL)
1639 {
1640 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001641#if defined(FEAT_MBYTE)
1642 if (enc_utf8)
1643 ScreenLinesUC[off] = NUL;
1644#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001645 }
1646 else
1647 {
1648#if defined(FEAT_MBYTE)
1649 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001650 {
1651 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001652 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001653 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001654 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001655 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001656 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001657 if (enc_utf8)
1658 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001659 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001660#else
1661 ScreenLines[off] = c;
1662#endif
1663 }
Bram Moolenaareeac6772017-07-23 15:48:37 +02001664 ScreenAttrs[off] = cell2attr(&cell);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001665
1666 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001667 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001668 if (cell.width == 2)
1669 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001670 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001671#if defined(FEAT_MBYTE)
1672 if (enc_utf8)
1673 ScreenLinesUC[off] = NUL;
1674#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001675 ++pos.col;
1676 ++off;
1677 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001678 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001679 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001680 else
1681 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001682
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001683 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1684 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001685 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001686
1687 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001688}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001689
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001690/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001691 * Return TRUE if "wp" is a terminal window where the job has finished.
1692 */
1693 int
1694term_is_finished(buf_T *buf)
1695{
1696 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1697}
1698
1699/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001700 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001701 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001702 */
1703 int
1704term_show_buffer(buf_T *buf)
1705{
1706 term_T *term = buf->b_term;
1707
Bram Moolenaar6d819742017-08-06 14:57:49 +02001708 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001709}
1710
1711/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001712 * The current buffer is going to be changed. If there is terminal
1713 * highlighting remove it now.
1714 */
1715 void
1716term_change_in_curbuf(void)
1717{
1718 term_T *term = curbuf->b_term;
1719
1720 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
1721 {
1722 free_scrollback(term);
1723 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02001724
1725 /* The buffer is now like a normal buffer, it cannot be easily
1726 * abandoned when changed. */
1727 set_string_option_direct((char_u *)"buftype", -1,
1728 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001729 }
1730}
1731
1732/*
1733 * Get the screen attribute for a position in the buffer.
1734 */
1735 int
1736term_get_attr(buf_T *buf, linenr_T lnum, int col)
1737{
1738 term_T *term = buf->b_term;
1739 sb_line_T *line;
1740
Bram Moolenaar70229f92017-07-29 16:01:53 +02001741 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001742 return 0;
1743 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
1744 if (col >= line->sb_cols)
1745 return 0;
1746 return cell2attr(line->sb_cells + col);
1747}
1748
1749/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001750 * Create a new vterm and initialize it.
1751 */
1752 static void
1753create_vterm(term_T *term, int rows, int cols)
1754{
1755 VTerm *vterm;
1756 VTermScreen *screen;
1757
1758 vterm = vterm_new(rows, cols);
1759 term->tl_vterm = vterm;
1760 screen = vterm_obtain_screen(vterm);
1761 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
1762 /* TODO: depends on 'encoding'. */
1763 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001764
1765 /* Vterm uses a default black background. Set it to white when
1766 * 'background' is "light". */
1767 if (*p_bg == 'l')
1768 {
1769 VTermColor fg, bg;
1770
1771 fg.red = fg.green = fg.blue = 0;
1772 bg.red = bg.green = bg.blue = 255;
1773 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
1774 }
1775
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001776 /* Required to initialize most things. */
1777 vterm_screen_reset(screen, 1 /* hard */);
1778}
1779
Bram Moolenaar21554412017-07-24 21:44:43 +02001780/*
1781 * Return the text to show for the buffer name and status.
1782 */
1783 char_u *
1784term_get_status_text(term_T *term)
1785{
1786 if (term->tl_status_text == NULL)
1787 {
1788 char_u *txt;
1789 size_t len;
1790
Bram Moolenaar6d819742017-08-06 14:57:49 +02001791 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001792 {
1793 if (term_job_running(term))
1794 txt = (char_u *)_("Terminal");
1795 else
1796 txt = (char_u *)_("Terminal-finished");
1797 }
1798 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02001799 txt = term->tl_title;
1800 else if (term_job_running(term))
1801 txt = (char_u *)_("running");
1802 else
1803 txt = (char_u *)_("finished");
1804 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02001805 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02001806 if (term->tl_status_text != NULL)
1807 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
1808 term->tl_buffer->b_fname, txt);
1809 }
1810 return term->tl_status_text;
1811}
1812
Bram Moolenaarf86eea92017-07-28 13:51:30 +02001813/*
1814 * Mark references in jobs of terminals.
1815 */
1816 int
1817set_ref_in_term(int copyID)
1818{
1819 int abort = FALSE;
1820 term_T *term;
1821 typval_T tv;
1822
1823 for (term = first_term; term != NULL; term = term->tl_next)
1824 if (term->tl_job != NULL)
1825 {
1826 tv.v_type = VAR_JOB;
1827 tv.vval.v_job = term->tl_job;
1828 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
1829 }
1830 return abort;
1831}
1832
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001833/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001834 * Get the buffer from the first argument in "argvars".
1835 * Returns NULL when the buffer is not for a terminal window.
1836 */
1837 static buf_T *
1838term_get_buf(typval_T *argvars)
1839{
1840 buf_T *buf;
1841
1842 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
1843 ++emsg_off;
1844 buf = get_buf_tv(&argvars[0], FALSE);
1845 --emsg_off;
1846 if (buf == NULL || buf->b_term == NULL)
1847 return NULL;
1848 return buf;
1849}
1850
1851/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001852 * "term_getattr(attr, name)" function
1853 */
1854 void
1855f_term_getattr(typval_T *argvars, typval_T *rettv)
1856{
1857 int attr;
1858 size_t i;
1859 char_u *name;
1860
1861 static struct {
1862 char *name;
1863 int attr;
1864 } attrs[] = {
1865 {"bold", HL_BOLD},
1866 {"italic", HL_ITALIC},
1867 {"underline", HL_UNDERLINE},
1868 {"strike", HL_STANDOUT},
1869 {"reverse", HL_INVERSE},
1870 };
1871
1872 attr = get_tv_number(&argvars[0]);
1873 name = get_tv_string_chk(&argvars[1]);
1874 if (name == NULL)
1875 return;
1876
1877 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
1878 if (STRCMP(name, attrs[i].name) == 0)
1879 {
1880 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
1881 break;
1882 }
1883}
1884
1885/*
Bram Moolenaar97870002017-07-30 18:28:38 +02001886 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001887 */
Bram Moolenaar97870002017-07-30 18:28:38 +02001888 void
1889f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001890{
Bram Moolenaar97870002017-07-30 18:28:38 +02001891 buf_T *buf = term_get_buf(argvars);
1892 list_T *l;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001893
Bram Moolenaar97870002017-07-30 18:28:38 +02001894 if (rettv_list_alloc(rettv) == FAIL)
1895 return;
1896 if (buf == NULL)
1897 return;
1898
1899 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001900 list_append_number(l, buf->b_term->tl_cursor_pos.row + 1);
1901 list_append_number(l, buf->b_term->tl_cursor_pos.col + 1);
Bram Moolenaar97870002017-07-30 18:28:38 +02001902 list_append_number(l, buf->b_term->tl_cursor_visible);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001903}
1904
1905/*
1906 * "term_getjob(buf)" function
1907 */
1908 void
1909f_term_getjob(typval_T *argvars, typval_T *rettv)
1910{
1911 buf_T *buf = term_get_buf(argvars);
1912
1913 rettv->v_type = VAR_JOB;
1914 rettv->vval.v_job = NULL;
1915 if (buf == NULL)
1916 return;
1917
1918 rettv->vval.v_job = buf->b_term->tl_job;
1919 if (rettv->vval.v_job != NULL)
1920 ++rettv->vval.v_job->jv_refcount;
1921}
1922
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001923 static int
1924get_row_number(typval_T *tv, term_T *term)
1925{
1926 if (tv->v_type == VAR_STRING
1927 && tv->vval.v_string != NULL
1928 && STRCMP(tv->vval.v_string, ".") == 0)
1929 return term->tl_cursor_pos.row;
1930 return (int)get_tv_number(tv) - 1;
1931}
1932
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001933/*
1934 * "term_getline(buf, row)" function
1935 */
1936 void
1937f_term_getline(typval_T *argvars, typval_T *rettv)
1938{
1939 buf_T *buf = term_get_buf(argvars);
1940 term_T *term;
1941 int row;
1942
1943 rettv->v_type = VAR_STRING;
1944 if (buf == NULL)
1945 return;
1946 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02001947 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001948
1949 if (term->tl_vterm == NULL)
1950 {
1951 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
1952
1953 /* vterm is finished, get the text from the buffer */
1954 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
1955 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
1956 }
1957 else
1958 {
1959 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
1960 VTermRect rect;
1961 int len;
1962 char_u *p;
1963
Bram Moolenaar5c838a32017-08-02 22:10:34 +02001964 if (row < 0 || row >= term->tl_rows)
1965 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001966 len = term->tl_cols * MB_MAXBYTES + 1;
1967 p = alloc(len);
1968 if (p == NULL)
1969 return;
1970 rettv->vval.v_string = p;
1971
1972 rect.start_col = 0;
1973 rect.end_col = term->tl_cols;
1974 rect.start_row = row;
1975 rect.end_row = row + 1;
1976 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
1977 }
1978}
1979
1980/*
1981 * "term_getsize(buf)" function
1982 */
1983 void
1984f_term_getsize(typval_T *argvars, typval_T *rettv)
1985{
1986 buf_T *buf = term_get_buf(argvars);
1987 list_T *l;
1988
1989 if (rettv_list_alloc(rettv) == FAIL)
1990 return;
1991 if (buf == NULL)
1992 return;
1993
1994 l = rettv->vval.v_list;
1995 list_append_number(l, buf->b_term->tl_rows);
1996 list_append_number(l, buf->b_term->tl_cols);
1997}
1998
1999/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002000 * "term_getstatus(buf)" function
2001 */
2002 void
2003f_term_getstatus(typval_T *argvars, typval_T *rettv)
2004{
2005 buf_T *buf = term_get_buf(argvars);
2006 term_T *term;
2007 char_u val[100];
2008
2009 rettv->v_type = VAR_STRING;
2010 if (buf == NULL)
2011 return;
2012 term = buf->b_term;
2013
2014 if (term_job_running(term))
2015 STRCPY(val, "running");
2016 else
2017 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002018 if (term->tl_normal_mode)
2019 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002020 rettv->vval.v_string = vim_strsave(val);
2021}
2022
2023/*
2024 * "term_gettitle(buf)" function
2025 */
2026 void
2027f_term_gettitle(typval_T *argvars, typval_T *rettv)
2028{
2029 buf_T *buf = term_get_buf(argvars);
2030
2031 rettv->v_type = VAR_STRING;
2032 if (buf == NULL)
2033 return;
2034
2035 if (buf->b_term->tl_title != NULL)
2036 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2037}
2038
2039/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002040 * "term_gettty(buf)" function
2041 */
2042 void
2043f_term_gettty(typval_T *argvars, typval_T *rettv)
2044{
2045 buf_T *buf = term_get_buf(argvars);
2046 char_u *p;
2047
2048 rettv->v_type = VAR_STRING;
2049 if (buf == NULL)
2050 return;
2051 if (buf->b_term->tl_job != NULL)
2052 p = buf->b_term->tl_job->jv_tty_name;
2053 else
2054 p = buf->b_term->tl_tty_name;
2055 if (p != NULL)
2056 rettv->vval.v_string = vim_strsave(p);
2057}
2058
2059/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002060 * "term_list()" function
2061 */
2062 void
2063f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2064{
2065 term_T *tp;
2066 list_T *l;
2067
2068 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2069 return;
2070
2071 l = rettv->vval.v_list;
2072 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2073 if (tp != NULL && tp->tl_buffer != NULL)
2074 if (list_append_number(l,
2075 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2076 return;
2077}
2078
2079/*
2080 * "term_scrape(buf, row)" function
2081 */
2082 void
2083f_term_scrape(typval_T *argvars, typval_T *rettv)
2084{
2085 buf_T *buf = term_get_buf(argvars);
2086 VTermScreen *screen = NULL;
2087 VTermPos pos;
2088 list_T *l;
2089 term_T *term;
2090
2091 if (rettv_list_alloc(rettv) == FAIL)
2092 return;
2093 if (buf == NULL)
2094 return;
2095 term = buf->b_term;
2096 if (term->tl_vterm != NULL)
2097 screen = vterm_obtain_screen(term->tl_vterm);
2098
2099 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002100 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002101 for (pos.col = 0; pos.col < term->tl_cols; )
2102 {
2103 dict_T *dcell;
2104 VTermScreenCell cell;
2105 char_u rgb[8];
2106 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2107 int off = 0;
2108 int i;
2109
2110 if (screen == NULL)
2111 {
2112 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2113 sb_line_T *line;
2114
2115 /* vterm has finished, get the cell from scrollback */
2116 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2117 break;
2118 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2119 if (pos.col >= line->sb_cols)
2120 break;
2121 cell = line->sb_cells[pos.col];
2122 }
2123 else if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2124 break;
2125 dcell = dict_alloc();
2126 list_append_dict(l, dcell);
2127
2128 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2129 {
2130 if (cell.chars[i] == 0)
2131 break;
2132 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2133 }
2134 mbs[off] = NUL;
2135 dict_add_nr_str(dcell, "chars", 0, mbs);
2136
2137 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2138 cell.fg.red, cell.fg.green, cell.fg.blue);
2139 dict_add_nr_str(dcell, "fg", 0, rgb);
2140 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
2141 cell.bg.red, cell.bg.green, cell.bg.blue);
2142 dict_add_nr_str(dcell, "bg", 0, rgb);
2143
2144 dict_add_nr_str(dcell, "attr", cell2attr(&cell), NULL);
2145 dict_add_nr_str(dcell, "width", cell.width, NULL);
2146
2147 ++pos.col;
2148 if (cell.width == 2)
2149 ++pos.col;
2150 }
2151}
2152
2153/*
2154 * "term_sendkeys(buf, keys)" function
2155 */
2156 void
2157f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2158{
2159 buf_T *buf = term_get_buf(argvars);
2160 char_u *msg;
2161 term_T *term;
2162
2163 rettv->v_type = VAR_UNKNOWN;
2164 if (buf == NULL)
2165 return;
2166
2167 msg = get_tv_string_chk(&argvars[1]);
2168 if (msg == NULL)
2169 return;
2170 term = buf->b_term;
2171 if (term->tl_vterm == NULL)
2172 return;
2173
2174 while (*msg != NUL)
2175 {
2176 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2177 msg += MB_PTR2LEN(msg);
2178 }
2179
Bram Moolenaar6d819742017-08-06 14:57:49 +02002180 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002181 {
2182 /* TODO: only update once in a while. */
2183 update_screen(0);
2184 if (buf == curbuf)
2185 update_cursor(term, TRUE);
2186 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002187}
2188
2189/*
2190 * "term_start(command, options)" function
2191 */
2192 void
2193f_term_start(typval_T *argvars, typval_T *rettv)
2194{
2195 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002196 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002197
2198 if (cmd == NULL)
2199 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002200 init_job_options(&opt);
2201 /* TODO: allow more job options */
2202 if (argvars[1].v_type != VAR_UNKNOWN
2203 && get_job_options(&argvars[1], &opt,
2204 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar78712a72017-08-05 14:50:12 +02002205 + JO_EXIT_CB + JO_CLOSE_CALLBACK
2206 + JO2_TERM_NAME) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002207 return;
2208
2209 term_start(cmd, &opt);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002210
2211 if (curbuf->b_term != NULL)
2212 rettv->vval.v_number = curbuf->b_fnum;
2213}
2214
2215/*
2216 * "term_wait" function
2217 */
2218 void
2219f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2220{
2221 buf_T *buf = term_get_buf(argvars);
2222
2223 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002224 {
2225 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002226 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002227 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002228 if (buf->b_term->tl_job == NULL)
2229 {
2230 ch_log(NULL, "term_wait(): no job to wait for");
2231 return;
2232 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002233
2234 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002235 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002236 {
2237 /* The job is dead, keep reading channel I/O until the channel is
2238 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002239 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002240 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2241 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002242 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002243 parse_queued_messages();
2244 ui_delay(10L, FALSE);
2245 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002246 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002247 parse_queued_messages();
2248 }
2249 else
2250 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002251 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002252 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002253
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002254 /* Wait for 10 msec for any channel I/O. */
2255 /* TODO: use delay from optional argument */
2256 ui_delay(10L, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002257 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002258
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002259 /* Flushing messages on channels is hopefully sufficient.
2260 * TODO: is there a better way? */
2261 parse_queued_messages();
2262 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002263}
2264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002265# ifdef WIN3264
2266
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002267/**************************************
2268 * 2. MS-Windows implementation.
2269 */
2270
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002271#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2272#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2273
Bram Moolenaar8a773062017-07-24 22:29:21 +02002274void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002275void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002276void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002277BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2278void (*winpty_config_set_initial_size)(void*, int, int);
2279LPCWSTR (*winpty_conin_name)(void*);
2280LPCWSTR (*winpty_conout_name)(void*);
2281LPCWSTR (*winpty_conerr_name)(void*);
2282void (*winpty_free)(void*);
2283void (*winpty_config_free)(void*);
2284void (*winpty_spawn_config_free)(void*);
2285void (*winpty_error_free)(void*);
2286LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002287BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002288HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002289
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002290#define WINPTY_DLL "winpty.dll"
2291
2292static HINSTANCE hWinPtyDLL = NULL;
2293
2294 int
2295dyn_winpty_init(void)
2296{
2297 int i;
2298 static struct
2299 {
2300 char *name;
2301 FARPROC *ptr;
2302 } winpty_entry[] =
2303 {
2304 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2305 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2306 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2307 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2308 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2309 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2310 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2311 {"winpty_free", (FARPROC*)&winpty_free},
2312 {"winpty_open", (FARPROC*)&winpty_open},
2313 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2314 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2315 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2316 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002317 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002318 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002319 {NULL, NULL}
2320 };
2321
2322 /* No need to initialize twice. */
2323 if (hWinPtyDLL)
2324 return 1;
2325 /* Load winpty.dll */
2326 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2327 if (!hWinPtyDLL)
2328 {
2329 EMSG2(_(e_loadlib), WINPTY_DLL);
2330 return 0;
2331 }
2332 for (i = 0; winpty_entry[i].name != NULL
2333 && winpty_entry[i].ptr != NULL; ++i)
2334 {
2335 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2336 winpty_entry[i].name)) == NULL)
2337 {
2338 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2339 return 0;
2340 }
2341 }
2342
2343 return 1;
2344}
2345
2346/*
2347 * Create a new terminal of "rows" by "cols" cells.
2348 * Store a reference in "term".
2349 * Return OK or FAIL.
2350 */
2351 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002352term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002353{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002354 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002355 channel_T *channel = NULL;
2356 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002357 DWORD error;
2358 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2359 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002360 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002361 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002362
2363 if (!dyn_winpty_init())
2364 return FAIL;
2365
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002366 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002367 if (p == NULL)
2368 return FAIL;
2369
2370 job = job_alloc();
2371 if (job == NULL)
2372 goto failed;
2373
2374 channel = add_channel();
2375 if (channel == NULL)
2376 goto failed;
2377
2378 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2379 if (term->tl_winpty_config == NULL)
2380 goto failed;
2381
2382 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2383 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2384 if (term->tl_winpty == NULL)
2385 goto failed;
2386
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002387 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002388 spawn_config = winpty_spawn_config_new(
2389 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2390 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2391 NULL,
2392 p,
2393 NULL,
2394 NULL,
2395 &winpty_err);
2396 if (spawn_config == NULL)
2397 goto failed;
2398
2399 channel = add_channel();
2400 if (channel == NULL)
2401 goto failed;
2402
2403 job = job_alloc();
2404 if (job == NULL)
2405 goto failed;
2406
2407 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2408 &child_thread_handle, &error, &winpty_err))
2409 goto failed;
2410
2411 channel_set_pipes(channel,
2412 (sock_T) CreateFileW(
2413 winpty_conin_name(term->tl_winpty),
2414 GENERIC_WRITE, 0, NULL,
2415 OPEN_EXISTING, 0, NULL),
2416 (sock_T) CreateFileW(
2417 winpty_conout_name(term->tl_winpty),
2418 GENERIC_READ, 0, NULL,
2419 OPEN_EXISTING, 0, NULL),
2420 (sock_T) CreateFileW(
2421 winpty_conerr_name(term->tl_winpty),
2422 GENERIC_READ, 0, NULL,
2423 OPEN_EXISTING, 0, NULL));
2424
2425 jo = CreateJobObject(NULL, NULL);
2426 if (jo == NULL)
2427 goto failed;
2428
2429 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002430 {
2431 /* Failed, switch the way to terminate process with TerminateProcess. */
2432 CloseHandle(jo);
2433 jo = NULL;
2434 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002435
2436 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002437 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002438
2439 create_vterm(term, rows, cols);
2440
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002441 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002442 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002443
2444 job->jv_channel = channel;
2445 job->jv_proc_info.hProcess = child_process_handle;
2446 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2447 job->jv_job_object = jo;
2448 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002449 sprintf(buf, "winpty://%lu",
2450 GetProcessId(winpty_agent_process(term->tl_winpty)));
2451 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002452 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002453 term->tl_job = job;
2454
2455 return OK;
2456
2457failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002458 if (spawn_config != NULL)
2459 winpty_spawn_config_free(spawn_config);
2460 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002461 if (channel != NULL)
2462 channel_clear(channel);
2463 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002464 {
2465 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002466 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002467 }
2468 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002469 if (jo != NULL)
2470 CloseHandle(jo);
2471 if (term->tl_winpty != NULL)
2472 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002473 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002474 if (term->tl_winpty_config != NULL)
2475 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002476 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002477 if (winpty_err != NULL)
2478 {
2479 char_u *msg = utf16_to_enc(
2480 (short_u *)winpty_error_msg(winpty_err), NULL);
2481
2482 EMSG(msg);
2483 winpty_error_free(winpty_err);
2484 }
2485 return FAIL;
2486}
2487
2488/*
2489 * Free the terminal emulator part of "term".
2490 */
2491 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002492term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002493{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002494 if (term->tl_winpty != NULL)
2495 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002496 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002497 if (term->tl_winpty_config != NULL)
2498 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002499 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002500 if (term->tl_vterm != NULL)
2501 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002502 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002503}
2504
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002505/*
2506 * Request size to terminal.
2507 */
2508 static void
2509term_report_winsize(term_T *term, int rows, int cols)
2510{
2511 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2512}
2513
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002514# else
2515
2516/**************************************
2517 * 3. Unix-like implementation.
2518 */
2519
2520/*
2521 * Create a new terminal of "rows" by "cols" cells.
2522 * Start job for "cmd".
2523 * Store the pointers in "term".
2524 * Return OK or FAIL.
2525 */
2526 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002527term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002528{
2529 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002530
2531 create_vterm(term, rows, cols);
2532
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002533 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002534 argvars[0].v_type = VAR_STRING;
2535 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002536
2537 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002538 if (term->tl_job != NULL)
2539 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002540
Bram Moolenaar61a66052017-07-22 18:39:00 +02002541 return term->tl_job != NULL
2542 && term->tl_job->jv_channel != NULL
2543 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002544}
2545
2546/*
2547 * Free the terminal emulator part of "term".
2548 */
2549 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002550term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002551{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002552 if (term->tl_vterm != NULL)
2553 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002554 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002555}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002556
2557/*
2558 * Request size to terminal.
2559 */
2560 static void
2561term_report_winsize(term_T *term, int rows, int cols)
2562{
2563 /* Use an ioctl() to report the new window size to the job. */
2564 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2565 {
2566 int fd = -1;
2567 int part;
2568
2569 for (part = PART_OUT; part < PART_COUNT; ++part)
2570 {
2571 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2572 if (isatty(fd))
2573 break;
2574 }
2575 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
2576 mch_stop_job(term->tl_job, (char_u *)"winch");
2577 }
2578}
2579
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002580# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002581
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002582#endif /* FEAT_TERMINAL */