blob: 149679e3fc0fbe6d97b3e0a7cc7fc13d5e126f15 [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 Moolenaar94053a52017-08-01 21:44:33 +020041 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020042 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020043 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020044 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020045 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020046 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
47 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
48 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
49 * Check that something is connected to the terminal.
50 * Test: "cat" reading from a file or buffer
51 * "ls" writing stdout to a file or buffer
52 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020053 * - For the GUI fill termios with default values, perhaps like pangoterm:
54 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020055 * - support ":term NONE" to open a terminal with a pty but not running a job
56 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020057 * - if the job in the terminal does not support the mouse, we can use the
58 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020059 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
60 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020061 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020062 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020063 * - Copy text in the vterm to the Vim buffer once in a while, so that
64 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020065 */
66
67#include "vim.h"
68
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020069#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070
Bram Moolenaard5310982017-08-05 15:16:32 +020071#ifndef MIN
72# define MIN(x,y) ((x) < (y) ? (x) : (y))
73#endif
74#ifndef MAX
75# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020076#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020078#include "libvterm/include/vterm.h"
79
Bram Moolenaar33a43be2017-08-06 21:36:22 +020080/* This is VTermScreenCell without the characters, thus much smaller. */
81typedef struct {
82 VTermScreenCellAttrs attrs;
83 char width;
84 VTermColor fg, bg;
85} cellattr_T;
86
Bram Moolenaard85f2712017-07-28 21:51:57 +020087typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020088 int sb_cols; /* can differ per line */
89 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020090} sb_line_T;
91
Bram Moolenaare4f25e42017-07-07 11:54:15 +020092/* typedef term_T in structs.h */
93struct terminal_S {
94 term_T *tl_next;
95
Bram Moolenaar423802d2017-07-30 16:52:24 +020096 VTerm *tl_vterm;
97 job_T *tl_job;
98 buf_T *tl_buffer;
99
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200100 /* used when tl_job is NULL and only a pty was created */
101 int tl_tty_fd;
102 char_u *tl_tty_name;
103
Bram Moolenaar6d819742017-08-06 14:57:49 +0200104 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200105 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200106 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200107 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200108
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200109#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200110 void *tl_winpty_config;
111 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200112#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200113
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200114 /* last known vterm size */
115 int tl_rows;
116 int tl_cols;
117 /* vterm size does not follow window size */
118 int tl_rows_fixed;
119 int tl_cols_fixed;
120
Bram Moolenaar21554412017-07-24 21:44:43 +0200121 char_u *tl_title; /* NULL or allocated */
122 char_u *tl_status_text; /* NULL or allocated */
123
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124 /* Range of screen rows to update. Zero based. */
125 int tl_dirty_row_start; /* -1 if nothing dirty */
126 int tl_dirty_row_end; /* row below last one to update */
127
Bram Moolenaard85f2712017-07-28 21:51:57 +0200128 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200129 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200130
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200131 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200132 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200133 int tl_cursor_blink;
134 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
135 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200136
137 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200138};
139
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200140#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
141#define TMODE_LOOP 2 /* CTRL-W N used */
142
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200143/*
144 * List of all active terminals.
145 */
146static term_T *first_term = NULL;
147
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200148/* Terminal active in terminal_loop(). */
149static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200150
151#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
152#define KEY_BUF_LEN 200
153
154/*
155 * Functions with separate implementation for MS-Windows and Unix-like systems.
156 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200157static int term_and_job_init(term_T *term, int rows, int cols,
158 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200159static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200160static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200162/* The characters that we know (or assume) that the terminal expects for the
163 * backspace and enter keys. */
164static int term_backspace_char = BS;
165static int term_enter_char = CAR;
166static int term_nl_does_cr = FALSE;
167
168
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200169/**************************************
170 * 1. Generic code for all systems.
171 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200172
173/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200174 * Determine the terminal size from 'termsize' and the current window.
175 * Assumes term->tl_rows and term->tl_cols are zero.
176 */
177 static void
178set_term_and_win_size(term_T *term)
179{
180 if (*curwin->w_p_tms != NUL)
181 {
182 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
183
184 term->tl_rows = atoi((char *)curwin->w_p_tms);
185 term->tl_cols = atoi((char *)p);
186 }
187 if (term->tl_rows == 0)
188 term->tl_rows = curwin->w_height;
189 else
190 {
191 win_setheight_win(term->tl_rows, curwin);
192 term->tl_rows_fixed = TRUE;
193 }
194 if (term->tl_cols == 0)
195 term->tl_cols = curwin->w_width;
196 else
197 {
198 win_setwidth_win(term->tl_cols, curwin);
199 term->tl_cols_fixed = TRUE;
200 }
201}
202
203/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200204 * Initialize job options for a terminal job.
205 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200206 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 static void
208init_job_options(jobopt_T *opt)
209{
210 clear_job_options(opt);
211
212 opt->jo_mode = MODE_RAW;
213 opt->jo_out_mode = MODE_RAW;
214 opt->jo_err_mode = MODE_RAW;
215 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
216
217 opt->jo_io[PART_OUT] = JIO_BUFFER;
218 opt->jo_io[PART_ERR] = JIO_BUFFER;
219 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
220
221 opt->jo_modifiable[PART_OUT] = 0;
222 opt->jo_modifiable[PART_ERR] = 0;
223 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
224
225 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
226}
227
228/*
229 * Set job options mandatory for a terminal job.
230 */
231 static void
232setup_job_options(jobopt_T *opt, int rows, int cols)
233{
234 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
235 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
236 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200237 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
238 opt->jo_term_rows = rows;
239 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
240 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200241}
242
243 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200244term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200245{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200246 exarg_T split_ea;
247 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200249 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250
251 if (check_restricted() || check_secure())
252 return;
253
254 term = (term_T *)alloc_clear(sizeof(term_T));
255 if (term == NULL)
256 return;
257 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200258 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200259 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200260 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200261 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200262
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200263 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200264 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200265 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 /* Create a new buffer in the current window. */
267 if (!can_abandon(curbuf, forceit))
268 {
269 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200270 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 return;
272 }
273 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
274 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200275 {
276 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200277 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 }
279 }
280 else if (opt->jo_hidden)
281 {
282 buf_T *buf;
283
284 /* Create a new buffer without a window. Make it the current buffer for
285 * a moment to be able to do the initialisations. */
286 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
287 BLN_NEW | BLN_LISTED);
288 if (buf == NULL || ml_open(buf) == FAIL)
289 {
290 vim_free(term);
291 return;
292 }
293 old_curbuf = curbuf;
294 --curbuf->b_nwindows;
295 curbuf = buf;
296 curwin->w_buffer = buf;
297 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200298 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200299 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 /* Open a new window or tab. */
302 split_ea.cmdidx = CMD_new;
303 split_ea.cmd = (char_u *)"new";
304 split_ea.arg = (char_u *)"";
305 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
306 {
307 split_ea.line2 = opt->jo_term_rows;
308 split_ea.addr_count = 1;
309 }
310 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
311 {
312 split_ea.line2 = opt->jo_term_cols;
313 split_ea.addr_count = 1;
314 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200315
Bram Moolenaarda43b612017-08-11 22:27:50 +0200316 ex_splitview(&split_ea);
317 if (curwin == old_curwin)
318 {
319 /* split failed */
320 vim_free(term);
321 return;
322 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200323 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200324 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200326
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200327 if (!opt->jo_hidden)
328 {
329 /* only one size was taken care of with :new, do the other one */
330 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
331 win_setheight(opt->jo_term_rows);
332 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
333 win_setwidth(opt->jo_term_cols);
334 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200335
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200336 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200337 term->tl_next = first_term;
338 first_term = term;
339
Bram Moolenaar78712a72017-08-05 14:50:12 +0200340 if (opt->jo_term_name != NULL)
341 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
342 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200343 {
344 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200345 size_t len;
346 char_u *cmd, *p;
347
348 if (argvar->v_type == VAR_STRING)
349 cmd = argvar->vval.v_string;
350 else if (argvar->v_type != VAR_LIST
351 || argvar->vval.v_list == NULL
352 || argvar->vval.v_list->lv_len < 1)
353 cmd = (char_u*)"";
354 else
355 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
356
357 len = STRLEN(cmd) + 10;
358 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200359
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200360 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200361 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200362 /* Prepend a ! to the command name to avoid the buffer name equals
363 * the executable, otherwise ":w!" would overwrite it. */
364 if (i == 0)
365 vim_snprintf((char *)p, len, "!%s", cmd);
366 else
367 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200368 if (buflist_findname(p) == NULL)
369 {
370 curbuf->b_ffname = p;
371 break;
372 }
373 }
374 }
375 curbuf->b_fname = curbuf->b_ffname;
376
Bram Moolenaar37c45832017-08-12 16:01:04 +0200377 if (opt->jo_term_opencmd != NULL)
378 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
379
Bram Moolenaareb44a682017-08-03 22:44:55 +0200380 set_string_option_direct((char_u *)"buftype", -1,
381 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
382
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200383 /* Mark the buffer as not modifiable. It can only be made modifiable after
384 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200385 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200386
387 /* Set 'bufhidden' to "hide": allow closing the window. */
388 set_string_option_direct((char_u *)"bufhidden", -1,
389 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200390
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200391 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200392 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200393
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200394 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200395 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt)
396 == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200397 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200398 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200400 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200401
402 if (old_curbuf != NULL)
403 {
404 --curbuf->b_nwindows;
405 curbuf = old_curbuf;
406 curwin->w_buffer = curbuf;
407 ++curbuf->b_nwindows;
408 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200409 }
410 else
411 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200412 buf_T *buf = curbuf;
413
Bram Moolenaard85f2712017-07-28 21:51:57 +0200414 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200415 if (old_curbuf != NULL)
416 {
417 --curbuf->b_nwindows;
418 curbuf = old_curbuf;
419 curwin->w_buffer = curbuf;
420 ++curbuf->b_nwindows;
421 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200422
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200423 /* Wiping out the buffer will also close the window and call
424 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200425 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200426 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200427}
428
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200429/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200430 * ":terminal": open a terminal window and execute a job in it.
431 */
432 void
433ex_terminal(exarg_T *eap)
434{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200435 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200436 jobopt_T opt;
437 char_u *cmd;
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200438 char_u *tofree = NULL;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200439
440 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200441
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 cmd = eap->arg;
443 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
444 {
445 char_u *p;
446
447 cmd += 2;
448 p = skiptowhite(cmd);
449 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
450 opt.jo_term_finish = 'c';
451 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
452 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200453 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
454 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200455 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
456 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200457 else
458 {
459 if (*p)
460 *p = NUL;
461 EMSG2(_("E181: Invalid attribute: %s"), cmd);
462 return;
463 }
464 cmd = skipwhite(p);
465 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200466 if (cmd == NULL || *cmd == NUL)
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200467 /* Make a copy, an autocommand may set 'shell'. */
468 tofree = cmd = vim_strsave(p_sh);
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200469
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200470 if (eap->addr_count == 2)
471 {
472 opt.jo_term_rows = eap->line1;
473 opt.jo_term_cols = eap->line2;
474 }
475 else if (eap->addr_count == 1)
476 {
477 if (cmdmod.split & WSP_VERT)
478 opt.jo_term_cols = eap->line2;
479 else
480 opt.jo_term_rows = eap->line2;
481 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200482
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200483 argvar.v_type = VAR_STRING;
484 argvar.vval.v_string = cmd;
485 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar4fa10192017-08-14 22:56:27 +0200486 vim_free(tofree);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200487}
488
489/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200490 * Free the scrollback buffer for "term".
491 */
492 static void
493free_scrollback(term_T *term)
494{
495 int i;
496
497 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
498 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
499 ga_clear(&term->tl_scrollback);
500}
501
502/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200503 * Free a terminal and everything it refers to.
504 * Kills the job if there is one.
505 * Called when wiping out a buffer.
506 */
507 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200508free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200509{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200510 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200511 term_T *tp;
512
513 if (term == NULL)
514 return;
515 if (first_term == term)
516 first_term = term->tl_next;
517 else
518 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
519 if (tp->tl_next == term)
520 {
521 tp->tl_next = term->tl_next;
522 break;
523 }
524
525 if (term->tl_job != NULL)
526 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200527 if (term->tl_job->jv_status != JOB_ENDED
528 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200529 job_stop(term->tl_job, NULL, "kill");
530 job_unref(term->tl_job);
531 }
532
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200533 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200534
535 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200536 vim_free(term->tl_title);
537 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200538 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200539 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200540 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200541 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200542 if (in_terminal_loop == term)
543 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200544}
545
546/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200547 * Write job output "msg[len]" to the vterm.
548 */
549 static void
550term_write_job_output(term_T *term, char_u *msg, size_t len)
551{
552 VTerm *vterm = term->tl_vterm;
553 char_u *p;
554 size_t done;
555 size_t len_now;
556
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200557 if (term_nl_does_cr)
558 vterm_input_write(vterm, (char *)msg, len);
559 else
560 /* need to convert NL to CR-NL */
561 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200562 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200563 for (p = msg + done; p < msg + len; )
564 {
565 if (*p == NL)
566 break;
567 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
568 }
569 len_now = p - msg - done;
570 vterm_input_write(vterm, (char *)msg + done, len_now);
571 if (p < msg + len && *p == NL)
572 {
573 vterm_input_write(vterm, "\r\n", 2);
574 ++len_now;
575 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200576 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200577
578 /* this invokes the damage callbacks */
579 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
580}
581
Bram Moolenaar1c844932017-07-24 23:36:41 +0200582 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200583update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200584{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200585 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200586 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200587 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200588 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200589 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200590 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200591 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200592 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200593#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200594 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200595 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200596#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200597 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200598}
599
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200600/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200601 * Invoked when "msg" output from a job was received. Write it to the terminal
602 * of "buffer".
603 */
604 void
605write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
606{
607 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200608 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200609
Bram Moolenaard85f2712017-07-28 21:51:57 +0200610 if (term->tl_vterm == NULL)
611 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200612 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200613 return;
614 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200615 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200616 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200617
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200618 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
619 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200620 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200621 {
622 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200623 ch_log(term->tl_job->jv_channel, "updating screen");
624 if (buffer == curbuf)
625 {
626 update_screen(0);
627 update_cursor(term, TRUE);
628 }
629 else
630 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200631 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200632}
633
634/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200635 * Send a mouse position and click to the vterm
636 */
637 static int
638term_send_mouse(VTerm *vterm, int button, int pressed)
639{
640 VTermModifier mod = VTERM_MOD_NONE;
641
642 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
643 mouse_col - W_WINCOL(curwin), mod);
644 vterm_mouse_button(vterm, button, pressed, mod);
645 return TRUE;
646}
647
648/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200649 * Convert typed key "c" into bytes to send to the job.
650 * Return the number of bytes in "buf".
651 */
652 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200653term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200654{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200655 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200656 VTermKey key = VTERM_KEY_NONE;
657 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200658 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200659
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200660 switch (c)
661 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200662 case CAR: c = term_enter_char; break;
663 /* don't use VTERM_KEY_BACKSPACE, it always
664 * becomes 0x7f DEL */
665 case K_BS: c = term_backspace_char; break;
666
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200668 case K_DEL: key = VTERM_KEY_DEL; break;
669 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200670 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
671 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200672 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200673 case K_S_END: mod = VTERM_MOD_SHIFT;
674 key = VTERM_KEY_END; break;
675 case K_C_END: mod = VTERM_MOD_CTRL;
676 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200677 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
678 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
679 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
680 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
681 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
682 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
683 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
684 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
685 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
686 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
687 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
688 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
689 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200690 case K_S_HOME: mod = VTERM_MOD_SHIFT;
691 key = VTERM_KEY_HOME; break;
692 case K_C_HOME: mod = VTERM_MOD_CTRL;
693 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200694 case K_INS: key = VTERM_KEY_INS; break;
695 case K_K0: key = VTERM_KEY_KP_0; break;
696 case K_K1: key = VTERM_KEY_KP_1; break;
697 case K_K2: key = VTERM_KEY_KP_2; break;
698 case K_K3: key = VTERM_KEY_KP_3; break;
699 case K_K4: key = VTERM_KEY_KP_4; break;
700 case K_K5: key = VTERM_KEY_KP_5; break;
701 case K_K6: key = VTERM_KEY_KP_6; break;
702 case K_K7: key = VTERM_KEY_KP_7; break;
703 case K_K8: key = VTERM_KEY_KP_8; break;
704 case K_K9: key = VTERM_KEY_KP_9; break;
705 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
706 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
707 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
708 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
709 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
710 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
711 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
712 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
713 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
714 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
715 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
716 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
717 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200718 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
719 key = VTERM_KEY_LEFT; break;
720 case K_C_LEFT: mod = VTERM_MOD_CTRL;
721 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200722 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
723 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
724 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200725 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
726 key = VTERM_KEY_RIGHT; break;
727 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
728 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200729 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200730 case K_S_UP: mod = VTERM_MOD_SHIFT;
731 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200732 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200733
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200734 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
735 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
736 case K_MOUSELEFT: /* TODO */ return 0;
737 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200738
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200739 case K_LEFTMOUSE:
740 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
741 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
742 case K_LEFTRELEASE:
743 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
744 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
745 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
746 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
747 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
748 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
749 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
750 case K_X1MOUSE: /* TODO */ return 0;
751 case K_X1DRAG: /* TODO */ return 0;
752 case K_X1RELEASE: /* TODO */ return 0;
753 case K_X2MOUSE: /* TODO */ return 0;
754 case K_X2DRAG: /* TODO */ return 0;
755 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200756
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200757 case K_IGNORE: return 0;
758 case K_NOP: return 0;
759 case K_UNDO: return 0;
760 case K_HELP: return 0;
761 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
762 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
763 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
764 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
765 case K_SELECT: return 0;
766#ifdef FEAT_GUI
767 case K_VER_SCROLLBAR: return 0;
768 case K_HOR_SCROLLBAR: return 0;
769#endif
770#ifdef FEAT_GUI_TABLINE
771 case K_TABLINE: return 0;
772 case K_TABMENU: return 0;
773#endif
774#ifdef FEAT_NETBEANS_INTG
775 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
776#endif
777#ifdef FEAT_DND
778 case K_DROP: return 0;
779#endif
780#ifdef FEAT_AUTOCMD
781 case K_CURSORHOLD: return 0;
782#endif
783 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
784 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200785 }
786
787 /*
788 * Convert special keys to vterm keys:
789 * - Write keys to vterm: vterm_keyboard_key()
790 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200791 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200792 */
793 if (key != VTERM_KEY_NONE)
794 /* Special key, let vterm convert it. */
795 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200796 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200797 /* Normal character, let vterm convert it. */
798 vterm_keyboard_unichar(vterm, c, mod);
799
800 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200801 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200802}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200803
Bram Moolenaar938783d2017-07-16 20:13:26 +0200804/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200805 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200806 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200807 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200808term_job_running(term_T *term)
809{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200810 /* Also consider the job finished when the channel is closed, to avoid a
811 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200812 return term != NULL
813 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200814 && term->tl_job->jv_status == JOB_STARTED
815 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200816}
817
818/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200819 * Add the last line of the scrollback buffer to the buffer in the window.
820 */
821 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200822add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200823{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200824 buf_T *buf = term->tl_buffer;
825 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
826 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200827
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200828 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200829 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830 {
831 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200832 curbuf = buf;
833 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834 curbuf = curwin->w_buffer;
835 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836}
837
838/*
839 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200840 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200841 */
842 static void
843move_terminal_to_buffer(term_T *term)
844{
845 win_T *wp;
846 int len;
847 int lines_skipped = 0;
848 VTermPos pos;
849 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200850 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200851 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200852
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200853 if (term->tl_vterm == NULL)
854 return;
855 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200856 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
857 {
858 len = 0;
859 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
860 if (vterm_screen_get_cell(screen, pos, &cell) != 0
861 && cell.chars[0] != NUL)
862 len = pos.col + 1;
863
864 if (len == 0)
865 ++lines_skipped;
866 else
867 {
868 while (lines_skipped > 0)
869 {
870 /* Line was skipped, add an empty line. */
871 --lines_skipped;
872 if (ga_grow(&term->tl_scrollback, 1) == OK)
873 {
874 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
875 + term->tl_scrollback.ga_len;
876
877 line->sb_cols = 0;
878 line->sb_cells = NULL;
879 ++term->tl_scrollback.ga_len;
880
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200881 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200882 }
883 }
884
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200885 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200886 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
887 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200888 garray_T ga;
889 int width;
890 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 + term->tl_scrollback.ga_len;
892
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 ga_init2(&ga, 1, 100);
894 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200895 {
896 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200897 {
898 width = 1;
899 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
900 if (ga_grow(&ga, 1) == OK)
901 ga.ga_len += mb_char2bytes(' ',
902 (char_u *)ga.ga_data + ga.ga_len);
903 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200904 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200905 {
906 width = cell.width;
907
908 p[pos.col].width = cell.width;
909 p[pos.col].attrs = cell.attrs;
910 p[pos.col].fg = cell.fg;
911 p[pos.col].bg = cell.bg;
912
913 if (ga_grow(&ga, MB_MAXBYTES) == OK)
914 {
915 int i;
916 int c;
917
918 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
919 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
920 (char_u *)ga.ga_data + ga.ga_len);
921 }
922 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200923 }
924 line->sb_cols = len;
925 line->sb_cells = p;
926 ++term->tl_scrollback.ga_len;
927
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200928 if (ga_grow(&ga, 1) == FAIL)
929 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
930 else
931 {
932 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
933 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
934 }
935 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200936 }
937 else
938 vim_free(p);
939 }
940 }
941
942 FOR_ALL_WINDOWS(wp)
943 {
944 if (wp->w_buffer == term->tl_buffer)
945 {
946 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
947 wp->w_cursor.col = 0;
948 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200949 if (wp->w_cursor.lnum >= wp->w_height)
950 {
951 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
952
953 if (wp->w_topline < min_topline)
954 wp->w_topline = min_topline;
955 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956 redraw_win_later(wp, NOT_VALID);
957 }
958 }
959}
960
961 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200962set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200963{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200964 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200965 vim_free(term->tl_status_text);
966 term->tl_status_text = NULL;
967 if (term->tl_buffer == curbuf)
968 maketitle();
969}
970
971/*
972 * Called after the job if finished and Terminal mode is not active:
973 * Move the vterm contents into the scrollback buffer and free the vterm.
974 */
975 static void
976cleanup_vterm(term_T *term)
977{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200978 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200979 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200980 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200981 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982}
983
984/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200985 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986 * Suspends updating the terminal window.
987 */
988 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200989term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200990{
991 term_T *term = curbuf->b_term;
992
993 /* Append the current terminal contents to the buffer. */
994 move_terminal_to_buffer(term);
995
Bram Moolenaar6d819742017-08-06 14:57:49 +0200996 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200997
Bram Moolenaar6d819742017-08-06 14:57:49 +0200998 /* Move the window cursor to the position of the cursor in the
999 * terminal. */
1000 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1001 + term->tl_cursor_pos.row + 1;
1002 check_cursor();
1003 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001004
Bram Moolenaar6d819742017-08-06 14:57:49 +02001005 /* Display the same lines as in the terminal. */
1006 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001007}
1008
1009/*
1010 * Returns TRUE if the current window contains a terminal and we are in
1011 * Terminal-Normal mode.
1012 */
1013 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001014term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001015{
1016 term_T *term = curbuf->b_term;
1017
Bram Moolenaar6d819742017-08-06 14:57:49 +02001018 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019}
1020
1021/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001022 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001023 * Restores updating the terminal window.
1024 */
1025 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001026term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001027{
1028 term_T *term = curbuf->b_term;
1029 sb_line_T *line;
1030 garray_T *gap;
1031
1032 /* Remove the terminal contents from the scrollback and the buffer. */
1033 gap = &term->tl_scrollback;
1034 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1035 {
1036 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1037 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1038 vim_free(line->sb_cells);
1039 --gap->ga_len;
1040 if (gap->ga_len == 0)
1041 break;
1042 }
1043 check_cursor();
1044
Bram Moolenaar6d819742017-08-06 14:57:49 +02001045 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001046
1047 if (term->tl_channel_closed)
1048 cleanup_vterm(term);
1049 redraw_buf_and_status_later(curbuf, NOT_VALID);
1050}
1051
1052/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001053 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001054 * Note: while waiting a terminal may be closed and freed if the channel is
1055 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001056 * TODO: use terminal mode mappings.
1057 */
1058 static int
1059term_vgetc()
1060{
1061 int c;
1062
1063 ++no_mapping;
1064 ++allow_keys;
1065 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001066#ifdef WIN3264
1067 ctrl_break_was_pressed = FALSE;
1068#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001069 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001070 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001071 --no_mapping;
1072 --allow_keys;
1073 return c;
1074}
1075
1076/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001077 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001078 * Return FAIL when the key needs to be handled in Normal mode.
1079 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001080 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001081 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001082send_keys_to_term(term_T *term, int c, int typed)
1083{
1084 char msg[KEY_BUF_LEN];
1085 size_t len;
1086 static int mouse_was_outside = FALSE;
1087 int dragging_outside = FALSE;
1088
1089 /* Catch keys that need to be handled as in Normal mode. */
1090 switch (c)
1091 {
1092 case NUL:
1093 case K_ZERO:
1094 if (typed)
1095 stuffcharReadbuff(c);
1096 return FAIL;
1097
1098 case K_IGNORE:
1099 return FAIL;
1100
1101 case K_LEFTDRAG:
1102 case K_MIDDLEDRAG:
1103 case K_RIGHTDRAG:
1104 case K_X1DRAG:
1105 case K_X2DRAG:
1106 dragging_outside = mouse_was_outside;
1107 /* FALLTHROUGH */
1108 case K_LEFTMOUSE:
1109 case K_LEFTMOUSE_NM:
1110 case K_LEFTRELEASE:
1111 case K_LEFTRELEASE_NM:
1112 case K_MIDDLEMOUSE:
1113 case K_MIDDLERELEASE:
1114 case K_RIGHTMOUSE:
1115 case K_RIGHTRELEASE:
1116 case K_X1MOUSE:
1117 case K_X1RELEASE:
1118 case K_X2MOUSE:
1119 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001120
1121 case K_MOUSEUP:
1122 case K_MOUSEDOWN:
1123 case K_MOUSELEFT:
1124 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001125 if (mouse_row < W_WINROW(curwin)
1126 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1127 || mouse_col < W_WINCOL(curwin)
1128 || mouse_col >= W_ENDCOL(curwin)
1129 || dragging_outside)
1130 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001131 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001132 if (typed)
1133 {
1134 stuffcharReadbuff(c);
1135 mouse_was_outside = TRUE;
1136 }
1137 return FAIL;
1138 }
1139 }
1140 if (typed)
1141 mouse_was_outside = FALSE;
1142
1143 /* Convert the typed key to a sequence of bytes for the job. */
1144 len = term_convert_key(term, c, msg);
1145 if (len > 0)
1146 /* TODO: if FAIL is returned, stop? */
1147 channel_send(term->tl_job->jv_channel, PART_IN,
1148 (char_u *)msg, (int)len, NULL);
1149
1150 return OK;
1151}
1152
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001153 static void
1154position_cursor(win_T *wp, VTermPos *pos)
1155{
1156 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1157 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1158 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1159}
1160
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001161/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001162 * Handle CTRL-W "": send register contents to the job.
1163 */
1164 static void
1165term_paste_register(int prev_c UNUSED)
1166{
1167 int c;
1168 list_T *l;
1169 listitem_T *item;
1170 long reglen = 0;
1171 int type;
1172
1173#ifdef FEAT_CMDL_INFO
1174 if (add_to_showcmd(prev_c))
1175 if (add_to_showcmd('"'))
1176 out_flush();
1177#endif
1178 c = term_vgetc();
1179#ifdef FEAT_CMDL_INFO
1180 clear_showcmd();
1181#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001182 if (!term_use_loop())
1183 /* job finished while waiting for a character */
1184 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001185
1186 /* CTRL-W "= prompt for expression to evaluate. */
1187 if (c == '=' && get_expr_register() != '=')
1188 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001189 if (!term_use_loop())
1190 /* job finished while waiting for a character */
1191 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001192
1193 l = (list_T *)get_reg_contents(c, GREG_LIST);
1194 if (l != NULL)
1195 {
1196 type = get_reg_type(c, &reglen);
1197 for (item = l->lv_first; item != NULL; item = item->li_next)
1198 {
1199 char_u *s = get_tv_string(&item->li_tv);
1200
1201 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1202 s, STRLEN(s), NULL);
1203 if (item->li_next != NULL || type == MLINE)
1204 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1205 (char_u *)"\r", 1, NULL);
1206 }
1207 list_free(l);
1208 }
1209}
1210
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001211#if defined(FEAT_GUI) || defined(PROTO)
1212/*
1213 * Return TRUE when the cursor of the terminal should be displayed.
1214 */
1215 int
1216use_terminal_cursor()
1217{
1218 return in_terminal_loop != NULL;
1219}
1220
1221 cursorentry_T *
1222term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1223{
1224 term_T *term = in_terminal_loop;
1225 static cursorentry_T entry;
1226
1227 vim_memset(&entry, 0, sizeof(entry));
1228 entry.shape = entry.mshape =
1229 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1230 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1231 SHAPE_BLOCK;
1232 entry.percentage = 20;
1233 if (term->tl_cursor_blink)
1234 {
1235 entry.blinkwait = 700;
1236 entry.blinkon = 400;
1237 entry.blinkon = 250;
1238 }
1239 *fg = gui.back_pixel;
1240 if (term->tl_cursor_color == NULL)
1241 *bg = gui.norm_pixel;
1242 else
1243 *bg = color_name2handle(term->tl_cursor_color);
1244 entry.name = "n";
1245 entry.used_for = SHAPE_CURSOR;
1246
1247 return &entry;
1248}
1249#endif
1250
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001251static int did_change_cursor = FALSE;
1252
1253 static void
1254may_set_cursor_props(term_T *term)
1255{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001256#ifdef FEAT_GUI
1257 /* For the GUI the cursor properties are obtained with
1258 * term_get_cursor_shape(). */
1259 if (gui.in_use)
1260 return;
1261#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001262 if (in_terminal_loop == term)
1263 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001264 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001265 if (term->tl_cursor_color != NULL)
1266 term_cursor_color(term->tl_cursor_color);
1267 else
1268 term_cursor_color((char_u *)"");
1269 /* do both blink and shape+blink, in case setting shape does not work */
1270 term_cursor_blink(term->tl_cursor_blink);
1271 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1272 }
1273}
1274
1275 static void
1276may_restore_cursor_props(void)
1277{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001278#ifdef FEAT_GUI
1279 if (gui.in_use)
1280 return;
1281#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001282 if (did_change_cursor)
1283 {
1284 did_change_cursor = FALSE;
1285 ui_cursor_shape_forced(TRUE);
1286 term_cursor_color((char_u *)"");
1287 term_cursor_blink(FALSE);
1288 }
1289}
1290
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001291/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001292 * Returns TRUE if the current window contains a terminal and we are sending
1293 * keys to the job.
1294 */
1295 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001296term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001297{
1298 term_T *term = curbuf->b_term;
1299
1300 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001301 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001302 && term->tl_vterm != NULL
1303 && term_job_running(term);
1304}
1305
1306/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001307 * Wait for input and send it to the job.
1308 * Return when the start of a CTRL-W command is typed or anything else that
1309 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001310 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1311 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001312 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001313 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001314terminal_loop(void)
1315{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001316 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001317 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001318 int ret;
1319
Bram Moolenaar679653e2017-08-13 14:13:19 +02001320 /* Remember the terminal we are sending keys to. However, the terminal
1321 * might be closed while waiting for a character, e.g. typing "exit" in a
1322 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1323 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001324 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001325
1326 if (*curwin->w_p_tk != NUL)
1327 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001328 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001329 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001330
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001331#ifdef UNIX
1332 {
1333 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1334
1335 if (isatty(fd))
1336 {
1337 ttyinfo_T info;
1338
1339 /* Get the current backspace and enter characters of the pty. */
1340 if (get_tty_info(fd, &info) == OK)
1341 {
1342 term_backspace_char = info.backspace;
1343 term_enter_char = info.enter;
1344 term_nl_does_cr = info.nl_does_cr;
1345 }
1346 }
1347 }
1348#endif
1349
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001350 for (;;)
1351 {
1352 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001353 /* Repeat redrawing in case a message is received while redrawing. */
1354 while (curwin->w_redr_type != 0)
1355 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001356 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001357
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001358 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001359 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001360 /* job finished while waiting for a character */
1361 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001362 if (c == K_IGNORE)
1363 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001364
Bram Moolenaarfae42832017-08-01 22:24:26 +02001365#ifdef UNIX
1366 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1367#endif
1368#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001369 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001370 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001371 if (ctrl_break_was_pressed)
1372 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001373#endif
1374
Bram Moolenaar69198192017-08-05 14:10:48 +02001375 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001376 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001377 int prev_c = c;
1378
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001379#ifdef FEAT_CMDL_INFO
1380 if (add_to_showcmd(c))
1381 out_flush();
1382#endif
1383 c = term_vgetc();
1384#ifdef FEAT_CMDL_INFO
1385 clear_showcmd();
1386#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001387 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001388 /* job finished while waiting for a character */
1389 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001390
Bram Moolenaar69198192017-08-05 14:10:48 +02001391 if (prev_c == Ctrl_BSL)
1392 {
1393 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001394 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001395 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1396 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001397 ret = FAIL;
1398 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001399 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001400 /* Send both keys to the terminal. */
1401 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1402 }
1403 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001404 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001405 /* "CTRL-W .": send CTRL-W to the job */
1406 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001407 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001408 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001409 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001410 /* CTRL-W N : go to Terminal-Normal mode. */
1411 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001412 ret = FAIL;
1413 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001414 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001415 else if (c == '"')
1416 {
1417 term_paste_register(prev_c);
1418 continue;
1419 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001420 else if (termkey == 0 || c != termkey)
1421 {
1422 stuffcharReadbuff(Ctrl_W);
1423 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001424 ret = OK;
1425 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001426 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001427 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001428 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001429 {
1430 ret = OK;
1431 goto theend;
1432 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001433 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001434 ret = FAIL;
1435
1436theend:
1437 in_terminal_loop = NULL;
1438 may_restore_cursor_props();
1439 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001440}
1441
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001442/*
1443 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001444 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001445 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001446 */
1447 void
1448term_job_ended(job_T *job)
1449{
1450 term_T *term;
1451 int did_one = FALSE;
1452
1453 for (term = first_term; term != NULL; term = term->tl_next)
1454 if (term->tl_job == job)
1455 {
1456 vim_free(term->tl_title);
1457 term->tl_title = NULL;
1458 vim_free(term->tl_status_text);
1459 term->tl_status_text = NULL;
1460 redraw_buf_and_status_later(term->tl_buffer, VALID);
1461 did_one = TRUE;
1462 }
1463 if (did_one)
1464 redraw_statuslines();
1465 if (curbuf->b_term != NULL)
1466 {
1467 if (curbuf->b_term->tl_job == job)
1468 maketitle();
1469 update_cursor(curbuf->b_term, TRUE);
1470 }
1471}
1472
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001473 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001474may_toggle_cursor(term_T *term)
1475{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001476 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001477 {
1478 if (term->tl_cursor_visible)
1479 cursor_on();
1480 else
1481 cursor_off();
1482 }
1483}
1484
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001485 static int
1486handle_damage(VTermRect rect, void *user)
1487{
1488 term_T *term = (term_T *)user;
1489
1490 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1491 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1492 redraw_buf_later(term->tl_buffer, NOT_VALID);
1493 return 1;
1494}
1495
1496 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001497handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001498{
1499 term_T *term = (term_T *)user;
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001500 win_T *wp;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001501
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001502 if (dest.start_col == src.start_col
1503 && dest.end_col == src.end_col
1504 && dest.start_row < src.start_row)
1505 FOR_ALL_WINDOWS(wp)
1506 {
1507 if (wp->w_buffer == term->tl_buffer)
1508 /* scrolling up is much more efficient when deleting lines */
1509 win_del_lines(wp, dest.start_row,
1510 src.start_row - dest.start_row, FALSE, FALSE);
1511 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001512 redraw_buf_later(term->tl_buffer, NOT_VALID);
1513 return 1;
1514}
1515
1516 static int
1517handle_movecursor(
1518 VTermPos pos,
1519 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001520 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001521 void *user)
1522{
1523 term_T *term = (term_T *)user;
1524 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001525
1526 term->tl_cursor_pos = pos;
1527 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001528
1529 FOR_ALL_WINDOWS(wp)
1530 {
1531 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001532 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001533 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001534 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001535 {
1536 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001537 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001538 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001539
1540 return 1;
1541}
1542
Bram Moolenaar21554412017-07-24 21:44:43 +02001543 static int
1544handle_settermprop(
1545 VTermProp prop,
1546 VTermValue *value,
1547 void *user)
1548{
1549 term_T *term = (term_T *)user;
1550
1551 switch (prop)
1552 {
1553 case VTERM_PROP_TITLE:
1554 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001555 /* a blank title isn't useful, make it empty, so that "running" is
1556 * displayed */
1557 if (*skipwhite((char_u *)value->string) == NUL)
1558 term->tl_title = NULL;
1559 else
1560 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001561 vim_free(term->tl_status_text);
1562 term->tl_status_text = NULL;
1563 if (term == curbuf->b_term)
1564 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001565 break;
1566
1567 case VTERM_PROP_CURSORVISIBLE:
1568 term->tl_cursor_visible = value->boolean;
1569 may_toggle_cursor(term);
1570 out_flush();
1571 break;
1572
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001573 case VTERM_PROP_CURSORBLINK:
1574 term->tl_cursor_blink = value->boolean;
1575 may_set_cursor_props(term);
1576 break;
1577
1578 case VTERM_PROP_CURSORSHAPE:
1579 term->tl_cursor_shape = value->number;
1580 may_set_cursor_props(term);
1581 break;
1582
1583 case VTERM_PROP_CURSORCOLOR:
1584 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001585 if (*value->string == NUL)
1586 term->tl_cursor_color = NULL;
1587 else
1588 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001589 may_set_cursor_props(term);
1590 break;
1591
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001592 case VTERM_PROP_ALTSCREEN:
1593 /* TODO: do anything else? */
1594 term->tl_using_altscreen = value->boolean;
1595 break;
1596
Bram Moolenaar21554412017-07-24 21:44:43 +02001597 default:
1598 break;
1599 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001600 /* Always return 1, otherwise vterm doesn't store the value internally. */
1601 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001602}
1603
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001604/*
1605 * The job running in the terminal resized the terminal.
1606 */
1607 static int
1608handle_resize(int rows, int cols, void *user)
1609{
1610 term_T *term = (term_T *)user;
1611 win_T *wp;
1612
1613 term->tl_rows = rows;
1614 term->tl_cols = cols;
1615 FOR_ALL_WINDOWS(wp)
1616 {
1617 if (wp->w_buffer == term->tl_buffer)
1618 {
1619 win_setheight_win(rows, wp);
1620 win_setwidth_win(cols, wp);
1621 }
1622 }
1623
1624 redraw_buf_later(term->tl_buffer, NOT_VALID);
1625 return 1;
1626}
1627
Bram Moolenaard85f2712017-07-28 21:51:57 +02001628/*
1629 * Handle a line that is pushed off the top of the screen.
1630 */
1631 static int
1632handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1633{
1634 term_T *term = (term_T *)user;
1635
1636 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001637 if (ga_grow(&term->tl_scrollback, 1) == OK)
1638 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001639 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001640 int len = 0;
1641 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001642 int c;
1643 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001644 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001645 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001646
1647 /* do not store empty cells at the end */
1648 for (i = 0; i < cols; ++i)
1649 if (cells[i].chars[0] != 0)
1650 len = i + 1;
1651
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001652 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001653 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001654 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001655 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001656 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001657 for (col = 0; col < len; col += cells[col].width)
1658 {
1659 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1660 {
1661 ga.ga_len = 0;
1662 break;
1663 }
1664 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1665 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1666 (char_u *)ga.ga_data + ga.ga_len);
1667 p[col].width = cells[col].width;
1668 p[col].attrs = cells[col].attrs;
1669 p[col].fg = cells[col].fg;
1670 p[col].bg = cells[col].bg;
1671 }
1672 }
1673 if (ga_grow(&ga, 1) == FAIL)
1674 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1675 else
1676 {
1677 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1678 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1679 }
1680 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001681
1682 line = (sb_line_T *)term->tl_scrollback.ga_data
1683 + term->tl_scrollback.ga_len;
1684 line->sb_cols = len;
1685 line->sb_cells = p;
1686 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001687 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001688 }
1689 return 0; /* ignored */
1690}
1691
Bram Moolenaar21554412017-07-24 21:44:43 +02001692static VTermScreenCallbacks screen_callbacks = {
1693 handle_damage, /* damage */
1694 handle_moverect, /* moverect */
1695 handle_movecursor, /* movecursor */
1696 handle_settermprop, /* settermprop */
1697 NULL, /* bell */
1698 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001699 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001700 NULL /* sb_popline */
1701};
1702
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001703/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001704 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001705 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001706 */
1707 void
1708term_channel_closed(channel_T *ch)
1709{
1710 term_T *term;
1711 int did_one = FALSE;
1712
1713 for (term = first_term; term != NULL; term = term->tl_next)
1714 if (term->tl_job == ch->ch_job)
1715 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001716 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001717 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001718
Bram Moolenaard85f2712017-07-28 21:51:57 +02001719 vim_free(term->tl_title);
1720 term->tl_title = NULL;
1721 vim_free(term->tl_status_text);
1722 term->tl_status_text = NULL;
1723
Bram Moolenaar423802d2017-07-30 16:52:24 +02001724 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001725 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001726 {
1727 int fnum = term->tl_buffer->b_fnum;
1728
Bram Moolenaar423802d2017-07-30 16:52:24 +02001729 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001730
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001731 if (term->tl_finish == 'c')
1732 {
1733 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001734 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001735 curbuf = term->tl_buffer;
1736 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1737 break;
1738 }
1739 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1740 {
1741 char buf[50];
1742
1743 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001744 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001745 vim_snprintf(buf, sizeof(buf),
1746 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001747 ? "botright sbuf %d"
1748 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001749 do_cmdline_cmd((char_u *)buf);
1750 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001751 else
1752 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001753 }
1754
Bram Moolenaard85f2712017-07-28 21:51:57 +02001755 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001756 }
1757 if (did_one)
1758 {
1759 redraw_statuslines();
1760
1761 /* Need to break out of vgetc(). */
1762 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001763 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001764
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001765 term = curbuf->b_term;
1766 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001767 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001768 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001769 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001770 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001771 }
1772 }
1773}
1774
1775/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001776 * Reverse engineer the RGB value into a cterm color index.
1777 * First color is 1. Return 0 if no match found.
1778 */
1779 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001780color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001781{
1782 int red = color->red;
1783 int blue = color->blue;
1784 int green = color->green;
1785
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001786 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001787 if (red == 0)
1788 {
1789 if (green == 0)
1790 {
1791 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001792 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001793 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001794 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001795 }
1796 else if (green == 224)
1797 {
1798 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001799 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001800 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001801 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001802 }
1803 }
1804 else if (red == 224)
1805 {
1806 if (green == 0)
1807 {
1808 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001809 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001810 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001811 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001812 }
1813 else if (green == 224)
1814 {
1815 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001816 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001817 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001818 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001819 }
1820 }
1821 else if (red == 128)
1822 {
1823 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001824 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001825 }
1826 else if (red == 255)
1827 {
1828 if (green == 64)
1829 {
1830 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001831 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001832 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001833 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001834 }
1835 else if (green == 255)
1836 {
1837 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001838 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001839 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001840 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001841 }
1842 }
1843 else if (red == 64)
1844 {
1845 if (green == 64)
1846 {
1847 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001848 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001849 }
1850 else if (green == 255)
1851 {
1852 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001853 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001854 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001855 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001856 }
1857 }
1858 if (t_colors >= 256)
1859 {
1860 if (red == blue && red == green)
1861 {
1862 /* 24-color greyscale */
1863 static int cutoff[23] = {
1864 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1865 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1866 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1867 int i;
1868
1869 for (i = 0; i < 23; ++i)
1870 if (red < cutoff[i])
1871 return i + 233;
1872 return 256;
1873 }
1874
1875 /* 216-color cube */
1876 return 17 + ((red + 25) / 0x33) * 36
1877 + ((green + 25) / 0x33) * 6
1878 + (blue + 25) / 0x33;
1879 }
1880 return 0;
1881}
1882
1883/*
1884 * Convert the attributes of a vterm cell into an attribute index.
1885 */
1886 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001887cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001888{
1889 int attr = 0;
1890
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001891 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001892 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001893 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001894 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001895 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001896 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001897 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001898 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001899 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001900 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001901
1902#ifdef FEAT_GUI
1903 if (gui.in_use)
1904 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001905 guicolor_T fg, bg;
1906
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001907 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1908 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001909 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001910 }
1911 else
1912#endif
1913#ifdef FEAT_TERMGUICOLORS
1914 if (p_tgc)
1915 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001916 guicolor_T fg, bg;
1917
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001918 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1919 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001920
1921 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001922 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001923 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001924#endif
1925 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001926 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001927 int fg = color2index(&cellfg, TRUE, &bold);
1928 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001929
1930 /* with 8 colors set the bold attribute to get a bright foreground */
1931 if (bold == TRUE)
1932 attr |= HL_BOLD;
1933 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001934 }
1935 return 0;
1936}
1937
1938/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001939 * Called to update a window that contains an active terminal.
1940 * Returns FAIL when there is no terminal running in this window or in
1941 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001942 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001943 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001944term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001945{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001946 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001947 VTerm *vterm;
1948 VTermScreen *screen;
1949 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001950 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001951
Bram Moolenaar6d819742017-08-06 14:57:49 +02001952 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001953 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001954
Bram Moolenaard85f2712017-07-28 21:51:57 +02001955 vterm = term->tl_vterm;
1956 screen = vterm_obtain_screen(vterm);
1957 state = vterm_obtain_state(vterm);
1958
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001959 /*
1960 * If the window was resized a redraw will be triggered and we get here.
1961 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1962 */
1963 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1964 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001965 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001966 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1967 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1968 win_T *twp;
1969
1970 FOR_ALL_WINDOWS(twp)
1971 {
1972 /* When more than one window shows the same terminal, use the
1973 * smallest size. */
1974 if (twp->w_buffer == term->tl_buffer)
1975 {
1976 if (!term->tl_rows_fixed && rows > twp->w_height)
1977 rows = twp->w_height;
1978 if (!term->tl_cols_fixed && cols > twp->w_width)
1979 cols = twp->w_width;
1980 }
1981 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001982
1983 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001984 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001985 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001986 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001987 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001988
1989 /* The cursor may have been moved when resizing. */
1990 vterm_state_get_cursorpos(state, &pos);
1991 position_cursor(wp, &pos);
1992
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001993 /* TODO: Only redraw what changed. */
1994 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001995 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001996 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001997 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001998
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001999 if (pos.row < term->tl_rows)
2000 {
2001 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002002 {
2003 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002004 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002005
Bram Moolenaareeac6772017-07-23 15:48:37 +02002006 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2007 vim_memset(&cell, 0, sizeof(cell));
2008
2009 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002010 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002011 if (c == NUL)
2012 {
2013 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002014#if defined(FEAT_MBYTE)
2015 if (enc_utf8)
2016 ScreenLinesUC[off] = NUL;
2017#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002018 }
2019 else
2020 {
2021#if defined(FEAT_MBYTE)
2022 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002023 {
2024 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002025 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002026 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002027 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002028 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002029 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002030 if (enc_utf8)
2031 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002032 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002033#else
2034 ScreenLines[off] = c;
2035#endif
2036 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002037 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002038
2039 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002040 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002041 if (cell.width == 2)
2042 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002043 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002044#if defined(FEAT_MBYTE)
2045 if (enc_utf8)
2046 ScreenLinesUC[off] = NUL;
2047#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 ++pos.col;
2049 ++off;
2050 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002051 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002052 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002053 else
2054 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002055
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002056 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2057 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002058 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002059
2060 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002061}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002062
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002063/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002064 * Return TRUE if "wp" is a terminal window where the job has finished.
2065 */
2066 int
2067term_is_finished(buf_T *buf)
2068{
2069 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2070}
2071
2072/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002073 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002074 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002075 */
2076 int
2077term_show_buffer(buf_T *buf)
2078{
2079 term_T *term = buf->b_term;
2080
Bram Moolenaar6d819742017-08-06 14:57:49 +02002081 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002082}
2083
2084/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002085 * The current buffer is going to be changed. If there is terminal
2086 * highlighting remove it now.
2087 */
2088 void
2089term_change_in_curbuf(void)
2090{
2091 term_T *term = curbuf->b_term;
2092
2093 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2094 {
2095 free_scrollback(term);
2096 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002097
2098 /* The buffer is now like a normal buffer, it cannot be easily
2099 * abandoned when changed. */
2100 set_string_option_direct((char_u *)"buftype", -1,
2101 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002102 }
2103}
2104
2105/*
2106 * Get the screen attribute for a position in the buffer.
2107 */
2108 int
2109term_get_attr(buf_T *buf, linenr_T lnum, int col)
2110{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002111 term_T *term = buf->b_term;
2112 sb_line_T *line;
2113 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002114
Bram Moolenaar70229f92017-07-29 16:01:53 +02002115 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002116 return 0;
2117 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2118 if (col >= line->sb_cols)
2119 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002120 cellattr = line->sb_cells + col;
2121 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002122}
2123
2124/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002125 * Create a new vterm and initialize it.
2126 */
2127 static void
2128create_vterm(term_T *term, int rows, int cols)
2129{
2130 VTerm *vterm;
2131 VTermScreen *screen;
2132
2133 vterm = vterm_new(rows, cols);
2134 term->tl_vterm = vterm;
2135 screen = vterm_obtain_screen(vterm);
2136 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2137 /* TODO: depends on 'encoding'. */
2138 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002139
2140 /* Vterm uses a default black background. Set it to white when
2141 * 'background' is "light". */
2142 if (*p_bg == 'l')
2143 {
2144 VTermColor fg, bg;
2145
2146 fg.red = fg.green = fg.blue = 0;
2147 bg.red = bg.green = bg.blue = 255;
2148 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2149 }
2150
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002151 /* Required to initialize most things. */
2152 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002153
2154 /* Allow using alternate screen. */
2155 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002156}
2157
Bram Moolenaar21554412017-07-24 21:44:43 +02002158/*
2159 * Return the text to show for the buffer name and status.
2160 */
2161 char_u *
2162term_get_status_text(term_T *term)
2163{
2164 if (term->tl_status_text == NULL)
2165 {
2166 char_u *txt;
2167 size_t len;
2168
Bram Moolenaar6d819742017-08-06 14:57:49 +02002169 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002170 {
2171 if (term_job_running(term))
2172 txt = (char_u *)_("Terminal");
2173 else
2174 txt = (char_u *)_("Terminal-finished");
2175 }
2176 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002177 txt = term->tl_title;
2178 else if (term_job_running(term))
2179 txt = (char_u *)_("running");
2180 else
2181 txt = (char_u *)_("finished");
2182 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002183 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002184 if (term->tl_status_text != NULL)
2185 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2186 term->tl_buffer->b_fname, txt);
2187 }
2188 return term->tl_status_text;
2189}
2190
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002191/*
2192 * Mark references in jobs of terminals.
2193 */
2194 int
2195set_ref_in_term(int copyID)
2196{
2197 int abort = FALSE;
2198 term_T *term;
2199 typval_T tv;
2200
2201 for (term = first_term; term != NULL; term = term->tl_next)
2202 if (term->tl_job != NULL)
2203 {
2204 tv.v_type = VAR_JOB;
2205 tv.vval.v_job = term->tl_job;
2206 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2207 }
2208 return abort;
2209}
2210
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002211/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002212 * Get the buffer from the first argument in "argvars".
2213 * Returns NULL when the buffer is not for a terminal window.
2214 */
2215 static buf_T *
2216term_get_buf(typval_T *argvars)
2217{
2218 buf_T *buf;
2219
2220 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2221 ++emsg_off;
2222 buf = get_buf_tv(&argvars[0], FALSE);
2223 --emsg_off;
2224 if (buf == NULL || buf->b_term == NULL)
2225 return NULL;
2226 return buf;
2227}
2228
2229/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002230 * "term_getaltscreen(buf)" function
2231 */
2232 void
2233f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2234{
2235 buf_T *buf = term_get_buf(argvars);
2236
2237 if (buf == NULL)
2238 return;
2239 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2240}
2241
2242/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002243 * "term_getattr(attr, name)" function
2244 */
2245 void
2246f_term_getattr(typval_T *argvars, typval_T *rettv)
2247{
2248 int attr;
2249 size_t i;
2250 char_u *name;
2251
2252 static struct {
2253 char *name;
2254 int attr;
2255 } attrs[] = {
2256 {"bold", HL_BOLD},
2257 {"italic", HL_ITALIC},
2258 {"underline", HL_UNDERLINE},
2259 {"strike", HL_STANDOUT},
2260 {"reverse", HL_INVERSE},
2261 };
2262
2263 attr = get_tv_number(&argvars[0]);
2264 name = get_tv_string_chk(&argvars[1]);
2265 if (name == NULL)
2266 return;
2267
2268 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2269 if (STRCMP(name, attrs[i].name) == 0)
2270 {
2271 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2272 break;
2273 }
2274}
2275
2276/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002277 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002278 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002279 void
2280f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002281{
Bram Moolenaar97870002017-07-30 18:28:38 +02002282 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002283 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002284 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002285 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002286
Bram Moolenaar97870002017-07-30 18:28:38 +02002287 if (rettv_list_alloc(rettv) == FAIL)
2288 return;
2289 if (buf == NULL)
2290 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002291 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002292
2293 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002294 list_append_number(l, term->tl_cursor_pos.row + 1);
2295 list_append_number(l, term->tl_cursor_pos.col + 1);
2296
2297 d = dict_alloc();
2298 if (d != NULL)
2299 {
2300 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2301 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2302 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2303 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2304 ? (char_u *)"" : term->tl_cursor_color);
2305 list_append_dict(l, d);
2306 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002307}
2308
2309/*
2310 * "term_getjob(buf)" function
2311 */
2312 void
2313f_term_getjob(typval_T *argvars, typval_T *rettv)
2314{
2315 buf_T *buf = term_get_buf(argvars);
2316
2317 rettv->v_type = VAR_JOB;
2318 rettv->vval.v_job = NULL;
2319 if (buf == NULL)
2320 return;
2321
2322 rettv->vval.v_job = buf->b_term->tl_job;
2323 if (rettv->vval.v_job != NULL)
2324 ++rettv->vval.v_job->jv_refcount;
2325}
2326
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002327 static int
2328get_row_number(typval_T *tv, term_T *term)
2329{
2330 if (tv->v_type == VAR_STRING
2331 && tv->vval.v_string != NULL
2332 && STRCMP(tv->vval.v_string, ".") == 0)
2333 return term->tl_cursor_pos.row;
2334 return (int)get_tv_number(tv) - 1;
2335}
2336
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002337/*
2338 * "term_getline(buf, row)" function
2339 */
2340 void
2341f_term_getline(typval_T *argvars, typval_T *rettv)
2342{
2343 buf_T *buf = term_get_buf(argvars);
2344 term_T *term;
2345 int row;
2346
2347 rettv->v_type = VAR_STRING;
2348 if (buf == NULL)
2349 return;
2350 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002351 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002352
2353 if (term->tl_vterm == NULL)
2354 {
2355 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2356
2357 /* vterm is finished, get the text from the buffer */
2358 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2359 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2360 }
2361 else
2362 {
2363 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2364 VTermRect rect;
2365 int len;
2366 char_u *p;
2367
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002368 if (row < 0 || row >= term->tl_rows)
2369 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002370 len = term->tl_cols * MB_MAXBYTES + 1;
2371 p = alloc(len);
2372 if (p == NULL)
2373 return;
2374 rettv->vval.v_string = p;
2375
2376 rect.start_col = 0;
2377 rect.end_col = term->tl_cols;
2378 rect.start_row = row;
2379 rect.end_row = row + 1;
2380 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2381 }
2382}
2383
2384/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002385 * "term_getscrolled(buf)" function
2386 */
2387 void
2388f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2389{
2390 buf_T *buf = term_get_buf(argvars);
2391
2392 if (buf == NULL)
2393 return;
2394 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2395}
2396
2397/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002398 * "term_getsize(buf)" function
2399 */
2400 void
2401f_term_getsize(typval_T *argvars, typval_T *rettv)
2402{
2403 buf_T *buf = term_get_buf(argvars);
2404 list_T *l;
2405
2406 if (rettv_list_alloc(rettv) == FAIL)
2407 return;
2408 if (buf == NULL)
2409 return;
2410
2411 l = rettv->vval.v_list;
2412 list_append_number(l, buf->b_term->tl_rows);
2413 list_append_number(l, buf->b_term->tl_cols);
2414}
2415
2416/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002417 * "term_getstatus(buf)" function
2418 */
2419 void
2420f_term_getstatus(typval_T *argvars, typval_T *rettv)
2421{
2422 buf_T *buf = term_get_buf(argvars);
2423 term_T *term;
2424 char_u val[100];
2425
2426 rettv->v_type = VAR_STRING;
2427 if (buf == NULL)
2428 return;
2429 term = buf->b_term;
2430
2431 if (term_job_running(term))
2432 STRCPY(val, "running");
2433 else
2434 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002435 if (term->tl_normal_mode)
2436 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002437 rettv->vval.v_string = vim_strsave(val);
2438}
2439
2440/*
2441 * "term_gettitle(buf)" function
2442 */
2443 void
2444f_term_gettitle(typval_T *argvars, typval_T *rettv)
2445{
2446 buf_T *buf = term_get_buf(argvars);
2447
2448 rettv->v_type = VAR_STRING;
2449 if (buf == NULL)
2450 return;
2451
2452 if (buf->b_term->tl_title != NULL)
2453 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2454}
2455
2456/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002457 * "term_gettty(buf)" function
2458 */
2459 void
2460f_term_gettty(typval_T *argvars, typval_T *rettv)
2461{
2462 buf_T *buf = term_get_buf(argvars);
2463 char_u *p;
2464
2465 rettv->v_type = VAR_STRING;
2466 if (buf == NULL)
2467 return;
2468 if (buf->b_term->tl_job != NULL)
2469 p = buf->b_term->tl_job->jv_tty_name;
2470 else
2471 p = buf->b_term->tl_tty_name;
2472 if (p != NULL)
2473 rettv->vval.v_string = vim_strsave(p);
2474}
2475
2476/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002477 * "term_list()" function
2478 */
2479 void
2480f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2481{
2482 term_T *tp;
2483 list_T *l;
2484
2485 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2486 return;
2487
2488 l = rettv->vval.v_list;
2489 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2490 if (tp != NULL && tp->tl_buffer != NULL)
2491 if (list_append_number(l,
2492 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2493 return;
2494}
2495
2496/*
2497 * "term_scrape(buf, row)" function
2498 */
2499 void
2500f_term_scrape(typval_T *argvars, typval_T *rettv)
2501{
2502 buf_T *buf = term_get_buf(argvars);
2503 VTermScreen *screen = NULL;
2504 VTermPos pos;
2505 list_T *l;
2506 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002507 char_u *p;
2508 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002509
2510 if (rettv_list_alloc(rettv) == FAIL)
2511 return;
2512 if (buf == NULL)
2513 return;
2514 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002515
2516 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002517 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002518
2519 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002520 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002521 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002522 p = NULL;
2523 line = NULL;
2524 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002525 else
2526 {
2527 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2528
2529 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2530 return;
2531 p = ml_get_buf(buf, lnum + 1, FALSE);
2532 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2533 }
2534
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002535 for (pos.col = 0; pos.col < term->tl_cols; )
2536 {
2537 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002538 int width;
2539 VTermScreenCellAttrs attrs;
2540 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002541 char_u rgb[8];
2542 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2543 int off = 0;
2544 int i;
2545
2546 if (screen == NULL)
2547 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002548 cellattr_T *cellattr;
2549 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002550
2551 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002552 if (pos.col >= line->sb_cols)
2553 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002554 cellattr = line->sb_cells + pos.col;
2555 width = cellattr->width;
2556 attrs = cellattr->attrs;
2557 fg = cellattr->fg;
2558 bg = cellattr->bg;
2559 len = MB_PTR2LEN(p);
2560 mch_memmove(mbs, p, len);
2561 mbs[len] = NUL;
2562 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002563 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002564 else
2565 {
2566 VTermScreenCell cell;
2567 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2568 break;
2569 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2570 {
2571 if (cell.chars[i] == 0)
2572 break;
2573 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2574 }
2575 mbs[off] = NUL;
2576 width = cell.width;
2577 attrs = cell.attrs;
2578 fg = cell.fg;
2579 bg = cell.bg;
2580 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002581 dcell = dict_alloc();
2582 list_append_dict(l, dcell);
2583
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002584 dict_add_nr_str(dcell, "chars", 0, mbs);
2585
2586 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002587 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002588 dict_add_nr_str(dcell, "fg", 0, rgb);
2589 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002590 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002591 dict_add_nr_str(dcell, "bg", 0, rgb);
2592
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002593 dict_add_nr_str(dcell, "attr",
2594 cell2attr(attrs, fg, bg), NULL);
2595 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002596
2597 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002598 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002599 ++pos.col;
2600 }
2601}
2602
2603/*
2604 * "term_sendkeys(buf, keys)" function
2605 */
2606 void
2607f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2608{
2609 buf_T *buf = term_get_buf(argvars);
2610 char_u *msg;
2611 term_T *term;
2612
2613 rettv->v_type = VAR_UNKNOWN;
2614 if (buf == NULL)
2615 return;
2616
2617 msg = get_tv_string_chk(&argvars[1]);
2618 if (msg == NULL)
2619 return;
2620 term = buf->b_term;
2621 if (term->tl_vterm == NULL)
2622 return;
2623
2624 while (*msg != NUL)
2625 {
2626 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2627 msg += MB_PTR2LEN(msg);
2628 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002629}
2630
2631/*
2632 * "term_start(command, options)" function
2633 */
2634 void
2635f_term_start(typval_T *argvars, typval_T *rettv)
2636{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002637 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002638
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002639 init_job_options(&opt);
2640 /* TODO: allow more job options */
2641 if (argvars[1].v_type != VAR_UNKNOWN
2642 && get_job_options(&argvars[1], &opt,
2643 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002644 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002645 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002646 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002647 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002648 return;
2649
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002650 if (opt.jo_vertical)
2651 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002652 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002653
2654 if (curbuf->b_term != NULL)
2655 rettv->vval.v_number = curbuf->b_fnum;
2656}
2657
2658/*
2659 * "term_wait" function
2660 */
2661 void
2662f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2663{
2664 buf_T *buf = term_get_buf(argvars);
2665
2666 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002667 {
2668 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002669 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002670 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002671 if (buf->b_term->tl_job == NULL)
2672 {
2673 ch_log(NULL, "term_wait(): no job to wait for");
2674 return;
2675 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002676
2677 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002678 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002679 {
2680 /* The job is dead, keep reading channel I/O until the channel is
2681 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002682 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002683 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2684 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002685 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002686 parse_queued_messages();
2687 ui_delay(10L, FALSE);
2688 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002689 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002690 parse_queued_messages();
2691 }
2692 else
2693 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002694 long wait = 10L;
2695
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002696 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002697 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002698
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002699 /* Wait for some time for any channel I/O. */
2700 if (argvars[1].v_type != VAR_UNKNOWN)
2701 wait = get_tv_number(&argvars[1]);
2702 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002703 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002704
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002705 /* Flushing messages on channels is hopefully sufficient.
2706 * TODO: is there a better way? */
2707 parse_queued_messages();
2708 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002709}
2710
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002711# ifdef WIN3264
2712
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002713/**************************************
2714 * 2. MS-Windows implementation.
2715 */
2716
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002717#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2718#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2719
Bram Moolenaar8a773062017-07-24 22:29:21 +02002720void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002721void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002722void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002723BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2724void (*winpty_config_set_initial_size)(void*, int, int);
2725LPCWSTR (*winpty_conin_name)(void*);
2726LPCWSTR (*winpty_conout_name)(void*);
2727LPCWSTR (*winpty_conerr_name)(void*);
2728void (*winpty_free)(void*);
2729void (*winpty_config_free)(void*);
2730void (*winpty_spawn_config_free)(void*);
2731void (*winpty_error_free)(void*);
2732LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002733BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002734HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002735
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002736#define WINPTY_DLL "winpty.dll"
2737
2738static HINSTANCE hWinPtyDLL = NULL;
2739
2740 int
2741dyn_winpty_init(void)
2742{
2743 int i;
2744 static struct
2745 {
2746 char *name;
2747 FARPROC *ptr;
2748 } winpty_entry[] =
2749 {
2750 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2751 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2752 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2753 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2754 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2755 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2756 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2757 {"winpty_free", (FARPROC*)&winpty_free},
2758 {"winpty_open", (FARPROC*)&winpty_open},
2759 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2760 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2761 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2762 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002763 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002764 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002765 {NULL, NULL}
2766 };
2767
2768 /* No need to initialize twice. */
2769 if (hWinPtyDLL)
2770 return 1;
2771 /* Load winpty.dll */
2772 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2773 if (!hWinPtyDLL)
2774 {
2775 EMSG2(_(e_loadlib), WINPTY_DLL);
2776 return 0;
2777 }
2778 for (i = 0; winpty_entry[i].name != NULL
2779 && winpty_entry[i].ptr != NULL; ++i)
2780 {
2781 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2782 winpty_entry[i].name)) == NULL)
2783 {
2784 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2785 return 0;
2786 }
2787 }
2788
2789 return 1;
2790}
2791
2792/*
2793 * Create a new terminal of "rows" by "cols" cells.
2794 * Store a reference in "term".
2795 * Return OK or FAIL.
2796 */
2797 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002798term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002799{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002800 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002801 channel_T *channel = NULL;
2802 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002803 DWORD error;
2804 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2805 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002806 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002807 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002808 garray_T ga;
2809 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002810
2811 if (!dyn_winpty_init())
2812 return FAIL;
2813
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002814 if (argvar->v_type == VAR_STRING)
2815 cmd = argvar->vval.v_string;
2816 else
2817 {
2818 ga_init2(&ga, (int)sizeof(char*), 20);
2819 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2820 goto failed;
2821 cmd = ga.ga_data;
2822 }
2823
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002824 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002825 if (p == NULL)
2826 return FAIL;
2827
2828 job = job_alloc();
2829 if (job == NULL)
2830 goto failed;
2831
2832 channel = add_channel();
2833 if (channel == NULL)
2834 goto failed;
2835
2836 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2837 if (term->tl_winpty_config == NULL)
2838 goto failed;
2839
2840 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2841 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2842 if (term->tl_winpty == NULL)
2843 goto failed;
2844
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002845 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002846 spawn_config = winpty_spawn_config_new(
2847 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2848 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2849 NULL,
2850 p,
2851 NULL,
2852 NULL,
2853 &winpty_err);
2854 if (spawn_config == NULL)
2855 goto failed;
2856
2857 channel = add_channel();
2858 if (channel == NULL)
2859 goto failed;
2860
2861 job = job_alloc();
2862 if (job == NULL)
2863 goto failed;
2864
2865 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2866 &child_thread_handle, &error, &winpty_err))
2867 goto failed;
2868
2869 channel_set_pipes(channel,
2870 (sock_T) CreateFileW(
2871 winpty_conin_name(term->tl_winpty),
2872 GENERIC_WRITE, 0, NULL,
2873 OPEN_EXISTING, 0, NULL),
2874 (sock_T) CreateFileW(
2875 winpty_conout_name(term->tl_winpty),
2876 GENERIC_READ, 0, NULL,
2877 OPEN_EXISTING, 0, NULL),
2878 (sock_T) CreateFileW(
2879 winpty_conerr_name(term->tl_winpty),
2880 GENERIC_READ, 0, NULL,
2881 OPEN_EXISTING, 0, NULL));
2882
2883 jo = CreateJobObject(NULL, NULL);
2884 if (jo == NULL)
2885 goto failed;
2886
2887 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002888 {
2889 /* Failed, switch the way to terminate process with TerminateProcess. */
2890 CloseHandle(jo);
2891 jo = NULL;
2892 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002893
2894 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002895 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002896
2897 create_vterm(term, rows, cols);
2898
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002899 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002900 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002901
2902 job->jv_channel = channel;
2903 job->jv_proc_info.hProcess = child_process_handle;
2904 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2905 job->jv_job_object = jo;
2906 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002907 sprintf(buf, "winpty://%lu",
2908 GetProcessId(winpty_agent_process(term->tl_winpty)));
2909 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002910 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002911 term->tl_job = job;
2912
2913 return OK;
2914
2915failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002916 if (argvar->v_type == VAR_LIST)
2917 vim_free(ga.ga_data);
2918 if (p != NULL)
2919 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002920 if (spawn_config != NULL)
2921 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002922 if (channel != NULL)
2923 channel_clear(channel);
2924 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002925 {
2926 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002927 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002928 }
2929 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002930 if (jo != NULL)
2931 CloseHandle(jo);
2932 if (term->tl_winpty != NULL)
2933 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002934 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002935 if (term->tl_winpty_config != NULL)
2936 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002937 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002938 if (winpty_err != NULL)
2939 {
2940 char_u *msg = utf16_to_enc(
2941 (short_u *)winpty_error_msg(winpty_err), NULL);
2942
2943 EMSG(msg);
2944 winpty_error_free(winpty_err);
2945 }
2946 return FAIL;
2947}
2948
2949/*
2950 * Free the terminal emulator part of "term".
2951 */
2952 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002953term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002955 if (term->tl_winpty != NULL)
2956 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002957 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002958 if (term->tl_winpty_config != NULL)
2959 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002960 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002961 if (term->tl_vterm != NULL)
2962 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002963 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002964}
2965
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002966/*
2967 * Request size to terminal.
2968 */
2969 static void
2970term_report_winsize(term_T *term, int rows, int cols)
2971{
2972 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2973}
2974
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002975# else
2976
2977/**************************************
2978 * 3. Unix-like implementation.
2979 */
2980
2981/*
2982 * Create a new terminal of "rows" by "cols" cells.
2983 * Start job for "cmd".
2984 * Store the pointers in "term".
2985 * Return OK or FAIL.
2986 */
2987 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002988term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002989{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002990 create_vterm(term, rows, cols);
2991
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002992 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002993 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002994 if (term->tl_job != NULL)
2995 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002996
Bram Moolenaar61a66052017-07-22 18:39:00 +02002997 return term->tl_job != NULL
2998 && term->tl_job->jv_channel != NULL
2999 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003000}
3001
3002/*
3003 * Free the terminal emulator part of "term".
3004 */
3005 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003006term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003008 if (term->tl_vterm != NULL)
3009 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003010 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003011}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003012
3013/*
3014 * Request size to terminal.
3015 */
3016 static void
3017term_report_winsize(term_T *term, int rows, int cols)
3018{
3019 /* Use an ioctl() to report the new window size to the job. */
3020 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3021 {
3022 int fd = -1;
3023 int part;
3024
3025 for (part = PART_OUT; part < PART_COUNT; ++part)
3026 {
3027 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3028 if (isatty(fd))
3029 break;
3030 }
3031 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003032 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003033 }
3034}
3035
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003036# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003037
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003038#endif /* FEAT_TERMINAL */