blob: 09b60726991ba4e3cbd5d6299664f3fb022443cf [file] [log] [blame]
Bram Moolenaar2e6ab182017-09-20 10:03:07 +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 *
13 * There are three parts:
14 * 1. Generic code for all systems.
15 * Uses libvterm for the terminal emulator.
16 * 2. The MS-Windows implementation.
17 * Uses winpty.
18 * 3. The Unix-like implementation.
19 * Uses pseudo-tty's (pty's).
20 *
21 * For each terminal one VTerm is constructed. This uses libvterm. A copy of
22 * this library is in the libvterm directory.
23 *
24 * When a terminal window is opened, a job is started that will be connected to
25 * the terminal emulator.
26 *
27 * If the terminal window has keyboard focus, typed keys are converted to the
28 * terminal encoding and writing to the job over a channel.
29 *
30 * 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.
34 *
35 * 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.
37 * When the buffer is changed it is turned into a normal buffer, the attributes
38 * in tl_scrollback are no longer used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020039 */
40
41#include "vim.h"
42
43#if defined(FEAT_TERMINAL) || defined(PROTO)
44
45#ifndef MIN
46# define MIN(x,y) ((x) < (y) ? (x) : (y))
47#endif
48#ifndef MAX
49# define MAX(x,y) ((x) > (y) ? (x) : (y))
50#endif
51
52#include "libvterm/include/vterm.h"
53
54/* This is VTermScreenCell without the characters, thus much smaller. */
55typedef struct {
56 VTermScreenCellAttrs attrs;
57 char width;
Bram Moolenaard96ff162018-02-18 22:13:29 +010058 VTermColor fg;
59 VTermColor bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020060} cellattr_T;
61
62typedef struct sb_line_S {
63 int sb_cols; /* can differ per line */
64 cellattr_T *sb_cells; /* allocated */
65 cellattr_T sb_fill_attr; /* for short line */
66} sb_line_T;
67
68/* typedef term_T in structs.h */
69struct terminal_S {
70 term_T *tl_next;
71
72 VTerm *tl_vterm;
73 job_T *tl_job;
74 buf_T *tl_buffer;
Bram Moolenaar13568252018-03-16 20:46:58 +010075#if defined(FEAT_GUI)
76 int tl_system; /* when non-zero used for :!cmd output */
77 int tl_toprow; /* row with first line of system terminal */
78#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020079
80 /* Set when setting the size of a vterm, reset after redrawing. */
81 int tl_vterm_size_changed;
82
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020083 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
84 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +020085 int tl_channel_recently_closed; // still need to handle tl_finish
86
Bram Moolenaar1dd98332018-03-16 22:54:53 +010087 int tl_finish;
88#define TL_FINISH_UNSET NUL
89#define TL_FINISH_CLOSE 'c' /* ++close or :terminal without argument */
90#define TL_FINISH_NOCLOSE 'n' /* ++noclose */
91#define TL_FINISH_OPEN 'o' /* ++open */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020092 char_u *tl_opencmd;
93 char_u *tl_eof_chars;
94
95#ifdef WIN3264
96 void *tl_winpty_config;
97 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +020098
99 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200100#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100101#if defined(FEAT_SESSION)
102 char_u *tl_command;
103#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100104 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200105
106 /* last known vterm size */
107 int tl_rows;
108 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200109
110 char_u *tl_title; /* NULL or allocated */
111 char_u *tl_status_text; /* NULL or allocated */
112
113 /* Range of screen rows to update. Zero based. */
Bram Moolenaar3a497e12017-09-30 20:40:27 +0200114 int tl_dirty_row_start; /* MAX_ROW if nothing dirty */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200115 int tl_dirty_row_end; /* row below last one to update */
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200116 int tl_dirty_snapshot; /* text updated after making snapshot */
117#ifdef FEAT_TIMERS
118 int tl_timer_set;
119 proftime_T tl_timer_due;
120#endif
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200121 int tl_postponed_scroll; /* to be scrolled up */
122
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200123 garray_T tl_scrollback;
124 int tl_scrollback_scrolled;
125 cellattr_T tl_default_color;
126
Bram Moolenaard96ff162018-02-18 22:13:29 +0100127 linenr_T tl_top_diff_rows; /* rows of top diff file or zero */
128 linenr_T tl_bot_diff_rows; /* rows of bottom diff file */
129
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200130 VTermPos tl_cursor_pos;
131 int tl_cursor_visible;
132 int tl_cursor_blink;
133 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
134 char_u *tl_cursor_color; /* NULL or allocated */
135
136 int tl_using_altscreen;
137};
138
139#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
140#define TMODE_LOOP 2 /* CTRL-W N used */
141
142/*
143 * List of all active terminals.
144 */
145static term_T *first_term = NULL;
146
147/* Terminal active in terminal_loop(). */
148static term_T *in_terminal_loop = NULL;
149
150#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
151#define KEY_BUF_LEN 200
152
153/*
154 * Functions with separate implementation for MS-Windows and Unix-like systems.
155 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200156static int term_and_job_init(term_T *term, typval_T *argvar, char **argv, jobopt_T *opt, jobopt_T *orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200157static int create_pty_only(term_T *term, jobopt_T *opt);
158static void term_report_winsize(term_T *term, int rows, int cols);
159static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100160#ifdef FEAT_GUI
161static void update_system_term(term_T *term);
162#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200163
Bram Moolenaar26d205d2017-11-09 17:33:11 +0100164/* The character that we know (or assume) that the terminal expects for the
165 * backspace key. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200166static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200167
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100168/* "Terminal" highlight group colors. */
169static int term_default_cterm_fg = -1;
170static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200171
Bram Moolenaard317b382018-02-08 22:33:31 +0100172/* Store the last set and the desired cursor properties, so that we only update
173 * them when needed. Doing it unnecessary may result in flicker. */
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200174static char_u *last_set_cursor_color = NULL;
175static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100176static int last_set_cursor_shape = -1;
177static int desired_cursor_shape = -1;
178static int last_set_cursor_blink = -1;
179static int desired_cursor_blink = -1;
180
181
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200182/**************************************
183 * 1. Generic code for all systems.
184 */
185
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200186 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200187cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
188{
189 if (lhs_color != NULL && rhs_color != NULL)
190 return STRCMP(lhs_color, rhs_color) == 0;
191 return lhs_color == NULL && rhs_color == NULL;
192}
193
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200194 static void
195cursor_color_copy(char_u **to_color, char_u *from_color)
196{
197 // Avoid a free & alloc if the value is already right.
198 if (cursor_color_equal(*to_color, from_color))
199 return;
200 vim_free(*to_color);
201 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
202}
203
204 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200205cursor_color_get(char_u *color)
206{
207 return (color == NULL) ? (char_u *)"" : color;
208}
209
210
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200211/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200212 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200213 * current window.
214 * Sets "rows" and/or "cols" to zero when it should follow the window size.
215 * Return TRUE if the size is the minimum size: "24*80".
216 */
217 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200218parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200219{
220 int minsize = FALSE;
221
222 *rows = 0;
223 *cols = 0;
224
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200225 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200226 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200227 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200228
229 /* Syntax of value was already checked when it's set. */
230 if (p == NULL)
231 {
232 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200233 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200234 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200235 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200236 *cols = atoi((char *)p + 1);
237 }
238 return minsize;
239}
240
241/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200242 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200243 */
244 static void
245set_term_and_win_size(term_T *term)
246{
Bram Moolenaar13568252018-03-16 20:46:58 +0100247#ifdef FEAT_GUI
248 if (term->tl_system)
249 {
250 /* Use the whole screen for the system command. However, it will start
251 * at the command line and scroll up as needed, using tl_toprow. */
252 term->tl_rows = Rows;
253 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200254 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100255 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100256#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200257 if (parse_termwinsize(curwin, &term->tl_rows, &term->tl_cols))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200258 {
Bram Moolenaar498c2562018-04-15 23:45:15 +0200259 if (term->tl_rows != 0)
260 term->tl_rows = MAX(term->tl_rows, curwin->w_height);
261 if (term->tl_cols != 0)
262 term->tl_cols = MAX(term->tl_cols, curwin->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200263 }
264 if (term->tl_rows == 0)
265 term->tl_rows = curwin->w_height;
266 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200267 win_setheight_win(term->tl_rows, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200268 if (term->tl_cols == 0)
269 term->tl_cols = curwin->w_width;
270 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200271 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200272}
273
274/*
275 * Initialize job options for a terminal job.
276 * Caller may overrule some of them.
277 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100278 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200279init_job_options(jobopt_T *opt)
280{
281 clear_job_options(opt);
282
283 opt->jo_mode = MODE_RAW;
284 opt->jo_out_mode = MODE_RAW;
285 opt->jo_err_mode = MODE_RAW;
286 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
287}
288
289/*
290 * Set job options mandatory for a terminal job.
291 */
292 static void
293setup_job_options(jobopt_T *opt, int rows, int cols)
294{
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200295#ifndef WIN3264
296 /* Win32: Redirecting the job output won't work, thus always connect stdout
297 * here. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200298 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200299#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200300 {
301 /* Connect stdout to the terminal. */
302 opt->jo_io[PART_OUT] = JIO_BUFFER;
303 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
304 opt->jo_modifiable[PART_OUT] = 0;
305 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
306 }
307
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200308#ifndef WIN3264
309 /* Win32: Redirecting the job output won't work, thus always connect stderr
310 * here. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200311 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200312#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200313 {
314 /* Connect stderr to the terminal. */
315 opt->jo_io[PART_ERR] = JIO_BUFFER;
316 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
317 opt->jo_modifiable[PART_ERR] = 0;
318 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
319 }
320
321 opt->jo_pty = TRUE;
322 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
323 opt->jo_term_rows = rows;
324 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
325 opt->jo_term_cols = cols;
326}
327
328/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100329 * Close a terminal buffer (and its window). Used when creating the terminal
330 * fails.
331 */
332 static void
333term_close_buffer(buf_T *buf, buf_T *old_curbuf)
334{
335 free_terminal(buf);
336 if (old_curbuf != NULL)
337 {
338 --curbuf->b_nwindows;
339 curbuf = old_curbuf;
340 curwin->w_buffer = curbuf;
341 ++curbuf->b_nwindows;
342 }
343
344 /* Wiping out the buffer will also close the window and call
345 * free_terminal(). */
346 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
347}
348
349/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200350 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100351 * Use either "argvar" or "argv", the other must be NULL.
352 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
353 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200354 * Returns NULL when failed.
355 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100356 buf_T *
357term_start(
358 typval_T *argvar,
359 char **argv,
360 jobopt_T *opt,
361 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200362{
363 exarg_T split_ea;
364 win_T *old_curwin = curwin;
365 term_T *term;
366 buf_T *old_curbuf = NULL;
367 int res;
368 buf_T *newbuf;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100369 int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200370 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200371
372 if (check_restricted() || check_secure())
373 return NULL;
374
375 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
376 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
377 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
378 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF)))
379 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100380 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200381 return NULL;
382 }
383
384 term = (term_T *)alloc_clear(sizeof(term_T));
385 if (term == NULL)
386 return NULL;
387 term->tl_dirty_row_end = MAX_ROW;
388 term->tl_cursor_visible = TRUE;
389 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
390 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100391#ifdef FEAT_GUI
392 term->tl_system = (flags & TERM_START_SYSTEM);
393#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200394 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
395
396 vim_memset(&split_ea, 0, sizeof(split_ea));
397 if (opt->jo_curwin)
398 {
399 /* Create a new buffer in the current window. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100400 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200401 {
402 no_write_message();
403 vim_free(term);
404 return NULL;
405 }
406 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaar13568252018-03-16 20:46:58 +0100407 ECMD_HIDE
408 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
409 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200410 {
411 vim_free(term);
412 return NULL;
413 }
414 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100415 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200416 {
417 buf_T *buf;
418
419 /* Create a new buffer without a window. Make it the current buffer for
420 * a moment to be able to do the initialisations. */
421 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
422 BLN_NEW | BLN_LISTED);
423 if (buf == NULL || ml_open(buf) == FAIL)
424 {
425 vim_free(term);
426 return NULL;
427 }
428 old_curbuf = curbuf;
429 --curbuf->b_nwindows;
430 curbuf = buf;
431 curwin->w_buffer = buf;
432 ++curbuf->b_nwindows;
433 }
434 else
435 {
436 /* Open a new window or tab. */
437 split_ea.cmdidx = CMD_new;
438 split_ea.cmd = (char_u *)"new";
439 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100440 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200441 {
442 split_ea.line2 = opt->jo_term_rows;
443 split_ea.addr_count = 1;
444 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100445 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200446 {
447 split_ea.line2 = opt->jo_term_cols;
448 split_ea.addr_count = 1;
449 }
450
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100451 if (vertical)
452 cmdmod.split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200453 ex_splitview(&split_ea);
454 if (curwin == old_curwin)
455 {
456 /* split failed */
457 vim_free(term);
458 return NULL;
459 }
460 }
461 term->tl_buffer = curbuf;
462 curbuf->b_term = term;
463
464 if (!opt->jo_hidden)
465 {
Bram Moolenaarda650582018-02-20 15:51:40 +0100466 /* Only one size was taken care of with :new, do the other one. With
467 * "curwin" both need to be done. */
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100468 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200469 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100470 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200471 win_setwidth(opt->jo_term_cols);
472 }
473
474 /* Link the new terminal in the list of active terminals. */
475 term->tl_next = first_term;
476 first_term = term;
477
478 if (opt->jo_term_name != NULL)
479 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaar13568252018-03-16 20:46:58 +0100480 else if (argv != NULL)
481 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200482 else
483 {
484 int i;
485 size_t len;
486 char_u *cmd, *p;
487
488 if (argvar->v_type == VAR_STRING)
489 {
490 cmd = argvar->vval.v_string;
491 if (cmd == NULL)
492 cmd = (char_u *)"";
493 else if (STRCMP(cmd, "NONE") == 0)
494 cmd = (char_u *)"pty";
495 }
496 else if (argvar->v_type != VAR_LIST
497 || argvar->vval.v_list == NULL
498 || argvar->vval.v_list->lv_len < 1
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100499 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200500 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
501 cmd = (char_u*)"";
502
503 len = STRLEN(cmd) + 10;
504 p = alloc((int)len);
505
506 for (i = 0; p != NULL; ++i)
507 {
508 /* Prepend a ! to the command name to avoid the buffer name equals
509 * the executable, otherwise ":w!" would overwrite it. */
510 if (i == 0)
511 vim_snprintf((char *)p, len, "!%s", cmd);
512 else
513 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
514 if (buflist_findname(p) == NULL)
515 {
516 vim_free(curbuf->b_ffname);
517 curbuf->b_ffname = p;
518 break;
519 }
520 }
521 }
522 curbuf->b_fname = curbuf->b_ffname;
523
524 if (opt->jo_term_opencmd != NULL)
525 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
526
527 if (opt->jo_eof_chars != NULL)
528 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
529
530 set_string_option_direct((char_u *)"buftype", -1,
531 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200532 // Avoid that 'buftype' is reset when this buffer is entered.
533 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200534
535 /* Mark the buffer as not modifiable. It can only be made modifiable after
536 * the job finished. */
537 curbuf->b_p_ma = FALSE;
538
539 set_term_and_win_size(term);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200540#ifdef WIN3264
541 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
542#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200543 setup_job_options(opt, term->tl_rows, term->tl_cols);
544
Bram Moolenaar13568252018-03-16 20:46:58 +0100545 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100546 return curbuf;
547
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100548#if defined(FEAT_SESSION)
549 /* Remember the command for the session file. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100550 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100551 {
552 term->tl_command = vim_strsave((char_u *)"NONE");
553 }
554 else if (argvar->v_type == VAR_STRING)
555 {
556 char_u *cmd = argvar->vval.v_string;
557
558 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
559 term->tl_command = vim_strsave(cmd);
560 }
561 else if (argvar->v_type == VAR_LIST
562 && argvar->vval.v_list != NULL
563 && argvar->vval.v_list->lv_len > 0)
564 {
565 garray_T ga;
566 listitem_T *item;
567
568 ga_init2(&ga, 1, 100);
569 for (item = argvar->vval.v_list->lv_first;
570 item != NULL; item = item->li_next)
571 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100572 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100573 char_u *p;
574
575 if (s == NULL)
576 break;
577 p = vim_strsave_fnameescape(s, FALSE);
578 if (p == NULL)
579 break;
580 ga_concat(&ga, p);
581 vim_free(p);
582 ga_append(&ga, ' ');
583 }
584 if (item == NULL)
585 {
586 ga_append(&ga, NUL);
587 term->tl_command = ga.ga_data;
588 }
589 else
590 ga_clear(&ga);
591 }
592#endif
593
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100594 if (opt->jo_term_kill != NULL)
595 {
596 char_u *p = skiptowhite(opt->jo_term_kill);
597
598 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
599 }
600
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200601 /* System dependent: setup the vterm and maybe start the job in it. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100602 if (argv == NULL
603 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200604 && argvar->vval.v_string != NULL
605 && STRCMP(argvar->vval.v_string, "NONE") == 0)
606 res = create_pty_only(term, opt);
607 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200608 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200609
610 newbuf = curbuf;
611 if (res == OK)
612 {
613 /* Get and remember the size we ended up with. Update the pty. */
614 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
615 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100616#ifdef FEAT_GUI
617 if (term->tl_system)
618 {
619 /* display first line below typed command */
620 term->tl_toprow = msg_row + 1;
621 term->tl_dirty_row_end = 0;
622 }
623#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200624
625 /* Make sure we don't get stuck on sending keys to the job, it leads to
626 * a deadlock if the job is waiting for Vim to read. */
627 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
628
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200629 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200630 {
631 --curbuf->b_nwindows;
632 curbuf = old_curbuf;
633 curwin->w_buffer = curbuf;
634 ++curbuf->b_nwindows;
635 }
636 }
637 else
638 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100639 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200640 return NULL;
641 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100642
Bram Moolenaar13568252018-03-16 20:46:58 +0100643 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200644 return newbuf;
645}
646
647/*
648 * ":terminal": open a terminal window and execute a job in it.
649 */
650 void
651ex_terminal(exarg_T *eap)
652{
653 typval_T argvar[2];
654 jobopt_T opt;
655 char_u *cmd;
656 char_u *tofree = NULL;
657
658 init_job_options(&opt);
659
660 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100661 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200662 {
663 char_u *p, *ep;
664
665 cmd += 2;
666 p = skiptowhite(cmd);
667 ep = vim_strchr(cmd, '=');
668 if (ep != NULL && ep < p)
669 p = ep;
670
671 if ((int)(p - cmd) == 5 && STRNICMP(cmd, "close", 5) == 0)
672 opt.jo_term_finish = 'c';
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100673 else if ((int)(p - cmd) == 7 && STRNICMP(cmd, "noclose", 7) == 0)
674 opt.jo_term_finish = 'n';
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200675 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "open", 4) == 0)
676 opt.jo_term_finish = 'o';
677 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "curwin", 6) == 0)
678 opt.jo_curwin = 1;
679 else if ((int)(p - cmd) == 6 && STRNICMP(cmd, "hidden", 6) == 0)
680 opt.jo_hidden = 1;
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100681 else if ((int)(p - cmd) == 9 && STRNICMP(cmd, "norestore", 9) == 0)
682 opt.jo_term_norestore = 1;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100683 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "kill", 4) == 0
684 && ep != NULL)
685 {
686 opt.jo_set2 |= JO2_TERM_KILL;
687 opt.jo_term_kill = ep + 1;
688 p = skiptowhite(cmd);
689 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200690 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "rows", 4) == 0
691 && ep != NULL && isdigit(ep[1]))
692 {
693 opt.jo_set2 |= JO2_TERM_ROWS;
694 opt.jo_term_rows = atoi((char *)ep + 1);
695 p = skiptowhite(cmd);
696 }
697 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "cols", 4) == 0
698 && ep != NULL && isdigit(ep[1]))
699 {
700 opt.jo_set2 |= JO2_TERM_COLS;
701 opt.jo_term_cols = atoi((char *)ep + 1);
702 p = skiptowhite(cmd);
703 }
704 else if ((int)(p - cmd) == 3 && STRNICMP(cmd, "eof", 3) == 0
705 && ep != NULL)
706 {
707 char_u *buf = NULL;
708 char_u *keys;
709
710 p = skiptowhite(cmd);
711 *p = NUL;
712 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE);
713 opt.jo_set2 |= JO2_EOF_CHARS;
714 opt.jo_eof_chars = vim_strsave(keys);
715 vim_free(buf);
716 *p = ' ';
717 }
718 else
719 {
720 if (*p)
721 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100722 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100723 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200724 }
725 cmd = skipwhite(p);
726 }
727 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100728 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200729 /* Make a copy of 'shell', an autocommand may change the option. */
730 tofree = cmd = vim_strsave(p_sh);
731
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100732 /* default to close when the shell exits */
733 if (opt.jo_term_finish == NUL)
734 opt.jo_term_finish = 'c';
735 }
736
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200737 if (eap->addr_count > 0)
738 {
739 /* Write lines from current buffer to the job. */
740 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
741 opt.jo_io[PART_IN] = JIO_BUFFER;
742 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
743 opt.jo_in_top = eap->line1;
744 opt.jo_in_bot = eap->line2;
745 }
746
747 argvar[0].v_type = VAR_STRING;
748 argvar[0].vval.v_string = cmd;
749 argvar[1].v_type = VAR_UNKNOWN;
Bram Moolenaar13568252018-03-16 20:46:58 +0100750 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200751 vim_free(tofree);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100752
753theend:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200754 vim_free(opt.jo_eof_chars);
755}
756
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100757#if defined(FEAT_SESSION) || defined(PROTO)
758/*
759 * Write a :terminal command to the session file to restore the terminal in
760 * window "wp".
761 * Return FAIL if writing fails.
762 */
763 int
764term_write_session(FILE *fd, win_T *wp)
765{
766 term_T *term = wp->w_buffer->b_term;
767
768 /* Create the terminal and run the command. This is not without
769 * risk, but let's assume the user only creates a session when this
770 * will be OK. */
771 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
772 term->tl_cols, term->tl_rows) < 0)
773 return FAIL;
774 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
775 return FAIL;
776
777 return put_eol(fd);
778}
779
780/*
781 * Return TRUE if "buf" has a terminal that should be restored.
782 */
783 int
784term_should_restore(buf_T *buf)
785{
786 term_T *term = buf->b_term;
787
788 return term != NULL && (term->tl_command == NULL
789 || STRCMP(term->tl_command, "NONE") != 0);
790}
791#endif
792
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200793/*
794 * Free the scrollback buffer for "term".
795 */
796 static void
797free_scrollback(term_T *term)
798{
799 int i;
800
801 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
802 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
803 ga_clear(&term->tl_scrollback);
804}
805
806/*
807 * Free a terminal and everything it refers to.
808 * Kills the job if there is one.
809 * Called when wiping out a buffer.
810 */
811 void
812free_terminal(buf_T *buf)
813{
814 term_T *term = buf->b_term;
815 term_T *tp;
816
817 if (term == NULL)
818 return;
819 if (first_term == term)
820 first_term = term->tl_next;
821 else
822 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
823 if (tp->tl_next == term)
824 {
825 tp->tl_next = term->tl_next;
826 break;
827 }
828
829 if (term->tl_job != NULL)
830 {
831 if (term->tl_job->jv_status != JOB_ENDED
832 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +0100833 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200834 job_stop(term->tl_job, NULL, "kill");
835 job_unref(term->tl_job);
836 }
837
838 free_scrollback(term);
839
840 term_free_vterm(term);
841 vim_free(term->tl_title);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100842#ifdef FEAT_SESSION
843 vim_free(term->tl_command);
844#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100845 vim_free(term->tl_kill);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200846 vim_free(term->tl_status_text);
847 vim_free(term->tl_opencmd);
848 vim_free(term->tl_eof_chars);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200849#ifdef WIN3264
850 if (term->tl_out_fd != NULL)
851 fclose(term->tl_out_fd);
852#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200853 vim_free(term->tl_cursor_color);
854 vim_free(term);
855 buf->b_term = NULL;
856 if (in_terminal_loop == term)
857 in_terminal_loop = NULL;
858}
859
860/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100861 * Get the part that is connected to the tty. Normally this is PART_IN, but
862 * when writing buffer lines to the job it can be another. This makes it
863 * possible to do "1,5term vim -".
864 */
865 static ch_part_T
866get_tty_part(term_T *term)
867{
868#ifdef UNIX
869 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
870 int i;
871
872 for (i = 0; i < 3; ++i)
873 {
874 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
875
876 if (isatty(fd))
877 return parts[i];
878 }
879#endif
880 return PART_IN;
881}
882
883/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200884 * Write job output "msg[len]" to the vterm.
885 */
886 static void
887term_write_job_output(term_T *term, char_u *msg, size_t len)
888{
889 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100890 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200891
Bram Moolenaar26d205d2017-11-09 17:33:11 +0100892 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200893
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100894 /* flush vterm buffer when vterm responded to control sequence */
895 if (prevlen != vterm_output_get_buffer_current(vterm))
896 {
897 char buf[KEY_BUF_LEN];
898 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
899
900 if (curlen > 0)
901 channel_send(term->tl_job->jv_channel, get_tty_part(term),
902 (char_u *)buf, (int)curlen, NULL);
903 }
904
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200905 /* this invokes the damage callbacks */
906 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
907}
908
909 static void
910update_cursor(term_T *term, int redraw)
911{
912 if (term->tl_normal_mode)
913 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100914#ifdef FEAT_GUI
915 if (term->tl_system)
916 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
917 term->tl_cursor_pos.col);
918 else
919#endif
920 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200921 if (redraw)
922 {
923 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
924 cursor_on();
925 out_flush();
926#ifdef FEAT_GUI
927 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +0100928 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200929 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +0100930 gui_mch_flush();
931 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200932#endif
933 }
934}
935
936/*
937 * Invoked when "msg" output from a job was received. Write it to the terminal
938 * of "buffer".
939 */
940 void
941write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
942{
943 size_t len = STRLEN(msg);
944 term_T *term = buffer->b_term;
945
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200946#ifdef WIN3264
947 /* Win32: Cannot redirect output of the job, intercept it here and write to
948 * the file. */
949 if (term->tl_out_fd != NULL)
950 {
951 ch_log(channel, "Writing %d bytes to output file", (int)len);
952 fwrite(msg, len, 1, term->tl_out_fd);
953 return;
954 }
955#endif
956
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200957 if (term->tl_vterm == NULL)
958 {
959 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
960 return;
961 }
962 ch_log(channel, "writing %d bytes to terminal", (int)len);
963 term_write_job_output(term, msg, len);
964
Bram Moolenaar13568252018-03-16 20:46:58 +0100965#ifdef FEAT_GUI
966 if (term->tl_system)
967 {
968 /* show system output, scrolling up the screen as needed */
969 update_system_term(term);
970 update_cursor(term, TRUE);
971 }
972 else
973#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200974 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
975 * contents, thus no screen update is needed. */
976 if (!term->tl_normal_mode)
977 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +0200978 // Don't use update_screen() when editing the command line, it gets
979 // cleared.
980 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200981 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +0200982 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200983 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +0200984 update_screen(VALID_NO_UPDATE);
Bram Moolenaara10ae5e2018-05-11 20:48:29 +0200985 /* update_screen() can be slow, check the terminal wasn't closed
986 * already */
987 if (buffer == curbuf && curbuf->b_term != NULL)
988 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200989 }
990 else
991 redraw_after_callback(TRUE);
992 }
993}
994
995/*
996 * Send a mouse position and click to the vterm
997 */
998 static int
999term_send_mouse(VTerm *vterm, int button, int pressed)
1000{
1001 VTermModifier mod = VTERM_MOD_NONE;
1002
1003 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
Bram Moolenaar53f81742017-09-22 14:35:51 +02001004 mouse_col - curwin->w_wincol, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001005 if (button != 0)
1006 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001007 return TRUE;
1008}
1009
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001010static int enter_mouse_col = -1;
1011static int enter_mouse_row = -1;
1012
1013/*
1014 * Handle a mouse click, drag or release.
1015 * Return TRUE when a mouse event is sent to the terminal.
1016 */
1017 static int
1018term_mouse_click(VTerm *vterm, int key)
1019{
1020#if defined(FEAT_CLIPBOARD)
1021 /* For modeless selection mouse drag and release events are ignored, unless
1022 * they are preceded with a mouse down event */
1023 static int ignore_drag_release = TRUE;
1024 VTermMouseState mouse_state;
1025
1026 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1027 if (mouse_state.flags == 0)
1028 {
1029 /* Terminal is not using the mouse, use modeless selection. */
1030 switch (key)
1031 {
1032 case K_LEFTDRAG:
1033 case K_LEFTRELEASE:
1034 case K_RIGHTDRAG:
1035 case K_RIGHTRELEASE:
1036 /* Ignore drag and release events when the button-down wasn't
1037 * seen before. */
1038 if (ignore_drag_release)
1039 {
1040 int save_mouse_col, save_mouse_row;
1041
1042 if (enter_mouse_col < 0)
1043 break;
1044
1045 /* mouse click in the window gave us focus, handle that
1046 * click now */
1047 save_mouse_col = mouse_col;
1048 save_mouse_row = mouse_row;
1049 mouse_col = enter_mouse_col;
1050 mouse_row = enter_mouse_row;
1051 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1052 mouse_col = save_mouse_col;
1053 mouse_row = save_mouse_row;
1054 }
1055 /* FALLTHROUGH */
1056 case K_LEFTMOUSE:
1057 case K_RIGHTMOUSE:
1058 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1059 ignore_drag_release = TRUE;
1060 else
1061 ignore_drag_release = FALSE;
1062 /* Should we call mouse_has() here? */
1063 if (clip_star.available)
1064 {
1065 int button, is_click, is_drag;
1066
1067 button = get_mouse_button(KEY2TERMCAP1(key),
1068 &is_click, &is_drag);
1069 if (mouse_model_popup() && button == MOUSE_LEFT
1070 && (mod_mask & MOD_MASK_SHIFT))
1071 {
1072 /* Translate shift-left to right button. */
1073 button = MOUSE_RIGHT;
1074 mod_mask &= ~MOD_MASK_SHIFT;
1075 }
1076 clip_modeless(button, is_click, is_drag);
1077 }
1078 break;
1079
1080 case K_MIDDLEMOUSE:
1081 if (clip_star.available)
1082 insert_reg('*', TRUE);
1083 break;
1084 }
1085 enter_mouse_col = -1;
1086 return FALSE;
1087 }
1088#endif
1089 enter_mouse_col = -1;
1090
1091 switch (key)
1092 {
1093 case K_LEFTMOUSE:
1094 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1095 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1096 case K_LEFTRELEASE:
1097 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1098 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1099 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1100 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1101 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1102 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1103 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1104 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1105 }
1106 return TRUE;
1107}
1108
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001109/*
1110 * Convert typed key "c" into bytes to send to the job.
1111 * Return the number of bytes in "buf".
1112 */
1113 static int
1114term_convert_key(term_T *term, int c, char *buf)
1115{
1116 VTerm *vterm = term->tl_vterm;
1117 VTermKey key = VTERM_KEY_NONE;
1118 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001119 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001120
1121 switch (c)
1122 {
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001123 /* don't use VTERM_KEY_ENTER, it may do an unwanted conversion */
1124
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001125 /* don't use VTERM_KEY_BACKSPACE, it always
1126 * becomes 0x7f DEL */
1127 case K_BS: c = term_backspace_char; break;
1128
1129 case ESC: key = VTERM_KEY_ESCAPE; break;
1130 case K_DEL: key = VTERM_KEY_DEL; break;
1131 case K_DOWN: key = VTERM_KEY_DOWN; break;
1132 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1133 key = VTERM_KEY_DOWN; break;
1134 case K_END: key = VTERM_KEY_END; break;
1135 case K_S_END: mod = VTERM_MOD_SHIFT;
1136 key = VTERM_KEY_END; break;
1137 case K_C_END: mod = VTERM_MOD_CTRL;
1138 key = VTERM_KEY_END; break;
1139 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1140 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1141 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1142 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1143 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1144 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1145 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1146 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1147 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1148 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1149 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1150 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1151 case K_HOME: key = VTERM_KEY_HOME; break;
1152 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1153 key = VTERM_KEY_HOME; break;
1154 case K_C_HOME: mod = VTERM_MOD_CTRL;
1155 key = VTERM_KEY_HOME; break;
1156 case K_INS: key = VTERM_KEY_INS; break;
1157 case K_K0: key = VTERM_KEY_KP_0; break;
1158 case K_K1: key = VTERM_KEY_KP_1; break;
1159 case K_K2: key = VTERM_KEY_KP_2; break;
1160 case K_K3: key = VTERM_KEY_KP_3; break;
1161 case K_K4: key = VTERM_KEY_KP_4; break;
1162 case K_K5: key = VTERM_KEY_KP_5; break;
1163 case K_K6: key = VTERM_KEY_KP_6; break;
1164 case K_K7: key = VTERM_KEY_KP_7; break;
1165 case K_K8: key = VTERM_KEY_KP_8; break;
1166 case K_K9: key = VTERM_KEY_KP_9; break;
1167 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
1168 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
1169 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
1170 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
1171 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
1172 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
1173 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1174 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
1175 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
1176 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
1177 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1178 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1179 case K_LEFT: key = VTERM_KEY_LEFT; break;
1180 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1181 key = VTERM_KEY_LEFT; break;
1182 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1183 key = VTERM_KEY_LEFT; break;
1184 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1185 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1186 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1187 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1188 key = VTERM_KEY_RIGHT; break;
1189 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1190 key = VTERM_KEY_RIGHT; break;
1191 case K_UP: key = VTERM_KEY_UP; break;
1192 case K_S_UP: mod = VTERM_MOD_SHIFT;
1193 key = VTERM_KEY_UP; break;
1194 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001195 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1196 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001197
Bram Moolenaara42ad572017-11-16 13:08:04 +01001198 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1199 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001200 case K_MOUSELEFT: /* TODO */ return 0;
1201 case K_MOUSERIGHT: /* TODO */ return 0;
1202
1203 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001204 case K_LEFTMOUSE_NM:
1205 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001206 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001207 case K_LEFTRELEASE_NM:
1208 case K_MOUSEMOVE:
1209 case K_MIDDLEMOUSE:
1210 case K_MIDDLEDRAG:
1211 case K_MIDDLERELEASE:
1212 case K_RIGHTMOUSE:
1213 case K_RIGHTDRAG:
1214 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1215 return 0;
1216 other = TRUE;
1217 break;
1218
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001219 case K_X1MOUSE: /* TODO */ return 0;
1220 case K_X1DRAG: /* TODO */ return 0;
1221 case K_X1RELEASE: /* TODO */ return 0;
1222 case K_X2MOUSE: /* TODO */ return 0;
1223 case K_X2DRAG: /* TODO */ return 0;
1224 case K_X2RELEASE: /* TODO */ return 0;
1225
1226 case K_IGNORE: return 0;
1227 case K_NOP: return 0;
1228 case K_UNDO: return 0;
1229 case K_HELP: return 0;
1230 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1231 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1232 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1233 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1234 case K_SELECT: return 0;
1235#ifdef FEAT_GUI
1236 case K_VER_SCROLLBAR: return 0;
1237 case K_HOR_SCROLLBAR: return 0;
1238#endif
1239#ifdef FEAT_GUI_TABLINE
1240 case K_TABLINE: return 0;
1241 case K_TABMENU: return 0;
1242#endif
1243#ifdef FEAT_NETBEANS_INTG
1244 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1245#endif
1246#ifdef FEAT_DND
1247 case K_DROP: return 0;
1248#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001249 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001250 case K_PS: vterm_keyboard_start_paste(vterm);
1251 other = TRUE;
1252 break;
1253 case K_PE: vterm_keyboard_end_paste(vterm);
1254 other = TRUE;
1255 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001256 }
1257
1258 /*
1259 * Convert special keys to vterm keys:
1260 * - Write keys to vterm: vterm_keyboard_key()
1261 * - Write output to channel.
1262 * TODO: use mod_mask
1263 */
1264 if (key != VTERM_KEY_NONE)
1265 /* Special key, let vterm convert it. */
1266 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001267 else if (!other)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001268 /* Normal character, let vterm convert it. */
1269 vterm_keyboard_unichar(vterm, c, mod);
1270
1271 /* Read back the converted escape sequence. */
1272 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1273}
1274
1275/*
1276 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001277 * If "check_job_status" is TRUE update the job status.
1278 */
1279 static int
1280term_job_running_check(term_T *term, int check_job_status)
1281{
1282 /* Also consider the job finished when the channel is closed, to avoid a
1283 * race condition when updating the title. */
1284 if (term != NULL
1285 && term->tl_job != NULL
1286 && channel_is_open(term->tl_job->jv_channel))
1287 {
1288 if (check_job_status)
1289 job_status(term->tl_job);
1290 return (term->tl_job->jv_status == JOB_STARTED
1291 || term->tl_job->jv_channel->ch_keep_open);
1292 }
1293 return FALSE;
1294}
1295
1296/*
1297 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001298 */
1299 int
1300term_job_running(term_T *term)
1301{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001302 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001303}
1304
1305/*
1306 * Return TRUE if "term" has an active channel and used ":term NONE".
1307 */
1308 int
1309term_none_open(term_T *term)
1310{
1311 /* Also consider the job finished when the channel is closed, to avoid a
1312 * race condition when updating the title. */
1313 return term != NULL
1314 && term->tl_job != NULL
1315 && channel_is_open(term->tl_job->jv_channel)
1316 && term->tl_job->jv_channel->ch_keep_open;
1317}
1318
1319/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001320 * Used when exiting: kill the job in "buf" if so desired.
1321 * Return OK when the job finished.
1322 * Return FAIL when the job is still running.
1323 */
1324 int
1325term_try_stop_job(buf_T *buf)
1326{
1327 int count;
1328 char *how = (char *)buf->b_term->tl_kill;
1329
1330#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1331 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1332 {
1333 char_u buff[DIALOG_MSG_SIZE];
1334 int ret;
1335
1336 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1337 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1338 if (ret == VIM_YES)
1339 how = "kill";
1340 else if (ret == VIM_CANCEL)
1341 return FAIL;
1342 }
1343#endif
1344 if (how == NULL || *how == NUL)
1345 return FAIL;
1346
1347 job_stop(buf->b_term->tl_job, NULL, how);
1348
1349 /* wait for up to a second for the job to die */
1350 for (count = 0; count < 100; ++count)
1351 {
1352 /* buffer, terminal and job may be cleaned up while waiting */
1353 if (!buf_valid(buf)
1354 || buf->b_term == NULL
1355 || buf->b_term->tl_job == NULL)
1356 return OK;
1357
1358 /* call job_status() to update jv_status */
1359 job_status(buf->b_term->tl_job);
1360 if (buf->b_term->tl_job->jv_status >= JOB_ENDED)
1361 return OK;
1362 ui_delay(10L, FALSE);
1363 mch_check_messages();
1364 parse_queued_messages();
1365 }
1366 return FAIL;
1367}
1368
1369/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001370 * Add the last line of the scrollback buffer to the buffer in the window.
1371 */
1372 static void
1373add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1374{
1375 buf_T *buf = term->tl_buffer;
1376 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1377 linenr_T lnum = buf->b_ml.ml_line_count;
1378
1379#ifdef WIN3264
1380 if (!enc_utf8 && enc_codepage > 0)
1381 {
1382 WCHAR *ret = NULL;
1383 int length = 0;
1384
1385 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1386 &ret, &length);
1387 if (ret != NULL)
1388 {
1389 WideCharToMultiByte_alloc(enc_codepage, 0,
1390 ret, length, (char **)&text, &len, 0, 0);
1391 vim_free(ret);
1392 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1393 vim_free(text);
1394 }
1395 }
1396 else
1397#endif
1398 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1399 if (empty)
1400 {
1401 /* Delete the empty line that was in the empty buffer. */
1402 curbuf = buf;
1403 ml_delete(1, FALSE);
1404 curbuf = curwin->w_buffer;
1405 }
1406}
1407
1408 static void
1409cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1410{
1411 attr->width = cell->width;
1412 attr->attrs = cell->attrs;
1413 attr->fg = cell->fg;
1414 attr->bg = cell->bg;
1415}
1416
1417 static int
1418equal_celattr(cellattr_T *a, cellattr_T *b)
1419{
1420 /* Comparing the colors should be sufficient. */
1421 return a->fg.red == b->fg.red
1422 && a->fg.green == b->fg.green
1423 && a->fg.blue == b->fg.blue
1424 && a->bg.red == b->bg.red
1425 && a->bg.green == b->bg.green
1426 && a->bg.blue == b->bg.blue;
1427}
1428
Bram Moolenaard96ff162018-02-18 22:13:29 +01001429/*
1430 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1431 * line at this position. Otherwise at the end.
1432 */
1433 static int
1434add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1435{
1436 if (ga_grow(&term->tl_scrollback, 1) == OK)
1437 {
1438 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1439 + term->tl_scrollback.ga_len;
1440
1441 if (lnum > 0)
1442 {
1443 int i;
1444
1445 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1446 {
1447 *line = *(line - 1);
1448 --line;
1449 }
1450 }
1451 line->sb_cols = 0;
1452 line->sb_cells = NULL;
1453 line->sb_fill_attr = *fill_attr;
1454 ++term->tl_scrollback.ga_len;
1455 return OK;
1456 }
1457 return FALSE;
1458}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001459
1460/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001461 * Remove the terminal contents from the scrollback and the buffer.
1462 * Used before adding a new scrollback line or updating the buffer for lines
1463 * displayed in the terminal.
1464 */
1465 static void
1466cleanup_scrollback(term_T *term)
1467{
1468 sb_line_T *line;
1469 garray_T *gap;
1470
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001471 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001472 gap = &term->tl_scrollback;
1473 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1474 && gap->ga_len > 0)
1475 {
1476 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1477 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1478 vim_free(line->sb_cells);
1479 --gap->ga_len;
1480 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001481 curbuf = curwin->w_buffer;
1482 if (curbuf == term->tl_buffer)
1483 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001484}
1485
1486/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001487 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001488 */
1489 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001490update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001491{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001492 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001493 int len;
1494 int lines_skipped = 0;
1495 VTermPos pos;
1496 VTermScreenCell cell;
1497 cellattr_T fill_attr, new_fill_attr;
1498 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001499
1500 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1501 "Adding terminal window snapshot to buffer");
1502
1503 /* First remove the lines that were appended before, they might be
1504 * outdated. */
1505 cleanup_scrollback(term);
1506
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001507 screen = vterm_obtain_screen(term->tl_vterm);
1508 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001509 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1510 {
1511 len = 0;
1512 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1513 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1514 && cell.chars[0] != NUL)
1515 {
1516 len = pos.col + 1;
1517 new_fill_attr = term->tl_default_color;
1518 }
1519 else
1520 /* Assume the last attr is the filler attr. */
1521 cell2cellattr(&cell, &new_fill_attr);
1522
1523 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1524 ++lines_skipped;
1525 else
1526 {
1527 while (lines_skipped > 0)
1528 {
1529 /* Line was skipped, add an empty line. */
1530 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001531 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001532 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001533 }
1534
1535 if (len == 0)
1536 p = NULL;
1537 else
1538 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
1539 if ((p != NULL || len == 0)
1540 && ga_grow(&term->tl_scrollback, 1) == OK)
1541 {
1542 garray_T ga;
1543 int width;
1544 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1545 + term->tl_scrollback.ga_len;
1546
1547 ga_init2(&ga, 1, 100);
1548 for (pos.col = 0; pos.col < len; pos.col += width)
1549 {
1550 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1551 {
1552 width = 1;
1553 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
1554 if (ga_grow(&ga, 1) == OK)
1555 ga.ga_len += utf_char2bytes(' ',
1556 (char_u *)ga.ga_data + ga.ga_len);
1557 }
1558 else
1559 {
1560 width = cell.width;
1561
1562 cell2cellattr(&cell, &p[pos.col]);
1563
Bram Moolenaara79fd562018-12-20 20:47:32 +01001564 // Each character can be up to 6 bytes.
1565 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001566 {
1567 int i;
1568 int c;
1569
1570 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1571 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1572 (char_u *)ga.ga_data + ga.ga_len);
1573 }
1574 }
1575 }
1576 line->sb_cols = len;
1577 line->sb_cells = p;
1578 line->sb_fill_attr = new_fill_attr;
1579 fill_attr = new_fill_attr;
1580 ++term->tl_scrollback.ga_len;
1581
1582 if (ga_grow(&ga, 1) == FAIL)
1583 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1584 else
1585 {
1586 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1587 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1588 }
1589 ga_clear(&ga);
1590 }
1591 else
1592 vim_free(p);
1593 }
1594 }
1595
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001596 // Add trailing empty lines.
1597 for (pos.row = term->tl_scrollback.ga_len;
1598 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1599 ++pos.row)
1600 {
1601 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1602 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1603 }
1604
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001605 term->tl_dirty_snapshot = FALSE;
1606#ifdef FEAT_TIMERS
1607 term->tl_timer_set = FALSE;
1608#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001609}
1610
1611/*
1612 * If needed, add the current lines of the terminal to scrollback and to the
1613 * buffer. Called after the job has ended and when switching to
1614 * Terminal-Normal mode.
1615 * When "redraw" is TRUE redraw the windows that show the terminal.
1616 */
1617 static void
1618may_move_terminal_to_buffer(term_T *term, int redraw)
1619{
1620 win_T *wp;
1621
1622 if (term->tl_vterm == NULL)
1623 return;
1624
1625 /* Update the snapshot only if something changes or the buffer does not
1626 * have all the lines. */
1627 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1628 <= term->tl_scrollback_scrolled)
1629 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001630
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001631 /* Obtain the current background color. */
1632 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1633 &term->tl_default_color.fg, &term->tl_default_color.bg);
1634
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001635 if (redraw)
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001636 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001637 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001638 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001639 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001640 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1641 wp->w_cursor.col = 0;
1642 wp->w_valid = 0;
1643 if (wp->w_cursor.lnum >= wp->w_height)
1644 {
1645 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001646
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001647 if (wp->w_topline < min_topline)
1648 wp->w_topline = min_topline;
1649 }
1650 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001651 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001652 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001653}
1654
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001655#if defined(FEAT_TIMERS) || defined(PROTO)
1656/*
1657 * Check if any terminal timer expired. If so, copy text from the terminal to
1658 * the buffer.
1659 * Return the time until the next timer will expire.
1660 */
1661 int
1662term_check_timers(int next_due_arg, proftime_T *now)
1663{
1664 term_T *term;
1665 int next_due = next_due_arg;
1666
1667 for (term = first_term; term != NULL; term = term->tl_next)
1668 {
1669 if (term->tl_timer_set && !term->tl_normal_mode)
1670 {
1671 long this_due = proftime_time_left(&term->tl_timer_due, now);
1672
1673 if (this_due <= 1)
1674 {
1675 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001676 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001677 }
1678 else if (next_due == -1 || next_due > this_due)
1679 next_due = this_due;
1680 }
1681 }
1682
1683 return next_due;
1684}
1685#endif
1686
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001687 static void
1688set_terminal_mode(term_T *term, int normal_mode)
1689{
1690 term->tl_normal_mode = normal_mode;
Bram Moolenaard23a8232018-02-10 18:45:26 +01001691 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001692 if (term->tl_buffer == curbuf)
1693 maketitle();
1694}
1695
1696/*
1697 * Called after the job if finished and Terminal mode is not active:
1698 * Move the vterm contents into the scrollback buffer and free the vterm.
1699 */
1700 static void
1701cleanup_vterm(term_T *term)
1702{
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001703 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001704 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001705 term_free_vterm(term);
1706 set_terminal_mode(term, FALSE);
1707}
1708
1709/*
1710 * Switch from Terminal-Job mode to Terminal-Normal mode.
1711 * Suspends updating the terminal window.
1712 */
1713 static void
1714term_enter_normal_mode(void)
1715{
1716 term_T *term = curbuf->b_term;
1717
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001718 set_terminal_mode(term, TRUE);
1719
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001720 /* Append the current terminal contents to the buffer. */
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001721 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001722
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001723 /* Move the window cursor to the position of the cursor in the
1724 * terminal. */
1725 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1726 + term->tl_cursor_pos.row + 1;
1727 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001728 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1729 coladvance(MAXCOL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001730
1731 /* Display the same lines as in the terminal. */
1732 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1733}
1734
1735/*
1736 * Returns TRUE if the current window contains a terminal and we are in
1737 * Terminal-Normal mode.
1738 */
1739 int
1740term_in_normal_mode(void)
1741{
1742 term_T *term = curbuf->b_term;
1743
1744 return term != NULL && term->tl_normal_mode;
1745}
1746
1747/*
1748 * Switch from Terminal-Normal mode to Terminal-Job mode.
1749 * Restores updating the terminal window.
1750 */
1751 void
1752term_enter_job_mode()
1753{
1754 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001755
1756 set_terminal_mode(term, FALSE);
1757
1758 if (term->tl_channel_closed)
1759 cleanup_vterm(term);
1760 redraw_buf_and_status_later(curbuf, NOT_VALID);
1761}
1762
1763/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001764 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001765 * Note: while waiting a terminal may be closed and freed if the channel is
1766 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001767 */
1768 static int
1769term_vgetc()
1770{
1771 int c;
1772 int save_State = State;
1773
1774 State = TERMINAL;
1775 got_int = FALSE;
1776#ifdef WIN3264
1777 ctrl_break_was_pressed = FALSE;
1778#endif
1779 c = vgetc();
1780 got_int = FALSE;
1781 State = save_State;
1782 return c;
1783}
1784
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001785static int mouse_was_outside = FALSE;
1786
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001787/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001788 * Send keys to terminal.
1789 * Return FAIL when the key needs to be handled in Normal mode.
1790 * Return OK when the key was dropped or sent to the terminal.
1791 */
1792 int
1793send_keys_to_term(term_T *term, int c, int typed)
1794{
1795 char msg[KEY_BUF_LEN];
1796 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001797 int dragging_outside = FALSE;
1798
1799 /* Catch keys that need to be handled as in Normal mode. */
1800 switch (c)
1801 {
1802 case NUL:
1803 case K_ZERO:
1804 if (typed)
1805 stuffcharReadbuff(c);
1806 return FAIL;
1807
Bram Moolenaar231a2db2018-05-06 13:53:50 +02001808 case K_TABLINE:
1809 stuffcharReadbuff(c);
1810 return FAIL;
1811
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001812 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001813 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001814 return FAIL;
1815
1816 case K_LEFTDRAG:
1817 case K_MIDDLEDRAG:
1818 case K_RIGHTDRAG:
1819 case K_X1DRAG:
1820 case K_X2DRAG:
1821 dragging_outside = mouse_was_outside;
1822 /* FALLTHROUGH */
1823 case K_LEFTMOUSE:
1824 case K_LEFTMOUSE_NM:
1825 case K_LEFTRELEASE:
1826 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001827 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001828 case K_MIDDLEMOUSE:
1829 case K_MIDDLERELEASE:
1830 case K_RIGHTMOUSE:
1831 case K_RIGHTRELEASE:
1832 case K_X1MOUSE:
1833 case K_X1RELEASE:
1834 case K_X2MOUSE:
1835 case K_X2RELEASE:
1836
1837 case K_MOUSEUP:
1838 case K_MOUSEDOWN:
1839 case K_MOUSELEFT:
1840 case K_MOUSERIGHT:
1841 if (mouse_row < W_WINROW(curwin)
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001842 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
Bram Moolenaar53f81742017-09-22 14:35:51 +02001843 || mouse_col < curwin->w_wincol
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001844 || mouse_col >= W_ENDCOL(curwin)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001845 || dragging_outside)
1846 {
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001847 /* click or scroll outside the current window or on status line
1848 * or vertical separator */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001849 if (typed)
1850 {
1851 stuffcharReadbuff(c);
1852 mouse_was_outside = TRUE;
1853 }
1854 return FAIL;
1855 }
1856 }
1857 if (typed)
1858 mouse_was_outside = FALSE;
1859
1860 /* Convert the typed key to a sequence of bytes for the job. */
1861 len = term_convert_key(term, c, msg);
1862 if (len > 0)
1863 /* TODO: if FAIL is returned, stop? */
1864 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1865 (char_u *)msg, (int)len, NULL);
1866
1867 return OK;
1868}
1869
1870 static void
1871position_cursor(win_T *wp, VTermPos *pos)
1872{
1873 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
1874 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
1875 wp->w_valid |= (VALID_WCOL|VALID_WROW);
1876}
1877
1878/*
1879 * Handle CTRL-W "": send register contents to the job.
1880 */
1881 static void
1882term_paste_register(int prev_c UNUSED)
1883{
1884 int c;
1885 list_T *l;
1886 listitem_T *item;
1887 long reglen = 0;
1888 int type;
1889
1890#ifdef FEAT_CMDL_INFO
1891 if (add_to_showcmd(prev_c))
1892 if (add_to_showcmd('"'))
1893 out_flush();
1894#endif
1895 c = term_vgetc();
1896#ifdef FEAT_CMDL_INFO
1897 clear_showcmd();
1898#endif
1899 if (!term_use_loop())
1900 /* job finished while waiting for a character */
1901 return;
1902
1903 /* CTRL-W "= prompt for expression to evaluate. */
1904 if (c == '=' && get_expr_register() != '=')
1905 return;
1906 if (!term_use_loop())
1907 /* job finished while waiting for a character */
1908 return;
1909
1910 l = (list_T *)get_reg_contents(c, GREG_LIST);
1911 if (l != NULL)
1912 {
1913 type = get_reg_type(c, &reglen);
1914 for (item = l->lv_first; item != NULL; item = item->li_next)
1915 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01001916 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001917#ifdef WIN3264
1918 char_u *tmp = s;
1919
1920 if (!enc_utf8 && enc_codepage > 0)
1921 {
1922 WCHAR *ret = NULL;
1923 int length = 0;
1924
1925 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
1926 (int)STRLEN(s), &ret, &length);
1927 if (ret != NULL)
1928 {
1929 WideCharToMultiByte_alloc(CP_UTF8, 0,
1930 ret, length, (char **)&s, &length, 0, 0);
1931 vim_free(ret);
1932 }
1933 }
1934#endif
1935 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1936 s, (int)STRLEN(s), NULL);
1937#ifdef WIN3264
1938 if (tmp != s)
1939 vim_free(s);
1940#endif
1941
1942 if (item->li_next != NULL || type == MLINE)
1943 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
1944 (char_u *)"\r", 1, NULL);
1945 }
1946 list_free(l);
1947 }
1948}
1949
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001950/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001951 * Return TRUE when waiting for a character in the terminal, the cursor of the
1952 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001953 */
1954 int
1955terminal_is_active()
1956{
1957 return in_terminal_loop != NULL;
1958}
1959
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001960#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001961 cursorentry_T *
1962term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
1963{
1964 term_T *term = in_terminal_loop;
1965 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02001966 int id;
1967 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968
1969 vim_memset(&entry, 0, sizeof(entry));
1970 entry.shape = entry.mshape =
1971 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
1972 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
1973 SHAPE_BLOCK;
1974 entry.percentage = 20;
1975 if (term->tl_cursor_blink)
1976 {
1977 entry.blinkwait = 700;
1978 entry.blinkon = 400;
1979 entry.blinkoff = 250;
1980 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02001981
1982 /* The "Terminal" highlight group overrules the defaults. */
1983 id = syn_name2id((char_u *)"Terminal");
1984 if (id != 0)
1985 {
1986 syn_id2colors(id, &term_fg, &term_bg);
1987 *fg = term_bg;
1988 }
1989 else
1990 *fg = gui.back_pixel;
1991
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001992 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02001993 {
1994 if (id != 0)
1995 *bg = term_fg;
1996 else
1997 *bg = gui.norm_pixel;
1998 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001999 else
2000 *bg = color_name2handle(term->tl_cursor_color);
2001 entry.name = "n";
2002 entry.used_for = SHAPE_CURSOR;
2003
2004 return &entry;
2005}
2006#endif
2007
Bram Moolenaard317b382018-02-08 22:33:31 +01002008 static void
2009may_output_cursor_props(void)
2010{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002011 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002012 || last_set_cursor_shape != desired_cursor_shape
2013 || last_set_cursor_blink != desired_cursor_blink)
2014 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002015 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002016 last_set_cursor_shape = desired_cursor_shape;
2017 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002018 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002019 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
2020 /* this will restore the initial cursor style, if possible */
2021 ui_cursor_shape_forced(TRUE);
2022 else
2023 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2024 }
2025}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002026
Bram Moolenaard317b382018-02-08 22:33:31 +01002027/*
2028 * Set the cursor color and shape, if not last set to these.
2029 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002030 static void
2031may_set_cursor_props(term_T *term)
2032{
2033#ifdef FEAT_GUI
2034 /* For the GUI the cursor properties are obtained with
2035 * term_get_cursor_shape(). */
2036 if (gui.in_use)
2037 return;
2038#endif
2039 if (in_terminal_loop == term)
2040 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002041 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002042 desired_cursor_shape = term->tl_cursor_shape;
2043 desired_cursor_blink = term->tl_cursor_blink;
2044 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002045 }
2046}
2047
Bram Moolenaard317b382018-02-08 22:33:31 +01002048/*
2049 * Reset the desired cursor properties and restore them when needed.
2050 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002051 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002052prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002053{
2054#ifdef FEAT_GUI
2055 if (gui.in_use)
2056 return;
2057#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002058 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002059 desired_cursor_shape = -1;
2060 desired_cursor_blink = -1;
2061 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002062}
2063
2064/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002065 * Returns TRUE if the current window contains a terminal and we are sending
2066 * keys to the job.
2067 * If "check_job_status" is TRUE update the job status.
2068 */
2069 static int
2070term_use_loop_check(int check_job_status)
2071{
2072 term_T *term = curbuf->b_term;
2073
2074 return term != NULL
2075 && !term->tl_normal_mode
2076 && term->tl_vterm != NULL
2077 && term_job_running_check(term, check_job_status);
2078}
2079
2080/*
2081 * Returns TRUE if the current window contains a terminal and we are sending
2082 * keys to the job.
2083 */
2084 int
2085term_use_loop(void)
2086{
2087 return term_use_loop_check(FALSE);
2088}
2089
2090/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002091 * Called when entering a window with the mouse. If this is a terminal window
2092 * we may want to change state.
2093 */
2094 void
2095term_win_entered()
2096{
2097 term_T *term = curbuf->b_term;
2098
2099 if (term != NULL)
2100 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002101 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002102 {
2103 reset_VIsual_and_resel();
2104 if (State & INSERT)
2105 stop_insert_mode = TRUE;
2106 }
2107 mouse_was_outside = FALSE;
2108 enter_mouse_col = mouse_col;
2109 enter_mouse_row = mouse_row;
2110 }
2111}
2112
2113/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002114 * Wait for input and send it to the job.
2115 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2116 * when there is no more typahead.
2117 * Return when the start of a CTRL-W command is typed or anything else that
2118 * should be handled as a Normal mode command.
2119 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2120 * the terminal was closed.
2121 */
2122 int
2123terminal_loop(int blocking)
2124{
2125 int c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002126 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002127 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002128#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002129 int tty_fd = curbuf->b_term->tl_job->jv_channel
2130 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002131#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002132 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002133
2134 /* Remember the terminal we are sending keys to. However, the terminal
2135 * might be closed while waiting for a character, e.g. typing "exit" in a
2136 * shell and ++close was used. Therefore use curbuf->b_term instead of a
2137 * stored reference. */
2138 in_terminal_loop = curbuf->b_term;
2139
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002140 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002141 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002142 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002143 if (termwinkey == Ctrl_W)
2144 termwinkey = 0;
2145 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002146 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
2147 may_set_cursor_props(curbuf->b_term);
2148
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002149 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002150 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002151#ifdef FEAT_GUI
2152 if (!curbuf->b_term->tl_system)
2153#endif
2154 /* TODO: skip screen update when handling a sequence of keys. */
2155 /* Repeat redrawing in case a message is received while redrawing.
2156 */
2157 while (must_redraw != 0)
2158 if (update_screen(0) == FAIL)
2159 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002160 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002161 /* job finished while redrawing */
2162 break;
2163
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002164 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002165 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002166
2167 c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002168 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002169 {
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002170 /* Job finished while waiting for a character. Push back the
2171 * received character. */
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002172 if (c != K_IGNORE)
2173 vungetc(c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002174 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002175 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002176 if (c == K_IGNORE)
2177 continue;
2178
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002179#ifdef UNIX
2180 /*
2181 * The shell or another program may change the tty settings. Getting
2182 * them for every typed character is a bit of overhead, but it's needed
2183 * for the first character typed, e.g. when Vim starts in a shell.
2184 */
2185 if (isatty(tty_fd))
2186 {
2187 ttyinfo_T info;
2188
2189 /* Get the current backspace character of the pty. */
2190 if (get_tty_info(tty_fd, &info) == OK)
2191 term_backspace_char = info.backspace;
2192 }
2193#endif
2194
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002195#ifdef WIN3264
2196 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2197 * Use CTRL-BREAK to kill the job. */
2198 if (ctrl_break_was_pressed)
2199 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2200#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002201 /* Was either CTRL-W (termwinkey) or CTRL-\ pressed?
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002202 * Not in a system terminal. */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002203 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002204#ifdef FEAT_GUI
2205 && !curbuf->b_term->tl_system
2206#endif
2207 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002208 {
2209 int prev_c = c;
2210
2211#ifdef FEAT_CMDL_INFO
2212 if (add_to_showcmd(c))
2213 out_flush();
2214#endif
2215 c = term_vgetc();
2216#ifdef FEAT_CMDL_INFO
2217 clear_showcmd();
2218#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002219 if (!term_use_loop_check(TRUE)
2220 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002221 /* job finished while waiting for a character */
2222 break;
2223
2224 if (prev_c == Ctrl_BSL)
2225 {
2226 if (c == Ctrl_N)
2227 {
2228 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
2229 term_enter_normal_mode();
2230 ret = FAIL;
2231 goto theend;
2232 }
2233 /* Send both keys to the terminal. */
2234 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
2235 }
2236 else if (c == Ctrl_C)
2237 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002238 /* "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002239 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2240 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002241 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002242 {
2243 /* "CTRL-W .": send CTRL-W to the job */
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002244 /* "'termwinkey' .": send 'termwinkey' to the job */
2245 c = termwinkey == 0 ? Ctrl_W : termwinkey;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002246 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002247 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002248 {
2249 /* "CTRL-W CTRL-\": send CTRL-\ to the job */
2250 c = Ctrl_BSL;
2251 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002252 else if (c == 'N')
2253 {
2254 /* CTRL-W N : go to Terminal-Normal mode. */
2255 term_enter_normal_mode();
2256 ret = FAIL;
2257 goto theend;
2258 }
2259 else if (c == '"')
2260 {
2261 term_paste_register(prev_c);
2262 continue;
2263 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002264 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002265 {
2266 stuffcharReadbuff(Ctrl_W);
2267 stuffcharReadbuff(c);
2268 ret = OK;
2269 goto theend;
2270 }
2271 }
2272# ifdef WIN3264
2273 if (!enc_utf8 && has_mbyte && c >= 0x80)
2274 {
2275 WCHAR wc;
2276 char_u mb[3];
2277
2278 mb[0] = (unsigned)c >> 8;
2279 mb[1] = c;
2280 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
2281 c = wc;
2282 }
2283# endif
2284 if (send_keys_to_term(curbuf->b_term, c, TRUE) != OK)
2285 {
Bram Moolenaard317b382018-02-08 22:33:31 +01002286 if (c == K_MOUSEMOVE)
2287 /* We are sure to come back here, don't reset the cursor color
2288 * and shape to avoid flickering. */
2289 restore_cursor = FALSE;
2290
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002291 ret = OK;
2292 goto theend;
2293 }
2294 }
2295 ret = FAIL;
2296
2297theend:
2298 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002299 if (restore_cursor)
2300 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002301
2302 /* Move a snapshot of the screen contents to the buffer, so that completion
2303 * works in other buffers. */
Bram Moolenaar620020e2018-05-13 19:06:12 +02002304 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2305 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002306
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002307 return ret;
2308}
2309
2310/*
2311 * Called when a job has finished.
2312 * This updates the title and status, but does not close the vterm, because
2313 * there might still be pending output in the channel.
2314 */
2315 void
2316term_job_ended(job_T *job)
2317{
2318 term_T *term;
2319 int did_one = FALSE;
2320
2321 for (term = first_term; term != NULL; term = term->tl_next)
2322 if (term->tl_job == job)
2323 {
Bram Moolenaard23a8232018-02-10 18:45:26 +01002324 VIM_CLEAR(term->tl_title);
2325 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002326 redraw_buf_and_status_later(term->tl_buffer, VALID);
2327 did_one = TRUE;
2328 }
2329 if (did_one)
2330 redraw_statuslines();
2331 if (curbuf->b_term != NULL)
2332 {
2333 if (curbuf->b_term->tl_job == job)
2334 maketitle();
2335 update_cursor(curbuf->b_term, TRUE);
2336 }
2337}
2338
2339 static void
2340may_toggle_cursor(term_T *term)
2341{
2342 if (in_terminal_loop == term)
2343 {
2344 if (term->tl_cursor_visible)
2345 cursor_on();
2346 else
2347 cursor_off();
2348 }
2349}
2350
2351/*
2352 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002353 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002354 */
2355 static int
2356color2index(VTermColor *color, int fg, int *boldp)
2357{
2358 int red = color->red;
2359 int blue = color->blue;
2360 int green = color->green;
2361
Bram Moolenaar46359e12017-11-29 22:33:38 +01002362 if (color->ansi_index != VTERM_ANSI_INDEX_NONE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002363 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002364 /* First 16 colors and default: use the ANSI index, because these
2365 * colors can be redefined. */
2366 if (t_colors >= 16)
2367 return color->ansi_index;
2368 switch (color->ansi_index)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002369 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002370 case 0: return 0;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01002371 case 1: return lookup_color( 0, fg, boldp) + 1; /* black */
Bram Moolenaar46359e12017-11-29 22:33:38 +01002372 case 2: return lookup_color( 4, fg, boldp) + 1; /* dark red */
2373 case 3: return lookup_color( 2, fg, boldp) + 1; /* dark green */
2374 case 4: return lookup_color( 6, fg, boldp) + 1; /* brown */
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002375 case 5: return lookup_color( 1, fg, boldp) + 1; /* dark blue */
Bram Moolenaar46359e12017-11-29 22:33:38 +01002376 case 6: return lookup_color( 5, fg, boldp) + 1; /* dark magenta */
2377 case 7: return lookup_color( 3, fg, boldp) + 1; /* dark cyan */
2378 case 8: return lookup_color( 8, fg, boldp) + 1; /* light grey */
2379 case 9: return lookup_color(12, fg, boldp) + 1; /* dark grey */
2380 case 10: return lookup_color(20, fg, boldp) + 1; /* red */
2381 case 11: return lookup_color(16, fg, boldp) + 1; /* green */
2382 case 12: return lookup_color(24, fg, boldp) + 1; /* yellow */
2383 case 13: return lookup_color(14, fg, boldp) + 1; /* blue */
2384 case 14: return lookup_color(22, fg, boldp) + 1; /* magenta */
2385 case 15: return lookup_color(18, fg, boldp) + 1; /* cyan */
2386 case 16: return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002387 }
2388 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002389
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002390 if (t_colors >= 256)
2391 {
2392 if (red == blue && red == green)
2393 {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002394 /* 24-color greyscale plus white and black */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002395 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002396 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2397 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2398 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002399 int i;
2400
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002401 if (red < 5)
2402 return 17; /* 00/00/00 */
2403 if (red > 245) /* ff/ff/ff */
2404 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002405 for (i = 0; i < 23; ++i)
2406 if (red < cutoff[i])
2407 return i + 233;
2408 return 256;
2409 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002410 {
2411 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2412 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002413
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002414 /* 216-color cube */
2415 for (ri = 0; ri < 5; ++ri)
2416 if (red < cutoff[ri])
2417 break;
2418 for (gi = 0; gi < 5; ++gi)
2419 if (green < cutoff[gi])
2420 break;
2421 for (bi = 0; bi < 5; ++bi)
2422 if (blue < cutoff[bi])
2423 break;
2424 return 17 + ri * 36 + gi * 6 + bi;
2425 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002426 }
2427 return 0;
2428}
2429
2430/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002431 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002432 */
2433 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002434vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002435{
2436 int attr = 0;
2437
2438 if (cellattrs.bold)
2439 attr |= HL_BOLD;
2440 if (cellattrs.underline)
2441 attr |= HL_UNDERLINE;
2442 if (cellattrs.italic)
2443 attr |= HL_ITALIC;
2444 if (cellattrs.strike)
2445 attr |= HL_STRIKETHROUGH;
2446 if (cellattrs.reverse)
2447 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002448 return attr;
2449}
2450
2451/*
2452 * Store Vterm attributes in "cell" from highlight flags.
2453 */
2454 static void
2455hl2vtermAttr(int attr, cellattr_T *cell)
2456{
2457 vim_memset(&cell->attrs, 0, sizeof(VTermScreenCellAttrs));
2458 if (attr & HL_BOLD)
2459 cell->attrs.bold = 1;
2460 if (attr & HL_UNDERLINE)
2461 cell->attrs.underline = 1;
2462 if (attr & HL_ITALIC)
2463 cell->attrs.italic = 1;
2464 if (attr & HL_STRIKETHROUGH)
2465 cell->attrs.strike = 1;
2466 if (attr & HL_INVERSE)
2467 cell->attrs.reverse = 1;
2468}
2469
2470/*
2471 * Convert the attributes of a vterm cell into an attribute index.
2472 */
2473 static int
2474cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
2475{
2476 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002477
2478#ifdef FEAT_GUI
2479 if (gui.in_use)
2480 {
2481 guicolor_T fg, bg;
2482
2483 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2484 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2485 return get_gui_attr_idx(attr, fg, bg);
2486 }
2487 else
2488#endif
2489#ifdef FEAT_TERMGUICOLORS
2490 if (p_tgc)
2491 {
2492 guicolor_T fg, bg;
2493
2494 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2495 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2496
2497 return get_tgc_attr_idx(attr, fg, bg);
2498 }
2499 else
2500#endif
2501 {
2502 int bold = MAYBE;
2503 int fg = color2index(&cellfg, TRUE, &bold);
2504 int bg = color2index(&cellbg, FALSE, &bold);
2505
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002506 /* Use the "Terminal" highlighting for the default colors. */
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002507 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002508 {
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002509 if (fg == 0 && term_default_cterm_fg >= 0)
2510 fg = term_default_cterm_fg + 1;
2511 if (bg == 0 && term_default_cterm_bg >= 0)
2512 bg = term_default_cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002513 }
2514
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002515 /* with 8 colors set the bold attribute to get a bright foreground */
2516 if (bold == TRUE)
2517 attr |= HL_BOLD;
2518 return get_cterm_attr_idx(attr, fg, bg);
2519 }
2520 return 0;
2521}
2522
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002523 static void
2524set_dirty_snapshot(term_T *term)
2525{
2526 term->tl_dirty_snapshot = TRUE;
2527#ifdef FEAT_TIMERS
2528 if (!term->tl_normal_mode)
2529 {
2530 /* Update the snapshot after 100 msec of not getting updates. */
2531 profile_setlimit(100L, &term->tl_timer_due);
2532 term->tl_timer_set = TRUE;
2533 }
2534#endif
2535}
2536
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002537 static int
2538handle_damage(VTermRect rect, void *user)
2539{
2540 term_T *term = (term_T *)user;
2541
2542 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2543 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002544 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002545 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002546 return 1;
2547}
2548
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002549 static void
2550term_scroll_up(term_T *term, int start_row, int count)
2551{
2552 win_T *wp;
2553 VTermColor fg, bg;
2554 VTermScreenCellAttrs attr;
2555 int clear_attr;
2556
2557 /* Set the color to clear lines with. */
2558 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2559 &fg, &bg);
2560 vim_memset(&attr, 0, sizeof(attr));
2561 clear_attr = cell2attr(attr, fg, bg);
2562
2563 FOR_ALL_WINDOWS(wp)
2564 {
2565 if (wp->w_buffer == term->tl_buffer)
2566 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
2567 }
2568}
2569
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002570 static int
2571handle_moverect(VTermRect dest, VTermRect src, void *user)
2572{
2573 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002574 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002575
2576 /* Scrolling up is done much more efficiently by deleting lines instead of
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002577 * redrawing the text. But avoid doing this multiple times, postpone until
2578 * the redraw happens. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002579 if (dest.start_col == src.start_col
2580 && dest.end_col == src.end_col
2581 && dest.start_row < src.start_row)
2582 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002583 if (dest.start_row == 0)
2584 term->tl_postponed_scroll += count;
2585 else
2586 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002587 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002588
2589 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2590 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002591 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002592
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002593 /* Note sure if the scrolling will work correctly, let's do a complete
2594 * redraw later. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002595 redraw_buf_later(term->tl_buffer, NOT_VALID);
2596 return 1;
2597}
2598
2599 static int
2600handle_movecursor(
2601 VTermPos pos,
2602 VTermPos oldpos UNUSED,
2603 int visible,
2604 void *user)
2605{
2606 term_T *term = (term_T *)user;
2607 win_T *wp;
2608
2609 term->tl_cursor_pos = pos;
2610 term->tl_cursor_visible = visible;
2611
2612 FOR_ALL_WINDOWS(wp)
2613 {
2614 if (wp->w_buffer == term->tl_buffer)
2615 position_cursor(wp, &pos);
2616 }
2617 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
2618 {
2619 may_toggle_cursor(term);
2620 update_cursor(term, term->tl_cursor_visible);
2621 }
2622
2623 return 1;
2624}
2625
2626 static int
2627handle_settermprop(
2628 VTermProp prop,
2629 VTermValue *value,
2630 void *user)
2631{
2632 term_T *term = (term_T *)user;
2633
2634 switch (prop)
2635 {
2636 case VTERM_PROP_TITLE:
2637 vim_free(term->tl_title);
2638 /* a blank title isn't useful, make it empty, so that "running" is
2639 * displayed */
2640 if (*skipwhite((char_u *)value->string) == NUL)
2641 term->tl_title = NULL;
2642#ifdef WIN3264
2643 else if (!enc_utf8 && enc_codepage > 0)
2644 {
2645 WCHAR *ret = NULL;
2646 int length = 0;
2647
2648 MultiByteToWideChar_alloc(CP_UTF8, 0,
2649 (char*)value->string, (int)STRLEN(value->string),
2650 &ret, &length);
2651 if (ret != NULL)
2652 {
2653 WideCharToMultiByte_alloc(enc_codepage, 0,
2654 ret, length, (char**)&term->tl_title,
2655 &length, 0, 0);
2656 vim_free(ret);
2657 }
2658 }
2659#endif
2660 else
2661 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002662 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002663 if (term == curbuf->b_term)
2664 maketitle();
2665 break;
2666
2667 case VTERM_PROP_CURSORVISIBLE:
2668 term->tl_cursor_visible = value->boolean;
2669 may_toggle_cursor(term);
2670 out_flush();
2671 break;
2672
2673 case VTERM_PROP_CURSORBLINK:
2674 term->tl_cursor_blink = value->boolean;
2675 may_set_cursor_props(term);
2676 break;
2677
2678 case VTERM_PROP_CURSORSHAPE:
2679 term->tl_cursor_shape = value->number;
2680 may_set_cursor_props(term);
2681 break;
2682
2683 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002684 cursor_color_copy(&term->tl_cursor_color, (char_u*)value->string);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002685 may_set_cursor_props(term);
2686 break;
2687
2688 case VTERM_PROP_ALTSCREEN:
2689 /* TODO: do anything else? */
2690 term->tl_using_altscreen = value->boolean;
2691 break;
2692
2693 default:
2694 break;
2695 }
2696 /* Always return 1, otherwise vterm doesn't store the value internally. */
2697 return 1;
2698}
2699
2700/*
2701 * The job running in the terminal resized the terminal.
2702 */
2703 static int
2704handle_resize(int rows, int cols, void *user)
2705{
2706 term_T *term = (term_T *)user;
2707 win_T *wp;
2708
2709 term->tl_rows = rows;
2710 term->tl_cols = cols;
2711 if (term->tl_vterm_size_changed)
2712 /* Size was set by vterm_set_size(), don't set the window size. */
2713 term->tl_vterm_size_changed = FALSE;
2714 else
2715 {
2716 FOR_ALL_WINDOWS(wp)
2717 {
2718 if (wp->w_buffer == term->tl_buffer)
2719 {
2720 win_setheight_win(rows, wp);
2721 win_setwidth_win(cols, wp);
2722 }
2723 }
2724 redraw_buf_later(term->tl_buffer, NOT_VALID);
2725 }
2726 return 1;
2727}
2728
2729/*
2730 * Handle a line that is pushed off the top of the screen.
2731 */
2732 static int
2733handle_pushline(int cols, const VTermScreenCell *cells, void *user)
2734{
2735 term_T *term = (term_T *)user;
2736
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002737 /* First remove the lines that were appended before, the pushed line goes
2738 * above it. */
2739 cleanup_scrollback(term);
2740
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002741 /* If the number of lines that are stored goes over 'termscrollback' then
2742 * delete the first 10%. */
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002743 if (term->tl_scrollback.ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002744 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002745 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002746 int i;
2747
2748 curbuf = term->tl_buffer;
2749 for (i = 0; i < todo; ++i)
2750 {
2751 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
2752 ml_delete(1, FALSE);
2753 }
2754 curbuf = curwin->w_buffer;
2755
2756 term->tl_scrollback.ga_len -= todo;
2757 mch_memmove(term->tl_scrollback.ga_data,
2758 (sb_line_T *)term->tl_scrollback.ga_data + todo,
2759 sizeof(sb_line_T) * term->tl_scrollback.ga_len);
Bram Moolenaar4d6cd292018-05-15 23:53:26 +02002760 term->tl_scrollback_scrolled -= todo;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002761 }
2762
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002763 if (ga_grow(&term->tl_scrollback, 1) == OK)
2764 {
2765 cellattr_T *p = NULL;
2766 int len = 0;
2767 int i;
2768 int c;
2769 int col;
2770 sb_line_T *line;
2771 garray_T ga;
2772 cellattr_T fill_attr = term->tl_default_color;
2773
2774 /* do not store empty cells at the end */
2775 for (i = 0; i < cols; ++i)
2776 if (cells[i].chars[0] != 0)
2777 len = i + 1;
2778 else
2779 cell2cellattr(&cells[i], &fill_attr);
2780
2781 ga_init2(&ga, 1, 100);
2782 if (len > 0)
2783 p = (cellattr_T *)alloc((int)sizeof(cellattr_T) * len);
2784 if (p != NULL)
2785 {
2786 for (col = 0; col < len; col += cells[col].width)
2787 {
2788 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
2789 {
2790 ga.ga_len = 0;
2791 break;
2792 }
2793 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
2794 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
2795 (char_u *)ga.ga_data + ga.ga_len);
2796 cell2cellattr(&cells[col], &p[col]);
2797 }
2798 }
2799 if (ga_grow(&ga, 1) == FAIL)
2800 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
2801 else
2802 {
2803 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
2804 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
2805 }
2806 ga_clear(&ga);
2807
2808 line = (sb_line_T *)term->tl_scrollback.ga_data
2809 + term->tl_scrollback.ga_len;
2810 line->sb_cols = len;
2811 line->sb_cells = p;
2812 line->sb_fill_attr = fill_attr;
2813 ++term->tl_scrollback.ga_len;
2814 ++term->tl_scrollback_scrolled;
2815 }
2816 return 0; /* ignored */
2817}
2818
2819static VTermScreenCallbacks screen_callbacks = {
2820 handle_damage, /* damage */
2821 handle_moverect, /* moverect */
2822 handle_movecursor, /* movecursor */
2823 handle_settermprop, /* settermprop */
2824 NULL, /* bell */
2825 handle_resize, /* resize */
2826 handle_pushline, /* sb_pushline */
2827 NULL /* sb_popline */
2828};
2829
2830/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002831 * Do the work after the channel of a terminal was closed.
2832 * Must be called only when updating_screen is FALSE.
2833 * Returns TRUE when a buffer was closed (list of terminals may have changed).
2834 */
2835 static int
2836term_after_channel_closed(term_T *term)
2837{
2838 /* Unless in Terminal-Normal mode: clear the vterm. */
2839 if (!term->tl_normal_mode)
2840 {
2841 int fnum = term->tl_buffer->b_fnum;
2842
2843 cleanup_vterm(term);
2844
2845 if (term->tl_finish == TL_FINISH_CLOSE)
2846 {
2847 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002848 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002849
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002850 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002851 ch_log(NULL, "terminal job finished, closing window");
2852 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002853 // Avoid closing the window if we temporarily use it.
2854 if (do_set_w_closing)
2855 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002856 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02002857 if (do_set_w_closing)
2858 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002859 aucmd_restbuf(&aco);
2860 return TRUE;
2861 }
2862 if (term->tl_finish == TL_FINISH_OPEN
2863 && term->tl_buffer->b_nwindows == 0)
2864 {
2865 char buf[50];
2866
2867 /* TODO: use term_opencmd */
2868 ch_log(NULL, "terminal job finished, opening window");
2869 vim_snprintf(buf, sizeof(buf),
2870 term->tl_opencmd == NULL
2871 ? "botright sbuf %d"
2872 : (char *)term->tl_opencmd, fnum);
2873 do_cmdline_cmd((char_u *)buf);
2874 }
2875 else
2876 ch_log(NULL, "terminal job finished");
2877 }
2878
2879 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
2880 return FALSE;
2881}
2882
2883/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002884 * Called when a channel has been closed.
2885 * If this was a channel for a terminal window then finish it up.
2886 */
2887 void
2888term_channel_closed(channel_T *ch)
2889{
2890 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002891 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002892 int did_one = FALSE;
2893
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002894 for (term = first_term; term != NULL; term = next_term)
2895 {
2896 next_term = term->tl_next;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002897 if (term->tl_job == ch->ch_job)
2898 {
2899 term->tl_channel_closed = TRUE;
2900 did_one = TRUE;
2901
Bram Moolenaard23a8232018-02-10 18:45:26 +01002902 VIM_CLEAR(term->tl_title);
2903 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar402c8392018-05-06 22:01:42 +02002904#ifdef WIN3264
2905 if (term->tl_out_fd != NULL)
2906 {
2907 fclose(term->tl_out_fd);
2908 term->tl_out_fd = NULL;
2909 }
2910#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002911
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002912 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002913 {
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002914 /* Cannot open or close windows now. Can happen when
2915 * 'lazyredraw' is set. */
2916 term->tl_channel_recently_closed = TRUE;
2917 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002918 }
2919
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002920 if (term_after_channel_closed(term))
2921 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002922 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002923 }
2924
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002925 if (did_one)
2926 {
2927 redraw_statuslines();
2928
2929 /* Need to break out of vgetc(). */
2930 ins_char_typebuf(K_IGNORE);
2931 typebuf_was_filled = TRUE;
2932
2933 term = curbuf->b_term;
2934 if (term != NULL)
2935 {
2936 if (term->tl_job == ch->ch_job)
2937 maketitle();
2938 update_cursor(term, term->tl_cursor_visible);
2939 }
2940 }
2941}
2942
2943/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02002944 * To be called after resetting updating_screen: handle any terminal where the
2945 * channel was closed.
2946 */
2947 void
2948term_check_channel_closed_recently()
2949{
2950 term_T *term;
2951 term_T *next_term;
2952
2953 for (term = first_term; term != NULL; term = next_term)
2954 {
2955 next_term = term->tl_next;
2956 if (term->tl_channel_recently_closed)
2957 {
2958 term->tl_channel_recently_closed = FALSE;
2959 if (term_after_channel_closed(term))
2960 // start over, the list may have changed
2961 next_term = first_term;
2962 }
2963 }
2964}
2965
2966/*
Bram Moolenaar13568252018-03-16 20:46:58 +01002967 * Fill one screen line from a line of the terminal.
2968 * Advances "pos" to past the last column.
2969 */
2970 static void
2971term_line2screenline(VTermScreen *screen, VTermPos *pos, int max_col)
2972{
2973 int off = screen_get_current_line_off();
2974
2975 for (pos->col = 0; pos->col < max_col; )
2976 {
2977 VTermScreenCell cell;
2978 int c;
2979
2980 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
2981 vim_memset(&cell, 0, sizeof(cell));
2982
2983 c = cell.chars[0];
2984 if (c == NUL)
2985 {
2986 ScreenLines[off] = ' ';
2987 if (enc_utf8)
2988 ScreenLinesUC[off] = NUL;
2989 }
2990 else
2991 {
2992 if (enc_utf8)
2993 {
2994 int i;
2995
2996 /* composing chars */
2997 for (i = 0; i < Screen_mco
2998 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
2999 {
3000 ScreenLinesC[i][off] = cell.chars[i + 1];
3001 if (cell.chars[i + 1] == 0)
3002 break;
3003 }
3004 if (c >= 0x80 || (Screen_mco > 0
3005 && ScreenLinesC[0][off] != 0))
3006 {
3007 ScreenLines[off] = ' ';
3008 ScreenLinesUC[off] = c;
3009 }
3010 else
3011 {
3012 ScreenLines[off] = c;
3013 ScreenLinesUC[off] = NUL;
3014 }
3015 }
3016#ifdef WIN3264
3017 else if (has_mbyte && c >= 0x80)
3018 {
3019 char_u mb[MB_MAXBYTES+1];
3020 WCHAR wc = c;
3021
3022 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3023 (char*)mb, 2, 0, 0) > 1)
3024 {
3025 ScreenLines[off] = mb[0];
3026 ScreenLines[off + 1] = mb[1];
3027 cell.width = mb_ptr2cells(mb);
3028 }
3029 else
3030 ScreenLines[off] = c;
3031 }
3032#endif
3033 else
3034 ScreenLines[off] = c;
3035 }
3036 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
3037
3038 ++pos->col;
3039 ++off;
3040 if (cell.width == 2)
3041 {
3042 if (enc_utf8)
3043 ScreenLinesUC[off] = NUL;
3044
3045 /* don't set the second byte to NUL for a DBCS encoding, it
3046 * has been set above */
3047 if (enc_utf8 || !has_mbyte)
3048 ScreenLines[off] = NUL;
3049
3050 ++pos->col;
3051 ++off;
3052 }
3053 }
3054}
3055
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003056#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003057 static void
3058update_system_term(term_T *term)
3059{
3060 VTermPos pos;
3061 VTermScreen *screen;
3062
3063 if (term->tl_vterm == NULL)
3064 return;
3065 screen = vterm_obtain_screen(term->tl_vterm);
3066
3067 /* Scroll up to make more room for terminal lines if needed. */
3068 while (term->tl_toprow > 0
3069 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3070 {
3071 int save_p_more = p_more;
3072
3073 p_more = FALSE;
3074 msg_row = Rows - 1;
3075 msg_puts((char_u *)"\n");
3076 p_more = save_p_more;
3077 --term->tl_toprow;
3078 }
3079
3080 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3081 && pos.row < Rows; ++pos.row)
3082 {
3083 if (pos.row < term->tl_rows)
3084 {
3085 int max_col = MIN(Columns, term->tl_cols);
3086
3087 term_line2screenline(screen, &pos, max_col);
3088 }
3089 else
3090 pos.col = 0;
3091
3092 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, FALSE);
3093 }
3094
3095 term->tl_dirty_row_start = MAX_ROW;
3096 term->tl_dirty_row_end = 0;
3097 update_cursor(term, TRUE);
3098}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003099#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003100
3101/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003102 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3103 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003104 * Terminal-Normal mode.
3105 */
3106 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003107term_do_update_window(win_T *wp)
3108{
3109 term_T *term = wp->w_buffer->b_term;
3110
3111 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3112}
3113
3114/*
3115 * Called to update a window that contains an active terminal.
3116 */
3117 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003118term_update_window(win_T *wp)
3119{
3120 term_T *term = wp->w_buffer->b_term;
3121 VTerm *vterm;
3122 VTermScreen *screen;
3123 VTermState *state;
3124 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003125 int rows, cols;
3126 int newrows, newcols;
3127 int minsize;
3128 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003129
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003130 vterm = term->tl_vterm;
3131 screen = vterm_obtain_screen(vterm);
3132 state = vterm_obtain_state(vterm);
3133
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003134 /* We use NOT_VALID on a resize or scroll, redraw everything then. With
3135 * SOME_VALID only redraw what was marked dirty. */
3136 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003137 {
3138 term->tl_dirty_row_start = 0;
3139 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003140
3141 if (term->tl_postponed_scroll > 0
3142 && term->tl_postponed_scroll < term->tl_rows / 3)
3143 /* Scrolling is usually faster than redrawing, when there are only
3144 * a few lines to scroll. */
3145 term_scroll_up(term, 0, term->tl_postponed_scroll);
3146 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003147 }
3148
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003149 /*
3150 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003151 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003152 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003153 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003154
Bram Moolenaar498c2562018-04-15 23:45:15 +02003155 newrows = 99999;
3156 newcols = 99999;
3157 FOR_ALL_WINDOWS(twp)
3158 {
3159 /* When more than one window shows the same terminal, use the
3160 * smallest size. */
3161 if (twp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003162 {
Bram Moolenaar498c2562018-04-15 23:45:15 +02003163 newrows = MIN(newrows, twp->w_height);
3164 newcols = MIN(newcols, twp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003165 }
Bram Moolenaar498c2562018-04-15 23:45:15 +02003166 }
3167 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3168 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3169
3170 if (term->tl_rows != newrows || term->tl_cols != newcols)
3171 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003172 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003173 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003174 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003175 newrows);
3176 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003177
3178 // Updating the terminal size will cause the snapshot to be cleared.
3179 // When not in terminal_loop() we need to restore it.
3180 if (term != in_terminal_loop)
3181 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003182 }
3183
3184 /* The cursor may have been moved when resizing. */
3185 vterm_state_get_cursorpos(state, &pos);
3186 position_cursor(wp, &pos);
3187
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003188 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3189 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003190 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003191 if (pos.row < term->tl_rows)
3192 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003193 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003194
Bram Moolenaar13568252018-03-16 20:46:58 +01003195 term_line2screenline(screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003196 }
3197 else
3198 pos.col = 0;
3199
Bram Moolenaarf118d482018-03-13 13:14:00 +01003200 screen_line(wp->w_winrow + pos.row
3201#ifdef FEAT_MENU
3202 + winbar_height(wp)
3203#endif
3204 , wp->w_wincol, pos.col, wp->w_width, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003205 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003206 term->tl_dirty_row_start = MAX_ROW;
3207 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003208}
3209
3210/*
3211 * Return TRUE if "wp" is a terminal window where the job has finished.
3212 */
3213 int
3214term_is_finished(buf_T *buf)
3215{
3216 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3217}
3218
3219/*
3220 * Return TRUE if "wp" is a terminal window where the job has finished or we
3221 * are in Terminal-Normal mode, thus we show the buffer contents.
3222 */
3223 int
3224term_show_buffer(buf_T *buf)
3225{
3226 term_T *term = buf->b_term;
3227
3228 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3229}
3230
3231/*
3232 * The current buffer is going to be changed. If there is terminal
3233 * highlighting remove it now.
3234 */
3235 void
3236term_change_in_curbuf(void)
3237{
3238 term_T *term = curbuf->b_term;
3239
3240 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3241 {
3242 free_scrollback(term);
3243 redraw_buf_later(term->tl_buffer, NOT_VALID);
3244
3245 /* The buffer is now like a normal buffer, it cannot be easily
3246 * abandoned when changed. */
3247 set_string_option_direct((char_u *)"buftype", -1,
3248 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3249 }
3250}
3251
3252/*
3253 * Get the screen attribute for a position in the buffer.
3254 * Use a negative "col" to get the filler background color.
3255 */
3256 int
3257term_get_attr(buf_T *buf, linenr_T lnum, int col)
3258{
3259 term_T *term = buf->b_term;
3260 sb_line_T *line;
3261 cellattr_T *cellattr;
3262
3263 if (lnum > term->tl_scrollback.ga_len)
3264 cellattr = &term->tl_default_color;
3265 else
3266 {
3267 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3268 if (col < 0 || col >= line->sb_cols)
3269 cellattr = &line->sb_fill_attr;
3270 else
3271 cellattr = line->sb_cells + col;
3272 }
3273 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
3274}
3275
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003276/*
3277 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003278 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003279 */
3280 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003281cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003282{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003283 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->ansi_index);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003284}
3285
3286/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003287 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003288 */
3289 static void
Bram Moolenaar52acb112018-03-18 19:20:22 +01003290init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003291{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003292 VTermColor *fg, *bg;
3293 int fgval, bgval;
3294 int id;
3295
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003296 vim_memset(&term->tl_default_color.attrs, 0, sizeof(VTermScreenCellAttrs));
3297 term->tl_default_color.width = 1;
3298 fg = &term->tl_default_color.fg;
3299 bg = &term->tl_default_color.bg;
3300
3301 /* Vterm uses a default black background. Set it to white when
3302 * 'background' is "light". */
3303 if (*p_bg == 'l')
3304 {
3305 fgval = 0;
3306 bgval = 255;
3307 }
3308 else
3309 {
3310 fgval = 255;
3311 bgval = 0;
3312 }
3313 fg->red = fg->green = fg->blue = fgval;
3314 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003315 fg->ansi_index = bg->ansi_index = VTERM_ANSI_INDEX_DEFAULT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003316
3317 /* The "Terminal" highlight group overrules the defaults. */
3318 id = syn_name2id((char_u *)"Terminal");
3319
Bram Moolenaar46359e12017-11-29 22:33:38 +01003320 /* Use the actual color for the GUI and when 'termguicolors' is set. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003321#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3322 if (0
3323# ifdef FEAT_GUI
3324 || gui.in_use
3325# endif
3326# ifdef FEAT_TERMGUICOLORS
3327 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003328# ifdef FEAT_VTP
3329 /* Finally get INVALCOLOR on this execution path */
3330 || (!p_tgc && t_colors >= 256)
3331# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003332# endif
3333 )
3334 {
3335 guicolor_T fg_rgb = INVALCOLOR;
3336 guicolor_T bg_rgb = INVALCOLOR;
3337
3338 if (id != 0)
3339 syn_id2colors(id, &fg_rgb, &bg_rgb);
3340
3341# ifdef FEAT_GUI
3342 if (gui.in_use)
3343 {
3344 if (fg_rgb == INVALCOLOR)
3345 fg_rgb = gui.norm_pixel;
3346 if (bg_rgb == INVALCOLOR)
3347 bg_rgb = gui.back_pixel;
3348 }
3349# ifdef FEAT_TERMGUICOLORS
3350 else
3351# endif
3352# endif
3353# ifdef FEAT_TERMGUICOLORS
3354 {
3355 if (fg_rgb == INVALCOLOR)
3356 fg_rgb = cterm_normal_fg_gui_color;
3357 if (bg_rgb == INVALCOLOR)
3358 bg_rgb = cterm_normal_bg_gui_color;
3359 }
3360# endif
3361 if (fg_rgb != INVALCOLOR)
3362 {
3363 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3364
3365 fg->red = (unsigned)(rgb >> 16);
3366 fg->green = (unsigned)(rgb >> 8) & 255;
3367 fg->blue = (unsigned)rgb & 255;
3368 }
3369 if (bg_rgb != INVALCOLOR)
3370 {
3371 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3372
3373 bg->red = (unsigned)(rgb >> 16);
3374 bg->green = (unsigned)(rgb >> 8) & 255;
3375 bg->blue = (unsigned)rgb & 255;
3376 }
3377 }
3378 else
3379#endif
3380 if (id != 0 && t_colors >= 16)
3381 {
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003382 if (term_default_cterm_fg >= 0)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003383 cterm_color2vterm(term_default_cterm_fg, fg);
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003384 if (term_default_cterm_bg >= 0)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003385 cterm_color2vterm(term_default_cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003386 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003387 else
3388 {
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003389#if defined(WIN3264) && !defined(FEAT_GUI_W32)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003390 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003391#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003392
3393 /* In an MS-Windows console we know the normal colors. */
3394 if (cterm_normal_fg_color > 0)
3395 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003396 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003397# if defined(WIN3264) && !defined(FEAT_GUI_W32)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003398 tmp = fg->red;
3399 fg->red = fg->blue;
3400 fg->blue = tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003401# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003402 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003403# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003404 else
3405 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003406# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003407
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003408 if (cterm_normal_bg_color > 0)
3409 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003410 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003411# if defined(WIN3264) && !defined(FEAT_GUI_W32)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003412 tmp = bg->red;
3413 bg->red = bg->blue;
3414 bg->blue = tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003415# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003416 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003417# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003418 else
3419 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003420# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003421 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01003422}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003423
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003424#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3425/*
3426 * Set the 16 ANSI colors from array of RGB values
3427 */
3428 static void
3429set_vterm_palette(VTerm *vterm, long_u *rgb)
3430{
3431 int index = 0;
3432 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003433
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003434 for (; index < 16; index++)
3435 {
3436 VTermColor color;
3437 color.red = (unsigned)(rgb[index] >> 16);
3438 color.green = (unsigned)(rgb[index] >> 8) & 255;
3439 color.blue = (unsigned)rgb[index] & 255;
3440 vterm_state_set_palette_color(state, index, &color);
3441 }
3442}
3443
3444/*
3445 * Set the ANSI color palette from a list of colors
3446 */
3447 static int
3448set_ansi_colors_list(VTerm *vterm, list_T *list)
3449{
3450 int n = 0;
3451 long_u rgb[16];
3452 listitem_T *li = list->lv_first;
3453
3454 for (; li != NULL && n < 16; li = li->li_next, n++)
3455 {
3456 char_u *color_name;
3457 guicolor_T guicolor;
3458
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003459 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003460 if (color_name == NULL)
3461 return FAIL;
3462
3463 guicolor = GUI_GET_COLOR(color_name);
3464 if (guicolor == INVALCOLOR)
3465 return FAIL;
3466
3467 rgb[n] = GUI_MCH_GET_RGB(guicolor);
3468 }
3469
3470 if (n != 16 || li != NULL)
3471 return FAIL;
3472
3473 set_vterm_palette(vterm, rgb);
3474
3475 return OK;
3476}
3477
3478/*
3479 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
3480 */
3481 static void
3482init_vterm_ansi_colors(VTerm *vterm)
3483{
3484 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
3485
3486 if (var != NULL
3487 && (var->di_tv.v_type != VAR_LIST
3488 || var->di_tv.vval.v_list == NULL
3489 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003490 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003491}
3492#endif
3493
Bram Moolenaar52acb112018-03-18 19:20:22 +01003494/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003495 * Handles a "drop" command from the job in the terminal.
3496 * "item" is the file name, "item->li_next" may have options.
3497 */
3498 static void
3499handle_drop_command(listitem_T *item)
3500{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003501 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003502 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003503 int bufnr;
3504 win_T *wp;
3505 tabpage_T *tp;
3506 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003507 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003508
3509 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
3510 FOR_ALL_TAB_WINDOWS(tp, wp)
3511 {
3512 if (wp->w_buffer->b_fnum == bufnr)
3513 {
3514 /* buffer is in a window already, go there */
3515 goto_tabpage_win(tp, wp);
3516 return;
3517 }
3518 }
3519
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003520 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003521
3522 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
3523 && opt_item->li_tv.vval.v_dict != NULL)
3524 {
3525 dict_T *dict = opt_item->li_tv.vval.v_dict;
3526 char_u *p;
3527
Bram Moolenaar8f667172018-12-14 15:38:31 +01003528 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003529 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01003530 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003531 if (p != NULL)
3532 {
3533 if (check_ff_value(p) == FAIL)
3534 ch_log(NULL, "Invalid ff argument to drop: %s", p);
3535 else
3536 ea.force_ff = *p;
3537 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01003538 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003539 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01003540 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003541 if (p != NULL)
3542 {
Bram Moolenaar3aa67fb2018-04-05 21:04:15 +02003543 ea.cmd = alloc((int)STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003544 if (ea.cmd != NULL)
3545 {
3546 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
3547 ea.force_enc = 11;
3548 tofree = ea.cmd;
3549 }
3550 }
3551
Bram Moolenaar8f667172018-12-14 15:38:31 +01003552 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003553 if (p != NULL)
3554 get_bad_opt(p, &ea);
3555
3556 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
3557 ea.force_bin = FORCE_BIN;
3558 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
3559 ea.force_bin = FORCE_BIN;
3560 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
3561 ea.force_bin = FORCE_NOBIN;
3562 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
3563 ea.force_bin = FORCE_NOBIN;
3564 }
3565
3566 /* open in new window, like ":split fname" */
3567 if (ea.cmd == NULL)
3568 ea.cmd = (char_u *)"split";
3569 ea.arg = fname;
3570 ea.cmdidx = CMD_split;
3571 ex_splitview(&ea);
3572
3573 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003574}
3575
3576/*
3577 * Handles a function call from the job running in a terminal.
3578 * "item" is the function name, "item->li_next" has the arguments.
3579 */
3580 static void
3581handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
3582{
3583 char_u *func;
3584 typval_T argvars[2];
3585 typval_T rettv;
3586 int doesrange;
3587
3588 if (item->li_next == NULL)
3589 {
3590 ch_log(channel, "Missing function arguments for call");
3591 return;
3592 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003593 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003594
Bram Moolenaar2a77d212018-03-26 21:38:52 +02003595 if (STRNCMP(func, "Tapi_", 5) != 0)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003596 {
3597 ch_log(channel, "Invalid function name: %s", func);
3598 return;
3599 }
3600
3601 argvars[0].v_type = VAR_NUMBER;
3602 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
3603 argvars[1] = item->li_next->li_tv;
Bram Moolenaar878c96d2018-04-04 23:00:06 +02003604 if (call_func(func, (int)STRLEN(func), &rettv,
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003605 2, argvars, /* argv_func */ NULL,
3606 /* firstline */ 1, /* lastline */ 1,
3607 &doesrange, /* evaluate */ TRUE,
3608 /* partial */ NULL, /* selfdict */ NULL) == OK)
3609 {
3610 clear_tv(&rettv);
3611 ch_log(channel, "Function %s called", func);
3612 }
3613 else
3614 ch_log(channel, "Calling function %s failed", func);
3615}
3616
3617/*
3618 * Called by libvterm when it cannot recognize an OSC sequence.
3619 * We recognize a terminal API command.
3620 */
3621 static int
3622parse_osc(const char *command, size_t cmdlen, void *user)
3623{
3624 term_T *term = (term_T *)user;
3625 js_read_T reader;
3626 typval_T tv;
3627 channel_T *channel = term->tl_job == NULL ? NULL
3628 : term->tl_job->jv_channel;
3629
3630 /* We recognize only OSC 5 1 ; {command} */
3631 if (cmdlen < 3 || STRNCMP(command, "51;", 3) != 0)
3632 return 0; /* not handled */
3633
Bram Moolenaar878c96d2018-04-04 23:00:06 +02003634 reader.js_buf = vim_strnsave((char_u *)command + 3, (int)(cmdlen - 3));
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003635 if (reader.js_buf == NULL)
3636 return 1;
3637 reader.js_fill = NULL;
3638 reader.js_used = 0;
3639 if (json_decode(&reader, &tv, 0) == OK
3640 && tv.v_type == VAR_LIST
3641 && tv.vval.v_list != NULL)
3642 {
3643 listitem_T *item = tv.vval.v_list->lv_first;
3644
3645 if (item == NULL)
3646 ch_log(channel, "Missing command");
3647 else
3648 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003649 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003650
Bram Moolenaara997b452018-04-17 23:24:06 +02003651 /* Make sure an invoked command doesn't delete the buffer (and the
3652 * terminal) under our fingers. */
3653 ++term->tl_buffer->b_locked;
3654
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003655 item = item->li_next;
3656 if (item == NULL)
3657 ch_log(channel, "Missing argument for %s", cmd);
3658 else if (STRCMP(cmd, "drop") == 0)
3659 handle_drop_command(item);
3660 else if (STRCMP(cmd, "call") == 0)
3661 handle_call_command(term, channel, item);
3662 else
3663 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02003664 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003665 }
3666 }
3667 else
3668 ch_log(channel, "Invalid JSON received");
3669
3670 vim_free(reader.js_buf);
3671 clear_tv(&tv);
3672 return 1;
3673}
3674
3675static VTermParserCallbacks parser_fallbacks = {
3676 NULL, /* text */
3677 NULL, /* control */
3678 NULL, /* escape */
3679 NULL, /* csi */
3680 parse_osc, /* osc */
3681 NULL, /* dcs */
3682 NULL /* resize */
3683};
3684
3685/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02003686 * Use Vim's allocation functions for vterm so profiling works.
3687 */
3688 static void *
3689vterm_malloc(size_t size, void *data UNUSED)
3690{
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02003691 return alloc_clear((unsigned) size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02003692}
3693
3694 static void
3695vterm_memfree(void *ptr, void *data UNUSED)
3696{
3697 vim_free(ptr);
3698}
3699
3700static VTermAllocatorFunctions vterm_allocator = {
3701 &vterm_malloc,
3702 &vterm_memfree
3703};
3704
3705/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003706 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003707 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01003708 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003709 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01003710create_vterm(term_T *term, int rows, int cols)
3711{
3712 VTerm *vterm;
3713 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003714 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01003715 VTermValue value;
3716
Bram Moolenaar756ef112018-04-10 12:04:27 +02003717 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01003718 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003719 if (vterm == NULL)
3720 return FAIL;
3721
3722 // Allocate screen and state here, so we can bail out if that fails.
3723 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01003724 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003725 if (state == NULL || screen == NULL)
3726 {
3727 vterm_free(vterm);
3728 return FAIL;
3729 }
3730
Bram Moolenaar52acb112018-03-18 19:20:22 +01003731 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
3732 /* TODO: depends on 'encoding'. */
3733 vterm_set_utf8(vterm, 1);
3734
3735 init_default_colors(term);
3736
3737 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003738 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01003739 &term->tl_default_color.fg,
3740 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003741
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003742 if (t_colors >= 16)
3743 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
3744
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003745 /* Required to initialize most things. */
3746 vterm_screen_reset(screen, 1 /* hard */);
3747
3748 /* Allow using alternate screen. */
3749 vterm_screen_enable_altscreen(screen, 1);
3750
3751 /* For unix do not use a blinking cursor. In an xterm this causes the
3752 * cursor to blink if it's blinking in the xterm.
3753 * For Windows we respect the system wide setting. */
3754#ifdef WIN3264
3755 if (GetCaretBlinkTime() == INFINITE)
3756 value.boolean = 0;
3757 else
3758 value.boolean = 1;
3759#else
3760 value.boolean = 0;
3761#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003762 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
3763 vterm_state_set_unrecognised_fallbacks(state, &parser_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003764
3765 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003766}
3767
3768/*
3769 * Return the text to show for the buffer name and status.
3770 */
3771 char_u *
3772term_get_status_text(term_T *term)
3773{
3774 if (term->tl_status_text == NULL)
3775 {
3776 char_u *txt;
3777 size_t len;
3778
3779 if (term->tl_normal_mode)
3780 {
3781 if (term_job_running(term))
3782 txt = (char_u *)_("Terminal");
3783 else
3784 txt = (char_u *)_("Terminal-finished");
3785 }
3786 else if (term->tl_title != NULL)
3787 txt = term->tl_title;
3788 else if (term_none_open(term))
3789 txt = (char_u *)_("active");
3790 else if (term_job_running(term))
3791 txt = (char_u *)_("running");
3792 else
3793 txt = (char_u *)_("finished");
3794 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
3795 term->tl_status_text = alloc((int)len);
3796 if (term->tl_status_text != NULL)
3797 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
3798 term->tl_buffer->b_fname, txt);
3799 }
3800 return term->tl_status_text;
3801}
3802
3803/*
3804 * Mark references in jobs of terminals.
3805 */
3806 int
3807set_ref_in_term(int copyID)
3808{
3809 int abort = FALSE;
3810 term_T *term;
3811 typval_T tv;
3812
3813 for (term = first_term; term != NULL; term = term->tl_next)
3814 if (term->tl_job != NULL)
3815 {
3816 tv.v_type = VAR_JOB;
3817 tv.vval.v_job = term->tl_job;
3818 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
3819 }
3820 return abort;
3821}
3822
3823/*
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003824 * Cache "Terminal" highlight group colors.
3825 */
3826 void
3827set_terminal_default_colors(int cterm_fg, int cterm_bg)
3828{
3829 term_default_cterm_fg = cterm_fg - 1;
3830 term_default_cterm_bg = cterm_bg - 1;
3831}
3832
3833/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003834 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01003835 * Returns NULL when the buffer is not for a terminal window and logs a message
3836 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003837 */
3838 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01003839term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003840{
3841 buf_T *buf;
3842
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003843 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003844 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01003845 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003846 --emsg_off;
3847 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01003848 {
3849 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003850 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01003851 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003852 return buf;
3853}
3854
Bram Moolenaard96ff162018-02-18 22:13:29 +01003855 static int
3856same_color(VTermColor *a, VTermColor *b)
3857{
3858 return a->red == b->red
3859 && a->green == b->green
3860 && a->blue == b->blue
3861 && a->ansi_index == b->ansi_index;
3862}
3863
3864 static void
3865dump_term_color(FILE *fd, VTermColor *color)
3866{
3867 fprintf(fd, "%02x%02x%02x%d",
3868 (int)color->red, (int)color->green, (int)color->blue,
3869 (int)color->ansi_index);
3870}
3871
3872/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003873 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01003874 *
3875 * Each screen cell in full is:
3876 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
3877 * {characters} is a space for an empty cell
3878 * For a double-width character "+" is changed to "*" and the next cell is
3879 * skipped.
3880 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
3881 * when "&" use the same as the previous cell.
3882 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
3883 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
3884 * {color-idx} is a number from 0 to 255
3885 *
3886 * Screen cell with same width, attributes and color as the previous one:
3887 * |{characters}
3888 *
3889 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
3890 *
3891 * Repeating the previous screen cell:
3892 * @{count}
3893 */
3894 void
3895f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
3896{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01003897 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01003898 term_T *term;
3899 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003900 int max_height = 0;
3901 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01003902 stat_T st;
3903 FILE *fd;
3904 VTermPos pos;
3905 VTermScreen *screen;
3906 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01003907 VTermState *state;
3908 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01003909
3910 if (check_restricted() || check_secure())
3911 return;
3912 if (buf == NULL)
3913 return;
3914 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02003915 if (term->tl_vterm == NULL)
3916 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003917 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02003918 return;
3919 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01003920
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003921 if (argvars[2].v_type != VAR_UNKNOWN)
3922 {
3923 dict_T *d;
3924
3925 if (argvars[2].v_type != VAR_DICT)
3926 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003927 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003928 return;
3929 }
3930 d = argvars[2].vval.v_dict;
3931 if (d != NULL)
3932 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01003933 max_height = dict_get_number(d, (char_u *)"rows");
3934 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003935 }
3936 }
3937
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003938 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01003939 if (fname == NULL)
3940 return;
3941 if (mch_stat((char *)fname, &st) >= 0)
3942 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003943 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01003944 return;
3945 }
3946
Bram Moolenaard96ff162018-02-18 22:13:29 +01003947 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
3948 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003949 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01003950 return;
3951 }
3952
3953 vim_memset(&prev_cell, 0, sizeof(prev_cell));
3954
3955 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01003956 state = vterm_obtain_state(term->tl_vterm);
3957 vterm_state_get_cursorpos(state, &cursor_pos);
3958
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003959 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
3960 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003961 {
3962 int repeat = 0;
3963
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01003964 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
3965 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003966 {
3967 VTermScreenCell cell;
3968 int same_attr;
3969 int same_chars = TRUE;
3970 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01003971 int is_cursor_pos = (pos.col == cursor_pos.col
3972 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01003973
3974 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
3975 vim_memset(&cell, 0, sizeof(cell));
3976
3977 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
3978 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01003979 int c = cell.chars[i];
3980 int pc = prev_cell.chars[i];
3981
3982 /* For the first character NUL is the same as space. */
3983 if (i == 0)
3984 {
3985 c = (c == NUL) ? ' ' : c;
3986 pc = (pc == NUL) ? ' ' : pc;
3987 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02003988 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003989 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02003990 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003991 break;
3992 }
3993 same_attr = vtermAttr2hl(cell.attrs)
3994 == vtermAttr2hl(prev_cell.attrs)
3995 && same_color(&cell.fg, &prev_cell.fg)
3996 && same_color(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01003997 if (same_chars && cell.width == prev_cell.width && same_attr
3998 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01003999 {
4000 ++repeat;
4001 }
4002 else
4003 {
4004 if (repeat > 0)
4005 {
4006 fprintf(fd, "@%d", repeat);
4007 repeat = 0;
4008 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004009 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004010
4011 if (cell.chars[0] == NUL)
4012 fputs(" ", fd);
4013 else
4014 {
4015 char_u charbuf[10];
4016 int len;
4017
4018 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4019 && cell.chars[i] != NUL; ++i)
4020 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004021 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004022 fwrite(charbuf, len, 1, fd);
4023 }
4024 }
4025
4026 /* When only the characters differ we don't write anything, the
4027 * following "|", "@" or NL will indicate using the same
4028 * attributes. */
4029 if (cell.width != prev_cell.width || !same_attr)
4030 {
4031 if (cell.width == 2)
4032 {
4033 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004034 }
4035 else
4036 fputs("+", fd);
4037
4038 if (same_attr)
4039 {
4040 fputs("&", fd);
4041 }
4042 else
4043 {
4044 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
4045 if (same_color(&cell.fg, &prev_cell.fg))
4046 fputs("&", fd);
4047 else
4048 {
4049 fputs("#", fd);
4050 dump_term_color(fd, &cell.fg);
4051 }
4052 if (same_color(&cell.bg, &prev_cell.bg))
4053 fputs("&", fd);
4054 else
4055 {
4056 fputs("#", fd);
4057 dump_term_color(fd, &cell.bg);
4058 }
4059 }
4060 }
4061
4062 prev_cell = cell;
4063 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004064
4065 if (cell.width == 2)
4066 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004067 }
4068 if (repeat > 0)
4069 fprintf(fd, "@%d", repeat);
4070 fputs("\n", fd);
4071 }
4072
4073 fclose(fd);
4074}
4075
4076/*
4077 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4078 */
4079 static void
4080dump_is_corrupt(garray_T *gap)
4081{
4082 ga_concat(gap, (char_u *)"CORRUPT");
4083}
4084
4085 static void
4086append_cell(garray_T *gap, cellattr_T *cell)
4087{
4088 if (ga_grow(gap, 1) == OK)
4089 {
4090 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4091 ++gap->ga_len;
4092 }
4093}
4094
4095/*
4096 * Read the dump file from "fd" and append lines to the current buffer.
4097 * Return the cell width of the longest line.
4098 */
4099 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004100read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004101{
4102 int c;
4103 garray_T ga_text;
4104 garray_T ga_cell;
4105 char_u *prev_char = NULL;
4106 int attr = 0;
4107 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004108 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004109 term_T *term = curbuf->b_term;
4110 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004111 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004112
4113 ga_init2(&ga_text, 1, 90);
4114 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
4115 vim_memset(&cell, 0, sizeof(cell));
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004116 vim_memset(&empty_cell, 0, sizeof(empty_cell));
Bram Moolenaar9271d052018-02-25 21:39:46 +01004117 cursor_pos->row = -1;
4118 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004119
4120 c = fgetc(fd);
4121 for (;;)
4122 {
4123 if (c == EOF)
4124 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004125 if (c == '\r')
4126 {
4127 // DOS line endings? Ignore.
4128 c = fgetc(fd);
4129 }
4130 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004131 {
4132 /* End of a line: append it to the buffer. */
4133 if (ga_text.ga_data == NULL)
4134 dump_is_corrupt(&ga_text);
4135 if (ga_grow(&term->tl_scrollback, 1) == OK)
4136 {
4137 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4138 + term->tl_scrollback.ga_len;
4139
4140 if (max_cells < ga_cell.ga_len)
4141 max_cells = ga_cell.ga_len;
4142 line->sb_cols = ga_cell.ga_len;
4143 line->sb_cells = ga_cell.ga_data;
4144 line->sb_fill_attr = term->tl_default_color;
4145 ++term->tl_scrollback.ga_len;
4146 ga_init(&ga_cell);
4147
4148 ga_append(&ga_text, NUL);
4149 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4150 ga_text.ga_len, FALSE);
4151 }
4152 else
4153 ga_clear(&ga_cell);
4154 ga_text.ga_len = 0;
4155
4156 c = fgetc(fd);
4157 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004158 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004159 {
4160 int prev_len = ga_text.ga_len;
4161
Bram Moolenaar9271d052018-02-25 21:39:46 +01004162 if (c == '>')
4163 {
4164 if (cursor_pos->row != -1)
4165 dump_is_corrupt(&ga_text); /* duplicate cursor */
4166 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4167 cursor_pos->col = ga_cell.ga_len;
4168 }
4169
Bram Moolenaard96ff162018-02-18 22:13:29 +01004170 /* normal character(s) followed by "+", "*", "|", "@" or NL */
4171 c = fgetc(fd);
4172 if (c != EOF)
4173 ga_append(&ga_text, c);
4174 for (;;)
4175 {
4176 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004177 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004178 || c == EOF || c == '\n')
4179 break;
4180 ga_append(&ga_text, c);
4181 }
4182
4183 /* save the character for repeating it */
4184 vim_free(prev_char);
4185 if (ga_text.ga_data != NULL)
4186 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4187 ga_text.ga_len - prev_len);
4188
Bram Moolenaar9271d052018-02-25 21:39:46 +01004189 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004190 {
4191 /* use all attributes from previous cell */
4192 }
4193 else if (c == '+' || c == '*')
4194 {
4195 int is_bg;
4196
4197 cell.width = c == '+' ? 1 : 2;
4198
4199 c = fgetc(fd);
4200 if (c == '&')
4201 {
4202 /* use same attr as previous cell */
4203 c = fgetc(fd);
4204 }
4205 else if (isdigit(c))
4206 {
4207 /* get the decimal attribute */
4208 attr = 0;
4209 while (isdigit(c))
4210 {
4211 attr = attr * 10 + (c - '0');
4212 c = fgetc(fd);
4213 }
4214 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004215
4216 /* is_bg == 0: fg, is_bg == 1: bg */
4217 for (is_bg = 0; is_bg <= 1; ++is_bg)
4218 {
4219 if (c == '&')
4220 {
4221 /* use same color as previous cell */
4222 c = fgetc(fd);
4223 }
4224 else if (c == '#')
4225 {
4226 int red, green, blue, index = 0;
4227
4228 c = fgetc(fd);
4229 red = hex2nr(c);
4230 c = fgetc(fd);
4231 red = (red << 4) + hex2nr(c);
4232 c = fgetc(fd);
4233 green = hex2nr(c);
4234 c = fgetc(fd);
4235 green = (green << 4) + hex2nr(c);
4236 c = fgetc(fd);
4237 blue = hex2nr(c);
4238 c = fgetc(fd);
4239 blue = (blue << 4) + hex2nr(c);
4240 c = fgetc(fd);
4241 if (!isdigit(c))
4242 dump_is_corrupt(&ga_text);
4243 while (isdigit(c))
4244 {
4245 index = index * 10 + (c - '0');
4246 c = fgetc(fd);
4247 }
4248
4249 if (is_bg)
4250 {
4251 cell.bg.red = red;
4252 cell.bg.green = green;
4253 cell.bg.blue = blue;
4254 cell.bg.ansi_index = index;
4255 }
4256 else
4257 {
4258 cell.fg.red = red;
4259 cell.fg.green = green;
4260 cell.fg.blue = blue;
4261 cell.fg.ansi_index = index;
4262 }
4263 }
4264 else
4265 dump_is_corrupt(&ga_text);
4266 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004267 }
4268 else
4269 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004270 }
4271 else
4272 dump_is_corrupt(&ga_text);
4273
4274 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004275 if (cell.width == 2)
4276 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004277 }
4278 else if (c == '@')
4279 {
4280 if (prev_char == NULL)
4281 dump_is_corrupt(&ga_text);
4282 else
4283 {
4284 int count = 0;
4285
4286 /* repeat previous character, get the count */
4287 for (;;)
4288 {
4289 c = fgetc(fd);
4290 if (!isdigit(c))
4291 break;
4292 count = count * 10 + (c - '0');
4293 }
4294
4295 while (count-- > 0)
4296 {
4297 ga_concat(&ga_text, prev_char);
4298 append_cell(&ga_cell, &cell);
4299 }
4300 }
4301 }
4302 else
4303 {
4304 dump_is_corrupt(&ga_text);
4305 c = fgetc(fd);
4306 }
4307 }
4308
4309 if (ga_text.ga_len > 0)
4310 {
4311 /* trailing characters after last NL */
4312 dump_is_corrupt(&ga_text);
4313 ga_append(&ga_text, NUL);
4314 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4315 ga_text.ga_len, FALSE);
4316 }
4317
4318 ga_clear(&ga_text);
4319 vim_free(prev_char);
4320
4321 return max_cells;
4322}
4323
4324/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02004325 * Return an allocated string with at least "text_width" "=" characters and
4326 * "fname" inserted in the middle.
4327 */
4328 static char_u *
4329get_separator(int text_width, char_u *fname)
4330{
4331 int width = MAX(text_width, curwin->w_width);
4332 char_u *textline;
4333 int fname_size;
4334 char_u *p = fname;
4335 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004336 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004337
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004338 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02004339 if (textline == NULL)
4340 return NULL;
4341
4342 fname_size = vim_strsize(fname);
4343 if (fname_size < width - 8)
4344 {
4345 /* enough room, don't use the full window width */
4346 width = MAX(text_width, fname_size + 8);
4347 }
4348 else if (fname_size > width - 8)
4349 {
4350 /* full name doesn't fit, use only the tail */
4351 p = gettail(fname);
4352 fname_size = vim_strsize(p);
4353 }
4354 /* skip characters until the name fits */
4355 while (fname_size > width - 8)
4356 {
4357 p += (*mb_ptr2len)(p);
4358 fname_size = vim_strsize(p);
4359 }
4360
4361 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
4362 textline[i] = '=';
4363 textline[i++] = ' ';
4364
4365 STRCPY(textline + i, p);
4366 off = STRLEN(textline);
4367 textline[off] = ' ';
4368 for (i = 1; i < (width - fname_size) / 2; ++i)
4369 textline[off + i] = '=';
4370 textline[off + i] = NUL;
4371
4372 return textline;
4373}
4374
4375/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01004376 * Common for "term_dumpdiff()" and "term_dumpload()".
4377 */
4378 static void
4379term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
4380{
4381 jobopt_T opt;
4382 buf_T *buf;
4383 char_u buf1[NUMBUFLEN];
4384 char_u buf2[NUMBUFLEN];
4385 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004386 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004387 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004388 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004389 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004390 char_u *textline = NULL;
4391
4392 /* First open the files. If this fails bail out. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004393 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004394 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004395 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004396 if (fname1 == NULL || (do_diff && fname2 == NULL))
4397 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004398 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01004399 return;
4400 }
4401 fd1 = mch_fopen((char *)fname1, READBIN);
4402 if (fd1 == NULL)
4403 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004404 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004405 return;
4406 }
4407 if (do_diff)
4408 {
4409 fd2 = mch_fopen((char *)fname2, READBIN);
4410 if (fd2 == NULL)
4411 {
4412 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004413 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004414 return;
4415 }
4416 }
4417
4418 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004419 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
4420 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
4421 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
4422 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
4423 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004424
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004425 if (opt.jo_term_name == NULL)
4426 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01004427 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004428
Bram Moolenaarb571c632018-03-21 22:27:59 +01004429 fname_tofree = alloc((int)len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004430 if (fname_tofree != NULL)
4431 {
4432 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
4433 opt.jo_term_name = fname_tofree;
4434 }
4435 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004436
Bram Moolenaar13568252018-03-16 20:46:58 +01004437 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004438 if (buf != NULL && buf->b_term != NULL)
4439 {
4440 int i;
4441 linenr_T bot_lnum;
4442 linenr_T lnum;
4443 term_T *term = buf->b_term;
4444 int width;
4445 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004446 VTermPos cursor_pos1;
4447 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004448
Bram Moolenaar52acb112018-03-18 19:20:22 +01004449 init_default_colors(term);
4450
Bram Moolenaard96ff162018-02-18 22:13:29 +01004451 rettv->vval.v_number = buf->b_fnum;
4452
4453 /* read the files, fill the buffer with the diff */
Bram Moolenaar9271d052018-02-25 21:39:46 +01004454 width = read_dump_file(fd1, &cursor_pos1);
4455
4456 /* position the cursor */
4457 if (cursor_pos1.row >= 0)
4458 {
4459 curwin->w_cursor.lnum = cursor_pos1.row + 1;
4460 coladvance(cursor_pos1.col);
4461 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004462
4463 /* Delete the empty line that was in the empty buffer. */
4464 ml_delete(1, FALSE);
4465
4466 /* For term_dumpload() we are done here. */
4467 if (!do_diff)
4468 goto theend;
4469
4470 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
4471
Bram Moolenaar4a696342018-04-05 18:45:26 +02004472 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004473 if (textline == NULL)
4474 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004475 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
4476 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
4477 vim_free(textline);
4478
4479 textline = get_separator(width, fname2);
4480 if (textline == NULL)
4481 goto theend;
4482 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
4483 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004484 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004485
4486 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004487 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004488 if (width2 > width)
4489 {
4490 vim_free(textline);
4491 textline = alloc(width2 + 1);
4492 if (textline == NULL)
4493 goto theend;
4494 width = width2;
4495 textline[width] = NUL;
4496 }
4497 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
4498
4499 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
4500 {
4501 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
4502 {
4503 /* bottom part has fewer rows, fill with "-" */
4504 for (i = 0; i < width; ++i)
4505 textline[i] = '-';
4506 }
4507 else
4508 {
4509 char_u *line1;
4510 char_u *line2;
4511 char_u *p1;
4512 char_u *p2;
4513 int col;
4514 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
4515 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
4516 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
4517 ->sb_cells;
4518
4519 /* Make a copy, getting the second line will invalidate it. */
4520 line1 = vim_strsave(ml_get(lnum));
4521 if (line1 == NULL)
4522 break;
4523 p1 = line1;
4524
4525 line2 = ml_get(lnum + bot_lnum);
4526 p2 = line2;
4527 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
4528 {
4529 int len1 = utfc_ptr2len(p1);
4530 int len2 = utfc_ptr2len(p2);
4531
4532 textline[col] = ' ';
4533 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar9271d052018-02-25 21:39:46 +01004534 /* text differs */
Bram Moolenaard96ff162018-02-18 22:13:29 +01004535 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01004536 else if (lnum == cursor_pos1.row + 1
4537 && col == cursor_pos1.col
4538 && (cursor_pos1.row != cursor_pos2.row
4539 || cursor_pos1.col != cursor_pos2.col))
4540 /* cursor in first but not in second */
4541 textline[col] = '>';
4542 else if (lnum == cursor_pos2.row + 1
4543 && col == cursor_pos2.col
4544 && (cursor_pos1.row != cursor_pos2.row
4545 || cursor_pos1.col != cursor_pos2.col))
4546 /* cursor in second but not in first */
4547 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01004548 else if (cellattr1 != NULL && cellattr2 != NULL)
4549 {
4550 if ((cellattr1 + col)->width
4551 != (cellattr2 + col)->width)
4552 textline[col] = 'w';
4553 else if (!same_color(&(cellattr1 + col)->fg,
4554 &(cellattr2 + col)->fg))
4555 textline[col] = 'f';
4556 else if (!same_color(&(cellattr1 + col)->bg,
4557 &(cellattr2 + col)->bg))
4558 textline[col] = 'b';
4559 else if (vtermAttr2hl((cellattr1 + col)->attrs)
4560 != vtermAttr2hl(((cellattr2 + col)->attrs)))
4561 textline[col] = 'a';
4562 }
4563 p1 += len1;
4564 p2 += len2;
4565 /* TODO: handle different width */
4566 }
4567 vim_free(line1);
4568
4569 while (col < width)
4570 {
4571 if (*p1 == NUL && *p2 == NUL)
4572 textline[col] = '?';
4573 else if (*p1 == NUL)
4574 {
4575 textline[col] = '+';
4576 p2 += utfc_ptr2len(p2);
4577 }
4578 else
4579 {
4580 textline[col] = '-';
4581 p1 += utfc_ptr2len(p1);
4582 }
4583 ++col;
4584 }
4585 }
4586 if (add_empty_scrollback(term, &term->tl_default_color,
4587 term->tl_top_diff_rows) == OK)
4588 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
4589 ++bot_lnum;
4590 }
4591
4592 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
4593 {
4594 /* bottom part has more rows, fill with "+" */
4595 for (i = 0; i < width; ++i)
4596 textline[i] = '+';
4597 if (add_empty_scrollback(term, &term->tl_default_color,
4598 term->tl_top_diff_rows) == OK)
4599 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
4600 ++lnum;
4601 ++bot_lnum;
4602 }
4603
4604 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004605
4606 /* looks better without wrapping */
4607 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004608 }
4609
4610theend:
4611 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004612 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004613 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004614 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004615 fclose(fd2);
4616}
4617
4618/*
4619 * If the current buffer shows the output of term_dumpdiff(), swap the top and
4620 * bottom files.
4621 * Return FAIL when this is not possible.
4622 */
4623 int
4624term_swap_diff()
4625{
4626 term_T *term = curbuf->b_term;
4627 linenr_T line_count;
4628 linenr_T top_rows;
4629 linenr_T bot_rows;
4630 linenr_T bot_start;
4631 linenr_T lnum;
4632 char_u *p;
4633 sb_line_T *sb_line;
4634
4635 if (term == NULL
4636 || !term_is_finished(curbuf)
4637 || term->tl_top_diff_rows == 0
4638 || term->tl_scrollback.ga_len == 0)
4639 return FAIL;
4640
4641 line_count = curbuf->b_ml.ml_line_count;
4642 top_rows = term->tl_top_diff_rows;
4643 bot_rows = term->tl_bot_diff_rows;
4644 bot_start = line_count - bot_rows;
4645 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
4646
4647 /* move lines from top to above the bottom part */
4648 for (lnum = 1; lnum <= top_rows; ++lnum)
4649 {
4650 p = vim_strsave(ml_get(1));
4651 if (p == NULL)
4652 return OK;
4653 ml_append(bot_start, p, 0, FALSE);
4654 ml_delete(1, FALSE);
4655 vim_free(p);
4656 }
4657
4658 /* move lines from bottom to the top */
4659 for (lnum = 1; lnum <= bot_rows; ++lnum)
4660 {
4661 p = vim_strsave(ml_get(bot_start + lnum));
4662 if (p == NULL)
4663 return OK;
4664 ml_delete(bot_start + lnum, FALSE);
4665 ml_append(lnum - 1, p, 0, FALSE);
4666 vim_free(p);
4667 }
4668
4669 if (top_rows == bot_rows)
4670 {
4671 /* rows counts are equal, can swap cell properties */
4672 for (lnum = 0; lnum < top_rows; ++lnum)
4673 {
4674 sb_line_T temp;
4675
4676 temp = *(sb_line + lnum);
4677 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
4678 *(sb_line + bot_start + lnum) = temp;
4679 }
4680 }
4681 else
4682 {
4683 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
4684 sb_line_T *temp = (sb_line_T *)alloc((int)size);
4685
4686 /* need to copy cell properties into temp memory */
4687 if (temp != NULL)
4688 {
4689 mch_memmove(temp, term->tl_scrollback.ga_data, size);
4690 mch_memmove(term->tl_scrollback.ga_data,
4691 temp + bot_start,
4692 sizeof(sb_line_T) * bot_rows);
4693 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
4694 temp + top_rows,
4695 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
4696 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
4697 + line_count - top_rows,
4698 temp,
4699 sizeof(sb_line_T) * top_rows);
4700 vim_free(temp);
4701 }
4702 }
4703
4704 term->tl_top_diff_rows = bot_rows;
4705 term->tl_bot_diff_rows = top_rows;
4706
4707 update_screen(NOT_VALID);
4708 return OK;
4709}
4710
4711/*
4712 * "term_dumpdiff(filename, filename, options)" function
4713 */
4714 void
4715f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
4716{
4717 term_load_dump(argvars, rettv, TRUE);
4718}
4719
4720/*
4721 * "term_dumpload(filename, options)" function
4722 */
4723 void
4724f_term_dumpload(typval_T *argvars, typval_T *rettv)
4725{
4726 term_load_dump(argvars, rettv, FALSE);
4727}
4728
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004729/*
4730 * "term_getaltscreen(buf)" function
4731 */
4732 void
4733f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
4734{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004735 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004736
4737 if (buf == NULL)
4738 return;
4739 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
4740}
4741
4742/*
4743 * "term_getattr(attr, name)" function
4744 */
4745 void
4746f_term_getattr(typval_T *argvars, typval_T *rettv)
4747{
4748 int attr;
4749 size_t i;
4750 char_u *name;
4751
4752 static struct {
4753 char *name;
4754 int attr;
4755 } attrs[] = {
4756 {"bold", HL_BOLD},
4757 {"italic", HL_ITALIC},
4758 {"underline", HL_UNDERLINE},
4759 {"strike", HL_STRIKETHROUGH},
4760 {"reverse", HL_INVERSE},
4761 };
4762
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004763 attr = tv_get_number(&argvars[0]);
4764 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004765 if (name == NULL)
4766 return;
4767
4768 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
4769 if (STRCMP(name, attrs[i].name) == 0)
4770 {
4771 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
4772 break;
4773 }
4774}
4775
4776/*
4777 * "term_getcursor(buf)" function
4778 */
4779 void
4780f_term_getcursor(typval_T *argvars, typval_T *rettv)
4781{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004782 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004783 term_T *term;
4784 list_T *l;
4785 dict_T *d;
4786
4787 if (rettv_list_alloc(rettv) == FAIL)
4788 return;
4789 if (buf == NULL)
4790 return;
4791 term = buf->b_term;
4792
4793 l = rettv->vval.v_list;
4794 list_append_number(l, term->tl_cursor_pos.row + 1);
4795 list_append_number(l, term->tl_cursor_pos.col + 1);
4796
4797 d = dict_alloc();
4798 if (d != NULL)
4799 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02004800 dict_add_number(d, "visible", term->tl_cursor_visible);
4801 dict_add_number(d, "blink", blink_state_is_inverted()
4802 ? !term->tl_cursor_blink : term->tl_cursor_blink);
4803 dict_add_number(d, "shape", term->tl_cursor_shape);
4804 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004805 list_append_dict(l, d);
4806 }
4807}
4808
4809/*
4810 * "term_getjob(buf)" function
4811 */
4812 void
4813f_term_getjob(typval_T *argvars, typval_T *rettv)
4814{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004815 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004816
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004817 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01004818 {
4819 rettv->v_type = VAR_SPECIAL;
4820 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004821 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01004822 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004823
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01004824 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004825 rettv->vval.v_job = buf->b_term->tl_job;
4826 if (rettv->vval.v_job != NULL)
4827 ++rettv->vval.v_job->jv_refcount;
4828}
4829
4830 static int
4831get_row_number(typval_T *tv, term_T *term)
4832{
4833 if (tv->v_type == VAR_STRING
4834 && tv->vval.v_string != NULL
4835 && STRCMP(tv->vval.v_string, ".") == 0)
4836 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004837 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004838}
4839
4840/*
4841 * "term_getline(buf, row)" function
4842 */
4843 void
4844f_term_getline(typval_T *argvars, typval_T *rettv)
4845{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004846 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004847 term_T *term;
4848 int row;
4849
4850 rettv->v_type = VAR_STRING;
4851 if (buf == NULL)
4852 return;
4853 term = buf->b_term;
4854 row = get_row_number(&argvars[1], term);
4855
4856 if (term->tl_vterm == NULL)
4857 {
4858 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
4859
4860 /* vterm is finished, get the text from the buffer */
4861 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
4862 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
4863 }
4864 else
4865 {
4866 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
4867 VTermRect rect;
4868 int len;
4869 char_u *p;
4870
4871 if (row < 0 || row >= term->tl_rows)
4872 return;
4873 len = term->tl_cols * MB_MAXBYTES + 1;
4874 p = alloc(len);
4875 if (p == NULL)
4876 return;
4877 rettv->vval.v_string = p;
4878
4879 rect.start_col = 0;
4880 rect.end_col = term->tl_cols;
4881 rect.start_row = row;
4882 rect.end_row = row + 1;
4883 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
4884 }
4885}
4886
4887/*
4888 * "term_getscrolled(buf)" function
4889 */
4890 void
4891f_term_getscrolled(typval_T *argvars, typval_T *rettv)
4892{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004893 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004894
4895 if (buf == NULL)
4896 return;
4897 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
4898}
4899
4900/*
4901 * "term_getsize(buf)" function
4902 */
4903 void
4904f_term_getsize(typval_T *argvars, typval_T *rettv)
4905{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004906 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004907 list_T *l;
4908
4909 if (rettv_list_alloc(rettv) == FAIL)
4910 return;
4911 if (buf == NULL)
4912 return;
4913
4914 l = rettv->vval.v_list;
4915 list_append_number(l, buf->b_term->tl_rows);
4916 list_append_number(l, buf->b_term->tl_cols);
4917}
4918
4919/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02004920 * "term_setsize(buf, rows, cols)" function
4921 */
4922 void
4923f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
4924{
4925 buf_T *buf = term_get_buf(argvars, "term_setsize()");
4926 term_T *term;
4927 varnumber_T rows, cols;
4928
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02004929 if (buf == NULL)
4930 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004931 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02004932 return;
4933 }
4934 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02004935 return;
4936 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004937 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02004938 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004939 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02004940 cols = cols <= 0 ? term->tl_cols : cols;
4941 vterm_set_size(term->tl_vterm, rows, cols);
4942 /* handle_resize() will resize the windows */
4943
4944 /* Get and remember the size we ended up with. Update the pty. */
4945 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
4946 term_report_winsize(term, term->tl_rows, term->tl_cols);
4947}
4948
4949/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004950 * "term_getstatus(buf)" function
4951 */
4952 void
4953f_term_getstatus(typval_T *argvars, typval_T *rettv)
4954{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004955 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004956 term_T *term;
4957 char_u val[100];
4958
4959 rettv->v_type = VAR_STRING;
4960 if (buf == NULL)
4961 return;
4962 term = buf->b_term;
4963
4964 if (term_job_running(term))
4965 STRCPY(val, "running");
4966 else
4967 STRCPY(val, "finished");
4968 if (term->tl_normal_mode)
4969 STRCAT(val, ",normal");
4970 rettv->vval.v_string = vim_strsave(val);
4971}
4972
4973/*
4974 * "term_gettitle(buf)" function
4975 */
4976 void
4977f_term_gettitle(typval_T *argvars, typval_T *rettv)
4978{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004979 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004980
4981 rettv->v_type = VAR_STRING;
4982 if (buf == NULL)
4983 return;
4984
4985 if (buf->b_term->tl_title != NULL)
4986 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
4987}
4988
4989/*
4990 * "term_gettty(buf)" function
4991 */
4992 void
4993f_term_gettty(typval_T *argvars, typval_T *rettv)
4994{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004995 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02004996 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004997 int num = 0;
4998
4999 rettv->v_type = VAR_STRING;
5000 if (buf == NULL)
5001 return;
5002 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005003 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005004
5005 switch (num)
5006 {
5007 case 0:
5008 if (buf->b_term->tl_job != NULL)
5009 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005010 break;
5011 case 1:
5012 if (buf->b_term->tl_job != NULL)
5013 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005014 break;
5015 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005016 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005017 return;
5018 }
5019 if (p != NULL)
5020 rettv->vval.v_string = vim_strsave(p);
5021}
5022
5023/*
5024 * "term_list()" function
5025 */
5026 void
5027f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5028{
5029 term_T *tp;
5030 list_T *l;
5031
5032 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5033 return;
5034
5035 l = rettv->vval.v_list;
5036 for (tp = first_term; tp != NULL; tp = tp->tl_next)
5037 if (tp != NULL && tp->tl_buffer != NULL)
5038 if (list_append_number(l,
5039 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5040 return;
5041}
5042
5043/*
5044 * "term_scrape(buf, row)" function
5045 */
5046 void
5047f_term_scrape(typval_T *argvars, typval_T *rettv)
5048{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005049 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005050 VTermScreen *screen = NULL;
5051 VTermPos pos;
5052 list_T *l;
5053 term_T *term;
5054 char_u *p;
5055 sb_line_T *line;
5056
5057 if (rettv_list_alloc(rettv) == FAIL)
5058 return;
5059 if (buf == NULL)
5060 return;
5061 term = buf->b_term;
5062
5063 l = rettv->vval.v_list;
5064 pos.row = get_row_number(&argvars[1], term);
5065
5066 if (term->tl_vterm != NULL)
5067 {
5068 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005069 if (screen == NULL) // can't really happen
5070 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005071 p = NULL;
5072 line = NULL;
5073 }
5074 else
5075 {
5076 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5077
5078 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5079 return;
5080 p = ml_get_buf(buf, lnum + 1, FALSE);
5081 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5082 }
5083
5084 for (pos.col = 0; pos.col < term->tl_cols; )
5085 {
5086 dict_T *dcell;
5087 int width;
5088 VTermScreenCellAttrs attrs;
5089 VTermColor fg, bg;
5090 char_u rgb[8];
5091 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5092 int off = 0;
5093 int i;
5094
5095 if (screen == NULL)
5096 {
5097 cellattr_T *cellattr;
5098 int len;
5099
5100 /* vterm has finished, get the cell from scrollback */
5101 if (pos.col >= line->sb_cols)
5102 break;
5103 cellattr = line->sb_cells + pos.col;
5104 width = cellattr->width;
5105 attrs = cellattr->attrs;
5106 fg = cellattr->fg;
5107 bg = cellattr->bg;
5108 len = MB_PTR2LEN(p);
5109 mch_memmove(mbs, p, len);
5110 mbs[len] = NUL;
5111 p += len;
5112 }
5113 else
5114 {
5115 VTermScreenCell cell;
5116 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5117 break;
5118 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5119 {
5120 if (cell.chars[i] == 0)
5121 break;
5122 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5123 }
5124 mbs[off] = NUL;
5125 width = cell.width;
5126 attrs = cell.attrs;
5127 fg = cell.fg;
5128 bg = cell.bg;
5129 }
5130 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005131 if (dcell == NULL)
5132 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005133 list_append_dict(l, dcell);
5134
Bram Moolenaare0be1672018-07-08 16:50:37 +02005135 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005136
5137 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5138 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005139 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005140 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5141 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005142 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005143
Bram Moolenaare0be1672018-07-08 16:50:37 +02005144 dict_add_number(dcell, "attr", cell2attr(attrs, fg, bg));
5145 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005146
5147 ++pos.col;
5148 if (width == 2)
5149 ++pos.col;
5150 }
5151}
5152
5153/*
5154 * "term_sendkeys(buf, keys)" function
5155 */
5156 void
5157f_term_sendkeys(typval_T *argvars, typval_T *rettv)
5158{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005159 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005160 char_u *msg;
5161 term_T *term;
5162
5163 rettv->v_type = VAR_UNKNOWN;
5164 if (buf == NULL)
5165 return;
5166
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005167 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005168 if (msg == NULL)
5169 return;
5170 term = buf->b_term;
5171 if (term->tl_vterm == NULL)
5172 return;
5173
5174 while (*msg != NUL)
5175 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005176 int c;
5177
5178 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5179 {
5180 c = TO_SPECIAL(msg[1], msg[2]);
5181 msg += 3;
5182 }
5183 else
5184 {
5185 c = PTR2CHAR(msg);
5186 msg += MB_CPTR2LEN(msg);
5187 }
5188 send_keys_to_term(term, c, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005189 }
5190}
5191
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005192#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5193/*
5194 * "term_getansicolors(buf)" function
5195 */
5196 void
5197f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5198{
5199 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5200 term_T *term;
5201 VTermState *state;
5202 VTermColor color;
5203 char_u hexbuf[10];
5204 int index;
5205 list_T *list;
5206
5207 if (rettv_list_alloc(rettv) == FAIL)
5208 return;
5209
5210 if (buf == NULL)
5211 return;
5212 term = buf->b_term;
5213 if (term->tl_vterm == NULL)
5214 return;
5215
5216 list = rettv->vval.v_list;
5217 state = vterm_obtain_state(term->tl_vterm);
5218 for (index = 0; index < 16; index++)
5219 {
5220 vterm_state_get_palette_color(state, index, &color);
5221 sprintf((char *)hexbuf, "#%02x%02x%02x",
5222 color.red, color.green, color.blue);
5223 if (list_append_string(list, hexbuf, 7) == FAIL)
5224 return;
5225 }
5226}
5227
5228/*
5229 * "term_setansicolors(buf, list)" function
5230 */
5231 void
5232f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5233{
5234 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5235 term_T *term;
5236
5237 if (buf == NULL)
5238 return;
5239 term = buf->b_term;
5240 if (term->tl_vterm == NULL)
5241 return;
5242
5243 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
5244 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005245 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005246 return;
5247 }
5248
5249 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005250 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005251}
5252#endif
5253
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005254/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005255 * "term_setrestore(buf, command)" function
5256 */
5257 void
5258f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5259{
5260#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005261 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005262 term_T *term;
5263 char_u *cmd;
5264
5265 if (buf == NULL)
5266 return;
5267 term = buf->b_term;
5268 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005269 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005270 if (cmd != NULL)
5271 term->tl_command = vim_strsave(cmd);
5272 else
5273 term->tl_command = NULL;
5274#endif
5275}
5276
5277/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005278 * "term_setkill(buf, how)" function
5279 */
5280 void
5281f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5282{
5283 buf_T *buf = term_get_buf(argvars, "term_setkill()");
5284 term_T *term;
5285 char_u *how;
5286
5287 if (buf == NULL)
5288 return;
5289 term = buf->b_term;
5290 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005291 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005292 if (how != NULL)
5293 term->tl_kill = vim_strsave(how);
5294 else
5295 term->tl_kill = NULL;
5296}
5297
5298/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005299 * "term_start(command, options)" function
5300 */
5301 void
5302f_term_start(typval_T *argvars, typval_T *rettv)
5303{
5304 jobopt_T opt;
5305 buf_T *buf;
5306
5307 init_job_options(&opt);
5308 if (argvars[1].v_type != VAR_UNKNOWN
5309 && get_job_options(&argvars[1], &opt,
5310 JO_TIMEOUT_ALL + JO_STOPONEXIT
5311 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
5312 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
5313 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
5314 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005315 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005316 + JO2_NORESTORE + JO2_TERM_KILL
5317 + JO2_ANSI_COLORS) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005318 return;
5319
Bram Moolenaar13568252018-03-16 20:46:58 +01005320 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005321
5322 if (buf != NULL && buf->b_term != NULL)
5323 rettv->vval.v_number = buf->b_fnum;
5324}
5325
5326/*
5327 * "term_wait" function
5328 */
5329 void
5330f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
5331{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005332 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005333
5334 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005335 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005336 if (buf->b_term->tl_job == NULL)
5337 {
5338 ch_log(NULL, "term_wait(): no job to wait for");
5339 return;
5340 }
5341 if (buf->b_term->tl_job->jv_channel == NULL)
5342 /* channel is closed, nothing to do */
5343 return;
5344
5345 /* Get the job status, this will detect a job that finished. */
Bram Moolenaara15ef452018-02-09 16:46:00 +01005346 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005347 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
5348 {
5349 /* The job is dead, keep reading channel I/O until the channel is
5350 * closed. buf->b_term may become NULL if the terminal was closed while
5351 * waiting. */
5352 ch_log(NULL, "term_wait(): waiting for channel to close");
5353 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
5354 {
5355 mch_check_messages();
5356 parse_queued_messages();
Bram Moolenaard45aa552018-05-21 22:50:29 +02005357 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01005358 if (!buf_valid(buf))
5359 /* If the terminal is closed when the channel is closed the
5360 * buffer disappears. */
5361 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005362 }
5363 mch_check_messages();
5364 parse_queued_messages();
5365 }
5366 else
5367 {
5368 long wait = 10L;
5369
5370 mch_check_messages();
5371 parse_queued_messages();
5372
5373 /* Wait for some time for any channel I/O. */
5374 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005375 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005376 ui_delay(wait, TRUE);
5377 mch_check_messages();
5378
5379 /* Flushing messages on channels is hopefully sufficient.
5380 * TODO: is there a better way? */
5381 parse_queued_messages();
5382 }
5383}
5384
5385/*
5386 * Called when a channel has sent all the lines to a terminal.
5387 * Send a CTRL-D to mark the end of the text.
5388 */
5389 void
5390term_send_eof(channel_T *ch)
5391{
5392 term_T *term;
5393
5394 for (term = first_term; term != NULL; term = term->tl_next)
5395 if (term->tl_job == ch->ch_job)
5396 {
5397 if (term->tl_eof_chars != NULL)
5398 {
5399 channel_send(ch, PART_IN, term->tl_eof_chars,
5400 (int)STRLEN(term->tl_eof_chars), NULL);
5401 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
5402 }
5403# ifdef WIN3264
5404 else
5405 /* Default: CTRL-D */
5406 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
5407# endif
5408 }
5409}
5410
Bram Moolenaarf9c38832018-06-19 19:59:20 +02005411 job_T *
5412term_getjob(term_T *term)
5413{
5414 return term != NULL ? term->tl_job : NULL;
5415}
5416
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005417# if defined(WIN3264) || defined(PROTO)
5418
5419/**************************************
5420 * 2. MS-Windows implementation.
5421 */
5422
5423# ifndef PROTO
5424
5425#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
5426#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01005427#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005428
5429void* (*winpty_config_new)(UINT64, void*);
5430void* (*winpty_open)(void*, void*);
5431void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
5432BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
5433void (*winpty_config_set_mouse_mode)(void*, int);
5434void (*winpty_config_set_initial_size)(void*, int, int);
5435LPCWSTR (*winpty_conin_name)(void*);
5436LPCWSTR (*winpty_conout_name)(void*);
5437LPCWSTR (*winpty_conerr_name)(void*);
5438void (*winpty_free)(void*);
5439void (*winpty_config_free)(void*);
5440void (*winpty_spawn_config_free)(void*);
5441void (*winpty_error_free)(void*);
5442LPCWSTR (*winpty_error_msg)(void*);
5443BOOL (*winpty_set_size)(void*, int, int, void*);
5444HANDLE (*winpty_agent_process)(void*);
5445
5446#define WINPTY_DLL "winpty.dll"
5447
5448static HINSTANCE hWinPtyDLL = NULL;
5449# endif
5450
5451 static int
5452dyn_winpty_init(int verbose)
5453{
5454 int i;
5455 static struct
5456 {
5457 char *name;
5458 FARPROC *ptr;
5459 } winpty_entry[] =
5460 {
5461 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
5462 {"winpty_config_free", (FARPROC*)&winpty_config_free},
5463 {"winpty_config_new", (FARPROC*)&winpty_config_new},
5464 {"winpty_config_set_mouse_mode",
5465 (FARPROC*)&winpty_config_set_mouse_mode},
5466 {"winpty_config_set_initial_size",
5467 (FARPROC*)&winpty_config_set_initial_size},
5468 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
5469 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
5470 {"winpty_error_free", (FARPROC*)&winpty_error_free},
5471 {"winpty_free", (FARPROC*)&winpty_free},
5472 {"winpty_open", (FARPROC*)&winpty_open},
5473 {"winpty_spawn", (FARPROC*)&winpty_spawn},
5474 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
5475 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
5476 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
5477 {"winpty_set_size", (FARPROC*)&winpty_set_size},
5478 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
5479 {NULL, NULL}
5480 };
5481
5482 /* No need to initialize twice. */
5483 if (hWinPtyDLL)
5484 return OK;
5485 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
5486 * winpty.dll. */
5487 if (*p_winptydll != NUL)
5488 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
5489 if (!hWinPtyDLL)
5490 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
5491 if (!hWinPtyDLL)
5492 {
5493 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005494 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005495 : (char_u *)WINPTY_DLL);
5496 return FAIL;
5497 }
5498 for (i = 0; winpty_entry[i].name != NULL
5499 && winpty_entry[i].ptr != NULL; ++i)
5500 {
5501 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
5502 winpty_entry[i].name)) == NULL)
5503 {
5504 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005505 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005506 return FAIL;
5507 }
5508 }
5509
5510 return OK;
5511}
5512
5513/*
5514 * Create a new terminal of "rows" by "cols" cells.
5515 * Store a reference in "term".
5516 * Return OK or FAIL.
5517 */
5518 static int
5519term_and_job_init(
5520 term_T *term,
5521 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01005522 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02005523 jobopt_T *opt,
5524 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005525{
5526 WCHAR *cmd_wchar = NULL;
5527 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005528 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005529 channel_T *channel = NULL;
5530 job_T *job = NULL;
5531 DWORD error;
5532 HANDLE jo = NULL;
5533 HANDLE child_process_handle;
5534 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01005535 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005536 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005537 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005538 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005539
5540 if (dyn_winpty_init(TRUE) == FAIL)
5541 return FAIL;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005542 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
5543 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005544
5545 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005546 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005547 cmd = argvar->vval.v_string;
5548 }
5549 else if (argvar->v_type == VAR_LIST)
5550 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005551 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005552 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005553 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005554 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005555 if (cmd == NULL || *cmd == NUL)
5556 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005557 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005558 goto failed;
5559 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005560
5561 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005562 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005563 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005564 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005565 if (opt->jo_cwd != NULL)
5566 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005567
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01005568 win32_build_env(opt->jo_env, &ga_env, TRUE);
5569 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005570
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005571 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
5572 if (term->tl_winpty_config == NULL)
5573 goto failed;
5574
5575 winpty_config_set_mouse_mode(term->tl_winpty_config,
5576 WINPTY_MOUSE_MODE_FORCE);
5577 winpty_config_set_initial_size(term->tl_winpty_config,
5578 term->tl_cols, term->tl_rows);
5579 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
5580 if (term->tl_winpty == NULL)
5581 goto failed;
5582
5583 spawn_config = winpty_spawn_config_new(
5584 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
5585 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
5586 NULL,
5587 cmd_wchar,
5588 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01005589 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005590 &winpty_err);
5591 if (spawn_config == NULL)
5592 goto failed;
5593
5594 channel = add_channel();
5595 if (channel == NULL)
5596 goto failed;
5597
5598 job = job_alloc();
5599 if (job == NULL)
5600 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02005601 if (argvar->v_type == VAR_STRING)
5602 {
5603 int argc;
5604
5605 build_argv_from_string(cmd, &job->jv_argv, &argc);
5606 }
5607 else
5608 {
5609 int argc;
5610
5611 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
5612 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005613
5614 if (opt->jo_set & JO_IN_BUF)
5615 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
5616
5617 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
5618 &child_thread_handle, &error, &winpty_err))
5619 goto failed;
5620
5621 channel_set_pipes(channel,
5622 (sock_T)CreateFileW(
5623 winpty_conin_name(term->tl_winpty),
5624 GENERIC_WRITE, 0, NULL,
5625 OPEN_EXISTING, 0, NULL),
5626 (sock_T)CreateFileW(
5627 winpty_conout_name(term->tl_winpty),
5628 GENERIC_READ, 0, NULL,
5629 OPEN_EXISTING, 0, NULL),
5630 (sock_T)CreateFileW(
5631 winpty_conerr_name(term->tl_winpty),
5632 GENERIC_READ, 0, NULL,
5633 OPEN_EXISTING, 0, NULL));
5634
5635 /* Write lines with CR instead of NL. */
5636 channel->ch_write_text_mode = TRUE;
5637
5638 jo = CreateJobObject(NULL, NULL);
5639 if (jo == NULL)
5640 goto failed;
5641
5642 if (!AssignProcessToJobObject(jo, child_process_handle))
5643 {
5644 /* Failed, switch the way to terminate process with TerminateProcess. */
5645 CloseHandle(jo);
5646 jo = NULL;
5647 }
5648
5649 winpty_spawn_config_free(spawn_config);
5650 vim_free(cmd_wchar);
5651 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005652 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005653
Bram Moolenaarcd929f72018-12-24 21:38:45 +01005654 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
5655 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005656
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005657#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5658 if (opt->jo_set2 & JO2_ANSI_COLORS)
5659 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
5660 else
5661 init_vterm_ansi_colors(term->tl_vterm);
5662#endif
5663
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005664 channel_set_job(channel, job, opt);
5665 job_set_options(job, opt);
5666
5667 job->jv_channel = channel;
5668 job->jv_proc_info.hProcess = child_process_handle;
5669 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
5670 job->jv_job_object = jo;
5671 job->jv_status = JOB_STARTED;
5672 job->jv_tty_in = utf16_to_enc(
5673 (short_u*)winpty_conin_name(term->tl_winpty), NULL);
5674 job->jv_tty_out = utf16_to_enc(
5675 (short_u*)winpty_conout_name(term->tl_winpty), NULL);
5676 ++job->jv_refcount;
5677 term->tl_job = job;
5678
Bram Moolenaarf25329c2018-05-06 21:49:32 +02005679 /* Redirecting stdout and stderr doesn't work at the job level. Instead
5680 * open the file here and handle it in. opt->jo_io was changed in
5681 * setup_job_options(), use the original flags here. */
5682 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
5683 {
5684 char_u *fname = opt->jo_io_name[PART_OUT];
5685
5686 ch_log(channel, "Opening output file %s", fname);
5687 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
5688 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005689 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02005690 }
5691
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005692 return OK;
5693
5694failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01005695 ga_clear(&ga_cmd);
5696 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005697 vim_free(cmd_wchar);
5698 vim_free(cwd_wchar);
5699 if (spawn_config != NULL)
5700 winpty_spawn_config_free(spawn_config);
5701 if (channel != NULL)
5702 channel_clear(channel);
5703 if (job != NULL)
5704 {
5705 job->jv_channel = NULL;
5706 job_cleanup(job);
5707 }
5708 term->tl_job = NULL;
5709 if (jo != NULL)
5710 CloseHandle(jo);
5711 if (term->tl_winpty != NULL)
5712 winpty_free(term->tl_winpty);
5713 term->tl_winpty = NULL;
5714 if (term->tl_winpty_config != NULL)
5715 winpty_config_free(term->tl_winpty_config);
5716 term->tl_winpty_config = NULL;
5717 if (winpty_err != NULL)
5718 {
5719 char_u *msg = utf16_to_enc(
5720 (short_u *)winpty_error_msg(winpty_err), NULL);
5721
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005722 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005723 winpty_error_free(winpty_err);
5724 }
5725 return FAIL;
5726}
5727
5728 static int
5729create_pty_only(term_T *term, jobopt_T *options)
5730{
5731 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
5732 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
5733 char in_name[80], out_name[80];
5734 channel_T *channel = NULL;
5735
Bram Moolenaarcd929f72018-12-24 21:38:45 +01005736 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
5737 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005738
5739 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
5740 GetCurrentProcessId(),
5741 curbuf->b_fnum);
5742 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
5743 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
5744 PIPE_UNLIMITED_INSTANCES,
5745 0, 0, NMPWAIT_NOWAIT, NULL);
5746 if (hPipeIn == INVALID_HANDLE_VALUE)
5747 goto failed;
5748
5749 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
5750 GetCurrentProcessId(),
5751 curbuf->b_fnum);
5752 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
5753 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
5754 PIPE_UNLIMITED_INSTANCES,
5755 0, 0, 0, NULL);
5756 if (hPipeOut == INVALID_HANDLE_VALUE)
5757 goto failed;
5758
5759 ConnectNamedPipe(hPipeIn, NULL);
5760 ConnectNamedPipe(hPipeOut, NULL);
5761
5762 term->tl_job = job_alloc();
5763 if (term->tl_job == NULL)
5764 goto failed;
5765 ++term->tl_job->jv_refcount;
5766
5767 /* behave like the job is already finished */
5768 term->tl_job->jv_status = JOB_FINISHED;
5769
5770 channel = add_channel();
5771 if (channel == NULL)
5772 goto failed;
5773 term->tl_job->jv_channel = channel;
5774 channel->ch_keep_open = TRUE;
5775 channel->ch_named_pipe = TRUE;
5776
5777 channel_set_pipes(channel,
5778 (sock_T)hPipeIn,
5779 (sock_T)hPipeOut,
5780 (sock_T)hPipeOut);
5781 channel_set_job(channel, term->tl_job, options);
5782 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
5783 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
5784
5785 return OK;
5786
5787failed:
5788 if (hPipeIn != NULL)
5789 CloseHandle(hPipeIn);
5790 if (hPipeOut != NULL)
5791 CloseHandle(hPipeOut);
5792 return FAIL;
5793}
5794
5795/*
5796 * Free the terminal emulator part of "term".
5797 */
5798 static void
5799term_free_vterm(term_T *term)
5800{
5801 if (term->tl_winpty != NULL)
5802 winpty_free(term->tl_winpty);
5803 term->tl_winpty = NULL;
5804 if (term->tl_winpty_config != NULL)
5805 winpty_config_free(term->tl_winpty_config);
5806 term->tl_winpty_config = NULL;
5807 if (term->tl_vterm != NULL)
5808 vterm_free(term->tl_vterm);
5809 term->tl_vterm = NULL;
5810}
5811
5812/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005813 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005814 */
5815 static void
5816term_report_winsize(term_T *term, int rows, int cols)
5817{
5818 if (term->tl_winpty)
5819 winpty_set_size(term->tl_winpty, cols, rows, NULL);
5820}
5821
5822 int
5823terminal_enabled(void)
5824{
5825 return dyn_winpty_init(FALSE) == OK;
5826}
5827
5828# else
5829
5830/**************************************
5831 * 3. Unix-like implementation.
5832 */
5833
5834/*
5835 * Create a new terminal of "rows" by "cols" cells.
5836 * Start job for "cmd".
5837 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01005838 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005839 * Return OK or FAIL.
5840 */
5841 static int
5842term_and_job_init(
5843 term_T *term,
5844 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01005845 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02005846 jobopt_T *opt,
5847 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005848{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01005849 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
5850 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005851
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005852#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
5853 if (opt->jo_set2 & JO2_ANSI_COLORS)
5854 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
5855 else
5856 init_vterm_ansi_colors(term->tl_vterm);
5857#endif
5858
Bram Moolenaar13568252018-03-16 20:46:58 +01005859 /* This may change a string in "argvar". */
Bram Moolenaar493359e2018-06-12 20:25:52 +02005860 term->tl_job = job_start(argvar, argv, opt, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005861 if (term->tl_job != NULL)
5862 ++term->tl_job->jv_refcount;
5863
5864 return term->tl_job != NULL
5865 && term->tl_job->jv_channel != NULL
5866 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
5867}
5868
5869 static int
5870create_pty_only(term_T *term, jobopt_T *opt)
5871{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01005872 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
5873 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005874
5875 term->tl_job = job_alloc();
5876 if (term->tl_job == NULL)
5877 return FAIL;
5878 ++term->tl_job->jv_refcount;
5879
5880 /* behave like the job is already finished */
5881 term->tl_job->jv_status = JOB_FINISHED;
5882
5883 return mch_create_pty_channel(term->tl_job, opt);
5884}
5885
5886/*
5887 * Free the terminal emulator part of "term".
5888 */
5889 static void
5890term_free_vterm(term_T *term)
5891{
5892 if (term->tl_vterm != NULL)
5893 vterm_free(term->tl_vterm);
5894 term->tl_vterm = NULL;
5895}
5896
5897/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005898 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005899 */
5900 static void
5901term_report_winsize(term_T *term, int rows, int cols)
5902{
5903 /* Use an ioctl() to report the new window size to the job. */
5904 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
5905 {
5906 int fd = -1;
5907 int part;
5908
5909 for (part = PART_OUT; part < PART_COUNT; ++part)
5910 {
5911 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
5912 if (isatty(fd))
5913 break;
5914 }
5915 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
5916 mch_signal_job(term->tl_job, (char_u *)"winch");
5917 }
5918}
5919
5920# endif
5921
5922#endif /* FEAT_TERMINAL */