blob: e8e187b8924a1551920a9fa4d0f0e3457e081b98 [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;
1039 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1040 {
1041 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1042 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1043 vim_free(line->sb_cells);
1044 --gap->ga_len;
1045 if (gap->ga_len == 0)
1046 break;
1047 }
1048 check_cursor();
1049
Bram Moolenaar6d819742017-08-06 14:57:49 +02001050 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001051
1052 if (term->tl_channel_closed)
1053 cleanup_vterm(term);
1054 redraw_buf_and_status_later(curbuf, NOT_VALID);
1055}
1056
1057/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001058 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001059 * Note: while waiting a terminal may be closed and freed if the channel is
1060 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001061 * TODO: use terminal mode mappings.
1062 */
1063 static int
1064term_vgetc()
1065{
1066 int c;
1067
1068 ++no_mapping;
1069 ++allow_keys;
1070 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001071#ifdef WIN3264
1072 ctrl_break_was_pressed = FALSE;
1073#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001074 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001075 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001076 --no_mapping;
1077 --allow_keys;
1078 return c;
1079}
1080
1081/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001083 * Return FAIL when the key needs to be handled in Normal mode.
1084 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001085 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001086 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001087send_keys_to_term(term_T *term, int c, int typed)
1088{
1089 char msg[KEY_BUF_LEN];
1090 size_t len;
1091 static int mouse_was_outside = FALSE;
1092 int dragging_outside = FALSE;
1093
1094 /* Catch keys that need to be handled as in Normal mode. */
1095 switch (c)
1096 {
1097 case NUL:
1098 case K_ZERO:
1099 if (typed)
1100 stuffcharReadbuff(c);
1101 return FAIL;
1102
1103 case K_IGNORE:
1104 return FAIL;
1105
1106 case K_LEFTDRAG:
1107 case K_MIDDLEDRAG:
1108 case K_RIGHTDRAG:
1109 case K_X1DRAG:
1110 case K_X2DRAG:
1111 dragging_outside = mouse_was_outside;
1112 /* FALLTHROUGH */
1113 case K_LEFTMOUSE:
1114 case K_LEFTMOUSE_NM:
1115 case K_LEFTRELEASE:
1116 case K_LEFTRELEASE_NM:
1117 case K_MIDDLEMOUSE:
1118 case K_MIDDLERELEASE:
1119 case K_RIGHTMOUSE:
1120 case K_RIGHTRELEASE:
1121 case K_X1MOUSE:
1122 case K_X1RELEASE:
1123 case K_X2MOUSE:
1124 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001125
1126 case K_MOUSEUP:
1127 case K_MOUSEDOWN:
1128 case K_MOUSELEFT:
1129 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001130 if (mouse_row < W_WINROW(curwin)
1131 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1132 || mouse_col < W_WINCOL(curwin)
1133 || mouse_col >= W_ENDCOL(curwin)
1134 || dragging_outside)
1135 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001136 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001137 if (typed)
1138 {
1139 stuffcharReadbuff(c);
1140 mouse_was_outside = TRUE;
1141 }
1142 return FAIL;
1143 }
1144 }
1145 if (typed)
1146 mouse_was_outside = FALSE;
1147
1148 /* Convert the typed key to a sequence of bytes for the job. */
1149 len = term_convert_key(term, c, msg);
1150 if (len > 0)
1151 /* TODO: if FAIL is returned, stop? */
1152 channel_send(term->tl_job->jv_channel, PART_IN,
1153 (char_u *)msg, (int)len, NULL);
1154
1155 return OK;
1156}
1157
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001158 static void
1159position_cursor(win_T *wp, VTermPos *pos)
1160{
1161 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1162 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1163 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1164}
1165
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001166/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001167 * Handle CTRL-W "": send register contents to the job.
1168 */
1169 static void
1170term_paste_register(int prev_c UNUSED)
1171{
1172 int c;
1173 list_T *l;
1174 listitem_T *item;
1175 long reglen = 0;
1176 int type;
1177
1178#ifdef FEAT_CMDL_INFO
1179 if (add_to_showcmd(prev_c))
1180 if (add_to_showcmd('"'))
1181 out_flush();
1182#endif
1183 c = term_vgetc();
1184#ifdef FEAT_CMDL_INFO
1185 clear_showcmd();
1186#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001187 if (!term_use_loop())
1188 /* job finished while waiting for a character */
1189 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001190
1191 /* CTRL-W "= prompt for expression to evaluate. */
1192 if (c == '=' && get_expr_register() != '=')
1193 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001194 if (!term_use_loop())
1195 /* job finished while waiting for a character */
1196 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001197
1198 l = (list_T *)get_reg_contents(c, GREG_LIST);
1199 if (l != NULL)
1200 {
1201 type = get_reg_type(c, &reglen);
1202 for (item = l->lv_first; item != NULL; item = item->li_next)
1203 {
1204 char_u *s = get_tv_string(&item->li_tv);
1205
1206 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1207 s, STRLEN(s), NULL);
1208 if (item->li_next != NULL || type == MLINE)
1209 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1210 (char_u *)"\r", 1, NULL);
1211 }
1212 list_free(l);
1213 }
1214}
1215
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001216#if defined(FEAT_GUI) || defined(PROTO)
1217/*
1218 * Return TRUE when the cursor of the terminal should be displayed.
1219 */
1220 int
1221use_terminal_cursor()
1222{
1223 return in_terminal_loop != NULL;
1224}
1225
1226 cursorentry_T *
1227term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1228{
1229 term_T *term = in_terminal_loop;
1230 static cursorentry_T entry;
1231
1232 vim_memset(&entry, 0, sizeof(entry));
1233 entry.shape = entry.mshape =
1234 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1235 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1236 SHAPE_BLOCK;
1237 entry.percentage = 20;
1238 if (term->tl_cursor_blink)
1239 {
1240 entry.blinkwait = 700;
1241 entry.blinkon = 400;
1242 entry.blinkon = 250;
1243 }
1244 *fg = gui.back_pixel;
1245 if (term->tl_cursor_color == NULL)
1246 *bg = gui.norm_pixel;
1247 else
1248 *bg = color_name2handle(term->tl_cursor_color);
1249 entry.name = "n";
1250 entry.used_for = SHAPE_CURSOR;
1251
1252 return &entry;
1253}
1254#endif
1255
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001256static int did_change_cursor = FALSE;
1257
1258 static void
1259may_set_cursor_props(term_T *term)
1260{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001261#ifdef FEAT_GUI
1262 /* For the GUI the cursor properties are obtained with
1263 * term_get_cursor_shape(). */
1264 if (gui.in_use)
1265 return;
1266#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001267 if (in_terminal_loop == term)
1268 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001269 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001270 if (term->tl_cursor_color != NULL)
1271 term_cursor_color(term->tl_cursor_color);
1272 else
1273 term_cursor_color((char_u *)"");
1274 /* do both blink and shape+blink, in case setting shape does not work */
1275 term_cursor_blink(term->tl_cursor_blink);
1276 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1277 }
1278}
1279
1280 static void
1281may_restore_cursor_props(void)
1282{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001283#ifdef FEAT_GUI
1284 if (gui.in_use)
1285 return;
1286#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001287 if (did_change_cursor)
1288 {
1289 did_change_cursor = FALSE;
1290 ui_cursor_shape_forced(TRUE);
1291 term_cursor_color((char_u *)"");
1292 term_cursor_blink(FALSE);
1293 }
1294}
1295
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001296/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001297 * Returns TRUE if the current window contains a terminal and we are sending
1298 * keys to the job.
1299 */
1300 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001301term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001302{
1303 term_T *term = curbuf->b_term;
1304
1305 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001306 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001307 && term->tl_vterm != NULL
1308 && term_job_running(term);
1309}
1310
1311/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001312 * Wait for input and send it to the job.
1313 * Return when the start of a CTRL-W command is typed or anything else that
1314 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001315 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1316 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001317 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001318 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001319terminal_loop(void)
1320{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001321 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001322 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001323 int ret;
1324
Bram Moolenaar679653e2017-08-13 14:13:19 +02001325 /* Remember the terminal we are sending keys to. However, the terminal
1326 * might be closed while waiting for a character, e.g. typing "exit" in a
1327 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1328 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001329 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001330
1331 if (*curwin->w_p_tk != NUL)
1332 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001333 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001334 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001335
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001336#ifdef UNIX
1337 {
1338 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1339
1340 if (isatty(fd))
1341 {
1342 ttyinfo_T info;
1343
1344 /* Get the current backspace and enter characters of the pty. */
1345 if (get_tty_info(fd, &info) == OK)
1346 {
1347 term_backspace_char = info.backspace;
1348 term_enter_char = info.enter;
1349 term_nl_does_cr = info.nl_does_cr;
1350 }
1351 }
1352 }
1353#endif
1354
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001355 for (;;)
1356 {
1357 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001358 /* Repeat redrawing in case a message is received while redrawing. */
1359 while (curwin->w_redr_type != 0)
1360 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001361 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001362
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001363 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001364 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001365 /* job finished while waiting for a character */
1366 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001367 if (c == K_IGNORE)
1368 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001369
Bram Moolenaarfae42832017-08-01 22:24:26 +02001370#ifdef UNIX
1371 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1372#endif
1373#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001374 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001375 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001376 if (ctrl_break_was_pressed)
1377 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001378#endif
1379
Bram Moolenaar69198192017-08-05 14:10:48 +02001380 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001381 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001382 int prev_c = c;
1383
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001384#ifdef FEAT_CMDL_INFO
1385 if (add_to_showcmd(c))
1386 out_flush();
1387#endif
1388 c = term_vgetc();
1389#ifdef FEAT_CMDL_INFO
1390 clear_showcmd();
1391#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001392 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001393 /* job finished while waiting for a character */
1394 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001395
Bram Moolenaar69198192017-08-05 14:10:48 +02001396 if (prev_c == Ctrl_BSL)
1397 {
1398 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001399 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001400 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1401 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001402 ret = FAIL;
1403 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001404 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001405 /* Send both keys to the terminal. */
1406 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1407 }
1408 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001409 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001410 /* "CTRL-W .": send CTRL-W to the job */
1411 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001412 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001413 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001414 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001415 /* CTRL-W N : go to Terminal-Normal mode. */
1416 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001417 ret = FAIL;
1418 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001419 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001420 else if (c == '"')
1421 {
1422 term_paste_register(prev_c);
1423 continue;
1424 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001425 else if (termkey == 0 || c != termkey)
1426 {
1427 stuffcharReadbuff(Ctrl_W);
1428 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001429 ret = OK;
1430 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001431 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001432 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001433 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001434 {
1435 ret = OK;
1436 goto theend;
1437 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001438 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001439 ret = FAIL;
1440
1441theend:
1442 in_terminal_loop = NULL;
1443 may_restore_cursor_props();
1444 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001445}
1446
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001447/*
1448 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001449 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001450 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001451 */
1452 void
1453term_job_ended(job_T *job)
1454{
1455 term_T *term;
1456 int did_one = FALSE;
1457
1458 for (term = first_term; term != NULL; term = term->tl_next)
1459 if (term->tl_job == job)
1460 {
1461 vim_free(term->tl_title);
1462 term->tl_title = NULL;
1463 vim_free(term->tl_status_text);
1464 term->tl_status_text = NULL;
1465 redraw_buf_and_status_later(term->tl_buffer, VALID);
1466 did_one = TRUE;
1467 }
1468 if (did_one)
1469 redraw_statuslines();
1470 if (curbuf->b_term != NULL)
1471 {
1472 if (curbuf->b_term->tl_job == job)
1473 maketitle();
1474 update_cursor(curbuf->b_term, TRUE);
1475 }
1476}
1477
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001478 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001479may_toggle_cursor(term_T *term)
1480{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001481 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001482 {
1483 if (term->tl_cursor_visible)
1484 cursor_on();
1485 else
1486 cursor_off();
1487 }
1488}
1489
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001490/*
1491 * Reverse engineer the RGB value into a cterm color index.
1492 * First color is 1. Return 0 if no match found.
1493 */
1494 static int
1495color2index(VTermColor *color, int fg, int *boldp)
1496{
1497 int red = color->red;
1498 int blue = color->blue;
1499 int green = color->green;
1500
1501 /* The argument for lookup_color() is for the color_names[] table. */
1502 if (red == 0)
1503 {
1504 if (green == 0)
1505 {
1506 if (blue == 0)
1507 return lookup_color(0, fg, boldp) + 1; /* black */
1508 if (blue == 224)
1509 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1510 }
1511 else if (green == 224)
1512 {
1513 if (blue == 0)
1514 return lookup_color(2, fg, boldp) + 1; /* dark green */
1515 if (blue == 224)
1516 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1517 }
1518 }
1519 else if (red == 224)
1520 {
1521 if (green == 0)
1522 {
1523 if (blue == 0)
1524 return lookup_color(4, fg, boldp) + 1; /* dark red */
1525 if (blue == 224)
1526 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1527 }
1528 else if (green == 224)
1529 {
1530 if (blue == 0)
1531 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1532 if (blue == 224)
1533 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1534 }
1535 }
1536 else if (red == 128)
1537 {
1538 if (green == 128 && blue == 128)
1539 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1540 }
1541 else if (red == 255)
1542 {
1543 if (green == 64)
1544 {
1545 if (blue == 64)
1546 return lookup_color(20, fg, boldp) + 1; /* light red */
1547 if (blue == 255)
1548 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1549 }
1550 else if (green == 255)
1551 {
1552 if (blue == 64)
1553 return lookup_color(24, fg, boldp) + 1; /* yellow */
1554 if (blue == 255)
1555 return lookup_color(26, fg, boldp) + 1; /* white */
1556 }
1557 }
1558 else if (red == 64)
1559 {
1560 if (green == 64)
1561 {
1562 if (blue == 255)
1563 return lookup_color(14, fg, boldp) + 1; /* light blue */
1564 }
1565 else if (green == 255)
1566 {
1567 if (blue == 64)
1568 return lookup_color(16, fg, boldp) + 1; /* light green */
1569 if (blue == 255)
1570 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1571 }
1572 }
1573 if (t_colors >= 256)
1574 {
1575 if (red == blue && red == green)
1576 {
1577 /* 24-color greyscale */
1578 static int cutoff[23] = {
1579 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1580 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1581 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1582 int i;
1583
1584 for (i = 0; i < 23; ++i)
1585 if (red < cutoff[i])
1586 return i + 233;
1587 return 256;
1588 }
1589
1590 /* 216-color cube */
1591 return 17 + ((red + 25) / 0x33) * 36
1592 + ((green + 25) / 0x33) * 6
1593 + (blue + 25) / 0x33;
1594 }
1595 return 0;
1596}
1597
1598/*
1599 * Convert the attributes of a vterm cell into an attribute index.
1600 */
1601 static int
1602cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1603{
1604 int attr = 0;
1605
1606 if (cellattrs.bold)
1607 attr |= HL_BOLD;
1608 if (cellattrs.underline)
1609 attr |= HL_UNDERLINE;
1610 if (cellattrs.italic)
1611 attr |= HL_ITALIC;
1612 if (cellattrs.strike)
1613 attr |= HL_STANDOUT;
1614 if (cellattrs.reverse)
1615 attr |= HL_INVERSE;
1616
1617#ifdef FEAT_GUI
1618 if (gui.in_use)
1619 {
1620 guicolor_T fg, bg;
1621
1622 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1623 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1624 return get_gui_attr_idx(attr, fg, bg);
1625 }
1626 else
1627#endif
1628#ifdef FEAT_TERMGUICOLORS
1629 if (p_tgc)
1630 {
1631 guicolor_T fg, bg;
1632
1633 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1634 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1635
1636 return get_tgc_attr_idx(attr, fg, bg);
1637 }
1638 else
1639#endif
1640 {
1641 int bold = MAYBE;
1642 int fg = color2index(&cellfg, TRUE, &bold);
1643 int bg = color2index(&cellbg, FALSE, &bold);
1644
1645 /* with 8 colors set the bold attribute to get a bright foreground */
1646 if (bold == TRUE)
1647 attr |= HL_BOLD;
1648 return get_cterm_attr_idx(attr, fg, bg);
1649 }
1650 return 0;
1651}
1652
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001653 static int
1654handle_damage(VTermRect rect, void *user)
1655{
1656 term_T *term = (term_T *)user;
1657
1658 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1659 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1660 redraw_buf_later(term->tl_buffer, NOT_VALID);
1661 return 1;
1662}
1663
1664 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001665handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001666{
1667 term_T *term = (term_T *)user;
1668
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001669 /* Scrolling up is done much more efficiently by deleting lines instead of
1670 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001671 if (dest.start_col == src.start_col
1672 && dest.end_col == src.end_col
1673 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001674 {
1675 win_T *wp;
1676 VTermColor fg, bg;
1677 VTermScreenCellAttrs attr;
1678 int clear_attr;
1679
1680 /* Set the color to clear lines with. */
1681 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1682 &fg, &bg);
1683 vim_memset(&attr, 0, sizeof(attr));
1684 clear_attr = cell2attr(attr, fg, bg);
1685
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001686 FOR_ALL_WINDOWS(wp)
1687 {
1688 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001689 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001690 src.start_row - dest.start_row, FALSE, FALSE,
1691 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001692 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001693 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001694 redraw_buf_later(term->tl_buffer, NOT_VALID);
1695 return 1;
1696}
1697
1698 static int
1699handle_movecursor(
1700 VTermPos pos,
1701 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001702 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001703 void *user)
1704{
1705 term_T *term = (term_T *)user;
1706 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001707
1708 term->tl_cursor_pos = pos;
1709 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001710
1711 FOR_ALL_WINDOWS(wp)
1712 {
1713 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001715 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001716 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001717 {
1718 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001719 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001720 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001721
1722 return 1;
1723}
1724
Bram Moolenaar21554412017-07-24 21:44:43 +02001725 static int
1726handle_settermprop(
1727 VTermProp prop,
1728 VTermValue *value,
1729 void *user)
1730{
1731 term_T *term = (term_T *)user;
1732
1733 switch (prop)
1734 {
1735 case VTERM_PROP_TITLE:
1736 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001737 /* a blank title isn't useful, make it empty, so that "running" is
1738 * displayed */
1739 if (*skipwhite((char_u *)value->string) == NUL)
1740 term->tl_title = NULL;
1741 else
1742 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001743 vim_free(term->tl_status_text);
1744 term->tl_status_text = NULL;
1745 if (term == curbuf->b_term)
1746 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001747 break;
1748
1749 case VTERM_PROP_CURSORVISIBLE:
1750 term->tl_cursor_visible = value->boolean;
1751 may_toggle_cursor(term);
1752 out_flush();
1753 break;
1754
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001755 case VTERM_PROP_CURSORBLINK:
1756 term->tl_cursor_blink = value->boolean;
1757 may_set_cursor_props(term);
1758 break;
1759
1760 case VTERM_PROP_CURSORSHAPE:
1761 term->tl_cursor_shape = value->number;
1762 may_set_cursor_props(term);
1763 break;
1764
1765 case VTERM_PROP_CURSORCOLOR:
1766 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001767 if (*value->string == NUL)
1768 term->tl_cursor_color = NULL;
1769 else
1770 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001771 may_set_cursor_props(term);
1772 break;
1773
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001774 case VTERM_PROP_ALTSCREEN:
1775 /* TODO: do anything else? */
1776 term->tl_using_altscreen = value->boolean;
1777 break;
1778
Bram Moolenaar21554412017-07-24 21:44:43 +02001779 default:
1780 break;
1781 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001782 /* Always return 1, otherwise vterm doesn't store the value internally. */
1783 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001784}
1785
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001786/*
1787 * The job running in the terminal resized the terminal.
1788 */
1789 static int
1790handle_resize(int rows, int cols, void *user)
1791{
1792 term_T *term = (term_T *)user;
1793 win_T *wp;
1794
1795 term->tl_rows = rows;
1796 term->tl_cols = cols;
1797 FOR_ALL_WINDOWS(wp)
1798 {
1799 if (wp->w_buffer == term->tl_buffer)
1800 {
1801 win_setheight_win(rows, wp);
1802 win_setwidth_win(cols, wp);
1803 }
1804 }
1805
1806 redraw_buf_later(term->tl_buffer, NOT_VALID);
1807 return 1;
1808}
1809
Bram Moolenaard85f2712017-07-28 21:51:57 +02001810/*
1811 * Handle a line that is pushed off the top of the screen.
1812 */
1813 static int
1814handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1815{
1816 term_T *term = (term_T *)user;
1817
1818 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001819 if (ga_grow(&term->tl_scrollback, 1) == OK)
1820 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001821 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001822 int len = 0;
1823 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001824 int c;
1825 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001826 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001827 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001828
1829 /* do not store empty cells at the end */
1830 for (i = 0; i < cols; ++i)
1831 if (cells[i].chars[0] != 0)
1832 len = i + 1;
1833
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001834 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001835 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001836 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001837 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001838 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001839 for (col = 0; col < len; col += cells[col].width)
1840 {
1841 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1842 {
1843 ga.ga_len = 0;
1844 break;
1845 }
1846 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1847 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1848 (char_u *)ga.ga_data + ga.ga_len);
1849 p[col].width = cells[col].width;
1850 p[col].attrs = cells[col].attrs;
1851 p[col].fg = cells[col].fg;
1852 p[col].bg = cells[col].bg;
1853 }
1854 }
1855 if (ga_grow(&ga, 1) == FAIL)
1856 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1857 else
1858 {
1859 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1860 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1861 }
1862 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001863
1864 line = (sb_line_T *)term->tl_scrollback.ga_data
1865 + term->tl_scrollback.ga_len;
1866 line->sb_cols = len;
1867 line->sb_cells = p;
1868 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001869 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001870 }
1871 return 0; /* ignored */
1872}
1873
Bram Moolenaar21554412017-07-24 21:44:43 +02001874static VTermScreenCallbacks screen_callbacks = {
1875 handle_damage, /* damage */
1876 handle_moverect, /* moverect */
1877 handle_movecursor, /* movecursor */
1878 handle_settermprop, /* settermprop */
1879 NULL, /* bell */
1880 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001881 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001882 NULL /* sb_popline */
1883};
1884
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001885/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001886 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001887 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001888 */
1889 void
1890term_channel_closed(channel_T *ch)
1891{
1892 term_T *term;
1893 int did_one = FALSE;
1894
1895 for (term = first_term; term != NULL; term = term->tl_next)
1896 if (term->tl_job == ch->ch_job)
1897 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001898 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001899 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001900
Bram Moolenaard85f2712017-07-28 21:51:57 +02001901 vim_free(term->tl_title);
1902 term->tl_title = NULL;
1903 vim_free(term->tl_status_text);
1904 term->tl_status_text = NULL;
1905
Bram Moolenaar423802d2017-07-30 16:52:24 +02001906 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001907 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001908 {
1909 int fnum = term->tl_buffer->b_fnum;
1910
Bram Moolenaar423802d2017-07-30 16:52:24 +02001911 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001912
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001913 if (term->tl_finish == 'c')
1914 {
1915 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001916 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001917 curbuf = term->tl_buffer;
1918 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1919 break;
1920 }
1921 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1922 {
1923 char buf[50];
1924
1925 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001926 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001927 vim_snprintf(buf, sizeof(buf),
1928 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001929 ? "botright sbuf %d"
1930 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001931 do_cmdline_cmd((char_u *)buf);
1932 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001933 else
1934 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001935 }
1936
Bram Moolenaard85f2712017-07-28 21:51:57 +02001937 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001938 }
1939 if (did_one)
1940 {
1941 redraw_statuslines();
1942
1943 /* Need to break out of vgetc(). */
1944 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001945 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001946
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001947 term = curbuf->b_term;
1948 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001949 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001950 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001951 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001952 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953 }
1954 }
1955}
1956
1957/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001958 * Called to update a window that contains an active terminal.
1959 * Returns FAIL when there is no terminal running in this window or in
1960 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001961 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001962 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001963term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001964{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001965 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001966 VTerm *vterm;
1967 VTermScreen *screen;
1968 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001969 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001970
Bram Moolenaar6d819742017-08-06 14:57:49 +02001971 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001972 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001973
Bram Moolenaard85f2712017-07-28 21:51:57 +02001974 vterm = term->tl_vterm;
1975 screen = vterm_obtain_screen(vterm);
1976 state = vterm_obtain_state(vterm);
1977
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001978 /*
1979 * If the window was resized a redraw will be triggered and we get here.
1980 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1981 */
1982 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1983 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001984 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001985 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1986 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1987 win_T *twp;
1988
1989 FOR_ALL_WINDOWS(twp)
1990 {
1991 /* When more than one window shows the same terminal, use the
1992 * smallest size. */
1993 if (twp->w_buffer == term->tl_buffer)
1994 {
1995 if (!term->tl_rows_fixed && rows > twp->w_height)
1996 rows = twp->w_height;
1997 if (!term->tl_cols_fixed && cols > twp->w_width)
1998 cols = twp->w_width;
1999 }
2000 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002001
2002 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002003 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002004 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002005 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002006 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002007
2008 /* The cursor may have been moved when resizing. */
2009 vterm_state_get_cursorpos(state, &pos);
2010 position_cursor(wp, &pos);
2011
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002012 /* TODO: Only redraw what changed. */
2013 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002014 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002015 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002016 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002017
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002018 if (pos.row < term->tl_rows)
2019 {
2020 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002021 {
2022 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002023 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002024
Bram Moolenaareeac6772017-07-23 15:48:37 +02002025 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2026 vim_memset(&cell, 0, sizeof(cell));
2027
2028 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002029 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002030 if (c == NUL)
2031 {
2032 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002033#if defined(FEAT_MBYTE)
2034 if (enc_utf8)
2035 ScreenLinesUC[off] = NUL;
2036#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002037 }
2038 else
2039 {
2040#if defined(FEAT_MBYTE)
2041 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002042 {
2043 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002044 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002045 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002046 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002047 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002049 if (enc_utf8)
2050 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002051 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002052#else
2053 ScreenLines[off] = c;
2054#endif
2055 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002056 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002057
2058 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002059 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002060 if (cell.width == 2)
2061 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002062 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002063#if defined(FEAT_MBYTE)
2064 if (enc_utf8)
2065 ScreenLinesUC[off] = NUL;
2066#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002067 ++pos.col;
2068 ++off;
2069 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002070 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002071 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002072 else
2073 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002074
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002075 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2076 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002077 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002078
2079 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002080}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002081
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002082/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002083 * Return TRUE if "wp" is a terminal window where the job has finished.
2084 */
2085 int
2086term_is_finished(buf_T *buf)
2087{
2088 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2089}
2090
2091/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002092 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002093 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002094 */
2095 int
2096term_show_buffer(buf_T *buf)
2097{
2098 term_T *term = buf->b_term;
2099
Bram Moolenaar6d819742017-08-06 14:57:49 +02002100 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002101}
2102
2103/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002104 * The current buffer is going to be changed. If there is terminal
2105 * highlighting remove it now.
2106 */
2107 void
2108term_change_in_curbuf(void)
2109{
2110 term_T *term = curbuf->b_term;
2111
2112 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2113 {
2114 free_scrollback(term);
2115 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002116
2117 /* The buffer is now like a normal buffer, it cannot be easily
2118 * abandoned when changed. */
2119 set_string_option_direct((char_u *)"buftype", -1,
2120 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002121 }
2122}
2123
2124/*
2125 * Get the screen attribute for a position in the buffer.
2126 */
2127 int
2128term_get_attr(buf_T *buf, linenr_T lnum, int col)
2129{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002130 term_T *term = buf->b_term;
2131 sb_line_T *line;
2132 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002133
Bram Moolenaar70229f92017-07-29 16:01:53 +02002134 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002135 return 0;
2136 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2137 if (col >= line->sb_cols)
2138 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002139 cellattr = line->sb_cells + col;
2140 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002141}
2142
2143/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002144 * Create a new vterm and initialize it.
2145 */
2146 static void
2147create_vterm(term_T *term, int rows, int cols)
2148{
2149 VTerm *vterm;
2150 VTermScreen *screen;
2151
2152 vterm = vterm_new(rows, cols);
2153 term->tl_vterm = vterm;
2154 screen = vterm_obtain_screen(vterm);
2155 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2156 /* TODO: depends on 'encoding'. */
2157 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002158
2159 /* Vterm uses a default black background. Set it to white when
2160 * 'background' is "light". */
2161 if (*p_bg == 'l')
2162 {
2163 VTermColor fg, bg;
2164
2165 fg.red = fg.green = fg.blue = 0;
2166 bg.red = bg.green = bg.blue = 255;
2167 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2168 }
2169
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002170 /* Required to initialize most things. */
2171 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002172
2173 /* Allow using alternate screen. */
2174 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002175}
2176
Bram Moolenaar21554412017-07-24 21:44:43 +02002177/*
2178 * Return the text to show for the buffer name and status.
2179 */
2180 char_u *
2181term_get_status_text(term_T *term)
2182{
2183 if (term->tl_status_text == NULL)
2184 {
2185 char_u *txt;
2186 size_t len;
2187
Bram Moolenaar6d819742017-08-06 14:57:49 +02002188 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002189 {
2190 if (term_job_running(term))
2191 txt = (char_u *)_("Terminal");
2192 else
2193 txt = (char_u *)_("Terminal-finished");
2194 }
2195 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002196 txt = term->tl_title;
2197 else if (term_job_running(term))
2198 txt = (char_u *)_("running");
2199 else
2200 txt = (char_u *)_("finished");
2201 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002202 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002203 if (term->tl_status_text != NULL)
2204 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2205 term->tl_buffer->b_fname, txt);
2206 }
2207 return term->tl_status_text;
2208}
2209
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002210/*
2211 * Mark references in jobs of terminals.
2212 */
2213 int
2214set_ref_in_term(int copyID)
2215{
2216 int abort = FALSE;
2217 term_T *term;
2218 typval_T tv;
2219
2220 for (term = first_term; term != NULL; term = term->tl_next)
2221 if (term->tl_job != NULL)
2222 {
2223 tv.v_type = VAR_JOB;
2224 tv.vval.v_job = term->tl_job;
2225 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2226 }
2227 return abort;
2228}
2229
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002230/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002231 * Get the buffer from the first argument in "argvars".
2232 * Returns NULL when the buffer is not for a terminal window.
2233 */
2234 static buf_T *
2235term_get_buf(typval_T *argvars)
2236{
2237 buf_T *buf;
2238
2239 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2240 ++emsg_off;
2241 buf = get_buf_tv(&argvars[0], FALSE);
2242 --emsg_off;
2243 if (buf == NULL || buf->b_term == NULL)
2244 return NULL;
2245 return buf;
2246}
2247
2248/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002249 * "term_getaltscreen(buf)" function
2250 */
2251 void
2252f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2253{
2254 buf_T *buf = term_get_buf(argvars);
2255
2256 if (buf == NULL)
2257 return;
2258 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2259}
2260
2261/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002262 * "term_getattr(attr, name)" function
2263 */
2264 void
2265f_term_getattr(typval_T *argvars, typval_T *rettv)
2266{
2267 int attr;
2268 size_t i;
2269 char_u *name;
2270
2271 static struct {
2272 char *name;
2273 int attr;
2274 } attrs[] = {
2275 {"bold", HL_BOLD},
2276 {"italic", HL_ITALIC},
2277 {"underline", HL_UNDERLINE},
2278 {"strike", HL_STANDOUT},
2279 {"reverse", HL_INVERSE},
2280 };
2281
2282 attr = get_tv_number(&argvars[0]);
2283 name = get_tv_string_chk(&argvars[1]);
2284 if (name == NULL)
2285 return;
2286
2287 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2288 if (STRCMP(name, attrs[i].name) == 0)
2289 {
2290 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2291 break;
2292 }
2293}
2294
2295/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002296 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002297 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002298 void
2299f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002300{
Bram Moolenaar97870002017-07-30 18:28:38 +02002301 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002302 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002303 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002304 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002305
Bram Moolenaar97870002017-07-30 18:28:38 +02002306 if (rettv_list_alloc(rettv) == FAIL)
2307 return;
2308 if (buf == NULL)
2309 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002310 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002311
2312 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002313 list_append_number(l, term->tl_cursor_pos.row + 1);
2314 list_append_number(l, term->tl_cursor_pos.col + 1);
2315
2316 d = dict_alloc();
2317 if (d != NULL)
2318 {
2319 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2320 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2321 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2322 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2323 ? (char_u *)"" : term->tl_cursor_color);
2324 list_append_dict(l, d);
2325 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002326}
2327
2328/*
2329 * "term_getjob(buf)" function
2330 */
2331 void
2332f_term_getjob(typval_T *argvars, typval_T *rettv)
2333{
2334 buf_T *buf = term_get_buf(argvars);
2335
2336 rettv->v_type = VAR_JOB;
2337 rettv->vval.v_job = NULL;
2338 if (buf == NULL)
2339 return;
2340
2341 rettv->vval.v_job = buf->b_term->tl_job;
2342 if (rettv->vval.v_job != NULL)
2343 ++rettv->vval.v_job->jv_refcount;
2344}
2345
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002346 static int
2347get_row_number(typval_T *tv, term_T *term)
2348{
2349 if (tv->v_type == VAR_STRING
2350 && tv->vval.v_string != NULL
2351 && STRCMP(tv->vval.v_string, ".") == 0)
2352 return term->tl_cursor_pos.row;
2353 return (int)get_tv_number(tv) - 1;
2354}
2355
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002356/*
2357 * "term_getline(buf, row)" function
2358 */
2359 void
2360f_term_getline(typval_T *argvars, typval_T *rettv)
2361{
2362 buf_T *buf = term_get_buf(argvars);
2363 term_T *term;
2364 int row;
2365
2366 rettv->v_type = VAR_STRING;
2367 if (buf == NULL)
2368 return;
2369 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002370 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002371
2372 if (term->tl_vterm == NULL)
2373 {
2374 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2375
2376 /* vterm is finished, get the text from the buffer */
2377 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2378 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2379 }
2380 else
2381 {
2382 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2383 VTermRect rect;
2384 int len;
2385 char_u *p;
2386
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002387 if (row < 0 || row >= term->tl_rows)
2388 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002389 len = term->tl_cols * MB_MAXBYTES + 1;
2390 p = alloc(len);
2391 if (p == NULL)
2392 return;
2393 rettv->vval.v_string = p;
2394
2395 rect.start_col = 0;
2396 rect.end_col = term->tl_cols;
2397 rect.start_row = row;
2398 rect.end_row = row + 1;
2399 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2400 }
2401}
2402
2403/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002404 * "term_getscrolled(buf)" function
2405 */
2406 void
2407f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2408{
2409 buf_T *buf = term_get_buf(argvars);
2410
2411 if (buf == NULL)
2412 return;
2413 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2414}
2415
2416/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002417 * "term_getsize(buf)" function
2418 */
2419 void
2420f_term_getsize(typval_T *argvars, typval_T *rettv)
2421{
2422 buf_T *buf = term_get_buf(argvars);
2423 list_T *l;
2424
2425 if (rettv_list_alloc(rettv) == FAIL)
2426 return;
2427 if (buf == NULL)
2428 return;
2429
2430 l = rettv->vval.v_list;
2431 list_append_number(l, buf->b_term->tl_rows);
2432 list_append_number(l, buf->b_term->tl_cols);
2433}
2434
2435/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002436 * "term_getstatus(buf)" function
2437 */
2438 void
2439f_term_getstatus(typval_T *argvars, typval_T *rettv)
2440{
2441 buf_T *buf = term_get_buf(argvars);
2442 term_T *term;
2443 char_u val[100];
2444
2445 rettv->v_type = VAR_STRING;
2446 if (buf == NULL)
2447 return;
2448 term = buf->b_term;
2449
2450 if (term_job_running(term))
2451 STRCPY(val, "running");
2452 else
2453 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002454 if (term->tl_normal_mode)
2455 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002456 rettv->vval.v_string = vim_strsave(val);
2457}
2458
2459/*
2460 * "term_gettitle(buf)" function
2461 */
2462 void
2463f_term_gettitle(typval_T *argvars, typval_T *rettv)
2464{
2465 buf_T *buf = term_get_buf(argvars);
2466
2467 rettv->v_type = VAR_STRING;
2468 if (buf == NULL)
2469 return;
2470
2471 if (buf->b_term->tl_title != NULL)
2472 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2473}
2474
2475/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002476 * "term_gettty(buf)" function
2477 */
2478 void
2479f_term_gettty(typval_T *argvars, typval_T *rettv)
2480{
2481 buf_T *buf = term_get_buf(argvars);
2482 char_u *p;
2483
2484 rettv->v_type = VAR_STRING;
2485 if (buf == NULL)
2486 return;
2487 if (buf->b_term->tl_job != NULL)
2488 p = buf->b_term->tl_job->jv_tty_name;
2489 else
2490 p = buf->b_term->tl_tty_name;
2491 if (p != NULL)
2492 rettv->vval.v_string = vim_strsave(p);
2493}
2494
2495/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002496 * "term_list()" function
2497 */
2498 void
2499f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2500{
2501 term_T *tp;
2502 list_T *l;
2503
2504 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2505 return;
2506
2507 l = rettv->vval.v_list;
2508 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2509 if (tp != NULL && tp->tl_buffer != NULL)
2510 if (list_append_number(l,
2511 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2512 return;
2513}
2514
2515/*
2516 * "term_scrape(buf, row)" function
2517 */
2518 void
2519f_term_scrape(typval_T *argvars, typval_T *rettv)
2520{
2521 buf_T *buf = term_get_buf(argvars);
2522 VTermScreen *screen = NULL;
2523 VTermPos pos;
2524 list_T *l;
2525 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002526 char_u *p;
2527 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002528
2529 if (rettv_list_alloc(rettv) == FAIL)
2530 return;
2531 if (buf == NULL)
2532 return;
2533 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002534
2535 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002536 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002537
2538 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002539 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002540 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002541 p = NULL;
2542 line = NULL;
2543 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002544 else
2545 {
2546 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2547
2548 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2549 return;
2550 p = ml_get_buf(buf, lnum + 1, FALSE);
2551 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2552 }
2553
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002554 for (pos.col = 0; pos.col < term->tl_cols; )
2555 {
2556 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002557 int width;
2558 VTermScreenCellAttrs attrs;
2559 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002560 char_u rgb[8];
2561 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2562 int off = 0;
2563 int i;
2564
2565 if (screen == NULL)
2566 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002567 cellattr_T *cellattr;
2568 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002569
2570 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002571 if (pos.col >= line->sb_cols)
2572 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002573 cellattr = line->sb_cells + pos.col;
2574 width = cellattr->width;
2575 attrs = cellattr->attrs;
2576 fg = cellattr->fg;
2577 bg = cellattr->bg;
2578 len = MB_PTR2LEN(p);
2579 mch_memmove(mbs, p, len);
2580 mbs[len] = NUL;
2581 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002582 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002583 else
2584 {
2585 VTermScreenCell cell;
2586 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2587 break;
2588 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2589 {
2590 if (cell.chars[i] == 0)
2591 break;
2592 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2593 }
2594 mbs[off] = NUL;
2595 width = cell.width;
2596 attrs = cell.attrs;
2597 fg = cell.fg;
2598 bg = cell.bg;
2599 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002600 dcell = dict_alloc();
2601 list_append_dict(l, dcell);
2602
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002603 dict_add_nr_str(dcell, "chars", 0, mbs);
2604
2605 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002606 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002607 dict_add_nr_str(dcell, "fg", 0, rgb);
2608 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002609 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002610 dict_add_nr_str(dcell, "bg", 0, rgb);
2611
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002612 dict_add_nr_str(dcell, "attr",
2613 cell2attr(attrs, fg, bg), NULL);
2614 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002615
2616 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002617 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002618 ++pos.col;
2619 }
2620}
2621
2622/*
2623 * "term_sendkeys(buf, keys)" function
2624 */
2625 void
2626f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2627{
2628 buf_T *buf = term_get_buf(argvars);
2629 char_u *msg;
2630 term_T *term;
2631
2632 rettv->v_type = VAR_UNKNOWN;
2633 if (buf == NULL)
2634 return;
2635
2636 msg = get_tv_string_chk(&argvars[1]);
2637 if (msg == NULL)
2638 return;
2639 term = buf->b_term;
2640 if (term->tl_vterm == NULL)
2641 return;
2642
2643 while (*msg != NUL)
2644 {
2645 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2646 msg += MB_PTR2LEN(msg);
2647 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002648}
2649
2650/*
2651 * "term_start(command, options)" function
2652 */
2653 void
2654f_term_start(typval_T *argvars, typval_T *rettv)
2655{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002656 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002657
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002658 init_job_options(&opt);
2659 /* TODO: allow more job options */
2660 if (argvars[1].v_type != VAR_UNKNOWN
2661 && get_job_options(&argvars[1], &opt,
2662 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002663 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002664 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002665 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002666 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002667 return;
2668
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002669 if (opt.jo_vertical)
2670 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002671 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002672
2673 if (curbuf->b_term != NULL)
2674 rettv->vval.v_number = curbuf->b_fnum;
2675}
2676
2677/*
2678 * "term_wait" function
2679 */
2680 void
2681f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2682{
2683 buf_T *buf = term_get_buf(argvars);
2684
2685 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002686 {
2687 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002688 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002689 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002690 if (buf->b_term->tl_job == NULL)
2691 {
2692 ch_log(NULL, "term_wait(): no job to wait for");
2693 return;
2694 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002695
2696 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002697 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002698 {
2699 /* The job is dead, keep reading channel I/O until the channel is
2700 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002701 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002702 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2703 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002704 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002705 parse_queued_messages();
2706 ui_delay(10L, FALSE);
2707 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002708 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002709 parse_queued_messages();
2710 }
2711 else
2712 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002713 long wait = 10L;
2714
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002715 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002716 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002717
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002718 /* Wait for some time for any channel I/O. */
2719 if (argvars[1].v_type != VAR_UNKNOWN)
2720 wait = get_tv_number(&argvars[1]);
2721 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002722 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002723
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002724 /* Flushing messages on channels is hopefully sufficient.
2725 * TODO: is there a better way? */
2726 parse_queued_messages();
2727 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002728}
2729
Bram Moolenaara83e3962017-08-17 14:39:07 +02002730# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002731
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002732/**************************************
2733 * 2. MS-Windows implementation.
2734 */
2735
Bram Moolenaara83e3962017-08-17 14:39:07 +02002736# ifndef PROTO
2737
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002738#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2739#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2740
Bram Moolenaar8a773062017-07-24 22:29:21 +02002741void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002742void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002743void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002744BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2745void (*winpty_config_set_initial_size)(void*, int, int);
2746LPCWSTR (*winpty_conin_name)(void*);
2747LPCWSTR (*winpty_conout_name)(void*);
2748LPCWSTR (*winpty_conerr_name)(void*);
2749void (*winpty_free)(void*);
2750void (*winpty_config_free)(void*);
2751void (*winpty_spawn_config_free)(void*);
2752void (*winpty_error_free)(void*);
2753LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002754BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002755HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002756
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002757#define WINPTY_DLL "winpty.dll"
2758
2759static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002760# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002761
Bram Moolenaara83e3962017-08-17 14:39:07 +02002762 static int
2763dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002764{
2765 int i;
2766 static struct
2767 {
2768 char *name;
2769 FARPROC *ptr;
2770 } winpty_entry[] =
2771 {
2772 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2773 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2774 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2775 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2776 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2777 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2778 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2779 {"winpty_free", (FARPROC*)&winpty_free},
2780 {"winpty_open", (FARPROC*)&winpty_open},
2781 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2782 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2783 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2784 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002785 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002786 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002787 {NULL, NULL}
2788 };
2789
2790 /* No need to initialize twice. */
2791 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002792 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002793 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2794 * winpty.dll. */
2795 if (*p_winptydll != NUL)
2796 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2797 if (!hWinPtyDLL)
2798 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002799 if (!hWinPtyDLL)
2800 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002801 if (verbose)
2802 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2803 : (char_u *)WINPTY_DLL);
2804 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002805 }
2806 for (i = 0; winpty_entry[i].name != NULL
2807 && winpty_entry[i].ptr != NULL; ++i)
2808 {
2809 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2810 winpty_entry[i].name)) == NULL)
2811 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002812 if (verbose)
2813 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2814 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002815 }
2816 }
2817
Bram Moolenaara83e3962017-08-17 14:39:07 +02002818 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002819}
2820
2821/*
2822 * Create a new terminal of "rows" by "cols" cells.
2823 * Store a reference in "term".
2824 * Return OK or FAIL.
2825 */
2826 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002827term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002828{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002829 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002830 channel_T *channel = NULL;
2831 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002832 DWORD error;
2833 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2834 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002835 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002836 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002837 garray_T ga;
2838 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002839
Bram Moolenaara83e3962017-08-17 14:39:07 +02002840 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002841 return FAIL;
2842
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002843 if (argvar->v_type == VAR_STRING)
2844 cmd = argvar->vval.v_string;
2845 else
2846 {
2847 ga_init2(&ga, (int)sizeof(char*), 20);
2848 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2849 goto failed;
2850 cmd = ga.ga_data;
2851 }
2852
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002853 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002854 if (p == NULL)
2855 return FAIL;
2856
2857 job = job_alloc();
2858 if (job == NULL)
2859 goto failed;
2860
2861 channel = add_channel();
2862 if (channel == NULL)
2863 goto failed;
2864
2865 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2866 if (term->tl_winpty_config == NULL)
2867 goto failed;
2868
2869 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2870 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2871 if (term->tl_winpty == NULL)
2872 goto failed;
2873
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002874 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002875 spawn_config = winpty_spawn_config_new(
2876 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2877 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2878 NULL,
2879 p,
2880 NULL,
2881 NULL,
2882 &winpty_err);
2883 if (spawn_config == NULL)
2884 goto failed;
2885
2886 channel = add_channel();
2887 if (channel == NULL)
2888 goto failed;
2889
2890 job = job_alloc();
2891 if (job == NULL)
2892 goto failed;
2893
2894 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2895 &child_thread_handle, &error, &winpty_err))
2896 goto failed;
2897
2898 channel_set_pipes(channel,
2899 (sock_T) CreateFileW(
2900 winpty_conin_name(term->tl_winpty),
2901 GENERIC_WRITE, 0, NULL,
2902 OPEN_EXISTING, 0, NULL),
2903 (sock_T) CreateFileW(
2904 winpty_conout_name(term->tl_winpty),
2905 GENERIC_READ, 0, NULL,
2906 OPEN_EXISTING, 0, NULL),
2907 (sock_T) CreateFileW(
2908 winpty_conerr_name(term->tl_winpty),
2909 GENERIC_READ, 0, NULL,
2910 OPEN_EXISTING, 0, NULL));
2911
2912 jo = CreateJobObject(NULL, NULL);
2913 if (jo == NULL)
2914 goto failed;
2915
2916 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002917 {
2918 /* Failed, switch the way to terminate process with TerminateProcess. */
2919 CloseHandle(jo);
2920 jo = NULL;
2921 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002922
2923 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002924 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002925
2926 create_vterm(term, rows, cols);
2927
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002928 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002929 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002930
2931 job->jv_channel = channel;
2932 job->jv_proc_info.hProcess = child_process_handle;
2933 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2934 job->jv_job_object = jo;
2935 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002936 sprintf(buf, "winpty://%lu",
2937 GetProcessId(winpty_agent_process(term->tl_winpty)));
2938 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002939 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002940 term->tl_job = job;
2941
2942 return OK;
2943
2944failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002945 if (argvar->v_type == VAR_LIST)
2946 vim_free(ga.ga_data);
2947 if (p != NULL)
2948 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002949 if (spawn_config != NULL)
2950 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002951 if (channel != NULL)
2952 channel_clear(channel);
2953 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002954 {
2955 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002956 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002957 }
2958 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002959 if (jo != NULL)
2960 CloseHandle(jo);
2961 if (term->tl_winpty != NULL)
2962 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002963 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002964 if (term->tl_winpty_config != NULL)
2965 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002966 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002967 if (winpty_err != NULL)
2968 {
2969 char_u *msg = utf16_to_enc(
2970 (short_u *)winpty_error_msg(winpty_err), NULL);
2971
2972 EMSG(msg);
2973 winpty_error_free(winpty_err);
2974 }
2975 return FAIL;
2976}
2977
2978/*
2979 * Free the terminal emulator part of "term".
2980 */
2981 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002982term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002983{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002984 if (term->tl_winpty != NULL)
2985 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002986 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002987 if (term->tl_winpty_config != NULL)
2988 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002989 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002990 if (term->tl_vterm != NULL)
2991 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002992 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002993}
2994
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002995/*
2996 * Request size to terminal.
2997 */
2998 static void
2999term_report_winsize(term_T *term, int rows, int cols)
3000{
3001 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3002}
3003
Bram Moolenaara83e3962017-08-17 14:39:07 +02003004 int
3005terminal_enabled(void)
3006{
3007 return dyn_winpty_init(FALSE) == OK;
3008}
3009
3010
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003011# else
3012
3013/**************************************
3014 * 3. Unix-like implementation.
3015 */
3016
3017/*
3018 * Create a new terminal of "rows" by "cols" cells.
3019 * Start job for "cmd".
3020 * Store the pointers in "term".
3021 * Return OK or FAIL.
3022 */
3023 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003024term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003025{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003026 create_vterm(term, rows, cols);
3027
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003028 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003029 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003030 if (term->tl_job != NULL)
3031 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003032
Bram Moolenaar61a66052017-07-22 18:39:00 +02003033 return term->tl_job != NULL
3034 && term->tl_job->jv_channel != NULL
3035 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036}
3037
3038/*
3039 * Free the terminal emulator part of "term".
3040 */
3041 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003042term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003043{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003044 if (term->tl_vterm != NULL)
3045 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003046 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003047}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003048
3049/*
3050 * Request size to terminal.
3051 */
3052 static void
3053term_report_winsize(term_T *term, int rows, int cols)
3054{
3055 /* Use an ioctl() to report the new window size to the job. */
3056 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3057 {
3058 int fd = -1;
3059 int part;
3060
3061 for (part = PART_OUT; part < PART_COUNT; ++part)
3062 {
3063 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3064 if (isatty(fd))
3065 break;
3066 }
3067 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003068 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003069 }
3070}
3071
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003072# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003073
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003074#endif /* FEAT_TERMINAL */