blob: a992b444dcd0f3b70882e0fa967821b14379cecd [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 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +010063 int sb_cols; // can differ per line
64 cellattr_T *sb_cells; // allocated
65 cellattr_T sb_fill_attr; // for short line
66 char_u *sb_text; // for tl_scrollback_postponed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020067} sb_line_T;
68
Bram Moolenaar4f974752019-02-17 17:44:42 +010069#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +010070# ifndef HPCON
71# define HPCON VOID*
72# endif
73# ifndef EXTENDED_STARTUPINFO_PRESENT
74# define EXTENDED_STARTUPINFO_PRESENT 0x00080000
75# endif
76# ifndef PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE
77# define PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE 0x00020016
78# endif
79typedef struct _DYN_STARTUPINFOEXW
80{
81 STARTUPINFOW StartupInfo;
82 LPPROC_THREAD_ATTRIBUTE_LIST lpAttributeList;
83} DYN_STARTUPINFOEXW, *PDYN_STARTUPINFOEXW;
84#endif
85
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020086/* typedef term_T in structs.h */
87struct terminal_S {
88 term_T *tl_next;
89
90 VTerm *tl_vterm;
91 job_T *tl_job;
92 buf_T *tl_buffer;
Bram Moolenaar13568252018-03-16 20:46:58 +010093#if defined(FEAT_GUI)
94 int tl_system; /* when non-zero used for :!cmd output */
95 int tl_toprow; /* row with first line of system terminal */
96#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +020097
98 /* Set when setting the size of a vterm, reset after redrawing. */
99 int tl_vterm_size_changed;
100
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200101 int tl_normal_mode; /* TRUE: Terminal-Normal mode */
102 int tl_channel_closed;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +0200103 int tl_channel_recently_closed; // still need to handle tl_finish
104
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100105 int tl_finish;
106#define TL_FINISH_UNSET NUL
107#define TL_FINISH_CLOSE 'c' /* ++close or :terminal without argument */
108#define TL_FINISH_NOCLOSE 'n' /* ++noclose */
109#define TL_FINISH_OPEN 'o' /* ++open */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200110 char_u *tl_opencmd;
111 char_u *tl_eof_chars;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200112 char_u *tl_api; // prefix for terminal API function
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200113
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100114 char_u *tl_arg0_cmd; // To format the status bar
115
Bram Moolenaar4f974752019-02-17 17:44:42 +0100116#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200117 void *tl_winpty_config;
118 void *tl_winpty;
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200119
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100120 HPCON tl_conpty;
121 DYN_STARTUPINFOEXW tl_siex; // Structure that always needs to be hold
122
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200123 FILE *tl_out_fd;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200124#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100125#if defined(FEAT_SESSION)
126 char_u *tl_command;
127#endif
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100128 char_u *tl_kill;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200129
130 /* last known vterm size */
131 int tl_rows;
132 int tl_cols;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200133
134 char_u *tl_title; /* NULL or allocated */
135 char_u *tl_status_text; /* NULL or allocated */
136
137 /* Range of screen rows to update. Zero based. */
Bram Moolenaar3a497e12017-09-30 20:40:27 +0200138 int tl_dirty_row_start; /* MAX_ROW if nothing dirty */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200139 int tl_dirty_row_end; /* row below last one to update */
Bram Moolenaar56bc8e22018-05-10 18:05:56 +0200140 int tl_dirty_snapshot; /* text updated after making snapshot */
141#ifdef FEAT_TIMERS
142 int tl_timer_set;
143 proftime_T tl_timer_due;
144#endif
Bram Moolenaar6eddadf2018-05-06 16:40:16 +0200145 int tl_postponed_scroll; /* to be scrolled up */
146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200147 garray_T tl_scrollback;
148 int tl_scrollback_scrolled;
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100149 garray_T tl_scrollback_postponed;
150
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200151 cellattr_T tl_default_color;
152
Bram Moolenaard96ff162018-02-18 22:13:29 +0100153 linenr_T tl_top_diff_rows; /* rows of top diff file or zero */
154 linenr_T tl_bot_diff_rows; /* rows of bottom diff file */
155
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200156 VTermPos tl_cursor_pos;
157 int tl_cursor_visible;
158 int tl_cursor_blink;
159 int tl_cursor_shape; /* 1: block, 2: underline, 3: bar */
160 char_u *tl_cursor_color; /* NULL or allocated */
161
162 int tl_using_altscreen;
163};
164
165#define TMODE_ONCE 1 /* CTRL-\ CTRL-N used */
166#define TMODE_LOOP 2 /* CTRL-W N used */
167
168/*
169 * List of all active terminals.
170 */
171static term_T *first_term = NULL;
172
173/* Terminal active in terminal_loop(). */
174static term_T *in_terminal_loop = NULL;
175
Bram Moolenaar4f974752019-02-17 17:44:42 +0100176#ifdef MSWIN
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100177static BOOL has_winpty = FALSE;
178static BOOL has_conpty = FALSE;
179#endif
180
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200181#define MAX_ROW 999999 /* used for tl_dirty_row_end to update all rows */
182#define KEY_BUF_LEN 200
183
184/*
185 * Functions with separate implementation for MS-Windows and Unix-like systems.
186 */
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200187static 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 +0200188static int create_pty_only(term_T *term, jobopt_T *opt);
189static void term_report_winsize(term_T *term, int rows, int cols);
190static void term_free_vterm(term_T *term);
Bram Moolenaar13568252018-03-16 20:46:58 +0100191#ifdef FEAT_GUI
192static void update_system_term(term_T *term);
193#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200194
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100195static void handle_postponed_scrollback(term_T *term);
196
Bram Moolenaar26d205d2017-11-09 17:33:11 +0100197/* The character that we know (or assume) that the terminal expects for the
198 * backspace key. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200199static int term_backspace_char = BS;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200200
Bram Moolenaara7c54cf2017-12-01 21:07:20 +0100201/* "Terminal" highlight group colors. */
202static int term_default_cterm_fg = -1;
203static int term_default_cterm_bg = -1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200204
Bram Moolenaard317b382018-02-08 22:33:31 +0100205/* Store the last set and the desired cursor properties, so that we only update
206 * them when needed. Doing it unnecessary may result in flicker. */
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200207static char_u *last_set_cursor_color = NULL;
208static char_u *desired_cursor_color = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +0100209static int last_set_cursor_shape = -1;
210static int desired_cursor_shape = -1;
211static int last_set_cursor_blink = -1;
212static int desired_cursor_blink = -1;
213
214
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200215/**************************************
216 * 1. Generic code for all systems.
217 */
218
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200219 static int
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200220cursor_color_equal(char_u *lhs_color, char_u *rhs_color)
221{
222 if (lhs_color != NULL && rhs_color != NULL)
223 return STRCMP(lhs_color, rhs_color) == 0;
224 return lhs_color == NULL && rhs_color == NULL;
225}
226
Bram Moolenaar05af9a42018-05-21 18:48:12 +0200227 static void
228cursor_color_copy(char_u **to_color, char_u *from_color)
229{
230 // Avoid a free & alloc if the value is already right.
231 if (cursor_color_equal(*to_color, from_color))
232 return;
233 vim_free(*to_color);
234 *to_color = (from_color == NULL) ? NULL : vim_strsave(from_color);
235}
236
237 static char_u *
Bram Moolenaar4f7fd562018-05-21 14:55:28 +0200238cursor_color_get(char_u *color)
239{
240 return (color == NULL) ? (char_u *)"" : color;
241}
242
243
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200244/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200245 * Parse 'termwinsize' and set "rows" and "cols" for the terminal size in the
Bram Moolenaar498c2562018-04-15 23:45:15 +0200246 * current window.
247 * Sets "rows" and/or "cols" to zero when it should follow the window size.
248 * Return TRUE if the size is the minimum size: "24*80".
249 */
250 static int
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200251parse_termwinsize(win_T *wp, int *rows, int *cols)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200252{
253 int minsize = FALSE;
254
255 *rows = 0;
256 *cols = 0;
257
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200258 if (*wp->w_p_tws != NUL)
Bram Moolenaar498c2562018-04-15 23:45:15 +0200259 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200260 char_u *p = vim_strchr(wp->w_p_tws, 'x');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200261
262 /* Syntax of value was already checked when it's set. */
263 if (p == NULL)
264 {
265 minsize = TRUE;
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200266 p = vim_strchr(wp->w_p_tws, '*');
Bram Moolenaar498c2562018-04-15 23:45:15 +0200267 }
Bram Moolenaar6d150f72018-04-21 20:03:20 +0200268 *rows = atoi((char *)wp->w_p_tws);
Bram Moolenaar498c2562018-04-15 23:45:15 +0200269 *cols = atoi((char *)p + 1);
270 }
271 return minsize;
272}
273
274/*
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200275 * Determine the terminal size from 'termwinsize' and the current window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200276 */
277 static void
278set_term_and_win_size(term_T *term)
279{
Bram Moolenaar13568252018-03-16 20:46:58 +0100280#ifdef FEAT_GUI
281 if (term->tl_system)
282 {
283 /* Use the whole screen for the system command. However, it will start
284 * at the command line and scroll up as needed, using tl_toprow. */
285 term->tl_rows = Rows;
286 term->tl_cols = Columns;
Bram Moolenaar07b46af2018-04-10 14:56:18 +0200287 return;
Bram Moolenaar13568252018-03-16 20:46:58 +0100288 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100289#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +0200290 if (parse_termwinsize(curwin, &term->tl_rows, &term->tl_cols))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200291 {
Bram Moolenaar498c2562018-04-15 23:45:15 +0200292 if (term->tl_rows != 0)
293 term->tl_rows = MAX(term->tl_rows, curwin->w_height);
294 if (term->tl_cols != 0)
295 term->tl_cols = MAX(term->tl_cols, curwin->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200296 }
297 if (term->tl_rows == 0)
298 term->tl_rows = curwin->w_height;
299 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200300 win_setheight_win(term->tl_rows, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200301 if (term->tl_cols == 0)
302 term->tl_cols = curwin->w_width;
303 else
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200304 win_setwidth_win(term->tl_cols, curwin);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200305}
306
307/*
308 * Initialize job options for a terminal job.
309 * Caller may overrule some of them.
310 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100311 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200312init_job_options(jobopt_T *opt)
313{
314 clear_job_options(opt);
315
316 opt->jo_mode = MODE_RAW;
317 opt->jo_out_mode = MODE_RAW;
318 opt->jo_err_mode = MODE_RAW;
319 opt->jo_set = JO_MODE | JO_OUT_MODE | JO_ERR_MODE;
320}
321
322/*
323 * Set job options mandatory for a terminal job.
324 */
325 static void
326setup_job_options(jobopt_T *opt, int rows, int cols)
327{
Bram Moolenaar4f974752019-02-17 17:44:42 +0100328#ifndef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200329 /* Win32: Redirecting the job output won't work, thus always connect stdout
330 * here. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200331 if (!(opt->jo_set & JO_OUT_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200332#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200333 {
334 /* Connect stdout to the terminal. */
335 opt->jo_io[PART_OUT] = JIO_BUFFER;
336 opt->jo_io_buf[PART_OUT] = curbuf->b_fnum;
337 opt->jo_modifiable[PART_OUT] = 0;
338 opt->jo_set |= JO_OUT_IO + JO_OUT_BUF + JO_OUT_MODIFIABLE;
339 }
340
Bram Moolenaar4f974752019-02-17 17:44:42 +0100341#ifndef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200342 /* Win32: Redirecting the job output won't work, thus always connect stderr
343 * here. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200344 if (!(opt->jo_set & JO_ERR_IO))
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200345#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200346 {
347 /* Connect stderr to the terminal. */
348 opt->jo_io[PART_ERR] = JIO_BUFFER;
349 opt->jo_io_buf[PART_ERR] = curbuf->b_fnum;
350 opt->jo_modifiable[PART_ERR] = 0;
351 opt->jo_set |= JO_ERR_IO + JO_ERR_BUF + JO_ERR_MODIFIABLE;
352 }
353
354 opt->jo_pty = TRUE;
355 if ((opt->jo_set2 & JO2_TERM_ROWS) == 0)
356 opt->jo_term_rows = rows;
357 if ((opt->jo_set2 & JO2_TERM_COLS) == 0)
358 opt->jo_term_cols = cols;
359}
360
361/*
Bram Moolenaar5c381eb2019-06-25 06:50:31 +0200362 * Flush messages on channels.
363 */
364 static void
365term_flush_messages()
366{
367 mch_check_messages();
368 parse_queued_messages();
369}
370
371/*
Bram Moolenaard96ff162018-02-18 22:13:29 +0100372 * Close a terminal buffer (and its window). Used when creating the terminal
373 * fails.
374 */
375 static void
376term_close_buffer(buf_T *buf, buf_T *old_curbuf)
377{
378 free_terminal(buf);
379 if (old_curbuf != NULL)
380 {
381 --curbuf->b_nwindows;
382 curbuf = old_curbuf;
383 curwin->w_buffer = curbuf;
384 ++curbuf->b_nwindows;
385 }
386
387 /* Wiping out the buffer will also close the window and call
388 * free_terminal(). */
389 do_buffer(DOBUF_WIPE, DOBUF_FIRST, FORWARD, buf->b_fnum, TRUE);
390}
391
392/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200393 * Start a terminal window and return its buffer.
Bram Moolenaar13568252018-03-16 20:46:58 +0100394 * Use either "argvar" or "argv", the other must be NULL.
395 * When "flags" has TERM_START_NOJOB only create the buffer, b_term and open
396 * the window.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200397 * Returns NULL when failed.
398 */
Bram Moolenaar13568252018-03-16 20:46:58 +0100399 buf_T *
400term_start(
401 typval_T *argvar,
402 char **argv,
403 jobopt_T *opt,
404 int flags)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200405{
406 exarg_T split_ea;
407 win_T *old_curwin = curwin;
408 term_T *term;
409 buf_T *old_curbuf = NULL;
410 int res;
411 buf_T *newbuf;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100412 int vertical = opt->jo_vertical || (cmdmod.split & WSP_VERT);
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200413 jobopt_T orig_opt; // only partly filled
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200414
415 if (check_restricted() || check_secure())
416 return NULL;
417
418 if ((opt->jo_set & (JO_IN_IO + JO_OUT_IO + JO_ERR_IO))
419 == (JO_IN_IO + JO_OUT_IO + JO_ERR_IO)
420 || (!(opt->jo_set & JO_OUT_IO) && (opt->jo_set & JO_OUT_BUF))
421 || (!(opt->jo_set & JO_ERR_IO) && (opt->jo_set & JO_ERR_BUF)))
422 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100423 emsg(_(e_invarg));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200424 return NULL;
425 }
426
Bram Moolenaarc799fe22019-05-28 23:08:19 +0200427 term = ALLOC_CLEAR_ONE(term_T);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200428 if (term == NULL)
429 return NULL;
430 term->tl_dirty_row_end = MAX_ROW;
431 term->tl_cursor_visible = TRUE;
432 term->tl_cursor_shape = VTERM_PROP_CURSORSHAPE_BLOCK;
433 term->tl_finish = opt->jo_term_finish;
Bram Moolenaar13568252018-03-16 20:46:58 +0100434#ifdef FEAT_GUI
435 term->tl_system = (flags & TERM_START_SYSTEM);
436#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200437 ga_init2(&term->tl_scrollback, sizeof(sb_line_T), 300);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100438 ga_init2(&term->tl_scrollback_postponed, sizeof(sb_line_T), 300);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200439
440 vim_memset(&split_ea, 0, sizeof(split_ea));
441 if (opt->jo_curwin)
442 {
443 /* Create a new buffer in the current window. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100444 if (!can_abandon(curbuf, flags & TERM_START_FORCEIT))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200445 {
446 no_write_message();
447 vim_free(term);
448 return NULL;
449 }
450 if (do_ecmd(0, NULL, NULL, &split_ea, ECMD_ONE,
Bram Moolenaar13568252018-03-16 20:46:58 +0100451 ECMD_HIDE
452 + ((flags & TERM_START_FORCEIT) ? ECMD_FORCEIT : 0),
453 curwin) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200454 {
455 vim_free(term);
456 return NULL;
457 }
458 }
Bram Moolenaar13568252018-03-16 20:46:58 +0100459 else if (opt->jo_hidden || (flags & TERM_START_SYSTEM))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200460 {
461 buf_T *buf;
462
463 /* Create a new buffer without a window. Make it the current buffer for
464 * a moment to be able to do the initialisations. */
465 buf = buflist_new((char_u *)"", NULL, (linenr_T)0,
466 BLN_NEW | BLN_LISTED);
467 if (buf == NULL || ml_open(buf) == FAIL)
468 {
469 vim_free(term);
470 return NULL;
471 }
472 old_curbuf = curbuf;
473 --curbuf->b_nwindows;
474 curbuf = buf;
475 curwin->w_buffer = buf;
476 ++curbuf->b_nwindows;
477 }
478 else
479 {
480 /* Open a new window or tab. */
481 split_ea.cmdidx = CMD_new;
482 split_ea.cmd = (char_u *)"new";
483 split_ea.arg = (char_u *)"";
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100484 if (opt->jo_term_rows > 0 && !vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200485 {
486 split_ea.line2 = opt->jo_term_rows;
487 split_ea.addr_count = 1;
488 }
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100489 if (opt->jo_term_cols > 0 && vertical)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200490 {
491 split_ea.line2 = opt->jo_term_cols;
492 split_ea.addr_count = 1;
493 }
494
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100495 if (vertical)
496 cmdmod.split |= WSP_VERT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200497 ex_splitview(&split_ea);
498 if (curwin == old_curwin)
499 {
500 /* split failed */
501 vim_free(term);
502 return NULL;
503 }
504 }
505 term->tl_buffer = curbuf;
506 curbuf->b_term = term;
507
508 if (!opt->jo_hidden)
509 {
Bram Moolenaarda650582018-02-20 15:51:40 +0100510 /* Only one size was taken care of with :new, do the other one. With
511 * "curwin" both need to be done. */
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100512 if (opt->jo_term_rows > 0 && (opt->jo_curwin || vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200513 win_setheight(opt->jo_term_rows);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +0100514 if (opt->jo_term_cols > 0 && (opt->jo_curwin || !vertical))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200515 win_setwidth(opt->jo_term_cols);
516 }
517
518 /* Link the new terminal in the list of active terminals. */
519 term->tl_next = first_term;
520 first_term = term;
521
522 if (opt->jo_term_name != NULL)
523 curbuf->b_ffname = vim_strsave(opt->jo_term_name);
Bram Moolenaar13568252018-03-16 20:46:58 +0100524 else if (argv != NULL)
525 curbuf->b_ffname = vim_strsave((char_u *)"!system");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200526 else
527 {
528 int i;
529 size_t len;
530 char_u *cmd, *p;
531
532 if (argvar->v_type == VAR_STRING)
533 {
534 cmd = argvar->vval.v_string;
535 if (cmd == NULL)
536 cmd = (char_u *)"";
537 else if (STRCMP(cmd, "NONE") == 0)
538 cmd = (char_u *)"pty";
539 }
540 else if (argvar->v_type != VAR_LIST
541 || argvar->vval.v_list == NULL
542 || argvar->vval.v_list->lv_len < 1
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100543 || (cmd = tv_get_string_chk(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200544 &argvar->vval.v_list->lv_first->li_tv)) == NULL)
545 cmd = (char_u*)"";
546
547 len = STRLEN(cmd) + 10;
Bram Moolenaar51e14382019-05-25 20:21:28 +0200548 p = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200549
550 for (i = 0; p != NULL; ++i)
551 {
552 /* Prepend a ! to the command name to avoid the buffer name equals
553 * the executable, otherwise ":w!" would overwrite it. */
554 if (i == 0)
555 vim_snprintf((char *)p, len, "!%s", cmd);
556 else
557 vim_snprintf((char *)p, len, "!%s (%d)", cmd, i);
558 if (buflist_findname(p) == NULL)
559 {
560 vim_free(curbuf->b_ffname);
561 curbuf->b_ffname = p;
562 break;
563 }
564 }
565 }
566 curbuf->b_fname = curbuf->b_ffname;
567
568 if (opt->jo_term_opencmd != NULL)
569 term->tl_opencmd = vim_strsave(opt->jo_term_opencmd);
570
571 if (opt->jo_eof_chars != NULL)
572 term->tl_eof_chars = vim_strsave(opt->jo_eof_chars);
573
574 set_string_option_direct((char_u *)"buftype", -1,
575 (char_u *)"terminal", OPT_FREE|OPT_LOCAL, 0);
Bram Moolenaar7da1fb52018-08-04 16:54:11 +0200576 // Avoid that 'buftype' is reset when this buffer is entered.
577 curbuf->b_p_initialized = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200578
579 /* Mark the buffer as not modifiable. It can only be made modifiable after
580 * the job finished. */
581 curbuf->b_p_ma = FALSE;
582
583 set_term_and_win_size(term);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100584#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200585 mch_memmove(orig_opt.jo_io, opt->jo_io, sizeof(orig_opt.jo_io));
586#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200587 setup_job_options(opt, term->tl_rows, term->tl_cols);
588
Bram Moolenaar13568252018-03-16 20:46:58 +0100589 if (flags & TERM_START_NOJOB)
Bram Moolenaard96ff162018-02-18 22:13:29 +0100590 return curbuf;
591
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100592#if defined(FEAT_SESSION)
593 /* Remember the command for the session file. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100594 if (opt->jo_term_norestore || argv != NULL)
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100595 {
596 term->tl_command = vim_strsave((char_u *)"NONE");
597 }
598 else if (argvar->v_type == VAR_STRING)
599 {
600 char_u *cmd = argvar->vval.v_string;
601
602 if (cmd != NULL && STRCMP(cmd, p_sh) != 0)
603 term->tl_command = vim_strsave(cmd);
604 }
605 else if (argvar->v_type == VAR_LIST
606 && argvar->vval.v_list != NULL
607 && argvar->vval.v_list->lv_len > 0)
608 {
609 garray_T ga;
610 listitem_T *item;
611
612 ga_init2(&ga, 1, 100);
613 for (item = argvar->vval.v_list->lv_first;
614 item != NULL; item = item->li_next)
615 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +0100616 char_u *s = tv_get_string_chk(&item->li_tv);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100617 char_u *p;
618
619 if (s == NULL)
620 break;
621 p = vim_strsave_fnameescape(s, FALSE);
622 if (p == NULL)
623 break;
624 ga_concat(&ga, p);
625 vim_free(p);
626 ga_append(&ga, ' ');
627 }
628 if (item == NULL)
629 {
630 ga_append(&ga, NUL);
631 term->tl_command = ga.ga_data;
632 }
633 else
634 ga_clear(&ga);
635 }
636#endif
637
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100638 if (opt->jo_term_kill != NULL)
639 {
640 char_u *p = skiptowhite(opt->jo_term_kill);
641
642 term->tl_kill = vim_strnsave(opt->jo_term_kill, p - opt->jo_term_kill);
643 }
644
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200645 if (opt->jo_term_api != NULL)
646 term->tl_api = vim_strsave(opt->jo_term_api);
647 else
648 term->tl_api = vim_strsave((char_u *)"Tapi_");
649
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200650 /* System dependent: setup the vterm and maybe start the job in it. */
Bram Moolenaar13568252018-03-16 20:46:58 +0100651 if (argv == NULL
652 && argvar->v_type == VAR_STRING
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200653 && argvar->vval.v_string != NULL
654 && STRCMP(argvar->vval.v_string, "NONE") == 0)
655 res = create_pty_only(term, opt);
656 else
Bram Moolenaarf25329c2018-05-06 21:49:32 +0200657 res = term_and_job_init(term, argvar, argv, opt, &orig_opt);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200658
659 newbuf = curbuf;
660 if (res == OK)
661 {
662 /* Get and remember the size we ended up with. Update the pty. */
663 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
664 term_report_winsize(term, term->tl_rows, term->tl_cols);
Bram Moolenaar13568252018-03-16 20:46:58 +0100665#ifdef FEAT_GUI
666 if (term->tl_system)
667 {
668 /* display first line below typed command */
669 term->tl_toprow = msg_row + 1;
670 term->tl_dirty_row_end = 0;
671 }
672#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200673
674 /* Make sure we don't get stuck on sending keys to the job, it leads to
675 * a deadlock if the job is waiting for Vim to read. */
676 channel_set_nonblock(term->tl_job->jv_channel, PART_IN);
677
Bram Moolenaar606cb8b2018-05-03 20:40:20 +0200678 if (old_curbuf != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200679 {
680 --curbuf->b_nwindows;
681 curbuf = old_curbuf;
682 curwin->w_buffer = curbuf;
683 ++curbuf->b_nwindows;
684 }
685 }
686 else
687 {
Bram Moolenaard96ff162018-02-18 22:13:29 +0100688 term_close_buffer(curbuf, old_curbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200689 return NULL;
690 }
Bram Moolenaarb852c3e2018-03-11 16:55:36 +0100691
Bram Moolenaar13568252018-03-16 20:46:58 +0100692 apply_autocmds(EVENT_TERMINALOPEN, NULL, NULL, FALSE, newbuf);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200693 return newbuf;
694}
695
696/*
697 * ":terminal": open a terminal window and execute a job in it.
698 */
699 void
700ex_terminal(exarg_T *eap)
701{
702 typval_T argvar[2];
703 jobopt_T opt;
704 char_u *cmd;
705 char_u *tofree = NULL;
706
707 init_job_options(&opt);
708
709 cmd = eap->arg;
Bram Moolenaara15ef452018-02-09 16:46:00 +0100710 while (*cmd == '+' && *(cmd + 1) == '+')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200711 {
712 char_u *p, *ep;
713
714 cmd += 2;
715 p = skiptowhite(cmd);
716 ep = vim_strchr(cmd, '=');
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200717 if (ep != NULL)
718 {
719 if (ep < p)
720 p = ep;
721 else
722 ep = NULL;
723 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200724
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200725# define OPTARG_HAS(name) ((int)(p - cmd) == sizeof(name) - 1 \
726 && STRNICMP(cmd, name, sizeof(name) - 1) == 0)
727 if (OPTARG_HAS("close"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200728 opt.jo_term_finish = 'c';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200729 else if (OPTARG_HAS("noclose"))
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100730 opt.jo_term_finish = 'n';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200731 else if (OPTARG_HAS("open"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200732 opt.jo_term_finish = 'o';
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200733 else if (OPTARG_HAS("curwin"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200734 opt.jo_curwin = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200735 else if (OPTARG_HAS("hidden"))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200736 opt.jo_hidden = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200737 else if (OPTARG_HAS("norestore"))
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100738 opt.jo_term_norestore = 1;
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200739 else if (OPTARG_HAS("kill") && ep != NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100740 {
741 opt.jo_set2 |= JO2_TERM_KILL;
742 opt.jo_term_kill = ep + 1;
743 p = skiptowhite(cmd);
744 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200745 else if (OPTARG_HAS("api"))
746 {
747 opt.jo_set2 |= JO2_TERM_API;
748 if (ep != NULL)
749 {
750 opt.jo_term_api = ep + 1;
751 p = skiptowhite(cmd);
752 }
753 else
754 opt.jo_term_api = NULL;
755 }
756 else if (OPTARG_HAS("rows") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200757 {
758 opt.jo_set2 |= JO2_TERM_ROWS;
759 opt.jo_term_rows = atoi((char *)ep + 1);
760 p = skiptowhite(cmd);
761 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200762 else if (OPTARG_HAS("cols") && ep != NULL && isdigit(ep[1]))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200763 {
764 opt.jo_set2 |= JO2_TERM_COLS;
765 opt.jo_term_cols = atoi((char *)ep + 1);
766 p = skiptowhite(cmd);
767 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200768 else if (OPTARG_HAS("eof") && ep != NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200769 {
770 char_u *buf = NULL;
771 char_u *keys;
772
773 p = skiptowhite(cmd);
774 *p = NUL;
775 keys = replace_termcodes(ep + 1, &buf, TRUE, TRUE, TRUE);
776 opt.jo_set2 |= JO2_EOF_CHARS;
777 opt.jo_eof_chars = vim_strsave(keys);
778 vim_free(buf);
779 *p = ' ';
780 }
Bram Moolenaar4f974752019-02-17 17:44:42 +0100781#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100782 else if ((int)(p - cmd) == 4 && STRNICMP(cmd, "type", 4) == 0
783 && ep != NULL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100784 {
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100785 int tty_type = NUL;
786
787 p = skiptowhite(cmd);
788 if (STRNICMP(ep + 1, "winpty", p - (ep + 1)) == 0)
789 tty_type = 'w';
790 else if (STRNICMP(ep + 1, "conpty", p - (ep + 1)) == 0)
791 tty_type = 'c';
792 else
793 {
794 semsg(e_invargval, "type");
795 goto theend;
796 }
797 opt.jo_set2 |= JO2_TTY_TYPE;
798 opt.jo_tty_type = tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100799 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100800#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200801 else
802 {
803 if (*p)
804 *p = NUL;
Bram Moolenaarf9e3e092019-01-13 23:38:42 +0100805 semsg(_("E181: Invalid attribute: %s"), cmd);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100806 goto theend;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200807 }
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200808# undef OPTARG_HAS
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200809 cmd = skipwhite(p);
810 }
811 if (*cmd == NUL)
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100812 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200813 /* Make a copy of 'shell', an autocommand may change the option. */
814 tofree = cmd = vim_strsave(p_sh);
815
Bram Moolenaar1dd98332018-03-16 22:54:53 +0100816 /* default to close when the shell exits */
817 if (opt.jo_term_finish == NUL)
818 opt.jo_term_finish = 'c';
819 }
820
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200821 if (eap->addr_count > 0)
822 {
823 /* Write lines from current buffer to the job. */
824 opt.jo_set |= JO_IN_IO | JO_IN_BUF | JO_IN_TOP | JO_IN_BOT;
825 opt.jo_io[PART_IN] = JIO_BUFFER;
826 opt.jo_io_buf[PART_IN] = curbuf->b_fnum;
827 opt.jo_in_top = eap->line1;
828 opt.jo_in_bot = eap->line2;
829 }
830
831 argvar[0].v_type = VAR_STRING;
832 argvar[0].vval.v_string = cmd;
833 argvar[1].v_type = VAR_UNKNOWN;
Bram Moolenaar13568252018-03-16 20:46:58 +0100834 term_start(argvar, NULL, &opt, eap->forceit ? TERM_START_FORCEIT : 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200835 vim_free(tofree);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +0100836
837theend:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200838 vim_free(opt.jo_eof_chars);
839}
840
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100841#if defined(FEAT_SESSION) || defined(PROTO)
842/*
843 * Write a :terminal command to the session file to restore the terminal in
844 * window "wp".
845 * Return FAIL if writing fails.
846 */
847 int
848term_write_session(FILE *fd, win_T *wp)
849{
850 term_T *term = wp->w_buffer->b_term;
851
852 /* Create the terminal and run the command. This is not without
853 * risk, but let's assume the user only creates a session when this
854 * will be OK. */
855 if (fprintf(fd, "terminal ++curwin ++cols=%d ++rows=%d ",
856 term->tl_cols, term->tl_rows) < 0)
857 return FAIL;
Bram Moolenaar4f974752019-02-17 17:44:42 +0100858#ifdef MSWIN
Bram Moolenaarc6ddce32019-02-08 12:47:03 +0100859 if (fprintf(fd, "++type=%s ", term->tl_job->jv_tty_type) < 0)
860 return FAIL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100861#endif
Bram Moolenaar4d8bac82018-03-09 21:33:34 +0100862 if (term->tl_command != NULL && fputs((char *)term->tl_command, fd) < 0)
863 return FAIL;
864
865 return put_eol(fd);
866}
867
868/*
869 * Return TRUE if "buf" has a terminal that should be restored.
870 */
871 int
872term_should_restore(buf_T *buf)
873{
874 term_T *term = buf->b_term;
875
876 return term != NULL && (term->tl_command == NULL
877 || STRCMP(term->tl_command, "NONE") != 0);
878}
879#endif
880
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200881/*
882 * Free the scrollback buffer for "term".
883 */
884 static void
885free_scrollback(term_T *term)
886{
887 int i;
888
889 for (i = 0; i < term->tl_scrollback.ga_len; ++i)
890 vim_free(((sb_line_T *)term->tl_scrollback.ga_data + i)->sb_cells);
891 ga_clear(&term->tl_scrollback);
Bram Moolenaar29ae2232019-02-14 21:22:01 +0100892 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
893 vim_free(((sb_line_T *)term->tl_scrollback_postponed.ga_data + i)->sb_cells);
894 ga_clear(&term->tl_scrollback_postponed);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200895}
896
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100897
898// Terminals that need to be freed soon.
Bram Moolenaar840d16f2019-09-10 21:27:18 +0200899static term_T *terminals_to_free = NULL;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100900
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200901/*
902 * Free a terminal and everything it refers to.
903 * Kills the job if there is one.
904 * Called when wiping out a buffer.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100905 * The actual terminal structure is freed later in free_unused_terminals(),
906 * because callbacks may wipe out a buffer while the terminal is still
907 * referenced.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200908 */
909 void
910free_terminal(buf_T *buf)
911{
912 term_T *term = buf->b_term;
913 term_T *tp;
914
915 if (term == NULL)
916 return;
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100917
918 // Unlink the terminal form the list of terminals.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200919 if (first_term == term)
920 first_term = term->tl_next;
921 else
922 for (tp = first_term; tp->tl_next != NULL; tp = tp->tl_next)
923 if (tp->tl_next == term)
924 {
925 tp->tl_next = term->tl_next;
926 break;
927 }
928
929 if (term->tl_job != NULL)
930 {
931 if (term->tl_job->jv_status != JOB_ENDED
932 && term->tl_job->jv_status != JOB_FINISHED
Bram Moolenaard317b382018-02-08 22:33:31 +0100933 && term->tl_job->jv_status != JOB_FAILED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200934 job_stop(term->tl_job, NULL, "kill");
935 job_unref(term->tl_job);
936 }
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100937 term->tl_next = terminals_to_free;
938 terminals_to_free = term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200939
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200940 buf->b_term = NULL;
941 if (in_terminal_loop == term)
942 in_terminal_loop = NULL;
943}
944
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100945 void
946free_unused_terminals()
947{
948 while (terminals_to_free != NULL)
949 {
950 term_T *term = terminals_to_free;
951
952 terminals_to_free = term->tl_next;
953
954 free_scrollback(term);
955
956 term_free_vterm(term);
Bram Moolenaard2842ea2019-09-26 23:08:54 +0200957 vim_free(term->tl_api);
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100958 vim_free(term->tl_title);
959#ifdef FEAT_SESSION
960 vim_free(term->tl_command);
961#endif
962 vim_free(term->tl_kill);
963 vim_free(term->tl_status_text);
964 vim_free(term->tl_opencmd);
965 vim_free(term->tl_eof_chars);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +0100966 vim_free(term->tl_arg0_cmd);
Bram Moolenaar4f974752019-02-17 17:44:42 +0100967#ifdef MSWIN
Bram Moolenaar2a4857a2019-01-29 22:29:07 +0100968 if (term->tl_out_fd != NULL)
969 fclose(term->tl_out_fd);
970#endif
971 vim_free(term->tl_cursor_color);
972 vim_free(term);
973 }
974}
975
Bram Moolenaar2e6ab182017-09-20 10:03:07 +0200976/*
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100977 * Get the part that is connected to the tty. Normally this is PART_IN, but
978 * when writing buffer lines to the job it can be another. This makes it
979 * possible to do "1,5term vim -".
980 */
981 static ch_part_T
Bram Moolenaarbd67aac2019-09-21 23:09:04 +0200982get_tty_part(term_T *term UNUSED)
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100983{
984#ifdef UNIX
985 ch_part_T parts[3] = {PART_IN, PART_OUT, PART_ERR};
986 int i;
987
988 for (i = 0; i < 3; ++i)
989 {
990 int fd = term->tl_job->jv_channel->ch_part[parts[i]].ch_fd;
991
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +0100992 if (mch_isatty(fd))
Bram Moolenaarb50773c2018-01-30 22:31:19 +0100993 return parts[i];
994 }
995#endif
996 return PART_IN;
997}
998
999/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001000 * Write job output "msg[len]" to the vterm.
1001 */
1002 static void
1003term_write_job_output(term_T *term, char_u *msg, size_t len)
1004{
1005 VTerm *vterm = term->tl_vterm;
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001006 size_t prevlen = vterm_output_get_buffer_current(vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001007
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001008 vterm_input_write(vterm, (char *)msg, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001009
Bram Moolenaarb50773c2018-01-30 22:31:19 +01001010 /* flush vterm buffer when vterm responded to control sequence */
1011 if (prevlen != vterm_output_get_buffer_current(vterm))
1012 {
1013 char buf[KEY_BUF_LEN];
1014 size_t curlen = vterm_output_read(vterm, buf, KEY_BUF_LEN);
1015
1016 if (curlen > 0)
1017 channel_send(term->tl_job->jv_channel, get_tty_part(term),
1018 (char_u *)buf, (int)curlen, NULL);
1019 }
1020
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001021 /* this invokes the damage callbacks */
1022 vterm_screen_flush_damage(vterm_obtain_screen(vterm));
1023}
1024
1025 static void
1026update_cursor(term_T *term, int redraw)
1027{
1028 if (term->tl_normal_mode)
1029 return;
Bram Moolenaar13568252018-03-16 20:46:58 +01001030#ifdef FEAT_GUI
1031 if (term->tl_system)
1032 windgoto(term->tl_cursor_pos.row + term->tl_toprow,
1033 term->tl_cursor_pos.col);
1034 else
1035#endif
1036 setcursor();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001037 if (redraw)
1038 {
1039 if (term->tl_buffer == curbuf && term->tl_cursor_visible)
1040 cursor_on();
1041 out_flush();
1042#ifdef FEAT_GUI
1043 if (gui.in_use)
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001044 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001045 gui_update_cursor(FALSE, FALSE);
Bram Moolenaar23c1b2b2017-12-05 21:32:33 +01001046 gui_mch_flush();
1047 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001048#endif
1049 }
1050}
1051
1052/*
1053 * Invoked when "msg" output from a job was received. Write it to the terminal
1054 * of "buffer".
1055 */
1056 void
1057write_to_term(buf_T *buffer, char_u *msg, channel_T *channel)
1058{
1059 size_t len = STRLEN(msg);
1060 term_T *term = buffer->b_term;
1061
Bram Moolenaar4f974752019-02-17 17:44:42 +01001062#ifdef MSWIN
Bram Moolenaarf25329c2018-05-06 21:49:32 +02001063 /* Win32: Cannot redirect output of the job, intercept it here and write to
1064 * the file. */
1065 if (term->tl_out_fd != NULL)
1066 {
1067 ch_log(channel, "Writing %d bytes to output file", (int)len);
1068 fwrite(msg, len, 1, term->tl_out_fd);
1069 return;
1070 }
1071#endif
1072
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001073 if (term->tl_vterm == NULL)
1074 {
1075 ch_log(channel, "NOT writing %d bytes to terminal", (int)len);
1076 return;
1077 }
1078 ch_log(channel, "writing %d bytes to terminal", (int)len);
1079 term_write_job_output(term, msg, len);
1080
Bram Moolenaar13568252018-03-16 20:46:58 +01001081#ifdef FEAT_GUI
1082 if (term->tl_system)
1083 {
1084 /* show system output, scrolling up the screen as needed */
1085 update_system_term(term);
1086 update_cursor(term, TRUE);
1087 }
1088 else
1089#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001090 /* In Terminal-Normal mode we are displaying the buffer, not the terminal
1091 * contents, thus no screen update is needed. */
1092 if (!term->tl_normal_mode)
1093 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001094 // Don't use update_screen() when editing the command line, it gets
1095 // cleared.
1096 // TODO: only update once in a while.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001097 ch_log(term->tl_job->jv_channel, "updating screen");
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001098 if (buffer == curbuf && (State & CMDLINE) == 0)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001099 {
Bram Moolenaar0ce74132018-06-18 22:15:50 +02001100 update_screen(VALID_NO_UPDATE);
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02001101 /* update_screen() can be slow, check the terminal wasn't closed
1102 * already */
1103 if (buffer == curbuf && curbuf->b_term != NULL)
1104 update_cursor(curbuf->b_term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001105 }
1106 else
1107 redraw_after_callback(TRUE);
1108 }
1109}
1110
1111/*
1112 * Send a mouse position and click to the vterm
1113 */
1114 static int
1115term_send_mouse(VTerm *vterm, int button, int pressed)
1116{
1117 VTermModifier mod = VTERM_MOD_NONE;
1118
1119 vterm_mouse_move(vterm, mouse_row - W_WINROW(curwin),
Bram Moolenaar53f81742017-09-22 14:35:51 +02001120 mouse_col - curwin->w_wincol, mod);
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001121 if (button != 0)
1122 vterm_mouse_button(vterm, button, pressed, mod);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001123 return TRUE;
1124}
1125
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001126static int enter_mouse_col = -1;
1127static int enter_mouse_row = -1;
1128
1129/*
1130 * Handle a mouse click, drag or release.
1131 * Return TRUE when a mouse event is sent to the terminal.
1132 */
1133 static int
1134term_mouse_click(VTerm *vterm, int key)
1135{
1136#if defined(FEAT_CLIPBOARD)
1137 /* For modeless selection mouse drag and release events are ignored, unless
1138 * they are preceded with a mouse down event */
1139 static int ignore_drag_release = TRUE;
1140 VTermMouseState mouse_state;
1141
1142 vterm_state_get_mousestate(vterm_obtain_state(vterm), &mouse_state);
1143 if (mouse_state.flags == 0)
1144 {
1145 /* Terminal is not using the mouse, use modeless selection. */
1146 switch (key)
1147 {
1148 case K_LEFTDRAG:
1149 case K_LEFTRELEASE:
1150 case K_RIGHTDRAG:
1151 case K_RIGHTRELEASE:
1152 /* Ignore drag and release events when the button-down wasn't
1153 * seen before. */
1154 if (ignore_drag_release)
1155 {
1156 int save_mouse_col, save_mouse_row;
1157
1158 if (enter_mouse_col < 0)
1159 break;
1160
1161 /* mouse click in the window gave us focus, handle that
1162 * click now */
1163 save_mouse_col = mouse_col;
1164 save_mouse_row = mouse_row;
1165 mouse_col = enter_mouse_col;
1166 mouse_row = enter_mouse_row;
1167 clip_modeless(MOUSE_LEFT, TRUE, FALSE);
1168 mouse_col = save_mouse_col;
1169 mouse_row = save_mouse_row;
1170 }
1171 /* FALLTHROUGH */
1172 case K_LEFTMOUSE:
1173 case K_RIGHTMOUSE:
1174 if (key == K_LEFTRELEASE || key == K_RIGHTRELEASE)
1175 ignore_drag_release = TRUE;
1176 else
1177 ignore_drag_release = FALSE;
1178 /* Should we call mouse_has() here? */
1179 if (clip_star.available)
1180 {
1181 int button, is_click, is_drag;
1182
1183 button = get_mouse_button(KEY2TERMCAP1(key),
1184 &is_click, &is_drag);
1185 if (mouse_model_popup() && button == MOUSE_LEFT
1186 && (mod_mask & MOD_MASK_SHIFT))
1187 {
1188 /* Translate shift-left to right button. */
1189 button = MOUSE_RIGHT;
1190 mod_mask &= ~MOD_MASK_SHIFT;
1191 }
1192 clip_modeless(button, is_click, is_drag);
1193 }
1194 break;
1195
1196 case K_MIDDLEMOUSE:
1197 if (clip_star.available)
1198 insert_reg('*', TRUE);
1199 break;
1200 }
1201 enter_mouse_col = -1;
1202 return FALSE;
1203 }
1204#endif
1205 enter_mouse_col = -1;
1206
1207 switch (key)
1208 {
1209 case K_LEFTMOUSE:
1210 case K_LEFTMOUSE_NM: term_send_mouse(vterm, 1, 1); break;
1211 case K_LEFTDRAG: term_send_mouse(vterm, 1, 1); break;
1212 case K_LEFTRELEASE:
1213 case K_LEFTRELEASE_NM: term_send_mouse(vterm, 1, 0); break;
1214 case K_MOUSEMOVE: term_send_mouse(vterm, 0, 0); break;
1215 case K_MIDDLEMOUSE: term_send_mouse(vterm, 2, 1); break;
1216 case K_MIDDLEDRAG: term_send_mouse(vterm, 2, 1); break;
1217 case K_MIDDLERELEASE: term_send_mouse(vterm, 2, 0); break;
1218 case K_RIGHTMOUSE: term_send_mouse(vterm, 3, 1); break;
1219 case K_RIGHTDRAG: term_send_mouse(vterm, 3, 1); break;
1220 case K_RIGHTRELEASE: term_send_mouse(vterm, 3, 0); break;
1221 }
1222 return TRUE;
1223}
1224
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001225/*
1226 * Convert typed key "c" into bytes to send to the job.
1227 * Return the number of bytes in "buf".
1228 */
1229 static int
1230term_convert_key(term_T *term, int c, char *buf)
1231{
1232 VTerm *vterm = term->tl_vterm;
1233 VTermKey key = VTERM_KEY_NONE;
1234 VTermModifier mod = VTERM_MOD_NONE;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001235 int other = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001236
1237 switch (c)
1238 {
Bram Moolenaar26d205d2017-11-09 17:33:11 +01001239 /* don't use VTERM_KEY_ENTER, it may do an unwanted conversion */
1240
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001241 /* don't use VTERM_KEY_BACKSPACE, it always
1242 * becomes 0x7f DEL */
1243 case K_BS: c = term_backspace_char; break;
1244
1245 case ESC: key = VTERM_KEY_ESCAPE; break;
1246 case K_DEL: key = VTERM_KEY_DEL; break;
1247 case K_DOWN: key = VTERM_KEY_DOWN; break;
1248 case K_S_DOWN: mod = VTERM_MOD_SHIFT;
1249 key = VTERM_KEY_DOWN; break;
1250 case K_END: key = VTERM_KEY_END; break;
1251 case K_S_END: mod = VTERM_MOD_SHIFT;
1252 key = VTERM_KEY_END; break;
1253 case K_C_END: mod = VTERM_MOD_CTRL;
1254 key = VTERM_KEY_END; break;
1255 case K_F10: key = VTERM_KEY_FUNCTION(10); break;
1256 case K_F11: key = VTERM_KEY_FUNCTION(11); break;
1257 case K_F12: key = VTERM_KEY_FUNCTION(12); break;
1258 case K_F1: key = VTERM_KEY_FUNCTION(1); break;
1259 case K_F2: key = VTERM_KEY_FUNCTION(2); break;
1260 case K_F3: key = VTERM_KEY_FUNCTION(3); break;
1261 case K_F4: key = VTERM_KEY_FUNCTION(4); break;
1262 case K_F5: key = VTERM_KEY_FUNCTION(5); break;
1263 case K_F6: key = VTERM_KEY_FUNCTION(6); break;
1264 case K_F7: key = VTERM_KEY_FUNCTION(7); break;
1265 case K_F8: key = VTERM_KEY_FUNCTION(8); break;
1266 case K_F9: key = VTERM_KEY_FUNCTION(9); break;
1267 case K_HOME: key = VTERM_KEY_HOME; break;
1268 case K_S_HOME: mod = VTERM_MOD_SHIFT;
1269 key = VTERM_KEY_HOME; break;
1270 case K_C_HOME: mod = VTERM_MOD_CTRL;
1271 key = VTERM_KEY_HOME; break;
1272 case K_INS: key = VTERM_KEY_INS; break;
1273 case K_K0: key = VTERM_KEY_KP_0; break;
1274 case K_K1: key = VTERM_KEY_KP_1; break;
1275 case K_K2: key = VTERM_KEY_KP_2; break;
1276 case K_K3: key = VTERM_KEY_KP_3; break;
1277 case K_K4: key = VTERM_KEY_KP_4; break;
1278 case K_K5: key = VTERM_KEY_KP_5; break;
1279 case K_K6: key = VTERM_KEY_KP_6; break;
1280 case K_K7: key = VTERM_KEY_KP_7; break;
1281 case K_K8: key = VTERM_KEY_KP_8; break;
1282 case K_K9: key = VTERM_KEY_KP_9; break;
1283 case K_KDEL: key = VTERM_KEY_DEL; break; /* TODO */
1284 case K_KDIVIDE: key = VTERM_KEY_KP_DIVIDE; break;
1285 case K_KEND: key = VTERM_KEY_KP_1; break; /* TODO */
1286 case K_KENTER: key = VTERM_KEY_KP_ENTER; break;
1287 case K_KHOME: key = VTERM_KEY_KP_7; break; /* TODO */
1288 case K_KINS: key = VTERM_KEY_KP_0; break; /* TODO */
1289 case K_KMINUS: key = VTERM_KEY_KP_MINUS; break;
1290 case K_KMULTIPLY: key = VTERM_KEY_KP_MULT; break;
1291 case K_KPAGEDOWN: key = VTERM_KEY_KP_3; break; /* TODO */
1292 case K_KPAGEUP: key = VTERM_KEY_KP_9; break; /* TODO */
1293 case K_KPLUS: key = VTERM_KEY_KP_PLUS; break;
1294 case K_KPOINT: key = VTERM_KEY_KP_PERIOD; break;
1295 case K_LEFT: key = VTERM_KEY_LEFT; break;
1296 case K_S_LEFT: mod = VTERM_MOD_SHIFT;
1297 key = VTERM_KEY_LEFT; break;
1298 case K_C_LEFT: mod = VTERM_MOD_CTRL;
1299 key = VTERM_KEY_LEFT; break;
1300 case K_PAGEDOWN: key = VTERM_KEY_PAGEDOWN; break;
1301 case K_PAGEUP: key = VTERM_KEY_PAGEUP; break;
1302 case K_RIGHT: key = VTERM_KEY_RIGHT; break;
1303 case K_S_RIGHT: mod = VTERM_MOD_SHIFT;
1304 key = VTERM_KEY_RIGHT; break;
1305 case K_C_RIGHT: mod = VTERM_MOD_CTRL;
1306 key = VTERM_KEY_RIGHT; break;
1307 case K_UP: key = VTERM_KEY_UP; break;
1308 case K_S_UP: mod = VTERM_MOD_SHIFT;
1309 key = VTERM_KEY_UP; break;
1310 case TAB: key = VTERM_KEY_TAB; break;
Bram Moolenaar73cddfd2018-02-16 20:01:04 +01001311 case K_S_TAB: mod = VTERM_MOD_SHIFT;
1312 key = VTERM_KEY_TAB; break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001313
Bram Moolenaara42ad572017-11-16 13:08:04 +01001314 case K_MOUSEUP: other = term_send_mouse(vterm, 5, 1); break;
1315 case K_MOUSEDOWN: other = term_send_mouse(vterm, 4, 1); break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001316 case K_MOUSELEFT: /* TODO */ return 0;
1317 case K_MOUSERIGHT: /* TODO */ return 0;
1318
1319 case K_LEFTMOUSE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001320 case K_LEFTMOUSE_NM:
1321 case K_LEFTDRAG:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001322 case K_LEFTRELEASE:
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001323 case K_LEFTRELEASE_NM:
1324 case K_MOUSEMOVE:
1325 case K_MIDDLEMOUSE:
1326 case K_MIDDLEDRAG:
1327 case K_MIDDLERELEASE:
1328 case K_RIGHTMOUSE:
1329 case K_RIGHTDRAG:
1330 case K_RIGHTRELEASE: if (!term_mouse_click(vterm, c))
1331 return 0;
1332 other = TRUE;
1333 break;
1334
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001335 case K_X1MOUSE: /* TODO */ return 0;
1336 case K_X1DRAG: /* TODO */ return 0;
1337 case K_X1RELEASE: /* TODO */ return 0;
1338 case K_X2MOUSE: /* TODO */ return 0;
1339 case K_X2DRAG: /* TODO */ return 0;
1340 case K_X2RELEASE: /* TODO */ return 0;
1341
1342 case K_IGNORE: return 0;
1343 case K_NOP: return 0;
1344 case K_UNDO: return 0;
1345 case K_HELP: return 0;
1346 case K_XF1: key = VTERM_KEY_FUNCTION(1); break;
1347 case K_XF2: key = VTERM_KEY_FUNCTION(2); break;
1348 case K_XF3: key = VTERM_KEY_FUNCTION(3); break;
1349 case K_XF4: key = VTERM_KEY_FUNCTION(4); break;
1350 case K_SELECT: return 0;
1351#ifdef FEAT_GUI
1352 case K_VER_SCROLLBAR: return 0;
1353 case K_HOR_SCROLLBAR: return 0;
1354#endif
1355#ifdef FEAT_GUI_TABLINE
1356 case K_TABLINE: return 0;
1357 case K_TABMENU: return 0;
1358#endif
1359#ifdef FEAT_NETBEANS_INTG
1360 case K_F21: key = VTERM_KEY_FUNCTION(21); break;
1361#endif
1362#ifdef FEAT_DND
1363 case K_DROP: return 0;
1364#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001365 case K_CURSORHOLD: return 0;
Bram Moolenaara42ad572017-11-16 13:08:04 +01001366 case K_PS: vterm_keyboard_start_paste(vterm);
1367 other = TRUE;
1368 break;
1369 case K_PE: vterm_keyboard_end_paste(vterm);
1370 other = TRUE;
1371 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001372 }
1373
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001374 // add modifiers for the typed key
1375 mod |= mod_mask;
1376
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001377 /*
1378 * Convert special keys to vterm keys:
1379 * - Write keys to vterm: vterm_keyboard_key()
1380 * - Write output to channel.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001381 */
1382 if (key != VTERM_KEY_NONE)
1383 /* Special key, let vterm convert it. */
1384 vterm_keyboard_key(vterm, key, mod);
Bram Moolenaara42ad572017-11-16 13:08:04 +01001385 else if (!other)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001386 /* Normal character, let vterm convert it. */
1387 vterm_keyboard_unichar(vterm, c, mod);
1388
1389 /* Read back the converted escape sequence. */
1390 return (int)vterm_output_read(vterm, buf, KEY_BUF_LEN);
1391}
1392
1393/*
1394 * Return TRUE if the job for "term" is still running.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001395 * If "check_job_status" is TRUE update the job status.
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001396 * NOTE: "term" may be freed by callbacks.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001397 */
1398 static int
1399term_job_running_check(term_T *term, int check_job_status)
1400{
1401 /* Also consider the job finished when the channel is closed, to avoid a
1402 * race condition when updating the title. */
1403 if (term != NULL
1404 && term->tl_job != NULL
1405 && channel_is_open(term->tl_job->jv_channel))
1406 {
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001407 job_T *job = term->tl_job;
1408
1409 // Careful: Checking the job status may invoked callbacks, which close
1410 // the buffer and terminate "term". However, "job" will not be freed
1411 // yet.
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001412 if (check_job_status)
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01001413 job_status(job);
1414 return (job->jv_status == JOB_STARTED
1415 || (job->jv_channel != NULL && job->jv_channel->ch_keep_open));
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001416 }
1417 return FALSE;
1418}
1419
1420/*
1421 * Return TRUE if the job for "term" is still running.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001422 */
1423 int
1424term_job_running(term_T *term)
1425{
Bram Moolenaar802bfb12018-04-15 17:28:13 +02001426 return term_job_running_check(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001427}
1428
1429/*
1430 * Return TRUE if "term" has an active channel and used ":term NONE".
1431 */
1432 int
1433term_none_open(term_T *term)
1434{
1435 /* Also consider the job finished when the channel is closed, to avoid a
1436 * race condition when updating the title. */
1437 return term != NULL
1438 && term->tl_job != NULL
1439 && channel_is_open(term->tl_job->jv_channel)
1440 && term->tl_job->jv_channel->ch_keep_open;
1441}
1442
1443/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001444 * Used when exiting: kill the job in "buf" if so desired.
1445 * Return OK when the job finished.
1446 * Return FAIL when the job is still running.
1447 */
1448 int
1449term_try_stop_job(buf_T *buf)
1450{
1451 int count;
1452 char *how = (char *)buf->b_term->tl_kill;
1453
1454#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
1455 if ((how == NULL || *how == NUL) && (p_confirm || cmdmod.confirm))
1456 {
1457 char_u buff[DIALOG_MSG_SIZE];
1458 int ret;
1459
1460 dialog_msg(buff, _("Kill job in \"%s\"?"), buf->b_fname);
1461 ret = vim_dialog_yesnocancel(VIM_QUESTION, NULL, buff, 1);
1462 if (ret == VIM_YES)
1463 how = "kill";
1464 else if (ret == VIM_CANCEL)
1465 return FAIL;
1466 }
1467#endif
1468 if (how == NULL || *how == NUL)
1469 return FAIL;
1470
1471 job_stop(buf->b_term->tl_job, NULL, how);
1472
Bram Moolenaar9172d232019-01-29 23:06:54 +01001473 // wait for up to a second for the job to die
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001474 for (count = 0; count < 100; ++count)
1475 {
Bram Moolenaar9172d232019-01-29 23:06:54 +01001476 job_T *job;
1477
1478 // buffer, terminal and job may be cleaned up while waiting
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001479 if (!buf_valid(buf)
1480 || buf->b_term == NULL
1481 || buf->b_term->tl_job == NULL)
1482 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001483 job = buf->b_term->tl_job;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001484
Bram Moolenaar9172d232019-01-29 23:06:54 +01001485 // Call job_status() to update jv_status. It may cause the job to be
1486 // cleaned up but it won't be freed.
1487 job_status(job);
1488 if (job->jv_status >= JOB_ENDED)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001489 return OK;
Bram Moolenaar9172d232019-01-29 23:06:54 +01001490
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001491 ui_delay(10L, FALSE);
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02001492 term_flush_messages();
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01001493 }
1494 return FAIL;
1495}
1496
1497/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001498 * Add the last line of the scrollback buffer to the buffer in the window.
1499 */
1500 static void
1501add_scrollback_line_to_buffer(term_T *term, char_u *text, int len)
1502{
1503 buf_T *buf = term->tl_buffer;
1504 int empty = (buf->b_ml.ml_flags & ML_EMPTY);
1505 linenr_T lnum = buf->b_ml.ml_line_count;
1506
Bram Moolenaar4f974752019-02-17 17:44:42 +01001507#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001508 if (!enc_utf8 && enc_codepage > 0)
1509 {
1510 WCHAR *ret = NULL;
1511 int length = 0;
1512
1513 MultiByteToWideChar_alloc(CP_UTF8, 0, (char*)text, len + 1,
1514 &ret, &length);
1515 if (ret != NULL)
1516 {
1517 WideCharToMultiByte_alloc(enc_codepage, 0,
1518 ret, length, (char **)&text, &len, 0, 0);
1519 vim_free(ret);
1520 ml_append_buf(term->tl_buffer, lnum, text, len, FALSE);
1521 vim_free(text);
1522 }
1523 }
1524 else
1525#endif
1526 ml_append_buf(term->tl_buffer, lnum, text, len + 1, FALSE);
1527 if (empty)
1528 {
1529 /* Delete the empty line that was in the empty buffer. */
1530 curbuf = buf;
1531 ml_delete(1, FALSE);
1532 curbuf = curwin->w_buffer;
1533 }
1534}
1535
1536 static void
1537cell2cellattr(const VTermScreenCell *cell, cellattr_T *attr)
1538{
1539 attr->width = cell->width;
1540 attr->attrs = cell->attrs;
1541 attr->fg = cell->fg;
1542 attr->bg = cell->bg;
1543}
1544
1545 static int
1546equal_celattr(cellattr_T *a, cellattr_T *b)
1547{
1548 /* Comparing the colors should be sufficient. */
1549 return a->fg.red == b->fg.red
1550 && a->fg.green == b->fg.green
1551 && a->fg.blue == b->fg.blue
1552 && a->bg.red == b->bg.red
1553 && a->bg.green == b->bg.green
1554 && a->bg.blue == b->bg.blue;
1555}
1556
Bram Moolenaard96ff162018-02-18 22:13:29 +01001557/*
1558 * Add an empty scrollback line to "term". When "lnum" is not zero, add the
1559 * line at this position. Otherwise at the end.
1560 */
1561 static int
1562add_empty_scrollback(term_T *term, cellattr_T *fill_attr, int lnum)
1563{
1564 if (ga_grow(&term->tl_scrollback, 1) == OK)
1565 {
1566 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1567 + term->tl_scrollback.ga_len;
1568
1569 if (lnum > 0)
1570 {
1571 int i;
1572
1573 for (i = 0; i < term->tl_scrollback.ga_len - lnum; ++i)
1574 {
1575 *line = *(line - 1);
1576 --line;
1577 }
1578 }
1579 line->sb_cols = 0;
1580 line->sb_cells = NULL;
1581 line->sb_fill_attr = *fill_attr;
1582 ++term->tl_scrollback.ga_len;
1583 return OK;
1584 }
1585 return FALSE;
1586}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001587
1588/*
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001589 * Remove the terminal contents from the scrollback and the buffer.
1590 * Used before adding a new scrollback line or updating the buffer for lines
1591 * displayed in the terminal.
1592 */
1593 static void
1594cleanup_scrollback(term_T *term)
1595{
1596 sb_line_T *line;
1597 garray_T *gap;
1598
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001599 curbuf = term->tl_buffer;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001600 gap = &term->tl_scrollback;
1601 while (curbuf->b_ml.ml_line_count > term->tl_scrollback_scrolled
1602 && gap->ga_len > 0)
1603 {
1604 ml_delete(curbuf->b_ml.ml_line_count, FALSE);
1605 line = (sb_line_T *)gap->ga_data + gap->ga_len - 1;
1606 vim_free(line->sb_cells);
1607 --gap->ga_len;
1608 }
Bram Moolenaar3f1a53c2018-05-12 16:55:14 +02001609 curbuf = curwin->w_buffer;
1610 if (curbuf == term->tl_buffer)
1611 check_cursor();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001612}
1613
1614/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001615 * Add the current lines of the terminal to scrollback and to the buffer.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001616 */
1617 static void
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001618update_snapshot(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001619{
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001620 VTermScreen *screen;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001621 int len;
1622 int lines_skipped = 0;
1623 VTermPos pos;
1624 VTermScreenCell cell;
1625 cellattr_T fill_attr, new_fill_attr;
1626 cellattr_T *p;
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001627
1628 ch_log(term->tl_job == NULL ? NULL : term->tl_job->jv_channel,
1629 "Adding terminal window snapshot to buffer");
1630
1631 /* First remove the lines that were appended before, they might be
1632 * outdated. */
1633 cleanup_scrollback(term);
1634
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001635 screen = vterm_obtain_screen(term->tl_vterm);
1636 fill_attr = new_fill_attr = term->tl_default_color;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001637 for (pos.row = 0; pos.row < term->tl_rows; ++pos.row)
1638 {
1639 len = 0;
1640 for (pos.col = 0; pos.col < term->tl_cols; ++pos.col)
1641 if (vterm_screen_get_cell(screen, pos, &cell) != 0
1642 && cell.chars[0] != NUL)
1643 {
1644 len = pos.col + 1;
1645 new_fill_attr = term->tl_default_color;
1646 }
1647 else
1648 /* Assume the last attr is the filler attr. */
1649 cell2cellattr(&cell, &new_fill_attr);
1650
1651 if (len == 0 && equal_celattr(&new_fill_attr, &fill_attr))
1652 ++lines_skipped;
1653 else
1654 {
1655 while (lines_skipped > 0)
1656 {
1657 /* Line was skipped, add an empty line. */
1658 --lines_skipped;
Bram Moolenaard96ff162018-02-18 22:13:29 +01001659 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001660 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001661 }
1662
1663 if (len == 0)
1664 p = NULL;
1665 else
Bram Moolenaarc799fe22019-05-28 23:08:19 +02001666 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001667 if ((p != NULL || len == 0)
1668 && ga_grow(&term->tl_scrollback, 1) == OK)
1669 {
1670 garray_T ga;
1671 int width;
1672 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
1673 + term->tl_scrollback.ga_len;
1674
1675 ga_init2(&ga, 1, 100);
1676 for (pos.col = 0; pos.col < len; pos.col += width)
1677 {
1678 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
1679 {
1680 width = 1;
1681 vim_memset(p + pos.col, 0, sizeof(cellattr_T));
1682 if (ga_grow(&ga, 1) == OK)
1683 ga.ga_len += utf_char2bytes(' ',
1684 (char_u *)ga.ga_data + ga.ga_len);
1685 }
1686 else
1687 {
1688 width = cell.width;
1689
1690 cell2cellattr(&cell, &p[pos.col]);
1691
Bram Moolenaara79fd562018-12-20 20:47:32 +01001692 // Each character can be up to 6 bytes.
1693 if (ga_grow(&ga, VTERM_MAX_CHARS_PER_CELL * 6) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001694 {
1695 int i;
1696 int c;
1697
1698 for (i = 0; (c = cell.chars[i]) > 0 || i == 0; ++i)
1699 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
1700 (char_u *)ga.ga_data + ga.ga_len);
1701 }
1702 }
1703 }
1704 line->sb_cols = len;
1705 line->sb_cells = p;
1706 line->sb_fill_attr = new_fill_attr;
1707 fill_attr = new_fill_attr;
1708 ++term->tl_scrollback.ga_len;
1709
1710 if (ga_grow(&ga, 1) == FAIL)
1711 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1712 else
1713 {
1714 *((char_u *)ga.ga_data + ga.ga_len) = NUL;
1715 add_scrollback_line_to_buffer(term, ga.ga_data, ga.ga_len);
1716 }
1717 ga_clear(&ga);
1718 }
1719 else
1720 vim_free(p);
1721 }
1722 }
1723
Bram Moolenaarf3aea592018-11-11 22:18:21 +01001724 // Add trailing empty lines.
1725 for (pos.row = term->tl_scrollback.ga_len;
1726 pos.row < term->tl_scrollback_scrolled + term->tl_cursor_pos.row;
1727 ++pos.row)
1728 {
1729 if (add_empty_scrollback(term, &fill_attr, 0) == OK)
1730 add_scrollback_line_to_buffer(term, (char_u *)"", 0);
1731 }
1732
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001733 term->tl_dirty_snapshot = FALSE;
1734#ifdef FEAT_TIMERS
1735 term->tl_timer_set = FALSE;
1736#endif
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001737}
1738
1739/*
1740 * If needed, add the current lines of the terminal to scrollback and to the
1741 * buffer. Called after the job has ended and when switching to
1742 * Terminal-Normal mode.
1743 * When "redraw" is TRUE redraw the windows that show the terminal.
1744 */
1745 static void
1746may_move_terminal_to_buffer(term_T *term, int redraw)
1747{
1748 win_T *wp;
1749
1750 if (term->tl_vterm == NULL)
1751 return;
1752
1753 /* Update the snapshot only if something changes or the buffer does not
1754 * have all the lines. */
1755 if (term->tl_dirty_snapshot || term->tl_buffer->b_ml.ml_line_count
1756 <= term->tl_scrollback_scrolled)
1757 update_snapshot(term);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001758
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001759 /* Obtain the current background color. */
1760 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
1761 &term->tl_default_color.fg, &term->tl_default_color.bg);
1762
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001763 if (redraw)
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001764 FOR_ALL_WINDOWS(wp)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001765 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001766 if (wp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001767 {
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001768 wp->w_cursor.lnum = term->tl_buffer->b_ml.ml_line_count;
1769 wp->w_cursor.col = 0;
1770 wp->w_valid = 0;
1771 if (wp->w_cursor.lnum >= wp->w_height)
1772 {
1773 linenr_T min_topline = wp->w_cursor.lnum - wp->w_height + 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001774
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001775 if (wp->w_topline < min_topline)
1776 wp->w_topline = min_topline;
1777 }
1778 redraw_win_later(wp, NOT_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001779 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001780 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001781}
1782
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001783#if defined(FEAT_TIMERS) || defined(PROTO)
1784/*
1785 * Check if any terminal timer expired. If so, copy text from the terminal to
1786 * the buffer.
1787 * Return the time until the next timer will expire.
1788 */
1789 int
1790term_check_timers(int next_due_arg, proftime_T *now)
1791{
1792 term_T *term;
1793 int next_due = next_due_arg;
1794
1795 for (term = first_term; term != NULL; term = term->tl_next)
1796 {
1797 if (term->tl_timer_set && !term->tl_normal_mode)
1798 {
1799 long this_due = proftime_time_left(&term->tl_timer_due, now);
1800
1801 if (this_due <= 1)
1802 {
1803 term->tl_timer_set = FALSE;
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001804 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02001805 }
1806 else if (next_due == -1 || next_due > this_due)
1807 next_due = this_due;
1808 }
1809 }
1810
1811 return next_due;
1812}
1813#endif
1814
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001815/*
1816 * When "normal_mode" is TRUE set the terminal to Terminal-Normal mode,
1817 * otherwise end it.
1818 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001819 static void
1820set_terminal_mode(term_T *term, int normal_mode)
1821{
1822 term->tl_normal_mode = normal_mode;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001823 if (!normal_mode)
1824 handle_postponed_scrollback(term);
Bram Moolenaard23a8232018-02-10 18:45:26 +01001825 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001826 if (term->tl_buffer == curbuf)
1827 maketitle();
1828}
1829
1830/*
1831 * Called after the job if finished and Terminal mode is not active:
1832 * Move the vterm contents into the scrollback buffer and free the vterm.
1833 */
1834 static void
1835cleanup_vterm(term_T *term)
1836{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01001837 set_terminal_mode(term, FALSE);
Bram Moolenaar1dd98332018-03-16 22:54:53 +01001838 if (term->tl_finish != TL_FINISH_CLOSE)
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001839 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001840 term_free_vterm(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001841}
1842
1843/*
1844 * Switch from Terminal-Job mode to Terminal-Normal mode.
1845 * Suspends updating the terminal window.
1846 */
1847 static void
1848term_enter_normal_mode(void)
1849{
1850 term_T *term = curbuf->b_term;
1851
Bram Moolenaar2bc79952018-05-12 20:36:24 +02001852 set_terminal_mode(term, TRUE);
1853
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001854 /* Append the current terminal contents to the buffer. */
Bram Moolenaar05c4a472018-05-13 15:15:43 +02001855 may_move_terminal_to_buffer(term, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001856
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001857 /* Move the window cursor to the position of the cursor in the
1858 * terminal. */
1859 curwin->w_cursor.lnum = term->tl_scrollback_scrolled
1860 + term->tl_cursor_pos.row + 1;
1861 check_cursor();
Bram Moolenaar620020e2018-05-13 19:06:12 +02001862 if (coladvance(term->tl_cursor_pos.col) == FAIL)
1863 coladvance(MAXCOL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001864
1865 /* Display the same lines as in the terminal. */
1866 curwin->w_topline = term->tl_scrollback_scrolled + 1;
1867}
1868
1869/*
1870 * Returns TRUE if the current window contains a terminal and we are in
1871 * Terminal-Normal mode.
1872 */
1873 int
1874term_in_normal_mode(void)
1875{
1876 term_T *term = curbuf->b_term;
1877
1878 return term != NULL && term->tl_normal_mode;
1879}
1880
1881/*
1882 * Switch from Terminal-Normal mode to Terminal-Job mode.
1883 * Restores updating the terminal window.
1884 */
1885 void
1886term_enter_job_mode()
1887{
1888 term_T *term = curbuf->b_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001889
1890 set_terminal_mode(term, FALSE);
1891
1892 if (term->tl_channel_closed)
1893 cleanup_vterm(term);
1894 redraw_buf_and_status_later(curbuf, NOT_VALID);
1895}
1896
1897/*
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01001898 * Get a key from the user with terminal mode mappings.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001899 * Note: while waiting a terminal may be closed and freed if the channel is
1900 * closed and ++close was used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001901 */
1902 static int
1903term_vgetc()
1904{
1905 int c;
1906 int save_State = State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001907 int modify_other_keys =
1908 vterm_is_modify_other_keys(curbuf->b_term->tl_vterm);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001909
1910 State = TERMINAL;
1911 got_int = FALSE;
Bram Moolenaar4f974752019-02-17 17:44:42 +01001912#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001913 ctrl_break_was_pressed = FALSE;
1914#endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001915 if (modify_other_keys)
1916 ++no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001917 c = vgetc();
1918 got_int = FALSE;
1919 State = save_State;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02001920 if (modify_other_keys)
1921 --no_reduce_keys;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001922 return c;
1923}
1924
Bram Moolenaarc48369c2018-03-11 19:30:45 +01001925static int mouse_was_outside = FALSE;
1926
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001927/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001928 * Send keys to terminal.
1929 * Return FAIL when the key needs to be handled in Normal mode.
1930 * Return OK when the key was dropped or sent to the terminal.
1931 */
1932 int
1933send_keys_to_term(term_T *term, int c, int typed)
1934{
1935 char msg[KEY_BUF_LEN];
1936 size_t len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001937 int dragging_outside = FALSE;
1938
1939 /* Catch keys that need to be handled as in Normal mode. */
1940 switch (c)
1941 {
1942 case NUL:
1943 case K_ZERO:
1944 if (typed)
1945 stuffcharReadbuff(c);
1946 return FAIL;
1947
Bram Moolenaar231a2db2018-05-06 13:53:50 +02001948 case K_TABLINE:
1949 stuffcharReadbuff(c);
1950 return FAIL;
1951
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001952 case K_IGNORE:
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02001953 case K_CANCEL: // used for :normal when running out of chars
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001954 return FAIL;
1955
1956 case K_LEFTDRAG:
1957 case K_MIDDLEDRAG:
1958 case K_RIGHTDRAG:
1959 case K_X1DRAG:
1960 case K_X2DRAG:
1961 dragging_outside = mouse_was_outside;
1962 /* FALLTHROUGH */
1963 case K_LEFTMOUSE:
1964 case K_LEFTMOUSE_NM:
1965 case K_LEFTRELEASE:
1966 case K_LEFTRELEASE_NM:
Bram Moolenaar51b0f372017-11-18 18:52:04 +01001967 case K_MOUSEMOVE:
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001968 case K_MIDDLEMOUSE:
1969 case K_MIDDLERELEASE:
1970 case K_RIGHTMOUSE:
1971 case K_RIGHTRELEASE:
1972 case K_X1MOUSE:
1973 case K_X1RELEASE:
1974 case K_X2MOUSE:
1975 case K_X2RELEASE:
1976
1977 case K_MOUSEUP:
1978 case K_MOUSEDOWN:
1979 case K_MOUSELEFT:
1980 case K_MOUSERIGHT:
1981 if (mouse_row < W_WINROW(curwin)
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001982 || mouse_row >= (W_WINROW(curwin) + curwin->w_height)
Bram Moolenaar53f81742017-09-22 14:35:51 +02001983 || mouse_col < curwin->w_wincol
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001984 || mouse_col >= W_ENDCOL(curwin)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001985 || dragging_outside)
1986 {
Bram Moolenaarce6179c2017-12-05 13:06:16 +01001987 /* click or scroll outside the current window or on status line
1988 * or vertical separator */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02001989 if (typed)
1990 {
1991 stuffcharReadbuff(c);
1992 mouse_was_outside = TRUE;
1993 }
1994 return FAIL;
1995 }
1996 }
1997 if (typed)
1998 mouse_was_outside = FALSE;
1999
2000 /* Convert the typed key to a sequence of bytes for the job. */
2001 len = term_convert_key(term, c, msg);
2002 if (len > 0)
2003 /* TODO: if FAIL is returned, stop? */
2004 channel_send(term->tl_job->jv_channel, get_tty_part(term),
2005 (char_u *)msg, (int)len, NULL);
2006
2007 return OK;
2008}
2009
2010 static void
2011position_cursor(win_T *wp, VTermPos *pos)
2012{
2013 wp->w_wrow = MIN(pos->row, MAX(0, wp->w_height - 1));
2014 wp->w_wcol = MIN(pos->col, MAX(0, wp->w_width - 1));
2015 wp->w_valid |= (VALID_WCOL|VALID_WROW);
2016}
2017
2018/*
2019 * Handle CTRL-W "": send register contents to the job.
2020 */
2021 static void
2022term_paste_register(int prev_c UNUSED)
2023{
2024 int c;
2025 list_T *l;
2026 listitem_T *item;
2027 long reglen = 0;
2028 int type;
2029
2030#ifdef FEAT_CMDL_INFO
2031 if (add_to_showcmd(prev_c))
2032 if (add_to_showcmd('"'))
2033 out_flush();
2034#endif
2035 c = term_vgetc();
2036#ifdef FEAT_CMDL_INFO
2037 clear_showcmd();
2038#endif
2039 if (!term_use_loop())
2040 /* job finished while waiting for a character */
2041 return;
2042
2043 /* CTRL-W "= prompt for expression to evaluate. */
2044 if (c == '=' && get_expr_register() != '=')
2045 return;
2046 if (!term_use_loop())
2047 /* job finished while waiting for a character */
2048 return;
2049
2050 l = (list_T *)get_reg_contents(c, GREG_LIST);
2051 if (l != NULL)
2052 {
2053 type = get_reg_type(c, &reglen);
2054 for (item = l->lv_first; item != NULL; item = item->li_next)
2055 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01002056 char_u *s = tv_get_string(&item->li_tv);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002057#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002058 char_u *tmp = s;
2059
2060 if (!enc_utf8 && enc_codepage > 0)
2061 {
2062 WCHAR *ret = NULL;
2063 int length = 0;
2064
2065 MultiByteToWideChar_alloc(enc_codepage, 0, (char *)s,
2066 (int)STRLEN(s), &ret, &length);
2067 if (ret != NULL)
2068 {
2069 WideCharToMultiByte_alloc(CP_UTF8, 0,
2070 ret, length, (char **)&s, &length, 0, 0);
2071 vim_free(ret);
2072 }
2073 }
2074#endif
2075 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2076 s, (int)STRLEN(s), NULL);
Bram Moolenaar4f974752019-02-17 17:44:42 +01002077#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002078 if (tmp != s)
2079 vim_free(s);
2080#endif
2081
2082 if (item->li_next != NULL || type == MLINE)
2083 channel_send(curbuf->b_term->tl_job->jv_channel, PART_IN,
2084 (char_u *)"\r", 1, NULL);
2085 }
2086 list_free(l);
2087 }
2088}
2089
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002090/*
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002091 * Return TRUE when waiting for a character in the terminal, the cursor of the
2092 * terminal should be displayed.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002093 */
2094 int
2095terminal_is_active()
2096{
2097 return in_terminal_loop != NULL;
2098}
2099
Bram Moolenaarb2ac14c2018-05-01 18:47:59 +02002100#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002101 cursorentry_T *
2102term_get_cursor_shape(guicolor_T *fg, guicolor_T *bg)
2103{
2104 term_T *term = in_terminal_loop;
2105 static cursorentry_T entry;
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002106 int id;
2107 guicolor_T term_fg, term_bg;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002108
2109 vim_memset(&entry, 0, sizeof(entry));
2110 entry.shape = entry.mshape =
2111 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_UNDERLINE ? SHAPE_HOR :
2112 term->tl_cursor_shape == VTERM_PROP_CURSORSHAPE_BAR_LEFT ? SHAPE_VER :
2113 SHAPE_BLOCK;
2114 entry.percentage = 20;
2115 if (term->tl_cursor_blink)
2116 {
2117 entry.blinkwait = 700;
2118 entry.blinkon = 400;
2119 entry.blinkoff = 250;
2120 }
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002121
2122 /* The "Terminal" highlight group overrules the defaults. */
2123 id = syn_name2id((char_u *)"Terminal");
2124 if (id != 0)
2125 {
2126 syn_id2colors(id, &term_fg, &term_bg);
2127 *fg = term_bg;
2128 }
2129 else
2130 *fg = gui.back_pixel;
2131
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002132 if (term->tl_cursor_color == NULL)
Bram Moolenaar29e7fe52018-10-16 22:13:00 +02002133 {
2134 if (id != 0)
2135 *bg = term_fg;
2136 else
2137 *bg = gui.norm_pixel;
2138 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002139 else
2140 *bg = color_name2handle(term->tl_cursor_color);
2141 entry.name = "n";
2142 entry.used_for = SHAPE_CURSOR;
2143
2144 return &entry;
2145}
2146#endif
2147
Bram Moolenaard317b382018-02-08 22:33:31 +01002148 static void
2149may_output_cursor_props(void)
2150{
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002151 if (!cursor_color_equal(last_set_cursor_color, desired_cursor_color)
Bram Moolenaard317b382018-02-08 22:33:31 +01002152 || last_set_cursor_shape != desired_cursor_shape
2153 || last_set_cursor_blink != desired_cursor_blink)
2154 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002155 cursor_color_copy(&last_set_cursor_color, desired_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002156 last_set_cursor_shape = desired_cursor_shape;
2157 last_set_cursor_blink = desired_cursor_blink;
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002158 term_cursor_color(cursor_color_get(desired_cursor_color));
Bram Moolenaard317b382018-02-08 22:33:31 +01002159 if (desired_cursor_shape == -1 || desired_cursor_blink == -1)
2160 /* this will restore the initial cursor style, if possible */
2161 ui_cursor_shape_forced(TRUE);
2162 else
2163 term_cursor_shape(desired_cursor_shape, desired_cursor_blink);
2164 }
2165}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002166
Bram Moolenaard317b382018-02-08 22:33:31 +01002167/*
2168 * Set the cursor color and shape, if not last set to these.
2169 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002170 static void
2171may_set_cursor_props(term_T *term)
2172{
2173#ifdef FEAT_GUI
2174 /* For the GUI the cursor properties are obtained with
2175 * term_get_cursor_shape(). */
2176 if (gui.in_use)
2177 return;
2178#endif
2179 if (in_terminal_loop == term)
2180 {
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002181 cursor_color_copy(&desired_cursor_color, term->tl_cursor_color);
Bram Moolenaard317b382018-02-08 22:33:31 +01002182 desired_cursor_shape = term->tl_cursor_shape;
2183 desired_cursor_blink = term->tl_cursor_blink;
2184 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002185 }
2186}
2187
Bram Moolenaard317b382018-02-08 22:33:31 +01002188/*
2189 * Reset the desired cursor properties and restore them when needed.
2190 */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002191 static void
Bram Moolenaard317b382018-02-08 22:33:31 +01002192prepare_restore_cursor_props(void)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002193{
2194#ifdef FEAT_GUI
2195 if (gui.in_use)
2196 return;
2197#endif
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002198 cursor_color_copy(&desired_cursor_color, NULL);
Bram Moolenaard317b382018-02-08 22:33:31 +01002199 desired_cursor_shape = -1;
2200 desired_cursor_blink = -1;
2201 may_output_cursor_props();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002202}
2203
2204/*
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002205 * Returns TRUE if the current window contains a terminal and we are sending
2206 * keys to the job.
2207 * If "check_job_status" is TRUE update the job status.
2208 */
2209 static int
2210term_use_loop_check(int check_job_status)
2211{
2212 term_T *term = curbuf->b_term;
2213
2214 return term != NULL
2215 && !term->tl_normal_mode
2216 && term->tl_vterm != NULL
2217 && term_job_running_check(term, check_job_status);
2218}
2219
2220/*
2221 * Returns TRUE if the current window contains a terminal and we are sending
2222 * keys to the job.
2223 */
2224 int
2225term_use_loop(void)
2226{
2227 return term_use_loop_check(FALSE);
2228}
2229
2230/*
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002231 * Called when entering a window with the mouse. If this is a terminal window
2232 * we may want to change state.
2233 */
2234 void
2235term_win_entered()
2236{
2237 term_T *term = curbuf->b_term;
2238
2239 if (term != NULL)
2240 {
Bram Moolenaar802bfb12018-04-15 17:28:13 +02002241 if (term_use_loop_check(TRUE))
Bram Moolenaarc48369c2018-03-11 19:30:45 +01002242 {
2243 reset_VIsual_and_resel();
2244 if (State & INSERT)
2245 stop_insert_mode = TRUE;
2246 }
2247 mouse_was_outside = FALSE;
2248 enter_mouse_col = mouse_col;
2249 enter_mouse_row = mouse_row;
2250 }
2251}
2252
2253/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002254 * Wait for input and send it to the job.
2255 * When "blocking" is TRUE wait for a character to be typed. Otherwise return
2256 * when there is no more typahead.
2257 * Return when the start of a CTRL-W command is typed or anything else that
2258 * should be handled as a Normal mode command.
2259 * Returns OK if a typed character is to be handled in Normal mode, FAIL if
2260 * the terminal was closed.
2261 */
2262 int
2263terminal_loop(int blocking)
2264{
2265 int c;
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002266 int raw_c;
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002267 int termwinkey = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002268 int ret;
Bram Moolenaar12326242017-11-04 20:12:14 +01002269#ifdef UNIX
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002270 int tty_fd = curbuf->b_term->tl_job->jv_channel
2271 ->ch_part[get_tty_part(curbuf->b_term)].ch_fd;
Bram Moolenaar12326242017-11-04 20:12:14 +01002272#endif
Bram Moolenaar73dd1bd2018-05-12 21:16:25 +02002273 int restore_cursor = FALSE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002274
2275 /* Remember the terminal we are sending keys to. However, the terminal
2276 * might be closed while waiting for a character, e.g. typing "exit" in a
2277 * shell and ++close was used. Therefore use curbuf->b_term instead of a
2278 * stored reference. */
2279 in_terminal_loop = curbuf->b_term;
2280
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002281 if (*curwin->w_p_twk != NUL)
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002282 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002283 termwinkey = string_to_key(curwin->w_p_twk, TRUE);
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002284 if (termwinkey == Ctrl_W)
2285 termwinkey = 0;
2286 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002287 position_cursor(curwin, &curbuf->b_term->tl_cursor_pos);
2288 may_set_cursor_props(curbuf->b_term);
2289
Bram Moolenaarc8bcfe72018-02-27 16:29:28 +01002290 while (blocking || vpeekc_nomap() != NUL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002291 {
Bram Moolenaar13568252018-03-16 20:46:58 +01002292#ifdef FEAT_GUI
2293 if (!curbuf->b_term->tl_system)
2294#endif
Bram Moolenaar2a4857a2019-01-29 22:29:07 +01002295 // TODO: skip screen update when handling a sequence of keys.
2296 // Repeat redrawing in case a message is received while redrawing.
Bram Moolenaar13568252018-03-16 20:46:58 +01002297 while (must_redraw != 0)
2298 if (update_screen(0) == FAIL)
2299 break;
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002300 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara10ae5e2018-05-11 20:48:29 +02002301 /* job finished while redrawing */
2302 break;
2303
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002304 update_cursor(curbuf->b_term, FALSE);
Bram Moolenaard317b382018-02-08 22:33:31 +01002305 restore_cursor = TRUE;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002306
2307 c = term_vgetc();
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002308 if (!term_use_loop_check(TRUE) || in_terminal_loop != curbuf->b_term)
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002309 {
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002310 /* Job finished while waiting for a character. Push back the
2311 * received character. */
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002312 if (c != K_IGNORE)
2313 vungetc(c);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002314 break;
Bram Moolenaara3f7e582017-11-09 13:21:58 +01002315 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002316 if (c == K_IGNORE)
2317 continue;
2318
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002319 // vgetc may not include CTRL in the key when modify_other_keys is set.
2320 raw_c = c;
2321 if ((mod_mask & MOD_MASK_CTRL)
2322 && ((c >= '`' && c <= 0x7f)
2323 || (c >= '@' && c <= '_')))
2324 c &= 0x1f;
2325
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002326#ifdef UNIX
2327 /*
2328 * The shell or another program may change the tty settings. Getting
2329 * them for every typed character is a bit of overhead, but it's needed
2330 * for the first character typed, e.g. when Vim starts in a shell.
2331 */
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01002332 if (mch_isatty(tty_fd))
Bram Moolenaar26d205d2017-11-09 17:33:11 +01002333 {
2334 ttyinfo_T info;
2335
2336 /* Get the current backspace character of the pty. */
2337 if (get_tty_info(tty_fd, &info) == OK)
2338 term_backspace_char = info.backspace;
2339 }
2340#endif
2341
Bram Moolenaar4f974752019-02-17 17:44:42 +01002342#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002343 /* On Windows winpty handles CTRL-C, don't send a CTRL_C_EVENT.
2344 * Use CTRL-BREAK to kill the job. */
2345 if (ctrl_break_was_pressed)
2346 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2347#endif
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002348 /* Was either CTRL-W (termwinkey) or CTRL-\ pressed?
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002349 * Not in a system terminal. */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002350 if ((c == (termwinkey == 0 ? Ctrl_W : termwinkey) || c == Ctrl_BSL)
Bram Moolenaaraf23bad2018-03-16 22:20:49 +01002351#ifdef FEAT_GUI
2352 && !curbuf->b_term->tl_system
2353#endif
2354 )
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002355 {
2356 int prev_c = c;
2357
2358#ifdef FEAT_CMDL_INFO
2359 if (add_to_showcmd(c))
2360 out_flush();
2361#endif
2362 c = term_vgetc();
2363#ifdef FEAT_CMDL_INFO
2364 clear_showcmd();
2365#endif
Bram Moolenaar05af9a42018-05-21 18:48:12 +02002366 if (!term_use_loop_check(TRUE)
2367 || in_terminal_loop != curbuf->b_term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002368 /* job finished while waiting for a character */
2369 break;
2370
2371 if (prev_c == Ctrl_BSL)
2372 {
2373 if (c == Ctrl_N)
2374 {
2375 /* CTRL-\ CTRL-N : go to Terminal-Normal mode. */
2376 term_enter_normal_mode();
2377 ret = FAIL;
2378 goto theend;
2379 }
2380 /* Send both keys to the terminal. */
2381 send_keys_to_term(curbuf->b_term, prev_c, TRUE);
2382 }
2383 else if (c == Ctrl_C)
2384 {
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002385 /* "CTRL-W CTRL-C" or 'termwinkey' CTRL-C: end the job */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002386 mch_signal_job(curbuf->b_term->tl_job, (char_u *)"kill");
2387 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002388 else if (c == '.')
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002389 {
2390 /* "CTRL-W .": send CTRL-W to the job */
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002391 /* "'termwinkey' .": send 'termwinkey' to the job */
2392 c = termwinkey == 0 ? Ctrl_W : termwinkey;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002393 }
Bram Moolenaardcdeaaf2018-06-17 22:19:12 +02002394 else if (c == Ctrl_BSL)
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002395 {
2396 /* "CTRL-W CTRL-\": send CTRL-\ to the job */
2397 c = Ctrl_BSL;
2398 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002399 else if (c == 'N')
2400 {
2401 /* CTRL-W N : go to Terminal-Normal mode. */
2402 term_enter_normal_mode();
2403 ret = FAIL;
2404 goto theend;
2405 }
2406 else if (c == '"')
2407 {
2408 term_paste_register(prev_c);
2409 continue;
2410 }
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02002411 else if (termwinkey == 0 || c != termwinkey)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002412 {
Bram Moolenaara4b26992019-08-15 20:58:54 +02002413 char_u buf[MB_MAXBYTES + 2];
2414
2415 // Put the command into the typeahead buffer, when using the
2416 // stuff buffer KeyStuffed is set and 'langmap' won't be used.
2417 buf[0] = Ctrl_W;
2418 buf[(*mb_char2bytes)(c, buf + 1) + 1] = NUL;
2419 ins_typebuf(buf, REMAP_NONE, 0, TRUE, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002420 ret = OK;
2421 goto theend;
2422 }
2423 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01002424# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002425 if (!enc_utf8 && has_mbyte && c >= 0x80)
2426 {
2427 WCHAR wc;
2428 char_u mb[3];
2429
2430 mb[0] = (unsigned)c >> 8;
2431 mb[1] = c;
2432 if (MultiByteToWideChar(GetACP(), 0, (char*)mb, 2, &wc, 1) > 0)
2433 c = wc;
2434 }
2435# endif
Bram Moolenaar6a0299d2019-10-10 21:14:03 +02002436 if (send_keys_to_term(curbuf->b_term, raw_c, TRUE) != OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002437 {
Bram Moolenaard317b382018-02-08 22:33:31 +01002438 if (c == K_MOUSEMOVE)
2439 /* We are sure to come back here, don't reset the cursor color
2440 * and shape to avoid flickering. */
2441 restore_cursor = FALSE;
2442
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002443 ret = OK;
2444 goto theend;
2445 }
2446 }
2447 ret = FAIL;
2448
2449theend:
2450 in_terminal_loop = NULL;
Bram Moolenaard317b382018-02-08 22:33:31 +01002451 if (restore_cursor)
2452 prepare_restore_cursor_props();
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002453
2454 /* Move a snapshot of the screen contents to the buffer, so that completion
2455 * works in other buffers. */
Bram Moolenaar620020e2018-05-13 19:06:12 +02002456 if (curbuf->b_term != NULL && !curbuf->b_term->tl_normal_mode)
2457 may_move_terminal_to_buffer(curbuf->b_term, FALSE);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002458
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002459 return ret;
2460}
2461
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002462 static void
2463may_toggle_cursor(term_T *term)
2464{
2465 if (in_terminal_loop == term)
2466 {
2467 if (term->tl_cursor_visible)
2468 cursor_on();
2469 else
2470 cursor_off();
2471 }
2472}
2473
2474/*
2475 * Reverse engineer the RGB value into a cterm color index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002476 * First color is 1. Return 0 if no match found (default color).
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002477 */
2478 static int
2479color2index(VTermColor *color, int fg, int *boldp)
2480{
2481 int red = color->red;
2482 int blue = color->blue;
2483 int green = color->green;
2484
Bram Moolenaar46359e12017-11-29 22:33:38 +01002485 if (color->ansi_index != VTERM_ANSI_INDEX_NONE)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002486 {
Bram Moolenaar1d79ce82019-04-12 22:27:39 +02002487 // The first 16 colors and default: use the ANSI index.
Bram Moolenaar46359e12017-11-29 22:33:38 +01002488 switch (color->ansi_index)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002489 {
Bram Moolenaar46359e12017-11-29 22:33:38 +01002490 case 0: return 0;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01002491 case 1: return lookup_color( 0, fg, boldp) + 1; /* black */
Bram Moolenaar46359e12017-11-29 22:33:38 +01002492 case 2: return lookup_color( 4, fg, boldp) + 1; /* dark red */
2493 case 3: return lookup_color( 2, fg, boldp) + 1; /* dark green */
2494 case 4: return lookup_color( 6, fg, boldp) + 1; /* brown */
Bram Moolenaarb59118d2018-04-13 22:11:56 +02002495 case 5: return lookup_color( 1, fg, boldp) + 1; /* dark blue */
Bram Moolenaar46359e12017-11-29 22:33:38 +01002496 case 6: return lookup_color( 5, fg, boldp) + 1; /* dark magenta */
2497 case 7: return lookup_color( 3, fg, boldp) + 1; /* dark cyan */
2498 case 8: return lookup_color( 8, fg, boldp) + 1; /* light grey */
2499 case 9: return lookup_color(12, fg, boldp) + 1; /* dark grey */
2500 case 10: return lookup_color(20, fg, boldp) + 1; /* red */
2501 case 11: return lookup_color(16, fg, boldp) + 1; /* green */
2502 case 12: return lookup_color(24, fg, boldp) + 1; /* yellow */
2503 case 13: return lookup_color(14, fg, boldp) + 1; /* blue */
2504 case 14: return lookup_color(22, fg, boldp) + 1; /* magenta */
2505 case 15: return lookup_color(18, fg, boldp) + 1; /* cyan */
2506 case 16: return lookup_color(26, fg, boldp) + 1; /* white */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002507 }
2508 }
Bram Moolenaar46359e12017-11-29 22:33:38 +01002509
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002510 if (t_colors >= 256)
2511 {
2512 if (red == blue && red == green)
2513 {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002514 /* 24-color greyscale plus white and black */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002515 static int cutoff[23] = {
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002516 0x0D, 0x17, 0x21, 0x2B, 0x35, 0x3F, 0x49, 0x53, 0x5D, 0x67,
2517 0x71, 0x7B, 0x85, 0x8F, 0x99, 0xA3, 0xAD, 0xB7, 0xC1, 0xCB,
2518 0xD5, 0xDF, 0xE9};
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002519 int i;
2520
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002521 if (red < 5)
2522 return 17; /* 00/00/00 */
2523 if (red > 245) /* ff/ff/ff */
2524 return 232;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002525 for (i = 0; i < 23; ++i)
2526 if (red < cutoff[i])
2527 return i + 233;
2528 return 256;
2529 }
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002530 {
2531 static int cutoff[5] = {0x2F, 0x73, 0x9B, 0xC3, 0xEB};
2532 int ri, gi, bi;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002533
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02002534 /* 216-color cube */
2535 for (ri = 0; ri < 5; ++ri)
2536 if (red < cutoff[ri])
2537 break;
2538 for (gi = 0; gi < 5; ++gi)
2539 if (green < cutoff[gi])
2540 break;
2541 for (bi = 0; bi < 5; ++bi)
2542 if (blue < cutoff[bi])
2543 break;
2544 return 17 + ri * 36 + gi * 6 + bi;
2545 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002546 }
2547 return 0;
2548}
2549
2550/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01002551 * Convert Vterm attributes to highlight flags.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002552 */
2553 static int
Bram Moolenaard96ff162018-02-18 22:13:29 +01002554vtermAttr2hl(VTermScreenCellAttrs cellattrs)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002555{
2556 int attr = 0;
2557
2558 if (cellattrs.bold)
2559 attr |= HL_BOLD;
2560 if (cellattrs.underline)
2561 attr |= HL_UNDERLINE;
2562 if (cellattrs.italic)
2563 attr |= HL_ITALIC;
2564 if (cellattrs.strike)
2565 attr |= HL_STRIKETHROUGH;
2566 if (cellattrs.reverse)
2567 attr |= HL_INVERSE;
Bram Moolenaard96ff162018-02-18 22:13:29 +01002568 return attr;
2569}
2570
2571/*
2572 * Store Vterm attributes in "cell" from highlight flags.
2573 */
2574 static void
2575hl2vtermAttr(int attr, cellattr_T *cell)
2576{
2577 vim_memset(&cell->attrs, 0, sizeof(VTermScreenCellAttrs));
2578 if (attr & HL_BOLD)
2579 cell->attrs.bold = 1;
2580 if (attr & HL_UNDERLINE)
2581 cell->attrs.underline = 1;
2582 if (attr & HL_ITALIC)
2583 cell->attrs.italic = 1;
2584 if (attr & HL_STRIKETHROUGH)
2585 cell->attrs.strike = 1;
2586 if (attr & HL_INVERSE)
2587 cell->attrs.reverse = 1;
2588}
2589
2590/*
2591 * Convert the attributes of a vterm cell into an attribute index.
2592 */
2593 static int
2594cell2attr(VTermScreenCellAttrs cellattrs, VTermColor cellfg, VTermColor cellbg)
2595{
2596 int attr = vtermAttr2hl(cellattrs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002597
2598#ifdef FEAT_GUI
2599 if (gui.in_use)
2600 {
2601 guicolor_T fg, bg;
2602
2603 fg = gui_mch_get_rgb_color(cellfg.red, cellfg.green, cellfg.blue);
2604 bg = gui_mch_get_rgb_color(cellbg.red, cellbg.green, cellbg.blue);
2605 return get_gui_attr_idx(attr, fg, bg);
2606 }
2607 else
2608#endif
2609#ifdef FEAT_TERMGUICOLORS
2610 if (p_tgc)
2611 {
2612 guicolor_T fg, bg;
2613
2614 fg = gui_get_rgb_color_cmn(cellfg.red, cellfg.green, cellfg.blue);
2615 bg = gui_get_rgb_color_cmn(cellbg.red, cellbg.green, cellbg.blue);
2616
2617 return get_tgc_attr_idx(attr, fg, bg);
2618 }
2619 else
2620#endif
2621 {
2622 int bold = MAYBE;
2623 int fg = color2index(&cellfg, TRUE, &bold);
2624 int bg = color2index(&cellbg, FALSE, &bold);
2625
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002626 /* Use the "Terminal" highlighting for the default colors. */
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002627 if ((fg == 0 || bg == 0) && t_colors >= 16)
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002628 {
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01002629 if (fg == 0 && term_default_cterm_fg >= 0)
2630 fg = term_default_cterm_fg + 1;
2631 if (bg == 0 && term_default_cterm_bg >= 0)
2632 bg = term_default_cterm_bg + 1;
Bram Moolenaar76bb7192017-11-30 22:07:07 +01002633 }
2634
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002635 /* with 8 colors set the bold attribute to get a bright foreground */
2636 if (bold == TRUE)
2637 attr |= HL_BOLD;
2638 return get_cterm_attr_idx(attr, fg, bg);
2639 }
2640 return 0;
2641}
2642
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002643 static void
2644set_dirty_snapshot(term_T *term)
2645{
2646 term->tl_dirty_snapshot = TRUE;
2647#ifdef FEAT_TIMERS
2648 if (!term->tl_normal_mode)
2649 {
2650 /* Update the snapshot after 100 msec of not getting updates. */
2651 profile_setlimit(100L, &term->tl_timer_due);
2652 term->tl_timer_set = TRUE;
2653 }
2654#endif
2655}
2656
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002657 static int
2658handle_damage(VTermRect rect, void *user)
2659{
2660 term_T *term = (term_T *)user;
2661
2662 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, rect.start_row);
2663 term->tl_dirty_row_end = MAX(term->tl_dirty_row_end, rect.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002664 set_dirty_snapshot(term);
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002665 redraw_buf_later(term->tl_buffer, SOME_VALID);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002666 return 1;
2667}
2668
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002669 static void
2670term_scroll_up(term_T *term, int start_row, int count)
2671{
2672 win_T *wp;
2673 VTermColor fg, bg;
2674 VTermScreenCellAttrs attr;
2675 int clear_attr;
2676
2677 /* Set the color to clear lines with. */
2678 vterm_state_get_default_colors(vterm_obtain_state(term->tl_vterm),
2679 &fg, &bg);
2680 vim_memset(&attr, 0, sizeof(attr));
2681 clear_attr = cell2attr(attr, fg, bg);
2682
2683 FOR_ALL_WINDOWS(wp)
2684 {
2685 if (wp->w_buffer == term->tl_buffer)
2686 win_del_lines(wp, start_row, count, FALSE, FALSE, clear_attr);
2687 }
2688}
2689
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002690 static int
2691handle_moverect(VTermRect dest, VTermRect src, void *user)
2692{
2693 term_T *term = (term_T *)user;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002694 int count = src.start_row - dest.start_row;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002695
2696 /* Scrolling up is done much more efficiently by deleting lines instead of
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002697 * redrawing the text. But avoid doing this multiple times, postpone until
2698 * the redraw happens. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002699 if (dest.start_col == src.start_col
2700 && dest.end_col == src.end_col
2701 && dest.start_row < src.start_row)
2702 {
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002703 if (dest.start_row == 0)
2704 term->tl_postponed_scroll += count;
2705 else
2706 term_scroll_up(term, dest.start_row, count);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002707 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002708
2709 term->tl_dirty_row_start = MIN(term->tl_dirty_row_start, dest.start_row);
2710 term->tl_dirty_row_end = MIN(term->tl_dirty_row_end, dest.end_row);
Bram Moolenaar56bc8e22018-05-10 18:05:56 +02002711 set_dirty_snapshot(term);
Bram Moolenaar3a497e12017-09-30 20:40:27 +02002712
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02002713 /* Note sure if the scrolling will work correctly, let's do a complete
2714 * redraw later. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002715 redraw_buf_later(term->tl_buffer, NOT_VALID);
2716 return 1;
2717}
2718
2719 static int
2720handle_movecursor(
2721 VTermPos pos,
2722 VTermPos oldpos UNUSED,
2723 int visible,
2724 void *user)
2725{
2726 term_T *term = (term_T *)user;
2727 win_T *wp;
2728
2729 term->tl_cursor_pos = pos;
2730 term->tl_cursor_visible = visible;
2731
2732 FOR_ALL_WINDOWS(wp)
2733 {
2734 if (wp->w_buffer == term->tl_buffer)
2735 position_cursor(wp, &pos);
2736 }
2737 if (term->tl_buffer == curbuf && !term->tl_normal_mode)
2738 {
2739 may_toggle_cursor(term);
2740 update_cursor(term, term->tl_cursor_visible);
2741 }
2742
2743 return 1;
2744}
2745
2746 static int
2747handle_settermprop(
2748 VTermProp prop,
2749 VTermValue *value,
2750 void *user)
2751{
2752 term_T *term = (term_T *)user;
2753
2754 switch (prop)
2755 {
2756 case VTERM_PROP_TITLE:
2757 vim_free(term->tl_title);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002758 // a blank title isn't useful, make it empty, so that "running" is
2759 // displayed
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002760 if (*skipwhite((char_u *)value->string) == NUL)
2761 term->tl_title = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01002762 // Same as blank
2763 else if (term->tl_arg0_cmd != NULL
2764 && STRNCMP(term->tl_arg0_cmd, (char_u *)value->string,
2765 (int)STRLEN(term->tl_arg0_cmd)) == 0)
2766 term->tl_title = NULL;
2767 // Empty corrupted data of winpty
2768 else if (STRNCMP(" - ", (char_u *)value->string, 4) == 0)
2769 term->tl_title = NULL;
Bram Moolenaar4f974752019-02-17 17:44:42 +01002770#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002771 else if (!enc_utf8 && enc_codepage > 0)
2772 {
2773 WCHAR *ret = NULL;
2774 int length = 0;
2775
2776 MultiByteToWideChar_alloc(CP_UTF8, 0,
2777 (char*)value->string, (int)STRLEN(value->string),
2778 &ret, &length);
2779 if (ret != NULL)
2780 {
2781 WideCharToMultiByte_alloc(enc_codepage, 0,
2782 ret, length, (char**)&term->tl_title,
2783 &length, 0, 0);
2784 vim_free(ret);
2785 }
2786 }
2787#endif
2788 else
2789 term->tl_title = vim_strsave((char_u *)value->string);
Bram Moolenaard23a8232018-02-10 18:45:26 +01002790 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002791 if (term == curbuf->b_term)
2792 maketitle();
2793 break;
2794
2795 case VTERM_PROP_CURSORVISIBLE:
2796 term->tl_cursor_visible = value->boolean;
2797 may_toggle_cursor(term);
2798 out_flush();
2799 break;
2800
2801 case VTERM_PROP_CURSORBLINK:
2802 term->tl_cursor_blink = value->boolean;
2803 may_set_cursor_props(term);
2804 break;
2805
2806 case VTERM_PROP_CURSORSHAPE:
2807 term->tl_cursor_shape = value->number;
2808 may_set_cursor_props(term);
2809 break;
2810
2811 case VTERM_PROP_CURSORCOLOR:
Bram Moolenaar4f7fd562018-05-21 14:55:28 +02002812 cursor_color_copy(&term->tl_cursor_color, (char_u*)value->string);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002813 may_set_cursor_props(term);
2814 break;
2815
2816 case VTERM_PROP_ALTSCREEN:
2817 /* TODO: do anything else? */
2818 term->tl_using_altscreen = value->boolean;
2819 break;
2820
2821 default:
2822 break;
2823 }
2824 /* Always return 1, otherwise vterm doesn't store the value internally. */
2825 return 1;
2826}
2827
2828/*
2829 * The job running in the terminal resized the terminal.
2830 */
2831 static int
2832handle_resize(int rows, int cols, void *user)
2833{
2834 term_T *term = (term_T *)user;
2835 win_T *wp;
2836
2837 term->tl_rows = rows;
2838 term->tl_cols = cols;
2839 if (term->tl_vterm_size_changed)
2840 /* Size was set by vterm_set_size(), don't set the window size. */
2841 term->tl_vterm_size_changed = FALSE;
2842 else
2843 {
2844 FOR_ALL_WINDOWS(wp)
2845 {
2846 if (wp->w_buffer == term->tl_buffer)
2847 {
2848 win_setheight_win(rows, wp);
2849 win_setwidth_win(cols, wp);
2850 }
2851 }
2852 redraw_buf_later(term->tl_buffer, NOT_VALID);
2853 }
2854 return 1;
2855}
2856
2857/*
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002858 * If the number of lines that are stored goes over 'termscrollback' then
2859 * delete the first 10%.
2860 * "gap" points to tl_scrollback or tl_scrollback_postponed.
2861 * "update_buffer" is TRUE when the buffer should be updated.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002862 */
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002863 static void
2864limit_scrollback(term_T *term, garray_T *gap, int update_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002865{
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002866 if (gap->ga_len >= term->tl_buffer->b_p_twsl)
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002867 {
Bram Moolenaar6d150f72018-04-21 20:03:20 +02002868 int todo = term->tl_buffer->b_p_twsl / 10;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002869 int i;
2870
2871 curbuf = term->tl_buffer;
2872 for (i = 0; i < todo; ++i)
2873 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002874 vim_free(((sb_line_T *)gap->ga_data + i)->sb_cells);
2875 if (update_buffer)
2876 ml_delete(1, FALSE);
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002877 }
2878 curbuf = curwin->w_buffer;
2879
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002880 gap->ga_len -= todo;
2881 mch_memmove(gap->ga_data,
2882 (sb_line_T *)gap->ga_data + todo,
2883 sizeof(sb_line_T) * gap->ga_len);
2884 if (update_buffer)
2885 term->tl_scrollback_scrolled -= todo;
2886 }
2887}
2888
2889/*
2890 * Handle a line that is pushed off the top of the screen.
2891 */
2892 static int
2893handle_pushline(int cols, const VTermScreenCell *cells, void *user)
2894{
2895 term_T *term = (term_T *)user;
2896 garray_T *gap;
2897 int update_buffer;
2898
2899 if (term->tl_normal_mode)
2900 {
2901 // In Terminal-Normal mode the user interacts with the buffer, thus we
2902 // must not change it. Postpone adding the scrollback lines.
2903 gap = &term->tl_scrollback_postponed;
2904 update_buffer = FALSE;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002905 }
2906 else
2907 {
2908 // First remove the lines that were appended before, the pushed line
2909 // goes above it.
2910 cleanup_scrollback(term);
2911 gap = &term->tl_scrollback;
2912 update_buffer = TRUE;
Bram Moolenaar8c041b62018-04-14 18:14:06 +02002913 }
2914
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002915 limit_scrollback(term, gap, update_buffer);
2916
2917 if (ga_grow(gap, 1) == OK)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002918 {
2919 cellattr_T *p = NULL;
2920 int len = 0;
2921 int i;
2922 int c;
2923 int col;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002924 int text_len;
2925 char_u *text;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002926 sb_line_T *line;
2927 garray_T ga;
2928 cellattr_T fill_attr = term->tl_default_color;
2929
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002930 // do not store empty cells at the end
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002931 for (i = 0; i < cols; ++i)
2932 if (cells[i].chars[0] != 0)
2933 len = i + 1;
2934 else
2935 cell2cellattr(&cells[i], &fill_attr);
2936
2937 ga_init2(&ga, 1, 100);
2938 if (len > 0)
Bram Moolenaarc799fe22019-05-28 23:08:19 +02002939 p = ALLOC_MULT(cellattr_T, len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002940 if (p != NULL)
2941 {
2942 for (col = 0; col < len; col += cells[col].width)
2943 {
2944 if (ga_grow(&ga, MB_MAXBYTES) == FAIL)
2945 {
2946 ga.ga_len = 0;
2947 break;
2948 }
2949 for (i = 0; (c = cells[col].chars[i]) > 0 || i == 0; ++i)
2950 ga.ga_len += utf_char2bytes(c == NUL ? ' ' : c,
2951 (char_u *)ga.ga_data + ga.ga_len);
2952 cell2cellattr(&cells[col], &p[col]);
2953 }
2954 }
2955 if (ga_grow(&ga, 1) == FAIL)
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002956 {
2957 if (update_buffer)
2958 text = (char_u *)"";
2959 else
2960 text = vim_strsave((char_u *)"");
2961 text_len = 0;
2962 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002963 else
2964 {
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002965 text = ga.ga_data;
2966 text_len = ga.ga_len;
2967 *(text + text_len) = NUL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002968 }
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002969 if (update_buffer)
2970 add_scrollback_line_to_buffer(term, text, text_len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002971
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002972 line = (sb_line_T *)gap->ga_data + gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002973 line->sb_cols = len;
2974 line->sb_cells = p;
2975 line->sb_fill_attr = fill_attr;
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002976 if (update_buffer)
2977 {
2978 line->sb_text = NULL;
2979 ++term->tl_scrollback_scrolled;
2980 ga_clear(&ga); // free the text
2981 }
2982 else
2983 {
2984 line->sb_text = text;
2985 ga_init(&ga); // text is kept in tl_scrollback_postponed
2986 }
2987 ++gap->ga_len;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02002988 }
2989 return 0; /* ignored */
2990}
2991
Bram Moolenaar29ae2232019-02-14 21:22:01 +01002992/*
2993 * Called when leaving Terminal-Normal mode: deal with any scrollback that was
2994 * received and stored in tl_scrollback_postponed.
2995 */
2996 static void
2997handle_postponed_scrollback(term_T *term)
2998{
2999 int i;
3000
Bram Moolenaar8376c3d2019-03-19 20:50:43 +01003001 if (term->tl_scrollback_postponed.ga_len == 0)
3002 return;
3003 ch_log(NULL, "Moving postponed scrollback to scrollback");
3004
Bram Moolenaar29ae2232019-02-14 21:22:01 +01003005 // First remove the lines that were appended before, the pushed lines go
3006 // above it.
3007 cleanup_scrollback(term);
3008
3009 for (i = 0; i < term->tl_scrollback_postponed.ga_len; ++i)
3010 {
3011 char_u *text;
3012 sb_line_T *pp_line;
3013 sb_line_T *line;
3014
3015 if (ga_grow(&term->tl_scrollback, 1) == FAIL)
3016 break;
3017 pp_line = (sb_line_T *)term->tl_scrollback_postponed.ga_data + i;
3018
3019 text = pp_line->sb_text;
3020 if (text == NULL)
3021 text = (char_u *)"";
3022 add_scrollback_line_to_buffer(term, text, (int)STRLEN(text));
3023 vim_free(pp_line->sb_text);
3024
3025 line = (sb_line_T *)term->tl_scrollback.ga_data
3026 + term->tl_scrollback.ga_len;
3027 line->sb_cols = pp_line->sb_cols;
3028 line->sb_cells = pp_line->sb_cells;
3029 line->sb_fill_attr = pp_line->sb_fill_attr;
3030 line->sb_text = NULL;
3031 ++term->tl_scrollback_scrolled;
3032 ++term->tl_scrollback.ga_len;
3033 }
3034
3035 ga_clear(&term->tl_scrollback_postponed);
3036 limit_scrollback(term, &term->tl_scrollback, TRUE);
3037}
3038
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003039static VTermScreenCallbacks screen_callbacks = {
3040 handle_damage, /* damage */
3041 handle_moverect, /* moverect */
3042 handle_movecursor, /* movecursor */
3043 handle_settermprop, /* settermprop */
3044 NULL, /* bell */
3045 handle_resize, /* resize */
3046 handle_pushline, /* sb_pushline */
3047 NULL /* sb_popline */
3048};
3049
3050/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003051 * Do the work after the channel of a terminal was closed.
3052 * Must be called only when updating_screen is FALSE.
3053 * Returns TRUE when a buffer was closed (list of terminals may have changed).
3054 */
3055 static int
3056term_after_channel_closed(term_T *term)
3057{
3058 /* Unless in Terminal-Normal mode: clear the vterm. */
3059 if (!term->tl_normal_mode)
3060 {
3061 int fnum = term->tl_buffer->b_fnum;
3062
3063 cleanup_vterm(term);
3064
3065 if (term->tl_finish == TL_FINISH_CLOSE)
3066 {
3067 aco_save_T aco;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003068 int do_set_w_closing = term->tl_buffer->b_nwindows == 0;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003069
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003070 // ++close or term_finish == "close"
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003071 ch_log(NULL, "terminal job finished, closing window");
3072 aucmd_prepbuf(&aco, term->tl_buffer);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003073 // Avoid closing the window if we temporarily use it.
Bram Moolenaar517f71a2019-06-17 22:40:41 +02003074 if (curwin == aucmd_win)
3075 do_set_w_closing = TRUE;
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003076 if (do_set_w_closing)
3077 curwin->w_closing = TRUE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003078 do_bufdel(DOBUF_WIPE, (char_u *)"", 1, fnum, fnum, FALSE);
Bram Moolenaar5db7eec2018-08-07 16:33:18 +02003079 if (do_set_w_closing)
3080 curwin->w_closing = FALSE;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003081 aucmd_restbuf(&aco);
3082 return TRUE;
3083 }
3084 if (term->tl_finish == TL_FINISH_OPEN
3085 && term->tl_buffer->b_nwindows == 0)
3086 {
3087 char buf[50];
3088
3089 /* TODO: use term_opencmd */
3090 ch_log(NULL, "terminal job finished, opening window");
3091 vim_snprintf(buf, sizeof(buf),
3092 term->tl_opencmd == NULL
3093 ? "botright sbuf %d"
3094 : (char *)term->tl_opencmd, fnum);
3095 do_cmdline_cmd((char_u *)buf);
3096 }
3097 else
3098 ch_log(NULL, "terminal job finished");
3099 }
3100
3101 redraw_buf_and_status_later(term->tl_buffer, NOT_VALID);
3102 return FALSE;
3103}
3104
3105/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003106 * Called when a channel has been closed.
3107 * If this was a channel for a terminal window then finish it up.
3108 */
3109 void
3110term_channel_closed(channel_T *ch)
3111{
3112 term_T *term;
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003113 term_T *next_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003114 int did_one = FALSE;
3115
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003116 for (term = first_term; term != NULL; term = next_term)
3117 {
3118 next_term = term->tl_next;
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02003119 if (term->tl_job == ch->ch_job && !term->tl_channel_closed)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003120 {
3121 term->tl_channel_closed = TRUE;
3122 did_one = TRUE;
3123
Bram Moolenaard23a8232018-02-10 18:45:26 +01003124 VIM_CLEAR(term->tl_title);
3125 VIM_CLEAR(term->tl_status_text);
Bram Moolenaar4f974752019-02-17 17:44:42 +01003126#ifdef MSWIN
Bram Moolenaar402c8392018-05-06 22:01:42 +02003127 if (term->tl_out_fd != NULL)
3128 {
3129 fclose(term->tl_out_fd);
3130 term->tl_out_fd = NULL;
3131 }
3132#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003133
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003134 if (updating_screen)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003135 {
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003136 /* Cannot open or close windows now. Can happen when
3137 * 'lazyredraw' is set. */
3138 term->tl_channel_recently_closed = TRUE;
3139 continue;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003140 }
3141
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003142 if (term_after_channel_closed(term))
3143 next_term = first_term;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003144 }
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003145 }
3146
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003147 if (did_one)
3148 {
3149 redraw_statuslines();
3150
3151 /* Need to break out of vgetc(). */
3152 ins_char_typebuf(K_IGNORE);
3153 typebuf_was_filled = TRUE;
3154
3155 term = curbuf->b_term;
3156 if (term != NULL)
3157 {
3158 if (term->tl_job == ch->ch_job)
3159 maketitle();
3160 update_cursor(term, term->tl_cursor_visible);
3161 }
3162 }
3163}
3164
3165/*
Bram Moolenaar0cb8ac72018-05-11 22:01:51 +02003166 * To be called after resetting updating_screen: handle any terminal where the
3167 * channel was closed.
3168 */
3169 void
3170term_check_channel_closed_recently()
3171{
3172 term_T *term;
3173 term_T *next_term;
3174
3175 for (term = first_term; term != NULL; term = next_term)
3176 {
3177 next_term = term->tl_next;
3178 if (term->tl_channel_recently_closed)
3179 {
3180 term->tl_channel_recently_closed = FALSE;
3181 if (term_after_channel_closed(term))
3182 // start over, the list may have changed
3183 next_term = first_term;
3184 }
3185 }
3186}
3187
3188/*
Bram Moolenaar13568252018-03-16 20:46:58 +01003189 * Fill one screen line from a line of the terminal.
3190 * Advances "pos" to past the last column.
3191 */
3192 static void
3193term_line2screenline(VTermScreen *screen, VTermPos *pos, int max_col)
3194{
3195 int off = screen_get_current_line_off();
3196
3197 for (pos->col = 0; pos->col < max_col; )
3198 {
3199 VTermScreenCell cell;
3200 int c;
3201
3202 if (vterm_screen_get_cell(screen, *pos, &cell) == 0)
3203 vim_memset(&cell, 0, sizeof(cell));
3204
3205 c = cell.chars[0];
3206 if (c == NUL)
3207 {
3208 ScreenLines[off] = ' ';
3209 if (enc_utf8)
3210 ScreenLinesUC[off] = NUL;
3211 }
3212 else
3213 {
3214 if (enc_utf8)
3215 {
3216 int i;
3217
3218 /* composing chars */
3219 for (i = 0; i < Screen_mco
3220 && i + 1 < VTERM_MAX_CHARS_PER_CELL; ++i)
3221 {
3222 ScreenLinesC[i][off] = cell.chars[i + 1];
3223 if (cell.chars[i + 1] == 0)
3224 break;
3225 }
3226 if (c >= 0x80 || (Screen_mco > 0
3227 && ScreenLinesC[0][off] != 0))
3228 {
3229 ScreenLines[off] = ' ';
3230 ScreenLinesUC[off] = c;
3231 }
3232 else
3233 {
3234 ScreenLines[off] = c;
3235 ScreenLinesUC[off] = NUL;
3236 }
3237 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01003238#ifdef MSWIN
Bram Moolenaar13568252018-03-16 20:46:58 +01003239 else if (has_mbyte && c >= 0x80)
3240 {
3241 char_u mb[MB_MAXBYTES+1];
3242 WCHAR wc = c;
3243
3244 if (WideCharToMultiByte(GetACP(), 0, &wc, 1,
3245 (char*)mb, 2, 0, 0) > 1)
3246 {
3247 ScreenLines[off] = mb[0];
3248 ScreenLines[off + 1] = mb[1];
3249 cell.width = mb_ptr2cells(mb);
3250 }
3251 else
3252 ScreenLines[off] = c;
3253 }
3254#endif
3255 else
3256 ScreenLines[off] = c;
3257 }
3258 ScreenAttrs[off] = cell2attr(cell.attrs, cell.fg, cell.bg);
3259
3260 ++pos->col;
3261 ++off;
3262 if (cell.width == 2)
3263 {
3264 if (enc_utf8)
3265 ScreenLinesUC[off] = NUL;
3266
3267 /* don't set the second byte to NUL for a DBCS encoding, it
3268 * has been set above */
3269 if (enc_utf8 || !has_mbyte)
3270 ScreenLines[off] = NUL;
3271
3272 ++pos->col;
3273 ++off;
3274 }
3275 }
3276}
3277
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003278#if defined(FEAT_GUI)
Bram Moolenaar13568252018-03-16 20:46:58 +01003279 static void
3280update_system_term(term_T *term)
3281{
3282 VTermPos pos;
3283 VTermScreen *screen;
3284
3285 if (term->tl_vterm == NULL)
3286 return;
3287 screen = vterm_obtain_screen(term->tl_vterm);
3288
3289 /* Scroll up to make more room for terminal lines if needed. */
3290 while (term->tl_toprow > 0
3291 && (Rows - term->tl_toprow) < term->tl_dirty_row_end)
3292 {
3293 int save_p_more = p_more;
3294
3295 p_more = FALSE;
3296 msg_row = Rows - 1;
Bram Moolenaar113e1072019-01-20 15:30:40 +01003297 msg_puts("\n");
Bram Moolenaar13568252018-03-16 20:46:58 +01003298 p_more = save_p_more;
3299 --term->tl_toprow;
3300 }
3301
3302 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3303 && pos.row < Rows; ++pos.row)
3304 {
3305 if (pos.row < term->tl_rows)
3306 {
3307 int max_col = MIN(Columns, term->tl_cols);
3308
3309 term_line2screenline(screen, &pos, max_col);
3310 }
3311 else
3312 pos.col = 0;
3313
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003314 screen_line(term->tl_toprow + pos.row, 0, pos.col, Columns, 0);
Bram Moolenaar13568252018-03-16 20:46:58 +01003315 }
3316
3317 term->tl_dirty_row_start = MAX_ROW;
3318 term->tl_dirty_row_end = 0;
Bram Moolenaar13568252018-03-16 20:46:58 +01003319}
Bram Moolenaar4ac31ee2018-03-16 21:34:25 +01003320#endif
Bram Moolenaar13568252018-03-16 20:46:58 +01003321
3322/*
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003323 * Return TRUE if window "wp" is to be redrawn with term_update_window().
3324 * Returns FALSE when there is no terminal running in this window or it is in
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003325 * Terminal-Normal mode.
3326 */
3327 int
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003328term_do_update_window(win_T *wp)
3329{
3330 term_T *term = wp->w_buffer->b_term;
3331
3332 return term != NULL && term->tl_vterm != NULL && !term->tl_normal_mode;
3333}
3334
3335/*
3336 * Called to update a window that contains an active terminal.
3337 */
3338 void
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003339term_update_window(win_T *wp)
3340{
3341 term_T *term = wp->w_buffer->b_term;
3342 VTerm *vterm;
3343 VTermScreen *screen;
3344 VTermState *state;
3345 VTermPos pos;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003346 int rows, cols;
3347 int newrows, newcols;
3348 int minsize;
3349 win_T *twp;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003350
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003351 vterm = term->tl_vterm;
3352 screen = vterm_obtain_screen(vterm);
3353 state = vterm_obtain_state(vterm);
3354
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003355 /* We use NOT_VALID on a resize or scroll, redraw everything then. With
3356 * SOME_VALID only redraw what was marked dirty. */
3357 if (wp->w_redr_type > SOME_VALID)
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003358 {
3359 term->tl_dirty_row_start = 0;
3360 term->tl_dirty_row_end = MAX_ROW;
Bram Moolenaar6eddadf2018-05-06 16:40:16 +02003361
3362 if (term->tl_postponed_scroll > 0
3363 && term->tl_postponed_scroll < term->tl_rows / 3)
3364 /* Scrolling is usually faster than redrawing, when there are only
3365 * a few lines to scroll. */
3366 term_scroll_up(term, 0, term->tl_postponed_scroll);
3367 term->tl_postponed_scroll = 0;
Bram Moolenaar19a3d682017-10-02 21:54:59 +02003368 }
3369
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003370 /*
3371 * If the window was resized a redraw will be triggered and we get here.
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003372 * Adjust the size of the vterm unless 'termwinsize' specifies a fixed size.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003373 */
Bram Moolenaarb833c1e2018-05-05 16:36:06 +02003374 minsize = parse_termwinsize(wp, &rows, &cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003375
Bram Moolenaar498c2562018-04-15 23:45:15 +02003376 newrows = 99999;
3377 newcols = 99999;
3378 FOR_ALL_WINDOWS(twp)
3379 {
3380 /* When more than one window shows the same terminal, use the
3381 * smallest size. */
3382 if (twp->w_buffer == term->tl_buffer)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003383 {
Bram Moolenaar498c2562018-04-15 23:45:15 +02003384 newrows = MIN(newrows, twp->w_height);
3385 newcols = MIN(newcols, twp->w_width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003386 }
Bram Moolenaar498c2562018-04-15 23:45:15 +02003387 }
Bram Moolenaare0d749a2019-09-25 22:14:48 +02003388 if (newrows == 99999 || newcols == 99999)
3389 return; // safety exit
Bram Moolenaar498c2562018-04-15 23:45:15 +02003390 newrows = rows == 0 ? newrows : minsize ? MAX(rows, newrows) : rows;
3391 newcols = cols == 0 ? newcols : minsize ? MAX(cols, newcols) : cols;
3392
3393 if (term->tl_rows != newrows || term->tl_cols != newcols)
3394 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003395 term->tl_vterm_size_changed = TRUE;
Bram Moolenaar498c2562018-04-15 23:45:15 +02003396 vterm_set_size(vterm, newrows, newcols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003397 ch_log(term->tl_job->jv_channel, "Resizing terminal to %d lines",
Bram Moolenaar498c2562018-04-15 23:45:15 +02003398 newrows);
3399 term_report_winsize(term, newrows, newcols);
Bram Moolenaar875cf872018-07-08 20:49:07 +02003400
3401 // Updating the terminal size will cause the snapshot to be cleared.
3402 // When not in terminal_loop() we need to restore it.
3403 if (term != in_terminal_loop)
3404 may_move_terminal_to_buffer(term, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003405 }
3406
3407 /* The cursor may have been moved when resizing. */
3408 vterm_state_get_cursorpos(state, &pos);
3409 position_cursor(wp, &pos);
3410
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003411 for (pos.row = term->tl_dirty_row_start; pos.row < term->tl_dirty_row_end
3412 && pos.row < wp->w_height; ++pos.row)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003413 {
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003414 if (pos.row < term->tl_rows)
3415 {
Bram Moolenaar13568252018-03-16 20:46:58 +01003416 int max_col = MIN(wp->w_width, term->tl_cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003417
Bram Moolenaar13568252018-03-16 20:46:58 +01003418 term_line2screenline(screen, &pos, max_col);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003419 }
3420 else
3421 pos.col = 0;
3422
Bram Moolenaarf118d482018-03-13 13:14:00 +01003423 screen_line(wp->w_winrow + pos.row
3424#ifdef FEAT_MENU
3425 + winbar_height(wp)
3426#endif
Bram Moolenaar4d784b22019-05-25 19:51:39 +02003427 , wp->w_wincol, pos.col, wp->w_width, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003428 }
Bram Moolenaar3a497e12017-09-30 20:40:27 +02003429 term->tl_dirty_row_start = MAX_ROW;
3430 term->tl_dirty_row_end = 0;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003431}
3432
3433/*
3434 * Return TRUE if "wp" is a terminal window where the job has finished.
3435 */
3436 int
3437term_is_finished(buf_T *buf)
3438{
3439 return buf->b_term != NULL && buf->b_term->tl_vterm == NULL;
3440}
3441
3442/*
3443 * Return TRUE if "wp" is a terminal window where the job has finished or we
3444 * are in Terminal-Normal mode, thus we show the buffer contents.
3445 */
3446 int
3447term_show_buffer(buf_T *buf)
3448{
3449 term_T *term = buf->b_term;
3450
3451 return term != NULL && (term->tl_vterm == NULL || term->tl_normal_mode);
3452}
3453
3454/*
3455 * The current buffer is going to be changed. If there is terminal
3456 * highlighting remove it now.
3457 */
3458 void
3459term_change_in_curbuf(void)
3460{
3461 term_T *term = curbuf->b_term;
3462
3463 if (term_is_finished(curbuf) && term->tl_scrollback.ga_len > 0)
3464 {
3465 free_scrollback(term);
3466 redraw_buf_later(term->tl_buffer, NOT_VALID);
3467
3468 /* The buffer is now like a normal buffer, it cannot be easily
3469 * abandoned when changed. */
3470 set_string_option_direct((char_u *)"buftype", -1,
3471 (char_u *)"", OPT_FREE|OPT_LOCAL, 0);
3472 }
3473}
3474
3475/*
3476 * Get the screen attribute for a position in the buffer.
3477 * Use a negative "col" to get the filler background color.
3478 */
3479 int
3480term_get_attr(buf_T *buf, linenr_T lnum, int col)
3481{
3482 term_T *term = buf->b_term;
3483 sb_line_T *line;
3484 cellattr_T *cellattr;
3485
3486 if (lnum > term->tl_scrollback.ga_len)
3487 cellattr = &term->tl_default_color;
3488 else
3489 {
3490 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum - 1;
3491 if (col < 0 || col >= line->sb_cols)
3492 cellattr = &line->sb_fill_attr;
3493 else
3494 cellattr = line->sb_cells + col;
3495 }
3496 return cell2attr(cellattr->attrs, cellattr->fg, cellattr->bg);
3497}
3498
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003499/*
3500 * Convert a cterm color number 0 - 255 to RGB.
Bram Moolenaara8fc0d32017-09-26 13:59:47 +02003501 * This is compatible with xterm.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003502 */
3503 static void
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003504cterm_color2vterm(int nr, VTermColor *rgb)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003505{
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003506 cterm_color2rgb(nr, &rgb->red, &rgb->green, &rgb->blue, &rgb->ansi_index);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003507}
3508
3509/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01003510 * Initialize term->tl_default_color from the environment.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003511 */
3512 static void
Bram Moolenaar52acb112018-03-18 19:20:22 +01003513init_default_colors(term_T *term)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003514{
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003515 VTermColor *fg, *bg;
3516 int fgval, bgval;
3517 int id;
3518
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003519 vim_memset(&term->tl_default_color.attrs, 0, sizeof(VTermScreenCellAttrs));
3520 term->tl_default_color.width = 1;
3521 fg = &term->tl_default_color.fg;
3522 bg = &term->tl_default_color.bg;
3523
3524 /* Vterm uses a default black background. Set it to white when
3525 * 'background' is "light". */
3526 if (*p_bg == 'l')
3527 {
3528 fgval = 0;
3529 bgval = 255;
3530 }
3531 else
3532 {
3533 fgval = 255;
3534 bgval = 0;
3535 }
3536 fg->red = fg->green = fg->blue = fgval;
3537 bg->red = bg->green = bg->blue = bgval;
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003538 fg->ansi_index = bg->ansi_index = VTERM_ANSI_INDEX_DEFAULT;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003539
3540 /* The "Terminal" highlight group overrules the defaults. */
3541 id = syn_name2id((char_u *)"Terminal");
3542
Bram Moolenaar46359e12017-11-29 22:33:38 +01003543 /* Use the actual color for the GUI and when 'termguicolors' is set. */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003544#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3545 if (0
3546# ifdef FEAT_GUI
3547 || gui.in_use
3548# endif
3549# ifdef FEAT_TERMGUICOLORS
3550 || p_tgc
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003551# ifdef FEAT_VTP
3552 /* Finally get INVALCOLOR on this execution path */
3553 || (!p_tgc && t_colors >= 256)
3554# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003555# endif
3556 )
3557 {
3558 guicolor_T fg_rgb = INVALCOLOR;
3559 guicolor_T bg_rgb = INVALCOLOR;
3560
3561 if (id != 0)
3562 syn_id2colors(id, &fg_rgb, &bg_rgb);
3563
3564# ifdef FEAT_GUI
3565 if (gui.in_use)
3566 {
3567 if (fg_rgb == INVALCOLOR)
3568 fg_rgb = gui.norm_pixel;
3569 if (bg_rgb == INVALCOLOR)
3570 bg_rgb = gui.back_pixel;
3571 }
3572# ifdef FEAT_TERMGUICOLORS
3573 else
3574# endif
3575# endif
3576# ifdef FEAT_TERMGUICOLORS
3577 {
3578 if (fg_rgb == INVALCOLOR)
3579 fg_rgb = cterm_normal_fg_gui_color;
3580 if (bg_rgb == INVALCOLOR)
3581 bg_rgb = cterm_normal_bg_gui_color;
3582 }
3583# endif
3584 if (fg_rgb != INVALCOLOR)
3585 {
3586 long_u rgb = GUI_MCH_GET_RGB(fg_rgb);
3587
3588 fg->red = (unsigned)(rgb >> 16);
3589 fg->green = (unsigned)(rgb >> 8) & 255;
3590 fg->blue = (unsigned)rgb & 255;
3591 }
3592 if (bg_rgb != INVALCOLOR)
3593 {
3594 long_u rgb = GUI_MCH_GET_RGB(bg_rgb);
3595
3596 bg->red = (unsigned)(rgb >> 16);
3597 bg->green = (unsigned)(rgb >> 8) & 255;
3598 bg->blue = (unsigned)rgb & 255;
3599 }
3600 }
3601 else
3602#endif
3603 if (id != 0 && t_colors >= 16)
3604 {
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003605 if (term_default_cterm_fg >= 0)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003606 cterm_color2vterm(term_default_cterm_fg, fg);
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01003607 if (term_default_cterm_bg >= 0)
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003608 cterm_color2vterm(term_default_cterm_bg, bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003609 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003610 else
3611 {
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003612#if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003613 int tmp;
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003614#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003615
3616 /* In an MS-Windows console we know the normal colors. */
3617 if (cterm_normal_fg_color > 0)
3618 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003619 cterm_color2vterm(cterm_normal_fg_color - 1, fg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003620# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3621# ifdef VIMDLL
3622 if (!gui.in_use)
3623# endif
3624 {
3625 tmp = fg->red;
3626 fg->red = fg->blue;
3627 fg->blue = tmp;
3628 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003629# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003630 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003631# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003632 else
3633 term_get_fg_color(&fg->red, &fg->green, &fg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003634# endif
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003635
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003636 if (cterm_normal_bg_color > 0)
3637 {
Bram Moolenaarc5cd8852018-05-01 15:47:38 +02003638 cterm_color2vterm(cterm_normal_bg_color - 1, bg);
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003639# if defined(MSWIN) && (!defined(FEAT_GUI_MSWIN) || defined(VIMDLL))
3640# ifdef VIMDLL
3641 if (!gui.in_use)
3642# endif
3643 {
3644 tmp = fg->red;
3645 fg->red = fg->blue;
3646 fg->blue = tmp;
3647 }
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003648# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003649 }
Bram Moolenaar9377df32017-10-15 13:22:01 +02003650# ifdef FEAT_TERMRESPONSE
Bram Moolenaar65e4c4f2017-10-14 23:24:25 +02003651 else
3652 term_get_bg_color(&bg->red, &bg->green, &bg->blue);
Bram Moolenaar9377df32017-10-15 13:22:01 +02003653# endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003654 }
Bram Moolenaar52acb112018-03-18 19:20:22 +01003655}
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02003656
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003657#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
3658/*
3659 * Set the 16 ANSI colors from array of RGB values
3660 */
3661 static void
3662set_vterm_palette(VTerm *vterm, long_u *rgb)
3663{
3664 int index = 0;
3665 VTermState *state = vterm_obtain_state(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01003666
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003667 for (; index < 16; index++)
3668 {
3669 VTermColor color;
Bram Moolenaaref8c83c2019-04-11 11:40:13 +02003670
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003671 color.red = (unsigned)(rgb[index] >> 16);
3672 color.green = (unsigned)(rgb[index] >> 8) & 255;
3673 color.blue = (unsigned)rgb[index] & 255;
3674 vterm_state_set_palette_color(state, index, &color);
3675 }
3676}
3677
3678/*
3679 * Set the ANSI color palette from a list of colors
3680 */
3681 static int
3682set_ansi_colors_list(VTerm *vterm, list_T *list)
3683{
3684 int n = 0;
3685 long_u rgb[16];
3686 listitem_T *li = list->lv_first;
3687
3688 for (; li != NULL && n < 16; li = li->li_next, n++)
3689 {
3690 char_u *color_name;
3691 guicolor_T guicolor;
3692
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003693 color_name = tv_get_string_chk(&li->li_tv);
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003694 if (color_name == NULL)
3695 return FAIL;
3696
3697 guicolor = GUI_GET_COLOR(color_name);
3698 if (guicolor == INVALCOLOR)
3699 return FAIL;
3700
3701 rgb[n] = GUI_MCH_GET_RGB(guicolor);
3702 }
3703
3704 if (n != 16 || li != NULL)
3705 return FAIL;
3706
3707 set_vterm_palette(vterm, rgb);
3708
3709 return OK;
3710}
3711
3712/*
3713 * Initialize the ANSI color palette from g:terminal_ansi_colors[0:15]
3714 */
3715 static void
3716init_vterm_ansi_colors(VTerm *vterm)
3717{
3718 dictitem_T *var = find_var((char_u *)"g:terminal_ansi_colors", NULL, TRUE);
3719
3720 if (var != NULL
3721 && (var->di_tv.v_type != VAR_LIST
3722 || var->di_tv.vval.v_list == NULL
3723 || set_ansi_colors_list(vterm, var->di_tv.vval.v_list) == FAIL))
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01003724 semsg(_(e_invarg2), "g:terminal_ansi_colors");
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02003725}
3726#endif
3727
Bram Moolenaar52acb112018-03-18 19:20:22 +01003728/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003729 * Handles a "drop" command from the job in the terminal.
3730 * "item" is the file name, "item->li_next" may have options.
3731 */
3732 static void
3733handle_drop_command(listitem_T *item)
3734{
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003735 char_u *fname = tv_get_string(&item->li_tv);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003736 listitem_T *opt_item = item->li_next;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003737 int bufnr;
3738 win_T *wp;
3739 tabpage_T *tp;
3740 exarg_T ea;
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003741 char_u *tofree = NULL;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003742
3743 bufnr = buflist_add(fname, BLN_LISTED | BLN_NOOPT);
3744 FOR_ALL_TAB_WINDOWS(tp, wp)
3745 {
3746 if (wp->w_buffer->b_fnum == bufnr)
3747 {
3748 /* buffer is in a window already, go there */
3749 goto_tabpage_win(tp, wp);
3750 return;
3751 }
3752 }
3753
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003754 vim_memset(&ea, 0, sizeof(ea));
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003755
3756 if (opt_item != NULL && opt_item->li_tv.v_type == VAR_DICT
3757 && opt_item->li_tv.vval.v_dict != NULL)
3758 {
3759 dict_T *dict = opt_item->li_tv.vval.v_dict;
3760 char_u *p;
3761
Bram Moolenaar8f667172018-12-14 15:38:31 +01003762 p = dict_get_string(dict, (char_u *)"ff", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003763 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01003764 p = dict_get_string(dict, (char_u *)"fileformat", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003765 if (p != NULL)
3766 {
3767 if (check_ff_value(p) == FAIL)
3768 ch_log(NULL, "Invalid ff argument to drop: %s", p);
3769 else
3770 ea.force_ff = *p;
3771 }
Bram Moolenaar8f667172018-12-14 15:38:31 +01003772 p = dict_get_string(dict, (char_u *)"enc", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003773 if (p == NULL)
Bram Moolenaar8f667172018-12-14 15:38:31 +01003774 p = dict_get_string(dict, (char_u *)"encoding", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003775 if (p != NULL)
3776 {
Bram Moolenaar51e14382019-05-25 20:21:28 +02003777 ea.cmd = alloc(STRLEN(p) + 12);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003778 if (ea.cmd != NULL)
3779 {
3780 sprintf((char *)ea.cmd, "sbuf ++enc=%s", p);
3781 ea.force_enc = 11;
3782 tofree = ea.cmd;
3783 }
3784 }
3785
Bram Moolenaar8f667172018-12-14 15:38:31 +01003786 p = dict_get_string(dict, (char_u *)"bad", FALSE);
Bram Moolenaar333b80a2018-04-04 22:57:29 +02003787 if (p != NULL)
3788 get_bad_opt(p, &ea);
3789
3790 if (dict_find(dict, (char_u *)"bin", -1) != NULL)
3791 ea.force_bin = FORCE_BIN;
3792 if (dict_find(dict, (char_u *)"binary", -1) != NULL)
3793 ea.force_bin = FORCE_BIN;
3794 if (dict_find(dict, (char_u *)"nobin", -1) != NULL)
3795 ea.force_bin = FORCE_NOBIN;
3796 if (dict_find(dict, (char_u *)"nobinary", -1) != NULL)
3797 ea.force_bin = FORCE_NOBIN;
3798 }
3799
3800 /* open in new window, like ":split fname" */
3801 if (ea.cmd == NULL)
3802 ea.cmd = (char_u *)"split";
3803 ea.arg = fname;
3804 ea.cmdidx = CMD_split;
3805 ex_splitview(&ea);
3806
3807 vim_free(tofree);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003808}
3809
3810/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02003811 * Return TRUE if "func" starts with "pat" and "pat" isn't empty.
3812 */
3813 static int
3814is_permitted_term_api(char_u *func, char_u *pat)
3815{
3816 return pat != NULL && *pat != NUL && STRNICMP(func, pat, STRLEN(pat)) == 0;
3817}
3818
3819/*
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003820 * Handles a function call from the job running in a terminal.
3821 * "item" is the function name, "item->li_next" has the arguments.
3822 */
3823 static void
3824handle_call_command(term_T *term, channel_T *channel, listitem_T *item)
3825{
3826 char_u *func;
3827 typval_T argvars[2];
3828 typval_T rettv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003829 funcexe_T funcexe;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003830
3831 if (item->li_next == NULL)
3832 {
3833 ch_log(channel, "Missing function arguments for call");
3834 return;
3835 }
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003836 func = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003837
Bram Moolenaard2842ea2019-09-26 23:08:54 +02003838 if (!is_permitted_term_api(func, term->tl_api))
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003839 {
Bram Moolenaard2842ea2019-09-26 23:08:54 +02003840 ch_log(channel, "Unpermitted function: %s", func);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003841 return;
3842 }
3843
3844 argvars[0].v_type = VAR_NUMBER;
3845 argvars[0].vval.v_number = term->tl_buffer->b_fnum;
3846 argvars[1] = item->li_next->li_tv;
Bram Moolenaarc6538bc2019-08-03 18:17:11 +02003847 vim_memset(&funcexe, 0, sizeof(funcexe));
3848 funcexe.firstline = 1L;
3849 funcexe.lastline = 1L;
3850 funcexe.evaluate = TRUE;
3851 if (call_func(func, -1, &rettv, 2, argvars, &funcexe) == OK)
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003852 {
3853 clear_tv(&rettv);
3854 ch_log(channel, "Function %s called", func);
3855 }
3856 else
3857 ch_log(channel, "Calling function %s failed", func);
3858}
3859
3860/*
3861 * Called by libvterm when it cannot recognize an OSC sequence.
3862 * We recognize a terminal API command.
3863 */
3864 static int
3865parse_osc(const char *command, size_t cmdlen, void *user)
3866{
3867 term_T *term = (term_T *)user;
3868 js_read_T reader;
3869 typval_T tv;
3870 channel_T *channel = term->tl_job == NULL ? NULL
3871 : term->tl_job->jv_channel;
3872
3873 /* We recognize only OSC 5 1 ; {command} */
3874 if (cmdlen < 3 || STRNCMP(command, "51;", 3) != 0)
3875 return 0; /* not handled */
3876
Bram Moolenaar878c96d2018-04-04 23:00:06 +02003877 reader.js_buf = vim_strnsave((char_u *)command + 3, (int)(cmdlen - 3));
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003878 if (reader.js_buf == NULL)
3879 return 1;
3880 reader.js_fill = NULL;
3881 reader.js_used = 0;
3882 if (json_decode(&reader, &tv, 0) == OK
3883 && tv.v_type == VAR_LIST
3884 && tv.vval.v_list != NULL)
3885 {
3886 listitem_T *item = tv.vval.v_list->lv_first;
3887
3888 if (item == NULL)
3889 ch_log(channel, "Missing command");
3890 else
3891 {
Bram Moolenaard155d7a2018-12-21 16:04:21 +01003892 char_u *cmd = tv_get_string(&item->li_tv);
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003893
Bram Moolenaara997b452018-04-17 23:24:06 +02003894 /* Make sure an invoked command doesn't delete the buffer (and the
3895 * terminal) under our fingers. */
3896 ++term->tl_buffer->b_locked;
3897
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003898 item = item->li_next;
3899 if (item == NULL)
3900 ch_log(channel, "Missing argument for %s", cmd);
3901 else if (STRCMP(cmd, "drop") == 0)
3902 handle_drop_command(item);
3903 else if (STRCMP(cmd, "call") == 0)
3904 handle_call_command(term, channel, item);
3905 else
3906 ch_log(channel, "Invalid command received: %s", cmd);
Bram Moolenaara997b452018-04-17 23:24:06 +02003907 --term->tl_buffer->b_locked;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003908 }
3909 }
3910 else
3911 ch_log(channel, "Invalid JSON received");
3912
3913 vim_free(reader.js_buf);
3914 clear_tv(&tv);
3915 return 1;
3916}
3917
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02003918/*
3919 * Called by libvterm when it cannot recognize a CSI sequence.
3920 * We recognize the window position report.
3921 */
3922 static int
3923parse_csi(
3924 const char *leader UNUSED,
3925 const long args[],
3926 int argcount,
3927 const char *intermed UNUSED,
3928 char command,
3929 void *user)
3930{
3931 term_T *term = (term_T *)user;
3932 char buf[100];
3933 int len;
3934 int x = 0;
3935 int y = 0;
3936 win_T *wp;
3937
3938 // We recognize only CSI 13 t
3939 if (command != 't' || argcount != 1 || args[0] != 13)
3940 return 0; // not handled
3941
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003942 // When getting the window position is not possible or it fails it results
3943 // in zero/zero.
Bram Moolenaar16c34c32019-04-06 22:01:24 +02003944#if defined(FEAT_GUI) \
3945 || (defined(HAVE_TGETENT) && defined(FEAT_TERMRESPONSE)) \
3946 || defined(MSWIN)
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02003947 (void)ui_get_winpos(&x, &y, (varnumber_T)100);
Bram Moolenaar6bc93052019-04-06 20:00:19 +02003948#endif
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02003949
3950 FOR_ALL_WINDOWS(wp)
3951 if (wp->w_buffer == term->tl_buffer)
3952 break;
3953 if (wp != NULL)
3954 {
3955#ifdef FEAT_GUI
3956 if (gui.in_use)
3957 {
3958 x += wp->w_wincol * gui.char_width;
3959 y += W_WINROW(wp) * gui.char_height;
3960 }
3961 else
3962#endif
3963 {
3964 // We roughly estimate the position of the terminal window inside
Bram Moolenaarafde13b2019-04-28 19:46:49 +02003965 // the Vim window by assuming a 10 x 7 character cell.
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02003966 x += wp->w_wincol * 7;
3967 y += W_WINROW(wp) * 10;
3968 }
3969 }
3970
3971 len = vim_snprintf(buf, 100, "\x1b[3;%d;%dt", x, y);
3972 channel_send(term->tl_job->jv_channel, get_tty_part(term),
3973 (char_u *)buf, len, NULL);
3974 return 1;
3975}
3976
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003977static VTermParserCallbacks parser_fallbacks = {
Bram Moolenaarfa1e90c2019-04-06 17:47:40 +02003978 NULL, // text
3979 NULL, // control
3980 NULL, // escape
3981 parse_csi, // csi
3982 parse_osc, // osc
3983 NULL, // dcs
3984 NULL // resize
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02003985};
3986
3987/*
Bram Moolenaar756ef112018-04-10 12:04:27 +02003988 * Use Vim's allocation functions for vterm so profiling works.
3989 */
3990 static void *
3991vterm_malloc(size_t size, void *data UNUSED)
3992{
Bram Moolenaar18a4ba22019-05-24 19:39:03 +02003993 return alloc_clear(size);
Bram Moolenaar756ef112018-04-10 12:04:27 +02003994}
3995
3996 static void
3997vterm_memfree(void *ptr, void *data UNUSED)
3998{
3999 vim_free(ptr);
4000}
4001
4002static VTermAllocatorFunctions vterm_allocator = {
4003 &vterm_malloc,
4004 &vterm_memfree
4005};
4006
4007/*
Bram Moolenaar52acb112018-03-18 19:20:22 +01004008 * Create a new vterm and initialize it.
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004009 * Return FAIL when out of memory.
Bram Moolenaar52acb112018-03-18 19:20:22 +01004010 */
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004011 static int
Bram Moolenaar52acb112018-03-18 19:20:22 +01004012create_vterm(term_T *term, int rows, int cols)
4013{
4014 VTerm *vterm;
4015 VTermScreen *screen;
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004016 VTermState *state;
Bram Moolenaar52acb112018-03-18 19:20:22 +01004017 VTermValue value;
4018
Bram Moolenaar756ef112018-04-10 12:04:27 +02004019 vterm = vterm_new_with_allocator(rows, cols, &vterm_allocator, NULL);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004020 term->tl_vterm = vterm;
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004021 if (vterm == NULL)
4022 return FAIL;
4023
4024 // Allocate screen and state here, so we can bail out if that fails.
4025 state = vterm_obtain_state(vterm);
Bram Moolenaar52acb112018-03-18 19:20:22 +01004026 screen = vterm_obtain_screen(vterm);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004027 if (state == NULL || screen == NULL)
4028 {
4029 vterm_free(vterm);
4030 return FAIL;
4031 }
4032
Bram Moolenaar52acb112018-03-18 19:20:22 +01004033 vterm_screen_set_callbacks(screen, &screen_callbacks, term);
4034 /* TODO: depends on 'encoding'. */
4035 vterm_set_utf8(vterm, 1);
4036
4037 init_default_colors(term);
4038
4039 vterm_state_set_default_colors(
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004040 state,
Bram Moolenaar52acb112018-03-18 19:20:22 +01004041 &term->tl_default_color.fg,
4042 &term->tl_default_color.bg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004043
Bram Moolenaar9e587872019-05-13 20:27:23 +02004044 if (t_colors < 16)
4045 // Less than 16 colors: assume that bold means using a bright color for
4046 // the foreground color.
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02004047 vterm_state_set_bold_highbright(vterm_obtain_state(vterm), 1);
4048
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004049 /* Required to initialize most things. */
4050 vterm_screen_reset(screen, 1 /* hard */);
4051
4052 /* Allow using alternate screen. */
4053 vterm_screen_enable_altscreen(screen, 1);
4054
4055 /* For unix do not use a blinking cursor. In an xterm this causes the
4056 * cursor to blink if it's blinking in the xterm.
4057 * For Windows we respect the system wide setting. */
Bram Moolenaar4f974752019-02-17 17:44:42 +01004058#ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004059 if (GetCaretBlinkTime() == INFINITE)
4060 value.boolean = 0;
4061 else
4062 value.boolean = 1;
4063#else
4064 value.boolean = 0;
4065#endif
Bram Moolenaar8fbaeb12018-03-25 18:20:17 +02004066 vterm_state_set_termprop(state, VTERM_PROP_CURSORBLINK, &value);
4067 vterm_state_set_unrecognised_fallbacks(state, &parser_fallbacks, term);
Bram Moolenaarcd929f72018-12-24 21:38:45 +01004068
4069 return OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004070}
4071
4072/*
4073 * Return the text to show for the buffer name and status.
4074 */
4075 char_u *
4076term_get_status_text(term_T *term)
4077{
4078 if (term->tl_status_text == NULL)
4079 {
4080 char_u *txt;
4081 size_t len;
4082
4083 if (term->tl_normal_mode)
4084 {
4085 if (term_job_running(term))
4086 txt = (char_u *)_("Terminal");
4087 else
4088 txt = (char_u *)_("Terminal-finished");
4089 }
4090 else if (term->tl_title != NULL)
4091 txt = term->tl_title;
4092 else if (term_none_open(term))
4093 txt = (char_u *)_("active");
4094 else if (term_job_running(term))
4095 txt = (char_u *)_("running");
4096 else
4097 txt = (char_u *)_("finished");
4098 len = 9 + STRLEN(term->tl_buffer->b_fname) + STRLEN(txt);
Bram Moolenaar51e14382019-05-25 20:21:28 +02004099 term->tl_status_text = alloc(len);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004100 if (term->tl_status_text != NULL)
4101 vim_snprintf((char *)term->tl_status_text, len, "%s [%s]",
4102 term->tl_buffer->b_fname, txt);
4103 }
4104 return term->tl_status_text;
4105}
4106
4107/*
4108 * Mark references in jobs of terminals.
4109 */
4110 int
4111set_ref_in_term(int copyID)
4112{
4113 int abort = FALSE;
4114 term_T *term;
4115 typval_T tv;
4116
Bram Moolenaar75a1a942019-06-20 03:45:36 +02004117 for (term = first_term; !abort && term != NULL; term = term->tl_next)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004118 if (term->tl_job != NULL)
4119 {
4120 tv.v_type = VAR_JOB;
4121 tv.vval.v_job = term->tl_job;
4122 abort = abort || set_ref_in_item(&tv, copyID, NULL, NULL);
4123 }
4124 return abort;
4125}
4126
4127/*
Bram Moolenaara7c54cf2017-12-01 21:07:20 +01004128 * Cache "Terminal" highlight group colors.
4129 */
4130 void
4131set_terminal_default_colors(int cterm_fg, int cterm_bg)
4132{
4133 term_default_cterm_fg = cterm_fg - 1;
4134 term_default_cterm_bg = cterm_bg - 1;
4135}
4136
4137/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004138 * Get the buffer from the first argument in "argvars".
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004139 * Returns NULL when the buffer is not for a terminal window and logs a message
4140 * with "where".
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004141 */
4142 static buf_T *
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004143term_get_buf(typval_T *argvars, char *where)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004144{
4145 buf_T *buf;
4146
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004147 (void)tv_get_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004148 ++emsg_off;
Bram Moolenaarf2d79fa2019-01-03 22:19:27 +01004149 buf = tv_get_buf(&argvars[0], FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004150 --emsg_off;
4151 if (buf == NULL || buf->b_term == NULL)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004152 {
4153 ch_log(NULL, "%s: invalid buffer argument", where);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004154 return NULL;
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004155 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02004156 return buf;
4157}
4158
Bram Moolenaard96ff162018-02-18 22:13:29 +01004159 static int
4160same_color(VTermColor *a, VTermColor *b)
4161{
4162 return a->red == b->red
4163 && a->green == b->green
4164 && a->blue == b->blue
4165 && a->ansi_index == b->ansi_index;
4166}
4167
4168 static void
4169dump_term_color(FILE *fd, VTermColor *color)
4170{
4171 fprintf(fd, "%02x%02x%02x%d",
4172 (int)color->red, (int)color->green, (int)color->blue,
4173 (int)color->ansi_index);
4174}
4175
4176/*
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004177 * "term_dumpwrite(buf, filename, options)" function
Bram Moolenaard96ff162018-02-18 22:13:29 +01004178 *
4179 * Each screen cell in full is:
4180 * |{characters}+{attributes}#{fg-color}{color-idx}#{bg-color}{color-idx}
4181 * {characters} is a space for an empty cell
4182 * For a double-width character "+" is changed to "*" and the next cell is
4183 * skipped.
4184 * {attributes} is the decimal value of HL_BOLD + HL_UNDERLINE, etc.
4185 * when "&" use the same as the previous cell.
4186 * {fg-color} is hex RGB, when "&" use the same as the previous cell.
4187 * {bg-color} is hex RGB, when "&" use the same as the previous cell.
4188 * {color-idx} is a number from 0 to 255
4189 *
4190 * Screen cell with same width, attributes and color as the previous one:
4191 * |{characters}
4192 *
4193 * To use the color of the previous cell, use "&" instead of {color}-{idx}.
4194 *
4195 * Repeating the previous screen cell:
4196 * @{count}
4197 */
4198 void
4199f_term_dumpwrite(typval_T *argvars, typval_T *rettv UNUSED)
4200{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01004201 buf_T *buf = term_get_buf(argvars, "term_dumpwrite()");
Bram Moolenaard96ff162018-02-18 22:13:29 +01004202 term_T *term;
4203 char_u *fname;
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004204 int max_height = 0;
4205 int max_width = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004206 stat_T st;
4207 FILE *fd;
4208 VTermPos pos;
4209 VTermScreen *screen;
4210 VTermScreenCell prev_cell;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004211 VTermState *state;
4212 VTermPos cursor_pos;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004213
4214 if (check_restricted() || check_secure())
4215 return;
4216 if (buf == NULL)
4217 return;
4218 term = buf->b_term;
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004219 if (term->tl_vterm == NULL)
4220 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004221 emsg(_("E958: Job already finished"));
Bram Moolenaara5c48c22018-09-09 19:56:07 +02004222 return;
4223 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004224
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004225 if (argvars[2].v_type != VAR_UNKNOWN)
4226 {
4227 dict_T *d;
4228
4229 if (argvars[2].v_type != VAR_DICT)
4230 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004231 emsg(_(e_dictreq));
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004232 return;
4233 }
4234 d = argvars[2].vval.v_dict;
4235 if (d != NULL)
4236 {
Bram Moolenaar8f667172018-12-14 15:38:31 +01004237 max_height = dict_get_number(d, (char_u *)"rows");
4238 max_width = dict_get_number(d, (char_u *)"columns");
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004239 }
4240 }
4241
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004242 fname = tv_get_string_chk(&argvars[1]);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004243 if (fname == NULL)
4244 return;
4245 if (mch_stat((char *)fname, &st) >= 0)
4246 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004247 semsg(_("E953: File exists: %s"), fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004248 return;
4249 }
4250
Bram Moolenaard96ff162018-02-18 22:13:29 +01004251 if (*fname == NUL || (fd = mch_fopen((char *)fname, WRITEBIN)) == NULL)
4252 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004253 semsg(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004254 return;
4255 }
4256
4257 vim_memset(&prev_cell, 0, sizeof(prev_cell));
4258
4259 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004260 state = vterm_obtain_state(term->tl_vterm);
4261 vterm_state_get_cursorpos(state, &cursor_pos);
4262
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004263 for (pos.row = 0; (max_height == 0 || pos.row < max_height)
4264 && pos.row < term->tl_rows; ++pos.row)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004265 {
4266 int repeat = 0;
4267
Bram Moolenaar6bb2cdf2018-02-24 19:53:53 +01004268 for (pos.col = 0; (max_width == 0 || pos.col < max_width)
4269 && pos.col < term->tl_cols; ++pos.col)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004270 {
4271 VTermScreenCell cell;
4272 int same_attr;
4273 int same_chars = TRUE;
4274 int i;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004275 int is_cursor_pos = (pos.col == cursor_pos.col
4276 && pos.row == cursor_pos.row);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004277
4278 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
4279 vim_memset(&cell, 0, sizeof(cell));
4280
4281 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
4282 {
Bram Moolenaar47015b82018-03-23 22:10:34 +01004283 int c = cell.chars[i];
4284 int pc = prev_cell.chars[i];
4285
4286 /* For the first character NUL is the same as space. */
4287 if (i == 0)
4288 {
4289 c = (c == NUL) ? ' ' : c;
4290 pc = (pc == NUL) ? ' ' : pc;
4291 }
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004292 if (c != pc)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004293 same_chars = FALSE;
Bram Moolenaar98fc8d72018-08-24 21:30:28 +02004294 if (c == NUL || pc == NUL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004295 break;
4296 }
4297 same_attr = vtermAttr2hl(cell.attrs)
4298 == vtermAttr2hl(prev_cell.attrs)
4299 && same_color(&cell.fg, &prev_cell.fg)
4300 && same_color(&cell.bg, &prev_cell.bg);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004301 if (same_chars && cell.width == prev_cell.width && same_attr
4302 && !is_cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004303 {
4304 ++repeat;
4305 }
4306 else
4307 {
4308 if (repeat > 0)
4309 {
4310 fprintf(fd, "@%d", repeat);
4311 repeat = 0;
4312 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004313 fputs(is_cursor_pos ? ">" : "|", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004314
4315 if (cell.chars[0] == NUL)
4316 fputs(" ", fd);
4317 else
4318 {
4319 char_u charbuf[10];
4320 int len;
4321
4322 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL
4323 && cell.chars[i] != NUL; ++i)
4324 {
Bram Moolenaarf06b0b62018-03-29 17:22:24 +02004325 len = utf_char2bytes(cell.chars[i], charbuf);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004326 fwrite(charbuf, len, 1, fd);
4327 }
4328 }
4329
4330 /* When only the characters differ we don't write anything, the
4331 * following "|", "@" or NL will indicate using the same
4332 * attributes. */
4333 if (cell.width != prev_cell.width || !same_attr)
4334 {
4335 if (cell.width == 2)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004336 fputs("*", fd);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004337 else
4338 fputs("+", fd);
4339
4340 if (same_attr)
4341 {
4342 fputs("&", fd);
4343 }
4344 else
4345 {
4346 fprintf(fd, "%d", vtermAttr2hl(cell.attrs));
4347 if (same_color(&cell.fg, &prev_cell.fg))
4348 fputs("&", fd);
4349 else
4350 {
4351 fputs("#", fd);
4352 dump_term_color(fd, &cell.fg);
4353 }
4354 if (same_color(&cell.bg, &prev_cell.bg))
4355 fputs("&", fd);
4356 else
4357 {
4358 fputs("#", fd);
4359 dump_term_color(fd, &cell.bg);
4360 }
4361 }
4362 }
4363
4364 prev_cell = cell;
4365 }
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004366
4367 if (cell.width == 2)
4368 ++pos.col;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004369 }
4370 if (repeat > 0)
4371 fprintf(fd, "@%d", repeat);
4372 fputs("\n", fd);
4373 }
4374
4375 fclose(fd);
4376}
4377
4378/*
4379 * Called when a dump is corrupted. Put a breakpoint here when debugging.
4380 */
4381 static void
4382dump_is_corrupt(garray_T *gap)
4383{
4384 ga_concat(gap, (char_u *)"CORRUPT");
4385}
4386
4387 static void
4388append_cell(garray_T *gap, cellattr_T *cell)
4389{
4390 if (ga_grow(gap, 1) == OK)
4391 {
4392 *(((cellattr_T *)gap->ga_data) + gap->ga_len) = *cell;
4393 ++gap->ga_len;
4394 }
4395}
4396
4397/*
4398 * Read the dump file from "fd" and append lines to the current buffer.
4399 * Return the cell width of the longest line.
4400 */
4401 static int
Bram Moolenaar9271d052018-02-25 21:39:46 +01004402read_dump_file(FILE *fd, VTermPos *cursor_pos)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004403{
4404 int c;
4405 garray_T ga_text;
4406 garray_T ga_cell;
4407 char_u *prev_char = NULL;
4408 int attr = 0;
4409 cellattr_T cell;
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004410 cellattr_T empty_cell;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004411 term_T *term = curbuf->b_term;
4412 int max_cells = 0;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004413 int start_row = term->tl_scrollback.ga_len;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004414
4415 ga_init2(&ga_text, 1, 90);
4416 ga_init2(&ga_cell, sizeof(cellattr_T), 90);
4417 vim_memset(&cell, 0, sizeof(cell));
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004418 vim_memset(&empty_cell, 0, sizeof(empty_cell));
Bram Moolenaar9271d052018-02-25 21:39:46 +01004419 cursor_pos->row = -1;
4420 cursor_pos->col = -1;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004421
4422 c = fgetc(fd);
4423 for (;;)
4424 {
4425 if (c == EOF)
4426 break;
Bram Moolenaar0fd6be72018-10-23 21:42:59 +02004427 if (c == '\r')
4428 {
4429 // DOS line endings? Ignore.
4430 c = fgetc(fd);
4431 }
4432 else if (c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004433 {
4434 /* End of a line: append it to the buffer. */
4435 if (ga_text.ga_data == NULL)
4436 dump_is_corrupt(&ga_text);
4437 if (ga_grow(&term->tl_scrollback, 1) == OK)
4438 {
4439 sb_line_T *line = (sb_line_T *)term->tl_scrollback.ga_data
4440 + term->tl_scrollback.ga_len;
4441
4442 if (max_cells < ga_cell.ga_len)
4443 max_cells = ga_cell.ga_len;
4444 line->sb_cols = ga_cell.ga_len;
4445 line->sb_cells = ga_cell.ga_data;
4446 line->sb_fill_attr = term->tl_default_color;
4447 ++term->tl_scrollback.ga_len;
4448 ga_init(&ga_cell);
4449
4450 ga_append(&ga_text, NUL);
4451 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4452 ga_text.ga_len, FALSE);
4453 }
4454 else
4455 ga_clear(&ga_cell);
4456 ga_text.ga_len = 0;
4457
4458 c = fgetc(fd);
4459 }
Bram Moolenaar9271d052018-02-25 21:39:46 +01004460 else if (c == '|' || c == '>')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004461 {
4462 int prev_len = ga_text.ga_len;
4463
Bram Moolenaar9271d052018-02-25 21:39:46 +01004464 if (c == '>')
4465 {
4466 if (cursor_pos->row != -1)
4467 dump_is_corrupt(&ga_text); /* duplicate cursor */
4468 cursor_pos->row = term->tl_scrollback.ga_len - start_row;
4469 cursor_pos->col = ga_cell.ga_len;
4470 }
4471
Bram Moolenaard96ff162018-02-18 22:13:29 +01004472 /* normal character(s) followed by "+", "*", "|", "@" or NL */
4473 c = fgetc(fd);
4474 if (c != EOF)
4475 ga_append(&ga_text, c);
4476 for (;;)
4477 {
4478 c = fgetc(fd);
Bram Moolenaar9271d052018-02-25 21:39:46 +01004479 if (c == '+' || c == '*' || c == '|' || c == '>' || c == '@'
Bram Moolenaard96ff162018-02-18 22:13:29 +01004480 || c == EOF || c == '\n')
4481 break;
4482 ga_append(&ga_text, c);
4483 }
4484
4485 /* save the character for repeating it */
4486 vim_free(prev_char);
4487 if (ga_text.ga_data != NULL)
4488 prev_char = vim_strnsave(((char_u *)ga_text.ga_data) + prev_len,
4489 ga_text.ga_len - prev_len);
4490
Bram Moolenaar9271d052018-02-25 21:39:46 +01004491 if (c == '@' || c == '|' || c == '>' || c == '\n')
Bram Moolenaard96ff162018-02-18 22:13:29 +01004492 {
4493 /* use all attributes from previous cell */
4494 }
4495 else if (c == '+' || c == '*')
4496 {
4497 int is_bg;
4498
4499 cell.width = c == '+' ? 1 : 2;
4500
4501 c = fgetc(fd);
4502 if (c == '&')
4503 {
4504 /* use same attr as previous cell */
4505 c = fgetc(fd);
4506 }
4507 else if (isdigit(c))
4508 {
4509 /* get the decimal attribute */
4510 attr = 0;
4511 while (isdigit(c))
4512 {
4513 attr = attr * 10 + (c - '0');
4514 c = fgetc(fd);
4515 }
4516 hl2vtermAttr(attr, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004517
4518 /* is_bg == 0: fg, is_bg == 1: bg */
4519 for (is_bg = 0; is_bg <= 1; ++is_bg)
4520 {
4521 if (c == '&')
4522 {
4523 /* use same color as previous cell */
4524 c = fgetc(fd);
4525 }
4526 else if (c == '#')
4527 {
4528 int red, green, blue, index = 0;
4529
4530 c = fgetc(fd);
4531 red = hex2nr(c);
4532 c = fgetc(fd);
4533 red = (red << 4) + hex2nr(c);
4534 c = fgetc(fd);
4535 green = hex2nr(c);
4536 c = fgetc(fd);
4537 green = (green << 4) + hex2nr(c);
4538 c = fgetc(fd);
4539 blue = hex2nr(c);
4540 c = fgetc(fd);
4541 blue = (blue << 4) + hex2nr(c);
4542 c = fgetc(fd);
4543 if (!isdigit(c))
4544 dump_is_corrupt(&ga_text);
4545 while (isdigit(c))
4546 {
4547 index = index * 10 + (c - '0');
4548 c = fgetc(fd);
4549 }
4550
4551 if (is_bg)
4552 {
4553 cell.bg.red = red;
4554 cell.bg.green = green;
4555 cell.bg.blue = blue;
4556 cell.bg.ansi_index = index;
4557 }
4558 else
4559 {
4560 cell.fg.red = red;
4561 cell.fg.green = green;
4562 cell.fg.blue = blue;
4563 cell.fg.ansi_index = index;
4564 }
4565 }
4566 else
4567 dump_is_corrupt(&ga_text);
4568 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004569 }
4570 else
4571 dump_is_corrupt(&ga_text);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004572 }
4573 else
4574 dump_is_corrupt(&ga_text);
4575
4576 append_cell(&ga_cell, &cell);
Bram Moolenaar617d7ef2019-01-17 13:04:30 +01004577 if (cell.width == 2)
4578 append_cell(&ga_cell, &empty_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004579 }
4580 else if (c == '@')
4581 {
4582 if (prev_char == NULL)
4583 dump_is_corrupt(&ga_text);
4584 else
4585 {
4586 int count = 0;
4587
4588 /* repeat previous character, get the count */
4589 for (;;)
4590 {
4591 c = fgetc(fd);
4592 if (!isdigit(c))
4593 break;
4594 count = count * 10 + (c - '0');
4595 }
4596
4597 while (count-- > 0)
4598 {
4599 ga_concat(&ga_text, prev_char);
4600 append_cell(&ga_cell, &cell);
4601 }
4602 }
4603 }
4604 else
4605 {
4606 dump_is_corrupt(&ga_text);
4607 c = fgetc(fd);
4608 }
4609 }
4610
4611 if (ga_text.ga_len > 0)
4612 {
4613 /* trailing characters after last NL */
4614 dump_is_corrupt(&ga_text);
4615 ga_append(&ga_text, NUL);
4616 ml_append(curbuf->b_ml.ml_line_count, ga_text.ga_data,
4617 ga_text.ga_len, FALSE);
4618 }
4619
4620 ga_clear(&ga_text);
Bram Moolenaar86173482019-10-01 17:02:16 +02004621 ga_clear(&ga_cell);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004622 vim_free(prev_char);
4623
4624 return max_cells;
4625}
4626
4627/*
Bram Moolenaar4a696342018-04-05 18:45:26 +02004628 * Return an allocated string with at least "text_width" "=" characters and
4629 * "fname" inserted in the middle.
4630 */
4631 static char_u *
4632get_separator(int text_width, char_u *fname)
4633{
4634 int width = MAX(text_width, curwin->w_width);
4635 char_u *textline;
4636 int fname_size;
4637 char_u *p = fname;
4638 int i;
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004639 size_t off;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004640
Bram Moolenaard6b4f2d2018-04-10 18:26:27 +02004641 textline = alloc(width + (int)STRLEN(fname) + 1);
Bram Moolenaar4a696342018-04-05 18:45:26 +02004642 if (textline == NULL)
4643 return NULL;
4644
4645 fname_size = vim_strsize(fname);
4646 if (fname_size < width - 8)
4647 {
4648 /* enough room, don't use the full window width */
4649 width = MAX(text_width, fname_size + 8);
4650 }
4651 else if (fname_size > width - 8)
4652 {
4653 /* full name doesn't fit, use only the tail */
4654 p = gettail(fname);
4655 fname_size = vim_strsize(p);
4656 }
4657 /* skip characters until the name fits */
4658 while (fname_size > width - 8)
4659 {
4660 p += (*mb_ptr2len)(p);
4661 fname_size = vim_strsize(p);
4662 }
4663
4664 for (i = 0; i < (width - fname_size) / 2 - 1; ++i)
4665 textline[i] = '=';
4666 textline[i++] = ' ';
4667
4668 STRCPY(textline + i, p);
4669 off = STRLEN(textline);
4670 textline[off] = ' ';
4671 for (i = 1; i < (width - fname_size) / 2; ++i)
4672 textline[off + i] = '=';
4673 textline[off + i] = NUL;
4674
4675 return textline;
4676}
4677
4678/*
Bram Moolenaard96ff162018-02-18 22:13:29 +01004679 * Common for "term_dumpdiff()" and "term_dumpload()".
4680 */
4681 static void
4682term_load_dump(typval_T *argvars, typval_T *rettv, int do_diff)
4683{
4684 jobopt_T opt;
Bram Moolenaar87abab92019-06-03 21:14:59 +02004685 buf_T *buf = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004686 char_u buf1[NUMBUFLEN];
4687 char_u buf2[NUMBUFLEN];
4688 char_u *fname1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004689 char_u *fname2 = NULL;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004690 char_u *fname_tofree = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004691 FILE *fd1;
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004692 FILE *fd2 = NULL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004693 char_u *textline = NULL;
4694
4695 /* First open the files. If this fails bail out. */
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004696 fname1 = tv_get_string_buf_chk(&argvars[0], buf1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004697 if (do_diff)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01004698 fname2 = tv_get_string_buf_chk(&argvars[1], buf2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004699 if (fname1 == NULL || (do_diff && fname2 == NULL))
4700 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004701 emsg(_(e_invarg));
Bram Moolenaard96ff162018-02-18 22:13:29 +01004702 return;
4703 }
4704 fd1 = mch_fopen((char *)fname1, READBIN);
4705 if (fd1 == NULL)
4706 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004707 semsg(_(e_notread), fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004708 return;
4709 }
4710 if (do_diff)
4711 {
4712 fd2 = mch_fopen((char *)fname2, READBIN);
4713 if (fd2 == NULL)
4714 {
4715 fclose(fd1);
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01004716 semsg(_(e_notread), fname2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004717 return;
4718 }
4719 }
4720
4721 init_job_options(&opt);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004722 if (argvars[do_diff ? 2 : 1].v_type != VAR_UNKNOWN
4723 && get_job_options(&argvars[do_diff ? 2 : 1], &opt, 0,
4724 JO2_TERM_NAME + JO2_TERM_COLS + JO2_TERM_ROWS
4725 + JO2_VERTICAL + JO2_CURWIN + JO2_NORESTORE) == FAIL)
4726 goto theend;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004727
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004728 if (opt.jo_term_name == NULL)
4729 {
Bram Moolenaarb571c632018-03-21 22:27:59 +01004730 size_t len = STRLEN(fname1) + 12;
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004731
Bram Moolenaar51e14382019-05-25 20:21:28 +02004732 fname_tofree = alloc(len);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004733 if (fname_tofree != NULL)
4734 {
4735 vim_snprintf((char *)fname_tofree, len, "dump diff %s", fname1);
4736 opt.jo_term_name = fname_tofree;
4737 }
4738 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004739
Bram Moolenaar87abab92019-06-03 21:14:59 +02004740 if (opt.jo_bufnr_buf != NULL)
4741 {
4742 win_T *wp = buf_jump_open_win(opt.jo_bufnr_buf);
4743
4744 // With "bufnr" argument: enter the window with this buffer and make it
4745 // empty.
4746 if (wp == NULL)
4747 semsg(_(e_invarg2), "bufnr");
4748 else
4749 {
4750 buf = curbuf;
4751 while (!(curbuf->b_ml.ml_flags & ML_EMPTY))
4752 ml_delete((linenr_T)1, FALSE);
Bram Moolenaar86173482019-10-01 17:02:16 +02004753 free_scrollback(curbuf->b_term);
Bram Moolenaar87abab92019-06-03 21:14:59 +02004754 redraw_later(NOT_VALID);
4755 }
4756 }
4757 else
4758 // Create a new terminal window.
4759 buf = term_start(&argvars[0], NULL, &opt, TERM_START_NOJOB);
4760
Bram Moolenaard96ff162018-02-18 22:13:29 +01004761 if (buf != NULL && buf->b_term != NULL)
4762 {
4763 int i;
4764 linenr_T bot_lnum;
4765 linenr_T lnum;
4766 term_T *term = buf->b_term;
4767 int width;
4768 int width2;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004769 VTermPos cursor_pos1;
4770 VTermPos cursor_pos2;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004771
Bram Moolenaar52acb112018-03-18 19:20:22 +01004772 init_default_colors(term);
4773
Bram Moolenaard96ff162018-02-18 22:13:29 +01004774 rettv->vval.v_number = buf->b_fnum;
4775
4776 /* read the files, fill the buffer with the diff */
Bram Moolenaar9271d052018-02-25 21:39:46 +01004777 width = read_dump_file(fd1, &cursor_pos1);
4778
4779 /* position the cursor */
4780 if (cursor_pos1.row >= 0)
4781 {
4782 curwin->w_cursor.lnum = cursor_pos1.row + 1;
4783 coladvance(cursor_pos1.col);
4784 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004785
4786 /* Delete the empty line that was in the empty buffer. */
4787 ml_delete(1, FALSE);
4788
4789 /* For term_dumpload() we are done here. */
4790 if (!do_diff)
4791 goto theend;
4792
4793 term->tl_top_diff_rows = curbuf->b_ml.ml_line_count;
4794
Bram Moolenaar4a696342018-04-05 18:45:26 +02004795 textline = get_separator(width, fname1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004796 if (textline == NULL)
4797 goto theend;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004798 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
4799 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
4800 vim_free(textline);
4801
4802 textline = get_separator(width, fname2);
4803 if (textline == NULL)
4804 goto theend;
4805 if (add_empty_scrollback(term, &term->tl_default_color, 0) == OK)
4806 ml_append(curbuf->b_ml.ml_line_count, textline, 0, FALSE);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004807 textline[width] = NUL;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004808
4809 bot_lnum = curbuf->b_ml.ml_line_count;
Bram Moolenaar9271d052018-02-25 21:39:46 +01004810 width2 = read_dump_file(fd2, &cursor_pos2);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004811 if (width2 > width)
4812 {
4813 vim_free(textline);
4814 textline = alloc(width2 + 1);
4815 if (textline == NULL)
4816 goto theend;
4817 width = width2;
4818 textline[width] = NUL;
4819 }
4820 term->tl_bot_diff_rows = curbuf->b_ml.ml_line_count - bot_lnum;
4821
4822 for (lnum = 1; lnum <= term->tl_top_diff_rows; ++lnum)
4823 {
4824 if (lnum + bot_lnum > curbuf->b_ml.ml_line_count)
4825 {
4826 /* bottom part has fewer rows, fill with "-" */
4827 for (i = 0; i < width; ++i)
4828 textline[i] = '-';
4829 }
4830 else
4831 {
4832 char_u *line1;
4833 char_u *line2;
4834 char_u *p1;
4835 char_u *p2;
4836 int col;
4837 sb_line_T *sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
4838 cellattr_T *cellattr1 = (sb_line + lnum - 1)->sb_cells;
4839 cellattr_T *cellattr2 = (sb_line + lnum + bot_lnum - 1)
4840 ->sb_cells;
4841
4842 /* Make a copy, getting the second line will invalidate it. */
4843 line1 = vim_strsave(ml_get(lnum));
4844 if (line1 == NULL)
4845 break;
4846 p1 = line1;
4847
4848 line2 = ml_get(lnum + bot_lnum);
4849 p2 = line2;
4850 for (col = 0; col < width && *p1 != NUL && *p2 != NUL; ++col)
4851 {
4852 int len1 = utfc_ptr2len(p1);
4853 int len2 = utfc_ptr2len(p2);
4854
4855 textline[col] = ' ';
4856 if (len1 != len2 || STRNCMP(p1, p2, len1) != 0)
Bram Moolenaar9271d052018-02-25 21:39:46 +01004857 /* text differs */
Bram Moolenaard96ff162018-02-18 22:13:29 +01004858 textline[col] = 'X';
Bram Moolenaar9271d052018-02-25 21:39:46 +01004859 else if (lnum == cursor_pos1.row + 1
4860 && col == cursor_pos1.col
4861 && (cursor_pos1.row != cursor_pos2.row
4862 || cursor_pos1.col != cursor_pos2.col))
4863 /* cursor in first but not in second */
4864 textline[col] = '>';
4865 else if (lnum == cursor_pos2.row + 1
4866 && col == cursor_pos2.col
4867 && (cursor_pos1.row != cursor_pos2.row
4868 || cursor_pos1.col != cursor_pos2.col))
4869 /* cursor in second but not in first */
4870 textline[col] = '<';
Bram Moolenaard96ff162018-02-18 22:13:29 +01004871 else if (cellattr1 != NULL && cellattr2 != NULL)
4872 {
4873 if ((cellattr1 + col)->width
4874 != (cellattr2 + col)->width)
4875 textline[col] = 'w';
4876 else if (!same_color(&(cellattr1 + col)->fg,
4877 &(cellattr2 + col)->fg))
4878 textline[col] = 'f';
4879 else if (!same_color(&(cellattr1 + col)->bg,
4880 &(cellattr2 + col)->bg))
4881 textline[col] = 'b';
4882 else if (vtermAttr2hl((cellattr1 + col)->attrs)
4883 != vtermAttr2hl(((cellattr2 + col)->attrs)))
4884 textline[col] = 'a';
4885 }
4886 p1 += len1;
4887 p2 += len2;
4888 /* TODO: handle different width */
4889 }
Bram Moolenaard96ff162018-02-18 22:13:29 +01004890
4891 while (col < width)
4892 {
4893 if (*p1 == NUL && *p2 == NUL)
4894 textline[col] = '?';
4895 else if (*p1 == NUL)
4896 {
4897 textline[col] = '+';
4898 p2 += utfc_ptr2len(p2);
4899 }
4900 else
4901 {
4902 textline[col] = '-';
4903 p1 += utfc_ptr2len(p1);
4904 }
4905 ++col;
4906 }
Bram Moolenaar81aa0f52019-02-14 23:23:19 +01004907
4908 vim_free(line1);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004909 }
4910 if (add_empty_scrollback(term, &term->tl_default_color,
4911 term->tl_top_diff_rows) == OK)
4912 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
4913 ++bot_lnum;
4914 }
4915
4916 while (lnum + bot_lnum <= curbuf->b_ml.ml_line_count)
4917 {
4918 /* bottom part has more rows, fill with "+" */
4919 for (i = 0; i < width; ++i)
4920 textline[i] = '+';
4921 if (add_empty_scrollback(term, &term->tl_default_color,
4922 term->tl_top_diff_rows) == OK)
4923 ml_append(term->tl_top_diff_rows + lnum, textline, 0, FALSE);
4924 ++lnum;
4925 ++bot_lnum;
4926 }
4927
4928 term->tl_cols = width;
Bram Moolenaar4a696342018-04-05 18:45:26 +02004929
4930 /* looks better without wrapping */
4931 curwin->w_p_wrap = 0;
Bram Moolenaard96ff162018-02-18 22:13:29 +01004932 }
4933
4934theend:
4935 vim_free(textline);
Bram Moolenaar5a3a49e2018-03-20 18:35:53 +01004936 vim_free(fname_tofree);
Bram Moolenaard96ff162018-02-18 22:13:29 +01004937 fclose(fd1);
Bram Moolenaar9c8816b2018-02-19 21:50:42 +01004938 if (fd2 != NULL)
Bram Moolenaard96ff162018-02-18 22:13:29 +01004939 fclose(fd2);
4940}
4941
4942/*
4943 * If the current buffer shows the output of term_dumpdiff(), swap the top and
4944 * bottom files.
4945 * Return FAIL when this is not possible.
4946 */
4947 int
4948term_swap_diff()
4949{
4950 term_T *term = curbuf->b_term;
4951 linenr_T line_count;
4952 linenr_T top_rows;
4953 linenr_T bot_rows;
4954 linenr_T bot_start;
4955 linenr_T lnum;
4956 char_u *p;
4957 sb_line_T *sb_line;
4958
4959 if (term == NULL
4960 || !term_is_finished(curbuf)
4961 || term->tl_top_diff_rows == 0
4962 || term->tl_scrollback.ga_len == 0)
4963 return FAIL;
4964
4965 line_count = curbuf->b_ml.ml_line_count;
4966 top_rows = term->tl_top_diff_rows;
4967 bot_rows = term->tl_bot_diff_rows;
4968 bot_start = line_count - bot_rows;
4969 sb_line = (sb_line_T *)term->tl_scrollback.ga_data;
4970
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01004971 // move lines from top to above the bottom part
Bram Moolenaard96ff162018-02-18 22:13:29 +01004972 for (lnum = 1; lnum <= top_rows; ++lnum)
4973 {
4974 p = vim_strsave(ml_get(1));
4975 if (p == NULL)
4976 return OK;
4977 ml_append(bot_start, p, 0, FALSE);
4978 ml_delete(1, FALSE);
4979 vim_free(p);
4980 }
4981
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01004982 // move lines from bottom to the top
Bram Moolenaard96ff162018-02-18 22:13:29 +01004983 for (lnum = 1; lnum <= bot_rows; ++lnum)
4984 {
4985 p = vim_strsave(ml_get(bot_start + lnum));
4986 if (p == NULL)
4987 return OK;
4988 ml_delete(bot_start + lnum, FALSE);
4989 ml_append(lnum - 1, p, 0, FALSE);
4990 vim_free(p);
4991 }
4992
Bram Moolenaarc3ef8962019-02-15 00:16:13 +01004993 // move top title to bottom
4994 p = vim_strsave(ml_get(bot_rows + 1));
4995 if (p == NULL)
4996 return OK;
4997 ml_append(line_count - top_rows - 1, p, 0, FALSE);
4998 ml_delete(bot_rows + 1, FALSE);
4999 vim_free(p);
5000
5001 // move bottom title to top
5002 p = vim_strsave(ml_get(line_count - top_rows));
5003 if (p == NULL)
5004 return OK;
5005 ml_delete(line_count - top_rows, FALSE);
5006 ml_append(bot_rows, p, 0, FALSE);
5007 vim_free(p);
5008
Bram Moolenaard96ff162018-02-18 22:13:29 +01005009 if (top_rows == bot_rows)
5010 {
5011 /* rows counts are equal, can swap cell properties */
5012 for (lnum = 0; lnum < top_rows; ++lnum)
5013 {
5014 sb_line_T temp;
5015
5016 temp = *(sb_line + lnum);
5017 *(sb_line + lnum) = *(sb_line + bot_start + lnum);
5018 *(sb_line + bot_start + lnum) = temp;
5019 }
5020 }
5021 else
5022 {
5023 size_t size = sizeof(sb_line_T) * term->tl_scrollback.ga_len;
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005024 sb_line_T *temp = alloc(size);
Bram Moolenaard96ff162018-02-18 22:13:29 +01005025
5026 /* need to copy cell properties into temp memory */
5027 if (temp != NULL)
5028 {
5029 mch_memmove(temp, term->tl_scrollback.ga_data, size);
5030 mch_memmove(term->tl_scrollback.ga_data,
5031 temp + bot_start,
5032 sizeof(sb_line_T) * bot_rows);
5033 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data + bot_rows,
5034 temp + top_rows,
5035 sizeof(sb_line_T) * (line_count - top_rows - bot_rows));
5036 mch_memmove((sb_line_T *)term->tl_scrollback.ga_data
5037 + line_count - top_rows,
5038 temp,
5039 sizeof(sb_line_T) * top_rows);
5040 vim_free(temp);
5041 }
5042 }
5043
5044 term->tl_top_diff_rows = bot_rows;
5045 term->tl_bot_diff_rows = top_rows;
5046
5047 update_screen(NOT_VALID);
5048 return OK;
5049}
5050
5051/*
5052 * "term_dumpdiff(filename, filename, options)" function
5053 */
5054 void
5055f_term_dumpdiff(typval_T *argvars, typval_T *rettv)
5056{
5057 term_load_dump(argvars, rettv, TRUE);
5058}
5059
5060/*
5061 * "term_dumpload(filename, options)" function
5062 */
5063 void
5064f_term_dumpload(typval_T *argvars, typval_T *rettv)
5065{
5066 term_load_dump(argvars, rettv, FALSE);
5067}
5068
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005069/*
5070 * "term_getaltscreen(buf)" function
5071 */
5072 void
5073f_term_getaltscreen(typval_T *argvars, typval_T *rettv)
5074{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005075 buf_T *buf = term_get_buf(argvars, "term_getaltscreen()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005076
5077 if (buf == NULL)
5078 return;
5079 rettv->vval.v_number = buf->b_term->tl_using_altscreen;
5080}
5081
5082/*
5083 * "term_getattr(attr, name)" function
5084 */
5085 void
5086f_term_getattr(typval_T *argvars, typval_T *rettv)
5087{
5088 int attr;
5089 size_t i;
5090 char_u *name;
5091
5092 static struct {
5093 char *name;
5094 int attr;
5095 } attrs[] = {
5096 {"bold", HL_BOLD},
5097 {"italic", HL_ITALIC},
5098 {"underline", HL_UNDERLINE},
5099 {"strike", HL_STRIKETHROUGH},
5100 {"reverse", HL_INVERSE},
5101 };
5102
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005103 attr = tv_get_number(&argvars[0]);
5104 name = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005105 if (name == NULL)
5106 return;
5107
Bram Moolenaar7ee80f72019-09-08 20:55:06 +02005108 if (attr > HL_ALL)
5109 attr = syn_attr2attr(attr);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005110 for (i = 0; i < sizeof(attrs)/sizeof(attrs[0]); ++i)
5111 if (STRCMP(name, attrs[i].name) == 0)
5112 {
5113 rettv->vval.v_number = (attr & attrs[i].attr) != 0 ? 1 : 0;
5114 break;
5115 }
5116}
5117
5118/*
5119 * "term_getcursor(buf)" function
5120 */
5121 void
5122f_term_getcursor(typval_T *argvars, typval_T *rettv)
5123{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005124 buf_T *buf = term_get_buf(argvars, "term_getcursor()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005125 term_T *term;
5126 list_T *l;
5127 dict_T *d;
5128
5129 if (rettv_list_alloc(rettv) == FAIL)
5130 return;
5131 if (buf == NULL)
5132 return;
5133 term = buf->b_term;
5134
5135 l = rettv->vval.v_list;
5136 list_append_number(l, term->tl_cursor_pos.row + 1);
5137 list_append_number(l, term->tl_cursor_pos.col + 1);
5138
5139 d = dict_alloc();
5140 if (d != NULL)
5141 {
Bram Moolenaare0be1672018-07-08 16:50:37 +02005142 dict_add_number(d, "visible", term->tl_cursor_visible);
5143 dict_add_number(d, "blink", blink_state_is_inverted()
5144 ? !term->tl_cursor_blink : term->tl_cursor_blink);
5145 dict_add_number(d, "shape", term->tl_cursor_shape);
5146 dict_add_string(d, "color", cursor_color_get(term->tl_cursor_color));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005147 list_append_dict(l, d);
5148 }
5149}
5150
5151/*
5152 * "term_getjob(buf)" function
5153 */
5154 void
5155f_term_getjob(typval_T *argvars, typval_T *rettv)
5156{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005157 buf_T *buf = term_get_buf(argvars, "term_getjob()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005158
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005159 if (buf == NULL)
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005160 {
5161 rettv->v_type = VAR_SPECIAL;
5162 rettv->vval.v_number = VVAL_NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005163 return;
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005164 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005165
Bram Moolenaar528ccfb2018-12-21 20:55:22 +01005166 rettv->v_type = VAR_JOB;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005167 rettv->vval.v_job = buf->b_term->tl_job;
5168 if (rettv->vval.v_job != NULL)
5169 ++rettv->vval.v_job->jv_refcount;
5170}
5171
5172 static int
5173get_row_number(typval_T *tv, term_T *term)
5174{
5175 if (tv->v_type == VAR_STRING
5176 && tv->vval.v_string != NULL
5177 && STRCMP(tv->vval.v_string, ".") == 0)
5178 return term->tl_cursor_pos.row;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005179 return (int)tv_get_number(tv) - 1;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005180}
5181
5182/*
5183 * "term_getline(buf, row)" function
5184 */
5185 void
5186f_term_getline(typval_T *argvars, typval_T *rettv)
5187{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005188 buf_T *buf = term_get_buf(argvars, "term_getline()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005189 term_T *term;
5190 int row;
5191
5192 rettv->v_type = VAR_STRING;
5193 if (buf == NULL)
5194 return;
5195 term = buf->b_term;
5196 row = get_row_number(&argvars[1], term);
5197
5198 if (term->tl_vterm == NULL)
5199 {
5200 linenr_T lnum = row + term->tl_scrollback_scrolled + 1;
5201
5202 /* vterm is finished, get the text from the buffer */
5203 if (lnum > 0 && lnum <= buf->b_ml.ml_line_count)
5204 rettv->vval.v_string = vim_strsave(ml_get_buf(buf, lnum, FALSE));
5205 }
5206 else
5207 {
5208 VTermScreen *screen = vterm_obtain_screen(term->tl_vterm);
5209 VTermRect rect;
5210 int len;
5211 char_u *p;
5212
5213 if (row < 0 || row >= term->tl_rows)
5214 return;
5215 len = term->tl_cols * MB_MAXBYTES + 1;
5216 p = alloc(len);
5217 if (p == NULL)
5218 return;
5219 rettv->vval.v_string = p;
5220
5221 rect.start_col = 0;
5222 rect.end_col = term->tl_cols;
5223 rect.start_row = row;
5224 rect.end_row = row + 1;
5225 p[vterm_screen_get_text(screen, (char *)p, len, rect)] = NUL;
5226 }
5227}
5228
5229/*
5230 * "term_getscrolled(buf)" function
5231 */
5232 void
5233f_term_getscrolled(typval_T *argvars, typval_T *rettv)
5234{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005235 buf_T *buf = term_get_buf(argvars, "term_getscrolled()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005236
5237 if (buf == NULL)
5238 return;
5239 rettv->vval.v_number = buf->b_term->tl_scrollback_scrolled;
5240}
5241
5242/*
5243 * "term_getsize(buf)" function
5244 */
5245 void
5246f_term_getsize(typval_T *argvars, typval_T *rettv)
5247{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005248 buf_T *buf = term_get_buf(argvars, "term_getsize()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005249 list_T *l;
5250
5251 if (rettv_list_alloc(rettv) == FAIL)
5252 return;
5253 if (buf == NULL)
5254 return;
5255
5256 l = rettv->vval.v_list;
5257 list_append_number(l, buf->b_term->tl_rows);
5258 list_append_number(l, buf->b_term->tl_cols);
5259}
5260
5261/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02005262 * "term_setsize(buf, rows, cols)" function
5263 */
5264 void
5265f_term_setsize(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5266{
5267 buf_T *buf = term_get_buf(argvars, "term_setsize()");
5268 term_T *term;
5269 varnumber_T rows, cols;
5270
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005271 if (buf == NULL)
5272 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005273 emsg(_("E955: Not a terminal buffer"));
Bram Moolenaar6e72cd02018-04-14 21:31:35 +02005274 return;
5275 }
5276 if (buf->b_term->tl_vterm == NULL)
Bram Moolenaara42d3632018-04-14 17:05:38 +02005277 return;
5278 term = buf->b_term;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005279 rows = tv_get_number(&argvars[1]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005280 rows = rows <= 0 ? term->tl_rows : rows;
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005281 cols = tv_get_number(&argvars[2]);
Bram Moolenaara42d3632018-04-14 17:05:38 +02005282 cols = cols <= 0 ? term->tl_cols : cols;
5283 vterm_set_size(term->tl_vterm, rows, cols);
5284 /* handle_resize() will resize the windows */
5285
5286 /* Get and remember the size we ended up with. Update the pty. */
5287 vterm_get_size(term->tl_vterm, &term->tl_rows, &term->tl_cols);
5288 term_report_winsize(term, term->tl_rows, term->tl_cols);
5289}
5290
5291/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005292 * "term_getstatus(buf)" function
5293 */
5294 void
5295f_term_getstatus(typval_T *argvars, typval_T *rettv)
5296{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005297 buf_T *buf = term_get_buf(argvars, "term_getstatus()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005298 term_T *term;
5299 char_u val[100];
5300
5301 rettv->v_type = VAR_STRING;
5302 if (buf == NULL)
5303 return;
5304 term = buf->b_term;
5305
5306 if (term_job_running(term))
5307 STRCPY(val, "running");
5308 else
5309 STRCPY(val, "finished");
5310 if (term->tl_normal_mode)
5311 STRCAT(val, ",normal");
5312 rettv->vval.v_string = vim_strsave(val);
5313}
5314
5315/*
5316 * "term_gettitle(buf)" function
5317 */
5318 void
5319f_term_gettitle(typval_T *argvars, typval_T *rettv)
5320{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005321 buf_T *buf = term_get_buf(argvars, "term_gettitle()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005322
5323 rettv->v_type = VAR_STRING;
5324 if (buf == NULL)
5325 return;
5326
5327 if (buf->b_term->tl_title != NULL)
5328 rettv->vval.v_string = vim_strsave(buf->b_term->tl_title);
5329}
5330
5331/*
5332 * "term_gettty(buf)" function
5333 */
5334 void
5335f_term_gettty(typval_T *argvars, typval_T *rettv)
5336{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005337 buf_T *buf = term_get_buf(argvars, "term_gettty()");
Bram Moolenaar9b50f362018-05-07 20:10:17 +02005338 char_u *p = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005339 int num = 0;
5340
5341 rettv->v_type = VAR_STRING;
5342 if (buf == NULL)
5343 return;
5344 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005345 num = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005346
5347 switch (num)
5348 {
5349 case 0:
5350 if (buf->b_term->tl_job != NULL)
5351 p = buf->b_term->tl_job->jv_tty_out;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005352 break;
5353 case 1:
5354 if (buf->b_term->tl_job != NULL)
5355 p = buf->b_term->tl_job->jv_tty_in;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005356 break;
5357 default:
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005358 semsg(_(e_invarg2), tv_get_string(&argvars[1]));
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005359 return;
5360 }
5361 if (p != NULL)
5362 rettv->vval.v_string = vim_strsave(p);
5363}
5364
5365/*
5366 * "term_list()" function
5367 */
5368 void
5369f_term_list(typval_T *argvars UNUSED, typval_T *rettv)
5370{
5371 term_T *tp;
5372 list_T *l;
5373
5374 if (rettv_list_alloc(rettv) == FAIL || first_term == NULL)
5375 return;
5376
5377 l = rettv->vval.v_list;
5378 for (tp = first_term; tp != NULL; tp = tp->tl_next)
5379 if (tp != NULL && tp->tl_buffer != NULL)
5380 if (list_append_number(l,
5381 (varnumber_T)tp->tl_buffer->b_fnum) == FAIL)
5382 return;
5383}
5384
5385/*
5386 * "term_scrape(buf, row)" function
5387 */
5388 void
5389f_term_scrape(typval_T *argvars, typval_T *rettv)
5390{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005391 buf_T *buf = term_get_buf(argvars, "term_scrape()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005392 VTermScreen *screen = NULL;
5393 VTermPos pos;
5394 list_T *l;
5395 term_T *term;
5396 char_u *p;
5397 sb_line_T *line;
5398
5399 if (rettv_list_alloc(rettv) == FAIL)
5400 return;
5401 if (buf == NULL)
5402 return;
5403 term = buf->b_term;
5404
5405 l = rettv->vval.v_list;
5406 pos.row = get_row_number(&argvars[1], term);
5407
5408 if (term->tl_vterm != NULL)
5409 {
5410 screen = vterm_obtain_screen(term->tl_vterm);
Bram Moolenaar06d62602018-12-27 21:27:03 +01005411 if (screen == NULL) // can't really happen
5412 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005413 p = NULL;
5414 line = NULL;
5415 }
5416 else
5417 {
5418 linenr_T lnum = pos.row + term->tl_scrollback_scrolled;
5419
5420 if (lnum < 0 || lnum >= term->tl_scrollback.ga_len)
5421 return;
5422 p = ml_get_buf(buf, lnum + 1, FALSE);
5423 line = (sb_line_T *)term->tl_scrollback.ga_data + lnum;
5424 }
5425
5426 for (pos.col = 0; pos.col < term->tl_cols; )
5427 {
5428 dict_T *dcell;
5429 int width;
5430 VTermScreenCellAttrs attrs;
5431 VTermColor fg, bg;
5432 char_u rgb[8];
5433 char_u mbs[MB_MAXBYTES * VTERM_MAX_CHARS_PER_CELL + 1];
5434 int off = 0;
5435 int i;
5436
5437 if (screen == NULL)
5438 {
5439 cellattr_T *cellattr;
5440 int len;
5441
5442 /* vterm has finished, get the cell from scrollback */
5443 if (pos.col >= line->sb_cols)
5444 break;
5445 cellattr = line->sb_cells + pos.col;
5446 width = cellattr->width;
5447 attrs = cellattr->attrs;
5448 fg = cellattr->fg;
5449 bg = cellattr->bg;
Bram Moolenaar1614a142019-10-06 22:00:13 +02005450 len = mb_ptr2len(p);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005451 mch_memmove(mbs, p, len);
5452 mbs[len] = NUL;
5453 p += len;
5454 }
5455 else
5456 {
5457 VTermScreenCell cell;
5458 if (vterm_screen_get_cell(screen, pos, &cell) == 0)
5459 break;
5460 for (i = 0; i < VTERM_MAX_CHARS_PER_CELL; ++i)
5461 {
5462 if (cell.chars[i] == 0)
5463 break;
5464 off += (*utf_char2bytes)((int)cell.chars[i], mbs + off);
5465 }
5466 mbs[off] = NUL;
5467 width = cell.width;
5468 attrs = cell.attrs;
5469 fg = cell.fg;
5470 bg = cell.bg;
5471 }
5472 dcell = dict_alloc();
Bram Moolenaar4b7e7be2018-02-11 14:53:30 +01005473 if (dcell == NULL)
5474 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005475 list_append_dict(l, dcell);
5476
Bram Moolenaare0be1672018-07-08 16:50:37 +02005477 dict_add_string(dcell, "chars", mbs);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005478
5479 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5480 fg.red, fg.green, fg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005481 dict_add_string(dcell, "fg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005482 vim_snprintf((char *)rgb, 8, "#%02x%02x%02x",
5483 bg.red, bg.green, bg.blue);
Bram Moolenaare0be1672018-07-08 16:50:37 +02005484 dict_add_string(dcell, "bg", rgb);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005485
Bram Moolenaare0be1672018-07-08 16:50:37 +02005486 dict_add_number(dcell, "attr", cell2attr(attrs, fg, bg));
5487 dict_add_number(dcell, "width", width);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005488
5489 ++pos.col;
5490 if (width == 2)
5491 ++pos.col;
5492 }
5493}
5494
5495/*
5496 * "term_sendkeys(buf, keys)" function
5497 */
5498 void
5499f_term_sendkeys(typval_T *argvars, typval_T *rettv)
5500{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005501 buf_T *buf = term_get_buf(argvars, "term_sendkeys()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005502 char_u *msg;
5503 term_T *term;
5504
5505 rettv->v_type = VAR_UNKNOWN;
5506 if (buf == NULL)
5507 return;
5508
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005509 msg = tv_get_string_chk(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005510 if (msg == NULL)
5511 return;
5512 term = buf->b_term;
5513 if (term->tl_vterm == NULL)
5514 return;
5515
5516 while (*msg != NUL)
5517 {
Bram Moolenaar6b810d92018-06-04 17:28:44 +02005518 int c;
5519
5520 if (*msg == K_SPECIAL && msg[1] != NUL && msg[2] != NUL)
5521 {
5522 c = TO_SPECIAL(msg[1], msg[2]);
5523 msg += 3;
5524 }
5525 else
5526 {
5527 c = PTR2CHAR(msg);
5528 msg += MB_CPTR2LEN(msg);
5529 }
5530 send_keys_to_term(term, c, FALSE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005531 }
5532}
5533
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005534#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS) || defined(PROTO)
5535/*
5536 * "term_getansicolors(buf)" function
5537 */
5538 void
5539f_term_getansicolors(typval_T *argvars, typval_T *rettv)
5540{
5541 buf_T *buf = term_get_buf(argvars, "term_getansicolors()");
5542 term_T *term;
5543 VTermState *state;
5544 VTermColor color;
5545 char_u hexbuf[10];
5546 int index;
5547 list_T *list;
5548
5549 if (rettv_list_alloc(rettv) == FAIL)
5550 return;
5551
5552 if (buf == NULL)
5553 return;
5554 term = buf->b_term;
5555 if (term->tl_vterm == NULL)
5556 return;
5557
5558 list = rettv->vval.v_list;
5559 state = vterm_obtain_state(term->tl_vterm);
5560 for (index = 0; index < 16; index++)
5561 {
5562 vterm_state_get_palette_color(state, index, &color);
5563 sprintf((char *)hexbuf, "#%02x%02x%02x",
5564 color.red, color.green, color.blue);
5565 if (list_append_string(list, hexbuf, 7) == FAIL)
5566 return;
5567 }
5568}
5569
5570/*
5571 * "term_setansicolors(buf, list)" function
5572 */
5573 void
5574f_term_setansicolors(typval_T *argvars, typval_T *rettv UNUSED)
5575{
5576 buf_T *buf = term_get_buf(argvars, "term_setansicolors()");
5577 term_T *term;
5578
5579 if (buf == NULL)
5580 return;
5581 term = buf->b_term;
5582 if (term->tl_vterm == NULL)
5583 return;
5584
5585 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
5586 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005587 emsg(_(e_listreq));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005588 return;
5589 }
5590
5591 if (set_ansi_colors_list(term->tl_vterm, argvars[1].vval.v_list) == FAIL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01005592 emsg(_(e_invarg));
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005593}
5594#endif
5595
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005596/*
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005597 * "term_setapi(buf, api)" function
5598 */
5599 void
5600f_term_setapi(typval_T *argvars, typval_T *rettv UNUSED)
5601{
5602 buf_T *buf = term_get_buf(argvars, "term_setapi()");
5603 term_T *term;
5604 char_u *api;
5605
5606 if (buf == NULL)
5607 return;
5608 term = buf->b_term;
5609 vim_free(term->tl_api);
5610 api = tv_get_string_chk(&argvars[1]);
5611 if (api != NULL)
5612 term->tl_api = vim_strsave(api);
5613 else
5614 term->tl_api = NULL;
5615}
5616
5617/*
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005618 * "term_setrestore(buf, command)" function
5619 */
5620 void
5621f_term_setrestore(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5622{
5623#if defined(FEAT_SESSION)
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005624 buf_T *buf = term_get_buf(argvars, "term_setrestore()");
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005625 term_T *term;
5626 char_u *cmd;
5627
5628 if (buf == NULL)
5629 return;
5630 term = buf->b_term;
5631 vim_free(term->tl_command);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005632 cmd = tv_get_string_chk(&argvars[1]);
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005633 if (cmd != NULL)
5634 term->tl_command = vim_strsave(cmd);
5635 else
5636 term->tl_command = NULL;
5637#endif
5638}
5639
5640/*
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005641 * "term_setkill(buf, how)" function
5642 */
5643 void
5644f_term_setkill(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
5645{
5646 buf_T *buf = term_get_buf(argvars, "term_setkill()");
5647 term_T *term;
5648 char_u *how;
5649
5650 if (buf == NULL)
5651 return;
5652 term = buf->b_term;
5653 vim_free(term->tl_kill);
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005654 how = tv_get_string_chk(&argvars[1]);
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005655 if (how != NULL)
5656 term->tl_kill = vim_strsave(how);
5657 else
5658 term->tl_kill = NULL;
5659}
5660
5661/*
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005662 * "term_start(command, options)" function
5663 */
5664 void
5665f_term_start(typval_T *argvars, typval_T *rettv)
5666{
5667 jobopt_T opt;
5668 buf_T *buf;
5669
5670 init_job_options(&opt);
5671 if (argvars[1].v_type != VAR_UNKNOWN
5672 && get_job_options(&argvars[1], &opt,
5673 JO_TIMEOUT_ALL + JO_STOPONEXIT
5674 + JO_CALLBACK + JO_OUT_CALLBACK + JO_ERR_CALLBACK
5675 + JO_EXIT_CB + JO_CLOSE_CALLBACK + JO_OUT_IO,
5676 JO2_TERM_NAME + JO2_TERM_FINISH + JO2_HIDDEN + JO2_TERM_OPENCMD
5677 + JO2_TERM_COLS + JO2_TERM_ROWS + JO2_VERTICAL + JO2_CURWIN
Bram Moolenaar4d8bac82018-03-09 21:33:34 +01005678 + JO2_CWD + JO2_ENV + JO2_EOF_CHARS
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02005679 + JO2_NORESTORE + JO2_TERM_KILL
Bram Moolenaard2842ea2019-09-26 23:08:54 +02005680 + JO2_ANSI_COLORS + JO2_TTY_TYPE + JO2_TERM_API) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005681 return;
5682
Bram Moolenaar13568252018-03-16 20:46:58 +01005683 buf = term_start(&argvars[0], NULL, &opt, 0);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005684
5685 if (buf != NULL && buf->b_term != NULL)
5686 rettv->vval.v_number = buf->b_fnum;
5687}
5688
5689/*
5690 * "term_wait" function
5691 */
5692 void
5693f_term_wait(typval_T *argvars, typval_T *rettv UNUSED)
5694{
Bram Moolenaar25cdd9c2018-03-10 20:28:12 +01005695 buf_T *buf = term_get_buf(argvars, "term_wait()");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005696
5697 if (buf == NULL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005698 return;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005699 if (buf->b_term->tl_job == NULL)
5700 {
5701 ch_log(NULL, "term_wait(): no job to wait for");
5702 return;
5703 }
5704 if (buf->b_term->tl_job->jv_channel == NULL)
5705 /* channel is closed, nothing to do */
5706 return;
5707
5708 /* Get the job status, this will detect a job that finished. */
Bram Moolenaara15ef452018-02-09 16:46:00 +01005709 if (!buf->b_term->tl_job->jv_channel->ch_keep_open
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005710 && STRCMP(job_status(buf->b_term->tl_job), "dead") == 0)
5711 {
5712 /* The job is dead, keep reading channel I/O until the channel is
5713 * closed. buf->b_term may become NULL if the terminal was closed while
5714 * waiting. */
5715 ch_log(NULL, "term_wait(): waiting for channel to close");
5716 while (buf->b_term != NULL && !buf->b_term->tl_channel_closed)
5717 {
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02005718 term_flush_messages();
5719
Bram Moolenaard45aa552018-05-21 22:50:29 +02005720 ui_delay(10L, FALSE);
Bram Moolenaare5182262017-11-19 15:05:44 +01005721 if (!buf_valid(buf))
5722 /* If the terminal is closed when the channel is closed the
5723 * buffer disappears. */
5724 break;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005725 }
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02005726
5727 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005728 }
5729 else
5730 {
5731 long wait = 10L;
5732
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02005733 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005734
5735 /* Wait for some time for any channel I/O. */
5736 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaard155d7a2018-12-21 16:04:21 +01005737 wait = tv_get_number(&argvars[1]);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005738 ui_delay(wait, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005739
5740 /* Flushing messages on channels is hopefully sufficient.
5741 * TODO: is there a better way? */
Bram Moolenaar5c381eb2019-06-25 06:50:31 +02005742 term_flush_messages();
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005743 }
5744}
5745
5746/*
5747 * Called when a channel has sent all the lines to a terminal.
5748 * Send a CTRL-D to mark the end of the text.
5749 */
5750 void
5751term_send_eof(channel_T *ch)
5752{
5753 term_T *term;
5754
5755 for (term = first_term; term != NULL; term = term->tl_next)
5756 if (term->tl_job == ch->ch_job)
5757 {
5758 if (term->tl_eof_chars != NULL)
5759 {
5760 channel_send(ch, PART_IN, term->tl_eof_chars,
5761 (int)STRLEN(term->tl_eof_chars), NULL);
5762 channel_send(ch, PART_IN, (char_u *)"\r", 1, NULL);
5763 }
Bram Moolenaar4f974752019-02-17 17:44:42 +01005764# ifdef MSWIN
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005765 else
5766 /* Default: CTRL-D */
5767 channel_send(ch, PART_IN, (char_u *)"\004\r", 2, NULL);
5768# endif
5769 }
5770}
5771
Bram Moolenaar113e1072019-01-20 15:30:40 +01005772#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaarf9c38832018-06-19 19:59:20 +02005773 job_T *
5774term_getjob(term_T *term)
5775{
5776 return term != NULL ? term->tl_job : NULL;
5777}
Bram Moolenaar113e1072019-01-20 15:30:40 +01005778#endif
Bram Moolenaarf9c38832018-06-19 19:59:20 +02005779
Bram Moolenaar4f974752019-02-17 17:44:42 +01005780# if defined(MSWIN) || defined(PROTO)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005781
5782/**************************************
5783 * 2. MS-Windows implementation.
5784 */
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02005785#ifdef PROTO
5786typedef int COORD;
5787typedef int DWORD;
5788typedef int HANDLE;
5789typedef int *DWORD_PTR;
5790typedef int HPCON;
5791typedef int HRESULT;
5792typedef int LPPROC_THREAD_ATTRIBUTE_LIST;
Bram Moolenaarad3ec762019-04-21 00:00:13 +02005793typedef int SIZE_T;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02005794typedef int PSIZE_T;
5795typedef int PVOID;
Bram Moolenaarb9cdb372019-04-17 18:24:35 +02005796typedef int WINAPI;
5797#endif
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02005798
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005799HRESULT (WINAPI *pCreatePseudoConsole)(COORD, HANDLE, HANDLE, DWORD, HPCON*);
5800HRESULT (WINAPI *pResizePseudoConsole)(HPCON, COORD);
5801HRESULT (WINAPI *pClosePseudoConsole)(HPCON);
Bram Moolenaar48773f12019-02-12 21:46:46 +01005802BOOL (WINAPI *pInitializeProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD, PSIZE_T);
5803BOOL (WINAPI *pUpdateProcThreadAttribute)(LPPROC_THREAD_ATTRIBUTE_LIST, DWORD, DWORD_PTR, PVOID, SIZE_T, PVOID, PSIZE_T);
5804void (WINAPI *pDeleteProcThreadAttributeList)(LPPROC_THREAD_ATTRIBUTE_LIST);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005805
5806 static int
5807dyn_conpty_init(int verbose)
5808{
Bram Moolenaar5acd9872019-02-16 13:35:13 +01005809 static HMODULE hKerneldll = NULL;
5810 int i;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005811 static struct
5812 {
5813 char *name;
5814 FARPROC *ptr;
5815 } conpty_entry[] =
5816 {
5817 {"CreatePseudoConsole", (FARPROC*)&pCreatePseudoConsole},
5818 {"ResizePseudoConsole", (FARPROC*)&pResizePseudoConsole},
5819 {"ClosePseudoConsole", (FARPROC*)&pClosePseudoConsole},
5820 {"InitializeProcThreadAttributeList",
5821 (FARPROC*)&pInitializeProcThreadAttributeList},
5822 {"UpdateProcThreadAttribute",
5823 (FARPROC*)&pUpdateProcThreadAttribute},
5824 {"DeleteProcThreadAttributeList",
5825 (FARPROC*)&pDeleteProcThreadAttributeList},
5826 {NULL, NULL}
5827 };
5828
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01005829 if (!has_conpty_working())
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005830 {
Bram Moolenaar5acd9872019-02-16 13:35:13 +01005831 if (verbose)
5832 emsg(_("E982: ConPTY is not available"));
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005833 return FAIL;
5834 }
5835
Bram Moolenaar5acd9872019-02-16 13:35:13 +01005836 // No need to initialize twice.
5837 if (hKerneldll)
5838 return OK;
5839
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005840 hKerneldll = vimLoadLib("kernel32.dll");
5841 for (i = 0; conpty_entry[i].name != NULL
5842 && conpty_entry[i].ptr != NULL; ++i)
5843 {
5844 if ((*conpty_entry[i].ptr = (FARPROC)GetProcAddress(hKerneldll,
5845 conpty_entry[i].name)) == NULL)
5846 {
5847 if (verbose)
5848 semsg(_(e_loadfunc), conpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01005849 hKerneldll = NULL;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005850 return FAIL;
5851 }
5852 }
5853
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005854 return OK;
5855}
5856
5857 static int
5858conpty_term_and_job_init(
5859 term_T *term,
5860 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02005861 char **argv UNUSED,
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005862 jobopt_T *opt,
5863 jobopt_T *orig_opt)
5864{
5865 WCHAR *cmd_wchar = NULL;
5866 WCHAR *cmd_wchar_copy = NULL;
5867 WCHAR *cwd_wchar = NULL;
5868 WCHAR *env_wchar = NULL;
5869 channel_T *channel = NULL;
5870 job_T *job = NULL;
5871 HANDLE jo = NULL;
5872 garray_T ga_cmd, ga_env;
5873 char_u *cmd = NULL;
5874 HRESULT hr;
5875 COORD consize;
5876 SIZE_T breq;
5877 PROCESS_INFORMATION proc_info;
5878 HANDLE i_theirs = NULL;
5879 HANDLE o_theirs = NULL;
5880 HANDLE i_ours = NULL;
5881 HANDLE o_ours = NULL;
5882
5883 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
5884 ga_init2(&ga_env, (int)sizeof(char*), 20);
5885
5886 if (argvar->v_type == VAR_STRING)
5887 {
5888 cmd = argvar->vval.v_string;
5889 }
5890 else if (argvar->v_type == VAR_LIST)
5891 {
5892 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
5893 goto failed;
5894 cmd = ga_cmd.ga_data;
5895 }
5896 if (cmd == NULL || *cmd == NUL)
5897 {
5898 emsg(_(e_invarg));
5899 goto failed;
5900 }
5901
5902 term->tl_arg0_cmd = vim_strsave(cmd);
5903
5904 cmd_wchar = enc_to_utf16(cmd, NULL);
5905
5906 if (cmd_wchar != NULL)
5907 {
5908 /* Request by CreateProcessW */
5909 breq = wcslen(cmd_wchar) + 1 + 1; /* Addition of NUL by API */
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005910 cmd_wchar_copy = ALLOC_MULT(WCHAR, breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005911 wcsncpy(cmd_wchar_copy, cmd_wchar, breq - 1);
5912 }
5913
5914 ga_clear(&ga_cmd);
5915 if (cmd_wchar == NULL)
5916 goto failed;
5917 if (opt->jo_cwd != NULL)
5918 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
5919
5920 win32_build_env(opt->jo_env, &ga_env, TRUE);
5921 env_wchar = ga_env.ga_data;
5922
5923 if (!CreatePipe(&i_theirs, &i_ours, NULL, 0))
5924 goto failed;
5925 if (!CreatePipe(&o_ours, &o_theirs, NULL, 0))
5926 goto failed;
5927
5928 consize.X = term->tl_cols;
5929 consize.Y = term->tl_rows;
5930 hr = pCreatePseudoConsole(consize, i_theirs, o_theirs, 0,
5931 &term->tl_conpty);
5932 if (FAILED(hr))
5933 goto failed;
5934
5935 term->tl_siex.StartupInfo.cb = sizeof(term->tl_siex);
5936
5937 /* Set up pipe inheritance safely: Vista or later. */
5938 pInitializeProcThreadAttributeList(NULL, 1, 0, &breq);
Bram Moolenaarc799fe22019-05-28 23:08:19 +02005939 term->tl_siex.lpAttributeList = alloc(breq);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01005940 if (!term->tl_siex.lpAttributeList)
5941 goto failed;
5942 if (!pInitializeProcThreadAttributeList(term->tl_siex.lpAttributeList, 1,
5943 0, &breq))
5944 goto failed;
5945 if (!pUpdateProcThreadAttribute(
5946 term->tl_siex.lpAttributeList, 0,
5947 PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, term->tl_conpty,
5948 sizeof(HPCON), NULL, NULL))
5949 goto failed;
5950
5951 channel = add_channel();
5952 if (channel == NULL)
5953 goto failed;
5954
5955 job = job_alloc();
5956 if (job == NULL)
5957 goto failed;
5958 if (argvar->v_type == VAR_STRING)
5959 {
5960 int argc;
5961
5962 build_argv_from_string(cmd, &job->jv_argv, &argc);
5963 }
5964 else
5965 {
5966 int argc;
5967
5968 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
5969 }
5970
5971 if (opt->jo_set & JO_IN_BUF)
5972 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
5973
5974 if (!CreateProcessW(NULL, cmd_wchar_copy, NULL, NULL, FALSE,
5975 EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT
5976 | CREATE_SUSPENDED | CREATE_NEW_PROCESS_GROUP
5977 | CREATE_DEFAULT_ERROR_MODE,
5978 env_wchar, cwd_wchar,
5979 &term->tl_siex.StartupInfo, &proc_info))
5980 goto failed;
5981
5982 CloseHandle(i_theirs);
5983 CloseHandle(o_theirs);
5984
5985 channel_set_pipes(channel,
5986 (sock_T)i_ours,
5987 (sock_T)o_ours,
5988 (sock_T)o_ours);
5989
5990 /* Write lines with CR instead of NL. */
5991 channel->ch_write_text_mode = TRUE;
5992
5993 /* Use to explicitly delete anonymous pipe handle. */
5994 channel->ch_anonymous_pipe = TRUE;
5995
5996 jo = CreateJobObject(NULL, NULL);
5997 if (jo == NULL)
5998 goto failed;
5999
6000 if (!AssignProcessToJobObject(jo, proc_info.hProcess))
6001 {
6002 /* Failed, switch the way to terminate process with TerminateProcess. */
6003 CloseHandle(jo);
6004 jo = NULL;
6005 }
6006
6007 ResumeThread(proc_info.hThread);
6008 CloseHandle(proc_info.hThread);
6009
6010 vim_free(cmd_wchar);
6011 vim_free(cmd_wchar_copy);
6012 vim_free(cwd_wchar);
6013 vim_free(env_wchar);
6014
6015 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6016 goto failed;
6017
6018#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6019 if (opt->jo_set2 & JO2_ANSI_COLORS)
6020 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6021 else
6022 init_vterm_ansi_colors(term->tl_vterm);
6023#endif
6024
6025 channel_set_job(channel, job, opt);
6026 job_set_options(job, opt);
6027
6028 job->jv_channel = channel;
6029 job->jv_proc_info = proc_info;
6030 job->jv_job_object = jo;
6031 job->jv_status = JOB_STARTED;
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006032 job->jv_tty_type = vim_strsave((char_u *)"conpty");
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006033 ++job->jv_refcount;
6034 term->tl_job = job;
6035
6036 /* Redirecting stdout and stderr doesn't work at the job level. Instead
6037 * open the file here and handle it in. opt->jo_io was changed in
6038 * setup_job_options(), use the original flags here. */
6039 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6040 {
6041 char_u *fname = opt->jo_io_name[PART_OUT];
6042
6043 ch_log(channel, "Opening output file %s", fname);
6044 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6045 if (term->tl_out_fd == NULL)
6046 semsg(_(e_notopen), fname);
6047 }
6048
6049 return OK;
6050
6051failed:
6052 ga_clear(&ga_cmd);
6053 ga_clear(&ga_env);
6054 vim_free(cmd_wchar);
6055 vim_free(cmd_wchar_copy);
6056 vim_free(cwd_wchar);
6057 if (channel != NULL)
6058 channel_clear(channel);
6059 if (job != NULL)
6060 {
6061 job->jv_channel = NULL;
6062 job_cleanup(job);
6063 }
6064 term->tl_job = NULL;
6065 if (jo != NULL)
6066 CloseHandle(jo);
6067
6068 if (term->tl_siex.lpAttributeList != NULL)
6069 {
6070 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6071 vim_free(term->tl_siex.lpAttributeList);
6072 }
6073 term->tl_siex.lpAttributeList = NULL;
6074 if (o_theirs != NULL)
6075 CloseHandle(o_theirs);
6076 if (o_ours != NULL)
6077 CloseHandle(o_ours);
6078 if (i_ours != NULL)
6079 CloseHandle(i_ours);
6080 if (i_theirs != NULL)
6081 CloseHandle(i_theirs);
6082 if (term->tl_conpty != NULL)
6083 pClosePseudoConsole(term->tl_conpty);
6084 term->tl_conpty = NULL;
6085 return FAIL;
6086}
6087
6088 static void
6089conpty_term_report_winsize(term_T *term, int rows, int cols)
6090{
6091 COORD consize;
6092
6093 consize.X = cols;
6094 consize.Y = rows;
6095 pResizePseudoConsole(term->tl_conpty, consize);
6096}
6097
Bram Moolenaar840d16f2019-09-10 21:27:18 +02006098 static void
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006099term_free_conpty(term_T *term)
6100{
6101 if (term->tl_siex.lpAttributeList != NULL)
6102 {
6103 pDeleteProcThreadAttributeList(term->tl_siex.lpAttributeList);
6104 vim_free(term->tl_siex.lpAttributeList);
6105 }
6106 term->tl_siex.lpAttributeList = NULL;
6107 if (term->tl_conpty != NULL)
6108 pClosePseudoConsole(term->tl_conpty);
6109 term->tl_conpty = NULL;
6110}
6111
6112 int
6113use_conpty(void)
6114{
6115 return has_conpty;
6116}
6117
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006118# ifndef PROTO
6119
6120#define WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN 1ul
6121#define WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN 2ull
Bram Moolenaard317b382018-02-08 22:33:31 +01006122#define WINPTY_MOUSE_MODE_FORCE 2
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006123
6124void* (*winpty_config_new)(UINT64, void*);
6125void* (*winpty_open)(void*, void*);
6126void* (*winpty_spawn_config_new)(UINT64, void*, LPCWSTR, void*, void*, void*);
6127BOOL (*winpty_spawn)(void*, void*, HANDLE*, HANDLE*, DWORD*, void*);
6128void (*winpty_config_set_mouse_mode)(void*, int);
6129void (*winpty_config_set_initial_size)(void*, int, int);
6130LPCWSTR (*winpty_conin_name)(void*);
6131LPCWSTR (*winpty_conout_name)(void*);
6132LPCWSTR (*winpty_conerr_name)(void*);
6133void (*winpty_free)(void*);
6134void (*winpty_config_free)(void*);
6135void (*winpty_spawn_config_free)(void*);
6136void (*winpty_error_free)(void*);
6137LPCWSTR (*winpty_error_msg)(void*);
6138BOOL (*winpty_set_size)(void*, int, int, void*);
6139HANDLE (*winpty_agent_process)(void*);
6140
6141#define WINPTY_DLL "winpty.dll"
6142
6143static HINSTANCE hWinPtyDLL = NULL;
6144# endif
6145
6146 static int
6147dyn_winpty_init(int verbose)
6148{
6149 int i;
6150 static struct
6151 {
6152 char *name;
6153 FARPROC *ptr;
6154 } winpty_entry[] =
6155 {
6156 {"winpty_conerr_name", (FARPROC*)&winpty_conerr_name},
6157 {"winpty_config_free", (FARPROC*)&winpty_config_free},
6158 {"winpty_config_new", (FARPROC*)&winpty_config_new},
6159 {"winpty_config_set_mouse_mode",
6160 (FARPROC*)&winpty_config_set_mouse_mode},
6161 {"winpty_config_set_initial_size",
6162 (FARPROC*)&winpty_config_set_initial_size},
6163 {"winpty_conin_name", (FARPROC*)&winpty_conin_name},
6164 {"winpty_conout_name", (FARPROC*)&winpty_conout_name},
6165 {"winpty_error_free", (FARPROC*)&winpty_error_free},
6166 {"winpty_free", (FARPROC*)&winpty_free},
6167 {"winpty_open", (FARPROC*)&winpty_open},
6168 {"winpty_spawn", (FARPROC*)&winpty_spawn},
6169 {"winpty_spawn_config_free", (FARPROC*)&winpty_spawn_config_free},
6170 {"winpty_spawn_config_new", (FARPROC*)&winpty_spawn_config_new},
6171 {"winpty_error_msg", (FARPROC*)&winpty_error_msg},
6172 {"winpty_set_size", (FARPROC*)&winpty_set_size},
6173 {"winpty_agent_process", (FARPROC*)&winpty_agent_process},
6174 {NULL, NULL}
6175 };
6176
6177 /* No need to initialize twice. */
6178 if (hWinPtyDLL)
6179 return OK;
6180 /* Load winpty.dll, prefer using the 'winptydll' option, fall back to just
6181 * winpty.dll. */
6182 if (*p_winptydll != NUL)
6183 hWinPtyDLL = vimLoadLib((char *)p_winptydll);
6184 if (!hWinPtyDLL)
6185 hWinPtyDLL = vimLoadLib(WINPTY_DLL);
6186 if (!hWinPtyDLL)
6187 {
6188 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006189 semsg(_(e_loadlib), *p_winptydll != NUL ? p_winptydll
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006190 : (char_u *)WINPTY_DLL);
6191 return FAIL;
6192 }
6193 for (i = 0; winpty_entry[i].name != NULL
6194 && winpty_entry[i].ptr != NULL; ++i)
6195 {
6196 if ((*winpty_entry[i].ptr = (FARPROC)GetProcAddress(hWinPtyDLL,
6197 winpty_entry[i].name)) == NULL)
6198 {
6199 if (verbose)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006200 semsg(_(e_loadfunc), winpty_entry[i].name);
Bram Moolenaar5acd9872019-02-16 13:35:13 +01006201 hWinPtyDLL = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006202 return FAIL;
6203 }
6204 }
6205
6206 return OK;
6207}
6208
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006209 static int
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006210winpty_term_and_job_init(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006211 term_T *term,
6212 typval_T *argvar,
Bram Moolenaarbd67aac2019-09-21 23:09:04 +02006213 char **argv UNUSED,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006214 jobopt_T *opt,
6215 jobopt_T *orig_opt)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006216{
6217 WCHAR *cmd_wchar = NULL;
6218 WCHAR *cwd_wchar = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006219 WCHAR *env_wchar = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006220 channel_T *channel = NULL;
6221 job_T *job = NULL;
6222 DWORD error;
6223 HANDLE jo = NULL;
6224 HANDLE child_process_handle;
6225 HANDLE child_thread_handle;
Bram Moolenaar4aad53c2018-01-26 21:11:03 +01006226 void *winpty_err = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006227 void *spawn_config = NULL;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006228 garray_T ga_cmd, ga_env;
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006229 char_u *cmd = NULL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006230
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006231 ga_init2(&ga_cmd, (int)sizeof(char*), 20);
6232 ga_init2(&ga_env, (int)sizeof(char*), 20);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006233
6234 if (argvar->v_type == VAR_STRING)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006235 {
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006236 cmd = argvar->vval.v_string;
6237 }
6238 else if (argvar->v_type == VAR_LIST)
6239 {
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006240 if (win32_build_cmd(argvar->vval.v_list, &ga_cmd) == FAIL)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006241 goto failed;
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006242 cmd = ga_cmd.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006243 }
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006244 if (cmd == NULL || *cmd == NUL)
6245 {
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006246 emsg(_(e_invarg));
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006247 goto failed;
6248 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006249
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006250 term->tl_arg0_cmd = vim_strsave(cmd);
6251
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006252 cmd_wchar = enc_to_utf16(cmd, NULL);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006253 ga_clear(&ga_cmd);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006254 if (cmd_wchar == NULL)
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006255 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006256 if (opt->jo_cwd != NULL)
6257 cwd_wchar = enc_to_utf16(opt->jo_cwd, NULL);
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006258
Bram Moolenaar52dbb5e2017-11-21 18:11:27 +01006259 win32_build_env(opt->jo_env, &ga_env, TRUE);
6260 env_wchar = ga_env.ga_data;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006261
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006262 term->tl_winpty_config = winpty_config_new(0, &winpty_err);
6263 if (term->tl_winpty_config == NULL)
6264 goto failed;
6265
6266 winpty_config_set_mouse_mode(term->tl_winpty_config,
6267 WINPTY_MOUSE_MODE_FORCE);
6268 winpty_config_set_initial_size(term->tl_winpty_config,
6269 term->tl_cols, term->tl_rows);
6270 term->tl_winpty = winpty_open(term->tl_winpty_config, &winpty_err);
6271 if (term->tl_winpty == NULL)
6272 goto failed;
6273
6274 spawn_config = winpty_spawn_config_new(
6275 WINPTY_SPAWN_FLAG_AUTO_SHUTDOWN |
6276 WINPTY_SPAWN_FLAG_EXIT_AFTER_SHUTDOWN,
6277 NULL,
6278 cmd_wchar,
6279 cwd_wchar,
Bram Moolenaarba6febd2017-10-30 21:56:23 +01006280 env_wchar,
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006281 &winpty_err);
6282 if (spawn_config == NULL)
6283 goto failed;
6284
6285 channel = add_channel();
6286 if (channel == NULL)
6287 goto failed;
6288
6289 job = job_alloc();
6290 if (job == NULL)
6291 goto failed;
Bram Moolenaarebe74b72018-04-21 23:34:43 +02006292 if (argvar->v_type == VAR_STRING)
6293 {
6294 int argc;
6295
6296 build_argv_from_string(cmd, &job->jv_argv, &argc);
6297 }
6298 else
6299 {
6300 int argc;
6301
6302 build_argv_from_list(argvar->vval.v_list, &job->jv_argv, &argc);
6303 }
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006304
6305 if (opt->jo_set & JO_IN_BUF)
6306 job->jv_in_buf = buflist_findnr(opt->jo_io_buf[PART_IN]);
6307
6308 if (!winpty_spawn(term->tl_winpty, spawn_config, &child_process_handle,
6309 &child_thread_handle, &error, &winpty_err))
6310 goto failed;
6311
6312 channel_set_pipes(channel,
6313 (sock_T)CreateFileW(
6314 winpty_conin_name(term->tl_winpty),
6315 GENERIC_WRITE, 0, NULL,
6316 OPEN_EXISTING, 0, NULL),
6317 (sock_T)CreateFileW(
6318 winpty_conout_name(term->tl_winpty),
6319 GENERIC_READ, 0, NULL,
6320 OPEN_EXISTING, 0, NULL),
6321 (sock_T)CreateFileW(
6322 winpty_conerr_name(term->tl_winpty),
6323 GENERIC_READ, 0, NULL,
6324 OPEN_EXISTING, 0, NULL));
6325
6326 /* Write lines with CR instead of NL. */
6327 channel->ch_write_text_mode = TRUE;
6328
6329 jo = CreateJobObject(NULL, NULL);
6330 if (jo == NULL)
6331 goto failed;
6332
6333 if (!AssignProcessToJobObject(jo, child_process_handle))
6334 {
6335 /* Failed, switch the way to terminate process with TerminateProcess. */
6336 CloseHandle(jo);
6337 jo = NULL;
6338 }
6339
6340 winpty_spawn_config_free(spawn_config);
6341 vim_free(cmd_wchar);
6342 vim_free(cwd_wchar);
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006343 vim_free(env_wchar);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006344
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006345 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6346 goto failed;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006347
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006348#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6349 if (opt->jo_set2 & JO2_ANSI_COLORS)
6350 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6351 else
6352 init_vterm_ansi_colors(term->tl_vterm);
6353#endif
6354
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006355 channel_set_job(channel, job, opt);
6356 job_set_options(job, opt);
6357
6358 job->jv_channel = channel;
6359 job->jv_proc_info.hProcess = child_process_handle;
6360 job->jv_proc_info.dwProcessId = GetProcessId(child_process_handle);
6361 job->jv_job_object = jo;
6362 job->jv_status = JOB_STARTED;
6363 job->jv_tty_in = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006364 (short_u *)winpty_conin_name(term->tl_winpty), NULL);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006365 job->jv_tty_out = utf16_to_enc(
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006366 (short_u *)winpty_conout_name(term->tl_winpty), NULL);
Bram Moolenaar18442cb2019-02-13 21:22:12 +01006367 job->jv_tty_type = vim_strsave((char_u *)"winpty");
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006368 ++job->jv_refcount;
6369 term->tl_job = job;
6370
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006371 /* Redirecting stdout and stderr doesn't work at the job level. Instead
6372 * open the file here and handle it in. opt->jo_io was changed in
6373 * setup_job_options(), use the original flags here. */
6374 if (orig_opt->jo_io[PART_OUT] == JIO_FILE)
6375 {
6376 char_u *fname = opt->jo_io_name[PART_OUT];
6377
6378 ch_log(channel, "Opening output file %s", fname);
6379 term->tl_out_fd = mch_fopen((char *)fname, WRITEBIN);
6380 if (term->tl_out_fd == NULL)
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006381 semsg(_(e_notopen), fname);
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006382 }
6383
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006384 return OK;
6385
6386failed:
Bram Moolenaarede35bb2018-01-26 20:05:18 +01006387 ga_clear(&ga_cmd);
6388 ga_clear(&ga_env);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006389 vim_free(cmd_wchar);
6390 vim_free(cwd_wchar);
6391 if (spawn_config != NULL)
6392 winpty_spawn_config_free(spawn_config);
6393 if (channel != NULL)
6394 channel_clear(channel);
6395 if (job != NULL)
6396 {
6397 job->jv_channel = NULL;
6398 job_cleanup(job);
6399 }
6400 term->tl_job = NULL;
6401 if (jo != NULL)
6402 CloseHandle(jo);
6403 if (term->tl_winpty != NULL)
6404 winpty_free(term->tl_winpty);
6405 term->tl_winpty = NULL;
6406 if (term->tl_winpty_config != NULL)
6407 winpty_config_free(term->tl_winpty_config);
6408 term->tl_winpty_config = NULL;
6409 if (winpty_err != NULL)
6410 {
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006411 char *msg = (char *)utf16_to_enc(
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006412 (short_u *)winpty_error_msg(winpty_err), NULL);
6413
Bram Moolenaarf9e3e092019-01-13 23:38:42 +01006414 emsg(msg);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006415 winpty_error_free(winpty_err);
6416 }
6417 return FAIL;
6418}
6419
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006420/*
6421 * Create a new terminal of "rows" by "cols" cells.
6422 * Store a reference in "term".
6423 * Return OK or FAIL.
6424 */
6425 static int
6426term_and_job_init(
6427 term_T *term,
6428 typval_T *argvar,
6429 char **argv UNUSED,
6430 jobopt_T *opt,
6431 jobopt_T *orig_opt)
6432{
6433 int use_winpty = FALSE;
6434 int use_conpty = FALSE;
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006435 int tty_type = *p_twt;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006436
6437 has_winpty = dyn_winpty_init(FALSE) != FAIL ? TRUE : FALSE;
6438 has_conpty = dyn_conpty_init(FALSE) != FAIL ? TRUE : FALSE;
6439
6440 if (!has_winpty && !has_conpty)
6441 // If neither is available give the errors for winpty, since when
6442 // conpty is not available it can't be installed either.
6443 return dyn_winpty_init(TRUE);
6444
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006445 if (opt->jo_tty_type != NUL)
6446 tty_type = opt->jo_tty_type;
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006447
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006448 if (tty_type == NUL)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006449 {
Bram Moolenaard9ef1b82019-02-13 19:23:10 +01006450 if (has_conpty && (is_conpty_stable() || !has_winpty))
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006451 use_conpty = TRUE;
6452 else if (has_winpty)
6453 use_winpty = TRUE;
6454 // else: error
6455 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006456 else if (tty_type == 'w') // winpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006457 {
6458 if (has_winpty)
6459 use_winpty = TRUE;
6460 }
Bram Moolenaarc6ddce32019-02-08 12:47:03 +01006461 else if (tty_type == 'c') // conpty
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006462 {
6463 if (has_conpty)
6464 use_conpty = TRUE;
6465 else
6466 return dyn_conpty_init(TRUE);
6467 }
6468
6469 if (use_conpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006470 return conpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006471
6472 if (use_winpty)
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006473 return winpty_term_and_job_init(term, argvar, argv, opt, orig_opt);
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006474
6475 // error
6476 return dyn_winpty_init(TRUE);
6477}
6478
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006479 static int
6480create_pty_only(term_T *term, jobopt_T *options)
6481{
6482 HANDLE hPipeIn = INVALID_HANDLE_VALUE;
6483 HANDLE hPipeOut = INVALID_HANDLE_VALUE;
6484 char in_name[80], out_name[80];
6485 channel_T *channel = NULL;
6486
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006487 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6488 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006489
6490 vim_snprintf(in_name, sizeof(in_name), "\\\\.\\pipe\\vim-%d-in-%d",
6491 GetCurrentProcessId(),
6492 curbuf->b_fnum);
6493 hPipeIn = CreateNamedPipe(in_name, PIPE_ACCESS_OUTBOUND,
6494 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6495 PIPE_UNLIMITED_INSTANCES,
6496 0, 0, NMPWAIT_NOWAIT, NULL);
6497 if (hPipeIn == INVALID_HANDLE_VALUE)
6498 goto failed;
6499
6500 vim_snprintf(out_name, sizeof(out_name), "\\\\.\\pipe\\vim-%d-out-%d",
6501 GetCurrentProcessId(),
6502 curbuf->b_fnum);
6503 hPipeOut = CreateNamedPipe(out_name, PIPE_ACCESS_INBOUND,
6504 PIPE_TYPE_MESSAGE | PIPE_NOWAIT,
6505 PIPE_UNLIMITED_INSTANCES,
6506 0, 0, 0, NULL);
6507 if (hPipeOut == INVALID_HANDLE_VALUE)
6508 goto failed;
6509
6510 ConnectNamedPipe(hPipeIn, NULL);
6511 ConnectNamedPipe(hPipeOut, NULL);
6512
6513 term->tl_job = job_alloc();
6514 if (term->tl_job == NULL)
6515 goto failed;
6516 ++term->tl_job->jv_refcount;
6517
6518 /* behave like the job is already finished */
6519 term->tl_job->jv_status = JOB_FINISHED;
6520
6521 channel = add_channel();
6522 if (channel == NULL)
6523 goto failed;
6524 term->tl_job->jv_channel = channel;
6525 channel->ch_keep_open = TRUE;
6526 channel->ch_named_pipe = TRUE;
6527
6528 channel_set_pipes(channel,
6529 (sock_T)hPipeIn,
6530 (sock_T)hPipeOut,
6531 (sock_T)hPipeOut);
6532 channel_set_job(channel, term->tl_job, options);
6533 term->tl_job->jv_tty_in = vim_strsave((char_u*)in_name);
6534 term->tl_job->jv_tty_out = vim_strsave((char_u*)out_name);
6535
6536 return OK;
6537
6538failed:
6539 if (hPipeIn != NULL)
6540 CloseHandle(hPipeIn);
6541 if (hPipeOut != NULL)
6542 CloseHandle(hPipeOut);
6543 return FAIL;
6544}
6545
6546/*
6547 * Free the terminal emulator part of "term".
6548 */
6549 static void
6550term_free_vterm(term_T *term)
6551{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006552 term_free_conpty(term);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006553 if (term->tl_winpty != NULL)
6554 winpty_free(term->tl_winpty);
6555 term->tl_winpty = NULL;
6556 if (term->tl_winpty_config != NULL)
6557 winpty_config_free(term->tl_winpty_config);
6558 term->tl_winpty_config = NULL;
6559 if (term->tl_vterm != NULL)
6560 vterm_free(term->tl_vterm);
6561 term->tl_vterm = NULL;
6562}
6563
6564/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006565 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006566 */
6567 static void
6568term_report_winsize(term_T *term, int rows, int cols)
6569{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006570 if (term->tl_conpty)
6571 conpty_term_report_winsize(term, rows, cols);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006572 if (term->tl_winpty)
6573 winpty_set_size(term->tl_winpty, cols, rows, NULL);
6574}
6575
6576 int
6577terminal_enabled(void)
6578{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006579 return dyn_winpty_init(FALSE) == OK || dyn_conpty_init(FALSE) == OK;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006580}
6581
6582# else
6583
6584/**************************************
6585 * 3. Unix-like implementation.
6586 */
6587
6588/*
6589 * Create a new terminal of "rows" by "cols" cells.
6590 * Start job for "cmd".
6591 * Store the pointers in "term".
Bram Moolenaar13568252018-03-16 20:46:58 +01006592 * When "argv" is not NULL then "argvar" is not used.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006593 * Return OK or FAIL.
6594 */
6595 static int
6596term_and_job_init(
6597 term_T *term,
6598 typval_T *argvar,
Bram Moolenaar13568252018-03-16 20:46:58 +01006599 char **argv,
Bram Moolenaarf25329c2018-05-06 21:49:32 +02006600 jobopt_T *opt,
6601 jobopt_T *orig_opt UNUSED)
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006602{
Bram Moolenaaraa5df7e2019-02-03 14:53:10 +01006603 term->tl_arg0_cmd = NULL;
6604
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006605 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6606 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006607
Bram Moolenaarf59c6e82018-04-10 15:59:11 +02006608#if defined(FEAT_GUI) || defined(FEAT_TERMGUICOLORS)
6609 if (opt->jo_set2 & JO2_ANSI_COLORS)
6610 set_vterm_palette(term->tl_vterm, opt->jo_ansi_colors);
6611 else
6612 init_vterm_ansi_colors(term->tl_vterm);
6613#endif
6614
Bram Moolenaar13568252018-03-16 20:46:58 +01006615 /* This may change a string in "argvar". */
Bram Moolenaar493359e2018-06-12 20:25:52 +02006616 term->tl_job = job_start(argvar, argv, opt, TRUE);
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006617 if (term->tl_job != NULL)
6618 ++term->tl_job->jv_refcount;
6619
6620 return term->tl_job != NULL
6621 && term->tl_job->jv_channel != NULL
6622 && term->tl_job->jv_status != JOB_FAILED ? OK : FAIL;
6623}
6624
6625 static int
6626create_pty_only(term_T *term, jobopt_T *opt)
6627{
Bram Moolenaarcd929f72018-12-24 21:38:45 +01006628 if (create_vterm(term, term->tl_rows, term->tl_cols) == FAIL)
6629 return FAIL;
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006630
6631 term->tl_job = job_alloc();
6632 if (term->tl_job == NULL)
6633 return FAIL;
6634 ++term->tl_job->jv_refcount;
6635
6636 /* behave like the job is already finished */
6637 term->tl_job->jv_status = JOB_FINISHED;
6638
6639 return mch_create_pty_channel(term->tl_job, opt);
6640}
6641
6642/*
6643 * Free the terminal emulator part of "term".
6644 */
6645 static void
6646term_free_vterm(term_T *term)
6647{
6648 if (term->tl_vterm != NULL)
6649 vterm_free(term->tl_vterm);
6650 term->tl_vterm = NULL;
6651}
6652
6653/*
Bram Moolenaara42d3632018-04-14 17:05:38 +02006654 * Report the size to the terminal.
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006655 */
6656 static void
6657term_report_winsize(term_T *term, int rows, int cols)
6658{
6659 /* Use an ioctl() to report the new window size to the job. */
6660 if (term->tl_job != NULL && term->tl_job->jv_channel != NULL)
6661 {
6662 int fd = -1;
6663 int part;
6664
6665 for (part = PART_OUT; part < PART_COUNT; ++part)
6666 {
6667 fd = term->tl_job->jv_channel->ch_part[part].ch_fd;
Bram Moolenaar1ecc5e42019-01-26 15:12:55 +01006668 if (mch_isatty(fd))
Bram Moolenaar2e6ab182017-09-20 10:03:07 +02006669 break;
6670 }
6671 if (part < PART_COUNT && mch_report_winsize(fd, rows, cols) == OK)
6672 mch_signal_job(term->tl_job, (char_u *)"winch");
6673 }
6674}
6675
6676# endif
6677
6678#endif /* FEAT_TERMINAL */