blob: 0024d5f8d4493e01c0fd8954c4ecc83d25f5b9c7 [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 Moolenaardd693ce2017-08-10 23:15:19 +020041 * - Make argument list work on MS-Windows. #1954
Bram Moolenaar292d5692017-08-08 21:52:22 +020042 * - MS-Windows: no redraw for 'updatetime' #1915
Bram Moolenaard85f2712017-07-28 21:51:57 +020043 * - To set BS correctly, check get_stty(); Pass the fd of the pty.
Bram Moolenaar69198192017-08-05 14:10:48 +020044 * For the GUI fill termios with default values, perhaps like pangoterm:
45 * http://bazaar.launchpad.net/~leonerd/pangoterm/trunk/view/head:/main.c#L134
46 * Also get the NL behavior from there.
Bram Moolenaar423802d2017-07-30 16:52:24 +020047 * - do not store terminal window in viminfo. Or prefix term:// ?
Bram Moolenaar21554412017-07-24 21:44:43 +020048 * - add a character in :ls output
Bram Moolenaar43c007f2017-07-30 17:45:37 +020049 * - add 't' to mode()
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020050 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020051 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
52 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020053 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020054 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020055 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020056 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020057 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020058 * terminal. Allow:
59 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
60 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
61 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
62 * Check that something is connected to the terminal.
63 * Test: "cat" reading from a file or buffer
64 * "ls" writing stdout to a file or buffer
65 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020066 * - support ":term NONE" to open a terminal with a pty but not running a job
67 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020068 * - if the job in the terminal does not support the mouse, we can use the
69 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020070 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
71 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020072 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020073 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020074 * - Copy text in the vterm to the Vim buffer once in a while, so that
75 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020076 */
77
78#include "vim.h"
79
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020080#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020081
Bram Moolenaard5310982017-08-05 15:16:32 +020082#ifndef MIN
83# define MIN(x,y) ((x) < (y) ? (x) : (y))
84#endif
85#ifndef MAX
86# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020087#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020088
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020089#include "libvterm/include/vterm.h"
90
Bram Moolenaar33a43be2017-08-06 21:36:22 +020091/* This is VTermScreenCell without the characters, thus much smaller. */
92typedef struct {
93 VTermScreenCellAttrs attrs;
94 char width;
95 VTermColor fg, bg;
96} cellattr_T;
97
Bram Moolenaard85f2712017-07-28 21:51:57 +020098typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +020099 int sb_cols; /* can differ per line */
100 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200101} sb_line_T;
102
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200103/* typedef term_T in structs.h */
104struct terminal_S {
105 term_T *tl_next;
106
Bram Moolenaar423802d2017-07-30 16:52:24 +0200107 VTerm *tl_vterm;
108 job_T *tl_job;
109 buf_T *tl_buffer;
110
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200111 /* used when tl_job is NULL and only a pty was created */
112 int tl_tty_fd;
113 char_u *tl_tty_name;
114
Bram Moolenaar6d819742017-08-06 14:57:49 +0200115 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200116 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200117 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200118 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200119
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200120#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200121 void *tl_winpty_config;
122 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200123#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200124
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200125 /* last known vterm size */
126 int tl_rows;
127 int tl_cols;
128 /* vterm size does not follow window size */
129 int tl_rows_fixed;
130 int tl_cols_fixed;
131
Bram Moolenaar21554412017-07-24 21:44:43 +0200132 char_u *tl_title; /* NULL or allocated */
133 char_u *tl_status_text; /* NULL or allocated */
134
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200135 /* Range of screen rows to update. Zero based. */
136 int tl_dirty_row_start; /* -1 if nothing dirty */
137 int tl_dirty_row_end; /* row below last one to update */
138
Bram Moolenaard85f2712017-07-28 21:51:57 +0200139 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200140 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200141
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200142 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200143 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200144 int tl_cursor_blink;
145 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
146 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200147
148 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200149};
150
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200151#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
152#define TMODE_LOOP 2 /* CTRL-W N used */
153
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200154/*
155 * List of all active terminals.
156 */
157static term_T *first_term = NULL;
158
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200159/* Terminal active in terminal_loop(). */
160static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200161
162#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
163#define KEY_BUF_LEN 200
164
165/*
166 * Functions with separate implementation for MS-Windows and Unix-like systems.
167 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200168static int term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt);
Bram Moolenaar43da3e32017-07-23 17:27:54 +0200169static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200170static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200171
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200172/**************************************
173 * 1. Generic code for all systems.
174 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200175
176/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200177 * Determine the terminal size from 'termsize' and the current window.
178 * Assumes term->tl_rows and term->tl_cols are zero.
179 */
180 static void
181set_term_and_win_size(term_T *term)
182{
183 if (*curwin->w_p_tms != NUL)
184 {
185 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
186
187 term->tl_rows = atoi((char *)curwin->w_p_tms);
188 term->tl_cols = atoi((char *)p);
189 }
190 if (term->tl_rows == 0)
191 term->tl_rows = curwin->w_height;
192 else
193 {
194 win_setheight_win(term->tl_rows, curwin);
195 term->tl_rows_fixed = TRUE;
196 }
197 if (term->tl_cols == 0)
198 term->tl_cols = curwin->w_width;
199 else
200 {
201 win_setwidth_win(term->tl_cols, curwin);
202 term->tl_cols_fixed = TRUE;
203 }
204}
205
206/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200207 * Initialize job options for a terminal job.
208 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200209 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200210 static void
211init_job_options(jobopt_T *opt)
212{
213 clear_job_options(opt);
214
215 opt->jo_mode = MODE_RAW;
216 opt->jo_out_mode = MODE_RAW;
217 opt->jo_err_mode = MODE_RAW;
218 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
219
220 opt->jo_io[PART_OUT] = JIO_BUFFER;
221 opt->jo_io[PART_ERR] = JIO_BUFFER;
222 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
223
224 opt->jo_modifiable[PART_OUT] = 0;
225 opt->jo_modifiable[PART_ERR] = 0;
226 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
227
228 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
229}
230
231/*
232 * Set job options mandatory for a terminal job.
233 */
234 static void
235setup_job_options(jobopt_T *opt, int rows, int cols)
236{
237 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
238 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
239 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200240 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
241 opt->jo_term_rows = rows;
242 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
243 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200244}
245
246 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200247term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200248{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249 exarg_T split_ea;
250 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200251 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200252 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200253
254 if (check_restricted() || check_secure())
255 return;
256
257 term = (term_T *)alloc_clear(sizeof(term_T));
258 if (term == NULL)
259 return;
260 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200261 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200262 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200263 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200264 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200265
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200267 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200268 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200269 /* Create a new buffer in the current window. */
270 if (!can_abandon(curbuf, forceit))
271 {
272 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200273 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200274 return;
275 }
276 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
277 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200278 {
279 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200280 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200281 }
282 }
283 else if (opt->jo_hidden)
284 {
285 buf_T *buf;
286
287 /* Create a new buffer without a window. Make it the current buffer for
288 * a moment to be able to do the initialisations. */
289 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
290 BLN_NEW | BLN_LISTED);
291 if (buf == NULL || ml_open(buf) == FAIL)
292 {
293 vim_free(term);
294 return;
295 }
296 old_curbuf = curbuf;
297 --curbuf->b_nwindows;
298 curbuf = buf;
299 curwin->w_buffer = buf;
300 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200301 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200302 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200303 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200304 /* Open a new window or tab. */
305 split_ea.cmdidx = CMD_new;
306 split_ea.cmd = (char_u *)"new";
307 split_ea.arg = (char_u *)"";
308 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
309 {
310 split_ea.line2 = opt->jo_term_rows;
311 split_ea.addr_count = 1;
312 }
313 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
314 {
315 split_ea.line2 = opt->jo_term_cols;
316 split_ea.addr_count = 1;
317 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200318
Bram Moolenaarda43b612017-08-11 22:27:50 +0200319 ex_splitview(&split_ea);
320 if (curwin == old_curwin)
321 {
322 /* split failed */
323 vim_free(term);
324 return;
325 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200326 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200327 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200328 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200329
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200330 if (!opt->jo_hidden)
331 {
332 /* only one size was taken care of with :new, do the other one */
333 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
334 win_setheight(opt->jo_term_rows);
335 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
336 win_setwidth(opt->jo_term_cols);
337 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200338
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200339 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200340 term->tl_next = first_term;
341 first_term = term;
342
Bram Moolenaar293424c2017-07-26 23:11:01 +0200343 if (cmd == NULL || *cmd == NUL)
344 cmd = p_sh;
345
Bram Moolenaar78712a72017-08-05 14:50:12 +0200346 if (opt->jo_term_name != NULL)
347 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
348 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200349 {
350 int i;
351 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200352 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200353
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200354 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200355 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200356 /* Prepend a ! to the command name to avoid the buffer name equals
357 * the executable, otherwise ":w!" would overwrite it. */
358 if (i == 0)
359 vim_snprintf((char *)p, len, "!%s", cmd);
360 else
361 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200362 if (buflist_findname(p) == NULL)
363 {
364 curbuf->b_ffname = p;
365 break;
366 }
367 }
368 }
369 curbuf->b_fname = curbuf->b_ffname;
370
Bram Moolenaar37c45832017-08-12 16:01:04 +0200371 if (opt->jo_term_opencmd != NULL)
372 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
373
Bram Moolenaareb44a682017-08-03 22:44:55 +0200374 set_string_option_direct((char_u *)"buftype", -1,
375 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
376
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200377 /* Mark the buffer as not modifiable. It can only be made modifiable after
378 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200379 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200380
381 /* Set 'bufhidden' to "hide": allow closing the window. */
382 set_string_option_direct((char_u *)"bufhidden", -1,
383 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200384
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200385 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200386 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200387
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200388 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200389 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200390 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200391 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200392 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200393 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200394
395 if (old_curbuf != NULL)
396 {
397 --curbuf->b_nwindows;
398 curbuf = old_curbuf;
399 curwin->w_buffer = curbuf;
400 ++curbuf->b_nwindows;
401 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200402 }
403 else
404 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200405 buf_T *buf = curbuf;
406
Bram Moolenaard85f2712017-07-28 21:51:57 +0200407 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200408 if (old_curbuf != NULL)
409 {
410 --curbuf->b_nwindows;
411 curbuf = old_curbuf;
412 curwin->w_buffer = curbuf;
413 ++curbuf->b_nwindows;
414 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200415
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200416 /* Wiping out the buffer will also close the window and call
417 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200418 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200419 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200420}
421
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200422/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200423 * ":terminal": open a terminal window and execute a job in it.
424 */
425 void
426ex_terminal(exarg_T *eap)
427{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200428 jobopt_T opt;
429 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200430
431 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200432
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200433 cmd = eap->arg;
434 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
435 {
436 char_u *p;
437
438 cmd += 2;
439 p = skiptowhite(cmd);
440 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
441 opt.jo_term_finish = 'c';
442 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
443 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200444 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
445 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200446 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
447 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200448 else
449 {
450 if (*p)
451 *p = NUL;
452 EMSG2(_("E181: Invalid attribute: %s"), cmd);
453 return;
454 }
455 cmd = skipwhite(p);
456 }
457
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200458 if (eap->addr_count == 2)
459 {
460 opt.jo_term_rows = eap->line1;
461 opt.jo_term_cols = eap->line2;
462 }
463 else if (eap->addr_count == 1)
464 {
465 if (cmdmod.split & WSP_VERT)
466 opt.jo_term_cols = eap->line2;
467 else
468 opt.jo_term_rows = eap->line2;
469 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200470
Bram Moolenaarda43b612017-08-11 22:27:50 +0200471 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200472}
473
474/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200475 * Free the scrollback buffer for "term".
476 */
477 static void
478free_scrollback(term_T *term)
479{
480 int i;
481
482 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
483 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
484 ga_clear(&term->tl_scrollback);
485}
486
487/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200488 * Free a terminal and everything it refers to.
489 * Kills the job if there is one.
490 * Called when wiping out a buffer.
491 */
492 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200493free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200494{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200495 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200496 term_T *tp;
497
498 if (term == NULL)
499 return;
500 if (first_term == term)
501 first_term = term->tl_next;
502 else
503 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
504 if (tp->tl_next == term)
505 {
506 tp->tl_next = term->tl_next;
507 break;
508 }
509
510 if (term->tl_job != NULL)
511 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200512 if (term->tl_job->jv_status != JOB_ENDED
513 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200514 job_stop(term->tl_job, NULL, "kill");
515 job_unref(term->tl_job);
516 }
517
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200518 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200519
520 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200521 vim_free(term->tl_title);
522 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200523 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200524 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200525 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200526 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200527 if (in_terminal_loop == term)
528 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200529}
530
531/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200532 * Write job output "msg[len]" to the vterm.
533 */
534 static void
535term_write_job_output(term_T *term, char_u *msg, size_t len)
536{
537 VTerm *vterm = term->tl_vterm;
538 char_u *p;
539 size_t done;
540 size_t len_now;
541
542 for (done = 0; done < len; done += len_now)
543 {
544 for (p = msg + done; p < msg + len; )
545 {
546 if (*p == NL)
547 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200548 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200549 }
550 len_now = p - msg - done;
551 vterm_input_write(vterm, (char *)msg + done, len_now);
552 if (p < msg + len && *p == NL)
553 {
554 /* Convert NL to CR-NL, that appears to work best. */
555 vterm_input_write(vterm, "\r\n", 2);
556 ++len_now;
557 }
558 }
559
560 /* this invokes the damage callbacks */
561 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
562}
563
Bram Moolenaar1c844932017-07-24 23:36:41 +0200564 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200565update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200566{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200567 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200568 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200569 setcursor();
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200570 if (redraw)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200571 {
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200572 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200573 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200574 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200575#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200576 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200577 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200578#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200579 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200580}
581
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200582/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200583 * Invoked when "msg" output from a job was received. Write it to the terminal
584 * of "buffer".
585 */
586 void
587write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
588{
589 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200590 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200591
Bram Moolenaard85f2712017-07-28 21:51:57 +0200592 if (term->tl_vterm == NULL)
593 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200594 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200595 return;
596 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200597 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200598 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200599
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200600 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
601 * contents, thus no screen update is needed. */
Bram Moolenaar6d819742017-08-06 14:57:49 +0200602 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200603 {
604 /* TODO: only update once in a while. */
Bram Moolenaar5cc1f2c2017-08-13 15:16:53 +0200605 ch_log(term->tl_job->jv_channel, "updating screen");
606 if (buffer == curbuf)
607 {
608 update_screen(0);
609 update_cursor(term, TRUE);
610 }
611 else
612 redraw_after_callback();
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200613 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200614}
615
616/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200617 * Send a mouse position and click to the vterm
618 */
619 static int
620term_send_mouse(VTerm *vterm, int button, int pressed)
621{
622 VTermModifier mod = VTERM_MOD_NONE;
623
624 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
625 mouse_col - W_WINCOL(curwin), mod);
626 vterm_mouse_button(vterm, button, pressed, mod);
627 return TRUE;
628}
629
630/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200631 * Convert typed key "c" into bytes to send to the job.
632 * Return the number of bytes in "buf".
633 */
634 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200635term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200636{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200637 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200638 VTermKey key = VTERM_KEY_NONE;
639 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200640 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200641
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200642 switch (c)
643 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200644 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200645 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200646 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
647 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200648 case K_DEL: key = VTERM_KEY_DEL; break;
649 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200650 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
651 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200652 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200653 case K_S_END: mod = VTERM_MOD_SHIFT;
654 key = VTERM_KEY_END; break;
655 case K_C_END: mod = VTERM_MOD_CTRL;
656 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200657 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
658 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
659 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
660 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
661 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
662 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
663 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
664 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
665 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
666 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
667 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
668 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
669 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200670 case K_S_HOME: mod = VTERM_MOD_SHIFT;
671 key = VTERM_KEY_HOME; break;
672 case K_C_HOME: mod = VTERM_MOD_CTRL;
673 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200674 case K_INS: key = VTERM_KEY_INS; break;
675 case K_K0: key = VTERM_KEY_KP_0; break;
676 case K_K1: key = VTERM_KEY_KP_1; break;
677 case K_K2: key = VTERM_KEY_KP_2; break;
678 case K_K3: key = VTERM_KEY_KP_3; break;
679 case K_K4: key = VTERM_KEY_KP_4; break;
680 case K_K5: key = VTERM_KEY_KP_5; break;
681 case K_K6: key = VTERM_KEY_KP_6; break;
682 case K_K7: key = VTERM_KEY_KP_7; break;
683 case K_K8: key = VTERM_KEY_KP_8; break;
684 case K_K9: key = VTERM_KEY_KP_9; break;
685 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
686 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
687 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
688 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
689 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
690 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
691 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
692 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
693 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
694 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
695 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
696 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
697 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200698 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
699 key = VTERM_KEY_LEFT; break;
700 case K_C_LEFT: mod = VTERM_MOD_CTRL;
701 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
703 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
704 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200705 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
706 key = VTERM_KEY_RIGHT; break;
707 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
708 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200709 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200710 case K_S_UP: mod = VTERM_MOD_SHIFT;
711 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200712 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200713
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200714 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
715 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
716 case K_MOUSELEFT: /* TODO */ return 0;
717 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200718
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200719 case K_LEFTMOUSE:
720 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
721 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
722 case K_LEFTRELEASE:
723 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
724 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
725 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
726 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
727 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
728 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
729 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
730 case K_X1MOUSE: /* TODO */ return 0;
731 case K_X1DRAG: /* TODO */ return 0;
732 case K_X1RELEASE: /* TODO */ return 0;
733 case K_X2MOUSE: /* TODO */ return 0;
734 case K_X2DRAG: /* TODO */ return 0;
735 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200736
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200737 case K_IGNORE: return 0;
738 case K_NOP: return 0;
739 case K_UNDO: return 0;
740 case K_HELP: return 0;
741 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
742 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
743 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
744 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
745 case K_SELECT: return 0;
746#ifdef FEAT_GUI
747 case K_VER_SCROLLBAR: return 0;
748 case K_HOR_SCROLLBAR: return 0;
749#endif
750#ifdef FEAT_GUI_TABLINE
751 case K_TABLINE: return 0;
752 case K_TABMENU: return 0;
753#endif
754#ifdef FEAT_NETBEANS_INTG
755 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
756#endif
757#ifdef FEAT_DND
758 case K_DROP: return 0;
759#endif
760#ifdef FEAT_AUTOCMD
761 case K_CURSORHOLD: return 0;
762#endif
763 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
764 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200765 }
766
767 /*
768 * Convert special keys to vterm keys:
769 * - Write keys to vterm: vterm_keyboard_key()
770 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200771 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200772 */
773 if (key != VTERM_KEY_NONE)
774 /* Special key, let vterm convert it. */
775 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200776 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200777 /* Normal character, let vterm convert it. */
778 vterm_keyboard_unichar(vterm, c, mod);
779
780 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200781 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200782}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200783
Bram Moolenaar938783d2017-07-16 20:13:26 +0200784/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200785 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200786 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200787 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200788term_job_running(term_T *term)
789{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200790 /* Also consider the job finished when the channel is closed, to avoid a
791 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200792 return term != NULL
793 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200794 && term->tl_job->jv_status == JOB_STARTED
795 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200796}
797
798/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200799 * Add the last line of the scrollback buffer to the buffer in the window.
800 */
801 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200802add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200803{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200804 buf_T *buf = term->tl_buffer;
805 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
806 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200807
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200808 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200809 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200810 {
811 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200812 curbuf = buf;
813 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814 curbuf = curwin->w_buffer;
815 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200816}
817
818/*
819 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200820 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200821 */
822 static void
823move_terminal_to_buffer(term_T *term)
824{
825 win_T *wp;
826 int len;
827 int lines_skipped = 0;
828 VTermPos pos;
829 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200830 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200831 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200832
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200833 if (term->tl_vterm == NULL)
834 return;
835 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200836 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
837 {
838 len = 0;
839 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
840 if (vterm_screen_get_cell(screen, pos, &cell) != 0
841 && cell.chars[0] != NUL)
842 len = pos.col + 1;
843
844 if (len == 0)
845 ++lines_skipped;
846 else
847 {
848 while (lines_skipped > 0)
849 {
850 /* Line was skipped, add an empty line. */
851 --lines_skipped;
852 if (ga_grow(&term->tl_scrollback, 1) == OK)
853 {
854 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
855 + term->tl_scrollback.ga_len;
856
857 line->sb_cols = 0;
858 line->sb_cells = NULL;
859 ++term->tl_scrollback.ga_len;
860
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200861 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200862 }
863 }
864
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200865 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200866 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
867 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200868 garray_T ga;
869 int width;
870 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200871 + term->tl_scrollback.ga_len;
872
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200873 ga_init2(&ga, 1, 100);
874 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200875 {
876 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200877 {
878 width = 1;
879 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
880 if (ga_grow(&ga, 1) == OK)
881 ga.ga_len += mb_char2bytes(' ',
882 (char_u *)ga.ga_data + ga.ga_len);
883 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200884 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200885 {
886 width = cell.width;
887
888 p[pos.col].width = cell.width;
889 p[pos.col].attrs = cell.attrs;
890 p[pos.col].fg = cell.fg;
891 p[pos.col].bg = cell.bg;
892
893 if (ga_grow(&ga, MB_MAXBYTES) == OK)
894 {
895 int i;
896 int c;
897
898 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
899 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
900 (char_u *)ga.ga_data + ga.ga_len);
901 }
902 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200903 }
904 line->sb_cols = len;
905 line->sb_cells = p;
906 ++term->tl_scrollback.ga_len;
907
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200908 if (ga_grow(&ga, 1) == FAIL)
909 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
910 else
911 {
912 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
913 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
914 }
915 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200916 }
917 else
918 vim_free(p);
919 }
920 }
921
922 FOR_ALL_WINDOWS(wp)
923 {
924 if (wp->w_buffer == term->tl_buffer)
925 {
926 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
927 wp->w_cursor.col = 0;
928 wp->w_valid = 0;
Bram Moolenaare0f314a2017-08-13 16:01:31 +0200929 if (wp->w_cursor.lnum >= wp->w_height)
930 {
931 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
932
933 if (wp->w_topline < min_topline)
934 wp->w_topline = min_topline;
935 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200936 redraw_win_later(wp, NOT_VALID);
937 }
938 }
939}
940
941 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200942set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200943{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200944 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200945 vim_free(term->tl_status_text);
946 term->tl_status_text = NULL;
947 if (term->tl_buffer == curbuf)
948 maketitle();
949}
950
951/*
952 * Called after the job if finished and Terminal mode is not active:
953 * Move the vterm contents into the scrollback buffer and free the vterm.
954 */
955 static void
956cleanup_vterm(term_T *term)
957{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200958 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200959 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200960 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200961 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200962}
963
964/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200965 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200966 * Suspends updating the terminal window.
967 */
968 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200969term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200970{
971 term_T *term = curbuf->b_term;
972
973 /* Append the current terminal contents to the buffer. */
974 move_terminal_to_buffer(term);
975
Bram Moolenaar6d819742017-08-06 14:57:49 +0200976 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200977
Bram Moolenaar6d819742017-08-06 14:57:49 +0200978 /* Move the window cursor to the position of the cursor in the
979 * terminal. */
980 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
981 + term->tl_cursor_pos.row + 1;
982 check_cursor();
983 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200984
Bram Moolenaar6d819742017-08-06 14:57:49 +0200985 /* Display the same lines as in the terminal. */
986 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200987}
988
989/*
990 * Returns TRUE if the current window contains a terminal and we are in
991 * Terminal-Normal mode.
992 */
993 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200994term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200995{
996 term_T *term = curbuf->b_term;
997
Bram Moolenaar6d819742017-08-06 14:57:49 +0200998 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200999}
1000
1001/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001002 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +02001003 * Restores updating the terminal window.
1004 */
1005 void
Bram Moolenaar6d819742017-08-06 14:57:49 +02001006term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +02001007{
1008 term_T *term = curbuf->b_term;
1009 sb_line_T *line;
1010 garray_T *gap;
1011
1012 /* Remove the terminal contents from the scrollback and the buffer. */
1013 gap = &term->tl_scrollback;
1014 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1015 {
1016 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1017 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1018 vim_free(line->sb_cells);
1019 --gap->ga_len;
1020 if (gap->ga_len == 0)
1021 break;
1022 }
1023 check_cursor();
1024
Bram Moolenaar6d819742017-08-06 14:57:49 +02001025 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001026
1027 if (term->tl_channel_closed)
1028 cleanup_vterm(term);
1029 redraw_buf_and_status_later(curbuf, NOT_VALID);
1030}
1031
1032/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001033 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001034 * Note: while waiting a terminal may be closed and freed if the channel is
1035 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001036 * TODO: use terminal mode mappings.
1037 */
1038 static int
1039term_vgetc()
1040{
1041 int c;
1042
1043 ++no_mapping;
1044 ++allow_keys;
1045 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001046#ifdef WIN3264
1047 ctrl_break_was_pressed = FALSE;
1048#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001049 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001050 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001051 --no_mapping;
1052 --allow_keys;
1053 return c;
1054}
1055
1056/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001057 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001058 * Return FAIL when the key needs to be handled in Normal mode.
1059 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001060 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001061 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001062send_keys_to_term(term_T *term, int c, int typed)
1063{
1064 char msg[KEY_BUF_LEN];
1065 size_t len;
1066 static int mouse_was_outside = FALSE;
1067 int dragging_outside = FALSE;
1068
1069 /* Catch keys that need to be handled as in Normal mode. */
1070 switch (c)
1071 {
1072 case NUL:
1073 case K_ZERO:
1074 if (typed)
1075 stuffcharReadbuff(c);
1076 return FAIL;
1077
1078 case K_IGNORE:
1079 return FAIL;
1080
1081 case K_LEFTDRAG:
1082 case K_MIDDLEDRAG:
1083 case K_RIGHTDRAG:
1084 case K_X1DRAG:
1085 case K_X2DRAG:
1086 dragging_outside = mouse_was_outside;
1087 /* FALLTHROUGH */
1088 case K_LEFTMOUSE:
1089 case K_LEFTMOUSE_NM:
1090 case K_LEFTRELEASE:
1091 case K_LEFTRELEASE_NM:
1092 case K_MIDDLEMOUSE:
1093 case K_MIDDLERELEASE:
1094 case K_RIGHTMOUSE:
1095 case K_RIGHTRELEASE:
1096 case K_X1MOUSE:
1097 case K_X1RELEASE:
1098 case K_X2MOUSE:
1099 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001100
1101 case K_MOUSEUP:
1102 case K_MOUSEDOWN:
1103 case K_MOUSELEFT:
1104 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001105 if (mouse_row < W_WINROW(curwin)
1106 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1107 || mouse_col < W_WINCOL(curwin)
1108 || mouse_col >= W_ENDCOL(curwin)
1109 || dragging_outside)
1110 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001111 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001112 if (typed)
1113 {
1114 stuffcharReadbuff(c);
1115 mouse_was_outside = TRUE;
1116 }
1117 return FAIL;
1118 }
1119 }
1120 if (typed)
1121 mouse_was_outside = FALSE;
1122
1123 /* Convert the typed key to a sequence of bytes for the job. */
1124 len = term_convert_key(term, c, msg);
1125 if (len > 0)
1126 /* TODO: if FAIL is returned, stop? */
1127 channel_send(term->tl_job->jv_channel, PART_IN,
1128 (char_u *)msg, (int)len, NULL);
1129
1130 return OK;
1131}
1132
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001133 static void
1134position_cursor(win_T *wp, VTermPos *pos)
1135{
1136 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1137 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1138 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1139}
1140
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001141/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001142 * Handle CTRL-W "": send register contents to the job.
1143 */
1144 static void
1145term_paste_register(int prev_c UNUSED)
1146{
1147 int c;
1148 list_T *l;
1149 listitem_T *item;
1150 long reglen = 0;
1151 int type;
1152
1153#ifdef FEAT_CMDL_INFO
1154 if (add_to_showcmd(prev_c))
1155 if (add_to_showcmd('"'))
1156 out_flush();
1157#endif
1158 c = term_vgetc();
1159#ifdef FEAT_CMDL_INFO
1160 clear_showcmd();
1161#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001162 if (!term_use_loop())
1163 /* job finished while waiting for a character */
1164 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001165
1166 /* CTRL-W "= prompt for expression to evaluate. */
1167 if (c == '=' && get_expr_register() != '=')
1168 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001169 if (!term_use_loop())
1170 /* job finished while waiting for a character */
1171 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001172
1173 l = (list_T *)get_reg_contents(c, GREG_LIST);
1174 if (l != NULL)
1175 {
1176 type = get_reg_type(c, &reglen);
1177 for (item = l->lv_first; item != NULL; item = item->li_next)
1178 {
1179 char_u *s = get_tv_string(&item->li_tv);
1180
1181 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1182 s, STRLEN(s), NULL);
1183 if (item->li_next != NULL || type == MLINE)
1184 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1185 (char_u *)"\r", 1, NULL);
1186 }
1187 list_free(l);
1188 }
1189}
1190
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001191#if defined(FEAT_GUI) || defined(PROTO)
1192/*
1193 * Return TRUE when the cursor of the terminal should be displayed.
1194 */
1195 int
1196use_terminal_cursor()
1197{
1198 return in_terminal_loop != NULL;
1199}
1200
1201 cursorentry_T *
1202term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1203{
1204 term_T *term = in_terminal_loop;
1205 static cursorentry_T entry;
1206
1207 vim_memset(&entry, 0, sizeof(entry));
1208 entry.shape = entry.mshape =
1209 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1210 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1211 SHAPE_BLOCK;
1212 entry.percentage = 20;
1213 if (term->tl_cursor_blink)
1214 {
1215 entry.blinkwait = 700;
1216 entry.blinkon = 400;
1217 entry.blinkon = 250;
1218 }
1219 *fg = gui.back_pixel;
1220 if (term->tl_cursor_color == NULL)
1221 *bg = gui.norm_pixel;
1222 else
1223 *bg = color_name2handle(term->tl_cursor_color);
1224 entry.name = "n";
1225 entry.used_for = SHAPE_CURSOR;
1226
1227 return &entry;
1228}
1229#endif
1230
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001231static int did_change_cursor = FALSE;
1232
1233 static void
1234may_set_cursor_props(term_T *term)
1235{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001236#ifdef FEAT_GUI
1237 /* For the GUI the cursor properties are obtained with
1238 * term_get_cursor_shape(). */
1239 if (gui.in_use)
1240 return;
1241#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001242 if (in_terminal_loop == term)
1243 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001244 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001245 if (term->tl_cursor_color != NULL)
1246 term_cursor_color(term->tl_cursor_color);
1247 else
1248 term_cursor_color((char_u *)"");
1249 /* do both blink and shape+blink, in case setting shape does not work */
1250 term_cursor_blink(term->tl_cursor_blink);
1251 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1252 }
1253}
1254
1255 static void
1256may_restore_cursor_props(void)
1257{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001258#ifdef FEAT_GUI
1259 if (gui.in_use)
1260 return;
1261#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001262 if (did_change_cursor)
1263 {
1264 did_change_cursor = FALSE;
1265 ui_cursor_shape_forced(TRUE);
1266 term_cursor_color((char_u *)"");
1267 term_cursor_blink(FALSE);
1268 }
1269}
1270
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001271/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001272 * Returns TRUE if the current window contains a terminal and we are sending
1273 * keys to the job.
1274 */
1275 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001276term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001277{
1278 term_T *term = curbuf->b_term;
1279
1280 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001281 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001282 && term->tl_vterm != NULL
1283 && term_job_running(term);
1284}
1285
1286/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001287 * Wait for input and send it to the job.
1288 * Return when the start of a CTRL-W command is typed or anything else that
1289 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001290 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1291 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001292 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001293 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001294terminal_loop(void)
1295{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001296 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001297 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001298 int ret;
1299
Bram Moolenaar679653e2017-08-13 14:13:19 +02001300 /* Remember the terminal we are sending keys to. However, the terminal
1301 * might be closed while waiting for a character, e.g. typing "exit" in a
1302 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1303 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001304 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001305
1306 if (*curwin->w_p_tk != NUL)
1307 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001308 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001309 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001310
1311 for (;;)
1312 {
1313 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001314 /* Repeat redrawing in case a message is received while redrawing. */
1315 while (curwin->w_redr_type != 0)
1316 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001317 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001318
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001319 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001320 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001321 /* job finished while waiting for a character */
1322 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001323 if (c == K_IGNORE)
1324 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001325
Bram Moolenaarfae42832017-08-01 22:24:26 +02001326#ifdef UNIX
1327 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1328#endif
1329#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001330 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001331 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001332 if (ctrl_break_was_pressed)
1333 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001334#endif
1335
Bram Moolenaar69198192017-08-05 14:10:48 +02001336 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001337 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001338 int prev_c = c;
1339
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001340#ifdef FEAT_CMDL_INFO
1341 if (add_to_showcmd(c))
1342 out_flush();
1343#endif
1344 c = term_vgetc();
1345#ifdef FEAT_CMDL_INFO
1346 clear_showcmd();
1347#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001348 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001349 /* job finished while waiting for a character */
1350 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001351
Bram Moolenaar69198192017-08-05 14:10:48 +02001352 if (prev_c == Ctrl_BSL)
1353 {
1354 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001355 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001356 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1357 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001358 ret = FAIL;
1359 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001360 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001361 /* Send both keys to the terminal. */
1362 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1363 }
1364 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001365 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001366 /* "CTRL-W .": send CTRL-W to the job */
1367 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001368 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001369 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001370 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001371 /* CTRL-W N : go to Terminal-Normal mode. */
1372 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001373 ret = FAIL;
1374 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001375 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001376 else if (c == '"')
1377 {
1378 term_paste_register(prev_c);
1379 continue;
1380 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001381 else if (termkey == 0 || c != termkey)
1382 {
1383 stuffcharReadbuff(Ctrl_W);
1384 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001385 ret = OK;
1386 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001387 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001388 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001389 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001390 {
1391 ret = OK;
1392 goto theend;
1393 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001394 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001395 ret = FAIL;
1396
1397theend:
1398 in_terminal_loop = NULL;
1399 may_restore_cursor_props();
1400 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001401}
1402
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001403/*
1404 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001405 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001406 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001407 */
1408 void
1409term_job_ended(job_T *job)
1410{
1411 term_T *term;
1412 int did_one = FALSE;
1413
1414 for (term = first_term; term != NULL; term = term->tl_next)
1415 if (term->tl_job == job)
1416 {
1417 vim_free(term->tl_title);
1418 term->tl_title = NULL;
1419 vim_free(term->tl_status_text);
1420 term->tl_status_text = NULL;
1421 redraw_buf_and_status_later(term->tl_buffer, VALID);
1422 did_one = TRUE;
1423 }
1424 if (did_one)
1425 redraw_statuslines();
1426 if (curbuf->b_term != NULL)
1427 {
1428 if (curbuf->b_term->tl_job == job)
1429 maketitle();
1430 update_cursor(curbuf->b_term, TRUE);
1431 }
1432}
1433
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001434 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001435may_toggle_cursor(term_T *term)
1436{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001437 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001438 {
1439 if (term->tl_cursor_visible)
1440 cursor_on();
1441 else
1442 cursor_off();
1443 }
1444}
1445
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001446 static int
1447handle_damage(VTermRect rect, void *user)
1448{
1449 term_T *term = (term_T *)user;
1450
1451 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1452 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1453 redraw_buf_later(term->tl_buffer, NOT_VALID);
1454 return 1;
1455}
1456
1457 static int
1458handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1459{
1460 term_T *term = (term_T *)user;
1461
1462 /* TODO */
1463 redraw_buf_later(term->tl_buffer, NOT_VALID);
1464 return 1;
1465}
1466
1467 static int
1468handle_movecursor(
1469 VTermPos pos,
1470 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001471 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001472 void *user)
1473{
1474 term_T *term = (term_T *)user;
1475 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001476
1477 term->tl_cursor_pos = pos;
1478 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001479
1480 FOR_ALL_WINDOWS(wp)
1481 {
1482 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001483 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001484 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001485 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001486 {
1487 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001488 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001489 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001490
1491 return 1;
1492}
1493
Bram Moolenaar21554412017-07-24 21:44:43 +02001494 static int
1495handle_settermprop(
1496 VTermProp prop,
1497 VTermValue *value,
1498 void *user)
1499{
1500 term_T *term = (term_T *)user;
1501
1502 switch (prop)
1503 {
1504 case VTERM_PROP_TITLE:
1505 vim_free(term->tl_title);
Bram Moolenaar274a52f2017-08-13 16:09:31 +02001506 /* a blank title isn't useful, make it empty, so that "running" is
1507 * displayed */
1508 if (*skipwhite((char_u *)value->string) == NUL)
1509 term->tl_title = NULL;
1510 else
1511 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaar21554412017-07-24 21:44:43 +02001512 vim_free(term->tl_status_text);
1513 term->tl_status_text = NULL;
1514 if (term == curbuf->b_term)
1515 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001516 break;
1517
1518 case VTERM_PROP_CURSORVISIBLE:
1519 term->tl_cursor_visible = value->boolean;
1520 may_toggle_cursor(term);
1521 out_flush();
1522 break;
1523
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001524 case VTERM_PROP_CURSORBLINK:
1525 term->tl_cursor_blink = value->boolean;
1526 may_set_cursor_props(term);
1527 break;
1528
1529 case VTERM_PROP_CURSORSHAPE:
1530 term->tl_cursor_shape = value->number;
1531 may_set_cursor_props(term);
1532 break;
1533
1534 case VTERM_PROP_CURSORCOLOR:
1535 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001536 if (*value->string == NUL)
1537 term->tl_cursor_color = NULL;
1538 else
1539 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001540 may_set_cursor_props(term);
1541 break;
1542
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001543 case VTERM_PROP_ALTSCREEN:
1544 /* TODO: do anything else? */
1545 term->tl_using_altscreen = value->boolean;
1546 break;
1547
Bram Moolenaar21554412017-07-24 21:44:43 +02001548 default:
1549 break;
1550 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001551 /* Always return 1, otherwise vterm doesn't store the value internally. */
1552 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001553}
1554
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001555/*
1556 * The job running in the terminal resized the terminal.
1557 */
1558 static int
1559handle_resize(int rows, int cols, void *user)
1560{
1561 term_T *term = (term_T *)user;
1562 win_T *wp;
1563
1564 term->tl_rows = rows;
1565 term->tl_cols = cols;
1566 FOR_ALL_WINDOWS(wp)
1567 {
1568 if (wp->w_buffer == term->tl_buffer)
1569 {
1570 win_setheight_win(rows, wp);
1571 win_setwidth_win(cols, wp);
1572 }
1573 }
1574
1575 redraw_buf_later(term->tl_buffer, NOT_VALID);
1576 return 1;
1577}
1578
Bram Moolenaard85f2712017-07-28 21:51:57 +02001579/*
1580 * Handle a line that is pushed off the top of the screen.
1581 */
1582 static int
1583handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1584{
1585 term_T *term = (term_T *)user;
1586
1587 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001588 if (ga_grow(&term->tl_scrollback, 1) == OK)
1589 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001590 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001591 int len = 0;
1592 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001593 int c;
1594 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001595 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001596 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001597
1598 /* do not store empty cells at the end */
1599 for (i = 0; i < cols; ++i)
1600 if (cells[i].chars[0] != 0)
1601 len = i + 1;
1602
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001603 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001604 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001605 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001606 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001607 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001608 for (col = 0; col < len; col += cells[col].width)
1609 {
1610 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1611 {
1612 ga.ga_len = 0;
1613 break;
1614 }
1615 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1616 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1617 (char_u *)ga.ga_data + ga.ga_len);
1618 p[col].width = cells[col].width;
1619 p[col].attrs = cells[col].attrs;
1620 p[col].fg = cells[col].fg;
1621 p[col].bg = cells[col].bg;
1622 }
1623 }
1624 if (ga_grow(&ga, 1) == FAIL)
1625 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1626 else
1627 {
1628 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1629 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1630 }
1631 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001632
1633 line = (sb_line_T *)term->tl_scrollback.ga_data
1634 + term->tl_scrollback.ga_len;
1635 line->sb_cols = len;
1636 line->sb_cells = p;
1637 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001638 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001639 }
1640 return 0; /* ignored */
1641}
1642
Bram Moolenaar21554412017-07-24 21:44:43 +02001643static VTermScreenCallbacks screen_callbacks = {
1644 handle_damage, /* damage */
1645 handle_moverect, /* moverect */
1646 handle_movecursor, /* movecursor */
1647 handle_settermprop, /* settermprop */
1648 NULL, /* bell */
1649 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001650 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001651 NULL /* sb_popline */
1652};
1653
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001654/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001655 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001656 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001657 */
1658 void
1659term_channel_closed(channel_T *ch)
1660{
1661 term_T *term;
1662 int did_one = FALSE;
1663
1664 for (term = first_term; term != NULL; term = term->tl_next)
1665 if (term->tl_job == ch->ch_job)
1666 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001667 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001668 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001669
Bram Moolenaard85f2712017-07-28 21:51:57 +02001670 vim_free(term->tl_title);
1671 term->tl_title = NULL;
1672 vim_free(term->tl_status_text);
1673 term->tl_status_text = NULL;
1674
Bram Moolenaar423802d2017-07-30 16:52:24 +02001675 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001676 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001677 {
1678 int fnum = term->tl_buffer->b_fnum;
1679
Bram Moolenaar423802d2017-07-30 16:52:24 +02001680 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001681
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001682 if (term->tl_finish == 'c')
1683 {
1684 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001685 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001686 curbuf = term->tl_buffer;
1687 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1688 break;
1689 }
1690 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1691 {
1692 char buf[50];
1693
1694 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001695 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001696 vim_snprintf(buf, sizeof(buf),
1697 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001698 ? "botright sbuf %d"
1699 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001700 do_cmdline_cmd((char_u *)buf);
1701 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001702 else
1703 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001704 }
1705
Bram Moolenaard85f2712017-07-28 21:51:57 +02001706 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001707 }
1708 if (did_one)
1709 {
1710 redraw_statuslines();
1711
1712 /* Need to break out of vgetc(). */
1713 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001714 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001715
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001716 term = curbuf->b_term;
1717 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001718 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001719 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001720 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001721 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001722 }
1723 }
1724}
1725
1726/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001727 * Reverse engineer the RGB value into a cterm color index.
1728 * First color is 1. Return 0 if no match found.
1729 */
1730 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001731color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001732{
1733 int red = color->red;
1734 int blue = color->blue;
1735 int green = color->green;
1736
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001737 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001738 if (red == 0)
1739 {
1740 if (green == 0)
1741 {
1742 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001743 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001744 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001745 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001746 }
1747 else if (green == 224)
1748 {
1749 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001750 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001751 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001752 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001753 }
1754 }
1755 else if (red == 224)
1756 {
1757 if (green == 0)
1758 {
1759 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001760 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001761 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001762 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001763 }
1764 else if (green == 224)
1765 {
1766 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001767 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001768 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001769 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001770 }
1771 }
1772 else if (red == 128)
1773 {
1774 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001775 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001776 }
1777 else if (red == 255)
1778 {
1779 if (green == 64)
1780 {
1781 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001782 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001783 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001784 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001785 }
1786 else if (green == 255)
1787 {
1788 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001789 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001790 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001791 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001792 }
1793 }
1794 else if (red == 64)
1795 {
1796 if (green == 64)
1797 {
1798 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001799 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001800 }
1801 else if (green == 255)
1802 {
1803 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001804 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001805 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001806 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001807 }
1808 }
1809 if (t_colors >= 256)
1810 {
1811 if (red == blue && red == green)
1812 {
1813 /* 24-color greyscale */
1814 static int cutoff[23] = {
1815 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1816 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1817 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1818 int i;
1819
1820 for (i = 0; i < 23; ++i)
1821 if (red < cutoff[i])
1822 return i + 233;
1823 return 256;
1824 }
1825
1826 /* 216-color cube */
1827 return 17 + ((red + 25) / 0x33) * 36
1828 + ((green + 25) / 0x33) * 6
1829 + (blue + 25) / 0x33;
1830 }
1831 return 0;
1832}
1833
1834/*
1835 * Convert the attributes of a vterm cell into an attribute index.
1836 */
1837 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001838cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001839{
1840 int attr = 0;
1841
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001842 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001843 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001844 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001845 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001846 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001847 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001848 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001849 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001850 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001851 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001852
1853#ifdef FEAT_GUI
1854 if (gui.in_use)
1855 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001856 guicolor_T fg, bg;
1857
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001858 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1859 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001860 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001861 }
1862 else
1863#endif
1864#ifdef FEAT_TERMGUICOLORS
1865 if (p_tgc)
1866 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001867 guicolor_T fg, bg;
1868
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001869 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1870 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001871
1872 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001873 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001874 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001875#endif
1876 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001877 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001878 int fg = color2index(&cellfg, TRUE, &bold);
1879 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001880
1881 /* with 8 colors set the bold attribute to get a bright foreground */
1882 if (bold == TRUE)
1883 attr |= HL_BOLD;
1884 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001885 }
1886 return 0;
1887}
1888
1889/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001890 * Called to update a window that contains an active terminal.
1891 * Returns FAIL when there is no terminal running in this window or in
1892 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001893 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001894 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001895term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001896{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001897 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001898 VTerm *vterm;
1899 VTermScreen *screen;
1900 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001901 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001902
Bram Moolenaar6d819742017-08-06 14:57:49 +02001903 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001904 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001905
Bram Moolenaard85f2712017-07-28 21:51:57 +02001906 vterm = term->tl_vterm;
1907 screen = vterm_obtain_screen(vterm);
1908 state = vterm_obtain_state(vterm);
1909
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001910 /*
1911 * If the window was resized a redraw will be triggered and we get here.
1912 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1913 */
1914 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1915 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001916 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001917 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1918 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1919 win_T *twp;
1920
1921 FOR_ALL_WINDOWS(twp)
1922 {
1923 /* When more than one window shows the same terminal, use the
1924 * smallest size. */
1925 if (twp->w_buffer == term->tl_buffer)
1926 {
1927 if (!term->tl_rows_fixed && rows > twp->w_height)
1928 rows = twp->w_height;
1929 if (!term->tl_cols_fixed && cols > twp->w_width)
1930 cols = twp->w_width;
1931 }
1932 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001933
1934 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001935 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001936 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001937 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001938 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001939
1940 /* The cursor may have been moved when resizing. */
1941 vterm_state_get_cursorpos(state, &pos);
1942 position_cursor(wp, &pos);
1943
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001944 /* TODO: Only redraw what changed. */
1945 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001946 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001947 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001948 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001949
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950 if (pos.row < term->tl_rows)
1951 {
1952 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001953 {
1954 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001955 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001956
Bram Moolenaareeac6772017-07-23 15:48:37 +02001957 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1958 vim_memset(&cell, 0, sizeof(cell));
1959
1960 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001961 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001962 if (c == NUL)
1963 {
1964 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001965#if defined(FEAT_MBYTE)
1966 if (enc_utf8)
1967 ScreenLinesUC[off] = NUL;
1968#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001969 }
1970 else
1971 {
1972#if defined(FEAT_MBYTE)
1973 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001974 {
1975 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001976 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001977 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001978 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001979 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001980 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001981 if (enc_utf8)
1982 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001983 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001984#else
1985 ScreenLines[off] = c;
1986#endif
1987 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001988 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001989
1990 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001991 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001992 if (cell.width == 2)
1993 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001994 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001995#if defined(FEAT_MBYTE)
1996 if (enc_utf8)
1997 ScreenLinesUC[off] = NUL;
1998#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001999 ++pos.col;
2000 ++off;
2001 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002002 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002003 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02002004 else
2005 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002006
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002007 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
2008 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02002009 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02002010
2011 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02002012}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002013
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002014/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002015 * Return TRUE if "wp" is a terminal window where the job has finished.
2016 */
2017 int
2018term_is_finished(buf_T *buf)
2019{
2020 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2021}
2022
2023/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002024 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002025 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002026 */
2027 int
2028term_show_buffer(buf_T *buf)
2029{
2030 term_T *term = buf->b_term;
2031
Bram Moolenaar6d819742017-08-06 14:57:49 +02002032 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002033}
2034
2035/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002036 * The current buffer is going to be changed. If there is terminal
2037 * highlighting remove it now.
2038 */
2039 void
2040term_change_in_curbuf(void)
2041{
2042 term_T *term = curbuf->b_term;
2043
2044 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2045 {
2046 free_scrollback(term);
2047 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002048
2049 /* The buffer is now like a normal buffer, it cannot be easily
2050 * abandoned when changed. */
2051 set_string_option_direct((char_u *)"buftype", -1,
2052 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002053 }
2054}
2055
2056/*
2057 * Get the screen attribute for a position in the buffer.
2058 */
2059 int
2060term_get_attr(buf_T *buf, linenr_T lnum, int col)
2061{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002062 term_T *term = buf->b_term;
2063 sb_line_T *line;
2064 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002065
Bram Moolenaar70229f92017-07-29 16:01:53 +02002066 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002067 return 0;
2068 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2069 if (col >= line->sb_cols)
2070 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002071 cellattr = line->sb_cells + col;
2072 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002073}
2074
2075/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002076 * Create a new vterm and initialize it.
2077 */
2078 static void
2079create_vterm(term_T *term, int rows, int cols)
2080{
2081 VTerm *vterm;
2082 VTermScreen *screen;
2083
2084 vterm = vterm_new(rows, cols);
2085 term->tl_vterm = vterm;
2086 screen = vterm_obtain_screen(vterm);
2087 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2088 /* TODO: depends on 'encoding'. */
2089 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002090
2091 /* Vterm uses a default black background. Set it to white when
2092 * 'background' is "light". */
2093 if (*p_bg == 'l')
2094 {
2095 VTermColor fg, bg;
2096
2097 fg.red = fg.green = fg.blue = 0;
2098 bg.red = bg.green = bg.blue = 255;
2099 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2100 }
2101
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002102 /* Required to initialize most things. */
2103 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002104
2105 /* Allow using alternate screen. */
2106 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002107}
2108
Bram Moolenaar21554412017-07-24 21:44:43 +02002109/*
2110 * Return the text to show for the buffer name and status.
2111 */
2112 char_u *
2113term_get_status_text(term_T *term)
2114{
2115 if (term->tl_status_text == NULL)
2116 {
2117 char_u *txt;
2118 size_t len;
2119
Bram Moolenaar6d819742017-08-06 14:57:49 +02002120 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002121 {
2122 if (term_job_running(term))
2123 txt = (char_u *)_("Terminal");
2124 else
2125 txt = (char_u *)_("Terminal-finished");
2126 }
2127 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002128 txt = term->tl_title;
2129 else if (term_job_running(term))
2130 txt = (char_u *)_("running");
2131 else
2132 txt = (char_u *)_("finished");
2133 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002134 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002135 if (term->tl_status_text != NULL)
2136 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2137 term->tl_buffer->b_fname, txt);
2138 }
2139 return term->tl_status_text;
2140}
2141
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002142/*
2143 * Mark references in jobs of terminals.
2144 */
2145 int
2146set_ref_in_term(int copyID)
2147{
2148 int abort = FALSE;
2149 term_T *term;
2150 typval_T tv;
2151
2152 for (term = first_term; term != NULL; term = term->tl_next)
2153 if (term->tl_job != NULL)
2154 {
2155 tv.v_type = VAR_JOB;
2156 tv.vval.v_job = term->tl_job;
2157 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2158 }
2159 return abort;
2160}
2161
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002162/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002163 * Get the buffer from the first argument in "argvars".
2164 * Returns NULL when the buffer is not for a terminal window.
2165 */
2166 static buf_T *
2167term_get_buf(typval_T *argvars)
2168{
2169 buf_T *buf;
2170
2171 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2172 ++emsg_off;
2173 buf = get_buf_tv(&argvars[0], FALSE);
2174 --emsg_off;
2175 if (buf == NULL || buf->b_term == NULL)
2176 return NULL;
2177 return buf;
2178}
2179
2180/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002181 * "term_getaltscreen(buf)" function
2182 */
2183 void
2184f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2185{
2186 buf_T *buf = term_get_buf(argvars);
2187
2188 if (buf == NULL)
2189 return;
2190 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2191}
2192
2193/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002194 * "term_getattr(attr, name)" function
2195 */
2196 void
2197f_term_getattr(typval_T *argvars, typval_T *rettv)
2198{
2199 int attr;
2200 size_t i;
2201 char_u *name;
2202
2203 static struct {
2204 char *name;
2205 int attr;
2206 } attrs[] = {
2207 {"bold", HL_BOLD},
2208 {"italic", HL_ITALIC},
2209 {"underline", HL_UNDERLINE},
2210 {"strike", HL_STANDOUT},
2211 {"reverse", HL_INVERSE},
2212 };
2213
2214 attr = get_tv_number(&argvars[0]);
2215 name = get_tv_string_chk(&argvars[1]);
2216 if (name == NULL)
2217 return;
2218
2219 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2220 if (STRCMP(name, attrs[i].name) == 0)
2221 {
2222 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2223 break;
2224 }
2225}
2226
2227/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002228 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002229 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002230 void
2231f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002232{
Bram Moolenaar97870002017-07-30 18:28:38 +02002233 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002234 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002235 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002236 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002237
Bram Moolenaar97870002017-07-30 18:28:38 +02002238 if (rettv_list_alloc(rettv) == FAIL)
2239 return;
2240 if (buf == NULL)
2241 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002242 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002243
2244 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002245 list_append_number(l, term->tl_cursor_pos.row + 1);
2246 list_append_number(l, term->tl_cursor_pos.col + 1);
2247
2248 d = dict_alloc();
2249 if (d != NULL)
2250 {
2251 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2252 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2253 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2254 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2255 ? (char_u *)"" : term->tl_cursor_color);
2256 list_append_dict(l, d);
2257 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002258}
2259
2260/*
2261 * "term_getjob(buf)" function
2262 */
2263 void
2264f_term_getjob(typval_T *argvars, typval_T *rettv)
2265{
2266 buf_T *buf = term_get_buf(argvars);
2267
2268 rettv->v_type = VAR_JOB;
2269 rettv->vval.v_job = NULL;
2270 if (buf == NULL)
2271 return;
2272
2273 rettv->vval.v_job = buf->b_term->tl_job;
2274 if (rettv->vval.v_job != NULL)
2275 ++rettv->vval.v_job->jv_refcount;
2276}
2277
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002278 static int
2279get_row_number(typval_T *tv, term_T *term)
2280{
2281 if (tv->v_type == VAR_STRING
2282 && tv->vval.v_string != NULL
2283 && STRCMP(tv->vval.v_string, ".") == 0)
2284 return term->tl_cursor_pos.row;
2285 return (int)get_tv_number(tv) - 1;
2286}
2287
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002288/*
2289 * "term_getline(buf, row)" function
2290 */
2291 void
2292f_term_getline(typval_T *argvars, typval_T *rettv)
2293{
2294 buf_T *buf = term_get_buf(argvars);
2295 term_T *term;
2296 int row;
2297
2298 rettv->v_type = VAR_STRING;
2299 if (buf == NULL)
2300 return;
2301 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002302 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002303
2304 if (term->tl_vterm == NULL)
2305 {
2306 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2307
2308 /* vterm is finished, get the text from the buffer */
2309 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2310 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2311 }
2312 else
2313 {
2314 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2315 VTermRect rect;
2316 int len;
2317 char_u *p;
2318
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002319 if (row < 0 || row >= term->tl_rows)
2320 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002321 len = term->tl_cols * MB_MAXBYTES + 1;
2322 p = alloc(len);
2323 if (p == NULL)
2324 return;
2325 rettv->vval.v_string = p;
2326
2327 rect.start_col = 0;
2328 rect.end_col = term->tl_cols;
2329 rect.start_row = row;
2330 rect.end_row = row + 1;
2331 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2332 }
2333}
2334
2335/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002336 * "term_getscrolled(buf)" function
2337 */
2338 void
2339f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2340{
2341 buf_T *buf = term_get_buf(argvars);
2342
2343 if (buf == NULL)
2344 return;
2345 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2346}
2347
2348/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002349 * "term_getsize(buf)" function
2350 */
2351 void
2352f_term_getsize(typval_T *argvars, typval_T *rettv)
2353{
2354 buf_T *buf = term_get_buf(argvars);
2355 list_T *l;
2356
2357 if (rettv_list_alloc(rettv) == FAIL)
2358 return;
2359 if (buf == NULL)
2360 return;
2361
2362 l = rettv->vval.v_list;
2363 list_append_number(l, buf->b_term->tl_rows);
2364 list_append_number(l, buf->b_term->tl_cols);
2365}
2366
2367/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002368 * "term_getstatus(buf)" function
2369 */
2370 void
2371f_term_getstatus(typval_T *argvars, typval_T *rettv)
2372{
2373 buf_T *buf = term_get_buf(argvars);
2374 term_T *term;
2375 char_u val[100];
2376
2377 rettv->v_type = VAR_STRING;
2378 if (buf == NULL)
2379 return;
2380 term = buf->b_term;
2381
2382 if (term_job_running(term))
2383 STRCPY(val, "running");
2384 else
2385 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002386 if (term->tl_normal_mode)
2387 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002388 rettv->vval.v_string = vim_strsave(val);
2389}
2390
2391/*
2392 * "term_gettitle(buf)" function
2393 */
2394 void
2395f_term_gettitle(typval_T *argvars, typval_T *rettv)
2396{
2397 buf_T *buf = term_get_buf(argvars);
2398
2399 rettv->v_type = VAR_STRING;
2400 if (buf == NULL)
2401 return;
2402
2403 if (buf->b_term->tl_title != NULL)
2404 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2405}
2406
2407/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002408 * "term_gettty(buf)" function
2409 */
2410 void
2411f_term_gettty(typval_T *argvars, typval_T *rettv)
2412{
2413 buf_T *buf = term_get_buf(argvars);
2414 char_u *p;
2415
2416 rettv->v_type = VAR_STRING;
2417 if (buf == NULL)
2418 return;
2419 if (buf->b_term->tl_job != NULL)
2420 p = buf->b_term->tl_job->jv_tty_name;
2421 else
2422 p = buf->b_term->tl_tty_name;
2423 if (p != NULL)
2424 rettv->vval.v_string = vim_strsave(p);
2425}
2426
2427/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002428 * "term_list()" function
2429 */
2430 void
2431f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2432{
2433 term_T *tp;
2434 list_T *l;
2435
2436 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2437 return;
2438
2439 l = rettv->vval.v_list;
2440 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2441 if (tp != NULL && tp->tl_buffer != NULL)
2442 if (list_append_number(l,
2443 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2444 return;
2445}
2446
2447/*
2448 * "term_scrape(buf, row)" function
2449 */
2450 void
2451f_term_scrape(typval_T *argvars, typval_T *rettv)
2452{
2453 buf_T *buf = term_get_buf(argvars);
2454 VTermScreen *screen = NULL;
2455 VTermPos pos;
2456 list_T *l;
2457 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002458 char_u *p;
2459 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002460
2461 if (rettv_list_alloc(rettv) == FAIL)
2462 return;
2463 if (buf == NULL)
2464 return;
2465 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002466
2467 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002468 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002469
2470 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002471 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002472 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002473 p = NULL;
2474 line = NULL;
2475 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002476 else
2477 {
2478 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2479
2480 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2481 return;
2482 p = ml_get_buf(buf, lnum + 1, FALSE);
2483 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2484 }
2485
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002486 for (pos.col = 0; pos.col < term->tl_cols; )
2487 {
2488 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002489 int width;
2490 VTermScreenCellAttrs attrs;
2491 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002492 char_u rgb[8];
2493 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2494 int off = 0;
2495 int i;
2496
2497 if (screen == NULL)
2498 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002499 cellattr_T *cellattr;
2500 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002501
2502 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002503 if (pos.col >= line->sb_cols)
2504 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002505 cellattr = line->sb_cells + pos.col;
2506 width = cellattr->width;
2507 attrs = cellattr->attrs;
2508 fg = cellattr->fg;
2509 bg = cellattr->bg;
2510 len = MB_PTR2LEN(p);
2511 mch_memmove(mbs, p, len);
2512 mbs[len] = NUL;
2513 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002514 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002515 else
2516 {
2517 VTermScreenCell cell;
2518 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2519 break;
2520 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2521 {
2522 if (cell.chars[i] == 0)
2523 break;
2524 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2525 }
2526 mbs[off] = NUL;
2527 width = cell.width;
2528 attrs = cell.attrs;
2529 fg = cell.fg;
2530 bg = cell.bg;
2531 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002532 dcell = dict_alloc();
2533 list_append_dict(l, dcell);
2534
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002535 dict_add_nr_str(dcell, "chars", 0, mbs);
2536
2537 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002538 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002539 dict_add_nr_str(dcell, "fg", 0, rgb);
2540 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002541 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002542 dict_add_nr_str(dcell, "bg", 0, rgb);
2543
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002544 dict_add_nr_str(dcell, "attr",
2545 cell2attr(attrs, fg, bg), NULL);
2546 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002547
2548 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002549 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002550 ++pos.col;
2551 }
2552}
2553
2554/*
2555 * "term_sendkeys(buf, keys)" function
2556 */
2557 void
2558f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2559{
2560 buf_T *buf = term_get_buf(argvars);
2561 char_u *msg;
2562 term_T *term;
2563
2564 rettv->v_type = VAR_UNKNOWN;
2565 if (buf == NULL)
2566 return;
2567
2568 msg = get_tv_string_chk(&argvars[1]);
2569 if (msg == NULL)
2570 return;
2571 term = buf->b_term;
2572 if (term->tl_vterm == NULL)
2573 return;
2574
2575 while (*msg != NUL)
2576 {
2577 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2578 msg += MB_PTR2LEN(msg);
2579 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002580}
2581
2582/*
2583 * "term_start(command, options)" function
2584 */
2585 void
2586f_term_start(typval_T *argvars, typval_T *rettv)
2587{
2588 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002589 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002590
2591 if (cmd == NULL)
2592 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002593 init_job_options(&opt);
2594 /* TODO: allow more job options */
2595 if (argvars[1].v_type != VAR_UNKNOWN
2596 && get_job_options(&argvars[1], &opt,
2597 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002598 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002599 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002600 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002601 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002602 return;
2603
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002604 if (opt.jo_vertical)
2605 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002606 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002607
2608 if (curbuf->b_term != NULL)
2609 rettv->vval.v_number = curbuf->b_fnum;
2610}
2611
2612/*
2613 * "term_wait" function
2614 */
2615 void
2616f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2617{
2618 buf_T *buf = term_get_buf(argvars);
2619
2620 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002621 {
2622 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002623 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002624 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002625 if (buf->b_term->tl_job == NULL)
2626 {
2627 ch_log(NULL, "term_wait(): no job to wait for");
2628 return;
2629 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002630
2631 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002632 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002633 {
2634 /* The job is dead, keep reading channel I/O until the channel is
2635 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002636 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002637 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2638 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002639 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002640 parse_queued_messages();
2641 ui_delay(10L, FALSE);
2642 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002643 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002644 parse_queued_messages();
2645 }
2646 else
2647 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002648 long wait = 10L;
2649
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002650 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002651 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002652
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002653 /* Wait for some time for any channel I/O. */
2654 if (argvars[1].v_type != VAR_UNKNOWN)
2655 wait = get_tv_number(&argvars[1]);
2656 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002657 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002658
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002659 /* Flushing messages on channels is hopefully sufficient.
2660 * TODO: is there a better way? */
2661 parse_queued_messages();
2662 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002663}
2664
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002665# ifdef WIN3264
2666
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002667/**************************************
2668 * 2. MS-Windows implementation.
2669 */
2670
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002671#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2672#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2673
Bram Moolenaar8a773062017-07-24 22:29:21 +02002674void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002675void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002676void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002677BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2678void (*winpty_config_set_initial_size)(void*, int, int);
2679LPCWSTR (*winpty_conin_name)(void*);
2680LPCWSTR (*winpty_conout_name)(void*);
2681LPCWSTR (*winpty_conerr_name)(void*);
2682void (*winpty_free)(void*);
2683void (*winpty_config_free)(void*);
2684void (*winpty_spawn_config_free)(void*);
2685void (*winpty_error_free)(void*);
2686LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002687BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002688HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002689
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002690#define WINPTY_DLL "winpty.dll"
2691
2692static HINSTANCE hWinPtyDLL = NULL;
2693
2694 int
2695dyn_winpty_init(void)
2696{
2697 int i;
2698 static struct
2699 {
2700 char *name;
2701 FARPROC *ptr;
2702 } winpty_entry[] =
2703 {
2704 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2705 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2706 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2707 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2708 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2709 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2710 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2711 {"winpty_free", (FARPROC*)&winpty_free},
2712 {"winpty_open", (FARPROC*)&winpty_open},
2713 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2714 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2715 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2716 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002717 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002718 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002719 {NULL, NULL}
2720 };
2721
2722 /* No need to initialize twice. */
2723 if (hWinPtyDLL)
2724 return 1;
2725 /* Load winpty.dll */
2726 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2727 if (!hWinPtyDLL)
2728 {
2729 EMSG2(_(e_loadlib), WINPTY_DLL);
2730 return 0;
2731 }
2732 for (i = 0; winpty_entry[i].name != NULL
2733 && winpty_entry[i].ptr != NULL; ++i)
2734 {
2735 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2736 winpty_entry[i].name)) == NULL)
2737 {
2738 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2739 return 0;
2740 }
2741 }
2742
2743 return 1;
2744}
2745
2746/*
2747 * Create a new terminal of "rows" by "cols" cells.
2748 * Store a reference in "term".
2749 * Return OK or FAIL.
2750 */
2751 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002752term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002753{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002754 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002755 channel_T *channel = NULL;
2756 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002757 DWORD error;
2758 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2759 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002760 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002761 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002762
2763 if (!dyn_winpty_init())
2764 return FAIL;
2765
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002766 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002767 if (p == NULL)
2768 return FAIL;
2769
2770 job = job_alloc();
2771 if (job == NULL)
2772 goto failed;
2773
2774 channel = add_channel();
2775 if (channel == NULL)
2776 goto failed;
2777
2778 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2779 if (term->tl_winpty_config == NULL)
2780 goto failed;
2781
2782 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2783 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2784 if (term->tl_winpty == NULL)
2785 goto failed;
2786
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002787 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002788 spawn_config = winpty_spawn_config_new(
2789 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2790 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2791 NULL,
2792 p,
2793 NULL,
2794 NULL,
2795 &winpty_err);
2796 if (spawn_config == NULL)
2797 goto failed;
2798
2799 channel = add_channel();
2800 if (channel == NULL)
2801 goto failed;
2802
2803 job = job_alloc();
2804 if (job == NULL)
2805 goto failed;
2806
2807 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2808 &child_thread_handle, &error, &winpty_err))
2809 goto failed;
2810
2811 channel_set_pipes(channel,
2812 (sock_T) CreateFileW(
2813 winpty_conin_name(term->tl_winpty),
2814 GENERIC_WRITE, 0, NULL,
2815 OPEN_EXISTING, 0, NULL),
2816 (sock_T) CreateFileW(
2817 winpty_conout_name(term->tl_winpty),
2818 GENERIC_READ, 0, NULL,
2819 OPEN_EXISTING, 0, NULL),
2820 (sock_T) CreateFileW(
2821 winpty_conerr_name(term->tl_winpty),
2822 GENERIC_READ, 0, NULL,
2823 OPEN_EXISTING, 0, NULL));
2824
2825 jo = CreateJobObject(NULL, NULL);
2826 if (jo == NULL)
2827 goto failed;
2828
2829 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002830 {
2831 /* Failed, switch the way to terminate process with TerminateProcess. */
2832 CloseHandle(jo);
2833 jo = NULL;
2834 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002835
2836 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002837 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002838
2839 create_vterm(term, rows, cols);
2840
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002841 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002842 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002843
2844 job->jv_channel = channel;
2845 job->jv_proc_info.hProcess = child_process_handle;
2846 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2847 job->jv_job_object = jo;
2848 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002849 sprintf(buf, "winpty://%lu",
2850 GetProcessId(winpty_agent_process(term->tl_winpty)));
2851 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002852 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002853 term->tl_job = job;
2854
2855 return OK;
2856
2857failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002858 if (spawn_config != NULL)
2859 winpty_spawn_config_free(spawn_config);
2860 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002861 if (channel != NULL)
2862 channel_clear(channel);
2863 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002864 {
2865 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002866 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002867 }
2868 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002869 if (jo != NULL)
2870 CloseHandle(jo);
2871 if (term->tl_winpty != NULL)
2872 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002873 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002874 if (term->tl_winpty_config != NULL)
2875 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002876 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002877 if (winpty_err != NULL)
2878 {
2879 char_u *msg = utf16_to_enc(
2880 (short_u *)winpty_error_msg(winpty_err), NULL);
2881
2882 EMSG(msg);
2883 winpty_error_free(winpty_err);
2884 }
2885 return FAIL;
2886}
2887
2888/*
2889 * Free the terminal emulator part of "term".
2890 */
2891 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002892term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002893{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002894 if (term->tl_winpty != NULL)
2895 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002896 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002897 if (term->tl_winpty_config != NULL)
2898 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002899 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002900 if (term->tl_vterm != NULL)
2901 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002902 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002903}
2904
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002905/*
2906 * Request size to terminal.
2907 */
2908 static void
2909term_report_winsize(term_T *term, int rows, int cols)
2910{
2911 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2912}
2913
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002914# else
2915
2916/**************************************
2917 * 3. Unix-like implementation.
2918 */
2919
2920/*
2921 * Create a new terminal of "rows" by "cols" cells.
2922 * Start job for "cmd".
2923 * Store the pointers in "term".
2924 * Return OK or FAIL.
2925 */
2926 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002927term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002928{
2929 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002930
2931 create_vterm(term, rows, cols);
2932
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002933 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002934 argvars[0].v_type = VAR_STRING;
2935 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002936
2937 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002938 if (term->tl_job != NULL)
2939 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002940
Bram Moolenaar61a66052017-07-22 18:39:00 +02002941 return term->tl_job != NULL
2942 && term->tl_job->jv_channel != NULL
2943 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002944}
2945
2946/*
2947 * Free the terminal emulator part of "term".
2948 */
2949 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002950term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002951{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002952 if (term->tl_vterm != NULL)
2953 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002954 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002955}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002956
2957/*
2958 * Request size to terminal.
2959 */
2960 static void
2961term_report_winsize(term_T *term, int rows, int cols)
2962{
2963 /* Use an ioctl() to report the new window size to the job. */
2964 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2965 {
2966 int fd = -1;
2967 int part;
2968
2969 for (part = PART_OUT; part < PART_COUNT; ++part)
2970 {
2971 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2972 if (isatty(fd))
2973 break;
2974 }
2975 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002976 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002977 }
2978}
2979
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002980# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002981
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002982#endif /* FEAT_TERMINAL */