blob: 344622a21923fec6c699709b6ba38fe07bbc6d5b [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.
Bram Moolenaar679653e2017-08-13 14:13:19 +020037 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar63ecdda2017-07-28 22:29:35 +020039 *
Bram Moolenaare4f25e42017-07-07 11:54:15 +020040 * TODO:
Bram Moolenaarb2412082017-08-20 18:09:14 +020041 * - test writing lines to terminal job when implemented for MS-Windows
Bram Moolenaar94053a52017-08-01 21:44:33 +020042 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020043 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020044 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020046 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020047 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
48 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
49 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
50 * Check that something is connected to the terminal.
51 * Test: "cat" reading from a file or buffer
52 * "ls" writing stdout to a file or buffer
53 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020054 * - For the GUI fill termios with default values, perhaps like pangoterm:
55 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020056 * - support ":term NONE" to open a terminal with a pty but not running a job
57 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020058 * - if the job in the terminal does not support the mouse, we can use the
59 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
61 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020062 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020063 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020064 * - Copy text in the vterm to the Vim buffer once in a while, so that
65 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 */
67
68#include "vim.h"
69
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020070#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071
Bram Moolenaard5310982017-08-05 15:16:32 +020072#ifndef MIN
73# define MIN(x,y) ((x) < (y) ? (x) : (y))
74#endif
75#ifndef MAX
76# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaar33a43be2017-08-06 21:36:22 +020081/* This is VTermScreenCell without the characters, thus much smaller. */
82typedef struct {
83 VTermScreenCellAttrs attrs;
84 char width;
85 VTermColor fg, bg;
86} cellattr_T;
87
Bram Moolenaard85f2712017-07-28 21:51:57 +020088typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089 int sb_cols; /* can differ per line */
90 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020091} sb_line_T;
92
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093/* typedef term_T in structs.h */
94struct terminal_S {
95 term_T *tl_next;
96
Bram Moolenaar423802d2017-07-30 16:52:24 +020097 VTerm *tl_vterm;
98 job_T *tl_job;
99 buf_T *tl_buffer;
100
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200101 /* used when tl_job is NULL and only a pty was created */
102 int tl_tty_fd;
103 char_u *tl_tty_name;
104
Bram Moolenaar6d819742017-08-06 14:57:49 +0200105 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200106 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200107 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200108 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200110#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200111 void *tl_winpty_config;
112 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200113#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 /* last known vterm size */
116 int tl_rows;
117 int tl_cols;
118 /* vterm size does not follow window size */
119 int tl_rows_fixed;
120 int tl_cols_fixed;
121
Bram Moolenaar21554412017-07-24 21:44:43 +0200122 char_u *tl_title; /* NULL or allocated */
123 char_u *tl_status_text; /* NULL or allocated */
124
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125 /* Range of screen rows to update. Zero based. */
126 int tl_dirty_row_start; /* -1 if nothing dirty */
127 int tl_dirty_row_end; /* row below last one to update */
128
Bram Moolenaard85f2712017-07-28 21:51:57 +0200129 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200130 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200131
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200132 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200133 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200134 int tl_cursor_blink;
135 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
136 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200137
138 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139};
140
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200141#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
142#define TMODE_LOOP 2 /* CTRL-W N used */
143
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144/*
145 * List of all active terminals.
146 */
147static term_T *first_term = NULL;
148
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200149/* Terminal active in terminal_loop(). */
150static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200151
152#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
153#define KEY_BUF_LEN 200
154
155/*
156 * Functions with separate implementation for MS-Windows and Unix-like systems.
157 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200158static int term_and_job_init(term_T *term, int rows, int cols,
159 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200160static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200161static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200163/* The characters that we know (or assume) that the terminal expects for the
164 * backspace and enter keys. */
165static int term_backspace_char = BS;
166static int term_enter_char = CAR;
167static int term_nl_does_cr = FALSE;
168
169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200238 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
239 opt->jo_term_rows = rows;
240 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
241 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200242}
243
244 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200245term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200250 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200260 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200261 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200262 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200265 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 /* Create a new buffer in the current window. */
268 if (!can_abandon(curbuf, forceit))
269 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200270 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200271 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200272 return;
273 }
274 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
275 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200276 {
277 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200278 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 }
280 }
281 else if (opt->jo_hidden)
282 {
283 buf_T *buf;
284
285 /* Create a new buffer without a window. Make it the current buffer for
286 * a moment to be able to do the initialisations. */
287 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
288 BLN_NEW | BLN_LISTED);
289 if (buf == NULL || ml_open(buf) == FAIL)
290 {
291 vim_free(term);
292 return;
293 }
294 old_curbuf = curbuf;
295 --curbuf->b_nwindows;
296 curbuf = buf;
297 curwin->w_buffer = buf;
298 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200300 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 /* Open a new window or tab. */
303 split_ea.cmdidx = CMD_new;
304 split_ea.cmd = (char_u *)"new";
305 split_ea.arg = (char_u *)"";
306 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
307 {
308 split_ea.line2 = opt->jo_term_rows;
309 split_ea.addr_count = 1;
310 }
311 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
312 {
313 split_ea.line2 = opt->jo_term_cols;
314 split_ea.addr_count = 1;
315 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200316
Bram Moolenaarda43b612017-08-11 22:27:50 +0200317 ex_splitview(&split_ea);
318 if (curwin == old_curwin)
319 {
320 /* split failed */
321 vim_free(term);
322 return;
323 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200324 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200325 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200327
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200328 if (!opt->jo_hidden)
329 {
330 /* only one size was taken care of with :new, do the other one */
331 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
332 win_setheight(opt->jo_term_rows);
333 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
334 win_setwidth(opt->jo_term_cols);
335 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 term->tl_next = first_term;
339 first_term = term;
340
Bram Moolenaar78712a72017-08-05 14:50:12 +0200341 if (opt->jo_term_name != NULL)
342 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
343 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200344 {
345 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200346 size_t len;
347 char_u *cmd, *p;
348
349 if (argvar->v_type == VAR_STRING)
350 cmd = argvar->vval.v_string;
351 else if (argvar->v_type != VAR_LIST
352 || argvar->vval.v_list == NULL
353 || argvar->vval.v_list->lv_len < 1)
354 cmd = (char_u*)"";
355 else
356 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
357
358 len = STRLEN(cmd) + 10;
359 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200360
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200361 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200363 /* Prepend a ! to the command name to avoid the buffer name equals
364 * the executable, otherwise ":w!" would overwrite it. */
365 if (i == 0)
366 vim_snprintf((char *)p, len, "!%s", cmd);
367 else
368 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200369 if (buflist_findname(p) == NULL)
370 {
371 curbuf->b_ffname = p;
372 break;
373 }
374 }
375 }
376 curbuf->b_fname = curbuf->b_ffname;
377
Bram Moolenaar37c45832017-08-12 16:01:04 +0200378 if (opt->jo_term_opencmd != NULL)
379 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
380
Bram Moolenaareb44a682017-08-03 22:44:55 +0200381 set_string_option_direct((char_u *)"buftype", -1,
382 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
383
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200384 /* Mark the buffer as not modifiable. It can only be made modifiable after
385 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200386 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200387
388 /* Set 'bufhidden' to "hide": allow closing the window. */
389 set_string_option_direct((char_u *)"bufhidden", -1,
390 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200391
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200392 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200393 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200395 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200396 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
397 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200399 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200401 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200403 /* Make sure we don't get stuck on sending keys to the job, it leads to
404 * a deadlock if the job is waiting for Vim to read. */
405 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
406
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200407 if (old_curbuf != NULL)
408 {
409 --curbuf->b_nwindows;
410 curbuf = old_curbuf;
411 curwin->w_buffer = curbuf;
412 ++curbuf->b_nwindows;
413 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200414 }
415 else
416 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200417 buf_T *buf = curbuf;
418
Bram Moolenaard85f2712017-07-28 21:51:57 +0200419 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200420 if (old_curbuf != NULL)
421 {
422 --curbuf->b_nwindows;
423 curbuf = old_curbuf;
424 curwin->w_buffer = curbuf;
425 ++curbuf->b_nwindows;
426 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200427
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200428 /* Wiping out the buffer will also close the window and call
429 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200430 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200431 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200432}
433
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200434/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200435 * ":terminal": open a terminal window and execute a job in it.
436 */
437 void
438ex_terminal(exarg_T *eap)
439{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200440 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 jobopt_T opt;
442 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200443 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200444
445 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200446
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200447 cmd = eap->arg;
448 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
449 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200450 char_u *p, *ep;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200451
452 cmd += 2;
453 p = skiptowhite(cmd);
Bram Moolenaarb2412082017-08-20 18:09:14 +0200454 ep = vim_strchr(cmd, '=');
455 if (ep != NULL && ep < p)
456 p = ep;
457
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200458 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
459 opt.jo_term_finish = 'c';
460 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
461 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200462 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
463 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200464 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
465 opt.jo_hidden = 1;
Bram Moolenaarb2412082017-08-20 18:09:14 +0200466 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
467 && ep != NULL && isdigit(ep[1]))
468 {
469 opt.jo_set2 |= JO2_TERM_ROWS;
470 opt.jo_term_rows = atoi((char *)ep + 1);
471 p = skiptowhite(cmd);
472 }
473 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
474 && ep != NULL && isdigit(ep[1]))
475 {
476 opt.jo_set2 |= JO2_TERM_COLS;
477 opt.jo_term_cols = atoi((char *)ep + 1);
478 p = skiptowhite(cmd);
479 }
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200480 else
481 {
482 if (*p)
483 *p = NUL;
484 EMSG2(_("E181: Invalid attribute: %s"), cmd);
485 return;
486 }
487 cmd = skipwhite(p);
488 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200489 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200490 /* Make a copy, an autocommand may set 'shell'. */
491 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200492
Bram Moolenaarb2412082017-08-20 18:09:14 +0200493 if (eap->addr_count > 0)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200494 {
Bram Moolenaarb2412082017-08-20 18:09:14 +0200495 /* Write lines from current buffer to the job. */
496 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
497 opt.jo_io[PART_IN] = JIO_BUFFER;
498 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
499 opt.jo_in_top = eap->line1;
500 opt.jo_in_bot = eap->line2;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200501 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200502
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200503 argvar.v_type = VAR_STRING;
504 argvar.vval.v_string = cmd;
505 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200506 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200507}
508
509/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200510 * Free the scrollback buffer for "term".
511 */
512 static void
513free_scrollback(term_T *term)
514{
515 int i;
516
517 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
518 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
519 ga_clear(&term->tl_scrollback);
520}
521
522/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200523 * Free a terminal and everything it refers to.
524 * Kills the job if there is one.
525 * Called when wiping out a buffer.
526 */
527 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200528free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200529{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200530 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200531 term_T *tp;
532
533 if (term == NULL)
534 return;
535 if (first_term == term)
536 first_term = term->tl_next;
537 else
538 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
539 if (tp->tl_next == term)
540 {
541 tp->tl_next = term->tl_next;
542 break;
543 }
544
545 if (term->tl_job != NULL)
546 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200547 if (term->tl_job->jv_status != JOB_ENDED
548 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200549 job_stop(term->tl_job, NULL, "kill");
550 job_unref(term->tl_job);
551 }
552
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200553 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200554
555 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200556 vim_free(term->tl_title);
557 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200558 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200559 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200560 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200561 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200562 if (in_terminal_loop == term)
563 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200564}
565
566/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200567 * Write job output "msg[len]" to the vterm.
568 */
569 static void
570term_write_job_output(term_T *term, char_u *msg, size_t len)
571{
572 VTerm *vterm = term->tl_vterm;
573 char_u *p;
574 size_t done;
575 size_t len_now;
576
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200577 if (term_nl_does_cr)
578 vterm_input_write(vterm, (char *)msg, len);
579 else
580 /* need to convert NL to CR-NL */
581 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200583 for (p = msg + done; p < msg + len; )
584 {
585 if (*p == NL)
586 break;
587 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
588 }
589 len_now = p - msg - done;
590 vterm_input_write(vterm, (char *)msg + done, len_now);
591 if (p < msg + len && *p == NL)
592 {
593 vterm_input_write(vterm, "\r\n", 2);
594 ++len_now;
595 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200596 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200597
598 /* this invokes the damage callbacks */
599 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
600}
601
Bram Moolenaar1c844932017-07-24 23:36:41 +0200602 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200603update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200604{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200605 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200606 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200607 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200608 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200609 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200610 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200611 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200612 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200613#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200614 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200615 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200616#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200617 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200618}
619
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200620/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200621 * Invoked when "msg" output from a job was received. Write it to the terminal
622 * of "buffer".
623 */
624 void
625write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
626{
627 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200628 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200629
Bram Moolenaard85f2712017-07-28 21:51:57 +0200630 if (term->tl_vterm == NULL)
631 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200632 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200633 return;
634 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200635 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200636 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200637
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200638 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
639 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200640 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200641 {
642 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200643 ch_log(term->tl_job->jv_channel, "updating screen");
644 if (buffer == curbuf)
645 {
646 update_screen(0);
647 update_cursor(term, TRUE);
648 }
649 else
650 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200651 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200652}
653
654/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200655 * Send a mouse position and click to the vterm
656 */
657 static int
658term_send_mouse(VTerm *vterm, int button, int pressed)
659{
660 VTermModifier mod = VTERM_MOD_NONE;
661
662 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
663 mouse_col - W_WINCOL(curwin), mod);
664 vterm_mouse_button(vterm, button, pressed, mod);
665 return TRUE;
666}
667
668/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 * Convert typed key "c" into bytes to send to the job.
670 * Return the number of bytes in "buf".
671 */
672 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200673term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200674{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200675 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200676 VTermKey key = VTERM_KEY_NONE;
677 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200678 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200679
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200680 switch (c)
681 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200682 case CAR: c = term_enter_char; break;
683 /* don't use VTERM_KEY_BACKSPACE, it always
684 * becomes 0x7f DEL */
685 case K_BS: c = term_backspace_char; break;
686
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200687 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200688 case K_DEL: key = VTERM_KEY_DEL; break;
689 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200690 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
691 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200692 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200693 case K_S_END: mod = VTERM_MOD_SHIFT;
694 key = VTERM_KEY_END; break;
695 case K_C_END: mod = VTERM_MOD_CTRL;
696 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200697 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
698 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
699 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
700 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
701 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
702 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
703 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
704 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
705 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
706 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
707 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
708 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
709 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200710 case K_S_HOME: mod = VTERM_MOD_SHIFT;
711 key = VTERM_KEY_HOME; break;
712 case K_C_HOME: mod = VTERM_MOD_CTRL;
713 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200714 case K_INS: key = VTERM_KEY_INS; break;
715 case K_K0: key = VTERM_KEY_KP_0; break;
716 case K_K1: key = VTERM_KEY_KP_1; break;
717 case K_K2: key = VTERM_KEY_KP_2; break;
718 case K_K3: key = VTERM_KEY_KP_3; break;
719 case K_K4: key = VTERM_KEY_KP_4; break;
720 case K_K5: key = VTERM_KEY_KP_5; break;
721 case K_K6: key = VTERM_KEY_KP_6; break;
722 case K_K7: key = VTERM_KEY_KP_7; break;
723 case K_K8: key = VTERM_KEY_KP_8; break;
724 case K_K9: key = VTERM_KEY_KP_9; break;
725 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
726 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
727 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
728 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
729 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
730 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
731 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
732 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
733 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
734 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
735 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
736 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
737 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200738 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
739 key = VTERM_KEY_LEFT; break;
740 case K_C_LEFT: mod = VTERM_MOD_CTRL;
741 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200742 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
743 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
744 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200745 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
746 key = VTERM_KEY_RIGHT; break;
747 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
748 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200749 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200750 case K_S_UP: mod = VTERM_MOD_SHIFT;
751 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200752 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200753
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200754 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
755 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
756 case K_MOUSELEFT: /* TODO */ return 0;
757 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200758
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200759 case K_LEFTMOUSE:
760 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
761 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
762 case K_LEFTRELEASE:
763 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
764 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
765 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
766 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
767 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
768 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
769 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
770 case K_X1MOUSE: /* TODO */ return 0;
771 case K_X1DRAG: /* TODO */ return 0;
772 case K_X1RELEASE: /* TODO */ return 0;
773 case K_X2MOUSE: /* TODO */ return 0;
774 case K_X2DRAG: /* TODO */ return 0;
775 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200776
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200777 case K_IGNORE: return 0;
778 case K_NOP: return 0;
779 case K_UNDO: return 0;
780 case K_HELP: return 0;
781 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
782 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
783 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
784 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
785 case K_SELECT: return 0;
786#ifdef FEAT_GUI
787 case K_VER_SCROLLBAR: return 0;
788 case K_HOR_SCROLLBAR: return 0;
789#endif
790#ifdef FEAT_GUI_TABLINE
791 case K_TABLINE: return 0;
792 case K_TABMENU: return 0;
793#endif
794#ifdef FEAT_NETBEANS_INTG
795 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
796#endif
797#ifdef FEAT_DND
798 case K_DROP: return 0;
799#endif
800#ifdef FEAT_AUTOCMD
801 case K_CURSORHOLD: return 0;
802#endif
803 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
804 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200805 }
806
807 /*
808 * Convert special keys to vterm keys:
809 * - Write keys to vterm: vterm_keyboard_key()
810 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200811 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200812 */
813 if (key != VTERM_KEY_NONE)
814 /* Special key, let vterm convert it. */
815 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200816 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200817 /* Normal character, let vterm convert it. */
818 vterm_keyboard_unichar(vterm, c, mod);
819
820 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200821 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200822}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200823
Bram Moolenaar938783d2017-07-16 20:13:26 +0200824/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200825 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200826 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200827 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200828term_job_running(term_T *term)
829{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200830 /* Also consider the job finished when the channel is closed, to avoid a
831 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200832 return term != NULL
833 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200834 && term->tl_job->jv_status == JOB_STARTED
835 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200836}
837
838/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200839 * Add the last line of the scrollback buffer to the buffer in the window.
840 */
841 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200842add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200843{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200844 buf_T *buf = term->tl_buffer;
845 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
846 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200848 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200849 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200850 {
851 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200852 curbuf = buf;
853 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200854 curbuf = curwin->w_buffer;
855 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856}
857
858/*
859 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200860 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 */
862 static void
863move_terminal_to_buffer(term_T *term)
864{
865 win_T *wp;
866 int len;
867 int lines_skipped = 0;
868 VTermPos pos;
869 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200870 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200871 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200872
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200873 if (term->tl_vterm == NULL)
874 return;
875 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200876 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
877 {
878 len = 0;
879 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
880 if (vterm_screen_get_cell(screen, pos, &cell) != 0
881 && cell.chars[0] != NUL)
882 len = pos.col + 1;
883
884 if (len == 0)
885 ++lines_skipped;
886 else
887 {
888 while (lines_skipped > 0)
889 {
890 /* Line was skipped, add an empty line. */
891 --lines_skipped;
892 if (ga_grow(&term->tl_scrollback, 1) == OK)
893 {
894 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
895 + term->tl_scrollback.ga_len;
896
897 line->sb_cols = 0;
898 line->sb_cells = NULL;
899 ++term->tl_scrollback.ga_len;
900
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200901 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200902 }
903 }
904
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200905 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200906 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
907 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200908 garray_T ga;
909 int width;
910 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200911 + term->tl_scrollback.ga_len;
912
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200913 ga_init2(&ga, 1, 100);
914 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200915 {
916 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200917 {
918 width = 1;
919 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
920 if (ga_grow(&ga, 1) == OK)
921 ga.ga_len += mb_char2bytes(' ',
922 (char_u *)ga.ga_data + ga.ga_len);
923 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200924 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200925 {
926 width = cell.width;
927
928 p[pos.col].width = cell.width;
929 p[pos.col].attrs = cell.attrs;
930 p[pos.col].fg = cell.fg;
931 p[pos.col].bg = cell.bg;
932
933 if (ga_grow(&ga, MB_MAXBYTES) == OK)
934 {
935 int i;
936 int c;
937
938 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
939 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
940 (char_u *)ga.ga_data + ga.ga_len);
941 }
942 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200943 }
944 line->sb_cols = len;
945 line->sb_cells = p;
946 ++term->tl_scrollback.ga_len;
947
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200948 if (ga_grow(&ga, 1) == FAIL)
949 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
950 else
951 {
952 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
953 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
954 }
955 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956 }
957 else
958 vim_free(p);
959 }
960 }
961
962 FOR_ALL_WINDOWS(wp)
963 {
964 if (wp->w_buffer == term->tl_buffer)
965 {
966 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
967 wp->w_cursor.col = 0;
968 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200969 if (wp->w_cursor.lnum >= wp->w_height)
970 {
971 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
972
973 if (wp->w_topline < min_topline)
974 wp->w_topline = min_topline;
975 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200976 redraw_win_later(wp, NOT_VALID);
977 }
978 }
979}
980
981 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200982set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200984 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985 vim_free(term->tl_status_text);
986 term->tl_status_text = NULL;
987 if (term->tl_buffer == curbuf)
988 maketitle();
989}
990
991/*
992 * Called after the job if finished and Terminal mode is not active:
993 * Move the vterm contents into the scrollback buffer and free the vterm.
994 */
995 static void
996cleanup_vterm(term_T *term)
997{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200998 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200999 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001000 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001001 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001002}
1003
1004/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001005 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001006 * Suspends updating the terminal window.
1007 */
1008 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001009term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001010{
1011 term_T *term = curbuf->b_term;
1012
1013 /* Append the current terminal contents to the buffer. */
1014 move_terminal_to_buffer(term);
1015
Bram Moolenaar6d819742017-08-06 14:57:49 +02001016 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001017
Bram Moolenaar6d819742017-08-06 14:57:49 +02001018 /* Move the window cursor to the position of the cursor in the
1019 * terminal. */
1020 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1021 + term->tl_cursor_pos.row + 1;
1022 check_cursor();
1023 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001024
Bram Moolenaar6d819742017-08-06 14:57:49 +02001025 /* Display the same lines as in the terminal. */
1026 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001027}
1028
1029/*
1030 * Returns TRUE if the current window contains a terminal and we are in
1031 * Terminal-Normal mode.
1032 */
1033 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001034term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001035{
1036 term_T *term = curbuf->b_term;
1037
Bram Moolenaar6d819742017-08-06 14:57:49 +02001038 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001039}
1040
1041/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001042 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001043 * Restores updating the terminal window.
1044 */
1045 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001046term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001047{
1048 term_T *term = curbuf->b_term;
1049 sb_line_T *line;
1050 garray_T *gap;
1051
1052 /* Remove the terminal contents from the scrollback and the buffer. */
1053 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001054 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1055 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001056 {
1057 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1058 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1059 vim_free(line->sb_cells);
1060 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001061 }
1062 check_cursor();
1063
Bram Moolenaar6d819742017-08-06 14:57:49 +02001064 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001065
1066 if (term->tl_channel_closed)
1067 cleanup_vterm(term);
1068 redraw_buf_and_status_later(curbuf, NOT_VALID);
1069}
1070
1071/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001072 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001073 * Note: while waiting a terminal may be closed and freed if the channel is
1074 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001075 * TODO: use terminal mode mappings.
1076 */
1077 static int
1078term_vgetc()
1079{
1080 int c;
1081
1082 ++no_mapping;
1083 ++allow_keys;
1084 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001085#ifdef WIN3264
1086 ctrl_break_was_pressed = FALSE;
1087#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001088 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001089 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001090 --no_mapping;
1091 --allow_keys;
1092 return c;
1093}
1094
1095/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001096 * Get the part that is connected to the tty. Normally this is PART_IN, but
1097 * when writing buffer lines to the job it can be another. This makes it
1098 * possible to do "1,5term vim -".
1099 */
1100 static ch_part_T
1101get_tty_part(term_T *term)
1102{
1103#ifdef UNIX
1104 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1105 int i;
1106
1107 for (i = 0; i < 3; ++i)
1108 {
1109 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1110
1111 if (isatty(fd))
1112 return parts[i];
1113 }
1114#endif
1115 return PART_IN;
1116}
1117
1118/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001119 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001120 * Return FAIL when the key needs to be handled in Normal mode.
1121 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001122 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001123 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001124send_keys_to_term(term_T *term, int c, int typed)
1125{
1126 char msg[KEY_BUF_LEN];
1127 size_t len;
1128 static int mouse_was_outside = FALSE;
1129 int dragging_outside = FALSE;
1130
1131 /* Catch keys that need to be handled as in Normal mode. */
1132 switch (c)
1133 {
1134 case NUL:
1135 case K_ZERO:
1136 if (typed)
1137 stuffcharReadbuff(c);
1138 return FAIL;
1139
1140 case K_IGNORE:
1141 return FAIL;
1142
1143 case K_LEFTDRAG:
1144 case K_MIDDLEDRAG:
1145 case K_RIGHTDRAG:
1146 case K_X1DRAG:
1147 case K_X2DRAG:
1148 dragging_outside = mouse_was_outside;
1149 /* FALLTHROUGH */
1150 case K_LEFTMOUSE:
1151 case K_LEFTMOUSE_NM:
1152 case K_LEFTRELEASE:
1153 case K_LEFTRELEASE_NM:
1154 case K_MIDDLEMOUSE:
1155 case K_MIDDLERELEASE:
1156 case K_RIGHTMOUSE:
1157 case K_RIGHTRELEASE:
1158 case K_X1MOUSE:
1159 case K_X1RELEASE:
1160 case K_X2MOUSE:
1161 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001162
1163 case K_MOUSEUP:
1164 case K_MOUSEDOWN:
1165 case K_MOUSELEFT:
1166 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001167 if (mouse_row < W_WINROW(curwin)
1168 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1169 || mouse_col < W_WINCOL(curwin)
1170 || mouse_col >= W_ENDCOL(curwin)
1171 || dragging_outside)
1172 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001173 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001174 if (typed)
1175 {
1176 stuffcharReadbuff(c);
1177 mouse_was_outside = TRUE;
1178 }
1179 return FAIL;
1180 }
1181 }
1182 if (typed)
1183 mouse_was_outside = FALSE;
1184
1185 /* Convert the typed key to a sequence of bytes for the job. */
1186 len = term_convert_key(term, c, msg);
1187 if (len > 0)
1188 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001189 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1190 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001191
1192 return OK;
1193}
1194
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001195 static void
1196position_cursor(win_T *wp, VTermPos *pos)
1197{
1198 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1199 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1200 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1201}
1202
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001203/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001204 * Handle CTRL-W "": send register contents to the job.
1205 */
1206 static void
1207term_paste_register(int prev_c UNUSED)
1208{
1209 int c;
1210 list_T *l;
1211 listitem_T *item;
1212 long reglen = 0;
1213 int type;
1214
1215#ifdef FEAT_CMDL_INFO
1216 if (add_to_showcmd(prev_c))
1217 if (add_to_showcmd('"'))
1218 out_flush();
1219#endif
1220 c = term_vgetc();
1221#ifdef FEAT_CMDL_INFO
1222 clear_showcmd();
1223#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001224 if (!term_use_loop())
1225 /* job finished while waiting for a character */
1226 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001227
1228 /* CTRL-W "= prompt for expression to evaluate. */
1229 if (c == '=' && get_expr_register() != '=')
1230 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001231 if (!term_use_loop())
1232 /* job finished while waiting for a character */
1233 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001234
1235 l = (list_T *)get_reg_contents(c, GREG_LIST);
1236 if (l != NULL)
1237 {
1238 type = get_reg_type(c, &reglen);
1239 for (item = l->lv_first; item != NULL; item = item->li_next)
1240 {
1241 char_u *s = get_tv_string(&item->li_tv);
1242
1243 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1244 s, STRLEN(s), NULL);
1245 if (item->li_next != NULL || type == MLINE)
1246 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1247 (char_u *)"\r", 1, NULL);
1248 }
1249 list_free(l);
1250 }
1251}
1252
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001253#if defined(FEAT_GUI) || defined(PROTO)
1254/*
1255 * Return TRUE when the cursor of the terminal should be displayed.
1256 */
1257 int
1258use_terminal_cursor()
1259{
1260 return in_terminal_loop != NULL;
1261}
1262
1263 cursorentry_T *
1264term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1265{
1266 term_T *term = in_terminal_loop;
1267 static cursorentry_T entry;
1268
1269 vim_memset(&entry, 0, sizeof(entry));
1270 entry.shape = entry.mshape =
1271 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1272 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1273 SHAPE_BLOCK;
1274 entry.percentage = 20;
1275 if (term->tl_cursor_blink)
1276 {
1277 entry.blinkwait = 700;
1278 entry.blinkon = 400;
1279 entry.blinkon = 250;
1280 }
1281 *fg = gui.back_pixel;
1282 if (term->tl_cursor_color == NULL)
1283 *bg = gui.norm_pixel;
1284 else
1285 *bg = color_name2handle(term->tl_cursor_color);
1286 entry.name = "n";
1287 entry.used_for = SHAPE_CURSOR;
1288
1289 return &entry;
1290}
1291#endif
1292
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001293static int did_change_cursor = FALSE;
1294
1295 static void
1296may_set_cursor_props(term_T *term)
1297{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001298#ifdef FEAT_GUI
1299 /* For the GUI the cursor properties are obtained with
1300 * term_get_cursor_shape(). */
1301 if (gui.in_use)
1302 return;
1303#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001304 if (in_terminal_loop == term)
1305 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001306 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001307 if (term->tl_cursor_color != NULL)
1308 term_cursor_color(term->tl_cursor_color);
1309 else
1310 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001311 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1312 }
1313}
1314
1315 static void
1316may_restore_cursor_props(void)
1317{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001318#ifdef FEAT_GUI
1319 if (gui.in_use)
1320 return;
1321#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001322 if (did_change_cursor)
1323 {
1324 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001325 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001326 /* this will restore the initial cursor style, if possible */
1327 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001328 }
1329}
1330
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001331/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001332 * Returns TRUE if the current window contains a terminal and we are sending
1333 * keys to the job.
1334 */
1335 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001336term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001337{
1338 term_T *term = curbuf->b_term;
1339
1340 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001341 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001342 && term->tl_vterm != NULL
1343 && term_job_running(term);
1344}
1345
1346/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001347 * Wait for input and send it to the job.
1348 * Return when the start of a CTRL-W command is typed or anything else that
1349 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001350 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1351 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001353 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001354terminal_loop(void)
1355{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001356 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001357 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001358 int ret;
1359
Bram Moolenaar679653e2017-08-13 14:13:19 +02001360 /* Remember the terminal we are sending keys to. However, the terminal
1361 * might be closed while waiting for a character, e.g. typing "exit" in a
1362 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1363 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001364 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001365
1366 if (*curwin->w_p_tk != NUL)
1367 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001368 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001369 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001370
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001371#ifdef UNIX
1372 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001373 int part = get_tty_part(curbuf->b_term);
1374 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001375
1376 if (isatty(fd))
1377 {
1378 ttyinfo_T info;
1379
1380 /* Get the current backspace and enter characters of the pty. */
1381 if (get_tty_info(fd, &info) == OK)
1382 {
1383 term_backspace_char = info.backspace;
1384 term_enter_char = info.enter;
1385 term_nl_does_cr = info.nl_does_cr;
1386 }
1387 }
1388 }
1389#endif
1390
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001391 for (;;)
1392 {
1393 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001394 /* Repeat redrawing in case a message is received while redrawing. */
1395 while (curwin->w_redr_type != 0)
1396 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001397 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001398
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001399 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001400 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001401 /* job finished while waiting for a character */
1402 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001403 if (c == K_IGNORE)
1404 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001405
Bram Moolenaarfae42832017-08-01 22:24:26 +02001406#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001407 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001408 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001409 if (ctrl_break_was_pressed)
1410 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001411#endif
1412
Bram Moolenaar69198192017-08-05 14:10:48 +02001413 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001414 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001415 int prev_c = c;
1416
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001417#ifdef FEAT_CMDL_INFO
1418 if (add_to_showcmd(c))
1419 out_flush();
1420#endif
1421 c = term_vgetc();
1422#ifdef FEAT_CMDL_INFO
1423 clear_showcmd();
1424#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001425 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001426 /* job finished while waiting for a character */
1427 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001428
Bram Moolenaar69198192017-08-05 14:10:48 +02001429 if (prev_c == Ctrl_BSL)
1430 {
1431 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001432 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001433 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1434 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001435 ret = FAIL;
1436 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001437 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001438 /* Send both keys to the terminal. */
1439 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1440 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001441 else if (c == Ctrl_C)
1442 {
1443 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1444 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1445 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001446 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001447 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001448 /* "CTRL-W .": send CTRL-W to the job */
1449 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001450 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001451 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001452 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001453 /* CTRL-W N : go to Terminal-Normal mode. */
1454 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001455 ret = FAIL;
1456 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001457 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001458 else if (c == '"')
1459 {
1460 term_paste_register(prev_c);
1461 continue;
1462 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001463 else if (termkey == 0 || c != termkey)
1464 {
1465 stuffcharReadbuff(Ctrl_W);
1466 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001467 ret = OK;
1468 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001469 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001470 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001471 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001472 {
1473 ret = OK;
1474 goto theend;
1475 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001476 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001477 ret = FAIL;
1478
1479theend:
1480 in_terminal_loop = NULL;
1481 may_restore_cursor_props();
1482 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001483}
1484
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001485/*
1486 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001487 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001488 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001489 */
1490 void
1491term_job_ended(job_T *job)
1492{
1493 term_T *term;
1494 int did_one = FALSE;
1495
1496 for (term = first_term; term != NULL; term = term->tl_next)
1497 if (term->tl_job == job)
1498 {
1499 vim_free(term->tl_title);
1500 term->tl_title = NULL;
1501 vim_free(term->tl_status_text);
1502 term->tl_status_text = NULL;
1503 redraw_buf_and_status_later(term->tl_buffer, VALID);
1504 did_one = TRUE;
1505 }
1506 if (did_one)
1507 redraw_statuslines();
1508 if (curbuf->b_term != NULL)
1509 {
1510 if (curbuf->b_term->tl_job == job)
1511 maketitle();
1512 update_cursor(curbuf->b_term, TRUE);
1513 }
1514}
1515
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001516 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001517may_toggle_cursor(term_T *term)
1518{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001519 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001520 {
1521 if (term->tl_cursor_visible)
1522 cursor_on();
1523 else
1524 cursor_off();
1525 }
1526}
1527
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001528/*
1529 * Reverse engineer the RGB value into a cterm color index.
1530 * First color is 1. Return 0 if no match found.
1531 */
1532 static int
1533color2index(VTermColor *color, int fg, int *boldp)
1534{
1535 int red = color->red;
1536 int blue = color->blue;
1537 int green = color->green;
1538
1539 /* The argument for lookup_color() is for the color_names[] table. */
1540 if (red == 0)
1541 {
1542 if (green == 0)
1543 {
1544 if (blue == 0)
1545 return lookup_color(0, fg, boldp) + 1; /* black */
1546 if (blue == 224)
1547 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1548 }
1549 else if (green == 224)
1550 {
1551 if (blue == 0)
1552 return lookup_color(2, fg, boldp) + 1; /* dark green */
1553 if (blue == 224)
1554 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1555 }
1556 }
1557 else if (red == 224)
1558 {
1559 if (green == 0)
1560 {
1561 if (blue == 0)
1562 return lookup_color(4, fg, boldp) + 1; /* dark red */
1563 if (blue == 224)
1564 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1565 }
1566 else if (green == 224)
1567 {
1568 if (blue == 0)
1569 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1570 if (blue == 224)
1571 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1572 }
1573 }
1574 else if (red == 128)
1575 {
1576 if (green == 128 && blue == 128)
1577 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1578 }
1579 else if (red == 255)
1580 {
1581 if (green == 64)
1582 {
1583 if (blue == 64)
1584 return lookup_color(20, fg, boldp) + 1; /* light red */
1585 if (blue == 255)
1586 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1587 }
1588 else if (green == 255)
1589 {
1590 if (blue == 64)
1591 return lookup_color(24, fg, boldp) + 1; /* yellow */
1592 if (blue == 255)
1593 return lookup_color(26, fg, boldp) + 1; /* white */
1594 }
1595 }
1596 else if (red == 64)
1597 {
1598 if (green == 64)
1599 {
1600 if (blue == 255)
1601 return lookup_color(14, fg, boldp) + 1; /* light blue */
1602 }
1603 else if (green == 255)
1604 {
1605 if (blue == 64)
1606 return lookup_color(16, fg, boldp) + 1; /* light green */
1607 if (blue == 255)
1608 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1609 }
1610 }
1611 if (t_colors >= 256)
1612 {
1613 if (red == blue && red == green)
1614 {
1615 /* 24-color greyscale */
1616 static int cutoff[23] = {
1617 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1618 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1619 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1620 int i;
1621
1622 for (i = 0; i < 23; ++i)
1623 if (red < cutoff[i])
1624 return i + 233;
1625 return 256;
1626 }
1627
1628 /* 216-color cube */
1629 return 17 + ((red + 25) / 0x33) * 36
1630 + ((green + 25) / 0x33) * 6
1631 + (blue + 25) / 0x33;
1632 }
1633 return 0;
1634}
1635
1636/*
1637 * Convert the attributes of a vterm cell into an attribute index.
1638 */
1639 static int
1640cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1641{
1642 int attr = 0;
1643
1644 if (cellattrs.bold)
1645 attr |= HL_BOLD;
1646 if (cellattrs.underline)
1647 attr |= HL_UNDERLINE;
1648 if (cellattrs.italic)
1649 attr |= HL_ITALIC;
1650 if (cellattrs.strike)
1651 attr |= HL_STANDOUT;
1652 if (cellattrs.reverse)
1653 attr |= HL_INVERSE;
1654
1655#ifdef FEAT_GUI
1656 if (gui.in_use)
1657 {
1658 guicolor_T fg, bg;
1659
1660 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1661 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1662 return get_gui_attr_idx(attr, fg, bg);
1663 }
1664 else
1665#endif
1666#ifdef FEAT_TERMGUICOLORS
1667 if (p_tgc)
1668 {
1669 guicolor_T fg, bg;
1670
1671 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1672 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1673
1674 return get_tgc_attr_idx(attr, fg, bg);
1675 }
1676 else
1677#endif
1678 {
1679 int bold = MAYBE;
1680 int fg = color2index(&cellfg, TRUE, &bold);
1681 int bg = color2index(&cellbg, FALSE, &bold);
1682
1683 /* with 8 colors set the bold attribute to get a bright foreground */
1684 if (bold == TRUE)
1685 attr |= HL_BOLD;
1686 return get_cterm_attr_idx(attr, fg, bg);
1687 }
1688 return 0;
1689}
1690
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001691 static int
1692handle_damage(VTermRect rect, void *user)
1693{
1694 term_T *term = (term_T *)user;
1695
1696 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1697 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1698 redraw_buf_later(term->tl_buffer, NOT_VALID);
1699 return 1;
1700}
1701
1702 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001703handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001704{
1705 term_T *term = (term_T *)user;
1706
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001707 /* Scrolling up is done much more efficiently by deleting lines instead of
1708 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001709 if (dest.start_col == src.start_col
1710 && dest.end_col == src.end_col
1711 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001712 {
1713 win_T *wp;
1714 VTermColor fg, bg;
1715 VTermScreenCellAttrs attr;
1716 int clear_attr;
1717
1718 /* Set the color to clear lines with. */
1719 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1720 &fg, &bg);
1721 vim_memset(&attr, 0, sizeof(attr));
1722 clear_attr = cell2attr(attr, fg, bg);
1723
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001724 FOR_ALL_WINDOWS(wp)
1725 {
1726 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001727 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001728 src.start_row - dest.start_row, FALSE, FALSE,
1729 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001730 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001731 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001732 redraw_buf_later(term->tl_buffer, NOT_VALID);
1733 return 1;
1734}
1735
1736 static int
1737handle_movecursor(
1738 VTermPos pos,
1739 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001740 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001741 void *user)
1742{
1743 term_T *term = (term_T *)user;
1744 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001745
1746 term->tl_cursor_pos = pos;
1747 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001748
1749 FOR_ALL_WINDOWS(wp)
1750 {
1751 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001752 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001753 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001754 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001755 {
1756 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001757 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001758 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001759
1760 return 1;
1761}
1762
Bram Moolenaar21554412017-07-24 21:44:43 +02001763 static int
1764handle_settermprop(
1765 VTermProp prop,
1766 VTermValue *value,
1767 void *user)
1768{
1769 term_T *term = (term_T *)user;
1770
1771 switch (prop)
1772 {
1773 case VTERM_PROP_TITLE:
1774 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001775 /* a blank title isn't useful, make it empty, so that "running" is
1776 * displayed */
1777 if (*skipwhite((char_u *)value->string) == NUL)
1778 term->tl_title = NULL;
1779 else
1780 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001781 vim_free(term->tl_status_text);
1782 term->tl_status_text = NULL;
1783 if (term == curbuf->b_term)
1784 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001785 break;
1786
1787 case VTERM_PROP_CURSORVISIBLE:
1788 term->tl_cursor_visible = value->boolean;
1789 may_toggle_cursor(term);
1790 out_flush();
1791 break;
1792
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001793 case VTERM_PROP_CURSORBLINK:
1794 term->tl_cursor_blink = value->boolean;
1795 may_set_cursor_props(term);
1796 break;
1797
1798 case VTERM_PROP_CURSORSHAPE:
1799 term->tl_cursor_shape = value->number;
1800 may_set_cursor_props(term);
1801 break;
1802
1803 case VTERM_PROP_CURSORCOLOR:
1804 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001805 if (*value->string == NUL)
1806 term->tl_cursor_color = NULL;
1807 else
1808 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001809 may_set_cursor_props(term);
1810 break;
1811
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001812 case VTERM_PROP_ALTSCREEN:
1813 /* TODO: do anything else? */
1814 term->tl_using_altscreen = value->boolean;
1815 break;
1816
Bram Moolenaar21554412017-07-24 21:44:43 +02001817 default:
1818 break;
1819 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001820 /* Always return 1, otherwise vterm doesn't store the value internally. */
1821 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001822}
1823
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001824/*
1825 * The job running in the terminal resized the terminal.
1826 */
1827 static int
1828handle_resize(int rows, int cols, void *user)
1829{
1830 term_T *term = (term_T *)user;
1831 win_T *wp;
1832
1833 term->tl_rows = rows;
1834 term->tl_cols = cols;
1835 FOR_ALL_WINDOWS(wp)
1836 {
1837 if (wp->w_buffer == term->tl_buffer)
1838 {
1839 win_setheight_win(rows, wp);
1840 win_setwidth_win(cols, wp);
1841 }
1842 }
1843
1844 redraw_buf_later(term->tl_buffer, NOT_VALID);
1845 return 1;
1846}
1847
Bram Moolenaard85f2712017-07-28 21:51:57 +02001848/*
1849 * Handle a line that is pushed off the top of the screen.
1850 */
1851 static int
1852handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1853{
1854 term_T *term = (term_T *)user;
1855
1856 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001857 if (ga_grow(&term->tl_scrollback, 1) == OK)
1858 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001859 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001860 int len = 0;
1861 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001862 int c;
1863 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001864 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001865 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001866
1867 /* do not store empty cells at the end */
1868 for (i = 0; i < cols; ++i)
1869 if (cells[i].chars[0] != 0)
1870 len = i + 1;
1871
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001872 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001873 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001874 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001875 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001876 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001877 for (col = 0; col < len; col += cells[col].width)
1878 {
1879 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1880 {
1881 ga.ga_len = 0;
1882 break;
1883 }
1884 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1885 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1886 (char_u *)ga.ga_data + ga.ga_len);
1887 p[col].width = cells[col].width;
1888 p[col].attrs = cells[col].attrs;
1889 p[col].fg = cells[col].fg;
1890 p[col].bg = cells[col].bg;
1891 }
1892 }
1893 if (ga_grow(&ga, 1) == FAIL)
1894 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1895 else
1896 {
1897 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1898 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1899 }
1900 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001901
1902 line = (sb_line_T *)term->tl_scrollback.ga_data
1903 + term->tl_scrollback.ga_len;
1904 line->sb_cols = len;
1905 line->sb_cells = p;
1906 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001907 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001908 }
1909 return 0; /* ignored */
1910}
1911
Bram Moolenaar21554412017-07-24 21:44:43 +02001912static VTermScreenCallbacks screen_callbacks = {
1913 handle_damage, /* damage */
1914 handle_moverect, /* moverect */
1915 handle_movecursor, /* movecursor */
1916 handle_settermprop, /* settermprop */
1917 NULL, /* bell */
1918 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001919 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001920 NULL /* sb_popline */
1921};
1922
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001923/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001924 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001925 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001926 */
1927 void
1928term_channel_closed(channel_T *ch)
1929{
1930 term_T *term;
1931 int did_one = FALSE;
1932
1933 for (term = first_term; term != NULL; term = term->tl_next)
1934 if (term->tl_job == ch->ch_job)
1935 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001936 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001937 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001938
Bram Moolenaard85f2712017-07-28 21:51:57 +02001939 vim_free(term->tl_title);
1940 term->tl_title = NULL;
1941 vim_free(term->tl_status_text);
1942 term->tl_status_text = NULL;
1943
Bram Moolenaar423802d2017-07-30 16:52:24 +02001944 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001945 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001946 {
1947 int fnum = term->tl_buffer->b_fnum;
1948
Bram Moolenaar423802d2017-07-30 16:52:24 +02001949 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001950
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001951 if (term->tl_finish == 'c')
1952 {
1953 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001954 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001955 curbuf = term->tl_buffer;
1956 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1957 break;
1958 }
1959 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1960 {
1961 char buf[50];
1962
1963 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001964 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001965 vim_snprintf(buf, sizeof(buf),
1966 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001967 ? "botright sbuf %d"
1968 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001969 do_cmdline_cmd((char_u *)buf);
1970 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001971 else
1972 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001973 }
1974
Bram Moolenaard85f2712017-07-28 21:51:57 +02001975 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001976 }
1977 if (did_one)
1978 {
1979 redraw_statuslines();
1980
1981 /* Need to break out of vgetc(). */
1982 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001983 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001984
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001985 term = curbuf->b_term;
1986 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001987 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001988 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001989 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001990 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001991 }
1992 }
1993}
1994
1995/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001996 * Called to update a window that contains an active terminal.
1997 * Returns FAIL when there is no terminal running in this window or in
1998 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001999 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002000 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002001term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002002{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002003 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002004 VTerm *vterm;
2005 VTermScreen *screen;
2006 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002007 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002008
Bram Moolenaar6d819742017-08-06 14:57:49 +02002009 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002010 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002011
Bram Moolenaard85f2712017-07-28 21:51:57 +02002012 vterm = term->tl_vterm;
2013 screen = vterm_obtain_screen(vterm);
2014 state = vterm_obtain_state(vterm);
2015
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002016 /*
2017 * If the window was resized a redraw will be triggered and we get here.
2018 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2019 */
2020 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2021 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002022 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002023 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2024 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2025 win_T *twp;
2026
2027 FOR_ALL_WINDOWS(twp)
2028 {
2029 /* When more than one window shows the same terminal, use the
2030 * smallest size. */
2031 if (twp->w_buffer == term->tl_buffer)
2032 {
2033 if (!term->tl_rows_fixed && rows > twp->w_height)
2034 rows = twp->w_height;
2035 if (!term->tl_cols_fixed && cols > twp->w_width)
2036 cols = twp->w_width;
2037 }
2038 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002039
2040 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002041 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002042 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002043 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002044 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002045
2046 /* The cursor may have been moved when resizing. */
2047 vterm_state_get_cursorpos(state, &pos);
2048 position_cursor(wp, &pos);
2049
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002050 /* TODO: Only redraw what changed. */
2051 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002052 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002053 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002054 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002055
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002056 if (pos.row < term->tl_rows)
2057 {
2058 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002059 {
2060 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002061 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002062
Bram Moolenaareeac6772017-07-23 15:48:37 +02002063 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2064 vim_memset(&cell, 0, sizeof(cell));
2065
2066 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002067 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002068 if (c == NUL)
2069 {
2070 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002071#if defined(FEAT_MBYTE)
2072 if (enc_utf8)
2073 ScreenLinesUC[off] = NUL;
2074#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002075 }
2076 else
2077 {
2078#if defined(FEAT_MBYTE)
2079 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002080 {
2081 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002082 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002083 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002084 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002085 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002086 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002087 if (enc_utf8)
2088 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002089 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002090#else
2091 ScreenLines[off] = c;
2092#endif
2093 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002094 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002095
2096 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002097 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002098 if (cell.width == 2)
2099 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002100 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002101#if defined(FEAT_MBYTE)
2102 if (enc_utf8)
2103 ScreenLinesUC[off] = NUL;
2104#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002105 ++pos.col;
2106 ++off;
2107 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002108 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002109 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002110 else
2111 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002112
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002113 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2114 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002115 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002116
2117 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002118}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002120/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002121 * Return TRUE if "wp" is a terminal window where the job has finished.
2122 */
2123 int
2124term_is_finished(buf_T *buf)
2125{
2126 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2127}
2128
2129/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002130 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002131 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002132 */
2133 int
2134term_show_buffer(buf_T *buf)
2135{
2136 term_T *term = buf->b_term;
2137
Bram Moolenaar6d819742017-08-06 14:57:49 +02002138 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002139}
2140
2141/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002142 * The current buffer is going to be changed. If there is terminal
2143 * highlighting remove it now.
2144 */
2145 void
2146term_change_in_curbuf(void)
2147{
2148 term_T *term = curbuf->b_term;
2149
2150 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2151 {
2152 free_scrollback(term);
2153 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002154
2155 /* The buffer is now like a normal buffer, it cannot be easily
2156 * abandoned when changed. */
2157 set_string_option_direct((char_u *)"buftype", -1,
2158 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002159 }
2160}
2161
2162/*
2163 * Get the screen attribute for a position in the buffer.
2164 */
2165 int
2166term_get_attr(buf_T *buf, linenr_T lnum, int col)
2167{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002168 term_T *term = buf->b_term;
2169 sb_line_T *line;
2170 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002171
Bram Moolenaar70229f92017-07-29 16:01:53 +02002172 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002173 return 0;
2174 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2175 if (col >= line->sb_cols)
2176 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002177 cellattr = line->sb_cells + col;
2178 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002179}
2180
2181/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002182 * Create a new vterm and initialize it.
2183 */
2184 static void
2185create_vterm(term_T *term, int rows, int cols)
2186{
2187 VTerm *vterm;
2188 VTermScreen *screen;
2189
2190 vterm = vterm_new(rows, cols);
2191 term->tl_vterm = vterm;
2192 screen = vterm_obtain_screen(vterm);
2193 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2194 /* TODO: depends on 'encoding'. */
2195 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002196
2197 /* Vterm uses a default black background. Set it to white when
2198 * 'background' is "light". */
2199 if (*p_bg == 'l')
2200 {
2201 VTermColor fg, bg;
2202
2203 fg.red = fg.green = fg.blue = 0;
2204 bg.red = bg.green = bg.blue = 255;
2205 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2206 }
2207
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002208 /* Required to initialize most things. */
2209 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002210
2211 /* Allow using alternate screen. */
2212 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002213}
2214
Bram Moolenaar21554412017-07-24 21:44:43 +02002215/*
2216 * Return the text to show for the buffer name and status.
2217 */
2218 char_u *
2219term_get_status_text(term_T *term)
2220{
2221 if (term->tl_status_text == NULL)
2222 {
2223 char_u *txt;
2224 size_t len;
2225
Bram Moolenaar6d819742017-08-06 14:57:49 +02002226 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002227 {
2228 if (term_job_running(term))
2229 txt = (char_u *)_("Terminal");
2230 else
2231 txt = (char_u *)_("Terminal-finished");
2232 }
2233 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002234 txt = term->tl_title;
2235 else if (term_job_running(term))
2236 txt = (char_u *)_("running");
2237 else
2238 txt = (char_u *)_("finished");
2239 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002240 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002241 if (term->tl_status_text != NULL)
2242 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2243 term->tl_buffer->b_fname, txt);
2244 }
2245 return term->tl_status_text;
2246}
2247
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002248/*
2249 * Mark references in jobs of terminals.
2250 */
2251 int
2252set_ref_in_term(int copyID)
2253{
2254 int abort = FALSE;
2255 term_T *term;
2256 typval_T tv;
2257
2258 for (term = first_term; term != NULL; term = term->tl_next)
2259 if (term->tl_job != NULL)
2260 {
2261 tv.v_type = VAR_JOB;
2262 tv.vval.v_job = term->tl_job;
2263 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2264 }
2265 return abort;
2266}
2267
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002268/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002269 * Get the buffer from the first argument in "argvars".
2270 * Returns NULL when the buffer is not for a terminal window.
2271 */
2272 static buf_T *
2273term_get_buf(typval_T *argvars)
2274{
2275 buf_T *buf;
2276
2277 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2278 ++emsg_off;
2279 buf = get_buf_tv(&argvars[0], FALSE);
2280 --emsg_off;
2281 if (buf == NULL || buf->b_term == NULL)
2282 return NULL;
2283 return buf;
2284}
2285
2286/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002287 * "term_getaltscreen(buf)" function
2288 */
2289 void
2290f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2291{
2292 buf_T *buf = term_get_buf(argvars);
2293
2294 if (buf == NULL)
2295 return;
2296 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2297}
2298
2299/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300 * "term_getattr(attr, name)" function
2301 */
2302 void
2303f_term_getattr(typval_T *argvars, typval_T *rettv)
2304{
2305 int attr;
2306 size_t i;
2307 char_u *name;
2308
2309 static struct {
2310 char *name;
2311 int attr;
2312 } attrs[] = {
2313 {"bold", HL_BOLD},
2314 {"italic", HL_ITALIC},
2315 {"underline", HL_UNDERLINE},
2316 {"strike", HL_STANDOUT},
2317 {"reverse", HL_INVERSE},
2318 };
2319
2320 attr = get_tv_number(&argvars[0]);
2321 name = get_tv_string_chk(&argvars[1]);
2322 if (name == NULL)
2323 return;
2324
2325 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2326 if (STRCMP(name, attrs[i].name) == 0)
2327 {
2328 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2329 break;
2330 }
2331}
2332
2333/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002334 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002335 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002336 void
2337f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002338{
Bram Moolenaar97870002017-07-30 18:28:38 +02002339 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002340 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002341 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002342 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002343
Bram Moolenaar97870002017-07-30 18:28:38 +02002344 if (rettv_list_alloc(rettv) == FAIL)
2345 return;
2346 if (buf == NULL)
2347 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002348 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002349
2350 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002351 list_append_number(l, term->tl_cursor_pos.row + 1);
2352 list_append_number(l, term->tl_cursor_pos.col + 1);
2353
2354 d = dict_alloc();
2355 if (d != NULL)
2356 {
2357 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2358 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2359 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2360 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2361 ? (char_u *)"" : term->tl_cursor_color);
2362 list_append_dict(l, d);
2363 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002364}
2365
2366/*
2367 * "term_getjob(buf)" function
2368 */
2369 void
2370f_term_getjob(typval_T *argvars, typval_T *rettv)
2371{
2372 buf_T *buf = term_get_buf(argvars);
2373
2374 rettv->v_type = VAR_JOB;
2375 rettv->vval.v_job = NULL;
2376 if (buf == NULL)
2377 return;
2378
2379 rettv->vval.v_job = buf->b_term->tl_job;
2380 if (rettv->vval.v_job != NULL)
2381 ++rettv->vval.v_job->jv_refcount;
2382}
2383
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002384 static int
2385get_row_number(typval_T *tv, term_T *term)
2386{
2387 if (tv->v_type == VAR_STRING
2388 && tv->vval.v_string != NULL
2389 && STRCMP(tv->vval.v_string, ".") == 0)
2390 return term->tl_cursor_pos.row;
2391 return (int)get_tv_number(tv) - 1;
2392}
2393
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002394/*
2395 * "term_getline(buf, row)" function
2396 */
2397 void
2398f_term_getline(typval_T *argvars, typval_T *rettv)
2399{
2400 buf_T *buf = term_get_buf(argvars);
2401 term_T *term;
2402 int row;
2403
2404 rettv->v_type = VAR_STRING;
2405 if (buf == NULL)
2406 return;
2407 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002408 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002409
2410 if (term->tl_vterm == NULL)
2411 {
2412 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2413
2414 /* vterm is finished, get the text from the buffer */
2415 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2416 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2417 }
2418 else
2419 {
2420 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2421 VTermRect rect;
2422 int len;
2423 char_u *p;
2424
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002425 if (row < 0 || row >= term->tl_rows)
2426 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002427 len = term->tl_cols * MB_MAXBYTES + 1;
2428 p = alloc(len);
2429 if (p == NULL)
2430 return;
2431 rettv->vval.v_string = p;
2432
2433 rect.start_col = 0;
2434 rect.end_col = term->tl_cols;
2435 rect.start_row = row;
2436 rect.end_row = row + 1;
2437 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2438 }
2439}
2440
2441/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002442 * "term_getscrolled(buf)" function
2443 */
2444 void
2445f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2446{
2447 buf_T *buf = term_get_buf(argvars);
2448
2449 if (buf == NULL)
2450 return;
2451 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2452}
2453
2454/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002455 * "term_getsize(buf)" function
2456 */
2457 void
2458f_term_getsize(typval_T *argvars, typval_T *rettv)
2459{
2460 buf_T *buf = term_get_buf(argvars);
2461 list_T *l;
2462
2463 if (rettv_list_alloc(rettv) == FAIL)
2464 return;
2465 if (buf == NULL)
2466 return;
2467
2468 l = rettv->vval.v_list;
2469 list_append_number(l, buf->b_term->tl_rows);
2470 list_append_number(l, buf->b_term->tl_cols);
2471}
2472
2473/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002474 * "term_getstatus(buf)" function
2475 */
2476 void
2477f_term_getstatus(typval_T *argvars, typval_T *rettv)
2478{
2479 buf_T *buf = term_get_buf(argvars);
2480 term_T *term;
2481 char_u val[100];
2482
2483 rettv->v_type = VAR_STRING;
2484 if (buf == NULL)
2485 return;
2486 term = buf->b_term;
2487
2488 if (term_job_running(term))
2489 STRCPY(val, "running");
2490 else
2491 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002492 if (term->tl_normal_mode)
2493 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002494 rettv->vval.v_string = vim_strsave(val);
2495}
2496
2497/*
2498 * "term_gettitle(buf)" function
2499 */
2500 void
2501f_term_gettitle(typval_T *argvars, typval_T *rettv)
2502{
2503 buf_T *buf = term_get_buf(argvars);
2504
2505 rettv->v_type = VAR_STRING;
2506 if (buf == NULL)
2507 return;
2508
2509 if (buf->b_term->tl_title != NULL)
2510 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2511}
2512
2513/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002514 * "term_gettty(buf)" function
2515 */
2516 void
2517f_term_gettty(typval_T *argvars, typval_T *rettv)
2518{
2519 buf_T *buf = term_get_buf(argvars);
2520 char_u *p;
2521
2522 rettv->v_type = VAR_STRING;
2523 if (buf == NULL)
2524 return;
2525 if (buf->b_term->tl_job != NULL)
2526 p = buf->b_term->tl_job->jv_tty_name;
2527 else
2528 p = buf->b_term->tl_tty_name;
2529 if (p != NULL)
2530 rettv->vval.v_string = vim_strsave(p);
2531}
2532
2533/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002534 * "term_list()" function
2535 */
2536 void
2537f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2538{
2539 term_T *tp;
2540 list_T *l;
2541
2542 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2543 return;
2544
2545 l = rettv->vval.v_list;
2546 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2547 if (tp != NULL && tp->tl_buffer != NULL)
2548 if (list_append_number(l,
2549 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2550 return;
2551}
2552
2553/*
2554 * "term_scrape(buf, row)" function
2555 */
2556 void
2557f_term_scrape(typval_T *argvars, typval_T *rettv)
2558{
2559 buf_T *buf = term_get_buf(argvars);
2560 VTermScreen *screen = NULL;
2561 VTermPos pos;
2562 list_T *l;
2563 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002564 char_u *p;
2565 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002566
2567 if (rettv_list_alloc(rettv) == FAIL)
2568 return;
2569 if (buf == NULL)
2570 return;
2571 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002572
2573 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002574 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002575
2576 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002577 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002578 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002579 p = NULL;
2580 line = NULL;
2581 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002582 else
2583 {
2584 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2585
2586 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2587 return;
2588 p = ml_get_buf(buf, lnum + 1, FALSE);
2589 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2590 }
2591
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002592 for (pos.col = 0; pos.col < term->tl_cols; )
2593 {
2594 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002595 int width;
2596 VTermScreenCellAttrs attrs;
2597 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002598 char_u rgb[8];
2599 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2600 int off = 0;
2601 int i;
2602
2603 if (screen == NULL)
2604 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002605 cellattr_T *cellattr;
2606 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002607
2608 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002609 if (pos.col >= line->sb_cols)
2610 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002611 cellattr = line->sb_cells + pos.col;
2612 width = cellattr->width;
2613 attrs = cellattr->attrs;
2614 fg = cellattr->fg;
2615 bg = cellattr->bg;
2616 len = MB_PTR2LEN(p);
2617 mch_memmove(mbs, p, len);
2618 mbs[len] = NUL;
2619 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002620 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002621 else
2622 {
2623 VTermScreenCell cell;
2624 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2625 break;
2626 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2627 {
2628 if (cell.chars[i] == 0)
2629 break;
2630 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2631 }
2632 mbs[off] = NUL;
2633 width = cell.width;
2634 attrs = cell.attrs;
2635 fg = cell.fg;
2636 bg = cell.bg;
2637 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002638 dcell = dict_alloc();
2639 list_append_dict(l, dcell);
2640
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002641 dict_add_nr_str(dcell, "chars", 0, mbs);
2642
2643 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002644 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002645 dict_add_nr_str(dcell, "fg", 0, rgb);
2646 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002647 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002648 dict_add_nr_str(dcell, "bg", 0, rgb);
2649
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002650 dict_add_nr_str(dcell, "attr",
2651 cell2attr(attrs, fg, bg), NULL);
2652 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002653
2654 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002655 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002656 ++pos.col;
2657 }
2658}
2659
2660/*
2661 * "term_sendkeys(buf, keys)" function
2662 */
2663 void
2664f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2665{
2666 buf_T *buf = term_get_buf(argvars);
2667 char_u *msg;
2668 term_T *term;
2669
2670 rettv->v_type = VAR_UNKNOWN;
2671 if (buf == NULL)
2672 return;
2673
2674 msg = get_tv_string_chk(&argvars[1]);
2675 if (msg == NULL)
2676 return;
2677 term = buf->b_term;
2678 if (term->tl_vterm == NULL)
2679 return;
2680
2681 while (*msg != NUL)
2682 {
2683 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2684 msg += MB_PTR2LEN(msg);
2685 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002686}
2687
2688/*
2689 * "term_start(command, options)" function
2690 */
2691 void
2692f_term_start(typval_T *argvars, typval_T *rettv)
2693{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002694 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002695
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002696 init_job_options(&opt);
2697 /* TODO: allow more job options */
2698 if (argvars[1].v_type != VAR_UNKNOWN
2699 && get_job_options(&argvars[1], &opt,
2700 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002701 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002702 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002703 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002704 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002705 return;
2706
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002707 if (opt.jo_vertical)
2708 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002709 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002710
2711 if (curbuf->b_term != NULL)
2712 rettv->vval.v_number = curbuf->b_fnum;
2713}
2714
2715/*
2716 * "term_wait" function
2717 */
2718 void
2719f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2720{
2721 buf_T *buf = term_get_buf(argvars);
2722
2723 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002724 {
2725 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002726 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002727 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002728 if (buf->b_term->tl_job == NULL)
2729 {
2730 ch_log(NULL, "term_wait(): no job to wait for");
2731 return;
2732 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002733
2734 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002735 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002736 {
2737 /* The job is dead, keep reading channel I/O until the channel is
2738 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002739 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002740 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2741 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002742 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002743 parse_queued_messages();
2744 ui_delay(10L, FALSE);
2745 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002746 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002747 parse_queued_messages();
2748 }
2749 else
2750 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002751 long wait = 10L;
2752
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002753 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002754 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002755
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002756 /* Wait for some time for any channel I/O. */
2757 if (argvars[1].v_type != VAR_UNKNOWN)
2758 wait = get_tv_number(&argvars[1]);
2759 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002760 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002761
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002762 /* Flushing messages on channels is hopefully sufficient.
2763 * TODO: is there a better way? */
2764 parse_queued_messages();
2765 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002766}
2767
Bram Moolenaara83e3962017-08-17 14:39:07 +02002768# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002769
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002770/**************************************
2771 * 2. MS-Windows implementation.
2772 */
2773
Bram Moolenaara83e3962017-08-17 14:39:07 +02002774# ifndef PROTO
2775
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002776#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2777#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2778
Bram Moolenaar8a773062017-07-24 22:29:21 +02002779void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002780void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002781void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002782BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2783void (*winpty_config_set_initial_size)(void*, int, int);
2784LPCWSTR (*winpty_conin_name)(void*);
2785LPCWSTR (*winpty_conout_name)(void*);
2786LPCWSTR (*winpty_conerr_name)(void*);
2787void (*winpty_free)(void*);
2788void (*winpty_config_free)(void*);
2789void (*winpty_spawn_config_free)(void*);
2790void (*winpty_error_free)(void*);
2791LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002792BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002793HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002794
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002795#define WINPTY_DLL "winpty.dll"
2796
2797static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002798# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002799
Bram Moolenaara83e3962017-08-17 14:39:07 +02002800 static int
2801dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002802{
2803 int i;
2804 static struct
2805 {
2806 char *name;
2807 FARPROC *ptr;
2808 } winpty_entry[] =
2809 {
2810 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2811 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2812 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2813 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2814 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2815 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2816 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2817 {"winpty_free", (FARPROC*)&winpty_free},
2818 {"winpty_open", (FARPROC*)&winpty_open},
2819 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2820 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2821 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2822 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002823 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002824 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002825 {NULL, NULL}
2826 };
2827
2828 /* No need to initialize twice. */
2829 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002830 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002831 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2832 * winpty.dll. */
2833 if (*p_winptydll != NUL)
2834 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2835 if (!hWinPtyDLL)
2836 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002837 if (!hWinPtyDLL)
2838 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002839 if (verbose)
2840 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2841 : (char_u *)WINPTY_DLL);
2842 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843 }
2844 for (i = 0; winpty_entry[i].name != NULL
2845 && winpty_entry[i].ptr != NULL; ++i)
2846 {
2847 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2848 winpty_entry[i].name)) == NULL)
2849 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002850 if (verbose)
2851 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2852 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002853 }
2854 }
2855
Bram Moolenaara83e3962017-08-17 14:39:07 +02002856 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002857}
2858
2859/*
2860 * Create a new terminal of "rows" by "cols" cells.
2861 * Store a reference in "term".
2862 * Return OK or FAIL.
2863 */
2864 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002865term_and_job_init(
2866 term_T *term,
2867 int rows,
2868 int cols,
2869 typval_T *argvar,
2870 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002871{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002872 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002873 channel_T *channel = NULL;
2874 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002875 DWORD error;
2876 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2877 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002878 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002879 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002880 garray_T ga;
2881 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002882
Bram Moolenaara83e3962017-08-17 14:39:07 +02002883 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002884 return FAIL;
2885
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002886 if (argvar->v_type == VAR_STRING)
2887 cmd = argvar->vval.v_string;
2888 else
2889 {
2890 ga_init2(&ga, (int)sizeof(char*), 20);
2891 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2892 goto failed;
2893 cmd = ga.ga_data;
2894 }
2895
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002896 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002897 if (p == NULL)
2898 return FAIL;
2899
2900 job = job_alloc();
2901 if (job == NULL)
2902 goto failed;
2903
2904 channel = add_channel();
2905 if (channel == NULL)
2906 goto failed;
2907
2908 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2909 if (term->tl_winpty_config == NULL)
2910 goto failed;
2911
2912 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2913 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2914 if (term->tl_winpty == NULL)
2915 goto failed;
2916
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002917 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918 spawn_config = winpty_spawn_config_new(
2919 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2920 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2921 NULL,
2922 p,
2923 NULL,
2924 NULL,
2925 &winpty_err);
2926 if (spawn_config == NULL)
2927 goto failed;
2928
2929 channel = add_channel();
2930 if (channel == NULL)
2931 goto failed;
2932
2933 job = job_alloc();
2934 if (job == NULL)
2935 goto failed;
2936
2937 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2938 &child_thread_handle, &error, &winpty_err))
2939 goto failed;
2940
2941 channel_set_pipes(channel,
2942 (sock_T) CreateFileW(
2943 winpty_conin_name(term->tl_winpty),
2944 GENERIC_WRITE, 0, NULL,
2945 OPEN_EXISTING, 0, NULL),
2946 (sock_T) CreateFileW(
2947 winpty_conout_name(term->tl_winpty),
2948 GENERIC_READ, 0, NULL,
2949 OPEN_EXISTING, 0, NULL),
2950 (sock_T) CreateFileW(
2951 winpty_conerr_name(term->tl_winpty),
2952 GENERIC_READ, 0, NULL,
2953 OPEN_EXISTING, 0, NULL));
2954
2955 jo = CreateJobObject(NULL, NULL);
2956 if (jo == NULL)
2957 goto failed;
2958
2959 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002960 {
2961 /* Failed, switch the way to terminate process with TerminateProcess. */
2962 CloseHandle(jo);
2963 jo = NULL;
2964 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002965
2966 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002967 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002968
2969 create_vterm(term, rows, cols);
2970
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002971 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002972 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002973
2974 job->jv_channel = channel;
2975 job->jv_proc_info.hProcess = child_process_handle;
2976 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2977 job->jv_job_object = jo;
2978 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002979 sprintf(buf, "winpty://%lu",
2980 GetProcessId(winpty_agent_process(term->tl_winpty)));
2981 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002982 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002983 term->tl_job = job;
2984
2985 return OK;
2986
2987failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002988 if (argvar->v_type == VAR_LIST)
2989 vim_free(ga.ga_data);
2990 if (p != NULL)
2991 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002992 if (spawn_config != NULL)
2993 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002994 if (channel != NULL)
2995 channel_clear(channel);
2996 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002997 {
2998 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002999 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003000 }
3001 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003002 if (jo != NULL)
3003 CloseHandle(jo);
3004 if (term->tl_winpty != NULL)
3005 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003006 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007 if (term->tl_winpty_config != NULL)
3008 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003009 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003010 if (winpty_err != NULL)
3011 {
3012 char_u *msg = utf16_to_enc(
3013 (short_u *)winpty_error_msg(winpty_err), NULL);
3014
3015 EMSG(msg);
3016 winpty_error_free(winpty_err);
3017 }
3018 return FAIL;
3019}
3020
3021/*
3022 * Free the terminal emulator part of "term".
3023 */
3024 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003025term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003026{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003027 if (term->tl_winpty != NULL)
3028 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003029 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003030 if (term->tl_winpty_config != NULL)
3031 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003032 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003033 if (term->tl_vterm != NULL)
3034 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003035 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036}
3037
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003038/*
3039 * Request size to terminal.
3040 */
3041 static void
3042term_report_winsize(term_T *term, int rows, int cols)
3043{
3044 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3045}
3046
Bram Moolenaara83e3962017-08-17 14:39:07 +02003047 int
3048terminal_enabled(void)
3049{
3050 return dyn_winpty_init(FALSE) == OK;
3051}
3052
3053
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003054# else
3055
3056/**************************************
3057 * 3. Unix-like implementation.
3058 */
3059
3060/*
3061 * Create a new terminal of "rows" by "cols" cells.
3062 * Start job for "cmd".
3063 * Store the pointers in "term".
3064 * Return OK or FAIL.
3065 */
3066 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003067term_and_job_init(
3068 term_T *term,
3069 int rows,
3070 int cols,
3071 typval_T *argvar,
3072 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003073{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003074 create_vterm(term, rows, cols);
3075
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003076 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003077 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003078 if (term->tl_job != NULL)
3079 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003080
Bram Moolenaar61a66052017-07-22 18:39:00 +02003081 return term->tl_job != NULL
3082 && term->tl_job->jv_channel != NULL
3083 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003084}
3085
3086/*
3087 * Free the terminal emulator part of "term".
3088 */
3089 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003090term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003091{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003092 if (term->tl_vterm != NULL)
3093 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003094 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003095}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003096
3097/*
3098 * Request size to terminal.
3099 */
3100 static void
3101term_report_winsize(term_T *term, int rows, int cols)
3102{
3103 /* Use an ioctl() to report the new window size to the job. */
3104 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3105 {
3106 int fd = -1;
3107 int part;
3108
3109 for (part = PART_OUT; part < PART_COUNT; ++part)
3110 {
3111 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3112 if (isatty(fd))
3113 break;
3114 }
3115 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003116 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003117 }
3118}
3119
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003120# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003121
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003122#endif /* FEAT_TERMINAL */