blob: 6d01a299c318224d468be237fffc267ed809d3aa [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 Moolenaar3cd43cc2017-08-12 19:51:41 +020039 * - cursor shape/color/blink in the GUI
Bram Moolenaardd693ce2017-08-10 23:15:19 +020040 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020041 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020042 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020043 * For the GUI fill termios with default values, perhaps like pangoterm:
44 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
45 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020046 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020047 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020048 * - add 't' to mode()
Bram Moolenaard8dc1792017-08-03 11:55:21 +020049 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020051 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
52 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020053 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020055 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020057 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020058 * terminal. Allow:
59 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
60 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
61 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
62 * Check that something is connected to the terminal.
63 * Test: "cat" reading from a file or buffer
64 * "ls" writing stdout to a file or buffer
65 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020066 * - support ":term NONE" to open a terminal with a pty but not running a job
67 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020068 * - if the job in the terminal does not support the mouse, we can use the
69 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
71 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020072 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020073 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020074 * - Copy text in the vterm to the Vim buffer once in a while, so that
75 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076 */
77
78#include "vim.h"
79
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020080#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
Bram Moolenaard5310982017-08-05 15:16:32 +020082#ifndef MIN
83# define MIN(x,y) ((x) < (y) ? (x) : (y))
84#endif
85#ifndef MAX
86# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089#include "libvterm/include/vterm.h"
90
Bram Moolenaar33a43be2017-08-06 21:36:22 +020091/* This is VTermScreenCell without the characters, thus much smaller. */
92typedef struct {
93 VTermScreenCellAttrs attrs;
94 char width;
95 VTermColor fg, bg;
96} cellattr_T;
97
Bram Moolenaard85f2712017-07-28 21:51:57 +020098typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020099 int sb_cols; /* can differ per line */
100 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200101} sb_line_T;
102
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200103/* typedef term_T in structs.h */
104struct terminal_S {
105 term_T *tl_next;
106
Bram Moolenaar423802d2017-07-30 16:52:24 +0200107 VTerm *tl_vterm;
108 job_T *tl_job;
109 buf_T *tl_buffer;
110
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200111 /* used when tl_job is NULL and only a pty was created */
112 int tl_tty_fd;
113 char_u *tl_tty_name;
114
Bram Moolenaar6d819742017-08-06 14:57:49 +0200115 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200116 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200117 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200118 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200119
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200120#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200121 void *tl_winpty_config;
122 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125 /* last known vterm size */
126 int tl_rows;
127 int tl_cols;
128 /* vterm size does not follow window size */
129 int tl_rows_fixed;
130 int tl_cols_fixed;
131
Bram Moolenaar21554412017-07-24 21:44:43 +0200132 char_u *tl_title; /* NULL or allocated */
133 char_u *tl_status_text; /* NULL or allocated */
134
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200135 /* Range of screen rows to update. Zero based. */
136 int tl_dirty_row_start; /* -1 if nothing dirty */
137 int tl_dirty_row_end; /* row below last one to update */
138
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200140 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200142 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200143 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200144 int tl_cursor_blink;
145 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
146 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200147
148 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149};
150
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200151#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
152#define TMODE_LOOP 2 /* CTRL-W N used */
153
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154/*
155 * List of all active terminals.
156 */
157static term_T *first_term = NULL;
158
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200159/* Terminal active in terminal_loop(). */
160static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
162#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
163#define KEY_BUF_LEN 200
164
165/*
166 * Functions with separate implementation for MS-Windows and Unix-like systems.
167 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200168static 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 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200240 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
241 opt->jo_term_rows = rows;
242 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
243 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200244}
245
246 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200247term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 exarg_T split_ea;
250 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200252 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253
254 if (check_restricted() || check_secure())
255 return;
256
257 term = (term_T *)alloc_clear(sizeof(term_T));
258 if (term == NULL)
259 return;
260 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200261 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200262 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200263 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200264 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200269 /* Create a new buffer in the current window. */
270 if (!can_abandon(curbuf, forceit))
271 {
272 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200273 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200274 return;
275 }
276 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
277 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 {
279 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200280 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200281 }
282 }
283 else if (opt->jo_hidden)
284 {
285 buf_T *buf;
286
287 /* Create a new buffer without a window. Make it the current buffer for
288 * a moment to be able to do the initialisations. */
289 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
290 BLN_NEW | BLN_LISTED);
291 if (buf == NULL || ml_open(buf) == FAIL)
292 {
293 vim_free(term);
294 return;
295 }
296 old_curbuf = curbuf;
297 --curbuf->b_nwindows;
298 curbuf = buf;
299 curwin->w_buffer = buf;
300 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200304 /* Open a new window or tab. */
305 split_ea.cmdidx = CMD_new;
306 split_ea.cmd = (char_u *)"new";
307 split_ea.arg = (char_u *)"";
308 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
309 {
310 split_ea.line2 = opt->jo_term_rows;
311 split_ea.addr_count = 1;
312 }
313 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
314 {
315 split_ea.line2 = opt->jo_term_cols;
316 split_ea.addr_count = 1;
317 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200318
Bram Moolenaarda43b612017-08-11 22:27:50 +0200319 ex_splitview(&split_ea);
320 if (curwin == old_curwin)
321 {
322 /* split failed */
323 vim_free(term);
324 return;
325 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200327 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200330 if (!opt->jo_hidden)
331 {
332 /* only one size was taken care of with :new, do the other one */
333 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
334 win_setheight(opt->jo_term_rows);
335 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
336 win_setwidth(opt->jo_term_cols);
337 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200338
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340 term->tl_next = first_term;
341 first_term = term;
342
Bram Moolenaar293424c2017-07-26 23:11:01 +0200343 if (cmd == NULL || *cmd == NUL)
344 cmd = p_sh;
345
Bram Moolenaar78712a72017-08-05 14:50:12 +0200346 if (opt->jo_term_name != NULL)
347 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
348 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200349 {
350 int i;
351 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200352 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200353
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200354 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200355 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200356 /* Prepend a ! to the command name to avoid the buffer name equals
357 * the executable, otherwise ":w!" would overwrite it. */
358 if (i == 0)
359 vim_snprintf((char *)p, len, "!%s", cmd);
360 else
361 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362 if (buflist_findname(p) == NULL)
363 {
364 curbuf->b_ffname = p;
365 break;
366 }
367 }
368 }
369 curbuf->b_fname = curbuf->b_ffname;
370
Bram Moolenaar37c45832017-08-12 16:01:04 +0200371 if (opt->jo_term_opencmd != NULL)
372 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
373
Bram Moolenaareb44a682017-08-03 22:44:55 +0200374 set_string_option_direct((char_u *)"buftype", -1,
375 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
376
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200377 /* Mark the buffer as not modifiable. It can only be made modifiable after
378 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200379 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200380
381 /* Set 'bufhidden' to "hide": allow closing the window. */
382 set_string_option_direct((char_u *)"bufhidden", -1,
383 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200384
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200385 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200386 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200387
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200388 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200389 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200390 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200391 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200392 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200393 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200394
395 if (old_curbuf != NULL)
396 {
397 --curbuf->b_nwindows;
398 curbuf = old_curbuf;
399 curwin->w_buffer = curbuf;
400 ++curbuf->b_nwindows;
401 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200402 }
403 else
404 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200405 buf_T *buf = curbuf;
406
Bram Moolenaard85f2712017-07-28 21:51:57 +0200407 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200408 if (old_curbuf != NULL)
409 {
410 --curbuf->b_nwindows;
411 curbuf = old_curbuf;
412 curwin->w_buffer = curbuf;
413 ++curbuf->b_nwindows;
414 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200415
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200416 /* Wiping out the buffer will also close the window and call
417 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200418 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200419 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200420}
421
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200422/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200423 * ":terminal": open a terminal window and execute a job in it.
424 */
425 void
426ex_terminal(exarg_T *eap)
427{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 jobopt_T opt;
429 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200430
431 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200432
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 cmd = eap->arg;
434 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
435 {
436 char_u *p;
437
438 cmd += 2;
439 p = skiptowhite(cmd);
440 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
441 opt.jo_term_finish = 'c';
442 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
443 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200444 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
445 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200446 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
447 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448 else
449 {
450 if (*p)
451 *p = NUL;
452 EMSG2(_("E181: Invalid attribute: %s"), cmd);
453 return;
454 }
455 cmd = skipwhite(p);
456 }
457
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200458 if (eap->addr_count == 2)
459 {
460 opt.jo_term_rows = eap->line1;
461 opt.jo_term_cols = eap->line2;
462 }
463 else if (eap->addr_count == 1)
464 {
465 if (cmdmod.split & WSP_VERT)
466 opt.jo_term_cols = eap->line2;
467 else
468 opt.jo_term_rows = eap->line2;
469 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200470
Bram Moolenaarda43b612017-08-11 22:27:50 +0200471 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200472}
473
474/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200475 * Free the scrollback buffer for "term".
476 */
477 static void
478free_scrollback(term_T *term)
479{
480 int i;
481
482 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
483 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
484 ga_clear(&term->tl_scrollback);
485}
486
487/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200488 * Free a terminal and everything it refers to.
489 * Kills the job if there is one.
490 * Called when wiping out a buffer.
491 */
492 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200493free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200494{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200495 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200496 term_T *tp;
497
498 if (term == NULL)
499 return;
500 if (first_term == term)
501 first_term = term->tl_next;
502 else
503 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
504 if (tp->tl_next == term)
505 {
506 tp->tl_next = term->tl_next;
507 break;
508 }
509
510 if (term->tl_job != NULL)
511 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200512 if (term->tl_job->jv_status != JOB_ENDED
513 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200514 job_stop(term->tl_job, NULL, "kill");
515 job_unref(term->tl_job);
516 }
517
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200518 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200519
520 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200521 vim_free(term->tl_title);
522 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200523 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200524 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200525 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200526 buf->b_term = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200527}
528
529/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200530 * Write job output "msg[len]" to the vterm.
531 */
532 static void
533term_write_job_output(term_T *term, char_u *msg, size_t len)
534{
535 VTerm *vterm = term->tl_vterm;
536 char_u *p;
537 size_t done;
538 size_t len_now;
539
540 for (done = 0; done < len; done += len_now)
541 {
542 for (p = msg + done; p < msg + len; )
543 {
544 if (*p == NL)
545 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200546 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200547 }
548 len_now = p - msg - done;
549 vterm_input_write(vterm, (char *)msg + done, len_now);
550 if (p < msg + len && *p == NL)
551 {
552 /* Convert NL to CR-NL, that appears to work best. */
553 vterm_input_write(vterm, "\r\n", 2);
554 ++len_now;
555 }
556 }
557
558 /* this invokes the damage callbacks */
559 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
560}
561
Bram Moolenaar1c844932017-07-24 23:36:41 +0200562 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200563update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200564{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200565 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200566 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200567 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200568 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200569 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200570 if (term->tl_cursor_visible)
571 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200572 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200573#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200574 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200575 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200576#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200577 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200578}
579
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200580/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200581 * Invoked when "msg" output from a job was received. Write it to the terminal
582 * of "buffer".
583 */
584 void
585write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
586{
587 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200588 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200589
Bram Moolenaard85f2712017-07-28 21:51:57 +0200590 if (term->tl_vterm == NULL)
591 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200592 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200593 return;
594 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200595 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200596 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200597
Bram Moolenaar6d819742017-08-06 14:57:49 +0200598 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200599 {
600 /* TODO: only update once in a while. */
601 update_screen(0);
602 update_cursor(term, TRUE);
603 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200604}
605
606/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200607 * Send a mouse position and click to the vterm
608 */
609 static int
610term_send_mouse(VTerm *vterm, int button, int pressed)
611{
612 VTermModifier mod = VTERM_MOD_NONE;
613
614 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
615 mouse_col - W_WINCOL(curwin), mod);
616 vterm_mouse_button(vterm, button, pressed, mod);
617 return TRUE;
618}
619
620/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621 * Convert typed key "c" into bytes to send to the job.
622 * Return the number of bytes in "buf".
623 */
624 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200625term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200626{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200627 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200628 VTermKey key = VTERM_KEY_NONE;
629 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200630 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200631
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200632 switch (c)
633 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200634 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200635 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200636 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
637 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200638 case K_DEL: key = VTERM_KEY_DEL; break;
639 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200640 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
641 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200643 case K_S_END: mod = VTERM_MOD_SHIFT;
644 key = VTERM_KEY_END; break;
645 case K_C_END: mod = VTERM_MOD_CTRL;
646 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200647 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
648 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
649 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
650 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
651 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
652 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
653 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
654 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
655 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
656 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
657 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
658 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
659 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200660 case K_S_HOME: mod = VTERM_MOD_SHIFT;
661 key = VTERM_KEY_HOME; break;
662 case K_C_HOME: mod = VTERM_MOD_CTRL;
663 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200664 case K_INS: key = VTERM_KEY_INS; break;
665 case K_K0: key = VTERM_KEY_KP_0; break;
666 case K_K1: key = VTERM_KEY_KP_1; break;
667 case K_K2: key = VTERM_KEY_KP_2; break;
668 case K_K3: key = VTERM_KEY_KP_3; break;
669 case K_K4: key = VTERM_KEY_KP_4; break;
670 case K_K5: key = VTERM_KEY_KP_5; break;
671 case K_K6: key = VTERM_KEY_KP_6; break;
672 case K_K7: key = VTERM_KEY_KP_7; break;
673 case K_K8: key = VTERM_KEY_KP_8; break;
674 case K_K9: key = VTERM_KEY_KP_9; break;
675 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
676 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
677 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
678 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
679 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
680 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
681 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
682 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
683 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
684 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
685 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
686 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
687 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200688 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
689 key = VTERM_KEY_LEFT; break;
690 case K_C_LEFT: mod = VTERM_MOD_CTRL;
691 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200692 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
693 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
694 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200695 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
696 key = VTERM_KEY_RIGHT; break;
697 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
698 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200699 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200700 case K_S_UP: mod = VTERM_MOD_SHIFT;
701 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200703
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200704 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
705 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
706 case K_MOUSELEFT: /* TODO */ return 0;
707 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200708
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200709 case K_LEFTMOUSE:
710 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
711 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
712 case K_LEFTRELEASE:
713 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
714 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
715 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
716 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
717 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
718 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
719 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
720 case K_X1MOUSE: /* TODO */ return 0;
721 case K_X1DRAG: /* TODO */ return 0;
722 case K_X1RELEASE: /* TODO */ return 0;
723 case K_X2MOUSE: /* TODO */ return 0;
724 case K_X2DRAG: /* TODO */ return 0;
725 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200726
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200727 case K_IGNORE: return 0;
728 case K_NOP: return 0;
729 case K_UNDO: return 0;
730 case K_HELP: return 0;
731 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
732 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
733 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
734 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
735 case K_SELECT: return 0;
736#ifdef FEAT_GUI
737 case K_VER_SCROLLBAR: return 0;
738 case K_HOR_SCROLLBAR: return 0;
739#endif
740#ifdef FEAT_GUI_TABLINE
741 case K_TABLINE: return 0;
742 case K_TABMENU: return 0;
743#endif
744#ifdef FEAT_NETBEANS_INTG
745 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
746#endif
747#ifdef FEAT_DND
748 case K_DROP: return 0;
749#endif
750#ifdef FEAT_AUTOCMD
751 case K_CURSORHOLD: return 0;
752#endif
753 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
754 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200755 }
756
757 /*
758 * Convert special keys to vterm keys:
759 * - Write keys to vterm: vterm_keyboard_key()
760 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200761 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200762 */
763 if (key != VTERM_KEY_NONE)
764 /* Special key, let vterm convert it. */
765 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200766 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200767 /* Normal character, let vterm convert it. */
768 vterm_keyboard_unichar(vterm, c, mod);
769
770 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200771 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200772}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200773
Bram Moolenaar938783d2017-07-16 20:13:26 +0200774/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200775 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200776 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200777 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200778term_job_running(term_T *term)
779{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200780 /* Also consider the job finished when the channel is closed, to avoid a
781 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200782 return term != NULL
783 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200784 && term->tl_job->jv_status == JOB_STARTED
785 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200786}
787
788/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200789 * Add the last line of the scrollback buffer to the buffer in the window.
790 */
791 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200792add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200793{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200794 buf_T *buf = term->tl_buffer;
795 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
796 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200797
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200798 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200799 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200800 {
801 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200802 curbuf = buf;
803 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200804 curbuf = curwin->w_buffer;
805 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200806}
807
808/*
809 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200810 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200811 */
812 static void
813move_terminal_to_buffer(term_T *term)
814{
815 win_T *wp;
816 int len;
817 int lines_skipped = 0;
818 VTermPos pos;
819 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200820 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200821 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200822
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200823 if (term->tl_vterm == NULL)
824 return;
825 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200826 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
827 {
828 len = 0;
829 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
830 if (vterm_screen_get_cell(screen, pos, &cell) != 0
831 && cell.chars[0] != NUL)
832 len = pos.col + 1;
833
834 if (len == 0)
835 ++lines_skipped;
836 else
837 {
838 while (lines_skipped > 0)
839 {
840 /* Line was skipped, add an empty line. */
841 --lines_skipped;
842 if (ga_grow(&term->tl_scrollback, 1) == OK)
843 {
844 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
845 + term->tl_scrollback.ga_len;
846
847 line->sb_cols = 0;
848 line->sb_cells = NULL;
849 ++term->tl_scrollback.ga_len;
850
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200851 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200852 }
853 }
854
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200855 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
857 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200858 garray_T ga;
859 int width;
860 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 + term->tl_scrollback.ga_len;
862
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200863 ga_init2(&ga, 1, 100);
864 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200865 {
866 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200867 {
868 width = 1;
869 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
870 if (ga_grow(&ga, 1) == OK)
871 ga.ga_len += mb_char2bytes(' ',
872 (char_u *)ga.ga_data + ga.ga_len);
873 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200874 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200875 {
876 width = cell.width;
877
878 p[pos.col].width = cell.width;
879 p[pos.col].attrs = cell.attrs;
880 p[pos.col].fg = cell.fg;
881 p[pos.col].bg = cell.bg;
882
883 if (ga_grow(&ga, MB_MAXBYTES) == OK)
884 {
885 int i;
886 int c;
887
888 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
889 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
890 (char_u *)ga.ga_data + ga.ga_len);
891 }
892 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200893 }
894 line->sb_cols = len;
895 line->sb_cells = p;
896 ++term->tl_scrollback.ga_len;
897
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200898 if (ga_grow(&ga, 1) == FAIL)
899 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
900 else
901 {
902 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
903 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
904 }
905 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200906 }
907 else
908 vim_free(p);
909 }
910 }
911
912 FOR_ALL_WINDOWS(wp)
913 {
914 if (wp->w_buffer == term->tl_buffer)
915 {
916 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
917 wp->w_cursor.col = 0;
918 wp->w_valid = 0;
919 redraw_win_later(wp, NOT_VALID);
920 }
921 }
922}
923
924 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200925set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200926{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200927 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 vim_free(term->tl_status_text);
929 term->tl_status_text = NULL;
930 if (term->tl_buffer == curbuf)
931 maketitle();
932}
933
934/*
935 * Called after the job if finished and Terminal mode is not active:
936 * Move the vterm contents into the scrollback buffer and free the vterm.
937 */
938 static void
939cleanup_vterm(term_T *term)
940{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200941 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200942 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200943 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200944 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945}
946
947/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200948 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200949 * Suspends updating the terminal window.
950 */
951 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200952term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200953{
954 term_T *term = curbuf->b_term;
955
956 /* Append the current terminal contents to the buffer. */
957 move_terminal_to_buffer(term);
958
Bram Moolenaar6d819742017-08-06 14:57:49 +0200959 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200960
Bram Moolenaar6d819742017-08-06 14:57:49 +0200961 /* Move the window cursor to the position of the cursor in the
962 * terminal. */
963 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
964 + term->tl_cursor_pos.row + 1;
965 check_cursor();
966 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200967
Bram Moolenaar6d819742017-08-06 14:57:49 +0200968 /* Display the same lines as in the terminal. */
969 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970}
971
972/*
973 * Returns TRUE if the current window contains a terminal and we are in
974 * Terminal-Normal mode.
975 */
976 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200977term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978{
979 term_T *term = curbuf->b_term;
980
Bram Moolenaar6d819742017-08-06 14:57:49 +0200981 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982}
983
984/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200985 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 * Restores updating the terminal window.
987 */
988 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200989term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200990{
991 term_T *term = curbuf->b_term;
992 sb_line_T *line;
993 garray_T *gap;
994
995 /* Remove the terminal contents from the scrollback and the buffer. */
996 gap = &term->tl_scrollback;
997 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
998 {
999 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1000 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1001 vim_free(line->sb_cells);
1002 --gap->ga_len;
1003 if (gap->ga_len == 0)
1004 break;
1005 }
1006 check_cursor();
1007
Bram Moolenaar6d819742017-08-06 14:57:49 +02001008 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001009
1010 if (term->tl_channel_closed)
1011 cleanup_vterm(term);
1012 redraw_buf_and_status_later(curbuf, NOT_VALID);
1013}
1014
1015/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001016 * Get a key from the user without mapping.
1017 * TODO: use terminal mode mappings.
1018 */
1019 static int
1020term_vgetc()
1021{
1022 int c;
1023
1024 ++no_mapping;
1025 ++allow_keys;
1026 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001027#ifdef WIN3264
1028 ctrl_break_was_pressed = FALSE;
1029#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001030 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001031 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001032 --no_mapping;
1033 --allow_keys;
1034 return c;
1035}
1036
1037/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001038 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001039 * Return FAIL when the key needs to be handled in Normal mode.
1040 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001041 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001042 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001043send_keys_to_term(term_T *term, int c, int typed)
1044{
1045 char msg[KEY_BUF_LEN];
1046 size_t len;
1047 static int mouse_was_outside = FALSE;
1048 int dragging_outside = FALSE;
1049
1050 /* Catch keys that need to be handled as in Normal mode. */
1051 switch (c)
1052 {
1053 case NUL:
1054 case K_ZERO:
1055 if (typed)
1056 stuffcharReadbuff(c);
1057 return FAIL;
1058
1059 case K_IGNORE:
1060 return FAIL;
1061
1062 case K_LEFTDRAG:
1063 case K_MIDDLEDRAG:
1064 case K_RIGHTDRAG:
1065 case K_X1DRAG:
1066 case K_X2DRAG:
1067 dragging_outside = mouse_was_outside;
1068 /* FALLTHROUGH */
1069 case K_LEFTMOUSE:
1070 case K_LEFTMOUSE_NM:
1071 case K_LEFTRELEASE:
1072 case K_LEFTRELEASE_NM:
1073 case K_MIDDLEMOUSE:
1074 case K_MIDDLERELEASE:
1075 case K_RIGHTMOUSE:
1076 case K_RIGHTRELEASE:
1077 case K_X1MOUSE:
1078 case K_X1RELEASE:
1079 case K_X2MOUSE:
1080 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001081
1082 case K_MOUSEUP:
1083 case K_MOUSEDOWN:
1084 case K_MOUSELEFT:
1085 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001086 if (mouse_row < W_WINROW(curwin)
1087 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1088 || mouse_col < W_WINCOL(curwin)
1089 || mouse_col >= W_ENDCOL(curwin)
1090 || dragging_outside)
1091 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001092 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001093 if (typed)
1094 {
1095 stuffcharReadbuff(c);
1096 mouse_was_outside = TRUE;
1097 }
1098 return FAIL;
1099 }
1100 }
1101 if (typed)
1102 mouse_was_outside = FALSE;
1103
1104 /* Convert the typed key to a sequence of bytes for the job. */
1105 len = term_convert_key(term, c, msg);
1106 if (len > 0)
1107 /* TODO: if FAIL is returned, stop? */
1108 channel_send(term->tl_job->jv_channel, PART_IN,
1109 (char_u *)msg, (int)len, NULL);
1110
1111 return OK;
1112}
1113
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001114 static void
1115position_cursor(win_T *wp, VTermPos *pos)
1116{
1117 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1118 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1119 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1120}
1121
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001122/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001123 * Handle CTRL-W "": send register contents to the job.
1124 */
1125 static void
1126term_paste_register(int prev_c UNUSED)
1127{
1128 int c;
1129 list_T *l;
1130 listitem_T *item;
1131 long reglen = 0;
1132 int type;
1133
1134#ifdef FEAT_CMDL_INFO
1135 if (add_to_showcmd(prev_c))
1136 if (add_to_showcmd('"'))
1137 out_flush();
1138#endif
1139 c = term_vgetc();
1140#ifdef FEAT_CMDL_INFO
1141 clear_showcmd();
1142#endif
1143
1144 /* CTRL-W "= prompt for expression to evaluate. */
1145 if (c == '=' && get_expr_register() != '=')
1146 return;
1147
1148 l = (list_T *)get_reg_contents(c, GREG_LIST);
1149 if (l != NULL)
1150 {
1151 type = get_reg_type(c, &reglen);
1152 for (item = l->lv_first; item != NULL; item = item->li_next)
1153 {
1154 char_u *s = get_tv_string(&item->li_tv);
1155
1156 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1157 s, STRLEN(s), NULL);
1158 if (item->li_next != NULL || type == MLINE)
1159 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1160 (char_u *)"\r", 1, NULL);
1161 }
1162 list_free(l);
1163 }
1164}
1165
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001166#if defined(FEAT_GUI) || defined(PROTO)
1167/*
1168 * Return TRUE when the cursor of the terminal should be displayed.
1169 */
1170 int
1171use_terminal_cursor()
1172{
1173 return in_terminal_loop != NULL;
1174}
1175
1176 cursorentry_T *
1177term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1178{
1179 term_T *term = in_terminal_loop;
1180 static cursorentry_T entry;
1181
1182 vim_memset(&entry, 0, sizeof(entry));
1183 entry.shape = entry.mshape =
1184 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1185 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1186 SHAPE_BLOCK;
1187 entry.percentage = 20;
1188 if (term->tl_cursor_blink)
1189 {
1190 entry.blinkwait = 700;
1191 entry.blinkon = 400;
1192 entry.blinkon = 250;
1193 }
1194 *fg = gui.back_pixel;
1195 if (term->tl_cursor_color == NULL)
1196 *bg = gui.norm_pixel;
1197 else
1198 *bg = color_name2handle(term->tl_cursor_color);
1199 entry.name = "n";
1200 entry.used_for = SHAPE_CURSOR;
1201
1202 return &entry;
1203}
1204#endif
1205
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001206static int did_change_cursor = FALSE;
1207
1208 static void
1209may_set_cursor_props(term_T *term)
1210{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001211#ifdef FEAT_GUI
1212 /* For the GUI the cursor properties are obtained with
1213 * term_get_cursor_shape(). */
1214 if (gui.in_use)
1215 return;
1216#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001217 if (in_terminal_loop == term)
1218 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001219 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001220 if (term->tl_cursor_color != NULL)
1221 term_cursor_color(term->tl_cursor_color);
1222 else
1223 term_cursor_color((char_u *)"");
1224 /* do both blink and shape+blink, in case setting shape does not work */
1225 term_cursor_blink(term->tl_cursor_blink);
1226 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1227 }
1228}
1229
1230 static void
1231may_restore_cursor_props(void)
1232{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001233#ifdef FEAT_GUI
1234 if (gui.in_use)
1235 return;
1236#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001237 if (did_change_cursor)
1238 {
1239 did_change_cursor = FALSE;
1240 ui_cursor_shape_forced(TRUE);
1241 term_cursor_color((char_u *)"");
1242 term_cursor_blink(FALSE);
1243 }
1244}
1245
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001246/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001247 * Returns TRUE if the current window contains a terminal and we are sending
1248 * keys to the job.
1249 */
1250 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001251term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001252{
1253 term_T *term = curbuf->b_term;
1254
1255 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001256 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001257 && term->tl_vterm != NULL
1258 && term_job_running(term);
1259}
1260
1261/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001262 * Wait for input and send it to the job.
1263 * Return when the start of a CTRL-W command is typed or anything else that
1264 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001265 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1266 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001267 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001268 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001269terminal_loop(void)
1270{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001271 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001272 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001273 int ret;
1274
1275 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001276
1277 if (*curwin->w_p_tk != NUL)
1278 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001279 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001280 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001281
1282 for (;;)
1283 {
1284 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001285 /* Repeat redrawing in case a message is received while redrawing. */
1286 while (curwin->w_redr_type != 0)
1287 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001288 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001289
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001290 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001291 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001292 /* job finished while waiting for a character */
1293 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001294 if (c == K_IGNORE)
1295 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001296
Bram Moolenaarfae42832017-08-01 22:24:26 +02001297#ifdef UNIX
1298 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1299#endif
1300#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001301 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001302 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001303 if (ctrl_break_was_pressed)
1304 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001305#endif
1306
Bram Moolenaar69198192017-08-05 14:10:48 +02001307 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001308 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001309 int prev_c = c;
1310
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001311#ifdef FEAT_CMDL_INFO
1312 if (add_to_showcmd(c))
1313 out_flush();
1314#endif
1315 c = term_vgetc();
1316#ifdef FEAT_CMDL_INFO
1317 clear_showcmd();
1318#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001319 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001320 /* job finished while waiting for a character */
1321 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001322
Bram Moolenaar69198192017-08-05 14:10:48 +02001323 if (prev_c == Ctrl_BSL)
1324 {
1325 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001326 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001327 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1328 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001329 ret = FAIL;
1330 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001331 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001332 /* Send both keys to the terminal. */
1333 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1334 }
1335 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001336 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001337 /* "CTRL-W .": send CTRL-W to the job */
1338 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001339 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001340 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001341 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001342 /* CTRL-W N : go to Terminal-Normal mode. */
1343 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001344 ret = FAIL;
1345 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001346 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001347 else if (c == '"')
1348 {
1349 term_paste_register(prev_c);
1350 continue;
1351 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001352 else if (termkey == 0 || c != termkey)
1353 {
1354 stuffcharReadbuff(Ctrl_W);
1355 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001356 ret = OK;
1357 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001358 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001359 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001360 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001361 {
1362 ret = OK;
1363 goto theend;
1364 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001365 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001366 ret = FAIL;
1367
1368theend:
1369 in_terminal_loop = NULL;
1370 may_restore_cursor_props();
1371 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001372}
1373
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001374/*
1375 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001376 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001377 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001378 */
1379 void
1380term_job_ended(job_T *job)
1381{
1382 term_T *term;
1383 int did_one = FALSE;
1384
1385 for (term = first_term; term != NULL; term = term->tl_next)
1386 if (term->tl_job == job)
1387 {
1388 vim_free(term->tl_title);
1389 term->tl_title = NULL;
1390 vim_free(term->tl_status_text);
1391 term->tl_status_text = NULL;
1392 redraw_buf_and_status_later(term->tl_buffer, VALID);
1393 did_one = TRUE;
1394 }
1395 if (did_one)
1396 redraw_statuslines();
1397 if (curbuf->b_term != NULL)
1398 {
1399 if (curbuf->b_term->tl_job == job)
1400 maketitle();
1401 update_cursor(curbuf->b_term, TRUE);
1402 }
1403}
1404
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001406may_toggle_cursor(term_T *term)
1407{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001408 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001409 {
1410 if (term->tl_cursor_visible)
1411 cursor_on();
1412 else
1413 cursor_off();
1414 }
1415}
1416
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001417 static int
1418handle_damage(VTermRect rect, void *user)
1419{
1420 term_T *term = (term_T *)user;
1421
1422 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1423 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1424 redraw_buf_later(term->tl_buffer, NOT_VALID);
1425 return 1;
1426}
1427
1428 static int
1429handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1430{
1431 term_T *term = (term_T *)user;
1432
1433 /* TODO */
1434 redraw_buf_later(term->tl_buffer, NOT_VALID);
1435 return 1;
1436}
1437
1438 static int
1439handle_movecursor(
1440 VTermPos pos,
1441 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001442 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001443 void *user)
1444{
1445 term_T *term = (term_T *)user;
1446 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001447
1448 term->tl_cursor_pos = pos;
1449 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001450
1451 FOR_ALL_WINDOWS(wp)
1452 {
1453 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001454 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001455 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001456 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001457 {
1458 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001459 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001460 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001461
1462 return 1;
1463}
1464
Bram Moolenaar21554412017-07-24 21:44:43 +02001465 static int
1466handle_settermprop(
1467 VTermProp prop,
1468 VTermValue *value,
1469 void *user)
1470{
1471 term_T *term = (term_T *)user;
1472
1473 switch (prop)
1474 {
1475 case VTERM_PROP_TITLE:
1476 vim_free(term->tl_title);
1477 term->tl_title = vim_strsave((char_u *)value->string);
1478 vim_free(term->tl_status_text);
1479 term->tl_status_text = NULL;
1480 if (term == curbuf->b_term)
1481 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001482 break;
1483
1484 case VTERM_PROP_CURSORVISIBLE:
1485 term->tl_cursor_visible = value->boolean;
1486 may_toggle_cursor(term);
1487 out_flush();
1488 break;
1489
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001490 case VTERM_PROP_CURSORBLINK:
1491 term->tl_cursor_blink = value->boolean;
1492 may_set_cursor_props(term);
1493 break;
1494
1495 case VTERM_PROP_CURSORSHAPE:
1496 term->tl_cursor_shape = value->number;
1497 may_set_cursor_props(term);
1498 break;
1499
1500 case VTERM_PROP_CURSORCOLOR:
1501 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001502 if (*value->string == NUL)
1503 term->tl_cursor_color = NULL;
1504 else
1505 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001506 may_set_cursor_props(term);
1507 break;
1508
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001509 case VTERM_PROP_ALTSCREEN:
1510 /* TODO: do anything else? */
1511 term->tl_using_altscreen = value->boolean;
1512 break;
1513
Bram Moolenaar21554412017-07-24 21:44:43 +02001514 default:
1515 break;
1516 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001517 /* Always return 1, otherwise vterm doesn't store the value internally. */
1518 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001519}
1520
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521/*
1522 * The job running in the terminal resized the terminal.
1523 */
1524 static int
1525handle_resize(int rows, int cols, void *user)
1526{
1527 term_T *term = (term_T *)user;
1528 win_T *wp;
1529
1530 term->tl_rows = rows;
1531 term->tl_cols = cols;
1532 FOR_ALL_WINDOWS(wp)
1533 {
1534 if (wp->w_buffer == term->tl_buffer)
1535 {
1536 win_setheight_win(rows, wp);
1537 win_setwidth_win(cols, wp);
1538 }
1539 }
1540
1541 redraw_buf_later(term->tl_buffer, NOT_VALID);
1542 return 1;
1543}
1544
Bram Moolenaard85f2712017-07-28 21:51:57 +02001545/*
1546 * Handle a line that is pushed off the top of the screen.
1547 */
1548 static int
1549handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1550{
1551 term_T *term = (term_T *)user;
1552
1553 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001554 if (ga_grow(&term->tl_scrollback, 1) == OK)
1555 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001556 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001557 int len = 0;
1558 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001559 int c;
1560 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001561 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001562 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001563
1564 /* do not store empty cells at the end */
1565 for (i = 0; i < cols; ++i)
1566 if (cells[i].chars[0] != 0)
1567 len = i + 1;
1568
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001569 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001570 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001571 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001572 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001573 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001574 for (col = 0; col < len; col += cells[col].width)
1575 {
1576 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1577 {
1578 ga.ga_len = 0;
1579 break;
1580 }
1581 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1582 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1583 (char_u *)ga.ga_data + ga.ga_len);
1584 p[col].width = cells[col].width;
1585 p[col].attrs = cells[col].attrs;
1586 p[col].fg = cells[col].fg;
1587 p[col].bg = cells[col].bg;
1588 }
1589 }
1590 if (ga_grow(&ga, 1) == FAIL)
1591 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1592 else
1593 {
1594 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1595 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1596 }
1597 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001598
1599 line = (sb_line_T *)term->tl_scrollback.ga_data
1600 + term->tl_scrollback.ga_len;
1601 line->sb_cols = len;
1602 line->sb_cells = p;
1603 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001604 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001605 }
1606 return 0; /* ignored */
1607}
1608
Bram Moolenaar21554412017-07-24 21:44:43 +02001609static VTermScreenCallbacks screen_callbacks = {
1610 handle_damage, /* damage */
1611 handle_moverect, /* moverect */
1612 handle_movecursor, /* movecursor */
1613 handle_settermprop, /* settermprop */
1614 NULL, /* bell */
1615 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001616 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001617 NULL /* sb_popline */
1618};
1619
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001620/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001621 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001622 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001623 */
1624 void
1625term_channel_closed(channel_T *ch)
1626{
1627 term_T *term;
1628 int did_one = FALSE;
1629
1630 for (term = first_term; term != NULL; term = term->tl_next)
1631 if (term->tl_job == ch->ch_job)
1632 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001633 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001634 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001635
Bram Moolenaard85f2712017-07-28 21:51:57 +02001636 vim_free(term->tl_title);
1637 term->tl_title = NULL;
1638 vim_free(term->tl_status_text);
1639 term->tl_status_text = NULL;
1640
Bram Moolenaar423802d2017-07-30 16:52:24 +02001641 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001642 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001643 {
1644 int fnum = term->tl_buffer->b_fnum;
1645
Bram Moolenaar423802d2017-07-30 16:52:24 +02001646 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001647
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001648 if (term->tl_finish == 'c')
1649 {
1650 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001651 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001652 curbuf = term->tl_buffer;
1653 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1654 break;
1655 }
1656 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1657 {
1658 char buf[50];
1659
1660 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001661 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001662 vim_snprintf(buf, sizeof(buf),
1663 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001664 ? "botright sbuf %d"
1665 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001666 do_cmdline_cmd((char_u *)buf);
1667 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001668 else
1669 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001670 }
1671
Bram Moolenaard85f2712017-07-28 21:51:57 +02001672 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001673 }
1674 if (did_one)
1675 {
1676 redraw_statuslines();
1677
1678 /* Need to break out of vgetc(). */
1679 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001680 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001681
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001682 term = curbuf->b_term;
1683 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001684 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001685 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001686 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001687 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001688 }
1689 }
1690}
1691
1692/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001693 * Reverse engineer the RGB value into a cterm color index.
1694 * First color is 1. Return 0 if no match found.
1695 */
1696 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001697color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001698{
1699 int red = color->red;
1700 int blue = color->blue;
1701 int green = color->green;
1702
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001703 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001704 if (red == 0)
1705 {
1706 if (green == 0)
1707 {
1708 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001709 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001710 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001711 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001712 }
1713 else if (green == 224)
1714 {
1715 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001716 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001717 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001718 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001719 }
1720 }
1721 else if (red == 224)
1722 {
1723 if (green == 0)
1724 {
1725 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001726 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001727 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001728 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001729 }
1730 else if (green == 224)
1731 {
1732 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001733 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001734 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001735 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001736 }
1737 }
1738 else if (red == 128)
1739 {
1740 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001741 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001742 }
1743 else if (red == 255)
1744 {
1745 if (green == 64)
1746 {
1747 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001748 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001749 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001750 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001751 }
1752 else if (green == 255)
1753 {
1754 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001755 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001756 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001757 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001758 }
1759 }
1760 else if (red == 64)
1761 {
1762 if (green == 64)
1763 {
1764 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001765 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001766 }
1767 else if (green == 255)
1768 {
1769 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001770 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001771 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001772 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001773 }
1774 }
1775 if (t_colors >= 256)
1776 {
1777 if (red == blue && red == green)
1778 {
1779 /* 24-color greyscale */
1780 static int cutoff[23] = {
1781 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1782 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1783 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1784 int i;
1785
1786 for (i = 0; i < 23; ++i)
1787 if (red < cutoff[i])
1788 return i + 233;
1789 return 256;
1790 }
1791
1792 /* 216-color cube */
1793 return 17 + ((red + 25) / 0x33) * 36
1794 + ((green + 25) / 0x33) * 6
1795 + (blue + 25) / 0x33;
1796 }
1797 return 0;
1798}
1799
1800/*
1801 * Convert the attributes of a vterm cell into an attribute index.
1802 */
1803 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001804cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001805{
1806 int attr = 0;
1807
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001808 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001809 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001810 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001811 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001812 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001813 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001814 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001815 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001816 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001817 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001818
1819#ifdef FEAT_GUI
1820 if (gui.in_use)
1821 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001822 guicolor_T fg, bg;
1823
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001824 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1825 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001826 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001827 }
1828 else
1829#endif
1830#ifdef FEAT_TERMGUICOLORS
1831 if (p_tgc)
1832 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001833 guicolor_T fg, bg;
1834
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001835 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1836 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001837
1838 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001839 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001840 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001841#endif
1842 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001843 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001844 int fg = color2index(&cellfg, TRUE, &bold);
1845 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001846
1847 /* with 8 colors set the bold attribute to get a bright foreground */
1848 if (bold == TRUE)
1849 attr |= HL_BOLD;
1850 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001851 }
1852 return 0;
1853}
1854
1855/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001856 * Called to update a window that contains an active terminal.
1857 * Returns FAIL when there is no terminal running in this window or in
1858 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001859 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001860 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001861term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001862{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001863 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001864 VTerm *vterm;
1865 VTermScreen *screen;
1866 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001867 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001868
Bram Moolenaar6d819742017-08-06 14:57:49 +02001869 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001870 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001871
Bram Moolenaard85f2712017-07-28 21:51:57 +02001872 vterm = term->tl_vterm;
1873 screen = vterm_obtain_screen(vterm);
1874 state = vterm_obtain_state(vterm);
1875
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001876 /*
1877 * If the window was resized a redraw will be triggered and we get here.
1878 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1879 */
1880 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1881 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001882 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001883 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1884 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1885 win_T *twp;
1886
1887 FOR_ALL_WINDOWS(twp)
1888 {
1889 /* When more than one window shows the same terminal, use the
1890 * smallest size. */
1891 if (twp->w_buffer == term->tl_buffer)
1892 {
1893 if (!term->tl_rows_fixed && rows > twp->w_height)
1894 rows = twp->w_height;
1895 if (!term->tl_cols_fixed && cols > twp->w_width)
1896 cols = twp->w_width;
1897 }
1898 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001899
1900 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001901 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001902 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001903 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001904 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001905
1906 /* The cursor may have been moved when resizing. */
1907 vterm_state_get_cursorpos(state, &pos);
1908 position_cursor(wp, &pos);
1909
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001910 /* TODO: Only redraw what changed. */
1911 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001912 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001913 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001914 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001915
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001916 if (pos.row < term->tl_rows)
1917 {
1918 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001919 {
1920 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001921 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001922
Bram Moolenaareeac6772017-07-23 15:48:37 +02001923 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1924 vim_memset(&cell, 0, sizeof(cell));
1925
1926 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001927 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001928 if (c == NUL)
1929 {
1930 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001931#if defined(FEAT_MBYTE)
1932 if (enc_utf8)
1933 ScreenLinesUC[off] = NUL;
1934#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001935 }
1936 else
1937 {
1938#if defined(FEAT_MBYTE)
1939 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001940 {
1941 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001942 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001943 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001944 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001945 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001946 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001947 if (enc_utf8)
1948 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001949 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950#else
1951 ScreenLines[off] = c;
1952#endif
1953 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001954 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001955
1956 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001957 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001958 if (cell.width == 2)
1959 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001960 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001961#if defined(FEAT_MBYTE)
1962 if (enc_utf8)
1963 ScreenLinesUC[off] = NUL;
1964#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001965 ++pos.col;
1966 ++off;
1967 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001968 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001969 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001970 else
1971 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001972
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001973 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1974 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001975 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001976
1977 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001978}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001979
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001980/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001981 * Return TRUE if "wp" is a terminal window where the job has finished.
1982 */
1983 int
1984term_is_finished(buf_T *buf)
1985{
1986 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
1987}
1988
1989/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001990 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02001991 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001992 */
1993 int
1994term_show_buffer(buf_T *buf)
1995{
1996 term_T *term = buf->b_term;
1997
Bram Moolenaar6d819742017-08-06 14:57:49 +02001998 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001999}
2000
2001/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002002 * The current buffer is going to be changed. If there is terminal
2003 * highlighting remove it now.
2004 */
2005 void
2006term_change_in_curbuf(void)
2007{
2008 term_T *term = curbuf->b_term;
2009
2010 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2011 {
2012 free_scrollback(term);
2013 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002014
2015 /* The buffer is now like a normal buffer, it cannot be easily
2016 * abandoned when changed. */
2017 set_string_option_direct((char_u *)"buftype", -1,
2018 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002019 }
2020}
2021
2022/*
2023 * Get the screen attribute for a position in the buffer.
2024 */
2025 int
2026term_get_attr(buf_T *buf, linenr_T lnum, int col)
2027{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002028 term_T *term = buf->b_term;
2029 sb_line_T *line;
2030 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002031
Bram Moolenaar70229f92017-07-29 16:01:53 +02002032 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002033 return 0;
2034 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2035 if (col >= line->sb_cols)
2036 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002037 cellattr = line->sb_cells + col;
2038 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002039}
2040
2041/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002042 * Create a new vterm and initialize it.
2043 */
2044 static void
2045create_vterm(term_T *term, int rows, int cols)
2046{
2047 VTerm *vterm;
2048 VTermScreen *screen;
2049
2050 vterm = vterm_new(rows, cols);
2051 term->tl_vterm = vterm;
2052 screen = vterm_obtain_screen(vterm);
2053 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2054 /* TODO: depends on 'encoding'. */
2055 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002056
2057 /* Vterm uses a default black background. Set it to white when
2058 * 'background' is "light". */
2059 if (*p_bg == 'l')
2060 {
2061 VTermColor fg, bg;
2062
2063 fg.red = fg.green = fg.blue = 0;
2064 bg.red = bg.green = bg.blue = 255;
2065 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2066 }
2067
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002068 /* Required to initialize most things. */
2069 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002070
2071 /* Allow using alternate screen. */
2072 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002073}
2074
Bram Moolenaar21554412017-07-24 21:44:43 +02002075/*
2076 * Return the text to show for the buffer name and status.
2077 */
2078 char_u *
2079term_get_status_text(term_T *term)
2080{
2081 if (term->tl_status_text == NULL)
2082 {
2083 char_u *txt;
2084 size_t len;
2085
Bram Moolenaar6d819742017-08-06 14:57:49 +02002086 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002087 {
2088 if (term_job_running(term))
2089 txt = (char_u *)_("Terminal");
2090 else
2091 txt = (char_u *)_("Terminal-finished");
2092 }
2093 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002094 txt = term->tl_title;
2095 else if (term_job_running(term))
2096 txt = (char_u *)_("running");
2097 else
2098 txt = (char_u *)_("finished");
2099 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002100 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002101 if (term->tl_status_text != NULL)
2102 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2103 term->tl_buffer->b_fname, txt);
2104 }
2105 return term->tl_status_text;
2106}
2107
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002108/*
2109 * Mark references in jobs of terminals.
2110 */
2111 int
2112set_ref_in_term(int copyID)
2113{
2114 int abort = FALSE;
2115 term_T *term;
2116 typval_T tv;
2117
2118 for (term = first_term; term != NULL; term = term->tl_next)
2119 if (term->tl_job != NULL)
2120 {
2121 tv.v_type = VAR_JOB;
2122 tv.vval.v_job = term->tl_job;
2123 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2124 }
2125 return abort;
2126}
2127
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002128/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002129 * Get the buffer from the first argument in "argvars".
2130 * Returns NULL when the buffer is not for a terminal window.
2131 */
2132 static buf_T *
2133term_get_buf(typval_T *argvars)
2134{
2135 buf_T *buf;
2136
2137 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2138 ++emsg_off;
2139 buf = get_buf_tv(&argvars[0], FALSE);
2140 --emsg_off;
2141 if (buf == NULL || buf->b_term == NULL)
2142 return NULL;
2143 return buf;
2144}
2145
2146/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002147 * "term_getaltscreen(buf)" function
2148 */
2149 void
2150f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2151{
2152 buf_T *buf = term_get_buf(argvars);
2153
2154 if (buf == NULL)
2155 return;
2156 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2157}
2158
2159/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002160 * "term_getattr(attr, name)" function
2161 */
2162 void
2163f_term_getattr(typval_T *argvars, typval_T *rettv)
2164{
2165 int attr;
2166 size_t i;
2167 char_u *name;
2168
2169 static struct {
2170 char *name;
2171 int attr;
2172 } attrs[] = {
2173 {"bold", HL_BOLD},
2174 {"italic", HL_ITALIC},
2175 {"underline", HL_UNDERLINE},
2176 {"strike", HL_STANDOUT},
2177 {"reverse", HL_INVERSE},
2178 };
2179
2180 attr = get_tv_number(&argvars[0]);
2181 name = get_tv_string_chk(&argvars[1]);
2182 if (name == NULL)
2183 return;
2184
2185 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2186 if (STRCMP(name, attrs[i].name) == 0)
2187 {
2188 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2189 break;
2190 }
2191}
2192
2193/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002194 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002195 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002196 void
2197f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002198{
Bram Moolenaar97870002017-07-30 18:28:38 +02002199 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002200 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002201 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002202 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002203
Bram Moolenaar97870002017-07-30 18:28:38 +02002204 if (rettv_list_alloc(rettv) == FAIL)
2205 return;
2206 if (buf == NULL)
2207 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002208 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002209
2210 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002211 list_append_number(l, term->tl_cursor_pos.row + 1);
2212 list_append_number(l, term->tl_cursor_pos.col + 1);
2213
2214 d = dict_alloc();
2215 if (d != NULL)
2216 {
2217 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2218 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2219 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2220 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2221 ? (char_u *)"" : term->tl_cursor_color);
2222 list_append_dict(l, d);
2223 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002224}
2225
2226/*
2227 * "term_getjob(buf)" function
2228 */
2229 void
2230f_term_getjob(typval_T *argvars, typval_T *rettv)
2231{
2232 buf_T *buf = term_get_buf(argvars);
2233
2234 rettv->v_type = VAR_JOB;
2235 rettv->vval.v_job = NULL;
2236 if (buf == NULL)
2237 return;
2238
2239 rettv->vval.v_job = buf->b_term->tl_job;
2240 if (rettv->vval.v_job != NULL)
2241 ++rettv->vval.v_job->jv_refcount;
2242}
2243
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002244 static int
2245get_row_number(typval_T *tv, term_T *term)
2246{
2247 if (tv->v_type == VAR_STRING
2248 && tv->vval.v_string != NULL
2249 && STRCMP(tv->vval.v_string, ".") == 0)
2250 return term->tl_cursor_pos.row;
2251 return (int)get_tv_number(tv) - 1;
2252}
2253
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002254/*
2255 * "term_getline(buf, row)" function
2256 */
2257 void
2258f_term_getline(typval_T *argvars, typval_T *rettv)
2259{
2260 buf_T *buf = term_get_buf(argvars);
2261 term_T *term;
2262 int row;
2263
2264 rettv->v_type = VAR_STRING;
2265 if (buf == NULL)
2266 return;
2267 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002268 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002269
2270 if (term->tl_vterm == NULL)
2271 {
2272 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2273
2274 /* vterm is finished, get the text from the buffer */
2275 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2276 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2277 }
2278 else
2279 {
2280 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2281 VTermRect rect;
2282 int len;
2283 char_u *p;
2284
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002285 if (row < 0 || row >= term->tl_rows)
2286 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002287 len = term->tl_cols * MB_MAXBYTES + 1;
2288 p = alloc(len);
2289 if (p == NULL)
2290 return;
2291 rettv->vval.v_string = p;
2292
2293 rect.start_col = 0;
2294 rect.end_col = term->tl_cols;
2295 rect.start_row = row;
2296 rect.end_row = row + 1;
2297 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2298 }
2299}
2300
2301/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002302 * "term_getscrolled(buf)" function
2303 */
2304 void
2305f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2306{
2307 buf_T *buf = term_get_buf(argvars);
2308
2309 if (buf == NULL)
2310 return;
2311 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2312}
2313
2314/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002315 * "term_getsize(buf)" function
2316 */
2317 void
2318f_term_getsize(typval_T *argvars, typval_T *rettv)
2319{
2320 buf_T *buf = term_get_buf(argvars);
2321 list_T *l;
2322
2323 if (rettv_list_alloc(rettv) == FAIL)
2324 return;
2325 if (buf == NULL)
2326 return;
2327
2328 l = rettv->vval.v_list;
2329 list_append_number(l, buf->b_term->tl_rows);
2330 list_append_number(l, buf->b_term->tl_cols);
2331}
2332
2333/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002334 * "term_getstatus(buf)" function
2335 */
2336 void
2337f_term_getstatus(typval_T *argvars, typval_T *rettv)
2338{
2339 buf_T *buf = term_get_buf(argvars);
2340 term_T *term;
2341 char_u val[100];
2342
2343 rettv->v_type = VAR_STRING;
2344 if (buf == NULL)
2345 return;
2346 term = buf->b_term;
2347
2348 if (term_job_running(term))
2349 STRCPY(val, "running");
2350 else
2351 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002352 if (term->tl_normal_mode)
2353 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002354 rettv->vval.v_string = vim_strsave(val);
2355}
2356
2357/*
2358 * "term_gettitle(buf)" function
2359 */
2360 void
2361f_term_gettitle(typval_T *argvars, typval_T *rettv)
2362{
2363 buf_T *buf = term_get_buf(argvars);
2364
2365 rettv->v_type = VAR_STRING;
2366 if (buf == NULL)
2367 return;
2368
2369 if (buf->b_term->tl_title != NULL)
2370 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2371}
2372
2373/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002374 * "term_gettty(buf)" function
2375 */
2376 void
2377f_term_gettty(typval_T *argvars, typval_T *rettv)
2378{
2379 buf_T *buf = term_get_buf(argvars);
2380 char_u *p;
2381
2382 rettv->v_type = VAR_STRING;
2383 if (buf == NULL)
2384 return;
2385 if (buf->b_term->tl_job != NULL)
2386 p = buf->b_term->tl_job->jv_tty_name;
2387 else
2388 p = buf->b_term->tl_tty_name;
2389 if (p != NULL)
2390 rettv->vval.v_string = vim_strsave(p);
2391}
2392
2393/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002394 * "term_list()" function
2395 */
2396 void
2397f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2398{
2399 term_T *tp;
2400 list_T *l;
2401
2402 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2403 return;
2404
2405 l = rettv->vval.v_list;
2406 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2407 if (tp != NULL && tp->tl_buffer != NULL)
2408 if (list_append_number(l,
2409 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2410 return;
2411}
2412
2413/*
2414 * "term_scrape(buf, row)" function
2415 */
2416 void
2417f_term_scrape(typval_T *argvars, typval_T *rettv)
2418{
2419 buf_T *buf = term_get_buf(argvars);
2420 VTermScreen *screen = NULL;
2421 VTermPos pos;
2422 list_T *l;
2423 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002424 char_u *p;
2425 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002426
2427 if (rettv_list_alloc(rettv) == FAIL)
2428 return;
2429 if (buf == NULL)
2430 return;
2431 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002432
2433 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002434 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002435
2436 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002437 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002438 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002439 p = NULL;
2440 line = NULL;
2441 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002442 else
2443 {
2444 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2445
2446 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2447 return;
2448 p = ml_get_buf(buf, lnum + 1, FALSE);
2449 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2450 }
2451
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002452 for (pos.col = 0; pos.col < term->tl_cols; )
2453 {
2454 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002455 int width;
2456 VTermScreenCellAttrs attrs;
2457 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002458 char_u rgb[8];
2459 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2460 int off = 0;
2461 int i;
2462
2463 if (screen == NULL)
2464 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002465 cellattr_T *cellattr;
2466 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002467
2468 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002469 if (pos.col >= line->sb_cols)
2470 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002471 cellattr = line->sb_cells + pos.col;
2472 width = cellattr->width;
2473 attrs = cellattr->attrs;
2474 fg = cellattr->fg;
2475 bg = cellattr->bg;
2476 len = MB_PTR2LEN(p);
2477 mch_memmove(mbs, p, len);
2478 mbs[len] = NUL;
2479 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002480 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002481 else
2482 {
2483 VTermScreenCell cell;
2484 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2485 break;
2486 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2487 {
2488 if (cell.chars[i] == 0)
2489 break;
2490 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2491 }
2492 mbs[off] = NUL;
2493 width = cell.width;
2494 attrs = cell.attrs;
2495 fg = cell.fg;
2496 bg = cell.bg;
2497 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002498 dcell = dict_alloc();
2499 list_append_dict(l, dcell);
2500
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002501 dict_add_nr_str(dcell, "chars", 0, mbs);
2502
2503 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002504 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002505 dict_add_nr_str(dcell, "fg", 0, rgb);
2506 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002507 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002508 dict_add_nr_str(dcell, "bg", 0, rgb);
2509
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002510 dict_add_nr_str(dcell, "attr",
2511 cell2attr(attrs, fg, bg), NULL);
2512 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002513
2514 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002515 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002516 ++pos.col;
2517 }
2518}
2519
2520/*
2521 * "term_sendkeys(buf, keys)" function
2522 */
2523 void
2524f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2525{
2526 buf_T *buf = term_get_buf(argvars);
2527 char_u *msg;
2528 term_T *term;
2529
2530 rettv->v_type = VAR_UNKNOWN;
2531 if (buf == NULL)
2532 return;
2533
2534 msg = get_tv_string_chk(&argvars[1]);
2535 if (msg == NULL)
2536 return;
2537 term = buf->b_term;
2538 if (term->tl_vterm == NULL)
2539 return;
2540
2541 while (*msg != NUL)
2542 {
2543 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2544 msg += MB_PTR2LEN(msg);
2545 }
2546
Bram Moolenaar6d819742017-08-06 14:57:49 +02002547 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002548 {
2549 /* TODO: only update once in a while. */
2550 update_screen(0);
2551 if (buf == curbuf)
2552 update_cursor(term, TRUE);
2553 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002554}
2555
2556/*
2557 * "term_start(command, options)" function
2558 */
2559 void
2560f_term_start(typval_T *argvars, typval_T *rettv)
2561{
2562 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002563 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002564
2565 if (cmd == NULL)
2566 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002567 init_job_options(&opt);
2568 /* TODO: allow more job options */
2569 if (argvars[1].v_type != VAR_UNKNOWN
2570 && get_job_options(&argvars[1], &opt,
2571 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002572 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002573 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002574 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002575 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002576 return;
2577
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002578 if (opt.jo_vertical)
2579 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002580 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002581
2582 if (curbuf->b_term != NULL)
2583 rettv->vval.v_number = curbuf->b_fnum;
2584}
2585
2586/*
2587 * "term_wait" function
2588 */
2589 void
2590f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2591{
2592 buf_T *buf = term_get_buf(argvars);
2593
2594 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002595 {
2596 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002597 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002598 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002599 if (buf->b_term->tl_job == NULL)
2600 {
2601 ch_log(NULL, "term_wait(): no job to wait for");
2602 return;
2603 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002604
2605 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002606 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002607 {
2608 /* The job is dead, keep reading channel I/O until the channel is
2609 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002610 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002611 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2612 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002613 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002614 parse_queued_messages();
2615 ui_delay(10L, FALSE);
2616 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002617 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002618 parse_queued_messages();
2619 }
2620 else
2621 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002622 long wait = 10L;
2623
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002624 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002625 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002626
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002627 /* Wait for some time for any channel I/O. */
2628 if (argvars[1].v_type != VAR_UNKNOWN)
2629 wait = get_tv_number(&argvars[1]);
2630 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002631 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002632
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002633 /* Flushing messages on channels is hopefully sufficient.
2634 * TODO: is there a better way? */
2635 parse_queued_messages();
2636 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002637}
2638
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002639# ifdef WIN3264
2640
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002641/**************************************
2642 * 2. MS-Windows implementation.
2643 */
2644
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002645#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2646#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2647
Bram Moolenaar8a773062017-07-24 22:29:21 +02002648void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002649void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002650void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002651BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2652void (*winpty_config_set_initial_size)(void*, int, int);
2653LPCWSTR (*winpty_conin_name)(void*);
2654LPCWSTR (*winpty_conout_name)(void*);
2655LPCWSTR (*winpty_conerr_name)(void*);
2656void (*winpty_free)(void*);
2657void (*winpty_config_free)(void*);
2658void (*winpty_spawn_config_free)(void*);
2659void (*winpty_error_free)(void*);
2660LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002661BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002662HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002663
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002664#define WINPTY_DLL "winpty.dll"
2665
2666static HINSTANCE hWinPtyDLL = NULL;
2667
2668 int
2669dyn_winpty_init(void)
2670{
2671 int i;
2672 static struct
2673 {
2674 char *name;
2675 FARPROC *ptr;
2676 } winpty_entry[] =
2677 {
2678 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2679 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2680 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2681 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2682 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2683 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2684 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2685 {"winpty_free", (FARPROC*)&winpty_free},
2686 {"winpty_open", (FARPROC*)&winpty_open},
2687 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2688 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2689 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2690 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002691 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002692 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002693 {NULL, NULL}
2694 };
2695
2696 /* No need to initialize twice. */
2697 if (hWinPtyDLL)
2698 return 1;
2699 /* Load winpty.dll */
2700 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2701 if (!hWinPtyDLL)
2702 {
2703 EMSG2(_(e_loadlib), WINPTY_DLL);
2704 return 0;
2705 }
2706 for (i = 0; winpty_entry[i].name != NULL
2707 && winpty_entry[i].ptr != NULL; ++i)
2708 {
2709 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2710 winpty_entry[i].name)) == NULL)
2711 {
2712 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2713 return 0;
2714 }
2715 }
2716
2717 return 1;
2718}
2719
2720/*
2721 * Create a new terminal of "rows" by "cols" cells.
2722 * Store a reference in "term".
2723 * Return OK or FAIL.
2724 */
2725 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002726term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002727{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002728 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002729 channel_T *channel = NULL;
2730 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002731 DWORD error;
2732 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2733 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002734 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002735 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002736
2737 if (!dyn_winpty_init())
2738 return FAIL;
2739
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002740 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002741 if (p == NULL)
2742 return FAIL;
2743
2744 job = job_alloc();
2745 if (job == NULL)
2746 goto failed;
2747
2748 channel = add_channel();
2749 if (channel == NULL)
2750 goto failed;
2751
2752 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2753 if (term->tl_winpty_config == NULL)
2754 goto failed;
2755
2756 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2757 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2758 if (term->tl_winpty == NULL)
2759 goto failed;
2760
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002761 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002762 spawn_config = winpty_spawn_config_new(
2763 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2764 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2765 NULL,
2766 p,
2767 NULL,
2768 NULL,
2769 &winpty_err);
2770 if (spawn_config == NULL)
2771 goto failed;
2772
2773 channel = add_channel();
2774 if (channel == NULL)
2775 goto failed;
2776
2777 job = job_alloc();
2778 if (job == NULL)
2779 goto failed;
2780
2781 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2782 &child_thread_handle, &error, &winpty_err))
2783 goto failed;
2784
2785 channel_set_pipes(channel,
2786 (sock_T) CreateFileW(
2787 winpty_conin_name(term->tl_winpty),
2788 GENERIC_WRITE, 0, NULL,
2789 OPEN_EXISTING, 0, NULL),
2790 (sock_T) CreateFileW(
2791 winpty_conout_name(term->tl_winpty),
2792 GENERIC_READ, 0, NULL,
2793 OPEN_EXISTING, 0, NULL),
2794 (sock_T) CreateFileW(
2795 winpty_conerr_name(term->tl_winpty),
2796 GENERIC_READ, 0, NULL,
2797 OPEN_EXISTING, 0, NULL));
2798
2799 jo = CreateJobObject(NULL, NULL);
2800 if (jo == NULL)
2801 goto failed;
2802
2803 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002804 {
2805 /* Failed, switch the way to terminate process with TerminateProcess. */
2806 CloseHandle(jo);
2807 jo = NULL;
2808 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002809
2810 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002811 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002812
2813 create_vterm(term, rows, cols);
2814
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002815 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002816 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002817
2818 job->jv_channel = channel;
2819 job->jv_proc_info.hProcess = child_process_handle;
2820 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2821 job->jv_job_object = jo;
2822 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002823 sprintf(buf, "winpty://%lu",
2824 GetProcessId(winpty_agent_process(term->tl_winpty)));
2825 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002826 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002827 term->tl_job = job;
2828
2829 return OK;
2830
2831failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002832 if (spawn_config != NULL)
2833 winpty_spawn_config_free(spawn_config);
2834 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002835 if (channel != NULL)
2836 channel_clear(channel);
2837 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002838 {
2839 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002840 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002841 }
2842 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843 if (jo != NULL)
2844 CloseHandle(jo);
2845 if (term->tl_winpty != NULL)
2846 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002847 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002848 if (term->tl_winpty_config != NULL)
2849 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002850 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002851 if (winpty_err != NULL)
2852 {
2853 char_u *msg = utf16_to_enc(
2854 (short_u *)winpty_error_msg(winpty_err), NULL);
2855
2856 EMSG(msg);
2857 winpty_error_free(winpty_err);
2858 }
2859 return FAIL;
2860}
2861
2862/*
2863 * Free the terminal emulator part of "term".
2864 */
2865 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002866term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002867{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002868 if (term->tl_winpty != NULL)
2869 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002870 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002871 if (term->tl_winpty_config != NULL)
2872 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002873 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002874 if (term->tl_vterm != NULL)
2875 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002876 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002877}
2878
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002879/*
2880 * Request size to terminal.
2881 */
2882 static void
2883term_report_winsize(term_T *term, int rows, int cols)
2884{
2885 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2886}
2887
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002888# else
2889
2890/**************************************
2891 * 3. Unix-like implementation.
2892 */
2893
2894/*
2895 * Create a new terminal of "rows" by "cols" cells.
2896 * Start job for "cmd".
2897 * Store the pointers in "term".
2898 * Return OK or FAIL.
2899 */
2900 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002901term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002902{
2903 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002904
2905 create_vterm(term, rows, cols);
2906
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002907 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002908 argvars[0].v_type = VAR_STRING;
2909 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002910
2911 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002912 if (term->tl_job != NULL)
2913 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914
Bram Moolenaar61a66052017-07-22 18:39:00 +02002915 return term->tl_job != NULL
2916 && term->tl_job->jv_channel != NULL
2917 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918}
2919
2920/*
2921 * Free the terminal emulator part of "term".
2922 */
2923 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002924term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002925{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002926 if (term->tl_vterm != NULL)
2927 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002928 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002930
2931/*
2932 * Request size to terminal.
2933 */
2934 static void
2935term_report_winsize(term_T *term, int rows, int cols)
2936{
2937 /* Use an ioctl() to report the new window size to the job. */
2938 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2939 {
2940 int fd = -1;
2941 int part;
2942
2943 for (part = PART_OUT; part < PART_COUNT; ++part)
2944 {
2945 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2946 if (isatty(fd))
2947 break;
2948 }
2949 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002950 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002951 }
2952}
2953
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002955
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002956#endif /* FEAT_TERMINAL */