blob: 7e3acf96e1ea618fb181cd261655d69b493961bd [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 Moolenaard85f2712017-07-28 21:51:57 +020041 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020042 * For the GUI fill termios with default values, perhaps like pangoterm:
43 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
44 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020045 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020046 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020047 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020048 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020049 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
50 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020051 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020052 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020053 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020055 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020056 * terminal. Allow:
57 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
58 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
59 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
60 * Check that something is connected to the terminal.
61 * Test: "cat" reading from a file or buffer
62 * "ls" writing stdout to a file or buffer
63 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020064 * - support ":term NONE" to open a terminal with a pty but not running a job
65 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020066 * - if the job in the terminal does not support the mouse, we can use the
67 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020068 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
69 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020070 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020071 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020072 * - Copy text in the vterm to the Vim buffer once in a while, so that
73 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020074 */
75
76#include "vim.h"
77
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020078#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020079
Bram Moolenaard5310982017-08-05 15:16:32 +020080#ifndef MIN
81# define MIN(x,y) ((x) < (y) ? (x) : (y))
82#endif
83#ifndef MAX
84# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020085#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020086
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020087#include "libvterm/include/vterm.h"
88
Bram Moolenaar33a43be2017-08-06 21:36:22 +020089/* This is VTermScreenCell without the characters, thus much smaller. */
90typedef struct {
91 VTermScreenCellAttrs attrs;
92 char width;
93 VTermColor fg, bg;
94} cellattr_T;
95
Bram Moolenaard85f2712017-07-28 21:51:57 +020096typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020097 int sb_cols; /* can differ per line */
98 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +020099} sb_line_T;
100
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200101/* typedef term_T in structs.h */
102struct terminal_S {
103 term_T *tl_next;
104
Bram Moolenaar423802d2017-07-30 16:52:24 +0200105 VTerm *tl_vterm;
106 job_T *tl_job;
107 buf_T *tl_buffer;
108
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200109 /* used when tl_job is NULL and only a pty was created */
110 int tl_tty_fd;
111 char_u *tl_tty_name;
112
Bram Moolenaar6d819742017-08-06 14:57:49 +0200113 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200114 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200115 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200116 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200117
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200118#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200119 void *tl_winpty_config;
120 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200122
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200123 /* last known vterm size */
124 int tl_rows;
125 int tl_cols;
126 /* vterm size does not follow window size */
127 int tl_rows_fixed;
128 int tl_cols_fixed;
129
Bram Moolenaar21554412017-07-24 21:44:43 +0200130 char_u *tl_title; /* NULL or allocated */
131 char_u *tl_status_text; /* NULL or allocated */
132
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200133 /* Range of screen rows to update. Zero based. */
134 int tl_dirty_row_start; /* -1 if nothing dirty */
135 int tl_dirty_row_end; /* row below last one to update */
136
Bram Moolenaard85f2712017-07-28 21:51:57 +0200137 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200138 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200140 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200141 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200142 int tl_cursor_blink;
143 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
144 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200145
146 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200147};
148
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200149#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
150#define TMODE_LOOP 2 /* CTRL-W N used */
151
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200152/*
153 * List of all active terminals.
154 */
155static term_T *first_term = NULL;
156
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200157/* Terminal active in terminal_loop(). */
158static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200159
160#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
161#define KEY_BUF_LEN 200
162
163/*
164 * Functions with separate implementation for MS-Windows and Unix-like systems.
165 */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200166static int term_and_job_init(term_T *term, int rows, int cols,
167 typval_T *argvar, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200168static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200169static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200170
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200171/**************************************
172 * 1. Generic code for all systems.
173 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200174
175/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200176 * Determine the terminal size from 'termsize' and the current window.
177 * Assumes term->tl_rows and term->tl_cols are zero.
178 */
179 static void
180set_term_and_win_size(term_T *term)
181{
182 if (*curwin->w_p_tms != NUL)
183 {
184 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
185
186 term->tl_rows = atoi((char *)curwin->w_p_tms);
187 term->tl_cols = atoi((char *)p);
188 }
189 if (term->tl_rows == 0)
190 term->tl_rows = curwin->w_height;
191 else
192 {
193 win_setheight_win(term->tl_rows, curwin);
194 term->tl_rows_fixed = TRUE;
195 }
196 if (term->tl_cols == 0)
197 term->tl_cols = curwin->w_width;
198 else
199 {
200 win_setwidth_win(term->tl_cols, curwin);
201 term->tl_cols_fixed = TRUE;
202 }
203}
204
205/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200206 * Initialize job options for a terminal job.
207 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200208 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200209 static void
210init_job_options(jobopt_T *opt)
211{
212 clear_job_options(opt);
213
214 opt->jo_mode = MODE_RAW;
215 opt->jo_out_mode = MODE_RAW;
216 opt->jo_err_mode = MODE_RAW;
217 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
218
219 opt->jo_io[PART_OUT] = JIO_BUFFER;
220 opt->jo_io[PART_ERR] = JIO_BUFFER;
221 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
222
223 opt->jo_modifiable[PART_OUT] = 0;
224 opt->jo_modifiable[PART_ERR] = 0;
225 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
226
227 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
228}
229
230/*
231 * Set job options mandatory for a terminal job.
232 */
233 static void
234setup_job_options(jobopt_T *opt, int rows, int cols)
235{
236 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
237 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
238 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200239 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
240 opt->jo_term_rows = rows;
241 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
242 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200243}
244
245 static void
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200246term_start(typval_T *argvar, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200247{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248 exarg_T split_ea;
249 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200251 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252
253 if (check_restricted() || check_secure())
254 return;
255
256 term = (term_T *)alloc_clear(sizeof(term_T));
257 if (term == NULL)
258 return;
259 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200260 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200261 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200262 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200263 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200264
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200266 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200267 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200268 /* Create a new buffer in the current window. */
269 if (!can_abandon(curbuf, forceit))
270 {
271 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200272 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200273 return;
274 }
275 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
276 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200277 {
278 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200279 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200280 }
281 }
282 else if (opt->jo_hidden)
283 {
284 buf_T *buf;
285
286 /* Create a new buffer without a window. Make it the current buffer for
287 * a moment to be able to do the initialisations. */
288 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
289 BLN_NEW | BLN_LISTED);
290 if (buf == NULL || ml_open(buf) == FAIL)
291 {
292 vim_free(term);
293 return;
294 }
295 old_curbuf = curbuf;
296 --curbuf->b_nwindows;
297 curbuf = buf;
298 curwin->w_buffer = buf;
299 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200300 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200301 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200303 /* Open a new window or tab. */
304 split_ea.cmdidx = CMD_new;
305 split_ea.cmd = (char_u *)"new";
306 split_ea.arg = (char_u *)"";
307 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
308 {
309 split_ea.line2 = opt->jo_term_rows;
310 split_ea.addr_count = 1;
311 }
312 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
313 {
314 split_ea.line2 = opt->jo_term_cols;
315 split_ea.addr_count = 1;
316 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200317
Bram Moolenaarda43b612017-08-11 22:27:50 +0200318 ex_splitview(&split_ea);
319 if (curwin == old_curwin)
320 {
321 /* split failed */
322 vim_free(term);
323 return;
324 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200325 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200326 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200328
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200329 if (!opt->jo_hidden)
330 {
331 /* only one size was taken care of with :new, do the other one */
332 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
333 win_setheight(opt->jo_term_rows);
334 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
335 win_setwidth(opt->jo_term_cols);
336 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200337
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200338 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200339 term->tl_next = first_term;
340 first_term = term;
341
Bram Moolenaar78712a72017-08-05 14:50:12 +0200342 if (opt->jo_term_name != NULL)
343 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
344 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200345 {
346 int i;
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200347 size_t len;
348 char_u *cmd, *p;
349
350 if (argvar->v_type == VAR_STRING)
351 cmd = argvar->vval.v_string;
352 else if (argvar->v_type != VAR_LIST
353 || argvar->vval.v_list == NULL
354 || argvar->vval.v_list->lv_len < 1)
355 cmd = (char_u*)"";
356 else
357 cmd = get_tv_string_chk(&argvar->vval.v_list->lv_first->li_tv);
358
359 len = STRLEN(cmd) + 10;
360 p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200361
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200362 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200363 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200364 /* Prepend a ! to the command name to avoid the buffer name equals
365 * the executable, otherwise ":w!" would overwrite it. */
366 if (i == 0)
367 vim_snprintf((char *)p, len, "!%s", cmd);
368 else
369 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200370 if (buflist_findname(p) == NULL)
371 {
372 curbuf->b_ffname = p;
373 break;
374 }
375 }
376 }
377 curbuf->b_fname = curbuf->b_ffname;
378
Bram Moolenaar37c45832017-08-12 16:01:04 +0200379 if (opt->jo_term_opencmd != NULL)
380 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
381
Bram Moolenaareb44a682017-08-03 22:44:55 +0200382 set_string_option_direct((char_u *)"buftype", -1,
383 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
384
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200385 /* Mark the buffer as not modifiable. It can only be made modifiable after
386 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200387 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200388
389 /* Set 'bufhidden' to "hide": allow closing the window. */
390 set_string_option_direct((char_u *)"bufhidden", -1,
391 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200392
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200393 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200394 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200395
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200396 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200397 if (term_and_job_init(term, term->tl_rows, term->tl_cols, argvar, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200398 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200399 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200400 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200401 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200402
403 if (old_curbuf != NULL)
404 {
405 --curbuf->b_nwindows;
406 curbuf = old_curbuf;
407 curwin->w_buffer = curbuf;
408 ++curbuf->b_nwindows;
409 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200410 }
411 else
412 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200413 buf_T *buf = curbuf;
414
Bram Moolenaard85f2712017-07-28 21:51:57 +0200415 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200416 if (old_curbuf != NULL)
417 {
418 --curbuf->b_nwindows;
419 curbuf = old_curbuf;
420 curwin->w_buffer = curbuf;
421 ++curbuf->b_nwindows;
422 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200423
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200424 /* Wiping out the buffer will also close the window and call
425 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200426 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200427 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200428}
429
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200430/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200431 * ":terminal": open a terminal window and execute a job in it.
432 */
433 void
434ex_terminal(exarg_T *eap)
435{
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200436 typval_T argvar;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200437 jobopt_T opt;
438 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200439
440 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200441
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200442 cmd = eap->arg;
443 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
444 {
445 char_u *p;
446
447 cmd += 2;
448 p = skiptowhite(cmd);
449 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
450 opt.jo_term_finish = 'c';
451 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
452 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200453 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
454 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200455 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
456 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200457 else
458 {
459 if (*p)
460 *p = NUL;
461 EMSG2(_("E181: Invalid attribute: %s"), cmd);
462 return;
463 }
464 cmd = skipwhite(p);
465 }
Bram Moolenaar2438ae32017-08-13 17:38:11 +0200466 if (cmd == NULL || *cmd == NUL)
467 cmd = p_sh;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200468
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200469 if (eap->addr_count == 2)
470 {
471 opt.jo_term_rows = eap->line1;
472 opt.jo_term_cols = eap->line2;
473 }
474 else if (eap->addr_count == 1)
475 {
476 if (cmdmod.split & WSP_VERT)
477 opt.jo_term_cols = eap->line2;
478 else
479 opt.jo_term_rows = eap->line2;
480 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200481
Bram Moolenaardcaa6132017-08-13 17:13:09 +0200482 argvar.v_type = VAR_STRING;
483 argvar.vval.v_string = cmd;
484 term_start(&argvar, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200485}
486
487/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200488 * Free the scrollback buffer for "term".
489 */
490 static void
491free_scrollback(term_T *term)
492{
493 int i;
494
495 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
496 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
497 ga_clear(&term->tl_scrollback);
498}
499
500/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200501 * Free a terminal and everything it refers to.
502 * Kills the job if there is one.
503 * Called when wiping out a buffer.
504 */
505 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200506free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200507{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200508 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200509 term_T *tp;
510
511 if (term == NULL)
512 return;
513 if (first_term == term)
514 first_term = term->tl_next;
515 else
516 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
517 if (tp->tl_next == term)
518 {
519 tp->tl_next = term->tl_next;
520 break;
521 }
522
523 if (term->tl_job != NULL)
524 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200525 if (term->tl_job->jv_status != JOB_ENDED
526 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200527 job_stop(term->tl_job, NULL, "kill");
528 job_unref(term->tl_job);
529 }
530
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200531 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200532
533 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200534 vim_free(term->tl_title);
535 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200536 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200537 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200538 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200539 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200540 if (in_terminal_loop == term)
541 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200542}
543
544/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200545 * Write job output "msg[len]" to the vterm.
546 */
547 static void
548term_write_job_output(term_T *term, char_u *msg, size_t len)
549{
550 VTerm *vterm = term->tl_vterm;
551 char_u *p;
552 size_t done;
553 size_t len_now;
554
555 for (done = 0; done < len; done += len_now)
556 {
557 for (p = msg + done; p < msg + len; )
558 {
559 if (*p == NL)
560 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200561 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200562 }
563 len_now = p - msg - done;
564 vterm_input_write(vterm, (char *)msg + done, len_now);
565 if (p < msg + len && *p == NL)
566 {
567 /* Convert NL to CR-NL, that appears to work best. */
568 vterm_input_write(vterm, "\r\n", 2);
569 ++len_now;
570 }
571 }
572
573 /* this invokes the damage callbacks */
574 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
575}
576
Bram Moolenaar1c844932017-07-24 23:36:41 +0200577 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200578update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200579{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200580 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200581 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200582 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200583 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200584 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200585 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200586 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200587 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200588#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200589 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200590 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200591#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200592 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200593}
594
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200595/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200596 * Invoked when "msg" output from a job was received. Write it to the terminal
597 * of "buffer".
598 */
599 void
600write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
601{
602 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200603 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200604
Bram Moolenaard85f2712017-07-28 21:51:57 +0200605 if (term->tl_vterm == NULL)
606 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200607 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200608 return;
609 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200610 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200611 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200612
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200613 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
614 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200615 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200616 {
617 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200618 ch_log(term->tl_job->jv_channel, "updating screen");
619 if (buffer == curbuf)
620 {
621 update_screen(0);
622 update_cursor(term, TRUE);
623 }
624 else
625 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200626 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200627}
628
629/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200630 * Send a mouse position and click to the vterm
631 */
632 static int
633term_send_mouse(VTerm *vterm, int button, int pressed)
634{
635 VTermModifier mod = VTERM_MOD_NONE;
636
637 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
638 mouse_col - W_WINCOL(curwin), mod);
639 vterm_mouse_button(vterm, button, pressed, mod);
640 return TRUE;
641}
642
643/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200644 * Convert typed key "c" into bytes to send to the job.
645 * Return the number of bytes in "buf".
646 */
647 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200648term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200649{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200650 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200651 VTermKey key = VTERM_KEY_NONE;
652 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200653 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200654
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200655 switch (c)
656 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200657 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200658 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200659 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
660 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200661 case K_DEL: key = VTERM_KEY_DEL; break;
662 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200663 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
664 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200665 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200666 case K_S_END: mod = VTERM_MOD_SHIFT;
667 key = VTERM_KEY_END; break;
668 case K_C_END: mod = VTERM_MOD_CTRL;
669 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200670 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
671 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
672 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
673 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
674 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
675 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
676 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
677 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
678 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
679 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
680 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
681 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
682 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200683 case K_S_HOME: mod = VTERM_MOD_SHIFT;
684 key = VTERM_KEY_HOME; break;
685 case K_C_HOME: mod = VTERM_MOD_CTRL;
686 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200687 case K_INS: key = VTERM_KEY_INS; break;
688 case K_K0: key = VTERM_KEY_KP_0; break;
689 case K_K1: key = VTERM_KEY_KP_1; break;
690 case K_K2: key = VTERM_KEY_KP_2; break;
691 case K_K3: key = VTERM_KEY_KP_3; break;
692 case K_K4: key = VTERM_KEY_KP_4; break;
693 case K_K5: key = VTERM_KEY_KP_5; break;
694 case K_K6: key = VTERM_KEY_KP_6; break;
695 case K_K7: key = VTERM_KEY_KP_7; break;
696 case K_K8: key = VTERM_KEY_KP_8; break;
697 case K_K9: key = VTERM_KEY_KP_9; break;
698 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
699 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
700 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
701 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
702 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
703 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
704 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
705 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
706 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
707 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
708 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
709 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
710 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200711 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
712 key = VTERM_KEY_LEFT; break;
713 case K_C_LEFT: mod = VTERM_MOD_CTRL;
714 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200715 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
716 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
717 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200718 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
719 key = VTERM_KEY_RIGHT; break;
720 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
721 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200722 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200723 case K_S_UP: mod = VTERM_MOD_SHIFT;
724 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200725 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200726
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200727 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
728 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
729 case K_MOUSELEFT: /* TODO */ return 0;
730 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200731
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200732 case K_LEFTMOUSE:
733 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
734 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
735 case K_LEFTRELEASE:
736 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
737 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
738 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
739 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
740 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
741 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
742 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
743 case K_X1MOUSE: /* TODO */ return 0;
744 case K_X1DRAG: /* TODO */ return 0;
745 case K_X1RELEASE: /* TODO */ return 0;
746 case K_X2MOUSE: /* TODO */ return 0;
747 case K_X2DRAG: /* TODO */ return 0;
748 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200749
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200750 case K_IGNORE: return 0;
751 case K_NOP: return 0;
752 case K_UNDO: return 0;
753 case K_HELP: return 0;
754 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
755 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
756 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
757 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
758 case K_SELECT: return 0;
759#ifdef FEAT_GUI
760 case K_VER_SCROLLBAR: return 0;
761 case K_HOR_SCROLLBAR: return 0;
762#endif
763#ifdef FEAT_GUI_TABLINE
764 case K_TABLINE: return 0;
765 case K_TABMENU: return 0;
766#endif
767#ifdef FEAT_NETBEANS_INTG
768 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
769#endif
770#ifdef FEAT_DND
771 case K_DROP: return 0;
772#endif
773#ifdef FEAT_AUTOCMD
774 case K_CURSORHOLD: return 0;
775#endif
776 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
777 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200778 }
779
780 /*
781 * Convert special keys to vterm keys:
782 * - Write keys to vterm: vterm_keyboard_key()
783 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200784 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200785 */
786 if (key != VTERM_KEY_NONE)
787 /* Special key, let vterm convert it. */
788 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200789 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200790 /* Normal character, let vterm convert it. */
791 vterm_keyboard_unichar(vterm, c, mod);
792
793 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200794 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200795}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200796
Bram Moolenaar938783d2017-07-16 20:13:26 +0200797/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200798 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200799 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200800 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200801term_job_running(term_T *term)
802{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200803 /* Also consider the job finished when the channel is closed, to avoid a
804 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200805 return term != NULL
806 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200807 && term->tl_job->jv_status == JOB_STARTED
808 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200809}
810
811/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200812 * Add the last line of the scrollback buffer to the buffer in the window.
813 */
814 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200815add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200816{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200817 buf_T *buf = term->tl_buffer;
818 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
819 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200820
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200821 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200822 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200823 {
824 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200825 curbuf = buf;
826 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200827 curbuf = curwin->w_buffer;
828 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200829}
830
831/*
832 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200833 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200834 */
835 static void
836move_terminal_to_buffer(term_T *term)
837{
838 win_T *wp;
839 int len;
840 int lines_skipped = 0;
841 VTermPos pos;
842 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200843 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200844 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200845
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200846 if (term->tl_vterm == NULL)
847 return;
848 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200849 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
850 {
851 len = 0;
852 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
853 if (vterm_screen_get_cell(screen, pos, &cell) != 0
854 && cell.chars[0] != NUL)
855 len = pos.col + 1;
856
857 if (len == 0)
858 ++lines_skipped;
859 else
860 {
861 while (lines_skipped > 0)
862 {
863 /* Line was skipped, add an empty line. */
864 --lines_skipped;
865 if (ga_grow(&term->tl_scrollback, 1) == OK)
866 {
867 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
868 + term->tl_scrollback.ga_len;
869
870 line->sb_cols = 0;
871 line->sb_cells = NULL;
872 ++term->tl_scrollback.ga_len;
873
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200874 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875 }
876 }
877
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200878 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200879 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
880 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200881 garray_T ga;
882 int width;
883 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200884 + term->tl_scrollback.ga_len;
885
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200886 ga_init2(&ga, 1, 100);
887 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200888 {
889 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200890 {
891 width = 1;
892 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
893 if (ga_grow(&ga, 1) == OK)
894 ga.ga_len += mb_char2bytes(' ',
895 (char_u *)ga.ga_data + ga.ga_len);
896 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200897 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200898 {
899 width = cell.width;
900
901 p[pos.col].width = cell.width;
902 p[pos.col].attrs = cell.attrs;
903 p[pos.col].fg = cell.fg;
904 p[pos.col].bg = cell.bg;
905
906 if (ga_grow(&ga, MB_MAXBYTES) == OK)
907 {
908 int i;
909 int c;
910
911 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
912 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
913 (char_u *)ga.ga_data + ga.ga_len);
914 }
915 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200916 }
917 line->sb_cols = len;
918 line->sb_cells = p;
919 ++term->tl_scrollback.ga_len;
920
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200921 if (ga_grow(&ga, 1) == FAIL)
922 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
923 else
924 {
925 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
926 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
927 }
928 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929 }
930 else
931 vim_free(p);
932 }
933 }
934
935 FOR_ALL_WINDOWS(wp)
936 {
937 if (wp->w_buffer == term->tl_buffer)
938 {
939 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
940 wp->w_cursor.col = 0;
941 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200942 if (wp->w_cursor.lnum >= wp->w_height)
943 {
944 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
945
946 if (wp->w_topline < min_topline)
947 wp->w_topline = min_topline;
948 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200949 redraw_win_later(wp, NOT_VALID);
950 }
951 }
952}
953
954 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200955set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200957 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200958 vim_free(term->tl_status_text);
959 term->tl_status_text = NULL;
960 if (term->tl_buffer == curbuf)
961 maketitle();
962}
963
964/*
965 * Called after the job if finished and Terminal mode is not active:
966 * Move the vterm contents into the scrollback buffer and free the vterm.
967 */
968 static void
969cleanup_vterm(term_T *term)
970{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200971 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200972 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200973 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200974 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200975}
976
977/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200978 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200979 * Suspends updating the terminal window.
980 */
981 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200982term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200983{
984 term_T *term = curbuf->b_term;
985
986 /* Append the current terminal contents to the buffer. */
987 move_terminal_to_buffer(term);
988
Bram Moolenaar6d819742017-08-06 14:57:49 +0200989 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200990
Bram Moolenaar6d819742017-08-06 14:57:49 +0200991 /* Move the window cursor to the position of the cursor in the
992 * terminal. */
993 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
994 + term->tl_cursor_pos.row + 1;
995 check_cursor();
996 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200997
Bram Moolenaar6d819742017-08-06 14:57:49 +0200998 /* Display the same lines as in the terminal. */
999 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001000}
1001
1002/*
1003 * Returns TRUE if the current window contains a terminal and we are in
1004 * Terminal-Normal mode.
1005 */
1006 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001007term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001008{
1009 term_T *term = curbuf->b_term;
1010
Bram Moolenaar6d819742017-08-06 14:57:49 +02001011 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012}
1013
1014/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001015 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001016 * Restores updating the terminal window.
1017 */
1018 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001019term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001020{
1021 term_T *term = curbuf->b_term;
1022 sb_line_T *line;
1023 garray_T *gap;
1024
1025 /* Remove the terminal contents from the scrollback and the buffer. */
1026 gap = &term->tl_scrollback;
1027 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1028 {
1029 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1030 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1031 vim_free(line->sb_cells);
1032 --gap->ga_len;
1033 if (gap->ga_len == 0)
1034 break;
1035 }
1036 check_cursor();
1037
Bram Moolenaar6d819742017-08-06 14:57:49 +02001038 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001039
1040 if (term->tl_channel_closed)
1041 cleanup_vterm(term);
1042 redraw_buf_and_status_later(curbuf, NOT_VALID);
1043}
1044
1045/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001046 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001047 * Note: while waiting a terminal may be closed and freed if the channel is
1048 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001049 * TODO: use terminal mode mappings.
1050 */
1051 static int
1052term_vgetc()
1053{
1054 int c;
1055
1056 ++no_mapping;
1057 ++allow_keys;
1058 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001059#ifdef WIN3264
1060 ctrl_break_was_pressed = FALSE;
1061#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001062 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001063 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001064 --no_mapping;
1065 --allow_keys;
1066 return c;
1067}
1068
1069/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001070 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001071 * Return FAIL when the key needs to be handled in Normal mode.
1072 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001073 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001074 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001075send_keys_to_term(term_T *term, int c, int typed)
1076{
1077 char msg[KEY_BUF_LEN];
1078 size_t len;
1079 static int mouse_was_outside = FALSE;
1080 int dragging_outside = FALSE;
1081
1082 /* Catch keys that need to be handled as in Normal mode. */
1083 switch (c)
1084 {
1085 case NUL:
1086 case K_ZERO:
1087 if (typed)
1088 stuffcharReadbuff(c);
1089 return FAIL;
1090
1091 case K_IGNORE:
1092 return FAIL;
1093
1094 case K_LEFTDRAG:
1095 case K_MIDDLEDRAG:
1096 case K_RIGHTDRAG:
1097 case K_X1DRAG:
1098 case K_X2DRAG:
1099 dragging_outside = mouse_was_outside;
1100 /* FALLTHROUGH */
1101 case K_LEFTMOUSE:
1102 case K_LEFTMOUSE_NM:
1103 case K_LEFTRELEASE:
1104 case K_LEFTRELEASE_NM:
1105 case K_MIDDLEMOUSE:
1106 case K_MIDDLERELEASE:
1107 case K_RIGHTMOUSE:
1108 case K_RIGHTRELEASE:
1109 case K_X1MOUSE:
1110 case K_X1RELEASE:
1111 case K_X2MOUSE:
1112 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001113
1114 case K_MOUSEUP:
1115 case K_MOUSEDOWN:
1116 case K_MOUSELEFT:
1117 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001118 if (mouse_row < W_WINROW(curwin)
1119 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1120 || mouse_col < W_WINCOL(curwin)
1121 || mouse_col >= W_ENDCOL(curwin)
1122 || dragging_outside)
1123 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001124 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001125 if (typed)
1126 {
1127 stuffcharReadbuff(c);
1128 mouse_was_outside = TRUE;
1129 }
1130 return FAIL;
1131 }
1132 }
1133 if (typed)
1134 mouse_was_outside = FALSE;
1135
1136 /* Convert the typed key to a sequence of bytes for the job. */
1137 len = term_convert_key(term, c, msg);
1138 if (len > 0)
1139 /* TODO: if FAIL is returned, stop? */
1140 channel_send(term->tl_job->jv_channel, PART_IN,
1141 (char_u *)msg, (int)len, NULL);
1142
1143 return OK;
1144}
1145
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001146 static void
1147position_cursor(win_T *wp, VTermPos *pos)
1148{
1149 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1150 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1151 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1152}
1153
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001154/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001155 * Handle CTRL-W "": send register contents to the job.
1156 */
1157 static void
1158term_paste_register(int prev_c UNUSED)
1159{
1160 int c;
1161 list_T *l;
1162 listitem_T *item;
1163 long reglen = 0;
1164 int type;
1165
1166#ifdef FEAT_CMDL_INFO
1167 if (add_to_showcmd(prev_c))
1168 if (add_to_showcmd('"'))
1169 out_flush();
1170#endif
1171 c = term_vgetc();
1172#ifdef FEAT_CMDL_INFO
1173 clear_showcmd();
1174#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001175 if (!term_use_loop())
1176 /* job finished while waiting for a character */
1177 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001178
1179 /* CTRL-W "= prompt for expression to evaluate. */
1180 if (c == '=' && get_expr_register() != '=')
1181 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001182 if (!term_use_loop())
1183 /* job finished while waiting for a character */
1184 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001185
1186 l = (list_T *)get_reg_contents(c, GREG_LIST);
1187 if (l != NULL)
1188 {
1189 type = get_reg_type(c, &reglen);
1190 for (item = l->lv_first; item != NULL; item = item->li_next)
1191 {
1192 char_u *s = get_tv_string(&item->li_tv);
1193
1194 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1195 s, STRLEN(s), NULL);
1196 if (item->li_next != NULL || type == MLINE)
1197 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1198 (char_u *)"\r", 1, NULL);
1199 }
1200 list_free(l);
1201 }
1202}
1203
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001204#if defined(FEAT_GUI) || defined(PROTO)
1205/*
1206 * Return TRUE when the cursor of the terminal should be displayed.
1207 */
1208 int
1209use_terminal_cursor()
1210{
1211 return in_terminal_loop != NULL;
1212}
1213
1214 cursorentry_T *
1215term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1216{
1217 term_T *term = in_terminal_loop;
1218 static cursorentry_T entry;
1219
1220 vim_memset(&entry, 0, sizeof(entry));
1221 entry.shape = entry.mshape =
1222 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1223 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1224 SHAPE_BLOCK;
1225 entry.percentage = 20;
1226 if (term->tl_cursor_blink)
1227 {
1228 entry.blinkwait = 700;
1229 entry.blinkon = 400;
1230 entry.blinkon = 250;
1231 }
1232 *fg = gui.back_pixel;
1233 if (term->tl_cursor_color == NULL)
1234 *bg = gui.norm_pixel;
1235 else
1236 *bg = color_name2handle(term->tl_cursor_color);
1237 entry.name = "n";
1238 entry.used_for = SHAPE_CURSOR;
1239
1240 return &entry;
1241}
1242#endif
1243
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001244static int did_change_cursor = FALSE;
1245
1246 static void
1247may_set_cursor_props(term_T *term)
1248{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001249#ifdef FEAT_GUI
1250 /* For the GUI the cursor properties are obtained with
1251 * term_get_cursor_shape(). */
1252 if (gui.in_use)
1253 return;
1254#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001255 if (in_terminal_loop == term)
1256 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001257 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001258 if (term->tl_cursor_color != NULL)
1259 term_cursor_color(term->tl_cursor_color);
1260 else
1261 term_cursor_color((char_u *)"");
1262 /* do both blink and shape+blink, in case setting shape does not work */
1263 term_cursor_blink(term->tl_cursor_blink);
1264 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1265 }
1266}
1267
1268 static void
1269may_restore_cursor_props(void)
1270{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001271#ifdef FEAT_GUI
1272 if (gui.in_use)
1273 return;
1274#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001275 if (did_change_cursor)
1276 {
1277 did_change_cursor = FALSE;
1278 ui_cursor_shape_forced(TRUE);
1279 term_cursor_color((char_u *)"");
1280 term_cursor_blink(FALSE);
1281 }
1282}
1283
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001284/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001285 * Returns TRUE if the current window contains a terminal and we are sending
1286 * keys to the job.
1287 */
1288 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001289term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001290{
1291 term_T *term = curbuf->b_term;
1292
1293 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001294 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001295 && term->tl_vterm != NULL
1296 && term_job_running(term);
1297}
1298
1299/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001300 * Wait for input and send it to the job.
1301 * Return when the start of a CTRL-W command is typed or anything else that
1302 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001303 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1304 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001305 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001306 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001307terminal_loop(void)
1308{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001309 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001310 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001311 int ret;
1312
Bram Moolenaar679653e2017-08-13 14:13:19 +02001313 /* Remember the terminal we are sending keys to. However, the terminal
1314 * might be closed while waiting for a character, e.g. typing "exit" in a
1315 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1316 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001317 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001318
1319 if (*curwin->w_p_tk != NUL)
1320 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001321 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001322 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001323
1324 for (;;)
1325 {
1326 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001327 /* Repeat redrawing in case a message is received while redrawing. */
1328 while (curwin->w_redr_type != 0)
1329 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001330 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001331
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001332 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001333 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001334 /* job finished while waiting for a character */
1335 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001336 if (c == K_IGNORE)
1337 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001338
Bram Moolenaarfae42832017-08-01 22:24:26 +02001339#ifdef UNIX
1340 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1341#endif
1342#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001343 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001344 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001345 if (ctrl_break_was_pressed)
1346 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001347#endif
1348
Bram Moolenaar69198192017-08-05 14:10:48 +02001349 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001350 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001351 int prev_c = c;
1352
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001353#ifdef FEAT_CMDL_INFO
1354 if (add_to_showcmd(c))
1355 out_flush();
1356#endif
1357 c = term_vgetc();
1358#ifdef FEAT_CMDL_INFO
1359 clear_showcmd();
1360#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001361 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001362 /* job finished while waiting for a character */
1363 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001364
Bram Moolenaar69198192017-08-05 14:10:48 +02001365 if (prev_c == Ctrl_BSL)
1366 {
1367 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001368 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001369 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1370 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001371 ret = FAIL;
1372 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001373 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001374 /* Send both keys to the terminal. */
1375 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1376 }
1377 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001378 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001379 /* "CTRL-W .": send CTRL-W to the job */
1380 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001381 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001382 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001383 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001384 /* CTRL-W N : go to Terminal-Normal mode. */
1385 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001386 ret = FAIL;
1387 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001388 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001389 else if (c == '"')
1390 {
1391 term_paste_register(prev_c);
1392 continue;
1393 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001394 else if (termkey == 0 || c != termkey)
1395 {
1396 stuffcharReadbuff(Ctrl_W);
1397 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001398 ret = OK;
1399 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001400 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001401 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001402 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001403 {
1404 ret = OK;
1405 goto theend;
1406 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001407 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001408 ret = FAIL;
1409
1410theend:
1411 in_terminal_loop = NULL;
1412 may_restore_cursor_props();
1413 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001414}
1415
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001416/*
1417 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001418 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001419 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001420 */
1421 void
1422term_job_ended(job_T *job)
1423{
1424 term_T *term;
1425 int did_one = FALSE;
1426
1427 for (term = first_term; term != NULL; term = term->tl_next)
1428 if (term->tl_job == job)
1429 {
1430 vim_free(term->tl_title);
1431 term->tl_title = NULL;
1432 vim_free(term->tl_status_text);
1433 term->tl_status_text = NULL;
1434 redraw_buf_and_status_later(term->tl_buffer, VALID);
1435 did_one = TRUE;
1436 }
1437 if (did_one)
1438 redraw_statuslines();
1439 if (curbuf->b_term != NULL)
1440 {
1441 if (curbuf->b_term->tl_job == job)
1442 maketitle();
1443 update_cursor(curbuf->b_term, TRUE);
1444 }
1445}
1446
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001447 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001448may_toggle_cursor(term_T *term)
1449{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001450 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001451 {
1452 if (term->tl_cursor_visible)
1453 cursor_on();
1454 else
1455 cursor_off();
1456 }
1457}
1458
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001459 static int
1460handle_damage(VTermRect rect, void *user)
1461{
1462 term_T *term = (term_T *)user;
1463
1464 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1465 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1466 redraw_buf_later(term->tl_buffer, NOT_VALID);
1467 return 1;
1468}
1469
1470 static int
1471handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1472{
1473 term_T *term = (term_T *)user;
1474
1475 /* TODO */
1476 redraw_buf_later(term->tl_buffer, NOT_VALID);
1477 return 1;
1478}
1479
1480 static int
1481handle_movecursor(
1482 VTermPos pos,
1483 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001484 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001485 void *user)
1486{
1487 term_T *term = (term_T *)user;
1488 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001489
1490 term->tl_cursor_pos = pos;
1491 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001492
1493 FOR_ALL_WINDOWS(wp)
1494 {
1495 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001496 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001497 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001498 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001499 {
1500 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001501 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001502 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001503
1504 return 1;
1505}
1506
Bram Moolenaar21554412017-07-24 21:44:43 +02001507 static int
1508handle_settermprop(
1509 VTermProp prop,
1510 VTermValue *value,
1511 void *user)
1512{
1513 term_T *term = (term_T *)user;
1514
1515 switch (prop)
1516 {
1517 case VTERM_PROP_TITLE:
1518 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001519 /* a blank title isn't useful, make it empty, so that "running" is
1520 * displayed */
1521 if (*skipwhite((char_u *)value->string) == NUL)
1522 term->tl_title = NULL;
1523 else
1524 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001525 vim_free(term->tl_status_text);
1526 term->tl_status_text = NULL;
1527 if (term == curbuf->b_term)
1528 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001529 break;
1530
1531 case VTERM_PROP_CURSORVISIBLE:
1532 term->tl_cursor_visible = value->boolean;
1533 may_toggle_cursor(term);
1534 out_flush();
1535 break;
1536
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001537 case VTERM_PROP_CURSORBLINK:
1538 term->tl_cursor_blink = value->boolean;
1539 may_set_cursor_props(term);
1540 break;
1541
1542 case VTERM_PROP_CURSORSHAPE:
1543 term->tl_cursor_shape = value->number;
1544 may_set_cursor_props(term);
1545 break;
1546
1547 case VTERM_PROP_CURSORCOLOR:
1548 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001549 if (*value->string == NUL)
1550 term->tl_cursor_color = NULL;
1551 else
1552 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001553 may_set_cursor_props(term);
1554 break;
1555
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001556 case VTERM_PROP_ALTSCREEN:
1557 /* TODO: do anything else? */
1558 term->tl_using_altscreen = value->boolean;
1559 break;
1560
Bram Moolenaar21554412017-07-24 21:44:43 +02001561 default:
1562 break;
1563 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001564 /* Always return 1, otherwise vterm doesn't store the value internally. */
1565 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001566}
1567
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001568/*
1569 * The job running in the terminal resized the terminal.
1570 */
1571 static int
1572handle_resize(int rows, int cols, void *user)
1573{
1574 term_T *term = (term_T *)user;
1575 win_T *wp;
1576
1577 term->tl_rows = rows;
1578 term->tl_cols = cols;
1579 FOR_ALL_WINDOWS(wp)
1580 {
1581 if (wp->w_buffer == term->tl_buffer)
1582 {
1583 win_setheight_win(rows, wp);
1584 win_setwidth_win(cols, wp);
1585 }
1586 }
1587
1588 redraw_buf_later(term->tl_buffer, NOT_VALID);
1589 return 1;
1590}
1591
Bram Moolenaard85f2712017-07-28 21:51:57 +02001592/*
1593 * Handle a line that is pushed off the top of the screen.
1594 */
1595 static int
1596handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1597{
1598 term_T *term = (term_T *)user;
1599
1600 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001601 if (ga_grow(&term->tl_scrollback, 1) == OK)
1602 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001603 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001604 int len = 0;
1605 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001606 int c;
1607 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001608 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001609 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001610
1611 /* do not store empty cells at the end */
1612 for (i = 0; i < cols; ++i)
1613 if (cells[i].chars[0] != 0)
1614 len = i + 1;
1615
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001616 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001617 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001618 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001619 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001620 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001621 for (col = 0; col < len; col += cells[col].width)
1622 {
1623 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1624 {
1625 ga.ga_len = 0;
1626 break;
1627 }
1628 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1629 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1630 (char_u *)ga.ga_data + ga.ga_len);
1631 p[col].width = cells[col].width;
1632 p[col].attrs = cells[col].attrs;
1633 p[col].fg = cells[col].fg;
1634 p[col].bg = cells[col].bg;
1635 }
1636 }
1637 if (ga_grow(&ga, 1) == FAIL)
1638 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1639 else
1640 {
1641 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1642 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1643 }
1644 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001645
1646 line = (sb_line_T *)term->tl_scrollback.ga_data
1647 + term->tl_scrollback.ga_len;
1648 line->sb_cols = len;
1649 line->sb_cells = p;
1650 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001651 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001652 }
1653 return 0; /* ignored */
1654}
1655
Bram Moolenaar21554412017-07-24 21:44:43 +02001656static VTermScreenCallbacks screen_callbacks = {
1657 handle_damage, /* damage */
1658 handle_moverect, /* moverect */
1659 handle_movecursor, /* movecursor */
1660 handle_settermprop, /* settermprop */
1661 NULL, /* bell */
1662 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001663 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001664 NULL /* sb_popline */
1665};
1666
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001667/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001668 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001669 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001670 */
1671 void
1672term_channel_closed(channel_T *ch)
1673{
1674 term_T *term;
1675 int did_one = FALSE;
1676
1677 for (term = first_term; term != NULL; term = term->tl_next)
1678 if (term->tl_job == ch->ch_job)
1679 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001680 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001681 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001682
Bram Moolenaard85f2712017-07-28 21:51:57 +02001683 vim_free(term->tl_title);
1684 term->tl_title = NULL;
1685 vim_free(term->tl_status_text);
1686 term->tl_status_text = NULL;
1687
Bram Moolenaar423802d2017-07-30 16:52:24 +02001688 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001689 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001690 {
1691 int fnum = term->tl_buffer->b_fnum;
1692
Bram Moolenaar423802d2017-07-30 16:52:24 +02001693 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001694
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001695 if (term->tl_finish == 'c')
1696 {
1697 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001698 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001699 curbuf = term->tl_buffer;
1700 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1701 break;
1702 }
1703 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1704 {
1705 char buf[50];
1706
1707 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001708 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001709 vim_snprintf(buf, sizeof(buf),
1710 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001711 ? "botright sbuf %d"
1712 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001713 do_cmdline_cmd((char_u *)buf);
1714 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001715 else
1716 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001717 }
1718
Bram Moolenaard85f2712017-07-28 21:51:57 +02001719 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001720 }
1721 if (did_one)
1722 {
1723 redraw_statuslines();
1724
1725 /* Need to break out of vgetc(). */
1726 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001727 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001728
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001729 term = curbuf->b_term;
1730 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001731 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001732 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001733 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001734 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001735 }
1736 }
1737}
1738
1739/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001740 * Reverse engineer the RGB value into a cterm color index.
1741 * First color is 1. Return 0 if no match found.
1742 */
1743 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001744color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001745{
1746 int red = color->red;
1747 int blue = color->blue;
1748 int green = color->green;
1749
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001750 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001751 if (red == 0)
1752 {
1753 if (green == 0)
1754 {
1755 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001756 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001757 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001758 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001759 }
1760 else if (green == 224)
1761 {
1762 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001763 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001764 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001765 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001766 }
1767 }
1768 else if (red == 224)
1769 {
1770 if (green == 0)
1771 {
1772 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001773 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001774 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001775 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001776 }
1777 else if (green == 224)
1778 {
1779 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001780 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001781 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001782 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001783 }
1784 }
1785 else if (red == 128)
1786 {
1787 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001788 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001789 }
1790 else if (red == 255)
1791 {
1792 if (green == 64)
1793 {
1794 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001795 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001796 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001797 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001798 }
1799 else if (green == 255)
1800 {
1801 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001802 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001803 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001804 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001805 }
1806 }
1807 else if (red == 64)
1808 {
1809 if (green == 64)
1810 {
1811 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001812 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001813 }
1814 else if (green == 255)
1815 {
1816 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001817 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001818 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001819 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001820 }
1821 }
1822 if (t_colors >= 256)
1823 {
1824 if (red == blue && red == green)
1825 {
1826 /* 24-color greyscale */
1827 static int cutoff[23] = {
1828 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1829 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1830 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1831 int i;
1832
1833 for (i = 0; i < 23; ++i)
1834 if (red < cutoff[i])
1835 return i + 233;
1836 return 256;
1837 }
1838
1839 /* 216-color cube */
1840 return 17 + ((red + 25) / 0x33) * 36
1841 + ((green + 25) / 0x33) * 6
1842 + (blue + 25) / 0x33;
1843 }
1844 return 0;
1845}
1846
1847/*
1848 * Convert the attributes of a vterm cell into an attribute index.
1849 */
1850 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001851cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001852{
1853 int attr = 0;
1854
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001855 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001856 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001857 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001858 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001859 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001860 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001861 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001862 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001863 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001864 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001865
1866#ifdef FEAT_GUI
1867 if (gui.in_use)
1868 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001869 guicolor_T fg, bg;
1870
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001871 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1872 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001873 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001874 }
1875 else
1876#endif
1877#ifdef FEAT_TERMGUICOLORS
1878 if (p_tgc)
1879 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001880 guicolor_T fg, bg;
1881
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001882 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1883 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001884
1885 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001886 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001887 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001888#endif
1889 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001890 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001891 int fg = color2index(&cellfg, TRUE, &bold);
1892 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001893
1894 /* with 8 colors set the bold attribute to get a bright foreground */
1895 if (bold == TRUE)
1896 attr |= HL_BOLD;
1897 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001898 }
1899 return 0;
1900}
1901
1902/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001903 * Called to update a window that contains an active terminal.
1904 * Returns FAIL when there is no terminal running in this window or in
1905 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001906 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001907 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001908term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001909{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001910 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001911 VTerm *vterm;
1912 VTermScreen *screen;
1913 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001914 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001915
Bram Moolenaar6d819742017-08-06 14:57:49 +02001916 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001917 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001918
Bram Moolenaard85f2712017-07-28 21:51:57 +02001919 vterm = term->tl_vterm;
1920 screen = vterm_obtain_screen(vterm);
1921 state = vterm_obtain_state(vterm);
1922
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001923 /*
1924 * If the window was resized a redraw will be triggered and we get here.
1925 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1926 */
1927 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1928 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001929 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001930 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1931 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1932 win_T *twp;
1933
1934 FOR_ALL_WINDOWS(twp)
1935 {
1936 /* When more than one window shows the same terminal, use the
1937 * smallest size. */
1938 if (twp->w_buffer == term->tl_buffer)
1939 {
1940 if (!term->tl_rows_fixed && rows > twp->w_height)
1941 rows = twp->w_height;
1942 if (!term->tl_cols_fixed && cols > twp->w_width)
1943 cols = twp->w_width;
1944 }
1945 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001946
1947 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001948 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001949 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001950 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001951 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001952
1953 /* The cursor may have been moved when resizing. */
1954 vterm_state_get_cursorpos(state, &pos);
1955 position_cursor(wp, &pos);
1956
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001957 /* TODO: Only redraw what changed. */
1958 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001959 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001960 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001961 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001962
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001963 if (pos.row < term->tl_rows)
1964 {
1965 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001966 {
1967 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001968 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001969
Bram Moolenaareeac6772017-07-23 15:48:37 +02001970 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1971 vim_memset(&cell, 0, sizeof(cell));
1972
1973 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001974 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001975 if (c == NUL)
1976 {
1977 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001978#if defined(FEAT_MBYTE)
1979 if (enc_utf8)
1980 ScreenLinesUC[off] = NUL;
1981#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001982 }
1983 else
1984 {
1985#if defined(FEAT_MBYTE)
1986 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001987 {
1988 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001989 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001990 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001991 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001992 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001993 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001994 if (enc_utf8)
1995 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001996 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001997#else
1998 ScreenLines[off] = c;
1999#endif
2000 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002001 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002002
2003 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002004 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002005 if (cell.width == 2)
2006 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02002007 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02002008#if defined(FEAT_MBYTE)
2009 if (enc_utf8)
2010 ScreenLinesUC[off] = NUL;
2011#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002012 ++pos.col;
2013 ++off;
2014 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002015 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002016 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002017 else
2018 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002019
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002020 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2021 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002022 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002023
2024 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002025}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002026
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002027/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002028 * Return TRUE if "wp" is a terminal window where the job has finished.
2029 */
2030 int
2031term_is_finished(buf_T *buf)
2032{
2033 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2034}
2035
2036/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002037 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002038 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002039 */
2040 int
2041term_show_buffer(buf_T *buf)
2042{
2043 term_T *term = buf->b_term;
2044
Bram Moolenaar6d819742017-08-06 14:57:49 +02002045 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002046}
2047
2048/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002049 * The current buffer is going to be changed. If there is terminal
2050 * highlighting remove it now.
2051 */
2052 void
2053term_change_in_curbuf(void)
2054{
2055 term_T *term = curbuf->b_term;
2056
2057 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2058 {
2059 free_scrollback(term);
2060 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002061
2062 /* The buffer is now like a normal buffer, it cannot be easily
2063 * abandoned when changed. */
2064 set_string_option_direct((char_u *)"buftype", -1,
2065 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002066 }
2067}
2068
2069/*
2070 * Get the screen attribute for a position in the buffer.
2071 */
2072 int
2073term_get_attr(buf_T *buf, linenr_T lnum, int col)
2074{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002075 term_T *term = buf->b_term;
2076 sb_line_T *line;
2077 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002078
Bram Moolenaar70229f92017-07-29 16:01:53 +02002079 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002080 return 0;
2081 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2082 if (col >= line->sb_cols)
2083 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002084 cellattr = line->sb_cells + col;
2085 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002086}
2087
2088/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002089 * Create a new vterm and initialize it.
2090 */
2091 static void
2092create_vterm(term_T *term, int rows, int cols)
2093{
2094 VTerm *vterm;
2095 VTermScreen *screen;
2096
2097 vterm = vterm_new(rows, cols);
2098 term->tl_vterm = vterm;
2099 screen = vterm_obtain_screen(vterm);
2100 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2101 /* TODO: depends on 'encoding'. */
2102 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002103
2104 /* Vterm uses a default black background. Set it to white when
2105 * 'background' is "light". */
2106 if (*p_bg == 'l')
2107 {
2108 VTermColor fg, bg;
2109
2110 fg.red = fg.green = fg.blue = 0;
2111 bg.red = bg.green = bg.blue = 255;
2112 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2113 }
2114
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002115 /* Required to initialize most things. */
2116 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002117
2118 /* Allow using alternate screen. */
2119 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002120}
2121
Bram Moolenaar21554412017-07-24 21:44:43 +02002122/*
2123 * Return the text to show for the buffer name and status.
2124 */
2125 char_u *
2126term_get_status_text(term_T *term)
2127{
2128 if (term->tl_status_text == NULL)
2129 {
2130 char_u *txt;
2131 size_t len;
2132
Bram Moolenaar6d819742017-08-06 14:57:49 +02002133 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002134 {
2135 if (term_job_running(term))
2136 txt = (char_u *)_("Terminal");
2137 else
2138 txt = (char_u *)_("Terminal-finished");
2139 }
2140 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002141 txt = term->tl_title;
2142 else if (term_job_running(term))
2143 txt = (char_u *)_("running");
2144 else
2145 txt = (char_u *)_("finished");
2146 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002147 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002148 if (term->tl_status_text != NULL)
2149 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2150 term->tl_buffer->b_fname, txt);
2151 }
2152 return term->tl_status_text;
2153}
2154
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002155/*
2156 * Mark references in jobs of terminals.
2157 */
2158 int
2159set_ref_in_term(int copyID)
2160{
2161 int abort = FALSE;
2162 term_T *term;
2163 typval_T tv;
2164
2165 for (term = first_term; term != NULL; term = term->tl_next)
2166 if (term->tl_job != NULL)
2167 {
2168 tv.v_type = VAR_JOB;
2169 tv.vval.v_job = term->tl_job;
2170 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2171 }
2172 return abort;
2173}
2174
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002175/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002176 * Get the buffer from the first argument in "argvars".
2177 * Returns NULL when the buffer is not for a terminal window.
2178 */
2179 static buf_T *
2180term_get_buf(typval_T *argvars)
2181{
2182 buf_T *buf;
2183
2184 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2185 ++emsg_off;
2186 buf = get_buf_tv(&argvars[0], FALSE);
2187 --emsg_off;
2188 if (buf == NULL || buf->b_term == NULL)
2189 return NULL;
2190 return buf;
2191}
2192
2193/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002194 * "term_getaltscreen(buf)" function
2195 */
2196 void
2197f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2198{
2199 buf_T *buf = term_get_buf(argvars);
2200
2201 if (buf == NULL)
2202 return;
2203 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2204}
2205
2206/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002207 * "term_getattr(attr, name)" function
2208 */
2209 void
2210f_term_getattr(typval_T *argvars, typval_T *rettv)
2211{
2212 int attr;
2213 size_t i;
2214 char_u *name;
2215
2216 static struct {
2217 char *name;
2218 int attr;
2219 } attrs[] = {
2220 {"bold", HL_BOLD},
2221 {"italic", HL_ITALIC},
2222 {"underline", HL_UNDERLINE},
2223 {"strike", HL_STANDOUT},
2224 {"reverse", HL_INVERSE},
2225 };
2226
2227 attr = get_tv_number(&argvars[0]);
2228 name = get_tv_string_chk(&argvars[1]);
2229 if (name == NULL)
2230 return;
2231
2232 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2233 if (STRCMP(name, attrs[i].name) == 0)
2234 {
2235 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2236 break;
2237 }
2238}
2239
2240/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002241 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002242 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002243 void
2244f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002245{
Bram Moolenaar97870002017-07-30 18:28:38 +02002246 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002247 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002248 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002249 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002250
Bram Moolenaar97870002017-07-30 18:28:38 +02002251 if (rettv_list_alloc(rettv) == FAIL)
2252 return;
2253 if (buf == NULL)
2254 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002255 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002256
2257 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002258 list_append_number(l, term->tl_cursor_pos.row + 1);
2259 list_append_number(l, term->tl_cursor_pos.col + 1);
2260
2261 d = dict_alloc();
2262 if (d != NULL)
2263 {
2264 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2265 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2266 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2267 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2268 ? (char_u *)"" : term->tl_cursor_color);
2269 list_append_dict(l, d);
2270 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002271}
2272
2273/*
2274 * "term_getjob(buf)" function
2275 */
2276 void
2277f_term_getjob(typval_T *argvars, typval_T *rettv)
2278{
2279 buf_T *buf = term_get_buf(argvars);
2280
2281 rettv->v_type = VAR_JOB;
2282 rettv->vval.v_job = NULL;
2283 if (buf == NULL)
2284 return;
2285
2286 rettv->vval.v_job = buf->b_term->tl_job;
2287 if (rettv->vval.v_job != NULL)
2288 ++rettv->vval.v_job->jv_refcount;
2289}
2290
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002291 static int
2292get_row_number(typval_T *tv, term_T *term)
2293{
2294 if (tv->v_type == VAR_STRING
2295 && tv->vval.v_string != NULL
2296 && STRCMP(tv->vval.v_string, ".") == 0)
2297 return term->tl_cursor_pos.row;
2298 return (int)get_tv_number(tv) - 1;
2299}
2300
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002301/*
2302 * "term_getline(buf, row)" function
2303 */
2304 void
2305f_term_getline(typval_T *argvars, typval_T *rettv)
2306{
2307 buf_T *buf = term_get_buf(argvars);
2308 term_T *term;
2309 int row;
2310
2311 rettv->v_type = VAR_STRING;
2312 if (buf == NULL)
2313 return;
2314 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002315 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002316
2317 if (term->tl_vterm == NULL)
2318 {
2319 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2320
2321 /* vterm is finished, get the text from the buffer */
2322 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2323 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2324 }
2325 else
2326 {
2327 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2328 VTermRect rect;
2329 int len;
2330 char_u *p;
2331
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002332 if (row < 0 || row >= term->tl_rows)
2333 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002334 len = term->tl_cols * MB_MAXBYTES + 1;
2335 p = alloc(len);
2336 if (p == NULL)
2337 return;
2338 rettv->vval.v_string = p;
2339
2340 rect.start_col = 0;
2341 rect.end_col = term->tl_cols;
2342 rect.start_row = row;
2343 rect.end_row = row + 1;
2344 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2345 }
2346}
2347
2348/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002349 * "term_getscrolled(buf)" function
2350 */
2351 void
2352f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2353{
2354 buf_T *buf = term_get_buf(argvars);
2355
2356 if (buf == NULL)
2357 return;
2358 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2359}
2360
2361/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002362 * "term_getsize(buf)" function
2363 */
2364 void
2365f_term_getsize(typval_T *argvars, typval_T *rettv)
2366{
2367 buf_T *buf = term_get_buf(argvars);
2368 list_T *l;
2369
2370 if (rettv_list_alloc(rettv) == FAIL)
2371 return;
2372 if (buf == NULL)
2373 return;
2374
2375 l = rettv->vval.v_list;
2376 list_append_number(l, buf->b_term->tl_rows);
2377 list_append_number(l, buf->b_term->tl_cols);
2378}
2379
2380/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002381 * "term_getstatus(buf)" function
2382 */
2383 void
2384f_term_getstatus(typval_T *argvars, typval_T *rettv)
2385{
2386 buf_T *buf = term_get_buf(argvars);
2387 term_T *term;
2388 char_u val[100];
2389
2390 rettv->v_type = VAR_STRING;
2391 if (buf == NULL)
2392 return;
2393 term = buf->b_term;
2394
2395 if (term_job_running(term))
2396 STRCPY(val, "running");
2397 else
2398 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002399 if (term->tl_normal_mode)
2400 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002401 rettv->vval.v_string = vim_strsave(val);
2402}
2403
2404/*
2405 * "term_gettitle(buf)" function
2406 */
2407 void
2408f_term_gettitle(typval_T *argvars, typval_T *rettv)
2409{
2410 buf_T *buf = term_get_buf(argvars);
2411
2412 rettv->v_type = VAR_STRING;
2413 if (buf == NULL)
2414 return;
2415
2416 if (buf->b_term->tl_title != NULL)
2417 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2418}
2419
2420/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002421 * "term_gettty(buf)" function
2422 */
2423 void
2424f_term_gettty(typval_T *argvars, typval_T *rettv)
2425{
2426 buf_T *buf = term_get_buf(argvars);
2427 char_u *p;
2428
2429 rettv->v_type = VAR_STRING;
2430 if (buf == NULL)
2431 return;
2432 if (buf->b_term->tl_job != NULL)
2433 p = buf->b_term->tl_job->jv_tty_name;
2434 else
2435 p = buf->b_term->tl_tty_name;
2436 if (p != NULL)
2437 rettv->vval.v_string = vim_strsave(p);
2438}
2439
2440/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002441 * "term_list()" function
2442 */
2443 void
2444f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2445{
2446 term_T *tp;
2447 list_T *l;
2448
2449 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2450 return;
2451
2452 l = rettv->vval.v_list;
2453 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2454 if (tp != NULL && tp->tl_buffer != NULL)
2455 if (list_append_number(l,
2456 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2457 return;
2458}
2459
2460/*
2461 * "term_scrape(buf, row)" function
2462 */
2463 void
2464f_term_scrape(typval_T *argvars, typval_T *rettv)
2465{
2466 buf_T *buf = term_get_buf(argvars);
2467 VTermScreen *screen = NULL;
2468 VTermPos pos;
2469 list_T *l;
2470 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002471 char_u *p;
2472 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002473
2474 if (rettv_list_alloc(rettv) == FAIL)
2475 return;
2476 if (buf == NULL)
2477 return;
2478 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002479
2480 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002481 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002482
2483 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002484 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002485 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002486 p = NULL;
2487 line = NULL;
2488 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002489 else
2490 {
2491 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2492
2493 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2494 return;
2495 p = ml_get_buf(buf, lnum + 1, FALSE);
2496 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2497 }
2498
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002499 for (pos.col = 0; pos.col < term->tl_cols; )
2500 {
2501 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002502 int width;
2503 VTermScreenCellAttrs attrs;
2504 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002505 char_u rgb[8];
2506 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2507 int off = 0;
2508 int i;
2509
2510 if (screen == NULL)
2511 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002512 cellattr_T *cellattr;
2513 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002514
2515 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002516 if (pos.col >= line->sb_cols)
2517 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002518 cellattr = line->sb_cells + pos.col;
2519 width = cellattr->width;
2520 attrs = cellattr->attrs;
2521 fg = cellattr->fg;
2522 bg = cellattr->bg;
2523 len = MB_PTR2LEN(p);
2524 mch_memmove(mbs, p, len);
2525 mbs[len] = NUL;
2526 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002527 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002528 else
2529 {
2530 VTermScreenCell cell;
2531 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2532 break;
2533 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2534 {
2535 if (cell.chars[i] == 0)
2536 break;
2537 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2538 }
2539 mbs[off] = NUL;
2540 width = cell.width;
2541 attrs = cell.attrs;
2542 fg = cell.fg;
2543 bg = cell.bg;
2544 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002545 dcell = dict_alloc();
2546 list_append_dict(l, dcell);
2547
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002548 dict_add_nr_str(dcell, "chars", 0, mbs);
2549
2550 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002551 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002552 dict_add_nr_str(dcell, "fg", 0, rgb);
2553 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002554 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002555 dict_add_nr_str(dcell, "bg", 0, rgb);
2556
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002557 dict_add_nr_str(dcell, "attr",
2558 cell2attr(attrs, fg, bg), NULL);
2559 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002560
2561 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002562 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002563 ++pos.col;
2564 }
2565}
2566
2567/*
2568 * "term_sendkeys(buf, keys)" function
2569 */
2570 void
2571f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2572{
2573 buf_T *buf = term_get_buf(argvars);
2574 char_u *msg;
2575 term_T *term;
2576
2577 rettv->v_type = VAR_UNKNOWN;
2578 if (buf == NULL)
2579 return;
2580
2581 msg = get_tv_string_chk(&argvars[1]);
2582 if (msg == NULL)
2583 return;
2584 term = buf->b_term;
2585 if (term->tl_vterm == NULL)
2586 return;
2587
2588 while (*msg != NUL)
2589 {
2590 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2591 msg += MB_PTR2LEN(msg);
2592 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002593}
2594
2595/*
2596 * "term_start(command, options)" function
2597 */
2598 void
2599f_term_start(typval_T *argvars, typval_T *rettv)
2600{
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002601 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002602
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002603 init_job_options(&opt);
2604 /* TODO: allow more job options */
2605 if (argvars[1].v_type != VAR_UNKNOWN
2606 && get_job_options(&argvars[1], &opt,
2607 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002608 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002609 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002610 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002611 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002612 return;
2613
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002614 if (opt.jo_vertical)
2615 cmdmod.split = WSP_VERT;
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002616 term_start(&argvars[0], &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002617
2618 if (curbuf->b_term != NULL)
2619 rettv->vval.v_number = curbuf->b_fnum;
2620}
2621
2622/*
2623 * "term_wait" function
2624 */
2625 void
2626f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2627{
2628 buf_T *buf = term_get_buf(argvars);
2629
2630 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002631 {
2632 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002633 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002634 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002635 if (buf->b_term->tl_job == NULL)
2636 {
2637 ch_log(NULL, "term_wait(): no job to wait for");
2638 return;
2639 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002640
2641 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002642 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002643 {
2644 /* The job is dead, keep reading channel I/O until the channel is
2645 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002646 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002647 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2648 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002649 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002650 parse_queued_messages();
2651 ui_delay(10L, FALSE);
2652 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002653 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002654 parse_queued_messages();
2655 }
2656 else
2657 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002658 long wait = 10L;
2659
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002660 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002661 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002662
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002663 /* Wait for some time for any channel I/O. */
2664 if (argvars[1].v_type != VAR_UNKNOWN)
2665 wait = get_tv_number(&argvars[1]);
2666 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002667 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002668
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002669 /* Flushing messages on channels is hopefully sufficient.
2670 * TODO: is there a better way? */
2671 parse_queued_messages();
2672 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002673}
2674
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002675# ifdef WIN3264
2676
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002677/**************************************
2678 * 2. MS-Windows implementation.
2679 */
2680
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002681#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2682#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2683
Bram Moolenaar8a773062017-07-24 22:29:21 +02002684void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002685void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002686void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002687BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2688void (*winpty_config_set_initial_size)(void*, int, int);
2689LPCWSTR (*winpty_conin_name)(void*);
2690LPCWSTR (*winpty_conout_name)(void*);
2691LPCWSTR (*winpty_conerr_name)(void*);
2692void (*winpty_free)(void*);
2693void (*winpty_config_free)(void*);
2694void (*winpty_spawn_config_free)(void*);
2695void (*winpty_error_free)(void*);
2696LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002697BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002698HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002699
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002700#define WINPTY_DLL "winpty.dll"
2701
2702static HINSTANCE hWinPtyDLL = NULL;
2703
2704 int
2705dyn_winpty_init(void)
2706{
2707 int i;
2708 static struct
2709 {
2710 char *name;
2711 FARPROC *ptr;
2712 } winpty_entry[] =
2713 {
2714 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2715 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2716 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2717 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2718 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2719 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2720 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2721 {"winpty_free", (FARPROC*)&winpty_free},
2722 {"winpty_open", (FARPROC*)&winpty_open},
2723 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2724 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2725 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2726 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002727 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002728 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002729 {NULL, NULL}
2730 };
2731
2732 /* No need to initialize twice. */
2733 if (hWinPtyDLL)
2734 return 1;
2735 /* Load winpty.dll */
2736 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2737 if (!hWinPtyDLL)
2738 {
2739 EMSG2(_(e_loadlib), WINPTY_DLL);
2740 return 0;
2741 }
2742 for (i = 0; winpty_entry[i].name != NULL
2743 && winpty_entry[i].ptr != NULL; ++i)
2744 {
2745 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2746 winpty_entry[i].name)) == NULL)
2747 {
2748 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2749 return 0;
2750 }
2751 }
2752
2753 return 1;
2754}
2755
2756/*
2757 * Create a new terminal of "rows" by "cols" cells.
2758 * Store a reference in "term".
2759 * Return OK or FAIL.
2760 */
2761 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002762term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002763{
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002764 WCHAR *p = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002765 channel_T *channel = NULL;
2766 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002767 DWORD error;
2768 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2769 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002770 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002771 char buf[MAX_PATH];
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002772 garray_T ga;
2773 char_u *cmd;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002774
2775 if (!dyn_winpty_init())
2776 return FAIL;
2777
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002778 if (argvar->v_type == VAR_STRING)
2779 cmd = argvar->vval.v_string;
2780 else
2781 {
2782 ga_init2(&ga, (int)sizeof(char*), 20);
2783 if (win32_build_cmd(argvar->vval.v_list, &ga) == FAIL)
2784 goto failed;
2785 cmd = ga.ga_data;
2786 }
2787
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002788 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002789 if (p == NULL)
2790 return FAIL;
2791
2792 job = job_alloc();
2793 if (job == NULL)
2794 goto failed;
2795
2796 channel = add_channel();
2797 if (channel == NULL)
2798 goto failed;
2799
2800 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2801 if (term->tl_winpty_config == NULL)
2802 goto failed;
2803
2804 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2805 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2806 if (term->tl_winpty == NULL)
2807 goto failed;
2808
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002809 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002810 spawn_config = winpty_spawn_config_new(
2811 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2812 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2813 NULL,
2814 p,
2815 NULL,
2816 NULL,
2817 &winpty_err);
2818 if (spawn_config == NULL)
2819 goto failed;
2820
2821 channel = add_channel();
2822 if (channel == NULL)
2823 goto failed;
2824
2825 job = job_alloc();
2826 if (job == NULL)
2827 goto failed;
2828
2829 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2830 &child_thread_handle, &error, &winpty_err))
2831 goto failed;
2832
2833 channel_set_pipes(channel,
2834 (sock_T) CreateFileW(
2835 winpty_conin_name(term->tl_winpty),
2836 GENERIC_WRITE, 0, NULL,
2837 OPEN_EXISTING, 0, NULL),
2838 (sock_T) CreateFileW(
2839 winpty_conout_name(term->tl_winpty),
2840 GENERIC_READ, 0, NULL,
2841 OPEN_EXISTING, 0, NULL),
2842 (sock_T) CreateFileW(
2843 winpty_conerr_name(term->tl_winpty),
2844 GENERIC_READ, 0, NULL,
2845 OPEN_EXISTING, 0, NULL));
2846
2847 jo = CreateJobObject(NULL, NULL);
2848 if (jo == NULL)
2849 goto failed;
2850
2851 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002852 {
2853 /* Failed, switch the way to terminate process with TerminateProcess. */
2854 CloseHandle(jo);
2855 jo = NULL;
2856 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002857
2858 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002859 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002860
2861 create_vterm(term, rows, cols);
2862
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002863 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002864 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002865
2866 job->jv_channel = channel;
2867 job->jv_proc_info.hProcess = child_process_handle;
2868 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2869 job->jv_job_object = jo;
2870 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002871 sprintf(buf, "winpty://%lu",
2872 GetProcessId(winpty_agent_process(term->tl_winpty)));
2873 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002874 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002875 term->tl_job = job;
2876
2877 return OK;
2878
2879failed:
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002880 if (argvar->v_type == VAR_LIST)
2881 vim_free(ga.ga_data);
2882 if (p != NULL)
2883 vim_free(p);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002884 if (spawn_config != NULL)
2885 winpty_spawn_config_free(spawn_config);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002886 if (channel != NULL)
2887 channel_clear(channel);
2888 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002889 {
2890 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002891 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002892 }
2893 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002894 if (jo != NULL)
2895 CloseHandle(jo);
2896 if (term->tl_winpty != NULL)
2897 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002898 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002899 if (term->tl_winpty_config != NULL)
2900 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002901 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002902 if (winpty_err != NULL)
2903 {
2904 char_u *msg = utf16_to_enc(
2905 (short_u *)winpty_error_msg(winpty_err), NULL);
2906
2907 EMSG(msg);
2908 winpty_error_free(winpty_err);
2909 }
2910 return FAIL;
2911}
2912
2913/*
2914 * Free the terminal emulator part of "term".
2915 */
2916 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002917term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002918{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002919 if (term->tl_winpty != NULL)
2920 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002921 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002922 if (term->tl_winpty_config != NULL)
2923 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002924 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002925 if (term->tl_vterm != NULL)
2926 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002927 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928}
2929
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002930/*
2931 * Request size to terminal.
2932 */
2933 static void
2934term_report_winsize(term_T *term, int rows, int cols)
2935{
2936 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2937}
2938
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002939# else
2940
2941/**************************************
2942 * 3. Unix-like implementation.
2943 */
2944
2945/*
2946 * Create a new terminal of "rows" by "cols" cells.
2947 * Start job for "cmd".
2948 * Store the pointers in "term".
2949 * Return OK or FAIL.
2950 */
2951 static int
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002952term_and_job_init(term_T *term, int rows, int cols, typval_T *argvar, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002953{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002954 create_vterm(term, rows, cols);
2955
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002956 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaardcaa6132017-08-13 17:13:09 +02002957 term->tl_job = job_start(argvar, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002958 if (term->tl_job != NULL)
2959 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002960
Bram Moolenaar61a66052017-07-22 18:39:00 +02002961 return term->tl_job != NULL
2962 && term->tl_job->jv_channel != NULL
2963 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002964}
2965
2966/*
2967 * Free the terminal emulator part of "term".
2968 */
2969 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002970term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002971{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002972 if (term->tl_vterm != NULL)
2973 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002974 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002975}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002976
2977/*
2978 * Request size to terminal.
2979 */
2980 static void
2981term_report_winsize(term_T *term, int rows, int cols)
2982{
2983 /* Use an ioctl() to report the new window size to the job. */
2984 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2985 {
2986 int fd = -1;
2987 int part;
2988
2989 for (part = PART_OUT; part < PART_COUNT; ++part)
2990 {
2991 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2992 if (isatty(fd))
2993 break;
2994 }
2995 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002996 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002997 }
2998}
2999
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02003000# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02003001
Bram Moolenaare4f25e42017-07-07 11:54:15 +02003002#endif /* FEAT_TERMINAL */