blob: 2cf4525ae4336d41564e334a40a9a014690afbdf [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 Moolenaar0cbba822017-08-21 21:39:28 +020041 * - test for writing lines to terminal job does not work on 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
Bram Moolenaar740c4332017-08-21 22:01:27 +020052 * "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 Moolenaar740c4332017-08-21 22:01:27 +0200848#ifdef _WIN32
849 if (!enc_utf8 && enc_codepage > 0)
850 {
851 WCHAR *ret = NULL;
852 int length = 0;
853
854 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
855 &ret, &length);
856 if (ret != NULL)
857 {
858 WideCharToMultiByte_alloc(enc_codepage, 0,
859 ret, length, (char **)&text, &len, 0, 0);
860 vim_free(ret);
861 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
862 vim_free(text);
863 }
864 }
865 else
866#endif
867 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200868 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200869 {
870 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200871 curbuf = buf;
872 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200873 curbuf = curwin->w_buffer;
874 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875}
876
877/*
878 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200879 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200880 */
881 static void
882move_terminal_to_buffer(term_T *term)
883{
884 win_T *wp;
885 int len;
886 int lines_skipped = 0;
887 VTermPos pos;
888 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200889 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200890 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200892 if (term->tl_vterm == NULL)
893 return;
894 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200895 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
896 {
897 len = 0;
898 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
899 if (vterm_screen_get_cell(screen, pos, &cell) != 0
900 && cell.chars[0] != NUL)
901 len = pos.col + 1;
902
903 if (len == 0)
904 ++lines_skipped;
905 else
906 {
907 while (lines_skipped > 0)
908 {
909 /* Line was skipped, add an empty line. */
910 --lines_skipped;
911 if (ga_grow(&term->tl_scrollback, 1) == OK)
912 {
913 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
914 + term->tl_scrollback.ga_len;
915
916 line->sb_cols = 0;
917 line->sb_cells = NULL;
918 ++term->tl_scrollback.ga_len;
919
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200920 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200921 }
922 }
923
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200924 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200925 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
926 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200927 garray_T ga;
928 int width;
929 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200930 + term->tl_scrollback.ga_len;
931
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200932 ga_init2(&ga, 1, 100);
933 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200934 {
935 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200936 {
937 width = 1;
938 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
939 if (ga_grow(&ga, 1) == OK)
940 ga.ga_len += mb_char2bytes(' ',
941 (char_u *)ga.ga_data + ga.ga_len);
942 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200943 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200944 {
945 width = cell.width;
946
947 p[pos.col].width = cell.width;
948 p[pos.col].attrs = cell.attrs;
949 p[pos.col].fg = cell.fg;
950 p[pos.col].bg = cell.bg;
951
952 if (ga_grow(&ga, MB_MAXBYTES) == OK)
953 {
954 int i;
955 int c;
956
957 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
Bram Moolenaar740c4332017-08-21 22:01:27 +0200958 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200959 (char_u *)ga.ga_data + ga.ga_len);
960 }
961 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962 }
963 line->sb_cols = len;
964 line->sb_cells = p;
965 ++term->tl_scrollback.ga_len;
966
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200967 if (ga_grow(&ga, 1) == FAIL)
968 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
969 else
970 {
971 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
972 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
973 }
974 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200975 }
976 else
977 vim_free(p);
978 }
979 }
980
981 FOR_ALL_WINDOWS(wp)
982 {
983 if (wp->w_buffer == term->tl_buffer)
984 {
985 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
986 wp->w_cursor.col = 0;
987 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200988 if (wp->w_cursor.lnum >= wp->w_height)
989 {
990 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
991
992 if (wp->w_topline < min_topline)
993 wp->w_topline = min_topline;
994 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995 redraw_win_later(wp, NOT_VALID);
996 }
997 }
998}
999
1000 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001001set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001002{
Bram Moolenaar6d819742017-08-06 14:57:49 +02001003 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001004 vim_free(term->tl_status_text);
1005 term->tl_status_text = NULL;
1006 if (term->tl_buffer == curbuf)
1007 maketitle();
1008}
1009
1010/*
1011 * Called after the job if finished and Terminal mode is not active:
1012 * Move the vterm contents into the scrollback buffer and free the vterm.
1013 */
1014 static void
1015cleanup_vterm(term_T *term)
1016{
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001017 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001018 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +02001020 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001021}
1022
1023/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001024 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001025 * Suspends updating the terminal window.
1026 */
1027 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001028term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029{
1030 term_T *term = curbuf->b_term;
1031
1032 /* Append the current terminal contents to the buffer. */
1033 move_terminal_to_buffer(term);
1034
Bram Moolenaar6d819742017-08-06 14:57:49 +02001035 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001036
Bram Moolenaar6d819742017-08-06 14:57:49 +02001037 /* Move the window cursor to the position of the cursor in the
1038 * terminal. */
1039 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1040 + term->tl_cursor_pos.row + 1;
1041 check_cursor();
1042 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001043
Bram Moolenaar6d819742017-08-06 14:57:49 +02001044 /* Display the same lines as in the terminal. */
1045 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001046}
1047
1048/*
1049 * Returns TRUE if the current window contains a terminal and we are in
1050 * Terminal-Normal mode.
1051 */
1052 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001053term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001054{
1055 term_T *term = curbuf->b_term;
1056
Bram Moolenaar6d819742017-08-06 14:57:49 +02001057 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001058}
1059
1060/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001061 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001062 * Restores updating the terminal window.
1063 */
1064 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001065term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001066{
1067 term_T *term = curbuf->b_term;
1068 sb_line_T *line;
1069 garray_T *gap;
1070
1071 /* Remove the terminal contents from the scrollback and the buffer. */
1072 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001073 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1074 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001075 {
1076 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1077 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1078 vim_free(line->sb_cells);
1079 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001080 }
1081 check_cursor();
1082
Bram Moolenaar6d819742017-08-06 14:57:49 +02001083 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001084
1085 if (term->tl_channel_closed)
1086 cleanup_vterm(term);
1087 redraw_buf_and_status_later(curbuf, NOT_VALID);
1088}
1089
1090/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001091 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001092 * Note: while waiting a terminal may be closed and freed if the channel is
1093 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001094 * TODO: use terminal mode mappings.
1095 */
1096 static int
1097term_vgetc()
1098{
1099 int c;
1100
1101 ++no_mapping;
1102 ++allow_keys;
1103 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001104#ifdef WIN3264
1105 ctrl_break_was_pressed = FALSE;
1106#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001107 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001108 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001109 --no_mapping;
1110 --allow_keys;
1111 return c;
1112}
1113
1114/*
Bram Moolenaarb2412082017-08-20 18:09:14 +02001115 * Get the part that is connected to the tty. Normally this is PART_IN, but
1116 * when writing buffer lines to the job it can be another. This makes it
1117 * possible to do "1,5term vim -".
1118 */
1119 static ch_part_T
1120get_tty_part(term_T *term)
1121{
1122#ifdef UNIX
1123 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
1124 int i;
1125
1126 for (i = 0; i < 3; ++i)
1127 {
1128 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
1129
1130 if (isatty(fd))
1131 return parts[i];
1132 }
1133#endif
1134 return PART_IN;
1135}
1136
1137/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001138 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001139 * Return FAIL when the key needs to be handled in Normal mode.
1140 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001141 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001142 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001143send_keys_to_term(term_T *term, int c, int typed)
1144{
1145 char msg[KEY_BUF_LEN];
1146 size_t len;
1147 static int mouse_was_outside = FALSE;
1148 int dragging_outside = FALSE;
1149
1150 /* Catch keys that need to be handled as in Normal mode. */
1151 switch (c)
1152 {
1153 case NUL:
1154 case K_ZERO:
1155 if (typed)
1156 stuffcharReadbuff(c);
1157 return FAIL;
1158
1159 case K_IGNORE:
1160 return FAIL;
1161
1162 case K_LEFTDRAG:
1163 case K_MIDDLEDRAG:
1164 case K_RIGHTDRAG:
1165 case K_X1DRAG:
1166 case K_X2DRAG:
1167 dragging_outside = mouse_was_outside;
1168 /* FALLTHROUGH */
1169 case K_LEFTMOUSE:
1170 case K_LEFTMOUSE_NM:
1171 case K_LEFTRELEASE:
1172 case K_LEFTRELEASE_NM:
1173 case K_MIDDLEMOUSE:
1174 case K_MIDDLERELEASE:
1175 case K_RIGHTMOUSE:
1176 case K_RIGHTRELEASE:
1177 case K_X1MOUSE:
1178 case K_X1RELEASE:
1179 case K_X2MOUSE:
1180 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001181
1182 case K_MOUSEUP:
1183 case K_MOUSEDOWN:
1184 case K_MOUSELEFT:
1185 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001186 if (mouse_row < W_WINROW(curwin)
1187 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1188 || mouse_col < W_WINCOL(curwin)
1189 || mouse_col >= W_ENDCOL(curwin)
1190 || dragging_outside)
1191 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001192 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001193 if (typed)
1194 {
1195 stuffcharReadbuff(c);
1196 mouse_was_outside = TRUE;
1197 }
1198 return FAIL;
1199 }
1200 }
1201 if (typed)
1202 mouse_was_outside = FALSE;
1203
1204 /* Convert the typed key to a sequence of bytes for the job. */
1205 len = term_convert_key(term, c, msg);
1206 if (len > 0)
1207 /* TODO: if FAIL is returned, stop? */
Bram Moolenaarb2412082017-08-20 18:09:14 +02001208 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1209 (char_u *)msg, (int)len, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001210
1211 return OK;
1212}
1213
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001214 static void
1215position_cursor(win_T *wp, VTermPos *pos)
1216{
1217 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1218 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1219 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1220}
1221
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001222/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001223 * Handle CTRL-W "": send register contents to the job.
1224 */
1225 static void
1226term_paste_register(int prev_c UNUSED)
1227{
1228 int c;
1229 list_T *l;
1230 listitem_T *item;
1231 long reglen = 0;
1232 int type;
1233
1234#ifdef FEAT_CMDL_INFO
1235 if (add_to_showcmd(prev_c))
1236 if (add_to_showcmd('"'))
1237 out_flush();
1238#endif
1239 c = term_vgetc();
1240#ifdef FEAT_CMDL_INFO
1241 clear_showcmd();
1242#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001243 if (!term_use_loop())
1244 /* job finished while waiting for a character */
1245 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001246
1247 /* CTRL-W "= prompt for expression to evaluate. */
1248 if (c == '=' && get_expr_register() != '=')
1249 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001250 if (!term_use_loop())
1251 /* job finished while waiting for a character */
1252 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001253
1254 l = (list_T *)get_reg_contents(c, GREG_LIST);
1255 if (l != NULL)
1256 {
1257 type = get_reg_type(c, &reglen);
1258 for (item = l->lv_first; item != NULL; item = item->li_next)
1259 {
1260 char_u *s = get_tv_string(&item->li_tv);
1261
1262 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1263 s, STRLEN(s), NULL);
1264 if (item->li_next != NULL || type == MLINE)
1265 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1266 (char_u *)"\r", 1, NULL);
1267 }
1268 list_free(l);
1269 }
1270}
1271
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001272#if defined(FEAT_GUI) || defined(PROTO)
1273/*
1274 * Return TRUE when the cursor of the terminal should be displayed.
1275 */
1276 int
1277use_terminal_cursor()
1278{
1279 return in_terminal_loop != NULL;
1280}
1281
1282 cursorentry_T *
1283term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1284{
1285 term_T *term = in_terminal_loop;
1286 static cursorentry_T entry;
1287
1288 vim_memset(&entry, 0, sizeof(entry));
1289 entry.shape = entry.mshape =
1290 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1291 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1292 SHAPE_BLOCK;
1293 entry.percentage = 20;
1294 if (term->tl_cursor_blink)
1295 {
1296 entry.blinkwait = 700;
1297 entry.blinkon = 400;
1298 entry.blinkon = 250;
1299 }
1300 *fg = gui.back_pixel;
1301 if (term->tl_cursor_color == NULL)
1302 *bg = gui.norm_pixel;
1303 else
1304 *bg = color_name2handle(term->tl_cursor_color);
1305 entry.name = "n";
1306 entry.used_for = SHAPE_CURSOR;
1307
1308 return &entry;
1309}
1310#endif
1311
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001312static int did_change_cursor = FALSE;
1313
1314 static void
1315may_set_cursor_props(term_T *term)
1316{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001317#ifdef FEAT_GUI
1318 /* For the GUI the cursor properties are obtained with
1319 * term_get_cursor_shape(). */
1320 if (gui.in_use)
1321 return;
1322#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001323 if (in_terminal_loop == term)
1324 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001325 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001326 if (term->tl_cursor_color != NULL)
1327 term_cursor_color(term->tl_cursor_color);
1328 else
1329 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001330 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1331 }
1332}
1333
1334 static void
1335may_restore_cursor_props(void)
1336{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001337#ifdef FEAT_GUI
1338 if (gui.in_use)
1339 return;
1340#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001341 if (did_change_cursor)
1342 {
1343 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001344 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001345 /* this will restore the initial cursor style, if possible */
1346 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001347 }
1348}
1349
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001350/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001351 * Returns TRUE if the current window contains a terminal and we are sending
1352 * keys to the job.
1353 */
1354 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001355term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001356{
1357 term_T *term = curbuf->b_term;
1358
1359 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001360 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001361 && term->tl_vterm != NULL
1362 && term_job_running(term);
1363}
1364
1365/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001366 * Wait for input and send it to the job.
1367 * Return when the start of a CTRL-W command is typed or anything else that
1368 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001369 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1370 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001371 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001372 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001373terminal_loop(void)
1374{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001375 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001376 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001377 int ret;
1378
Bram Moolenaar679653e2017-08-13 14:13:19 +02001379 /* Remember the terminal we are sending keys to. However, the terminal
1380 * might be closed while waiting for a character, e.g. typing "exit" in a
1381 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1382 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001383 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001384
1385 if (*curwin->w_p_tk != NUL)
1386 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001387 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001388 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001389
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001390#ifdef UNIX
1391 {
Bram Moolenaarb2412082017-08-20 18:09:14 +02001392 int part = get_tty_part(curbuf->b_term);
1393 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001394
1395 if (isatty(fd))
1396 {
1397 ttyinfo_T info;
1398
1399 /* Get the current backspace and enter characters of the pty. */
1400 if (get_tty_info(fd, &info) == OK)
1401 {
1402 term_backspace_char = info.backspace;
1403 term_enter_char = info.enter;
1404 term_nl_does_cr = info.nl_does_cr;
1405 }
1406 }
1407 }
1408#endif
1409
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001410 for (;;)
1411 {
1412 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001413 /* Repeat redrawing in case a message is received while redrawing. */
1414 while (curwin->w_redr_type != 0)
1415 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001416 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001417
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001418 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001419 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001420 /* job finished while waiting for a character */
1421 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001422 if (c == K_IGNORE)
1423 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001424
Bram Moolenaarfae42832017-08-01 22:24:26 +02001425#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001426 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001427 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001428 if (ctrl_break_was_pressed)
1429 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001430#endif
1431
Bram Moolenaar69198192017-08-05 14:10:48 +02001432 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001433 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001434 int prev_c = c;
1435
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001436#ifdef FEAT_CMDL_INFO
1437 if (add_to_showcmd(c))
1438 out_flush();
1439#endif
1440 c = term_vgetc();
1441#ifdef FEAT_CMDL_INFO
1442 clear_showcmd();
1443#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001444 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001445 /* job finished while waiting for a character */
1446 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001447
Bram Moolenaar69198192017-08-05 14:10:48 +02001448 if (prev_c == Ctrl_BSL)
1449 {
1450 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001451 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001452 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1453 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001454 ret = FAIL;
1455 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001456 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001457 /* Send both keys to the terminal. */
1458 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1459 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001460 else if (c == Ctrl_C)
1461 {
1462 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1463 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1464 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001465 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001466 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001467 /* "CTRL-W .": send CTRL-W to the job */
1468 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001469 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001470 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001471 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001472 /* CTRL-W N : go to Terminal-Normal mode. */
1473 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001474 ret = FAIL;
1475 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001476 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001477 else if (c == '"')
1478 {
1479 term_paste_register(prev_c);
1480 continue;
1481 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001482 else if (termkey == 0 || c != termkey)
1483 {
1484 stuffcharReadbuff(Ctrl_W);
1485 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001486 ret = OK;
1487 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001488 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001489 }
Bram Moolenaar740c4332017-08-21 22:01:27 +02001490# ifdef _WIN32
1491 if (!enc_utf8 && has_mbyte && c >= 0x80)
1492 {
1493 WCHAR wc;
1494 char_u mb[3];
1495
1496 mb[0] = (unsigned)c >> 8;
1497 mb[1] = c;
1498 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
1499 c = wc;
1500 }
1501# endif
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001502 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001503 {
1504 ret = OK;
1505 goto theend;
1506 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001507 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001508 ret = FAIL;
1509
1510theend:
1511 in_terminal_loop = NULL;
1512 may_restore_cursor_props();
1513 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001514}
1515
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001516/*
1517 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001518 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001519 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001520 */
1521 void
1522term_job_ended(job_T *job)
1523{
1524 term_T *term;
1525 int did_one = FALSE;
1526
1527 for (term = first_term; term != NULL; term = term->tl_next)
1528 if (term->tl_job == job)
1529 {
1530 vim_free(term->tl_title);
1531 term->tl_title = NULL;
1532 vim_free(term->tl_status_text);
1533 term->tl_status_text = NULL;
1534 redraw_buf_and_status_later(term->tl_buffer, VALID);
1535 did_one = TRUE;
1536 }
1537 if (did_one)
1538 redraw_statuslines();
1539 if (curbuf->b_term != NULL)
1540 {
1541 if (curbuf->b_term->tl_job == job)
1542 maketitle();
1543 update_cursor(curbuf->b_term, TRUE);
1544 }
1545}
1546
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001547 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001548may_toggle_cursor(term_T *term)
1549{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001550 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001551 {
1552 if (term->tl_cursor_visible)
1553 cursor_on();
1554 else
1555 cursor_off();
1556 }
1557}
1558
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001559/*
1560 * Reverse engineer the RGB value into a cterm color index.
1561 * First color is 1. Return 0 if no match found.
1562 */
1563 static int
1564color2index(VTermColor *color, int fg, int *boldp)
1565{
1566 int red = color->red;
1567 int blue = color->blue;
1568 int green = color->green;
1569
1570 /* The argument for lookup_color() is for the color_names[] table. */
1571 if (red == 0)
1572 {
1573 if (green == 0)
1574 {
1575 if (blue == 0)
1576 return lookup_color(0, fg, boldp) + 1; /* black */
1577 if (blue == 224)
1578 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1579 }
1580 else if (green == 224)
1581 {
1582 if (blue == 0)
1583 return lookup_color(2, fg, boldp) + 1; /* dark green */
1584 if (blue == 224)
1585 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1586 }
1587 }
1588 else if (red == 224)
1589 {
1590 if (green == 0)
1591 {
1592 if (blue == 0)
1593 return lookup_color(4, fg, boldp) + 1; /* dark red */
1594 if (blue == 224)
1595 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1596 }
1597 else if (green == 224)
1598 {
1599 if (blue == 0)
1600 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1601 if (blue == 224)
1602 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1603 }
1604 }
1605 else if (red == 128)
1606 {
1607 if (green == 128 && blue == 128)
1608 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1609 }
1610 else if (red == 255)
1611 {
1612 if (green == 64)
1613 {
1614 if (blue == 64)
1615 return lookup_color(20, fg, boldp) + 1; /* light red */
1616 if (blue == 255)
1617 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1618 }
1619 else if (green == 255)
1620 {
1621 if (blue == 64)
1622 return lookup_color(24, fg, boldp) + 1; /* yellow */
1623 if (blue == 255)
1624 return lookup_color(26, fg, boldp) + 1; /* white */
1625 }
1626 }
1627 else if (red == 64)
1628 {
1629 if (green == 64)
1630 {
1631 if (blue == 255)
1632 return lookup_color(14, fg, boldp) + 1; /* light blue */
1633 }
1634 else if (green == 255)
1635 {
1636 if (blue == 64)
1637 return lookup_color(16, fg, boldp) + 1; /* light green */
1638 if (blue == 255)
1639 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1640 }
1641 }
1642 if (t_colors >= 256)
1643 {
1644 if (red == blue && red == green)
1645 {
1646 /* 24-color greyscale */
1647 static int cutoff[23] = {
1648 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1649 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1650 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1651 int i;
1652
1653 for (i = 0; i < 23; ++i)
1654 if (red < cutoff[i])
1655 return i + 233;
1656 return 256;
1657 }
1658
1659 /* 216-color cube */
1660 return 17 + ((red + 25) / 0x33) * 36
Bram Moolenaar740c4332017-08-21 22:01:27 +02001661 + ((green + 25) / 0x33) * 6
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001662 + (blue + 25) / 0x33;
1663 }
1664 return 0;
1665}
1666
1667/*
1668 * Convert the attributes of a vterm cell into an attribute index.
1669 */
1670 static int
1671cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1672{
1673 int attr = 0;
1674
1675 if (cellattrs.bold)
1676 attr |= HL_BOLD;
1677 if (cellattrs.underline)
1678 attr |= HL_UNDERLINE;
1679 if (cellattrs.italic)
1680 attr |= HL_ITALIC;
1681 if (cellattrs.strike)
1682 attr |= HL_STANDOUT;
1683 if (cellattrs.reverse)
1684 attr |= HL_INVERSE;
1685
1686#ifdef FEAT_GUI
1687 if (gui.in_use)
1688 {
1689 guicolor_T fg, bg;
1690
1691 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1692 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1693 return get_gui_attr_idx(attr, fg, bg);
1694 }
1695 else
1696#endif
1697#ifdef FEAT_TERMGUICOLORS
1698 if (p_tgc)
1699 {
1700 guicolor_T fg, bg;
1701
1702 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1703 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1704
1705 return get_tgc_attr_idx(attr, fg, bg);
1706 }
1707 else
1708#endif
1709 {
1710 int bold = MAYBE;
1711 int fg = color2index(&cellfg, TRUE, &bold);
1712 int bg = color2index(&cellbg, FALSE, &bold);
1713
1714 /* with 8 colors set the bold attribute to get a bright foreground */
1715 if (bold == TRUE)
1716 attr |= HL_BOLD;
1717 return get_cterm_attr_idx(attr, fg, bg);
1718 }
1719 return 0;
1720}
1721
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001722 static int
1723handle_damage(VTermRect rect, void *user)
1724{
1725 term_T *term = (term_T *)user;
1726
1727 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1728 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1729 redraw_buf_later(term->tl_buffer, NOT_VALID);
1730 return 1;
1731}
1732
1733 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001734handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001735{
1736 term_T *term = (term_T *)user;
1737
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001738 /* Scrolling up is done much more efficiently by deleting lines instead of
1739 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001740 if (dest.start_col == src.start_col
1741 && dest.end_col == src.end_col
1742 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001743 {
1744 win_T *wp;
1745 VTermColor fg, bg;
1746 VTermScreenCellAttrs attr;
1747 int clear_attr;
1748
1749 /* Set the color to clear lines with. */
1750 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1751 &fg, &bg);
1752 vim_memset(&attr, 0, sizeof(attr));
1753 clear_attr = cell2attr(attr, fg, bg);
1754
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001755 FOR_ALL_WINDOWS(wp)
1756 {
1757 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001758 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001759 src.start_row - dest.start_row, FALSE, FALSE,
1760 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001761 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001762 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001763 redraw_buf_later(term->tl_buffer, NOT_VALID);
1764 return 1;
1765}
1766
1767 static int
1768handle_movecursor(
1769 VTermPos pos,
1770 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001771 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001772 void *user)
1773{
1774 term_T *term = (term_T *)user;
1775 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001776
1777 term->tl_cursor_pos = pos;
1778 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001779
1780 FOR_ALL_WINDOWS(wp)
1781 {
1782 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001783 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001784 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001785 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001786 {
1787 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001788 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001789 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001790
1791 return 1;
1792}
1793
Bram Moolenaar21554412017-07-24 21:44:43 +02001794 static int
1795handle_settermprop(
1796 VTermProp prop,
1797 VTermValue *value,
1798 void *user)
1799{
1800 term_T *term = (term_T *)user;
1801
1802 switch (prop)
1803 {
1804 case VTERM_PROP_TITLE:
1805 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001806 /* a blank title isn't useful, make it empty, so that "running" is
1807 * displayed */
1808 if (*skipwhite((char_u *)value->string) == NUL)
1809 term->tl_title = NULL;
1810 else
1811 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001812 vim_free(term->tl_status_text);
1813 term->tl_status_text = NULL;
1814 if (term == curbuf->b_term)
1815 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001816 break;
1817
1818 case VTERM_PROP_CURSORVISIBLE:
1819 term->tl_cursor_visible = value->boolean;
1820 may_toggle_cursor(term);
1821 out_flush();
1822 break;
1823
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001824 case VTERM_PROP_CURSORBLINK:
1825 term->tl_cursor_blink = value->boolean;
1826 may_set_cursor_props(term);
1827 break;
1828
1829 case VTERM_PROP_CURSORSHAPE:
1830 term->tl_cursor_shape = value->number;
1831 may_set_cursor_props(term);
1832 break;
1833
1834 case VTERM_PROP_CURSORCOLOR:
1835 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001836 if (*value->string == NUL)
1837 term->tl_cursor_color = NULL;
1838 else
1839 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001840 may_set_cursor_props(term);
1841 break;
1842
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001843 case VTERM_PROP_ALTSCREEN:
1844 /* TODO: do anything else? */
1845 term->tl_using_altscreen = value->boolean;
1846 break;
1847
Bram Moolenaar21554412017-07-24 21:44:43 +02001848 default:
1849 break;
1850 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001851 /* Always return 1, otherwise vterm doesn't store the value internally. */
1852 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001853}
1854
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001855/*
1856 * The job running in the terminal resized the terminal.
1857 */
1858 static int
1859handle_resize(int rows, int cols, void *user)
1860{
1861 term_T *term = (term_T *)user;
1862 win_T *wp;
1863
1864 term->tl_rows = rows;
1865 term->tl_cols = cols;
1866 FOR_ALL_WINDOWS(wp)
1867 {
1868 if (wp->w_buffer == term->tl_buffer)
1869 {
1870 win_setheight_win(rows, wp);
1871 win_setwidth_win(cols, wp);
1872 }
1873 }
1874
1875 redraw_buf_later(term->tl_buffer, NOT_VALID);
1876 return 1;
1877}
1878
Bram Moolenaard85f2712017-07-28 21:51:57 +02001879/*
1880 * Handle a line that is pushed off the top of the screen.
1881 */
1882 static int
1883handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1884{
1885 term_T *term = (term_T *)user;
1886
1887 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001888 if (ga_grow(&term->tl_scrollback, 1) == OK)
1889 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001890 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001891 int len = 0;
1892 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001893 int c;
1894 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001895 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001896 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001897
1898 /* do not store empty cells at the end */
1899 for (i = 0; i < cols; ++i)
1900 if (cells[i].chars[0] != 0)
1901 len = i + 1;
1902
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001903 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001904 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001905 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001906 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001907 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001908 for (col = 0; col < len; col += cells[col].width)
1909 {
1910 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1911 {
1912 ga.ga_len = 0;
1913 break;
1914 }
1915 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1916 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1917 (char_u *)ga.ga_data + ga.ga_len);
1918 p[col].width = cells[col].width;
1919 p[col].attrs = cells[col].attrs;
1920 p[col].fg = cells[col].fg;
1921 p[col].bg = cells[col].bg;
1922 }
1923 }
1924 if (ga_grow(&ga, 1) == FAIL)
1925 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1926 else
1927 {
1928 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1929 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1930 }
1931 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001932
1933 line = (sb_line_T *)term->tl_scrollback.ga_data
1934 + term->tl_scrollback.ga_len;
1935 line->sb_cols = len;
1936 line->sb_cells = p;
1937 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001938 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001939 }
1940 return 0; /* ignored */
1941}
1942
Bram Moolenaar21554412017-07-24 21:44:43 +02001943static VTermScreenCallbacks screen_callbacks = {
1944 handle_damage, /* damage */
1945 handle_moverect, /* moverect */
1946 handle_movecursor, /* movecursor */
1947 handle_settermprop, /* settermprop */
1948 NULL, /* bell */
1949 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001950 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001951 NULL /* sb_popline */
1952};
1953
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001954/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001955 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001956 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001957 */
1958 void
1959term_channel_closed(channel_T *ch)
1960{
1961 term_T *term;
1962 int did_one = FALSE;
1963
1964 for (term = first_term; term != NULL; term = term->tl_next)
1965 if (term->tl_job == ch->ch_job)
1966 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001967 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001968 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001969
Bram Moolenaard85f2712017-07-28 21:51:57 +02001970 vim_free(term->tl_title);
1971 term->tl_title = NULL;
1972 vim_free(term->tl_status_text);
1973 term->tl_status_text = NULL;
1974
Bram Moolenaar423802d2017-07-30 16:52:24 +02001975 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001976 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001977 {
1978 int fnum = term->tl_buffer->b_fnum;
1979
Bram Moolenaar423802d2017-07-30 16:52:24 +02001980 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001981
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001982 if (term->tl_finish == 'c')
1983 {
1984 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001985 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001986 curbuf = term->tl_buffer;
1987 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1988 break;
1989 }
1990 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1991 {
1992 char buf[50];
1993
1994 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001995 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001996 vim_snprintf(buf, sizeof(buf),
1997 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001998 ? "botright sbuf %d"
1999 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002000 do_cmdline_cmd((char_u *)buf);
2001 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02002002 else
2003 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02002004 }
2005
Bram Moolenaard85f2712017-07-28 21:51:57 +02002006 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002007 }
2008 if (did_one)
2009 {
2010 redraw_statuslines();
2011
2012 /* Need to break out of vgetc(). */
2013 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002014 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002015
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002016 term = curbuf->b_term;
2017 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002018 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002019 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002020 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02002021 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002022 }
2023 }
2024}
2025
2026/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02002027 * Called to update a window that contains an active terminal.
2028 * Returns FAIL when there is no terminal running in this window or in
2029 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002030 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02002031 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002032term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002033{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002034 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02002035 VTerm *vterm;
2036 VTermScreen *screen;
2037 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002038 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002039
Bram Moolenaar6d819742017-08-06 14:57:49 +02002040 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02002041 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02002042
Bram Moolenaard85f2712017-07-28 21:51:57 +02002043 vterm = term->tl_vterm;
2044 screen = vterm_obtain_screen(vterm);
2045 state = vterm_obtain_state(vterm);
2046
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002047 /*
2048 * If the window was resized a redraw will be triggered and we get here.
2049 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
2050 */
2051 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
2052 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002053 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02002054 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
2055 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
2056 win_T *twp;
2057
2058 FOR_ALL_WINDOWS(twp)
2059 {
2060 /* When more than one window shows the same terminal, use the
2061 * smallest size. */
2062 if (twp->w_buffer == term->tl_buffer)
2063 {
2064 if (!term->tl_rows_fixed && rows > twp->w_height)
2065 rows = twp->w_height;
2066 if (!term->tl_cols_fixed && cols > twp->w_width)
2067 cols = twp->w_width;
2068 }
2069 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002070
2071 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002072 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002073 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002074 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002075 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002076
2077 /* The cursor may have been moved when resizing. */
2078 vterm_state_get_cursorpos(state, &pos);
2079 position_cursor(wp, &pos);
2080
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002081 /* TODO: Only redraw what changed. */
2082 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002083 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002084 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002085 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002086
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002087 if (pos.row < term->tl_rows)
2088 {
2089 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002090 {
2091 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002092 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002093
Bram Moolenaareeac6772017-07-23 15:48:37 +02002094 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2095 vim_memset(&cell, 0, sizeof(cell));
2096
2097 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002098 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002099 if (c == NUL)
2100 {
2101 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002102#if defined(FEAT_MBYTE)
2103 if (enc_utf8)
2104 ScreenLinesUC[off] = NUL;
2105#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002106 }
2107 else
2108 {
2109#if defined(FEAT_MBYTE)
Bram Moolenaar740c4332017-08-21 22:01:27 +02002110 if (enc_utf8)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002111 {
Bram Moolenaar740c4332017-08-21 22:01:27 +02002112 if (c >= 0x80)
2113 {
2114 ScreenLines[off] = ' ';
2115 ScreenLinesUC[off] = c;
2116 }
2117 else
2118 {
2119 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002120 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002121 }
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002122 }
Bram Moolenaar740c4332017-08-21 22:01:27 +02002123# ifdef _WIN32
2124 else if (has_mbyte && c >= 0x80)
2125 {
2126 char_u mb[MB_MAXBYTES+1];
2127 WCHAR wc = c;
2128
2129 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
2130 (char*)mb, 2, 0, 0) > 1)
2131 {
2132 ScreenLines[off] = mb[0];
2133 ScreenLines[off+1] = mb[1];
2134 cell.width = mb_ptr2cells(mb);
2135 }
2136 else
2137 ScreenLines[off] = c;
2138 }
2139# endif
2140 else
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002141#endif
Bram Moolenaar740c4332017-08-21 22:01:27 +02002142 ScreenLines[off] = c;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002143 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002144 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002145
2146 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002147 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002148 if (cell.width == 2)
2149 {
Bram Moolenaar8a773062017-07-24 22:29:21 +02002150#if defined(FEAT_MBYTE)
2151 if (enc_utf8)
2152 ScreenLinesUC[off] = NUL;
Bram Moolenaar740c4332017-08-21 22:01:27 +02002153 else if (!has_mbyte)
Bram Moolenaar8a773062017-07-24 22:29:21 +02002154#endif
Bram Moolenaar740c4332017-08-21 22:01:27 +02002155 ScreenLines[off] = NUL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002156 ++pos.col;
2157 ++off;
2158 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002159 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002160 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002161 else
2162 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002163
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002164 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2165 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002166 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002167
2168 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002169}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002170
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002171/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002172 * Return TRUE if "wp" is a terminal window where the job has finished.
2173 */
2174 int
2175term_is_finished(buf_T *buf)
2176{
2177 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2178}
2179
2180/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002181 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002182 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002183 */
2184 int
2185term_show_buffer(buf_T *buf)
2186{
2187 term_T *term = buf->b_term;
2188
Bram Moolenaar6d819742017-08-06 14:57:49 +02002189 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002190}
2191
2192/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002193 * The current buffer is going to be changed. If there is terminal
2194 * highlighting remove it now.
2195 */
2196 void
2197term_change_in_curbuf(void)
2198{
2199 term_T *term = curbuf->b_term;
2200
2201 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2202 {
2203 free_scrollback(term);
2204 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002205
2206 /* The buffer is now like a normal buffer, it cannot be easily
2207 * abandoned when changed. */
2208 set_string_option_direct((char_u *)"buftype", -1,
2209 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002210 }
2211}
2212
2213/*
2214 * Get the screen attribute for a position in the buffer.
2215 */
2216 int
2217term_get_attr(buf_T *buf, linenr_T lnum, int col)
2218{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002219 term_T *term = buf->b_term;
2220 sb_line_T *line;
2221 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002222
Bram Moolenaar70229f92017-07-29 16:01:53 +02002223 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002224 return 0;
2225 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2226 if (col >= line->sb_cols)
2227 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002228 cellattr = line->sb_cells + col;
2229 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002230}
2231
2232/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002233 * Create a new vterm and initialize it.
2234 */
2235 static void
2236create_vterm(term_T *term, int rows, int cols)
2237{
2238 VTerm *vterm;
2239 VTermScreen *screen;
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002240 VTermValue value;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002241
2242 vterm = vterm_new(rows, cols);
2243 term->tl_vterm = vterm;
2244 screen = vterm_obtain_screen(vterm);
2245 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2246 /* TODO: depends on 'encoding'. */
2247 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002248
2249 /* Vterm uses a default black background. Set it to white when
2250 * 'background' is "light". */
2251 if (*p_bg == 'l')
2252 {
2253 VTermColor fg, bg;
2254
2255 fg.red = fg.green = fg.blue = 0;
2256 bg.red = bg.green = bg.blue = 255;
2257 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2258 }
2259
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002260 /* Required to initialize most things. */
2261 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002262
2263 /* Allow using alternate screen. */
2264 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar0cbba822017-08-21 21:39:28 +02002265
2266 /* We do not want a blinking cursor by default. */
2267 value.boolean = 0;
2268 vterm_state_set_termprop(vterm_obtain_state(vterm),
2269 VTERM_PROP_CURSORBLINK, &value);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002270}
2271
Bram Moolenaar21554412017-07-24 21:44:43 +02002272/*
2273 * Return the text to show for the buffer name and status.
2274 */
2275 char_u *
2276term_get_status_text(term_T *term)
2277{
2278 if (term->tl_status_text == NULL)
2279 {
2280 char_u *txt;
2281 size_t len;
2282
Bram Moolenaar6d819742017-08-06 14:57:49 +02002283 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002284 {
2285 if (term_job_running(term))
2286 txt = (char_u *)_("Terminal");
2287 else
2288 txt = (char_u *)_("Terminal-finished");
2289 }
2290 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002291 txt = term->tl_title;
2292 else if (term_job_running(term))
2293 txt = (char_u *)_("running");
2294 else
2295 txt = (char_u *)_("finished");
2296 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002297 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002298 if (term->tl_status_text != NULL)
2299 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2300 term->tl_buffer->b_fname, txt);
2301 }
2302 return term->tl_status_text;
2303}
2304
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002305/*
2306 * Mark references in jobs of terminals.
2307 */
2308 int
2309set_ref_in_term(int copyID)
2310{
2311 int abort = FALSE;
2312 term_T *term;
2313 typval_T tv;
2314
2315 for (term = first_term; term != NULL; term = term->tl_next)
2316 if (term->tl_job != NULL)
2317 {
2318 tv.v_type = VAR_JOB;
2319 tv.vval.v_job = term->tl_job;
2320 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2321 }
2322 return abort;
2323}
2324
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002325/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002326 * Get the buffer from the first argument in "argvars".
2327 * Returns NULL when the buffer is not for a terminal window.
2328 */
2329 static buf_T *
2330term_get_buf(typval_T *argvars)
2331{
2332 buf_T *buf;
2333
2334 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2335 ++emsg_off;
2336 buf = get_buf_tv(&argvars[0], FALSE);
2337 --emsg_off;
2338 if (buf == NULL || buf->b_term == NULL)
2339 return NULL;
2340 return buf;
2341}
2342
2343/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002344 * "term_getaltscreen(buf)" function
2345 */
2346 void
2347f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2348{
2349 buf_T *buf = term_get_buf(argvars);
2350
2351 if (buf == NULL)
2352 return;
2353 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2354}
2355
2356/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002357 * "term_getattr(attr, name)" function
2358 */
2359 void
2360f_term_getattr(typval_T *argvars, typval_T *rettv)
2361{
2362 int attr;
2363 size_t i;
2364 char_u *name;
2365
2366 static struct {
2367 char *name;
2368 int attr;
2369 } attrs[] = {
2370 {"bold", HL_BOLD},
2371 {"italic", HL_ITALIC},
2372 {"underline", HL_UNDERLINE},
2373 {"strike", HL_STANDOUT},
2374 {"reverse", HL_INVERSE},
2375 };
2376
2377 attr = get_tv_number(&argvars[0]);
2378 name = get_tv_string_chk(&argvars[1]);
2379 if (name == NULL)
2380 return;
2381
2382 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2383 if (STRCMP(name, attrs[i].name) == 0)
2384 {
2385 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2386 break;
2387 }
2388}
2389
2390/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002391 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002392 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002393 void
2394f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002395{
Bram Moolenaar97870002017-07-30 18:28:38 +02002396 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002397 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002398 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002399 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002400
Bram Moolenaar97870002017-07-30 18:28:38 +02002401 if (rettv_list_alloc(rettv) == FAIL)
2402 return;
2403 if (buf == NULL)
2404 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002405 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002406
2407 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002408 list_append_number(l, term->tl_cursor_pos.row + 1);
2409 list_append_number(l, term->tl_cursor_pos.col + 1);
2410
2411 d = dict_alloc();
2412 if (d != NULL)
2413 {
2414 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2415 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2416 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2417 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2418 ? (char_u *)"" : term->tl_cursor_color);
2419 list_append_dict(l, d);
2420 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002421}
2422
2423/*
2424 * "term_getjob(buf)" function
2425 */
2426 void
2427f_term_getjob(typval_T *argvars, typval_T *rettv)
2428{
2429 buf_T *buf = term_get_buf(argvars);
2430
2431 rettv->v_type = VAR_JOB;
2432 rettv->vval.v_job = NULL;
2433 if (buf == NULL)
2434 return;
2435
2436 rettv->vval.v_job = buf->b_term->tl_job;
2437 if (rettv->vval.v_job != NULL)
2438 ++rettv->vval.v_job->jv_refcount;
2439}
2440
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002441 static int
2442get_row_number(typval_T *tv, term_T *term)
2443{
2444 if (tv->v_type == VAR_STRING
2445 && tv->vval.v_string != NULL
2446 && STRCMP(tv->vval.v_string, ".") == 0)
2447 return term->tl_cursor_pos.row;
2448 return (int)get_tv_number(tv) - 1;
2449}
2450
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002451/*
2452 * "term_getline(buf, row)" function
2453 */
2454 void
2455f_term_getline(typval_T *argvars, typval_T *rettv)
2456{
2457 buf_T *buf = term_get_buf(argvars);
2458 term_T *term;
2459 int row;
2460
2461 rettv->v_type = VAR_STRING;
2462 if (buf == NULL)
2463 return;
2464 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002465 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002466
2467 if (term->tl_vterm == NULL)
2468 {
2469 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2470
2471 /* vterm is finished, get the text from the buffer */
2472 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2473 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2474 }
2475 else
2476 {
2477 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2478 VTermRect rect;
2479 int len;
2480 char_u *p;
2481
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002482 if (row < 0 || row >= term->tl_rows)
2483 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002484 len = term->tl_cols * MB_MAXBYTES + 1;
2485 p = alloc(len);
2486 if (p == NULL)
2487 return;
2488 rettv->vval.v_string = p;
2489
2490 rect.start_col = 0;
2491 rect.end_col = term->tl_cols;
2492 rect.start_row = row;
2493 rect.end_row = row + 1;
2494 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2495 }
2496}
2497
2498/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002499 * "term_getscrolled(buf)" function
2500 */
2501 void
2502f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2503{
2504 buf_T *buf = term_get_buf(argvars);
2505
2506 if (buf == NULL)
2507 return;
2508 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2509}
2510
2511/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002512 * "term_getsize(buf)" function
2513 */
2514 void
2515f_term_getsize(typval_T *argvars, typval_T *rettv)
2516{
2517 buf_T *buf = term_get_buf(argvars);
2518 list_T *l;
2519
2520 if (rettv_list_alloc(rettv) == FAIL)
2521 return;
2522 if (buf == NULL)
2523 return;
2524
2525 l = rettv->vval.v_list;
2526 list_append_number(l, buf->b_term->tl_rows);
2527 list_append_number(l, buf->b_term->tl_cols);
2528}
2529
2530/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002531 * "term_getstatus(buf)" function
2532 */
2533 void
2534f_term_getstatus(typval_T *argvars, typval_T *rettv)
2535{
2536 buf_T *buf = term_get_buf(argvars);
2537 term_T *term;
2538 char_u val[100];
2539
2540 rettv->v_type = VAR_STRING;
2541 if (buf == NULL)
2542 return;
2543 term = buf->b_term;
2544
2545 if (term_job_running(term))
2546 STRCPY(val, "running");
2547 else
2548 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002549 if (term->tl_normal_mode)
2550 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002551 rettv->vval.v_string = vim_strsave(val);
2552}
2553
2554/*
2555 * "term_gettitle(buf)" function
2556 */
2557 void
2558f_term_gettitle(typval_T *argvars, typval_T *rettv)
2559{
2560 buf_T *buf = term_get_buf(argvars);
2561
2562 rettv->v_type = VAR_STRING;
2563 if (buf == NULL)
2564 return;
2565
2566 if (buf->b_term->tl_title != NULL)
2567 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2568}
2569
2570/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002571 * "term_gettty(buf)" function
2572 */
2573 void
2574f_term_gettty(typval_T *argvars, typval_T *rettv)
2575{
2576 buf_T *buf = term_get_buf(argvars);
2577 char_u *p;
2578
2579 rettv->v_type = VAR_STRING;
2580 if (buf == NULL)
2581 return;
2582 if (buf->b_term->tl_job != NULL)
2583 p = buf->b_term->tl_job->jv_tty_name;
2584 else
2585 p = buf->b_term->tl_tty_name;
2586 if (p != NULL)
2587 rettv->vval.v_string = vim_strsave(p);
2588}
2589
2590/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002591 * "term_list()" function
2592 */
2593 void
2594f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2595{
2596 term_T *tp;
2597 list_T *l;
2598
2599 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2600 return;
2601
2602 l = rettv->vval.v_list;
2603 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2604 if (tp != NULL && tp->tl_buffer != NULL)
2605 if (list_append_number(l,
2606 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2607 return;
2608}
2609
2610/*
2611 * "term_scrape(buf, row)" function
2612 */
2613 void
2614f_term_scrape(typval_T *argvars, typval_T *rettv)
2615{
2616 buf_T *buf = term_get_buf(argvars);
2617 VTermScreen *screen = NULL;
2618 VTermPos pos;
2619 list_T *l;
2620 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002621 char_u *p;
2622 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002623
2624 if (rettv_list_alloc(rettv) == FAIL)
2625 return;
2626 if (buf == NULL)
2627 return;
2628 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002629
2630 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002631 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002632
2633 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002634 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002635 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002636 p = NULL;
2637 line = NULL;
2638 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002639 else
2640 {
2641 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2642
2643 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2644 return;
2645 p = ml_get_buf(buf, lnum + 1, FALSE);
2646 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2647 }
2648
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002649 for (pos.col = 0; pos.col < term->tl_cols; )
2650 {
2651 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002652 int width;
2653 VTermScreenCellAttrs attrs;
2654 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002655 char_u rgb[8];
2656 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2657 int off = 0;
2658 int i;
2659
2660 if (screen == NULL)
2661 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002662 cellattr_T *cellattr;
2663 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002664
2665 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002666 if (pos.col >= line->sb_cols)
2667 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002668 cellattr = line->sb_cells + pos.col;
2669 width = cellattr->width;
2670 attrs = cellattr->attrs;
2671 fg = cellattr->fg;
2672 bg = cellattr->bg;
2673 len = MB_PTR2LEN(p);
2674 mch_memmove(mbs, p, len);
2675 mbs[len] = NUL;
2676 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002677 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002678 else
2679 {
2680 VTermScreenCell cell;
2681 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2682 break;
2683 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2684 {
2685 if (cell.chars[i] == 0)
2686 break;
2687 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2688 }
2689 mbs[off] = NUL;
2690 width = cell.width;
2691 attrs = cell.attrs;
2692 fg = cell.fg;
2693 bg = cell.bg;
2694 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002695 dcell = dict_alloc();
2696 list_append_dict(l, dcell);
2697
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002698 dict_add_nr_str(dcell, "chars", 0, mbs);
2699
2700 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002701 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002702 dict_add_nr_str(dcell, "fg", 0, rgb);
2703 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002704 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002705 dict_add_nr_str(dcell, "bg", 0, rgb);
2706
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002707 dict_add_nr_str(dcell, "attr",
2708 cell2attr(attrs, fg, bg), NULL);
2709 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002710
2711 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002712 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002713 ++pos.col;
2714 }
2715}
2716
2717/*
2718 * "term_sendkeys(buf, keys)" function
2719 */
2720 void
2721f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2722{
2723 buf_T *buf = term_get_buf(argvars);
2724 char_u *msg;
2725 term_T *term;
2726
2727 rettv->v_type = VAR_UNKNOWN;
2728 if (buf == NULL)
2729 return;
2730
2731 msg = get_tv_string_chk(&argvars[1]);
2732 if (msg == NULL)
2733 return;
2734 term = buf->b_term;
2735 if (term->tl_vterm == NULL)
2736 return;
2737
2738 while (*msg != NUL)
2739 {
2740 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2741 msg += MB_PTR2LEN(msg);
2742 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002743}
2744
2745/*
2746 * "term_start(command, options)" function
2747 */
2748 void
2749f_term_start(typval_T *argvars, typval_T *rettv)
2750{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002751 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002752
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002753 init_job_options(&opt);
2754 /* TODO: allow more job options */
2755 if (argvars[1].v_type != VAR_UNKNOWN
2756 && get_job_options(&argvars[1], &opt,
2757 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002758 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002759 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002760 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002761 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002762 return;
2763
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002764 if (opt.jo_vertical)
2765 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002766 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002767
2768 if (curbuf->b_term != NULL)
2769 rettv->vval.v_number = curbuf->b_fnum;
2770}
2771
2772/*
2773 * "term_wait" function
2774 */
2775 void
2776f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2777{
2778 buf_T *buf = term_get_buf(argvars);
2779
2780 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002781 {
2782 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002783 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002784 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002785 if (buf->b_term->tl_job == NULL)
2786 {
2787 ch_log(NULL, "term_wait(): no job to wait for");
2788 return;
2789 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002790
2791 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002792 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002793 {
2794 /* The job is dead, keep reading channel I/O until the channel is
2795 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002796 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002797 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2798 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002799 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002800 parse_queued_messages();
2801 ui_delay(10L, FALSE);
2802 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002803 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002804 parse_queued_messages();
2805 }
2806 else
2807 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002808 long wait = 10L;
2809
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002810 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002811 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002812
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002813 /* Wait for some time for any channel I/O. */
2814 if (argvars[1].v_type != VAR_UNKNOWN)
2815 wait = get_tv_number(&argvars[1]);
2816 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002817 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002818
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002819 /* Flushing messages on channels is hopefully sufficient.
2820 * TODO: is there a better way? */
2821 parse_queued_messages();
2822 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002823}
2824
Bram Moolenaara83e3962017-08-17 14:39:07 +02002825# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002826
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002827/**************************************
2828 * 2. MS-Windows implementation.
2829 */
2830
Bram Moolenaara83e3962017-08-17 14:39:07 +02002831# ifndef PROTO
2832
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002833#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2834#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2835
Bram Moolenaar8a773062017-07-24 22:29:21 +02002836void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002837void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002838void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002839BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2840void (*winpty_config_set_initial_size)(void*, int, int);
2841LPCWSTR (*winpty_conin_name)(void*);
2842LPCWSTR (*winpty_conout_name)(void*);
2843LPCWSTR (*winpty_conerr_name)(void*);
2844void (*winpty_free)(void*);
2845void (*winpty_config_free)(void*);
2846void (*winpty_spawn_config_free)(void*);
2847void (*winpty_error_free)(void*);
2848LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002849BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002850HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002851
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002852#define WINPTY_DLL "winpty.dll"
2853
2854static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002855# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002856
Bram Moolenaara83e3962017-08-17 14:39:07 +02002857 static int
2858dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002859{
2860 int i;
2861 static struct
2862 {
2863 char *name;
2864 FARPROC *ptr;
2865 } winpty_entry[] =
2866 {
2867 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2868 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2869 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2870 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2871 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2872 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2873 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2874 {"winpty_free", (FARPROC*)&winpty_free},
2875 {"winpty_open", (FARPROC*)&winpty_open},
2876 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2877 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2878 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2879 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002880 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002881 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002882 {NULL, NULL}
2883 };
2884
2885 /* No need to initialize twice. */
2886 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002887 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002888 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2889 * winpty.dll. */
2890 if (*p_winptydll != NUL)
2891 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2892 if (!hWinPtyDLL)
2893 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002894 if (!hWinPtyDLL)
2895 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002896 if (verbose)
2897 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2898 : (char_u *)WINPTY_DLL);
2899 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002900 }
2901 for (i = 0; winpty_entry[i].name != NULL
2902 && winpty_entry[i].ptr != NULL; ++i)
2903 {
2904 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2905 winpty_entry[i].name)) == NULL)
2906 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002907 if (verbose)
2908 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2909 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002910 }
2911 }
2912
Bram Moolenaara83e3962017-08-17 14:39:07 +02002913 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914}
2915
2916/*
2917 * Create a new terminal of "rows" by "cols" cells.
2918 * Store a reference in "term".
2919 * Return OK or FAIL.
2920 */
2921 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02002922term_and_job_init(
2923 term_T *term,
2924 int rows,
2925 int cols,
2926 typval_T *argvar,
2927 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928{
Bram Moolenaar5983d502017-08-20 19:22:56 +02002929 WCHAR *cmd_wchar = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002930 channel_T *channel = NULL;
2931 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002932 DWORD error;
Bram Moolenaar5983d502017-08-20 19:22:56 +02002933 HANDLE jo = NULL;
2934 HANDLE child_process_handle;
2935 HANDLE child_thread_handle;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002936 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002937 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002938 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002939 garray_T ga;
2940 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002941
Bram Moolenaara83e3962017-08-17 14:39:07 +02002942 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002943 return FAIL;
2944
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002945 if (argvar->v_type == VAR_STRING)
2946 cmd = argvar->vval.v_string;
2947 else
2948 {
2949 ga_init2(&ga, (int)sizeof(char*), 20);
2950 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2951 goto failed;
2952 cmd = ga.ga_data;
2953 }
2954
Bram Moolenaar5983d502017-08-20 19:22:56 +02002955 cmd_wchar = enc_to_utf16(cmd, NULL);
2956 if (cmd_wchar == NULL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002957 return FAIL;
2958
2959 job = job_alloc();
2960 if (job == NULL)
2961 goto failed;
2962
2963 channel = add_channel();
2964 if (channel == NULL)
2965 goto failed;
2966
2967 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2968 if (term->tl_winpty_config == NULL)
2969 goto failed;
2970
2971 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2972 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2973 if (term->tl_winpty == NULL)
2974 goto failed;
2975
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002976 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002977 spawn_config = winpty_spawn_config_new(
2978 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2979 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2980 NULL,
Bram Moolenaar5983d502017-08-20 19:22:56 +02002981 cmd_wchar,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002982 NULL,
2983 NULL,
2984 &winpty_err);
2985 if (spawn_config == NULL)
2986 goto failed;
2987
2988 channel = add_channel();
2989 if (channel == NULL)
2990 goto failed;
2991
2992 job = job_alloc();
2993 if (job == NULL)
2994 goto failed;
2995
Bram Moolenaar5983d502017-08-20 19:22:56 +02002996 /* TODO: when all lines are written and the fd is closed, the command
2997 * doesn't get EOF and hangs. */
2998 if (opt->jo_set & JO_IN_BUF)
2999 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
3000
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003001 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
3002 &child_thread_handle, &error, &winpty_err))
3003 goto failed;
3004
3005 channel_set_pipes(channel,
Bram Moolenaar5983d502017-08-20 19:22:56 +02003006 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007 winpty_conin_name(term->tl_winpty),
3008 GENERIC_WRITE, 0, NULL,
3009 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003010 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003011 winpty_conout_name(term->tl_winpty),
3012 GENERIC_READ, 0, NULL,
3013 OPEN_EXISTING, 0, NULL),
Bram Moolenaar5983d502017-08-20 19:22:56 +02003014 (sock_T)CreateFileW(
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003015 winpty_conerr_name(term->tl_winpty),
3016 GENERIC_READ, 0, NULL,
3017 OPEN_EXISTING, 0, NULL));
3018
3019 jo = CreateJobObject(NULL, NULL);
3020 if (jo == NULL)
3021 goto failed;
3022
3023 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003024 {
3025 /* Failed, switch the way to terminate process with TerminateProcess. */
3026 CloseHandle(jo);
3027 jo = NULL;
3028 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003029
3030 winpty_spawn_config_free(spawn_config);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003031 vim_free(cmd_wchar);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003032
3033 create_vterm(term, rows, cols);
3034
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02003035 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02003036 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003037
3038 job->jv_channel = channel;
3039 job->jv_proc_info.hProcess = child_process_handle;
3040 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
3041 job->jv_job_object = jo;
3042 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02003043 sprintf(buf, "winpty://%lu",
3044 GetProcessId(winpty_agent_process(term->tl_winpty)));
3045 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003046 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003047 term->tl_job = job;
3048
3049 return OK;
3050
3051failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003052 if (argvar->v_type == VAR_LIST)
3053 vim_free(ga.ga_data);
Bram Moolenaar5983d502017-08-20 19:22:56 +02003054 if (cmd_wchar != NULL)
3055 vim_free(cmd_wchar);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02003056 if (spawn_config != NULL)
3057 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003058 if (channel != NULL)
3059 channel_clear(channel);
3060 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003061 {
3062 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003063 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003064 }
3065 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003066 if (jo != NULL)
3067 CloseHandle(jo);
3068 if (term->tl_winpty != NULL)
3069 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003070 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003071 if (term->tl_winpty_config != NULL)
3072 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003073 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003074 if (winpty_err != NULL)
3075 {
3076 char_u *msg = utf16_to_enc(
3077 (short_u *)winpty_error_msg(winpty_err), NULL);
3078
3079 EMSG(msg);
3080 winpty_error_free(winpty_err);
3081 }
3082 return FAIL;
3083}
3084
3085/*
3086 * Free the terminal emulator part of "term".
3087 */
3088 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003089term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003090{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003091 if (term->tl_winpty != NULL)
3092 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003093 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003094 if (term->tl_winpty_config != NULL)
3095 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003096 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003097 if (term->tl_vterm != NULL)
3098 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02003099 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003100}
3101
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003102/*
3103 * Request size to terminal.
3104 */
3105 static void
3106term_report_winsize(term_T *term, int rows, int cols)
3107{
3108 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3109}
3110
Bram Moolenaara83e3962017-08-17 14:39:07 +02003111 int
3112terminal_enabled(void)
3113{
3114 return dyn_winpty_init(FALSE) == OK;
3115}
3116
3117
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003118# else
3119
3120/**************************************
3121 * 3. Unix-like implementation.
3122 */
3123
3124/*
3125 * Create a new terminal of "rows" by "cols" cells.
3126 * Start job for "cmd".
3127 * Store the pointers in "term".
3128 * Return OK or FAIL.
3129 */
3130 static int
Bram Moolenaarb2412082017-08-20 18:09:14 +02003131term_and_job_init(
3132 term_T *term,
3133 int rows,
3134 int cols,
3135 typval_T *argvar,
3136 jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003137{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003138 create_vterm(term, rows, cols);
3139
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003140 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003141 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003142 if (term->tl_job != NULL)
3143 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003144
Bram Moolenaar61a66052017-07-22 18:39:00 +02003145 return term->tl_job != NULL
3146 && term->tl_job->jv_channel != NULL
3147 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003148}
3149
3150/*
3151 * Free the terminal emulator part of "term".
3152 */
3153 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003154term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003155{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003156 if (term->tl_vterm != NULL)
3157 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003158 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003159}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003160
3161/*
3162 * Request size to terminal.
3163 */
3164 static void
3165term_report_winsize(term_T *term, int rows, int cols)
3166{
3167 /* Use an ioctl() to report the new window size to the job. */
3168 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3169 {
3170 int fd = -1;
3171 int part;
3172
3173 for (part = PART_OUT; part < PART_COUNT; ++part)
3174 {
3175 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3176 if (isatty(fd))
3177 break;
3178 }
3179 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003180 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003181 }
3182}
3183
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003184# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003185
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003186#endif /* FEAT_TERMINAL */