blob: 317615cfd870ba48ea3db7cc3ec43704065686a8 [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 Moolenaard8dc1792017-08-03 11:55:21 +020050 * - set 'filetype' to "terminal"?
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020051 * - use win_del_lines() to make scroll-up efficient.
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020052 * - Make StatusLineTerm adjust UserN highlighting like StatusLineNC does, see
53 * use of hightlight_stlnc[].
Bram Moolenaar94053a52017-08-01 21:44:33 +020054 * - implement term_setsize()
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020055 * - add test for giving error for invalid 'termsize' value.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020056 * - support minimal size when 'termsize' is "rows*cols".
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020057 * - support minimal size when 'termsize' is empty?
Bram Moolenaar5a1feb82017-07-22 18:04:08 +020058 * - implement "term" for job_start(): more job options when starting a
Bram Moolenaar78712a72017-08-05 14:50:12 +020059 * terminal. Allow:
60 * "in_io", "in_top", "in_bot", "in_name", "in_buf"
61 "out_io", "out_name", "out_buf", "out_modifiable", "out_msg"
62 "err_io", "err_name", "err_buf", "err_modifiable", "err_msg"
63 * Check that something is connected to the terminal.
64 * Test: "cat" reading from a file or buffer
65 * "ls" writing stdout to a file or buffer
66 * shell writing stderr to a file or buffer
Bram Moolenaar7c9aec42017-08-03 13:51:25 +020067 * - support ":term NONE" to open a terminal with a pty but not running a job
68 * in it. The pty can be passed to gdb to run the executable in.
Bram Moolenaar423802d2017-07-30 16:52:24 +020069 * - if the job in the terminal does not support the mouse, we can use the
70 * mouse in the Terminal window for copy/paste.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020071 * - when 'encoding' is not utf-8, or the job is using another encoding, setup
72 * conversions.
Bram Moolenaarc9456ce2017-07-30 21:46:04 +020073 * - update ":help function-list" for terminal functions.
Bram Moolenaardbe948d2017-07-23 22:50:51 +020074 * - In the GUI use a terminal emulator for :!cmd.
Bram Moolenaar12d853f2017-08-01 18:04:04 +020075 * - Copy text in the vterm to the Vim buffer once in a while, so that
76 * completion works.
Bram Moolenaare4f25e42017-07-07 11:54:15 +020077 */
78
79#include "vim.h"
80
Bram Moolenaarc6df10e2017-07-29 20:15:08 +020081#if defined(FEAT_TERMINAL) || defined(PROTO)
Bram Moolenaare4f25e42017-07-07 11:54:15 +020082
Bram Moolenaard5310982017-08-05 15:16:32 +020083#ifndef MIN
84# define MIN(x,y) ((x) < (y) ? (x) : (y))
85#endif
86#ifndef MAX
87# define MAX(x,y) ((x) > (y) ? (x) : (y))
Bram Moolenaar8c0095c2017-07-18 22:53:21 +020088#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +020089
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +020090#include "libvterm/include/vterm.h"
91
Bram Moolenaar33a43be2017-08-06 21:36:22 +020092/* This is VTermScreenCell without the characters, thus much smaller. */
93typedef struct {
94 VTermScreenCellAttrs attrs;
95 char width;
96 VTermColor fg, bg;
97} cellattr_T;
98
Bram Moolenaard85f2712017-07-28 21:51:57 +020099typedef struct sb_line_S {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200100 int sb_cols; /* can differ per line */
101 cellattr_T *sb_cells; /* allocated */
Bram Moolenaard85f2712017-07-28 21:51:57 +0200102} sb_line_T;
103
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200104/* typedef term_T in structs.h */
105struct terminal_S {
106 term_T *tl_next;
107
Bram Moolenaar423802d2017-07-30 16:52:24 +0200108 VTerm *tl_vterm;
109 job_T *tl_job;
110 buf_T *tl_buffer;
111
Bram Moolenaar7c9aec42017-08-03 13:51:25 +0200112 /* used when tl_job is NULL and only a pty was created */
113 int tl_tty_fd;
114 char_u *tl_tty_name;
115
Bram Moolenaar6d819742017-08-06 14:57:49 +0200116 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
Bram Moolenaar423802d2017-07-30 16:52:24 +0200117 int tl_channel_closed;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200118 int tl_finish; /* 'c' for ++close, 'o' for ++open */
Bram Moolenaar37c45832017-08-12 16:01:04 +0200119 char_u *tl_opencmd;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200120
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200121#ifdef WIN3264
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200122 void *tl_winpty_config;
123 void *tl_winpty;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200124#endif
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200125
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200126 /* last known vterm size */
127 int tl_rows;
128 int tl_cols;
129 /* vterm size does not follow window size */
130 int tl_rows_fixed;
131 int tl_cols_fixed;
132
Bram Moolenaar21554412017-07-24 21:44:43 +0200133 char_u *tl_title; /* NULL or allocated */
134 char_u *tl_status_text; /* NULL or allocated */
135
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200136 /* Range of screen rows to update. Zero based. */
137 int tl_dirty_row_start; /* -1 if nothing dirty */
138 int tl_dirty_row_end; /* row below last one to update */
139
Bram Moolenaard85f2712017-07-28 21:51:57 +0200140 garray_T tl_scrollback;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200141 int tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200142
Bram Moolenaar22aad2f2017-07-30 18:19:46 +0200143 VTermPos tl_cursor_pos;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200144 int tl_cursor_visible;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200145 int tl_cursor_blink;
146 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
147 char_u *tl_cursor_color; /* NULL or allocated */
Bram Moolenaare41e3b42017-08-11 16:24:50 +0200148
149 int tl_using_altscreen;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200150};
151
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200152#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
153#define TMODE_LOOP 2 /* CTRL-W N used */
154
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200155/*
156 * List of all active terminals.
157 */
158static term_T *first_term = NULL;
159
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200160/* Terminal active in terminal_loop(). */
161static term_T *in_terminal_loop = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200162
163#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
164#define KEY_BUF_LEN 200
165
166/*
167 * Functions with separate implementation for MS-Windows and Unix-like systems.
168 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200169static 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 +0200170static void term_report_winsize(term_T *term, int rows, int cols);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200171static void term_free_vterm(term_T *term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200172
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200173/**************************************
174 * 1. Generic code for all systems.
175 */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200176
177/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200178 * Determine the terminal size from 'termsize' and the current window.
179 * Assumes term->tl_rows and term->tl_cols are zero.
180 */
181 static void
182set_term_and_win_size(term_T *term)
183{
184 if (*curwin->w_p_tms != NUL)
185 {
186 char_u *p = vim_strchr(curwin->w_p_tms, 'x') + 1;
187
188 term->tl_rows = atoi((char *)curwin->w_p_tms);
189 term->tl_cols = atoi((char *)p);
190 }
191 if (term->tl_rows == 0)
192 term->tl_rows = curwin->w_height;
193 else
194 {
195 win_setheight_win(term->tl_rows, curwin);
196 term->tl_rows_fixed = TRUE;
197 }
198 if (term->tl_cols == 0)
199 term->tl_cols = curwin->w_width;
200 else
201 {
202 win_setwidth_win(term->tl_cols, curwin);
203 term->tl_cols_fixed = TRUE;
204 }
205}
206
207/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200208 * Initialize job options for a terminal job.
209 * Caller may overrule some of them.
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200210 */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200211 static void
212init_job_options(jobopt_T *opt)
213{
214 clear_job_options(opt);
215
216 opt->jo_mode = MODE_RAW;
217 opt->jo_out_mode = MODE_RAW;
218 opt->jo_err_mode = MODE_RAW;
219 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
220
221 opt->jo_io[PART_OUT] = JIO_BUFFER;
222 opt->jo_io[PART_ERR] = JIO_BUFFER;
223 opt->jo_set |= JO_OUT_IO + JO_ERR_IO;
224
225 opt->jo_modifiable[PART_OUT] = 0;
226 opt->jo_modifiable[PART_ERR] = 0;
227 opt->jo_set |= JO_OUT_MODIFIABLE + JO_ERR_MODIFIABLE;
228
229 opt->jo_set |= JO_OUT_BUF + JO_ERR_BUF;
230}
231
232/*
233 * Set job options mandatory for a terminal job.
234 */
235 static void
236setup_job_options(jobopt_T *opt, int rows, int cols)
237{
238 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
239 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
240 opt->jo_pty = TRUE;
Bram Moolenaar08d384f2017-08-11 21:51:23 +0200241 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
242 opt->jo_term_rows = rows;
243 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
244 opt->jo_term_cols = cols;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200245}
246
247 static void
Bram Moolenaarda43b612017-08-11 22:27:50 +0200248term_start(char_u *cmd, jobopt_T *opt, int forceit)
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200249{
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200250 exarg_T split_ea;
251 win_T *old_curwin = curwin;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200252 term_T *term;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200253 buf_T *old_curbuf = NULL;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200254
255 if (check_restricted() || check_secure())
256 return;
257
258 term = (term_T *)alloc_clear(sizeof(term_T));
259 if (term == NULL)
260 return;
261 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200262 term->tl_cursor_visible = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200263 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200264 term->tl_finish = opt->jo_term_finish;
Bram Moolenaard85f2712017-07-28 21:51:57 +0200265 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200266
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200267 vim_memset(&split_ea, 0, sizeof(split_ea));
Bram Moolenaarda43b612017-08-11 22:27:50 +0200268 if (opt->jo_curwin)
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200269 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200270 /* Create a new buffer in the current window. */
271 if (!can_abandon(curbuf, forceit))
272 {
273 EMSG(_(e_nowrtmsg));
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200274 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200275 return;
276 }
277 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
278 ECMD_HIDE + (forceit ? ECMD_FORCEIT : 0), curwin) == FAIL)
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200279 {
280 vim_free(term);
Bram Moolenaarda43b612017-08-11 22:27:50 +0200281 return;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200282 }
283 }
284 else if (opt->jo_hidden)
285 {
286 buf_T *buf;
287
288 /* Create a new buffer without a window. Make it the current buffer for
289 * a moment to be able to do the initialisations. */
290 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
291 BLN_NEW | BLN_LISTED);
292 if (buf == NULL || ml_open(buf) == FAIL)
293 {
294 vim_free(term);
295 return;
296 }
297 old_curbuf = curbuf;
298 --curbuf->b_nwindows;
299 curbuf = buf;
300 curwin->w_buffer = buf;
301 ++curbuf->b_nwindows;
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200302 }
Bram Moolenaarda43b612017-08-11 22:27:50 +0200303 else
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200304 {
Bram Moolenaarda43b612017-08-11 22:27:50 +0200305 /* Open a new window or tab. */
306 split_ea.cmdidx = CMD_new;
307 split_ea.cmd = (char_u *)"new";
308 split_ea.arg = (char_u *)"";
309 if (opt->jo_term_rows > 0 && !(cmdmod.split & WSP_VERT))
310 {
311 split_ea.line2 = opt->jo_term_rows;
312 split_ea.addr_count = 1;
313 }
314 if (opt->jo_term_cols > 0 && (cmdmod.split & WSP_VERT))
315 {
316 split_ea.line2 = opt->jo_term_cols;
317 split_ea.addr_count = 1;
318 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200319
Bram Moolenaarda43b612017-08-11 22:27:50 +0200320 ex_splitview(&split_ea);
321 if (curwin == old_curwin)
322 {
323 /* split failed */
324 vim_free(term);
325 return;
326 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200327 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200328 term->tl_buffer = curbuf;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200329 curbuf->b_term = term;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200330
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200331 if (!opt->jo_hidden)
332 {
333 /* only one size was taken care of with :new, do the other one */
334 if (opt->jo_term_rows > 0 && (cmdmod.split & WSP_VERT))
335 win_setheight(opt->jo_term_rows);
336 if (opt->jo_term_cols > 0 && !(cmdmod.split & WSP_VERT))
337 win_setwidth(opt->jo_term_cols);
338 }
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200339
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200340 /* Link the new terminal in the list of active terminals. */
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200341 term->tl_next = first_term;
342 first_term = term;
343
Bram Moolenaar293424c2017-07-26 23:11:01 +0200344 if (cmd == NULL || *cmd == NUL)
345 cmd = p_sh;
346
Bram Moolenaar78712a72017-08-05 14:50:12 +0200347 if (opt->jo_term_name != NULL)
348 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
349 else
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200350 {
351 int i;
352 size_t len = STRLEN(cmd) + 10;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200353 char_u *p = alloc((int)len);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200354
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200355 for (i = 0; p != NULL; ++i)
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200356 {
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200357 /* Prepend a ! to the command name to avoid the buffer name equals
358 * the executable, otherwise ":w!" would overwrite it. */
359 if (i == 0)
360 vim_snprintf((char *)p, len, "!%s", cmd);
361 else
362 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200363 if (buflist_findname(p) == NULL)
364 {
365 curbuf->b_ffname = p;
366 break;
367 }
368 }
369 }
370 curbuf->b_fname = curbuf->b_ffname;
371
Bram Moolenaar37c45832017-08-12 16:01:04 +0200372 if (opt->jo_term_opencmd != NULL)
373 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
374
Bram Moolenaareb44a682017-08-03 22:44:55 +0200375 set_string_option_direct((char_u *)"buftype", -1,
376 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
377
Bram Moolenaar20e6cd02017-08-01 20:25:22 +0200378 /* Mark the buffer as not modifiable. It can only be made modifiable after
379 * the job finished. */
Bram Moolenaar1f2903c2017-07-23 19:51:01 +0200380 curbuf->b_p_ma = FALSE;
Bram Moolenaareb44a682017-08-03 22:44:55 +0200381
382 /* Set 'bufhidden' to "hide": allow closing the window. */
383 set_string_option_direct((char_u *)"bufhidden", -1,
384 (char_u *)"hide", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200385
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200386 set_term_and_win_size(term);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200387 setup_job_options(opt, term->tl_rows, term->tl_cols);
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200388
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200389 /* System dependent: setup the vterm and start the job in it. */
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200390 if (term_and_job_init(term, term->tl_rows, term->tl_cols, cmd, opt) == OK)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200391 {
Bram Moolenaar292d5692017-08-08 21:52:22 +0200392 /* Get and remember the size we ended up with. Update the pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200393 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
Bram Moolenaar292d5692017-08-08 21:52:22 +0200394 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200395
396 if (old_curbuf != NULL)
397 {
398 --curbuf->b_nwindows;
399 curbuf = old_curbuf;
400 curwin->w_buffer = curbuf;
401 ++curbuf->b_nwindows;
402 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200403 }
404 else
405 {
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200406 buf_T *buf = curbuf;
407
Bram Moolenaard85f2712017-07-28 21:51:57 +0200408 free_terminal(curbuf);
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200409 if (old_curbuf != NULL)
410 {
411 --curbuf->b_nwindows;
412 curbuf = old_curbuf;
413 curwin->w_buffer = curbuf;
414 ++curbuf->b_nwindows;
415 }
Bram Moolenaar61a66052017-07-22 18:39:00 +0200416
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200417 /* Wiping out the buffer will also close the window and call
418 * free_terminal(). */
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200419 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200420 }
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200421}
422
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200423/*
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200424 * ":terminal": open a terminal window and execute a job in it.
425 */
426 void
427ex_terminal(exarg_T *eap)
428{
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200429 jobopt_T opt;
430 char_u *cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200431
432 init_job_options(&opt);
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200433
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200434 cmd = eap->arg;
435 while (*cmd && *cmd == '+' && *(cmd + 1) == '+')
436 {
437 char_u *p;
438
439 cmd += 2;
440 p = skiptowhite(cmd);
441 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
442 opt.jo_term_finish = 'c';
443 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
444 opt.jo_term_finish = 'o';
Bram Moolenaarda43b612017-08-11 22:27:50 +0200445 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
446 opt.jo_curwin = 1;
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200447 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
448 opt.jo_hidden = 1;
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200449 else
450 {
451 if (*p)
452 *p = NUL;
453 EMSG2(_("E181: Invalid attribute: %s"), cmd);
454 return;
455 }
456 cmd = skipwhite(p);
457 }
458
Bram Moolenaarcfcc0222017-08-05 17:13:48 +0200459 if (eap->addr_count == 2)
460 {
461 opt.jo_term_rows = eap->line1;
462 opt.jo_term_cols = eap->line2;
463 }
464 else if (eap->addr_count == 1)
465 {
466 if (cmdmod.split & WSP_VERT)
467 opt.jo_term_cols = eap->line2;
468 else
469 opt.jo_term_rows = eap->line2;
470 }
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200471
Bram Moolenaarda43b612017-08-11 22:27:50 +0200472 term_start(cmd, &opt, eap->forceit);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +0200473}
474
475/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200476 * Free the scrollback buffer for "term".
477 */
478 static void
479free_scrollback(term_T *term)
480{
481 int i;
482
483 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
484 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
485 ga_clear(&term->tl_scrollback);
486}
487
488/*
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200489 * Free a terminal and everything it refers to.
490 * Kills the job if there is one.
491 * Called when wiping out a buffer.
492 */
493 void
Bram Moolenaard85f2712017-07-28 21:51:57 +0200494free_terminal(buf_T *buf)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200495{
Bram Moolenaard85f2712017-07-28 21:51:57 +0200496 term_T *term = buf->b_term;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200497 term_T *tp;
498
499 if (term == NULL)
500 return;
501 if (first_term == term)
502 first_term = term->tl_next;
503 else
504 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
505 if (tp->tl_next == term)
506 {
507 tp->tl_next = term->tl_next;
508 break;
509 }
510
511 if (term->tl_job != NULL)
512 {
Bram Moolenaar61a66052017-07-22 18:39:00 +0200513 if (term->tl_job->jv_status != JOB_ENDED
514 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200515 job_stop(term->tl_job, NULL, "kill");
516 job_unref(term->tl_job);
517 }
518
Bram Moolenaar63ecdda2017-07-28 22:29:35 +0200519 free_scrollback(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200520
521 term_free_vterm(term);
Bram Moolenaar21554412017-07-24 21:44:43 +0200522 vim_free(term->tl_title);
523 vim_free(term->tl_status_text);
Bram Moolenaar37c45832017-08-12 16:01:04 +0200524 vim_free(term->tl_opencmd);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +0200525 vim_free(term->tl_cursor_color);
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200526 vim_free(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200527 buf->b_term = NULL;
Bram Moolenaar679653e2017-08-13 14:13:19 +0200528 if (in_terminal_loop == term)
529 in_terminal_loop = NULL;
Bram Moolenaar96ca27a2017-07-17 23:20:24 +0200530}
531
532/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200533 * Write job output "msg[len]" to the vterm.
534 */
535 static void
536term_write_job_output(term_T *term, char_u *msg, size_t len)
537{
538 VTerm *vterm = term->tl_vterm;
539 char_u *p;
540 size_t done;
541 size_t len_now;
542
543 for (done = 0; done < len; done += len_now)
544 {
545 for (p = msg + done; p < msg + len; )
546 {
547 if (*p == NL)
548 break;
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200549 p += utf_ptr2len_len(p, (int)(len - (p - msg)));
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200550 }
551 len_now = p - msg - done;
552 vterm_input_write(vterm, (char *)msg + done, len_now);
553 if (p < msg + len && *p == NL)
554 {
555 /* Convert NL to CR-NL, that appears to work best. */
556 vterm_input_write(vterm, "\r\n", 2);
557 ++len_now;
558 }
559 }
560
561 /* this invokes the damage callbacks */
562 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
563}
564
Bram Moolenaar1c844932017-07-24 23:36:41 +0200565 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200566update_cursor(term_T *term, int redraw)
Bram Moolenaar1c844932017-07-24 23:36:41 +0200567{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200568 if (term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200569 return;
Bram Moolenaar1c844932017-07-24 23:36:41 +0200570 setcursor();
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200571 if (redraw && term->tl_buffer == curbuf)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200572 {
Bram Moolenaar4cc93dc2017-07-26 21:49:37 +0200573 if (term->tl_cursor_visible)
574 cursor_on();
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200575 out_flush();
Bram Moolenaar1c844932017-07-24 23:36:41 +0200576#ifdef FEAT_GUI
Bram Moolenaar12d93ee2017-07-30 19:02:02 +0200577 if (gui.in_use)
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200578 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar1c844932017-07-24 23:36:41 +0200579#endif
Bram Moolenaarfc716d72017-07-25 23:08:47 +0200580 }
Bram Moolenaar1c844932017-07-24 23:36:41 +0200581}
582
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +0200583/*
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200584 * Invoked when "msg" output from a job was received. Write it to the terminal
585 * of "buffer".
586 */
587 void
588write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
589{
590 size_t len = STRLEN(msg);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200591 term_T *term = buffer->b_term;
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200592
Bram Moolenaard85f2712017-07-28 21:51:57 +0200593 if (term->tl_vterm == NULL)
594 {
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200595 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200596 return;
597 }
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +0200598 ch_log(channel, "writing %d bytes to terminal", (int)len);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200599 term_write_job_output(term, msg, len);
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200600
Bram Moolenaar6d819742017-08-06 14:57:49 +0200601 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +0200602 {
603 /* TODO: only update once in a while. */
604 update_screen(0);
605 update_cursor(term, TRUE);
606 }
Bram Moolenaarcb8bbe92017-07-16 13:48:22 +0200607}
608
609/*
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200610 * Send a mouse position and click to the vterm
611 */
612 static int
613term_send_mouse(VTerm *vterm, int button, int pressed)
614{
615 VTermModifier mod = VTERM_MOD_NONE;
616
617 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
618 mouse_col - W_WINCOL(curwin), mod);
619 vterm_mouse_button(vterm, button, pressed, mod);
620 return TRUE;
621}
622
623/*
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200624 * Convert typed key "c" into bytes to send to the job.
625 * Return the number of bytes in "buf".
626 */
627 static int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200628term_convert_key(term_T *term, int c, char *buf)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200629{
Bram Moolenaarc6df10e2017-07-29 20:15:08 +0200630 VTerm *vterm = term->tl_vterm;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200631 VTermKey key = VTERM_KEY_NONE;
632 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200633 int mouse = FALSE;
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200634
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200635 switch (c)
636 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200637 case CAR: key = VTERM_KEY_ENTER; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200638 case ESC: key = VTERM_KEY_ESCAPE; break;
Bram Moolenaare906ae82017-07-21 21:10:01 +0200639 /* VTERM_KEY_BACKSPACE becomes 0x7f DEL */
640 case K_BS: c = BS; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200641 case K_DEL: key = VTERM_KEY_DEL; break;
642 case K_DOWN: key = VTERM_KEY_DOWN; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200643 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
644 key = VTERM_KEY_DOWN; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200645 case K_END: key = VTERM_KEY_END; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200646 case K_S_END: mod = VTERM_MOD_SHIFT;
647 key = VTERM_KEY_END; break;
648 case K_C_END: mod = VTERM_MOD_CTRL;
649 key = VTERM_KEY_END; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200650 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
651 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
652 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
653 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
654 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
655 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
656 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
657 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
658 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
659 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
660 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
661 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
662 case K_HOME: key = VTERM_KEY_HOME; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200663 case K_S_HOME: mod = VTERM_MOD_SHIFT;
664 key = VTERM_KEY_HOME; break;
665 case K_C_HOME: mod = VTERM_MOD_CTRL;
666 key = VTERM_KEY_HOME; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200667 case K_INS: key = VTERM_KEY_INS; break;
668 case K_K0: key = VTERM_KEY_KP_0; break;
669 case K_K1: key = VTERM_KEY_KP_1; break;
670 case K_K2: key = VTERM_KEY_KP_2; break;
671 case K_K3: key = VTERM_KEY_KP_3; break;
672 case K_K4: key = VTERM_KEY_KP_4; break;
673 case K_K5: key = VTERM_KEY_KP_5; break;
674 case K_K6: key = VTERM_KEY_KP_6; break;
675 case K_K7: key = VTERM_KEY_KP_7; break;
676 case K_K8: key = VTERM_KEY_KP_8; break;
677 case K_K9: key = VTERM_KEY_KP_9; break;
678 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
679 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
680 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
681 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
682 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
683 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
684 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
685 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
686 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
687 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
688 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
689 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
690 case K_LEFT: key = VTERM_KEY_LEFT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200691 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
692 key = VTERM_KEY_LEFT; break;
693 case K_C_LEFT: mod = VTERM_MOD_CTRL;
694 key = VTERM_KEY_LEFT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200695 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
696 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
697 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200698 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
699 key = VTERM_KEY_RIGHT; break;
700 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
701 key = VTERM_KEY_RIGHT; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200702 case K_UP: key = VTERM_KEY_UP; break;
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200703 case K_S_UP: mod = VTERM_MOD_SHIFT;
704 key = VTERM_KEY_UP; break;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200705 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200706
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200707 case K_MOUSEUP: mouse = term_send_mouse(vterm, 5, 1); break;
708 case K_MOUSEDOWN: mouse = term_send_mouse(vterm, 4, 1); break;
709 case K_MOUSELEFT: /* TODO */ return 0;
710 case K_MOUSERIGHT: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200711
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200712 case K_LEFTMOUSE:
713 case K_LEFTMOUSE_NM: mouse = term_send_mouse(vterm, 1, 1); break;
714 case K_LEFTDRAG: mouse = term_send_mouse(vterm, 1, 1); break;
715 case K_LEFTRELEASE:
716 case K_LEFTRELEASE_NM: mouse = term_send_mouse(vterm, 1, 0); break;
717 case K_MIDDLEMOUSE: mouse = term_send_mouse(vterm, 2, 1); break;
718 case K_MIDDLEDRAG: mouse = term_send_mouse(vterm, 2, 1); break;
719 case K_MIDDLERELEASE: mouse = term_send_mouse(vterm, 2, 0); break;
720 case K_RIGHTMOUSE: mouse = term_send_mouse(vterm, 3, 1); break;
721 case K_RIGHTDRAG: mouse = term_send_mouse(vterm, 3, 1); break;
722 case K_RIGHTRELEASE: mouse = term_send_mouse(vterm, 3, 0); break;
723 case K_X1MOUSE: /* TODO */ return 0;
724 case K_X1DRAG: /* TODO */ return 0;
725 case K_X1RELEASE: /* TODO */ return 0;
726 case K_X2MOUSE: /* TODO */ return 0;
727 case K_X2DRAG: /* TODO */ return 0;
728 case K_X2RELEASE: /* TODO */ return 0;
Bram Moolenaare825d8b2017-07-19 23:20:19 +0200729
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200730 case K_IGNORE: return 0;
731 case K_NOP: return 0;
732 case K_UNDO: return 0;
733 case K_HELP: return 0;
734 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
735 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
736 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
737 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
738 case K_SELECT: return 0;
739#ifdef FEAT_GUI
740 case K_VER_SCROLLBAR: return 0;
741 case K_HOR_SCROLLBAR: return 0;
742#endif
743#ifdef FEAT_GUI_TABLINE
744 case K_TABLINE: return 0;
745 case K_TABMENU: return 0;
746#endif
747#ifdef FEAT_NETBEANS_INTG
748 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
749#endif
750#ifdef FEAT_DND
751 case K_DROP: return 0;
752#endif
753#ifdef FEAT_AUTOCMD
754 case K_CURSORHOLD: return 0;
755#endif
756 case K_PS: vterm_keyboard_start_paste(vterm); return 0;
757 case K_PE: vterm_keyboard_end_paste(vterm); return 0;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200758 }
759
760 /*
761 * Convert special keys to vterm keys:
762 * - Write keys to vterm: vterm_keyboard_key()
763 * - Write output to channel.
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200764 * TODO: use mod_mask
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200765 */
766 if (key != VTERM_KEY_NONE)
767 /* Special key, let vterm convert it. */
768 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaar6e1ef282017-07-29 22:23:40 +0200769 else if (!mouse)
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200770 /* Normal character, let vterm convert it. */
771 vterm_keyboard_unichar(vterm, c, mod);
772
773 /* Read back the converted escape sequence. */
Bram Moolenaara1b5b092017-07-26 21:29:34 +0200774 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
Bram Moolenaar8c0095c2017-07-18 22:53:21 +0200775}
Bram Moolenaare4f25e42017-07-07 11:54:15 +0200776
Bram Moolenaar938783d2017-07-16 20:13:26 +0200777/*
Bram Moolenaarb000e322017-07-30 19:38:21 +0200778 * Return TRUE if the job for "term" is still running.
Bram Moolenaard85f2712017-07-28 21:51:57 +0200779 */
Bram Moolenaar94053a52017-08-01 21:44:33 +0200780 int
Bram Moolenaard85f2712017-07-28 21:51:57 +0200781term_job_running(term_T *term)
782{
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200783 /* Also consider the job finished when the channel is closed, to avoid a
784 * race condition when updating the title. */
Bram Moolenaarb4a67212017-08-03 19:22:36 +0200785 return term != NULL
786 && term->tl_job != NULL
Bram Moolenaar1e8340b2017-07-29 15:53:39 +0200787 && term->tl_job->jv_status == JOB_STARTED
788 && channel_is_open(term->tl_job->jv_channel);
Bram Moolenaard85f2712017-07-28 21:51:57 +0200789}
790
791/*
Bram Moolenaar423802d2017-07-30 16:52:24 +0200792 * Add the last line of the scrollback buffer to the buffer in the window.
793 */
794 static void
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200795add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200796{
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200797 buf_T *buf = term->tl_buffer;
798 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
799 linenr_T lnum = buf->b_ml.ml_line_count;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200800
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200801 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200802 if (empty)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200803 {
804 /* Delete the empty line that was in the empty buffer. */
Bram Moolenaarf8d57a52017-08-07 20:38:42 +0200805 curbuf = buf;
806 ml_delete(1, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200807 curbuf = curwin->w_buffer;
808 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200809}
810
811/*
812 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200813 * Called after the job has ended and when switching to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200814 */
815 static void
816move_terminal_to_buffer(term_T *term)
817{
818 win_T *wp;
819 int len;
820 int lines_skipped = 0;
821 VTermPos pos;
822 VTermScreenCell cell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200823 cellattr_T *p;
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200824 VTermScreen *screen;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200825
Bram Moolenaar8e5eece2017-08-04 20:29:53 +0200826 if (term->tl_vterm == NULL)
827 return;
828 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200829 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
830 {
831 len = 0;
832 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
833 if (vterm_screen_get_cell(screen, pos, &cell) != 0
834 && cell.chars[0] != NUL)
835 len = pos.col + 1;
836
837 if (len == 0)
838 ++lines_skipped;
839 else
840 {
841 while (lines_skipped > 0)
842 {
843 /* Line was skipped, add an empty line. */
844 --lines_skipped;
845 if (ga_grow(&term->tl_scrollback, 1) == OK)
846 {
847 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
848 + term->tl_scrollback.ga_len;
849
850 line->sb_cols = 0;
851 line->sb_cells = NULL;
852 ++term->tl_scrollback.ga_len;
853
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200854 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200855 }
856 }
857
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200858 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200859 if (p != NULL && ga_grow(&term->tl_scrollback, 1) == OK)
860 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200861 garray_T ga;
862 int width;
863 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
Bram Moolenaar423802d2017-07-30 16:52:24 +0200864 + term->tl_scrollback.ga_len;
865
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200866 ga_init2(&ga, 1, 100);
867 for (pos.col = 0; pos.col < len; pos.col += width)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200868 {
869 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200870 {
871 width = 1;
872 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
873 if (ga_grow(&ga, 1) == OK)
874 ga.ga_len += mb_char2bytes(' ',
875 (char_u *)ga.ga_data + ga.ga_len);
876 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200877 else
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200878 {
879 width = cell.width;
880
881 p[pos.col].width = cell.width;
882 p[pos.col].attrs = cell.attrs;
883 p[pos.col].fg = cell.fg;
884 p[pos.col].bg = cell.bg;
885
886 if (ga_grow(&ga, MB_MAXBYTES) == OK)
887 {
888 int i;
889 int c;
890
891 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
892 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
893 (char_u *)ga.ga_data + ga.ga_len);
894 }
895 }
Bram Moolenaar423802d2017-07-30 16:52:24 +0200896 }
897 line->sb_cols = len;
898 line->sb_cells = p;
899 ++term->tl_scrollback.ga_len;
900
Bram Moolenaar33a43be2017-08-06 21:36:22 +0200901 if (ga_grow(&ga, 1) == FAIL)
902 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
903 else
904 {
905 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
906 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
907 }
908 ga_clear(&ga);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200909 }
910 else
911 vim_free(p);
912 }
913 }
914
915 FOR_ALL_WINDOWS(wp)
916 {
917 if (wp->w_buffer == term->tl_buffer)
918 {
919 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
920 wp->w_cursor.col = 0;
921 wp->w_valid = 0;
922 redraw_win_later(wp, NOT_VALID);
923 }
924 }
925}
926
927 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200928set_terminal_mode(term_T *term, int normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200929{
Bram Moolenaar6d819742017-08-06 14:57:49 +0200930 term->tl_normal_mode = normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200931 vim_free(term->tl_status_text);
932 term->tl_status_text = NULL;
933 if (term->tl_buffer == curbuf)
934 maketitle();
935}
936
937/*
938 * Called after the job if finished and Terminal mode is not active:
939 * Move the vterm contents into the scrollback buffer and free the vterm.
940 */
941 static void
942cleanup_vterm(term_T *term)
943{
Bram Moolenaar8cad9302017-08-12 14:32:32 +0200944 if (term->tl_finish != 'c')
Bram Moolenaardd693ce2017-08-10 23:15:19 +0200945 move_terminal_to_buffer(term);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200946 term_free_vterm(term);
Bram Moolenaar6d819742017-08-06 14:57:49 +0200947 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +0200948}
949
950/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200951 * Switch from Terminal-Job mode to Terminal-Normal mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200952 * Suspends updating the terminal window.
953 */
954 static void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200955term_enter_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200956{
957 term_T *term = curbuf->b_term;
958
959 /* Append the current terminal contents to the buffer. */
960 move_terminal_to_buffer(term);
961
Bram Moolenaar6d819742017-08-06 14:57:49 +0200962 set_terminal_mode(term, TRUE);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200963
Bram Moolenaar6d819742017-08-06 14:57:49 +0200964 /* Move the window cursor to the position of the cursor in the
965 * terminal. */
966 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
967 + term->tl_cursor_pos.row + 1;
968 check_cursor();
969 coladvance(term->tl_cursor_pos.col);
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200970
Bram Moolenaar6d819742017-08-06 14:57:49 +0200971 /* Display the same lines as in the terminal. */
972 curwin->w_topline = term->tl_scrollback_scrolled + 1;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200973}
974
975/*
976 * Returns TRUE if the current window contains a terminal and we are in
977 * Terminal-Normal mode.
978 */
979 int
Bram Moolenaar6d819742017-08-06 14:57:49 +0200980term_in_normal_mode(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +0200981{
982 term_T *term = curbuf->b_term;
983
Bram Moolenaar6d819742017-08-06 14:57:49 +0200984 return term != NULL && term->tl_normal_mode;
Bram Moolenaar423802d2017-07-30 16:52:24 +0200985}
986
987/*
Bram Moolenaaraaa8a352017-08-05 20:17:00 +0200988 * Switch from Terminal-Normal mode to Terminal-Job mode.
Bram Moolenaar423802d2017-07-30 16:52:24 +0200989 * Restores updating the terminal window.
990 */
991 void
Bram Moolenaar6d819742017-08-06 14:57:49 +0200992term_enter_job_mode()
Bram Moolenaar423802d2017-07-30 16:52:24 +0200993{
994 term_T *term = curbuf->b_term;
995 sb_line_T *line;
996 garray_T *gap;
997
998 /* Remove the terminal contents from the scrollback and the buffer. */
999 gap = &term->tl_scrollback;
1000 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled)
1001 {
1002 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1003 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1004 vim_free(line->sb_cells);
1005 --gap->ga_len;
1006 if (gap->ga_len == 0)
1007 break;
1008 }
1009 check_cursor();
1010
Bram Moolenaar6d819742017-08-06 14:57:49 +02001011 set_terminal_mode(term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001012
1013 if (term->tl_channel_closed)
1014 cleanup_vterm(term);
1015 redraw_buf_and_status_later(curbuf, NOT_VALID);
1016}
1017
1018/*
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001019 * Get a key from the user without mapping.
Bram Moolenaar679653e2017-08-13 14:13:19 +02001020 * Note: while waiting a terminal may be closed and freed if the channel is
1021 * closed and ++close was used.
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001022 * TODO: use terminal mode mappings.
1023 */
1024 static int
1025term_vgetc()
1026{
1027 int c;
1028
1029 ++no_mapping;
1030 ++allow_keys;
1031 got_int = FALSE;
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001032#ifdef WIN3264
1033 ctrl_break_was_pressed = FALSE;
1034#endif
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001035 c = vgetc();
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001036 got_int = FALSE;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001037 --no_mapping;
1038 --allow_keys;
1039 return c;
1040}
1041
1042/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001043 * Send keys to terminal.
Bram Moolenaar69198192017-08-05 14:10:48 +02001044 * Return FAIL when the key needs to be handled in Normal mode.
1045 * Return OK when the key was dropped or sent to the terminal.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001046 */
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001047 int
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001048send_keys_to_term(term_T *term, int c, int typed)
1049{
1050 char msg[KEY_BUF_LEN];
1051 size_t len;
1052 static int mouse_was_outside = FALSE;
1053 int dragging_outside = FALSE;
1054
1055 /* Catch keys that need to be handled as in Normal mode. */
1056 switch (c)
1057 {
1058 case NUL:
1059 case K_ZERO:
1060 if (typed)
1061 stuffcharReadbuff(c);
1062 return FAIL;
1063
1064 case K_IGNORE:
1065 return FAIL;
1066
1067 case K_LEFTDRAG:
1068 case K_MIDDLEDRAG:
1069 case K_RIGHTDRAG:
1070 case K_X1DRAG:
1071 case K_X2DRAG:
1072 dragging_outside = mouse_was_outside;
1073 /* FALLTHROUGH */
1074 case K_LEFTMOUSE:
1075 case K_LEFTMOUSE_NM:
1076 case K_LEFTRELEASE:
1077 case K_LEFTRELEASE_NM:
1078 case K_MIDDLEMOUSE:
1079 case K_MIDDLERELEASE:
1080 case K_RIGHTMOUSE:
1081 case K_RIGHTRELEASE:
1082 case K_X1MOUSE:
1083 case K_X1RELEASE:
1084 case K_X2MOUSE:
1085 case K_X2RELEASE:
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001086
1087 case K_MOUSEUP:
1088 case K_MOUSEDOWN:
1089 case K_MOUSELEFT:
1090 case K_MOUSERIGHT:
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001091 if (mouse_row < W_WINROW(curwin)
1092 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
1093 || mouse_col < W_WINCOL(curwin)
1094 || mouse_col >= W_ENDCOL(curwin)
1095 || dragging_outside)
1096 {
Bram Moolenaar98fd66d2017-08-05 19:34:47 +02001097 /* click or scroll outside the current window */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001098 if (typed)
1099 {
1100 stuffcharReadbuff(c);
1101 mouse_was_outside = TRUE;
1102 }
1103 return FAIL;
1104 }
1105 }
1106 if (typed)
1107 mouse_was_outside = FALSE;
1108
1109 /* Convert the typed key to a sequence of bytes for the job. */
1110 len = term_convert_key(term, c, msg);
1111 if (len > 0)
1112 /* TODO: if FAIL is returned, stop? */
1113 channel_send(term->tl_job->jv_channel, PART_IN,
1114 (char_u *)msg, (int)len, NULL);
1115
1116 return OK;
1117}
1118
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001119 static void
1120position_cursor(win_T *wp, VTermPos *pos)
1121{
1122 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1123 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1124 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1125}
1126
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001127/*
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001128 * Handle CTRL-W "": send register contents to the job.
1129 */
1130 static void
1131term_paste_register(int prev_c UNUSED)
1132{
1133 int c;
1134 list_T *l;
1135 listitem_T *item;
1136 long reglen = 0;
1137 int type;
1138
1139#ifdef FEAT_CMDL_INFO
1140 if (add_to_showcmd(prev_c))
1141 if (add_to_showcmd('"'))
1142 out_flush();
1143#endif
1144 c = term_vgetc();
1145#ifdef FEAT_CMDL_INFO
1146 clear_showcmd();
1147#endif
Bram Moolenaar679653e2017-08-13 14:13:19 +02001148 if (!term_use_loop())
1149 /* job finished while waiting for a character */
1150 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001151
1152 /* CTRL-W "= prompt for expression to evaluate. */
1153 if (c == '=' && get_expr_register() != '=')
1154 return;
Bram Moolenaar679653e2017-08-13 14:13:19 +02001155 if (!term_use_loop())
1156 /* job finished while waiting for a character */
1157 return;
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001158
1159 l = (list_T *)get_reg_contents(c, GREG_LIST);
1160 if (l != NULL)
1161 {
1162 type = get_reg_type(c, &reglen);
1163 for (item = l->lv_first; item != NULL; item = item->li_next)
1164 {
1165 char_u *s = get_tv_string(&item->li_tv);
1166
1167 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1168 s, STRLEN(s), NULL);
1169 if (item->li_next != NULL || type == MLINE)
1170 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1171 (char_u *)"\r", 1, NULL);
1172 }
1173 list_free(l);
1174 }
1175}
1176
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001177#if defined(FEAT_GUI) || defined(PROTO)
1178/*
1179 * Return TRUE when the cursor of the terminal should be displayed.
1180 */
1181 int
1182use_terminal_cursor()
1183{
1184 return in_terminal_loop != NULL;
1185}
1186
1187 cursorentry_T *
1188term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1189{
1190 term_T *term = in_terminal_loop;
1191 static cursorentry_T entry;
1192
1193 vim_memset(&entry, 0, sizeof(entry));
1194 entry.shape = entry.mshape =
1195 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1196 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1197 SHAPE_BLOCK;
1198 entry.percentage = 20;
1199 if (term->tl_cursor_blink)
1200 {
1201 entry.blinkwait = 700;
1202 entry.blinkon = 400;
1203 entry.blinkon = 250;
1204 }
1205 *fg = gui.back_pixel;
1206 if (term->tl_cursor_color == NULL)
1207 *bg = gui.norm_pixel;
1208 else
1209 *bg = color_name2handle(term->tl_cursor_color);
1210 entry.name = "n";
1211 entry.used_for = SHAPE_CURSOR;
1212
1213 return &entry;
1214}
1215#endif
1216
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001217static int did_change_cursor = FALSE;
1218
1219 static void
1220may_set_cursor_props(term_T *term)
1221{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001222#ifdef FEAT_GUI
1223 /* For the GUI the cursor properties are obtained with
1224 * term_get_cursor_shape(). */
1225 if (gui.in_use)
1226 return;
1227#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001228 if (in_terminal_loop == term)
1229 {
Bram Moolenaar893029a2017-08-12 21:15:34 +02001230 did_change_cursor = TRUE;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001231 if (term->tl_cursor_color != NULL)
1232 term_cursor_color(term->tl_cursor_color);
1233 else
1234 term_cursor_color((char_u *)"");
1235 /* do both blink and shape+blink, in case setting shape does not work */
1236 term_cursor_blink(term->tl_cursor_blink);
1237 term_cursor_shape(term->tl_cursor_shape, term->tl_cursor_blink);
1238 }
1239}
1240
1241 static void
1242may_restore_cursor_props(void)
1243{
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001244#ifdef FEAT_GUI
1245 if (gui.in_use)
1246 return;
1247#endif
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001248 if (did_change_cursor)
1249 {
1250 did_change_cursor = FALSE;
1251 ui_cursor_shape_forced(TRUE);
1252 term_cursor_color((char_u *)"");
1253 term_cursor_blink(FALSE);
1254 }
1255}
1256
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001257/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02001258 * Returns TRUE if the current window contains a terminal and we are sending
1259 * keys to the job.
1260 */
1261 int
Bram Moolenaar6d819742017-08-06 14:57:49 +02001262term_use_loop(void)
Bram Moolenaar423802d2017-07-30 16:52:24 +02001263{
1264 term_T *term = curbuf->b_term;
1265
1266 return term != NULL
Bram Moolenaar6d819742017-08-06 14:57:49 +02001267 && !term->tl_normal_mode
Bram Moolenaar423802d2017-07-30 16:52:24 +02001268 && term->tl_vterm != NULL
1269 && term_job_running(term);
1270}
1271
1272/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001273 * Wait for input and send it to the job.
1274 * Return when the start of a CTRL-W command is typed or anything else that
1275 * should be handled as a Normal mode command.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001276 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
1277 * the terminal was closed.
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001278 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001279 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001280terminal_loop(void)
1281{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001282 int c;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001283 int termkey = 0;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001284 int ret;
1285
Bram Moolenaar679653e2017-08-13 14:13:19 +02001286 /* Remember the terminal we are sending keys to. However, the terminal
1287 * might be closed while waiting for a character, e.g. typing "exit" in a
1288 * shell and ++close was used. Therefore use curbuf->b_term instead of a
1289 * stored reference. */
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001290 in_terminal_loop = curbuf->b_term;
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001291
1292 if (*curwin->w_p_tk != NUL)
1293 termkey = string_to_key(curwin->w_p_tk, TRUE);
Bram Moolenaar0e23e9c2017-07-30 18:47:19 +02001294 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001295 may_set_cursor_props(curbuf->b_term);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001296
1297 for (;;)
1298 {
1299 /* TODO: skip screen update when handling a sequence of keys. */
Bram Moolenaar43c007f2017-07-30 17:45:37 +02001300 /* Repeat redrawing in case a message is received while redrawing. */
1301 while (curwin->w_redr_type != 0)
1302 update_screen(0);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001303 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaar423802d2017-07-30 16:52:24 +02001304
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001305 c = term_vgetc();
Bram Moolenaar6d819742017-08-06 14:57:49 +02001306 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001307 /* job finished while waiting for a character */
1308 break;
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001309 if (c == K_IGNORE)
1310 continue;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001311
Bram Moolenaarfae42832017-08-01 22:24:26 +02001312#ifdef UNIX
1313 may_send_sigint(c, curbuf->b_term->tl_job->jv_pid, 0);
1314#endif
1315#ifdef WIN3264
Bram Moolenaar589b1102017-08-12 16:39:05 +02001316 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001317 * Use CTRL-BREAK to kill the job. */
Bram Moolenaar9698ad72017-08-12 14:52:15 +02001318 if (ctrl_break_was_pressed)
1319 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
Bram Moolenaarfae42832017-08-01 22:24:26 +02001320#endif
1321
Bram Moolenaar69198192017-08-05 14:10:48 +02001322 if (c == (termkey == 0 ? Ctrl_W : termkey) || c == Ctrl_BSL)
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001323 {
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001324 int prev_c = c;
1325
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001326#ifdef FEAT_CMDL_INFO
1327 if (add_to_showcmd(c))
1328 out_flush();
1329#endif
1330 c = term_vgetc();
1331#ifdef FEAT_CMDL_INFO
1332 clear_showcmd();
1333#endif
Bram Moolenaar6d819742017-08-06 14:57:49 +02001334 if (!term_use_loop())
Bram Moolenaard85f2712017-07-28 21:51:57 +02001335 /* job finished while waiting for a character */
1336 break;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001337
Bram Moolenaar69198192017-08-05 14:10:48 +02001338 if (prev_c == Ctrl_BSL)
1339 {
1340 if (c == Ctrl_N)
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001341 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001342 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
1343 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001344 ret = FAIL;
1345 goto theend;
Bram Moolenaaraaa8a352017-08-05 20:17:00 +02001346 }
Bram Moolenaar69198192017-08-05 14:10:48 +02001347 /* Send both keys to the terminal. */
1348 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
1349 }
1350 else if (termkey == 0 && c == '.')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001351 {
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001352 /* "CTRL-W .": send CTRL-W to the job */
1353 c = Ctrl_W;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001354 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001355 else if (c == 'N')
Bram Moolenaar423802d2017-07-30 16:52:24 +02001356 {
Bram Moolenaar6d819742017-08-06 14:57:49 +02001357 /* CTRL-W N : go to Terminal-Normal mode. */
1358 term_enter_normal_mode();
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001359 ret = FAIL;
1360 goto theend;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001361 }
Bram Moolenaarc9456ce2017-07-30 21:46:04 +02001362 else if (c == '"')
1363 {
1364 term_paste_register(prev_c);
1365 continue;
1366 }
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001367 else if (termkey == 0 || c != termkey)
1368 {
1369 stuffcharReadbuff(Ctrl_W);
1370 stuffcharReadbuff(c);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001371 ret = OK;
1372 goto theend;
Bram Moolenaar1f28b4c2017-07-28 13:48:34 +02001373 }
Bram Moolenaardbe948d2017-07-23 22:50:51 +02001374 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001375 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001376 {
1377 ret = OK;
1378 goto theend;
1379 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001380 }
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001381 ret = FAIL;
1382
1383theend:
1384 in_terminal_loop = NULL;
1385 may_restore_cursor_props();
1386 return ret;
Bram Moolenaar1f2903c2017-07-23 19:51:01 +02001387}
1388
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001389/*
1390 * Called when a job has finished.
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001391 * This updates the title and status, but does not close the vterm, because
Bram Moolenaar423802d2017-07-30 16:52:24 +02001392 * there might still be pending output in the channel.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001393 */
1394 void
1395term_job_ended(job_T *job)
1396{
1397 term_T *term;
1398 int did_one = FALSE;
1399
1400 for (term = first_term; term != NULL; term = term->tl_next)
1401 if (term->tl_job == job)
1402 {
1403 vim_free(term->tl_title);
1404 term->tl_title = NULL;
1405 vim_free(term->tl_status_text);
1406 term->tl_status_text = NULL;
1407 redraw_buf_and_status_later(term->tl_buffer, VALID);
1408 did_one = TRUE;
1409 }
1410 if (did_one)
1411 redraw_statuslines();
1412 if (curbuf->b_term != NULL)
1413 {
1414 if (curbuf->b_term->tl_job == job)
1415 maketitle();
1416 update_cursor(curbuf->b_term, TRUE);
1417 }
1418}
1419
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001420 static void
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001421may_toggle_cursor(term_T *term)
1422{
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001423 if (in_terminal_loop == term)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001424 {
1425 if (term->tl_cursor_visible)
1426 cursor_on();
1427 else
1428 cursor_off();
1429 }
1430}
1431
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001432 static int
1433handle_damage(VTermRect rect, void *user)
1434{
1435 term_T *term = (term_T *)user;
1436
1437 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
1438 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
1439 redraw_buf_later(term->tl_buffer, NOT_VALID);
1440 return 1;
1441}
1442
1443 static int
1444handle_moverect(VTermRect dest UNUSED, VTermRect src UNUSED, void *user)
1445{
1446 term_T *term = (term_T *)user;
1447
1448 /* TODO */
1449 redraw_buf_later(term->tl_buffer, NOT_VALID);
1450 return 1;
1451}
1452
1453 static int
1454handle_movecursor(
1455 VTermPos pos,
1456 VTermPos oldpos UNUSED,
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001457 int visible,
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001458 void *user)
1459{
1460 term_T *term = (term_T *)user;
1461 win_T *wp;
Bram Moolenaar22aad2f2017-07-30 18:19:46 +02001462
1463 term->tl_cursor_pos = pos;
1464 term->tl_cursor_visible = visible;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001465
1466 FOR_ALL_WINDOWS(wp)
1467 {
1468 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001469 position_cursor(wp, &pos);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001470 }
Bram Moolenaar6d819742017-08-06 14:57:49 +02001471 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001472 {
1473 may_toggle_cursor(term);
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001474 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001475 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001476
1477 return 1;
1478}
1479
Bram Moolenaar21554412017-07-24 21:44:43 +02001480 static int
1481handle_settermprop(
1482 VTermProp prop,
1483 VTermValue *value,
1484 void *user)
1485{
1486 term_T *term = (term_T *)user;
1487
1488 switch (prop)
1489 {
1490 case VTERM_PROP_TITLE:
1491 vim_free(term->tl_title);
1492 term->tl_title = vim_strsave((char_u *)value->string);
1493 vim_free(term->tl_status_text);
1494 term->tl_status_text = NULL;
1495 if (term == curbuf->b_term)
1496 maketitle();
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001497 break;
1498
1499 case VTERM_PROP_CURSORVISIBLE:
1500 term->tl_cursor_visible = value->boolean;
1501 may_toggle_cursor(term);
1502 out_flush();
1503 break;
1504
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001505 case VTERM_PROP_CURSORBLINK:
1506 term->tl_cursor_blink = value->boolean;
1507 may_set_cursor_props(term);
1508 break;
1509
1510 case VTERM_PROP_CURSORSHAPE:
1511 term->tl_cursor_shape = value->number;
1512 may_set_cursor_props(term);
1513 break;
1514
1515 case VTERM_PROP_CURSORCOLOR:
1516 vim_free(term->tl_cursor_color);
Bram Moolenaar3d9bdfe2017-08-12 22:55:58 +02001517 if (*value->string == NUL)
1518 term->tl_cursor_color = NULL;
1519 else
1520 term->tl_cursor_color = vim_strsave((char_u *)value->string);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02001521 may_set_cursor_props(term);
1522 break;
1523
Bram Moolenaare41e3b42017-08-11 16:24:50 +02001524 case VTERM_PROP_ALTSCREEN:
1525 /* TODO: do anything else? */
1526 term->tl_using_altscreen = value->boolean;
1527 break;
1528
Bram Moolenaar21554412017-07-24 21:44:43 +02001529 default:
1530 break;
1531 }
Bram Moolenaarfc716d72017-07-25 23:08:47 +02001532 /* Always return 1, otherwise vterm doesn't store the value internally. */
1533 return 1;
Bram Moolenaar21554412017-07-24 21:44:43 +02001534}
1535
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001536/*
1537 * The job running in the terminal resized the terminal.
1538 */
1539 static int
1540handle_resize(int rows, int cols, void *user)
1541{
1542 term_T *term = (term_T *)user;
1543 win_T *wp;
1544
1545 term->tl_rows = rows;
1546 term->tl_cols = cols;
1547 FOR_ALL_WINDOWS(wp)
1548 {
1549 if (wp->w_buffer == term->tl_buffer)
1550 {
1551 win_setheight_win(rows, wp);
1552 win_setwidth_win(cols, wp);
1553 }
1554 }
1555
1556 redraw_buf_later(term->tl_buffer, NOT_VALID);
1557 return 1;
1558}
1559
Bram Moolenaard85f2712017-07-28 21:51:57 +02001560/*
1561 * Handle a line that is pushed off the top of the screen.
1562 */
1563 static int
1564handle_pushline(int cols, const VTermScreenCell *cells, void *user)
1565{
1566 term_T *term = (term_T *)user;
1567
1568 /* TODO: Limit the number of lines that are stored. */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001569 if (ga_grow(&term->tl_scrollback, 1) == OK)
1570 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001571 cellattr_T *p = NULL;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001572 int len = 0;
1573 int i;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001574 int c;
1575 int col;
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001576 sb_line_T *line;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001577 garray_T ga;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001578
1579 /* do not store empty cells at the end */
1580 for (i = 0; i < cols; ++i)
1581 if (cells[i].chars[0] != 0)
1582 len = i + 1;
1583
Bram Moolenaar7fadbf82017-08-07 22:08:05 +02001584 ga_init2(&ga, 1, 100);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001585 if (len > 0)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001586 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001587 if (p != NULL)
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001588 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001589 for (col = 0; col < len; col += cells[col].width)
1590 {
1591 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
1592 {
1593 ga.ga_len = 0;
1594 break;
1595 }
1596 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
1597 ga.ga_len += mb_char2bytes(c == NUL ? ' ' : c,
1598 (char_u *)ga.ga_data + ga.ga_len);
1599 p[col].width = cells[col].width;
1600 p[col].attrs = cells[col].attrs;
1601 p[col].fg = cells[col].fg;
1602 p[col].bg = cells[col].bg;
1603 }
1604 }
1605 if (ga_grow(&ga, 1) == FAIL)
1606 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1607 else
1608 {
1609 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1610 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1611 }
1612 ga_clear(&ga);
Bram Moolenaar696d00f2017-07-29 14:52:43 +02001613
1614 line = (sb_line_T *)term->tl_scrollback.ga_data
1615 + term->tl_scrollback.ga_len;
1616 line->sb_cols = len;
1617 line->sb_cells = p;
1618 ++term->tl_scrollback.ga_len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001619 ++term->tl_scrollback_scrolled;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001620 }
1621 return 0; /* ignored */
1622}
1623
Bram Moolenaar21554412017-07-24 21:44:43 +02001624static VTermScreenCallbacks screen_callbacks = {
1625 handle_damage, /* damage */
1626 handle_moverect, /* moverect */
1627 handle_movecursor, /* movecursor */
1628 handle_settermprop, /* settermprop */
1629 NULL, /* bell */
1630 handle_resize, /* resize */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001631 handle_pushline, /* sb_pushline */
Bram Moolenaar21554412017-07-24 21:44:43 +02001632 NULL /* sb_popline */
1633};
1634
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001635/*
Bram Moolenaard85f2712017-07-28 21:51:57 +02001636 * Called when a channel has been closed.
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02001637 * If this was a channel for a terminal window then finish it up.
Bram Moolenaard85f2712017-07-28 21:51:57 +02001638 */
1639 void
1640term_channel_closed(channel_T *ch)
1641{
1642 term_T *term;
1643 int did_one = FALSE;
1644
1645 for (term = first_term; term != NULL; term = term->tl_next)
1646 if (term->tl_job == ch->ch_job)
1647 {
Bram Moolenaar423802d2017-07-30 16:52:24 +02001648 term->tl_channel_closed = TRUE;
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001649 did_one = TRUE;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001650
Bram Moolenaard85f2712017-07-28 21:51:57 +02001651 vim_free(term->tl_title);
1652 term->tl_title = NULL;
1653 vim_free(term->tl_status_text);
1654 term->tl_status_text = NULL;
1655
Bram Moolenaar423802d2017-07-30 16:52:24 +02001656 /* Unless in Terminal-Normal mode: clear the vterm. */
Bram Moolenaar6d819742017-08-06 14:57:49 +02001657 if (!term->tl_normal_mode)
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001658 {
1659 int fnum = term->tl_buffer->b_fnum;
1660
Bram Moolenaar423802d2017-07-30 16:52:24 +02001661 cleanup_vterm(term);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001662
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001663 if (term->tl_finish == 'c')
1664 {
1665 /* ++close or term_finish == "close" */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001666 ch_log(NULL, "terminal job finished, closing window");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001667 curbuf = term->tl_buffer;
1668 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
1669 break;
1670 }
1671 if (term->tl_finish == 'o' && term->tl_buffer->b_nwindows == 0)
1672 {
1673 char buf[50];
1674
1675 /* TODO: use term_opencmd */
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001676 ch_log(NULL, "terminal job finished, opening window");
Bram Moolenaar37c45832017-08-12 16:01:04 +02001677 vim_snprintf(buf, sizeof(buf),
1678 term->tl_opencmd == NULL
Bram Moolenaar589b1102017-08-12 16:39:05 +02001679 ? "botright sbuf %d"
1680 : (char *)term->tl_opencmd, fnum);
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001681 do_cmdline_cmd((char_u *)buf);
1682 }
Bram Moolenaar8cad9302017-08-12 14:32:32 +02001683 else
1684 ch_log(NULL, "terminal job finished");
Bram Moolenaardd693ce2017-08-10 23:15:19 +02001685 }
1686
Bram Moolenaard85f2712017-07-28 21:51:57 +02001687 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001688 }
1689 if (did_one)
1690 {
1691 redraw_statuslines();
1692
1693 /* Need to break out of vgetc(). */
1694 ins_char_typebuf(K_IGNORE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02001695 typebuf_was_filled = TRUE;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001696
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001697 term = curbuf->b_term;
1698 if (term != NULL)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001699 {
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001700 if (term->tl_job == ch->ch_job)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001701 maketitle();
Bram Moolenaar12d93ee2017-07-30 19:02:02 +02001702 update_cursor(term, term->tl_cursor_visible);
Bram Moolenaard85f2712017-07-28 21:51:57 +02001703 }
1704 }
1705}
1706
1707/*
Bram Moolenaareeac6772017-07-23 15:48:37 +02001708 * Reverse engineer the RGB value into a cterm color index.
1709 * First color is 1. Return 0 if no match found.
1710 */
1711 static int
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001712color2index(VTermColor *color, int fg, int *boldp)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001713{
1714 int red = color->red;
1715 int blue = color->blue;
1716 int green = color->green;
1717
Bram Moolenaarb41bf8e2017-07-28 15:11:38 +02001718 /* The argument for lookup_color() is for the color_names[] table. */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001719 if (red == 0)
1720 {
1721 if (green == 0)
1722 {
1723 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001724 return lookup_color(0, fg, boldp) + 1; /* black */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001725 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001726 return lookup_color(1, fg, boldp) + 1; /* dark blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001727 }
1728 else if (green == 224)
1729 {
1730 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001731 return lookup_color(2, fg, boldp) + 1; /* dark green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001732 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001733 return lookup_color(3, fg, boldp) + 1; /* dark cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001734 }
1735 }
1736 else if (red == 224)
1737 {
1738 if (green == 0)
1739 {
1740 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001741 return lookup_color(4, fg, boldp) + 1; /* dark red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001742 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001743 return lookup_color(5, fg, boldp) + 1; /* dark magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001744 }
1745 else if (green == 224)
1746 {
1747 if (blue == 0)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001748 return lookup_color(6, fg, boldp) + 1; /* dark yellow / brown */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001749 if (blue == 224)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001750 return lookup_color(8, fg, boldp) + 1; /* white / light grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001751 }
1752 }
1753 else if (red == 128)
1754 {
1755 if (green == 128 && blue == 128)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001756 return lookup_color(12, fg, boldp) + 1; /* high intensity black / dark grey */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001757 }
1758 else if (red == 255)
1759 {
1760 if (green == 64)
1761 {
1762 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001763 return lookup_color(20, fg, boldp) + 1; /* light red */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001764 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001765 return lookup_color(22, fg, boldp) + 1; /* light magenta */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001766 }
1767 else if (green == 255)
1768 {
1769 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001770 return lookup_color(24, fg, boldp) + 1; /* yellow */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001771 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001772 return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001773 }
1774 }
1775 else if (red == 64)
1776 {
1777 if (green == 64)
1778 {
1779 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001780 return lookup_color(14, fg, boldp) + 1; /* light blue */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001781 }
1782 else if (green == 255)
1783 {
1784 if (blue == 64)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001785 return lookup_color(16, fg, boldp) + 1; /* light green */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001786 if (blue == 255)
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001787 return lookup_color(18, fg, boldp) + 1; /* light cyan */
Bram Moolenaareeac6772017-07-23 15:48:37 +02001788 }
1789 }
1790 if (t_colors >= 256)
1791 {
1792 if (red == blue && red == green)
1793 {
1794 /* 24-color greyscale */
1795 static int cutoff[23] = {
1796 0x05, 0x10, 0x1B, 0x26, 0x31, 0x3C, 0x47, 0x52,
1797 0x5D, 0x68, 0x73, 0x7F, 0x8A, 0x95, 0xA0, 0xAB,
1798 0xB6, 0xC1, 0xCC, 0xD7, 0xE2, 0xED, 0xF9};
1799 int i;
1800
1801 for (i = 0; i < 23; ++i)
1802 if (red < cutoff[i])
1803 return i + 233;
1804 return 256;
1805 }
1806
1807 /* 216-color cube */
1808 return 17 + ((red + 25) / 0x33) * 36
1809 + ((green + 25) / 0x33) * 6
1810 + (blue + 25) / 0x33;
1811 }
1812 return 0;
1813}
1814
1815/*
1816 * Convert the attributes of a vterm cell into an attribute index.
1817 */
1818 static int
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001819cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001820{
1821 int attr = 0;
1822
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001823 if (cellattrs.bold)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001824 attr |= HL_BOLD;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001825 if (cellattrs.underline)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001826 attr |= HL_UNDERLINE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001827 if (cellattrs.italic)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001828 attr |= HL_ITALIC;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001829 if (cellattrs.strike)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001830 attr |= HL_STANDOUT;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001831 if (cellattrs.reverse)
Bram Moolenaareeac6772017-07-23 15:48:37 +02001832 attr |= HL_INVERSE;
Bram Moolenaareeac6772017-07-23 15:48:37 +02001833
1834#ifdef FEAT_GUI
1835 if (gui.in_use)
1836 {
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001837 guicolor_T fg, bg;
1838
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001839 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
1840 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar26af85d2017-07-23 16:45:10 +02001841 return get_gui_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001842 }
1843 else
1844#endif
1845#ifdef FEAT_TERMGUICOLORS
1846 if (p_tgc)
1847 {
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001848 guicolor_T fg, bg;
1849
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001850 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
1851 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001852
1853 return get_tgc_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001854 }
Bram Moolenaar065f41c2017-07-23 18:07:56 +02001855 else
Bram Moolenaareeac6772017-07-23 15:48:37 +02001856#endif
1857 {
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001858 int bold = MAYBE;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001859 int fg = color2index(&cellfg, TRUE, &bold);
1860 int bg = color2index(&cellbg, FALSE, &bold);
Bram Moolenaar12d853f2017-08-01 18:04:04 +02001861
1862 /* with 8 colors set the bold attribute to get a bright foreground */
1863 if (bold == TRUE)
1864 attr |= HL_BOLD;
1865 return get_cterm_attr_idx(attr, fg, bg);
Bram Moolenaareeac6772017-07-23 15:48:37 +02001866 }
1867 return 0;
1868}
1869
1870/*
Bram Moolenaar6d819742017-08-06 14:57:49 +02001871 * Called to update a window that contains an active terminal.
1872 * Returns FAIL when there is no terminal running in this window or in
1873 * Terminal-Normal mode.
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001874 */
Bram Moolenaard85f2712017-07-28 21:51:57 +02001875 int
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001876term_update_window(win_T *wp)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001877{
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001878 term_T *term = wp->w_buffer->b_term;
Bram Moolenaard85f2712017-07-28 21:51:57 +02001879 VTerm *vterm;
1880 VTermScreen *screen;
1881 VTermState *state;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001882 VTermPos pos;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001883
Bram Moolenaar6d819742017-08-06 14:57:49 +02001884 if (term == NULL || term->tl_vterm == NULL || term->tl_normal_mode)
Bram Moolenaard85f2712017-07-28 21:51:57 +02001885 return FAIL;
Bram Moolenaar423802d2017-07-30 16:52:24 +02001886
Bram Moolenaard85f2712017-07-28 21:51:57 +02001887 vterm = term->tl_vterm;
1888 screen = vterm_obtain_screen(vterm);
1889 state = vterm_obtain_state(vterm);
1890
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001891 /*
1892 * If the window was resized a redraw will be triggered and we get here.
1893 * Adjust the size of the vterm unless 'termsize' specifies a fixed size.
1894 */
1895 if ((!term->tl_rows_fixed && term->tl_rows != wp->w_height)
1896 || (!term->tl_cols_fixed && term->tl_cols != wp->w_width))
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001897 {
Bram Moolenaar96ad8c92017-07-28 14:17:34 +02001898 int rows = term->tl_rows_fixed ? term->tl_rows : wp->w_height;
1899 int cols = term->tl_cols_fixed ? term->tl_cols : wp->w_width;
1900 win_T *twp;
1901
1902 FOR_ALL_WINDOWS(twp)
1903 {
1904 /* When more than one window shows the same terminal, use the
1905 * smallest size. */
1906 if (twp->w_buffer == term->tl_buffer)
1907 {
1908 if (!term->tl_rows_fixed && rows > twp->w_height)
1909 rows = twp->w_height;
1910 if (!term->tl_cols_fixed && cols > twp->w_width)
1911 cols = twp->w_width;
1912 }
1913 }
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001914
1915 vterm_set_size(vterm, rows, cols);
Bram Moolenaar2f3a90a2017-08-03 14:49:29 +02001916 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001917 rows);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02001918 term_report_winsize(term, rows, cols);
Bram Moolenaarb13501f2017-07-22 22:32:56 +02001919 }
Bram Moolenaar58556cd2017-07-20 23:04:46 +02001920
1921 /* The cursor may have been moved when resizing. */
1922 vterm_state_get_cursorpos(state, &pos);
1923 position_cursor(wp, &pos);
1924
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001925 /* TODO: Only redraw what changed. */
1926 for (pos.row = 0; pos.row < wp->w_height; ++pos.row)
Bram Moolenaar938783d2017-07-16 20:13:26 +02001927 {
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001928 int off = screen_get_current_line_off();
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001929 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001930
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001931 if (pos.row < term->tl_rows)
1932 {
1933 for (pos.col = 0; pos.col < max_col; )
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001934 {
1935 VTermScreenCell cell;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001936 int c;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001937
Bram Moolenaareeac6772017-07-23 15:48:37 +02001938 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1939 vim_memset(&cell, 0, sizeof(cell));
1940
1941 /* TODO: composing chars */
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001942 c = cell.chars[0];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001943 if (c == NUL)
1944 {
1945 ScreenLines[off] = ' ';
Bram Moolenaar8a773062017-07-24 22:29:21 +02001946#if defined(FEAT_MBYTE)
1947 if (enc_utf8)
1948 ScreenLinesUC[off] = NUL;
1949#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001950 }
1951 else
1952 {
1953#if defined(FEAT_MBYTE)
1954 if (enc_utf8 && c >= 0x80)
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001955 {
1956 ScreenLines[off] = ' ';
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001957 ScreenLinesUC[off] = c;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001958 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001959 else
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001960 {
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001961 ScreenLines[off] = c;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001962 if (enc_utf8)
1963 ScreenLinesUC[off] = NUL;
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001964 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001965#else
1966 ScreenLines[off] = c;
1967#endif
1968 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02001969 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001970
1971 ++pos.col;
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001972 ++off;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001973 if (cell.width == 2)
1974 {
Bram Moolenaar9f1f49b2017-07-22 18:14:17 +02001975 ScreenLines[off] = NUL;
Bram Moolenaar8a773062017-07-24 22:29:21 +02001976#if defined(FEAT_MBYTE)
1977 if (enc_utf8)
1978 ScreenLinesUC[off] = NUL;
1979#endif
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001980 ++pos.col;
1981 ++off;
1982 }
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02001983 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001984 }
Bram Moolenaare825d8b2017-07-19 23:20:19 +02001985 else
1986 pos.col = 0;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001987
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001988 screen_line(wp->w_winrow + pos.row, wp->w_wincol,
1989 pos.col, wp->w_width, FALSE);
Bram Moolenaar938783d2017-07-16 20:13:26 +02001990 }
Bram Moolenaard85f2712017-07-28 21:51:57 +02001991
1992 return OK;
Bram Moolenaar938783d2017-07-16 20:13:26 +02001993}
Bram Moolenaare4f25e42017-07-07 11:54:15 +02001994
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02001995/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02001996 * Return TRUE if "wp" is a terminal window where the job has finished.
1997 */
1998 int
1999term_is_finished(buf_T *buf)
2000{
2001 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
2002}
2003
2004/*
Bram Moolenaar423802d2017-07-30 16:52:24 +02002005 * Return TRUE if "wp" is a terminal window where the job has finished or we
Bram Moolenaar6d819742017-08-06 14:57:49 +02002006 * are in Terminal-Normal mode, thus we show the buffer contents.
Bram Moolenaar423802d2017-07-30 16:52:24 +02002007 */
2008 int
2009term_show_buffer(buf_T *buf)
2010{
2011 term_T *term = buf->b_term;
2012
Bram Moolenaar6d819742017-08-06 14:57:49 +02002013 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
Bram Moolenaar423802d2017-07-30 16:52:24 +02002014}
2015
2016/*
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002017 * The current buffer is going to be changed. If there is terminal
2018 * highlighting remove it now.
2019 */
2020 void
2021term_change_in_curbuf(void)
2022{
2023 term_T *term = curbuf->b_term;
2024
2025 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
2026 {
2027 free_scrollback(term);
2028 redraw_buf_later(term->tl_buffer, NOT_VALID);
Bram Moolenaar20e6cd02017-08-01 20:25:22 +02002029
2030 /* The buffer is now like a normal buffer, it cannot be easily
2031 * abandoned when changed. */
2032 set_string_option_direct((char_u *)"buftype", -1,
2033 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002034 }
2035}
2036
2037/*
2038 * Get the screen attribute for a position in the buffer.
2039 */
2040 int
2041term_get_attr(buf_T *buf, linenr_T lnum, int col)
2042{
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002043 term_T *term = buf->b_term;
2044 sb_line_T *line;
2045 cellattr_T *cellattr;
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002046
Bram Moolenaar70229f92017-07-29 16:01:53 +02002047 if (lnum > term->tl_scrollback.ga_len)
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002048 return 0;
2049 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
2050 if (col >= line->sb_cols)
2051 return 0;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002052 cellattr = line->sb_cells + col;
2053 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
Bram Moolenaar63ecdda2017-07-28 22:29:35 +02002054}
2055
2056/*
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002057 * Create a new vterm and initialize it.
2058 */
2059 static void
2060create_vterm(term_T *term, int rows, int cols)
2061{
2062 VTerm *vterm;
2063 VTermScreen *screen;
2064
2065 vterm = vterm_new(rows, cols);
2066 term->tl_vterm = vterm;
2067 screen = vterm_obtain_screen(vterm);
2068 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
2069 /* TODO: depends on 'encoding'. */
2070 vterm_set_utf8(vterm, 1);
Bram Moolenaareeac6772017-07-23 15:48:37 +02002071
2072 /* Vterm uses a default black background. Set it to white when
2073 * 'background' is "light". */
2074 if (*p_bg == 'l')
2075 {
2076 VTermColor fg, bg;
2077
2078 fg.red = fg.green = fg.blue = 0;
2079 bg.red = bg.green = bg.blue = 255;
2080 vterm_state_set_default_colors(vterm_obtain_state(vterm), &fg, &bg);
2081 }
2082
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002083 /* Required to initialize most things. */
2084 vterm_screen_reset(screen, 1 /* hard */);
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002085
2086 /* Allow using alternate screen. */
2087 vterm_screen_enable_altscreen(screen, 1);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002088}
2089
Bram Moolenaar21554412017-07-24 21:44:43 +02002090/*
2091 * Return the text to show for the buffer name and status.
2092 */
2093 char_u *
2094term_get_status_text(term_T *term)
2095{
2096 if (term->tl_status_text == NULL)
2097 {
2098 char_u *txt;
2099 size_t len;
2100
Bram Moolenaar6d819742017-08-06 14:57:49 +02002101 if (term->tl_normal_mode)
Bram Moolenaar423802d2017-07-30 16:52:24 +02002102 {
2103 if (term_job_running(term))
2104 txt = (char_u *)_("Terminal");
2105 else
2106 txt = (char_u *)_("Terminal-finished");
2107 }
2108 else if (term->tl_title != NULL)
Bram Moolenaar21554412017-07-24 21:44:43 +02002109 txt = term->tl_title;
2110 else if (term_job_running(term))
2111 txt = (char_u *)_("running");
2112 else
2113 txt = (char_u *)_("finished");
2114 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaara1b5b092017-07-26 21:29:34 +02002115 term->tl_status_text = alloc((int)len);
Bram Moolenaar21554412017-07-24 21:44:43 +02002116 if (term->tl_status_text != NULL)
2117 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
2118 term->tl_buffer->b_fname, txt);
2119 }
2120 return term->tl_status_text;
2121}
2122
Bram Moolenaarf86eea92017-07-28 13:51:30 +02002123/*
2124 * Mark references in jobs of terminals.
2125 */
2126 int
2127set_ref_in_term(int copyID)
2128{
2129 int abort = FALSE;
2130 term_T *term;
2131 typval_T tv;
2132
2133 for (term = first_term; term != NULL; term = term->tl_next)
2134 if (term->tl_job != NULL)
2135 {
2136 tv.v_type = VAR_JOB;
2137 tv.vval.v_job = term->tl_job;
2138 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
2139 }
2140 return abort;
2141}
2142
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002143/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002144 * Get the buffer from the first argument in "argvars".
2145 * Returns NULL when the buffer is not for a terminal window.
2146 */
2147 static buf_T *
2148term_get_buf(typval_T *argvars)
2149{
2150 buf_T *buf;
2151
2152 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
2153 ++emsg_off;
2154 buf = get_buf_tv(&argvars[0], FALSE);
2155 --emsg_off;
2156 if (buf == NULL || buf->b_term == NULL)
2157 return NULL;
2158 return buf;
2159}
2160
2161/*
Bram Moolenaare41e3b42017-08-11 16:24:50 +02002162 * "term_getaltscreen(buf)" function
2163 */
2164 void
2165f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
2166{
2167 buf_T *buf = term_get_buf(argvars);
2168
2169 if (buf == NULL)
2170 return;
2171 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
2172}
2173
2174/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002175 * "term_getattr(attr, name)" function
2176 */
2177 void
2178f_term_getattr(typval_T *argvars, typval_T *rettv)
2179{
2180 int attr;
2181 size_t i;
2182 char_u *name;
2183
2184 static struct {
2185 char *name;
2186 int attr;
2187 } attrs[] = {
2188 {"bold", HL_BOLD},
2189 {"italic", HL_ITALIC},
2190 {"underline", HL_UNDERLINE},
2191 {"strike", HL_STANDOUT},
2192 {"reverse", HL_INVERSE},
2193 };
2194
2195 attr = get_tv_number(&argvars[0]);
2196 name = get_tv_string_chk(&argvars[1]);
2197 if (name == NULL)
2198 return;
2199
2200 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
2201 if (STRCMP(name, attrs[i].name) == 0)
2202 {
2203 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
2204 break;
2205 }
2206}
2207
2208/*
Bram Moolenaar97870002017-07-30 18:28:38 +02002209 * "term_getcursor(buf)" function
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002210 */
Bram Moolenaar97870002017-07-30 18:28:38 +02002211 void
2212f_term_getcursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002213{
Bram Moolenaar97870002017-07-30 18:28:38 +02002214 buf_T *buf = term_get_buf(argvars);
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002215 term_T *term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002216 list_T *l;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002217 dict_T *d;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002218
Bram Moolenaar97870002017-07-30 18:28:38 +02002219 if (rettv_list_alloc(rettv) == FAIL)
2220 return;
2221 if (buf == NULL)
2222 return;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002223 term = buf->b_term;
Bram Moolenaar97870002017-07-30 18:28:38 +02002224
2225 l = rettv->vval.v_list;
Bram Moolenaar3cd43cc2017-08-12 19:51:41 +02002226 list_append_number(l, term->tl_cursor_pos.row + 1);
2227 list_append_number(l, term->tl_cursor_pos.col + 1);
2228
2229 d = dict_alloc();
2230 if (d != NULL)
2231 {
2232 dict_add_nr_str(d, "visible", term->tl_cursor_visible, NULL);
2233 dict_add_nr_str(d, "blink", term->tl_cursor_blink, NULL);
2234 dict_add_nr_str(d, "shape", term->tl_cursor_shape, NULL);
2235 dict_add_nr_str(d, "color", 0L, term->tl_cursor_color == NULL
2236 ? (char_u *)"" : term->tl_cursor_color);
2237 list_append_dict(l, d);
2238 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002239}
2240
2241/*
2242 * "term_getjob(buf)" function
2243 */
2244 void
2245f_term_getjob(typval_T *argvars, typval_T *rettv)
2246{
2247 buf_T *buf = term_get_buf(argvars);
2248
2249 rettv->v_type = VAR_JOB;
2250 rettv->vval.v_job = NULL;
2251 if (buf == NULL)
2252 return;
2253
2254 rettv->vval.v_job = buf->b_term->tl_job;
2255 if (rettv->vval.v_job != NULL)
2256 ++rettv->vval.v_job->jv_refcount;
2257}
2258
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002259 static int
2260get_row_number(typval_T *tv, term_T *term)
2261{
2262 if (tv->v_type == VAR_STRING
2263 && tv->vval.v_string != NULL
2264 && STRCMP(tv->vval.v_string, ".") == 0)
2265 return term->tl_cursor_pos.row;
2266 return (int)get_tv_number(tv) - 1;
2267}
2268
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002269/*
2270 * "term_getline(buf, row)" function
2271 */
2272 void
2273f_term_getline(typval_T *argvars, typval_T *rettv)
2274{
2275 buf_T *buf = term_get_buf(argvars);
2276 term_T *term;
2277 int row;
2278
2279 rettv->v_type = VAR_STRING;
2280 if (buf == NULL)
2281 return;
2282 term = buf->b_term;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002283 row = get_row_number(&argvars[1], term);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002284
2285 if (term->tl_vterm == NULL)
2286 {
2287 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
2288
2289 /* vterm is finished, get the text from the buffer */
2290 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
2291 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
2292 }
2293 else
2294 {
2295 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
2296 VTermRect rect;
2297 int len;
2298 char_u *p;
2299
Bram Moolenaar5c838a32017-08-02 22:10:34 +02002300 if (row < 0 || row >= term->tl_rows)
2301 return;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002302 len = term->tl_cols * MB_MAXBYTES + 1;
2303 p = alloc(len);
2304 if (p == NULL)
2305 return;
2306 rettv->vval.v_string = p;
2307
2308 rect.start_col = 0;
2309 rect.end_col = term->tl_cols;
2310 rect.start_row = row;
2311 rect.end_row = row + 1;
2312 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
2313 }
2314}
2315
2316/*
Bram Moolenaar82b9ca02017-08-08 23:06:46 +02002317 * "term_getscrolled(buf)" function
2318 */
2319 void
2320f_term_getscrolled(typval_T *argvars, typval_T *rettv)
2321{
2322 buf_T *buf = term_get_buf(argvars);
2323
2324 if (buf == NULL)
2325 return;
2326 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
2327}
2328
2329/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002330 * "term_getsize(buf)" function
2331 */
2332 void
2333f_term_getsize(typval_T *argvars, typval_T *rettv)
2334{
2335 buf_T *buf = term_get_buf(argvars);
2336 list_T *l;
2337
2338 if (rettv_list_alloc(rettv) == FAIL)
2339 return;
2340 if (buf == NULL)
2341 return;
2342
2343 l = rettv->vval.v_list;
2344 list_append_number(l, buf->b_term->tl_rows);
2345 list_append_number(l, buf->b_term->tl_cols);
2346}
2347
2348/*
Bram Moolenaarb000e322017-07-30 19:38:21 +02002349 * "term_getstatus(buf)" function
2350 */
2351 void
2352f_term_getstatus(typval_T *argvars, typval_T *rettv)
2353{
2354 buf_T *buf = term_get_buf(argvars);
2355 term_T *term;
2356 char_u val[100];
2357
2358 rettv->v_type = VAR_STRING;
2359 if (buf == NULL)
2360 return;
2361 term = buf->b_term;
2362
2363 if (term_job_running(term))
2364 STRCPY(val, "running");
2365 else
2366 STRCPY(val, "finished");
Bram Moolenaar6d819742017-08-06 14:57:49 +02002367 if (term->tl_normal_mode)
2368 STRCAT(val, ",normal");
Bram Moolenaarb000e322017-07-30 19:38:21 +02002369 rettv->vval.v_string = vim_strsave(val);
2370}
2371
2372/*
2373 * "term_gettitle(buf)" function
2374 */
2375 void
2376f_term_gettitle(typval_T *argvars, typval_T *rettv)
2377{
2378 buf_T *buf = term_get_buf(argvars);
2379
2380 rettv->v_type = VAR_STRING;
2381 if (buf == NULL)
2382 return;
2383
2384 if (buf->b_term->tl_title != NULL)
2385 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
2386}
2387
2388/*
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002389 * "term_gettty(buf)" function
2390 */
2391 void
2392f_term_gettty(typval_T *argvars, typval_T *rettv)
2393{
2394 buf_T *buf = term_get_buf(argvars);
2395 char_u *p;
2396
2397 rettv->v_type = VAR_STRING;
2398 if (buf == NULL)
2399 return;
2400 if (buf->b_term->tl_job != NULL)
2401 p = buf->b_term->tl_job->jv_tty_name;
2402 else
2403 p = buf->b_term->tl_tty_name;
2404 if (p != NULL)
2405 rettv->vval.v_string = vim_strsave(p);
2406}
2407
2408/*
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002409 * "term_list()" function
2410 */
2411 void
2412f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
2413{
2414 term_T *tp;
2415 list_T *l;
2416
2417 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
2418 return;
2419
2420 l = rettv->vval.v_list;
2421 for (tp = first_term; tp != NULL; tp = tp->tl_next)
2422 if (tp != NULL && tp->tl_buffer != NULL)
2423 if (list_append_number(l,
2424 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
2425 return;
2426}
2427
2428/*
2429 * "term_scrape(buf, row)" function
2430 */
2431 void
2432f_term_scrape(typval_T *argvars, typval_T *rettv)
2433{
2434 buf_T *buf = term_get_buf(argvars);
2435 VTermScreen *screen = NULL;
2436 VTermPos pos;
2437 list_T *l;
2438 term_T *term;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002439 char_u *p;
2440 sb_line_T *line;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002441
2442 if (rettv_list_alloc(rettv) == FAIL)
2443 return;
2444 if (buf == NULL)
2445 return;
2446 term = buf->b_term;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002447
2448 l = rettv->vval.v_list;
Bram Moolenaarc2ce52c2017-08-01 18:35:38 +02002449 pos.row = get_row_number(&argvars[1], term);
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002450
2451 if (term->tl_vterm != NULL)
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002452 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002453 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaare20b3eb2017-08-07 21:26:29 +02002454 p = NULL;
2455 line = NULL;
2456 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002457 else
2458 {
2459 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
2460
2461 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
2462 return;
2463 p = ml_get_buf(buf, lnum + 1, FALSE);
2464 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
2465 }
2466
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002467 for (pos.col = 0; pos.col < term->tl_cols; )
2468 {
2469 dict_T *dcell;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002470 int width;
2471 VTermScreenCellAttrs attrs;
2472 VTermColor fg, bg;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002473 char_u rgb[8];
2474 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
2475 int off = 0;
2476 int i;
2477
2478 if (screen == NULL)
2479 {
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002480 cellattr_T *cellattr;
2481 int len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002482
2483 /* vterm has finished, get the cell from scrollback */
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002484 if (pos.col >= line->sb_cols)
2485 break;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002486 cellattr = line->sb_cells + pos.col;
2487 width = cellattr->width;
2488 attrs = cellattr->attrs;
2489 fg = cellattr->fg;
2490 bg = cellattr->bg;
2491 len = MB_PTR2LEN(p);
2492 mch_memmove(mbs, p, len);
2493 mbs[len] = NUL;
2494 p += len;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002495 }
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002496 else
2497 {
2498 VTermScreenCell cell;
2499 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
2500 break;
2501 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
2502 {
2503 if (cell.chars[i] == 0)
2504 break;
2505 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
2506 }
2507 mbs[off] = NUL;
2508 width = cell.width;
2509 attrs = cell.attrs;
2510 fg = cell.fg;
2511 bg = cell.bg;
2512 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002513 dcell = dict_alloc();
2514 list_append_dict(l, dcell);
2515
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002516 dict_add_nr_str(dcell, "chars", 0, mbs);
2517
2518 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002519 fg.red, fg.green, fg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002520 dict_add_nr_str(dcell, "fg", 0, rgb);
2521 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002522 bg.red, bg.green, bg.blue);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002523 dict_add_nr_str(dcell, "bg", 0, rgb);
2524
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002525 dict_add_nr_str(dcell, "attr",
2526 cell2attr(attrs, fg, bg), NULL);
2527 dict_add_nr_str(dcell, "width", width, NULL);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002528
2529 ++pos.col;
Bram Moolenaar33a43be2017-08-06 21:36:22 +02002530 if (width == 2)
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002531 ++pos.col;
2532 }
2533}
2534
2535/*
2536 * "term_sendkeys(buf, keys)" function
2537 */
2538 void
2539f_term_sendkeys(typval_T *argvars, typval_T *rettv)
2540{
2541 buf_T *buf = term_get_buf(argvars);
2542 char_u *msg;
2543 term_T *term;
2544
2545 rettv->v_type = VAR_UNKNOWN;
2546 if (buf == NULL)
2547 return;
2548
2549 msg = get_tv_string_chk(&argvars[1]);
2550 if (msg == NULL)
2551 return;
2552 term = buf->b_term;
2553 if (term->tl_vterm == NULL)
2554 return;
2555
2556 while (*msg != NUL)
2557 {
2558 send_keys_to_term(term, PTR2CHAR(msg), FALSE);
2559 msg += MB_PTR2LEN(msg);
2560 }
2561
Bram Moolenaar6d819742017-08-06 14:57:49 +02002562 if (!term->tl_normal_mode)
Bram Moolenaar392d1bf2017-07-31 21:18:58 +02002563 {
2564 /* TODO: only update once in a while. */
2565 update_screen(0);
2566 if (buf == curbuf)
2567 update_cursor(term, TRUE);
2568 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002569}
2570
2571/*
2572 * "term_start(command, options)" function
2573 */
2574 void
2575f_term_start(typval_T *argvars, typval_T *rettv)
2576{
2577 char_u *cmd = get_tv_string_chk(&argvars[0]);
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002578 jobopt_T opt;
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002579
2580 if (cmd == NULL)
2581 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002582 init_job_options(&opt);
2583 /* TODO: allow more job options */
2584 if (argvars[1].v_type != VAR_UNKNOWN
2585 && get_job_options(&argvars[1], &opt,
2586 JO_TIMEOUT_ALL + JO_STOPONEXIT
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002587 + JO_EXIT_CB + JO_CLOSE_CALLBACK,
Bram Moolenaar37c45832017-08-12 16:01:04 +02002588 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
Bram Moolenaarda43b612017-08-11 22:27:50 +02002589 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002590 + JO2_CWD + JO2_ENV) == FAIL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002591 return;
2592
Bram Moolenaar08d384f2017-08-11 21:51:23 +02002593 if (opt.jo_vertical)
2594 cmdmod.split = WSP_VERT;
Bram Moolenaarda43b612017-08-11 22:27:50 +02002595 term_start(cmd, &opt, FALSE);
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002596
2597 if (curbuf->b_term != NULL)
2598 rettv->vval.v_number = curbuf->b_fnum;
2599}
2600
2601/*
2602 * "term_wait" function
2603 */
2604 void
2605f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
2606{
2607 buf_T *buf = term_get_buf(argvars);
2608
2609 if (buf == NULL)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002610 {
2611 ch_log(NULL, "term_wait(): invalid argument");
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002612 return;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002613 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002614 if (buf->b_term->tl_job == NULL)
2615 {
2616 ch_log(NULL, "term_wait(): no job to wait for");
2617 return;
2618 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002619
2620 /* Get the job status, this will detect a job that finished. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002621 if (STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002622 {
2623 /* The job is dead, keep reading channel I/O until the channel is
2624 * closed. */
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002625 ch_log(NULL, "term_wait(): waiting for channel to close");
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002626 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
2627 {
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002628 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002629 parse_queued_messages();
2630 ui_delay(10L, FALSE);
2631 }
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002632 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002633 parse_queued_messages();
2634 }
2635 else
2636 {
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002637 long wait = 10L;
2638
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002639 mch_check_messages();
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002640 parse_queued_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002641
Bram Moolenaarf3402b12017-08-06 19:07:08 +02002642 /* Wait for some time for any channel I/O. */
2643 if (argvars[1].v_type != VAR_UNKNOWN)
2644 wait = get_tv_number(&argvars[1]);
2645 ui_delay(wait, TRUE);
Bram Moolenaare9c21ae2017-08-03 20:44:48 +02002646 mch_check_messages();
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002647
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002648 /* Flushing messages on channels is hopefully sufficient.
2649 * TODO: is there a better way? */
2650 parse_queued_messages();
2651 }
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002652}
2653
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002654# ifdef WIN3264
2655
Bram Moolenaarc6df10e2017-07-29 20:15:08 +02002656/**************************************
2657 * 2. MS-Windows implementation.
2658 */
2659
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002660#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
2661#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
2662
Bram Moolenaar8a773062017-07-24 22:29:21 +02002663void* (*winpty_config_new)(UINT64, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002664void* (*winpty_open)(void*, void*);
Bram Moolenaar8a773062017-07-24 22:29:21 +02002665void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002666BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
2667void (*winpty_config_set_initial_size)(void*, int, int);
2668LPCWSTR (*winpty_conin_name)(void*);
2669LPCWSTR (*winpty_conout_name)(void*);
2670LPCWSTR (*winpty_conerr_name)(void*);
2671void (*winpty_free)(void*);
2672void (*winpty_config_free)(void*);
2673void (*winpty_spawn_config_free)(void*);
2674void (*winpty_error_free)(void*);
2675LPCWSTR (*winpty_error_msg)(void*);
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002676BOOL (*winpty_set_size)(void*, int, int, void*);
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002677HANDLE (*winpty_agent_process)(void*);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002678
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002679#define WINPTY_DLL "winpty.dll"
2680
2681static HINSTANCE hWinPtyDLL = NULL;
2682
2683 int
2684dyn_winpty_init(void)
2685{
2686 int i;
2687 static struct
2688 {
2689 char *name;
2690 FARPROC *ptr;
2691 } winpty_entry[] =
2692 {
2693 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
2694 {"winpty_config_free", (FARPROC*)&winpty_config_free},
2695 {"winpty_config_new", (FARPROC*)&winpty_config_new},
2696 {"winpty_config_set_initial_size", (FARPROC*)&winpty_config_set_initial_size},
2697 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
2698 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
2699 {"winpty_error_free", (FARPROC*)&winpty_error_free},
2700 {"winpty_free", (FARPROC*)&winpty_free},
2701 {"winpty_open", (FARPROC*)&winpty_open},
2702 {"winpty_spawn", (FARPROC*)&winpty_spawn},
2703 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
2704 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
2705 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002706 {"winpty_set_size", (FARPROC*)&winpty_set_size},
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002707 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002708 {NULL, NULL}
2709 };
2710
2711 /* No need to initialize twice. */
2712 if (hWinPtyDLL)
2713 return 1;
2714 /* Load winpty.dll */
2715 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
2716 if (!hWinPtyDLL)
2717 {
2718 EMSG2(_(e_loadlib), WINPTY_DLL);
2719 return 0;
2720 }
2721 for (i = 0; winpty_entry[i].name != NULL
2722 && winpty_entry[i].ptr != NULL; ++i)
2723 {
2724 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
2725 winpty_entry[i].name)) == NULL)
2726 {
2727 EMSG2(_(e_loadfunc), winpty_entry[i].name);
2728 return 0;
2729 }
2730 }
2731
2732 return 1;
2733}
2734
2735/*
2736 * Create a new terminal of "rows" by "cols" cells.
2737 * Store a reference in "term".
2738 * Return OK or FAIL.
2739 */
2740 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002741term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002742{
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002743 WCHAR *p;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002744 channel_T *channel = NULL;
2745 job_T *job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002746 DWORD error;
2747 HANDLE jo = NULL, child_process_handle, child_thread_handle;
2748 void *winpty_err;
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002749 void *spawn_config = NULL;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002750 char buf[MAX_PATH];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002751
2752 if (!dyn_winpty_init())
2753 return FAIL;
2754
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002755 p = enc_to_utf16(cmd, NULL);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002756 if (p == NULL)
2757 return FAIL;
2758
2759 job = job_alloc();
2760 if (job == NULL)
2761 goto failed;
2762
2763 channel = add_channel();
2764 if (channel == NULL)
2765 goto failed;
2766
2767 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
2768 if (term->tl_winpty_config == NULL)
2769 goto failed;
2770
2771 winpty_config_set_initial_size(term->tl_winpty_config, cols, rows);
2772 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
2773 if (term->tl_winpty == NULL)
2774 goto failed;
2775
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002776 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002777 spawn_config = winpty_spawn_config_new(
2778 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
2779 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
2780 NULL,
2781 p,
2782 NULL,
2783 NULL,
2784 &winpty_err);
2785 if (spawn_config == NULL)
2786 goto failed;
2787
2788 channel = add_channel();
2789 if (channel == NULL)
2790 goto failed;
2791
2792 job = job_alloc();
2793 if (job == NULL)
2794 goto failed;
2795
2796 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
2797 &child_thread_handle, &error, &winpty_err))
2798 goto failed;
2799
2800 channel_set_pipes(channel,
2801 (sock_T) CreateFileW(
2802 winpty_conin_name(term->tl_winpty),
2803 GENERIC_WRITE, 0, NULL,
2804 OPEN_EXISTING, 0, NULL),
2805 (sock_T) CreateFileW(
2806 winpty_conout_name(term->tl_winpty),
2807 GENERIC_READ, 0, NULL,
2808 OPEN_EXISTING, 0, NULL),
2809 (sock_T) CreateFileW(
2810 winpty_conerr_name(term->tl_winpty),
2811 GENERIC_READ, 0, NULL,
2812 OPEN_EXISTING, 0, NULL));
2813
2814 jo = CreateJobObject(NULL, NULL);
2815 if (jo == NULL)
2816 goto failed;
2817
2818 if (!AssignProcessToJobObject(jo, child_process_handle))
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002819 {
2820 /* Failed, switch the way to terminate process with TerminateProcess. */
2821 CloseHandle(jo);
2822 jo = NULL;
2823 }
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002824
2825 winpty_spawn_config_free(spawn_config);
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002826 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002827
2828 create_vterm(term, rows, cols);
2829
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002830 channel_set_job(channel, job, opt);
Bram Moolenaar102dc7f2017-08-03 20:59:29 +02002831 job_set_options(job, opt);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002832
2833 job->jv_channel = channel;
2834 job->jv_proc_info.hProcess = child_process_handle;
2835 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
2836 job->jv_job_object = jo;
2837 job->jv_status = JOB_STARTED;
Bram Moolenaar5be8dd02017-08-03 20:52:19 +02002838 sprintf(buf, "winpty://%lu",
2839 GetProcessId(winpty_agent_process(term->tl_winpty)));
2840 job->jv_tty_name = vim_strsave((char_u*)buf);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002841 ++job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002842 term->tl_job = job;
2843
2844 return OK;
2845
2846failed:
Bram Moolenaarab6eec32017-07-27 21:46:43 +02002847 if (spawn_config != NULL)
2848 winpty_spawn_config_free(spawn_config);
2849 vim_free(p);
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002850 if (channel != NULL)
2851 channel_clear(channel);
2852 if (job != NULL)
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002853 {
2854 job->jv_channel = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002855 job_cleanup(job);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002856 }
2857 term->tl_job = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002858 if (jo != NULL)
2859 CloseHandle(jo);
2860 if (term->tl_winpty != NULL)
2861 winpty_free(term->tl_winpty);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002862 term->tl_winpty = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002863 if (term->tl_winpty_config != NULL)
2864 winpty_config_free(term->tl_winpty_config);
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002865 term->tl_winpty_config = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002866 if (winpty_err != NULL)
2867 {
2868 char_u *msg = utf16_to_enc(
2869 (short_u *)winpty_error_msg(winpty_err), NULL);
2870
2871 EMSG(msg);
2872 winpty_error_free(winpty_err);
2873 }
2874 return FAIL;
2875}
2876
2877/*
2878 * Free the terminal emulator part of "term".
2879 */
2880 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002881term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002882{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002883 if (term->tl_winpty != NULL)
2884 winpty_free(term->tl_winpty);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002885 term->tl_winpty = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002886 if (term->tl_winpty_config != NULL)
2887 winpty_config_free(term->tl_winpty_config);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002888 term->tl_winpty_config = NULL;
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002889 if (term->tl_vterm != NULL)
2890 vterm_free(term->tl_vterm);
Bram Moolenaardcbfa332017-07-28 23:16:13 +02002891 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002892}
2893
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002894/*
2895 * Request size to terminal.
2896 */
2897 static void
2898term_report_winsize(term_T *term, int rows, int cols)
2899{
2900 winpty_set_size(term->tl_winpty, cols, rows, NULL);
2901}
2902
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002903# else
2904
2905/**************************************
2906 * 3. Unix-like implementation.
2907 */
2908
2909/*
2910 * Create a new terminal of "rows" by "cols" cells.
2911 * Start job for "cmd".
2912 * Store the pointers in "term".
2913 * Return OK or FAIL.
2914 */
2915 static int
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002916term_and_job_init(term_T *term, int rows, int cols, char_u *cmd, jobopt_T *opt)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002917{
2918 typval_T argvars[2];
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002919
2920 create_vterm(term, rows, cols);
2921
Bram Moolenaar7c9aec42017-08-03 13:51:25 +02002922 /* TODO: if the command is "NONE" only create a pty. */
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002923 argvars[0].v_type = VAR_STRING;
2924 argvars[0].vval.v_string = cmd;
Bram Moolenaar3c3a80d2017-08-03 17:06:45 +02002925
2926 term->tl_job = job_start(argvars, opt);
Bram Moolenaar0e83f022017-07-27 22:07:35 +02002927 if (term->tl_job != NULL)
2928 ++term->tl_job->jv_refcount;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002929
Bram Moolenaar61a66052017-07-22 18:39:00 +02002930 return term->tl_job != NULL
2931 && term->tl_job->jv_channel != NULL
2932 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002933}
2934
2935/*
2936 * Free the terminal emulator part of "term".
2937 */
2938 static void
Bram Moolenaard85f2712017-07-28 21:51:57 +02002939term_free_vterm(term_T *term)
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002940{
Bram Moolenaarcdeae992017-07-23 17:22:35 +02002941 if (term->tl_vterm != NULL)
2942 vterm_free(term->tl_vterm);
Bram Moolenaard85f2712017-07-28 21:51:57 +02002943 term->tl_vterm = NULL;
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002944}
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002945
2946/*
2947 * Request size to terminal.
2948 */
2949 static void
2950term_report_winsize(term_T *term, int rows, int cols)
2951{
2952 /* Use an ioctl() to report the new window size to the job. */
2953 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
2954 {
2955 int fd = -1;
2956 int part;
2957
2958 for (part = PART_OUT; part < PART_COUNT; ++part)
2959 {
2960 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
2961 if (isatty(fd))
2962 break;
2963 }
2964 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
Bram Moolenaar2d33e902017-08-11 16:31:54 +02002965 mch_signal_job(term->tl_job, (char_u *)"winch");
Bram Moolenaar43da3e32017-07-23 17:27:54 +02002966 }
2967}
2968
Bram Moolenaar8f84c3a2017-07-22 16:14:44 +02002969# endif
Bram Moolenaar8c0095c2017-07-18 22:53:21 +02002970
Bram Moolenaare4f25e42017-07-07 11:54:15 +02002971#endif /* FEAT_TERMINAL */