blob: d4a45fb8cb6c2c8588854eae39c29d239e151c1c [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 Moolenaardcaa6132017-08-13 17:13:09 +0200395 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200397 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200399 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200400
401 if (old_curbuf != NULL)
402 {
403 --curbuf->b_nwindows;
404 curbuf = old_curbuf;
405 curwin->w_buffer = curbuf;
406 ++curbuf->b_nwindows;
407 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200408 }
409 else
410 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200411 buf_T *buf = curbuf;
412
Bram Moolenaard85f2712017-07-28 21:51:57 +0200413 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200414 if (old_curbuf != NULL)
415 {
416 --curbuf->b_nwindows;
417 curbuf = old_curbuf;
418 curwin->w_buffer = curbuf;
419 ++curbuf->b_nwindows;
420 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200421
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200422 /* Wiping out the buffer will also close the window and call
423 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200424 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200425 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200426}
427
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200428/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200429 * ":terminal": open a terminal window and execute a job in it.
430 */
431 void
432ex_terminal(exarg_T *eap)
433{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200434 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200435 jobopt_T opt;
436 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200437
438 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200439
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200440 cmd = eap->arg;
441 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
442 {
443 char_u *p;
444
445 cmd += 2;
446 p = skiptowhite(cmd);
447 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
448 opt.jo_term_finish = 'c';
449 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
450 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200451 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
452 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200453 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
454 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200455 else
456 {
457 if (*p)
458 *p = NUL;
459 EMSG2(_("E181: Invalid attribute: %s"), cmd);
460 return;
461 }
462 cmd = skipwhite(p);
463 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200464 if (cmd == NULL || *cmd == NUL)
465 cmd = p_sh;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200466
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200467 if (eap->addr_count == 2)
468 {
469 opt.jo_term_rows = eap->line1;
470 opt.jo_term_cols = eap->line2;
471 }
472 else if (eap->addr_count == 1)
473 {
474 if (cmdmod.split & WSP_VERT)
475 opt.jo_term_cols = eap->line2;
476 else
477 opt.jo_term_rows = eap->line2;
478 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200479
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200480 argvar.v_type = VAR_STRING;
481 argvar.vval.v_string = cmd;
482 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200483}
484
485/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200486 * Free the scrollback buffer for "term".
487 */
488 static void
489free_scrollback(term_T *term)
490{
491 int i;
492
493 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
494 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
495 ga_clear(&term->tl_scrollback);
496}
497
498/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200499 * Free a terminal and everything it refers to.
500 * Kills the job if there is one.
501 * Called when wiping out a buffer.
502 */
503 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200504free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200505{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200506 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200507 term_T *tp;
508
509 if (term == NULL)
510 return;
511 if (first_term == term)
512 first_term = term->tl_next;
513 else
514 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
515 if (tp->tl_next == term)
516 {
517 tp->tl_next = term->tl_next;
518 break;
519 }
520
521 if (term->tl_job != NULL)
522 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200523 if (term->tl_job->jv_status != JOB_ENDED
524 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200525 job_stop(term->tl_job, NULL, "kill");
526 job_unref(term->tl_job);
527 }
528
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200529 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200530
531 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200532 vim_free(term->tl_title);
533 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200534 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200535 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200536 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200537 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200538 if (in_terminal_loop == term)
539 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200540}
541
542/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200543 * Write job output "msg[len]" to the vterm.
544 */
545 static void
546term_write_job_output(term_T *term, char_u *msg, size_t len)
547{
548 VTerm *vterm = term->tl_vterm;
549 char_u *p;
550 size_t done;
551 size_t len_now;
552
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200553 if (term_nl_does_cr)
554 vterm_input_write(vterm, (char *)msg, len);
555 else
556 /* need to convert NL to CR-NL */
557 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200558 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200559 for (p = msg + done; p < msg + len; )
560 {
561 if (*p == NL)
562 break;
563 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
564 }
565 len_now = p - msg - done;
566 vterm_input_write(vterm, (char *)msg + done, len_now);
567 if (p < msg + len && *p == NL)
568 {
569 vterm_input_write(vterm, "\r\n", 2);
570 ++len_now;
571 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200572 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200573
574 /* this invokes the damage callbacks */
575 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
576}
577
Bram Moolenaar1c844932017-07-24 23:36:41 +0200578 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200579update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200580{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200581 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200582 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200583 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200584 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200585 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200586 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200587 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200588 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200589#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200590 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200591 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200592#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200593 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200594}
595
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200596/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200597 * Invoked when "msg" output from a job was received. Write it to the terminal
598 * of "buffer".
599 */
600 void
601write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
602{
603 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200604 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200605
Bram Moolenaard85f2712017-07-28 21:51:57 +0200606 if (term->tl_vterm == NULL)
607 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200608 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200609 return;
610 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200611 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200612 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200613
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200614 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
615 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200616 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200617 {
618 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200619 ch_log(term->tl_job->jv_channel, "updating screen");
620 if (buffer == curbuf)
621 {
622 update_screen(0);
623 update_cursor(term, TRUE);
624 }
625 else
626 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200627 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200628}
629
630/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200631 * Send a mouse position and click to the vterm
632 */
633 static int
634term_send_mouse(VTerm *vterm, int button, int pressed)
635{
636 VTermModifier mod = VTERM_MOD_NONE;
637
638 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
639 mouse_col - W_WINCOL(curwin), mod);
640 vterm_mouse_button(vterm, button, pressed, mod);
641 return TRUE;
642}
643
644/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200645 * Convert typed key "c" into bytes to send to the job.
646 * Return the number of bytes in "buf".
647 */
648 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200649term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200651 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200652 VTermKey key = VTERM_KEY_NONE;
653 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200654 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200655
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200656 switch (c)
657 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200658 case CAR: c = term_enter_char; break;
659 /* don't use VTERM_KEY_BACKSPACE, it always
660 * becomes 0x7f DEL */
661 case K_BS: c = term_backspace_char; break;
662
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200663 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200664 case K_DEL: key = VTERM_KEY_DEL; break;
665 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200666 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
667 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200668 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200669 case K_S_END: mod = VTERM_MOD_SHIFT;
670 key = VTERM_KEY_END; break;
671 case K_C_END: mod = VTERM_MOD_CTRL;
672 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
674 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
675 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
676 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
677 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
678 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
679 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
680 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
681 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
682 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
683 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
684 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
685 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200686 case K_S_HOME: mod = VTERM_MOD_SHIFT;
687 key = VTERM_KEY_HOME; break;
688 case K_C_HOME: mod = VTERM_MOD_CTRL;
689 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200690 case K_INS: key = VTERM_KEY_INS; break;
691 case K_K0: key = VTERM_KEY_KP_0; break;
692 case K_K1: key = VTERM_KEY_KP_1; break;
693 case K_K2: key = VTERM_KEY_KP_2; break;
694 case K_K3: key = VTERM_KEY_KP_3; break;
695 case K_K4: key = VTERM_KEY_KP_4; break;
696 case K_K5: key = VTERM_KEY_KP_5; break;
697 case K_K6: key = VTERM_KEY_KP_6; break;
698 case K_K7: key = VTERM_KEY_KP_7; break;
699 case K_K8: key = VTERM_KEY_KP_8; break;
700 case K_K9: key = VTERM_KEY_KP_9; break;
701 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
702 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
703 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
704 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
705 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
706 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
707 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
708 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
709 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
710 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
711 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
712 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
713 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200714 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
715 key = VTERM_KEY_LEFT; break;
716 case K_C_LEFT: mod = VTERM_MOD_CTRL;
717 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200718 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
719 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
720 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200721 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
722 key = VTERM_KEY_RIGHT; break;
723 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
724 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200725 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200726 case K_S_UP: mod = VTERM_MOD_SHIFT;
727 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200728 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200729
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200730 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
731 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
732 case K_MOUSELEFT: /* TODO */ return 0;
733 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200734
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200735 case K_LEFTMOUSE:
736 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
737 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
738 case K_LEFTRELEASE:
739 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
740 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
741 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
742 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
743 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
744 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
745 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
746 case K_X1MOUSE: /* TODO */ return 0;
747 case K_X1DRAG: /* TODO */ return 0;
748 case K_X1RELEASE: /* TODO */ return 0;
749 case K_X2MOUSE: /* TODO */ return 0;
750 case K_X2DRAG: /* TODO */ return 0;
751 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200752
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200753 case K_IGNORE: return 0;
754 case K_NOP: return 0;
755 case K_UNDO: return 0;
756 case K_HELP: return 0;
757 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
758 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
759 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
760 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
761 case K_SELECT: return 0;
762#ifdef FEAT_GUI
763 case K_VER_SCROLLBAR: return 0;
764 case K_HOR_SCROLLBAR: return 0;
765#endif
766#ifdef FEAT_GUI_TABLINE
767 case K_TABLINE: return 0;
768 case K_TABMENU: return 0;
769#endif
770#ifdef FEAT_NETBEANS_INTG
771 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
772#endif
773#ifdef FEAT_DND
774 case K_DROP: return 0;
775#endif
776#ifdef FEAT_AUTOCMD
777 case K_CURSORHOLD: return 0;
778#endif
779 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
780 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200781 }
782
783 /*
784 * Convert special keys to vterm keys:
785 * - Write keys to vterm: vterm_keyboard_key()
786 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200787 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200788 */
789 if (key != VTERM_KEY_NONE)
790 /* Special key, let vterm convert it. */
791 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200792 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200793 /* Normal character, let vterm convert it. */
794 vterm_keyboard_unichar(vterm, c, mod);
795
796 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200797 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200798}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200799
Bram Moolenaar938783d2017-07-16 20:13:26 +0200800/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200801 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200802 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200803 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200804term_job_running(term_T *term)
805{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200806 /* Also consider the job finished when the channel is closed, to avoid a
807 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200808 return term != NULL
809 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200810 && term->tl_job->jv_status == JOB_STARTED
811 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200812}
813
814/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200815 * Add the last line of the scrollback buffer to the buffer in the window.
816 */
817 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200818add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200819{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200820 buf_T *buf = term->tl_buffer;
821 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
822 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200823
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200824 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200825 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200826 {
827 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200828 curbuf = buf;
829 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200830 curbuf = curwin->w_buffer;
831 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200832}
833
834/*
835 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200836 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200837 */
838 static void
839move_terminal_to_buffer(term_T *term)
840{
841 win_T *wp;
842 int len;
843 int lines_skipped = 0;
844 VTermPos pos;
845 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200846 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200847 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200848
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200849 if (term->tl_vterm == NULL)
850 return;
851 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200852 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
853 {
854 len = 0;
855 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
856 if (vterm_screen_get_cell(screen, pos, &cell) != 0
857 && cell.chars[0] != NUL)
858 len = pos.col + 1;
859
860 if (len == 0)
861 ++lines_skipped;
862 else
863 {
864 while (lines_skipped > 0)
865 {
866 /* Line was skipped, add an empty line. */
867 --lines_skipped;
868 if (ga_grow(&term->tl_scrollback, 1) == OK)
869 {
870 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
871 + term->tl_scrollback.ga_len;
872
873 line->sb_cols = 0;
874 line->sb_cells = NULL;
875 ++term->tl_scrollback.ga_len;
876
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200877 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200878 }
879 }
880
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200881 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200882 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
883 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200884 garray_T ga;
885 int width;
886 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 + term->tl_scrollback.ga_len;
888
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200889 ga_init2(&ga, 1, 100);
890 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200891 {
892 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200893 {
894 width = 1;
895 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
896 if (ga_grow(&ga, 1) == OK)
897 ga.ga_len += mb_char2bytes(' ',
898 (char_u *)ga.ga_data + ga.ga_len);
899 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200900 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200901 {
902 width = cell.width;
903
904 p[pos.col].width = cell.width;
905 p[pos.col].attrs = cell.attrs;
906 p[pos.col].fg = cell.fg;
907 p[pos.col].bg = cell.bg;
908
909 if (ga_grow(&ga, MB_MAXBYTES) == OK)
910 {
911 int i;
912 int c;
913
914 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
915 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
916 (char_u *)ga.ga_data + ga.ga_len);
917 }
918 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200919 }
920 line->sb_cols = len;
921 line->sb_cells = p;
922 ++term->tl_scrollback.ga_len;
923
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200924 if (ga_grow(&ga, 1) == FAIL)
925 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
926 else
927 {
928 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
929 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
930 }
931 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200932 }
933 else
934 vim_free(p);
935 }
936 }
937
938 FOR_ALL_WINDOWS(wp)
939 {
940 if (wp->w_buffer == term->tl_buffer)
941 {
942 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
943 wp->w_cursor.col = 0;
944 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200945 if (wp->w_cursor.lnum >= wp->w_height)
946 {
947 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
948
949 if (wp->w_topline < min_topline)
950 wp->w_topline = min_topline;
951 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200952 redraw_win_later(wp, NOT_VALID);
953 }
954 }
955}
956
957 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200958set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200959{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200960 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200961 vim_free(term->tl_status_text);
962 term->tl_status_text = NULL;
963 if (term->tl_buffer == curbuf)
964 maketitle();
965}
966
967/*
968 * Called after the job if finished and Terminal mode is not active:
969 * Move the vterm contents into the scrollback buffer and free the vterm.
970 */
971 static void
972cleanup_vterm(term_T *term)
973{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200974 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200975 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200976 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200977 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200978}
979
980/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200981 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200982 * Suspends updating the terminal window.
983 */
984 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200985term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200986{
987 term_T *term = curbuf->b_term;
988
989 /* Append the current terminal contents to the buffer. */
990 move_terminal_to_buffer(term);
991
Bram Moolenaar6d819742017-08-06 14:57:49 +0200992 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200993
Bram Moolenaar6d819742017-08-06 14:57:49 +0200994 /* Move the window cursor to the position of the cursor in the
995 * terminal. */
996 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
997 + term->tl_cursor_pos.row + 1;
998 check_cursor();
999 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001000
Bram Moolenaar6d819742017-08-06 14:57:49 +02001001 /* Display the same lines as in the terminal. */
1002 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001003}
1004
1005/*
1006 * Returns TRUE if the current window contains a terminal and we are in
1007 * Terminal-Normal mode.
1008 */
1009 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001010term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001011{
1012 term_T *term = curbuf->b_term;
1013
Bram Moolenaar6d819742017-08-06 14:57:49 +02001014 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001015}
1016
1017/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001018 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001019 * Restores updating the terminal window.
1020 */
1021 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001022term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001023{
1024 term_T *term = curbuf->b_term;
1025 sb_line_T *line;
1026 garray_T *gap;
1027
1028 /* Remove the terminal contents from the scrollback and the buffer. */
1029 gap = &term->tl_scrollback;
1030 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1031 {
1032 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1033 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1034 vim_free(line->sb_cells);
1035 --gap->ga_len;
1036 if (gap->ga_len == 0)
1037 break;
1038 }
1039 check_cursor();
1040
Bram Moolenaar6d819742017-08-06 14:57:49 +02001041 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001042
1043 if (term->tl_channel_closed)
1044 cleanup_vterm(term);
1045 redraw_buf_and_status_later(curbuf, NOT_VALID);
1046}
1047
1048/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001049 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001050 * Note: while waiting a terminal may be closed and freed if the channel is
1051 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001052 * TODO: use terminal mode mappings.
1053 */
1054 static int
1055term_vgetc()
1056{
1057 int c;
1058
1059 ++no_mapping;
1060 ++allow_keys;
1061 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001062#ifdef WIN3264
1063 ctrl_break_was_pressed = FALSE;
1064#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001065 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001066 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001067 --no_mapping;
1068 --allow_keys;
1069 return c;
1070}
1071
1072/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001073 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001074 * Return FAIL when the key needs to be handled in Normal mode.
1075 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001076 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001077 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001078send_keys_to_term(term_T *term, int c, int typed)
1079{
1080 char msg[KEY_BUF_LEN];
1081 size_t len;
1082 static int mouse_was_outside = FALSE;
1083 int dragging_outside = FALSE;
1084
1085 /* Catch keys that need to be handled as in Normal mode. */
1086 switch (c)
1087 {
1088 case NUL:
1089 case K_ZERO:
1090 if (typed)
1091 stuffcharReadbuff(c);
1092 return FAIL;
1093
1094 case K_IGNORE:
1095 return FAIL;
1096
1097 case K_LEFTDRAG:
1098 case K_MIDDLEDRAG:
1099 case K_RIGHTDRAG:
1100 case K_X1DRAG:
1101 case K_X2DRAG:
1102 dragging_outside = mouse_was_outside;
1103 /* FALLTHROUGH */
1104 case K_LEFTMOUSE:
1105 case K_LEFTMOUSE_NM:
1106 case K_LEFTRELEASE:
1107 case K_LEFTRELEASE_NM:
1108 case K_MIDDLEMOUSE:
1109 case K_MIDDLERELEASE:
1110 case K_RIGHTMOUSE:
1111 case K_RIGHTRELEASE:
1112 case K_X1MOUSE:
1113 case K_X1RELEASE:
1114 case K_X2MOUSE:
1115 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001116
1117 case K_MOUSEUP:
1118 case K_MOUSEDOWN:
1119 case K_MOUSELEFT:
1120 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001121 if (mouse_row < W_WINROW(curwin)
1122 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1123 || mouse_col < W_WINCOL(curwin)
1124 || mouse_col >= W_ENDCOL(curwin)
1125 || dragging_outside)
1126 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001127 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001128 if (typed)
1129 {
1130 stuffcharReadbuff(c);
1131 mouse_was_outside = TRUE;
1132 }
1133 return FAIL;
1134 }
1135 }
1136 if (typed)
1137 mouse_was_outside = FALSE;
1138
1139 /* Convert the typed key to a sequence of bytes for the job. */
1140 len = term_convert_key(term, c, msg);
1141 if (len > 0)
1142 /* TODO: if FAIL is returned, stop? */
1143 channel_send(term->tl_job->jv_channel, PART_IN,
1144 (char_u *)msg, (int)len, NULL);
1145
1146 return OK;
1147}
1148
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001149 static void
1150position_cursor(win_T *wp, VTermPos *pos)
1151{
1152 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1153 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1154 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1155}
1156
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001157/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001158 * Handle CTRL-W "": send register contents to the job.
1159 */
1160 static void
1161term_paste_register(int prev_c UNUSED)
1162{
1163 int c;
1164 list_T *l;
1165 listitem_T *item;
1166 long reglen = 0;
1167 int type;
1168
1169#ifdef FEAT_CMDL_INFO
1170 if (add_to_showcmd(prev_c))
1171 if (add_to_showcmd('"'))
1172 out_flush();
1173#endif
1174 c = term_vgetc();
1175#ifdef FEAT_CMDL_INFO
1176 clear_showcmd();
1177#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001178 if (!term_use_loop())
1179 /* job finished while waiting for a character */
1180 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001181
1182 /* CTRL-W "= prompt for expression to evaluate. */
1183 if (c == '=' && get_expr_register() != '=')
1184 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001185 if (!term_use_loop())
1186 /* job finished while waiting for a character */
1187 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001188
1189 l = (list_T *)get_reg_contents(c, GREG_LIST);
1190 if (l != NULL)
1191 {
1192 type = get_reg_type(c, &reglen);
1193 for (item = l->lv_first; item != NULL; item = item->li_next)
1194 {
1195 char_u *s = get_tv_string(&item->li_tv);
1196
1197 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1198 s, STRLEN(s), NULL);
1199 if (item->li_next != NULL || type == MLINE)
1200 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1201 (char_u *)"\r", 1, NULL);
1202 }
1203 list_free(l);
1204 }
1205}
1206
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001207#if defined(FEAT_GUI) || defined(PROTO)
1208/*
1209 * Return TRUE when the cursor of the terminal should be displayed.
1210 */
1211 int
1212use_terminal_cursor()
1213{
1214 return in_terminal_loop != NULL;
1215}
1216
1217 cursorentry_T *
1218term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1219{
1220 term_T *term = in_terminal_loop;
1221 static cursorentry_T entry;
1222
1223 vim_memset(&entry, 0, sizeof(entry));
1224 entry.shape = entry.mshape =
1225 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1226 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1227 SHAPE_BLOCK;
1228 entry.percentage = 20;
1229 if (term->tl_cursor_blink)
1230 {
1231 entry.blinkwait = 700;
1232 entry.blinkon = 400;
1233 entry.blinkon = 250;
1234 }
1235 *fg = gui.back_pixel;
1236 if (term->tl_cursor_color == NULL)
1237 *bg = gui.norm_pixel;
1238 else
1239 *bg = color_name2handle(term->tl_cursor_color);
1240 entry.name = "n";
1241 entry.used_for = SHAPE_CURSOR;
1242
1243 return &entry;
1244}
1245#endif
1246
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001247static int did_change_cursor = FALSE;
1248
1249 static void
1250may_set_cursor_props(term_T *term)
1251{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001252#ifdef FEAT_GUI
1253 /* For the GUI the cursor properties are obtained with
1254 * term_get_cursor_shape(). */
1255 if (gui.in_use)
1256 return;
1257#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001258 if (in_terminal_loop == term)
1259 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001260 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001261 if (term->tl_cursor_color != NULL)
1262 term_cursor_color(term->tl_cursor_color);
1263 else
1264 term_cursor_color((char_u *)"");
1265 /* do both blink and shape+blink, in case setting shape does not work */
1266 term_cursor_blink(term->tl_cursor_blink);
1267 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1268 }
1269}
1270
1271 static void
1272may_restore_cursor_props(void)
1273{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001274#ifdef FEAT_GUI
1275 if (gui.in_use)
1276 return;
1277#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001278 if (did_change_cursor)
1279 {
1280 did_change_cursor = FALSE;
1281 ui_cursor_shape_forced(TRUE);
1282 term_cursor_color((char_u *)"");
1283 term_cursor_blink(FALSE);
1284 }
1285}
1286
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001287/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001288 * Returns TRUE if the current window contains a terminal and we are sending
1289 * keys to the job.
1290 */
1291 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001292term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001293{
1294 term_T *term = curbuf->b_term;
1295
1296 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001297 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001298 && term->tl_vterm != NULL
1299 && term_job_running(term);
1300}
1301
1302/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001303 * Wait for input and send it to the job.
1304 * Return when the start of a CTRL-W command is typed or anything else that
1305 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001306 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1307 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001308 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001309 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001310terminal_loop(void)
1311{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001312 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001313 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001314 int ret;
1315
Bram Moolenaar679653e2017-08-13 14:13:19 +02001316 /* Remember the terminal we are sending keys to. However, the terminal
1317 * might be closed while waiting for a character, e.g. typing "exit" in a
1318 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1319 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001320 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001321
1322 if (*curwin->w_p_tk != NUL)
1323 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001324 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001325 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001326
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001327#ifdef UNIX
1328 {
1329 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1330
1331 if (isatty(fd))
1332 {
1333 ttyinfo_T info;
1334
1335 /* Get the current backspace and enter characters of the pty. */
1336 if (get_tty_info(fd, &info) == OK)
1337 {
1338 term_backspace_char = info.backspace;
1339 term_enter_char = info.enter;
1340 term_nl_does_cr = info.nl_does_cr;
1341 }
1342 }
1343 }
1344#endif
1345
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001346 for (;;)
1347 {
1348 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001349 /* Repeat redrawing in case a message is received while redrawing. */
1350 while (curwin->w_redr_type != 0)
1351 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001352 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001353
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001354 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001355 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001356 /* job finished while waiting for a character */
1357 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001358 if (c == K_IGNORE)
1359 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001360
Bram Moolenaarfae42832017-08-01 22:24:26 +02001361#ifdef UNIX
1362 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1363#endif
1364#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001365 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001366 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001367 if (ctrl_break_was_pressed)
1368 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001369#endif
1370
Bram Moolenaar69198192017-08-05 14:10:48 +02001371 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001372 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001373 int prev_c = c;
1374
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001375#ifdef FEAT_CMDL_INFO
1376 if (add_to_showcmd(c))
1377 out_flush();
1378#endif
1379 c = term_vgetc();
1380#ifdef FEAT_CMDL_INFO
1381 clear_showcmd();
1382#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001383 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001384 /* job finished while waiting for a character */
1385 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001386
Bram Moolenaar69198192017-08-05 14:10:48 +02001387 if (prev_c == Ctrl_BSL)
1388 {
1389 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001390 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001391 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1392 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001393 ret = FAIL;
1394 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001395 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001396 /* Send both keys to the terminal. */
1397 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1398 }
1399 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001400 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001401 /* "CTRL-W .": send CTRL-W to the job */
1402 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001403 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001404 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001405 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001406 /* CTRL-W N : go to Terminal-Normal mode. */
1407 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001408 ret = FAIL;
1409 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001410 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001411 else if (c == '"')
1412 {
1413 term_paste_register(prev_c);
1414 continue;
1415 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001416 else if (termkey == 0 || c != termkey)
1417 {
1418 stuffcharReadbuff(Ctrl_W);
1419 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001420 ret = OK;
1421 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001422 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001423 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001424 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001425 {
1426 ret = OK;
1427 goto theend;
1428 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001429 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001430 ret = FAIL;
1431
1432theend:
1433 in_terminal_loop = NULL;
1434 may_restore_cursor_props();
1435 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001436}
1437
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001438/*
1439 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001440 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001441 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001442 */
1443 void
1444term_job_ended(job_T *job)
1445{
1446 term_T *term;
1447 int did_one = FALSE;
1448
1449 for (term = first_term; term != NULL; term = term->tl_next)
1450 if (term->tl_job == job)
1451 {
1452 vim_free(term->tl_title);
1453 term->tl_title = NULL;
1454 vim_free(term->tl_status_text);
1455 term->tl_status_text = NULL;
1456 redraw_buf_and_status_later(term->tl_buffer, VALID);
1457 did_one = TRUE;
1458 }
1459 if (did_one)
1460 redraw_statuslines();
1461 if (curbuf->b_term != NULL)
1462 {
1463 if (curbuf->b_term->tl_job == job)
1464 maketitle();
1465 update_cursor(curbuf->b_term, TRUE);
1466 }
1467}
1468
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001469 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001470may_toggle_cursor(term_T *term)
1471{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001472 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001473 {
1474 if (term->tl_cursor_visible)
1475 cursor_on();
1476 else
1477 cursor_off();
1478 }
1479}
1480
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001481 static int
1482handle_damage(VTermRect rect, void *user)
1483{
1484 term_T *term = (term_T *)user;
1485
1486 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1487 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1488 redraw_buf_later(term->tl_buffer, NOT_VALID);
1489 return 1;
1490}
1491
1492 static int
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001493handle_moverect(VTermRect dest, VTermRect src, void *user)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001494{
1495 term_T *term = (term_T *)user;
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001496 win_T *wp;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001497
Bram Moolenaar6bb18a82017-08-13 22:14:17 +02001498 if (dest.start_col == src.start_col
1499 && dest.end_col == src.end_col
1500 && dest.start_row < src.start_row)
1501 FOR_ALL_WINDOWS(wp)
1502 {
1503 if (wp->w_buffer == term->tl_buffer)
1504 /* scrolling up is much more efficient when deleting lines */
1505 win_del_lines(wp, dest.start_row,
1506 src.start_row - dest.start_row, FALSE, FALSE);
1507 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001508 redraw_buf_later(term->tl_buffer, NOT_VALID);
1509 return 1;
1510}
1511
1512 static int
1513handle_movecursor(
1514 VTermPos pos,
1515 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001516 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001517 void *user)
1518{
1519 term_T *term = (term_T *)user;
1520 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001521
1522 term->tl_cursor_pos = pos;
1523 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001524
1525 FOR_ALL_WINDOWS(wp)
1526 {
1527 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001528 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001529 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001530 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001531 {
1532 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001533 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001534 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001535
1536 return 1;
1537}
1538
Bram Moolenaar21554412017-07-24 21:44:43 +02001539 static int
1540handle_settermprop(
1541 VTermProp prop,
1542 VTermValue *value,
1543 void *user)
1544{
1545 term_T *term = (term_T *)user;
1546
1547 switch (prop)
1548 {
1549 case VTERM_PROP_TITLE:
1550 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001551 /* a blank title isn't useful, make it empty, so that "running" is
1552 * displayed */
1553 if (*skipwhite((char_u *)value->string) == NUL)
1554 term->tl_title = NULL;
1555 else
1556 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001557 vim_free(term->tl_status_text);
1558 term->tl_status_text = NULL;
1559 if (term == curbuf->b_term)
1560 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001561 break;
1562
1563 case VTERM_PROP_CURSORVISIBLE:
1564 term->tl_cursor_visible = value->boolean;
1565 may_toggle_cursor(term);
1566 out_flush();
1567 break;
1568
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001569 case VTERM_PROP_CURSORBLINK:
1570 term->tl_cursor_blink = value->boolean;
1571 may_set_cursor_props(term);
1572 break;
1573
1574 case VTERM_PROP_CURSORSHAPE:
1575 term->tl_cursor_shape = value->number;
1576 may_set_cursor_props(term);
1577 break;
1578
1579 case VTERM_PROP_CURSORCOLOR:
1580 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001581 if (*value->string == NUL)
1582 term->tl_cursor_color = NULL;
1583 else
1584 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001585 may_set_cursor_props(term);
1586 break;
1587
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001588 case VTERM_PROP_ALTSCREEN:
1589 /* TODO: do anything else? */
1590 term->tl_using_altscreen = value->boolean;
1591 break;
1592
Bram Moolenaar21554412017-07-24 21:44:43 +02001593 default:
1594 break;
1595 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001596 /* Always return 1, otherwise vterm doesn't store the value internally. */
1597 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001598}
1599
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001600/*
1601 * The job running in the terminal resized the terminal.
1602 */
1603 static int
1604handle_resize(int rows, int cols, void *user)
1605{
1606 term_T *term = (term_T *)user;
1607 win_T *wp;
1608
1609 term->tl_rows = rows;
1610 term->tl_cols = cols;
1611 FOR_ALL_WINDOWS(wp)
1612 {
1613 if (wp->w_buffer == term->tl_buffer)
1614 {
1615 win_setheight_win(rows, wp);
1616 win_setwidth_win(cols, wp);
1617 }
1618 }
1619
1620 redraw_buf_later(term->tl_buffer, NOT_VALID);
1621 return 1;
1622}
1623
Bram Moolenaard85f2712017-07-28 21:51:57 +02001624/*
1625 * Handle a line that is pushed off the top of the screen.
1626 */
1627 static int
1628handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1629{
1630 term_T *term = (term_T *)user;
1631
1632 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001633 if (ga_grow(&term->tl_scrollback, 1) == OK)
1634 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001635 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001636 int len = 0;
1637 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001638 int c;
1639 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001640 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001641 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001642
1643 /* do not store empty cells at the end */
1644 for (i = 0; i < cols; ++i)
1645 if (cells[i].chars[0] != 0)
1646 len = i + 1;
1647
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001648 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001649 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001650 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001651 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001652 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001653 for (col = 0; col < len; col += cells[col].width)
1654 {
1655 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1656 {
1657 ga.ga_len = 0;
1658 break;
1659 }
1660 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1661 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1662 (char_u *)ga.ga_data + ga.ga_len);
1663 p[col].width = cells[col].width;
1664 p[col].attrs = cells[col].attrs;
1665 p[col].fg = cells[col].fg;
1666 p[col].bg = cells[col].bg;
1667 }
1668 }
1669 if (ga_grow(&ga, 1) == FAIL)
1670 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1671 else
1672 {
1673 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1674 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1675 }
1676 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001677
1678 line = (sb_line_T *)term->tl_scrollback.ga_data
1679 + term->tl_scrollback.ga_len;
1680 line->sb_cols = len;
1681 line->sb_cells = p;
1682 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001683 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001684 }
1685 return 0; /* ignored */
1686}
1687
Bram Moolenaar21554412017-07-24 21:44:43 +02001688static VTermScreenCallbacks screen_callbacks = {
1689 handle_damage, /* damage */
1690 handle_moverect, /* moverect */
1691 handle_movecursor, /* movecursor */
1692 handle_settermprop, /* settermprop */
1693 NULL, /* bell */
1694 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001695 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001696 NULL /* sb_popline */
1697};
1698
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001699/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001700 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001701 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001702 */
1703 void
1704term_channel_closed(channel_T *ch)
1705{
1706 term_T *term;
1707 int did_one = FALSE;
1708
1709 for (term = first_term; term != NULL; term = term->tl_next)
1710 if (term->tl_job == ch->ch_job)
1711 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001712 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001713 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001714
Bram Moolenaard85f2712017-07-28 21:51:57 +02001715 vim_free(term->tl_title);
1716 term->tl_title = NULL;
1717 vim_free(term->tl_status_text);
1718 term->tl_status_text = NULL;
1719
Bram Moolenaar423802d2017-07-30 16:52:24 +02001720 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001721 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001722 {
1723 int fnum = term->tl_buffer->b_fnum;
1724
Bram Moolenaar423802d2017-07-30 16:52:24 +02001725 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001726
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001727 if (term->tl_finish == 'c')
1728 {
1729 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001730 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001731 curbuf = term->tl_buffer;
1732 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1733 break;
1734 }
1735 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1736 {
1737 char buf[50];
1738
1739 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001740 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001741 vim_snprintf(buf, sizeof(buf),
1742 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001743 ? "botright sbuf %d"
1744 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001745 do_cmdline_cmd((char_u *)buf);
1746 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001747 else
1748 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001749 }
1750
Bram Moolenaard85f2712017-07-28 21:51:57 +02001751 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001752 }
1753 if (did_one)
1754 {
1755 redraw_statuslines();
1756
1757 /* Need to break out of vgetc(). */
1758 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001759 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001760
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001761 term = curbuf->b_term;
1762 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001763 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001764 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001765 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001766 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001767 }
1768 }
1769}
1770
1771/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001772 * Reverse engineer the RGB value into a cterm color index.
1773 * First color is 1. Return 0 if no match found.
1774 */
1775 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001776color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001777{
1778 int red = color->red;
1779 int blue = color->blue;
1780 int green = color->green;
1781
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001782 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001783 if (red == 0)
1784 {
1785 if (green == 0)
1786 {
1787 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001788 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001789 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001790 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001791 }
1792 else if (green == 224)
1793 {
1794 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001795 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001796 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001797 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001798 }
1799 }
1800 else if (red == 224)
1801 {
1802 if (green == 0)
1803 {
1804 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001805 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001806 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001807 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001808 }
1809 else if (green == 224)
1810 {
1811 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001812 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001813 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001814 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001815 }
1816 }
1817 else if (red == 128)
1818 {
1819 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001820 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001821 }
1822 else if (red == 255)
1823 {
1824 if (green == 64)
1825 {
1826 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001827 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001828 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001829 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001830 }
1831 else if (green == 255)
1832 {
1833 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001834 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001835 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001836 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001837 }
1838 }
1839 else if (red == 64)
1840 {
1841 if (green == 64)
1842 {
1843 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001844 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001845 }
1846 else if (green == 255)
1847 {
1848 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001849 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001850 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001851 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001852 }
1853 }
1854 if (t_colors >= 256)
1855 {
1856 if (red == blue && red == green)
1857 {
1858 /* 24-color greyscale */
1859 static int cutoff[23] = {
1860 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1861 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1862 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1863 int i;
1864
1865 for (i = 0; i < 23; ++i)
1866 if (red < cutoff[i])
1867 return i + 233;
1868 return 256;
1869 }
1870
1871 /* 216-color cube */
1872 return 17 + ((red + 25) / 0x33) * 36
1873 + ((green + 25) / 0x33) * 6
1874 + (blue + 25) / 0x33;
1875 }
1876 return 0;
1877}
1878
1879/*
1880 * Convert the attributes of a vterm cell into an attribute index.
1881 */
1882 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001883cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001884{
1885 int attr = 0;
1886
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001887 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001888 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001889 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001890 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001891 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001892 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001893 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001894 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001895 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001896 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001897
1898#ifdef FEAT_GUI
1899 if (gui.in_use)
1900 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001901 guicolor_T fg, bg;
1902
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001903 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1904 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001905 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001906 }
1907 else
1908#endif
1909#ifdef FEAT_TERMGUICOLORS
1910 if (p_tgc)
1911 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001912 guicolor_T fg, bg;
1913
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001914 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1915 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001916
1917 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001918 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001919 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001920#endif
1921 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001922 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001923 int fg = color2index(&cellfg, TRUE, &bold);
1924 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001925
1926 /* with 8 colors set the bold attribute to get a bright foreground */
1927 if (bold == TRUE)
1928 attr |= HL_BOLD;
1929 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001930 }
1931 return 0;
1932}
1933
1934/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001935 * Called to update a window that contains an active terminal.
1936 * Returns FAIL when there is no terminal running in this window or in
1937 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001938 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001939 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001940term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001941{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001942 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001943 VTerm *vterm;
1944 VTermScreen *screen;
1945 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001946 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001947
Bram Moolenaar6d819742017-08-06 14:57:49 +02001948 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001949 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001950
Bram Moolenaard85f2712017-07-28 21:51:57 +02001951 vterm = term->tl_vterm;
1952 screen = vterm_obtain_screen(vterm);
1953 state = vterm_obtain_state(vterm);
1954
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001955 /*
1956 * If the window was resized a redraw will be triggered and we get here.
1957 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1958 */
1959 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1960 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001961 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001962 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1963 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1964 win_T *twp;
1965
1966 FOR_ALL_WINDOWS(twp)
1967 {
1968 /* When more than one window shows the same terminal, use the
1969 * smallest size. */
1970 if (twp->w_buffer == term->tl_buffer)
1971 {
1972 if (!term->tl_rows_fixed && rows > twp->w_height)
1973 rows = twp->w_height;
1974 if (!term->tl_cols_fixed && cols > twp->w_width)
1975 cols = twp->w_width;
1976 }
1977 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001978
1979 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001980 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001981 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001982 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001983 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001984
1985 /* The cursor may have been moved when resizing. */
1986 vterm_state_get_cursorpos(state, &pos);
1987 position_cursor(wp, &pos);
1988
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001989 /* TODO: Only redraw what changed. */
1990 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001991 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001992 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001993 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001994
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001995 if (pos.row < term->tl_rows)
1996 {
1997 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001998 {
1999 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002000 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002001
Bram Moolenaareeac6772017-07-23 15:48:37 +02002002 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2003 vim_memset(&cell, 0, sizeof(cell));
2004
2005 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002006 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002007 if (c == NUL)
2008 {
2009 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002010#if defined(FEAT_MBYTE)
2011 if (enc_utf8)
2012 ScreenLinesUC[off] = NUL;
2013#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002014 }
2015 else
2016 {
2017#if defined(FEAT_MBYTE)
2018 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002019 {
2020 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002021 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002022 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002023 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002024 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002025 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002026 if (enc_utf8)
2027 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002028 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002029#else
2030 ScreenLines[off] = c;
2031#endif
2032 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002033 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002034
2035 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002036 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002037 if (cell.width == 2)
2038 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002039 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002040#if defined(FEAT_MBYTE)
2041 if (enc_utf8)
2042 ScreenLinesUC[off] = NUL;
2043#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002044 ++pos.col;
2045 ++off;
2046 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002047 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002048 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002049 else
2050 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002051
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002052 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2053 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002054 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002055
2056 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002057}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002058
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002059/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002060 * Return TRUE if "wp" is a terminal window where the job has finished.
2061 */
2062 int
2063term_is_finished(buf_T *buf)
2064{
2065 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2066}
2067
2068/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002069 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002070 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002071 */
2072 int
2073term_show_buffer(buf_T *buf)
2074{
2075 term_T *term = buf->b_term;
2076
Bram Moolenaar6d819742017-08-06 14:57:49 +02002077 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002078}
2079
2080/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002081 * The current buffer is going to be changed. If there is terminal
2082 * highlighting remove it now.
2083 */
2084 void
2085term_change_in_curbuf(void)
2086{
2087 term_T *term = curbuf->b_term;
2088
2089 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2090 {
2091 free_scrollback(term);
2092 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002093
2094 /* The buffer is now like a normal buffer, it cannot be easily
2095 * abandoned when changed. */
2096 set_string_option_direct((char_u *)"buftype", -1,
2097 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002098 }
2099}
2100
2101/*
2102 * Get the screen attribute for a position in the buffer.
2103 */
2104 int
2105term_get_attr(buf_T *buf, linenr_T lnum, int col)
2106{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002107 term_T *term = buf->b_term;
2108 sb_line_T *line;
2109 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002110
Bram Moolenaar70229f92017-07-29 16:01:53 +02002111 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002112 return 0;
2113 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2114 if (col >= line->sb_cols)
2115 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002116 cellattr = line->sb_cells + col;
2117 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002118}
2119
2120/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002121 * Create a new vterm and initialize it.
2122 */
2123 static void
2124create_vterm(term_T *term, int rows, int cols)
2125{
2126 VTerm *vterm;
2127 VTermScreen *screen;
2128
2129 vterm = vterm_new(rows, cols);
2130 term->tl_vterm = vterm;
2131 screen = vterm_obtain_screen(vterm);
2132 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2133 /* TODO: depends on 'encoding'. */
2134 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002135
2136 /* Vterm uses a default black background. Set it to white when
2137 * 'background' is "light". */
2138 if (*p_bg == 'l')
2139 {
2140 VTermColor fg, bg;
2141
2142 fg.red = fg.green = fg.blue = 0;
2143 bg.red = bg.green = bg.blue = 255;
2144 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2145 }
2146
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147 /* Required to initialize most things. */
2148 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002149
2150 /* Allow using alternate screen. */
2151 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002152}
2153
Bram Moolenaar21554412017-07-24 21:44:43 +02002154/*
2155 * Return the text to show for the buffer name and status.
2156 */
2157 char_u *
2158term_get_status_text(term_T *term)
2159{
2160 if (term->tl_status_text == NULL)
2161 {
2162 char_u *txt;
2163 size_t len;
2164
Bram Moolenaar6d819742017-08-06 14:57:49 +02002165 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002166 {
2167 if (term_job_running(term))
2168 txt = (char_u *)_("Terminal");
2169 else
2170 txt = (char_u *)_("Terminal-finished");
2171 }
2172 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002173 txt = term->tl_title;
2174 else if (term_job_running(term))
2175 txt = (char_u *)_("running");
2176 else
2177 txt = (char_u *)_("finished");
2178 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002179 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002180 if (term->tl_status_text != NULL)
2181 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2182 term->tl_buffer->b_fname, txt);
2183 }
2184 return term->tl_status_text;
2185}
2186
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002187/*
2188 * Mark references in jobs of terminals.
2189 */
2190 int
2191set_ref_in_term(int copyID)
2192{
2193 int abort = FALSE;
2194 term_T *term;
2195 typval_T tv;
2196
2197 for (term = first_term; term != NULL; term = term->tl_next)
2198 if (term->tl_job != NULL)
2199 {
2200 tv.v_type = VAR_JOB;
2201 tv.vval.v_job = term->tl_job;
2202 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2203 }
2204 return abort;
2205}
2206
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002207/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002208 * Get the buffer from the first argument in "argvars".
2209 * Returns NULL when the buffer is not for a terminal window.
2210 */
2211 static buf_T *
2212term_get_buf(typval_T *argvars)
2213{
2214 buf_T *buf;
2215
2216 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2217 ++emsg_off;
2218 buf = get_buf_tv(&argvars[0], FALSE);
2219 --emsg_off;
2220 if (buf == NULL || buf->b_term == NULL)
2221 return NULL;
2222 return buf;
2223}
2224
2225/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002226 * "term_getaltscreen(buf)" function
2227 */
2228 void
2229f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2230{
2231 buf_T *buf = term_get_buf(argvars);
2232
2233 if (buf == NULL)
2234 return;
2235 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2236}
2237
2238/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002239 * "term_getattr(attr, name)" function
2240 */
2241 void
2242f_term_getattr(typval_T *argvars, typval_T *rettv)
2243{
2244 int attr;
2245 size_t i;
2246 char_u *name;
2247
2248 static struct {
2249 char *name;
2250 int attr;
2251 } attrs[] = {
2252 {"bold", HL_BOLD},
2253 {"italic", HL_ITALIC},
2254 {"underline", HL_UNDERLINE},
2255 {"strike", HL_STANDOUT},
2256 {"reverse", HL_INVERSE},
2257 };
2258
2259 attr = get_tv_number(&argvars[0]);
2260 name = get_tv_string_chk(&argvars[1]);
2261 if (name == NULL)
2262 return;
2263
2264 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2265 if (STRCMP(name, attrs[i].name) == 0)
2266 {
2267 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2268 break;
2269 }
2270}
2271
2272/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002273 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002274 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002275 void
2276f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002277{
Bram Moolenaar97870002017-07-30 18:28:38 +02002278 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002279 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002280 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002281 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002282
Bram Moolenaar97870002017-07-30 18:28:38 +02002283 if (rettv_list_alloc(rettv) == FAIL)
2284 return;
2285 if (buf == NULL)
2286 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002287 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002288
2289 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002290 list_append_number(l, term->tl_cursor_pos.row + 1);
2291 list_append_number(l, term->tl_cursor_pos.col + 1);
2292
2293 d = dict_alloc();
2294 if (d != NULL)
2295 {
2296 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2297 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2298 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2299 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2300 ? (char_u *)"" : term->tl_cursor_color);
2301 list_append_dict(l, d);
2302 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002303}
2304
2305/*
2306 * "term_getjob(buf)" function
2307 */
2308 void
2309f_term_getjob(typval_T *argvars, typval_T *rettv)
2310{
2311 buf_T *buf = term_get_buf(argvars);
2312
2313 rettv->v_type = VAR_JOB;
2314 rettv->vval.v_job = NULL;
2315 if (buf == NULL)
2316 return;
2317
2318 rettv->vval.v_job = buf->b_term->tl_job;
2319 if (rettv->vval.v_job != NULL)
2320 ++rettv->vval.v_job->jv_refcount;
2321}
2322
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002323 static int
2324get_row_number(typval_T *tv, term_T *term)
2325{
2326 if (tv->v_type == VAR_STRING
2327 && tv->vval.v_string != NULL
2328 && STRCMP(tv->vval.v_string, ".") == 0)
2329 return term->tl_cursor_pos.row;
2330 return (int)get_tv_number(tv) - 1;
2331}
2332
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002333/*
2334 * "term_getline(buf, row)" function
2335 */
2336 void
2337f_term_getline(typval_T *argvars, typval_T *rettv)
2338{
2339 buf_T *buf = term_get_buf(argvars);
2340 term_T *term;
2341 int row;
2342
2343 rettv->v_type = VAR_STRING;
2344 if (buf == NULL)
2345 return;
2346 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002347 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002348
2349 if (term->tl_vterm == NULL)
2350 {
2351 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2352
2353 /* vterm is finished, get the text from the buffer */
2354 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2355 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2356 }
2357 else
2358 {
2359 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2360 VTermRect rect;
2361 int len;
2362 char_u *p;
2363
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002364 if (row < 0 || row >= term->tl_rows)
2365 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002366 len = term->tl_cols * MB_MAXBYTES + 1;
2367 p = alloc(len);
2368 if (p == NULL)
2369 return;
2370 rettv->vval.v_string = p;
2371
2372 rect.start_col = 0;
2373 rect.end_col = term->tl_cols;
2374 rect.start_row = row;
2375 rect.end_row = row + 1;
2376 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2377 }
2378}
2379
2380/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002381 * "term_getscrolled(buf)" function
2382 */
2383 void
2384f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2385{
2386 buf_T *buf = term_get_buf(argvars);
2387
2388 if (buf == NULL)
2389 return;
2390 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2391}
2392
2393/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002394 * "term_getsize(buf)" function
2395 */
2396 void
2397f_term_getsize(typval_T *argvars, typval_T *rettv)
2398{
2399 buf_T *buf = term_get_buf(argvars);
2400 list_T *l;
2401
2402 if (rettv_list_alloc(rettv) == FAIL)
2403 return;
2404 if (buf == NULL)
2405 return;
2406
2407 l = rettv->vval.v_list;
2408 list_append_number(l, buf->b_term->tl_rows);
2409 list_append_number(l, buf->b_term->tl_cols);
2410}
2411
2412/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002413 * "term_getstatus(buf)" function
2414 */
2415 void
2416f_term_getstatus(typval_T *argvars, typval_T *rettv)
2417{
2418 buf_T *buf = term_get_buf(argvars);
2419 term_T *term;
2420 char_u val[100];
2421
2422 rettv->v_type = VAR_STRING;
2423 if (buf == NULL)
2424 return;
2425 term = buf->b_term;
2426
2427 if (term_job_running(term))
2428 STRCPY(val, "running");
2429 else
2430 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002431 if (term->tl_normal_mode)
2432 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002433 rettv->vval.v_string = vim_strsave(val);
2434}
2435
2436/*
2437 * "term_gettitle(buf)" function
2438 */
2439 void
2440f_term_gettitle(typval_T *argvars, typval_T *rettv)
2441{
2442 buf_T *buf = term_get_buf(argvars);
2443
2444 rettv->v_type = VAR_STRING;
2445 if (buf == NULL)
2446 return;
2447
2448 if (buf->b_term->tl_title != NULL)
2449 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2450}
2451
2452/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002453 * "term_gettty(buf)" function
2454 */
2455 void
2456f_term_gettty(typval_T *argvars, typval_T *rettv)
2457{
2458 buf_T *buf = term_get_buf(argvars);
2459 char_u *p;
2460
2461 rettv->v_type = VAR_STRING;
2462 if (buf == NULL)
2463 return;
2464 if (buf->b_term->tl_job != NULL)
2465 p = buf->b_term->tl_job->jv_tty_name;
2466 else
2467 p = buf->b_term->tl_tty_name;
2468 if (p != NULL)
2469 rettv->vval.v_string = vim_strsave(p);
2470}
2471
2472/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002473 * "term_list()" function
2474 */
2475 void
2476f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2477{
2478 term_T *tp;
2479 list_T *l;
2480
2481 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2482 return;
2483
2484 l = rettv->vval.v_list;
2485 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2486 if (tp != NULL && tp->tl_buffer != NULL)
2487 if (list_append_number(l,
2488 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2489 return;
2490}
2491
2492/*
2493 * "term_scrape(buf, row)" function
2494 */
2495 void
2496f_term_scrape(typval_T *argvars, typval_T *rettv)
2497{
2498 buf_T *buf = term_get_buf(argvars);
2499 VTermScreen *screen = NULL;
2500 VTermPos pos;
2501 list_T *l;
2502 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002503 char_u *p;
2504 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002505
2506 if (rettv_list_alloc(rettv) == FAIL)
2507 return;
2508 if (buf == NULL)
2509 return;
2510 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002511
2512 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002513 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002514
2515 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002516 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002517 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002518 p = NULL;
2519 line = NULL;
2520 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002521 else
2522 {
2523 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2524
2525 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2526 return;
2527 p = ml_get_buf(buf, lnum + 1, FALSE);
2528 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2529 }
2530
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002531 for (pos.col = 0; pos.col < term->tl_cols; )
2532 {
2533 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002534 int width;
2535 VTermScreenCellAttrs attrs;
2536 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002537 char_u rgb[8];
2538 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2539 int off = 0;
2540 int i;
2541
2542 if (screen == NULL)
2543 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002544 cellattr_T *cellattr;
2545 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002546
2547 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002548 if (pos.col >= line->sb_cols)
2549 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002550 cellattr = line->sb_cells + pos.col;
2551 width = cellattr->width;
2552 attrs = cellattr->attrs;
2553 fg = cellattr->fg;
2554 bg = cellattr->bg;
2555 len = MB_PTR2LEN(p);
2556 mch_memmove(mbs, p, len);
2557 mbs[len] = NUL;
2558 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002559 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002560 else
2561 {
2562 VTermScreenCell cell;
2563 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2564 break;
2565 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2566 {
2567 if (cell.chars[i] == 0)
2568 break;
2569 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2570 }
2571 mbs[off] = NUL;
2572 width = cell.width;
2573 attrs = cell.attrs;
2574 fg = cell.fg;
2575 bg = cell.bg;
2576 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002577 dcell = dict_alloc();
2578 list_append_dict(l, dcell);
2579
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002580 dict_add_nr_str(dcell, "chars", 0, mbs);
2581
2582 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002583 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002584 dict_add_nr_str(dcell, "fg", 0, rgb);
2585 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002586 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002587 dict_add_nr_str(dcell, "bg", 0, rgb);
2588
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002589 dict_add_nr_str(dcell, "attr",
2590 cell2attr(attrs, fg, bg), NULL);
2591 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002592
2593 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002594 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002595 ++pos.col;
2596 }
2597}
2598
2599/*
2600 * "term_sendkeys(buf, keys)" function
2601 */
2602 void
2603f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2604{
2605 buf_T *buf = term_get_buf(argvars);
2606 char_u *msg;
2607 term_T *term;
2608
2609 rettv->v_type = VAR_UNKNOWN;
2610 if (buf == NULL)
2611 return;
2612
2613 msg = get_tv_string_chk(&argvars[1]);
2614 if (msg == NULL)
2615 return;
2616 term = buf->b_term;
2617 if (term->tl_vterm == NULL)
2618 return;
2619
2620 while (*msg != NUL)
2621 {
2622 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2623 msg += MB_PTR2LEN(msg);
2624 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002625}
2626
2627/*
2628 * "term_start(command, options)" function
2629 */
2630 void
2631f_term_start(typval_T *argvars, typval_T *rettv)
2632{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002633 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002634
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002635 init_job_options(&opt);
2636 /* TODO: allow more job options */
2637 if (argvars[1].v_type != VAR_UNKNOWN
2638 && get_job_options(&argvars[1], &opt,
2639 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002640 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002641 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002642 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002643 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002644 return;
2645
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002646 if (opt.jo_vertical)
2647 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002648 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002649
2650 if (curbuf->b_term != NULL)
2651 rettv->vval.v_number = curbuf->b_fnum;
2652}
2653
2654/*
2655 * "term_wait" function
2656 */
2657 void
2658f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2659{
2660 buf_T *buf = term_get_buf(argvars);
2661
2662 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002663 {
2664 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002665 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002666 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002667 if (buf->b_term->tl_job == NULL)
2668 {
2669 ch_log(NULL, "term_wait(): no job to wait for");
2670 return;
2671 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002672
2673 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002674 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002675 {
2676 /* The job is dead, keep reading channel I/O until the channel is
2677 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002678 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002679 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2680 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002681 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002682 parse_queued_messages();
2683 ui_delay(10L, FALSE);
2684 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002685 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002686 parse_queued_messages();
2687 }
2688 else
2689 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002690 long wait = 10L;
2691
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002692 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002693 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002694
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002695 /* Wait for some time for any channel I/O. */
2696 if (argvars[1].v_type != VAR_UNKNOWN)
2697 wait = get_tv_number(&argvars[1]);
2698 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002699 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002700
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002701 /* Flushing messages on channels is hopefully sufficient.
2702 * TODO: is there a better way? */
2703 parse_queued_messages();
2704 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002705}
2706
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002707# ifdef WIN3264
2708
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002709/**************************************
2710 * 2. MS-Windows implementation.
2711 */
2712
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002713#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2714#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2715
Bram Moolenaar8a773062017-07-24 22:29:21 +02002716void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002717void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002718void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002719BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2720void (*winpty_config_set_initial_size)(void*, int, int);
2721LPCWSTR (*winpty_conin_name)(void*);
2722LPCWSTR (*winpty_conout_name)(void*);
2723LPCWSTR (*winpty_conerr_name)(void*);
2724void (*winpty_free)(void*);
2725void (*winpty_config_free)(void*);
2726void (*winpty_spawn_config_free)(void*);
2727void (*winpty_error_free)(void*);
2728LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002729BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002730HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002731
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002732#define WINPTY_DLL "winpty.dll"
2733
2734static HINSTANCE hWinPtyDLL = NULL;
2735
2736 int
2737dyn_winpty_init(void)
2738{
2739 int i;
2740 static struct
2741 {
2742 char *name;
2743 FARPROC *ptr;
2744 } winpty_entry[] =
2745 {
2746 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2747 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2748 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2749 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2750 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2751 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2752 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2753 {"winpty_free", (FARPROC*)&winpty_free},
2754 {"winpty_open", (FARPROC*)&winpty_open},
2755 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2756 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2757 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2758 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002759 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002760 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002761 {NULL, NULL}
2762 };
2763
2764 /* No need to initialize twice. */
2765 if (hWinPtyDLL)
2766 return 1;
2767 /* Load winpty.dll */
2768 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2769 if (!hWinPtyDLL)
2770 {
2771 EMSG2(_(e_loadlib), WINPTY_DLL);
2772 return 0;
2773 }
2774 for (i = 0; winpty_entry[i].name != NULL
2775 && winpty_entry[i].ptr != NULL; ++i)
2776 {
2777 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2778 winpty_entry[i].name)) == NULL)
2779 {
2780 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2781 return 0;
2782 }
2783 }
2784
2785 return 1;
2786}
2787
2788/*
2789 * Create a new terminal of "rows" by "cols" cells.
2790 * Store a reference in "term".
2791 * Return OK or FAIL.
2792 */
2793 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002794term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002795{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002796 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002797 channel_T *channel = NULL;
2798 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002799 DWORD error;
2800 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2801 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002802 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002803 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002804 garray_T ga;
2805 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002806
2807 if (!dyn_winpty_init())
2808 return FAIL;
2809
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002810 if (argvar->v_type == VAR_STRING)
2811 cmd = argvar->vval.v_string;
2812 else
2813 {
2814 ga_init2(&ga, (int)sizeof(char*), 20);
2815 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2816 goto failed;
2817 cmd = ga.ga_data;
2818 }
2819
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002820 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002821 if (p == NULL)
2822 return FAIL;
2823
2824 job = job_alloc();
2825 if (job == NULL)
2826 goto failed;
2827
2828 channel = add_channel();
2829 if (channel == NULL)
2830 goto failed;
2831
2832 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2833 if (term->tl_winpty_config == NULL)
2834 goto failed;
2835
2836 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2837 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2838 if (term->tl_winpty == NULL)
2839 goto failed;
2840
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002841 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002842 spawn_config = winpty_spawn_config_new(
2843 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2844 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2845 NULL,
2846 p,
2847 NULL,
2848 NULL,
2849 &winpty_err);
2850 if (spawn_config == NULL)
2851 goto failed;
2852
2853 channel = add_channel();
2854 if (channel == NULL)
2855 goto failed;
2856
2857 job = job_alloc();
2858 if (job == NULL)
2859 goto failed;
2860
2861 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2862 &child_thread_handle, &error, &winpty_err))
2863 goto failed;
2864
2865 channel_set_pipes(channel,
2866 (sock_T) CreateFileW(
2867 winpty_conin_name(term->tl_winpty),
2868 GENERIC_WRITE, 0, NULL,
2869 OPEN_EXISTING, 0, NULL),
2870 (sock_T) CreateFileW(
2871 winpty_conout_name(term->tl_winpty),
2872 GENERIC_READ, 0, NULL,
2873 OPEN_EXISTING, 0, NULL),
2874 (sock_T) CreateFileW(
2875 winpty_conerr_name(term->tl_winpty),
2876 GENERIC_READ, 0, NULL,
2877 OPEN_EXISTING, 0, NULL));
2878
2879 jo = CreateJobObject(NULL, NULL);
2880 if (jo == NULL)
2881 goto failed;
2882
2883 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002884 {
2885 /* Failed, switch the way to terminate process with TerminateProcess. */
2886 CloseHandle(jo);
2887 jo = NULL;
2888 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002889
2890 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002891 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002892
2893 create_vterm(term, rows, cols);
2894
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002895 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002896 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002897
2898 job->jv_channel = channel;
2899 job->jv_proc_info.hProcess = child_process_handle;
2900 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2901 job->jv_job_object = jo;
2902 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002903 sprintf(buf, "winpty://%lu",
2904 GetProcessId(winpty_agent_process(term->tl_winpty)));
2905 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002906 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002907 term->tl_job = job;
2908
2909 return OK;
2910
2911failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002912 if (argvar->v_type == VAR_LIST)
2913 vim_free(ga.ga_data);
2914 if (p != NULL)
2915 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002916 if (spawn_config != NULL)
2917 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918 if (channel != NULL)
2919 channel_clear(channel);
2920 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002921 {
2922 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002923 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002924 }
2925 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002926 if (jo != NULL)
2927 CloseHandle(jo);
2928 if (term->tl_winpty != NULL)
2929 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002930 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002931 if (term->tl_winpty_config != NULL)
2932 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002933 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934 if (winpty_err != NULL)
2935 {
2936 char_u *msg = utf16_to_enc(
2937 (short_u *)winpty_error_msg(winpty_err), NULL);
2938
2939 EMSG(msg);
2940 winpty_error_free(winpty_err);
2941 }
2942 return FAIL;
2943}
2944
2945/*
2946 * Free the terminal emulator part of "term".
2947 */
2948 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002949term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002950{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002951 if (term->tl_winpty != NULL)
2952 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002953 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002954 if (term->tl_winpty_config != NULL)
2955 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002956 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002957 if (term->tl_vterm != NULL)
2958 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002959 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002960}
2961
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002962/*
2963 * Request size to terminal.
2964 */
2965 static void
2966term_report_winsize(term_T *term, int rows, int cols)
2967{
2968 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2969}
2970
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002971# else
2972
2973/**************************************
2974 * 3. Unix-like implementation.
2975 */
2976
2977/*
2978 * Create a new terminal of "rows" by "cols" cells.
2979 * Start job for "cmd".
2980 * Store the pointers in "term".
2981 * Return OK or FAIL.
2982 */
2983 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002984term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002985{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002986 create_vterm(term, rows, cols);
2987
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002988 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002989 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002990 if (term->tl_job != NULL)
2991 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002992
Bram Moolenaar61a66052017-07-22 18:39:00 +02002993 return term->tl_job != NULL
2994 && term->tl_job->jv_channel != NULL
2995 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002996}
2997
2998/*
2999 * Free the terminal emulator part of "term".
3000 */
3001 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02003002term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003003{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02003004 if (term->tl_vterm != NULL)
3005 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003006 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003007}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003008
3009/*
3010 * Request size to terminal.
3011 */
3012 static void
3013term_report_winsize(term_T *term, int rows, int cols)
3014{
3015 /* Use an ioctl() to report the new window size to the job. */
3016 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3017 {
3018 int fd = -1;
3019 int part;
3020
3021 for (part = PART_OUT; part < PART_COUNT; ++part)
3022 {
3023 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3024 if (isatty(fd))
3025 break;
3026 }
3027 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003028 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003029 }
3030}
3031
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003032# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003033
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003034#endif /* FEAT_TERMINAL */