blob: 08e0f9b9382e9fb02993e21ed7426e44e238aba1 [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 Moolenaar9e13aa72017-08-16 23:14:08 +020041 * - make [range]terminal pipe [range] lines to the terminal
Bram Moolenaar94053a52017-08-01 21:44:33 +020042 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020043 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020044 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020045 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020046 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020047 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
48 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
49 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
50 * Check that something is connected to the terminal.
51 * Test: "cat" reading from a file or buffer
52 * "ls" writing stdout to a file or buffer
53 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020054 * - For the GUI fill termios with default values, perhaps like pangoterm:
55 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020056 * - support ":term NONE" to open a terminal with a pty but not running a job
57 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020058 * - if the job in the terminal does not support the mouse, we can use the
59 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020060 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
61 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020062 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020063 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020064 * - Copy text in the vterm to the Vim buffer once in a while, so that
65 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020066 */
67
68#include "vim.h"
69
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020070#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020071
Bram Moolenaard5310982017-08-05 15:16:32 +020072#ifndef MIN
73# define MIN(x,y) ((x) < (y) ? (x) : (y))
74#endif
75#ifndef MAX
76# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020077#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020079#include "libvterm/include/vterm.h"
80
Bram Moolenaar33a43be2017-08-06 21:36:22 +020081/* This is VTermScreenCell without the characters, thus much smaller. */
82typedef struct {
83 VTermScreenCellAttrs attrs;
84 char width;
85 VTermColor fg, bg;
86} cellattr_T;
87
Bram Moolenaard85f2712017-07-28 21:51:57 +020088typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089 int sb_cols; /* can differ per line */
90 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020091} sb_line_T;
92
Bram Moolenaare4f25e42017-07-07 11:54:15 +020093/* typedef term_T in structs.h */
94struct terminal_S {
95 term_T *tl_next;
96
Bram Moolenaar423802d2017-07-30 16:52:24 +020097 VTerm *tl_vterm;
98 job_T *tl_job;
99 buf_T *tl_buffer;
100
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200101 /* used when tl_job is NULL and only a pty was created */
102 int tl_tty_fd;
103 char_u *tl_tty_name;
104
Bram Moolenaar6d819742017-08-06 14:57:49 +0200105 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200106 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200107 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200108 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200109
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200110#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200111 void *tl_winpty_config;
112 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200113#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 /* last known vterm size */
116 int tl_rows;
117 int tl_cols;
118 /* vterm size does not follow window size */
119 int tl_rows_fixed;
120 int tl_cols_fixed;
121
Bram Moolenaar21554412017-07-24 21:44:43 +0200122 char_u *tl_title; /* NULL or allocated */
123 char_u *tl_status_text; /* NULL or allocated */
124
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125 /* Range of screen rows to update. Zero based. */
126 int tl_dirty_row_start; /* -1 if nothing dirty */
127 int tl_dirty_row_end; /* row below last one to update */
128
Bram Moolenaard85f2712017-07-28 21:51:57 +0200129 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200130 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200131
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200132 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200133 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200134 int tl_cursor_blink;
135 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
136 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200137
138 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200139};
140
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200141#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
142#define TMODE_LOOP 2 /* CTRL-W N used */
143
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200144/*
145 * List of all active terminals.
146 */
147static term_T *first_term = NULL;
148
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200149/* Terminal active in terminal_loop(). */
150static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200151
152#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
153#define KEY_BUF_LEN 200
154
155/*
156 * Functions with separate implementation for MS-Windows and Unix-like systems.
157 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200158static int term_and_job_init(term_T *term, int rows, int cols,
159 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200160static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200161static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200163/* The characters that we know (or assume) that the terminal expects for the
164 * backspace and enter keys. */
165static int term_backspace_char = BS;
166static int term_enter_char = CAR;
167static int term_nl_does_cr = FALSE;
168
169
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200170/**************************************
171 * 1. Generic code for all systems.
172 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200173
174/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200175 * Determine the terminal size from 'termsize' and the current window.
176 * Assumes term->tl_rows and term->tl_cols are zero.
177 */
178 static void
179set_term_and_win_size(term_T *term)
180{
181 if (*curwin->w_p_tms != NUL)
182 {
183 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
184
185 term->tl_rows = atoi((char *)curwin->w_p_tms);
186 term->tl_cols = atoi((char *)p);
187 }
188 if (term->tl_rows == 0)
189 term->tl_rows = curwin->w_height;
190 else
191 {
192 win_setheight_win(term->tl_rows, curwin);
193 term->tl_rows_fixed = TRUE;
194 }
195 if (term->tl_cols == 0)
196 term->tl_cols = curwin->w_width;
197 else
198 {
199 win_setwidth_win(term->tl_cols, curwin);
200 term->tl_cols_fixed = TRUE;
201 }
202}
203
204/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200205 * Initialize job options for a terminal job.
206 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200207 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 static void
209init_job_options(jobopt_T *opt)
210{
211 clear_job_options(opt);
212
213 opt->jo_mode = MODE_RAW;
214 opt->jo_out_mode = MODE_RAW;
215 opt->jo_err_mode = MODE_RAW;
216 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
217
218 opt->jo_io[PART_OUT] = JIO_BUFFER;
219 opt->jo_io[PART_ERR] = JIO_BUFFER;
220 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
221
222 opt->jo_modifiable[PART_OUT] = 0;
223 opt->jo_modifiable[PART_ERR] = 0;
224 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
225
226 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
227}
228
229/*
230 * Set job options mandatory for a terminal job.
231 */
232 static void
233setup_job_options(jobopt_T *opt, int rows, int cols)
234{
235 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
236 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
237 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200238 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
239 opt->jo_term_rows = rows;
240 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
241 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200242}
243
244 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200245term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247 exarg_T split_ea;
248 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200250 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251
252 if (check_restricted() || check_secure())
253 return;
254
255 term = (term_T *)alloc_clear(sizeof(term_T));
256 if (term == NULL)
257 return;
258 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200259 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200260 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200261 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200262 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200265 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200266 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 /* Create a new buffer in the current window. */
268 if (!can_abandon(curbuf, forceit))
269 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200270 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200271 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200272 return;
273 }
274 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
275 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200276 {
277 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200278 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 }
280 }
281 else if (opt->jo_hidden)
282 {
283 buf_T *buf;
284
285 /* Create a new buffer without a window. Make it the current buffer for
286 * a moment to be able to do the initialisations. */
287 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
288 BLN_NEW | BLN_LISTED);
289 if (buf == NULL || ml_open(buf) == FAIL)
290 {
291 vim_free(term);
292 return;
293 }
294 old_curbuf = curbuf;
295 --curbuf->b_nwindows;
296 curbuf = buf;
297 curwin->w_buffer = buf;
298 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200299 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200300 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 /* Open a new window or tab. */
303 split_ea.cmdidx = CMD_new;
304 split_ea.cmd = (char_u *)"new";
305 split_ea.arg = (char_u *)"";
306 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
307 {
308 split_ea.line2 = opt->jo_term_rows;
309 split_ea.addr_count = 1;
310 }
311 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
312 {
313 split_ea.line2 = opt->jo_term_cols;
314 split_ea.addr_count = 1;
315 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200316
Bram Moolenaarda43b612017-08-11 22:27:50 +0200317 ex_splitview(&split_ea);
318 if (curwin == old_curwin)
319 {
320 /* split failed */
321 vim_free(term);
322 return;
323 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200324 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200325 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200327
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200328 if (!opt->jo_hidden)
329 {
330 /* only one size was taken care of with :new, do the other one */
331 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
332 win_setheight(opt->jo_term_rows);
333 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
334 win_setwidth(opt->jo_term_cols);
335 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200336
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200337 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200338 term->tl_next = first_term;
339 first_term = term;
340
Bram Moolenaar78712a72017-08-05 14:50:12 +0200341 if (opt->jo_term_name != NULL)
342 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
343 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200344 {
345 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200346 size_t len;
347 char_u *cmd, *p;
348
349 if (argvar->v_type == VAR_STRING)
350 cmd = argvar->vval.v_string;
351 else if (argvar->v_type != VAR_LIST
352 || argvar->vval.v_list == NULL
353 || argvar->vval.v_list->lv_len < 1)
354 cmd = (char_u*)"";
355 else
356 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
357
358 len = STRLEN(cmd) + 10;
359 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200360
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200361 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200363 /* Prepend a ! to the command name to avoid the buffer name equals
364 * the executable, otherwise ":w!" would overwrite it. */
365 if (i == 0)
366 vim_snprintf((char *)p, len, "!%s", cmd);
367 else
368 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200369 if (buflist_findname(p) == NULL)
370 {
371 curbuf->b_ffname = p;
372 break;
373 }
374 }
375 }
376 curbuf->b_fname = curbuf->b_ffname;
377
Bram Moolenaar37c45832017-08-12 16:01:04 +0200378 if (opt->jo_term_opencmd != NULL)
379 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
380
Bram Moolenaareb44a682017-08-03 22:44:55 +0200381 set_string_option_direct((char_u *)"buftype", -1,
382 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
383
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200384 /* Mark the buffer as not modifiable. It can only be made modifiable after
385 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200386 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200387
388 /* Set 'bufhidden' to "hide": allow closing the window. */
389 set_string_option_direct((char_u *)"bufhidden", -1,
390 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200391
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200392 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200393 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200394
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200395 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200396 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
397 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200399 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200401 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200403 /* Make sure we don't get stuck on sending keys to the job, it leads to
404 * a deadlock if the job is waiting for Vim to read. */
405 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
406
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200407 if (old_curbuf != NULL)
408 {
409 --curbuf->b_nwindows;
410 curbuf = old_curbuf;
411 curwin->w_buffer = curbuf;
412 ++curbuf->b_nwindows;
413 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200414 }
415 else
416 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200417 buf_T *buf = curbuf;
418
Bram Moolenaard85f2712017-07-28 21:51:57 +0200419 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200420 if (old_curbuf != NULL)
421 {
422 --curbuf->b_nwindows;
423 curbuf = old_curbuf;
424 curwin->w_buffer = curbuf;
425 ++curbuf->b_nwindows;
426 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200427
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200428 /* Wiping out the buffer will also close the window and call
429 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200430 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200431 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200432}
433
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200434/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200435 * ":terminal": open a terminal window and execute a job in it.
436 */
437 void
438ex_terminal(exarg_T *eap)
439{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200440 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200441 jobopt_T opt;
442 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200443 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200444
445 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200446
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200447 cmd = eap->arg;
448 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
449 {
450 char_u *p;
451
452 cmd += 2;
453 p = skiptowhite(cmd);
454 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
455 opt.jo_term_finish = 'c';
456 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
457 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200458 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
459 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200460 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
461 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200462 else
463 {
464 if (*p)
465 *p = NUL;
466 EMSG2(_("E181: Invalid attribute: %s"), cmd);
467 return;
468 }
469 cmd = skipwhite(p);
470 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200471 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200472 /* Make a copy, an autocommand may set 'shell'. */
473 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200474
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200475 if (eap->addr_count == 2)
476 {
477 opt.jo_term_rows = eap->line1;
478 opt.jo_term_cols = eap->line2;
479 }
480 else if (eap->addr_count == 1)
481 {
482 if (cmdmod.split & WSP_VERT)
483 opt.jo_term_cols = eap->line2;
484 else
485 opt.jo_term_rows = eap->line2;
486 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200487
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200488 argvar.v_type = VAR_STRING;
489 argvar.vval.v_string = cmd;
490 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200491 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200492}
493
494/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200495 * Free the scrollback buffer for "term".
496 */
497 static void
498free_scrollback(term_T *term)
499{
500 int i;
501
502 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
503 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
504 ga_clear(&term->tl_scrollback);
505}
506
507/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200508 * Free a terminal and everything it refers to.
509 * Kills the job if there is one.
510 * Called when wiping out a buffer.
511 */
512 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200513free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200514{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200515 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200516 term_T *tp;
517
518 if (term == NULL)
519 return;
520 if (first_term == term)
521 first_term = term->tl_next;
522 else
523 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
524 if (tp->tl_next == term)
525 {
526 tp->tl_next = term->tl_next;
527 break;
528 }
529
530 if (term->tl_job != NULL)
531 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200532 if (term->tl_job->jv_status != JOB_ENDED
533 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200534 job_stop(term->tl_job, NULL, "kill");
535 job_unref(term->tl_job);
536 }
537
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200538 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200539
540 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200541 vim_free(term->tl_title);
542 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200543 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200544 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200545 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200546 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200547 if (in_terminal_loop == term)
548 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200549}
550
551/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200552 * Write job output "msg[len]" to the vterm.
553 */
554 static void
555term_write_job_output(term_T *term, char_u *msg, size_t len)
556{
557 VTerm *vterm = term->tl_vterm;
558 char_u *p;
559 size_t done;
560 size_t len_now;
561
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200562 if (term_nl_does_cr)
563 vterm_input_write(vterm, (char *)msg, len);
564 else
565 /* need to convert NL to CR-NL */
566 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200567 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200568 for (p = msg + done; p < msg + len; )
569 {
570 if (*p == NL)
571 break;
572 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
573 }
574 len_now = p - msg - done;
575 vterm_input_write(vterm, (char *)msg + done, len_now);
576 if (p < msg + len && *p == NL)
577 {
578 vterm_input_write(vterm, "\r\n", 2);
579 ++len_now;
580 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200581 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582
583 /* this invokes the damage callbacks */
584 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
585}
586
Bram Moolenaar1c844932017-07-24 23:36:41 +0200587 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200588update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200589{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200590 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200591 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200592 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200593 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200594 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200595 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200596 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200597 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200598#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200599 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200600 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200601#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200602 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200603}
604
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200605/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200606 * Invoked when "msg" output from a job was received. Write it to the terminal
607 * of "buffer".
608 */
609 void
610write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
611{
612 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200613 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200614
Bram Moolenaard85f2712017-07-28 21:51:57 +0200615 if (term->tl_vterm == NULL)
616 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200617 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200618 return;
619 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200620 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200621 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200622
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200623 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
624 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200625 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200626 {
627 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200628 ch_log(term->tl_job->jv_channel, "updating screen");
629 if (buffer == curbuf)
630 {
631 update_screen(0);
632 update_cursor(term, TRUE);
633 }
634 else
635 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200636 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200637}
638
639/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200640 * Send a mouse position and click to the vterm
641 */
642 static int
643term_send_mouse(VTerm *vterm, int button, int pressed)
644{
645 VTermModifier mod = VTERM_MOD_NONE;
646
647 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
648 mouse_col - W_WINCOL(curwin), mod);
649 vterm_mouse_button(vterm, button, pressed, mod);
650 return TRUE;
651}
652
653/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654 * Convert typed key "c" into bytes to send to the job.
655 * Return the number of bytes in "buf".
656 */
657 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200658term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200659{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200660 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 VTermKey key = VTERM_KEY_NONE;
662 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200663 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200664
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200665 switch (c)
666 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200667 case CAR: c = term_enter_char; break;
668 /* don't use VTERM_KEY_BACKSPACE, it always
669 * becomes 0x7f DEL */
670 case K_BS: c = term_backspace_char; break;
671
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case K_DEL: key = VTERM_KEY_DEL; break;
674 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200675 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
676 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200678 case K_S_END: mod = VTERM_MOD_SHIFT;
679 key = VTERM_KEY_END; break;
680 case K_C_END: mod = VTERM_MOD_CTRL;
681 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200682 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
683 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
684 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
685 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
686 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
687 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
688 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
689 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
690 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
691 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
692 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
693 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
694 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200695 case K_S_HOME: mod = VTERM_MOD_SHIFT;
696 key = VTERM_KEY_HOME; break;
697 case K_C_HOME: mod = VTERM_MOD_CTRL;
698 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200699 case K_INS: key = VTERM_KEY_INS; break;
700 case K_K0: key = VTERM_KEY_KP_0; break;
701 case K_K1: key = VTERM_KEY_KP_1; break;
702 case K_K2: key = VTERM_KEY_KP_2; break;
703 case K_K3: key = VTERM_KEY_KP_3; break;
704 case K_K4: key = VTERM_KEY_KP_4; break;
705 case K_K5: key = VTERM_KEY_KP_5; break;
706 case K_K6: key = VTERM_KEY_KP_6; break;
707 case K_K7: key = VTERM_KEY_KP_7; break;
708 case K_K8: key = VTERM_KEY_KP_8; break;
709 case K_K9: key = VTERM_KEY_KP_9; break;
710 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
711 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
712 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
713 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
714 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
715 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
716 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
717 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
718 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
719 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
720 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
721 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
722 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200723 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
724 key = VTERM_KEY_LEFT; break;
725 case K_C_LEFT: mod = VTERM_MOD_CTRL;
726 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200727 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
728 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
729 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200730 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
731 key = VTERM_KEY_RIGHT; break;
732 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
733 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200734 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200735 case K_S_UP: mod = VTERM_MOD_SHIFT;
736 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200737 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200738
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200739 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
740 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
741 case K_MOUSELEFT: /* TODO */ return 0;
742 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200743
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200744 case K_LEFTMOUSE:
745 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
746 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
747 case K_LEFTRELEASE:
748 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
749 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
750 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
751 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
752 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
753 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
754 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
755 case K_X1MOUSE: /* TODO */ return 0;
756 case K_X1DRAG: /* TODO */ return 0;
757 case K_X1RELEASE: /* TODO */ return 0;
758 case K_X2MOUSE: /* TODO */ return 0;
759 case K_X2DRAG: /* TODO */ return 0;
760 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200761
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200762 case K_IGNORE: return 0;
763 case K_NOP: return 0;
764 case K_UNDO: return 0;
765 case K_HELP: return 0;
766 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
767 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
768 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
769 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
770 case K_SELECT: return 0;
771#ifdef FEAT_GUI
772 case K_VER_SCROLLBAR: return 0;
773 case K_HOR_SCROLLBAR: return 0;
774#endif
775#ifdef FEAT_GUI_TABLINE
776 case K_TABLINE: return 0;
777 case K_TABMENU: return 0;
778#endif
779#ifdef FEAT_NETBEANS_INTG
780 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
781#endif
782#ifdef FEAT_DND
783 case K_DROP: return 0;
784#endif
785#ifdef FEAT_AUTOCMD
786 case K_CURSORHOLD: return 0;
787#endif
788 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
789 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200790 }
791
792 /*
793 * Convert special keys to vterm keys:
794 * - Write keys to vterm: vterm_keyboard_key()
795 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200796 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200797 */
798 if (key != VTERM_KEY_NONE)
799 /* Special key, let vterm convert it. */
800 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200801 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200802 /* Normal character, let vterm convert it. */
803 vterm_keyboard_unichar(vterm, c, mod);
804
805 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200806 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200807}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200808
Bram Moolenaar938783d2017-07-16 20:13:26 +0200809/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200810 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200811 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200812 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200813term_job_running(term_T *term)
814{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200815 /* Also consider the job finished when the channel is closed, to avoid a
816 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200817 return term != NULL
818 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200819 && term->tl_job->jv_status == JOB_STARTED
820 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200821}
822
823/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200824 * Add the last line of the scrollback buffer to the buffer in the window.
825 */
826 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200827add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200828{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200829 buf_T *buf = term->tl_buffer;
830 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
831 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200832
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200833 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200834 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200835 {
836 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200837 curbuf = buf;
838 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200839 curbuf = curwin->w_buffer;
840 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200841}
842
843/*
844 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200845 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200846 */
847 static void
848move_terminal_to_buffer(term_T *term)
849{
850 win_T *wp;
851 int len;
852 int lines_skipped = 0;
853 VTermPos pos;
854 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200855 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200856 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200857
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200858 if (term->tl_vterm == NULL)
859 return;
860 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200861 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
862 {
863 len = 0;
864 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
865 if (vterm_screen_get_cell(screen, pos, &cell) != 0
866 && cell.chars[0] != NUL)
867 len = pos.col + 1;
868
869 if (len == 0)
870 ++lines_skipped;
871 else
872 {
873 while (lines_skipped > 0)
874 {
875 /* Line was skipped, add an empty line. */
876 --lines_skipped;
877 if (ga_grow(&term->tl_scrollback, 1) == OK)
878 {
879 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
880 + term->tl_scrollback.ga_len;
881
882 line->sb_cols = 0;
883 line->sb_cells = NULL;
884 ++term->tl_scrollback.ga_len;
885
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200886 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 }
888 }
889
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200890 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
892 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 garray_T ga;
894 int width;
895 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 + term->tl_scrollback.ga_len;
897
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200898 ga_init2(&ga, 1, 100);
899 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200900 {
901 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200902 {
903 width = 1;
904 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
905 if (ga_grow(&ga, 1) == OK)
906 ga.ga_len += mb_char2bytes(' ',
907 (char_u *)ga.ga_data + ga.ga_len);
908 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200909 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200910 {
911 width = cell.width;
912
913 p[pos.col].width = cell.width;
914 p[pos.col].attrs = cell.attrs;
915 p[pos.col].fg = cell.fg;
916 p[pos.col].bg = cell.bg;
917
918 if (ga_grow(&ga, MB_MAXBYTES) == OK)
919 {
920 int i;
921 int c;
922
923 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
924 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
925 (char_u *)ga.ga_data + ga.ga_len);
926 }
927 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200928 }
929 line->sb_cols = len;
930 line->sb_cells = p;
931 ++term->tl_scrollback.ga_len;
932
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200933 if (ga_grow(&ga, 1) == FAIL)
934 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
935 else
936 {
937 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
938 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
939 }
940 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200941 }
942 else
943 vim_free(p);
944 }
945 }
946
947 FOR_ALL_WINDOWS(wp)
948 {
949 if (wp->w_buffer == term->tl_buffer)
950 {
951 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
952 wp->w_cursor.col = 0;
953 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200954 if (wp->w_cursor.lnum >= wp->w_height)
955 {
956 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
957
958 if (wp->w_topline < min_topline)
959 wp->w_topline = min_topline;
960 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200961 redraw_win_later(wp, NOT_VALID);
962 }
963 }
964}
965
966 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200967set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200968{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200969 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970 vim_free(term->tl_status_text);
971 term->tl_status_text = NULL;
972 if (term->tl_buffer == curbuf)
973 maketitle();
974}
975
976/*
977 * Called after the job if finished and Terminal mode is not active:
978 * Move the vterm contents into the scrollback buffer and free the vterm.
979 */
980 static void
981cleanup_vterm(term_T *term)
982{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200983 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200984 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200986 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200987}
988
989/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200990 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200991 * Suspends updating the terminal window.
992 */
993 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200994term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995{
996 term_T *term = curbuf->b_term;
997
998 /* Append the current terminal contents to the buffer. */
999 move_terminal_to_buffer(term);
1000
Bram Moolenaar6d819742017-08-06 14:57:49 +02001001 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001002
Bram Moolenaar6d819742017-08-06 14:57:49 +02001003 /* Move the window cursor to the position of the cursor in the
1004 * terminal. */
1005 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1006 + term->tl_cursor_pos.row + 1;
1007 check_cursor();
1008 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001009
Bram Moolenaar6d819742017-08-06 14:57:49 +02001010 /* Display the same lines as in the terminal. */
1011 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012}
1013
1014/*
1015 * Returns TRUE if the current window contains a terminal and we are in
1016 * Terminal-Normal mode.
1017 */
1018 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001019term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001020{
1021 term_T *term = curbuf->b_term;
1022
Bram Moolenaar6d819742017-08-06 14:57:49 +02001023 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001024}
1025
1026/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001027 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001028 * Restores updating the terminal window.
1029 */
1030 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001031term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001032{
1033 term_T *term = curbuf->b_term;
1034 sb_line_T *line;
1035 garray_T *gap;
1036
1037 /* Remove the terminal contents from the scrollback and the buffer. */
1038 gap = &term->tl_scrollback;
Bram Moolenaar77ac9b52017-08-19 21:23:05 +02001039 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1040 && gap->ga_len > 0)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001041 {
1042 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1043 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1044 vim_free(line->sb_cells);
1045 --gap->ga_len;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001046 }
1047 check_cursor();
1048
Bram Moolenaar6d819742017-08-06 14:57:49 +02001049 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001050
1051 if (term->tl_channel_closed)
1052 cleanup_vterm(term);
1053 redraw_buf_and_status_later(curbuf, NOT_VALID);
1054}
1055
1056/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001057 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001058 * Note: while waiting a terminal may be closed and freed if the channel is
1059 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001060 * TODO: use terminal mode mappings.
1061 */
1062 static int
1063term_vgetc()
1064{
1065 int c;
1066
1067 ++no_mapping;
1068 ++allow_keys;
1069 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001070#ifdef WIN3264
1071 ctrl_break_was_pressed = FALSE;
1072#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001073 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001074 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001075 --no_mapping;
1076 --allow_keys;
1077 return c;
1078}
1079
1080/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001081 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001082 * Return FAIL when the key needs to be handled in Normal mode.
1083 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001084 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001085 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001086send_keys_to_term(term_T *term, int c, int typed)
1087{
1088 char msg[KEY_BUF_LEN];
1089 size_t len;
1090 static int mouse_was_outside = FALSE;
1091 int dragging_outside = FALSE;
1092
1093 /* Catch keys that need to be handled as in Normal mode. */
1094 switch (c)
1095 {
1096 case NUL:
1097 case K_ZERO:
1098 if (typed)
1099 stuffcharReadbuff(c);
1100 return FAIL;
1101
1102 case K_IGNORE:
1103 return FAIL;
1104
1105 case K_LEFTDRAG:
1106 case K_MIDDLEDRAG:
1107 case K_RIGHTDRAG:
1108 case K_X1DRAG:
1109 case K_X2DRAG:
1110 dragging_outside = mouse_was_outside;
1111 /* FALLTHROUGH */
1112 case K_LEFTMOUSE:
1113 case K_LEFTMOUSE_NM:
1114 case K_LEFTRELEASE:
1115 case K_LEFTRELEASE_NM:
1116 case K_MIDDLEMOUSE:
1117 case K_MIDDLERELEASE:
1118 case K_RIGHTMOUSE:
1119 case K_RIGHTRELEASE:
1120 case K_X1MOUSE:
1121 case K_X1RELEASE:
1122 case K_X2MOUSE:
1123 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001124
1125 case K_MOUSEUP:
1126 case K_MOUSEDOWN:
1127 case K_MOUSELEFT:
1128 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001129 if (mouse_row < W_WINROW(curwin)
1130 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1131 || mouse_col < W_WINCOL(curwin)
1132 || mouse_col >= W_ENDCOL(curwin)
1133 || dragging_outside)
1134 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001135 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001136 if (typed)
1137 {
1138 stuffcharReadbuff(c);
1139 mouse_was_outside = TRUE;
1140 }
1141 return FAIL;
1142 }
1143 }
1144 if (typed)
1145 mouse_was_outside = FALSE;
1146
1147 /* Convert the typed key to a sequence of bytes for the job. */
1148 len = term_convert_key(term, c, msg);
1149 if (len > 0)
1150 /* TODO: if FAIL is returned, stop? */
1151 channel_send(term->tl_job->jv_channel, PART_IN,
1152 (char_u *)msg, (int)len, NULL);
1153
1154 return OK;
1155}
1156
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001157 static void
1158position_cursor(win_T *wp, VTermPos *pos)
1159{
1160 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1161 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1162 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1163}
1164
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001165/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001166 * Handle CTRL-W "": send register contents to the job.
1167 */
1168 static void
1169term_paste_register(int prev_c UNUSED)
1170{
1171 int c;
1172 list_T *l;
1173 listitem_T *item;
1174 long reglen = 0;
1175 int type;
1176
1177#ifdef FEAT_CMDL_INFO
1178 if (add_to_showcmd(prev_c))
1179 if (add_to_showcmd('"'))
1180 out_flush();
1181#endif
1182 c = term_vgetc();
1183#ifdef FEAT_CMDL_INFO
1184 clear_showcmd();
1185#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001186 if (!term_use_loop())
1187 /* job finished while waiting for a character */
1188 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001189
1190 /* CTRL-W "= prompt for expression to evaluate. */
1191 if (c == '=' && get_expr_register() != '=')
1192 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001193 if (!term_use_loop())
1194 /* job finished while waiting for a character */
1195 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001196
1197 l = (list_T *)get_reg_contents(c, GREG_LIST);
1198 if (l != NULL)
1199 {
1200 type = get_reg_type(c, &reglen);
1201 for (item = l->lv_first; item != NULL; item = item->li_next)
1202 {
1203 char_u *s = get_tv_string(&item->li_tv);
1204
1205 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1206 s, STRLEN(s), NULL);
1207 if (item->li_next != NULL || type == MLINE)
1208 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1209 (char_u *)"\r", 1, NULL);
1210 }
1211 list_free(l);
1212 }
1213}
1214
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001215#if defined(FEAT_GUI) || defined(PROTO)
1216/*
1217 * Return TRUE when the cursor of the terminal should be displayed.
1218 */
1219 int
1220use_terminal_cursor()
1221{
1222 return in_terminal_loop != NULL;
1223}
1224
1225 cursorentry_T *
1226term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1227{
1228 term_T *term = in_terminal_loop;
1229 static cursorentry_T entry;
1230
1231 vim_memset(&entry, 0, sizeof(entry));
1232 entry.shape = entry.mshape =
1233 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1234 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1235 SHAPE_BLOCK;
1236 entry.percentage = 20;
1237 if (term->tl_cursor_blink)
1238 {
1239 entry.blinkwait = 700;
1240 entry.blinkon = 400;
1241 entry.blinkon = 250;
1242 }
1243 *fg = gui.back_pixel;
1244 if (term->tl_cursor_color == NULL)
1245 *bg = gui.norm_pixel;
1246 else
1247 *bg = color_name2handle(term->tl_cursor_color);
1248 entry.name = "n";
1249 entry.used_for = SHAPE_CURSOR;
1250
1251 return &entry;
1252}
1253#endif
1254
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001255static int did_change_cursor = FALSE;
1256
1257 static void
1258may_set_cursor_props(term_T *term)
1259{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001260#ifdef FEAT_GUI
1261 /* For the GUI the cursor properties are obtained with
1262 * term_get_cursor_shape(). */
1263 if (gui.in_use)
1264 return;
1265#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001266 if (in_terminal_loop == term)
1267 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001268 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001269 if (term->tl_cursor_color != NULL)
1270 term_cursor_color(term->tl_cursor_color);
1271 else
1272 term_cursor_color((char_u *)"");
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001273 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1274 }
1275}
1276
1277 static void
1278may_restore_cursor_props(void)
1279{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001280#ifdef FEAT_GUI
1281 if (gui.in_use)
1282 return;
1283#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001284 if (did_change_cursor)
1285 {
1286 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001287 term_cursor_color((char_u *)"");
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001288 /* this will restore the initial cursor style, if possible */
1289 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001290 }
1291}
1292
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001293/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001294 * Returns TRUE if the current window contains a terminal and we are sending
1295 * keys to the job.
1296 */
1297 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001298term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001299{
1300 term_T *term = curbuf->b_term;
1301
1302 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001303 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001304 && term->tl_vterm != NULL
1305 && term_job_running(term);
1306}
1307
1308/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001309 * Wait for input and send it to the job.
1310 * Return when the start of a CTRL-W command is typed or anything else that
1311 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001312 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1313 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001314 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001315 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001316terminal_loop(void)
1317{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001318 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001319 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001320 int ret;
1321
Bram Moolenaar679653e2017-08-13 14:13:19 +02001322 /* Remember the terminal we are sending keys to. However, the terminal
1323 * might be closed while waiting for a character, e.g. typing "exit" in a
1324 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1325 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001326 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001327
1328 if (*curwin->w_p_tk != NUL)
1329 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001330 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001331 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001332
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001333#ifdef UNIX
1334 {
1335 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1336
1337 if (isatty(fd))
1338 {
1339 ttyinfo_T info;
1340
1341 /* Get the current backspace and enter characters of the pty. */
1342 if (get_tty_info(fd, &info) == OK)
1343 {
1344 term_backspace_char = info.backspace;
1345 term_enter_char = info.enter;
1346 term_nl_does_cr = info.nl_does_cr;
1347 }
1348 }
1349 }
1350#endif
1351
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001352 for (;;)
1353 {
1354 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001355 /* Repeat redrawing in case a message is received while redrawing. */
1356 while (curwin->w_redr_type != 0)
1357 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001358 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001359
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001360 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001361 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001362 /* job finished while waiting for a character */
1363 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001364 if (c == K_IGNORE)
1365 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001366
Bram Moolenaarfae42832017-08-01 22:24:26 +02001367#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001368 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001369 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001370 if (ctrl_break_was_pressed)
1371 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001372#endif
1373
Bram Moolenaar69198192017-08-05 14:10:48 +02001374 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001375 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001376 int prev_c = c;
1377
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001378#ifdef FEAT_CMDL_INFO
1379 if (add_to_showcmd(c))
1380 out_flush();
1381#endif
1382 c = term_vgetc();
1383#ifdef FEAT_CMDL_INFO
1384 clear_showcmd();
1385#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001386 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001387 /* job finished while waiting for a character */
1388 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001389
Bram Moolenaar69198192017-08-05 14:10:48 +02001390 if (prev_c == Ctrl_BSL)
1391 {
1392 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001393 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001394 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1395 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001396 ret = FAIL;
1397 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001398 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001399 /* Send both keys to the terminal. */
1400 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1401 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001402 else if (c == Ctrl_C)
1403 {
1404 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1405 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1406 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001407 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001408 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001409 /* "CTRL-W .": send CTRL-W to the job */
1410 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001411 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001412 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001413 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001414 /* CTRL-W N : go to Terminal-Normal mode. */
1415 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001416 ret = FAIL;
1417 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001418 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001419 else if (c == '"')
1420 {
1421 term_paste_register(prev_c);
1422 continue;
1423 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001424 else if (termkey == 0 || c != termkey)
1425 {
1426 stuffcharReadbuff(Ctrl_W);
1427 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001428 ret = OK;
1429 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001430 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001431 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001432 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001433 {
1434 ret = OK;
1435 goto theend;
1436 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001437 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001438 ret = FAIL;
1439
1440theend:
1441 in_terminal_loop = NULL;
1442 may_restore_cursor_props();
1443 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001444}
1445
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001446/*
1447 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001448 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001449 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001450 */
1451 void
1452term_job_ended(job_T *job)
1453{
1454 term_T *term;
1455 int did_one = FALSE;
1456
1457 for (term = first_term; term != NULL; term = term->tl_next)
1458 if (term->tl_job == job)
1459 {
1460 vim_free(term->tl_title);
1461 term->tl_title = NULL;
1462 vim_free(term->tl_status_text);
1463 term->tl_status_text = NULL;
1464 redraw_buf_and_status_later(term->tl_buffer, VALID);
1465 did_one = TRUE;
1466 }
1467 if (did_one)
1468 redraw_statuslines();
1469 if (curbuf->b_term != NULL)
1470 {
1471 if (curbuf->b_term->tl_job == job)
1472 maketitle();
1473 update_cursor(curbuf->b_term, TRUE);
1474 }
1475}
1476
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001477 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001478may_toggle_cursor(term_T *term)
1479{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001480 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001481 {
1482 if (term->tl_cursor_visible)
1483 cursor_on();
1484 else
1485 cursor_off();
1486 }
1487}
1488
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001489/*
1490 * Reverse engineer the RGB value into a cterm color index.
1491 * First color is 1. Return 0 if no match found.
1492 */
1493 static int
1494color2index(VTermColor *color, int fg, int *boldp)
1495{
1496 int red = color->red;
1497 int blue = color->blue;
1498 int green = color->green;
1499
1500 /* The argument for lookup_color() is for the color_names[] table. */
1501 if (red == 0)
1502 {
1503 if (green == 0)
1504 {
1505 if (blue == 0)
1506 return lookup_color(0, fg, boldp) + 1; /* black */
1507 if (blue == 224)
1508 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1509 }
1510 else if (green == 224)
1511 {
1512 if (blue == 0)
1513 return lookup_color(2, fg, boldp) + 1; /* dark green */
1514 if (blue == 224)
1515 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1516 }
1517 }
1518 else if (red == 224)
1519 {
1520 if (green == 0)
1521 {
1522 if (blue == 0)
1523 return lookup_color(4, fg, boldp) + 1; /* dark red */
1524 if (blue == 224)
1525 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1526 }
1527 else if (green == 224)
1528 {
1529 if (blue == 0)
1530 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1531 if (blue == 224)
1532 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1533 }
1534 }
1535 else if (red == 128)
1536 {
1537 if (green == 128 && blue == 128)
1538 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1539 }
1540 else if (red == 255)
1541 {
1542 if (green == 64)
1543 {
1544 if (blue == 64)
1545 return lookup_color(20, fg, boldp) + 1; /* light red */
1546 if (blue == 255)
1547 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1548 }
1549 else if (green == 255)
1550 {
1551 if (blue == 64)
1552 return lookup_color(24, fg, boldp) + 1; /* yellow */
1553 if (blue == 255)
1554 return lookup_color(26, fg, boldp) + 1; /* white */
1555 }
1556 }
1557 else if (red == 64)
1558 {
1559 if (green == 64)
1560 {
1561 if (blue == 255)
1562 return lookup_color(14, fg, boldp) + 1; /* light blue */
1563 }
1564 else if (green == 255)
1565 {
1566 if (blue == 64)
1567 return lookup_color(16, fg, boldp) + 1; /* light green */
1568 if (blue == 255)
1569 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1570 }
1571 }
1572 if (t_colors >= 256)
1573 {
1574 if (red == blue && red == green)
1575 {
1576 /* 24-color greyscale */
1577 static int cutoff[23] = {
1578 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1579 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1580 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1581 int i;
1582
1583 for (i = 0; i < 23; ++i)
1584 if (red < cutoff[i])
1585 return i + 233;
1586 return 256;
1587 }
1588
1589 /* 216-color cube */
1590 return 17 + ((red + 25) / 0x33) * 36
1591 + ((green + 25) / 0x33) * 6
1592 + (blue + 25) / 0x33;
1593 }
1594 return 0;
1595}
1596
1597/*
1598 * Convert the attributes of a vterm cell into an attribute index.
1599 */
1600 static int
1601cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1602{
1603 int attr = 0;
1604
1605 if (cellattrs.bold)
1606 attr |= HL_BOLD;
1607 if (cellattrs.underline)
1608 attr |= HL_UNDERLINE;
1609 if (cellattrs.italic)
1610 attr |= HL_ITALIC;
1611 if (cellattrs.strike)
1612 attr |= HL_STANDOUT;
1613 if (cellattrs.reverse)
1614 attr |= HL_INVERSE;
1615
1616#ifdef FEAT_GUI
1617 if (gui.in_use)
1618 {
1619 guicolor_T fg, bg;
1620
1621 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1622 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1623 return get_gui_attr_idx(attr, fg, bg);
1624 }
1625 else
1626#endif
1627#ifdef FEAT_TERMGUICOLORS
1628 if (p_tgc)
1629 {
1630 guicolor_T fg, bg;
1631
1632 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1633 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1634
1635 return get_tgc_attr_idx(attr, fg, bg);
1636 }
1637 else
1638#endif
1639 {
1640 int bold = MAYBE;
1641 int fg = color2index(&cellfg, TRUE, &bold);
1642 int bg = color2index(&cellbg, FALSE, &bold);
1643
1644 /* with 8 colors set the bold attribute to get a bright foreground */
1645 if (bold == TRUE)
1646 attr |= HL_BOLD;
1647 return get_cterm_attr_idx(attr, fg, bg);
1648 }
1649 return 0;
1650}
1651
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001652 static int
1653handle_damage(VTermRect rect, void *user)
1654{
1655 term_T *term = (term_T *)user;
1656
1657 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1658 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1659 redraw_buf_later(term->tl_buffer, NOT_VALID);
1660 return 1;
1661}
1662
1663 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001664handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001665{
1666 term_T *term = (term_T *)user;
1667
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001668 /* Scrolling up is done much more efficiently by deleting lines instead of
1669 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001670 if (dest.start_col == src.start_col
1671 && dest.end_col == src.end_col
1672 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001673 {
1674 win_T *wp;
1675 VTermColor fg, bg;
1676 VTermScreenCellAttrs attr;
1677 int clear_attr;
1678
1679 /* Set the color to clear lines with. */
1680 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1681 &fg, &bg);
1682 vim_memset(&attr, 0, sizeof(attr));
1683 clear_attr = cell2attr(attr, fg, bg);
1684
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001685 FOR_ALL_WINDOWS(wp)
1686 {
1687 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001688 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001689 src.start_row - dest.start_row, FALSE, FALSE,
1690 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001691 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001692 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001693 redraw_buf_later(term->tl_buffer, NOT_VALID);
1694 return 1;
1695}
1696
1697 static int
1698handle_movecursor(
1699 VTermPos pos,
1700 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001701 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001702 void *user)
1703{
1704 term_T *term = (term_T *)user;
1705 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001706
1707 term->tl_cursor_pos = pos;
1708 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001709
1710 FOR_ALL_WINDOWS(wp)
1711 {
1712 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001713 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001715 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001716 {
1717 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001718 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001719 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001720
1721 return 1;
1722}
1723
Bram Moolenaar21554412017-07-24 21:44:43 +02001724 static int
1725handle_settermprop(
1726 VTermProp prop,
1727 VTermValue *value,
1728 void *user)
1729{
1730 term_T *term = (term_T *)user;
1731
1732 switch (prop)
1733 {
1734 case VTERM_PROP_TITLE:
1735 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001736 /* a blank title isn't useful, make it empty, so that "running" is
1737 * displayed */
1738 if (*skipwhite((char_u *)value->string) == NUL)
1739 term->tl_title = NULL;
1740 else
1741 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001742 vim_free(term->tl_status_text);
1743 term->tl_status_text = NULL;
1744 if (term == curbuf->b_term)
1745 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001746 break;
1747
1748 case VTERM_PROP_CURSORVISIBLE:
1749 term->tl_cursor_visible = value->boolean;
1750 may_toggle_cursor(term);
1751 out_flush();
1752 break;
1753
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001754 case VTERM_PROP_CURSORBLINK:
1755 term->tl_cursor_blink = value->boolean;
1756 may_set_cursor_props(term);
1757 break;
1758
1759 case VTERM_PROP_CURSORSHAPE:
1760 term->tl_cursor_shape = value->number;
1761 may_set_cursor_props(term);
1762 break;
1763
1764 case VTERM_PROP_CURSORCOLOR:
1765 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001766 if (*value->string == NUL)
1767 term->tl_cursor_color = NULL;
1768 else
1769 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001770 may_set_cursor_props(term);
1771 break;
1772
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001773 case VTERM_PROP_ALTSCREEN:
1774 /* TODO: do anything else? */
1775 term->tl_using_altscreen = value->boolean;
1776 break;
1777
Bram Moolenaar21554412017-07-24 21:44:43 +02001778 default:
1779 break;
1780 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001781 /* Always return 1, otherwise vterm doesn't store the value internally. */
1782 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001783}
1784
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001785/*
1786 * The job running in the terminal resized the terminal.
1787 */
1788 static int
1789handle_resize(int rows, int cols, void *user)
1790{
1791 term_T *term = (term_T *)user;
1792 win_T *wp;
1793
1794 term->tl_rows = rows;
1795 term->tl_cols = cols;
1796 FOR_ALL_WINDOWS(wp)
1797 {
1798 if (wp->w_buffer == term->tl_buffer)
1799 {
1800 win_setheight_win(rows, wp);
1801 win_setwidth_win(cols, wp);
1802 }
1803 }
1804
1805 redraw_buf_later(term->tl_buffer, NOT_VALID);
1806 return 1;
1807}
1808
Bram Moolenaard85f2712017-07-28 21:51:57 +02001809/*
1810 * Handle a line that is pushed off the top of the screen.
1811 */
1812 static int
1813handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1814{
1815 term_T *term = (term_T *)user;
1816
1817 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001818 if (ga_grow(&term->tl_scrollback, 1) == OK)
1819 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001820 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001821 int len = 0;
1822 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001823 int c;
1824 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001825 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001826 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001827
1828 /* do not store empty cells at the end */
1829 for (i = 0; i < cols; ++i)
1830 if (cells[i].chars[0] != 0)
1831 len = i + 1;
1832
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001833 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001834 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001835 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001836 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001837 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001838 for (col = 0; col < len; col += cells[col].width)
1839 {
1840 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1841 {
1842 ga.ga_len = 0;
1843 break;
1844 }
1845 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1846 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1847 (char_u *)ga.ga_data + ga.ga_len);
1848 p[col].width = cells[col].width;
1849 p[col].attrs = cells[col].attrs;
1850 p[col].fg = cells[col].fg;
1851 p[col].bg = cells[col].bg;
1852 }
1853 }
1854 if (ga_grow(&ga, 1) == FAIL)
1855 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1856 else
1857 {
1858 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1859 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1860 }
1861 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001862
1863 line = (sb_line_T *)term->tl_scrollback.ga_data
1864 + term->tl_scrollback.ga_len;
1865 line->sb_cols = len;
1866 line->sb_cells = p;
1867 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001868 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001869 }
1870 return 0; /* ignored */
1871}
1872
Bram Moolenaar21554412017-07-24 21:44:43 +02001873static VTermScreenCallbacks screen_callbacks = {
1874 handle_damage, /* damage */
1875 handle_moverect, /* moverect */
1876 handle_movecursor, /* movecursor */
1877 handle_settermprop, /* settermprop */
1878 NULL, /* bell */
1879 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001880 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001881 NULL /* sb_popline */
1882};
1883
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001884/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001885 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001886 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001887 */
1888 void
1889term_channel_closed(channel_T *ch)
1890{
1891 term_T *term;
1892 int did_one = FALSE;
1893
1894 for (term = first_term; term != NULL; term = term->tl_next)
1895 if (term->tl_job == ch->ch_job)
1896 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001897 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001898 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001899
Bram Moolenaard85f2712017-07-28 21:51:57 +02001900 vim_free(term->tl_title);
1901 term->tl_title = NULL;
1902 vim_free(term->tl_status_text);
1903 term->tl_status_text = NULL;
1904
Bram Moolenaar423802d2017-07-30 16:52:24 +02001905 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001906 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001907 {
1908 int fnum = term->tl_buffer->b_fnum;
1909
Bram Moolenaar423802d2017-07-30 16:52:24 +02001910 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001911
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001912 if (term->tl_finish == 'c')
1913 {
1914 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001915 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001916 curbuf = term->tl_buffer;
1917 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1918 break;
1919 }
1920 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1921 {
1922 char buf[50];
1923
1924 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001925 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001926 vim_snprintf(buf, sizeof(buf),
1927 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001928 ? "botright sbuf %d"
1929 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001930 do_cmdline_cmd((char_u *)buf);
1931 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001932 else
1933 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001934 }
1935
Bram Moolenaard85f2712017-07-28 21:51:57 +02001936 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001937 }
1938 if (did_one)
1939 {
1940 redraw_statuslines();
1941
1942 /* Need to break out of vgetc(). */
1943 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001944 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001945
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001946 term = curbuf->b_term;
1947 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001948 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001949 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001950 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001951 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001952 }
1953 }
1954}
1955
1956/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001957 * Called to update a window that contains an active terminal.
1958 * Returns FAIL when there is no terminal running in this window or in
1959 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001960 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001961 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001962term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001963{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001964 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001965 VTerm *vterm;
1966 VTermScreen *screen;
1967 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001968 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001969
Bram Moolenaar6d819742017-08-06 14:57:49 +02001970 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001971 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001972
Bram Moolenaard85f2712017-07-28 21:51:57 +02001973 vterm = term->tl_vterm;
1974 screen = vterm_obtain_screen(vterm);
1975 state = vterm_obtain_state(vterm);
1976
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001977 /*
1978 * If the window was resized a redraw will be triggered and we get here.
1979 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1980 */
1981 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1982 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001983 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001984 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1985 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1986 win_T *twp;
1987
1988 FOR_ALL_WINDOWS(twp)
1989 {
1990 /* When more than one window shows the same terminal, use the
1991 * smallest size. */
1992 if (twp->w_buffer == term->tl_buffer)
1993 {
1994 if (!term->tl_rows_fixed && rows > twp->w_height)
1995 rows = twp->w_height;
1996 if (!term->tl_cols_fixed && cols > twp->w_width)
1997 cols = twp->w_width;
1998 }
1999 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002000
2001 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002002 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002003 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002004 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002005 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002006
2007 /* The cursor may have been moved when resizing. */
2008 vterm_state_get_cursorpos(state, &pos);
2009 position_cursor(wp, &pos);
2010
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002011 /* TODO: Only redraw what changed. */
2012 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002013 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002014 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002015 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002016
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002017 if (pos.row < term->tl_rows)
2018 {
2019 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002020 {
2021 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002022 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002023
Bram Moolenaareeac6772017-07-23 15:48:37 +02002024 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2025 vim_memset(&cell, 0, sizeof(cell));
2026
2027 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002028 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002029 if (c == NUL)
2030 {
2031 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002032#if defined(FEAT_MBYTE)
2033 if (enc_utf8)
2034 ScreenLinesUC[off] = NUL;
2035#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002036 }
2037 else
2038 {
2039#if defined(FEAT_MBYTE)
2040 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002041 {
2042 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002043 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002044 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002045 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002046 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002047 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002048 if (enc_utf8)
2049 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002050 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002051#else
2052 ScreenLines[off] = c;
2053#endif
2054 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002055 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002056
2057 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002058 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002059 if (cell.width == 2)
2060 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002061 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002062#if defined(FEAT_MBYTE)
2063 if (enc_utf8)
2064 ScreenLinesUC[off] = NUL;
2065#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002066 ++pos.col;
2067 ++off;
2068 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002069 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002070 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002071 else
2072 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002073
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002074 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2075 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002076 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002077
2078 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002079}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002080
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002081/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002082 * Return TRUE if "wp" is a terminal window where the job has finished.
2083 */
2084 int
2085term_is_finished(buf_T *buf)
2086{
2087 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2088}
2089
2090/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002091 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002092 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002093 */
2094 int
2095term_show_buffer(buf_T *buf)
2096{
2097 term_T *term = buf->b_term;
2098
Bram Moolenaar6d819742017-08-06 14:57:49 +02002099 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002100}
2101
2102/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002103 * The current buffer is going to be changed. If there is terminal
2104 * highlighting remove it now.
2105 */
2106 void
2107term_change_in_curbuf(void)
2108{
2109 term_T *term = curbuf->b_term;
2110
2111 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2112 {
2113 free_scrollback(term);
2114 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002115
2116 /* The buffer is now like a normal buffer, it cannot be easily
2117 * abandoned when changed. */
2118 set_string_option_direct((char_u *)"buftype", -1,
2119 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002120 }
2121}
2122
2123/*
2124 * Get the screen attribute for a position in the buffer.
2125 */
2126 int
2127term_get_attr(buf_T *buf, linenr_T lnum, int col)
2128{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002129 term_T *term = buf->b_term;
2130 sb_line_T *line;
2131 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002132
Bram Moolenaar70229f92017-07-29 16:01:53 +02002133 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002134 return 0;
2135 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2136 if (col >= line->sb_cols)
2137 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002138 cellattr = line->sb_cells + col;
2139 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002140}
2141
2142/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002143 * Create a new vterm and initialize it.
2144 */
2145 static void
2146create_vterm(term_T *term, int rows, int cols)
2147{
2148 VTerm *vterm;
2149 VTermScreen *screen;
2150
2151 vterm = vterm_new(rows, cols);
2152 term->tl_vterm = vterm;
2153 screen = vterm_obtain_screen(vterm);
2154 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2155 /* TODO: depends on 'encoding'. */
2156 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002157
2158 /* Vterm uses a default black background. Set it to white when
2159 * 'background' is "light". */
2160 if (*p_bg == 'l')
2161 {
2162 VTermColor fg, bg;
2163
2164 fg.red = fg.green = fg.blue = 0;
2165 bg.red = bg.green = bg.blue = 255;
2166 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2167 }
2168
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002169 /* Required to initialize most things. */
2170 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002171
2172 /* Allow using alternate screen. */
2173 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002174}
2175
Bram Moolenaar21554412017-07-24 21:44:43 +02002176/*
2177 * Return the text to show for the buffer name and status.
2178 */
2179 char_u *
2180term_get_status_text(term_T *term)
2181{
2182 if (term->tl_status_text == NULL)
2183 {
2184 char_u *txt;
2185 size_t len;
2186
Bram Moolenaar6d819742017-08-06 14:57:49 +02002187 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002188 {
2189 if (term_job_running(term))
2190 txt = (char_u *)_("Terminal");
2191 else
2192 txt = (char_u *)_("Terminal-finished");
2193 }
2194 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002195 txt = term->tl_title;
2196 else if (term_job_running(term))
2197 txt = (char_u *)_("running");
2198 else
2199 txt = (char_u *)_("finished");
2200 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002201 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002202 if (term->tl_status_text != NULL)
2203 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2204 term->tl_buffer->b_fname, txt);
2205 }
2206 return term->tl_status_text;
2207}
2208
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002209/*
2210 * Mark references in jobs of terminals.
2211 */
2212 int
2213set_ref_in_term(int copyID)
2214{
2215 int abort = FALSE;
2216 term_T *term;
2217 typval_T tv;
2218
2219 for (term = first_term; term != NULL; term = term->tl_next)
2220 if (term->tl_job != NULL)
2221 {
2222 tv.v_type = VAR_JOB;
2223 tv.vval.v_job = term->tl_job;
2224 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2225 }
2226 return abort;
2227}
2228
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002229/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002230 * Get the buffer from the first argument in "argvars".
2231 * Returns NULL when the buffer is not for a terminal window.
2232 */
2233 static buf_T *
2234term_get_buf(typval_T *argvars)
2235{
2236 buf_T *buf;
2237
2238 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2239 ++emsg_off;
2240 buf = get_buf_tv(&argvars[0], FALSE);
2241 --emsg_off;
2242 if (buf == NULL || buf->b_term == NULL)
2243 return NULL;
2244 return buf;
2245}
2246
2247/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002248 * "term_getaltscreen(buf)" function
2249 */
2250 void
2251f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2252{
2253 buf_T *buf = term_get_buf(argvars);
2254
2255 if (buf == NULL)
2256 return;
2257 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2258}
2259
2260/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002261 * "term_getattr(attr, name)" function
2262 */
2263 void
2264f_term_getattr(typval_T *argvars, typval_T *rettv)
2265{
2266 int attr;
2267 size_t i;
2268 char_u *name;
2269
2270 static struct {
2271 char *name;
2272 int attr;
2273 } attrs[] = {
2274 {"bold", HL_BOLD},
2275 {"italic", HL_ITALIC},
2276 {"underline", HL_UNDERLINE},
2277 {"strike", HL_STANDOUT},
2278 {"reverse", HL_INVERSE},
2279 };
2280
2281 attr = get_tv_number(&argvars[0]);
2282 name = get_tv_string_chk(&argvars[1]);
2283 if (name == NULL)
2284 return;
2285
2286 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2287 if (STRCMP(name, attrs[i].name) == 0)
2288 {
2289 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2290 break;
2291 }
2292}
2293
2294/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002295 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002296 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002297 void
2298f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002299{
Bram Moolenaar97870002017-07-30 18:28:38 +02002300 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002301 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002302 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002303 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002304
Bram Moolenaar97870002017-07-30 18:28:38 +02002305 if (rettv_list_alloc(rettv) == FAIL)
2306 return;
2307 if (buf == NULL)
2308 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002309 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002310
2311 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002312 list_append_number(l, term->tl_cursor_pos.row + 1);
2313 list_append_number(l, term->tl_cursor_pos.col + 1);
2314
2315 d = dict_alloc();
2316 if (d != NULL)
2317 {
2318 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2319 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2320 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2321 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2322 ? (char_u *)"" : term->tl_cursor_color);
2323 list_append_dict(l, d);
2324 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002325}
2326
2327/*
2328 * "term_getjob(buf)" function
2329 */
2330 void
2331f_term_getjob(typval_T *argvars, typval_T *rettv)
2332{
2333 buf_T *buf = term_get_buf(argvars);
2334
2335 rettv->v_type = VAR_JOB;
2336 rettv->vval.v_job = NULL;
2337 if (buf == NULL)
2338 return;
2339
2340 rettv->vval.v_job = buf->b_term->tl_job;
2341 if (rettv->vval.v_job != NULL)
2342 ++rettv->vval.v_job->jv_refcount;
2343}
2344
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002345 static int
2346get_row_number(typval_T *tv, term_T *term)
2347{
2348 if (tv->v_type == VAR_STRING
2349 && tv->vval.v_string != NULL
2350 && STRCMP(tv->vval.v_string, ".") == 0)
2351 return term->tl_cursor_pos.row;
2352 return (int)get_tv_number(tv) - 1;
2353}
2354
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002355/*
2356 * "term_getline(buf, row)" function
2357 */
2358 void
2359f_term_getline(typval_T *argvars, typval_T *rettv)
2360{
2361 buf_T *buf = term_get_buf(argvars);
2362 term_T *term;
2363 int row;
2364
2365 rettv->v_type = VAR_STRING;
2366 if (buf == NULL)
2367 return;
2368 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002369 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002370
2371 if (term->tl_vterm == NULL)
2372 {
2373 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2374
2375 /* vterm is finished, get the text from the buffer */
2376 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2377 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2378 }
2379 else
2380 {
2381 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2382 VTermRect rect;
2383 int len;
2384 char_u *p;
2385
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002386 if (row < 0 || row >= term->tl_rows)
2387 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002388 len = term->tl_cols * MB_MAXBYTES + 1;
2389 p = alloc(len);
2390 if (p == NULL)
2391 return;
2392 rettv->vval.v_string = p;
2393
2394 rect.start_col = 0;
2395 rect.end_col = term->tl_cols;
2396 rect.start_row = row;
2397 rect.end_row = row + 1;
2398 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2399 }
2400}
2401
2402/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002403 * "term_getscrolled(buf)" function
2404 */
2405 void
2406f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2407{
2408 buf_T *buf = term_get_buf(argvars);
2409
2410 if (buf == NULL)
2411 return;
2412 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2413}
2414
2415/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002416 * "term_getsize(buf)" function
2417 */
2418 void
2419f_term_getsize(typval_T *argvars, typval_T *rettv)
2420{
2421 buf_T *buf = term_get_buf(argvars);
2422 list_T *l;
2423
2424 if (rettv_list_alloc(rettv) == FAIL)
2425 return;
2426 if (buf == NULL)
2427 return;
2428
2429 l = rettv->vval.v_list;
2430 list_append_number(l, buf->b_term->tl_rows);
2431 list_append_number(l, buf->b_term->tl_cols);
2432}
2433
2434/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002435 * "term_getstatus(buf)" function
2436 */
2437 void
2438f_term_getstatus(typval_T *argvars, typval_T *rettv)
2439{
2440 buf_T *buf = term_get_buf(argvars);
2441 term_T *term;
2442 char_u val[100];
2443
2444 rettv->v_type = VAR_STRING;
2445 if (buf == NULL)
2446 return;
2447 term = buf->b_term;
2448
2449 if (term_job_running(term))
2450 STRCPY(val, "running");
2451 else
2452 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002453 if (term->tl_normal_mode)
2454 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002455 rettv->vval.v_string = vim_strsave(val);
2456}
2457
2458/*
2459 * "term_gettitle(buf)" function
2460 */
2461 void
2462f_term_gettitle(typval_T *argvars, typval_T *rettv)
2463{
2464 buf_T *buf = term_get_buf(argvars);
2465
2466 rettv->v_type = VAR_STRING;
2467 if (buf == NULL)
2468 return;
2469
2470 if (buf->b_term->tl_title != NULL)
2471 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2472}
2473
2474/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002475 * "term_gettty(buf)" function
2476 */
2477 void
2478f_term_gettty(typval_T *argvars, typval_T *rettv)
2479{
2480 buf_T *buf = term_get_buf(argvars);
2481 char_u *p;
2482
2483 rettv->v_type = VAR_STRING;
2484 if (buf == NULL)
2485 return;
2486 if (buf->b_term->tl_job != NULL)
2487 p = buf->b_term->tl_job->jv_tty_name;
2488 else
2489 p = buf->b_term->tl_tty_name;
2490 if (p != NULL)
2491 rettv->vval.v_string = vim_strsave(p);
2492}
2493
2494/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002495 * "term_list()" function
2496 */
2497 void
2498f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2499{
2500 term_T *tp;
2501 list_T *l;
2502
2503 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2504 return;
2505
2506 l = rettv->vval.v_list;
2507 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2508 if (tp != NULL && tp->tl_buffer != NULL)
2509 if (list_append_number(l,
2510 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2511 return;
2512}
2513
2514/*
2515 * "term_scrape(buf, row)" function
2516 */
2517 void
2518f_term_scrape(typval_T *argvars, typval_T *rettv)
2519{
2520 buf_T *buf = term_get_buf(argvars);
2521 VTermScreen *screen = NULL;
2522 VTermPos pos;
2523 list_T *l;
2524 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002525 char_u *p;
2526 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002527
2528 if (rettv_list_alloc(rettv) == FAIL)
2529 return;
2530 if (buf == NULL)
2531 return;
2532 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002533
2534 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002535 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002536
2537 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002538 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002539 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002540 p = NULL;
2541 line = NULL;
2542 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002543 else
2544 {
2545 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2546
2547 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2548 return;
2549 p = ml_get_buf(buf, lnum + 1, FALSE);
2550 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2551 }
2552
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002553 for (pos.col = 0; pos.col < term->tl_cols; )
2554 {
2555 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002556 int width;
2557 VTermScreenCellAttrs attrs;
2558 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002559 char_u rgb[8];
2560 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2561 int off = 0;
2562 int i;
2563
2564 if (screen == NULL)
2565 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002566 cellattr_T *cellattr;
2567 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002568
2569 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002570 if (pos.col >= line->sb_cols)
2571 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002572 cellattr = line->sb_cells + pos.col;
2573 width = cellattr->width;
2574 attrs = cellattr->attrs;
2575 fg = cellattr->fg;
2576 bg = cellattr->bg;
2577 len = MB_PTR2LEN(p);
2578 mch_memmove(mbs, p, len);
2579 mbs[len] = NUL;
2580 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002581 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002582 else
2583 {
2584 VTermScreenCell cell;
2585 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2586 break;
2587 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2588 {
2589 if (cell.chars[i] == 0)
2590 break;
2591 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2592 }
2593 mbs[off] = NUL;
2594 width = cell.width;
2595 attrs = cell.attrs;
2596 fg = cell.fg;
2597 bg = cell.bg;
2598 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002599 dcell = dict_alloc();
2600 list_append_dict(l, dcell);
2601
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002602 dict_add_nr_str(dcell, "chars", 0, mbs);
2603
2604 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002605 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002606 dict_add_nr_str(dcell, "fg", 0, rgb);
2607 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002608 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002609 dict_add_nr_str(dcell, "bg", 0, rgb);
2610
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002611 dict_add_nr_str(dcell, "attr",
2612 cell2attr(attrs, fg, bg), NULL);
2613 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002614
2615 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002616 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002617 ++pos.col;
2618 }
2619}
2620
2621/*
2622 * "term_sendkeys(buf, keys)" function
2623 */
2624 void
2625f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2626{
2627 buf_T *buf = term_get_buf(argvars);
2628 char_u *msg;
2629 term_T *term;
2630
2631 rettv->v_type = VAR_UNKNOWN;
2632 if (buf == NULL)
2633 return;
2634
2635 msg = get_tv_string_chk(&argvars[1]);
2636 if (msg == NULL)
2637 return;
2638 term = buf->b_term;
2639 if (term->tl_vterm == NULL)
2640 return;
2641
2642 while (*msg != NUL)
2643 {
2644 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2645 msg += MB_PTR2LEN(msg);
2646 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002647}
2648
2649/*
2650 * "term_start(command, options)" function
2651 */
2652 void
2653f_term_start(typval_T *argvars, typval_T *rettv)
2654{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002655 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002656
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002657 init_job_options(&opt);
2658 /* TODO: allow more job options */
2659 if (argvars[1].v_type != VAR_UNKNOWN
2660 && get_job_options(&argvars[1], &opt,
2661 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002662 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002663 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002664 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002665 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002666 return;
2667
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002668 if (opt.jo_vertical)
2669 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002670 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002671
2672 if (curbuf->b_term != NULL)
2673 rettv->vval.v_number = curbuf->b_fnum;
2674}
2675
2676/*
2677 * "term_wait" function
2678 */
2679 void
2680f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2681{
2682 buf_T *buf = term_get_buf(argvars);
2683
2684 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002685 {
2686 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002687 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002688 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002689 if (buf->b_term->tl_job == NULL)
2690 {
2691 ch_log(NULL, "term_wait(): no job to wait for");
2692 return;
2693 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002694
2695 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002696 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002697 {
2698 /* The job is dead, keep reading channel I/O until the channel is
2699 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002700 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002701 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2702 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002703 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002704 parse_queued_messages();
2705 ui_delay(10L, FALSE);
2706 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002707 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002708 parse_queued_messages();
2709 }
2710 else
2711 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002712 long wait = 10L;
2713
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002714 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002715 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002716
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002717 /* Wait for some time for any channel I/O. */
2718 if (argvars[1].v_type != VAR_UNKNOWN)
2719 wait = get_tv_number(&argvars[1]);
2720 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002721 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002722
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002723 /* Flushing messages on channels is hopefully sufficient.
2724 * TODO: is there a better way? */
2725 parse_queued_messages();
2726 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002727}
2728
Bram Moolenaara83e3962017-08-17 14:39:07 +02002729# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002730
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002731/**************************************
2732 * 2. MS-Windows implementation.
2733 */
2734
Bram Moolenaara83e3962017-08-17 14:39:07 +02002735# ifndef PROTO
2736
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002737#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2738#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2739
Bram Moolenaar8a773062017-07-24 22:29:21 +02002740void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002741void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002742void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002743BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2744void (*winpty_config_set_initial_size)(void*, int, int);
2745LPCWSTR (*winpty_conin_name)(void*);
2746LPCWSTR (*winpty_conout_name)(void*);
2747LPCWSTR (*winpty_conerr_name)(void*);
2748void (*winpty_free)(void*);
2749void (*winpty_config_free)(void*);
2750void (*winpty_spawn_config_free)(void*);
2751void (*winpty_error_free)(void*);
2752LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002753BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002754HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002755
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002756#define WINPTY_DLL "winpty.dll"
2757
2758static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002759# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002760
Bram Moolenaara83e3962017-08-17 14:39:07 +02002761 static int
2762dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002763{
2764 int i;
2765 static struct
2766 {
2767 char *name;
2768 FARPROC *ptr;
2769 } winpty_entry[] =
2770 {
2771 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2772 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2773 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2774 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2775 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2776 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2777 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2778 {"winpty_free", (FARPROC*)&winpty_free},
2779 {"winpty_open", (FARPROC*)&winpty_open},
2780 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2781 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2782 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2783 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002784 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002785 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002786 {NULL, NULL}
2787 };
2788
2789 /* No need to initialize twice. */
2790 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002791 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002792 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2793 * winpty.dll. */
2794 if (*p_winptydll != NUL)
2795 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2796 if (!hWinPtyDLL)
2797 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002798 if (!hWinPtyDLL)
2799 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002800 if (verbose)
2801 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2802 : (char_u *)WINPTY_DLL);
2803 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002804 }
2805 for (i = 0; winpty_entry[i].name != NULL
2806 && winpty_entry[i].ptr != NULL; ++i)
2807 {
2808 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2809 winpty_entry[i].name)) == NULL)
2810 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002811 if (verbose)
2812 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2813 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002814 }
2815 }
2816
Bram Moolenaara83e3962017-08-17 14:39:07 +02002817 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002818}
2819
2820/*
2821 * Create a new terminal of "rows" by "cols" cells.
2822 * Store a reference in "term".
2823 * Return OK or FAIL.
2824 */
2825 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002826term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002827{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002828 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002829 channel_T *channel = NULL;
2830 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002831 DWORD error;
2832 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2833 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002834 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002835 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002836 garray_T ga;
2837 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002838
Bram Moolenaara83e3962017-08-17 14:39:07 +02002839 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002840 return FAIL;
2841
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002842 if (argvar->v_type == VAR_STRING)
2843 cmd = argvar->vval.v_string;
2844 else
2845 {
2846 ga_init2(&ga, (int)sizeof(char*), 20);
2847 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2848 goto failed;
2849 cmd = ga.ga_data;
2850 }
2851
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002852 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002853 if (p == NULL)
2854 return FAIL;
2855
2856 job = job_alloc();
2857 if (job == NULL)
2858 goto failed;
2859
2860 channel = add_channel();
2861 if (channel == NULL)
2862 goto failed;
2863
2864 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2865 if (term->tl_winpty_config == NULL)
2866 goto failed;
2867
2868 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2869 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2870 if (term->tl_winpty == NULL)
2871 goto failed;
2872
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002873 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002874 spawn_config = winpty_spawn_config_new(
2875 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2876 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2877 NULL,
2878 p,
2879 NULL,
2880 NULL,
2881 &winpty_err);
2882 if (spawn_config == NULL)
2883 goto failed;
2884
2885 channel = add_channel();
2886 if (channel == NULL)
2887 goto failed;
2888
2889 job = job_alloc();
2890 if (job == NULL)
2891 goto failed;
2892
2893 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2894 &child_thread_handle, &error, &winpty_err))
2895 goto failed;
2896
2897 channel_set_pipes(channel,
2898 (sock_T) CreateFileW(
2899 winpty_conin_name(term->tl_winpty),
2900 GENERIC_WRITE, 0, NULL,
2901 OPEN_EXISTING, 0, NULL),
2902 (sock_T) CreateFileW(
2903 winpty_conout_name(term->tl_winpty),
2904 GENERIC_READ, 0, NULL,
2905 OPEN_EXISTING, 0, NULL),
2906 (sock_T) CreateFileW(
2907 winpty_conerr_name(term->tl_winpty),
2908 GENERIC_READ, 0, NULL,
2909 OPEN_EXISTING, 0, NULL));
2910
2911 jo = CreateJobObject(NULL, NULL);
2912 if (jo == NULL)
2913 goto failed;
2914
2915 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002916 {
2917 /* Failed, switch the way to terminate process with TerminateProcess. */
2918 CloseHandle(jo);
2919 jo = NULL;
2920 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002921
2922 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002923 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002924
2925 create_vterm(term, rows, cols);
2926
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002927 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002928 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929
2930 job->jv_channel = channel;
2931 job->jv_proc_info.hProcess = child_process_handle;
2932 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2933 job->jv_job_object = jo;
2934 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002935 sprintf(buf, "winpty://%lu",
2936 GetProcessId(winpty_agent_process(term->tl_winpty)));
2937 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002938 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002939 term->tl_job = job;
2940
2941 return OK;
2942
2943failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002944 if (argvar->v_type == VAR_LIST)
2945 vim_free(ga.ga_data);
2946 if (p != NULL)
2947 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002948 if (spawn_config != NULL)
2949 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002950 if (channel != NULL)
2951 channel_clear(channel);
2952 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002953 {
2954 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002955 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002956 }
2957 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002958 if (jo != NULL)
2959 CloseHandle(jo);
2960 if (term->tl_winpty != NULL)
2961 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002962 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002963 if (term->tl_winpty_config != NULL)
2964 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002965 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002966 if (winpty_err != NULL)
2967 {
2968 char_u *msg = utf16_to_enc(
2969 (short_u *)winpty_error_msg(winpty_err), NULL);
2970
2971 EMSG(msg);
2972 winpty_error_free(winpty_err);
2973 }
2974 return FAIL;
2975}
2976
2977/*
2978 * Free the terminal emulator part of "term".
2979 */
2980 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002981term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002982{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002983 if (term->tl_winpty != NULL)
2984 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002985 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002986 if (term->tl_winpty_config != NULL)
2987 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002988 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002989 if (term->tl_vterm != NULL)
2990 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002991 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002992}
2993
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002994/*
2995 * Request size to terminal.
2996 */
2997 static void
2998term_report_winsize(term_T *term, int rows, int cols)
2999{
3000 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3001}
3002
Bram Moolenaara83e3962017-08-17 14:39:07 +02003003 int
3004terminal_enabled(void)
3005{
3006 return dyn_winpty_init(FALSE) == OK;
3007}
3008
3009
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003010# else
3011
3012/**************************************
3013 * 3. Unix-like implementation.
3014 */
3015
3016/*
3017 * Create a new terminal of "rows" by "cols" cells.
3018 * Start job for "cmd".
3019 * Store the pointers in "term".
3020 * Return OK or FAIL.
3021 */
3022 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003023term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003024{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003025 create_vterm(term, rows, cols);
3026
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003027 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003028 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003029 if (term->tl_job != NULL)
3030 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003031
Bram Moolenaar61a66052017-07-22 18:39:00 +02003032 return term->tl_job != NULL
3033 && term->tl_job->jv_channel != NULL
3034 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003035}
3036
3037/*
3038 * Free the terminal emulator part of "term".
3039 */
3040 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003041term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003042{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003043 if (term->tl_vterm != NULL)
3044 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003045 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003046}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003047
3048/*
3049 * Request size to terminal.
3050 */
3051 static void
3052term_report_winsize(term_T *term, int rows, int cols)
3053{
3054 /* Use an ioctl() to report the new window size to the job. */
3055 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3056 {
3057 int fd = -1;
3058 int part;
3059
3060 for (part = PART_OUT; part < PART_COUNT; ++part)
3061 {
3062 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3063 if (isatty(fd))
3064 break;
3065 }
3066 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003067 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003068 }
3069}
3070
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003071# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003072
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003073#endif /* FEAT_TERMINAL */