blob: 3171936b1968eb83753b1c4eda9729c0601d5b52 [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 Moolenaar21554412017-07-24 21:44:43 +020041 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020042 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020043 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020044 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
45 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020046 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020047 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020048 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020049 * - support minimal size when 'termsize' is empty?
Bram Moolenaar4f44b882017-08-13 20:06:18 +020050 * - implement job options when starting a terminal. Allow:
Bram Moolenaar78712a72017-08-05 14:50:12 +020051 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
52 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
53 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
54 * Check that something is connected to the terminal.
55 * Test: "cat" reading from a file or buffer
56 * "ls" writing stdout to a file or buffer
57 * shell writing stderr to a file or buffer
Bram Moolenaar4f44b882017-08-13 20:06:18 +020058 * - For the GUI fill termios with default values, perhaps like pangoterm:
59 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020060 * - support ":term NONE" to open a terminal with a pty but not running a job
61 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020062 * - if the job in the terminal does not support the mouse, we can use the
63 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020064 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
65 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020066 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020067 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020068 * - Copy text in the vterm to the Vim buffer once in a while, so that
69 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020070 */
71
72#include "vim.h"
73
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020074#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020075
Bram Moolenaard5310982017-08-05 15:16:32 +020076#ifndef MIN
77# define MIN(x,y) ((x) < (y) ? (x) : (y))
78#endif
79#ifndef MAX
80# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020081#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020083#include "libvterm/include/vterm.h"
84
Bram Moolenaar33a43be2017-08-06 21:36:22 +020085/* This is VTermScreenCell without the characters, thus much smaller. */
86typedef struct {
87 VTermScreenCellAttrs attrs;
88 char width;
89 VTermColor fg, bg;
90} cellattr_T;
91
Bram Moolenaard85f2712017-07-28 21:51:57 +020092typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020093 int sb_cols; /* can differ per line */
94 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020095} sb_line_T;
96
Bram Moolenaare4f25e42017-07-07 11:54:15 +020097/* typedef term_T in structs.h */
98struct terminal_S {
99 term_T *tl_next;
100
Bram Moolenaar423802d2017-07-30 16:52:24 +0200101 VTerm *tl_vterm;
102 job_T *tl_job;
103 buf_T *tl_buffer;
104
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200105 /* used when tl_job is NULL and only a pty was created */
106 int tl_tty_fd;
107 char_u *tl_tty_name;
108
Bram Moolenaar6d819742017-08-06 14:57:49 +0200109 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200110 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200111 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200112 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200113
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200114#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200115 void *tl_winpty_config;
116 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200117#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200118
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 /* last known vterm size */
120 int tl_rows;
121 int tl_cols;
122 /* vterm size does not follow window size */
123 int tl_rows_fixed;
124 int tl_cols_fixed;
125
Bram Moolenaar21554412017-07-24 21:44:43 +0200126 char_u *tl_title; /* NULL or allocated */
127 char_u *tl_status_text; /* NULL or allocated */
128
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200129 /* Range of screen rows to update. Zero based. */
130 int tl_dirty_row_start; /* -1 if nothing dirty */
131 int tl_dirty_row_end; /* row below last one to update */
132
Bram Moolenaard85f2712017-07-28 21:51:57 +0200133 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200134 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200135
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200136 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200137 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200138 int tl_cursor_blink;
139 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
140 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200141
142 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200143};
144
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200145#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
146#define TMODE_LOOP 2 /* CTRL-W N used */
147
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200148/*
149 * List of all active terminals.
150 */
151static term_T *first_term = NULL;
152
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200153/* Terminal active in terminal_loop(). */
154static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200155
156#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
157#define KEY_BUF_LEN 200
158
159/*
160 * Functions with separate implementation for MS-Windows and Unix-like systems.
161 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200162static int term_and_job_init(term_T *term, int rows, int cols,
163 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200164static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200165static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200166
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200167/* The characters that we know (or assume) that the terminal expects for the
168 * backspace and enter keys. */
169static int term_backspace_char = BS;
170static int term_enter_char = CAR;
171static int term_nl_does_cr = FALSE;
172
173
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200174/**************************************
175 * 1. Generic code for all systems.
176 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200177
178/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200179 * Determine the terminal size from 'termsize' and the current window.
180 * Assumes term->tl_rows and term->tl_cols are zero.
181 */
182 static void
183set_term_and_win_size(term_T *term)
184{
185 if (*curwin->w_p_tms != NUL)
186 {
187 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
188
189 term->tl_rows = atoi((char *)curwin->w_p_tms);
190 term->tl_cols = atoi((char *)p);
191 }
192 if (term->tl_rows == 0)
193 term->tl_rows = curwin->w_height;
194 else
195 {
196 win_setheight_win(term->tl_rows, curwin);
197 term->tl_rows_fixed = TRUE;
198 }
199 if (term->tl_cols == 0)
200 term->tl_cols = curwin->w_width;
201 else
202 {
203 win_setwidth_win(term->tl_cols, curwin);
204 term->tl_cols_fixed = TRUE;
205 }
206}
207
208/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 * Initialize job options for a terminal job.
210 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200211 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200212 static void
213init_job_options(jobopt_T *opt)
214{
215 clear_job_options(opt);
216
217 opt->jo_mode = MODE_RAW;
218 opt->jo_out_mode = MODE_RAW;
219 opt->jo_err_mode = MODE_RAW;
220 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
221
222 opt->jo_io[PART_OUT] = JIO_BUFFER;
223 opt->jo_io[PART_ERR] = JIO_BUFFER;
224 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
225
226 opt->jo_modifiable[PART_OUT] = 0;
227 opt->jo_modifiable[PART_ERR] = 0;
228 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
229
230 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
231}
232
233/*
234 * Set job options mandatory for a terminal job.
235 */
236 static void
237setup_job_options(jobopt_T *opt, int rows, int cols)
238{
239 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
240 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
241 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200242 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
243 opt->jo_term_rows = rows;
244 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
245 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200246}
247
248 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200249term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 exarg_T split_ea;
252 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200254 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200255
256 if (check_restricted() || check_secure())
257 return;
258
259 term = (term_T *)alloc_clear(sizeof(term_T));
260 if (term == NULL)
261 return;
262 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200263 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200264 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200265 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200266 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200268 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200269 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200270 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200271 /* Create a new buffer in the current window. */
272 if (!can_abandon(curbuf, forceit))
273 {
274 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200275 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200276 return;
277 }
278 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
279 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 {
281 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200282 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200283 }
284 }
285 else if (opt->jo_hidden)
286 {
287 buf_T *buf;
288
289 /* Create a new buffer without a window. Make it the current buffer for
290 * a moment to be able to do the initialisations. */
291 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
292 BLN_NEW | BLN_LISTED);
293 if (buf == NULL || ml_open(buf) == FAIL)
294 {
295 vim_free(term);
296 return;
297 }
298 old_curbuf = curbuf;
299 --curbuf->b_nwindows;
300 curbuf = buf;
301 curwin->w_buffer = buf;
302 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200304 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200305 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200306 /* Open a new window or tab. */
307 split_ea.cmdidx = CMD_new;
308 split_ea.cmd = (char_u *)"new";
309 split_ea.arg = (char_u *)"";
310 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
311 {
312 split_ea.line2 = opt->jo_term_rows;
313 split_ea.addr_count = 1;
314 }
315 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
316 {
317 split_ea.line2 = opt->jo_term_cols;
318 split_ea.addr_count = 1;
319 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200320
Bram Moolenaarda43b612017-08-11 22:27:50 +0200321 ex_splitview(&split_ea);
322 if (curwin == old_curwin)
323 {
324 /* split failed */
325 vim_free(term);
326 return;
327 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200329 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200330 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200331
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200332 if (!opt->jo_hidden)
333 {
334 /* only one size was taken care of with :new, do the other one */
335 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
336 win_setheight(opt->jo_term_rows);
337 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
338 win_setwidth(opt->jo_term_cols);
339 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200340
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200341 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200342 term->tl_next = first_term;
343 first_term = term;
344
Bram Moolenaar78712a72017-08-05 14:50:12 +0200345 if (opt->jo_term_name != NULL)
346 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
347 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200348 {
349 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200350 size_t len;
351 char_u *cmd, *p;
352
353 if (argvar->v_type == VAR_STRING)
354 cmd = argvar->vval.v_string;
355 else if (argvar->v_type != VAR_LIST
356 || argvar->vval.v_list == NULL
357 || argvar->vval.v_list->lv_len < 1)
358 cmd = (char_u*)"";
359 else
360 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
361
362 len = STRLEN(cmd) + 10;
363 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200364
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200365 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200366 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200367 /* Prepend a ! to the command name to avoid the buffer name equals
368 * the executable, otherwise ":w!" would overwrite it. */
369 if (i == 0)
370 vim_snprintf((char *)p, len, "!%s", cmd);
371 else
372 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200373 if (buflist_findname(p) == NULL)
374 {
375 curbuf->b_ffname = p;
376 break;
377 }
378 }
379 }
380 curbuf->b_fname = curbuf->b_ffname;
381
Bram Moolenaar37c45832017-08-12 16:01:04 +0200382 if (opt->jo_term_opencmd != NULL)
383 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
384
Bram Moolenaareb44a682017-08-03 22:44:55 +0200385 set_string_option_direct((char_u *)"buftype", -1,
386 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
387
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200388 /* Mark the buffer as not modifiable. It can only be made modifiable after
389 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200390 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200391
392 /* Set 'bufhidden' to "hide": allow closing the window. */
393 set_string_option_direct((char_u *)"bufhidden", -1,
394 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200397 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200398
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200399 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200400 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200401 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200402 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200403 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200404 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200405
406 if (old_curbuf != NULL)
407 {
408 --curbuf->b_nwindows;
409 curbuf = old_curbuf;
410 curwin->w_buffer = curbuf;
411 ++curbuf->b_nwindows;
412 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200413 }
414 else
415 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200416 buf_T *buf = curbuf;
417
Bram Moolenaard85f2712017-07-28 21:51:57 +0200418 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200419 if (old_curbuf != NULL)
420 {
421 --curbuf->b_nwindows;
422 curbuf = old_curbuf;
423 curwin->w_buffer = curbuf;
424 ++curbuf->b_nwindows;
425 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200426
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200427 /* Wiping out the buffer will also close the window and call
428 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200429 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200430 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200431}
432
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200433/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200434 * ":terminal": open a terminal window and execute a job in it.
435 */
436 void
437ex_terminal(exarg_T *eap)
438{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200439 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200440 jobopt_T opt;
441 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200442
443 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200444
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200445 cmd = eap->arg;
446 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
447 {
448 char_u *p;
449
450 cmd += 2;
451 p = skiptowhite(cmd);
452 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
453 opt.jo_term_finish = 'c';
454 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
455 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200456 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
457 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200458 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
459 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200460 else
461 {
462 if (*p)
463 *p = NUL;
464 EMSG2(_("E181: Invalid attribute: %s"), cmd);
465 return;
466 }
467 cmd = skipwhite(p);
468 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200469 if (cmd == NULL || *cmd == NUL)
470 cmd = p_sh;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200471
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200472 if (eap->addr_count == 2)
473 {
474 opt.jo_term_rows = eap->line1;
475 opt.jo_term_cols = eap->line2;
476 }
477 else if (eap->addr_count == 1)
478 {
479 if (cmdmod.split & WSP_VERT)
480 opt.jo_term_cols = eap->line2;
481 else
482 opt.jo_term_rows = eap->line2;
483 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200484
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200485 argvar.v_type = VAR_STRING;
486 argvar.vval.v_string = cmd;
487 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200488}
489
490/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200491 * Free the scrollback buffer for "term".
492 */
493 static void
494free_scrollback(term_T *term)
495{
496 int i;
497
498 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
499 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
500 ga_clear(&term->tl_scrollback);
501}
502
503/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200504 * Free a terminal and everything it refers to.
505 * Kills the job if there is one.
506 * Called when wiping out a buffer.
507 */
508 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200509free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200510{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200511 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200512 term_T *tp;
513
514 if (term == NULL)
515 return;
516 if (first_term == term)
517 first_term = term->tl_next;
518 else
519 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
520 if (tp->tl_next == term)
521 {
522 tp->tl_next = term->tl_next;
523 break;
524 }
525
526 if (term->tl_job != NULL)
527 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200528 if (term->tl_job->jv_status != JOB_ENDED
529 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200530 job_stop(term->tl_job, NULL, "kill");
531 job_unref(term->tl_job);
532 }
533
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200534 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200535
536 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200537 vim_free(term->tl_title);
538 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200539 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200540 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200541 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200542 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200543 if (in_terminal_loop == term)
544 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200545}
546
547/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200548 * Write job output "msg[len]" to the vterm.
549 */
550 static void
551term_write_job_output(term_T *term, char_u *msg, size_t len)
552{
553 VTerm *vterm = term->tl_vterm;
554 char_u *p;
555 size_t done;
556 size_t len_now;
557
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200558 if (term_nl_does_cr)
559 vterm_input_write(vterm, (char *)msg, len);
560 else
561 /* need to convert NL to CR-NL */
562 for (done = 0; done < len; done += len_now)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200563 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200564 for (p = msg + done; p < msg + len; )
565 {
566 if (*p == NL)
567 break;
568 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
569 }
570 len_now = p - msg - done;
571 vterm_input_write(vterm, (char *)msg + done, len_now);
572 if (p < msg + len && *p == NL)
573 {
574 vterm_input_write(vterm, "\r\n", 2);
575 ++len_now;
576 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200577 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200578
579 /* this invokes the damage callbacks */
580 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
581}
582
Bram Moolenaar1c844932017-07-24 23:36:41 +0200583 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200584update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200585{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200586 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200587 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200588 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200589 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200590 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200591 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200592 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200593 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200594#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200595 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200596 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200597#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200598 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200599}
600
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200601/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200602 * Invoked when "msg" output from a job was received. Write it to the terminal
603 * of "buffer".
604 */
605 void
606write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
607{
608 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200609 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200610
Bram Moolenaard85f2712017-07-28 21:51:57 +0200611 if (term->tl_vterm == NULL)
612 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200613 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200614 return;
615 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200616 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200617 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200618
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200619 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
620 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200621 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200622 {
623 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200624 ch_log(term->tl_job->jv_channel, "updating screen");
625 if (buffer == curbuf)
626 {
627 update_screen(0);
628 update_cursor(term, TRUE);
629 }
630 else
631 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200632 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200633}
634
635/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200636 * Send a mouse position and click to the vterm
637 */
638 static int
639term_send_mouse(VTerm *vterm, int button, int pressed)
640{
641 VTermModifier mod = VTERM_MOD_NONE;
642
643 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
644 mouse_col - W_WINCOL(curwin), mod);
645 vterm_mouse_button(vterm, button, pressed, mod);
646 return TRUE;
647}
648
649/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 * Convert typed key "c" into bytes to send to the job.
651 * Return the number of bytes in "buf".
652 */
653 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200654term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200656 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200657 VTermKey key = VTERM_KEY_NONE;
658 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200659 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200660
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 switch (c)
662 {
Bram Moolenaar4f44b882017-08-13 20:06:18 +0200663 case CAR: c = term_enter_char; break;
664 /* don't use VTERM_KEY_BACKSPACE, it always
665 * becomes 0x7f DEL */
666 case K_BS: c = term_backspace_char; break;
667
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200668 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200669 case K_DEL: key = VTERM_KEY_DEL; break;
670 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200671 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
672 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200673 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200674 case K_S_END: mod = VTERM_MOD_SHIFT;
675 key = VTERM_KEY_END; break;
676 case K_C_END: mod = VTERM_MOD_CTRL;
677 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200678 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
679 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
680 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
681 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
682 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
683 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
684 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
685 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
686 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
687 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
688 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
689 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
690 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200691 case K_S_HOME: mod = VTERM_MOD_SHIFT;
692 key = VTERM_KEY_HOME; break;
693 case K_C_HOME: mod = VTERM_MOD_CTRL;
694 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695 case K_INS: key = VTERM_KEY_INS; break;
696 case K_K0: key = VTERM_KEY_KP_0; break;
697 case K_K1: key = VTERM_KEY_KP_1; break;
698 case K_K2: key = VTERM_KEY_KP_2; break;
699 case K_K3: key = VTERM_KEY_KP_3; break;
700 case K_K4: key = VTERM_KEY_KP_4; break;
701 case K_K5: key = VTERM_KEY_KP_5; break;
702 case K_K6: key = VTERM_KEY_KP_6; break;
703 case K_K7: key = VTERM_KEY_KP_7; break;
704 case K_K8: key = VTERM_KEY_KP_8; break;
705 case K_K9: key = VTERM_KEY_KP_9; break;
706 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
707 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
708 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
709 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
710 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
711 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
712 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
713 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
714 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
715 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
716 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
717 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
718 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200719 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
720 key = VTERM_KEY_LEFT; break;
721 case K_C_LEFT: mod = VTERM_MOD_CTRL;
722 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200723 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
724 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
725 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200726 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
727 key = VTERM_KEY_RIGHT; break;
728 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
729 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200730 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200731 case K_S_UP: mod = VTERM_MOD_SHIFT;
732 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200733 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200734
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200735 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
736 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
737 case K_MOUSELEFT: /* TODO */ return 0;
738 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200739
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200740 case K_LEFTMOUSE:
741 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
742 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
743 case K_LEFTRELEASE:
744 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
745 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
746 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
747 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
748 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
749 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
750 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
751 case K_X1MOUSE: /* TODO */ return 0;
752 case K_X1DRAG: /* TODO */ return 0;
753 case K_X1RELEASE: /* TODO */ return 0;
754 case K_X2MOUSE: /* TODO */ return 0;
755 case K_X2DRAG: /* TODO */ return 0;
756 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200757
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200758 case K_IGNORE: return 0;
759 case K_NOP: return 0;
760 case K_UNDO: return 0;
761 case K_HELP: return 0;
762 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
763 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
764 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
765 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
766 case K_SELECT: return 0;
767#ifdef FEAT_GUI
768 case K_VER_SCROLLBAR: return 0;
769 case K_HOR_SCROLLBAR: return 0;
770#endif
771#ifdef FEAT_GUI_TABLINE
772 case K_TABLINE: return 0;
773 case K_TABMENU: return 0;
774#endif
775#ifdef FEAT_NETBEANS_INTG
776 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
777#endif
778#ifdef FEAT_DND
779 case K_DROP: return 0;
780#endif
781#ifdef FEAT_AUTOCMD
782 case K_CURSORHOLD: return 0;
783#endif
784 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
785 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200786 }
787
788 /*
789 * Convert special keys to vterm keys:
790 * - Write keys to vterm: vterm_keyboard_key()
791 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200792 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200793 */
794 if (key != VTERM_KEY_NONE)
795 /* Special key, let vterm convert it. */
796 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200797 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200798 /* Normal character, let vterm convert it. */
799 vterm_keyboard_unichar(vterm, c, mod);
800
801 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200802 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200803}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200804
Bram Moolenaar938783d2017-07-16 20:13:26 +0200805/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200806 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200807 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200808 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200809term_job_running(term_T *term)
810{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200811 /* Also consider the job finished when the channel is closed, to avoid a
812 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200813 return term != NULL
814 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200815 && term->tl_job->jv_status == JOB_STARTED
816 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200817}
818
819/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200820 * Add the last line of the scrollback buffer to the buffer in the window.
821 */
822 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200823add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200824{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200825 buf_T *buf = term->tl_buffer;
826 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
827 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200828
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200829 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200830 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200831 {
832 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200833 curbuf = buf;
834 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200835 curbuf = curwin->w_buffer;
836 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200837}
838
839/*
840 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200841 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200842 */
843 static void
844move_terminal_to_buffer(term_T *term)
845{
846 win_T *wp;
847 int len;
848 int lines_skipped = 0;
849 VTermPos pos;
850 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200851 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200852 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200853
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200854 if (term->tl_vterm == NULL)
855 return;
856 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200857 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
858 {
859 len = 0;
860 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
861 if (vterm_screen_get_cell(screen, pos, &cell) != 0
862 && cell.chars[0] != NUL)
863 len = pos.col + 1;
864
865 if (len == 0)
866 ++lines_skipped;
867 else
868 {
869 while (lines_skipped > 0)
870 {
871 /* Line was skipped, add an empty line. */
872 --lines_skipped;
873 if (ga_grow(&term->tl_scrollback, 1) == OK)
874 {
875 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
876 + term->tl_scrollback.ga_len;
877
878 line->sb_cols = 0;
879 line->sb_cells = NULL;
880 ++term->tl_scrollback.ga_len;
881
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200882 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200883 }
884 }
885
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200886 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200887 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
888 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200889 garray_T ga;
890 int width;
891 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200892 + term->tl_scrollback.ga_len;
893
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200894 ga_init2(&ga, 1, 100);
895 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 {
897 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200898 {
899 width = 1;
900 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
901 if (ga_grow(&ga, 1) == OK)
902 ga.ga_len += mb_char2bytes(' ',
903 (char_u *)ga.ga_data + ga.ga_len);
904 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200905 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200906 {
907 width = cell.width;
908
909 p[pos.col].width = cell.width;
910 p[pos.col].attrs = cell.attrs;
911 p[pos.col].fg = cell.fg;
912 p[pos.col].bg = cell.bg;
913
914 if (ga_grow(&ga, MB_MAXBYTES) == OK)
915 {
916 int i;
917 int c;
918
919 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
920 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
921 (char_u *)ga.ga_data + ga.ga_len);
922 }
923 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200924 }
925 line->sb_cols = len;
926 line->sb_cells = p;
927 ++term->tl_scrollback.ga_len;
928
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200929 if (ga_grow(&ga, 1) == FAIL)
930 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
931 else
932 {
933 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
934 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
935 }
936 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200937 }
938 else
939 vim_free(p);
940 }
941 }
942
943 FOR_ALL_WINDOWS(wp)
944 {
945 if (wp->w_buffer == term->tl_buffer)
946 {
947 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
948 wp->w_cursor.col = 0;
949 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200950 if (wp->w_cursor.lnum >= wp->w_height)
951 {
952 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
953
954 if (wp->w_topline < min_topline)
955 wp->w_topline = min_topline;
956 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200957 redraw_win_later(wp, NOT_VALID);
958 }
959 }
960}
961
962 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200963set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200964{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200965 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200966 vim_free(term->tl_status_text);
967 term->tl_status_text = NULL;
968 if (term->tl_buffer == curbuf)
969 maketitle();
970}
971
972/*
973 * Called after the job if finished and Terminal mode is not active:
974 * Move the vterm contents into the scrollback buffer and free the vterm.
975 */
976 static void
977cleanup_vterm(term_T *term)
978{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200979 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200980 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200981 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200982 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983}
984
985/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200986 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200987 * Suspends updating the terminal window.
988 */
989 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200990term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200991{
992 term_T *term = curbuf->b_term;
993
994 /* Append the current terminal contents to the buffer. */
995 move_terminal_to_buffer(term);
996
Bram Moolenaar6d819742017-08-06 14:57:49 +0200997 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200998
Bram Moolenaar6d819742017-08-06 14:57:49 +0200999 /* Move the window cursor to the position of the cursor in the
1000 * terminal. */
1001 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1002 + term->tl_cursor_pos.row + 1;
1003 check_cursor();
1004 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001005
Bram Moolenaar6d819742017-08-06 14:57:49 +02001006 /* Display the same lines as in the terminal. */
1007 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001008}
1009
1010/*
1011 * Returns TRUE if the current window contains a terminal and we are in
1012 * Terminal-Normal mode.
1013 */
1014 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001015term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001016{
1017 term_T *term = curbuf->b_term;
1018
Bram Moolenaar6d819742017-08-06 14:57:49 +02001019 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001020}
1021
1022/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001023 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001024 * Restores updating the terminal window.
1025 */
1026 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001027term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001028{
1029 term_T *term = curbuf->b_term;
1030 sb_line_T *line;
1031 garray_T *gap;
1032
1033 /* Remove the terminal contents from the scrollback and the buffer. */
1034 gap = &term->tl_scrollback;
1035 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1036 {
1037 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1038 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1039 vim_free(line->sb_cells);
1040 --gap->ga_len;
1041 if (gap->ga_len == 0)
1042 break;
1043 }
1044 check_cursor();
1045
Bram Moolenaar6d819742017-08-06 14:57:49 +02001046 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001047
1048 if (term->tl_channel_closed)
1049 cleanup_vterm(term);
1050 redraw_buf_and_status_later(curbuf, NOT_VALID);
1051}
1052
1053/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001054 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001055 * Note: while waiting a terminal may be closed and freed if the channel is
1056 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001057 * TODO: use terminal mode mappings.
1058 */
1059 static int
1060term_vgetc()
1061{
1062 int c;
1063
1064 ++no_mapping;
1065 ++allow_keys;
1066 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001067#ifdef WIN3264
1068 ctrl_break_was_pressed = FALSE;
1069#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001070 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001071 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001072 --no_mapping;
1073 --allow_keys;
1074 return c;
1075}
1076
1077/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001078 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001079 * Return FAIL when the key needs to be handled in Normal mode.
1080 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001081 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001082 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001083send_keys_to_term(term_T *term, int c, int typed)
1084{
1085 char msg[KEY_BUF_LEN];
1086 size_t len;
1087 static int mouse_was_outside = FALSE;
1088 int dragging_outside = FALSE;
1089
1090 /* Catch keys that need to be handled as in Normal mode. */
1091 switch (c)
1092 {
1093 case NUL:
1094 case K_ZERO:
1095 if (typed)
1096 stuffcharReadbuff(c);
1097 return FAIL;
1098
1099 case K_IGNORE:
1100 return FAIL;
1101
1102 case K_LEFTDRAG:
1103 case K_MIDDLEDRAG:
1104 case K_RIGHTDRAG:
1105 case K_X1DRAG:
1106 case K_X2DRAG:
1107 dragging_outside = mouse_was_outside;
1108 /* FALLTHROUGH */
1109 case K_LEFTMOUSE:
1110 case K_LEFTMOUSE_NM:
1111 case K_LEFTRELEASE:
1112 case K_LEFTRELEASE_NM:
1113 case K_MIDDLEMOUSE:
1114 case K_MIDDLERELEASE:
1115 case K_RIGHTMOUSE:
1116 case K_RIGHTRELEASE:
1117 case K_X1MOUSE:
1118 case K_X1RELEASE:
1119 case K_X2MOUSE:
1120 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001121
1122 case K_MOUSEUP:
1123 case K_MOUSEDOWN:
1124 case K_MOUSELEFT:
1125 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001126 if (mouse_row < W_WINROW(curwin)
1127 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1128 || mouse_col < W_WINCOL(curwin)
1129 || mouse_col >= W_ENDCOL(curwin)
1130 || dragging_outside)
1131 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001132 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001133 if (typed)
1134 {
1135 stuffcharReadbuff(c);
1136 mouse_was_outside = TRUE;
1137 }
1138 return FAIL;
1139 }
1140 }
1141 if (typed)
1142 mouse_was_outside = FALSE;
1143
1144 /* Convert the typed key to a sequence of bytes for the job. */
1145 len = term_convert_key(term, c, msg);
1146 if (len > 0)
1147 /* TODO: if FAIL is returned, stop? */
1148 channel_send(term->tl_job->jv_channel, PART_IN,
1149 (char_u *)msg, (int)len, NULL);
1150
1151 return OK;
1152}
1153
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001154 static void
1155position_cursor(win_T *wp, VTermPos *pos)
1156{
1157 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1158 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1159 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1160}
1161
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001162/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001163 * Handle CTRL-W "": send register contents to the job.
1164 */
1165 static void
1166term_paste_register(int prev_c UNUSED)
1167{
1168 int c;
1169 list_T *l;
1170 listitem_T *item;
1171 long reglen = 0;
1172 int type;
1173
1174#ifdef FEAT_CMDL_INFO
1175 if (add_to_showcmd(prev_c))
1176 if (add_to_showcmd('"'))
1177 out_flush();
1178#endif
1179 c = term_vgetc();
1180#ifdef FEAT_CMDL_INFO
1181 clear_showcmd();
1182#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001183 if (!term_use_loop())
1184 /* job finished while waiting for a character */
1185 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001186
1187 /* CTRL-W "= prompt for expression to evaluate. */
1188 if (c == '=' && get_expr_register() != '=')
1189 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001190 if (!term_use_loop())
1191 /* job finished while waiting for a character */
1192 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001193
1194 l = (list_T *)get_reg_contents(c, GREG_LIST);
1195 if (l != NULL)
1196 {
1197 type = get_reg_type(c, &reglen);
1198 for (item = l->lv_first; item != NULL; item = item->li_next)
1199 {
1200 char_u *s = get_tv_string(&item->li_tv);
1201
1202 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1203 s, STRLEN(s), NULL);
1204 if (item->li_next != NULL || type == MLINE)
1205 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1206 (char_u *)"\r", 1, NULL);
1207 }
1208 list_free(l);
1209 }
1210}
1211
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001212#if defined(FEAT_GUI) || defined(PROTO)
1213/*
1214 * Return TRUE when the cursor of the terminal should be displayed.
1215 */
1216 int
1217use_terminal_cursor()
1218{
1219 return in_terminal_loop != NULL;
1220}
1221
1222 cursorentry_T *
1223term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1224{
1225 term_T *term = in_terminal_loop;
1226 static cursorentry_T entry;
1227
1228 vim_memset(&entry, 0, sizeof(entry));
1229 entry.shape = entry.mshape =
1230 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1231 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1232 SHAPE_BLOCK;
1233 entry.percentage = 20;
1234 if (term->tl_cursor_blink)
1235 {
1236 entry.blinkwait = 700;
1237 entry.blinkon = 400;
1238 entry.blinkon = 250;
1239 }
1240 *fg = gui.back_pixel;
1241 if (term->tl_cursor_color == NULL)
1242 *bg = gui.norm_pixel;
1243 else
1244 *bg = color_name2handle(term->tl_cursor_color);
1245 entry.name = "n";
1246 entry.used_for = SHAPE_CURSOR;
1247
1248 return &entry;
1249}
1250#endif
1251
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001252static int did_change_cursor = FALSE;
1253
1254 static void
1255may_set_cursor_props(term_T *term)
1256{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001257#ifdef FEAT_GUI
1258 /* For the GUI the cursor properties are obtained with
1259 * term_get_cursor_shape(). */
1260 if (gui.in_use)
1261 return;
1262#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001263 if (in_terminal_loop == term)
1264 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001265 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001266 if (term->tl_cursor_color != NULL)
1267 term_cursor_color(term->tl_cursor_color);
1268 else
1269 term_cursor_color((char_u *)"");
1270 /* do both blink and shape+blink, in case setting shape does not work */
1271 term_cursor_blink(term->tl_cursor_blink);
1272 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1273 }
1274}
1275
1276 static void
1277may_restore_cursor_props(void)
1278{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001279#ifdef FEAT_GUI
1280 if (gui.in_use)
1281 return;
1282#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001283 if (did_change_cursor)
1284 {
1285 did_change_cursor = FALSE;
1286 ui_cursor_shape_forced(TRUE);
1287 term_cursor_color((char_u *)"");
1288 term_cursor_blink(FALSE);
1289 }
1290}
1291
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001292/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001293 * Returns TRUE if the current window contains a terminal and we are sending
1294 * keys to the job.
1295 */
1296 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001297term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001298{
1299 term_T *term = curbuf->b_term;
1300
1301 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001302 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001303 && term->tl_vterm != NULL
1304 && term_job_running(term);
1305}
1306
1307/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001308 * Wait for input and send it to the job.
1309 * Return when the start of a CTRL-W command is typed or anything else that
1310 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001311 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1312 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001313 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001314 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001315terminal_loop(void)
1316{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001317 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001318 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001319 int ret;
1320
Bram Moolenaar679653e2017-08-13 14:13:19 +02001321 /* Remember the terminal we are sending keys to. However, the terminal
1322 * might be closed while waiting for a character, e.g. typing "exit" in a
1323 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1324 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001325 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001326
1327 if (*curwin->w_p_tk != NUL)
1328 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001329 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001330 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001331
Bram Moolenaar4f44b882017-08-13 20:06:18 +02001332#ifdef UNIX
1333 {
1334 int fd = curbuf->b_term->tl_job->jv_channel->ch_part[PART_IN].ch_fd;
1335
1336 if (isatty(fd))
1337 {
1338 ttyinfo_T info;
1339
1340 /* Get the current backspace and enter characters of the pty. */
1341 if (get_tty_info(fd, &info) == OK)
1342 {
1343 term_backspace_char = info.backspace;
1344 term_enter_char = info.enter;
1345 term_nl_does_cr = info.nl_does_cr;
1346 }
1347 }
1348 }
1349#endif
1350
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001351 for (;;)
1352 {
1353 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001354 /* Repeat redrawing in case a message is received while redrawing. */
1355 while (curwin->w_redr_type != 0)
1356 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001357 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001358
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001359 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001360 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001361 /* job finished while waiting for a character */
1362 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001363 if (c == K_IGNORE)
1364 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001365
Bram Moolenaarfae42832017-08-01 22:24:26 +02001366#ifdef UNIX
1367 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1368#endif
1369#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001370 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001371 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001372 if (ctrl_break_was_pressed)
1373 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001374#endif
1375
Bram Moolenaar69198192017-08-05 14:10:48 +02001376 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001377 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001378 int prev_c = c;
1379
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001380#ifdef FEAT_CMDL_INFO
1381 if (add_to_showcmd(c))
1382 out_flush();
1383#endif
1384 c = term_vgetc();
1385#ifdef FEAT_CMDL_INFO
1386 clear_showcmd();
1387#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001388 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001389 /* job finished while waiting for a character */
1390 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001391
Bram Moolenaar69198192017-08-05 14:10:48 +02001392 if (prev_c == Ctrl_BSL)
1393 {
1394 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001395 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001396 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1397 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001398 ret = FAIL;
1399 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001400 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001401 /* Send both keys to the terminal. */
1402 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1403 }
1404 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001405 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001406 /* "CTRL-W .": send CTRL-W to the job */
1407 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001408 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001409 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001410 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001411 /* CTRL-W N : go to Terminal-Normal mode. */
1412 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001413 ret = FAIL;
1414 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001415 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001416 else if (c == '"')
1417 {
1418 term_paste_register(prev_c);
1419 continue;
1420 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001421 else if (termkey == 0 || c != termkey)
1422 {
1423 stuffcharReadbuff(Ctrl_W);
1424 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001425 ret = OK;
1426 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001427 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001428 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001429 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001430 {
1431 ret = OK;
1432 goto theend;
1433 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001435 ret = FAIL;
1436
1437theend:
1438 in_terminal_loop = NULL;
1439 may_restore_cursor_props();
1440 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001441}
1442
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001443/*
1444 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001445 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001446 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001447 */
1448 void
1449term_job_ended(job_T *job)
1450{
1451 term_T *term;
1452 int did_one = FALSE;
1453
1454 for (term = first_term; term != NULL; term = term->tl_next)
1455 if (term->tl_job == job)
1456 {
1457 vim_free(term->tl_title);
1458 term->tl_title = NULL;
1459 vim_free(term->tl_status_text);
1460 term->tl_status_text = NULL;
1461 redraw_buf_and_status_later(term->tl_buffer, VALID);
1462 did_one = TRUE;
1463 }
1464 if (did_one)
1465 redraw_statuslines();
1466 if (curbuf->b_term != NULL)
1467 {
1468 if (curbuf->b_term->tl_job == job)
1469 maketitle();
1470 update_cursor(curbuf->b_term, TRUE);
1471 }
1472}
1473
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001474 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001475may_toggle_cursor(term_T *term)
1476{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001477 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001478 {
1479 if (term->tl_cursor_visible)
1480 cursor_on();
1481 else
1482 cursor_off();
1483 }
1484}
1485
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001486 static int
1487handle_damage(VTermRect rect, void *user)
1488{
1489 term_T *term = (term_T *)user;
1490
1491 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1492 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1493 redraw_buf_later(term->tl_buffer, NOT_VALID);
1494 return 1;
1495}
1496
1497 static int
1498handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1499{
1500 term_T *term = (term_T *)user;
1501
1502 /* TODO */
1503 redraw_buf_later(term->tl_buffer, NOT_VALID);
1504 return 1;
1505}
1506
1507 static int
1508handle_movecursor(
1509 VTermPos pos,
1510 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001511 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001512 void *user)
1513{
1514 term_T *term = (term_T *)user;
1515 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001516
1517 term->tl_cursor_pos = pos;
1518 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001519
1520 FOR_ALL_WINDOWS(wp)
1521 {
1522 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001523 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001524 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001525 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001526 {
1527 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001528 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001529 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001530
1531 return 1;
1532}
1533
Bram Moolenaar21554412017-07-24 21:44:43 +02001534 static int
1535handle_settermprop(
1536 VTermProp prop,
1537 VTermValue *value,
1538 void *user)
1539{
1540 term_T *term = (term_T *)user;
1541
1542 switch (prop)
1543 {
1544 case VTERM_PROP_TITLE:
1545 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001546 /* a blank title isn't useful, make it empty, so that "running" is
1547 * displayed */
1548 if (*skipwhite((char_u *)value->string) == NUL)
1549 term->tl_title = NULL;
1550 else
1551 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001552 vim_free(term->tl_status_text);
1553 term->tl_status_text = NULL;
1554 if (term == curbuf->b_term)
1555 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001556 break;
1557
1558 case VTERM_PROP_CURSORVISIBLE:
1559 term->tl_cursor_visible = value->boolean;
1560 may_toggle_cursor(term);
1561 out_flush();
1562 break;
1563
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001564 case VTERM_PROP_CURSORBLINK:
1565 term->tl_cursor_blink = value->boolean;
1566 may_set_cursor_props(term);
1567 break;
1568
1569 case VTERM_PROP_CURSORSHAPE:
1570 term->tl_cursor_shape = value->number;
1571 may_set_cursor_props(term);
1572 break;
1573
1574 case VTERM_PROP_CURSORCOLOR:
1575 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001576 if (*value->string == NUL)
1577 term->tl_cursor_color = NULL;
1578 else
1579 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001580 may_set_cursor_props(term);
1581 break;
1582
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001583 case VTERM_PROP_ALTSCREEN:
1584 /* TODO: do anything else? */
1585 term->tl_using_altscreen = value->boolean;
1586 break;
1587
Bram Moolenaar21554412017-07-24 21:44:43 +02001588 default:
1589 break;
1590 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001591 /* Always return 1, otherwise vterm doesn't store the value internally. */
1592 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001593}
1594
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001595/*
1596 * The job running in the terminal resized the terminal.
1597 */
1598 static int
1599handle_resize(int rows, int cols, void *user)
1600{
1601 term_T *term = (term_T *)user;
1602 win_T *wp;
1603
1604 term->tl_rows = rows;
1605 term->tl_cols = cols;
1606 FOR_ALL_WINDOWS(wp)
1607 {
1608 if (wp->w_buffer == term->tl_buffer)
1609 {
1610 win_setheight_win(rows, wp);
1611 win_setwidth_win(cols, wp);
1612 }
1613 }
1614
1615 redraw_buf_later(term->tl_buffer, NOT_VALID);
1616 return 1;
1617}
1618
Bram Moolenaard85f2712017-07-28 21:51:57 +02001619/*
1620 * Handle a line that is pushed off the top of the screen.
1621 */
1622 static int
1623handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1624{
1625 term_T *term = (term_T *)user;
1626
1627 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001628 if (ga_grow(&term->tl_scrollback, 1) == OK)
1629 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001630 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001631 int len = 0;
1632 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001633 int c;
1634 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001635 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001636 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001637
1638 /* do not store empty cells at the end */
1639 for (i = 0; i < cols; ++i)
1640 if (cells[i].chars[0] != 0)
1641 len = i + 1;
1642
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001643 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001644 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001645 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001646 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001647 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001648 for (col = 0; col < len; col += cells[col].width)
1649 {
1650 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1651 {
1652 ga.ga_len = 0;
1653 break;
1654 }
1655 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1656 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1657 (char_u *)ga.ga_data + ga.ga_len);
1658 p[col].width = cells[col].width;
1659 p[col].attrs = cells[col].attrs;
1660 p[col].fg = cells[col].fg;
1661 p[col].bg = cells[col].bg;
1662 }
1663 }
1664 if (ga_grow(&ga, 1) == FAIL)
1665 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1666 else
1667 {
1668 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1669 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1670 }
1671 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001672
1673 line = (sb_line_T *)term->tl_scrollback.ga_data
1674 + term->tl_scrollback.ga_len;
1675 line->sb_cols = len;
1676 line->sb_cells = p;
1677 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001678 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001679 }
1680 return 0; /* ignored */
1681}
1682
Bram Moolenaar21554412017-07-24 21:44:43 +02001683static VTermScreenCallbacks screen_callbacks = {
1684 handle_damage, /* damage */
1685 handle_moverect, /* moverect */
1686 handle_movecursor, /* movecursor */
1687 handle_settermprop, /* settermprop */
1688 NULL, /* bell */
1689 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001690 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001691 NULL /* sb_popline */
1692};
1693
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001694/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001695 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001696 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001697 */
1698 void
1699term_channel_closed(channel_T *ch)
1700{
1701 term_T *term;
1702 int did_one = FALSE;
1703
1704 for (term = first_term; term != NULL; term = term->tl_next)
1705 if (term->tl_job == ch->ch_job)
1706 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001707 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001708 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001709
Bram Moolenaard85f2712017-07-28 21:51:57 +02001710 vim_free(term->tl_title);
1711 term->tl_title = NULL;
1712 vim_free(term->tl_status_text);
1713 term->tl_status_text = NULL;
1714
Bram Moolenaar423802d2017-07-30 16:52:24 +02001715 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001716 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001717 {
1718 int fnum = term->tl_buffer->b_fnum;
1719
Bram Moolenaar423802d2017-07-30 16:52:24 +02001720 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001721
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001722 if (term->tl_finish == 'c')
1723 {
1724 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001725 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001726 curbuf = term->tl_buffer;
1727 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1728 break;
1729 }
1730 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1731 {
1732 char buf[50];
1733
1734 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001735 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001736 vim_snprintf(buf, sizeof(buf),
1737 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001738 ? "botright sbuf %d"
1739 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001740 do_cmdline_cmd((char_u *)buf);
1741 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001742 else
1743 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001744 }
1745
Bram Moolenaard85f2712017-07-28 21:51:57 +02001746 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001747 }
1748 if (did_one)
1749 {
1750 redraw_statuslines();
1751
1752 /* Need to break out of vgetc(). */
1753 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001754 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001755
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001756 term = curbuf->b_term;
1757 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001758 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001759 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001760 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001761 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001762 }
1763 }
1764}
1765
1766/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001767 * Reverse engineer the RGB value into a cterm color index.
1768 * First color is 1. Return 0 if no match found.
1769 */
1770 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001771color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001772{
1773 int red = color->red;
1774 int blue = color->blue;
1775 int green = color->green;
1776
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001777 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001778 if (red == 0)
1779 {
1780 if (green == 0)
1781 {
1782 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001783 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001784 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001785 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001786 }
1787 else if (green == 224)
1788 {
1789 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001790 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001791 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001792 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001793 }
1794 }
1795 else if (red == 224)
1796 {
1797 if (green == 0)
1798 {
1799 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001800 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001801 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001802 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001803 }
1804 else if (green == 224)
1805 {
1806 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001807 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001808 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001809 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001810 }
1811 }
1812 else if (red == 128)
1813 {
1814 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001815 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001816 }
1817 else if (red == 255)
1818 {
1819 if (green == 64)
1820 {
1821 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001822 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001823 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001824 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001825 }
1826 else if (green == 255)
1827 {
1828 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001829 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001830 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001831 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001832 }
1833 }
1834 else if (red == 64)
1835 {
1836 if (green == 64)
1837 {
1838 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001839 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001840 }
1841 else if (green == 255)
1842 {
1843 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001844 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001845 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001846 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001847 }
1848 }
1849 if (t_colors >= 256)
1850 {
1851 if (red == blue && red == green)
1852 {
1853 /* 24-color greyscale */
1854 static int cutoff[23] = {
1855 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1856 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1857 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1858 int i;
1859
1860 for (i = 0; i < 23; ++i)
1861 if (red < cutoff[i])
1862 return i + 233;
1863 return 256;
1864 }
1865
1866 /* 216-color cube */
1867 return 17 + ((red + 25) / 0x33) * 36
1868 + ((green + 25) / 0x33) * 6
1869 + (blue + 25) / 0x33;
1870 }
1871 return 0;
1872}
1873
1874/*
1875 * Convert the attributes of a vterm cell into an attribute index.
1876 */
1877 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001878cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001879{
1880 int attr = 0;
1881
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001882 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001883 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001884 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001885 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001886 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001887 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001888 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001889 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001890 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001891 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001892
1893#ifdef FEAT_GUI
1894 if (gui.in_use)
1895 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001896 guicolor_T fg, bg;
1897
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001898 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1899 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001900 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001901 }
1902 else
1903#endif
1904#ifdef FEAT_TERMGUICOLORS
1905 if (p_tgc)
1906 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001907 guicolor_T fg, bg;
1908
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001909 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1910 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001911
1912 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001913 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001914 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001915#endif
1916 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001917 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001918 int fg = color2index(&cellfg, TRUE, &bold);
1919 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001920
1921 /* with 8 colors set the bold attribute to get a bright foreground */
1922 if (bold == TRUE)
1923 attr |= HL_BOLD;
1924 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001925 }
1926 return 0;
1927}
1928
1929/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001930 * Called to update a window that contains an active terminal.
1931 * Returns FAIL when there is no terminal running in this window or in
1932 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001933 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001934 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001935term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001936{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001937 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001938 VTerm *vterm;
1939 VTermScreen *screen;
1940 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001941 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001942
Bram Moolenaar6d819742017-08-06 14:57:49 +02001943 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001944 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001945
Bram Moolenaard85f2712017-07-28 21:51:57 +02001946 vterm = term->tl_vterm;
1947 screen = vterm_obtain_screen(vterm);
1948 state = vterm_obtain_state(vterm);
1949
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950 /*
1951 * If the window was resized a redraw will be triggered and we get here.
1952 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1953 */
1954 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1955 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001956 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001957 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1958 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1959 win_T *twp;
1960
1961 FOR_ALL_WINDOWS(twp)
1962 {
1963 /* When more than one window shows the same terminal, use the
1964 * smallest size. */
1965 if (twp->w_buffer == term->tl_buffer)
1966 {
1967 if (!term->tl_rows_fixed && rows > twp->w_height)
1968 rows = twp->w_height;
1969 if (!term->tl_cols_fixed && cols > twp->w_width)
1970 cols = twp->w_width;
1971 }
1972 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001973
1974 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001975 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001976 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001977 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001978 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001979
1980 /* The cursor may have been moved when resizing. */
1981 vterm_state_get_cursorpos(state, &pos);
1982 position_cursor(wp, &pos);
1983
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001984 /* TODO: Only redraw what changed. */
1985 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001986 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001987 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001988 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001989
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001990 if (pos.row < term->tl_rows)
1991 {
1992 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001993 {
1994 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001995 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001996
Bram Moolenaareeac6772017-07-23 15:48:37 +02001997 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1998 vim_memset(&cell, 0, sizeof(cell));
1999
2000 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002001 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002002 if (c == NUL)
2003 {
2004 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02002005#if defined(FEAT_MBYTE)
2006 if (enc_utf8)
2007 ScreenLinesUC[off] = NUL;
2008#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002009 }
2010 else
2011 {
2012#if defined(FEAT_MBYTE)
2013 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002014 {
2015 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002016 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002017 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002018 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002019 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002020 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002021 if (enc_utf8)
2022 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002023 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002024#else
2025 ScreenLines[off] = c;
2026#endif
2027 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002028 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002029
2030 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002031 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002032 if (cell.width == 2)
2033 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002034 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002035#if defined(FEAT_MBYTE)
2036 if (enc_utf8)
2037 ScreenLinesUC[off] = NUL;
2038#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002039 ++pos.col;
2040 ++off;
2041 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002042 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002043 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002044 else
2045 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002046
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002047 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2048 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002049 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002050
2051 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002052}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002053
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002054/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002055 * Return TRUE if "wp" is a terminal window where the job has finished.
2056 */
2057 int
2058term_is_finished(buf_T *buf)
2059{
2060 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2061}
2062
2063/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002064 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002065 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002066 */
2067 int
2068term_show_buffer(buf_T *buf)
2069{
2070 term_T *term = buf->b_term;
2071
Bram Moolenaar6d819742017-08-06 14:57:49 +02002072 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002073}
2074
2075/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002076 * The current buffer is going to be changed. If there is terminal
2077 * highlighting remove it now.
2078 */
2079 void
2080term_change_in_curbuf(void)
2081{
2082 term_T *term = curbuf->b_term;
2083
2084 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2085 {
2086 free_scrollback(term);
2087 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002088
2089 /* The buffer is now like a normal buffer, it cannot be easily
2090 * abandoned when changed. */
2091 set_string_option_direct((char_u *)"buftype", -1,
2092 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002093 }
2094}
2095
2096/*
2097 * Get the screen attribute for a position in the buffer.
2098 */
2099 int
2100term_get_attr(buf_T *buf, linenr_T lnum, int col)
2101{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002102 term_T *term = buf->b_term;
2103 sb_line_T *line;
2104 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002105
Bram Moolenaar70229f92017-07-29 16:01:53 +02002106 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002107 return 0;
2108 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2109 if (col >= line->sb_cols)
2110 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002111 cellattr = line->sb_cells + col;
2112 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002113}
2114
2115/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002116 * Create a new vterm and initialize it.
2117 */
2118 static void
2119create_vterm(term_T *term, int rows, int cols)
2120{
2121 VTerm *vterm;
2122 VTermScreen *screen;
2123
2124 vterm = vterm_new(rows, cols);
2125 term->tl_vterm = vterm;
2126 screen = vterm_obtain_screen(vterm);
2127 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2128 /* TODO: depends on 'encoding'. */
2129 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002130
2131 /* Vterm uses a default black background. Set it to white when
2132 * 'background' is "light". */
2133 if (*p_bg == 'l')
2134 {
2135 VTermColor fg, bg;
2136
2137 fg.red = fg.green = fg.blue = 0;
2138 bg.red = bg.green = bg.blue = 255;
2139 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2140 }
2141
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002142 /* Required to initialize most things. */
2143 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002144
2145 /* Allow using alternate screen. */
2146 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002147}
2148
Bram Moolenaar21554412017-07-24 21:44:43 +02002149/*
2150 * Return the text to show for the buffer name and status.
2151 */
2152 char_u *
2153term_get_status_text(term_T *term)
2154{
2155 if (term->tl_status_text == NULL)
2156 {
2157 char_u *txt;
2158 size_t len;
2159
Bram Moolenaar6d819742017-08-06 14:57:49 +02002160 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002161 {
2162 if (term_job_running(term))
2163 txt = (char_u *)_("Terminal");
2164 else
2165 txt = (char_u *)_("Terminal-finished");
2166 }
2167 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002168 txt = term->tl_title;
2169 else if (term_job_running(term))
2170 txt = (char_u *)_("running");
2171 else
2172 txt = (char_u *)_("finished");
2173 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002174 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002175 if (term->tl_status_text != NULL)
2176 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2177 term->tl_buffer->b_fname, txt);
2178 }
2179 return term->tl_status_text;
2180}
2181
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002182/*
2183 * Mark references in jobs of terminals.
2184 */
2185 int
2186set_ref_in_term(int copyID)
2187{
2188 int abort = FALSE;
2189 term_T *term;
2190 typval_T tv;
2191
2192 for (term = first_term; term != NULL; term = term->tl_next)
2193 if (term->tl_job != NULL)
2194 {
2195 tv.v_type = VAR_JOB;
2196 tv.vval.v_job = term->tl_job;
2197 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2198 }
2199 return abort;
2200}
2201
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002202/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002203 * Get the buffer from the first argument in "argvars".
2204 * Returns NULL when the buffer is not for a terminal window.
2205 */
2206 static buf_T *
2207term_get_buf(typval_T *argvars)
2208{
2209 buf_T *buf;
2210
2211 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2212 ++emsg_off;
2213 buf = get_buf_tv(&argvars[0], FALSE);
2214 --emsg_off;
2215 if (buf == NULL || buf->b_term == NULL)
2216 return NULL;
2217 return buf;
2218}
2219
2220/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002221 * "term_getaltscreen(buf)" function
2222 */
2223 void
2224f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2225{
2226 buf_T *buf = term_get_buf(argvars);
2227
2228 if (buf == NULL)
2229 return;
2230 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2231}
2232
2233/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002234 * "term_getattr(attr, name)" function
2235 */
2236 void
2237f_term_getattr(typval_T *argvars, typval_T *rettv)
2238{
2239 int attr;
2240 size_t i;
2241 char_u *name;
2242
2243 static struct {
2244 char *name;
2245 int attr;
2246 } attrs[] = {
2247 {"bold", HL_BOLD},
2248 {"italic", HL_ITALIC},
2249 {"underline", HL_UNDERLINE},
2250 {"strike", HL_STANDOUT},
2251 {"reverse", HL_INVERSE},
2252 };
2253
2254 attr = get_tv_number(&argvars[0]);
2255 name = get_tv_string_chk(&argvars[1]);
2256 if (name == NULL)
2257 return;
2258
2259 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2260 if (STRCMP(name, attrs[i].name) == 0)
2261 {
2262 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2263 break;
2264 }
2265}
2266
2267/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002268 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002269 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002270 void
2271f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002272{
Bram Moolenaar97870002017-07-30 18:28:38 +02002273 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002274 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002275 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002276 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002277
Bram Moolenaar97870002017-07-30 18:28:38 +02002278 if (rettv_list_alloc(rettv) == FAIL)
2279 return;
2280 if (buf == NULL)
2281 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002282 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002283
2284 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002285 list_append_number(l, term->tl_cursor_pos.row + 1);
2286 list_append_number(l, term->tl_cursor_pos.col + 1);
2287
2288 d = dict_alloc();
2289 if (d != NULL)
2290 {
2291 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2292 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2293 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2294 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2295 ? (char_u *)"" : term->tl_cursor_color);
2296 list_append_dict(l, d);
2297 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002298}
2299
2300/*
2301 * "term_getjob(buf)" function
2302 */
2303 void
2304f_term_getjob(typval_T *argvars, typval_T *rettv)
2305{
2306 buf_T *buf = term_get_buf(argvars);
2307
2308 rettv->v_type = VAR_JOB;
2309 rettv->vval.v_job = NULL;
2310 if (buf == NULL)
2311 return;
2312
2313 rettv->vval.v_job = buf->b_term->tl_job;
2314 if (rettv->vval.v_job != NULL)
2315 ++rettv->vval.v_job->jv_refcount;
2316}
2317
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002318 static int
2319get_row_number(typval_T *tv, term_T *term)
2320{
2321 if (tv->v_type == VAR_STRING
2322 && tv->vval.v_string != NULL
2323 && STRCMP(tv->vval.v_string, ".") == 0)
2324 return term->tl_cursor_pos.row;
2325 return (int)get_tv_number(tv) - 1;
2326}
2327
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002328/*
2329 * "term_getline(buf, row)" function
2330 */
2331 void
2332f_term_getline(typval_T *argvars, typval_T *rettv)
2333{
2334 buf_T *buf = term_get_buf(argvars);
2335 term_T *term;
2336 int row;
2337
2338 rettv->v_type = VAR_STRING;
2339 if (buf == NULL)
2340 return;
2341 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002342 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002343
2344 if (term->tl_vterm == NULL)
2345 {
2346 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2347
2348 /* vterm is finished, get the text from the buffer */
2349 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2350 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2351 }
2352 else
2353 {
2354 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2355 VTermRect rect;
2356 int len;
2357 char_u *p;
2358
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002359 if (row < 0 || row >= term->tl_rows)
2360 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002361 len = term->tl_cols * MB_MAXBYTES + 1;
2362 p = alloc(len);
2363 if (p == NULL)
2364 return;
2365 rettv->vval.v_string = p;
2366
2367 rect.start_col = 0;
2368 rect.end_col = term->tl_cols;
2369 rect.start_row = row;
2370 rect.end_row = row + 1;
2371 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2372 }
2373}
2374
2375/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002376 * "term_getscrolled(buf)" function
2377 */
2378 void
2379f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2380{
2381 buf_T *buf = term_get_buf(argvars);
2382
2383 if (buf == NULL)
2384 return;
2385 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2386}
2387
2388/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002389 * "term_getsize(buf)" function
2390 */
2391 void
2392f_term_getsize(typval_T *argvars, typval_T *rettv)
2393{
2394 buf_T *buf = term_get_buf(argvars);
2395 list_T *l;
2396
2397 if (rettv_list_alloc(rettv) == FAIL)
2398 return;
2399 if (buf == NULL)
2400 return;
2401
2402 l = rettv->vval.v_list;
2403 list_append_number(l, buf->b_term->tl_rows);
2404 list_append_number(l, buf->b_term->tl_cols);
2405}
2406
2407/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002408 * "term_getstatus(buf)" function
2409 */
2410 void
2411f_term_getstatus(typval_T *argvars, typval_T *rettv)
2412{
2413 buf_T *buf = term_get_buf(argvars);
2414 term_T *term;
2415 char_u val[100];
2416
2417 rettv->v_type = VAR_STRING;
2418 if (buf == NULL)
2419 return;
2420 term = buf->b_term;
2421
2422 if (term_job_running(term))
2423 STRCPY(val, "running");
2424 else
2425 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002426 if (term->tl_normal_mode)
2427 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002428 rettv->vval.v_string = vim_strsave(val);
2429}
2430
2431/*
2432 * "term_gettitle(buf)" function
2433 */
2434 void
2435f_term_gettitle(typval_T *argvars, typval_T *rettv)
2436{
2437 buf_T *buf = term_get_buf(argvars);
2438
2439 rettv->v_type = VAR_STRING;
2440 if (buf == NULL)
2441 return;
2442
2443 if (buf->b_term->tl_title != NULL)
2444 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2445}
2446
2447/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002448 * "term_gettty(buf)" function
2449 */
2450 void
2451f_term_gettty(typval_T *argvars, typval_T *rettv)
2452{
2453 buf_T *buf = term_get_buf(argvars);
2454 char_u *p;
2455
2456 rettv->v_type = VAR_STRING;
2457 if (buf == NULL)
2458 return;
2459 if (buf->b_term->tl_job != NULL)
2460 p = buf->b_term->tl_job->jv_tty_name;
2461 else
2462 p = buf->b_term->tl_tty_name;
2463 if (p != NULL)
2464 rettv->vval.v_string = vim_strsave(p);
2465}
2466
2467/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002468 * "term_list()" function
2469 */
2470 void
2471f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2472{
2473 term_T *tp;
2474 list_T *l;
2475
2476 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2477 return;
2478
2479 l = rettv->vval.v_list;
2480 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2481 if (tp != NULL && tp->tl_buffer != NULL)
2482 if (list_append_number(l,
2483 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2484 return;
2485}
2486
2487/*
2488 * "term_scrape(buf, row)" function
2489 */
2490 void
2491f_term_scrape(typval_T *argvars, typval_T *rettv)
2492{
2493 buf_T *buf = term_get_buf(argvars);
2494 VTermScreen *screen = NULL;
2495 VTermPos pos;
2496 list_T *l;
2497 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002498 char_u *p;
2499 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002500
2501 if (rettv_list_alloc(rettv) == FAIL)
2502 return;
2503 if (buf == NULL)
2504 return;
2505 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002506
2507 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002508 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002509
2510 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002511 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002512 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002513 p = NULL;
2514 line = NULL;
2515 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002516 else
2517 {
2518 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2519
2520 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2521 return;
2522 p = ml_get_buf(buf, lnum + 1, FALSE);
2523 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2524 }
2525
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002526 for (pos.col = 0; pos.col < term->tl_cols; )
2527 {
2528 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002529 int width;
2530 VTermScreenCellAttrs attrs;
2531 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002532 char_u rgb[8];
2533 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2534 int off = 0;
2535 int i;
2536
2537 if (screen == NULL)
2538 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002539 cellattr_T *cellattr;
2540 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002541
2542 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002543 if (pos.col >= line->sb_cols)
2544 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002545 cellattr = line->sb_cells + pos.col;
2546 width = cellattr->width;
2547 attrs = cellattr->attrs;
2548 fg = cellattr->fg;
2549 bg = cellattr->bg;
2550 len = MB_PTR2LEN(p);
2551 mch_memmove(mbs, p, len);
2552 mbs[len] = NUL;
2553 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002554 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002555 else
2556 {
2557 VTermScreenCell cell;
2558 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2559 break;
2560 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2561 {
2562 if (cell.chars[i] == 0)
2563 break;
2564 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2565 }
2566 mbs[off] = NUL;
2567 width = cell.width;
2568 attrs = cell.attrs;
2569 fg = cell.fg;
2570 bg = cell.bg;
2571 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002572 dcell = dict_alloc();
2573 list_append_dict(l, dcell);
2574
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002575 dict_add_nr_str(dcell, "chars", 0, mbs);
2576
2577 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002578 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002579 dict_add_nr_str(dcell, "fg", 0, rgb);
2580 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002581 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002582 dict_add_nr_str(dcell, "bg", 0, rgb);
2583
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002584 dict_add_nr_str(dcell, "attr",
2585 cell2attr(attrs, fg, bg), NULL);
2586 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002587
2588 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002589 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002590 ++pos.col;
2591 }
2592}
2593
2594/*
2595 * "term_sendkeys(buf, keys)" function
2596 */
2597 void
2598f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2599{
2600 buf_T *buf = term_get_buf(argvars);
2601 char_u *msg;
2602 term_T *term;
2603
2604 rettv->v_type = VAR_UNKNOWN;
2605 if (buf == NULL)
2606 return;
2607
2608 msg = get_tv_string_chk(&argvars[1]);
2609 if (msg == NULL)
2610 return;
2611 term = buf->b_term;
2612 if (term->tl_vterm == NULL)
2613 return;
2614
2615 while (*msg != NUL)
2616 {
2617 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2618 msg += MB_PTR2LEN(msg);
2619 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002620}
2621
2622/*
2623 * "term_start(command, options)" function
2624 */
2625 void
2626f_term_start(typval_T *argvars, typval_T *rettv)
2627{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002628 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002629
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002630 init_job_options(&opt);
2631 /* TODO: allow more job options */
2632 if (argvars[1].v_type != VAR_UNKNOWN
2633 && get_job_options(&argvars[1], &opt,
2634 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002635 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002636 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002637 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002638 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002639 return;
2640
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002641 if (opt.jo_vertical)
2642 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002643 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002644
2645 if (curbuf->b_term != NULL)
2646 rettv->vval.v_number = curbuf->b_fnum;
2647}
2648
2649/*
2650 * "term_wait" function
2651 */
2652 void
2653f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2654{
2655 buf_T *buf = term_get_buf(argvars);
2656
2657 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002658 {
2659 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002660 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002661 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002662 if (buf->b_term->tl_job == NULL)
2663 {
2664 ch_log(NULL, "term_wait(): no job to wait for");
2665 return;
2666 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002667
2668 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002669 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002670 {
2671 /* The job is dead, keep reading channel I/O until the channel is
2672 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002673 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002674 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2675 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002676 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002677 parse_queued_messages();
2678 ui_delay(10L, FALSE);
2679 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002680 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002681 parse_queued_messages();
2682 }
2683 else
2684 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002685 long wait = 10L;
2686
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002687 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002688 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002689
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002690 /* Wait for some time for any channel I/O. */
2691 if (argvars[1].v_type != VAR_UNKNOWN)
2692 wait = get_tv_number(&argvars[1]);
2693 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002694 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002695
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002696 /* Flushing messages on channels is hopefully sufficient.
2697 * TODO: is there a better way? */
2698 parse_queued_messages();
2699 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002700}
2701
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002702# ifdef WIN3264
2703
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002704/**************************************
2705 * 2. MS-Windows implementation.
2706 */
2707
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002708#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2709#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2710
Bram Moolenaar8a773062017-07-24 22:29:21 +02002711void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002712void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002713void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002714BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2715void (*winpty_config_set_initial_size)(void*, int, int);
2716LPCWSTR (*winpty_conin_name)(void*);
2717LPCWSTR (*winpty_conout_name)(void*);
2718LPCWSTR (*winpty_conerr_name)(void*);
2719void (*winpty_free)(void*);
2720void (*winpty_config_free)(void*);
2721void (*winpty_spawn_config_free)(void*);
2722void (*winpty_error_free)(void*);
2723LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002724BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002725HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002726
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002727#define WINPTY_DLL "winpty.dll"
2728
2729static HINSTANCE hWinPtyDLL = NULL;
2730
2731 int
2732dyn_winpty_init(void)
2733{
2734 int i;
2735 static struct
2736 {
2737 char *name;
2738 FARPROC *ptr;
2739 } winpty_entry[] =
2740 {
2741 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2742 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2743 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2744 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2745 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2746 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2747 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2748 {"winpty_free", (FARPROC*)&winpty_free},
2749 {"winpty_open", (FARPROC*)&winpty_open},
2750 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2751 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2752 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2753 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002754 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002755 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002756 {NULL, NULL}
2757 };
2758
2759 /* No need to initialize twice. */
2760 if (hWinPtyDLL)
2761 return 1;
2762 /* Load winpty.dll */
2763 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2764 if (!hWinPtyDLL)
2765 {
2766 EMSG2(_(e_loadlib), WINPTY_DLL);
2767 return 0;
2768 }
2769 for (i = 0; winpty_entry[i].name != NULL
2770 && winpty_entry[i].ptr != NULL; ++i)
2771 {
2772 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2773 winpty_entry[i].name)) == NULL)
2774 {
2775 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2776 return 0;
2777 }
2778 }
2779
2780 return 1;
2781}
2782
2783/*
2784 * Create a new terminal of "rows" by "cols" cells.
2785 * Store a reference in "term".
2786 * Return OK or FAIL.
2787 */
2788 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002789term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002790{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002791 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002792 channel_T *channel = NULL;
2793 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002794 DWORD error;
2795 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2796 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002797 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002798 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002799 garray_T ga;
2800 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002801
2802 if (!dyn_winpty_init())
2803 return FAIL;
2804
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002805 if (argvar->v_type == VAR_STRING)
2806 cmd = argvar->vval.v_string;
2807 else
2808 {
2809 ga_init2(&ga, (int)sizeof(char*), 20);
2810 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2811 goto failed;
2812 cmd = ga.ga_data;
2813 }
2814
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002815 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002816 if (p == NULL)
2817 return FAIL;
2818
2819 job = job_alloc();
2820 if (job == NULL)
2821 goto failed;
2822
2823 channel = add_channel();
2824 if (channel == NULL)
2825 goto failed;
2826
2827 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2828 if (term->tl_winpty_config == NULL)
2829 goto failed;
2830
2831 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2832 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2833 if (term->tl_winpty == NULL)
2834 goto failed;
2835
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002836 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002837 spawn_config = winpty_spawn_config_new(
2838 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2839 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2840 NULL,
2841 p,
2842 NULL,
2843 NULL,
2844 &winpty_err);
2845 if (spawn_config == NULL)
2846 goto failed;
2847
2848 channel = add_channel();
2849 if (channel == NULL)
2850 goto failed;
2851
2852 job = job_alloc();
2853 if (job == NULL)
2854 goto failed;
2855
2856 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2857 &child_thread_handle, &error, &winpty_err))
2858 goto failed;
2859
2860 channel_set_pipes(channel,
2861 (sock_T) CreateFileW(
2862 winpty_conin_name(term->tl_winpty),
2863 GENERIC_WRITE, 0, NULL,
2864 OPEN_EXISTING, 0, NULL),
2865 (sock_T) CreateFileW(
2866 winpty_conout_name(term->tl_winpty),
2867 GENERIC_READ, 0, NULL,
2868 OPEN_EXISTING, 0, NULL),
2869 (sock_T) CreateFileW(
2870 winpty_conerr_name(term->tl_winpty),
2871 GENERIC_READ, 0, NULL,
2872 OPEN_EXISTING, 0, NULL));
2873
2874 jo = CreateJobObject(NULL, NULL);
2875 if (jo == NULL)
2876 goto failed;
2877
2878 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002879 {
2880 /* Failed, switch the way to terminate process with TerminateProcess. */
2881 CloseHandle(jo);
2882 jo = NULL;
2883 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002884
2885 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002886 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002887
2888 create_vterm(term, rows, cols);
2889
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002890 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002891 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002892
2893 job->jv_channel = channel;
2894 job->jv_proc_info.hProcess = child_process_handle;
2895 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2896 job->jv_job_object = jo;
2897 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002898 sprintf(buf, "winpty://%lu",
2899 GetProcessId(winpty_agent_process(term->tl_winpty)));
2900 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002901 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002902 term->tl_job = job;
2903
2904 return OK;
2905
2906failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002907 if (argvar->v_type == VAR_LIST)
2908 vim_free(ga.ga_data);
2909 if (p != NULL)
2910 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002911 if (spawn_config != NULL)
2912 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002913 if (channel != NULL)
2914 channel_clear(channel);
2915 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002916 {
2917 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002919 }
2920 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002921 if (jo != NULL)
2922 CloseHandle(jo);
2923 if (term->tl_winpty != NULL)
2924 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002925 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002926 if (term->tl_winpty_config != NULL)
2927 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002928 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929 if (winpty_err != NULL)
2930 {
2931 char_u *msg = utf16_to_enc(
2932 (short_u *)winpty_error_msg(winpty_err), NULL);
2933
2934 EMSG(msg);
2935 winpty_error_free(winpty_err);
2936 }
2937 return FAIL;
2938}
2939
2940/*
2941 * Free the terminal emulator part of "term".
2942 */
2943 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002944term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002945{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002946 if (term->tl_winpty != NULL)
2947 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002948 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002949 if (term->tl_winpty_config != NULL)
2950 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002951 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002952 if (term->tl_vterm != NULL)
2953 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002954 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002955}
2956
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002957/*
2958 * Request size to terminal.
2959 */
2960 static void
2961term_report_winsize(term_T *term, int rows, int cols)
2962{
2963 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2964}
2965
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002966# else
2967
2968/**************************************
2969 * 3. Unix-like implementation.
2970 */
2971
2972/*
2973 * Create a new terminal of "rows" by "cols" cells.
2974 * Start job for "cmd".
2975 * Store the pointers in "term".
2976 * Return OK or FAIL.
2977 */
2978 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002979term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002980{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002981 create_vterm(term, rows, cols);
2982
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002983 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002984 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002985 if (term->tl_job != NULL)
2986 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002987
Bram Moolenaar61a66052017-07-22 18:39:00 +02002988 return term->tl_job != NULL
2989 && term->tl_job->jv_channel != NULL
2990 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002991}
2992
2993/*
2994 * Free the terminal emulator part of "term".
2995 */
2996 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002997term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002998{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002999 if (term->tl_vterm != NULL)
3000 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02003001 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003002}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003003
3004/*
3005 * Request size to terminal.
3006 */
3007 static void
3008term_report_winsize(term_T *term, int rows, int cols)
3009{
3010 /* Use an ioctl() to report the new window size to the job. */
3011 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
3012 {
3013 int fd = -1;
3014 int part;
3015
3016 for (part = PART_OUT; part < PART_COUNT; ++part)
3017 {
3018 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
3019 if (isatty(fd))
3020 break;
3021 }
3022 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02003023 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02003024 }
3025}
3026
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003027# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003028
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003029#endif /* FEAT_TERMINAL */