blob: 6b401beee85993621f3223aa165dfa76ff3a512b [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 Moolenaar3eee06e2017-08-19 19:40:50 +020041 * - help index for winptydll, optwin entry for winptydll
Bram Moolenaar9e13aa72017-08-16 23:14:08 +020042 * - make [range]terminal pipe [range] lines to the terminal
Bram Moolenaar94053a52017-08-01 21:44:33 +020043 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020045 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020046 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020047 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020048 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
49 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
50 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
51 * Check that something is connected to the terminal.
52 * Test: "cat" reading from a file or buffer
53 * "ls" writing stdout to a file or buffer
54 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020055 * - For the GUI fill termios with default values, perhaps like pangoterm:
56 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020057 * - support ":term NONE" to open a terminal with a pty but not running a job
58 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020059 * - if the job in the terminal does not support the mouse, we can use the
60 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020061 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
62 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020063 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020064 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020065 * - Copy text in the vterm to the Vim buffer once in a while, so that
66 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020067 */
68
69#include "vim.h"
70
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020071#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020072
Bram Moolenaard5310982017-08-05 15:16:32 +020073#ifndef MIN
74# define MIN(x,y) ((x) < (y) ? (x) : (y))
75#endif
76#ifndef MAX
77# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020078#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020080#include "libvterm/include/vterm.h"
81
Bram Moolenaar33a43be2017-08-06 21:36:22 +020082/* This is VTermScreenCell without the characters, thus much smaller. */
83typedef struct {
84 VTermScreenCellAttrs attrs;
85 char width;
86 VTermColor fg, bg;
87} cellattr_T;
88
Bram Moolenaard85f2712017-07-28 21:51:57 +020089typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020090 int sb_cols; /* can differ per line */
91 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020092} sb_line_T;
93
Bram Moolenaare4f25e42017-07-07 11:54:15 +020094/* typedef term_T in structs.h */
95struct terminal_S {
96 term_T *tl_next;
97
Bram Moolenaar423802d2017-07-30 16:52:24 +020098 VTerm *tl_vterm;
99 job_T *tl_job;
100 buf_T *tl_buffer;
101
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200102 /* used when tl_job is NULL and only a pty was created */
103 int tl_tty_fd;
104 char_u *tl_tty_name;
105
Bram Moolenaar6d819742017-08-06 14:57:49 +0200106 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200107 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200108 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200109 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200111#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200112 void *tl_winpty_config;
113 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200115
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200116 /* last known vterm size */
117 int tl_rows;
118 int tl_cols;
119 /* vterm size does not follow window size */
120 int tl_rows_fixed;
121 int tl_cols_fixed;
122
Bram Moolenaar21554412017-07-24 21:44:43 +0200123 char_u *tl_title; /* NULL or allocated */
124 char_u *tl_status_text; /* NULL or allocated */
125
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200126 /* Range of screen rows to update. Zero based. */
127 int tl_dirty_row_start; /* -1 if nothing dirty */
128 int tl_dirty_row_end; /* row below last one to update */
129
Bram Moolenaard85f2712017-07-28 21:51:57 +0200130 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200131 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200132
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200133 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200134 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200135 int tl_cursor_blink;
136 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
137 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200138
139 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200140};
141
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200142#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
143#define TMODE_LOOP 2 /* CTRL-W N used */
144
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200145/*
146 * List of all active terminals.
147 */
148static term_T *first_term = NULL;
149
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200150/* Terminal active in terminal_loop(). */
151static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200152
153#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
154#define KEY_BUF_LEN 200
155
156/*
157 * Functions with separate implementation for MS-Windows and Unix-like systems.
158 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200159static int term_and_job_init(term_T *term, int rows, int cols,
160 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200161static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200162static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200163
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200164/* The characters that we know (or assume) that the terminal expects for the
165 * backspace and enter keys. */
166static int term_backspace_char = BS;
167static int term_enter_char = CAR;
168static int term_nl_does_cr = FALSE;
169
170
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200171/**************************************
172 * 1. Generic code for all systems.
173 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200174
175/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200176 * Determine the terminal size from 'termsize' and the current window.
177 * Assumes term->tl_rows and term->tl_cols are zero.
178 */
179 static void
180set_term_and_win_size(term_T *term)
181{
182 if (*curwin->w_p_tms != NUL)
183 {
184 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
185
186 term->tl_rows = atoi((char *)curwin->w_p_tms);
187 term->tl_cols = atoi((char *)p);
188 }
189 if (term->tl_rows == 0)
190 term->tl_rows = curwin->w_height;
191 else
192 {
193 win_setheight_win(term->tl_rows, curwin);
194 term->tl_rows_fixed = TRUE;
195 }
196 if (term->tl_cols == 0)
197 term->tl_cols = curwin->w_width;
198 else
199 {
200 win_setwidth_win(term->tl_cols, curwin);
201 term->tl_cols_fixed = TRUE;
202 }
203}
204
205/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200206 * Initialize job options for a terminal job.
207 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200208 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 static void
210init_job_options(jobopt_T *opt)
211{
212 clear_job_options(opt);
213
214 opt->jo_mode = MODE_RAW;
215 opt->jo_out_mode = MODE_RAW;
216 opt->jo_err_mode = MODE_RAW;
217 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
218
219 opt->jo_io[PART_OUT] = JIO_BUFFER;
220 opt->jo_io[PART_ERR] = JIO_BUFFER;
221 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
222
223 opt->jo_modifiable[PART_OUT] = 0;
224 opt->jo_modifiable[PART_ERR] = 0;
225 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
226
227 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
228}
229
230/*
231 * Set job options mandatory for a terminal job.
232 */
233 static void
234setup_job_options(jobopt_T *opt, int rows, int cols)
235{
236 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
237 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
238 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200239 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
240 opt->jo_term_rows = rows;
241 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
242 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200243}
244
245 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200246term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 exarg_T split_ea;
249 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200251 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 if (check_restricted() || check_secure())
254 return;
255
256 term = (term_T *)alloc_clear(sizeof(term_T));
257 if (term == NULL)
258 return;
259 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200260 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200261 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200262 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200263 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200268 /* Create a new buffer in the current window. */
269 if (!can_abandon(curbuf, forceit))
270 {
Bram Moolenaarf5be7cd2017-08-17 16:55:13 +0200271 no_write_message();
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200272 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 return;
274 }
275 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
276 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 {
278 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200279 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 }
281 }
282 else if (opt->jo_hidden)
283 {
284 buf_T *buf;
285
286 /* Create a new buffer without a window. Make it the current buffer for
287 * a moment to be able to do the initialisations. */
288 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
289 BLN_NEW | BLN_LISTED);
290 if (buf == NULL || ml_open(buf) == FAIL)
291 {
292 vim_free(term);
293 return;
294 }
295 old_curbuf = curbuf;
296 --curbuf->b_nwindows;
297 curbuf = buf;
298 curwin->w_buffer = buf;
299 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200303 /* Open a new window or tab. */
304 split_ea.cmdidx = CMD_new;
305 split_ea.cmd = (char_u *)"new";
306 split_ea.arg = (char_u *)"";
307 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
308 {
309 split_ea.line2 = opt->jo_term_rows;
310 split_ea.addr_count = 1;
311 }
312 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
313 {
314 split_ea.line2 = opt->jo_term_cols;
315 split_ea.addr_count = 1;
316 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200317
Bram Moolenaarda43b612017-08-11 22:27:50 +0200318 ex_splitview(&split_ea);
319 if (curwin == old_curwin)
320 {
321 /* split failed */
322 vim_free(term);
323 return;
324 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200326 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200328
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200329 if (!opt->jo_hidden)
330 {
331 /* only one size was taken care of with :new, do the other one */
332 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
333 win_setheight(opt->jo_term_rows);
334 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
335 win_setwidth(opt->jo_term_cols);
336 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200337
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200338 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339 term->tl_next = first_term;
340 first_term = term;
341
Bram Moolenaar78712a72017-08-05 14:50:12 +0200342 if (opt->jo_term_name != NULL)
343 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
344 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200345 {
346 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200347 size_t len;
348 char_u *cmd, *p;
349
350 if (argvar->v_type == VAR_STRING)
351 cmd = argvar->vval.v_string;
352 else if (argvar->v_type != VAR_LIST
353 || argvar->vval.v_list == NULL
354 || argvar->vval.v_list->lv_len < 1)
355 cmd = (char_u*)"";
356 else
357 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
358
359 len = STRLEN(cmd) + 10;
360 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200361
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200362 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200363 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200364 /* Prepend a ! to the command name to avoid the buffer name equals
365 * the executable, otherwise ":w!" would overwrite it. */
366 if (i == 0)
367 vim_snprintf((char *)p, len, "!%s", cmd);
368 else
369 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200370 if (buflist_findname(p) == NULL)
371 {
372 curbuf->b_ffname = p;
373 break;
374 }
375 }
376 }
377 curbuf->b_fname = curbuf->b_ffname;
378
Bram Moolenaar37c45832017-08-12 16:01:04 +0200379 if (opt->jo_term_opencmd != NULL)
380 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
381
Bram Moolenaareb44a682017-08-03 22:44:55 +0200382 set_string_option_direct((char_u *)"buftype", -1,
383 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
384
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200385 /* Mark the buffer as not modifiable. It can only be made modifiable after
386 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200387 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200388
389 /* Set 'bufhidden' to "hide": allow closing the window. */
390 set_string_option_direct((char_u *)"bufhidden", -1,
391 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200392
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200393 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200394 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200397 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
398 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200400 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200402 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200403
Bram Moolenaar97bd5e62017-08-18 20:50:30 +0200404 /* Make sure we don't get stuck on sending keys to the job, it leads to
405 * a deadlock if the job is waiting for Vim to read. */
406 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
407
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200408 if (old_curbuf != NULL)
409 {
410 --curbuf->b_nwindows;
411 curbuf = old_curbuf;
412 curwin->w_buffer = curbuf;
413 ++curbuf->b_nwindows;
414 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200415 }
416 else
417 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200418 buf_T *buf = curbuf;
419
Bram Moolenaard85f2712017-07-28 21:51:57 +0200420 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200421 if (old_curbuf != NULL)
422 {
423 --curbuf->b_nwindows;
424 curbuf = old_curbuf;
425 curwin->w_buffer = curbuf;
426 ++curbuf->b_nwindows;
427 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200428
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200429 /* Wiping out the buffer will also close the window and call
430 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200431 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200432 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200433}
434
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200435/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200436 * ":terminal": open a terminal window and execute a job in it.
437 */
438 void
439ex_terminal(exarg_T *eap)
440{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200441 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 jobopt_T opt;
443 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200444 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200445
446 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200447
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448 cmd = eap->arg;
449 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
450 {
451 char_u *p;
452
453 cmd += 2;
454 p = skiptowhite(cmd);
455 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
456 opt.jo_term_finish = 'c';
457 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
458 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200459 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
460 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200461 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
462 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200463 else
464 {
465 if (*p)
466 *p = NUL;
467 EMSG2(_("E181: Invalid attribute: %s"), cmd);
468 return;
469 }
470 cmd = skipwhite(p);
471 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200472 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200473 /* Make a copy, an autocommand may set 'shell'. */
474 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200475
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200476 if (eap->addr_count == 2)
477 {
478 opt.jo_term_rows = eap->line1;
479 opt.jo_term_cols = eap->line2;
480 }
481 else if (eap->addr_count == 1)
482 {
483 if (cmdmod.split & WSP_VERT)
484 opt.jo_term_cols = eap->line2;
485 else
486 opt.jo_term_rows = eap->line2;
487 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200488
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200489 argvar.v_type = VAR_STRING;
490 argvar.vval.v_string = cmd;
491 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200492 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200493}
494
495/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200496 * Free the scrollback buffer for "term".
497 */
498 static void
499free_scrollback(term_T *term)
500{
501 int i;
502
503 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
504 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
505 ga_clear(&term->tl_scrollback);
506}
507
508/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200509 * Free a terminal and everything it refers to.
510 * Kills the job if there is one.
511 * Called when wiping out a buffer.
512 */
513 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200514free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200515{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200516 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200517 term_T *tp;
518
519 if (term == NULL)
520 return;
521 if (first_term == term)
522 first_term = term->tl_next;
523 else
524 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
525 if (tp->tl_next == term)
526 {
527 tp->tl_next = term->tl_next;
528 break;
529 }
530
531 if (term->tl_job != NULL)
532 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200533 if (term->tl_job->jv_status != JOB_ENDED
534 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200535 job_stop(term->tl_job, NULL, "kill");
536 job_unref(term->tl_job);
537 }
538
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200539 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200540
541 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200542 vim_free(term->tl_title);
543 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200544 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200545 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200546 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200547 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200548 if (in_terminal_loop == term)
549 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200550}
551
552/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200553 * Write job output "msg[len]" to the vterm.
554 */
555 static void
556term_write_job_output(term_T *term, char_u *msg, size_t len)
557{
558 VTerm *vterm = term->tl_vterm;
559 char_u *p;
560 size_t done;
561 size_t len_now;
562
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200563 if (term_nl_does_cr)
564 vterm_input_write(vterm, (char *)msg, len);
565 else
566 /* need to convert NL to CR-NL */
567 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200568 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200569 for (p = msg + done; p < msg + len; )
570 {
571 if (*p == NL)
572 break;
573 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
574 }
575 len_now = p - msg - done;
576 vterm_input_write(vterm, (char *)msg + done, len_now);
577 if (p < msg + len && *p == NL)
578 {
579 vterm_input_write(vterm, "\r\n", 2);
580 ++len_now;
581 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200583
584 /* this invokes the damage callbacks */
585 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
586}
587
Bram Moolenaar1c844932017-07-24 23:36:41 +0200588 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200589update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200590{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200591 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200592 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200593 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200594 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200595 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200596 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200597 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200598 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200599#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200600 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200601 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200602#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200603 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200604}
605
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200606/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200607 * Invoked when "msg" output from a job was received. Write it to the terminal
608 * of "buffer".
609 */
610 void
611write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
612{
613 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200614 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200615
Bram Moolenaard85f2712017-07-28 21:51:57 +0200616 if (term->tl_vterm == NULL)
617 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200618 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200619 return;
620 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200621 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200622 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200623
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200624 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
625 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200626 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200627 {
628 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200629 ch_log(term->tl_job->jv_channel, "updating screen");
630 if (buffer == curbuf)
631 {
632 update_screen(0);
633 update_cursor(term, TRUE);
634 }
635 else
636 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200637 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200638}
639
640/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200641 * Send a mouse position and click to the vterm
642 */
643 static int
644term_send_mouse(VTerm *vterm, int button, int pressed)
645{
646 VTermModifier mod = VTERM_MOD_NONE;
647
648 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
649 mouse_col - W_WINCOL(curwin), mod);
650 vterm_mouse_button(vterm, button, pressed, mod);
651 return TRUE;
652}
653
654/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 * Convert typed key "c" into bytes to send to the job.
656 * Return the number of bytes in "buf".
657 */
658 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200659term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200661 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200662 VTermKey key = VTERM_KEY_NONE;
663 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200664 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200665
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200666 switch (c)
667 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200668 case CAR: c = term_enter_char; break;
669 /* don't use VTERM_KEY_BACKSPACE, it always
670 * becomes 0x7f DEL */
671 case K_BS: c = term_backspace_char; break;
672
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200674 case K_DEL: key = VTERM_KEY_DEL; break;
675 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200676 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
677 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200678 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200679 case K_S_END: mod = VTERM_MOD_SHIFT;
680 key = VTERM_KEY_END; break;
681 case K_C_END: mod = VTERM_MOD_CTRL;
682 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200683 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
684 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
685 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
686 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
687 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
688 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
689 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
690 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
691 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
692 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
693 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
694 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
695 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200696 case K_S_HOME: mod = VTERM_MOD_SHIFT;
697 key = VTERM_KEY_HOME; break;
698 case K_C_HOME: mod = VTERM_MOD_CTRL;
699 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200700 case K_INS: key = VTERM_KEY_INS; break;
701 case K_K0: key = VTERM_KEY_KP_0; break;
702 case K_K1: key = VTERM_KEY_KP_1; break;
703 case K_K2: key = VTERM_KEY_KP_2; break;
704 case K_K3: key = VTERM_KEY_KP_3; break;
705 case K_K4: key = VTERM_KEY_KP_4; break;
706 case K_K5: key = VTERM_KEY_KP_5; break;
707 case K_K6: key = VTERM_KEY_KP_6; break;
708 case K_K7: key = VTERM_KEY_KP_7; break;
709 case K_K8: key = VTERM_KEY_KP_8; break;
710 case K_K9: key = VTERM_KEY_KP_9; break;
711 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
712 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
713 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
714 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
715 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
716 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
717 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
718 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
719 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
720 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
721 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
722 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
723 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200724 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
725 key = VTERM_KEY_LEFT; break;
726 case K_C_LEFT: mod = VTERM_MOD_CTRL;
727 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200728 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
729 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
730 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
732 key = VTERM_KEY_RIGHT; break;
733 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
734 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200735 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200736 case K_S_UP: mod = VTERM_MOD_SHIFT;
737 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200738 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200739
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200740 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
741 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
742 case K_MOUSELEFT: /* TODO */ return 0;
743 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200744
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200745 case K_LEFTMOUSE:
746 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
747 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
748 case K_LEFTRELEASE:
749 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
750 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
751 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
752 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
753 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
754 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
755 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
756 case K_X1MOUSE: /* TODO */ return 0;
757 case K_X1DRAG: /* TODO */ return 0;
758 case K_X1RELEASE: /* TODO */ return 0;
759 case K_X2MOUSE: /* TODO */ return 0;
760 case K_X2DRAG: /* TODO */ return 0;
761 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200762
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200763 case K_IGNORE: return 0;
764 case K_NOP: return 0;
765 case K_UNDO: return 0;
766 case K_HELP: return 0;
767 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
768 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
769 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
770 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
771 case K_SELECT: return 0;
772#ifdef FEAT_GUI
773 case K_VER_SCROLLBAR: return 0;
774 case K_HOR_SCROLLBAR: return 0;
775#endif
776#ifdef FEAT_GUI_TABLINE
777 case K_TABLINE: return 0;
778 case K_TABMENU: return 0;
779#endif
780#ifdef FEAT_NETBEANS_INTG
781 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
782#endif
783#ifdef FEAT_DND
784 case K_DROP: return 0;
785#endif
786#ifdef FEAT_AUTOCMD
787 case K_CURSORHOLD: return 0;
788#endif
789 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
790 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200791 }
792
793 /*
794 * Convert special keys to vterm keys:
795 * - Write keys to vterm: vterm_keyboard_key()
796 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200797 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200798 */
799 if (key != VTERM_KEY_NONE)
800 /* Special key, let vterm convert it. */
801 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200802 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200803 /* Normal character, let vterm convert it. */
804 vterm_keyboard_unichar(vterm, c, mod);
805
806 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200807 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200808}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200809
Bram Moolenaar938783d2017-07-16 20:13:26 +0200810/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200811 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200812 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200813 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200814term_job_running(term_T *term)
815{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200816 /* Also consider the job finished when the channel is closed, to avoid a
817 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200818 return term != NULL
819 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200820 && term->tl_job->jv_status == JOB_STARTED
821 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200822}
823
824/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200825 * Add the last line of the scrollback buffer to the buffer in the window.
826 */
827 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200828add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200829{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200830 buf_T *buf = term->tl_buffer;
831 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
832 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200833
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200834 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200835 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836 {
837 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200838 curbuf = buf;
839 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200840 curbuf = curwin->w_buffer;
841 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200842}
843
844/*
845 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200847 */
848 static void
849move_terminal_to_buffer(term_T *term)
850{
851 win_T *wp;
852 int len;
853 int lines_skipped = 0;
854 VTermPos pos;
855 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200856 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200857 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200858
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200859 if (term->tl_vterm == NULL)
860 return;
861 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
863 {
864 len = 0;
865 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
866 if (vterm_screen_get_cell(screen, pos, &cell) != 0
867 && cell.chars[0] != NUL)
868 len = pos.col + 1;
869
870 if (len == 0)
871 ++lines_skipped;
872 else
873 {
874 while (lines_skipped > 0)
875 {
876 /* Line was skipped, add an empty line. */
877 --lines_skipped;
878 if (ga_grow(&term->tl_scrollback, 1) == OK)
879 {
880 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
881 + term->tl_scrollback.ga_len;
882
883 line->sb_cols = 0;
884 line->sb_cells = NULL;
885 ++term->tl_scrollback.ga_len;
886
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200887 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 }
889 }
890
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200891 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200892 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
893 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200894 garray_T ga;
895 int width;
896 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897 + term->tl_scrollback.ga_len;
898
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200899 ga_init2(&ga, 1, 100);
900 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200901 {
902 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200903 {
904 width = 1;
905 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
906 if (ga_grow(&ga, 1) == OK)
907 ga.ga_len += mb_char2bytes(' ',
908 (char_u *)ga.ga_data + ga.ga_len);
909 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200910 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200911 {
912 width = cell.width;
913
914 p[pos.col].width = cell.width;
915 p[pos.col].attrs = cell.attrs;
916 p[pos.col].fg = cell.fg;
917 p[pos.col].bg = cell.bg;
918
919 if (ga_grow(&ga, MB_MAXBYTES) == OK)
920 {
921 int i;
922 int c;
923
924 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
925 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
926 (char_u *)ga.ga_data + ga.ga_len);
927 }
928 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 }
930 line->sb_cols = len;
931 line->sb_cells = p;
932 ++term->tl_scrollback.ga_len;
933
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200934 if (ga_grow(&ga, 1) == FAIL)
935 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
936 else
937 {
938 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
939 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
940 }
941 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200942 }
943 else
944 vim_free(p);
945 }
946 }
947
948 FOR_ALL_WINDOWS(wp)
949 {
950 if (wp->w_buffer == term->tl_buffer)
951 {
952 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
953 wp->w_cursor.col = 0;
954 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200955 if (wp->w_cursor.lnum >= wp->w_height)
956 {
957 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
958
959 if (wp->w_topline < min_topline)
960 wp->w_topline = min_topline;
961 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962 redraw_win_later(wp, NOT_VALID);
963 }
964 }
965}
966
967 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200968set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200969{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200970 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200971 vim_free(term->tl_status_text);
972 term->tl_status_text = NULL;
973 if (term->tl_buffer == curbuf)
974 maketitle();
975}
976
977/*
978 * Called after the job if finished and Terminal mode is not active:
979 * Move the vterm contents into the scrollback buffer and free the vterm.
980 */
981 static void
982cleanup_vterm(term_T *term)
983{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200984 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200985 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200987 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200988}
989
990/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200991 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200992 * Suspends updating the terminal window.
993 */
994 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200995term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200996{
997 term_T *term = curbuf->b_term;
998
999 /* Append the current terminal contents to the buffer. */
1000 move_terminal_to_buffer(term);
1001
Bram Moolenaar6d819742017-08-06 14:57:49 +02001002 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001003
Bram Moolenaar6d819742017-08-06 14:57:49 +02001004 /* Move the window cursor to the position of the cursor in the
1005 * terminal. */
1006 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1007 + term->tl_cursor_pos.row + 1;
1008 check_cursor();
1009 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001010
Bram Moolenaar6d819742017-08-06 14:57:49 +02001011 /* Display the same lines as in the terminal. */
1012 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001013}
1014
1015/*
1016 * Returns TRUE if the current window contains a terminal and we are in
1017 * Terminal-Normal mode.
1018 */
1019 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001020term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001021{
1022 term_T *term = curbuf->b_term;
1023
Bram Moolenaar6d819742017-08-06 14:57:49 +02001024 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001025}
1026
1027/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001028 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001029 * Restores updating the terminal window.
1030 */
1031 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001032term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001033{
1034 term_T *term = curbuf->b_term;
1035 sb_line_T *line;
1036 garray_T *gap;
1037
1038 /* Remove the terminal contents from the scrollback and the buffer. */
1039 gap = &term->tl_scrollback;
1040 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1041 {
1042 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1043 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1044 vim_free(line->sb_cells);
1045 --gap->ga_len;
1046 if (gap->ga_len == 0)
1047 break;
1048 }
1049 check_cursor();
1050
Bram Moolenaar6d819742017-08-06 14:57:49 +02001051 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001052
1053 if (term->tl_channel_closed)
1054 cleanup_vterm(term);
1055 redraw_buf_and_status_later(curbuf, NOT_VALID);
1056}
1057
1058/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001059 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001060 * Note: while waiting a terminal may be closed and freed if the channel is
1061 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001062 * TODO: use terminal mode mappings.
1063 */
1064 static int
1065term_vgetc()
1066{
1067 int c;
1068
1069 ++no_mapping;
1070 ++allow_keys;
1071 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001072#ifdef WIN3264
1073 ctrl_break_was_pressed = FALSE;
1074#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001075 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001076 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001077 --no_mapping;
1078 --allow_keys;
1079 return c;
1080}
1081
1082/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001083 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001084 * Return FAIL when the key needs to be handled in Normal mode.
1085 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001086 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001087 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001088send_keys_to_term(term_T *term, int c, int typed)
1089{
1090 char msg[KEY_BUF_LEN];
1091 size_t len;
1092 static int mouse_was_outside = FALSE;
1093 int dragging_outside = FALSE;
1094
1095 /* Catch keys that need to be handled as in Normal mode. */
1096 switch (c)
1097 {
1098 case NUL:
1099 case K_ZERO:
1100 if (typed)
1101 stuffcharReadbuff(c);
1102 return FAIL;
1103
1104 case K_IGNORE:
1105 return FAIL;
1106
1107 case K_LEFTDRAG:
1108 case K_MIDDLEDRAG:
1109 case K_RIGHTDRAG:
1110 case K_X1DRAG:
1111 case K_X2DRAG:
1112 dragging_outside = mouse_was_outside;
1113 /* FALLTHROUGH */
1114 case K_LEFTMOUSE:
1115 case K_LEFTMOUSE_NM:
1116 case K_LEFTRELEASE:
1117 case K_LEFTRELEASE_NM:
1118 case K_MIDDLEMOUSE:
1119 case K_MIDDLERELEASE:
1120 case K_RIGHTMOUSE:
1121 case K_RIGHTRELEASE:
1122 case K_X1MOUSE:
1123 case K_X1RELEASE:
1124 case K_X2MOUSE:
1125 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001126
1127 case K_MOUSEUP:
1128 case K_MOUSEDOWN:
1129 case K_MOUSELEFT:
1130 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001131 if (mouse_row < W_WINROW(curwin)
1132 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1133 || mouse_col < W_WINCOL(curwin)
1134 || mouse_col >= W_ENDCOL(curwin)
1135 || dragging_outside)
1136 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001137 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001138 if (typed)
1139 {
1140 stuffcharReadbuff(c);
1141 mouse_was_outside = TRUE;
1142 }
1143 return FAIL;
1144 }
1145 }
1146 if (typed)
1147 mouse_was_outside = FALSE;
1148
1149 /* Convert the typed key to a sequence of bytes for the job. */
1150 len = term_convert_key(term, c, msg);
1151 if (len > 0)
1152 /* TODO: if FAIL is returned, stop? */
1153 channel_send(term->tl_job->jv_channel, PART_IN,
1154 (char_u *)msg, (int)len, NULL);
1155
1156 return OK;
1157}
1158
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001159 static void
1160position_cursor(win_T *wp, VTermPos *pos)
1161{
1162 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1163 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1164 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1165}
1166
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001167/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001168 * Handle CTRL-W "": send register contents to the job.
1169 */
1170 static void
1171term_paste_register(int prev_c UNUSED)
1172{
1173 int c;
1174 list_T *l;
1175 listitem_T *item;
1176 long reglen = 0;
1177 int type;
1178
1179#ifdef FEAT_CMDL_INFO
1180 if (add_to_showcmd(prev_c))
1181 if (add_to_showcmd('"'))
1182 out_flush();
1183#endif
1184 c = term_vgetc();
1185#ifdef FEAT_CMDL_INFO
1186 clear_showcmd();
1187#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001188 if (!term_use_loop())
1189 /* job finished while waiting for a character */
1190 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001191
1192 /* CTRL-W "= prompt for expression to evaluate. */
1193 if (c == '=' && get_expr_register() != '=')
1194 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001195 if (!term_use_loop())
1196 /* job finished while waiting for a character */
1197 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001198
1199 l = (list_T *)get_reg_contents(c, GREG_LIST);
1200 if (l != NULL)
1201 {
1202 type = get_reg_type(c, &reglen);
1203 for (item = l->lv_first; item != NULL; item = item->li_next)
1204 {
1205 char_u *s = get_tv_string(&item->li_tv);
1206
1207 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1208 s, STRLEN(s), NULL);
1209 if (item->li_next != NULL || type == MLINE)
1210 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1211 (char_u *)"\r", 1, NULL);
1212 }
1213 list_free(l);
1214 }
1215}
1216
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001217#if defined(FEAT_GUI) || defined(PROTO)
1218/*
1219 * Return TRUE when the cursor of the terminal should be displayed.
1220 */
1221 int
1222use_terminal_cursor()
1223{
1224 return in_terminal_loop != NULL;
1225}
1226
1227 cursorentry_T *
1228term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1229{
1230 term_T *term = in_terminal_loop;
1231 static cursorentry_T entry;
1232
1233 vim_memset(&entry, 0, sizeof(entry));
1234 entry.shape = entry.mshape =
1235 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1236 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1237 SHAPE_BLOCK;
1238 entry.percentage = 20;
1239 if (term->tl_cursor_blink)
1240 {
1241 entry.blinkwait = 700;
1242 entry.blinkon = 400;
1243 entry.blinkon = 250;
1244 }
1245 *fg = gui.back_pixel;
1246 if (term->tl_cursor_color == NULL)
1247 *bg = gui.norm_pixel;
1248 else
1249 *bg = color_name2handle(term->tl_cursor_color);
1250 entry.name = "n";
1251 entry.used_for = SHAPE_CURSOR;
1252
1253 return &entry;
1254}
1255#endif
1256
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001257static int did_change_cursor = FALSE;
1258
1259 static void
1260may_set_cursor_props(term_T *term)
1261{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001262#ifdef FEAT_GUI
1263 /* For the GUI the cursor properties are obtained with
1264 * term_get_cursor_shape(). */
1265 if (gui.in_use)
1266 return;
1267#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001268 if (in_terminal_loop == term)
1269 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001270 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001271 if (term->tl_cursor_color != NULL)
1272 term_cursor_color(term->tl_cursor_color);
1273 else
1274 term_cursor_color((char_u *)"");
1275 /* do both blink and shape+blink, in case setting shape does not work */
1276 term_cursor_blink(term->tl_cursor_blink);
1277 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1278 }
1279}
1280
1281 static void
1282may_restore_cursor_props(void)
1283{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001284#ifdef FEAT_GUI
1285 if (gui.in_use)
1286 return;
1287#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001288 if (did_change_cursor)
1289 {
1290 did_change_cursor = FALSE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001291 term_cursor_color((char_u *)"");
1292 term_cursor_blink(FALSE);
Bram Moolenaar3eee06e2017-08-19 19:40:50 +02001293 /* this will restore the initial cursor style, if possible */
1294 ui_cursor_shape_forced(TRUE);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001295 }
1296}
1297
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001298/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001299 * Returns TRUE if the current window contains a terminal and we are sending
1300 * keys to the job.
1301 */
1302 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001303term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001304{
1305 term_T *term = curbuf->b_term;
1306
1307 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001308 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001309 && term->tl_vterm != NULL
1310 && term_job_running(term);
1311}
1312
1313/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001314 * Wait for input and send it to the job.
1315 * Return when the start of a CTRL-W command is typed or anything else that
1316 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001317 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1318 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001319 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001320 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001321terminal_loop(void)
1322{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001323 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001324 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001325 int ret;
1326
Bram Moolenaar679653e2017-08-13 14:13:19 +02001327 /* Remember the terminal we are sending keys to. However, the terminal
1328 * might be closed while waiting for a character, e.g. typing "exit" in a
1329 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1330 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001331 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001332
1333 if (*curwin->w_p_tk != NUL)
1334 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001335 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001336 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001337
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001338#ifdef UNIX
1339 {
1340 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1341
1342 if (isatty(fd))
1343 {
1344 ttyinfo_T info;
1345
1346 /* Get the current backspace and enter characters of the pty. */
1347 if (get_tty_info(fd, &info) == OK)
1348 {
1349 term_backspace_char = info.backspace;
1350 term_enter_char = info.enter;
1351 term_nl_does_cr = info.nl_does_cr;
1352 }
1353 }
1354 }
1355#endif
1356
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001357 for (;;)
1358 {
1359 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001360 /* Repeat redrawing in case a message is received while redrawing. */
1361 while (curwin->w_redr_type != 0)
1362 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001363 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001364
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001365 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001366 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001367 /* job finished while waiting for a character */
1368 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001369 if (c == K_IGNORE)
1370 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001371
Bram Moolenaarfae42832017-08-01 22:24:26 +02001372#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001373 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001374 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001375 if (ctrl_break_was_pressed)
1376 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001377#endif
1378
Bram Moolenaar69198192017-08-05 14:10:48 +02001379 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001380 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001381 int prev_c = c;
1382
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001383#ifdef FEAT_CMDL_INFO
1384 if (add_to_showcmd(c))
1385 out_flush();
1386#endif
1387 c = term_vgetc();
1388#ifdef FEAT_CMDL_INFO
1389 clear_showcmd();
1390#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001391 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001392 /* job finished while waiting for a character */
1393 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001394
Bram Moolenaar69198192017-08-05 14:10:48 +02001395 if (prev_c == Ctrl_BSL)
1396 {
1397 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001398 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001399 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1400 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001401 ret = FAIL;
1402 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001403 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001404 /* Send both keys to the terminal. */
1405 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1406 }
Bram Moolenaar8e539c52017-08-18 22:57:06 +02001407 else if (c == Ctrl_C)
1408 {
1409 /* "CTRL-W CTRL-C" or 'termkey' CTRL-C: end the job */
1410 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
1411 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001412 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001413 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001414 /* "CTRL-W .": send CTRL-W to the job */
1415 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001416 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001417 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001418 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001419 /* CTRL-W N : go to Terminal-Normal mode. */
1420 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001421 ret = FAIL;
1422 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001423 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001424 else if (c == '"')
1425 {
1426 term_paste_register(prev_c);
1427 continue;
1428 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001429 else if (termkey == 0 || c != termkey)
1430 {
1431 stuffcharReadbuff(Ctrl_W);
1432 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001433 ret = OK;
1434 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001435 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001436 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001437 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001438 {
1439 ret = OK;
1440 goto theend;
1441 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001442 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001443 ret = FAIL;
1444
1445theend:
1446 in_terminal_loop = NULL;
1447 may_restore_cursor_props();
1448 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001449}
1450
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001451/*
1452 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001453 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001454 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001455 */
1456 void
1457term_job_ended(job_T *job)
1458{
1459 term_T *term;
1460 int did_one = FALSE;
1461
1462 for (term = first_term; term != NULL; term = term->tl_next)
1463 if (term->tl_job == job)
1464 {
1465 vim_free(term->tl_title);
1466 term->tl_title = NULL;
1467 vim_free(term->tl_status_text);
1468 term->tl_status_text = NULL;
1469 redraw_buf_and_status_later(term->tl_buffer, VALID);
1470 did_one = TRUE;
1471 }
1472 if (did_one)
1473 redraw_statuslines();
1474 if (curbuf->b_term != NULL)
1475 {
1476 if (curbuf->b_term->tl_job == job)
1477 maketitle();
1478 update_cursor(curbuf->b_term, TRUE);
1479 }
1480}
1481
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001482 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001483may_toggle_cursor(term_T *term)
1484{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001485 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001486 {
1487 if (term->tl_cursor_visible)
1488 cursor_on();
1489 else
1490 cursor_off();
1491 }
1492}
1493
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001494/*
1495 * Reverse engineer the RGB value into a cterm color index.
1496 * First color is 1. Return 0 if no match found.
1497 */
1498 static int
1499color2index(VTermColor *color, int fg, int *boldp)
1500{
1501 int red = color->red;
1502 int blue = color->blue;
1503 int green = color->green;
1504
1505 /* The argument for lookup_color() is for the color_names[] table. */
1506 if (red == 0)
1507 {
1508 if (green == 0)
1509 {
1510 if (blue == 0)
1511 return lookup_color(0, fg, boldp) + 1; /* black */
1512 if (blue == 224)
1513 return lookup_color(1, fg, boldp) + 1; /* dark blue */
1514 }
1515 else if (green == 224)
1516 {
1517 if (blue == 0)
1518 return lookup_color(2, fg, boldp) + 1; /* dark green */
1519 if (blue == 224)
1520 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
1521 }
1522 }
1523 else if (red == 224)
1524 {
1525 if (green == 0)
1526 {
1527 if (blue == 0)
1528 return lookup_color(4, fg, boldp) + 1; /* dark red */
1529 if (blue == 224)
1530 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
1531 }
1532 else if (green == 224)
1533 {
1534 if (blue == 0)
1535 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
1536 if (blue == 224)
1537 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
1538 }
1539 }
1540 else if (red == 128)
1541 {
1542 if (green == 128 && blue == 128)
1543 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
1544 }
1545 else if (red == 255)
1546 {
1547 if (green == 64)
1548 {
1549 if (blue == 64)
1550 return lookup_color(20, fg, boldp) + 1; /* light red */
1551 if (blue == 255)
1552 return lookup_color(22, fg, boldp) + 1; /* light magenta */
1553 }
1554 else if (green == 255)
1555 {
1556 if (blue == 64)
1557 return lookup_color(24, fg, boldp) + 1; /* yellow */
1558 if (blue == 255)
1559 return lookup_color(26, fg, boldp) + 1; /* white */
1560 }
1561 }
1562 else if (red == 64)
1563 {
1564 if (green == 64)
1565 {
1566 if (blue == 255)
1567 return lookup_color(14, fg, boldp) + 1; /* light blue */
1568 }
1569 else if (green == 255)
1570 {
1571 if (blue == 64)
1572 return lookup_color(16, fg, boldp) + 1; /* light green */
1573 if (blue == 255)
1574 return lookup_color(18, fg, boldp) + 1; /* light cyan */
1575 }
1576 }
1577 if (t_colors >= 256)
1578 {
1579 if (red == blue && red == green)
1580 {
1581 /* 24-color greyscale */
1582 static int cutoff[23] = {
1583 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1584 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1585 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1586 int i;
1587
1588 for (i = 0; i < 23; ++i)
1589 if (red < cutoff[i])
1590 return i + 233;
1591 return 256;
1592 }
1593
1594 /* 216-color cube */
1595 return 17 + ((red + 25) / 0x33) * 36
1596 + ((green + 25) / 0x33) * 6
1597 + (blue + 25) / 0x33;
1598 }
1599 return 0;
1600}
1601
1602/*
1603 * Convert the attributes of a vterm cell into an attribute index.
1604 */
1605 static int
1606cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
1607{
1608 int attr = 0;
1609
1610 if (cellattrs.bold)
1611 attr |= HL_BOLD;
1612 if (cellattrs.underline)
1613 attr |= HL_UNDERLINE;
1614 if (cellattrs.italic)
1615 attr |= HL_ITALIC;
1616 if (cellattrs.strike)
1617 attr |= HL_STANDOUT;
1618 if (cellattrs.reverse)
1619 attr |= HL_INVERSE;
1620
1621#ifdef FEAT_GUI
1622 if (gui.in_use)
1623 {
1624 guicolor_T fg, bg;
1625
1626 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1627 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
1628 return get_gui_attr_idx(attr, fg, bg);
1629 }
1630 else
1631#endif
1632#ifdef FEAT_TERMGUICOLORS
1633 if (p_tgc)
1634 {
1635 guicolor_T fg, bg;
1636
1637 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1638 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
1639
1640 return get_tgc_attr_idx(attr, fg, bg);
1641 }
1642 else
1643#endif
1644 {
1645 int bold = MAYBE;
1646 int fg = color2index(&cellfg, TRUE, &bold);
1647 int bg = color2index(&cellbg, FALSE, &bold);
1648
1649 /* with 8 colors set the bold attribute to get a bright foreground */
1650 if (bold == TRUE)
1651 attr |= HL_BOLD;
1652 return get_cterm_attr_idx(attr, fg, bg);
1653 }
1654 return 0;
1655}
1656
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001657 static int
1658handle_damage(VTermRect rect, void *user)
1659{
1660 term_T *term = (term_T *)user;
1661
1662 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1663 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1664 redraw_buf_later(term->tl_buffer, NOT_VALID);
1665 return 1;
1666}
1667
1668 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001669handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001670{
1671 term_T *term = (term_T *)user;
1672
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001673 /* Scrolling up is done much more efficiently by deleting lines instead of
1674 * redrawing the text. */
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001675 if (dest.start_col == src.start_col
1676 && dest.end_col == src.end_col
1677 && dest.start_row < src.start_row)
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001678 {
1679 win_T *wp;
1680 VTermColor fg, bg;
1681 VTermScreenCellAttrs attr;
1682 int clear_attr;
1683
1684 /* Set the color to clear lines with. */
1685 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1686 &fg, &bg);
1687 vim_memset(&attr, 0, sizeof(attr));
1688 clear_attr = cell2attr(attr, fg, bg);
1689
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001690 FOR_ALL_WINDOWS(wp)
1691 {
1692 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001693 win_del_lines(wp, dest.start_row,
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001694 src.start_row - dest.start_row, FALSE, FALSE,
1695 clear_attr);
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001696 }
Bram Moolenaarcfce7172017-08-17 20:31:48 +02001697 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001698 redraw_buf_later(term->tl_buffer, NOT_VALID);
1699 return 1;
1700}
1701
1702 static int
1703handle_movecursor(
1704 VTermPos pos,
1705 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001706 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001707 void *user)
1708{
1709 term_T *term = (term_T *)user;
1710 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001711
1712 term->tl_cursor_pos = pos;
1713 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001714
1715 FOR_ALL_WINDOWS(wp)
1716 {
1717 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001718 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001719 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001720 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001721 {
1722 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001723 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001724 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001725
1726 return 1;
1727}
1728
Bram Moolenaar21554412017-07-24 21:44:43 +02001729 static int
1730handle_settermprop(
1731 VTermProp prop,
1732 VTermValue *value,
1733 void *user)
1734{
1735 term_T *term = (term_T *)user;
1736
1737 switch (prop)
1738 {
1739 case VTERM_PROP_TITLE:
1740 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001741 /* a blank title isn't useful, make it empty, so that "running" is
1742 * displayed */
1743 if (*skipwhite((char_u *)value->string) == NUL)
1744 term->tl_title = NULL;
1745 else
1746 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001747 vim_free(term->tl_status_text);
1748 term->tl_status_text = NULL;
1749 if (term == curbuf->b_term)
1750 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001751 break;
1752
1753 case VTERM_PROP_CURSORVISIBLE:
1754 term->tl_cursor_visible = value->boolean;
1755 may_toggle_cursor(term);
1756 out_flush();
1757 break;
1758
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001759 case VTERM_PROP_CURSORBLINK:
1760 term->tl_cursor_blink = value->boolean;
1761 may_set_cursor_props(term);
1762 break;
1763
1764 case VTERM_PROP_CURSORSHAPE:
1765 term->tl_cursor_shape = value->number;
1766 may_set_cursor_props(term);
1767 break;
1768
1769 case VTERM_PROP_CURSORCOLOR:
1770 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001771 if (*value->string == NUL)
1772 term->tl_cursor_color = NULL;
1773 else
1774 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001775 may_set_cursor_props(term);
1776 break;
1777
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001778 case VTERM_PROP_ALTSCREEN:
1779 /* TODO: do anything else? */
1780 term->tl_using_altscreen = value->boolean;
1781 break;
1782
Bram Moolenaar21554412017-07-24 21:44:43 +02001783 default:
1784 break;
1785 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001786 /* Always return 1, otherwise vterm doesn't store the value internally. */
1787 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001788}
1789
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001790/*
1791 * The job running in the terminal resized the terminal.
1792 */
1793 static int
1794handle_resize(int rows, int cols, void *user)
1795{
1796 term_T *term = (term_T *)user;
1797 win_T *wp;
1798
1799 term->tl_rows = rows;
1800 term->tl_cols = cols;
1801 FOR_ALL_WINDOWS(wp)
1802 {
1803 if (wp->w_buffer == term->tl_buffer)
1804 {
1805 win_setheight_win(rows, wp);
1806 win_setwidth_win(cols, wp);
1807 }
1808 }
1809
1810 redraw_buf_later(term->tl_buffer, NOT_VALID);
1811 return 1;
1812}
1813
Bram Moolenaard85f2712017-07-28 21:51:57 +02001814/*
1815 * Handle a line that is pushed off the top of the screen.
1816 */
1817 static int
1818handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1819{
1820 term_T *term = (term_T *)user;
1821
1822 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001823 if (ga_grow(&term->tl_scrollback, 1) == OK)
1824 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001825 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001826 int len = 0;
1827 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001828 int c;
1829 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001830 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001831 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001832
1833 /* do not store empty cells at the end */
1834 for (i = 0; i < cols; ++i)
1835 if (cells[i].chars[0] != 0)
1836 len = i + 1;
1837
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001838 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001839 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001840 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001841 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001842 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001843 for (col = 0; col < len; col += cells[col].width)
1844 {
1845 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1846 {
1847 ga.ga_len = 0;
1848 break;
1849 }
1850 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1851 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1852 (char_u *)ga.ga_data + ga.ga_len);
1853 p[col].width = cells[col].width;
1854 p[col].attrs = cells[col].attrs;
1855 p[col].fg = cells[col].fg;
1856 p[col].bg = cells[col].bg;
1857 }
1858 }
1859 if (ga_grow(&ga, 1) == FAIL)
1860 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1861 else
1862 {
1863 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1864 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1865 }
1866 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001867
1868 line = (sb_line_T *)term->tl_scrollback.ga_data
1869 + term->tl_scrollback.ga_len;
1870 line->sb_cols = len;
1871 line->sb_cells = p;
1872 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001873 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001874 }
1875 return 0; /* ignored */
1876}
1877
Bram Moolenaar21554412017-07-24 21:44:43 +02001878static VTermScreenCallbacks screen_callbacks = {
1879 handle_damage, /* damage */
1880 handle_moverect, /* moverect */
1881 handle_movecursor, /* movecursor */
1882 handle_settermprop, /* settermprop */
1883 NULL, /* bell */
1884 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001885 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001886 NULL /* sb_popline */
1887};
1888
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001889/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001890 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001891 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001892 */
1893 void
1894term_channel_closed(channel_T *ch)
1895{
1896 term_T *term;
1897 int did_one = FALSE;
1898
1899 for (term = first_term; term != NULL; term = term->tl_next)
1900 if (term->tl_job == ch->ch_job)
1901 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001902 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001903 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001904
Bram Moolenaard85f2712017-07-28 21:51:57 +02001905 vim_free(term->tl_title);
1906 term->tl_title = NULL;
1907 vim_free(term->tl_status_text);
1908 term->tl_status_text = NULL;
1909
Bram Moolenaar423802d2017-07-30 16:52:24 +02001910 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001911 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001912 {
1913 int fnum = term->tl_buffer->b_fnum;
1914
Bram Moolenaar423802d2017-07-30 16:52:24 +02001915 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001916
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001917 if (term->tl_finish == 'c')
1918 {
1919 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001920 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001921 curbuf = term->tl_buffer;
1922 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1923 break;
1924 }
1925 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1926 {
1927 char buf[50];
1928
1929 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001930 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001931 vim_snprintf(buf, sizeof(buf),
1932 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001933 ? "botright sbuf %d"
1934 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001935 do_cmdline_cmd((char_u *)buf);
1936 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001937 else
1938 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001939 }
1940
Bram Moolenaard85f2712017-07-28 21:51:57 +02001941 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001942 }
1943 if (did_one)
1944 {
1945 redraw_statuslines();
1946
1947 /* Need to break out of vgetc(). */
1948 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001949 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001950
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001951 term = curbuf->b_term;
1952 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001954 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001955 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001956 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001957 }
1958 }
1959}
1960
1961/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001962 * Called to update a window that contains an active terminal.
1963 * Returns FAIL when there is no terminal running in this window or in
1964 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001965 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001966 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001967term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001968{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001969 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001970 VTerm *vterm;
1971 VTermScreen *screen;
1972 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001973 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001974
Bram Moolenaar6d819742017-08-06 14:57:49 +02001975 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001976 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001977
Bram Moolenaard85f2712017-07-28 21:51:57 +02001978 vterm = term->tl_vterm;
1979 screen = vterm_obtain_screen(vterm);
1980 state = vterm_obtain_state(vterm);
1981
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001982 /*
1983 * If the window was resized a redraw will be triggered and we get here.
1984 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1985 */
1986 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1987 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001988 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001989 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1990 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1991 win_T *twp;
1992
1993 FOR_ALL_WINDOWS(twp)
1994 {
1995 /* When more than one window shows the same terminal, use the
1996 * smallest size. */
1997 if (twp->w_buffer == term->tl_buffer)
1998 {
1999 if (!term->tl_rows_fixed && rows > twp->w_height)
2000 rows = twp->w_height;
2001 if (!term->tl_cols_fixed && cols > twp->w_width)
2002 cols = twp->w_width;
2003 }
2004 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002005
2006 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02002007 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002008 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002009 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02002010 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02002011
2012 /* The cursor may have been moved when resizing. */
2013 vterm_state_get_cursorpos(state, &pos);
2014 position_cursor(wp, &pos);
2015
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002016 /* TODO: Only redraw what changed. */
2017 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02002018 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002019 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002020 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002021
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002022 if (pos.row < term->tl_rows)
2023 {
2024 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002025 {
2026 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002027 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002028
Bram Moolenaareeac6772017-07-23 15:48:37 +02002029 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2030 vim_memset(&cell, 0, sizeof(cell));
2031
2032 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002033 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002034 if (c == NUL)
2035 {
2036 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002037#if defined(FEAT_MBYTE)
2038 if (enc_utf8)
2039 ScreenLinesUC[off] = NUL;
2040#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002041 }
2042 else
2043 {
2044#if defined(FEAT_MBYTE)
2045 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002046 {
2047 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002049 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002050 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002051 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002052 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002053 if (enc_utf8)
2054 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002055 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002056#else
2057 ScreenLines[off] = c;
2058#endif
2059 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002060 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002061
2062 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002063 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002064 if (cell.width == 2)
2065 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002066 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002067#if defined(FEAT_MBYTE)
2068 if (enc_utf8)
2069 ScreenLinesUC[off] = NUL;
2070#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002071 ++pos.col;
2072 ++off;
2073 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002074 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002075 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002076 else
2077 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002078
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002079 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2080 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002081 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002082
2083 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002084}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002085
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002086/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002087 * Return TRUE if "wp" is a terminal window where the job has finished.
2088 */
2089 int
2090term_is_finished(buf_T *buf)
2091{
2092 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2093}
2094
2095/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002096 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002097 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002098 */
2099 int
2100term_show_buffer(buf_T *buf)
2101{
2102 term_T *term = buf->b_term;
2103
Bram Moolenaar6d819742017-08-06 14:57:49 +02002104 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002105}
2106
2107/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002108 * The current buffer is going to be changed. If there is terminal
2109 * highlighting remove it now.
2110 */
2111 void
2112term_change_in_curbuf(void)
2113{
2114 term_T *term = curbuf->b_term;
2115
2116 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2117 {
2118 free_scrollback(term);
2119 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002120
2121 /* The buffer is now like a normal buffer, it cannot be easily
2122 * abandoned when changed. */
2123 set_string_option_direct((char_u *)"buftype", -1,
2124 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002125 }
2126}
2127
2128/*
2129 * Get the screen attribute for a position in the buffer.
2130 */
2131 int
2132term_get_attr(buf_T *buf, linenr_T lnum, int col)
2133{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002134 term_T *term = buf->b_term;
2135 sb_line_T *line;
2136 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002137
Bram Moolenaar70229f92017-07-29 16:01:53 +02002138 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002139 return 0;
2140 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2141 if (col >= line->sb_cols)
2142 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002143 cellattr = line->sb_cells + col;
2144 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002145}
2146
2147/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002148 * Create a new vterm and initialize it.
2149 */
2150 static void
2151create_vterm(term_T *term, int rows, int cols)
2152{
2153 VTerm *vterm;
2154 VTermScreen *screen;
2155
2156 vterm = vterm_new(rows, cols);
2157 term->tl_vterm = vterm;
2158 screen = vterm_obtain_screen(vterm);
2159 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2160 /* TODO: depends on 'encoding'. */
2161 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002162
2163 /* Vterm uses a default black background. Set it to white when
2164 * 'background' is "light". */
2165 if (*p_bg == 'l')
2166 {
2167 VTermColor fg, bg;
2168
2169 fg.red = fg.green = fg.blue = 0;
2170 bg.red = bg.green = bg.blue = 255;
2171 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2172 }
2173
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002174 /* Required to initialize most things. */
2175 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002176
2177 /* Allow using alternate screen. */
2178 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002179}
2180
Bram Moolenaar21554412017-07-24 21:44:43 +02002181/*
2182 * Return the text to show for the buffer name and status.
2183 */
2184 char_u *
2185term_get_status_text(term_T *term)
2186{
2187 if (term->tl_status_text == NULL)
2188 {
2189 char_u *txt;
2190 size_t len;
2191
Bram Moolenaar6d819742017-08-06 14:57:49 +02002192 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002193 {
2194 if (term_job_running(term))
2195 txt = (char_u *)_("Terminal");
2196 else
2197 txt = (char_u *)_("Terminal-finished");
2198 }
2199 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002200 txt = term->tl_title;
2201 else if (term_job_running(term))
2202 txt = (char_u *)_("running");
2203 else
2204 txt = (char_u *)_("finished");
2205 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002206 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002207 if (term->tl_status_text != NULL)
2208 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2209 term->tl_buffer->b_fname, txt);
2210 }
2211 return term->tl_status_text;
2212}
2213
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002214/*
2215 * Mark references in jobs of terminals.
2216 */
2217 int
2218set_ref_in_term(int copyID)
2219{
2220 int abort = FALSE;
2221 term_T *term;
2222 typval_T tv;
2223
2224 for (term = first_term; term != NULL; term = term->tl_next)
2225 if (term->tl_job != NULL)
2226 {
2227 tv.v_type = VAR_JOB;
2228 tv.vval.v_job = term->tl_job;
2229 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2230 }
2231 return abort;
2232}
2233
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002234/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002235 * Get the buffer from the first argument in "argvars".
2236 * Returns NULL when the buffer is not for a terminal window.
2237 */
2238 static buf_T *
2239term_get_buf(typval_T *argvars)
2240{
2241 buf_T *buf;
2242
2243 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2244 ++emsg_off;
2245 buf = get_buf_tv(&argvars[0], FALSE);
2246 --emsg_off;
2247 if (buf == NULL || buf->b_term == NULL)
2248 return NULL;
2249 return buf;
2250}
2251
2252/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002253 * "term_getaltscreen(buf)" function
2254 */
2255 void
2256f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2257{
2258 buf_T *buf = term_get_buf(argvars);
2259
2260 if (buf == NULL)
2261 return;
2262 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2263}
2264
2265/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002266 * "term_getattr(attr, name)" function
2267 */
2268 void
2269f_term_getattr(typval_T *argvars, typval_T *rettv)
2270{
2271 int attr;
2272 size_t i;
2273 char_u *name;
2274
2275 static struct {
2276 char *name;
2277 int attr;
2278 } attrs[] = {
2279 {"bold", HL_BOLD},
2280 {"italic", HL_ITALIC},
2281 {"underline", HL_UNDERLINE},
2282 {"strike", HL_STANDOUT},
2283 {"reverse", HL_INVERSE},
2284 };
2285
2286 attr = get_tv_number(&argvars[0]);
2287 name = get_tv_string_chk(&argvars[1]);
2288 if (name == NULL)
2289 return;
2290
2291 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2292 if (STRCMP(name, attrs[i].name) == 0)
2293 {
2294 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2295 break;
2296 }
2297}
2298
2299/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002300 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002301 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002302 void
2303f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002304{
Bram Moolenaar97870002017-07-30 18:28:38 +02002305 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002306 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002307 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002308 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002309
Bram Moolenaar97870002017-07-30 18:28:38 +02002310 if (rettv_list_alloc(rettv) == FAIL)
2311 return;
2312 if (buf == NULL)
2313 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002314 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002315
2316 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002317 list_append_number(l, term->tl_cursor_pos.row + 1);
2318 list_append_number(l, term->tl_cursor_pos.col + 1);
2319
2320 d = dict_alloc();
2321 if (d != NULL)
2322 {
2323 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2324 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2325 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2326 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2327 ? (char_u *)"" : term->tl_cursor_color);
2328 list_append_dict(l, d);
2329 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002330}
2331
2332/*
2333 * "term_getjob(buf)" function
2334 */
2335 void
2336f_term_getjob(typval_T *argvars, typval_T *rettv)
2337{
2338 buf_T *buf = term_get_buf(argvars);
2339
2340 rettv->v_type = VAR_JOB;
2341 rettv->vval.v_job = NULL;
2342 if (buf == NULL)
2343 return;
2344
2345 rettv->vval.v_job = buf->b_term->tl_job;
2346 if (rettv->vval.v_job != NULL)
2347 ++rettv->vval.v_job->jv_refcount;
2348}
2349
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002350 static int
2351get_row_number(typval_T *tv, term_T *term)
2352{
2353 if (tv->v_type == VAR_STRING
2354 && tv->vval.v_string != NULL
2355 && STRCMP(tv->vval.v_string, ".") == 0)
2356 return term->tl_cursor_pos.row;
2357 return (int)get_tv_number(tv) - 1;
2358}
2359
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002360/*
2361 * "term_getline(buf, row)" function
2362 */
2363 void
2364f_term_getline(typval_T *argvars, typval_T *rettv)
2365{
2366 buf_T *buf = term_get_buf(argvars);
2367 term_T *term;
2368 int row;
2369
2370 rettv->v_type = VAR_STRING;
2371 if (buf == NULL)
2372 return;
2373 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002374 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002375
2376 if (term->tl_vterm == NULL)
2377 {
2378 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2379
2380 /* vterm is finished, get the text from the buffer */
2381 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2382 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2383 }
2384 else
2385 {
2386 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2387 VTermRect rect;
2388 int len;
2389 char_u *p;
2390
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002391 if (row < 0 || row >= term->tl_rows)
2392 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002393 len = term->tl_cols * MB_MAXBYTES + 1;
2394 p = alloc(len);
2395 if (p == NULL)
2396 return;
2397 rettv->vval.v_string = p;
2398
2399 rect.start_col = 0;
2400 rect.end_col = term->tl_cols;
2401 rect.start_row = row;
2402 rect.end_row = row + 1;
2403 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2404 }
2405}
2406
2407/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002408 * "term_getscrolled(buf)" function
2409 */
2410 void
2411f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2412{
2413 buf_T *buf = term_get_buf(argvars);
2414
2415 if (buf == NULL)
2416 return;
2417 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2418}
2419
2420/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002421 * "term_getsize(buf)" function
2422 */
2423 void
2424f_term_getsize(typval_T *argvars, typval_T *rettv)
2425{
2426 buf_T *buf = term_get_buf(argvars);
2427 list_T *l;
2428
2429 if (rettv_list_alloc(rettv) == FAIL)
2430 return;
2431 if (buf == NULL)
2432 return;
2433
2434 l = rettv->vval.v_list;
2435 list_append_number(l, buf->b_term->tl_rows);
2436 list_append_number(l, buf->b_term->tl_cols);
2437}
2438
2439/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002440 * "term_getstatus(buf)" function
2441 */
2442 void
2443f_term_getstatus(typval_T *argvars, typval_T *rettv)
2444{
2445 buf_T *buf = term_get_buf(argvars);
2446 term_T *term;
2447 char_u val[100];
2448
2449 rettv->v_type = VAR_STRING;
2450 if (buf == NULL)
2451 return;
2452 term = buf->b_term;
2453
2454 if (term_job_running(term))
2455 STRCPY(val, "running");
2456 else
2457 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002458 if (term->tl_normal_mode)
2459 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002460 rettv->vval.v_string = vim_strsave(val);
2461}
2462
2463/*
2464 * "term_gettitle(buf)" function
2465 */
2466 void
2467f_term_gettitle(typval_T *argvars, typval_T *rettv)
2468{
2469 buf_T *buf = term_get_buf(argvars);
2470
2471 rettv->v_type = VAR_STRING;
2472 if (buf == NULL)
2473 return;
2474
2475 if (buf->b_term->tl_title != NULL)
2476 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2477}
2478
2479/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002480 * "term_gettty(buf)" function
2481 */
2482 void
2483f_term_gettty(typval_T *argvars, typval_T *rettv)
2484{
2485 buf_T *buf = term_get_buf(argvars);
2486 char_u *p;
2487
2488 rettv->v_type = VAR_STRING;
2489 if (buf == NULL)
2490 return;
2491 if (buf->b_term->tl_job != NULL)
2492 p = buf->b_term->tl_job->jv_tty_name;
2493 else
2494 p = buf->b_term->tl_tty_name;
2495 if (p != NULL)
2496 rettv->vval.v_string = vim_strsave(p);
2497}
2498
2499/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002500 * "term_list()" function
2501 */
2502 void
2503f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2504{
2505 term_T *tp;
2506 list_T *l;
2507
2508 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2509 return;
2510
2511 l = rettv->vval.v_list;
2512 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2513 if (tp != NULL && tp->tl_buffer != NULL)
2514 if (list_append_number(l,
2515 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2516 return;
2517}
2518
2519/*
2520 * "term_scrape(buf, row)" function
2521 */
2522 void
2523f_term_scrape(typval_T *argvars, typval_T *rettv)
2524{
2525 buf_T *buf = term_get_buf(argvars);
2526 VTermScreen *screen = NULL;
2527 VTermPos pos;
2528 list_T *l;
2529 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002530 char_u *p;
2531 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002532
2533 if (rettv_list_alloc(rettv) == FAIL)
2534 return;
2535 if (buf == NULL)
2536 return;
2537 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002538
2539 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002540 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002541
2542 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002543 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002544 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002545 p = NULL;
2546 line = NULL;
2547 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002548 else
2549 {
2550 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2551
2552 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2553 return;
2554 p = ml_get_buf(buf, lnum + 1, FALSE);
2555 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2556 }
2557
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002558 for (pos.col = 0; pos.col < term->tl_cols; )
2559 {
2560 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002561 int width;
2562 VTermScreenCellAttrs attrs;
2563 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002564 char_u rgb[8];
2565 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2566 int off = 0;
2567 int i;
2568
2569 if (screen == NULL)
2570 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002571 cellattr_T *cellattr;
2572 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002573
2574 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002575 if (pos.col >= line->sb_cols)
2576 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002577 cellattr = line->sb_cells + pos.col;
2578 width = cellattr->width;
2579 attrs = cellattr->attrs;
2580 fg = cellattr->fg;
2581 bg = cellattr->bg;
2582 len = MB_PTR2LEN(p);
2583 mch_memmove(mbs, p, len);
2584 mbs[len] = NUL;
2585 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002586 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002587 else
2588 {
2589 VTermScreenCell cell;
2590 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2591 break;
2592 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2593 {
2594 if (cell.chars[i] == 0)
2595 break;
2596 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2597 }
2598 mbs[off] = NUL;
2599 width = cell.width;
2600 attrs = cell.attrs;
2601 fg = cell.fg;
2602 bg = cell.bg;
2603 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002604 dcell = dict_alloc();
2605 list_append_dict(l, dcell);
2606
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002607 dict_add_nr_str(dcell, "chars", 0, mbs);
2608
2609 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002610 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002611 dict_add_nr_str(dcell, "fg", 0, rgb);
2612 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002613 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002614 dict_add_nr_str(dcell, "bg", 0, rgb);
2615
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002616 dict_add_nr_str(dcell, "attr",
2617 cell2attr(attrs, fg, bg), NULL);
2618 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002619
2620 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002621 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002622 ++pos.col;
2623 }
2624}
2625
2626/*
2627 * "term_sendkeys(buf, keys)" function
2628 */
2629 void
2630f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2631{
2632 buf_T *buf = term_get_buf(argvars);
2633 char_u *msg;
2634 term_T *term;
2635
2636 rettv->v_type = VAR_UNKNOWN;
2637 if (buf == NULL)
2638 return;
2639
2640 msg = get_tv_string_chk(&argvars[1]);
2641 if (msg == NULL)
2642 return;
2643 term = buf->b_term;
2644 if (term->tl_vterm == NULL)
2645 return;
2646
2647 while (*msg != NUL)
2648 {
2649 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2650 msg += MB_PTR2LEN(msg);
2651 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002652}
2653
2654/*
2655 * "term_start(command, options)" function
2656 */
2657 void
2658f_term_start(typval_T *argvars, typval_T *rettv)
2659{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002660 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002661
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002662 init_job_options(&opt);
2663 /* TODO: allow more job options */
2664 if (argvars[1].v_type != VAR_UNKNOWN
2665 && get_job_options(&argvars[1], &opt,
2666 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002667 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002668 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002669 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002670 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002671 return;
2672
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002673 if (opt.jo_vertical)
2674 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002675 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002676
2677 if (curbuf->b_term != NULL)
2678 rettv->vval.v_number = curbuf->b_fnum;
2679}
2680
2681/*
2682 * "term_wait" function
2683 */
2684 void
2685f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2686{
2687 buf_T *buf = term_get_buf(argvars);
2688
2689 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002690 {
2691 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002692 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002693 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002694 if (buf->b_term->tl_job == NULL)
2695 {
2696 ch_log(NULL, "term_wait(): no job to wait for");
2697 return;
2698 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002699
2700 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002701 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002702 {
2703 /* The job is dead, keep reading channel I/O until the channel is
2704 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002705 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002706 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2707 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002708 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002709 parse_queued_messages();
2710 ui_delay(10L, FALSE);
2711 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002712 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002713 parse_queued_messages();
2714 }
2715 else
2716 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002717 long wait = 10L;
2718
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002719 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002720 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002721
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002722 /* Wait for some time for any channel I/O. */
2723 if (argvars[1].v_type != VAR_UNKNOWN)
2724 wait = get_tv_number(&argvars[1]);
2725 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002726 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002727
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002728 /* Flushing messages on channels is hopefully sufficient.
2729 * TODO: is there a better way? */
2730 parse_queued_messages();
2731 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002732}
2733
Bram Moolenaara83e3962017-08-17 14:39:07 +02002734# if defined(WIN3264) || defined(PROTO)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002735
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002736/**************************************
2737 * 2. MS-Windows implementation.
2738 */
2739
Bram Moolenaara83e3962017-08-17 14:39:07 +02002740# ifndef PROTO
2741
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002742#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2743#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2744
Bram Moolenaar8a773062017-07-24 22:29:21 +02002745void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002746void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002747void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002748BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2749void (*winpty_config_set_initial_size)(void*, int, int);
2750LPCWSTR (*winpty_conin_name)(void*);
2751LPCWSTR (*winpty_conout_name)(void*);
2752LPCWSTR (*winpty_conerr_name)(void*);
2753void (*winpty_free)(void*);
2754void (*winpty_config_free)(void*);
2755void (*winpty_spawn_config_free)(void*);
2756void (*winpty_error_free)(void*);
2757LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002758BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002759HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002760
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002761#define WINPTY_DLL "winpty.dll"
2762
2763static HINSTANCE hWinPtyDLL = NULL;
Bram Moolenaara83e3962017-08-17 14:39:07 +02002764# endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002765
Bram Moolenaara83e3962017-08-17 14:39:07 +02002766 static int
2767dyn_winpty_init(int verbose)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002768{
2769 int i;
2770 static struct
2771 {
2772 char *name;
2773 FARPROC *ptr;
2774 } winpty_entry[] =
2775 {
2776 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2777 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2778 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2779 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2780 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2781 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2782 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2783 {"winpty_free", (FARPROC*)&winpty_free},
2784 {"winpty_open", (FARPROC*)&winpty_open},
2785 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2786 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2787 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2788 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002789 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002790 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002791 {NULL, NULL}
2792 };
2793
2794 /* No need to initialize twice. */
2795 if (hWinPtyDLL)
Bram Moolenaara83e3962017-08-17 14:39:07 +02002796 return OK;
Bram Moolenaar9e13aa72017-08-16 23:14:08 +02002797 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
2798 * winpty.dll. */
2799 if (*p_winptydll != NUL)
2800 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
2801 if (!hWinPtyDLL)
2802 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002803 if (!hWinPtyDLL)
2804 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002805 if (verbose)
2806 EMSG2(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
2807 : (char_u *)WINPTY_DLL);
2808 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002809 }
2810 for (i = 0; winpty_entry[i].name != NULL
2811 && winpty_entry[i].ptr != NULL; ++i)
2812 {
2813 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2814 winpty_entry[i].name)) == NULL)
2815 {
Bram Moolenaara83e3962017-08-17 14:39:07 +02002816 if (verbose)
2817 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2818 return FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002819 }
2820 }
2821
Bram Moolenaara83e3962017-08-17 14:39:07 +02002822 return OK;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002823}
2824
2825/*
2826 * Create a new terminal of "rows" by "cols" cells.
2827 * Store a reference in "term".
2828 * Return OK or FAIL.
2829 */
2830 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002831term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002832{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002833 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002834 channel_T *channel = NULL;
2835 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002836 DWORD error;
2837 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2838 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002839 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002840 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002841 garray_T ga;
2842 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843
Bram Moolenaara83e3962017-08-17 14:39:07 +02002844 if (dyn_winpty_init(TRUE) == FAIL)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002845 return FAIL;
2846
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002847 if (argvar->v_type == VAR_STRING)
2848 cmd = argvar->vval.v_string;
2849 else
2850 {
2851 ga_init2(&ga, (int)sizeof(char*), 20);
2852 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2853 goto failed;
2854 cmd = ga.ga_data;
2855 }
2856
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002857 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002858 if (p == NULL)
2859 return FAIL;
2860
2861 job = job_alloc();
2862 if (job == NULL)
2863 goto failed;
2864
2865 channel = add_channel();
2866 if (channel == NULL)
2867 goto failed;
2868
2869 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2870 if (term->tl_winpty_config == NULL)
2871 goto failed;
2872
2873 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2874 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2875 if (term->tl_winpty == NULL)
2876 goto failed;
2877
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002878 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002879 spawn_config = winpty_spawn_config_new(
2880 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2881 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2882 NULL,
2883 p,
2884 NULL,
2885 NULL,
2886 &winpty_err);
2887 if (spawn_config == NULL)
2888 goto failed;
2889
2890 channel = add_channel();
2891 if (channel == NULL)
2892 goto failed;
2893
2894 job = job_alloc();
2895 if (job == NULL)
2896 goto failed;
2897
2898 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2899 &child_thread_handle, &error, &winpty_err))
2900 goto failed;
2901
2902 channel_set_pipes(channel,
2903 (sock_T) CreateFileW(
2904 winpty_conin_name(term->tl_winpty),
2905 GENERIC_WRITE, 0, NULL,
2906 OPEN_EXISTING, 0, NULL),
2907 (sock_T) CreateFileW(
2908 winpty_conout_name(term->tl_winpty),
2909 GENERIC_READ, 0, NULL,
2910 OPEN_EXISTING, 0, NULL),
2911 (sock_T) CreateFileW(
2912 winpty_conerr_name(term->tl_winpty),
2913 GENERIC_READ, 0, NULL,
2914 OPEN_EXISTING, 0, NULL));
2915
2916 jo = CreateJobObject(NULL, NULL);
2917 if (jo == NULL)
2918 goto failed;
2919
2920 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002921 {
2922 /* Failed, switch the way to terminate process with TerminateProcess. */
2923 CloseHandle(jo);
2924 jo = NULL;
2925 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002926
2927 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002928 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929
2930 create_vterm(term, rows, cols);
2931
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002932 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002933 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934
2935 job->jv_channel = channel;
2936 job->jv_proc_info.hProcess = child_process_handle;
2937 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2938 job->jv_job_object = jo;
2939 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002940 sprintf(buf, "winpty://%lu",
2941 GetProcessId(winpty_agent_process(term->tl_winpty)));
2942 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002943 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002944 term->tl_job = job;
2945
2946 return OK;
2947
2948failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002949 if (argvar->v_type == VAR_LIST)
2950 vim_free(ga.ga_data);
2951 if (p != NULL)
2952 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002953 if (spawn_config != NULL)
2954 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002955 if (channel != NULL)
2956 channel_clear(channel);
2957 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002958 {
2959 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002960 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002961 }
2962 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002963 if (jo != NULL)
2964 CloseHandle(jo);
2965 if (term->tl_winpty != NULL)
2966 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002967 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002968 if (term->tl_winpty_config != NULL)
2969 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002970 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002971 if (winpty_err != NULL)
2972 {
2973 char_u *msg = utf16_to_enc(
2974 (short_u *)winpty_error_msg(winpty_err), NULL);
2975
2976 EMSG(msg);
2977 winpty_error_free(winpty_err);
2978 }
2979 return FAIL;
2980}
2981
2982/*
2983 * Free the terminal emulator part of "term".
2984 */
2985 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002986term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002987{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002988 if (term->tl_winpty != NULL)
2989 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002990 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002991 if (term->tl_winpty_config != NULL)
2992 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002993 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002994 if (term->tl_vterm != NULL)
2995 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002996 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002997}
2998
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002999/*
3000 * Request size to terminal.
3001 */
3002 static void
3003term_report_winsize(term_T *term, int rows, int cols)
3004{
3005 winpty_set_size(term->tl_winpty, cols, rows, NULL);
3006}
3007
Bram Moolenaara83e3962017-08-17 14:39:07 +02003008 int
3009terminal_enabled(void)
3010{
3011 return dyn_winpty_init(FALSE) == OK;
3012}
3013
3014
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003015# else
3016
3017/**************************************
3018 * 3. Unix-like implementation.
3019 */
3020
3021/*
3022 * Create a new terminal of "rows" by "cols" cells.
3023 * Start job for "cmd".
3024 * Store the pointers in "term".
3025 * Return OK or FAIL.
3026 */
3027 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003028term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003029{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003030 create_vterm(term, rows, cols);
3031
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02003032 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02003033 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02003034 if (term->tl_job != NULL)
3035 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036
Bram Moolenaar61a66052017-07-22 18:39:00 +02003037 return term->tl_job != NULL
3038 && term->tl_job->jv_channel != NULL
3039 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003040}
3041
3042/*
3043 * Free the terminal emulator part of "term".
3044 */
3045 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003046term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003047{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003048 if (term->tl_vterm != NULL)
3049 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003050 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003051}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003052
3053/*
3054 * Request size to terminal.
3055 */
3056 static void
3057term_report_winsize(term_T *term, int rows, int cols)
3058{
3059 /* Use an ioctl() to report the new window size to the job. */
3060 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3061 {
3062 int fd = -1;
3063 int part;
3064
3065 for (part = PART_OUT; part < PART_COUNT; ++part)
3066 {
3067 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3068 if (isatty(fd))
3069 break;
3070 }
3071 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003072 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003073 }
3074}
3075
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003076# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003077
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003078#endif /* FEAT_TERMINAL */